test(provider/civo): Improved test coverage to 90.4% (#5455)

This commit is contained in:
Saurav Upadhyay 2025-05-23 20:12:44 +05:30 committed by GitHub
parent 7c0881477a
commit d4013c22e2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -129,6 +129,44 @@ func TestCivoProviderRecords(t *testing.T) {
assert.Equal(t, int(records[1].RecordTTL), expected[1].TTL) assert.Equal(t, int(records[1].RecordTTL), expected[1].TTL)
} }
func TestCivoProviderRecordsWithError(t *testing.T) {
client, server, _ := civogo.NewAdvancedClientForTesting([]civogo.ConfigAdvanceClientForTesting{
{
Method: "GET",
Value: []civogo.ValueAdvanceClientForTesting{
{
RequestBody: ``,
URL: "/v2/dns/12345/records",
ResponseBody: `[
{"id": "1", "domain_id":"12345", "account_id": "1", "name": "", "type": "A", "value": "10.0.0.0", "ttl": 600},
{"id": "2", "account_id": "1", "domain_id":"12345", "name": "", "type": "A", "value": "10.0.0.1", "ttl": 600}
]`,
},
{
RequestBody: ``,
URL: "/v2/dns",
ResponseBody: `invalid-json-data`,
},
},
},
})
defer server.Close()
provider := &CivoProvider{
Client: *client,
domainFilter: endpoint.NewDomainFilter([]string{"example.com"}),
}
_, err := client.ListDNSRecords("12345")
assert.NoError(t, err)
endpoint, err := provider.Records(context.Background())
assert.Error(t, err)
assert.Nil(t, endpoint)
}
func TestCivoProviderWithoutRecords(t *testing.T) { func TestCivoProviderWithoutRecords(t *testing.T) {
client, server, _ := civogo.NewClientForTesting(map[string]string{ client, server, _ := civogo.NewClientForTesting(map[string]string{
"/v2/dns/12345/records": `[]`, "/v2/dns/12345/records": `[]`,
@ -149,6 +187,68 @@ func TestCivoProviderWithoutRecords(t *testing.T) {
assert.Empty(t, records) assert.Empty(t, records)
} }
func TestCivoProcessCreateActionsLogs(t *testing.T) {
t.Run("Logs Skipping Zone, no creates found", func(t *testing.T) {
zonesByID := map[string]civogo.DNSDomain{
"example.com": {
ID: "1",
AccountID: "1",
Name: "example.com",
},
}
recordsByZoneID := map[string][]civogo.DNSRecord{
"example.com": {
{
ID: "1",
AccountID: "1",
Name: "abc",
Value: "12.12.12.1",
Type: "A",
TTL: 600,
},
},
}
updateByZone := map[string][]*endpoint.Endpoint{
"example.com": {
endpoint.NewEndpoint("abc.example.com", endpoint.RecordTypeA, "1.2.3.4"),
},
}
var civoChanges CivoChanges
err := processCreateActions(zonesByID, recordsByZoneID, updateByZone, &civoChanges)
require.NoError(t, err)
assert.Len(t, civoChanges.Creates, 1)
assert.Empty(t, civoChanges.Deletes)
assert.Empty(t, civoChanges.Updates)
})
t.Run("Records found which should not exist", func(t *testing.T) {
zonesByID := map[string]civogo.DNSDomain{
"example.com": {
ID: "1",
AccountID: "1",
Name: "example.com",
},
}
recordsByZoneID := map[string][]civogo.DNSRecord{
"example.com": {},
}
updateByZone := map[string][]*endpoint.Endpoint{
"example.com": {},
}
var civoChanges CivoChanges
err := processCreateActions(zonesByID, recordsByZoneID, updateByZone, &civoChanges)
require.NoError(t, err)
assert.Empty(t, civoChanges.Creates)
assert.Empty(t, civoChanges.Creates)
assert.Empty(t, civoChanges.Updates)
})
}
func TestCivoProcessCreateActions(t *testing.T) { func TestCivoProcessCreateActions(t *testing.T) {
zoneByID := map[string]civogo.DNSDomain{ zoneByID := map[string]civogo.DNSDomain{
"example.com": { "example.com": {
@ -255,6 +355,41 @@ func TestCivoProcessCreateActionsWithError(t *testing.T) {
assert.Equal(t, "invalid Record Type: AAAA", err.Error()) assert.Equal(t, "invalid Record Type: AAAA", err.Error())
} }
func TestCivoProcessUpdateActionsWithError(t *testing.T) {
zoneByID := map[string]civogo.DNSDomain{
"example.com": {
ID: "1",
AccountID: "1",
Name: "example.com",
},
}
recordsByZoneID := map[string][]civogo.DNSRecord{
"example.com": {
{
ID: "1",
AccountID: "1",
DNSDomainID: "1",
Name: "txt",
Value: "12.12.12.1",
Type: "A",
TTL: 600,
},
},
}
updatesByZone := map[string][]*endpoint.Endpoint{
"example.com": {
endpoint.NewEndpoint("foo.example.com", "AAAA", "1.2.3.4"),
endpoint.NewEndpoint("txt.example.com", endpoint.RecordTypeCNAME, "foo.example.com"),
},
}
var changes CivoChanges
err := processUpdateActions(zoneByID, recordsByZoneID, updatesByZone, &changes)
require.Error(t, err)
}
func TestCivoProcessUpdateActions(t *testing.T) { func TestCivoProcessUpdateActions(t *testing.T) {
zoneByID := map[string]civogo.DNSDomain{ zoneByID := map[string]civogo.DNSDomain{
"example.com": { "example.com": {
@ -515,6 +650,64 @@ func TestCivoApplyChanges(t *testing.T) {
assert.NoError(t, err) assert.NoError(t, err)
} }
func TestCivoApplyChangesError(t *testing.T) {
client, server, _ := civogo.NewAdvancedClientForTesting([]civogo.ConfigAdvanceClientForTesting{
{
Method: "GET",
Value: []civogo.ValueAdvanceClientForTesting{
{
RequestBody: "",
URL: "/v2/dns",
ResponseBody: `[{"id": "12345", "account_id": "1", "name": "example.com"}]`,
},
{
RequestBody: "",
URL: "/v2/dns/12345/records",
ResponseBody: `[]`,
},
},
},
})
defer server.Close()
provider := &CivoProvider{
Client: *client,
}
cases := []struct {
Name string
changes *plan.Changes
}{
{
Name: "invalid record type from processCreateActions",
changes: &plan.Changes{
Create: []*endpoint.Endpoint{
endpoint.NewEndpoint("bad.example.com", "AAAA", "1.2.3.4"),
},
},
},
{
Name: "invalid record type from processUpdateActions",
changes: &plan.Changes{
UpdateOld: []*endpoint.Endpoint{
endpoint.NewEndpoint("bad.example.com", "AAAA", "1.2.3.4"),
},
UpdateNew: []*endpoint.Endpoint{
endpoint.NewEndpoint("bad.example.com", "AAAA", "5.6.7.8"),
},
},
},
}
for _, tt := range cases {
t.Run(tt.Name, func(t *testing.T) {
err := provider.ApplyChanges(context.Background(), tt.changes)
assert.Equal(t, "invalid Record Type: AAAA", string(err.Error()))
})
}
}
func TestCivoProviderFetchZones(t *testing.T) { func TestCivoProviderFetchZones(t *testing.T) {
client, server, _ := civogo.NewClientForTesting(map[string]string{ client, server, _ := civogo.NewClientForTesting(map[string]string{
"/v2/dns": `[ "/v2/dns": `[
@ -688,39 +881,19 @@ func TestCivo_submitChangesCreate(t *testing.T) {
}, },
}, },
}, },
}) {
defer server.Close() Method: "DELETE",
Value: []civogo.ValueAdvanceClientForTesting{
provider := &CivoProvider{ {
Client: *client, URL: "/v2/dns/12345/records/76cc107f-fbef-4e2b-b97f-f5d34f4075d3",
DryRun: false, ResponseBody: `{"result": "success"}`,
}
changes := CivoChanges{
Creates: []*CivoChangeCreate{
{
Domain: civogo.DNSDomain{
ID: "12345",
AccountID: "1",
Name: "example.com",
}, },
Options: &civogo.DNSRecordConfig{ {
Type: "MX", URL: "/v2/dns/12345/records/error-record-id",
Name: "mail", ResponseBody: `{"result": "error", "error": "failed to delete record"}`,
Value: "10.0.0.1",
Priority: 10,
TTL: 600,
}, },
}, },
}, },
}
err := provider.submitChanges(context.Background(), changes)
assert.NoError(t, err)
}
func TestCivo_submitChangesUpdate(t *testing.T) {
client, server, _ := civogo.NewAdvancedClientForTesting([]civogo.ConfigAdvanceClientForTesting{
{ {
Method: "PUT", Method: "PUT",
Value: []civogo.ValueAdvanceClientForTesting{ Value: []civogo.ValueAdvanceClientForTesting{
@ -738,6 +911,11 @@ func TestCivo_submitChangesUpdate(t *testing.T) {
"ttl": 600 "ttl": 600
}`, }`,
}, },
{
RequestBody: `{"type":"MX","name":"mail","value":"10.0.0.3","priority":10,"ttl":600}`,
URL: "/v2/dns/12345/records/error-record-id",
ResponseBody: `{"result": "error", "error": "failed to update record"}`,
},
}, },
}, },
}) })
@ -745,36 +923,66 @@ func TestCivo_submitChangesUpdate(t *testing.T) {
provider := &CivoProvider{ provider := &CivoProvider{
Client: *client, Client: *client,
DryRun: false, DryRun: true,
} }
changes := CivoChanges{ cases := []struct {
Updates: []*CivoChangeUpdate{ name string
{ changes *CivoChanges
Domain: civogo.DNSDomain{ID: "12345", AccountID: "1", Name: "example.com"}, expectedResult error
DomainRecord: civogo.DNSRecord{ }{
ID: "76cc107f-fbef-4e2b-b97f-f5d34f4075d3", {
AccountID: "1", name: "changes slice is empty",
DNSDomainID: "12345", changes: &CivoChanges{},
Name: "mail", expectedResult: nil,
Value: "10.0.0.1", },
Type: "MX", {
Priority: 10, name: "changes slice has changes and update changes",
TTL: 600, changes: &CivoChanges{
Creates: []*CivoChangeCreate{
{
Domain: civogo.DNSDomain{
ID: "12345",
AccountID: "1",
Name: "example.com",
},
Options: &civogo.DNSRecordConfig{
Type: "MX",
Name: "mail",
Value: "10.0.0.1",
Priority: 10,
TTL: 600,
},
},
}, },
Options: civogo.DNSRecordConfig{
Type: "MX", Updates: []*CivoChangeUpdate{
Name: "mail", {
Value: "10.0.0.2", Domain: civogo.DNSDomain{
Priority: 10, ID: "12345",
TTL: 600, AccountID: "2",
Name: "example.org",
},
},
{
Domain: civogo.DNSDomain{
ID: "67890",
AccountID: "3",
Name: "example.COM",
},
},
}, },
}, },
expectedResult: nil,
}, },
} }
err := provider.submitChanges(context.Background(), changes) for _, c := range cases {
assert.NoError(t, err) t.Run(c.name, func(t *testing.T) {
err := provider.submitChanges(context.Background(), *c.changes)
assert.NoError(t, err)
})
}
} }
func TestCivo_submitChangesDelete(t *testing.T) { func TestCivo_submitChangesDelete(t *testing.T) {