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

refactor and add unit tests for operator reconciler #115

Merged

Conversation

joelanford
Copy link
Member

@joelanford joelanford commented Feb 2, 2023

Another facet of #107, covering the following:

  • move reconciler unit tests to a separate file from the suite. if we add other controllers in the future, we'll likely want separate test files for each controller.
  • export the reconciler's resolver field to make it easy to swap out for tests (also add a test entity source)
  • remove the manager setup in the unit tests. this ensures we can test individual runs of the Reconcile function, which is good for two reasons:
    • no need to use Eventually, no polling, etc. so it speeds up the tests
    • it ensures that we fully understand the individual transitions that occur from a beginning state to an end state when multiple Reconcile calls are necessary. (e.g. r1: create BD, r2: update Op based on BD status change to unpacking, r3: update Op again based on BD status change to installed successfully)
      added more unit tests to increase code coverage.
  • moved conditions and reasons checks to invariants that run after every test of reconciling an existing Operator object.

Closes #86

Copy link
Contributor

@tmshort tmshort left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make test-unit locally succeeds
make e2e locally succeeds
But make test locally fails (even after a rebase to main). It passes on main, however.
(make test should apparently be part of the CI)
It's actually failing on make test-e2e...

if err != nil {
status = metav1.ConditionTrue
status = metav1.ConditionFalse
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this remain ConditionTrue as resolution did fail?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ConditionTrue is saying Ready: True. Since resolution failed, shouldn't we be saying Ready: false?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As an aside, I think the structure of how these condition values gets set makes it difficult to reason about. I've fixed this in #107, so as soon as this change lands, I can layer in another PR that simplifies the Reconcile function and makes it more obvious how/when the ready condition is set.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right. I was conflating "Resolution failed" with the type.

OK, I think the confusion is that it's not obvious this is for the "Ready" type, since TypeReady is not very local, it's down 30+ lines.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

^ Exactly - That problem will be resolved in #118.

// to ensure that exec-entrypoint and run can make use of them.
_ "k8s.io/client-go/plugin/pkg/client/auth"

rukpakv1alpha1 "github.com/operator-framework/rukpak/api/v1alpha1"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wondering why this moved up here (i.e. away from the other github.com imports). I have seen go fmt move things around, but it seems fine with this.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yeah, that my IDE running gosimports (https://github.com/rinchsan/gosimports) to automatically organize imports into groups and sort them.

@joelanford
Copy link
Member Author

Re: make test, I think that's broken? If I clear out my kind clusters and run make test, it fails on main.

make test only include test-e2e, but not e2e, so the kind cluster setup doesn't happen.

Copy link
Member

@varshaprasad96 varshaprasad96 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/lgtm

By("running reconcile")
res, err := reconciler.Reconcile(ctx, ctrl.Request{NamespacedName: opKey})
Expect(res).To(Equal(ctrl.Result{}))
// TODO: should resolution failure return an error?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should probably trigger reconcile again with the error that we are setting in the condition.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1, I've got that fixed up in #118. When this merges, I'll rebase that one.

Expect(cond).NotTo(BeNil())
Expect(cond.Status).To(Equal(metav1.ConditionTrue))
Expect(cond.Reason).To(Equal(operatorsv1alpha1.ReasonResolutionSucceeded))
Expect(cond.Message).To(Equal("resolution was successful"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Not related to the PR, but just noticed) We should probably define separate error types and constants for the messages to make it easier for users to check the status message.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not opposed to constants for messages we reuse, but I'm pretty sure condition messages are not considered stable or part of the API a consumer is supposed to depend on.

Copy link
Contributor

@tmshort tmshort Feb 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with Joe. Reason is the programatic interface.
But using constants for messages is not a bad idea, either.

@openshift-ci openshift-ci bot added the lgtm Indicates that a PR is ready to be merged. label Feb 3, 2023
@tmshort
Copy link
Contributor

tmshort commented Feb 3, 2023

Re: make test, I think that's broken? If I clear out my kind clusters and run make test, it fails on main.

make test only include test-e2e, but not e2e, so the kind cluster setup doesn't happen.

See #119

@joelanford
Copy link
Member Author

@tmshort anything else you want to review/discuss on this one?

@tmshort
Copy link
Contributor

tmshort commented Feb 6, 2023

@tmshort anything else you want to review/discuss on this one?

Nope, I fixed the tests in #119, and got my head around the conditions, I think you're good.

Are you concerned about the go-apidiff failure (assuming not, since this is still in development)?

/lgtm

@joelanford
Copy link
Member Author

Not concerned about the apidiff failure. While we're in development, the purpose of that is to inform us of the changes we're making to the public API, and potentially have discussion about it. Honestly, the main thing I'm watching for at this stage is unnecessarily adding new surface area to the public API. Not really at all concerns about removals or changes at this point.

@joelanford joelanford merged commit e596002 into operator-framework:main Feb 7, 2023
@joelanford joelanford deleted the reconciler-unit-tests branch February 7, 2023 04:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lgtm Indicates that a PR is ready to be merged.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Start using envtest for testing reconciler
3 participants