Skip to content

Commit

Permalink
Tests for mergeAnswerToRrsets
Browse files Browse the repository at this point in the history
Also make mergeAnswerToRrsets not add duplicate IPs to Rrdatas
  • Loading branch information
gerrowadat committed Nov 6, 2023
1 parent 917aa45 commit 4d24f74
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 2 deletions.
12 changes: 10 additions & 2 deletions clouddns.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,16 @@ func mergeAnswerToRrsets(rrsets []*dns.ResourceRecordSet, name string, ip string
// Only handles simple A records.
for _, rr := range rrsets {
if rr.Name == name {
// Name already exists, append the additional IP.
rr.Rrdatas = append(rr.Rrdatas, ip)
// Name already exists, append the additional IP (if it's not there already)
ip_found := false
for _, rrd := range rr.Rrdatas {
if rrd == ip {
ip_found = true
}
}
if !ip_found {
rr.Rrdatas = append(rr.Rrdatas, ip)
}
return rrsets
}
}
Expand Down
112 changes: 112 additions & 0 deletions clouddns_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"reflect"
"testing"

"google.golang.org/api/dns/v1"
Expand Down Expand Up @@ -185,3 +186,114 @@ func TestZoneFileFragment(t *testing.T) {
})
}
}

func Test_mergeAnswerToRrsets(t *testing.T) {
type args struct {
rrsets []*dns.ResourceRecordSet
name string
ip string
}
tests := []struct {
name string
args args
want []*dns.ResourceRecordSet
}{
// TODO: Add test cases.
{
name: "MergeSimpleRecordtoNothing",
args: args{
rrsets: []*dns.ResourceRecordSet{},
name: "doot",
ip: "1.2.3.4",
},
want: []*dns.ResourceRecordSet{
{
Name: "doot",
Type: "A",
Rrdatas: []string{"1.2.3.4"},
},
},
},
{
name: "MergeSimpleRecordtoOther",
args: args{
rrsets: []*dns.ResourceRecordSet{
{
Name: "blarg",
Type: "A",
Rrdatas: []string{"1.2.3.4"},
},
},
name: "doot",
ip: "5.6.7.8",
},
want: []*dns.ResourceRecordSet{
{
Name: "blarg",
Type: "A",
Rrdatas: []string{"1.2.3.4"},
},
{
Name: "doot",
Type: "A",
Rrdatas: []string{"5.6.7.8"},
},
},
},
{
name: "MergeSimpleRecordtoSameName",
args: args{
rrsets: []*dns.ResourceRecordSet{
{
Name: "doot",
Type: "A",
Rrdatas: []string{"1.2.3.4"},
},
},
name: "doot",
ip: "5.6.7.8",
},
want: []*dns.ResourceRecordSet{
{
Name: "doot",
Type: "A",
Rrdatas: []string{"1.2.3.4", "5.6.7.8"},
},
},
},
{
name: "MergeSimpleRecordtoSameNameSameIP",
args: args{
rrsets: []*dns.ResourceRecordSet{
{
Name: "doot",
Type: "A",
Rrdatas: []string{"1.2.3.4"},
},
},
name: "doot",
ip: "1.2.3.4",
},
want: []*dns.ResourceRecordSet{
{
Name: "doot",
Type: "A",
Rrdatas: []string{"1.2.3.4"},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := mergeAnswerToRrsets(tt.args.rrsets, tt.args.name, tt.args.ip); !reflect.DeepEqual(got, tt.want) {
for _, rr := range got {
t.Logf("Got: %s (%s), %d answers.", rr.Name, rr.Type, len(rr.Rrdatas))
for _, rrd := range rr.Rrdatas {
t.Logf("Got: - %s", rrd)
}
}
t.Errorf("mergeAnswerToRrsets() = %v records, want %v", len(got), len(tt.want))
}
})
}
}

0 comments on commit 4d24f74

Please sign in to comment.