Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
carmal891 opened this issue Nov 5, 2024 · 1 comment
Assignees
Labels
area/provider/ibmcloud Issues or PRs related to ibmcloud provider kind/bug Categorizes issue or PR as related to a bug.

Comments

@carmal891
Copy link
Contributor

carmal891 commented Nov 5, 2024

/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):
@k8s-ci-robot k8s-ci-robot added kind/bug Categorizes issue or PR as related to a bug. area/provider/ibmcloud Issues or PRs related to ibmcloud provider labels Nov 5, 2024
@carmal891 carmal891 reopened this Nov 5, 2024
@carmal891
Copy link
Contributor Author

/assign

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/provider/ibmcloud Issues or PRs related to ibmcloud provider kind/bug Categorizes issue or PR as related to a bug.
Projects
None yet
Development

No branches or pull requests

2 participants