From a2c0d7d08fa833dc525639e94fe34213d68031c6 Mon Sep 17 00:00:00 2001 From: codethulu Date: Thu, 13 Jun 2024 17:34:33 +0100 Subject: [PATCH] addressed changes --- discourse_rock/patches/discourse-charm.patch | 4 +-- docs/how-to/contribute.md | 2 +- src/charm.py | 32 +++++++++++++++++--- tests/integration/conftest.py | 3 +- tests/integration/test_users.py | 17 ----------- 5 files changed, 31 insertions(+), 27 deletions(-) diff --git a/discourse_rock/patches/discourse-charm.patch b/discourse_rock/patches/discourse-charm.patch index 68a0ff03..3aac35df 100644 --- a/discourse_rock/patches/discourse-charm.patch +++ b/discourse_rock/patches/discourse-charm.patch @@ -11,7 +11,7 @@ + exit 0 + end + puts "ERROR: User with email #{email} not found" -+ exit 1 ++ exit 2 +end + +desc "Activate a user account" @@ -20,7 +20,7 @@ + user = User.find_by_email(email) + if !user + puts "User with email #{email} does not exist" -+ exit 1 ++ exit 2 + end + user.email_tokens.update_all(confirmed: true) + puts "User with email #{email} activated" diff --git a/docs/how-to/contribute.md b/docs/how-to/contribute.md index 052aa594..7d6de760 100644 --- a/docs/how-to/contribute.md +++ b/docs/how-to/contribute.md @@ -83,7 +83,7 @@ the registry: ```shell cd [project_dir]/discourse_rock && rockcraft pack rockcraft.yaml - skopeo --insecure-policy copy --dest-tls-verify=false oci-archive:discourse_1.0_amd64.rock docker://localhost:32000/discourse:latest + rockcraft.skopeo --insecure-policy copy --dest-tls-verify=false oci-archive:discourse_1.0_amd64.rock docker://localhost:32000/discourse:latest ``` ### Deploy diff --git a/src/charm.py b/src/charm.py index be301abf..268f9668 100755 --- a/src/charm.py +++ b/src/charm.py @@ -718,8 +718,13 @@ def _on_promote_user_action(self, event: ActionEvent) -> None: ) try: user_exists.wait_output() - except ExecError: - event.fail(f"User with email {email} does not exist") + except ExecError as ex: + if ex.exit_code == 2: + event.fail(f"User with email {email} does not exist") + else: + event.fail( + f"Error checking if user with email {email} exists: {ex.stdout}" # type: ignore + ) return process = container.exec( @@ -766,8 +771,12 @@ def _on_create_user_action(self, event: ActionEvent) -> None: user_exists.wait_output() event.fail(f"User with email {email} already exists") return - except ExecError: - pass + except ExecError as ex: + if ex.exit_code == 2: + pass + else: + event.fail(f"Error checking if user with email {email} exists: {ex.stdout}") + return # Admin flag is optional, if it is true, the user will be created as an admin admin_flag = "Y" if event.params.get("admin") else "N" process = container.exec( @@ -788,6 +797,7 @@ def _on_create_user_action(self, event: ActionEvent) -> None: event.set_results({"user": email, "password": password}) except ExecError as ex: event.fail(f"Failed to make user with email {email}: {ex.stdout}") # type: ignore + return if event.params.get("admin") or not event.params.get("active"): return @@ -803,7 +813,19 @@ def _on_create_user_action(self, event: ActionEvent) -> None: user=CONTAINER_APP_USERNAME, environment=self._create_discourse_environment_settings(), ) - activate_process.wait_output() + try: + activate_process.wait_output() + except ExecError as ex: + if ex.exit_code == 2: + event.fail( + f"Could not find user {email} for activation: {ex.stdout}" # type: ignore + ) + return + event.fail( + # Parameter validation errors are printed to stdout + # Ignore mypy warning when formatting stdout + f"Failed to activate user with email {email}: {ex.stdout}" # type: ignore + ) def _generate_password(self, length: int) -> str: """Generate a random password. diff --git a/tests/integration/conftest.py b/tests/integration/conftest.py index 1024fab6..a88491a7 100644 --- a/tests/integration/conftest.py +++ b/tests/integration/conftest.py @@ -157,8 +157,7 @@ async def app_fixture( async with ops_test.fast_forward(): await model.wait_for_idle(apps=[postgres_app.name], status="active") - # Revision 28 is being used due to https://github.com/canonical/redis-k8s-operator/issues/87. - redis_app = await model.deploy("redis-k8s", series="jammy", channel="latest/edge", revision=28) + redis_app = await model.deploy("redis-k8s", series="jammy", channel="latest/edge") await model.wait_for_idle(apps=[redis_app.name], status="active") await model.deploy("nginx-ingress-integrator", series="focal", trust=True) diff --git a/tests/integration/test_users.py b/tests/integration/test_users.py index 8eab7e27..85db149a 100644 --- a/tests/integration/test_users.py +++ b/tests/integration/test_users.py @@ -10,7 +10,6 @@ from juju.action import Action from juju.application import Application from juju.unit import Unit -from pytest_operator.plugin import Model logger = logging.getLogger(__name__) @@ -44,8 +43,6 @@ async def test_create_user( async def test_promote_user( app: Application, discourse_address: str, - model: Model, - requests_timeout: float, ): """ arrange: A discourse application @@ -53,14 +50,6 @@ async def test_promote_user( assert: User cannot access the admin API before being promoted """ - def test_discourse_srv_status_ok(): - response = requests.get(f"{discourse_address}/srv/status", timeout=requests_timeout) - assert response.status_code == 200 - - # The charm should be active when starting this test - await model.wait_for_idle(status="active") - test_discourse_srv_status_ok() - with requests.session() as session: def get_api_key(csrf_token: str) -> bool: @@ -107,12 +96,6 @@ def get_api_key(csrf_token: str) -> bool: }, ) - try: - "error" not in response.json() - except Exception as e: - logger.error("Error in response: %s", response.text) - raise e - assert response.ok, response.text # pylint: disable=no-member assert "error" not in response.json()