Skip to content

docs: separate test_workload_version_is_set in k8s tutorial chapter 3#2638

Open
tromai wants to merge 3 commits into
canonical:mainfrom
tromai:keep-test-workload-version-separate-in-chapter-3-k8s-tutorial
Open

docs: separate test_workload_version_is_set in k8s tutorial chapter 3#2638
tromai wants to merge 3 commits into
canonical:mainfrom
tromai:keep-test-workload-version-separate-in-chapter-3-k8s-tutorial

Conversation

@tromai

@tromai tromai commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

This PR brought test_workload_version_is_set back to life in chapter 3, and propagate that change to chapter 4 and 5. It also updated the example examples/k8s-*/tests/integration/test_charm.py files.

Doc preview

It was raised by David:

In the k8s tutorial Chapter, section write an integration test, we replace the contents of test_charm.py. That loses test_workload_version_is_set and we never recreate it. In the corresponding file in the example charm, we keep the version check as part of test_database_integration, but we don't have that in the tutorial.

logger = logging.getLogger(__name__)

METADATA = yaml.safe_load(pathlib.Path("./charmcraft.yaml").read_text())
METADATA = yaml.safe_load(pathlib.Path("charmcraft.yaml").read_text())

@tromai tromai Jul 9, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I made this change to following what we have in Chapter 1. Please let me know if the ./ is intentional here.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Good catch, thanks. The version without ./ matches the Charmcraft profile (see https://github.com/canonical/charmcraft/blob/main/charmcraft/templates/init-kubernetes/tests/integration/test_charm.py.j2)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure if it's in the scope.

The content we suggest here is different from Chapter 2 in 2 places:

  • From juju.wait(jubilant.all_active) to juju.wait(jubilant.all_blocked)
  • The doc string Assert on the unit status before any relations/configurations take place.

We can simplify by describing a diff, instead of a whole file replacement:

- """Deploy the charm under test."""
+ """Deploy the charm under test.
+
+ Assert on the unit status before any relations/configurations take place.
+ """

...
- juju.wait(jubilant.all_active)
+ juju.wait(jubilant.all_blocked)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I agree with you. I don't see why we need to rewrite the whole file. I think it will be clearer for the reader if we tell them exactly what to change.

With that in mind, I suggest making the format of test_deploy match closer with chapter 2. Keep the same docstring and remove the comment before juju.deploy (the tutorial already explains things sufficiently).

This means the only change needed is all_active --> all_blocked. The tutorial can effectively just say that 😁

I've added suggestions on the testing code.

@tromai tromai marked this pull request as ready for review July 9, 2026 23:25
Comment thread examples/k8s-3-postgresql/tests/integration/test_charm.py Outdated
Comment on lines +49 to +52
def test_workload_version_is_set(juju: jubilant.Juju):
# Verify that the workload version has been set.
version = juju.status().apps[APP_NAME].version
assert version == "1.0.4" # Hardcoded for simplicity.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Interesting, I was expecting we'd put this test after test_database_integration (which unblocks the charm). But I think you're right; it doesn't matter that the charm is blocked, as that's coming from the collect-status handler.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Oh, I only moved this function because it follows the tutorial where we said "add the test_database_integration" test.
I thought it's more natural for test_database_integration to be after test_workload_version_is_set. It works well as you confirmed, but I'm happy to revert it back this change doesn't make sense 🤔

Comment thread examples/k8s-5-observe/tests/integration/test_charm.py Outdated
Comment thread examples/k8s-5-observe/tests/integration/test_charm.py Outdated
Comment thread examples/k8s-4-action/tests/integration/test_charm.py Outdated
Comment thread examples/k8s-3-postgresql/tests/integration/test_charm.py Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I agree with you. I don't see why we need to rewrite the whole file. I think it will be clearer for the reader if we tell them exactly what to change.

With that in mind, I suggest making the format of test_deploy match closer with chapter 2. Keep the same docstring and remove the comment before juju.deploy (the tutorial already explains things sufficiently).

This means the only change needed is all_active --> all_blocked. The tutorial can effectively just say that 😁

I've added suggestions on the testing code.

Comment thread examples/k8s-5-observe/tests/integration/test_charm.py Outdated
…leaning up; mention why we need charm fixture in all integration tests; simplify integration test section of chapter 3
@tromai tromai requested a review from dwilding July 10, 2026 04:57
## Write an integration test

Now that our charm integrates with the database, if there's not a database relation, the app will be in `blocked` status instead of `active`. Let's tweak our existing integration test `test_deploy` accordingly, to expect blocked status in `juju.wait`. Replace the contents of `tests/integration/test_charm.py` with:
Now that our charm integrates with the database, if there's not a database relation, the app will be in `blocked` status instead of `active`. In your `tests/integration/test_charm.py` file, update `test_deploy` to expect blocked status:

@tromai tromai Jul 10, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This is my attempt to simplify the description. I am happy to keep the "Let's tweak ..." if that is preferred

Let's tweak our existing integration test `test_deploy` accordingly, to expect blocked status in `juju.wait`. 
Update `test_deploy` in your `tests/integration/test_charm.py` with:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants