forked from buildpacks/pack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage_cache_test.go
151 lines (129 loc) · 4.38 KB
/
image_cache_test.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package cache_test
import (
"context"
"math/rand"
"testing"
"time"
"github.com/buildpacks/imgutil/local"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/client"
"github.com/google/go-containerregistry/pkg/name"
"github.com/heroku/color"
"github.com/sclevine/spec"
"github.com/sclevine/spec/report"
"github.com/buildpacks/pack/internal/cache"
h "github.com/buildpacks/pack/testhelpers"
)
func TestImageCache(t *testing.T) {
h.RequireDocker(t)
color.Disable(true)
defer color.Disable(false)
rand.Seed(time.Now().UTC().UnixNano())
spec.Run(t, "ImageCache", testImageCache, spec.Parallel(), spec.Report(report.Terminal{}))
}
func testImageCache(t *testing.T, when spec.G, it spec.S) {
when("#NewImageCache", func() {
var dockerClient client.CommonAPIClient
it.Before(func() {
var err error
dockerClient, err = client.NewClientWithOpts(client.FromEnv, client.WithVersion("1.38"))
h.AssertNil(t, err)
})
it("reusing the same cache for the same repo name", func() {
ref, err := name.ParseReference("my/repo", name.WeakValidation)
h.AssertNil(t, err)
subject := cache.NewImageCache(ref, dockerClient)
expected := cache.NewImageCache(ref, dockerClient)
if subject.Name() != expected.Name() {
t.Fatalf("The same repo name should result in the same volume")
}
})
it("supplies different images for different tags", func() {
ref, err := name.ParseReference("my/repo:other-tag", name.WeakValidation)
h.AssertNil(t, err)
subject := cache.NewImageCache(ref, dockerClient)
ref, err = name.ParseReference("my/repo", name.WeakValidation)
h.AssertNil(t, err)
notExpected := cache.NewImageCache(ref, dockerClient)
if subject.Name() == notExpected.Name() {
t.Fatalf("Different image tags should result in different images")
}
})
it("supplies different images for different registries", func() {
ref, err := name.ParseReference("registry.com/my/repo:other-tag", name.WeakValidation)
h.AssertNil(t, err)
subject := cache.NewImageCache(ref, dockerClient)
ref, err = name.ParseReference("my/repo", name.WeakValidation)
h.AssertNil(t, err)
notExpected := cache.NewImageCache(ref, dockerClient)
if subject.Name() == notExpected.Name() {
t.Fatalf("Different image registries should result in different images")
}
})
it("resolves implied tag", func() {
ref, err := name.ParseReference("my/repo:latest", name.WeakValidation)
h.AssertNil(t, err)
subject := cache.NewImageCache(ref, dockerClient)
ref, err = name.ParseReference("my/repo", name.WeakValidation)
h.AssertNil(t, err)
expected := cache.NewImageCache(ref, dockerClient)
h.AssertEq(t, subject.Name(), expected.Name())
})
it("resolves implied registry", func() {
ref, err := name.ParseReference("index.docker.io/my/repo", name.WeakValidation)
h.AssertNil(t, err)
subject := cache.NewImageCache(ref, dockerClient)
ref, err = name.ParseReference("my/repo", name.WeakValidation)
h.AssertNil(t, err)
expected := cache.NewImageCache(ref, dockerClient)
if subject.Name() != expected.Name() {
t.Fatalf("The same repo name should result in the same image")
}
})
})
when("#Clear", func() {
var (
imageName string
dockerClient client.CommonAPIClient
subject *cache.ImageCache
ctx context.Context
)
it.Before(func() {
var err error
dockerClient, err = client.NewClientWithOpts(client.FromEnv, client.WithVersion("1.38"))
h.AssertNil(t, err)
ctx = context.TODO()
ref, err := name.ParseReference(h.RandString(10), name.WeakValidation)
h.AssertNil(t, err)
subject = cache.NewImageCache(ref, dockerClient)
h.AssertNil(t, err)
imageName = subject.Name()
})
when("there is a cache image", func() {
it.Before(func() {
img, err := local.NewImage(imageName, dockerClient)
h.AssertNil(t, err)
h.AssertNil(t, img.Save())
})
it("removes the image", func() {
err := subject.Clear(ctx)
h.AssertNil(t, err)
images, err := dockerClient.ImageList(context.TODO(), types.ImageListOptions{
Filters: filters.NewArgs(filters.KeyValuePair{
Key: "reference",
Value: imageName,
}),
})
h.AssertNil(t, err)
h.AssertEq(t, len(images), 0)
})
})
when("there is no cache image", func() {
it("does not fail", func() {
err := subject.Clear(ctx)
h.AssertNil(t, err)
})
})
})
}