Skip to content

Commit

Permalink
Add more checks to repo-policy-compliance setup in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yhaliaw committed Sep 2, 2024
1 parent 436325d commit f1b0d68
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/manager/runner_scaler.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def reconcile(self, quantity: int) -> int:
metric_stats = self._manager.cleanup()
runners = self._manager.get_runners()
logger.info("Reconcile runners from %s to %s", len(runners), quantity)
runner_diff = quantity - len(runners)
runner_diff = quantity - len(runners)
if runner_diff > 0:
try:
self._manager.create_runners(runner_diff)
Expand Down
35 changes: 22 additions & 13 deletions tests/integration/helpers/openstack.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ async def expose_to_instance(
unit: The juju unit of the github-runner charm.
port: The port on the juju machine to expose to the runner.
"""
runner = self._get_runner(unit=unit)
runner = self._get_single_runner(unit=unit)
assert runner, f"Runner not found for unit {unit.name}"
network_address_list = runner.addresses.values()
logger.warning(network_address_list)
Expand All @@ -60,7 +60,9 @@ async def expose_to_instance(
assert key_path.exists(), f"SSH key for runner {runner.name} not found in the juju unit"
ssh_cmd = f'ssh -fNT -R {port}:localhost:{port} -i /home/ubuntu/.ssh/{runner.name}.key -o "StrictHostKeyChecking no" -o "ControlPersist yes" ubuntu@{ip} &'
exit_code, _, stderr = await run_in_unit(unit, ssh_cmd)
assert exit_code == 0, f"Error in SSH remote forwarding of port {port}: {stderr}"
assert (
exit_code == 0
), f"Error in starting background process of SSH remote forwarding of port {port}: {stderr}"

async def run_in_instance(
self,
Expand All @@ -82,7 +84,7 @@ async def run_in_instance(
Returns:
Tuple of return code, stdout and stderr.
"""
runner = self._get_runner(unit=unit)
runner = self._get_single_runner(unit=unit)
assert runner, f"Runner not found for unit {unit.name}"
network_address_list = runner.addresses.values()
logger.warning(network_address_list)
Expand Down Expand Up @@ -157,12 +159,14 @@ async def _get_runner_names(self, unit: Unit) -> tuple[str, ...]:
Returns:
Tuple of runner names.
"""
runner = self._get_runner(unit)
runner = self._get_single_runner(unit)
assert runner, "Failed to find runner server"
return (cast(str, runner.name),)

def _get_runner(self, unit: Unit) -> Server | None:
"""Get the runner server.
def _get_single_runner(self, unit: Unit) -> Server | None:
"""Get the only runner for the unit.
This method asserts for exactly one runner for the unit.
Args:
unit: The unit to get the runner for.
Expand All @@ -171,14 +175,12 @@ def _get_runner(self, unit: Unit) -> Server | None:
The runner server.
"""
servers: list[Server] = self.openstack_connection.list_servers()
runner = None
unit_name_without_slash = unit.name.replace("/", "-")
for server in servers:
if server.name.startswith(unit_name_without_slash):
runner = server
break

return runner
runners = [server for server in servers if server.name.startswith(unit_name_without_slash)]
assert (
len(runners) == 1
), f"In {unit.name} found more than one runners or no runners: {runners}"
return runners[0]


async def setup_repo_policy(
Expand Down Expand Up @@ -219,6 +221,13 @@ async def setup_repo_policy(

await instance_helper.ensure_charm_has_runner(app=app)
await instance_helper.expose_to_instance(unit, 8080)
# This tests the connection to the repo policy compliance, not a health check of service.
await instance_helper.run_in_instance(
unit=unit,
command="curl http://localhost/8080",
assert_on_failure=True,
assert_msg="Unable to reach the repo policy compliance server setup",
)


async def _install_repo_policy(
Expand Down

0 comments on commit f1b0d68

Please sign in to comment.