v3.3.2 9697f9d

service_scope_test.go RawHistoryFolder

1package apps_test
2
3import (
4	"context"
5	"testing"
6
7	"github.com/bnema/zerowrap"
8	"github.com/stretchr/testify/assert"
9	"github.com/stretchr/testify/mock"
10	"github.com/stretchr/testify/require"
11
12	"github.com/bnema/gordon/internal/domain"
13	"github.com/bnema/gordon/internal/usecase/apps"
14	"github.com/bnema/gordon/internal/usecase/deployment"
15)
16
17func scopeDesired(names ...string) domain.AppDesiredRevision {
18	rev := domain.AppDesiredRevision{Revision: "rev-1", App: "blog"}
19	for _, name := range names {
20		rev.Spec.Services = append(rev.Spec.Services, domain.AppService{Name: name})
21	}
22	return rev
23}
24
25func scopeRevision(revision string, names ...string) domain.AppDesiredRevision {
26	rev := scopeDesired(names...)
27	rev.Revision = revision
28	return rev
29}
30
31func scopeActive(names ...string) domain.AppActive {
32	active := domain.AppActive{App: "blog", Services: map[string]domain.AppEffectiveService{}}
33	for _, name := range names {
34		active.Services[name] = domain.AppEffectiveService{}
35	}
36	return active
37}
38
39func TestAppServiceImpl_Deploy_MultiServiceRequiresScope(t *testing.T) {
40	store := newMockAppState(t)
41	deploy := newMockDeployEngine(t)
42	svc := apps.NewAppServiceImpl(store, deploy, newMockSecretWriter(t), zerowrap.Default())
43	t.Cleanup(func() { require.NoError(t, svc.Shutdown(context.Background())) })
44
45	// A first deploy has no ACTIVE yet: desired services still count.
46	store.EXPECT().LoadDesired(mock.Anything, "blog").Return(scopeDesired("web", "db"), true, nil)
47	store.EXPECT().LoadActive(mock.Anything, "blog").Return(domain.AppActive{}, false, nil)
48	deploy.startDeployFn = func(context.Context, deployment.DeployInput) (*deployment.StartDeployResult, error) {
49		t.Fatal("no claim may be made without a service scope")
50		return nil, nil
51	}
52
53	op, err := svc.Deploy(context.Background(), "blog", "", "", false, "key-1")
54	require.ErrorIs(t, err, domain.ErrAppServiceScope)
55	assert.Nil(t, op)
56	assert.Contains(t, err.Error(), "app blog has several services (db, web): pass --service NAME or --all")
57}
58
59func TestAppServiceImpl_Deploy_OldRevisionWithMultipleServicesRequiresScope(t *testing.T) {
60	store := newMockAppState(t)
61	deploy := newMockDeployEngine(t)
62	svc := apps.NewAppServiceImpl(store, deploy, newMockSecretWriter(t), zerowrap.Default())
63	t.Cleanup(func() { require.NoError(t, svc.Shutdown(context.Background())) })
64
65	// The head and ACTIVE know only one service, but the revision the
66	// deploy would activate declares two: the scope check must count the
67	// selected revision, not the head.
68	store.EXPECT().LoadRevision(mock.Anything, "blog", "rev-2").Return(scopeRevision("rev-2", "web", "db"), nil)
69	store.EXPECT().LoadActive(mock.Anything, "blog").Return(scopeActive("web"), true, nil)
70	deploy.startDeployFn = func(context.Context, deployment.DeployInput) (*deployment.StartDeployResult, error) {
71		t.Fatal("no claim may be made without a service scope")
72		return nil, nil
73	}
74
75	op, err := svc.Deploy(context.Background(), "blog", "rev-2", "", false, "key-1")
76	require.ErrorIs(t, err, domain.ErrAppServiceScope)
77	assert.Nil(t, op)
78	assert.Contains(t, err.Error(), "app blog has several services (db, web): pass --service NAME or --all")
79}
80
81func TestAppServiceImpl_Deploy_SingleServiceNeedsNoScope(t *testing.T) {
82	store := newMockAppState(t)
83	deploy := newMockDeployEngine(t)
84	svc := apps.NewAppServiceImpl(store, deploy, newMockSecretWriter(t), zerowrap.Default())
85	t.Cleanup(func() { require.NoError(t, svc.Shutdown(context.Background())) })
86
87	store.EXPECT().LoadDesired(mock.Anything, "blog").Return(scopeDesired("web"), true, nil)
88	store.EXPECT().LoadActive(mock.Anything, "blog").Return(scopeActive("web"), true, nil)
89	started := make(chan struct{}, 1)
90	deploy.startDeployFn = func(context.Context, deployment.DeployInput) (*deployment.StartDeployResult, error) {
91		started <- struct{}{}
92		return nil, domain.ErrAppStateConflict
93	}
94
95	_, err := svc.Deploy(context.Background(), "blog", "", "", false, "key-1")
96	require.ErrorIs(t, err, domain.ErrAppStateConflict)
97	require.Len(t, started, 1, "a single-service app deploys without --service or --all")
98}
99
100func TestAppServiceImpl_Deploy_AllSkipsScopeCheck(t *testing.T) {
101	store := newMockAppState(t)
102	deploy := newMockDeployEngine(t)
103	svc := apps.NewAppServiceImpl(store, deploy, newMockSecretWriter(t), zerowrap.Default())
104	t.Cleanup(func() { require.NoError(t, svc.Shutdown(context.Background())) })
105
106	var got deployment.DeployInput
107	deploy.startDeployFn = func(_ context.Context, input deployment.DeployInput) (*deployment.StartDeployResult, error) {
108		got = input
109		return nil, domain.ErrAppStateConflict
110	}
111
112	_, err := svc.Deploy(context.Background(), "blog", "", "", true, "key-1")
113	require.ErrorIs(t, err, domain.ErrAppStateConflict)
114	assert.Equal(t, deployment.DeployInput{App: "blog", Op: "key-1"}, got, "--all deploys every service")
115}
116
117func TestAppServiceImpl_Deploy_AllAndServiceAreExclusive(t *testing.T) {
118	svc := apps.NewAppServiceImpl(newMockAppState(t), newMockDeployEngine(t), newMockSecretWriter(t), zerowrap.Default())
119	t.Cleanup(func() { require.NoError(t, svc.Shutdown(context.Background())) })
120
121	_, err := svc.Deploy(context.Background(), "blog", "", "web", true, "key-1")
122	require.ErrorIs(t, err, domain.ErrAppServiceScope)
123}
124
125func TestAppServiceImpl_Restart_MultiServiceRequiresScope(t *testing.T) {
126	store := newMockAppState(t)
127	svc := apps.NewAppServiceImpl(store, newMockDeployEngine(t), newMockSecretWriter(t), zerowrap.Default())
128	t.Cleanup(func() { require.NoError(t, svc.Shutdown(context.Background())) })
129
130	store.EXPECT().LoadActive(mock.Anything, "blog").Return(scopeActive("web", "db"), true, nil)
131
132	// The engine mock has no Restart expectation: reaching it fails the test.
133	op, err := svc.Restart(context.Background(), "blog", "", false, "key-1")
134	require.ErrorIs(t, err, domain.ErrAppServiceScope)
135	assert.Nil(t, op)
136	assert.Contains(t, err.Error(), "(db, web)")
137}
138
139func TestAppServiceImpl_Restart_AllAndServiceAreExclusive(t *testing.T) {
140	svc := apps.NewAppServiceImpl(newMockAppState(t), newMockDeployEngine(t), newMockSecretWriter(t), zerowrap.Default())
141	t.Cleanup(func() { require.NoError(t, svc.Shutdown(context.Background())) })
142
143	_, err := svc.Restart(context.Background(), "blog", "web", true, "key-1")
144	require.ErrorIs(t, err, domain.ErrAppServiceScope)
145}

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