Skip to content

Commit

Permalink
Boilerplate for tests and etc.
Browse files Browse the repository at this point in the history
  • Loading branch information
gerrowadat committed Nov 5, 2023
1 parent f396bee commit 421cd02
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 8 deletions.
15 changes: 7 additions & 8 deletions clouddns.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,14 @@ func dumpZonefile(dnsSpec *CloudDNSSpec) {

}

func rrsetsDiffer(x *dns.ResourceRecordSet, y *dns.ResourceRecordSet) bool {
func rrsetsEqual(x *dns.ResourceRecordSet, y *dns.ResourceRecordSet) bool {
if x.Type != y.Type ||
x.Name != y.Name {
return true
return false
}

if len(x.Rrdatas) != len(y.Rrdatas) {
return true
return false
}

for _, xv := range x.Rrdatas {
Expand All @@ -108,11 +108,10 @@ func rrsetsDiffer(x *dns.ResourceRecordSet, y *dns.ResourceRecordSet) bool {
}
}
if !found {
fmt.Printf("%s not found in %s", xv, y.Name)
return true
return false
}
}
return false
return true
}

func addDomainForZone(name string, domain string) string {
Expand Down Expand Up @@ -165,7 +164,7 @@ func buildNomadDnsChange(dnsSpec *CloudDNSSpec, tasks []TaskInfo, pruneMissing b
for _, cr := range cloud_rrs {
if nr.Name == cr.Name && nr.Type == cr.Type {
in_cloud = true
if rrsetsDiffer(nr, cr) {
if !rrsetsEqual(nr, cr) {
// pointer in nomad differs from cloud.
// Delete cloud record and replace.
log.Printf("Updating %s record in cloud: %s", nr.Type, nr.Name)
Expand Down Expand Up @@ -334,7 +333,7 @@ func uploadZonefile(dnsSpec *CloudDNSSpec, zoneFilename *string, dryRun *bool, p
for _, c := range cloud_rrs {
if z.Type == c.Type && z.Name == c.Name {
found = true
if rrsetsDiffer(z, c) {
if !rrsetsEqual(z, c) {
// Modify means a delete of the exact old record plus
// addition of the new one.
change.Additions = append(change.Additions, z)
Expand Down
111 changes: 111 additions & 0 deletions clouddns_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package main

import (
"testing"

"google.golang.org/api/dns/v1"
)

func Test_rrsetsEqual(t *testing.T) {
type args struct {
x *dns.ResourceRecordSet
y *dns.ResourceRecordSet
}
tests := []struct {
name string
args args
want bool
}{
{
name: "EmptySetsAreEqual",
args: args{
x: &dns.ResourceRecordSet{},
y: &dns.ResourceRecordSet{},
},
want: true,
},
{
name: "SimpleEqual",
args: args{
x: &dns.ResourceRecordSet{
Type: "A",
Name: "hostname.example.com",
},
y: &dns.ResourceRecordSet{
Type: "A",
Name: "hostname.example.com",
},
},
want: true,
},
{
name: "DifferingType",
args: args{
x: &dns.ResourceRecordSet{
Type: "A",
Name: "hostname.example.com",
},
y: &dns.ResourceRecordSet{
Type: "CNAME",
Name: "hostname.example.com",
},
},
want: false,
},
{
name: "DifferingRRdatas_len",
args: args{
x: &dns.ResourceRecordSet{
Type: "A",
Name: "hostname.example.com",
Rrdatas: []string{"1.2.3.4"},
},
y: &dns.ResourceRecordSet{
Type: "A",
Name: "hostname.example.com",
Rrdatas: []string{"1.2.3.4", "5.6.7.8"},
},
},
want: false,
},
{
name: "DifferingRRdatas_data",
args: args{
x: &dns.ResourceRecordSet{
Type: "A",
Name: "hostname.example.com",
Rrdatas: []string{"1.2.3.4"},
},
y: &dns.ResourceRecordSet{
Type: "A",
Name: "hostname.example.com",
Rrdatas: []string{"1.2.3.5"},
},
},
want: false,
},
{
name: "SameRRdatas",
args: args{
x: &dns.ResourceRecordSet{
Type: "A",
Name: "hostname.example.com",
Rrdatas: []string{"1.2.3.4", "5.6.7.8"},
},
y: &dns.ResourceRecordSet{
Type: "A",
Name: "hostname.example.com",
Rrdatas: []string{"1.2.3.4", "5.6.7.8"},
},
},
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := rrsetsEqual(tt.args.x, tt.args.y); got != tt.want {
t.Errorf("rrsetsEqual() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 421cd02

Please sign in to comment.