github.com/teknogeek/dnscontrol/v2@v2.10.1-0.20200227202244-ae299b55ba42/providers/bind/soa_test.go (about) 1 package bind 2 3 import ( 4 "fmt" 5 "testing" 6 "time" 7 8 "github.com/StackExchange/dnscontrol/v2/models" 9 ) 10 11 func Test_makeSoa(t *testing.T) { 12 origin := "example.com" 13 var tests = []struct { 14 def *SoaInfo 15 existing *models.RecordConfig 16 desired *models.RecordConfig 17 expectedSoa *models.RecordConfig 18 expectedSerial uint32 19 }{ 20 { 21 // If everything is blank, the hard-coded defaults should kick in. 22 &SoaInfo{"", "", 0, 0, 0, 0, 0}, 23 &models.RecordConfig{Target: "", SoaMbox: "", SoaSerial: 0, SoaRefresh: 0, SoaRetry: 0, SoaExpire: 0, SoaMinttl: 0}, 24 &models.RecordConfig{Target: "", SoaMbox: "", SoaSerial: 0, SoaRefresh: 0, SoaRetry: 0, SoaExpire: 0, SoaMinttl: 0}, 25 &models.RecordConfig{Target: "DEFAULT_NOT_SET.", SoaMbox: "DEFAULT_NOT_SET.", SoaSerial: 1, SoaRefresh: 3600, SoaRetry: 600, SoaExpire: 604800, SoaMinttl: 1440}, 26 2019022300, 27 }, 28 { 29 // If everything is filled, leave the desired values in place. 30 &SoaInfo{"ns.example.com", "root.example.com", 1, 2, 3, 4, 5}, 31 &models.RecordConfig{Target: "a", SoaMbox: "aa", SoaSerial: 10, SoaRefresh: 11, SoaRetry: 12, SoaExpire: 13, SoaMinttl: 14}, 32 &models.RecordConfig{Target: "b", SoaMbox: "bb", SoaSerial: 15, SoaRefresh: 16, SoaRetry: 17, SoaExpire: 18, SoaMinttl: 19}, 33 &models.RecordConfig{Target: "b", SoaMbox: "bb", SoaSerial: 15, SoaRefresh: 16, SoaRetry: 17, SoaExpire: 18, SoaMinttl: 19}, 34 2019022300, 35 }, 36 { 37 // Test incrementing serial. 38 &SoaInfo{"ns.example.com", "root.example.com", 1, 2, 3, 4, 5}, 39 &models.RecordConfig{Target: "a", SoaMbox: "aa", SoaSerial: 2019022301, SoaRefresh: 11, SoaRetry: 12, SoaExpire: 13, SoaMinttl: 14}, 40 &models.RecordConfig{Target: "b", SoaMbox: "bb", SoaSerial: 0, SoaRefresh: 16, SoaRetry: 17, SoaExpire: 18, SoaMinttl: 19}, 41 &models.RecordConfig{Target: "b", SoaMbox: "bb", SoaSerial: 2019022301, SoaRefresh: 16, SoaRetry: 17, SoaExpire: 18, SoaMinttl: 19}, 42 2019022302, 43 }, 44 { 45 // Test incrementing serial_2. 46 &SoaInfo{"ns.example.com", "root.example.com", 1, 2, 3, 4, 5}, 47 &models.RecordConfig{Target: "a", SoaMbox: "aa", SoaSerial: 0, SoaRefresh: 11, SoaRetry: 12, SoaExpire: 13, SoaMinttl: 14}, 48 &models.RecordConfig{Target: "b", SoaMbox: "bb", SoaSerial: 2019022304, SoaRefresh: 16, SoaRetry: 17, SoaExpire: 18, SoaMinttl: 19}, 49 &models.RecordConfig{Target: "b", SoaMbox: "bb", SoaSerial: 2019022304, SoaRefresh: 16, SoaRetry: 17, SoaExpire: 18, SoaMinttl: 19}, 50 2019022305, 51 }, 52 { 53 // If there are gaps in existing or desired, fill in as appropriate. 54 &SoaInfo{"ns.example.com", "root.example.com", 1, 2, 3, 4, 5}, 55 &models.RecordConfig{Target: "", SoaMbox: "aa", SoaSerial: 0, SoaRefresh: 11, SoaRetry: 0, SoaExpire: 13, SoaMinttl: 0}, 56 &models.RecordConfig{Target: "b", SoaMbox: "", SoaSerial: 15, SoaRefresh: 0, SoaRetry: 17, SoaExpire: 0, SoaMinttl: 19}, 57 &models.RecordConfig{Target: "b", SoaMbox: "aa", SoaSerial: 15, SoaRefresh: 11, SoaRetry: 17, SoaExpire: 13, SoaMinttl: 19}, 58 2019022300, 59 }, 60 { 61 // Gaps + existing==nil 62 &SoaInfo{"ns.example.com", "root.example.com", 1, 2, 3, 4, 5}, 63 nil, 64 &models.RecordConfig{Target: "b", SoaMbox: "", SoaSerial: 15, SoaRefresh: 0, SoaRetry: 17, SoaExpire: 0, SoaMinttl: 19}, 65 &models.RecordConfig{Target: "b", SoaMbox: "root.example.com", SoaSerial: 15, SoaRefresh: 2, SoaRetry: 17, SoaExpire: 4, SoaMinttl: 19}, 66 2019022300, 67 }, 68 { 69 // Gaps + desired==nil 70 // NB(tom): In the code as of 2020-02-23, desired will never be nil. 71 &SoaInfo{"ns.example.com", "root.example.com", 1, 2, 3, 4, 5}, 72 &models.RecordConfig{Target: "", SoaMbox: "aa", SoaSerial: 0, SoaRefresh: 11, SoaRetry: 0, SoaExpire: 13, SoaMinttl: 0}, 73 nil, 74 &models.RecordConfig{Target: "ns.example.com", SoaMbox: "aa", SoaSerial: 1, SoaRefresh: 11, SoaRetry: 3, SoaExpire: 13, SoaMinttl: 5}, 75 2019022300, 76 }, 77 } 78 79 // Fake out the tests so they think today is 2019-02-23 80 nowFunc = func() time.Time { 81 fakeToday, _ := time.Parse("20060102", "20190223") 82 return fakeToday 83 } 84 85 for i, tst := range tests { 86 87 if tst.existing != nil { 88 tst.existing.SetLabel("@", origin) 89 tst.existing.Type = "SOA" 90 } 91 if tst.desired != nil { 92 tst.desired.SetLabel("@", origin) 93 tst.desired.Type = "SOA" 94 } 95 96 tst.expectedSoa.SetLabel("@", origin) 97 tst.expectedSoa.Type = "SOA" 98 99 r1, r2 := makeSoa(origin, tst.def, tst.existing, tst.desired) 100 if !areEqualSoa(r1, tst.expectedSoa) { 101 t.Fatalf("Test %d soa:\nExpected (%v)\n got (%v)\n", i, tst.expectedSoa.String(), r1.String()) 102 } 103 if r2 != tst.expectedSerial { 104 t.Fatalf("Test:%d soa: Expected (%v) got (%v)\n", i, tst.expectedSerial, r2) 105 } 106 } 107 } 108 109 func areEqualSoa(r1, r2 *models.RecordConfig) bool { 110 if r1.NameFQDN != r2.NameFQDN { 111 fmt.Printf("ERROR: fqdn %q != %q\n", r1.NameFQDN, r2.NameFQDN) 112 return false 113 } 114 if r1.Name != r2.Name { 115 fmt.Printf("ERROR: name %q != %q\n", r1.Name, r2.Name) 116 return false 117 } 118 if r1.Target != r2.Target { 119 fmt.Printf("ERROR: target %q != %q\n", r1.Target, r2.Target) 120 return false 121 } 122 if r1.SoaMbox != r2.SoaMbox { 123 fmt.Printf("ERROR: mbox %v != %v\n", r1.SoaMbox, r2.SoaMbox) 124 return false 125 } 126 if r1.SoaSerial != r2.SoaSerial { 127 fmt.Printf("ERROR: serial %v != %v\n", r1.SoaSerial, r2.SoaSerial) 128 return false 129 } 130 if r1.SoaRefresh != r2.SoaRefresh { 131 fmt.Printf("ERROR: refresh %v != %v\n", r1.SoaRefresh, r2.SoaRefresh) 132 return false 133 } 134 if r1.SoaRetry != r2.SoaRetry { 135 fmt.Printf("ERROR: retry %v != %v\n", r1.SoaRetry, r2.SoaRetry) 136 return false 137 } 138 if r1.SoaExpire != r2.SoaExpire { 139 fmt.Printf("ERROR: expire %v != %v\n", r1.SoaExpire, r2.SoaExpire) 140 return false 141 } 142 if r1.SoaMinttl != r2.SoaMinttl { 143 fmt.Printf("ERROR: minttl %v != %v\n", r1.SoaMinttl, r2.SoaMinttl) 144 return false 145 } 146 return true 147 }