Skip to content

Commit

Permalink
add failing test to restorer
Browse files Browse the repository at this point in the history
  • Loading branch information
joeybrown-sf committed May 9, 2024
1 parent 82fdf23 commit 1832f36
Showing 1 changed file with 85 additions and 47 deletions.
132 changes: 85 additions & 47 deletions phase/restorer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,53 +203,7 @@ func testRestorer(buildpackAPI, platformAPI string) func(t *testing.T, when spec
h.AssertNil(t, os.RemoveAll(layersDir))
h.AssertNil(t, os.Mkdir(layersDir, 0777))

contents := fmt.Sprintf(`{
"buildpacks": [
{
"key": "buildpack.id",
"layers": {
"cache-false": {
"cache": false,
"sha": "%s"
},
"cache-launch": {
"cache": true,
"launch": true,
"sha": "%s"
},
"cache-only": {
"cache": true,
"data": {
"cache-only-key": "cache-only-val"
},
"sha": "%s"
}
}
},
{
"key": "nogroup.buildpack.id",
"layers": {
"some-layer": {
"cache": true,
"sha": "%s"
}
}
},
{
"key": "escaped/buildpack/id",
"layers": {
"escaped-bp-layer": {
"cache": true,
"data": {
"escaped-bp-key": "escaped-bp-val"
},
"sha": "%s"
}
}
}
]
}
`, cacheFalseLayerSHA, cacheLaunchLayerSHA, cacheOnlyLayerSHA, noGroupLayerSHA, escapedLayerSHA)
contents := buildMetadata(cacheFalseLayerSHA, cacheLaunchLayerSHA, cacheOnlyLayerSHA, noGroupLayerSHA, escapedLayerSHA)

err = os.WriteFile(
filepath.Join(cacheDir, "committed", "io.buildpacks.lifecycle.cache.metadata"),
Expand Down Expand Up @@ -348,6 +302,40 @@ func testRestorer(buildpackAPI, platformAPI string) func(t *testing.T, when spec
})
})

when("there is a cache-only layer referenced in metadata that does not exist", func() {
var nonExistentCacheLaunchLayerSHA string

it.Before(func() {
nonExistentCacheLaunchLayerSHA = "some-made-up-sha"
contents := buildMetadata(cacheFalseLayerSHA, nonExistentCacheLaunchLayerSHA, cacheOnlyLayerSHA, noGroupLayerSHA, escapedLayerSHA)

err := os.WriteFile(
filepath.Join(cacheDir, "committed", "io.buildpacks.lifecycle.cache.metadata"),
[]byte(contents),
0600,
)
h.AssertNil(t, err)

err = restorer.Restore(testCache)
h.AssertNil(t, err)
})

it("restores expected cache-only layer", func() {
got := h.MustReadFile(t, filepath.Join(layersDir, "buildpack.id", "cache-only", "file-from-cache-only-layer"))
want := "echo text from cache-only layer\n"
h.AssertEq(t, string(got), want)
})

it("keeps expected layer metadata", func() {
got := h.MustReadFile(t, filepath.Join(layersDir, "buildpack.id", "cache-only.toml"))
h.AssertEq(t, string(got), "[metadata]\n cache-only-key = \"cache-only-val\"\n")
})

it("skips restoring non-existent cache-launch layer", func() {
h.AssertPathDoesNotExist(t, filepath.Join(layersDir, "buildpack.id", "cache-launch"))
})
})

when("there is a cache=true layer not in cache", func() {
it.Before(func() {
var meta, sha string
Expand Down Expand Up @@ -550,3 +538,53 @@ func TestWriteLayer(t *testing.T) {

h.AssertPathDoesNotExist(t, filepath.Join(layersDir, "test-buildpack", "test-layer"))
}

func buildMetadata(cacheFalseLayerSHA string, cacheLaunchLayerSHA string, cacheOnlyLayerSHA string, noGroupLayerSHA string, escapedLayerSHA string) string {
return fmt.Sprintf(`{
"buildpacks": [
{
"key": "buildpack.id",
"layers": {
"cache-false": {
"cache": false,
"sha": "%s"
},
"cache-launch": {
"cache": true,
"launch": true,
"sha": "%s"
},
"cache-only": {
"cache": true,
"data": {
"cache-only-key": "cache-only-val"
},
"sha": "%s"
}
}
},
{
"key": "nogroup.buildpack.id",
"layers": {
"some-layer": {
"cache": true,
"sha": "%s"
}
}
},
{
"key": "escaped/buildpack/id",
"layers": {
"escaped-bp-layer": {
"cache": true,
"data": {
"escaped-bp-key": "escaped-bp-val"
},
"sha": "%s"
}
}
}
]
}
`, cacheFalseLayerSHA, cacheLaunchLayerSHA, cacheOnlyLayerSHA, noGroupLayerSHA, escapedLayerSHA)
}

0 comments on commit 1832f36

Please sign in to comment.