mirror of
https://github.com/coredns/coredns.git
synced 2025-10-27 08:14:18 -04:00
Add TestServer (#102)
Add a fullblown testing server. This allows us to do integration tests. Also add a basic proxy test. Further tests will test etcd proxy and stub zone communication and other "wildish" configurations. Redo the server startup, so we can access the ports it listens on when it has started up (with dns.ActivateAndServer). Extend the .travis file to download etcd and test for that as well. Put integration tests in test dir
This commit is contained in:
11
test/etcd_test.go
Normal file
11
test/etcd_test.go
Normal file
@@ -0,0 +1,11 @@
|
||||
// +build etcd
|
||||
|
||||
package test
|
||||
|
||||
import "testing"
|
||||
|
||||
// This test starts two coredns servers (and needs etcd). Configure a stubzones in both (that will loop) and
|
||||
// will then test if we detect this loop.
|
||||
func TestEtcdStubForwarding(t *testing.T) {
|
||||
// TODO(miek)
|
||||
}
|
||||
53
test/server_test.go
Normal file
53
test/server_test.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/miekg/dns"
|
||||
)
|
||||
|
||||
// Start 2 tests server, server A will proxy to B, server B is an CH server.
|
||||
func TestProxyToChaosServer(t *testing.T) {
|
||||
corefile := `.:0 {
|
||||
chaos CoreDNS-001 miek@miek.nl
|
||||
}
|
||||
`
|
||||
chaos, tcpCH, udpCH, err := testServer(t, corefile)
|
||||
if err != nil {
|
||||
t.Fatalf("Could get server: %s", err)
|
||||
}
|
||||
defer chaos.Stop()
|
||||
|
||||
corefileProxy := `.:0 {
|
||||
proxy . ` + udpCH + `
|
||||
}
|
||||
`
|
||||
proxy, _, udp, err := testServer(t, corefileProxy)
|
||||
if err != nil {
|
||||
t.Fatalf("Could get server: %s", err)
|
||||
}
|
||||
defer proxy.Stop()
|
||||
|
||||
chaosTest(t, udpCH, "udp")
|
||||
chaosTest(t, tcpCH, "tcp")
|
||||
|
||||
chaosTest(t, udp, "udp")
|
||||
// chaosTest(t, tcp, "tcp"), commented out because we use the original transport to reach the
|
||||
// proxy and we only forward to the udp port.
|
||||
}
|
||||
|
||||
func chaosTest(t *testing.T, server, net string) {
|
||||
m := testMsg("version.bind.", dns.TypeTXT, nil)
|
||||
m.Question[0].Qclass = dns.ClassCHAOS
|
||||
|
||||
r, err := testExchange(m, server, net)
|
||||
if err != nil {
|
||||
t.Fatalf("Could not send message: %s", err)
|
||||
}
|
||||
if r.Rcode != dns.RcodeSuccess || len(r.Answer) == 0 {
|
||||
t.Fatalf("Expected successful reply on %s, got %s", net, dns.RcodeToString[r.Rcode])
|
||||
}
|
||||
if r.Answer[0].String() != `version.bind. 0 CH TXT "CoreDNS-001"` {
|
||||
t.Fatalf("Expected version.bind. reply, got %s", r.Answer[0].String())
|
||||
}
|
||||
}
|
||||
40
test/tests.go
Normal file
40
test/tests.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/miekg/coredns/core"
|
||||
"github.com/miekg/coredns/middleware"
|
||||
"github.com/miekg/coredns/server"
|
||||
|
||||
"github.com/miekg/dns"
|
||||
)
|
||||
|
||||
func testMsg(zone string, typ uint16, o *dns.OPT) *dns.Msg {
|
||||
m := new(dns.Msg)
|
||||
m.SetQuestion(zone, typ)
|
||||
if o != nil {
|
||||
m.Extra = []dns.RR{o}
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
func testExchange(m *dns.Msg, server, net string) (*dns.Msg, error) {
|
||||
c := new(dns.Client)
|
||||
c.Net = net
|
||||
return middleware.Exchange(c, m, server)
|
||||
}
|
||||
|
||||
// testServer returns a test server and the tcp and udp listeners addresses.
|
||||
func testServer(t *testing.T, corefile string) (*server.Server, string, string, error) {
|
||||
srv, err := core.TestServer(t, corefile)
|
||||
if err != nil {
|
||||
return nil, "", "", err
|
||||
}
|
||||
go srv.ListenAndServe()
|
||||
|
||||
time.Sleep(1 * time.Second)
|
||||
tcp, udp := srv.LocalAddr()
|
||||
return srv, tcp.String(), udp.String(), nil
|
||||
}
|
||||
Reference in New Issue
Block a user