zones_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package main
  2. import (
  3. "github.com/abh/dns"
  4. "io"
  5. "io/ioutil"
  6. . "launchpad.net/gocheck"
  7. "os"
  8. "testing"
  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. // Got logging option
  36. c.Check(tz.Logging.StatHat, Equals, true)
  37. c.Check(tz.Labels["weight"].MaxHosts, Equals, 1)
  38. /* test different cname targets */
  39. c.Check(tz.Labels["www"].
  40. firstRR(dns.TypeCNAME).(*dns.CNAME).
  41. Target, Equals, "geo.bitnames.com.")
  42. c.Check(tz.Labels["www-cname"].
  43. firstRR(dns.TypeCNAME).(*dns.CNAME).
  44. Target, Equals, "bar.test.example.com.")
  45. c.Check(tz.Labels["www-alias"].
  46. firstRR(dns.TypeMF).(*dns.MF).
  47. Mf, Equals, "www")
  48. }
  49. func (s *ConfigSuite) TestRemoveConfig(c *C) {
  50. // restore the dns.Mux
  51. defer zonesReadDir("dns", s.zones)
  52. dir, err := ioutil.TempDir("", "geodns-test.")
  53. if err != nil {
  54. c.Fail()
  55. }
  56. defer os.RemoveAll(dir)
  57. _, err = CopyFile(c, "dns/test.example.org.json", dir+"/test.example.org.json")
  58. if err != nil {
  59. c.Log(err)
  60. c.Fail()
  61. }
  62. _, err = CopyFile(c, "dns/test.example.org.json", dir+"/test2.example.org.json")
  63. if err != nil {
  64. c.Log(err)
  65. c.Fail()
  66. }
  67. zonesReadDir(dir, s.zones)
  68. c.Check(s.zones["test.example.org"].Origin, Equals, "test.example.org")
  69. c.Check(s.zones["test2.example.org"].Origin, Equals, "test2.example.org")
  70. os.Remove(dir + "/test2.example.org.json")
  71. zonesReadDir(dir, s.zones)
  72. c.Check(s.zones["test.example.org"].Origin, Equals, "test.example.org")
  73. _, ok := s.zones["test2.example.org"]
  74. c.Check(ok, Equals, false)
  75. }
  76. func CopyFile(c *C, src, dst string) (int64, error) {
  77. sf, err := os.Open(src)
  78. if err != nil {
  79. c.Log("Could not copy", src, "to", dst, "because", err)
  80. c.Fail()
  81. return 0, err
  82. }
  83. defer sf.Close()
  84. df, err := os.Create(dst)
  85. if err != nil {
  86. c.Log("Could not copy", src, "to", dst, "because", err)
  87. c.Fail()
  88. return 0, err
  89. }
  90. defer df.Close()
  91. return io.Copy(df, sf)
  92. }