Skip to content

Commit

Permalink
Added Test case for Delete Record with Owner Label
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaideep Bellani committed Jul 1, 2021
1 parent 4c2aaa4 commit d62aa68
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 39 deletions.
48 changes: 31 additions & 17 deletions provider/bluecat/bluecat.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ type BluecatTXTRecord struct {
ID int `json:"id"`
Name string `json:"name"`
Properties string `json:"properties"`
Owner string `json:"owner"`
}

type bluecatRecordSet struct {
Expand Down Expand Up @@ -212,9 +211,8 @@ func (p *BluecatProvider) Records(ctx context.Context) (endpoints []*endpoint.En
return nil, errors.Wrapf(err, "could not fetch TXT records for zone: %v", zone)
}
for _, rec := range resT {
rec.Owner = extractOwnerfromTXTRecord(rec.Properties)
tempEndpoint := endpoint.NewEndpoint(rec.Name, endpoint.RecordTypeTXT, rec.Properties, rec.Owner)
tempEndpoint.Labels[endpoint.OwnerLabelKey] = rec.Owner
tempEndpoint := endpoint.NewEndpoint(rec.Name, endpoint.RecordTypeTXT, rec.Properties)
tempEndpoint.Labels[endpoint.OwnerLabelKey], err = extractOwnerfromTXTRecord(rec.Properties)
endpoints = append(endpoints, tempEndpoint)
}

Expand All @@ -236,7 +234,10 @@ func (p *BluecatProvider) Records(ctx context.Context) (endpoints []*endpoint.En
ep := endpoint.NewEndpointWithTTL(propMap["absoluteName"], endpoint.RecordTypeA, endpoint.TTL(ttl), ip)
for _, txtRec := range resT {
if strings.Compare(rec.Name, txtRec.Name) == 0 {
ep.Labels[endpoint.OwnerLabelKey] = strings.Split(extractOwnerfromTXTRecord(txtRec.Properties), "=")[1]
ep.Labels[endpoint.OwnerLabelKey], err = extractOwnerfromTXTRecord(txtRec.Properties)
if err != nil {
return nil, errors.Wrapf(err, "owner not parsed correctly")
}
}
}
endpoints = append(endpoints, ep)
Expand All @@ -246,7 +247,10 @@ func (p *BluecatProvider) Records(ctx context.Context) (endpoints []*endpoint.En
ep := endpoint.NewEndpoint(propMap["absoluteName"], endpoint.RecordTypeA, ip)
for _, txtRec := range resT {
if strings.Compare(rec.Name, txtRec.Name) == 0 {
ep.Labels[endpoint.OwnerLabelKey] = strings.Split(extractOwnerfromTXTRecord(txtRec.Properties), "=")[1]
ep.Labels[endpoint.OwnerLabelKey], err = extractOwnerfromTXTRecord(txtRec.Properties)
if err != nil {
return nil, errors.Wrapf(err, "owner not parsed correctly")
}
}
}
endpoints = append(endpoints, ep)
Expand All @@ -269,15 +273,21 @@ func (p *BluecatProvider) Records(ctx context.Context) (endpoints []*endpoint.En
tempEndpoint := endpoint.NewEndpointWithTTL(propMap["absoluteName"], endpoint.RecordTypeCNAME, endpoint.TTL(ttl), propMap["linkedRecordName"])
for _, txtRec := range resT {
if strings.Compare(rec.Name, txtRec.Name) == 0 {
tempEndpoint.Labels[endpoint.OwnerLabelKey] = strings.Split(extractOwnerfromTXTRecord(txtRec.Properties), "=")[1]
tempEndpoint.Labels[endpoint.OwnerLabelKey], err = extractOwnerfromTXTRecord(txtRec.Properties)
if err != nil {
return nil, errors.Wrapf(err, "owner not parsed correctly")
}
}
}
endpoints = append(endpoints, tempEndpoint)
} else {
tempEndpoint := endpoint.NewEndpoint(propMap["absoluteName"], endpoint.RecordTypeCNAME, propMap["linkedRecordName"])
for _, txtRec := range resT {
if strings.Compare(rec.Name, txtRec.Name) == 0 {
tempEndpoint.Labels[endpoint.OwnerLabelKey] = strings.Split(extractOwnerfromTXTRecord(txtRec.Properties), "=")[1]
tempEndpoint.Labels[endpoint.OwnerLabelKey], err = extractOwnerfromTXTRecord(txtRec.Properties)
if err != nil {
return nil, errors.Wrapf(err, "owner not parsed correctly")
}
}
}
endpoints = append(endpoints, tempEndpoint)
Expand Down Expand Up @@ -305,8 +315,6 @@ func (p *BluecatProvider) ApplyChanges(ctx context.Context, changes *plan.Change
p.deleteRecords(deleted)
p.createRecords(created)

// TODO: add bluecat deploy API call here

return nil
}

Expand Down Expand Up @@ -1037,15 +1045,21 @@ func expandZone(zone string) string {

//Function to parse the owner from the TXT record
//It returns in the format externaldns/owner:"OWNER_NAME"
func extractOwnerfromTXTRecord(propString string) string {
func extractOwnerfromTXTRecord(propString string) (string, error) {
if len(propString) == 0 {
return "", errors.Errorf("External-DNS Owner not found")
}
propsTemp := strings.TrimSuffix(propString, "|")
splits := strings.Split(propsTemp, "|")
items := strings.Split(splits[1], ",")
var owner = ""
for _, value := range items {
if strings.Contains(value, "owner") {
owner = value
if strings.Contains(strings.Join(splits, ""), "external-dns") {
items := strings.Split(strings.Join(splits, ""), ",")
var owner = ""
for _, value := range items {
if strings.Contains(value, "owner") {
owner = strings.Split(value, "=")[1]
}
}
return owner, nil
}
return owner
return "", errors.Errorf("External-DNS Owner not found")
}
87 changes: 65 additions & 22 deletions provider/bluecat/bluecat_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
/*
Copyright 2020 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand Down Expand Up @@ -148,8 +145,8 @@ func createMockBluecatCNAME(alias, target string, ttl int) BluecatCNAMERecord {

func createMockBluecatTXT(fqdn, txt string) BluecatTXTRecord {
return BluecatTXTRecord{
Name: fqdn,
//Text: txt,
Name: fqdn,
Properties: txt,
}
}

Expand All @@ -169,7 +166,7 @@ type bluecatTestData []struct {

var tests = bluecatTestData{
{
"first test case", // TODO: better test description
"first test case",
[]*endpoint.Endpoint{
{
DNSName: "example.com",
Expand Down Expand Up @@ -200,6 +197,17 @@ var tests = bluecatTestData{
RecordType: endpoint.RecordTypeTXT,
Targets: endpoint.Targets{"hello"},
},
{
DNSName: "bcd.example.com",
RecordType: endpoint.RecordTypeTXT,
Targets: endpoint.Targets{""},
},
{
DNSName: "kdb.example.com",
RecordType: endpoint.RecordTypeTXT,
Targets: endpoint.Targets{"heritage=external-dns,external-dns/owner=default,external-dns/resource=service/openshift-ingress/router-default"},
Labels: endpoint.Labels{"owner": "default"},
},
},
},
}
Expand All @@ -209,6 +217,11 @@ func TestBluecatRecords(t *testing.T) {
mockBluecatZones: &[]BluecatZone{
createMockBluecatZone("example.com"),
},
mockBluecatTXTs: &[]BluecatTXTRecord{
createMockBluecatTXT("abc.example.com", "hello"),
createMockBluecatTXT("bcd.example.com", ""),
createMockBluecatTXT("kdb.example.com", "heritage=external-dns,external-dns/owner=default,external-dns/resource=service/openshift-ingress/router-default"),
},
mockBluecatHosts: &[]BluecatHostRecord{
createMockBluecatHostRecord("example.com", "123.123.123.122", 30),
createMockBluecatHostRecord("nginx.example.com", "123.123.123.123", 30),
Expand All @@ -217,9 +230,6 @@ func TestBluecatRecords(t *testing.T) {
mockBluecatCNAMEs: &[]BluecatCNAMERecord{
createMockBluecatCNAME("hack.example.com", "bluecatnetworks.com", 30),
},
mockBluecatTXTs: &[]BluecatTXTRecord{
createMockBluecatTXT("abc.example.com", "hello"),
},
}

provider := newBluecatProvider(
Expand Down Expand Up @@ -262,19 +272,6 @@ func TestBluecatApplyChangesCreate(t *testing.T) {
validateEndpoints(t, actual, []*endpoint.Endpoint{})
}
}

/*
* Jai's understanding about this code.
1) Creates a mock client, witch Zome and Host Records, CName
records and txt records.
2) Creates a new provider that is bluecat on the same zone.
3) ***Q*** what does the for loop run over and what is the range of tests and ti
4) provider applies changes, what is the context for? also, plan.changes refers to
the one in plan.go
5) what are the ti endpoints
6) what are the actual and err endpoints.
7) finally validate the endpoints.
*/
func TestBluecatApplyChangesDelete(t *testing.T) {
client := mockGatewayClient{
mockBluecatZones: &[]BluecatZone{
Expand All @@ -290,6 +287,8 @@ func TestBluecatApplyChangesDelete(t *testing.T) {
},
mockBluecatTXTs: &[]BluecatTXTRecord{
createMockBluecatTXT("abc.example.com", "hello"),
createMockBluecatTXT("bcd.example.com", ""),
createMockBluecatTXT("kdb.example.com", "heritage=external-dns,external-dns/owner=default,external-dns/resource=service/openshift-ingress/router-default"),
},
}

Expand All @@ -311,6 +310,50 @@ func TestBluecatApplyChangesDelete(t *testing.T) {
}
}

func TestBluecatApplyChangesDeleteWithOwner(t *testing.T) {
client := mockGatewayClient{
mockBluecatZones: &[]BluecatZone{
createMockBluecatZone("example.com"),
},
mockBluecatHosts: &[]BluecatHostRecord{
createMockBluecatHostRecord("example.com", "123.123.123.122", 30),
createMockBluecatHostRecord("nginx.example.com", "123.123.123.123", 30),
createMockBluecatHostRecord("whitespace.example.com", "123.123.123.124", 30),
},
mockBluecatCNAMEs: &[]BluecatCNAMERecord{
createMockBluecatCNAME("hack.example.com", "bluecatnetworks.com", 30),
},
mockBluecatTXTs: &[]BluecatTXTRecord{
createMockBluecatTXT("abc.example.com", "hello"),
createMockBluecatTXT("bcd.example.com", ""),
createMockBluecatTXT("kdb.example.com", "heritage=external-dns,external-dns/owner=default,external-dns/resource=service/openshift-ingress/router-default"),
},
}

provider := newBluecatProvider(
endpoint.NewDomainFilter([]string{"example.com"}),
provider.NewZoneIDFilter([]string{""}), false, client)

for _, ti := range tests {
for _, ep := range ti.Endpoints {
if strings.Contains(ep.Targets.String(), "external-dns") {
owner, err := extractOwnerfromTXTRecord(ep.Targets.String())
t.Logf("Owner %s %s", owner, err)
}
}
err := provider.ApplyChanges(context.Background(), &plan.Changes{Delete: ti.Endpoints})
if err != nil {
t.Fatal(err)
}
actual, err := provider.Records(context.Background())
if err != nil {
t.Fatal(err)
}
validateEndpoints(t, actual, []*endpoint.Endpoint{})
}

}

// TODO: ensure mapChanges method is tested
// TODO: ensure findZone method is tested
// TODO: ensure zones method is tested
Expand Down

0 comments on commit d62aa68

Please sign in to comment.