ReconcileCOSInstance Unit Test: cosClient resetting and losing mocked properties #2034
Labels
area/provider/ibmcloud
Issues or PRs related to ibmcloud provider
kind/bug
Categorizes issue or PR as related to a bug.
Milestone
/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.
cluster-api-provider-ibmcloud/cloud/scope/powervs_cluster.go
Lines 2211 to 2215 in 2a3745e
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:
kubectl version
):/etc/os-release
):The text was updated successfully, but these errors were encountered: