Skip to content

Commit

Permalink
Let secrets tests pass if not supported (by 2.9)
Browse files Browse the repository at this point in the history
  • Loading branch information
cderici committed Jan 26, 2024
1 parent b608259 commit 3938d1e
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 31 deletions.
23 changes: 18 additions & 5 deletions juju/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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},
Expand All @@ -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,
Expand All @@ -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,
}
Expand All @@ -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),
Expand All @@ -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),
Expand Down
71 changes: 45 additions & 26 deletions tests/integration/test_secrets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

0 comments on commit 3938d1e

Please sign in to comment.