Skip to content

Commit

Permalink
adding tests for builder create and improving logging messages
Browse files Browse the repository at this point in the history
Signed-off-by: Juan Bustamante <[email protected]>
  • Loading branch information
jjbustamante committed May 22, 2024
1 parent 574b3a2 commit 166c3d8
Show file tree
Hide file tree
Showing 7 changed files with 311 additions and 58 deletions.
179 changes: 179 additions & 0 deletions acceptance/acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3296,6 +3296,185 @@ include = [ "*.jar", "media/mountain.jpg", "/media/person.png", ]
})
})
})

when("multi-platform", func() {
var (
tmpDir string
multiArchBuildpackPackage string
builderTomlPath string
remoteRunImage string
remoteBuildImage string
err error
)

it.Before(func() {
h.SkipIf(t, !pack.SupportsFeature(invoke.MultiPlatformBuildersAndBuildPackages), "multi-platform builders and buildpack packages are available since 0.34.0")

tmpDir, err = os.MkdirTemp("", "multi-platform-builder-create-tests")
assert.Nil(err)

// used to avoid authentication issues with the local registry
os.Setenv("DOCKER_CONFIG", registryConfig.DockerConfigDir)

// create a multi-platform buildpack and push it to a registry
multiArchBuildpackPackage = registryConfig.RepoName("simple-multi-platform-buildpack" + h.RandString(8))
sourceDir := filepath.Join("testdata", "mock_buildpacks")
path := filepath.Join(tmpDir, "simple-layers-buildpack")
err = buildpacks.BpFolderSimpleLayers.Prepare(sourceDir, tmpDir)
h.AssertNil(t, err)

output := pack.RunSuccessfully(
"buildpack", "package", multiArchBuildpackPackage,
"--path", path,
"--publish",
"--target", "linux/amd64",
"--target", "windows/amd64",
)
assertions.NewOutputAssertionManager(t, output).ReportsPackagePublished(multiArchBuildpackPackage)
assertions.NewOutputAssertionManager(t, output).ReportsSuccessfulIndexPushed(multiArchBuildpackPackage)
h.AssertRemoteImageIndex(t, multiArchBuildpackPackage, types.OCIImageIndex, 2)

// runImage and buildImage are saved in the daemon, for this test we want them to be available in a registry
remoteRunImage = registryConfig.RepoName(runImage + h.RandString(8))
remoteBuildImage = registryConfig.RepoName(buildImage + h.RandString(8))

imageManager.TagImage(runImage, remoteRunImage)
imageManager.TagImage(buildImage, remoteBuildImage)

h.AssertNil(t, h.PushImage(dockerCli, remoteRunImage, registryConfig))
h.AssertNil(t, h.PushImage(dockerCli, remoteBuildImage, registryConfig))
})

it.After(func() {
imageManager.CleanupImages(remoteBuildImage)
imageManager.CleanupImages(remoteRunImage)
os.RemoveAll(tmpDir)
})

generateMultiPlatformBuilderToml := func(template, buildpackURI, buildImage, runImage string) string {
t.Helper()
buildpackToml, err := os.CreateTemp(tmpDir, "buildpack-*.toml")
assert.Nil(err)

pack.FixtureManager().TemplateFixtureToFile(
template,
buildpackToml,
map[string]interface{}{
"BuildpackURI": buildpackURI,
"BuildImage": buildImage,
"RunImage": runImage,
},
)
assert.Nil(buildpackToml.Close())
return buildpackToml.Name()
}

when("builder.toml has no targets", func() {
when("--publish", func() {
it.Before(func() {
builderName = registryConfig.RepoName("remote-multi-platform-builder" + h.RandString(8))

// We need to configure our builder.toml with image references that points to our ephemeral registry
builderTomlPath = generateMultiPlatformBuilderToml("builder_multi_platform-no-targets.toml", multiArchBuildpackPackage, remoteBuildImage, remoteRunImage)
})

it("publishes builder images for each requested target to the registry and creates an image index", func() {
output := pack.RunSuccessfully(
"builder", "create", builderName,
"--config", builderTomlPath,
"--publish",
"--target", "linux/amd64",
"--target", "windows/amd64",
)

defer imageManager.CleanupImages(builderName)
assertions.NewOutputAssertionManager(t, output).ReportsBuilderCreated(builderName)

assertImage.CanBePulledFromRegistry(builderName)

assertions.NewOutputAssertionManager(t, output).ReportsSuccessfulIndexPushed(builderName)
h.AssertRemoteImageIndex(t, builderName, types.OCIImageIndex, 2)
})
})

when("--daemon", func() {
it.Before(func() {
builderName = registryConfig.RepoName("local-multi-platform-builder" + h.RandString(8))

// We need to configure our builder.toml with image references that points to our ephemeral registry
builderTomlPath = generateMultiPlatformBuilderToml("builder_multi_platform-no-targets.toml", multiArchBuildpackPackage, buildImage, runImage)
})

it("publishes builder image to the daemon for the given target", func() {
platform := "linux/amd64"
if runtime.GOOS == "windows" {
platform = "windows/amd64"
}

output := pack.RunSuccessfully(
"builder", "create", builderName,
"--config", builderTomlPath,
"--target", platform,
)

defer imageManager.CleanupImages(builderName)
assertions.NewOutputAssertionManager(t, output).ReportsBuilderCreated(builderName)
})
})
})

when("builder.toml has targets", func() {
when("--publish", func() {
it.Before(func() {
builderName = registryConfig.RepoName("remote-multi-platform-builder" + h.RandString(8))

// We need to configure our builder.toml with image references that points to our ephemeral registry
builderTomlPath = generateMultiPlatformBuilderToml("builder_multi_platform.toml", multiArchBuildpackPackage, remoteBuildImage, remoteRunImage)
})

it("publishes builder images for each configured target to the registry and creates an image index", func() {
output := pack.RunSuccessfully(
"builder", "create", builderName,
"--config", builderTomlPath,
"--publish",
)

defer imageManager.CleanupImages(builderName)
assertions.NewOutputAssertionManager(t, output).ReportsBuilderCreated(builderName)

assertImage.CanBePulledFromRegistry(builderName)

assertions.NewOutputAssertionManager(t, output).ReportsSuccessfulIndexPushed(builderName)
h.AssertRemoteImageIndex(t, builderName, types.OCIImageIndex, 2)
})
})

when("--daemon", func() {
it.Before(func() {
builderName = registryConfig.RepoName("local-multi-platform-builder" + h.RandString(8))

// We need to configure our builder.toml with image references that points to our ephemeral registry
builderTomlPath = generateMultiPlatformBuilderToml("builder_multi_platform.toml", multiArchBuildpackPackage, buildImage, runImage)
})

it("publishes builder image to the daemon for the given target", func() {
platform := "linux/amd64"
if runtime.GOOS == "windows" {
platform = "windows/amd64"
}

output := pack.RunSuccessfully(
"builder", "create", builderName,
"--config", builderTomlPath,
"--target", platform,
)

defer imageManager.CleanupImages(builderName)
assertions.NewOutputAssertionManager(t, output).ReportsBuilderCreated(builderName)
})
})
})
})
})

when("builder create", func() {
Expand Down
6 changes: 6 additions & 0 deletions acceptance/assertions/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,12 @@ func (o OutputAssertionManager) IncludesUsagePrompt() {
o.assert.Contains(o.output, "Run 'pack --help' for usage.")
}

func (o OutputAssertionManager) ReportsBuilderCreated(name string) {
o.testObject.Helper()

o.assert.ContainsF(o.output, "Successfully created builder image '%s'", name)
}

func (o OutputAssertionManager) ReportsSettingDefaultBuilder(name string) {
o.testObject.Helper()

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[[buildpacks]]
id = "simple/layers"
version = "simple-layers-version"
uri = "{{ .BuildpackURI }}"

[[order]]
[[order.group]]
id = "simple/layers"
version = "simple-layers-version"

[build]
image = "{{ .BuildImage }}"

[run]
[[run.images]]
image = "{{ .RunImage }}"



28 changes: 28 additions & 0 deletions acceptance/testdata/pack_fixtures/builder_multi_platform.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
[[buildpacks]]
id = "simple/layers"
version = "simple-layers-version"
uri = "{{ .BuildpackURI }}"

[[order]]
[[order.group]]
id = "simple/layers"
version = "simple-layers-version"

# Targets the buildpack will work with
[[targets]]
os = "linux"
arch = "amd64"

[[targets]]
os = "windows"
arch = "arm64"

[build]
image = "{{ .BuildImage }}"

[run]
[[run.images]]
image = "{{ .RunImage }}"



2 changes: 1 addition & 1 deletion internal/commands/builder_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ Creating a custom builder allows you to control what buildpacks are used and wha
}

if len(multiArchCfg.Targets()) == 0 {
logger.Warnf("A new '--target' flag is available to set the platform")
logger.Infof("Pro tip: use --targets flag OR [[targets]] in builder.toml to specify the desired platform")
}

imageName := args[0]
Expand Down
Loading

0 comments on commit 166c3d8

Please sign in to comment.