Skip to content

Commit

Permalink
Add 'docker cp' command
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuatcasey authored and ForestEckhardt committed Aug 2, 2022
1 parent 93980d6 commit 539ec4c
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
20 changes: 20 additions & 0 deletions docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type Docker struct {
}

Pull DockerPull
Copy DockerCopy
}

func NewDocker() Docker {
Expand All @@ -52,6 +53,7 @@ func NewDocker() Docker {
docker.Volume.Remove = DockerVolumeRemove{executable: executable}

docker.Pull = DockerPull{executable: executable}
docker.Copy = DockerCopy{executable: executable}

return docker
}
Expand All @@ -71,6 +73,7 @@ func (d Docker) WithExecutable(executable Executable) Docker {
d.Volume.Remove.executable = executable

d.Pull.executable = executable
d.Copy.executable = executable

return d
}
Expand Down Expand Up @@ -411,3 +414,20 @@ func (p DockerPull) Execute(image string) error {

return nil
}

type DockerCopy struct {
executable Executable
}

func (docker DockerCopy) Execute(source, dest string) error {
stderr := bytes.NewBuffer(nil)
err := docker.executable.Execute(pexec.Execution{
Args: []string{"cp", source, dest},
Stderr: stderr,
})
if err != nil {
return fmt.Errorf("'docker cp' failed: %w: %s", err, strings.TrimSpace(stderr.String()))
}

return nil
}
30 changes: 30 additions & 0 deletions docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -880,4 +880,34 @@ func testDocker(t *testing.T, context spec.G, it spec.S) {

})
})

context("Copy", func() {
it("will execute 'docker cp SOURCE DEST'", func() {
err := docker.Copy.Execute("source/path", "dest-container:/path")
Expect(err).NotTo(HaveOccurred())

Expect(executable.ExecuteCall.Receives.Execution.Args).To(Equal([]string{
"cp",
"source/path",
"dest-container:/path",
}))
})

context("failure cases", func() {
context("when the cp command fails", func() {
it.Before(func() {
executable.ExecuteCall.Stub = func(execution pexec.Execution) error {
_, err := fmt.Fprint(execution.Stderr, "must specify at least one container source")
Expect(err).NotTo(HaveOccurred())
return errors.New("exit status 1")
}
})

it("returns an error", func() {
err := docker.Copy.Execute("source", "dest")
Expect(err).To(MatchError("'docker cp' failed: exit status 1: must specify at least one container source"))
})
})
})
})
}

0 comments on commit 539ec4c

Please sign in to comment.