-
Notifications
You must be signed in to change notification settings - Fork 16
/
layer_reuse_test.go
196 lines (150 loc) · 5.45 KB
/
layer_reuse_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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package integration_test
import (
"os"
"path/filepath"
"testing"
"github.com/paketo-buildpacks/occam"
"github.com/sclevine/spec"
. "github.com/onsi/gomega"
. "github.com/paketo-buildpacks/occam/matchers"
)
func testLayerReuse(t *testing.T, context spec.G, it spec.S) {
var (
Expect = NewWithT(t).Expect
Eventually = NewWithT(t).Eventually
docker occam.Docker
pack occam.Pack
imageIDs map[string]struct{}
containerIDs map[string]struct{}
name string
source string
)
it.Before(func() {
var err error
name, err = occam.RandomName()
Expect(err).NotTo(HaveOccurred())
docker = occam.NewDocker()
pack = occam.NewPack()
imageIDs = map[string]struct{}{}
containerIDs = map[string]struct{}{}
})
it.After(func() {
for id := range containerIDs {
Expect(docker.Container.Remove.Execute(id)).To(Succeed())
}
for id := range imageIDs {
Expect(docker.Image.Remove.Execute(id)).To(Succeed())
}
Expect(docker.Volume.Remove.Execute(occam.CacheVolumeNames(name))).To(Succeed())
Expect(os.RemoveAll(source)).To(Succeed())
})
context("when an app is rebuilt and does not change", func() {
it("reuses a layer from a previous build", func() {
var (
err error
firstImage occam.Image
secondImage occam.Image
firstContainer occam.Container
secondContainer occam.Container
)
build := pack.WithNoColor().Build.
WithPullPolicy("never").
WithBuildpacks(
settings.Buildpacks.Cpython.Online,
settings.Buildpacks.BuildPlan.Online,
)
source, err = occam.Source(filepath.Join("testdata", "default_app"))
Expect(err).NotTo(HaveOccurred())
firstImage, _, err = build.
Execute(name, source)
Expect(err).NotTo(HaveOccurred())
imageIDs[firstImage.ID] = struct{}{}
Expect(firstImage.Buildpacks).To(HaveLen(2))
Expect(firstImage.Buildpacks[0].Key).To(Equal(buildpackInfo.Buildpack.ID))
Expect(firstImage.Buildpacks[0].Layers).To(HaveKey("cpython"))
firstContainer, err = docker.Container.Run.
WithCommand("python3 server.py").
WithEnv(map[string]string{"PORT": "8080"}).
WithPublish("8080").
Execute(firstImage.ID)
Expect(err).ToNot(HaveOccurred())
containerIDs[firstContainer.ID] = struct{}{}
Eventually(firstContainer).Should(BeAvailable())
// Second pack build
secondImage, _, err = build.
Execute(name, source)
Expect(err).NotTo(HaveOccurred())
imageIDs[secondImage.ID] = struct{}{}
Expect(secondImage.Buildpacks).To(HaveLen(2))
Expect(secondImage.Buildpacks[0].Key).To(Equal(buildpackInfo.Buildpack.ID))
Expect(secondImage.Buildpacks[0].Layers).To(HaveKey("cpython"))
secondContainer, err = docker.Container.Run.
WithCommand("python3 server.py").
WithEnv(map[string]string{"PORT": "8080"}).
WithPublish("8080").
Execute(secondImage.ID)
Expect(err).ToNot(HaveOccurred())
containerIDs[secondContainer.ID] = struct{}{}
Eventually(secondContainer).Should(BeAvailable())
Eventually(secondContainer).Should(Serve(ContainSubstring("hello world")).OnPort(8080))
Expect(secondImage.Buildpacks[0].Layers["cpython"].SHA).To(Equal(firstImage.Buildpacks[0].Layers["cpython"].SHA))
})
})
context("when an app is rebuilt and there is a change", func() {
it("rebuilds the layer", func() {
var (
err error
firstImage occam.Image
secondImage occam.Image
firstContainer occam.Container
secondContainer occam.Container
)
source, err = occam.Source(filepath.Join("testdata", "default_app"))
Expect(err).NotTo(HaveOccurred())
firstImage, _, err = pack.WithNoColor().Build.
WithPullPolicy("never").
WithBuildpacks(
settings.Buildpacks.Cpython.Online,
settings.Buildpacks.BuildPlan.Online,
).
WithEnv(map[string]string{"BP_CPYTHON_VERSION": "3.9.*"}).
Execute(name, source)
Expect(err).NotTo(HaveOccurred())
imageIDs[firstImage.ID] = struct{}{}
Expect(firstImage.Buildpacks).To(HaveLen(2))
Expect(firstImage.Buildpacks[0].Key).To(Equal(buildpackInfo.Buildpack.ID))
Expect(firstImage.Buildpacks[0].Layers).To(HaveKey("cpython"))
firstContainer, err = docker.Container.Run.
WithCommand("python3 server.py").
WithEnv(map[string]string{"PORT": "8080"}).
WithPublish("8080").
Execute(firstImage.ID)
Expect(err).ToNot(HaveOccurred())
containerIDs[firstContainer.ID] = struct{}{}
Eventually(firstContainer).Should(BeAvailable())
secondImage, _, err = pack.WithNoColor().Build.
WithPullPolicy("never").
WithBuildpacks(
settings.Buildpacks.Cpython.Online,
settings.Buildpacks.BuildPlan.Online,
).
WithEnv(map[string]string{"BP_CPYTHON_VERSION": "3.10.*"}).
Execute(name, source)
Expect(err).NotTo(HaveOccurred())
imageIDs[secondImage.ID] = struct{}{}
Expect(secondImage.Buildpacks).To(HaveLen(2))
Expect(secondImage.Buildpacks[0].Key).To(Equal(buildpackInfo.Buildpack.ID))
Expect(secondImage.Buildpacks[0].Layers).To(HaveKey("cpython"))
secondContainer, err = docker.Container.Run.
WithCommand("python3 server.py").
WithEnv(map[string]string{"PORT": "8080"}).
WithPublish("8080").
Execute(secondImage.ID)
Expect(err).ToNot(HaveOccurred())
containerIDs[secondContainer.ID] = struct{}{}
Eventually(secondContainer).Should(BeAvailable())
Eventually(secondContainer).Should(Serve(ContainSubstring("hello world")).OnPort(8080))
Expect(secondImage.Buildpacks[0].Layers["cpython"].SHA).NotTo(Equal(firstImage.Buildpacks[0].Layers["cpython"].SHA))
})
})
}