Skip to content

Commit

Permalink
add asset cache acceptance test
Browse files Browse the repository at this point in the history
Signed-off-by: dwillist <[email protected]>
  • Loading branch information
dwillist committed Feb 23, 2021
1 parent 9f93ecd commit 179da37
Show file tree
Hide file tree
Showing 14 changed files with 351 additions and 1 deletion.
209 changes: 209 additions & 0 deletions acceptance/acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2253,6 +2253,208 @@ include = [ "*.jar", "media/mountain.jpg", "/media/person.png", ]
})
})

when("creating asset cache", func() {
var tmpDir string
it.Before(func() {
var err error
tmpDir, err = ioutil.TempDir("", "asset-cache-testing")
assert.Nil(err)
})
it.After(func() {
os.RemoveAll(tmpDir)
})

it("creates reproducable asset image", func() {
buildpackManager = buildpacks.NewBuildpackManager(
t,
assert,
buildpacks.WithBuildpackSource(tmpDir),
)

templateMapping := map[string]interface{}{}
// resolve local assets to absolute paths
assetAPath, err := filepath.Abs(filepath.Join("testdata", "mock_assets", "assetA.txt"))
assert.Nil(err)
assetBPath, err := filepath.Abs(filepath.Join("testdata", "mock_assets", "assetB.txt"))
assert.Nil(err)
assetCPath, err := filepath.Abs(filepath.Join("testdata", "mock_assets", "assetC.txt"))
assert.Nil(err)

templateMapping["assetAURI"] = assetAPath
templateMapping["assetBURI"] = assetBPath
templateMapping["assetCURI"] = assetCPath

simpleBuildpackRoot := filepath.Join(tmpDir, "simple-buildpack")
assert.Succeeds(os.Mkdir(simpleBuildpackRoot, os.ModePerm))

secondSimpleBuildpackRoot := filepath.Join(tmpDir, "second-simple-buildpack")
assert.Succeeds(os.Mkdir(secondSimpleBuildpackRoot, os.ModePerm))

nestedBuildpackRoot := filepath.Join(tmpDir, "nested-assets-buildpack")
assert.Succeeds(os.Mkdir(nestedBuildpackRoot, os.ModePerm))

h.RecursiveCopy(t, filepath.Join("testdata", "mock_buildpacks", "with_assets", "simple-buildpack"), simpleBuildpackRoot)
h.RecursiveCopy(t, filepath.Join("testdata", "mock_buildpacks", "with_assets", "second-simple-buildpack"), secondSimpleBuildpackRoot)
h.RecursiveCopy(t, filepath.Join("testdata", "mock_buildpacks", "with_assets", "nested-assets-buildpack"), nestedBuildpackRoot)

simpleBuildpackTOML := filepath.Join(simpleBuildpackRoot, "buildpack.toml")
simpleBuildpackTOMLFile, err := os.OpenFile(simpleBuildpackTOML, os.O_CREATE|os.O_RDWR, os.ModePerm)
assert.Nil(err)

secondSimpleBuildpackTOML := filepath.Join(secondSimpleBuildpackRoot, "buildpack.toml")
secondSimpleBuildpackTOMLFile, err := os.OpenFile(secondSimpleBuildpackTOML, os.O_CREATE|os.O_RDWR, os.ModePerm)
assert.Nil(err)

// create asset buildpacks
simpleArchiveBuildpack := buildpacks.NewArchiveBuildpack("simple-buildpack")
secondSimpleArchiveBuildpack := buildpacks.NewArchiveBuildpack("second-simple-buildpack")
nestedArchiveBuildpack := buildpacks.NewArchiveBuildpack("nested-assets-buildpack")

// template buildpack assets
fixtureManager := pack.FixtureManager()
fixtureManager.TemplateFile(simpleBuildpackTOMLFile, templateMapping)
assert.Succeeds(simpleBuildpackTOMLFile.Close())

fixtureManager.TemplateFile(secondSimpleBuildpackTOMLFile, templateMapping)
assert.Succeeds(secondSimpleBuildpackTOMLFile.Close())

// template asset buildpacks using buildpack images

simpleBuildpackConfigFile, err := ioutil.TempFile(tmpDir, "simple_package.toml")
assert.Nil(err)
fixtureManager.TemplateFixtureToFile(
"generic_package.toml",
simpleBuildpackConfigFile,
map[string]interface{}{
"buildpack_uri": "simple-buildpack.tgz",
"OS": dockerHostOS(),
},
)
err = simpleBuildpackConfigFile.Close()
assert.Nil(err)

secondBuildpackConfigFile, err := ioutil.TempFile(tmpDir, "second_package.toml")
assert.Nil(err)
fixtureManager.TemplateFixtureToFile(
"generic_package.toml",
secondBuildpackConfigFile,
map[string]interface{}{
"buildpack_uri": "second-simple-buildpack.tgz",
"OS": dockerHostOS(),
},
)
err = secondBuildpackConfigFile.Close()
assert.Nil(err)

// buildpack image names
simpleBuildpackImageName := registryConfig.RepoName("simple-assets-buildpack-" + h.RandString(8))
secondSimpleBuildpackImageName := registryConfig.RepoName("second-simple-assets-buildpack-" + h.RandString(8))
nestedBuildpackImageName := registryConfig.RepoName("nested-assets-buildpack-" + h.RandString(8))

templateMapping["simple_buildpack"] = simpleBuildpackImageName
templateMapping["second_simple_buildpack"] = secondSimpleBuildpackImageName

nestedConfigFile, err := ioutil.TempFile(tmpDir, "nested_assets_buildpack_package.toml")
fixtureManager.TemplateFixtureToFile(
"nested_assets_buildpack_package.toml",
nestedConfigFile,
templateMapping,
)

assert.Succeeds(nestedConfigFile.Close())

packageImageBuildpack := buildpacks.NewPackageImage(
t,
pack,
nestedBuildpackImageName,
nestedConfigFile.Name(),
buildpacks.WithRequiredBuildpacks(
nestedArchiveBuildpack,
buildpacks.NewPackageImage(
t,
pack,
simpleBuildpackImageName,
simpleBuildpackConfigFile.Name(),
buildpacks.WithRequiredBuildpacks(simpleArchiveBuildpack),
),
buildpacks.NewPackageImage(
t,
pack,
secondSimpleBuildpackImageName,
secondBuildpackConfigFile.Name(),
buildpacks.WithRequiredBuildpacks(secondSimpleArchiveBuildpack),
),
),
)

buildpackManager.PrepareBuildpacks(tmpDir, packageImageBuildpack)
defer h.DockerRmi(dockerCli, simpleBuildpackImageName, secondSimpleBuildpackImageName, nestedBuildpackImageName)

assetCacheName := registryConfig.RepoName("some-asset-org/" + h.RandString(10))
pack.RunSuccessfully(
"asset-cache", "create",
assetCacheName,
"--buildpack", nestedBuildpackImageName,
)
defer h.DockerRmi(dockerCli, assetCacheName)

allAssetsBuildpackRoot := filepath.Join(tmpDir, "all-assets-buildpack")
assert.Succeeds(os.Mkdir(allAssetsBuildpackRoot, os.ModePerm))

h.RecursiveCopy(t, filepath.Join("testdata", "mock_buildpacks", "with_assets", "all-assets-buildpack"), allAssetsBuildpackRoot)

allAssetsBuildpackTOML := filepath.Join(allAssetsBuildpackRoot, "buildpack.toml")
allAssetsBuildpackTOMLFile, err := os.OpenFile(allAssetsBuildpackTOML, os.O_CREATE|os.O_RDWR, os.ModePerm)
assert.Nil(err)

fixtureManager.TemplateFile(allAssetsBuildpackTOMLFile, templateMapping)
assert.Succeeds(allAssetsBuildpackTOMLFile.Close())

allAssetsBuildpackConfigFile, err := ioutil.TempFile(tmpDir, "all_assets_package.toml")
assert.Nil(err)
fixtureManager.TemplateFixtureToFile(
"generic_package.toml",
allAssetsBuildpackConfigFile,
map[string]interface{}{
"buildpack_uri": "all-assets-buildpack.tgz",
"OS": dockerHostOS(),
},
)
err = allAssetsBuildpackConfigFile.Close()
assert.Nil(err)

allAssetsBuildpackImageName := registryConfig.RepoName("all-assets-buildpack-" + h.RandString(8))
allAssetsArchiveBuildpack := buildpacks.NewArchiveBuildpack("all-assets-buildpack")

allAssetsImageBuildpack := buildpacks.NewPackageImage(
t,
pack,
allAssetsBuildpackImageName,
allAssetsBuildpackConfigFile.Name(),
buildpacks.WithRequiredBuildpacks(
allAssetsArchiveBuildpack,
),
)

buildpackManager.PrepareBuildpacks(tmpDir, allAssetsImageBuildpack)
defer h.DockerRmi(dockerCli, allAssetsBuildpackImageName)

identicalAssetCacheName := registryConfig.RepoName("some-identical-asset-org/" + h.RandString(10))
pack.RunSuccessfully(
"asset-cache", "create",
identicalAssetCacheName,
"--buildpack", allAssetsBuildpackImageName,
)
defer h.DockerRmi(dockerCli, identicalAssetCacheName)

firstSha := imageSha(t, assert, dockerCli, assetCacheName)
secondSha := imageSha(t, assert, dockerCli, identicalAssetCacheName)
assert.Equal(firstSha, secondSha)

// now lets check the sha256 values of these two assets are the same
})
})

when("rebase", func() {
var repoName, runBefore, origID string
var buildRunImage func(string, string, string)
Expand Down Expand Up @@ -2877,6 +3079,13 @@ func imageLabel(t *testing.T, assert h.AssertionManager, dockerCli client.Common
return label
}

func imageSha(t *testing.T, assert h.AssertionManager, dockerCli client.CommonAPIClient, repoName string) string {
t.Helper()
inspect, _, err := dockerCli.ImageInspectWithRaw(context.Background(), repoName)
assert.Nil(err)
return inspect.ID
}

func dockerHostOS() string {
daemonInfo, err := dockerCli.Info(context.TODO())
if err != nil {
Expand Down
6 changes: 6 additions & 0 deletions acceptance/buildpacks/archive_buildpack.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ const (
defaultMode = 0755
)

func NewArchiveBuildpack(name string) archiveBuildpack {
return archiveBuildpack{
name: name,
}
}

type archiveBuildpack struct {
name string
}
Expand Down
6 changes: 6 additions & 0 deletions acceptance/buildpacks/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ func WithBuildpackAPIVersion(apiVersion string) func(b *BuildpackManager) {
}
}

func WithBuildpackSource(source string) func(b *BuildpackManager) {
return func(b *BuildpackManager) {
b.sourceDir = source
}
}

func NewBuildpackManager(t *testing.T, assert testhelpers.AssertionManager, modifiers ...BuildpackManagerModifier) BuildpackManager {
m := BuildpackManager{
testObject: t,
Expand Down
1 change: 0 additions & 1 deletion acceptance/buildpacks/package_image_buildpack.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package buildpacks
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"

Expand Down
16 changes: 16 additions & 0 deletions acceptance/invoke/pack_fixtures.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,22 @@ func (m PackFixtureManager) TemplateFixtureToFile(name string, destination *os.F
m.assert.Nil(err)
}

func (m PackFixtureManager) TemplateFile(file *os.File, data map[string]interface{}) {
m.testObject.Helper()

outputTemplate, err := ioutil.ReadAll(file)
m.assert.Nil(err)

_, err = file.Seek(0, 0)
m.assert.Nil(err)

err = file.Truncate(0)
m.assert.Nil(err)

_, err = io.WriteString(file, m.fillTemplate(outputTemplate, data))
m.assert.Nil(err)
}

func (m PackFixtureManager) fillTemplate(templateContents []byte, data map[string]interface{}) string {
tpl, err := template.New("").
Funcs(template.FuncMap{
Expand Down
1 change: 1 addition & 0 deletions acceptance/testdata/mock_assets/assetA.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
A pretty cool asset Ayyyy.
1 change: 1 addition & 0 deletions acceptance/testdata/mock_assets/assetB.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Just another asset.
1 change: 1 addition & 0 deletions acceptance/testdata/mock_assets/assetC.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Just another-nother asset.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
api = "0.2"

[buildpack]
id = "all-assets/asset-buildpack"
version = "all-assets-builapck-version"
name = "All Assets Buildpack Buildpack"

[[stacks]]
id = "pack.test.stack"

[[assets]]
id = "asset A"
sha256 = "797b3f6bf2b2c10a8299d51dfdbcfed329d3c133fdc7e695beddbe8f70b49da9"
name = "A Asset"
stacks = ["pack.test.stack"]
uri = "{{ .assetAURI }}"
version = "1.2.3"

[[assets]]
id = "asset C"
sha256 = "339b5181dac7c2d01cbbd6fff90f541f0350864f03a02f05faf852baa2826064"
name = "C Asset"
stacks = ["pack.test.stack"]
uri = "{{ .assetCURI }}"
version = "1.3.5"

[[assets]]
id = "asset B"
name = "B Asset"
sha256 = "61eea2ec4053ca25b9bd5d7bebaba48ee5398569aa1da5bc3541cbab1d09b86b"
stacks = ["pack.test.stack"]
uri = "{{ .assetBURI }}"
version = "4.5.6"
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
api = "0.2"

[buildpack]
id = "simple/nested"
version = "nested-version"
name = "Nested Asset Buildpack"

[[order]]
[[order.group]]
id = "simple/asset-buildpack"
version = "simple-asset-buildpack-version"
[[order.group]]
id = "second-simple/asset-buildpack"
version = "second-simple-asset-buildpack-version"
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
api = "0.2"

[buildpack]
id = "second-simple/asset-buildpack"
version = "second-simple-asset-buildpack-version"
name = "Second Simple Asset Buildpack Buildpack"

[[stacks]]
id = "pack.test.stack"


[[assets]]
id = "asset C"
sha256 = "339b5181dac7c2d01cbbd6fff90f541f0350864f03a02f05faf852baa2826064"
name = "C Asset"
stacks = ["pack.test.stack"]
uri = "{{ .assetCURI }}"
version = "1.3.5"

[[assets]]
id = "asset B repeat"
name = "B Asset Repeat"
sha256 = "61eea2ec4053ca25b9bd5d7bebaba48ee5398569aa1da5bc3541cbab1d09b86b"
stacks = ["pack.test.stack"]
uri = "{{ .assetBURI }}"
version = "4.5.6"
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
api = "0.2"

[buildpack]
id = "simple/asset-buildpack"
version = "simple-asset-buildpack-version"
name = "Simple Asset Buildpack Buildpack"

[[stacks]]
id = "pack.test.stack"

[[assets]]
id = "asset A"
sha256 = "797b3f6bf2b2c10a8299d51dfdbcfed329d3c133fdc7e695beddbe8f70b49da9"
name = "A Asset"
stacks = ["pack.test.stack"]
uri = "{{ .assetAURI }}"
version = "1.2.3"

[[assets]]
id = "asset B"
name = "B Asset"
sha256 = "61eea2ec4053ca25b9bd5d7bebaba48ee5398569aa1da5bc3541cbab1d09b86b"
stacks = ["pack.test.stack"]
uri = "{{ .assetBURI }}"
version = "4.5.6"
Loading

0 comments on commit 179da37

Please sign in to comment.