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

Add envtest controller tests for DevWorkspaces #957

Merged
merged 21 commits into from
Dec 14, 2022

Conversation

amisevsk
Copy link
Collaborator

What does this PR do?

Sets up tooling and configuration to allow running controller tests via envtest. These tests work by launching a control plane and allowing the controller to "interact" with it -- i.e. creating DevWorkspaces and related objects.

Currently a draft PR as this is a revival of something I started back in January and hadn't touched again until a few weeks ago; I'm not sure the overall quality.

Tests can still be run without installing the ginkgo CLI, but the output is nicer if it's present.

Controller-gen assumes a newer version of ginkgo/gomega than was used previously; these packages are updated and that requires updates to existing e2e test code.

Docs:

What issues does this PR fix or reference?

Closes #930

Is it tested? How?

They're tests :)

PR Checklist

  • E2E tests pass (when PR is ready, comment /test v8-devworkspace-operator-e2e, v8-che-happy-path to trigger)
    • v8-devworkspace-operator-e2e: DevWorkspace e2e test
    • v8-che-happy-path: Happy path for verification integration with Che

@openshift-ci
Copy link

openshift-ci bot commented Oct 22, 2022

Skipping CI for Draft Pull Request.
If you want CI signal for your change, please convert it to an actual PR.
You can still manually trigger a test run with /test all

@amisevsk amisevsk force-pushed the test-coverage branch 2 times, most recently from 970f556 to 755db23 Compare October 22, 2022 05:16
@codecov
Copy link

codecov bot commented Oct 22, 2022

Codecov Report

Base: 54.11% // Head: 52.36% // Decreases project coverage by -1.74% ⚠️

Coverage data is based on head (b54b578) compared to base (255d699).
Patch coverage: 16.66% of modified lines in pull request are covered.

❗ Current head b54b578 differs from pull request most recent head e1128bc. Consider uploading reports for the commit e1128bc to get more accurate results

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #957      +/-   ##
==========================================
- Coverage   54.11%   52.36%   -1.75%     
==========================================
  Files          44       46       +2     
  Lines        2770     4289    +1519     
==========================================
+ Hits         1499     2246     +747     
- Misses       1135     1841     +706     
- Partials      136      202      +66     
Impacted Files Coverage Δ
pkg/config/sync.go 65.21% <0.00%> (-6.05%) ⬇️
controllers/workspace/status.go 75.58% <100.00%> (ø)
pkg/library/overrides/containers.go 63.15% <0.00%> (-12.52%) ⬇️
pkg/provision/storage/ephemeralStorage.go 28.00% <0.00%> (-7.30%) ⬇️
pkg/library/flatten/flatten.go 77.82% <0.00%> (-3.54%) ⬇️
pkg/library/container/mountSources.go 89.83% <0.00%> (-2.86%) ⬇️
pkg/provision/storage/shared.go 59.39% <0.00%> (-2.59%) ⬇️
pkg/library/flatten/merge.go 75.13% <0.00%> (-1.61%) ⬇️
pkg/config/migrate.go 85.57% <0.00%> (-1.61%) ⬇️
... and 44 more

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

Copy link
Collaborator

@AObuchow AObuchow left a comment

Choose a reason for hiding this comment

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

Looks awesome to me so far :)

One additional note:
The README should probably be updated to specify that go v1.17 is required, as well as Ginkgo CLI v2.0.0 (I initially tried v.2.4.0, and it wouldn't work).

By("Loading DevWorkspace from test file")
devworkspace := &dw.DevWorkspace{}
err := loadObjectFromFile(name, devworkspace, fromFile)
Expect(err).NotTo(HaveOccurred())
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is purely stylistic, but error checks for loadObjectFromFile like this one could be written: Expect(loadObjectFromFile(name, devworkspace, fromFile)).Should(Succeed())

Obviously, there's a tradeoff: you lose a line of code and don't care have to declare err, but it's a bit more verbose of a line to write out.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Good idea -- updated the PR.

}
}

func namespacedName(name, namespace string) types.NamespacedName {
Copy link
Collaborator

Choose a reason for hiding this comment

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

In a future PR, we probably should add a similar function to use elsewhere in the codebase 👍

Copy link
Collaborator

Choose a reason for hiding this comment

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

Let me know if you think this is worthwhile, and I'll create an issue about it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Eh, I think it's probably not worth a separate package that has to be imported everywhere. Golang is a language that includes code duplication by design 🤷

Copy link
Collaborator

Choose a reason for hiding this comment

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

probably not worth a separate package that has to be imported everywhere

Agreed +1

Golang is a language that includes code duplication by design 🤷

Very true ;P

make test
-
name: Build Codecov report
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is there a particular reason why this step was added to the PR CI action? The code-coverage CI action already occurs on pull-requests.

I know codecov was acting weird for this PR - I assume it's because there's no controller.cover.out being referenced by codecov on the main branch, so it has nothing to compare the coverage against.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Note that the Codecov action was also disabled for PRs; previously we were running the tests as part of the PR check and generating coverage files, and then running them again as part of the Codecov action; it's easier to just upload the already-generated files.

I should break this out into a separate PR perhaps; maybe that would fix the codecov weirdness?

Copy link
Collaborator

Choose a reason for hiding this comment

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

it's easier to just upload the already-generated files

Agreed, it'll speed things up. If we do this, it's probably worth removing the PR trigger for the code coverage action (and only have it run on push to main), cause otherwise it will get run twice for PR's (I would imagine?).

I should break this out into a separate PR perhaps; maybe that would fix the codecov weirdness?

It should fix the weirdness because then there will be a reference point of coverage % for controller.cover.out on the main branch. But if this causes other problems, then it's totally fine to leave it. Future PR's should not have this issue once the controller.cover.out is uploaded to codecov.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Agreed, it'll speed things up. If we do this, it's probably worth removing the PR trigger for the code coverage action (and only have it run on push to main), cause otherwise it will get run twice for PR's (I would imagine?).

I thought I had already done this -- https://github.com/devfile/devworkspace-operator/pull/957/files#diff-49708f979e226a1e7bd7a68d71b2e91aae8114dd3e9254d9830cd3b4d62d4303L7-L8. Is anything else required?

My understanding is that codecov merges all uploaded files: https://docs.codecov.com/docs/merging-reports. I think this means that it should correctly compute a diff even if we were to e.g. split coverage reports per package.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I thought I had already done this -- #957 (files). Is anything else required?

Oops I forgot about that when I wrote this - that should be all that's needed for the code coverage action.

My understanding is that codecov merges all uploaded files: docs.codecov.com/docs/merging-reports. I think this means that it should correctly compute a diff even if we were to e.g. split coverage reports per package.

Yes, that seems to be the case. My theory about the controller.cover.out not being present on master doesn't seem to hold up then.

I think it's okay to just include the CI changes in this PR and if codecov continues to display incorrect coverage reports after this PR is merged, I will look into it.

@openshift-ci
Copy link

openshift-ci bot commented Dec 6, 2022

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: amisevsk, AObuchow, ibuziuk

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:

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

Update to testing packages Ginkgo and Gomega to version expected by
kubebuilder bootstrapped test files.

This requires minor updates to the previous end to end tests as Ginkgo
v2 does not support custom reporters in the same way.

Signed-off-by: Angel Misevski <[email protected]>
Bootstrap test files for DevWorkspace controller and add a basic
DevWorkspace creation test.

Signed-off-by: Angel Misevski <[email protected]>
Add controller tests to cover creation of various objects used
workspaces (roles, rolebindings, devworkspaceroutings, deployments,
etc.)

Verify that each created object is owned by the DevWorkspace that
defines it.

Signed-off-by: Angel Misevski <[email protected]>
Introduce a fake http.Transport to allow simulating responses to HTTP
calls in DevWorkspace Operator tests. This is required for testing that
DevWorkspaces with mainUrls can enter the Running state.

Signed-off-by: Angel Misevski <[email protected]>
Add test to verify that workspaces can enter the running state

Signed-off-by: Angel Misevski <[email protected]>
Fix issue where workspaces with a mainUrl with path that did not end in
a '/' resulted in the incorrect path for the '/healthz' endpoint due to
DWO only concatenating strings.

Signed-off-by: Angel Misevski <[email protected]>
Extract common test functionality to functions that can be reused to
make test code less painfully long.

Signed-off-by: Angel Misevski <[email protected]>
Add test cases covering automatic mounting of image pull secrets,
configmaps/secrets, git credentials.

Signed-off-by: Angel Misevski <[email protected]>
Rework makefile rules to automatically download binaries needed for
running envtest tests. New versions of envtest ship a binary instead of
shell script, making the install process different, so the new rules are
copied from the Makefile bootstrapped by recent versions of
controller-gen

Signed-off-by: Angel Misevski <[email protected]>
Add fixups for tests (originally written in January 2022) to accommodate
changes to DevWorkspace controller functionality since then.

Signed-off-by: Angel Misevski <[email protected]>
Regenerate DeepCopy files for CRDs after switching how controller-gen is
downloaded. Previously, we would download + build controller-gen locally
and use that in scripts. Now we `go install` it to ./bin and use that,
which is a lot simpler.

However, despite both instances using controller-gen v0.6.1, the output
of deepcopy-gen is different:

Previously, locally-built controller gen included the line
//go:build !ignore_autogenerated
at the top of deepcopy files

The new, `go install`ed version omits this line.

¯\_(ツ)_/¯

Signed-off-by: Angel Misevski <[email protected]>
Creating multiple workspaces during a test is required for testing
functionality that only triggers when multiple workspaces exist (e.g.
PVC cleanup)

Signed-off-by: Angel Misevski <[email protected]>
* Update make rule to run envtest tests using Ginkgo CLI if available
* Update github actions to install ginkgo and upload both coverage files
* Allow skipping envtest tests by setting env var

Signed-off-by: Angel Misevski <[email protected]>
@openshift-ci
Copy link

openshift-ci bot commented Dec 12, 2022

New changes are detected. LGTM label has been removed.

* Test new ServiceAccount behavior (role binds to individual
  serviceaccounts rather than all SAs in namespace)
* Set default PodSecurityContext when using a config for testing to
  accomodate changes around how PodSecurityContext is handled in
  OpenShift

Signed-off-by: Angel Misevski <[email protected]>
@amisevsk amisevsk merged commit 4bb7a56 into devfile:main Dec 14, 2022
@amisevsk amisevsk deleted the test-coverage branch December 14, 2022 16:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add testing framework for controller reconcile function
3 participants