mirror of
https://github.com/coredns/coredns.git
synced 2025-10-27 08:14:18 -04:00
All (non etcd) tests are now local (#105)
We don't need to network to do tests, we up enough local servers to we don't need to forward to,s say 8.8.8.8
This commit is contained in:
@@ -41,7 +41,7 @@ func Etcd(c *Controller) (middleware.Middleware, error) {
|
||||
func etcdParse(c *Controller) (etcd.Etcd, bool, error) {
|
||||
stub := make(map[string]proxy.Proxy)
|
||||
etc := etcd.Etcd{
|
||||
Proxy: proxy.New([]string{"8.8.8.8:53"}),
|
||||
Proxy: proxy.New([]string{"8.8.8.8:53", "8.8.4.4:53"}),
|
||||
PathPrefix: "skydns",
|
||||
Ctx: context.Background(),
|
||||
Inflight: &singleflight.Group{},
|
||||
|
||||
@@ -1,36 +0,0 @@
|
||||
package proxy
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/miekg/coredns/middleware"
|
||||
"github.com/miekg/coredns/middleware/test"
|
||||
|
||||
"github.com/miekg/dns"
|
||||
)
|
||||
|
||||
func TestLookupProxy(t *testing.T) {
|
||||
// TODO(miek): make this fakeDNS backend and ask the question locally
|
||||
log.SetOutput(ioutil.Discard)
|
||||
defer log.SetOutput(os.Stderr)
|
||||
|
||||
p := New([]string{"8.8.8.8:53"})
|
||||
resp, err := p.Lookup(fakeState(), "example.org.", dns.TypeA)
|
||||
if err != nil {
|
||||
t.Error("Expected to receive reply, but didn't")
|
||||
}
|
||||
// expect answer section with A record in it
|
||||
if len(resp.Answer) == 0 {
|
||||
t.Error("Expected to at least one RR in the answer section, got none")
|
||||
}
|
||||
if resp.Answer[0].Header().Rrtype != dns.TypeA {
|
||||
t.Error("Expected RR to A, got: %d", resp.Answer[0].Header().Rrtype)
|
||||
}
|
||||
}
|
||||
|
||||
func fakeState() middleware.State {
|
||||
return middleware.State{W: &test.ResponseWriter{}, Req: new(dns.Msg)}
|
||||
}
|
||||
21
middleware/test/zone.go
Normal file
21
middleware/test/zone.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// Zone will create a temporary file on disk and returns the name and
|
||||
// cleanup function to remove it later.
|
||||
func Zone(t *testing.T, dir, zonefile string) (string, func(), error) {
|
||||
f, err := ioutil.TempFile(dir, "go-test-zone")
|
||||
if err != nil {
|
||||
return "", nil, err
|
||||
}
|
||||
if err := ioutil.WriteFile(f.Name(), []byte(zonefile), 0644); err != nil {
|
||||
return "", nil, err
|
||||
}
|
||||
rmFunc := func() { os.Remove(f.Name()) }
|
||||
return f.Name(), rmFunc, nil
|
||||
}
|
||||
59
test/proxy_test.go
Normal file
59
test/proxy_test.go
Normal file
@@ -0,0 +1,59 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/miekg/coredns/middleware"
|
||||
"github.com/miekg/coredns/middleware/proxy"
|
||||
"github.com/miekg/coredns/middleware/test"
|
||||
|
||||
"github.com/miekg/dns"
|
||||
)
|
||||
|
||||
const exampleOrg = `; example.org test file
|
||||
example.org. IN SOA sns.dns.icann.org. noc.dns.icann.org. 2015082541 7200 3600 1209600 3600
|
||||
example.org. IN NS b.iana-servers.net.
|
||||
example.org. IN NS a.iana-servers.net.
|
||||
example.org. IN A 127.0.0.1
|
||||
`
|
||||
|
||||
func TestLookupProxy(t *testing.T) {
|
||||
name, rm, err := test.Zone(t, ".", exampleOrg)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to created zone: %s", err)
|
||||
}
|
||||
defer rm()
|
||||
|
||||
corefile := `example.org:0 {
|
||||
file ` + name + `
|
||||
}
|
||||
`
|
||||
ex, _, udp, err := Server(t, corefile)
|
||||
if err != nil {
|
||||
t.Fatalf("Could get server: %s", err)
|
||||
}
|
||||
defer ex.Stop()
|
||||
|
||||
log.SetOutput(ioutil.Discard)
|
||||
defer log.SetOutput(os.Stderr)
|
||||
|
||||
p := proxy.New([]string{udp})
|
||||
state := middleware.State{W: &test.ResponseWriter{}, Req: new(dns.Msg)}
|
||||
resp, err := p.Lookup(state, "example.org.", dns.TypeA)
|
||||
if err != nil {
|
||||
t.Error("Expected to receive reply, but didn't")
|
||||
}
|
||||
// expect answer section with A record in it
|
||||
if len(resp.Answer) == 0 {
|
||||
t.Error("Expected to at least one RR in the answer section, got none")
|
||||
}
|
||||
if resp.Answer[0].Header().Rrtype != dns.TypeA {
|
||||
t.Error("Expected RR to A, got: %d", resp.Answer[0].Header().Rrtype)
|
||||
}
|
||||
if resp.Answer[0].(*dns.A).A.String() != "127.0.0.1" {
|
||||
t.Error("Expected 127.0.0.1, got: %d", resp.Answer[0].(*dns.A).A.String())
|
||||
}
|
||||
}
|
||||
@@ -12,7 +12,7 @@ func TestProxyToChaosServer(t *testing.T) {
|
||||
chaos CoreDNS-001 miek@miek.nl
|
||||
}
|
||||
`
|
||||
chaos, tcpCH, udpCH, err := testServer(t, corefile)
|
||||
chaos, tcpCH, udpCH, err := Server(t, corefile)
|
||||
if err != nil {
|
||||
t.Fatalf("Could get server: %s", err)
|
||||
}
|
||||
@@ -22,7 +22,7 @@ func TestProxyToChaosServer(t *testing.T) {
|
||||
proxy . ` + udpCH + `
|
||||
}
|
||||
`
|
||||
proxy, _, udp, err := testServer(t, corefileProxy)
|
||||
proxy, _, udp, err := Server(t, corefileProxy)
|
||||
if err != nil {
|
||||
t.Fatalf("Could get server: %s", err)
|
||||
}
|
||||
@@ -37,10 +37,10 @@ func TestProxyToChaosServer(t *testing.T) {
|
||||
}
|
||||
|
||||
func chaosTest(t *testing.T, server, net string) {
|
||||
m := testMsg("version.bind.", dns.TypeTXT, nil)
|
||||
m := Msg("version.bind.", dns.TypeTXT, nil)
|
||||
m.Question[0].Qclass = dns.ClassCHAOS
|
||||
|
||||
r, err := testExchange(m, server, net)
|
||||
r, err := Exchange(m, server, net)
|
||||
if err != nil {
|
||||
t.Fatalf("Could not send message: %s", err)
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@ import (
|
||||
"github.com/miekg/dns"
|
||||
)
|
||||
|
||||
func testMsg(zone string, typ uint16, o *dns.OPT) *dns.Msg {
|
||||
func Msg(zone string, typ uint16, o *dns.OPT) *dns.Msg {
|
||||
m := new(dns.Msg)
|
||||
m.SetQuestion(zone, typ)
|
||||
if o != nil {
|
||||
@@ -20,14 +20,14 @@ func testMsg(zone string, typ uint16, o *dns.OPT) *dns.Msg {
|
||||
return m
|
||||
}
|
||||
|
||||
func testExchange(m *dns.Msg, server, net string) (*dns.Msg, error) {
|
||||
func Exchange(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) {
|
||||
// Server returns a test server and the tcp and udp listeners addresses.
|
||||
func Server(t *testing.T, corefile string) (*server.Server, string, string, error) {
|
||||
srv, err := core.TestServer(t, corefile)
|
||||
if err != nil {
|
||||
return nil, "", "", err
|
||||
|
||||
Reference in New Issue
Block a user