Skip to content

Commit

Permalink
inject lxd & wait for update event
Browse files Browse the repository at this point in the history
  • Loading branch information
yanksyoon committed Jun 11, 2024
1 parent dee3465 commit dfc4aa5
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 21 deletions.
18 changes: 18 additions & 0 deletions tests/integration/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
21 changes: 5 additions & 16 deletions tests/integration/test_charm_no_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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,
Expand Down Expand Up @@ -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(
Expand Down
20 changes: 15 additions & 5 deletions tests/integration/test_charm_upgrade.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

"""Integration tests for charm upgrades."""

import functools
import pathlib

import pytest
Expand All @@ -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
Expand All @@ -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",
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit dfc4aa5

Please sign in to comment.