func Test_RRs_actualWorkload(t *testing.T) { rr := RRs("domain", task.ClusterState([]task.Task{ { Id: "bind", Service: "dns", Domain: "infra", Ports: []task.Port{{net.IPv4(192, 168, 0, 3), 53, "udp"}}, }, { Id: "web1", Service: "api", Ports: []task.Port{{net.IPv4(192, 168, 0, 1), 8000, "tcp"}}, }, { Id: "web2", Service: "api", Ports: []task.Port{ {net.IPv4(192, 168, 0, 2), 8000, "tcp"}, {net.IPv4(192, 168, 0, 2), 5000, "udp"}, }, }, { Id: "nginx", Service: "frontend", Domain: "blog", Ports: []task.Port{ {net.IPv4(192, 168, 0, 3), 8000, "tcp"}, }, }, { // no proto on port Id: "debian", Service: "test", Ports: []task.Port{{net.IPv4(192, 168, 0, 3), 500, ""}}, }, { // no service name Id: "debian", Ports: []task.Port{{net.IPv4(192, 168, 0, 3), 500, "udp"}}, }, })) expected := rrstore.RRs(map[uint16]map[string][]string{ dns.TypeA: { "dns.infra.domain.": []string{"192.168.0.3"}, "api.domain.": []string{"192.168.0.1", "192.168.0.2"}, "frontend.blog.domain.": []string{"192.168.0.3"}, }, dns.TypeSRV: { "_dns._udp.infra.domain.": []string{"192.168.0.3:53"}, "_api._tcp.domain.": []string{"192.168.0.1:8000", "192.168.0.2:8000"}, "_api._udp.domain.": []string{"192.168.0.2:5000"}, "_frontend._tcp.blog.domain.": []string{"192.168.0.3:8000"}, }}) if !reflect.DeepEqual(rr, expected) { t.Fatalf("wrong value.\nexp: %#v\ngot: %#v", expected, rr) } }
func Test_RRs_empty(t *testing.T) { rr := getRRs("domain", nil) if len(rr) > 0 { t.Fatal("output has records") } rr = RRs("domain", task.ClusterState([]task.Task{ { Id: "no-ports", Service: "api", Ports: []task.Port{}, }, { Id: "no-service-name", Ports: []task.Port{{net.IPv4(10, 0, 0, 2), 8001, "tcp"}}, }, })) if len(rr) > 0 { t.Fatal("output has records") } }
func TestGetTasks(t *testing.T) { srv := testServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { // implement partial /info endpoint b := `[ { "Id": "nginx", "Labels": { "dns.domain": "bilLING", "dns.service": "API" }, "Ports": [ { "IP": "192.168.99.103", "PrivatePort": 80, "PublicPort": 8000, "Type": "tcp" }, { "IP": "", "PrivatePort": 443, "PublicPort": 0, "Type": "tcp" } ] }, { "Id": "no-ports-but-has-labels", "Labels": { "dns.domain": "billing", "dns.service": "db" } } ]` w.Write([]byte(b)) })) defer srv.Close() sw, err := New(srv.URL, nil) if err != nil { t.Fatal(err) } out, err := sw.Tasks() if err != nil { t.Fatal(err) } expected := task.ClusterState([]task.Task{ { Id: "nginx", Service: "api", Domain: "billing", Ports: []task.Port{{ HostIP: net.IPv4(192, 168, 99, 103), HostPort: 8000, Proto: "tcp", }}, }, { Id: "no-ports-but-has-labels", Service: "db", Domain: "billing", Ports: []task.Port{}, }, }) if !reflect.DeepEqual(out, expected) { t.Fatalf("got wrong value.\nexpected: %#v\ngot:%#v", expected, out) } }