reader_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. package zones
  2. import (
  3. "fmt"
  4. "io"
  5. "io/ioutil"
  6. "os"
  7. "testing"
  8. "github.com/abh/geodns/targeting"
  9. "github.com/abh/geodns/targeting/geoip2"
  10. "github.com/stretchr/testify/assert"
  11. )
  12. func loadZones(t *testing.T) *MuxManager {
  13. if targeting.Geo() == nil {
  14. t.Logf("Setting up geo provider")
  15. dbDir := geoip2.FindDB()
  16. if len(dbDir) == 0 {
  17. t.Fatalf("Could not find geoip directory")
  18. }
  19. geoprovider, err := geoip2.New(dbDir)
  20. if err == nil {
  21. targeting.Setup(geoprovider)
  22. } else {
  23. t.Fatalf("error setting up geo provider: %s", err)
  24. }
  25. }
  26. muxm, err := NewMuxManager("../dns", &NilReg{})
  27. if err != nil {
  28. t.Logf("loading zones: %s", err)
  29. t.Fail()
  30. }
  31. // Just check that example.com and test.example.org loaded, too.
  32. for _, zonename := range []string{"example.com", "test.example.com", "hc.example.com"} {
  33. if z, ok := muxm.zonelist[zonename]; ok {
  34. if z.Origin != zonename {
  35. t.Logf("zone '%s' doesn't have that Origin '%s'", zonename, z.Origin)
  36. t.Fail()
  37. }
  38. if z.Options.Serial == 0 {
  39. t.Logf("Zone '%s' Serial number is 0, should be set by file timestamp", zonename)
  40. t.Fail()
  41. }
  42. } else {
  43. t.Fatalf("Didn't load '%s'", zonename)
  44. }
  45. }
  46. return muxm
  47. }
  48. func TestReadConfigs(t *testing.T) {
  49. muxm := loadZones(t)
  50. // The real tests are in test.example.com so we have a place
  51. // to make nutty configuration entries
  52. tz := muxm.zonelist["test.example.com"]
  53. // test.example.com was loaded
  54. if tz.Options.MaxHosts != 2 {
  55. t.Logf("MaxHosts=%d, expected 2", tz.Options.MaxHosts)
  56. t.Fail()
  57. }
  58. if tz.Options.Contact != "support.bitnames.com" {
  59. t.Logf("Contact='%s', expected support.bitnames.com", tz.Options.Contact)
  60. t.Fail()
  61. }
  62. assert.Equal(t, tz.Options.Targeting.String(), "@ continent country regiongroup region asn ip", "Targeting.String()")
  63. assert.Equal(t, tz.Labels["weight"].MaxHosts, 1, "weight label has max_hosts=1")
  64. // /* test different cname targets */
  65. // c.Check(tz.Labels["www"].
  66. // FirstRR(dns.TypeCNAME).(*dns.CNAME).
  67. // Target, Equals, "geo.bitnames.com.")
  68. // c.Check(tz.Labels["www-cname"].
  69. // FirstRR(dns.TypeCNAME).(*dns.CNAME).
  70. // Target, Equals, "bar.test.example.com.")
  71. // c.Check(tz.Labels["www-alias"].
  72. // FirstRR(dns.TypeMF).(*dns.MF).
  73. // Mf, Equals, "www")
  74. // // The header name should just have a dot-prefix
  75. // c.Check(tz.Labels[""].Records[dns.TypeNS][0].RR.(*dns.NS).Hdr.Name, Equals, "test.example.com.")
  76. }
  77. func TestRemoveConfig(t *testing.T) {
  78. dir, err := ioutil.TempDir("", "geodns-test.")
  79. if err != nil {
  80. t.Fail()
  81. }
  82. defer os.RemoveAll(dir)
  83. muxm, err := NewMuxManager(dir, &NilReg{})
  84. if err != nil {
  85. t.Logf("loading zones: %s", err)
  86. t.Fail()
  87. }
  88. muxm.reload()
  89. _, err = CopyFile("../dns/test.example.org.json", dir+"/test.example.org.json")
  90. if err != nil {
  91. t.Log(err)
  92. t.Fail()
  93. }
  94. _, err = CopyFile("../dns/test.example.org.json", dir+"/test2.example.org.json")
  95. if err != nil {
  96. t.Log(err)
  97. t.Fail()
  98. }
  99. err = ioutil.WriteFile(dir+"/invalid.example.org.json", []byte("not-json"), 0644)
  100. if err != nil {
  101. t.Log(err)
  102. t.Fail()
  103. }
  104. muxm.reload()
  105. if muxm.zonelist["test.example.org"].Origin != "test.example.org" {
  106. t.Errorf("test.example.org has unexpected Origin: '%s'", muxm.zonelist["test.example.org"].Origin)
  107. }
  108. if muxm.zonelist["test2.example.org"].Origin != "test2.example.org" {
  109. t.Errorf("test2.example.org has unexpected Origin: '%s'", muxm.zonelist["test2.example.org"].Origin)
  110. }
  111. os.Remove(dir + "/test2.example.org.json")
  112. os.Remove(dir + "/invalid.example.org.json")
  113. muxm.reload()
  114. if muxm.zonelist["test.example.org"].Origin != "test.example.org" {
  115. t.Errorf("test.example.org has unexpected Origin: '%s'", muxm.zonelist["test.example.org"].Origin)
  116. }
  117. _, ok := muxm.zonelist["test2.example.org"]
  118. if ok != false {
  119. t.Log("test2.example.org is still loaded")
  120. t.Fail()
  121. }
  122. }
  123. func CopyFile(src, dst string) (int64, error) {
  124. sf, err := os.Open(src)
  125. if err != nil {
  126. return 0, fmt.Errorf("Could not copy '%s' to '%s': %s", src, dst, err)
  127. }
  128. defer sf.Close()
  129. df, err := os.Create(dst)
  130. if err != nil {
  131. return 0, fmt.Errorf("Could not copy '%s' to '%s': %s", src, dst, err)
  132. }
  133. defer df.Close()
  134. return io.Copy(df, sf)
  135. }