Skip to content

Commit

Permalink
Add Test Case for Create in Test_createOrUpdateDNSRecord
Browse files Browse the repository at this point in the history
Test_createOrUpdateDNSRecord previously only tested the update logic.
In order to test the create logic, `CreateDNSRecord` needed to be
implemented in `fake_client.go`. Additionally, the new test case
required the ability to control the results of `ListAllDnsRecords`,
which were previously hardcoded. As a result, `OutputResults` was
added to the `ListAllDnsRecordsInputOutput` struct, allowing the new
test cases to specify no result (indicating no existing DNS record)
so we can trigger the create logic.
  • Loading branch information
gcs278 committed Aug 22, 2024
1 parent 6cc0559 commit ea74d3b
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 3 deletions.
52 changes: 52 additions & 0 deletions pkg/dns/ibm/public/cis_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -164,6 +177,7 @@ func Test_createOrUpdateDNSRecord(t *testing.T) {
recordedCall string
listAllDnsRecordsInputOutput dnsclient.ListAllDnsRecordsInputOutput
updateDnsRecordInputOutput dnsclient.UpdateDnsRecordInputOutput
createDnsRecordInputOutput dnsclient.CreateDnsRecordInputOutput
expectErrorContains string
}{
{
Expand All @@ -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",
Expand Down Expand Up @@ -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",
Expand All @@ -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: "",
Expand All @@ -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)

Expand Down
26 changes: 23 additions & 3 deletions pkg/dns/ibm/public/client/fake_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
type FakeDnsClient struct {
CallHistory map[string]string
DeleteDnsRecordInputOutput DeleteDnsRecordInputOutput
CreateDnsRecordInputOutput CreateDnsRecordInputOutput
ListAllDnsRecordsInputOutput ListAllDnsRecordsInputOutput
UpdateDnsRecordInputOutput UpdateDnsRecordInputOutput
}
Expand All @@ -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) {
Expand All @@ -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,
Expand All @@ -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) {
Expand Down

0 comments on commit ea74d3b

Please sign in to comment.