Skip to content

Commit

Permalink
bug: fix dns panic during tests
Browse files Browse the repository at this point in the history
Depending on your router, the dns test panics:

```
$ go19 test -v -race -run TestBatch .
[Jan 15 09:32:30]  INFO coprocess: Disabled feature
=== RUN   TestBatch
panic: Following domain not mocked:localhost.fritz.box.

goroutine 338 [running]:
github.com/TykTechnologies/tyk.(*dnsMockHandler).ServeDNS(0x2922f78, 0x2816a20, 0xc420730400, 0xc4203fa480)
	/Users/ahmet/go/src/github.com/TykTechnologies/tyk/helpers_test.go:539 +0x8cc
github.com/TykTechnologies/tyk/vendor/github.com/miekg/dns.(*Server).serve(0xc4204e6000, 0x280abc0, 0xc42019f3b0, 0x2801cc0, 0x2922f78, 0xc4204fc000, 0x25, 0x200, 0xc42000e030, 0xc4202a05e0, ...)
	/Users/ahmet/go/src/github.com/TykTechnologies/tyk/vendor/github.com/miekg/dns/server.go:558 +0x7a8
created by github.com/TykTechnologies/tyk/vendor/github.com/miekg/dns.(*Server).serveUDP
	/Users/ahmet/go/src/github.com/TykTechnologies/tyk/vendor/github.com/miekg/dns/server.go:514 +0x363
exit status 2
FAIL	github.com/TykTechnologies/tyk	0.346s
```

This PR fixes the issue by 127.0.0.1 if domain name matches valid regex.
  • Loading branch information
asoorm authored and buger committed Jan 15, 2018
1 parent f5d9bf7 commit 0d1e374
Showing 1 changed file with 23 additions and 13 deletions.
36 changes: 23 additions & 13 deletions helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"net/url"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
"sync"
Expand Down Expand Up @@ -513,31 +514,40 @@ func buildAndLoadAPI(apiGens ...func(spec *APISpec)) (specs []*APISpec) {
return loadAPI(buildAPI(apiGens...)...)
}

var domainsToAddresses map[string]string = map[string]string{
"host1.local.": "127.0.0.1",
"host2.local.": "127.0.0.1",
"host3.local.": "127.0.0.1",
"localhost.local.": "127.0.0.1",
var domainsToAddresses = map[string]string{
"host1.local.": "127.0.0.1",
"host2.local.": "127.0.0.1",
"host3.local.": "127.0.0.1",
}

type dnsMockHandler struct{}

func (this *dnsMockHandler) ServeDNS(w dns.ResponseWriter, r *dns.Msg) {
func (d *dnsMockHandler) ServeDNS(w dns.ResponseWriter, r *dns.Msg) {
msg := dns.Msg{}
msg.SetReply(r)
switch r.Question[0].Qtype {
case dns.TypeA:
msg.Authoritative = true
domain := msg.Question[0].Name

address, ok := domainsToAddresses[domain]
if ok {
msg.Answer = append(msg.Answer, &dns.A{
Hdr: dns.RR_Header{Name: domain, Rrtype: dns.TypeA, Class: dns.ClassINET, Ttl: 60},
A: net.ParseIP(address),
})
} else {
panic("Following domain not mocked:" + domain)
if !ok {
// ^ start of line
// localhost\. match literally
// ()* match between 0 and unlimited times
// [[:alnum:]]+\. match single character in [a-zA-Z0-9] minimum one time and ending in . literally
reg := regexp.MustCompile(`^localhost\.([[:alnum:]]+\.)*`)
if matched := reg.MatchString(domain); !matched {
panic("domain not mocked: " + domain)
}

address = "127.0.0.1"
}

msg.Answer = append(msg.Answer, &dns.A{
Hdr: dns.RR_Header{Name: domain, Rrtype: dns.TypeA, Class: dns.ClassINET, Ttl: 60},
A: net.ParseIP(address),
})
}
w.WriteMsg(&msg)
}
Expand Down

0 comments on commit 0d1e374

Please sign in to comment.