From cd93a8f5c8babd98befce642da3edc46e783f8d2 Mon Sep 17 00:00:00 2001 From: Nathan Johnson Date: Mon, 20 Aug 2018 14:54:05 -0500 Subject: [PATCH] removing dnsmasq requirement, moving to miekg/dns --- .travis.yml | 5 +-- resolver/resolver_test.go | 80 +++++++++++++++++++++++---------------- 2 files changed, 48 insertions(+), 37 deletions(-) diff --git a/.travis.yml b/.travis.yml index 7b2a86cf..db33339d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,9 +2,6 @@ language: go sudo: false go: - tip -before_install: - - sudo apt-get install -y dnsmasq - - sudo service dnsmasq stop install: - go get -v github.com/golang/lint/golint - go get -d -t -v ./... @@ -12,4 +9,4 @@ install: script: - go vet ./... - $HOME/gopath/bin/golint . - - VEGETA_TESTDNSMASQ_ENABLE=1 go test -v ./... + - go test -v ./... diff --git a/resolver/resolver_test.go b/resolver/resolver_test.go index 67bc42d8..abbd584b 100644 --- a/resolver/resolver_test.go +++ b/resolver/resolver_test.go @@ -1,7 +1,6 @@ package resolver import ( - "bytes" "fmt" "io/ioutil" "net" @@ -9,58 +8,73 @@ import ( "net/http/httptest" "net/url" "os" - "os/exec" "strings" "testing" "time" + + "github.com/miekg/dns" ) const ( - dnsmasqRunEnv = "VEGETA_TESTDNSMASQ_ENABLE" - dnsmasqPathEnv = "VEGETA_TESTDNSMASQ_PATH" dnsmasqPortEnv = "VEGETA_TESTDNSMASQ_PORT" + fakeDomain = "acme.notadomain" ) -func TestResolveDNSMasq(t *testing.T) { +func TestResolveMiekg(t *testing.T) { + + dns.HandleFunc(".", func(w dns.ResponseWriter, r *dns.Msg) { + m := &dns.Msg{} + m.SetReply(r) + localIP := net.ParseIP("127.0.0.1") + defer func() { + w.WriteMsg(m) + }() + if len(r.Question) == 0 { + m.RecursionAvailable = true + m.SetRcode(r, dns.RcodeRefused) + return + } + + q := r.Question[0] + + if q.Name == fakeDomain+"." { + m.Answer = []dns.RR{&dns.A{ + Hdr: dns.RR_Header{ + Name: r.Question[0].Name, + Rrtype: dns.TypeA, + Class: dns.ClassINET, + Ttl: 1, + }, + A: localIP, + }} + } else { + m.SetRcode(r, dns.RcodeNameError) + } + }) const payload = "there is no cloud, just someone else's computer" var ( - path = "dnsmasq" port = "5300" ) - if _, ok := os.LookupEnv(dnsmasqRunEnv); !ok { - t.Skipf("skipping test becuase %s is not set", dnsmasqRunEnv) - } + if ePort, ok := os.LookupEnv(dnsmasqPortEnv); ok { port = ePort } - if ePath, ok := os.LookupEnv(dnsmasqPathEnv); ok { - path = ePath - } - stdout := bytes.Buffer{} - stderr := bytes.Buffer{} - cmd := exec.Command(path, "-h", "-H", "./hosts", "-p", port, "-d") - - cmd.Stdout = &stdout - cmd.Stderr = &stderr - - err := cmd.Start() - if err != nil { - t.Fatalf("failed starting dnsmasq: %s", err) + ds := dns.Server{ + Addr: fmt.Sprintf("%s:%s", "127.0.0.1", port), + Net: "udp", + UDPSize: dns.MinMsgSize, + ReadTimeout: 2 * time.Second, + WriteTimeout: 2 * time.Second, + // Unsafe instructs the server to disregard any sanity checks and directly hand the message to + // the handler. It will specifically not check if the query has the QR bit not set. + Unsafe: false, } - defer func() { - err := cmd.Wait() - if err != nil { - t.Logf("unclean shutdown of dnsmasq: %s", err) - } - t.Log(stdout.String()) - t.Log(stderr.String()) - }() - time.Sleep(time.Second) + go ds.ListenAndServe() defer func() { - _ = cmd.Process.Kill() + _ = ds.Shutdown() }() res, err := NewResolver([]string{net.JoinHostPort("127.0.0.1", port)}) @@ -82,7 +96,7 @@ func TestResolveDNSMasq(t *testing.T) { t.Errorf("could not parse port from httptest url %s: %s", ts.URL, err) return } - tsurl.Host = net.JoinHostPort("acme.notadomain", hport) + tsurl.Host = net.JoinHostPort(fakeDomain, hport) resp, err := http.Get(tsurl.String()) if err != nil { t.Errorf("failed resolver round trip: %s", err)