|
8 | 8 | cerrdefs "github.com/containerd/errdefs" |
9 | 9 | "github.com/moby/moby/api/types/image" |
10 | 10 | "github.com/moby/moby/api/types/system" |
| 11 | + ocispec "github.com/opencontainers/image-spec/specs-go/v1" |
11 | 12 | "gotest.tools/v3/assert" |
12 | 13 | is "gotest.tools/v3/assert/cmp" |
13 | 14 | ) |
@@ -154,3 +155,122 @@ func TestLegacyDiskUsage(t *testing.T) { |
154 | 155 | assert.Equal(t, du.Images.TotalSize, int64(4096)) |
155 | 156 | assert.Equal(t, len(du.Images.Items), 0) |
156 | 157 | } |
| 158 | + |
| 159 | +func TestImageDiskUsageFromLegacyAPI(t *testing.T) { |
| 160 | + const legacyVersion = "1.51" |
| 161 | + const expectedURL = "/system/df" |
| 162 | + |
| 163 | + tests := []struct { |
| 164 | + name string |
| 165 | + mockResponse *legacyDiskUsage |
| 166 | + expectedActiveCount int64 |
| 167 | + expectedTotalCount int64 |
| 168 | + expectedReclaimable int64 |
| 169 | + expectedTotalSize int64 |
| 170 | + }{ |
| 171 | + { |
| 172 | + name: "no images", |
| 173 | + mockResponse: &legacyDiskUsage{ |
| 174 | + LayersSize: 0, |
| 175 | + Images: []image.Summary{}, |
| 176 | + }, |
| 177 | + expectedActiveCount: 0, |
| 178 | + expectedTotalCount: 0, |
| 179 | + expectedReclaimable: 0, |
| 180 | + expectedTotalSize: 0, |
| 181 | + }, |
| 182 | + { |
| 183 | + name: "images with no containers", |
| 184 | + mockResponse: &legacyDiskUsage{ |
| 185 | + LayersSize: 8192, |
| 186 | + Images: []image.Summary{ |
| 187 | + {ID: "image1", Size: 4096, SharedSize: 0, Containers: 0}, |
| 188 | + {ID: "image2", Size: 4096, SharedSize: 0, Containers: 0}, |
| 189 | + }, |
| 190 | + }, |
| 191 | + expectedActiveCount: 0, |
| 192 | + expectedTotalCount: 2, |
| 193 | + expectedReclaimable: 8192, |
| 194 | + expectedTotalSize: 8192, |
| 195 | + }, |
| 196 | + { |
| 197 | + name: "images with containers", |
| 198 | + mockResponse: &legacyDiskUsage{ |
| 199 | + LayersSize: 12288, |
| 200 | + Images: []image.Summary{ |
| 201 | + {ID: "image1", Size: 4096, SharedSize: 0, Containers: 2}, |
| 202 | + {ID: "image2", Size: 2048, SharedSize: 0, Containers: 0}, |
| 203 | + {ID: "image3", Size: 6144, SharedSize: 0, Containers: 1}, |
| 204 | + }, |
| 205 | + }, |
| 206 | + expectedActiveCount: 2, |
| 207 | + expectedTotalCount: 3, |
| 208 | + expectedReclaimable: 2048, |
| 209 | + expectedTotalSize: 12288, |
| 210 | + }, |
| 211 | + { |
| 212 | + name: "images with shared size", |
| 213 | + mockResponse: &legacyDiskUsage{ |
| 214 | + LayersSize: 15360, |
| 215 | + Images: []image.Summary{ |
| 216 | + {ID: "image1", Size: 4096, SharedSize: 1024, Containers: 1}, |
| 217 | + {ID: "image2", Size: 8192, SharedSize: 2048, Containers: 0}, |
| 218 | + {ID: "image3", Size: 3072, SharedSize: 1024, Containers: 0}, |
| 219 | + }, |
| 220 | + }, |
| 221 | + expectedActiveCount: 1, |
| 222 | + expectedTotalCount: 3, |
| 223 | + expectedReclaimable: 8192, // (8192-2048) + (3072-1024) |
| 224 | + expectedTotalSize: 15360, |
| 225 | + }, |
| 226 | + { |
| 227 | + name: "multiplatform image with an image index", |
| 228 | + mockResponse: &legacyDiskUsage{ |
| 229 | + LayersSize: 4096, |
| 230 | + Images: []image.Summary{ |
| 231 | + {ID: "image1", Size: 4096, SharedSize: 0, Containers: 0, Descriptor: &ocispec.Descriptor{MediaType: ocispec.MediaTypeImageIndex, Size: 512}}, |
| 232 | + }, |
| 233 | + }, |
| 234 | + expectedActiveCount: 0, |
| 235 | + expectedTotalCount: 1, |
| 236 | + expectedReclaimable: 4096, // (4096 - 0) |
| 237 | + expectedTotalSize: 4096, |
| 238 | + }, |
| 239 | + { |
| 240 | + name: "multiplatform image with a Docker distribution manifest", |
| 241 | + mockResponse: &legacyDiskUsage{ |
| 242 | + LayersSize: 4096, |
| 243 | + Images: []image.Summary{ |
| 244 | + {ID: "image1", Size: 4096, SharedSize: 0, Containers: 0, Descriptor: &ocispec.Descriptor{MediaType: "application/vnd.docker.distribution.manifest.v2+json", Size: 427}}, |
| 245 | + }, |
| 246 | + }, |
| 247 | + expectedActiveCount: 0, |
| 248 | + expectedTotalCount: 1, |
| 249 | + expectedReclaimable: 4096, // (4096 - 0) |
| 250 | + expectedTotalSize: 4096, |
| 251 | + }, |
| 252 | + } |
| 253 | + |
| 254 | + for _, tt := range tests { |
| 255 | + t.Run(tt.name, func(t *testing.T) { |
| 256 | + client, err := New( |
| 257 | + WithAPIVersion(legacyVersion), |
| 258 | + WithMockClient(func(req *http.Request) (*http.Response, error) { |
| 259 | + if err := assertRequest(req, http.MethodGet, "/v"+legacyVersion+expectedURL); err != nil { |
| 260 | + return nil, err |
| 261 | + } |
| 262 | + |
| 263 | + return mockJSONResponse(http.StatusOK, nil, tt.mockResponse)(req) |
| 264 | + })) |
| 265 | + assert.NilError(t, err) |
| 266 | + |
| 267 | + du, err := client.DiskUsage(t.Context(), DiskUsageOptions{Images: true}) |
| 268 | + assert.NilError(t, err) |
| 269 | + assert.Equal(t, du.Images.ActiveCount, tt.expectedActiveCount, tt.name) |
| 270 | + assert.Equal(t, du.Images.TotalCount, tt.expectedTotalCount, tt.name) |
| 271 | + assert.Equal(t, du.Images.Reclaimable, tt.expectedReclaimable, tt.name) |
| 272 | + assert.Equal(t, du.Images.TotalSize, tt.expectedTotalSize, tt.name) |
| 273 | + assert.Equal(t, len(du.Images.Items), len(tt.mockResponse.Images), tt.name) |
| 274 | + }) |
| 275 | + } |
| 276 | +} |
0 commit comments