Skip to content

Commit

Permalink
Add fix for same Dockerfile bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Sophie Wigmore authored and sophiewigmore committed Jun 6, 2022
1 parent 950df4e commit c7e4f27
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 10 deletions.
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

0 comments on commit c7e4f27

Please sign in to comment.