Add tests for server.go
This commit is contained in:
1 parent
bd2844d186
commit
f24edf755f
2 files changed
+97
No files matched your search
@@ -30,6 +30,18 @@ func AssertNoErr(t *testing.T, err error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func AssertEqualInt(t *testing.T, got, wanted int, desc string) {
|
||||||
|
if got != wanted {
|
||||||
|
t.Fatalf("%s: Expected %s %d but got %d", CallSite(), desc, wanted, got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func AssertEqualString(t *testing.T, got, wanted string, desc string) {
|
||||||
|
if got != wanted {
|
||||||
|
t.Fatalf("%s: Expected %s '%s' but got '%s'", CallSite(), desc, wanted, got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func AssertStatus(t *testing.T, got int, wanted int, desc string) {
|
func AssertStatus(t *testing.T, got int, wanted int, desc string) {
|
||||||
if got != wanted {
|
if got != wanted {
|
||||||
t.Fatalf("%s: Expected %s %d but got %d", CallSite(), desc, wanted, got)
|
t.Fatalf("%s: Expected %s %d but got %d", CallSite(), desc, wanted, got)
|
||||||
@@ -49,3 +61,10 @@ func AssertErrorType(t *testing.T, got interface{}, wanted interface{}, desc str
|
|||||||
t.Fatalf("%s: Expected %s but got %s (%s)", CallSite(), wantedT.String(), gotT.String(), desc)
|
t.Fatalf("%s: Expected %s but got %s (%s)", CallSite(), wantedT.String(), gotT.String(), desc)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func AssertType(t *testing.T, got interface{}, wanted interface{}, desc string) {
|
||||||
|
gotT, wantedT := reflect.TypeOf(got), reflect.TypeOf(wanted)
|
||||||
|
if gotT != wantedT {
|
||||||
|
t.Fatalf("%s: Expected %s but got %s (%s)", CallSite(), wantedT.String(), gotT.String(), desc)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,78 @@
|
|||||||
|
package nameserver
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/miekg/dns"
|
||||||
|
wt "github.com/zettio/weave/common"
|
||||||
|
"net"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestDNSServer(t *testing.T) {
|
||||||
|
const (
|
||||||
|
port = 17625
|
||||||
|
successTestName = "test1.weave.local."
|
||||||
|
failTestName = "test2.weave.local."
|
||||||
|
nonLocalName = "weave.works."
|
||||||
|
testAddr1 = "10.0.2.1"
|
||||||
|
testRDNSsuccess = "1.2.0.10.in-addr.arpa."
|
||||||
|
testRDNSfail = "4.3.2.1.in-addr.arpa."
|
||||||
|
)
|
||||||
|
dnsAddr := fmt.Sprintf("localhost:%d", port)
|
||||||
|
testCIDR1 := testAddr1 + "/24"
|
||||||
|
|
||||||
|
wt.InitDefaultLogging(true)
|
||||||
|
var zone = new(ZoneDb)
|
||||||
|
ip, _, _ := net.ParseCIDR(testCIDR1)
|
||||||
|
zone.AddRecord(containerID, successTestName, ip)
|
||||||
|
|
||||||
|
go StartServer(zone, nil, port, 0)
|
||||||
|
time.Sleep(100 * time.Millisecond) // Allow sever goroutine to start
|
||||||
|
|
||||||
|
c := new(dns.Client)
|
||||||
|
m := new(dns.Msg)
|
||||||
|
m.SetQuestion(successTestName, dns.TypeA)
|
||||||
|
m.RecursionDesired = true
|
||||||
|
r, _, err := c.Exchange(m, dnsAddr)
|
||||||
|
wt.AssertNoErr(t, err)
|
||||||
|
wt.AssertStatus(t, r.Rcode, dns.RcodeSuccess, "DNS response code")
|
||||||
|
wt.AssertEqualInt(t, len(r.Answer), 1, "Number of answers")
|
||||||
|
wt.AssertType(t, r.Answer[0], (*dns.A)(nil), "DNS record")
|
||||||
|
wt.AssertEqualString(t, r.Answer[0].(*dns.A).A.String(), testAddr1, "IP address")
|
||||||
|
|
||||||
|
m.SetQuestion(failTestName, dns.TypeA)
|
||||||
|
r, _, err = c.Exchange(m, dnsAddr)
|
||||||
|
wt.AssertNoErr(t, err)
|
||||||
|
wt.AssertStatus(t, r.Rcode, dns.RcodeNameError, "DNS response code")
|
||||||
|
wt.AssertEqualInt(t, len(r.Answer), 0, "Number of answers")
|
||||||
|
|
||||||
|
m.SetQuestion(testRDNSsuccess, dns.TypePTR)
|
||||||
|
r, _, err = c.Exchange(m, dnsAddr)
|
||||||
|
wt.AssertNoErr(t, err)
|
||||||
|
wt.AssertStatus(t, r.Rcode, dns.RcodeSuccess, "DNS response code")
|
||||||
|
wt.AssertEqualInt(t, len(r.Answer), 1, "Number of answers")
|
||||||
|
wt.AssertType(t, r.Answer[0], (*dns.PTR)(nil), "DNS record")
|
||||||
|
wt.AssertEqualString(t, r.Answer[0].(*dns.PTR).Ptr, successTestName, "IP address")
|
||||||
|
m.SetQuestion(testRDNSfail, dns.TypePTR)
|
||||||
|
r, _, err = c.Exchange(m, dnsAddr)
|
||||||
|
wt.AssertNoErr(t, err)
|
||||||
|
wt.AssertStatus(t, r.Rcode, dns.RcodeNameError, "DNS response code")
|
||||||
|
wt.AssertEqualInt(t, len(r.Answer), 0, "Number of answers")
|
||||||
|
|
||||||
|
// This should fail because we don't handle MX records
|
||||||
|
m.SetQuestion(successTestName, dns.TypeMX)
|
||||||
|
r, _, err = c.Exchange(m, dnsAddr)
|
||||||
|
wt.AssertNoErr(t, err)
|
||||||
|
wt.AssertStatus(t, r.Rcode, dns.RcodeNameError, "DNS response code")
|
||||||
|
wt.AssertEqualInt(t, len(r.Answer), 0, "Number of answers")
|
||||||
|
|
||||||
|
// This non-local query should fail because we don't handle MX records
|
||||||
|
m.SetQuestion(nonLocalName, dns.TypeMX)
|
||||||
|
r, _, err = c.Exchange(m, dnsAddr)
|
||||||
|
wt.AssertNoErr(t, err)
|
||||||
|
wt.AssertStatus(t, r.Rcode, dns.RcodeNameError, "DNS response code")
|
||||||
|
wt.AssertEqualInt(t, len(r.Answer), 0, "Number of answers")
|
||||||
|
|
||||||
|
// Not testing MDNS functionality of server here (yet)
|
||||||
|
}
|
||||||
Reference in new issue
Block a user