From 3938d1eba2d536b112130020b3f875689714bb68 Mon Sep 17 00:00:00 2001 From: Caner Derici Date: Fri, 26 Jan 2024 11:45:44 -0700 Subject: [PATCH] Let secrets tests pass if not supported (by 2.9) --- juju/model.py | 23 +++++++--- tests/integration/test_secrets.py | 71 ++++++++++++++++++++----------- 2 files changed, 63 insertions(+), 31 deletions(-) diff --git a/juju/model.py b/juju/model.py index 56af31e7..e2836c13 100644 --- a/juju/model.py +++ b/juju/model.py @@ -2633,7 +2633,8 @@ async def add_secret(self, name, data_args, file="", info=""): # Caution: key/value pairs in files overwrite the ones in the args. data[k] = v - if client.SecretsFacade.best_facade_version(self.connection()) < 2: + secrets_facade_version = client.SecretsFacade.best_facade_version(self.connection()) + if not secrets_facade_version or secrets_facade_version < 2: raise JujuNotSupportedError("user secrets") secretsFacade = client.SecretsFacade.from_connection(self.connection()) @@ -2667,8 +2668,10 @@ async def update_secret(self, name, data_args=[], new_name="", file="", info="") # Caution: key/value pairs in files overwrite the ones in the args. data[k] = v - if client.SecretsFacade.best_facade_version(self.connection()) < 2: + secrets_facade_version = client.SecretsFacade.best_facade_version(self.connection()) + if not secrets_facade_version or secrets_facade_version < 2: raise JujuNotSupportedError("user secrets") + secretsFacade = client.SecretsFacade.from_connection(self.connection()) results = await secretsFacade.UpdateSecrets([{ 'content': {'data': data}, @@ -2686,6 +2689,10 @@ async def list_secrets(self, filter="", show_secrets=False): """ Returns the list of available secrets. """ + secrets_facade_version = client.SecretsFacade.best_facade_version(self.connection()) + if not secrets_facade_version or secrets_facade_version < 2: + raise JujuNotSupportedError("user secrets") + facade = client.SecretsFacade.from_connection(self.connection()) results = await facade.ListSecrets({ 'filter': filter, @@ -2699,8 +2706,10 @@ async def remove_secret(self, secret_name, revision=-1): :param secret_name str: ID|name of the secret to remove. :param revision int: remove the specified revision. """ - if client.SecretsFacade.best_facade_version(self.connection()) < 2: + secrets_facade_version = client.SecretsFacade.best_facade_version(self.connection()) + if not secrets_facade_version or secrets_facade_version < 2: raise JujuNotSupportedError("user secrets") + remove_secret_arg = { 'label': secret_name, } @@ -2722,8 +2731,10 @@ async def grant_secret(self, secret_name, application, *applications): :param application str: name of an application for which the access is granted :param applications []str: names of more applications to associate the secret with """ - if client.SecretsFacade.best_facade_version(self.connection()) < 2: + secrets_facade_version = client.SecretsFacade.best_facade_version(self.connection()) + if not secrets_facade_version or secrets_facade_version < 2: raise JujuNotSupportedError("user secrets") + secretsFacade = client.SecretsFacade.from_connection(self.connection()) results = await secretsFacade.GrantSecret( applications=[application] + list(applications), @@ -2743,8 +2754,10 @@ async def revoke_secret(self, secret_name, application, *applications): :param application str: name of an application for which the access to the secret is revoked :param applications []str: names of more applications to disassociate the secret with """ - if client.SecretsFacade.best_facade_version(self.connection()) < 2: + secrets_facade_version = client.SecretsFacade.best_facade_version(self.connection()) + if not secrets_facade_version or secrets_facade_version < 2: raise JujuNotSupportedError("user secrets") + secretsFacade = client.SecretsFacade.from_connection(self.connection()) results = await secretsFacade.RevokeSecret( applications=[application] + list(applications), diff --git a/tests/integration/test_secrets.py b/tests/integration/test_secrets.py index 7893ba8f..2edc504f 100644 --- a/tests/integration/test_secrets.py +++ b/tests/integration/test_secrets.py @@ -4,18 +4,22 @@ import pytest from .. import base from ..utils import TESTS_DIR +from juju.errors import JujuNotSupportedError @base.bootstrapped @pytest.mark.bundle async def test_add_secret(): async with base.CleanModel() as model: - secret = await model.add_secret(name='my-apitoken', data_args=['token=34ae35facd4']) - assert secret.startswith('secret:') + try: + secret = await model.add_secret(name='my-apitoken', data_args=['token=34ae35facd4']) + assert secret.startswith('secret:') - secrets = await model.list_secrets() - assert len(secrets) == 1 - assert secrets[0].label == 'my-apitoken' + secrets = await model.list_secrets() + assert len(secrets) == 1 + assert secrets[0].label == 'my-apitoken' + except JujuNotSupportedError: + pass # This test can only work if we can fully upgrade the whole charm @@ -33,54 +37,69 @@ async def test_list_secrets(): await model.wait_for_idle(status="active") assert model.units['charm-secret/0'].workload_status == 'active' - secrets = await model.list_secrets(show_secrets=True) - assert secrets is not None - assert len(secrets) == 1 + try: + secrets = await model.list_secrets(show_secrets=True) + assert secrets is not None + assert len(secrets) == 1 + except JujuNotSupportedError: + pass @base.bootstrapped @pytest.mark.bundle async def test_update_secret(): async with base.CleanModel() as model: - secret = await model.add_secret(name='my-apitoken', data_args=['token=34ae35facd4']) - assert secret.startswith('secret:') + try: + secret = await model.add_secret(name='my-apitoken', data_args=['token=34ae35facd4']) + assert secret.startswith('secret:') - await model.update_secret(name='my-apitoken', new_name='new-token') + await model.update_secret(name='my-apitoken', new_name='new-token') - secrets = await model.list_secrets() - assert len(secrets) == 1 - assert secrets[0].label == 'new-token' + secrets = await model.list_secrets() + assert len(secrets) == 1 + assert secrets[0].label == 'new-token' + except JujuNotSupportedError: + pass @base.bootstrapped @pytest.mark.bundle async def test_remove_secret(): async with base.CleanModel() as model: - secret = await model.add_secret(name='my-apitoken', data_args=['token=34ae35facd4']) - assert secret.startswith('secret:') + try: + secret = await model.add_secret(name='my-apitoken', data_args=['token=34ae35facd4']) + assert secret.startswith('secret:') - await model.remove_secret('my-apitoken') + await model.remove_secret('my-apitoken') - secrets = await model.list_secrets() - assert len(secrets) == 0 + secrets = await model.list_secrets() + assert len(secrets) == 0 + except JujuNotSupportedError: + pass @base.bootstrapped @pytest.mark.bundle async def test_grant_secret(): async with base.CleanModel() as model: - secret = await model.add_secret(name='my-apitoken', data_args=['token=34ae35facd4']) - assert secret.startswith('secret:') + try: + secret = await model.add_secret(name='my-apitoken', data_args=['token=34ae35facd4']) + assert secret.startswith('secret:') - await model.deploy('ubuntu') + await model.deploy('ubuntu') - await model.grant_secret('my-apitoken', 'ubuntu') + await model.grant_secret('my-apitoken', 'ubuntu') + except JujuNotSupportedError: + pass @base.bootstrapped @pytest.mark.bundle async def test_revoke_secret(): async with base.CleanModel() as model: - secret = await model.add_secret(name='my-apitoken', data_args=['token=34ae35facd4']) - assert secret.startswith('secret:') - await model.revoke_secret('my-apitoken', 'ubuntu') + try: + secret = await model.add_secret(name='my-apitoken', data_args=['token=34ae35facd4']) + assert secret.startswith('secret:') + await model.revoke_secret('my-apitoken', 'ubuntu') + except JujuNotSupportedError: + pass