Skip to content

ReconcileCOSInstance Unit Test: cosClient resetting and losing mocked properties #2034

Open
@carmal891

Description

@carmal891

/kind bug
/area provider/ibmcloud

What steps did you take and what happened:

When writing a unit test for the ReconcileCOSInstance function, encountered an issue due to resetting of the cosClient in below lines.

cosClient, err := cos.NewService(cosOptions, apiKey, *cosServiceInstanceStatus.GUID)
if err != nil {
return fmt.Errorf("failed to create COS client: %w", err)
}
s.COSClient = cosClient

The current implementation does not retain properties set in the mock cosClient, resulting in unexpected behavior and test failures. This problem occurs due to the reinitialization of cosClient within the ReconcileCOSInstance function, which overwrites the mock with a new instance, causing loss of the mocked configurations essential for downstream checks

What did you expect to happen:

The test case expects the cosClient to retain the properties set in the mock for verification, specifically for the checkCOSBucket function called later in the control flow. However, reinitializing the cosClient here resets the mock, so any properties set on the mock are lost, and checkCOSBucket cannot be validated as expected.

Anything else you would like to add:

As a solution suggestion would be to adapt the existing ClientFactory pattern to enable more flexible client mocking in tests.

Currently, ClientFactory allows overriding clients like PowerVSClientFactory, VPCClientFactory, etc., using custom factory functions for testing. By adding a new CosClientFactory function to ClientFactory, we can dynamically replace cosClient with a mock version and retain its mock configurations during the test

Environment:

  • Cluster-api version:
  • Minikube/KIND version:
  • Kubernetes version: (use kubectl version):
  • OS (e.g. from /etc/os-release):

Metadata

Metadata

Assignees

Labels

area/provider/ibmcloudIssues or PRs related to ibmcloud providerkind/bugCategorizes issue or PR as related to a bug.

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions