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

Can't unit test own usage of InClusterFactory #103

Open
phantomjinx opened this issue Feb 22, 2022 · 6 comments
Open

Can't unit test own usage of InClusterFactory #103

phantomjinx opened this issue Feb 22, 2022 · 6 comments
Labels
lifecycle/frozen Indicates that an issue or PR should not be auto-closed due to staleness.

Comments

@phantomjinx
Copy link

Bug Report

What did you do?
Created a wrapper function in our operator code that finds the name of the associated OLM OperatorCondition and then uses the InClusterFactory API to set the upgradeable condition. Then we started writing tests to test this function.

What did you expect to see?
Tests to pass or fail depending on the correct writing of the wrapper function and calling the correct InClusterFactory and Condition code.

What did you see instead? Under which circumstances?
The error: get operator condition namespace: namespace not found for current environment.
Since the unit tests are using a fake runtime client and running locally, this was to be expected since InClusterFactory.GetNamespacedName() calls utils.GetOperatorNamespace(). However, what was unexpected was in our tests there is no way to override this since:

  • utils package is internal so private
  • readNamespace is private
  • the library's own tests can override since they share the same package as readNamespace.

Environment

  • operator-lib version: 0.9.x
  • github.com/operator-framework/operator-lib v0.9.0

Possible Solutions

  • make readNamespace public
  • provide an interface to allow GetNamespacedName to be overridden
  • allow the namespace to be injected into InClusterFactory (since the operator provides the namespace through request.namespace, it might be a nice alternative?)

Additional context
Add any other context about the problem here.

@phantomjinx phantomjinx changed the title Can unit test own usage of InClusterFactory Can't unit test own usage of InClusterFactory Feb 22, 2022
@nunnatsa
Copy link
Contributor

Very similar to #50

@joelanford
Copy link
Member

The purpose of the PR (#58) that introduced the InClusterFactory is that you could substitute that out in your tests and use a separate implementation of conditions.Factory such that GetNamespacedName() can return whatever you need it to.

@phantomjinx
Copy link
Author

The purpose of the PR (#58) that introduced the InClusterFactory is that you could substitute that out in your tests and use a separate implementation of conditions.Factory such that GetNamespacedName() can return whatever you need it to.

I did try that earlier but found that condition type is also private -> https://github.com/operator-framework/operator-lib/blob/v0.9.x/conditions/factory.go#L49.

So am not sure how that would help. My interface would have to return a NewCondition of type Condition * but that would then require a re-implementation of Get and Set. Either way it would be a lot of work just to workaround for a unit test.

If it would help my code is located here:

@joelanford
Copy link
Member

Yeah Condition is also an interface. Perhaps we need to implement a Factory and Condition that are more suitable for use in tests so that operator-lib users aren't all re-inventing the wheel when they need alternates.

However, I don't think we want to change anything about the functionality of the InClusterFactory because it is meant to work for in-cluster use cases only. Background here: #50 (comment)

phantomjinx added a commit to phantomjinx/syndesis that referenced this issue Feb 23, 2022
* When operator is installed via OLM, a separate OperatorCondition
  CR is created which can be updated with an "upgradeable"
  condition flagging whether the OLM can upgrade the operator

* Turn off upgradeable state when
 * operator is initializing
 * operator is upgrading

* Turn on upgradeable state when
 * operator has started
 * operator has completed an upgrade

* Finds the OperatorCondition using the deployment name, deployment
  owner (CSV) and the CSV name (OperatorCondition has same name as
  the CSV).

* Adds conditions tests but cannot completely implement due to
  operator-framework/operator-lib#103
phantomjinx added a commit to phantomjinx/syndesis that referenced this issue Feb 23, 2022
* When operator is installed via OLM, a separate OperatorCondition
  CR is created which can be updated with an "upgradeable"
  condition flagging whether the OLM can upgrade the operator

* Turn off upgradeable state when
 * operator is initializing
 * operator is upgrading

* Turn on upgradeable state when
 * operator has started
 * operator has completed an upgrade

* Finds the OperatorCondition using the deployment name, deployment
  owner (CSV) and the CSV name (OperatorCondition has same name as
  the CSV).

* Adds conditions tests but cannot completely implement due to
  operator-framework/operator-lib#103
@openshift-bot
Copy link

Issues go stale after 90d of inactivity.

Mark the issue as fresh by commenting /remove-lifecycle stale.
Stale issues rot after an additional 30d of inactivity and eventually close.
Exclude this issue from closing by commenting /lifecycle frozen.

If this issue is safe to close now please do so with /close.

/lifecycle stale

@openshift-ci openshift-ci bot added the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label May 24, 2022
@joelanford
Copy link
Member

/lifecycle frozen

Perhaps we need to implement a Factory and Condition that are more suitable for use in tests so that operator-lib users aren't all re-inventing the wheel when they need alternates.

We'd be happy to accept a PR that adds MockFactory and MockCondition structs that implement these interfaces so that these can be subbed in during unit tests for fine-grained testing of the OperatorCondition feature.

@openshift-ci openshift-ci bot added lifecycle/frozen Indicates that an issue or PR should not be auto-closed due to staleness. and removed lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. labels May 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lifecycle/frozen Indicates that an issue or PR should not be auto-closed due to staleness.
Projects
None yet
Development

No branches or pull requests

4 participants