Skip to content

Commit

Permalink
Adds docker container restart command
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Moran authored and sophiewigmore committed Aug 1, 2022
1 parent 75b5894 commit 93980d6
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
22 changes: 21 additions & 1 deletion docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ type Docker struct {
Container struct {
Inspect DockerContainerInspect
Logs DockerContainerLogs
Run DockerContainerRun
Remove DockerContainerRemove
Restart DockerContainerRestart
Run DockerContainerRun
Stop DockerContainerStop
}

Expand All @@ -45,6 +46,7 @@ func NewDocker() Docker {
}

docker.Container.Remove = DockerContainerRemove{executable: executable}
docker.Container.Restart = DockerContainerRestart{executable: executable}
docker.Container.Stop = DockerContainerStop{executable: executable}

docker.Volume.Remove = DockerVolumeRemove{executable: executable}
Expand All @@ -60,6 +62,7 @@ func (d Docker) WithExecutable(executable Executable) Docker {

d.Container.Inspect.executable = executable
d.Container.Logs.executable = executable
d.Container.Restart.executable = executable
d.Container.Remove.executable = executable
d.Container.Run.executable = executable
d.Container.Run.inspect = d.Container.Inspect
Expand Down Expand Up @@ -277,6 +280,23 @@ func (r DockerContainerRun) Execute(imageID string) (Container, error) {
return r.inspect.Execute(strings.TrimSpace(stdout.String()))
}

type DockerContainerRestart struct {
executable Executable
}

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

return nil
}

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

context("Restart", func() {
it("restarts a docker container with the given container id", func() {
err := docker.Container.Restart.Execute("some-container-id")
Expect(err).NotTo(HaveOccurred())

Expect(executable.ExecuteCall.Receives.Execution.Args).To(Equal([]string{
"container", "restart", "some-container-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 container: some-container-id")
return errors.New("exit status 1")
}
})

it("returns an error", func() {
err := docker.Container.Restart.Execute("some-container-id")
Expect(err).To(MatchError("failed to restart docker container: exit status 1: Error: No such container: some-container-id"))
})
})
})
})

context("Remove", func() {
it("removes a docker container with the given container id", func() {
err := docker.Container.Remove.Execute("some-container-id")
Expand Down

0 comments on commit 93980d6

Please sign in to comment.