151 lines
5.7 KiB
Go
151 lines
5.7 KiB
Go
package agent
|
|
|
|
import (
|
|
"reflect"
|
|
"strings"
|
|
"testing"
|
|
|
|
"sultan/pkg/docker"
|
|
"sultan/pkg/registry"
|
|
)
|
|
|
|
func ctr(name string, labels map[string]string, ports ...docker.Port) docker.Container {
|
|
return docker.Container{ID: name + "-id", Names: []string{"/" + name}, State: "running", Labels: labels, Ports: ports}
|
|
}
|
|
|
|
func pub(private, public uint16) docker.Port {
|
|
return docker.Port{IP: "0.0.0.0", PrivatePort: private, PublicPort: public, Type: "tcp"}
|
|
}
|
|
|
|
func wpLabels() map[string]string {
|
|
return map[string]string{
|
|
"traefik.enable": "true",
|
|
"traefik.http.routers.wp.rule": "Host(`kunde.de`)",
|
|
"traefik.http.services.wp.loadbalancer.server.port": "80",
|
|
}
|
|
}
|
|
|
|
func TestWorkloads_TranslatesPublishedPort(t *testing.T) {
|
|
in := ctr("wordpress", wpLabels(), pub(80, 8081), pub(443, 8443))
|
|
got, warnings := Workloads([]docker.Container{in}, "100.64.0.42")
|
|
if len(warnings) != 0 {
|
|
t.Errorf("Warnungen: %v", warnings)
|
|
}
|
|
want := []registry.Workload{{
|
|
Name: "wordpress", Host: "100.64.0.42",
|
|
Labels: map[string]string{
|
|
"traefik.enable": "true",
|
|
"traefik.http.routers.wp.rule": "Host(`kunde.de`)",
|
|
"traefik.http.services.wp.loadbalancer.server.port": "8081",
|
|
},
|
|
}}
|
|
if !reflect.DeepEqual(got, want) {
|
|
t.Errorf("Workloads =\n%+v\nerwartet\n%+v", got, want)
|
|
}
|
|
if in.Labels["traefik.http.services.wp.loadbalancer.server.port"] != "80" {
|
|
t.Error("Eingabe-Labels wurden verändert")
|
|
}
|
|
}
|
|
|
|
func TestWorkloads_SkipsDisabled(t *testing.T) {
|
|
off := wpLabels()
|
|
off["traefik.enable"] = "false"
|
|
none := wpLabels()
|
|
delete(none, "traefik.enable")
|
|
got, warnings := Workloads([]docker.Container{
|
|
ctr("aus", off, pub(80, 8081)),
|
|
ctr("ohne", none, pub(80, 8082)),
|
|
ctr("db", map[string]string{}, pub(5432, 5432)),
|
|
}, "h")
|
|
if len(got) != 0 || len(warnings) != 0 {
|
|
t.Errorf("got=%v warnings=%v", got, warnings)
|
|
}
|
|
}
|
|
|
|
func TestWorkloads_UnreachablePorts(t *testing.T) {
|
|
tests := map[string][]docker.Port{
|
|
"nicht veröffentlicht": {{PrivatePort: 80, Type: "tcp"}},
|
|
"nur IPv4-Loopback": {{IP: "127.0.0.1", PrivatePort: 80, PublicPort: 8081, Type: "tcp"}},
|
|
"nur IPv6-Loopback": {{IP: "::1", PrivatePort: 80, PublicPort: 8081, Type: "tcp"}},
|
|
"nur UDP": {{IP: "0.0.0.0", PrivatePort: 80, PublicPort: 8081, Type: "udp"}},
|
|
"anderer Port": {pub(8080, 8081)},
|
|
}
|
|
for name, ports := range tests {
|
|
got, warnings := Workloads([]docker.Container{ctr("wordpress", wpLabels(), ports...)}, "h")
|
|
if len(got) != 0 {
|
|
t.Errorf("%s: Workload trotz unerreichbarem Port: %+v", name, got)
|
|
}
|
|
if len(warnings) != 1 || !strings.Contains(warnings[0], "wordpress") || !strings.Contains(warnings[0], "80") {
|
|
t.Errorf("%s: Warnungen = %v", name, warnings)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestWorkloads_IPv6AnyIsReachable(t *testing.T) {
|
|
got, _ := Workloads([]docker.Container{ctr("wordpress", wpLabels(),
|
|
docker.Port{IP: "::", PrivatePort: 80, PublicPort: 8081, Type: "tcp"})}, "h")
|
|
if len(got) != 1 || got[0].Labels["traefik.http.services.wp.loadbalancer.server.port"] != "8081" {
|
|
t.Errorf("got = %+v", got)
|
|
}
|
|
}
|
|
|
|
// Wie Traefiks Docker-Provider: ohne Service-Labels gibt es einen Service
|
|
// mit dem Containernamen und dem einzigen veröffentlichten Port.
|
|
func TestWorkloads_DefaultService(t *testing.T) {
|
|
labels := map[string]string{"traefik.enable": "true", "traefik.http.routers.app.rule": "Host(`app.kunde.de`)"}
|
|
got, warnings := Workloads([]docker.Container{ctr("my.app", labels, pub(3000, 13000))}, "h")
|
|
if len(warnings) != 0 || len(got) != 1 {
|
|
t.Fatalf("got=%+v warnings=%v", got, warnings)
|
|
}
|
|
if got[0].Labels["traefik.http.services.my-app.loadbalancer.server.port"] != "13000" {
|
|
t.Errorf("Default-Service fehlt: %v", got[0].Labels)
|
|
}
|
|
|
|
_, warnings = Workloads([]docker.Container{ctr("multi", labels, pub(3000, 13000), pub(3001, 13001))}, "h")
|
|
if len(warnings) != 1 || !strings.Contains(warnings[0], "multi") {
|
|
t.Errorf("mehrere Ports ohne Port-Label: Warnungen = %v", warnings)
|
|
}
|
|
}
|
|
|
|
func TestWorkloads_ServiceWithoutPortLabel(t *testing.T) {
|
|
labels := map[string]string{
|
|
"traefik.enable": "true",
|
|
"traefik.http.routers.wp.rule": "Host(`kunde.de`)",
|
|
"traefik.http.services.wp.loadbalancer.passhostheader": "true",
|
|
}
|
|
got, warnings := Workloads([]docker.Container{ctr("wordpress", labels, pub(80, 8081))}, "h")
|
|
if len(warnings) != 0 || len(got) != 1 || got[0].Labels["traefik.http.services.wp.loadbalancer.server.port"] != "8081" {
|
|
t.Errorf("got=%+v warnings=%v", got, warnings)
|
|
}
|
|
}
|
|
|
|
func TestWorkloads_MultipleServicesAndCaseInsensitiveKeys(t *testing.T) {
|
|
labels := map[string]string{
|
|
"Traefik.Enable": "TRUE",
|
|
"traefik.http.routers.web.rule": "Host(`kunde.de`)",
|
|
"traefik.http.routers.web.service": "web",
|
|
"traefik.http.routers.api.rule": "Host(`api.kunde.de`)",
|
|
"traefik.http.routers.api.service": "api",
|
|
"traefik.http.services.web.loadbalancer.server.port": "80",
|
|
"Traefik.HTTP.Services.Api.LoadBalancer.Server.Port": "9000",
|
|
}
|
|
got, warnings := Workloads([]docker.Container{ctr("app", labels, pub(80, 8080), pub(9000, 19000))}, "h")
|
|
if len(warnings) != 0 || len(got) != 1 {
|
|
t.Fatalf("got=%+v warnings=%v", got, warnings)
|
|
}
|
|
l := got[0].Labels
|
|
if l["traefik.http.services.web.loadbalancer.server.port"] != "8080" || l["Traefik.HTTP.Services.Api.LoadBalancer.Server.Port"] != "19000" {
|
|
t.Errorf("Labels = %v", l)
|
|
}
|
|
}
|
|
|
|
func TestWorkloads_SortedByName(t *testing.T) {
|
|
got, _ := Workloads([]docker.Container{
|
|
ctr("b", wpLabels(), pub(80, 8082)),
|
|
ctr("a", wpLabels(), pub(80, 8081)),
|
|
}, "h")
|
|
if len(got) != 2 || got[0].Name != "a" || got[1].Name != "b" {
|
|
t.Errorf("Reihenfolge: %+v", got)
|
|
}
|
|
}
|