mirror of
https://github.com/StackExchange/dnscontrol.git
synced 2024-05-11 05:55:12 +00:00
CHORE: Make Test_filterBy more readable (#1869)
This commit is contained in:
@ -73,7 +73,7 @@ func analyzeByLabel(cc *CompareConfig) ChangeList {
|
|||||||
instructions = append(instructions, mkDelete(label, "", accExisting, accMsgs))
|
instructions = append(instructions, mkDelete(label, "", accExisting, accMsgs))
|
||||||
} else if len(accExisting) == 0 { // No old records at the label? This must be a change.
|
} else if len(accExisting) == 0 { // No old records at the label? This must be a change.
|
||||||
//fmt.Printf("DEBUG: analyzeByLabel: %02d: create\n", i)
|
//fmt.Printf("DEBUG: analyzeByLabel: %02d: create\n", i)
|
||||||
fmt.Printf("DEBUG: analyzeByLabel mkAdd msgs=%d\n", len(accMsgs))
|
//fmt.Printf("DEBUG: analyzeByLabel mkAdd msgs=%d\n", len(accMsgs))
|
||||||
instructions = append(instructions, mkAddByLabel(label, "", accMsgs, accDesired))
|
instructions = append(instructions, mkAddByLabel(label, "", accMsgs, accDesired))
|
||||||
} else { // If we get here, it must be a change.
|
} else { // If we get here, it must be a change.
|
||||||
_ = i
|
_ = i
|
||||||
@ -82,7 +82,7 @@ func analyzeByLabel(cc *CompareConfig) ChangeList {
|
|||||||
// len(accDesired), accDesired,
|
// len(accDesired), accDesired,
|
||||||
// accMsgs,
|
// accMsgs,
|
||||||
// )
|
// )
|
||||||
fmt.Printf("DEBUG: analyzeByLabel mkchange msgs=%d\n", len(accMsgs))
|
//fmt.Printf("DEBUG: analyzeByLabel mkchange msgs=%d\n", len(accMsgs))
|
||||||
instructions = append(instructions, mkChangeLabel(label, "", accMsgs, accExisting, accDesired, msgsByKey))
|
instructions = append(instructions, mkChangeLabel(label, "", accMsgs, accExisting, accDesired, msgsByKey))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -121,8 +121,8 @@ func mkAdd(l string, t string, msgs []string, recs models.Records) Change {
|
|||||||
// TODO(tlim): Clean these up. Some of them are exact duplicates!
|
// TODO(tlim): Clean these up. Some of them are exact duplicates!
|
||||||
|
|
||||||
func mkAddByLabel(l string, t string, msgs []string, newRecs models.Records) Change {
|
func mkAddByLabel(l string, t string, msgs []string, newRecs models.Records) Change {
|
||||||
fmt.Printf("DEBUG: mkAddByLabel: len(o)=%d len(m)=%d\n", len(newRecs), len(msgs))
|
//fmt.Printf("DEBUG: mkAddByLabel: len(o)=%d len(m)=%d\n", len(newRecs), len(msgs))
|
||||||
fmt.Printf("DEBUG: mkAddByLabel: msgs = %v\n", msgs)
|
//fmt.Printf("DEBUG: mkAddByLabel: msgs = %v\n", msgs)
|
||||||
c := Change{Type: CREATE, Msgs: msgs}
|
c := Change{Type: CREATE, Msgs: msgs}
|
||||||
c.Key.NameFQDN = l
|
c.Key.NameFQDN = l
|
||||||
c.Key.Type = t
|
c.Key.Type = t
|
||||||
@ -185,13 +185,21 @@ func removeCommon(existing, desired []targetConfig) ([]targetConfig, []targetCon
|
|||||||
return filterBy(existing, dKeys), filterBy(desired, eKeys)
|
return filterBy(existing, dKeys), filterBy(desired, eKeys)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Return s but remove any items that can be found in m.
|
||||||
func filterBy(s []targetConfig, m map[string]*targetConfig) []targetConfig {
|
func filterBy(s []targetConfig, m map[string]*targetConfig) []targetConfig {
|
||||||
|
// fmt.Printf("DEBUG: filterBy called with %v\n", s)
|
||||||
|
// for k := range m {
|
||||||
|
// fmt.Printf("DEBUG: map %q\n", k)
|
||||||
|
// }
|
||||||
i := 0 // output index
|
i := 0 // output index
|
||||||
for _, x := range s {
|
for _, x := range s {
|
||||||
if _, ok := m[x.compareable]; !ok {
|
if _, ok := m[x.compareable]; !ok {
|
||||||
|
//fmt.Printf("DEBUG: comp %q NO\n", x.compareable)
|
||||||
// copy and increment index
|
// copy and increment index
|
||||||
s[i] = x
|
s[i] = x
|
||||||
i++
|
i++
|
||||||
|
} else {
|
||||||
|
//fmt.Printf("DEBUG: comp %q YES\n", x.compareable)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// // Prevent memory leak by erasing truncated values
|
// // Prevent memory leak by erasing truncated values
|
||||||
@ -200,6 +208,7 @@ func filterBy(s []targetConfig, m map[string]*targetConfig) []targetConfig {
|
|||||||
// s[j] = nil
|
// s[j] = nil
|
||||||
// }
|
// }
|
||||||
s = s[:i]
|
s = s[:i]
|
||||||
|
// fmt.Printf("DEBUG: filterBy returns %v\n", s)
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -9,20 +9,22 @@ import (
|
|||||||
"github.com/kylelemons/godebug/diff"
|
"github.com/kylelemons/godebug/diff"
|
||||||
)
|
)
|
||||||
|
|
||||||
var testDataAA1234 = makeRec("laba", "A", "1.2.3.4") // [0]
|
var testDataAA1234 = makeRec("laba", "A", "1.2.3.4") // [0]
|
||||||
var testDataAA5678 = makeRec("laba", "A", "5.6.7.8") // [0]
|
var testDataAA5678 = makeRec("laba", "A", "5.6.7.8") //
|
||||||
var testDataAMX10a = makeRec("laba", "MX", "10 laba") // [1]
|
var testDataAA1234ttl700 = makeRecTTL("laba", "A", "1.2.3.4", 700) //
|
||||||
var testDataCCa = makeRec("labc", "CNAME", "laba") // [2]
|
var testDataAA5678ttl700 = makeRecTTL("laba", "A", "5.6.7.8", 700) //
|
||||||
var testDataEA15 = makeRec("labe", "A", "10.10.10.15") // [3]
|
var testDataAMX10a = makeRec("laba", "MX", "10 laba") // [1]
|
||||||
var e4 = makeRec("labe", "A", "10.10.10.16") // [4]
|
var testDataCCa = makeRec("labc", "CNAME", "laba") // [2]
|
||||||
var e5 = makeRec("labe", "A", "10.10.10.17") // [5]
|
var testDataEA15 = makeRec("labe", "A", "10.10.10.15") // [3]
|
||||||
var e6 = makeRec("labe", "A", "10.10.10.18") // [6]
|
var e4 = makeRec("labe", "A", "10.10.10.16") // [4]
|
||||||
var e7 = makeRec("labg", "NS", "10.10.10.15") // [7]
|
var e5 = makeRec("labe", "A", "10.10.10.17") // [5]
|
||||||
var e8 = makeRec("labg", "NS", "10.10.10.16") // [8]
|
var e6 = makeRec("labe", "A", "10.10.10.18") // [6]
|
||||||
var e9 = makeRec("labg", "NS", "10.10.10.17") // [9]
|
var e7 = makeRec("labg", "NS", "10.10.10.15") // [7]
|
||||||
var e10 = makeRec("labg", "NS", "10.10.10.18") // [10]
|
var e8 = makeRec("labg", "NS", "10.10.10.16") // [8]
|
||||||
var e11mx = makeRec("labh", "MX", "22 ttt") // [11]
|
var e9 = makeRec("labg", "NS", "10.10.10.17") // [9]
|
||||||
var e11 = makeRec("labh", "CNAME", "labd") // [11]
|
var e10 = makeRec("labg", "NS", "10.10.10.18") // [10]
|
||||||
|
var e11mx = makeRec("labh", "MX", "22 ttt") // [11]
|
||||||
|
var e11 = makeRec("labh", "CNAME", "labd") // [11]
|
||||||
var testDataApexMX1aaa = makeRec("", "MX", "1 aaa")
|
var testDataApexMX1aaa = makeRec("", "MX", "1 aaa")
|
||||||
|
|
||||||
var testDataAA1234clone = makeRec("laba", "A", "1.2.3.4") // [0']
|
var testDataAA1234clone = makeRec("laba", "A", "1.2.3.4") // [0']
|
||||||
@ -40,7 +42,7 @@ var d11 = makeRec("labg", "NS", "10.10.10.97") // [11']
|
|||||||
var d12 = makeRec("labh", "A", "1.2.3.4") // [12']
|
var d12 = makeRec("labh", "A", "1.2.3.4") // [12']
|
||||||
var testDataApexMX22bbb = makeRec("", "MX", "22 bbb")
|
var testDataApexMX22bbb = makeRec("", "MX", "22 bbb")
|
||||||
|
|
||||||
var d0tc = targetConfig{compareable: "1.2.3.4 ttl=0", rec: testDataAA1234clone}
|
var d0tc = mkTargetConfig(testDataAA1234clone)
|
||||||
|
|
||||||
func makeChange(v Verb, l, t string, old, new models.Records, msgs []string) Change {
|
func makeChange(v Verb, l, t string, old, new models.Records, msgs []string) Change {
|
||||||
c := Change{
|
c := Change{
|
||||||
@ -402,6 +404,26 @@ ChangeList: len=12
|
|||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func mkTargetConfig(x ...*models.RecordConfig) []targetConfig {
|
||||||
|
var tc []targetConfig
|
||||||
|
for _, r := range x {
|
||||||
|
tc = append(tc, targetConfig{
|
||||||
|
compareable: comparable(r, nil),
|
||||||
|
rec: r,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return tc
|
||||||
|
}
|
||||||
|
|
||||||
|
func mkTargetConfigMap(x ...*models.RecordConfig) map[string]*targetConfig {
|
||||||
|
var m = map[string]*targetConfig{}
|
||||||
|
for _, v := range mkTargetConfig(x...) {
|
||||||
|
m[v.compareable] = &v
|
||||||
|
}
|
||||||
|
return m
|
||||||
|
}
|
||||||
|
|
||||||
func Test_diffTargets(t *testing.T) {
|
func Test_diffTargets(t *testing.T) {
|
||||||
type args struct {
|
type args struct {
|
||||||
existing []targetConfig
|
existing []targetConfig
|
||||||
@ -416,12 +438,8 @@ func Test_diffTargets(t *testing.T) {
|
|||||||
{
|
{
|
||||||
name: "single",
|
name: "single",
|
||||||
args: args{
|
args: args{
|
||||||
existing: []targetConfig{
|
existing: mkTargetConfig(testDataAA1234),
|
||||||
{compareable: "1.2.3.4", rec: testDataAA1234},
|
desired: mkTargetConfig(testDataAA1234),
|
||||||
},
|
|
||||||
desired: []targetConfig{
|
|
||||||
{compareable: "1.2.3.4", rec: testDataAA1234},
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
//want: ,
|
//want: ,
|
||||||
},
|
},
|
||||||
@ -429,13 +447,8 @@ func Test_diffTargets(t *testing.T) {
|
|||||||
{
|
{
|
||||||
name: "add1",
|
name: "add1",
|
||||||
args: args{
|
args: args{
|
||||||
existing: []targetConfig{
|
existing: mkTargetConfig(testDataAA1234),
|
||||||
{compareable: "1.2.3.4", rec: testDataAA1234},
|
desired: mkTargetConfig(testDataAA1234, testDataAMX10a),
|
||||||
},
|
|
||||||
desired: []targetConfig{
|
|
||||||
{compareable: "1.2.3.4", rec: testDataAA1234},
|
|
||||||
{compareable: "10 laba", rec: testDataAMX10a},
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
want: ChangeList{
|
want: ChangeList{
|
||||||
Change{Type: CREATE,
|
Change{Type: CREATE,
|
||||||
@ -449,13 +462,8 @@ func Test_diffTargets(t *testing.T) {
|
|||||||
{
|
{
|
||||||
name: "del1",
|
name: "del1",
|
||||||
args: args{
|
args: args{
|
||||||
existing: []targetConfig{
|
existing: mkTargetConfig(testDataAA1234, testDataAMX10a),
|
||||||
{compareable: "1.2.3.4", rec: testDataAA1234},
|
desired: mkTargetConfig(testDataAA1234),
|
||||||
{compareable: "10 laba", rec: testDataAMX10a},
|
|
||||||
},
|
|
||||||
desired: []targetConfig{
|
|
||||||
{compareable: "1.2.3.4", rec: testDataAA1234},
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
want: ChangeList{
|
want: ChangeList{
|
||||||
Change{Type: DELETE,
|
Change{Type: DELETE,
|
||||||
@ -469,14 +477,8 @@ func Test_diffTargets(t *testing.T) {
|
|||||||
{
|
{
|
||||||
name: "change2nd",
|
name: "change2nd",
|
||||||
args: args{
|
args: args{
|
||||||
existing: []targetConfig{
|
existing: mkTargetConfig(testDataAA1234, testDataAMX10a),
|
||||||
{compareable: "1.2.3.4", rec: testDataAA1234},
|
desired: mkTargetConfig(testDataAA1234, testDataAMX20b),
|
||||||
{compareable: "10 laba", rec: testDataAMX10a},
|
|
||||||
},
|
|
||||||
desired: []targetConfig{
|
|
||||||
{compareable: "1.2.3.4", rec: testDataAA1234},
|
|
||||||
{compareable: "20 laba", rec: testDataAMX20b},
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
want: ChangeList{
|
want: ChangeList{
|
||||||
Change{Type: CHANGE,
|
Change{Type: CHANGE,
|
||||||
@ -491,13 +493,8 @@ func Test_diffTargets(t *testing.T) {
|
|||||||
{
|
{
|
||||||
name: "del2nd",
|
name: "del2nd",
|
||||||
args: args{
|
args: args{
|
||||||
existing: []targetConfig{
|
existing: mkTargetConfig(testDataAA1234, testDataAA5678),
|
||||||
{compareable: "1.2.3.4", rec: testDataAA1234},
|
desired: mkTargetConfig(testDataAA1234),
|
||||||
{compareable: "5.6.7.8", rec: testDataAA5678},
|
|
||||||
},
|
|
||||||
desired: []targetConfig{
|
|
||||||
{compareable: "1.2.3.4", rec: testDataAA1234},
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
want: ChangeList{
|
want: ChangeList{
|
||||||
Change{Type: CHANGE,
|
Change{Type: CHANGE,
|
||||||
@ -536,8 +533,8 @@ func Test_removeCommon(t *testing.T) {
|
|||||||
{
|
{
|
||||||
name: "same",
|
name: "same",
|
||||||
args: args{
|
args: args{
|
||||||
existing: []targetConfig{d0tc},
|
existing: d0tc,
|
||||||
desired: []targetConfig{d0tc},
|
desired: d0tc,
|
||||||
},
|
},
|
||||||
want: []targetConfig{},
|
want: []targetConfig{},
|
||||||
want1: []targetConfig{},
|
want1: []targetConfig{},
|
||||||
@ -578,14 +575,8 @@ func Test_filterBy(t *testing.T) {
|
|||||||
{
|
{
|
||||||
name: "removeall",
|
name: "removeall",
|
||||||
args: args{
|
args: args{
|
||||||
s: []targetConfig{
|
s: mkTargetConfig(testDataAA1234, testDataAMX10a),
|
||||||
{compareable: "1.2.3.4", rec: testDataAA1234},
|
m: mkTargetConfigMap(testDataAA1234, testDataAMX10a),
|
||||||
{compareable: "10 laba", rec: testDataAMX10a},
|
|
||||||
},
|
|
||||||
m: map[string]*targetConfig{
|
|
||||||
"1.2.3.4": {compareable: "1.2.3.4", rec: testDataAA1234},
|
|
||||||
"10 laba": {compareable: "10 laba", rec: testDataAMX10a},
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
want: []targetConfig{},
|
want: []targetConfig{},
|
||||||
},
|
},
|
||||||
@ -593,36 +584,19 @@ func Test_filterBy(t *testing.T) {
|
|||||||
{
|
{
|
||||||
name: "keepall",
|
name: "keepall",
|
||||||
args: args{
|
args: args{
|
||||||
s: []targetConfig{
|
s: mkTargetConfig(testDataAA1234, testDataAMX10a),
|
||||||
{compareable: "1.2.3.4", rec: testDataAA1234},
|
m: mkTargetConfigMap(),
|
||||||
{compareable: "10 laba", rec: testDataAMX10a},
|
|
||||||
},
|
|
||||||
m: map[string]*targetConfig{
|
|
||||||
"nothing": {compareable: "1.2.3.4", rec: testDataAA1234},
|
|
||||||
"matches": {compareable: "10 laba", rec: testDataAMX10a},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
want: []targetConfig{
|
|
||||||
{compareable: "1.2.3.4", rec: testDataAA1234},
|
|
||||||
{compareable: "10 laba", rec: testDataAMX10a},
|
|
||||||
},
|
},
|
||||||
|
want: mkTargetConfig(testDataAA1234, testDataAMX10a),
|
||||||
},
|
},
|
||||||
|
|
||||||
{
|
{
|
||||||
name: "keepsome",
|
name: "keepsome",
|
||||||
args: args{
|
args: args{
|
||||||
s: []targetConfig{
|
s: mkTargetConfig(testDataAA1234, testDataAMX10a),
|
||||||
{compareable: "1.2.3.4", rec: testDataAA1234},
|
m: mkTargetConfigMap(testDataAMX10a),
|
||||||
{compareable: "10 laba", rec: testDataAMX10a},
|
|
||||||
},
|
|
||||||
m: map[string]*targetConfig{
|
|
||||||
"nothing": {compareable: "1.2.3.4", rec: testDataAA1234},
|
|
||||||
"10 laba": {compareable: "10 laba", rec: testDataAMX10a},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
want: []targetConfig{
|
|
||||||
{compareable: "1.2.3.4", rec: testDataAA1234},
|
|
||||||
},
|
},
|
||||||
|
want: mkTargetConfig(testDataAA1234),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
|
@ -9,11 +9,16 @@ import (
|
|||||||
|
|
||||||
func makeRec(label, rtype, content string) *models.RecordConfig {
|
func makeRec(label, rtype, content string) *models.RecordConfig {
|
||||||
origin := "f.com"
|
origin := "f.com"
|
||||||
r := models.RecordConfig{}
|
r := models.RecordConfig{TTL: 300}
|
||||||
r.SetLabel(label, origin)
|
r.SetLabel(label, origin)
|
||||||
r.PopulateFromString(rtype, content, origin)
|
r.PopulateFromString(rtype, content, origin)
|
||||||
return &r
|
return &r
|
||||||
}
|
}
|
||||||
|
func makeRecTTL(label, rtype, content string, ttl uint32) *models.RecordConfig {
|
||||||
|
r := makeRec(label, rtype, content)
|
||||||
|
r.TTL = ttl
|
||||||
|
return r
|
||||||
|
}
|
||||||
func makeRecSet(recs ...*models.RecordConfig) *recset {
|
func makeRecSet(recs ...*models.RecordConfig) *recset {
|
||||||
result := recset{}
|
result := recset{}
|
||||||
result.Key = recs[0].Key()
|
result.Key = recs[0].Key()
|
||||||
|
@ -97,7 +97,7 @@ func compileTypeGlob(g string) map[string]bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func match(rc *models.RecordConfig, glabel, gtarget glob.Glob, hasRType map[string]bool) bool {
|
func match(rc *models.RecordConfig, glabel, gtarget glob.Glob, hasRType map[string]bool) bool {
|
||||||
printer.Printf("DEBUG: match(%v, %v, %v, %v)\n", rc.NameFQDN, glabel, gtarget, hasRType)
|
//printer.Printf("DEBUG: match(%v, %v, %v, %v)\n", rc.NameFQDN, glabel, gtarget, hasRType)
|
||||||
|
|
||||||
// _ = glabel.Match(rc.NameFQDN)
|
// _ = glabel.Match(rc.NameFQDN)
|
||||||
// _ = matchType(rc.Type, hasRType)
|
// _ = matchType(rc.Type, hasRType)
|
||||||
@ -105,22 +105,22 @@ func match(rc *models.RecordConfig, glabel, gtarget glob.Glob, hasRType map[stri
|
|||||||
// _ = gtarget.Match(x)
|
// _ = gtarget.Match(x)
|
||||||
|
|
||||||
if !glabel.Match(rc.NameFQDN) {
|
if !glabel.Match(rc.NameFQDN) {
|
||||||
printer.Printf("DEBUG: REJECTED LABEL: %s:%v\n", rc.NameFQDN, glabel)
|
//printer.Printf("DEBUG: REJECTED LABEL: %s:%v\n", rc.NameFQDN, glabel)
|
||||||
return false
|
return false
|
||||||
} else if !matchType(rc.Type, hasRType) {
|
} else if !matchType(rc.Type, hasRType) {
|
||||||
printer.Printf("DEBUG: REJECTED TYPE: %s:%v\n", rc.Type, hasRType)
|
//printer.Printf("DEBUG: REJECTED TYPE: %s:%v\n", rc.Type, hasRType)
|
||||||
return false
|
return false
|
||||||
} else if gtarget == nil {
|
} else if gtarget == nil {
|
||||||
return true
|
return true
|
||||||
} else if !gtarget.Match(rc.GetTargetField()) {
|
} else if !gtarget.Match(rc.GetTargetField()) {
|
||||||
printer.Printf("DEBUG: REJECTED TARGET: %v:%v\n", rc.GetTargetField(), gtarget)
|
//printer.Printf("DEBUG: REJECTED TARGET: %v:%v\n", rc.GetTargetField(), gtarget)
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func matchType(s string, hasRType map[string]bool) bool {
|
func matchType(s string, hasRType map[string]bool) bool {
|
||||||
printer.Printf("DEBUG: matchType map=%v\n", hasRType)
|
//printer.Printf("DEBUG: matchType map=%v\n", hasRType)
|
||||||
if len(hasRType) == 0 {
|
if len(hasRType) == 0 {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user