diff --git a/pkg/dns/ibm/public/cis_provider_test.go b/pkg/dns/ibm/public/cis_provider_test.go index fdb2e7d46b..a8e8aa30f0 100644 --- a/pkg/dns/ibm/public/cis_provider_test.go +++ b/pkg/dns/ibm/public/cis_provider_test.go @@ -11,6 +11,10 @@ import ( "github.com/stretchr/testify/assert" dnsclient "github.com/openshift/cluster-ingress-operator/pkg/dns/ibm/public/client" + + "k8s.io/utils/pointer" + + "github.com/IBM/networking-go-sdk/dnsrecordsv1" ) func Test_Delete(t *testing.T) { @@ -43,6 +47,9 @@ func Test_Delete(t *testing.T) { listAllDnsRecordsInputOutput: dnsclient.ListAllDnsRecordsInputOutput{ OutputError: nil, OutputStatusCode: http.StatusOK, + OutputResults: []dnsrecordsv1.DnsrecordDetails{{ + ID: pointer.String("testDelete"), + }}, }, deleteDnsRecordInputOutput: dnsclient.DeleteDnsRecordInputOutput{ InputId: "testDelete", @@ -79,6 +86,9 @@ func Test_Delete(t *testing.T) { listAllDnsRecordsInputOutput: dnsclient.ListAllDnsRecordsInputOutput{ OutputError: nil, OutputStatusCode: http.StatusOK, + OutputResults: []dnsrecordsv1.DnsrecordDetails{{ + ID: pointer.String("testDeleteRecordNotFound"), + }}, }, deleteDnsRecordInputOutput: dnsclient.DeleteDnsRecordInputOutput{ InputId: "testDeleteRecordNotFound", @@ -93,6 +103,9 @@ func Test_Delete(t *testing.T) { listAllDnsRecordsInputOutput: dnsclient.ListAllDnsRecordsInputOutput{ OutputError: nil, OutputStatusCode: http.StatusOK, + OutputResults: []dnsrecordsv1.DnsrecordDetails{{ + ID: pointer.String("testDeleteError"), + }}, }, deleteDnsRecordInputOutput: dnsclient.DeleteDnsRecordInputOutput{ InputId: "testDeleteError", @@ -164,6 +177,7 @@ func Test_createOrUpdateDNSRecord(t *testing.T) { recordedCall string listAllDnsRecordsInputOutput dnsclient.ListAllDnsRecordsInputOutput updateDnsRecordInputOutput dnsclient.UpdateDnsRecordInputOutput + createDnsRecordInputOutput dnsclient.CreateDnsRecordInputOutput expectErrorContains string }{ { @@ -173,6 +187,9 @@ func Test_createOrUpdateDNSRecord(t *testing.T) { listAllDnsRecordsInputOutput: dnsclient.ListAllDnsRecordsInputOutput{ OutputError: nil, OutputStatusCode: http.StatusOK, + OutputResults: []dnsrecordsv1.DnsrecordDetails{{ + ID: pointer.String("testUpdate"), + }}, }, updateDnsRecordInputOutput: dnsclient.UpdateDnsRecordInputOutput{ InputId: "testUpdate", @@ -209,6 +226,9 @@ func Test_createOrUpdateDNSRecord(t *testing.T) { listAllDnsRecordsInputOutput: dnsclient.ListAllDnsRecordsInputOutput{ OutputError: nil, OutputStatusCode: http.StatusOK, + OutputResults: []dnsrecordsv1.DnsrecordDetails{{ + ID: pointer.String("testUpdateError"), + }}, }, updateDnsRecordInputOutput: dnsclient.UpdateDnsRecordInputOutput{ InputId: "testUpdateError", @@ -217,6 +237,37 @@ func Test_createOrUpdateDNSRecord(t *testing.T) { }, expectErrorContains: "error in UpdateDnsRecord", }, + { + desc: "create happy path", + DNSName: "testCreate", + recordedCall: "CREATE", + listAllDnsRecordsInputOutput: dnsclient.ListAllDnsRecordsInputOutput{ + OutputError: nil, + OutputStatusCode: http.StatusOK, + OutputResults: []dnsrecordsv1.DnsrecordDetails{}, + }, + createDnsRecordInputOutput: dnsclient.CreateDnsRecordInputOutput{ + Name: "testCreate", + OutputError: nil, + OutputStatusCode: http.StatusOK, + }, + }, + { + desc: "createError", + DNSName: "testCreateError", + recordedCall: "CREATE", + listAllDnsRecordsInputOutput: dnsclient.ListAllDnsRecordsInputOutput{ + OutputError: nil, + OutputStatusCode: http.StatusOK, + OutputResults: []dnsrecordsv1.DnsrecordDetails{}, + }, + createDnsRecordInputOutput: dnsclient.CreateDnsRecordInputOutput{ + Name: "testCreateError", + OutputError: errors.New("error in CreateDnsRecord"), + OutputStatusCode: http.StatusRequestTimeout, + }, + expectErrorContains: "error in CreateDnsRecord", + }, { desc: "empty DNSName", DNSName: "", @@ -239,6 +290,7 @@ func Test_createOrUpdateDNSRecord(t *testing.T) { dnsService.ListAllDnsRecordsInputOutput = tc.listAllDnsRecordsInputOutput dnsService.UpdateDnsRecordInputOutput = tc.updateDnsRecordInputOutput + dnsService.CreateDnsRecordInputOutput = tc.createDnsRecordInputOutput err = provider.createOrUpdateDNSRecord(&record, zone) diff --git a/pkg/dns/ibm/public/client/fake_client.go b/pkg/dns/ibm/public/client/fake_client.go index 13ccd1cd0a..b0f0dbfe2f 100644 --- a/pkg/dns/ibm/public/client/fake_client.go +++ b/pkg/dns/ibm/public/client/fake_client.go @@ -10,6 +10,7 @@ import ( type FakeDnsClient struct { CallHistory map[string]string DeleteDnsRecordInputOutput DeleteDnsRecordInputOutput + CreateDnsRecordInputOutput CreateDnsRecordInputOutput ListAllDnsRecordsInputOutput ListAllDnsRecordsInputOutput UpdateDnsRecordInputOutput UpdateDnsRecordInputOutput } @@ -26,9 +27,16 @@ type UpdateDnsRecordInputOutput struct { OutputStatusCode int } +type CreateDnsRecordInputOutput struct { + Name string + OutputError error + OutputStatusCode int +} + type ListAllDnsRecordsInputOutput struct { OutputError error OutputStatusCode int + OutputResults []dnsrecordsv1.DnsrecordDetails } func NewFake() (*FakeDnsClient, error) { @@ -43,7 +51,7 @@ func (c *FakeDnsClient) RecordedCall(zoneID string) (string, bool) { func (fdc FakeDnsClient) ListAllDnsRecords(listAllDnsRecordsOptions *dnsrecordsv1.ListAllDnsRecordsOptions) (result *dnsrecordsv1.ListDnsrecordsResp, response *core.DetailedResponse, err error) { fakeListDnsrecordsResp := &dnsrecordsv1.ListDnsrecordsResp{} - fakeListDnsrecordsResp.Result = append(fakeListDnsrecordsResp.Result, dnsrecordsv1.DnsrecordDetails{ID: listAllDnsRecordsOptions.Name}) + fakeListDnsrecordsResp.Result = fdc.ListAllDnsRecordsInputOutput.OutputResults resp := &core.DetailedResponse{ StatusCode: fdc.ListAllDnsRecordsInputOutput.OutputStatusCode, @@ -55,8 +63,20 @@ func (fdc FakeDnsClient) ListAllDnsRecords(listAllDnsRecordsOptions *dnsrecordsv return fakeListDnsrecordsResp, resp, fdc.ListAllDnsRecordsInputOutput.OutputError } -func (FakeDnsClient) CreateDnsRecord(createDnsRecordOptions *dnsrecordsv1.CreateDnsRecordOptions) (result *dnsrecordsv1.DnsrecordResp, response *core.DetailedResponse, err error) { - return nil, nil, nil +func (fdc FakeDnsClient) CreateDnsRecord(createDnsRecordOptions *dnsrecordsv1.CreateDnsRecordOptions) (result *dnsrecordsv1.DnsrecordResp, response *core.DetailedResponse, err error) { + if fdc.CreateDnsRecordInputOutput.Name != *createDnsRecordOptions.Name { + return nil, nil, errors.New("createDnsRecord: inputs don't match") + } + + resp := &core.DetailedResponse{ + StatusCode: fdc.CreateDnsRecordInputOutput.OutputStatusCode, + Headers: map[string][]string{}, + Result: result, + RawResult: []byte{}, + } + + fdc.CallHistory[*createDnsRecordOptions.Name] = "CREATE" + return nil, resp, fdc.CreateDnsRecordInputOutput.OutputError } func (fdc FakeDnsClient) DeleteDnsRecord(deleteDnsRecordOptions *dnsrecordsv1.DeleteDnsRecordOptions) (result *dnsrecordsv1.DeleteDnsrecordResp, response *core.DetailedResponse, err error) {