Skip to content

Commit 8040b9c

Browse files
author
Natalie Arellano
authored
Fix creator acceptance test flake (#990)
* Use launch cache when calling SaveAs on caching image - Failure to use the launch cache will result in slower second builds, due to the time it takes to pull layers from the daemon - Improve creator acceptance test Signed-off-by: Natalie Arellano <narellano@vmware.com> * Try to make test less flakey by forcing first build to be slower Signed-off-by: Natalie Arellano <narellano@vmware.com> Signed-off-by: Natalie Arellano <narellano@vmware.com>
1 parent 55ce12c commit 8040b9c

3 files changed

Lines changed: 34 additions & 10 deletions

File tree

acceptance/creator_test.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,9 @@ func testCreatorFunc(platformAPI string) func(t *testing.T, when spec.G, it spec
183183

184184
when("multiple builds", func() {
185185
var (
186-
createFlags []string
187-
createArgs []string
186+
createFlags []string
187+
createArgs []string
188+
duration1, duration2 time.Duration
188189
)
189190

190191
it.Before(func() {
@@ -200,6 +201,7 @@ func testCreatorFunc(platformAPI string) func(t *testing.T, when spec.G, it spec
200201
createArgs = append([]string{ctrPath(creatorPath)}, createFlags...)
201202
createArgs = append(createArgs, imageName)
202203

204+
startTime := time.Now()
203205
// first build
204206
output := h.DockerRunAndCopy(t,
205207
container1,
@@ -216,6 +218,8 @@ func testCreatorFunc(platformAPI string) func(t *testing.T, when spec.G, it spec
216218
)...),
217219
h.WithArgs(createArgs...),
218220
)
221+
duration1 = time.Now().Sub(startTime)
222+
t.Logf("First build duration: %s", duration1)
219223
h.AssertStringDoesNotContain(t, output, "restored with content")
220224
h.AssertPathExists(t, filepath.Join(dirBuild1, "layers", "sbom", "build", "samples_hello-world", "sbom.cdx.json"))
221225
h.AssertPathExists(t, filepath.Join(dirBuild1, "layers", "sbom", "build", "samples_hello-world", "some-build-layer", "sbom.cdx.json"))
@@ -258,10 +262,11 @@ func testCreatorFunc(platformAPI string) func(t *testing.T, when spec.G, it spec
258262
h.WithArgs(createArgs...),
259263
)
260264
// check that launch cache was used
261-
duration := time.Now().Sub(startTime)
262-
t.Logf("Build duration: %s", duration)
263-
if duration > 3*time.Second {
264-
t.Fatalf("Expected second build to complete in less than 3 seconds; took %s", duration)
265+
duration2 = time.Now().Sub(startTime)
266+
t.Logf("Second build duration: %s", duration2)
267+
if duration2+time.Duration(0.1*float64(time.Second)) >= duration1 {
268+
t.Logf("Second build output: %s", output)
269+
t.Fatalf("Expected second build to complete 0.1s faster than first build; first build took %s, second build took %s", duration1, duration2)
265270
}
266271
h.AssertStringContains(t, output, "some-layer.sbom.cdx.json restored with content: {\"key\": \"some-launch-true-bom-content\"}")
267272
h.AssertStringContains(t, output, "some-cache-layer.sbom.cdx.json restored with content: {\"key\": \"some-cache-true-bom-content\"}")

acceptance/testdata/creator/container/cnb/buildpacks/samples_hello-world/0.0.1/bin/build

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,15 @@ fi
2828

2929
echo -n "{\"key\": \"some-launch-true-bom-content\"}" > ${layers_dir}/some-layer.sbom.cdx.json
3030

31-
cat <<EOF > "$layers_dir"/some-layer.toml
31+
if test -f ${layers_dir}/some-layer.toml; then
32+
# mimic not downloading new content
33+
echo "nop"
34+
else
35+
# mimic downloading new content
36+
sleep 1
37+
fi
38+
39+
cat <<EOF > ${layers_dir}/some-layer.toml
3240
[types]
3341
launch = true
3442
EOF
@@ -42,7 +50,7 @@ fi
4250

4351
echo -n "{\"key\": \"some-cache-true-bom-content\"}" > ${layers_dir}/some-cache-layer.sbom.cdx.json
4452

45-
cat <<EOF > "$layers_dir"/some-cache-layer.toml
53+
cat <<EOF > ${layers_dir}/some-cache-layer.toml
4654
[types]
4755
cache = true
4856
EOF
@@ -56,7 +64,7 @@ fi
5664

5765
echo -n "{\"key\": \"some-launch-true-cache-true-bom-content\"}" > ${layers_dir}/some-launch-cache-layer.sbom.cdx.json
5866

59-
cat <<EOF > "$layers_dir"/some-launch-cache-layer.toml
67+
cat <<EOF > ${layers_dir}/some-launch-cache-layer.toml
6068
[types]
6169
launch = true
6270
cache = true
@@ -72,7 +80,7 @@ fi
7280

7381
echo -n "{\"key\": \"some-bom-content\"}" > ${layers_dir}/some-build-layer.sbom.cdx.json
7482

75-
cat <<EOF > "$layers_dir"/some-build-layer.toml
83+
cat <<EOF > ${layers_dir}/some-build-layer.toml
7684
[types]
7785
build = true
7886
EOF

cache/caching_image.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,17 @@ func (c *CachingImage) Save(additionalNames ...string) error {
9393
return err
9494
}
9595

96+
func (c *CachingImage) SaveAs(name string, additionalNames ...string) error {
97+
err := c.Image.SaveAs(name, additionalNames...)
98+
99+
if saveSucceededFor(c.Name(), err) {
100+
if err := c.cache.Commit(); err != nil {
101+
return errors.Wrap(err, "failed to commit cache")
102+
}
103+
}
104+
return err
105+
}
106+
96107
func saveSucceededFor(imageName string, err error) bool {
97108
if err == nil {
98109
return true

0 commit comments

Comments
 (0)