From 663f10c34daed200c6cfd2f27c9adc94907bf1ae Mon Sep 17 00:00:00 2001 From: Miek Gieben Date: Sat, 25 Jan 2020 08:19:00 +0100 Subject: [PATCH] add locality tests Signed-off-by: Miek Gieben --- plugin/traffic/traffic_test.go | 29 ++++++++++------------------- 1 file changed, 10 insertions(+), 19 deletions(-) diff --git a/plugin/traffic/traffic_test.go b/plugin/traffic/traffic_test.go index 8404894af..50e0152cf 100644 --- a/plugin/traffic/traffic_test.go +++ b/plugin/traffic/traffic_test.go @@ -161,12 +161,9 @@ func TestTrafficLocality(t *testing.T) { tr := &Traffic{c: c, origins: []string{"lb.example.org."}} tests := []struct { - cla *xdspb.ClusterLoadAssignment - cluster string - loc xds.Locality // where we run - qtype uint16 - rcode int - answer int // number of records in answer section + cla *xdspb.ClusterLoadAssignment + loc xds.Locality // where we run + answer string }{ { cla: &xdspb.ClusterLoadAssignment{ @@ -177,13 +174,12 @@ func TestTrafficLocality(t *testing.T) { {"127.0.0.2", 18008, corepb.HealthStatus_HEALTHY}}, xds.Locality{Region: "us"}), endpointsWithLocality([]EndpointHealth{ - {"127.0.1.1", 18008, corepb.HealthStatus_HEALTHY}, - {"127.0.1.2", 18008, corepb.HealthStatus_HEALTHY}}, + {"127.0.1.1", 18008, corepb.HealthStatus_HEALTHY}}, xds.Locality{Region: "eu"})..., ), }, - cluster: "web", qtype: dns.TypeA, rcode: dns.RcodeSuccess, answer: 2, - loc: xds.Locality{Region: "eu"}, // our location + answer: "127.0.1.1", + loc: xds.Locality{Region: "eu"}, // our location }, } @@ -191,23 +187,18 @@ func TestTrafficLocality(t *testing.T) { for i, tc := range tests { a := xds.NewAssignment() - a.SetClusterLoadAssignment("web", tc.cla) // web is our cluster + a.SetClusterLoadAssignment("web", tc.cla) c.SetAssignments(a) - m := new(dns.Msg) - cl := dnsutil.Join(tc.cluster, tr.origins[0]) - m.SetQuestion(cl, tc.qtype) + m := new(dns.Msg).SetQuestion(dnsutil.Join("web", tr.origins[0]), dns.TypeA) rec := dnstest.NewRecorder(&test.ResponseWriter{}) _, err := tr.ServeDNS(ctx, rec, m) if err != nil { t.Errorf("Test %d: Expected no error, but got %q", i, err) } - if rec.Msg.Rcode != tc.rcode { - t.Errorf("Test %d: Expected no rcode %d, but got %d", i, tc.rcode, rec.Msg.Rcode) - } - if tc.answer != len(rec.Msg.Answer) { - t.Fatalf("Test %d: Expected %d answers, but got %d", i, tc.answer, len(rec.Msg.Answer)) + if x := rec.Msg.Answer[0].(*dns.A).A.String(); x != tc.answer { + t.Fatalf("Test %d: Expected %s, but got %s", i, tc.answer, x) } } }