Skip to content

Commit

Permalink
add image tag and remove with force
Browse files Browse the repository at this point in the history
  • Loading branch information
Sophie Wigmore authored and robdimsdale committed Feb 2, 2023
1 parent e30c3ea commit 8dcaf0a
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
34 changes: 33 additions & 1 deletion docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type Docker struct {
Image struct {
Inspect DockerImageInspect
Remove DockerImageRemove
Tag DockerImageTag
}

Container struct {
Expand All @@ -39,6 +40,7 @@ func NewDocker() Docker {

docker.Image.Inspect = DockerImageInspect{executable: executable}
docker.Image.Remove = DockerImageRemove{executable: executable}
docker.Image.Tag = DockerImageTag{executable: executable}

docker.Container.Copy = DockerContainerCopy{executable: executable}
docker.Container.Exec = DockerContainerExec{executable: executable}
Expand All @@ -63,6 +65,7 @@ func NewDocker() Docker {
func (d Docker) WithExecutable(executable Executable) Docker {
d.Image.Inspect.executable = executable
d.Image.Remove.executable = executable
d.Image.Tag.executable = executable

d.Container.Copy.executable = executable
d.Container.Exec.executable = executable
Expand Down Expand Up @@ -102,12 +105,24 @@ func (i DockerImageInspect) Execute(ref string) (Image, error) {

type DockerImageRemove struct {
executable Executable
force bool
}

func (r DockerImageRemove) WithForce() DockerImageRemove {
r.force = true
return r
}

func (r DockerImageRemove) Execute(ref string) error {
args := []string{"image", "remove", ref}

if r.force {
args = append(args, "--force")
}

stderr := bytes.NewBuffer(nil)
err := r.executable.Execute(pexec.Execution{
Args: []string{"image", "remove", ref},
Args: args,
Stderr: stderr,
})
if err != nil {
Expand All @@ -117,6 +132,23 @@ func (r DockerImageRemove) Execute(ref string) error {
return nil
}

type DockerImageTag struct {
executable Executable
}

func (r DockerImageTag) Execute(ref, target string) error {
stderr := bytes.NewBuffer(nil)
err := r.executable.Execute(pexec.Execution{
Args: []string{"image", "tag", ref, target},
Stderr: stderr,
})
if err != nil {
return fmt.Errorf("failed to tag docker image: %w: %s", err, strings.TrimSpace(stderr.String()))
}

return nil
}

type DockerContainerRun struct {
executable Executable
inspect DockerContainerInspect
Expand Down
38 changes: 38 additions & 0 deletions docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,17 @@ func testDocker(t *testing.T, context spec.G, it spec.S) {
}))
})

context("when given the optional force setting", func() {
it("sets the force flag on the remove command", func() {
err := docker.Image.Remove.WithForce().Execute("some-image-id")
Expect(err).NotTo(HaveOccurred())

Expect(executable.ExecuteCall.Receives.Execution.Args).To(Equal([]string{
"image", "remove", "some-image-id", "--force",
}))
})
})

context("failure cases", func() {
context("when the executable fails", func() {
it.Before(func() {
Expand All @@ -153,6 +164,33 @@ func testDocker(t *testing.T, context spec.G, it spec.S) {
})
})
})

context("Tag", func() {
it("Tags the image with the target name", func() {
err := docker.Image.Tag.Execute("some-image-id", "new-image-id")
Expect(err).NotTo(HaveOccurred())

Expect(executable.ExecuteCall.Receives.Execution.Args).To(Equal([]string{
"image", "tag", "some-image-id", "new-image-id",
}))
})

context("failure cases", func() {
context("when the executable fails", func() {
it.Before(func() {
executable.ExecuteCall.Stub = func(execution pexec.Execution) error {
fmt.Fprintln(execution.Stderr, "Error: No such image: some-image-id")
return errors.New("exit status 1")
}
})

it("returns an error", func() {
err := docker.Image.Tag.Execute("some-image-id", "some-other-id")
Expect(err).To(MatchError("failed to tag docker image: exit status 1: Error: No such image: some-image-id"))
})
})
})
})
})

context("Container", func() {
Expand Down

0 comments on commit 8dcaf0a

Please sign in to comment.