Skip to content

Commit 4a8c463

Browse files
committed
added mount option
1 parent 77bd974 commit 4a8c463

File tree

6 files changed

+91
-42
lines changed

6 files changed

+91
-42
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
[![Go Report Card](https://goreportcard.com/badge/github.com/abecodes/dft)](https://goreportcard.com/report/github.com/abecodes/dft)
1+
[![Go Report Card](https://goreportcard.com/badge/github.com/abecodes/dft)](https://goreportcard.com/report/github.com/abecodes/dft)
22
[![Go Reference](https://pkg.go.dev/badge/github.com/abecodes/dft.svg)](https://pkg.go.dev/github.com/abecodes/dft)
33

44
# 🥼 DFT
@@ -134,6 +134,7 @@ func TestUserService(tt *testing.T) {
134134
| --- | --- | --- |
135135
| WithCmd | Overwrite [CMD]. | `WithCmd([]string{"--tlsCAFile", "/run/tls/ca.crt"})` |
136136
| WithEnvVar | Set an envvar inside the container.<br>Can be called multiple times.<br>If two options use the same key the latest one will overwrite existing ones. | `WithEnvVar("intent", "prod")` |
137+
| WithMount | Mount a local dir or file<br>Can be called multiple times. | `WithMount("./host/folder", "/target")` |
137138
| WithPort | Expose an internal port on a specific host port. | `WithPort(27017,8080)` |
138139
| WithRandomPort | Expose an internal port on a random host port.<br>Use `ExposedPorts` or `ExposedPortAddresses` to get the correct host port. | `WithRandomPort(27017)` |
139140

container.go

+49-5
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,52 @@ type Container struct {
1919
func newContainer(
2020
ctx context.Context,
2121
imageName string,
22-
exposedPorts [][2]uint,
23-
envVars []string,
24-
args []string,
22+
opts ...ContainerOption,
2523
) (*Container, error) {
26-
id, err := startContainer(ctx, imageName, exposedPorts, envVars, args)
24+
cfg := containerCfg{
25+
args: nil,
26+
env: nil,
27+
mounts: nil,
28+
ports: nil,
29+
}
30+
31+
// INFO: we could pass the options further down and parse them in functions
32+
// we are calling, but we need the exposed ports here to check if we are up
33+
for i := range opts {
34+
opts[i](&cfg)
35+
}
36+
37+
var (
38+
arguments []string
39+
envVars []string
40+
exposedPorts [][2]uint
41+
mounts [][2]string
42+
)
43+
44+
if cfg.args != nil {
45+
arguments = *cfg.args
46+
}
47+
48+
if cfg.env != nil {
49+
envVars = *cfg.env
50+
}
51+
52+
if cfg.mounts != nil {
53+
mounts = *cfg.mounts
54+
}
55+
56+
if cfg.ports != nil {
57+
exposedPorts = *cfg.ports
58+
}
59+
60+
id, err := startContainer(
61+
ctx,
62+
imageName,
63+
arguments,
64+
envVars,
65+
exposedPorts,
66+
mounts,
67+
)
2768
if err != nil {
2869
return nil, fmt.Errorf(
2970
"[%s](%s) %w",
@@ -43,7 +84,10 @@ func newContainer(
4384
if err != nil {
4485
ctr := Container{id: id}
4586

46-
sCtx, sCtxCancel := context.WithTimeout(context.Background(), 5*time.Second)
87+
sCtx, sCtxCancel := context.WithTimeout(
88+
context.Background(),
89+
5*time.Second,
90+
)
4791
_ = ctr.Stop(sCtx)
4892
sCtxCancel()
4993
}

dft.go

+1-30
Original file line numberDiff line numberDiff line change
@@ -19,38 +19,9 @@ func StartContainer(
1919
return nil, err
2020
}
2121

22-
cfg := containerCfg{
23-
env: nil,
24-
ports: nil,
25-
}
26-
27-
for i := range opts {
28-
opts[i](&cfg)
29-
}
30-
31-
var (
32-
args []string
33-
envVars []string
34-
exposedPorts [][2]uint
35-
)
36-
37-
if cfg.args != nil {
38-
args = *cfg.args
39-
}
40-
41-
if cfg.env != nil {
42-
envVars = *cfg.env
43-
}
44-
45-
if cfg.ports != nil {
46-
exposedPorts = *cfg.ports
47-
}
48-
4922
return newContainer(
5023
ctx,
5124
imageName,
52-
exposedPorts,
53-
envVars,
54-
args,
25+
opts...,
5526
)
5627
}

dft_test.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,10 @@ func TestDFT(tt *testing.T) {
4848
},
4949
),
5050
)
51-
if !strings.Contains(err.Error(), "container in invalid state: 'exited'") {
51+
if !strings.Contains(
52+
err.Error(),
53+
"container in invalid state: 'exited'",
54+
) {
5255
t.Errorf("[dft.StartContainer] unexpected error: %v", err)
5356
tt.FailNow()
5457

@@ -66,6 +69,7 @@ func TestDFT(tt *testing.T) {
6669
ctr, err = dft.StartContainer(
6770
ctx,
6871
"mongo:7-jammy",
72+
dft.WithMount("./testfile", "/etc/testfile"),
6973
dft.WithRandomPort(27017),
7074
dft.WithPort(27017, 27017),
7175
)

docker.go

+16-2
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,10 @@ const (
3434
func startContainer(
3535
ctx context.Context,
3636
imageName string,
37-
exposedPorts [][2]uint,
38-
envVars []string,
3937
arguments []string,
38+
envVars []string,
39+
exposedPorts [][2]uint,
40+
mounts [][2]string,
4041
) (string, error) {
4142
var (
4243
stdOutCapture bytes.Buffer
@@ -70,6 +71,19 @@ func startContainer(
7071
args = append(args, "-e", envVars[i])
7172
}
7273

74+
// passing envVars
75+
for i := range mounts {
76+
args = append(
77+
args,
78+
"--mount",
79+
fmt.Sprintf(
80+
"type=bind,source=%s,target=%s",
81+
mounts[i][0],
82+
mounts[i][1],
83+
),
84+
)
85+
}
86+
7387
args = append(args, imageName)
7488

7589
// appending command overwrites

options.go

+18-3
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ package dft
33
import "strings"
44

55
type containerCfg struct {
6-
args *[]string
7-
env *[]string
8-
ports *[][2]uint
6+
args *[]string
7+
env *[]string
8+
mounts *[][2]string
9+
ports *[][2]uint
910
}
1011

1112
type waitCfg struct {
@@ -53,6 +54,20 @@ func WithEnvVar(key string, value string) ContainerOption {
5354
}
5455
}
5556

57+
// WithExecuteInsideContainer defines if the wait cmd is executed inside the container
58+
// or on the host machine
59+
func WithMount(dest string, trgt string) ContainerOption {
60+
return func(cfg *containerCfg) {
61+
if cfg.mounts == nil {
62+
cfg.mounts = &[][2]string{{dest, trgt}}
63+
64+
return
65+
}
66+
67+
*cfg.mounts = append(*cfg.mounts, [2]string{dest, trgt})
68+
}
69+
}
70+
5671
// WithPort will expose the passed internal port via a given target port on the host.
5772
func WithPort(port uint, target uint) ContainerOption {
5873
return func(cfg *containerCfg) {

0 commit comments

Comments
 (0)