Skip to content

Commit

Permalink
Add pack.Build.WithVolumes()
Browse files Browse the repository at this point in the history
- Also update Docker WithVolume to WithVolumes to match

Signed-off-by: Sophie Wigmore <swigmore@vmware.com>
  • Loading branch information
Frankie Gallina-Jones authored and ryanmoran committed Jan 5, 2022
1 parent 0983662 commit e290aff
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 4 deletions.
14 changes: 10 additions & 4 deletions docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ type DockerContainerRun struct {
entrypoint string
publishPorts []string
publishAll bool
volume string
volumes []string
}

func (r DockerContainerRun) WithEnv(env map[string]string) DockerContainerRun {
Expand Down Expand Up @@ -151,8 +151,14 @@ func (r DockerContainerRun) WithPublishAll() DockerContainerRun {
return r
}

// Deprecated: Use WithVolumes(...volumes) instead.
func (r DockerContainerRun) WithVolume(volume string) DockerContainerRun {
r.volume = volume
r.volumes = append(r.volumes, volume)
return r
}

func (r DockerContainerRun) WithVolumes(volumes ...string) DockerContainerRun {
r.volumes = append(r.volumes, volumes...)
return r
}

Expand Down Expand Up @@ -194,8 +200,8 @@ func (r DockerContainerRun) Execute(imageID string) (Container, error) {
args = append(args, "--entrypoint", r.entrypoint)
}

if r.volume != "" {
args = append(args, "--volume", r.volume)
for _, volume := range r.volumes {
args = append(args, "--volume", volume)
}

args = append(args, imageID)
Expand Down
47 changes: 47 additions & 0 deletions docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,53 @@ func testDocker(t *testing.T, context spec.G, it spec.S) {
})
})

// TODO: remove this when WithVolume is deprecated.
context("when given optionial volume setting", func() {
it("sets the volume flag on the run command", func() {
container, err := docker.Container.Run.
WithVolume("/tmp/host-source:/tmp/dir-on-container:rw").
Execute("some-image-id")

Expect(err).NotTo(HaveOccurred())
Expect(container).To(Equal(occam.Container{
ID: "some-container-id",
}))

Expect(executeArgs).To(HaveLen(2))
Expect(executeArgs[0]).To(Equal([]string{
"container", "run",
"--detach",
"--volume", "/tmp/host-source:/tmp/dir-on-container:rw",
"some-image-id",
}))
})
})

context("when given optional volumes setting", func() {
it("sets the volume flags on the run command", func() {
container, err := docker.Container.Run.
WithVolumes(
"/tmp/host-source:/tmp/dir-on-container:rw",
"/tmp/second-host-source:/tmp/second-dir-on-container:ro",
).
Execute("some-image-id")

Expect(err).NotTo(HaveOccurred())
Expect(container).To(Equal(occam.Container{
ID: "some-container-id",
}))

Expect(executeArgs).To(HaveLen(2))
Expect(executeArgs[0]).To(Equal([]string{
"container", "run",
"--detach",
"--volume", "/tmp/host-source:/tmp/dir-on-container:rw",
"--volume", "/tmp/second-host-source:/tmp/second-dir-on-container:ro",
"some-image-id",
}))
})
})

context("when given optionial volume setting", func() {
it("sets the volume flag on the run command", func() {
container, err := docker.Container.Run.
Expand Down
10 changes: 10 additions & 0 deletions pack.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ type PackBuild struct {
env map[string]string
trustBuilder bool
pullPolicy string
volumes []string

// TODO: remove after deprecation period
noPull bool
Expand Down Expand Up @@ -111,6 +112,11 @@ func (pb PackBuild) WithTrustBuilder() PackBuild {
return pb
}

func (pb PackBuild) WithVolumes(volumes ...string) PackBuild {
pb.volumes = append(pb.volumes, volumes...)
return pb
}

func (pb PackBuild) Execute(name, path string) (Image, fmt.Stringer, error) {
args := []string{"build", name}

Expand Down Expand Up @@ -165,6 +171,10 @@ func (pb PackBuild) Execute(name, path string) (Image, fmt.Stringer, error) {
args = append(args, "--trust-builder")
}

for _, volume := range pb.volumes {
args = append(args, "--volume", volume)
}

buildLogBuffer := bytes.NewBuffer(nil)
err := pb.executable.Execute(pexec.Execution{
Args: args,
Expand Down
24 changes: 24 additions & 0 deletions pack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,31 @@ func testPack(t *testing.T, context spec.G, it spec.S) {
"--trust-builder",
}))
Expect(dockerImageInspectClient.ExecuteCall.Receives.Ref).To(Equal("myapp"))
})
})

context("when given optional volumes", func() {
it("includes the --volume option and args on all commands", func() {
image, logs, err := pack.Build.
WithVolumes(
"/tmp/host-source:/tmp/dir-on-image:rw",
"/tmp/second-host-source:/tmp/second-dir-on-image:ro",
).
Execute("myapp", "/some/app/path")

Expect(err).NotTo(HaveOccurred())
Expect(image).To(Equal(occam.Image{
ID: "some-image-id",
}))
Expect(logs.String()).To(Equal("some stdout output\nsome stderr output\n"))

Expect(executable.ExecuteCall.Receives.Execution.Args).To(Equal([]string{
"build", "myapp",
"--path", "/some/app/path",
"--volume", "/tmp/host-source:/tmp/dir-on-image:rw",
"--volume", "/tmp/second-host-source:/tmp/second-dir-on-image:ro",
}))
Expect(dockerImageInspectClient.ExecuteCall.Receives.Ref).To(Equal("myapp"))
})
})

Expand Down

0 comments on commit e290aff

Please sign in to comment.