main bac21c6

traffic_failure_test.go RawHistoryFolder

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 container serves beside the candidate and must survive
54	// the failed switch: no stop, no removal, no claim release.
55	runtime.EXPECT().CreateContainer(mock.Anything, mock.Anything).Return(&domain.Container{ID: "c-new", Name: "web"}, nil).Once()
56	runtime.EXPECT().StartContainer(mock.Anything, "c-new").Return(nil).Once()
57	runtime.EXPECT().InspectContainer(mock.Anything, "c-new").Return(&domain.Container{ID: "c-new", Status: "running", StartedAt: executionStartedAt}, nil).Maybe()
58	runtime.EXPECT().GetContainerBackendBinds(mock.Anything, "c-new", mock.Anything).Return(
59		[]domain.ContainerBackendBind{{ContainerPort: 8080, HostPort: 18080, Protocol: domain.NetworkProtocolTCP}}, nil).Once()
60	state.EXPECT().RegisterBackendBinds(mock.Anything, mock.Anything).Return(nil).Once()
61	// The durable retirement marker is the cleanup owner of the superseded
62	// container; it is recorded before the switch and never cleared here.
63	state.EXPECT().SaveRecoveryInhibition(mock.Anything, mock.MatchedBy(func(i domain.AppRecoveryInhibition) bool {
64		return i.ContainerID == "c-old" && i.Reason == domain.AppInhibitRetirementPending
65	})).Return(nil).Once()
66
67	var saved []domain.AppOperation
68	state.EXPECT().SaveOperation(mock.Anything, mock.Anything).RunAndReturn(
69		func(_ context.Context, op domain.AppOperation) error {
70			saved = append(saved, op)
71			return nil
72		})
73
74	traffic := &recordingTraffic{failRebuild: true}
75	svc := deployment.NewService(deployment.Deps{
76		State: state, Runtime: runtime, Images: images, Secrets: secrets, Traffic: traffic,
77	}, zerowrap.Default()).WithProbeDeps(deployment.NewTestProbeDeps(runtime,
78		func(context.Context, string, string) (int, error) { return 200, nil },
79		func(context.Context, string) error { return nil },
80	))
81
82	result, err := svc.Deploy(ctx, deployment.DeployInput{App: "blog"})
83	require.Error(t, err)
84	require.NotNil(t, result)
85	assert.Equal(t, "failed", result.Services["web"].Result, "a service without routable publication is not deployed")
86
87	require.NotEmpty(t, saved)
88	last := saved[len(saved)-1]
89	assert.Equal(t, domain.AppOutcomeFailed, last.Outcome, "a failed publication must not be journaled as success")
90	step, ok := opStep(last, "service.web.replace")
91	require.True(t, ok)
92	assert.Equal(t, domain.AppStepFailed, step.State, "the service step must not claim success before traffic applied")
93	// ACTIVE already names c-new while the proxy graph was refused, so the
94	// superseded container may still be the one a stale graph reaches: it is
95	// kept running. Both generations stay traceable in the journal for
96	// reconciliation, and the leftover is reported.
97	runtime.AssertNotCalled(t, "StopContainer", mock.Anything, mock.Anything, mock.Anything)
98	runtime.AssertNotCalled(t, "RemoveContainer", mock.Anything, mock.Anything, mock.Anything)
99	state.AssertNotCalled(t, "ReleaseBackendBinds", mock.Anything, mock.Anything, mock.Anything)
100	assert.Equal(t, "c-old", step.Before)
101	assert.Equal(t, "c-new", step.After)
102	require.Len(t, result.Services["web"].CleanupWarnings, 1)
103	assert.Equal(t, "c-old", result.Services["web"].CleanupWarnings[0].Leftover)
104	require.Len(t, last.Warnings, 1)
105	assert.Equal(t, "c-old", last.Warnings[0].Leftover)
106}
107
108// TestStop_TrafficFailureRecordsTerminalFailure proves a rejected graph
109// apply after a stop is recorded as a failure, not as a successful stop
110// whose rerun would read success.
111func TestStop_TrafficFailureRecordsTerminalFailure(t *testing.T) {
112	ctx := context.Background()
113	state, runtime, _, _ := mockDeps(t)
114
115	active := domain.AppActive{App: "blog", Services: map[string]domain.AppEffectiveService{
116		"web": {Container: "c-1", EffectiveRevision: "rev-1", Spec: headlessSpec()},
117	}}
118	state.EXPECT().Recover(mock.Anything).Return(nil)
119	state.EXPECT().LoadActive(mock.Anything, "blog").Return(active, true, nil)
120	state.EXPECT().SaveIntent(mock.Anything, mock.Anything).Return(nil).Once()
121	state.EXPECT().LoadRecoveryInhibitions(mock.Anything, "blog").Return(nil, nil).Once()
122	runtime.EXPECT().StopContainer(mock.Anything, "c-1", mock.Anything).Return(nil).Once()
123	runtime.EXPECT().RemoveContainer(mock.Anything, "c-1", false).Return(nil).Once()
124	state.EXPECT().ReleaseBackendBinds(mock.Anything, "blog", "c-1").Return(nil).Once()
125	state.EXPECT().ClearRecoveryInhibition(mock.Anything, "blog", "web", "c-1").Return(nil).Once()
126
127	var saved []domain.AppOperation
128	state.EXPECT().SaveOperation(mock.Anything, mock.Anything).RunAndReturn(
129		func(_ context.Context, op domain.AppOperation) error {
130			saved = append(saved, op)
131			return nil
132		})
133
134	traffic := &recordingTraffic{failRebuild: true}
135	svc := deployment.NewService(deployment.Deps{
136		State: state, Runtime: runtime, Traffic: traffic,
137	}, zerowrap.Default())
138
139	result, err := svc.Stop(ctx, "blog", "")
140	require.Error(t, err)
141	require.NotNil(t, result)
142	assert.Equal(t, "deployed", result.Services["web"].Result, "the workload really was stopped")
143
144	require.NotEmpty(t, saved)
145	terminal := saved[len(saved)-1]
146	assert.Equal(t, domain.AppOutcomeFailed, terminal.Outcome, "the rejected graph apply must not replay as success")
147	step, ok := opStep(terminal, "traffic.publish")
148	require.True(t, ok, "the rejected publication is journaled")
149	assert.Equal(t, domain.AppStepFailed, step.State)
150}

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 7

Tags 106

↑↓ moveenter openesc close