Skip to content

Commit d4013c2

Browse files
authored
test(provider/civo): Improved test coverage to 90.4% (#5455)
1 parent 7c08814 commit d4013c2

File tree

1 file changed

+259
-51
lines changed

1 file changed

+259
-51
lines changed

provider/civo/civo_test.go

Lines changed: 259 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,44 @@ func TestCivoProviderRecords(t *testing.T) {
129129
assert.Equal(t, int(records[1].RecordTTL), expected[1].TTL)
130130
}
131131

132+
func TestCivoProviderRecordsWithError(t *testing.T) {
133+
client, server, _ := civogo.NewAdvancedClientForTesting([]civogo.ConfigAdvanceClientForTesting{
134+
{
135+
Method: "GET",
136+
Value: []civogo.ValueAdvanceClientForTesting{
137+
{
138+
RequestBody: ``,
139+
URL: "/v2/dns/12345/records",
140+
ResponseBody: `[
141+
{"id": "1", "domain_id":"12345", "account_id": "1", "name": "", "type": "A", "value": "10.0.0.0", "ttl": 600},
142+
{"id": "2", "account_id": "1", "domain_id":"12345", "name": "", "type": "A", "value": "10.0.0.1", "ttl": 600}
143+
]`,
144+
},
145+
{
146+
RequestBody: ``,
147+
URL: "/v2/dns",
148+
ResponseBody: `invalid-json-data`,
149+
},
150+
},
151+
},
152+
})
153+
154+
defer server.Close()
155+
156+
provider := &CivoProvider{
157+
Client: *client,
158+
domainFilter: endpoint.NewDomainFilter([]string{"example.com"}),
159+
}
160+
161+
_, err := client.ListDNSRecords("12345")
162+
assert.NoError(t, err)
163+
164+
endpoint, err := provider.Records(context.Background())
165+
assert.Error(t, err)
166+
assert.Nil(t, endpoint)
167+
168+
}
169+
132170
func TestCivoProviderWithoutRecords(t *testing.T) {
133171
client, server, _ := civogo.NewClientForTesting(map[string]string{
134172
"/v2/dns/12345/records": `[]`,
@@ -149,6 +187,68 @@ func TestCivoProviderWithoutRecords(t *testing.T) {
149187
assert.Empty(t, records)
150188
}
151189

190+
func TestCivoProcessCreateActionsLogs(t *testing.T) {
191+
t.Run("Logs Skipping Zone, no creates found", func(t *testing.T) {
192+
zonesByID := map[string]civogo.DNSDomain{
193+
"example.com": {
194+
ID: "1",
195+
AccountID: "1",
196+
Name: "example.com",
197+
},
198+
}
199+
200+
recordsByZoneID := map[string][]civogo.DNSRecord{
201+
"example.com": {
202+
{
203+
ID: "1",
204+
AccountID: "1",
205+
Name: "abc",
206+
Value: "12.12.12.1",
207+
Type: "A",
208+
TTL: 600,
209+
},
210+
},
211+
}
212+
213+
updateByZone := map[string][]*endpoint.Endpoint{
214+
"example.com": {
215+
endpoint.NewEndpoint("abc.example.com", endpoint.RecordTypeA, "1.2.3.4"),
216+
},
217+
}
218+
var civoChanges CivoChanges
219+
220+
err := processCreateActions(zonesByID, recordsByZoneID, updateByZone, &civoChanges)
221+
require.NoError(t, err)
222+
assert.Len(t, civoChanges.Creates, 1)
223+
assert.Empty(t, civoChanges.Deletes)
224+
assert.Empty(t, civoChanges.Updates)
225+
})
226+
227+
t.Run("Records found which should not exist", func(t *testing.T) {
228+
zonesByID := map[string]civogo.DNSDomain{
229+
"example.com": {
230+
ID: "1",
231+
AccountID: "1",
232+
Name: "example.com",
233+
},
234+
}
235+
236+
recordsByZoneID := map[string][]civogo.DNSRecord{
237+
"example.com": {},
238+
}
239+
240+
updateByZone := map[string][]*endpoint.Endpoint{
241+
"example.com": {},
242+
}
243+
var civoChanges CivoChanges
244+
245+
err := processCreateActions(zonesByID, recordsByZoneID, updateByZone, &civoChanges)
246+
require.NoError(t, err)
247+
assert.Empty(t, civoChanges.Creates)
248+
assert.Empty(t, civoChanges.Creates)
249+
assert.Empty(t, civoChanges.Updates)
250+
})
251+
}
152252
func TestCivoProcessCreateActions(t *testing.T) {
153253
zoneByID := map[string]civogo.DNSDomain{
154254
"example.com": {
@@ -255,6 +355,41 @@ func TestCivoProcessCreateActionsWithError(t *testing.T) {
255355
assert.Equal(t, "invalid Record Type: AAAA", err.Error())
256356
}
257357

358+
func TestCivoProcessUpdateActionsWithError(t *testing.T) {
359+
zoneByID := map[string]civogo.DNSDomain{
360+
"example.com": {
361+
ID: "1",
362+
AccountID: "1",
363+
Name: "example.com",
364+
},
365+
}
366+
367+
recordsByZoneID := map[string][]civogo.DNSRecord{
368+
"example.com": {
369+
{
370+
ID: "1",
371+
AccountID: "1",
372+
DNSDomainID: "1",
373+
Name: "txt",
374+
Value: "12.12.12.1",
375+
Type: "A",
376+
TTL: 600,
377+
},
378+
},
379+
}
380+
381+
updatesByZone := map[string][]*endpoint.Endpoint{
382+
"example.com": {
383+
endpoint.NewEndpoint("foo.example.com", "AAAA", "1.2.3.4"),
384+
endpoint.NewEndpoint("txt.example.com", endpoint.RecordTypeCNAME, "foo.example.com"),
385+
},
386+
}
387+
388+
var changes CivoChanges
389+
err := processUpdateActions(zoneByID, recordsByZoneID, updatesByZone, &changes)
390+
require.Error(t, err)
391+
}
392+
258393
func TestCivoProcessUpdateActions(t *testing.T) {
259394
zoneByID := map[string]civogo.DNSDomain{
260395
"example.com": {
@@ -515,6 +650,64 @@ func TestCivoApplyChanges(t *testing.T) {
515650
assert.NoError(t, err)
516651
}
517652

653+
func TestCivoApplyChangesError(t *testing.T) {
654+
client, server, _ := civogo.NewAdvancedClientForTesting([]civogo.ConfigAdvanceClientForTesting{
655+
{
656+
Method: "GET",
657+
Value: []civogo.ValueAdvanceClientForTesting{
658+
{
659+
RequestBody: "",
660+
URL: "/v2/dns",
661+
ResponseBody: `[{"id": "12345", "account_id": "1", "name": "example.com"}]`,
662+
},
663+
{
664+
RequestBody: "",
665+
URL: "/v2/dns/12345/records",
666+
ResponseBody: `[]`,
667+
},
668+
},
669+
},
670+
})
671+
672+
defer server.Close()
673+
674+
provider := &CivoProvider{
675+
Client: *client,
676+
}
677+
678+
cases := []struct {
679+
Name string
680+
changes *plan.Changes
681+
}{
682+
{
683+
Name: "invalid record type from processCreateActions",
684+
changes: &plan.Changes{
685+
Create: []*endpoint.Endpoint{
686+
endpoint.NewEndpoint("bad.example.com", "AAAA", "1.2.3.4"),
687+
},
688+
},
689+
},
690+
{
691+
Name: "invalid record type from processUpdateActions",
692+
changes: &plan.Changes{
693+
UpdateOld: []*endpoint.Endpoint{
694+
endpoint.NewEndpoint("bad.example.com", "AAAA", "1.2.3.4"),
695+
},
696+
UpdateNew: []*endpoint.Endpoint{
697+
endpoint.NewEndpoint("bad.example.com", "AAAA", "5.6.7.8"),
698+
},
699+
},
700+
},
701+
}
702+
703+
for _, tt := range cases {
704+
t.Run(tt.Name, func(t *testing.T) {
705+
err := provider.ApplyChanges(context.Background(), tt.changes)
706+
assert.Equal(t, "invalid Record Type: AAAA", string(err.Error()))
707+
})
708+
}
709+
}
710+
518711
func TestCivoProviderFetchZones(t *testing.T) {
519712
client, server, _ := civogo.NewClientForTesting(map[string]string{
520713
"/v2/dns": `[
@@ -688,39 +881,19 @@ func TestCivo_submitChangesCreate(t *testing.T) {
688881
},
689882
},
690883
},
691-
})
692-
defer server.Close()
693-
694-
provider := &CivoProvider{
695-
Client: *client,
696-
DryRun: false,
697-
}
698-
699-
changes := CivoChanges{
700-
Creates: []*CivoChangeCreate{
701-
{
702-
Domain: civogo.DNSDomain{
703-
ID: "12345",
704-
AccountID: "1",
705-
Name: "example.com",
884+
{
885+
Method: "DELETE",
886+
Value: []civogo.ValueAdvanceClientForTesting{
887+
{
888+
URL: "/v2/dns/12345/records/76cc107f-fbef-4e2b-b97f-f5d34f4075d3",
889+
ResponseBody: `{"result": "success"}`,
706890
},
707-
Options: &civogo.DNSRecordConfig{
708-
Type: "MX",
709-
Name: "mail",
710-
Value: "10.0.0.1",
711-
Priority: 10,
712-
TTL: 600,
891+
{
892+
URL: "/v2/dns/12345/records/error-record-id",
893+
ResponseBody: `{"result": "error", "error": "failed to delete record"}`,
713894
},
714895
},
715896
},
716-
}
717-
718-
err := provider.submitChanges(context.Background(), changes)
719-
assert.NoError(t, err)
720-
}
721-
722-
func TestCivo_submitChangesUpdate(t *testing.T) {
723-
client, server, _ := civogo.NewAdvancedClientForTesting([]civogo.ConfigAdvanceClientForTesting{
724897
{
725898
Method: "PUT",
726899
Value: []civogo.ValueAdvanceClientForTesting{
@@ -738,43 +911,78 @@ func TestCivo_submitChangesUpdate(t *testing.T) {
738911
"ttl": 600
739912
}`,
740913
},
914+
{
915+
RequestBody: `{"type":"MX","name":"mail","value":"10.0.0.3","priority":10,"ttl":600}`,
916+
URL: "/v2/dns/12345/records/error-record-id",
917+
ResponseBody: `{"result": "error", "error": "failed to update record"}`,
918+
},
741919
},
742920
},
743921
})
744922
defer server.Close()
745923

746924
provider := &CivoProvider{
747925
Client: *client,
748-
DryRun: false,
926+
DryRun: true,
749927
}
750928

751-
changes := CivoChanges{
752-
Updates: []*CivoChangeUpdate{
753-
{
754-
Domain: civogo.DNSDomain{ID: "12345", AccountID: "1", Name: "example.com"},
755-
DomainRecord: civogo.DNSRecord{
756-
ID: "76cc107f-fbef-4e2b-b97f-f5d34f4075d3",
757-
AccountID: "1",
758-
DNSDomainID: "12345",
759-
Name: "mail",
760-
Value: "10.0.0.1",
761-
Type: "MX",
762-
Priority: 10,
763-
TTL: 600,
929+
cases := []struct {
930+
name string
931+
changes *CivoChanges
932+
expectedResult error
933+
}{
934+
{
935+
name: "changes slice is empty",
936+
changes: &CivoChanges{},
937+
expectedResult: nil,
938+
},
939+
{
940+
name: "changes slice has changes and update changes",
941+
changes: &CivoChanges{
942+
Creates: []*CivoChangeCreate{
943+
{
944+
Domain: civogo.DNSDomain{
945+
ID: "12345",
946+
AccountID: "1",
947+
Name: "example.com",
948+
},
949+
Options: &civogo.DNSRecordConfig{
950+
Type: "MX",
951+
Name: "mail",
952+
Value: "10.0.0.1",
953+
Priority: 10,
954+
TTL: 600,
955+
},
956+
},
764957
},
765-
Options: civogo.DNSRecordConfig{
766-
Type: "MX",
767-
Name: "mail",
768-
Value: "10.0.0.2",
769-
Priority: 10,
770-
TTL: 600,
958+
959+
Updates: []*CivoChangeUpdate{
960+
{
961+
Domain: civogo.DNSDomain{
962+
ID: "12345",
963+
AccountID: "2",
964+
Name: "example.org",
965+
},
966+
},
967+
{
968+
Domain: civogo.DNSDomain{
969+
ID: "67890",
970+
AccountID: "3",
971+
Name: "example.COM",
972+
},
973+
},
771974
},
772975
},
976+
expectedResult: nil,
773977
},
774978
}
775979

776-
err := provider.submitChanges(context.Background(), changes)
777-
assert.NoError(t, err)
980+
for _, c := range cases {
981+
t.Run(c.name, func(t *testing.T) {
982+
err := provider.submitChanges(context.Background(), *c.changes)
983+
assert.NoError(t, err)
984+
})
985+
}
778986
}
779987

780988
func TestCivo_submitChangesDelete(t *testing.T) {

0 commit comments

Comments
 (0)