-
-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathgnomock_internal_test.go
162 lines (125 loc) · 3.85 KB
/
gnomock_internal_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
package gnomock
import (
"context"
"errors"
"os"
"testing"
"time"
"github.com/stretchr/testify/require"
"go.uber.org/goleak"
)
const testImage = "docker.io/orlangure/gnomock-test-image"
func TestMain(m *testing.M) {
goleak.VerifyTestMain(m)
}
func TestWaitForContainerNetwork(t *testing.T) {
t.Parallel()
// this test starts a regular container using existing API, and then uses
// it to test specific error flows
namedPorts := NamedPorts{
"web80": TCP(80),
"web8080": TCP(8080),
}
container, err := StartCustom(
testImage, namedPorts,
WithTimeout(time.Minute),
)
require.NoError(t, err)
require.NotNil(t, container)
id, _ := parseID(container.ID)
gg, err := newG(false)
require.NoError(t, err)
d, err := gg.dockerConnect()
require.NoError(t, err)
defer func() {
require.NoError(t, d.stopClient())
}()
ctx := context.Background()
t.Run("fails after context cancellation", func(t *testing.T) {
ctx, cancel := context.WithCancel(ctx)
cancel()
_, err = d.waitForContainerNetwork(ctx, id, namedPorts)
require.EqualError(t, err, "container network is unavailable after timeout")
})
t.Run("fails with wrong container id", func(t *testing.T) {
_, err = d.waitForContainerNetwork(ctx, "wrong-id", namedPorts)
require.Error(t, err)
require.Contains(t, err.Error(), "No such container")
})
t.Run("returns ErrPortNotFound for wrong port number", func(t *testing.T) {
_, err := d.waitForContainerNetwork(ctx, id, DefaultTCP(42))
require.Error(t, err)
require.True(t, errors.Is(err, ErrPortNotFound), err.Error())
})
require.NoError(t, Stop(container))
}
func TestEnvAwareClone(t *testing.T) {
// this test cannot run in parallel with other tests since it modifies the
// environment, which affects other tests
original := &Container{
ID: "foo",
Host: "bar",
gateway: "gateway",
}
t.Run("same host returned in regular flow", func(t *testing.T) {
cloned := envAwareClone(original)
require.Equal(t, original.ID, cloned.ID)
require.Equal(t, original.Host, cloned.Host)
})
t.Run("container gateway returned when no internal host available", func(t *testing.T) {
currentEnv := os.Getenv("GNOMOCK_ENV")
_ = os.Setenv("GNOMOCK_ENV", "gnomockd")
defer func() {
_ = os.Setenv("GNOMOCK_ENV", currentEnv)
}()
cloned := envAwareClone(original)
require.Equal(t, original.ID, cloned.ID)
require.Equal(t, original.gateway, cloned.Host)
})
}
func TestCustomDockerHost(t *testing.T) {
// this test cannot run in parallel with other tests since it modifies the
// environment, which affects other tests
t.Run("fails with misconfigured docker host", func(t *testing.T) {
currentHost := os.Getenv("DOCKER_HOST")
defer func() {
_ = os.Setenv("DOCKER_HOST", currentHost)
}()
_ = os.Setenv("DOCKER_HOST", "example.com")
c, err := StartCustom(testImage, DefaultTCP(80))
require.True(t, errors.Is(err, ErrEnvClient))
require.Nil(t, c)
})
t.Run("hostAddr returns docker host address", func(t *testing.T) {
currentHost := os.Getenv("DOCKER_HOST")
defer func() {
_ = os.Setenv("DOCKER_HOST", currentHost)
}()
_ = os.Setenv("DOCKER_HOST", "tcp://1.1.1.1:2375")
d := &docker{}
addr := d.hostAddr()
require.Equal(t, "1.1.1.1", addr)
})
t.Run("hostAddr falls back to local", func(t *testing.T) {
t.Run("wrong url", func(t *testing.T) {
currentHost := os.Getenv("DOCKER_HOST")
defer func() {
_ = os.Setenv("DOCKER_HOST", currentHost)
}()
_ = os.Setenv("DOCKER_HOST", ":")
d := &docker{}
addr := d.hostAddr()
require.Equal(t, localhostAddr, addr)
})
t.Run("unix socket", func(t *testing.T) {
currentHost := os.Getenv("DOCKER_HOST")
defer func() {
_ = os.Setenv("DOCKER_HOST", currentHost)
}()
_ = os.Setenv("DOCKER_HOST", "unix:///run/user/1000/docker.sock")
d := &docker{}
addr := d.hostAddr()
require.Equal(t, localhostAddr, addr)
})
})
}