mirror of
https://github.com/psviderski/uncloud.git
synced 2026-08-28 03:53:33 +00:00
feat(pre-deploy): implement pre-deploy hook operations for deployment
This commit is contained in:
@@ -9,6 +9,7 @@ import (
|
||||
"github.com/psviderski/uncloud/internal/machine/api/pb"
|
||||
"github.com/psviderski/uncloud/pkg/api"
|
||||
"github.com/psviderski/uncloud/pkg/client/deploy/operation"
|
||||
"github.com/psviderski/uncloud/pkg/client/deploy/scheduler"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
@@ -423,6 +424,246 @@ func TestReconcileGlobalContainer(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestPreDeployOperations(t *testing.T) {
|
||||
hook := &api.PreDeployHook{
|
||||
Command: []string{"db", "migrate"},
|
||||
}
|
||||
|
||||
strategy := &RollingStrategy{
|
||||
state: &scheduler.ClusterState{
|
||||
Machines: []*scheduler.Machine{
|
||||
{Info: &pb.MachineInfo{Id: "m-1", Name: "machine-1"}},
|
||||
{Info: &pb.MachineInfo{Id: "m-2", Name: "machine-2"}},
|
||||
{Info: &pb.MachineInfo{Id: "m-3", Name: "machine-3"}},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
runningHook1 := newServiceContainer("running-hook-1", container.State{Running: true, Status: "running"})
|
||||
runningHook2 := newServiceContainer("running-hook-2", container.State{Running: true, Status: "running"})
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
plan ServicePlan
|
||||
svc *api.Service
|
||||
expected []operation.Operation
|
||||
}{
|
||||
{
|
||||
name: "no pre-deploy hook in spec",
|
||||
plan: ServicePlan{
|
||||
ServiceID: "svc-1",
|
||||
ServiceName: "app",
|
||||
Spec: api.ServiceSpec{},
|
||||
SequenceOperation: operation.SequenceOperation{
|
||||
Operations: []operation.Operation{
|
||||
&operation.RunContainerOperation{MachineID: "m-1", MachineName: "machine-1"},
|
||||
},
|
||||
},
|
||||
},
|
||||
expected: nil,
|
||||
},
|
||||
{
|
||||
name: "plan has RunContainerOperation",
|
||||
plan: ServicePlan{
|
||||
ServiceID: "svc-1",
|
||||
ServiceName: "app",
|
||||
Spec: api.ServiceSpec{PreDeploy: hook},
|
||||
SequenceOperation: operation.SequenceOperation{
|
||||
Operations: []operation.Operation{
|
||||
&operation.RunContainerOperation{MachineID: "m-1", MachineName: "machine-1"},
|
||||
},
|
||||
},
|
||||
},
|
||||
expected: []operation.Operation{
|
||||
&operation.RunPreDeployOperation{
|
||||
ServiceID: "svc-1",
|
||||
MachineID: "m-1",
|
||||
MachineName: "machine-1",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "plan has ReplaceContainerOperation",
|
||||
plan: ServicePlan{
|
||||
ServiceID: "svc-1",
|
||||
ServiceName: "app",
|
||||
Spec: api.ServiceSpec{PreDeploy: hook},
|
||||
SequenceOperation: operation.SequenceOperation{
|
||||
Operations: []operation.Operation{
|
||||
&operation.ReplaceContainerOperation{MachineID: "m-2", MachineName: "machine-2"},
|
||||
},
|
||||
},
|
||||
},
|
||||
expected: []operation.Operation{
|
||||
&operation.RunPreDeployOperation{
|
||||
ServiceID: "svc-1",
|
||||
MachineID: "m-2",
|
||||
MachineName: "machine-2",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "plan has only RemoveContainerOperation",
|
||||
plan: ServicePlan{
|
||||
ServiceID: "svc-1",
|
||||
ServiceName: "app",
|
||||
Spec: api.ServiceSpec{PreDeploy: hook},
|
||||
SequenceOperation: operation.SequenceOperation{
|
||||
Operations: []operation.Operation{
|
||||
&operation.RemoveContainerOperation{MachineID: "m-1", MachineName: "machine-1"},
|
||||
},
|
||||
},
|
||||
},
|
||||
expected: nil,
|
||||
},
|
||||
{
|
||||
name: "plan has only StopContainerOperation",
|
||||
plan: ServicePlan{
|
||||
ServiceID: "svc-1",
|
||||
ServiceName: "app",
|
||||
Spec: api.ServiceSpec{PreDeploy: hook},
|
||||
SequenceOperation: operation.SequenceOperation{
|
||||
Operations: []operation.Operation{
|
||||
&operation.StopContainerOperation{MachineID: "m-1", MachineName: "machine-1"},
|
||||
},
|
||||
},
|
||||
},
|
||||
expected: nil,
|
||||
},
|
||||
{
|
||||
name: "empty plan with no operations",
|
||||
plan: ServicePlan{
|
||||
ServiceID: "svc-1",
|
||||
ServiceName: "app",
|
||||
Spec: api.ServiceSpec{PreDeploy: hook},
|
||||
},
|
||||
expected: nil,
|
||||
},
|
||||
{
|
||||
name: "stopped hook containers are collected for cleanup",
|
||||
plan: ServicePlan{
|
||||
ServiceID: "svc-1",
|
||||
ServiceName: "app",
|
||||
Spec: api.ServiceSpec{PreDeploy: hook},
|
||||
SequenceOperation: operation.SequenceOperation{
|
||||
Operations: []operation.Operation{
|
||||
&operation.RunContainerOperation{MachineID: "m-1", MachineName: "machine-1"},
|
||||
},
|
||||
},
|
||||
},
|
||||
svc: &api.Service{
|
||||
HookContainers: []api.MachineServiceContainer{
|
||||
{MachineID: "m-1", Container: newServiceContainer("old-hook-1", container.State{Status: "exited"})},
|
||||
},
|
||||
},
|
||||
expected: []operation.Operation{
|
||||
&operation.RunPreDeployOperation{
|
||||
ServiceID: "svc-1",
|
||||
MachineID: "m-1",
|
||||
MachineName: "machine-1",
|
||||
OldContainerIDs: []string{"old-hook-1"},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "running hook containers are stopped before run",
|
||||
plan: ServicePlan{
|
||||
ServiceID: "svc-1",
|
||||
ServiceName: "app",
|
||||
Spec: api.ServiceSpec{PreDeploy: hook},
|
||||
SequenceOperation: operation.SequenceOperation{
|
||||
Operations: []operation.Operation{
|
||||
&operation.RunContainerOperation{MachineID: "m-1", MachineName: "machine-1"},
|
||||
},
|
||||
},
|
||||
},
|
||||
svc: &api.Service{
|
||||
HookContainers: []api.MachineServiceContainer{
|
||||
{MachineID: "m-2", Container: runningHook1},
|
||||
},
|
||||
},
|
||||
expected: []operation.Operation{
|
||||
&operation.StopPreDeployOperation{
|
||||
MachineID: "m-2",
|
||||
MachineName: "machine-2",
|
||||
Container: runningHook1,
|
||||
},
|
||||
&operation.RunPreDeployOperation{
|
||||
ServiceID: "svc-1",
|
||||
MachineID: "m-1",
|
||||
MachineName: "machine-1",
|
||||
OldContainerIDs: []string{"running-hook-1"},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "mixed stopped and running hook containers and mixed container operations",
|
||||
plan: ServicePlan{
|
||||
ServiceID: "svc-1",
|
||||
ServiceName: "app",
|
||||
Spec: api.ServiceSpec{PreDeploy: hook},
|
||||
SequenceOperation: operation.SequenceOperation{
|
||||
Operations: []operation.Operation{
|
||||
&operation.StopContainerOperation{MachineID: "m-1", MachineName: "machine-1"},
|
||||
&operation.RemoveContainerOperation{MachineID: "m-1", MachineName: "machine-1"},
|
||||
&operation.ReplaceContainerOperation{MachineID: "m-2", MachineName: "machine-2"},
|
||||
&operation.RunContainerOperation{MachineID: "m-3", MachineName: "machine-3"},
|
||||
&operation.RemoveContainerOperation{MachineID: "m-3", MachineName: "machine-3"},
|
||||
},
|
||||
},
|
||||
},
|
||||
svc: &api.Service{
|
||||
HookContainers: []api.MachineServiceContainer{
|
||||
{MachineID: "m-1", Container: newServiceContainer("stopped-hook", container.State{Status: "exited"})},
|
||||
{MachineID: "m-1", Container: runningHook1},
|
||||
{MachineID: "m-3", Container: runningHook2},
|
||||
},
|
||||
},
|
||||
expected: []operation.Operation{
|
||||
&operation.StopPreDeployOperation{
|
||||
MachineID: "m-1",
|
||||
MachineName: "machine-1",
|
||||
Container: runningHook1,
|
||||
},
|
||||
&operation.StopPreDeployOperation{
|
||||
MachineID: "m-3",
|
||||
MachineName: "machine-3",
|
||||
Container: runningHook2,
|
||||
},
|
||||
&operation.RunPreDeployOperation{
|
||||
ServiceID: "svc-1",
|
||||
MachineID: "m-2",
|
||||
MachineName: "machine-2",
|
||||
OldContainerIDs: []string{"stopped-hook", "running-hook-1", "running-hook-2"},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
result := strategy.preDeployOperations(tt.svc, tt.plan)
|
||||
if tt.expected == nil {
|
||||
assert.Nil(t, result)
|
||||
return
|
||||
}
|
||||
assertOperationsEqual(t, tt.expected, result)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// newServiceContainer creates an api.ServiceContainer with the given ID and state.
|
||||
func newServiceContainer(id string, state container.State) api.ServiceContainer {
|
||||
return api.ServiceContainer{Container: api.Container{
|
||||
InspectResponse: container.InspectResponse{
|
||||
ContainerJSONBase: &container.ContainerJSONBase{
|
||||
ID: id,
|
||||
State: &state,
|
||||
},
|
||||
},
|
||||
}}
|
||||
}
|
||||
|
||||
// assertOperationsEqual compares expected and actual operations, ignoring the Spec field
|
||||
// which is passed separately to the function and not the focus of these tests.
|
||||
func assertOperationsEqual(t *testing.T, expected, actual []operation.Operation) {
|
||||
@@ -430,6 +671,7 @@ func assertOperationsEqual(t *testing.T, expected, actual []operation.Operation)
|
||||
opts := cmp.Options{
|
||||
cmpopts.IgnoreFields(operation.RunContainerOperation{}, "Spec"),
|
||||
cmpopts.IgnoreFields(operation.ReplaceContainerOperation{}, "Spec"),
|
||||
cmpopts.IgnoreFields(operation.RunPreDeployOperation{}, "Spec"),
|
||||
cmpopts.IgnoreUnexported(api.Container{}),
|
||||
}
|
||||
if diff := cmp.Diff(expected, actual, opts); diff != "" {
|
||||
|
||||
Reference in New Issue
Block a user