v2.30.0-pre5 7568a1c

runtime_image_cleanup_test.go RawHistoryFolder

1package docker
2
3import (
4	"context"
5	"math"
6	"net/http"
7	"net/http/httptest"
8	"strings"
9	"testing"
10	"time"
11
12	"github.com/docker/docker/api/types/filters"
13	"github.com/docker/docker/client"
14	"github.com/stretchr/testify/assert"
15	"github.com/stretchr/testify/require"
16)
17
18func TestRuntime_PruneImages_DanglingOnlyFilter(t *testing.T) {
19	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
20		assert.Equal(t, http.MethodPost, r.Method)
21		assert.Equal(t, "/v1.41/images/prune", r.URL.Path)
22
23		parsedFilters, err := filters.FromJSON(r.URL.Query().Get("filters"))
24		require.NoError(t, err)
25		assert.Equal(t, []string{"true"}, parsedFilters.Get("dangling"))
26
27		w.Header().Set("Content-Type", "application/json")
28		_, _ = w.Write([]byte(`{"ImagesDeleted":[{"Deleted":"sha256:abc"},{"Untagged":"example:old"}],"SpaceReclaimed":1234}`))
29	}))
30	defer server.Close()
31
32	runtime := newRuntimeForHTTPServer(t, server)
33	report, err := runtime.PruneImages(context.Background(), true)
34
35	require.NoError(t, err)
36	assert.Equal(t, []string{"sha256:abc", "example:old"}, report.DeletedIDs)
37	assert.EqualValues(t, 1234, report.SpaceReclaimed)
38}
39
40func TestRuntime_PruneImages_FullUnusedHasNoDanglingFilter(t *testing.T) {
41	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
42		assert.Equal(t, http.MethodPost, r.Method)
43		assert.Equal(t, "/v1.41/images/prune", r.URL.Path)
44
45		parsedFilters, err := filters.FromJSON(r.URL.Query().Get("filters"))
46		require.NoError(t, err)
47		assert.Empty(t, parsedFilters.Get("dangling"))
48
49		w.Header().Set("Content-Type", "application/json")
50		_, _ = w.Write([]byte(`{"ImagesDeleted":[{"Deleted":"sha256:def"}],"SpaceReclaimed":5678}`))
51	}))
52	defer server.Close()
53
54	runtime := newRuntimeForHTTPServer(t, server)
55	report, err := runtime.PruneImages(context.Background(), false)
56
57	require.NoError(t, err)
58	assert.Equal(t, []string{"sha256:def"}, report.DeletedIDs)
59	assert.EqualValues(t, 5678, report.SpaceReclaimed)
60}
61
62func TestRuntime_PruneImages_ReturnsErrorOnNon2xxResponse(t *testing.T) {
63	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
64		assert.Equal(t, http.MethodPost, r.Method)
65		assert.Equal(t, "/v1.41/images/prune", r.URL.Path)
66
67		w.WriteHeader(http.StatusInternalServerError)
68		_, _ = w.Write([]byte(`{"message":"internal error"}`))
69	}))
70	defer server.Close()
71
72	runtime := newRuntimeForHTTPServer(t, server)
73	_, err := runtime.PruneImages(context.Background(), true)
74
75	require.Error(t, err)
76}
77
78func TestRuntime_PruneImages_ReturnsErrorOnInvalidPayload(t *testing.T) {
79	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
80		assert.Equal(t, http.MethodPost, r.Method)
81		assert.Equal(t, "/v1.41/images/prune", r.URL.Path)
82
83		w.Header().Set("Content-Type", "application/json")
84		_, _ = w.Write([]byte(`not-json`))
85	}))
86	defer server.Close()
87
88	runtime := newRuntimeForHTTPServer(t, server)
89	_, err := runtime.PruneImages(context.Background(), true)
90
91	require.Error(t, err)
92}
93
94func TestRuntime_PruneImages_CapsSpaceReclaimedOverflow(t *testing.T) {
95	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
96		assert.Equal(t, http.MethodPost, r.Method)
97		assert.Equal(t, "/v1.41/images/prune", r.URL.Path)
98
99		w.Header().Set("Content-Type", "application/json")
100		_, _ = w.Write([]byte(`{"ImagesDeleted":[],"SpaceReclaimed":9223372036854775808}`))
101	}))
102	defer server.Close()
103
104	runtime := newRuntimeForHTTPServer(t, server)
105	report, err := runtime.PruneImages(context.Background(), true)
106
107	require.NoError(t, err)
108	assert.EqualValues(t, math.MaxInt64, report.SpaceReclaimed)
109}
110
111func TestRuntime_ListImagesDetailed_MapsImageSummary(t *testing.T) {
112	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
113		assert.Equal(t, http.MethodGet, r.Method)
114		assert.Equal(t, "/v1.41/images/json", r.URL.Path)
115		assert.Equal(t, "1", r.URL.Query().Get("all"))
116
117		w.Header().Set("Content-Type", "application/json")
118		_, _ = w.Write([]byte(`[{"Id":"sha256:img1","RepoTags":["alpine:latest"],"Size":321,"Created":1700000000}]`))
119	}))
120	defer server.Close()
121
122	runtime := newRuntimeForHTTPServer(t, server)
123	images, err := runtime.ListImagesDetailed(context.Background())
124
125	require.NoError(t, err)
126	require.Len(t, images, 1)
127	assert.Equal(t, "sha256:img1", images[0].ID)
128	assert.Equal(t, []string{"alpine:latest"}, images[0].RepoTags)
129	assert.EqualValues(t, 321, images[0].Size)
130	assert.Equal(t, time.Unix(1700000000, 0), images[0].Created)
131}
132
133func TestRuntime_ListImagesDetailed_ReturnsErrorOnNon2xxResponse(t *testing.T) {
134	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
135		assert.Equal(t, http.MethodGet, r.Method)
136		assert.Equal(t, "/v1.41/images/json", r.URL.Path)
137
138		w.WriteHeader(http.StatusBadGateway)
139		_, _ = w.Write([]byte(`{"message":"upstream error"}`))
140	}))
141	defer server.Close()
142
143	runtime := newRuntimeForHTTPServer(t, server)
144	_, err := runtime.ListImagesDetailed(context.Background())
145
146	require.Error(t, err)
147}
148
149func TestRuntime_ListImagesDetailed_ReturnsErrorOnInvalidPayload(t *testing.T) {
150	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
151		assert.Equal(t, http.MethodGet, r.Method)
152		assert.Equal(t, "/v1.41/images/json", r.URL.Path)
153
154		w.Header().Set("Content-Type", "application/json")
155		_, _ = w.Write([]byte(`{"not":`))
156	}))
157	defer server.Close()
158
159	runtime := newRuntimeForHTTPServer(t, server)
160	_, err := runtime.ListImagesDetailed(context.Background())
161
162	require.Error(t, err)
163}
164
165func TestRuntime_PruneImages_UsesMaxInt64Boundary(t *testing.T) {
166	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
167		assert.Equal(t, http.MethodPost, r.Method)
168		assert.Equal(t, "/v1.41/images/prune", r.URL.Path)
169
170		w.Header().Set("Content-Type", "application/json")
171		_, _ = w.Write([]byte(`{"ImagesDeleted":[],"SpaceReclaimed":9223372036854775807}`))
172	}))
173	defer server.Close()
174
175	runtime := newRuntimeForHTTPServer(t, server)
176	report, err := runtime.PruneImages(context.Background(), true)
177
178	require.NoError(t, err)
179	assert.EqualValues(t, math.MaxInt64, report.SpaceReclaimed)
180}
181
182func newRuntimeForHTTPServer(t *testing.T, server *httptest.Server) *Runtime {
183	t.Helper()
184
185	host := strings.TrimPrefix(server.URL, "http://")
186	cli, err := client.NewClientWithOpts(client.WithHost("tcp://"+host), client.WithVersion("1.41"), client.WithHTTPClient(server.Client()))
187	require.NoError(t, err)
188
189	return NewRuntimeWithClient(cli)
190}

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