config_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package main
  2. import (
  3. "github.com/miekg/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. tz := s.zones["test.example.com"]
  25. // The real tests are in test.example.com so we have a place
  26. // to make nutty configuration entries
  27. c.Check(tz.Origin, Equals, "test.example.com")
  28. c.Check(tz.Options.MaxHosts, Equals, 2)
  29. c.Check(tz.Options.Contact, Equals, "support.bitnames.com")
  30. c.Check(tz.Labels["weight"].MaxHosts, Equals, 1)
  31. /* test different cname targets */
  32. c.Check(tz.Labels["www"].
  33. firstRR(dns.TypeCNAME).(*dns.CNAME).
  34. Target, Equals, "geo.bitnames.com.")
  35. c.Check(tz.Labels["www-cname"].
  36. firstRR(dns.TypeCNAME).(*dns.CNAME).
  37. Target, Equals, "bar.test.example.com.")
  38. c.Check(tz.Labels["www-alias"].
  39. firstRR(dns.TypeMF).(*dns.MF).
  40. Mf, Equals, "www")
  41. }
  42. func (s *ConfigSuite) TestRemoveConfig(c *C) {
  43. // restore the dns.Mux
  44. defer zonesReadDir("dns", s.zones)
  45. dir, err := ioutil.TempDir("", "geodns-test.")
  46. if err != nil {
  47. c.Fail()
  48. }
  49. defer os.RemoveAll(dir)
  50. _, err = CopyFile(c, "dns/test.example.org.json", dir+"/test.example.org.json")
  51. if err != nil {
  52. c.Log(err)
  53. c.Fail()
  54. }
  55. _, err = CopyFile(c, "dns/test.example.org.json", dir+"/test2.example.org.json")
  56. if err != nil {
  57. c.Log(err)
  58. c.Fail()
  59. }
  60. zonesReadDir(dir, s.zones)
  61. c.Check(s.zones["test.example.org"].Origin, Equals, "test.example.org")
  62. c.Check(s.zones["test2.example.org"].Origin, Equals, "test2.example.org")
  63. os.Remove(dir + "/test2.example.org.json")
  64. zonesReadDir(dir, s.zones)
  65. c.Check(s.zones["test.example.org"].Origin, Equals, "test.example.org")
  66. _, ok := s.zones["test2.example.org"]
  67. c.Check(ok, Equals, false)
  68. }
  69. func CopyFile(c *C, src, dst string) (int64, error) {
  70. sf, err := os.Open(src)
  71. if err != nil {
  72. c.Log("Could not copy", src, "to", dst, "because", err)
  73. c.Fail()
  74. return 0, err
  75. }
  76. defer sf.Close()
  77. df, err := os.Create(dst)
  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 df.Close()
  84. return io.Copy(df, sf)
  85. }