Skip to content

Commit

Permalink
[DPE-2609] Remove support of Juju 2.9 (#257)
Browse files Browse the repository at this point in the history
The goal of this PR is to remove Juju 2.9 specific code related to
secrets since we are dropping support of Juju 2.9
  • Loading branch information
dmitry-ratushnyy authored Sep 25, 2023
1 parent 04480b0 commit e27b155
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 97 deletions.
10 changes: 5 additions & 5 deletions lib/charms/mongodb/v0/mongodb_backups.py
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ def _try_to_restore(self, backup_id: str) -> None:
restore_cmd = restore_cmd + remapping_args.split(" ")
self.charm.run_pbm_command(restore_cmd)
except (subprocess.CalledProcessError, ExecError) as e:
if type(e) == subprocess.CalledProcessError:
if type(e) is subprocess.CalledProcessError:
error_message = e.output.decode("utf-8")
else:
error_message = str(e.stderr)
Expand Down Expand Up @@ -560,7 +560,7 @@ def _try_to_backup(self):
)
return backup_id_match.group("backup_id") if backup_id_match else "N/A"
except (subprocess.CalledProcessError, ExecError) as e:
if type(e) == subprocess.CalledProcessError:
if type(e) is subprocess.CalledProcessError:
error_message = e.output.decode("utf-8")
else:
error_message = str(e.stderr)
Expand Down Expand Up @@ -636,13 +636,13 @@ def _get_backup_restore_operation_result(self, current_pbm_status, previous_pbm_
to contain the operation type (backup/restore) and the backup id.
"""
if (
type(current_pbm_status) == type(previous_pbm_status)
type(current_pbm_status) is type(previous_pbm_status)
and current_pbm_status.message == previous_pbm_status.message
):
return f"Operation is still in progress: '{current_pbm_status.message}'"

if (
type(previous_pbm_status) == MaintenanceStatus
type(previous_pbm_status) is MaintenanceStatus
and "backup id:" in previous_pbm_status.message
):
backup_id = previous_pbm_status.message.split("backup id:")[-1].strip()
Expand Down Expand Up @@ -679,7 +679,7 @@ def retrieve_error_message(self, pbm_status: Dict) -> str:

def process_pbm_error(self, pbm_status: Optional[_StrOrBytes]) -> str:
"""Returns errors found in PBM status."""
if type(pbm_status) == bytes:
if type(pbm_status) is bytes:
pbm_status = pbm_status.decode("utf-8")

try:
Expand Down
35 changes: 4 additions & 31 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
OperatorUser,
)
from charms.operator_libs_linux.v1 import snap
from ops import JujuVersion
from ops.charm import (
ActionEvent,
CharmBase,
Expand Down Expand Up @@ -252,10 +251,6 @@ def db_initialised(self, value):
f"'db_initialised' must be a boolean value. Proivded: {value} is of type {type(value)}"
)

@property
def _juju_has_secrets(self) -> bool:
return JujuVersion.from_environ().has_secrets

# END: properties

# BEGIN: charm event handlers
Expand Down Expand Up @@ -1006,39 +1001,17 @@ def _unit_ip(self, unit: Unit) -> str:

def get_secret(self, scope: str, key: str) -> Optional[str]:
"""Get secret from the secret storage."""
if self._juju_has_secrets:
return self._juju_secret_get(scope, key)

if scope == UNIT_SCOPE:
return self.unit_peer_data.get(key, None)
elif scope == APP_SCOPE:
return self.app_peer_data.get(key, None)
else:
raise RuntimeError("Unknown secret scope.")
return self._juju_secret_get(scope, key)

def set_secret(self, scope: str, key: str, value: Optional[str]) -> Optional[str]:
"""Set secret in the secret storage.
Juju versions > 3.0 use `juju secrets`, this function first checks
which secret store is being used before setting the secret.
"""
if self._juju_has_secrets:
if not value:
return self._juju_secret_remove(scope, key)
return self._juju_secret_set(scope, key, value)

if scope == UNIT_SCOPE:
if not value:
del self.unit_peer_data[key]
return
self.unit_peer_data.update({key: str(value)})
elif scope == APP_SCOPE:
if not value:
del self.app_peer_data[key]
return
self.app_peer_data.update({key: str(value)})
else:
raise RuntimeError("Unknown secret scope.")
if not value:
return self._juju_secret_remove(scope, key)
return self._juju_secret_set(scope, key, value)

def start_mongod_service(self):
"""Starts the mongod service and if necessary starts mongos.
Expand Down
44 changes: 0 additions & 44 deletions tests/conftest.py

This file was deleted.

17 changes: 0 additions & 17 deletions tests/integration/test_charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,6 @@ async def test_only_leader_can_set_while_all_can_read_password_secret(ops_test:
assert password2 == password


@pytest.mark.usefixtures("only_with_juju_secrets")
async def test_reset_and_get_password_secret_same_as_cli(ops_test: OpsTest) -> None:
"""Test verifies that we can set and retrieve the correct password using Juju 3.x secrets."""
new_password = str(uuid4())
Expand All @@ -231,21 +230,6 @@ async def test_reset_and_get_password_secret_same_as_cli(ops_test: OpsTest) -> N
assert data[secret_id]["content"]["Data"]["monitor-password"] == password


@pytest.mark.usefixtures("only_without_juju_secrets")
async def test_reset_and_get_password_no_secret(ops_test: OpsTest, mocker) -> None:
"""Test verifies that we can set and retrieve the correct password using Juju 2.x."""
new_password = str(uuid4())

# Re=setting existing password
leader_id = await get_leader_id(ops_test)
await set_password(ops_test, unit_id=leader_id, username="monitor", password=new_password)

# Getting back the pw programmatically
password = await get_password(ops_test, username="monitor")
assert password == new_password


@pytest.mark.usefixtures("only_with_juju_secrets")
async def test_empty_password(ops_test: OpsTest) -> None:
"""Test that the password can't be set to an empty string."""
leader_id = await get_leader_id(ops_test)
Expand All @@ -258,7 +242,6 @@ async def test_empty_password(ops_test: OpsTest) -> None:
assert password1 == password2


@pytest.mark.usefixtures("only_with_juju_secrets")
async def test_no_password_change_on_invalid_password(ops_test: OpsTest) -> None:
"""Test that in general, there is no change when password validation fails."""
leader_id = await get_leader_id(ops_test)
Expand Down

0 comments on commit e27b155

Please sign in to comment.