zones_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package main
  2. import (
  3. "io"
  4. "io/ioutil"
  5. "os"
  6. "testing"
  7. "github.com/abh/dns"
  8. . "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. zonesReadDir("dns", s.zones)
  19. }
  20. func (s *ConfigSuite) TestReadConfigs(c *C) {
  21. // Just check that example.com and test.example.org loaded, too.
  22. c.Check(s.zones["example.com"].Origin, Equals, "example.com")
  23. c.Check(s.zones["test.example.org"].Origin, Equals, "test.example.org")
  24. if s.zones["test.example.org"].Options.Serial == 0 {
  25. c.Log("Serial number is 0, should be set by file timestamp")
  26. c.Fail()
  27. }
  28. // The real tests are in test.example.com so we have a place
  29. // to make nutty configuration entries
  30. tz := s.zones["test.example.com"]
  31. // test.example.com was loaded
  32. c.Check(tz.Origin, Equals, "test.example.com")
  33. c.Check(tz.Options.MaxHosts, Equals, 2)
  34. c.Check(tz.Options.Contact, Equals, "support.bitnames.com")
  35. c.Check(tz.Options.Targeting.String(), Equals, "@ continent country regiongroup region asn ip")
  36. // Got logging option
  37. c.Check(tz.Logging.StatHat, Equals, true)
  38. c.Check(tz.Labels["weight"].MaxHosts, Equals, 1)
  39. /* test different cname targets */
  40. c.Check(tz.Labels["www"].
  41. firstRR(dns.TypeCNAME).(*dns.CNAME).
  42. Target, Equals, "geo.bitnames.com.")
  43. c.Check(tz.Labels["www-cname"].
  44. firstRR(dns.TypeCNAME).(*dns.CNAME).
  45. Target, Equals, "bar.test.example.com.")
  46. c.Check(tz.Labels["www-alias"].
  47. firstRR(dns.TypeMF).(*dns.MF).
  48. Mf, Equals, "www")
  49. // The header name should just have a dot-prefix
  50. c.Check(tz.Labels[""].Records[dns.TypeNS][0].RR.(*dns.NS).Hdr.Name, Equals, "test.example.com.")
  51. }
  52. func (s *ConfigSuite) TestRemoveConfig(c *C) {
  53. // restore the dns.Mux
  54. defer zonesReadDir("dns", s.zones)
  55. dir, err := ioutil.TempDir("", "geodns-test.")
  56. if err != nil {
  57. c.Fail()
  58. }
  59. defer os.RemoveAll(dir)
  60. _, err = CopyFile(c, "dns/test.example.org.json", dir+"/test.example.org.json")
  61. if err != nil {
  62. c.Log(err)
  63. c.Fail()
  64. }
  65. _, err = CopyFile(c, "dns/test.example.org.json", dir+"/test2.example.org.json")
  66. if err != nil {
  67. c.Log(err)
  68. c.Fail()
  69. }
  70. zonesReadDir(dir, s.zones)
  71. c.Check(s.zones["test.example.org"].Origin, Equals, "test.example.org")
  72. c.Check(s.zones["test2.example.org"].Origin, Equals, "test2.example.org")
  73. os.Remove(dir + "/test2.example.org.json")
  74. zonesReadDir(dir, s.zones)
  75. c.Check(s.zones["test.example.org"].Origin, Equals, "test.example.org")
  76. _, ok := s.zones["test2.example.org"]
  77. c.Check(ok, Equals, false)
  78. }
  79. func CopyFile(c *C, src, dst string) (int64, error) {
  80. sf, err := os.Open(src)
  81. if err != nil {
  82. c.Log("Could not copy", src, "to", dst, "because", err)
  83. c.Fail()
  84. return 0, err
  85. }
  86. defer sf.Close()
  87. df, err := os.Create(dst)
  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 df.Close()
  94. return io.Copy(df, sf)
  95. }