2
0

zones_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package main
  2. import (
  3. "io"
  4. "io/ioutil"
  5. "os"
  6. "testing"
  7. "github.com/abh/geodns/Godeps/_workspace/src/github.com/miekg/dns"
  8. . "github.com/abh/geodns/Godeps/_workspace/src/gopkg.in/check.v1"
  9. )
  10. // Hook up gocheck into the gotest runner.
  11. func Test(t *testing.T) { TestingT(t) }
  12. type ConfigSuite struct {
  13. zones Zones
  14. }
  15. var _ = Suite(&ConfigSuite{})
  16. func (s *ConfigSuite) SetUpSuite(c *C) {
  17. s.zones = make(Zones)
  18. lastRead = map[string]*ZoneReadRecord{}
  19. zonesReadDir("dns", s.zones)
  20. }
  21. func (s *ConfigSuite) TestReadConfigs(c *C) {
  22. // Just check that example.com and test.example.org loaded, too.
  23. c.Check(s.zones["example.com"].Origin, Equals, "example.com")
  24. c.Check(s.zones["test.example.org"].Origin, Equals, "test.example.org")
  25. if s.zones["test.example.org"].Options.Serial == 0 {
  26. c.Log("Serial number is 0, should be set by file timestamp")
  27. c.Fail()
  28. }
  29. // The real tests are in test.example.com so we have a place
  30. // to make nutty configuration entries
  31. tz := s.zones["test.example.com"]
  32. // test.example.com was loaded
  33. c.Check(tz.Origin, Equals, "test.example.com")
  34. c.Check(tz.Options.MaxHosts, Equals, 2)
  35. c.Check(tz.Options.Contact, Equals, "support.bitnames.com")
  36. c.Check(tz.Options.Targeting.String(), Equals, "@ continent country regiongroup region asn ip")
  37. // Got logging option
  38. c.Check(tz.Logging.StatHat, Equals, true)
  39. c.Check(tz.Labels["weight"].MaxHosts, Equals, 1)
  40. /* test different cname targets */
  41. c.Check(tz.Labels["www"].
  42. firstRR(dns.TypeCNAME).(*dns.CNAME).
  43. Target, Equals, "geo.bitnames.com.")
  44. c.Check(tz.Labels["www-cname"].
  45. firstRR(dns.TypeCNAME).(*dns.CNAME).
  46. Target, Equals, "bar.test.example.com.")
  47. c.Check(tz.Labels["www-alias"].
  48. firstRR(dns.TypeMF).(*dns.MF).
  49. Mf, Equals, "www")
  50. // The header name should just have a dot-prefix
  51. c.Check(tz.Labels[""].Records[dns.TypeNS][0].RR.(*dns.NS).Hdr.Name, Equals, "test.example.com.")
  52. }
  53. func (s *ConfigSuite) TestRemoveConfig(c *C) {
  54. // restore the dns.Mux
  55. defer zonesReadDir("dns", s.zones)
  56. dir, err := ioutil.TempDir("", "geodns-test.")
  57. if err != nil {
  58. c.Fail()
  59. }
  60. defer os.RemoveAll(dir)
  61. _, err = CopyFile(c, "dns/test.example.org.json", dir+"/test.example.org.json")
  62. if err != nil {
  63. c.Log(err)
  64. c.Fail()
  65. }
  66. _, err = CopyFile(c, "dns/test.example.org.json", dir+"/test2.example.org.json")
  67. if err != nil {
  68. c.Log(err)
  69. c.Fail()
  70. }
  71. err = ioutil.WriteFile(dir+"/invalid.example.org.json", []byte("not-json"), 0644)
  72. if err != nil {
  73. c.Log(err)
  74. c.Fail()
  75. }
  76. zonesReadDir(dir, s.zones)
  77. c.Check(s.zones["test.example.org"].Origin, Equals, "test.example.org")
  78. c.Check(s.zones["test2.example.org"].Origin, Equals, "test2.example.org")
  79. os.Remove(dir + "/test2.example.org.json")
  80. os.Remove(dir + "/invalid.example.org.json")
  81. zonesReadDir(dir, s.zones)
  82. c.Check(s.zones["test.example.org"].Origin, Equals, "test.example.org")
  83. _, ok := s.zones["test2.example.org"]
  84. c.Check(ok, Equals, false)
  85. }
  86. func CopyFile(c *C, src, dst string) (int64, error) {
  87. sf, err := os.Open(src)
  88. if err != nil {
  89. c.Log("Could not copy", src, "to", dst, "because", err)
  90. c.Fail()
  91. return 0, err
  92. }
  93. defer sf.Close()
  94. df, err := os.Create(dst)
  95. if err != nil {
  96. c.Log("Could not copy", src, "to", dst, "because", err)
  97. c.Fail()
  98. return 0, err
  99. }
  100. defer df.Close()
  101. return io.Copy(df, sf)
  102. }