forked from buildpacks/pack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvolume_cache_test.go
155 lines (133 loc) · 4.78 KB
/
volume_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
152
153
154
155
package cache_test
import (
"context"
"math/rand"
"strings"
"testing"
"time"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/api/types/volume"
"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 TestVolumeCache(t *testing.T) {
h.RequireDocker(t)
color.Disable(true)
defer color.Disable(false)
rand.Seed(time.Now().UTC().UnixNano())
spec.Run(t, "VolumeCache", testCache, spec.Parallel(), spec.Report(report.Terminal{}))
}
func testCache(t *testing.T, when spec.G, it spec.S) {
when("#NewVolumeCache", 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("adds suffix to calculated name", func() {
ref, err := name.ParseReference("my/repo", name.WeakValidation)
h.AssertNil(t, err)
subject := cache.NewVolumeCache(ref, "some-suffix", dockerClient)
if !strings.HasSuffix(subject.Name(), ".some-suffix") {
t.Fatalf("Calculated volume name '%s' should end with '.some-suffix'", subject.Name())
}
})
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.NewVolumeCache(ref, "some-suffix", dockerClient)
expected := cache.NewVolumeCache(ref, "some-suffix", dockerClient)
if subject.Name() != expected.Name() {
t.Fatalf("The same repo name should result in the same volume")
}
})
it("supplies different volumes for different tags", func() {
ref, err := name.ParseReference("my/repo:other-tag", name.WeakValidation)
h.AssertNil(t, err)
subject := cache.NewVolumeCache(ref, "some-suffix", dockerClient)
ref, err = name.ParseReference("my/repo", name.WeakValidation)
h.AssertNil(t, err)
notExpected := cache.NewVolumeCache(ref, "some-suffix", dockerClient)
if subject.Name() == notExpected.Name() {
t.Fatalf("Different image tags should result in different volumes")
}
})
it("supplies different volumes for different registries", func() {
ref, err := name.ParseReference("registry.com/my/repo:other-tag", name.WeakValidation)
h.AssertNil(t, err)
subject := cache.NewVolumeCache(ref, "some-suffix", dockerClient)
ref, err = name.ParseReference("my/repo", name.WeakValidation)
h.AssertNil(t, err)
notExpected := cache.NewVolumeCache(ref, "some-suffix", dockerClient)
if subject.Name() == notExpected.Name() {
t.Fatalf("Different image registries should result in different volumes")
}
})
it("resolves implied tag", func() {
ref, err := name.ParseReference("my/repo:latest", name.WeakValidation)
h.AssertNil(t, err)
subject := cache.NewVolumeCache(ref, "some-suffix", dockerClient)
ref, err = name.ParseReference("my/repo", name.WeakValidation)
h.AssertNil(t, err)
expected := cache.NewVolumeCache(ref, "some-suffix", 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.NewVolumeCache(ref, "some-suffix", dockerClient)
ref, err = name.ParseReference("my/repo", name.WeakValidation)
h.AssertNil(t, err)
expected := cache.NewVolumeCache(ref, "some-suffix", dockerClient)
h.AssertEq(t, subject.Name(), expected.Name())
})
})
when("#Clear", func() {
var (
volumeName string
dockerClient client.CommonAPIClient
subject *cache.VolumeCache
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.NewVolumeCache(ref, "some-suffix", dockerClient)
h.AssertNil(t, err)
volumeName = subject.Name()
})
when("there is a cache volume", func() {
it.Before(func() {
dockerClient.VolumeCreate(context.TODO(), volume.VolumeCreateBody{
Name: volumeName,
})
})
it("removes the volume", func() {
err := subject.Clear(ctx)
h.AssertNil(t, err)
volumes, err := dockerClient.VolumeList(context.TODO(), filters.NewArgs(filters.KeyValuePair{
Key: "name",
Value: volumeName,
}))
h.AssertNil(t, err)
h.AssertEq(t, len(volumes.Volumes), 0)
})
})
when("there is no cache volume", func() {
it("does not fail", func() {
err := subject.Clear(ctx)
h.AssertNil(t, err)
})
})
})
}