-
Notifications
You must be signed in to change notification settings - Fork 112
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
Simplify testing code that checks the Juju version #1037
Comments
Yeah, agreed this is a good idea. We'll have to discuss the exact API. It's probably going to be simpler to use and more discoverable if we make it a I think we should probably leave the production 0.0.0 fallback alone (unless there's an easy way to get the actual Juju version?) but change the default for testing to the latest stable version of Juju. |
For what it's worth, if my goal was to exercise all my tests under different (simulated) Juju versions - and I wasn't using unittest - then I think I would do it all entirely with pytest. For example, with this very simplified (and contrived) test: import pytest
import ops
def test_secret():
if ops.JujuVersion.from_environ() < "3.0.3":
pytest.xfail("secrets are not available in Juju 2")
assert ops.JujuVersion.from_environ().has_secrets I would have a conftest like this: import pytest
@pytest.fixture(autouse=True)
def _juju_version(request, monkeypatch):
monkeypatch.setenv("JUJU_VERSION", request.param)
def pytest_generate_tests(metafunc):
"""Duplicate all tests under multiple (simulated) Juju versions."""
metafunc.parametrize("_juju_version", ("2.9", "3.0.0", "3.0.3", "3.1", "3.3", "3.4"), indirect=True) pytest output
With a @pytest.fixture(autouse=True)
def _juju_version(request, harness):
harness.set_juju_version(requst.param) I think it would be nice to avoid having to do something like this: @pytest.mark.skipif(not ops.JujuVersion.from_environ().has_secrets, reason="Checks secret-specific code") And instead also allow getting the version, e.g.: @pytest.mark.skipif(harness.get_juju_version().has_secrets, reason="Checks secret-specific code") Although: (a) I'm not sure if you can use a fixture in a |
@canonical/charm-tech will discuss this in person in Madrid to see if we can brainstorm a good approach. |
From @sed-i in a Matrix thread:
|
In our team discussion, we decided the following:
|
Charms, such as mysql-k8s-operator, have code like:
in order to determine whether Juju features are available (and behave differently as a result). However, the default version for
from_environ()
is0.0.0
, which means that all of thehas_*
methods that do a version check will return False if the version isn't in the environment variables.The Charmer can work around this, e.g.:
or more indirectly by mocking the environment variable
JUJU_VERSION
. However, we could make this nicer for Charmers.One possibility would be to reverse the situation so that
JujuVersion.from_environ()
defaults to the latest stable version ifJUJU_VERSION
is not in the environment variables. This more closely reflects thetesting.Harness
behaviour where (simulated) secret functionality is always available. In production, it's less clear, so maybe this is only done when testing?Alternatively, or additionally, we could provide a mechanism for specifying the (simulated) Juju version, particularly if we are already going to be patching that. For example:
or
(This might give weird behaviour if you're simulating a feature set mix that never existed in Juju).
The text was updated successfully, but these errors were encountered: