Skip to content

Commit

Permalink
removing dnsmasq requirement, moving to miekg/dns
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanejohnson committed Aug 28, 2018
1 parent f51d570 commit cd93a8f
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 37 deletions.
5 changes: 1 addition & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@ 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 ./...
- go build -v ./...
script:
- go vet ./...
- $HOME/gopath/bin/golint .
- VEGETA_TESTDNSMASQ_ENABLE=1 go test -v ./...
- go test -v ./...
80 changes: 47 additions & 33 deletions resolver/resolver_test.go
Original file line number Diff line number Diff line change
@@ -1,66 +1,80 @@
package resolver

import (
"bytes"
"fmt"
"io/ioutil"
"net"
"net/http"
"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)})
Expand All @@ -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)
Expand Down

0 comments on commit cd93a8f

Please sign in to comment.