v2.31.0-dev16 8d0a092

service_test.go RawHistoryFolder

1package volumes
2
3import (
4	"context"
5	"fmt"
6	"testing"
7
8	"github.com/stretchr/testify/assert"
9	"github.com/stretchr/testify/require"
10
11	outmocks "github.com/bnema/gordon/internal/boundaries/out/mocks"
12	"github.com/bnema/gordon/internal/domain"
13)
14
15func TestService_ListVolumes(t *testing.T) {
16	runtime := outmocks.NewMockContainerRuntime(t)
17
18	vols := []*domain.VolumeInfo{
19		{Name: "vol1", InUse: true, Containers: []string{"web"}},
20		{Name: "vol2", InUse: false},
21	}
22	runtime.EXPECT().ListVolumes(context.Background()).Return(vols, nil)
23
24	svc := NewService(runtime)
25	result, err := svc.ListVolumes(context.Background())
26	require.NoError(t, err)
27	assert.Len(t, result, 2)
28}
29
30func TestService_PruneVolumes_RemovesOrphaned(t *testing.T) {
31	runtime := outmocks.NewMockContainerRuntime(t)
32
33	vols := []*domain.VolumeInfo{
34		{Name: "vol-in-use", InUse: true, Containers: []string{"web"}, Labels: map[string]string{domain.LabelManaged: "true"}},
35		{Name: "vol-orphaned", InUse: false, Size: 1024, Labels: map[string]string{domain.LabelManaged: "true"}},
36	}
37	runtime.EXPECT().ListVolumes(context.Background()).Return(vols, nil)
38	runtime.EXPECT().RemoveVolume(context.Background(), "vol-orphaned", false).Return(nil)
39
40	svc := NewService(runtime)
41	report, removed, err := svc.PruneVolumes(context.Background(), false)
42	require.NoError(t, err)
43	assert.Equal(t, 1, report.VolumesRemoved)
44	assert.Equal(t, int64(1024), report.SpaceReclaimed)
45	assert.Len(t, removed, 1)
46	assert.Equal(t, "vol-orphaned", removed[0].Name)
47}
48
49func TestService_PruneVolumes_DryRunDoesNotRemove(t *testing.T) {
50	runtime := outmocks.NewMockContainerRuntime(t)
51
52	vols := []*domain.VolumeInfo{
53		{Name: "vol-orphaned", InUse: false, Size: 2048, Labels: map[string]string{domain.LabelManaged: "true"}},
54	}
55	runtime.EXPECT().ListVolumes(context.Background()).Return(vols, nil)
56
57	svc := NewService(runtime)
58	report, removed, err := svc.PruneVolumes(context.Background(), true)
59	require.NoError(t, err)
60	assert.Equal(t, 1, report.VolumesRemoved)
61	assert.Equal(t, int64(2048), report.SpaceReclaimed)
62	assert.Len(t, removed, 1)
63}
64
65func TestService_PruneVolumes_OnlyRemovesUnusedGordonManagedVolumes(t *testing.T) {
66	runtime := outmocks.NewMockContainerRuntime(t)
67
68	vols := []*domain.VolumeInfo{
69		{Name: "gordon-used", InUse: true, Size: 100, Labels: map[string]string{domain.LabelManaged: "true"}},
70		{Name: "gordon-unused", InUse: false, Size: 200, Labels: map[string]string{domain.LabelManaged: "true"}},
71		{Name: "other-unused", InUse: false, Size: 300},
72		{Name: "other-labelled-false", InUse: false, Size: 400, Labels: map[string]string{domain.LabelManaged: "false"}},
73		{Name: "other-used", InUse: true, Size: 500},
74	}
75	runtime.EXPECT().ListVolumes(context.Background()).Return(vols, nil)
76	runtime.EXPECT().RemoveVolume(context.Background(), "gordon-unused", false).Return(nil)
77
78	svc := NewService(runtime)
79	report, removed, err := svc.PruneVolumes(context.Background(), false)
80	require.NoError(t, err)
81	assert.Equal(t, 1, report.VolumesRemoved)
82	assert.Equal(t, int64(200), report.SpaceReclaimed)
83	require.Len(t, removed, 1)
84	assert.Equal(t, "gordon-unused", removed[0].Name)
85}
86
87func TestService_PruneVolumes_DryRunReportsOnlyUnusedGordonManagedVolumes(t *testing.T) {
88	runtime := outmocks.NewMockContainerRuntime(t)
89
90	vols := []*domain.VolumeInfo{
91		{Name: "gordon-used", InUse: true, Size: 100, Labels: map[string]string{domain.LabelManaged: "true"}},
92		{Name: "gordon-unused", InUse: false, Size: 200, Labels: map[string]string{domain.LabelManaged: "true"}},
93		{Name: "other-unused", InUse: false, Size: 300},
94	}
95	runtime.EXPECT().ListVolumes(context.Background()).Return(vols, nil)
96
97	svc := NewService(runtime)
98	report, removed, err := svc.PruneVolumes(context.Background(), true)
99	require.NoError(t, err)
100	assert.Equal(t, 1, report.VolumesRemoved)
101	assert.Equal(t, int64(200), report.SpaceReclaimed)
102	require.Len(t, removed, 1)
103	assert.Equal(t, "gordon-unused", removed[0].Name)
104}
105
106func TestService_PruneVolumes_AllInUse(t *testing.T) {
107	runtime := outmocks.NewMockContainerRuntime(t)
108
109	vols := []*domain.VolumeInfo{
110		{Name: "vol1", InUse: true, Containers: []string{"web"}},
111	}
112	runtime.EXPECT().ListVolumes(context.Background()).Return(vols, nil)
113
114	svc := NewService(runtime)
115	report, removed, err := svc.PruneVolumes(context.Background(), false)
116	require.NoError(t, err)
117	assert.Equal(t, 0, report.VolumesRemoved)
118	assert.Empty(t, removed)
119}
120
121func TestService_ListVolumes_PropagatesError(t *testing.T) {
122	runtime := outmocks.NewMockContainerRuntime(t)
123	runtime.EXPECT().ListVolumes(context.Background()).Return(nil, fmt.Errorf("connection refused"))
124
125	svc := NewService(runtime)
126	_, err := svc.ListVolumes(context.Background())
127	require.Error(t, err)
128	assert.Contains(t, err.Error(), "connection refused")
129}
130
131func TestService_PruneVolumes_SkipsFailedRemoval(t *testing.T) {
132	runtime := outmocks.NewMockContainerRuntime(t)
133
134	vols := []*domain.VolumeInfo{
135		{Name: "vol-fail", InUse: false, Size: 1024, Labels: map[string]string{domain.LabelManaged: "true"}},
136		{Name: "vol-ok", InUse: false, Size: 2048, Labels: map[string]string{domain.LabelManaged: "true"}},
137	}
138	runtime.EXPECT().ListVolumes(context.Background()).Return(vols, nil)
139	runtime.EXPECT().RemoveVolume(context.Background(), "vol-fail", false).Return(fmt.Errorf("volume in use"))
140	runtime.EXPECT().RemoveVolume(context.Background(), "vol-ok", false).Return(nil)
141
142	svc := NewService(runtime)
143	report, removed, err := svc.PruneVolumes(context.Background(), false)
144	require.NoError(t, err)
145	assert.Equal(t, 1, report.VolumesRemoved)
146	assert.Equal(t, int64(2048), report.SpaceReclaimed)
147	assert.Len(t, removed, 1)
148	assert.Equal(t, "vol-ok", removed[0].Name)
149}

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