-
-
Notifications
You must be signed in to change notification settings - Fork 68
/
preset_test.go
175 lines (138 loc) · 4.34 KB
/
preset_test.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package gnomock_test
import (
"context"
"testing"
"time"
"github.com/docker/docker/client"
"github.com/orlangure/gnomock"
"github.com/orlangure/gnomock/internal/health"
"github.com/orlangure/gnomock/internal/testutil"
"github.com/stretchr/testify/require"
)
func TestPreset_parallel(t *testing.T) {
t.Parallel()
containers, err := gnomock.InParallel().
Start(&testutil.TestPreset{Img: testutil.TestImage}).
Start(&testutil.TestPreset{Img: testutil.TestImage}).
Start(&testutil.TestPreset{Img: testutil.TestImage}).
Start(&testutil.TestPreset{Img: testutil.TestImage}).
Start(&testutil.TestPreset{Img: testutil.TestImage}).
Go()
defer func() { require.NoError(t, gnomock.Stop(containers...)) }()
require.NoError(t, err)
require.Len(t, containers, 5)
ctx := context.Background()
for _, c := range containers {
require.NoError(t, health.HTTPGet(ctx, c.Address("web80")))
require.NoError(t, health.HTTPGet(ctx, c.Address("web8080")))
}
for _, c := range containers {
require.NoError(t, gnomock.Stop(c))
}
}
func TestPreset(t *testing.T) {
t.Parallel()
p := &testutil.TestPreset{Img: testutil.TestImage}
container, err := gnomock.Start(p)
defer func(c *gnomock.Container) {
require.NoError(t, gnomock.Stop(c))
}(container)
require.NoError(t, err)
}
func TestPreset_containerRemainsIfDebug(t *testing.T) {
t.Parallel()
p := &testutil.TestPreset{Img: testutil.TestImage}
container, err := gnomock.Start(
p,
gnomock.WithTimeout(time.Second*30),
gnomock.WithDebugMode(),
gnomock.WithHealthCheck(failingHealthcheck),
)
require.Error(t, err)
require.NotNil(t, container)
// if stopped without error, container existed at this point
err = gnomock.Stop(container)
require.NoError(t, err)
// allow the container to actually stop
time.Sleep(time.Second)
// confirm it doesn't exist anymore
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
require.NoError(t, err)
containerList, err := testutil.ListContainerByID(cli, container.ID)
require.NoError(t, err)
require.Len(t, containerList, 0)
require.NoError(t, cli.Close())
}
func TestPreset_duplicateContainerName(t *testing.T) {
t.Parallel()
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
require.NoError(t, err)
p := &testutil.TestPreset{Img: testutil.TestImage}
originalContainer, err := gnomock.Start(
p,
gnomock.WithTimeout(time.Minute),
gnomock.WithContainerName("gnomock"),
gnomock.WithDebugMode(),
)
require.NoError(t, err)
newContainer, err := gnomock.Start(
p,
gnomock.WithTimeout(time.Minute),
gnomock.WithContainerName("gnomock"),
gnomock.WithDebugMode(),
)
require.NoError(t, err)
containerList, err := testutil.ListContainerByID(cli, originalContainer.ID)
require.NoError(t, err)
require.Len(t, containerList, 0)
require.NoError(t, gnomock.Stop(newContainer))
require.NoError(t, cli.Close())
}
func TestPreset_reusableContainerSucceeds(t *testing.T) {
t.Parallel()
p := &testutil.TestPreset{Img: testutil.TestImage}
originalContainer, err := gnomock.Start(
p,
gnomock.WithTimeout(time.Minute),
gnomock.WithContainerName("gnomock-reuse"),
gnomock.WithDebugMode(),
)
require.NoError(t, err)
newContainer, err := gnomock.Start(
p,
gnomock.WithTimeout(time.Minute),
gnomock.WithContainerName("gnomock-reuse"),
gnomock.WithDebugMode(),
gnomock.WithContainerReuse(),
)
require.NoError(t, err)
require.Equal(t, originalContainer.ID, newContainer.ID)
require.NoError(t, gnomock.Stop(newContainer))
}
func TestPreset_reusableContainerFailsWithoutName(t *testing.T) {
t.Parallel()
p := &testutil.TestPreset{Img: testutil.TestImage}
_, err := gnomock.Start(
p,
gnomock.WithTimeout(time.Minute),
gnomock.WithContainerReuse(),
gnomock.WithDebugMode(),
)
require.EqualError(t, err, "can't start container: container name is required when container reuse is enabled")
}
func TestPreset_customNamedPorts(t *testing.T) {
t.Parallel()
p := &testutil.TestPreset{Img: testutil.TestImage}
presetPorts := p.Ports()
pr := presetPorts["web80"]
pr.HostPort = 23080
presetPorts["web80"] = pr
container, err := gnomock.Start(
p,
gnomock.WithCustomNamedPorts(presetPorts),
gnomock.WithDebugMode(),
)
t.Cleanup(func() { require.NoError(t, gnomock.Stop(container)) })
require.NoError(t, err)
require.Equal(t, 23080, container.Ports.Get("web80").Port)
}