Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Amend update-builder functionality #342

Merged
merged 2 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion commands/update_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,20 @@ func updateBuilderRun(flags updateBuilderFlags) error {

builder.Lifecycle.Version = lifecycleImage.Version

buildImage, err := internal.FindLatestBuildImage(builder.Stack.RunImage, builder.Stack.BuildImage)
runImage, buildImage, err := internal.FindLatestStackImages(builder.Stack.RunImage, builder.Stack.BuildImage)
if err != nil {
return err
}

builder.Stack.BuildImage = fmt.Sprintf("%s:%s", buildImage.Name, buildImage.Version)
if runImage != (internal.Image{}) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are the () necessary?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you need them when comparing an instantiation inline but that could be old news.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes they are necessary unfortunately. expected ';', found '{' (typecheck)

builder.Stack.RunImage = fmt.Sprintf("%s:%s", runImage.Name, runImage.Version)
updatedMirrors, err := internal.UpdateRunImageMirrors(runImage.Version, builder.Stack.RunImageMirrors)
if err != nil {
return err
}
builder.Stack.RunImageMirrors = updatedMirrors
}

err = internal.OverwriteBuilderConfig(flags.builderFile, builder)
if err != nil {
Expand Down
226 changes: 209 additions & 17 deletions integration/update_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,150 @@ description = "Some description"
`, "REGISTRY-URI", strings.TrimPrefix(server.URL, "http://"))))
})

context("when the run image is set to latest", func() {
it.Before(func() {
err := os.WriteFile(filepath.Join(builderDir, "builder.toml"), bytes.ReplaceAll([]byte(`
description = "Some description"

[lifecycle]
version = "0.10.2"

[stack]
id = "io.paketo.stacks.some-stack"
build-image = "REGISTRY-URI/somerepository/build:0.0.10"
run-image = "REGISTRY-URI/somerepository/run:latest"
run-image-mirrors = ["another-registry-uri/somerepository/run:latest"]
`), []byte(`REGISTRY-URI`), []byte(strings.TrimPrefix(server.URL, "http://"))), 0600)
Expect(err).NotTo(HaveOccurred())
})

it("updates the build image as expected", func() {
command := exec.Command(
path,
"update-builder",
"--builder-file", filepath.Join(builderDir, "builder.toml"),
"--lifecycle-uri", fmt.Sprintf("%s/some-repository/lifecycle", strings.TrimPrefix(server.URL, "http://")),
)

buffer := gbytes.NewBuffer()
session, err := gexec.Start(command, buffer, buffer)
Expect(err).NotTo(HaveOccurred())

Eventually(session).Should(gexec.Exit(0), func() string { return string(buffer.Contents()) })

builderContents, err := os.ReadFile(filepath.Join(builderDir, "builder.toml"))
Expect(err).NotTo(HaveOccurred())
Expect(string(builderContents)).To(MatchTOML(strings.ReplaceAll(`
description = "Some description"

[lifecycle]
version = "0.21.1"

[stack]
build-image = "REGISTRY-URI/somerepository/build:0.20.1"
id = "io.paketo.stacks.some-stack"
run-image = "REGISTRY-URI/somerepository/run:latest"
run-image-mirrors = ["another-registry-uri/somerepository/run:latest"]
`, "REGISTRY-URI", strings.TrimPrefix(server.URL, "http://"))))
})
})

context("when the run image is set to a semantic version, without a tag suffix", func() {
it.Before(func() {
err := os.WriteFile(filepath.Join(builderDir, "builder.toml"), bytes.ReplaceAll([]byte(`
description = "Some description"

[lifecycle]
version = "0.10.2"

[stack]
id = "io.paketo.stacks.some-stack"
build-image = "REGISTRY-URI/somerepository/build:0.0.10"
run-image = "REGISTRY-URI/somerepository/run:0.0.10"
run-image-mirrors = ["SOME-OTHER-REGISTRY-URI/somerepository/run:0.0.10"]
`), []byte(`REGISTRY-URI`), []byte(strings.TrimPrefix(server.URL, "http://"))), 0600)
Expect(err).NotTo(HaveOccurred())
})

it("updates the build, run, and mirror images", func() {
command := exec.Command(
path,
"update-builder",
"--builder-file", filepath.Join(builderDir, "builder.toml"),
"--lifecycle-uri", fmt.Sprintf("%s/some-repository/lifecycle", strings.TrimPrefix(server.URL, "http://")),
)

buffer := gbytes.NewBuffer()
session, err := gexec.Start(command, buffer, buffer)
Expect(err).NotTo(HaveOccurred())

Eventually(session).Should(gexec.Exit(0), func() string { return string(buffer.Contents()) })

builderContents, err := os.ReadFile(filepath.Join(builderDir, "builder.toml"))
Expect(err).NotTo(HaveOccurred())
Expect(string(builderContents)).To(MatchTOML(strings.ReplaceAll(`
description = "Some description"

[lifecycle]
version = "0.21.1"

[stack]
build-image = "REGISTRY-URI/somerepository/build:0.20.1"
id = "io.paketo.stacks.some-stack"
run-image = "REGISTRY-URI/somerepository/run:0.20.1"
run-image-mirrors = ["SOME-OTHER-REGISTRY-URI/somerepository/run:0.20.1"]
`, "REGISTRY-URI", strings.TrimPrefix(server.URL, "http://"))))
})
})

context("when the run image is set to a semantic version, with tag suffix", func() {
it.Before(func() {
err := os.WriteFile(filepath.Join(builderDir, "builder.toml"), bytes.ReplaceAll([]byte(`
description = "Some description"

[lifecycle]
version = "0.10.2"

[stack]
id = "io.paketo.stacks.some-stack"
build-image = "REGISTRY-URI/somerepository/build:0.0.10"
run-image = "REGISTRY-URI/somerepository/run:0.0.10-some-cnb"
run-image-mirrors = ["SOME-OTHER-REGISTRY-URI/somerepository/run:0.0.10"]
`), []byte(`REGISTRY-URI`), []byte(strings.TrimPrefix(server.URL, "http://"))), 0600)
Expect(err).NotTo(HaveOccurred())
})

it("updates the build, run, and mirror images to the highest version with a tag suffix", func() {
command := exec.Command(
path,
"update-builder",
"--builder-file", filepath.Join(builderDir, "builder.toml"),
"--lifecycle-uri", fmt.Sprintf("%s/some-repository/lifecycle", strings.TrimPrefix(server.URL, "http://")),
)

buffer := gbytes.NewBuffer()
session, err := gexec.Start(command, buffer, buffer)
Expect(err).NotTo(HaveOccurred())

Eventually(session).Should(gexec.Exit(0), func() string { return string(buffer.Contents()) })

builderContents, err := os.ReadFile(filepath.Join(builderDir, "builder.toml"))
Expect(err).NotTo(HaveOccurred())
Expect(string(builderContents)).To(MatchTOML(strings.ReplaceAll(`
description = "Some description"

[lifecycle]
version = "0.21.1"

[stack]
build-image = "REGISTRY-URI/somerepository/build:0.20.12-some-cnb"
id = "io.paketo.stacks.some-stack"
run-image = "REGISTRY-URI/somerepository/run:0.20.12-some-cnb"
run-image-mirrors = ["SOME-OTHER-REGISTRY-URI/somerepository/run:0.20.12-some-cnb"]
`, "REGISTRY-URI", strings.TrimPrefix(server.URL, "http://"))))
})
})

context("failure cases", func() {
context("when the --builder-file flag is missing", func() {
it("prints an error and exits non-zero", func() {
Expand Down Expand Up @@ -467,34 +611,82 @@ description = "Some description"
err := os.WriteFile(filepath.Join(builderDir, "builder.toml"), bytes.ReplaceAll([]byte(`
description = "Some description"

[[buildpacks]]
uri = "docker://REGISTRY-URI/paketo-buildpacks/go:0.0.10"
version = "0.0.10"
[lifecycle]
version = "0.10.2"

[[buildpacks]]
uri = "docker://REGISTRY-URI/paketobuildpacks/nodejs:0.20.22"
version = "0.20.22"
[stack]
id = "io.paketo.stacks.some-stack"
build-image = "REGISTRY-URI/somerepository/error-build:0.0.10-some-cnb"
run-image = "REGISTRY-URI/somerepository/run:some-cnb"
run-image-mirrors = ["REGISTRY-URI/some-repository/run:some-cnb"]
`), []byte(`REGISTRY-URI`), []byte(strings.TrimPrefix(server.URL, "http://"))), 0600)
Expect(err).NotTo(HaveOccurred())
})

it("prints an error and exits non-zero", func() {
command := exec.Command(
path,
"update-builder",
"--builder-file", filepath.Join(builderDir, "builder.toml"),
"--lifecycle-uri", fmt.Sprintf("%s/some-repository/lifecycle", strings.TrimPrefix(server.URL, "http://")),
)

buffer := gbytes.NewBuffer()
session, err := gexec.Start(command, buffer, buffer)
Expect(err).NotTo(HaveOccurred())

Eventually(session).Should(gexec.Exit(1), func() string { return string(buffer.Contents()) })
Expect(string(buffer.Contents())).To(ContainSubstring("failed to list tags"))
})
})

context("when no build images with a matching run image tag suffix exist", func() {
it.Before(func() {
err := os.WriteFile(filepath.Join(builderDir, "builder.toml"), bytes.ReplaceAll([]byte(`
description = "Some description"

[lifecycle]
version = "0.10.2"

[[order]]
[stack]
id = "io.paketo.stacks.some-stack"
build-image = "REGISTRY-URI/somerepository/error-build:0.0.10-some-cnb"
run-image = "REGISTRY-URI/somerepository/run:some-random-suffix"
run-image-mirrors = []
`), []byte(`REGISTRY-URI`), []byte(strings.TrimPrefix(server.URL, "http://"))), 0600)
Expect(err).NotTo(HaveOccurred())
})

[[order.group]]
id = "paketo-buildpacks/nodejs"
version = "0.20.22"
it("prints an error and exits non-zero", func() {
command := exec.Command(
path,
"update-builder",
"--builder-file", filepath.Join(builderDir, "builder.toml"),
"--lifecycle-uri", fmt.Sprintf("%s/some-repository/lifecycle", strings.TrimPrefix(server.URL, "http://")),
)

[[order]]
buffer := gbytes.NewBuffer()
session, err := gexec.Start(command, buffer, buffer)
Expect(err).NotTo(HaveOccurred())

[[order.group]]
id = "paketo-buildpacks/go"
version = "0.0.10"
Eventually(session).Should(gexec.Exit(1), func() string { return string(buffer.Contents()) })
Expect(string(buffer.Contents())).To(ContainSubstring("failed to find latest build image: failed to list tags"))
})
})

context("when the run image URI cannot be parsed", func() {
it.Before(func() {
err := os.WriteFile(filepath.Join(builderDir, "builder.toml"), bytes.ReplaceAll([]byte(`
description = "Some description"

[lifecycle]
version = "0.10.2"

[stack]
id = "io.paketo.stacks.some-stack"
build-image = "REGISTRY-URI/somerepository/error-build:0.0.10-some-cnb"
run-image = "REGISTRY-URI/somerepository/run:some-cnb"
run-image-mirrors = ["REGISTRY-URI/some-repository/run:some-cnb"]
run-image = "bad uri"
run-image-mirrors = []
`), []byte(`REGISTRY-URI`), []byte(strings.TrimPrefix(server.URL, "http://"))), 0600)
Expect(err).NotTo(HaveOccurred())
})
Expand All @@ -512,7 +704,7 @@ description = "Some description"
Expect(err).NotTo(HaveOccurred())

Eventually(session).Should(gexec.Exit(1), func() string { return string(buffer.Contents()) })
Expect(string(buffer.Contents())).To(ContainSubstring("failed to list tags"))
Expect(string(buffer.Contents())).To(ContainSubstring("failed to parse run image"))
})
})

Expand Down
Loading
Loading