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

Add fix for same Dockerfile bug #72

Merged
merged 3 commits into from
Jun 6, 2022
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
13 changes: 10 additions & 3 deletions internal/ihop/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,12 +283,19 @@ func (c Client) Build(def DefinitionImage, platform string) (Image, error) {
// Update will apply any modifications made to the Image reference onto the
// actual container image in the Docker daemon.
func (c Client) Update(image Image) (Image, error) {
img, err := image.ToDaemonImage()
// Add a random tag to the original image to distinguish it from other
// identical images
random, err := randomName()
if err != nil {
return Image{}, err
}
originalImage := fmt.Sprintf("%s:%s", image.Tag, random)
err = c.docker.ImageTag(context.Background(), image.Tag, originalImage)
if err != nil {
return Image{}, err
}

configName, err := img.ConfigName()
img, err := image.ToDaemonImage()
if err != nil {
return Image{}, err
}
Expand Down Expand Up @@ -342,7 +349,7 @@ func (c Client) Update(image Image) (Image, error) {
return Image{}, err
}

err = c.Cleanup(Image{Tag: configName.String()})
err = c.Cleanup(Image{Tag: originalImage})
if err != nil {
return Image{}, err
}
Expand Down
45 changes: 38 additions & 7 deletions internal/ihop/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"archive/tar"
"bytes"
ctx "context"
"fmt"
"io/ioutil"
"os"
"os/exec"
Expand Down Expand Up @@ -296,22 +297,52 @@ RUN --mount=type=secret,id=test-secret,dst=/temp cat /temp > /secret`), 0600)
Expect(img).To(HaveFileWithContent("/some/file", ContainSubstring("some-layer-content")))
})

context("when there are two identical images to be updated", func() {
var image2 ihop.Image

it.Before(func() {
contents, err := exec.Command("docker", "tag", fmt.Sprintf("%s:latest", image.Tag), "image2:latest").CombinedOutput()
Expect(err).NotTo(HaveOccurred(), string(contents))

image2 = image
image2.Tag = "image2"
images = append(images, image2)
})

it("both are updated successfully", func() {
_, err := client.Update(image)
Expect(err).NotTo(HaveOccurred())

_, err = client.Update(image2)
Expect(err).NotTo(HaveOccurred())
})
})

context("failure cases", func() {

context("when the image tag cannot be parsed", func() {
it("returns an error", func() {
_, err := client.Update(ihop.Image{Tag: "not a valid tag"})
Expect(err).To(MatchError(ContainSubstring("could not parse reference")))
Expect(err).To(MatchError(ContainSubstring("invalid reference format")))
})
})

context("when the image layer diff ID is not valid", func() {
var img ihop.Image
it.Before(func() {
img = image
img.Layers[0].DiffID = "this is not a diff id"
})
it.After(func() {
daemonImage, err := img.ToDaemonImage()
Expect(err).NotTo(HaveOccurred())

configName, _ := daemonImage.ConfigName()
images = append(images, ihop.Image{Tag: configName.String()})
})

it("returns an error", func() {
_, err := client.Update(ihop.Image{
Tag: "busybox:latest",
Layers: []ihop.Layer{
{DiffID: "this is not a diff id"},
},
})
_, err := client.Update(img)
Expect(err).To(MatchError(ContainSubstring("cannot parse hash")))
})
})
Expand Down