forked from paketo-buildpacks/occam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestcontainers.go
71 lines (58 loc) · 1.7 KB
/
testcontainers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package occam
import (
"context"
"time"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/wait"
)
type TestContainers struct {
env map[string]string
publishPorts []string
waitStrategy wait.Strategy
containerMounts []testcontainers.ContainerMount
noStart bool
startupTimeout int
}
func NewTestContainers() TestContainers {
return TestContainers{startupTimeout: 20}
}
func (r TestContainers) WithEnv(env map[string]string) TestContainers {
r.env = env
return r
}
func (r TestContainers) WithMounts(containerMounts ...testcontainers.ContainerMount) TestContainers {
r.containerMounts = append(r.containerMounts, containerMounts...)
return r
}
func (r TestContainers) WithExposedPorts(values ...string) TestContainers {
r.publishPorts = append(r.publishPorts, values...)
return r
}
func (r TestContainers) WithWaitingFor(waitStrategy wait.Strategy) TestContainers {
r.waitStrategy = waitStrategy
return r
}
func (r TestContainers) WithNoStart() TestContainers {
r.noStart = true
return r
}
func (r TestContainers) WithTimeout(t int) TestContainers {
r.startupTimeout = t
return r
}
func (r TestContainers) Execute(imageID string) (testcontainers.Container, error) {
req := testcontainers.ContainerRequest{
Image: imageID,
ExposedPorts: r.publishPorts,
WaitingFor: r.waitStrategy,
Env: r.env,
Mounts: r.containerMounts,
SkipReaper: true,
}
c, cancel := context.WithTimeout(context.Background(), time.Second*time.Duration(r.startupTimeout))
defer cancel()
return testcontainers.GenericContainer(c, testcontainers.GenericContainerRequest{
ContainerRequest: req,
Started: !r.noStart,
})
}