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

✨ (deploy-image/v1alpha): Improve tests scaffolded for the controllers #4197

Conversation

mogsie
Copy link
Contributor

@mogsie mogsie commented Sep 28, 2024

Motivation: #4135

  • To(Not()) → NotTo() (It seems that NotTo is used a lot elsewhere in the repository)
  • Use default eventually timeouts, so we don't need to send them everywhere
  • Eventually(func() error) → Eventually(func(g Gomega)) And then use g.Expect inside those Eventually() functions
  • Use Gomega assertions for the condition check, which is more robust

@k8s-ci-robot k8s-ci-robot added do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Sep 28, 2024
@k8s-ci-robot
Copy link
Contributor

Hi @mogsie. Thanks for your PR.

I'm waiting for a kubernetes-sigs member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@mogsie mogsie force-pushed the improve-deploy-image-controller-test branch from e0e08f4 to bbb191d Compare September 28, 2024 16:45
@mogsie mogsie marked this pull request as ready for review September 28, 2024 16:47
@k8s-ci-robot k8s-ci-robot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Sep 28, 2024
@mogsie
Copy link
Contributor Author

mogsie commented Sep 28, 2024

I have the possibility of rewriting that last eventually as follows:

Eventually(func(g Gomega) {
	conditions := []metav1.Condition{}
	g.Expect(busybox.Status.Conditions).To(ContainElement(
		HaveField("Type", Equal(typeAvailableBusybox)), &conditions))
	g.Expect(conditions).To(HaveLen(1), "Multiple conditions of type "+typeAvailableBusybox)
	g.Expect(conditions[0].Status).To(Equal(metav1.ConditionTrue))
	g.Expect(conditions[0].Reason).To(Equal("Reconciling"))
}).Should(Succeed())

This would improve the robustness of the test, as the test wouldn't be checking the non-computer friendly message but only the fields that constitute the API: Type, Status, Reason. It would also be testing for duplicates in the status.

The trick is that g.Expect().To(ContainElement(..., &conditions)) actually fills out the &conditions with the found elements, i.e. the one(s) that have the right Type. There should only be one in the status.conditions. This way it only looks for the condition that has the right Type and then later we assert that that type has the correct status and reason.

The older test would ignore multiple duplicate conditions (i.e. conditions of the same type), since it only would assert that one status matched.

I will add this too.

@mogsie mogsie force-pushed the improve-deploy-image-controller-test branch 3 times, most recently from bdb9391 to 84980cf Compare September 28, 2024 19:12
@mogsie mogsie force-pushed the improve-deploy-image-controller-test branch from 84980cf to d629145 Compare September 28, 2024 19:53
Copy link
Member

@camilamacedo86 camilamacedo86 left a comment

Choose a reason for hiding this comment

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

/lgtm

@k8s-ci-robot k8s-ci-robot added lgtm "Looks good to me", indicates that a PR is ready to be merged. approved Indicates a PR has been approved by an approver from all required OWNERS files. labels Sep 29, 2024
@camilamacedo86 camilamacedo86 changed the title 🐛 Impove deploy-image controller-test.go ✨ (deploy-image/v1alpha): Improve tests scaffolded for the controllers Sep 29, 2024
@camilamacedo86
Copy link
Member

/ok-to-test

@k8s-ci-robot k8s-ci-robot added ok-to-test Indicates a non-member PR verified by an org member that is safe to test. and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Sep 29, 2024
@mogsie mogsie force-pushed the improve-deploy-image-controller-test branch from d629145 to 322e871 Compare September 29, 2024 08:57
@k8s-ci-robot k8s-ci-robot removed the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Sep 29, 2024
Fixes a weakness in the controller-test.  Previously, the test did not
check for duplicates.  Duplicate conditions can be a problem, and this
new way of writing the test ensures that this is caught early.

* To(Not()) → NotTo()
* Use default eventually timeouts
* Eventually(func() error) → Eventually(func(g Gomega)), and then use
  g.Expect inside those Eventually() functions
* Reconcile a second time, since the first time, it is still "unknown"
* Remove use of Eventually when nothing else is happening
* Use Gomega assertions for the condition check
* Check for uniqueness for the conditions
* Use gomega's format string support to explain the context of failures
@mogsie mogsie force-pushed the improve-deploy-image-controller-test branch from 322e871 to 618a87b Compare September 29, 2024 10:10
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

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Sep 30, 2024
@k8s-ci-robot k8s-ci-robot merged commit e1546d4 into kubernetes-sigs:master Sep 30, 2024
23 checks passed
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: camilamacedo86, mogsie, varshaprasad96

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:
  • OWNERS [camilamacedo86,varshaprasad96]

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants