serve_test.go 1001 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package main
  2. import (
  3. "github.com/miekg/dns"
  4. . "launchpad.net/gocheck"
  5. "strings"
  6. "time"
  7. )
  8. func (s *ConfigSuite) TestServing(c *C) {
  9. Zones := make(Zones)
  10. setupPgeodnsZone(Zones)
  11. go configReader("dns", Zones)
  12. go listenAndServe(":8853", &Zones)
  13. time.Sleep(100 * time.Millisecond)
  14. r := exchange(c, "_status.pgeodns.", dns.TypeTXT)
  15. txt := r.Answer[0].(*dns.RR_TXT).Txt[0]
  16. if !strings.HasPrefix(txt, "{") {
  17. c.Log("Unexpected result for _status.pgeodns", txt)
  18. c.Fail()
  19. }
  20. r = exchange(c, "bar.example.com.", dns.TypeA)
  21. ip := r.Answer[0].(*dns.RR_A).A
  22. c.Check(ip.String(), Equals, "192.168.1.2")
  23. r = exchange(c, "example.com.", dns.TypeSOA)
  24. soa := r.Answer[0].(*dns.RR_SOA)
  25. serial := soa.Serial
  26. c.Check(int(serial), Equals, 3)
  27. }
  28. func exchange(c *C, name string, dnstype uint16) *dns.Msg {
  29. msg := new(dns.Msg)
  30. cli := new(dns.Client)
  31. msg.SetQuestion(name, dnstype)
  32. r, err := cli.Exchange(msg, "127.0.0.1:8853")
  33. if err != nil {
  34. c.Log("err", err)
  35. c.Fail()
  36. }
  37. return r
  38. }