1package deployment_test
2
3import (
4 "context"
5 "testing"
6 "time"
7
8 "github.com/bnema/zerowrap"
9 "github.com/stretchr/testify/assert"
10 "github.com/stretchr/testify/mock"
11 "github.com/stretchr/testify/require"
12
13 "github.com/bnema/gordon/internal/domain"
14 "github.com/bnema/gordon/internal/usecase/deployment"
15)
16
17// opStep returns one journaled step by id.
18func opStep(op domain.AppOperation, id string) (domain.AppOperationStep, bool) {
19 for _, step := range op.Steps {
20 if step.ID == id {
21 return step, true
22 }
23 }
24 return domain.AppOperationStep{}, false
25}
26
27// TestDeploy_TrafficFailureRecordsFailedServiceStep proves the ordering
28// invariant: a service step is never journaled as succeeded while the
29// routing graph refused its publication.
30func TestDeploy_TrafficFailureRecordsFailedServiceStep(t *testing.T) {
31 ctx := context.Background()
32 state, runtime, images, secrets := mockDeps(t)
33 rev := mockRevision()
34 rev.Spec.Services[0].StopGrace = time.Millisecond
35
36 oldActive := domain.AppActive{App: "blog", Services: map[string]domain.AppEffectiveService{
37 "web": {EffectiveRevision: "rev-0", Image: "img:0", Container: "c-old"},
38 }}
39
40 state.EXPECT().Recover(mock.Anything).Return(nil)
41 state.EXPECT().LoadDesired(mock.Anything, "blog").Return(rev, true, nil)
42 images.EXPECT().ResolveDigest(mock.Anything, rev.Spec.Services[0].Image).Return(
43 "sha256:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", nil).Once()
44 secrets.EXPECT().GetSecret(mock.Anything, "gordon/apps/app-blog/web/database-url").Return("x", nil)
45 runtime.EXPECT().InspectImageVolumes(mock.Anything, rev.Spec.Services[0].Image).Return(nil, nil).Once()
46 state.EXPECT().LoadCheckpoint(mock.Anything).Return(domain.AppStoreCheckpoint{}, nil).Once()
47 state.EXPECT().LoadOwnership(mock.Anything, "blog").Return(domain.AppOwnership{App: "blog", ID: "app-blog"}, nil)
48 state.EXPECT().LoadActive(mock.Anything, "blog").Return(oldActive, true, nil)
49 state.EXPECT().SaveOwnership(mock.Anything, mock.Anything).Return(nil)
50 state.EXPECT().SaveActive(mock.Anything, mock.Anything).Return(nil)
51
52 expectNetworkProvision(runtime, "app-blog", 1)
53 // The superseded generation is confirmed gone before the replacement is
54 // created: a deploy never overlaps two generations of one service.
55 runtime.EXPECT().StopContainer(mock.Anything, "c-old", mock.Anything).Return(nil).Once()
56 runtime.EXPECT().RemoveContainer(mock.Anything, "c-old", false).Return(nil).Once()
57 state.EXPECT().ReleaseBackendBinds(mock.Anything, "blog", "c-old").Return(nil).Once()
58 runtime.EXPECT().CreateContainer(mock.Anything, mock.Anything).Return(&domain.Container{ID: "c-new", Name: "web"}, nil).Once()
59 runtime.EXPECT().StartContainer(mock.Anything, "c-new").Return(nil).Once()
60 runtime.EXPECT().GetContainerBackendBinds(mock.Anything, "c-new", mock.Anything).Return(
61 []domain.ContainerBackendBind{{ContainerPort: 8080, HostPort: 18080, Protocol: domain.NetworkProtocolTCP}}, nil).Once()
62 state.EXPECT().RegisterBackendBinds(mock.Anything, mock.Anything).Return(nil).Once()
63 state.EXPECT().ClearRecoveryInhibition(mock.Anything, "blog", "web", "c-old").Return(nil).Once()
64
65 var saved []domain.AppOperation
66 state.EXPECT().SaveOperation(mock.Anything, mock.Anything).RunAndReturn(
67 func(_ context.Context, op domain.AppOperation) error {
68 saved = append(saved, op)
69 return nil
70 })
71
72 traffic := &recordingTraffic{failRebuild: true}
73 svc := deployment.NewService(deployment.Deps{
74 State: state, Runtime: runtime, Images: images, Secrets: secrets, Traffic: traffic,
75 }, zerowrap.Default()).WithProbeDeps(deployment.NewTestProbeDeps(runtime,
76 func(context.Context, string, string) (int, error) { return 200, nil },
77 func(context.Context, string) error { return nil },
78 ))
79
80 result, err := svc.Deploy(ctx, deployment.DeployInput{App: "blog"})
81 require.Error(t, err)
82 require.NotNil(t, result)
83 assert.Equal(t, "failed", result.Services["web"].Result, "a service without routable publication is not deployed")
84
85 require.NotEmpty(t, saved)
86 last := saved[len(saved)-1]
87 assert.Equal(t, domain.AppOutcomeFailed, last.Outcome, "a failed publication must not be journaled as success")
88 step, ok := opStep(last, "service.web.replace")
89 require.True(t, ok)
90 assert.Equal(t, domain.AppStepFailed, step.State, "the service step must not claim success before traffic applied")
91 // The superseded container is gone: a rejected graph is reported as a
92 // failure of the new generation, never as a return to the old one.
93 runtime.AssertCalled(t, "RemoveContainer", mock.Anything, "c-old", false)
94 runtime.AssertNotCalled(t, "RemoveContainer", mock.Anything, "c-new", mock.Anything)
95}
96
97// TestStop_TrafficFailureRecordsTerminalFailure proves a rejected graph
98// apply after a stop is recorded as a failure, not as a successful stop
99// whose rerun would read success.
100func TestStop_TrafficFailureRecordsTerminalFailure(t *testing.T) {
101 ctx := context.Background()
102 state, runtime, _, _ := mockDeps(t)
103
104 active := domain.AppActive{App: "blog", Services: map[string]domain.AppEffectiveService{
105 "web": {Container: "c-1", EffectiveRevision: "rev-1", Spec: headlessSpec()},
106 }}
107 state.EXPECT().Recover(mock.Anything).Return(nil)
108 state.EXPECT().LoadActive(mock.Anything, "blog").Return(active, true, nil)
109 state.EXPECT().SaveIntent(mock.Anything, mock.Anything).Return(nil).Once()
110 runtime.EXPECT().StopContainer(mock.Anything, "c-1", mock.Anything).Return(nil).Once()
111 runtime.EXPECT().RemoveContainer(mock.Anything, "c-1", false).Return(nil).Once()
112 state.EXPECT().ReleaseBackendBinds(mock.Anything, "blog", "c-1").Return(nil).Once()
113 state.EXPECT().ClearRecoveryInhibition(mock.Anything, "blog", "web", "c-1").Return(nil).Once()
114
115 var saved []domain.AppOperation
116 state.EXPECT().SaveOperation(mock.Anything, mock.Anything).RunAndReturn(
117 func(_ context.Context, op domain.AppOperation) error {
118 saved = append(saved, op)
119 return nil
120 })
121
122 traffic := &recordingTraffic{failRebuild: true}
123 svc := deployment.NewService(deployment.Deps{
124 State: state, Runtime: runtime, Traffic: traffic,
125 }, zerowrap.Default())
126
127 result, err := svc.Stop(ctx, "blog", "")
128 require.Error(t, err)
129 require.NotNil(t, result)
130 assert.Equal(t, "deployed", result.Services["web"].Result, "the workload really was stopped")
131
132 require.NotEmpty(t, saved)
133 terminal := saved[len(saved)-1]
134 assert.Equal(t, domain.AppOutcomeFailed, terminal.Outcome, "the rejected graph apply must not replay as success")
135 step, ok := opStep(terminal, "traffic.publish")
136 require.True(t, ok, "the rejected publication is journaled")
137 assert.Equal(t, domain.AppStepFailed, step.State)
138}