main bac21c6

device_policy_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	outmocks "github.com/bnema/gordon/internal/boundaries/out/mocks"
13	"github.com/bnema/gordon/internal/domain"
14	"github.com/bnema/gordon/internal/usecase/apps"
15)
16
17func devicePolicy(apps, services []string) domain.AppDevicePolicy {
18	return domain.AppDevicePolicy{
19		Name:            "test_gpu",
20		CDI:             []string{"example.com/gpu=GPU-test-uuid"},
21		AllowedApps:     apps,
22		AllowedServices: services,
23	}
24}
25
26func TestApply_AcceptsDevicesUnderAdministrativePolicy(t *testing.T) {
27	store := outmocks.NewMockAppState(t)
28	spec := testSpec("blog")
29	spec.Services[0].Devices = []string{"test_gpu"}
30	applySuccess(store, spec)
31
32	svc := apps.NewService(store, zerowrap.Default()).
33		WithDevicePolicies(map[string]domain.AppDevicePolicy{
34			"test_gpu": devicePolicy([]string{"blog"}, []string{"web"}),
35		})
36	_, _, err := svc.Apply(context.Background(), spec, []byte("manifest"), false)
37	require.NoError(t, err)
38}
39
40func TestApply_RejectsDeviceWithoutPolicyBeforeStoreAccess(t *testing.T) {
41	store := outmocks.NewMockAppState(t)
42	spec := testSpec("blog")
43	spec.Services[0].Devices = []string{"test_gpu"}
44	svc := apps.NewService(store, zerowrap.Default())
45
46	_, _, err := svc.Apply(context.Background(), spec, []byte("manifest"), false)
47
48	require.ErrorIs(t, err, domain.ErrDevicePolicy)
49	assert.Contains(t, err.Error(), "blog")
50	assert.Contains(t, err.Error(), "web")
51	assert.Contains(t, err.Error(), "test_gpu")
52}
53
54func TestApply_RejectsDeviceOutsideAllowlists(t *testing.T) {
55	spec := testSpec("blog")
56	spec.Services[0].Devices = []string{"test_gpu"}
57
58	t.Run("app not allowed", func(t *testing.T) {
59		store := outmocks.NewMockAppState(t)
60		svc := apps.NewService(store, zerowrap.Default()).
61			WithDevicePolicies(map[string]domain.AppDevicePolicy{
62				"test_gpu": devicePolicy([]string{"other"}, []string{"web"}),
63			})
64		_, _, err := svc.Apply(context.Background(), spec, []byte("manifest"), false)
65		require.ErrorIs(t, err, domain.ErrDevicePolicy)
66		assert.NotContains(t, err.Error(), "GPU-test-uuid")
67	})
68
69	t.Run("service not allowed", func(t *testing.T) {
70		store := outmocks.NewMockAppState(t)
71		svc := apps.NewService(store, zerowrap.Default()).
72			WithDevicePolicies(map[string]domain.AppDevicePolicy{
73				"test_gpu": devicePolicy([]string{"blog"}, []string{"other"}),
74			})
75		_, _, err := svc.Apply(context.Background(), spec, []byte("manifest"), false)
76		require.ErrorIs(t, err, domain.ErrDevicePolicy)
77		assert.NotContains(t, err.Error(), "GPU-test-uuid")
78	})
79}
80
81// TestApply_DevicePoliciesReplaceOnReload proves a reload-style replacement
82// takes effect immediately: the second apply is refused before any store
83// access.
84func TestApply_DevicePoliciesReplaceOnReload(t *testing.T) {
85	spec := testSpec("blog")
86	spec.Services[0].Devices = []string{"test_gpu"}
87
88	store := outmocks.NewMockAppState(t)
89	applySuccess(store, spec)
90	svc := apps.NewService(store, zerowrap.Default()).
91		WithDevicePolicies(map[string]domain.AppDevicePolicy{
92			"test_gpu": devicePolicy([]string{"blog"}, []string{"web"}),
93		})
94	_, _, err := svc.Apply(context.Background(), spec, []byte("manifest"), false)
95	require.NoError(t, err)
96
97	svc.SetDevicePolicies(map[string]domain.AppDevicePolicy{
98		"test_gpu": devicePolicy([]string{"other"}, []string{"web"}),
99	})
100	_, _, err = svc.Apply(context.Background(), spec, []byte("manifest"), false)
101	require.ErrorIs(t, err, domain.ErrDevicePolicy)
102}
103
104func TestApply_DeviceDryRunRefusedWithoutMutation(t *testing.T) {
105	store := outmocks.NewMockAppState(t)
106	spec := testSpec("blog")
107	spec.Services[0].Devices = []string{"test_gpu"}
108	svc := apps.NewService(store, zerowrap.Default())
109
110	_, _, err := svc.Apply(context.Background(), spec, []byte("manifest"), true)
111	require.ErrorIs(t, err, domain.ErrDevicePolicy)
112	store.AssertNotCalled(t, "AcceptApply", mock.Anything, mock.Anything)
113}
114
115func TestApply_RejectsDuplicateCDIIDsAcrossDevices(t *testing.T) {
116	shared := "example.com/gpu=GPU-shared"
117	store := outmocks.NewMockAppState(t)
118	spec := testSpec("blog")
119	spec.Services[0].Devices = []string{"gpu-a", "gpu-b"}
120	svc := apps.NewService(store, zerowrap.Default()).
121		WithDevicePolicies(map[string]domain.AppDevicePolicy{
122			"gpu-a": {Name: "gpu-a", CDI: []string{shared}, AllowedApps: []string{"blog"}, AllowedServices: []string{"web"}},
123			"gpu-b": {Name: "gpu-b", CDI: []string{shared}, AllowedApps: []string{"blog"}, AllowedServices: []string{"web"}},
124		})
125
126	_, _, err := svc.Apply(context.Background(), spec, []byte("manifest"), false)
127
128	require.ErrorIs(t, err, domain.ErrDevicePolicy)
129	assert.NotContains(t, err.Error(), shared, "apply errors must never leak CDI IDs")
130}

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