-
Notifications
You must be signed in to change notification settings - Fork 16
/
docker_test.go
146 lines (119 loc) · 3.85 KB
/
docker_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
package traefikkop
import (
"context"
"log"
"os"
"testing"
"github.com/docker/docker/client"
"github.com/gofiber/fiber/v2"
"github.com/stretchr/testify/assert"
)
var app *fiber.App
var dockerEndpoint string
var dc client.APIClient
var dockerAPI = &DockerAPIStub{}
func setup() {
app, dockerEndpoint = createHTTPServer()
var err error
dc, err = createDockerClient(dockerEndpoint)
if err != nil {
log.Fatal(err)
}
}
func teardown() {
err := app.Shutdown()
if err != nil {
log.Fatal(err)
}
}
func TestMain(m *testing.M) {
setup()
code := m.Run()
teardown()
os.Exit(code)
}
func Test_httpServerVersion(t *testing.T) {
v, err := dc.ServerVersion(context.Background())
assert.NoError(t, err)
assert.Equal(t, "1.0.0", v.Version)
}
func Test_helloWorld(t *testing.T) {
store := doTest(t, "helloworld.yml", nil)
assert.NotNil(t, store)
assert.NotNil(t, store.kv)
assert.Equal(t, "hello1", store.kv["traefik/http/routers/hello1/service"])
assert.Equal(t, "hello2", store.kv["traefik/http/routers/hello2/service"])
assert.NotNil(t, store.kv["traefik/http/routers/hello1/tls/certResolver"])
assert.NotNil(t, store.kv["traefik/http/routers/hello2/tls/certResolver"])
assertServiceIPs(t, store, []svc{
{"hello1", "http", "http://192.168.100.100:5555"},
{"hello2", "http", "http://192.168.100.100:5566"},
})
// assertServiceIP(t, store, "hello1", "http://192.168.100.100:5555")
// assert.Equal(t, "http://192.168.100.100:5555", store.kv["traefik/http/services/hello1/loadBalancer/servers/0/url"])
// assert.Equal(t, "http://192.168.100.100:5566", store.kv["traefik/http/services/hello2/loadBalancer/servers/0/url"])
}
func Test_helloDetect(t *testing.T) {
// both services get mapped to the same port (error case)
store := doTest(t, "hellodetect.yml", nil)
assertServiceIPs(t, store, []svc{
{"hello-detect", "http", "http://192.168.100.100:5577"},
{"hello-detect2", "http", "http://192.168.100.100:5577"},
})
}
func Test_helloIP(t *testing.T) {
// override ip via labels
store := doTest(t, "helloip.yml", nil)
assertServiceIPs(t, store, []svc{
{"helloip", "http", "http://4.4.4.4:5599"},
{"helloip2", "http", "http://3.3.3.3:5599"},
})
}
func Test_helloNetwork(t *testing.T) {
// use ip from specific docker network
store := doTest(t, "network.yml", nil)
assertServiceIPs(t, store, []svc{
{"hello1", "http", "http://10.10.10.5:5555"},
})
}
func Test_TCP(t *testing.T) {
// tcp service
store := doTest(t, "gitea.yml", nil)
assertServiceIPs(t, store, []svc{
{"gitea-ssh", "tcp", "192.168.100.100:20022"},
})
}
func Test_TCPMQTT(t *testing.T) {
// from https://github.com/jittering/traefik-kop/issues/35
store := doTest(t, "mqtt.yml", nil)
assertServiceIPs(t, store, []svc{
{"mqtt", "http", "http://192.168.100.100:9001"},
{"mqtt", "tcp", "192.168.100.100:1883"},
})
}
func Test_helloWorldNoCert(t *testing.T) {
store := doTest(t, "hello-no-cert.yml", nil)
assert.Equal(t, "hello1", store.kv["traefik/http/routers/hello1/service"])
assert.Nil(t, store.kv["traefik/http/routers/hello1/tls/certResolver"])
assertServiceIPs(t, store, []svc{
{"hello1", "http", "http://192.168.100.100:5555"},
})
}
func Test_helloWorldIgnore(t *testing.T) {
store := doTest(t, "hello-ignore.yml", nil)
assert.Nil(t, store.kv["traefik/http/routers/hello1/service"])
store = doTest(t, "hello-ignore.yml", &Config{Namespace: "foobar"})
assert.Equal(t, "hello1", store.kv["traefik/http/routers/hello1/service"])
assertServiceIPs(t, store, []svc{
{"hello1", "http", "http://192.168.100.100:5555"},
})
}
func Test_samePrefix(t *testing.T) {
store := doTest(t, "prefix.yml", nil)
// Two services `hello` and `hello-test`.
// The former's name is a prefix of the latter. Ensure the matching does not mix them up.
assertServiceIPs(t, store, []svc{
{"hello", "http", "http://192.168.100.100:5555"},
{"hello-test", "http", "http://192.168.100.100:5566"},
})
}