Skip to content

Conversation

@winiciusallan
Copy link
Contributor

Closes #563

@github-actions github-actions bot added the semver:patch No API change label Nov 14, 2025
@winiciusallan
Copy link
Contributor Author

I thought that'd be good to have a new section about how to contribute to the docs, and describe how the contributor can set up the mkdocs.

If it makes sense, I can add to this PR or open another with the changes.

Copy link
Collaborator

@mandre mandre left a comment

Choose a reason for hiding this comment

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

Thanks! This was indeed a gap in our developer documentation and I'm glad someone's taking the time to fix it. I've left a couple of comments, for possible improvement (it's a good start already).

I see we also talk about API validations at the top of the file, do we want to regroup that with unit tests?

We may also want to highlight these tests run in CI.

You can run them with:

```bash
$ go test ./...
Copy link
Collaborator

Choose a reason for hiding this comment

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

See also the test make target, which should probably be preferred over running go test directly, since it sets up envtest for controllers tests that have leverage it. You can filter which controller you want to run tests for with the TEST_PATHS environment variable.

.PHONY: test
TEST_PATHS ?= ./...
test: envtest
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" go test $$(go list $(TEST_PATHS) | grep -v /e2e) -coverprofile cover.out

I believe image is currently the only controller using it.

Copy link
Contributor Author

@winiciusallan winiciusallan Nov 16, 2025

Choose a reason for hiding this comment

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

Yeah, I agree. Using target is good because it also runs tests that leverage envtest.

I can commit the change to include the make target and document the TEST_PATHS variable. What do you think?

Copy link
Collaborator

Choose a reason for hiding this comment

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

That'd be great!


## Unit tests

For each controller, we have a file called `actuator_test.go`. It is highly
Copy link
Collaborator

Choose a reason for hiding this comment

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

All controllers should have unit tests, however, because for the most part the controllers are boilerplate code, we've been a bit lax. We require unit tests for custom code, so if your controller handles mutability, there should be a test for each mutable field.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry, I think I didn't get this comment. Are you suggesting a replacement text or making a comment regarding the tests? In case of the first option, I can highlight those things that you've commented on; it makes a lot of sense.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Not necessarily, I wanted to highlight the reasons why we had an actuator_test.go for each controller that had tests for the update handlers. Perhaps it's still a good idea to be explicit about when we require tests (custom code, such as update handlers) in the docs. I'll leave it up to you.

@winiciusallan
Copy link
Contributor Author

I see we also talk about API validations at the top of the file, do we want to regroup that with unit tests?

To be honest, I didn't see this section at the start of the page. Both API validation and actuator tests are unit tests in a sense, so it makes sense to have them under the same section, but it is important to separate them regarding their goals.

We may also want to highlight these tests run in CI.

+1

I think we can change the "Developing controllers" to include a step where the contributor may implement apivalidations tests, so we can have good coverage code for all of the controllers and check corner cases.

@mandre
Copy link
Collaborator

mandre commented Nov 16, 2025

I see we also talk about API validations at the top of the file, do we want to regroup that with unit tests?

To be honest, I didn't see this section at the start of the page. Both API validation and actuator tests are unit tests in a sense, so it makes sense to have them under the same section, but it is important to separate them regarding their goals.

Yup, exactly.

We may also want to highlight these tests run in CI.

+1

I think we can change the "Developing controllers" to include a step where the contributor may implement apivalidations tests, so we can have good coverage code for all of the controllers and check corner cases.

We may also want to add a stub the API validation in the controller scaffolding tool to make it obvious we ideally want API validations unit tests, WDYT? That should be part of another PR if we decide it's a good idea.

@mandre
Copy link
Collaborator

mandre commented Nov 16, 2025

I thought that'd be good to have a new section about how to contribute to the docs, and describe how the contributor can set up the mkdocs.

If it makes sense, I can add to this PR or open another with the changes.

If you feel it helps onboarding new contributors, I'm not going to block you, and would actually be very happy to merge any change that makes the docs better.

@winiciusallan
Copy link
Contributor Author

We may also want to add a stub the API validation in the controller scaffolding tool to make it obvious we ideally want API validations unit tests, WDYT? That should be part of another PR if we decide it's a good idea.

It'd be great.

The scaffolding tool generates too many files, most of which are just boilerplate code, so newcomer contributors may feel a little lost. Could you please open an issue for this?

@winiciusallan
Copy link
Contributor Author

I thought that'd be good to have a new section about how to contribute to the docs, and describe how the contributor can set up the mkdocs.
If it makes sense, I can add to this PR or open another with the changes.

If you feel it helps onboarding new contributors, I'm not going to block you, and would actually be very happy to merge any change that makes the docs better.

Do you think it will be necessary a new issue to address it?

@winiciusallan
Copy link
Contributor Author

Sorry, I amended the commit and have lost discussions along the code.

Well, I made a few adjustments. Check if you think it is ready to go. Don't hesitate to ask for changes :)

separated by a blank space, for example:

```bash
$ TEST_PATHS=$(go list ./internal/controller/server ./internal/controller/image) make test
Copy link
Collaborator

Choose a reason for hiding this comment

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

It should be:

Suggested change
$ TEST_PATHS=$(go list ./internal/controller/server ./internal/controller/image) make test
$ TEST_PATHS="./internal/controllers/server ./internal/controllers/image" make test

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

coverage from all of our controllers. So, if your controller implements mutable
fields, you should add tests for those fields.

You can run all with:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Thoughts about moving lines 12-25 outside of the Mutability tests subsection since it applies to all unit tests?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Looks good. Done!

```

You can also specify which controller do you want to test using the `TEST_PATHS`
environment variable. This variable indicates a list of module paths, and you
Copy link
Collaborator

Choose a reason for hiding this comment

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

Actually, the TEST_PATHS environment variable is given to go list to retrieve module names, so it must constain directory paths, and not module names.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

True. Thanks for the correction.

Signed-off-by: Winicius Silva <winiciusab12@gmail.com>
@mandre mandre enabled auto-merge November 17, 2025 12:39
@mandre mandre added this pull request to the merge queue Nov 17, 2025
Merged via the queue into k-orc:main with commit ef65729 Nov 17, 2025
9 checks passed
@winiciusallan winiciusallan deleted the docs-unittests branch November 17, 2025 17:19
@mandre mandre added this to the Release 2.4 milestone Dec 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

semver:patch No API change

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Enhance docs with how to run unit tests for actuators

2 participants