-
Notifications
You must be signed in to change notification settings - Fork 5
/
imagetest.go
115 lines (95 loc) · 3.18 KB
/
imagetest.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package main
import (
"context"
"github.com/containerd/containerd"
"github.com/containerd/containerd/errdefs"
"github.com/containerd/containerd/images"
"github.com/containerd/containerd/namespaces"
"github.com/containerd/containerd/platforms"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
log "github.com/sirupsen/logrus"
)
const (
defaultContainerdPath = "/run/containerd/containerd.sock"
defaultImage = "docker.io/library/busybox:latest"
)
func main() {
log.SetLevel(log.InfoLevel)
imageName := defaultImage
ctx := namespaces.WithNamespace(context.Background(), "examplectr")
// connect to containerd daemon over UNIX socket
client, err := containerd.New(defaultContainerdPath)
if err != nil {
log.Errorf("error connecting to containerd daemon: %v", err)
return
}
defer client.Close()
// Cleanup
err = client.ImageService().Delete(ctx, imageName, images.SynchronousDelete())
if err != nil && !errdefs.IsNotFound(err) {
log.Fatal(err)
}
testPlatform := platforms.Only(ocispec.Platform{
OS: "linux",
Architecture: "amd64",
})
// Pull single platform, do not unpack
image, err := client.Pull(ctx, imageName, containerd.WithPlatformMatcher(testPlatform))
if err != nil {
log.Fatal(err)
}
s1, err := image.Usage(ctx, containerd.WithUsageManifestLimit(1))
if err != nil {
log.Fatal(err)
}
log.Infof("Pulled %s: size %d", imageName, s1)
// Pin image name to specific version for future fetches
imageName = imageName + "@" + image.Target().Digest.String()
defer client.ImageService().Delete(ctx, imageName, images.SynchronousDelete())
// Fetch single platforms, but all manifests pulled
if _, err := client.Fetch(ctx, imageName, containerd.WithPlatformMatcher(testPlatform), containerd.WithAllMetadata()); err != nil {
log.Fatal(err)
}
if s, err := image.Usage(ctx, containerd.WithUsageManifestLimit(1)); err != nil {
log.Fatal(err)
} else if s != s1 {
log.Fatalf("unexpected usage %d, expected %d", s, s1)
}
s2, err := image.Usage(ctx, containerd.WithUsageManifestLimit(0))
if err != nil {
log.Fatal(err)
}
log.Infof("Total size with all manifests: %d", s2)
if s2 <= s1 {
log.Fatalf("Expected larger usage counting all manifests: %d <= %d", s2, s1)
}
s3, err := image.Usage(ctx, containerd.WithUsageManifestLimit(0), containerd.WithManifestUsage())
if err != nil {
log.Fatal(err)
}
if s3 < s2 {
log.Fatalf("Expected larger usage counting all manifest reported sizes: %d <= %d", s3, s2)
}
log.Infof("All reported sizes with all manifests: %d", s3)
// Fetch everything
if _, err = client.Fetch(ctx, imageName); err != nil {
log.Fatal(err)
}
log.Infof("Fetched all content for %s", imageName)
s, err := image.Usage(ctx)
if err != nil {
log.Fatal(err)
} else if s != s3 {
log.Fatalf("Expected actual usage to equal manifest reported usage of %d: got %d", s3, s)
}
log.Infof("post-fetch sizes with all manifests: %d", s)
err = image.Unpack(ctx, containerd.DefaultSnapshotter)
if err != nil {
log.Fatal(err)
}
if s, err := image.Usage(ctx, containerd.WithSnapshotUsage()); err != nil {
log.Fatal(err)
} else if s <= s3 {
log.Fatalf("Expected actual usage with snapshots to be greater: %d <= %d", s, s3)
}
}