Переглянути джерело

Finish redoing zone_test.go

Ask Bjørn Hansen 7 роки тому
батько
коміт
7e7922be10
5 змінених файлів з 127 додано та 89 видалено
  1. 1 3
      dns/test.example.com.json
  2. 1 1
      http_test.go
  3. 3 6
      zones/reader_test.go
  4. 11 1
      zones/zone.go
  5. 111 78
      zones/zone_test.go

+ 1 - 3
dns/test.example.com.json

@@ -2,8 +2,6 @@
   "ttl":    600,
   "max_hosts": 2,
   "logging": {
-    "stathat": true,
-    "stathat_api": "abc-test"
   },
   "targeting": "country continent @ regiongroup region ip asn",
   "contact": "support.bitnames.com",
@@ -23,7 +21,7 @@
     },
     "weight": {
       "a": [ [ "192.168.1.2", 100 ], [ "192.168.1.3", 50 ], [ "192.168.1.4", 25 ] ],
-      "txt": [ { "txt": "w1000", "weight": 1000 },
+      "txt": [ { "txt": "w10000", "weight": 10000 },
                { "txt": "w1",   "weight": 1 }
              ],
       "max_hosts": "1"

+ 1 - 1
http_test.go

@@ -17,7 +17,7 @@ import (
 
 func TestHTTP(t *testing.T) {
 
-	geoprovider, err := geoip2.New("/usr/local/share/GeoIP")
+	geoprovider, err := geoip2.New(geoip2.FindDB())
 	if err == nil {
 		targeting.Setup(geoprovider)
 	}

+ 3 - 6
zones/reader_test.go

@@ -111,12 +111,10 @@ func TestRemoveConfig(t *testing.T) {
 
 	muxm.reload()
 	if muxm.zonelist["test.example.org"].Origin != "test.example.org" {
-		t.Log("test.example.org has unexpected Origin: '%s'", muxm.zonelist["test.example.org"].Origin)
-		t.Fail()
+		t.Errorf("test.example.org has unexpected Origin: '%s'", muxm.zonelist["test.example.org"].Origin)
 	}
 	if muxm.zonelist["test2.example.org"].Origin != "test2.example.org" {
-		t.Log("test2.example.org has unexpected Origin: '%s'", muxm.zonelist["test2.example.org"].Origin)
-		t.Fail()
+		t.Errorf("test2.example.org has unexpected Origin: '%s'", muxm.zonelist["test2.example.org"].Origin)
 	}
 
 	os.Remove(dir + "/test2.example.org.json")
@@ -125,8 +123,7 @@ func TestRemoveConfig(t *testing.T) {
 	muxm.reload()
 
 	if muxm.zonelist["test.example.org"].Origin != "test.example.org" {
-		t.Log("test.example.org has unexpected Origin: '%s'", muxm.zonelist["test.example.org"].Origin)
-		t.Fail()
+		t.Errorf("test.example.org has unexpected Origin: '%s'", muxm.zonelist["test.example.org"].Origin)
 	}
 	_, ok := muxm.zonelist["test2.example.org"]
 	if ok != false {

+ 11 - 1
zones/zone.go

@@ -214,11 +214,21 @@ func (zone *Zone) addSOA() {
 	label.Records[dns.TypeSOA][0] = &record
 }
 
+func (z *Zone) findFirstLabel(s string, targets []string, qts []uint16) *LabelMatch {
+	matches := z.FindLabels(s, targets, qts)
+	if len(matches) == 0 {
+		return nil
+	}
+	return &matches[0]
+}
+
 // Find label "s" in country "cc" falling back to the appropriate
 // continent and the global label name as needed. Looks for the
 // first available qType at each targeting level. Returns a list of
 // LabelMatch for potential labels that might satisfy the query.
-// "MF" records are treated as aliases.
+// "MF" records are treated as aliases. The API returns all the
+// matches the targeting will allow so health check filtering won't
+// filter out the "best" results leaving no others.
 func (z *Zone) FindLabels(s string, targets []string, qts []uint16) []LabelMatch {
 
 	matches := make([]LabelMatch, 0)

+ 111 - 78
zones/zone_test.go

@@ -1,6 +1,7 @@
 package zones
 
 import (
+	"regexp"
 	"testing"
 
 	"github.com/miekg/dns"
@@ -24,90 +25,125 @@ func TestExampleComZone(t *testing.T) {
 	}
 
 	// Make sure that the empty "no.bar" zone gets skipped and "bar" is used
-	matches := ex.FindLabels("bar", []string{"no", "europe", "@"}, []uint16{dns.TypeA})
-	label := matches[0].Label
-	qtype := matches[0].Type
-	if l := len(label.Records[dns.TypeA]); l != 1 {
+	m := ex.findFirstLabel("bar", []string{"no", "europe", "@"}, []uint16{dns.TypeA})
+	if l := len(m.Label.Records[dns.TypeA]); l != 1 {
 		t.Logf("Unexpected number of A records: '%d'", l)
 		t.Fail()
 	}
-	if qtype != dns.TypeA {
-		t.Fatalf("Expected qtype = A record (type %d), got type %d", dns.TypeA, qtype)
+	if m.Type != dns.TypeA {
+		t.Fatalf("Expected qtype = A record (type %d), got type %d", dns.TypeA, m.Type)
 	}
-	if str := label.Records[qtype][0].RR.(*dns.A).A.String(); str != "192.168.1.2" {
-		t.Logf("Got A '%s', expected '%s'", str, "192.168.1.2")
-		t.Fail()
+	if str := m.Label.Records[m.Type][0].RR.(*dns.A).A.String(); str != "192.168.1.2" {
+		t.Errorf("Got A '%s', expected '%s'", str, "192.168.1.2")
 	}
 
-	// label, qtype = ex.FindLabels("", []string{"@"}, []uint16{dns.TypeMX})
-	// Mxs := label.Records[dns.TypeMX]
-	// c.Check(Mxs, HasLen, 2)
-	// c.Check(Mxs[0].RR.(*dns.MX).Mx, Equals, "mx.example.net.")
-	// c.Check(Mxs[1].RR.(*dns.MX).Mx, Equals, "mx2.example.net.")
+	m = ex.findFirstLabel("", []string{"@"}, []uint16{dns.TypeMX})
+
+	Mx := m.Label.Records[dns.TypeMX]
+	if len(Mx) != 2 {
+		t.Errorf("Expected 2 MX records but got %d", len(Mx))
+	}
+	if Mx[0].RR.(*dns.MX).Mx != "mx.example.net." {
+		t.Errorf("First MX should have been mx.example.net, but was %s", Mx[0].RR.(*dns.MX).Mx)
+	}
 
-	// label, qtype = ex.FindLabels("", []string{"dk", "europe", "@"}, []uint16{dns.TypeMX})
-	// Mxs = label.Records[dns.TypeMX]
-	// c.Check(Mxs, HasLen, 1)
-	// c.Check(Mxs[0].RR.(*dns.MX).Mx, Equals, "mx-eu.example.net.")
-	// c.Check(qtype, Equals, dns.TypeMX)
+	m = ex.findFirstLabel("", []string{"dk", "europe", "@"}, []uint16{dns.TypeMX})
+	Mx = m.Label.Records[dns.TypeMX]
+	if len(Mx) != 1 {
+		t.Errorf("Got %d MX record for dk,europe,@ - expected %d", len(Mx), 1)
+	}
+	if Mx[0].RR.(*dns.MX).Mx != "mx-eu.example.net." {
+		t.Errorf("First MX should have been mx-eu.example.net, but was %s", Mx[0].RR.(*dns.MX).Mx)
+	}
 
 	// // look for multiple record types
-	// label, qtype = ex.FindLabels("www", []string{"@"}, []uint16{dns.TypeCNAME, dns.TypeA})
-	// c.Check(label.Records[dns.TypeCNAME], HasLen, 1)
-	// c.Check(qtype, Equals, dns.TypeCNAME)
-
-	// // pretty.Println(ex.Labels[""].Records[dns.TypeNS])
-
-	// label, qtype = ex.FindLabels("", []string{"@"}, []uint16{dns.TypeNS})
-	// Ns := label.Records[dns.TypeNS]
-	// c.Check(Ns, HasLen, 2)
-	// // Test that we get the expected NS records (in any order because
-	// // of the configuration format used for this zone)
-	// c.Check(Ns[0].RR.(*dns.NS).Ns, Matches, "^ns[12]\\.example\\.net.$")
-	// c.Check(Ns[1].RR.(*dns.NS).Ns, Matches, "^ns[12]\\.example\\.net.$")
-
-	// label, qtype = ex.FindLabels("", []string{"@"}, []uint16{dns.TypeSPF})
-	// Spf := label.Records[dns.TypeSPF]
-	// c.Check(Spf, HasLen, 1)
-	// c.Check(Spf[0].RR.(*dns.SPF).Txt[0], Equals, "v=spf1 ~all")
-
-	// label, qtype = ex.FindLabels("foo", []string{"@"}, []uint16{dns.TypeTXT})
-	// Txt := label.Records[dns.TypeTXT]
-	// c.Check(Txt, HasLen, 1)
-	// c.Check(Txt[0].RR.(*dns.TXT).Txt[0], Equals, "this is foo")
-
-	// label, qtype = ex.FindLabels("weight", []string{"@"}, []uint16{dns.TypeTXT})
-	// Txt = label.Records[dns.TypeTXT]
-	// c.Check(Txt, HasLen, 2)
-	// c.Check(Txt[0].RR.(*dns.TXT).Txt[0], Equals, "w1000")
-	// c.Check(Txt[1].RR.(*dns.TXT).Txt[0], Equals, "w1")
-
-	// //verify empty labels are created
-	// label, qtype = ex.FindLabels("a.b.c", []string{"@"}, []uint16{dns.TypeA})
-	// c.Check(label.Records[dns.TypeA], HasLen, 1)
-	// c.Check(label.Records[dns.TypeA][0].RR.(*dns.A).A.String(), Equals, "192.168.1.7")
-
-	// label, qtype = ex.FindLabels("b.c", []string{"@"}, []uint16{dns.TypeA})
-	// c.Check(label.Records[dns.TypeA], HasLen, 0)
-	// c.Check(label.Label, Equals, "b.c")
-
-	// label, qtype = ex.FindLabels("c", []string{"@"}, []uint16{dns.TypeA})
-	// c.Check(label.Records[dns.TypeA], HasLen, 0)
-	// c.Check(label.Label, Equals, "c")
-
-	// //verify label is created
-	// label, qtype = ex.FindLabels("three.two.one", []string{"@"}, []uint16{dns.TypeA})
-	// c.Check(label.Records[dns.TypeA], HasLen, 1)
-	// c.Check(label.Records[dns.TypeA][0].RR.(*dns.A).A.String(), Equals, "192.168.1.5")
-
-	// label, qtype = ex.FindLabels("two.one", []string{"@"}, []uint16{dns.TypeA})
-	// c.Check(label.Records[dns.TypeA], HasLen, 0)
-	// c.Check(label.Label, Equals, "two.one")
-
-	// //verify label isn't overwritten
-	// label, qtype = ex.FindLabels("one", []string{"@"}, []uint16{dns.TypeA})
-	// c.Check(label.Records[dns.TypeA], HasLen, 1)
-	// c.Check(label.Records[dns.TypeA][0].RR.(*dns.A).A.String(), Equals, "192.168.1.6")
+	m = ex.findFirstLabel("www", []string{"@"}, []uint16{dns.TypeCNAME, dns.TypeA})
+	if m.Type != dns.TypeCNAME {
+		t.Errorf("www should have been a CNAME, but was a %s", dns.TypeToString[m.Type])
+	}
+
+	m = ex.findFirstLabel("", []string{"@"}, []uint16{dns.TypeNS})
+	Ns := m.Label.Records[dns.TypeNS]
+	if len(Ns) != 2 {
+		t.Errorf("root should have returned 2 NS records but got %d", len(Ns))
+	}
+
+	// Test that we get the expected NS records (in any order because
+	// of the configuration format used for this zone)
+	for i := 0; i < 2; i++ {
+		if matched, err := regexp.MatchString("^ns[12]\\.example\\.net.$", Ns[i].RR.(*dns.NS).Ns); err != nil || !matched {
+			if err != nil {
+				t.Fatal(err)
+			}
+			t.Errorf("Unexpected NS record data '%s'", Ns[i].RR.(*dns.NS).Ns)
+		}
+	}
+
+	m = ex.findFirstLabel("", []string{"@"}, []uint16{dns.TypeSPF})
+	Spf := m.Label.Records[dns.TypeSPF]
+	if txt := Spf[0].RR.(*dns.SPF).Txt[0]; txt != "v=spf1 ~all" {
+		t.Errorf("Wrong SPF data '%s'", txt)
+	}
+
+	m = ex.findFirstLabel("foo", []string{"@"}, []uint16{dns.TypeTXT})
+	Txt := m.Label.Records[dns.TypeTXT]
+	if txt := Txt[0].RR.(*dns.TXT).Txt[0]; txt != "this is foo" {
+		t.Errorf("Wrong TXT data '%s'", txt)
+	}
+
+	m = ex.findFirstLabel("weight", []string{"@"}, []uint16{dns.TypeTXT})
+	Txt = m.Label.Records[dns.TypeTXT]
+
+	txts := []string{"w10000", "w1"}
+	for i, r := range Txt {
+		if txt := r.RR.(*dns.TXT).Txt[0]; txt != txts[i] {
+			t.Errorf("txt record %d was '%s', expected '%s'", i, txt, txts[i])
+		}
+	}
+
+	// verify empty labels are created
+	m = ex.findFirstLabel("a.b.c", []string{"@"}, []uint16{dns.TypeA})
+	if a := m.Label.Records[dns.TypeA][0].RR.(*dns.A); a.A.String() != "192.168.1.7" {
+		t.Errorf("unexpected IP for a.b.c '%s'", a)
+	}
+
+	emptyLabels := []string{"b.c", "c"}
+	for _, el := range emptyLabels {
+		m = ex.findFirstLabel(el, []string{"@"}, []uint16{dns.TypeA})
+		if len(m.Label.Records[dns.TypeA]) > 0 {
+			t.Errorf("Unexpected A record for '%s'", el)
+		}
+		if m.Label.Label != el {
+			t.Errorf("'%s' label is '%s'", el, m.Label.Label)
+		}
+	}
+
+	//verify label is created
+	m = ex.findFirstLabel("three.two.one", []string{"@"}, []uint16{dns.TypeA})
+	if l := len(m.Label.Records[dns.TypeA]); l != 1 {
+		t.Errorf("Unexpected A record count for 'three.two.one' %d, expected 1", l)
+	}
+	if a := m.Label.Records[dns.TypeA][0].RR.(*dns.A); a.A.String() != "192.168.1.5" {
+		t.Errorf("unexpected IP for three.two.one '%s'", a)
+	}
+
+	el := "two.one"
+	m = ex.findFirstLabel(el, []string{"@"}, []uint16{dns.TypeA})
+	if len(m.Label.Records[dns.TypeA]) > 0 {
+		t.Errorf("Unexpected A record for '%s'", el)
+	}
+	if m.Label.Label != el {
+		t.Errorf("'%s' label is '%s'", el, m.Label.Label)
+	}
+
+	//verify label isn't overwritten
+	m = ex.findFirstLabel("one", []string{"@"}, []uint16{dns.TypeA})
+	if l := len(m.Label.Records[dns.TypeA]); l != 1 {
+		t.Errorf("Unexpected A record count for 'one' %d, expected 1", l)
+	}
+	if a := m.Label.Records[dns.TypeA][0].RR.(*dns.A); a.A.String() != "192.168.1.6" {
+		t.Errorf("unexpected IP for one '%s'", a)
+	}
 }
 
 func TestExampleOrgZone(t *testing.T) {
@@ -130,7 +166,4 @@ func TestExampleOrgZone(t *testing.T) {
 	if l := len(Ns); l != 2 {
 		t.Fatalf("Expected 2 NS records, got '%d'", l)
 	}
-	// c.Check(Ns[0].RR.(*dns.NS).Ns, Equals, "ns1.example.com.")
-	// c.Check(Ns[1].RR.(*dns.NS).Ns, Equals, "ns2.example.com.")
-
 }