diff --git a/tests/integration/helpers.py b/tests/integration/helpers.py index d81d01ed8..c539d6c50 100644 --- a/tests/integration/helpers.py +++ b/tests/integration/helpers.py @@ -648,3 +648,21 @@ def inject_lxd_profile(charm_file: pathlib.Path, loop_device: str | None) -> Non "lxd-profile.yaml", lxd_profile_str, ) + + +async def is_upgrade_charm_event_emitted(unit: Unit) -> bool: + """Check if the upgrade_charm event is emitted. + + This is to ensure false positives from only waiting for ACTIVE status. + + Args: + unit: The unit to check for upgrade charm event. + + Returns: + bool: True if the event is emitted, False otherwise. + """ + unit_name_without_slash = unit.name.replace("/", "-") + juju_unit_log_file = f"/var/log/juju/unit-{unit_name_without_slash}.log" + ret_code, stdout, stderr = await run_in_unit(unit=unit, command=f"cat {juju_unit_log_file}") + assert ret_code == 0, f"Failed to read the log file: {stderr}" + return stdout is not None and "Emitting Juju event upgrade_charm." in stdout diff --git a/tests/integration/test_charm_no_runner.py b/tests/integration/test_charm_no_runner.py index f2a42955c..10f760c28 100644 --- a/tests/integration/test_charm_no_runner.py +++ b/tests/integration/test_charm_no_runner.py @@ -2,6 +2,7 @@ # See LICENSE file for licensing details. """Integration tests for github-runner charm with no runner.""" +import functools import json from datetime import datetime, timezone @@ -14,6 +15,7 @@ check_runner_binary_exists, get_repo_policy_compliance_pip_info, install_repo_policy_compliance_from_git_source, + is_upgrade_charm_event_emitted, reconcile, remove_runner_bin, run_in_unit, @@ -213,22 +215,9 @@ async def test_charm_no_runner_upgrade( await app_no_runner.refresh(path=charm_file) unit = app_no_runner.units[0] - unit_name_without_slash = unit.name.replace("/", "-") - juju_unit_log_file = f"/var/log/juju/unit-{unit_name_without_slash}.log" - - async def is_upgrade_charm_event_emitted() -> bool: - """Check if the upgrade_charm event is emitted. - - Returns: - bool: True if the event is emitted, False otherwise. - """ - ret_code, stdout, stderr = await run_in_unit( - unit=unit, command=f"cat {juju_unit_log_file}" - ) - assert ret_code == 0, f"Failed to read the log file: {stderr}" - return stdout is not None and "Emitting Juju event upgrade_charm." in stdout - - await wait_for(is_upgrade_charm_event_emitted, timeout=360, check_interval=60) + await wait_for( + functools.partial(is_upgrade_charm_event_emitted, unit), timeout=360, check_interval=60 + ) await model.wait_for_idle(status=ACTIVE) ret_code, stdout, stderr = await run_in_unit( diff --git a/tests/integration/test_charm_upgrade.py b/tests/integration/test_charm_upgrade.py index 8964cffec..5b6192e11 100644 --- a/tests/integration/test_charm_upgrade.py +++ b/tests/integration/test_charm_upgrade.py @@ -3,6 +3,7 @@ """Integration tests for charm upgrades.""" +import functools import pathlib import pytest @@ -11,7 +12,12 @@ from pytest_operator.plugin import OpsTest from charm_state import VIRTUAL_MACHINES_CONFIG_NAME -from tests.integration.helpers import deploy_github_runner_charm, inject_lxd_profile +from tests.integration.helpers import ( + deploy_github_runner_charm, + inject_lxd_profile, + is_upgrade_charm_event_emitted, + wait_for, +) @pytest.mark.asyncio @@ -29,18 +35,18 @@ async def test_charm_upgrade( tmp_path: pathlib.Path, ): """ - arrange: given latest stable version of the charm. + arrange: given latest stable version of the charm (current 161). act: charm upgrade is called. assert: the charm is upgraded successfully. """ latest_stable_path = tmp_path / "github-runner.charm" - latest_stable_revision = 161 + latest_stable_revision = 161 # update this value every release to stable. # download the charm and inject lxd profile for testing retcode, stdout, stderr = await ops_test.juju( "download", "github-runner", - "--channel", - "latest/stable", + # do not specify channel - + # --revision cannot be specified together with --arch, --base, --channel "--revision", str(latest_stable_revision), "--filepath", @@ -85,6 +91,10 @@ async def test_charm_upgrade( # upgrade the charm with current local charm await application.local_refresh(path=charm_file, charm_origin=origin) + unit = application.units[0] + await wait_for( + functools.partial(is_upgrade_charm_event_emitted, unit), timeout=360, check_interval=60 + ) await model.wait_for_idle( apps=[application.name], raise_on_error=False,