Skip to content

Commit

Permalink
test: allow reusing svc names for http and tcp/udp
Browse files Browse the repository at this point in the history
  • Loading branch information
chetan committed Jun 30, 2024
1 parent d372fd4 commit 1a0cdfd
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 25 deletions.
7 changes: 4 additions & 3 deletions docker_helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,17 +305,18 @@ func assertServiceIP(t *testing.T, store *testStore, serviceName string, ip stri
}

type svc struct {
name string
proto string
ip string
}

func assertServiceIPs(t *testing.T, store *testStore, svcs map[string]svc) {
for serviceName, svc := range svcs {
func assertServiceIPs(t *testing.T, store *testStore, svcs []svc) {
for _, svc := range svcs {
path := "url"
if svc.proto != "http" {
path = "address"
}
key := fmt.Sprintf("traefik/%s/services/%s/loadBalancer/servers/0/%s", svc.proto, serviceName, path)
key := fmt.Sprintf("traefik/%s/services/%s/loadBalancer/servers/0/%s", svc.proto, svc.name, path)
assert.Equal(t,
svc.ip,
store.kv[key],
Expand Down
32 changes: 16 additions & 16 deletions docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ func Test_helloWorld(t *testing.T) {
assert.Equal(t, "hello1", store.kv["traefik/http/routers/hello1/service"])
assert.Equal(t, "hello2", store.kv["traefik/http/routers/hello2/service"])

assertServiceIPs(t, store, map[string]svc{
"hello1": {"http", "http://192.168.100.100:5555"},
"hello2": {"http", "http://192.168.100.100:5566"},
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")
Expand All @@ -70,42 +70,42 @@ func Test_helloWorld(t *testing.T) {
func Test_helloDetect(t *testing.T) {
// both services get mapped to the same port (error case)
store := doTest(t, "hellodetect.yml")
assertServiceIPs(t, store, map[string]svc{
"hello-detect": {"http", "http://192.168.100.100:5577"},
"hello-detect2": {"http", "http://192.168.100.100:5577"},
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")
assertServiceIPs(t, store, map[string]svc{
"helloip": {"http", "http://4.4.4.4:5599"},
"helloip2": {"http", "http://3.3.3.3:5599"},
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")
assertServiceIPs(t, store, map[string]svc{
"hello1": {"http", "http://10.10.10.5:5555"},
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")
assertServiceIPs(t, store, map[string]svc{
"gitea-ssh": {"tcp", "192.168.100.100:20022"},
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")
assertServiceIPs(t, store, map[string]svc{
"mqtt-http": {"http", "http://192.168.100.100:9001"},
"mqtt-tcp": {"tcp", "192.168.100.100:1883"},
assertServiceIPs(t, store, []svc{
{"mqtt", "http", "http://192.168.100.100:9001"},
{"mqtt", "tcp", "192.168.100.100:1883"},
})
}
12 changes: 6 additions & 6 deletions fixtures/mqtt.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ services:
image: gitea/gitea
labels:
- "traefik.enable=true"
- "traefik.http.routers.mqtt-http.rule=Host(`mqtt.local`)"
- "traefik.http.services.mqtt-http.loadbalancer.server.port=9001"
- "traefik.http.routers.mqtt.rule=Host(`mqtt.local`)"
- "traefik.http.services.mqtt.loadbalancer.server.port=9001"
# MQTT routing
- "traefik.tcp.routers.mqtt-tcp.rule=HostSNI(`*`)"
- "traefik.tcp.routers.mqtt-tcp.entrypoints=mqtt"
- "traefik.tcp.routers.mqtt-tcp.service=service-broker-mqtt"
- "traefik.tcp.services.mqtt-tcp.loadbalancer.server.port=1883"
- "traefik.tcp.routers.mqtt.rule=HostSNI(`*`)"
- "traefik.tcp.routers.mqtt.entrypoints=mqtt"
- "traefik.tcp.routers.mqtt.service=service-broker-mqtt"
- "traefik.tcp.services.mqtt.loadbalancer.server.port=1883"

0 comments on commit 1a0cdfd

Please sign in to comment.