From 6fdc6ce4146df4d99e7d33aa841cfeaeb4866997 Mon Sep 17 00:00:00 2001 From: yhaliaw <43424755+yhaliaw@users.noreply.github.com> Date: Tue, 30 Jan 2024 19:17:59 +0800 Subject: [PATCH] Refactor test to use helper functions --- src-docs/runner.py.md | 6 +++--- src/runner.py | 8 +++++++- tests/integration/helpers.py | 4 ++++ tests/integration/test_charm_one_runner.py | 2 ++ tests/integration/test_self_hosted_runner.py | 15 +++++++-------- 5 files changed, 23 insertions(+), 12 deletions(-) diff --git a/src-docs/runner.py.md b/src-docs/runner.py.md index 9916319b7..f9801909a 100644 --- a/src-docs/runner.py.md +++ b/src-docs/runner.py.md @@ -39,7 +39,7 @@ The configuration values for creating a single runner instance. ## class `Runner` Single instance of GitHub self-hosted runner. - + ### function `__init__` @@ -67,7 +67,7 @@ Construct the runner instance. --- - + ### function `create` @@ -91,7 +91,7 @@ Create the runner instance on LXD and register it on GitHub. --- - + ### function `remove` diff --git a/src/runner.py b/src/runner.py index f5343d363..4dbfb82ec 100644 --- a/src/runner.py +++ b/src/runner.py @@ -19,6 +19,7 @@ from dataclasses import dataclass from pathlib import Path from typing import Iterable, NamedTuple, Optional, Sequence +from urllib.error import HTTPError import yaml @@ -229,7 +230,12 @@ def remove(self, remove_token: str) -> None: self.status.runner_id, self.config.path.path(), ) - self._clients.github.delete_runner(self.config.path, self.status.runner_id) + try: + self._clients.github.delete_runner(self.config.path, self.status.runner_id) + except HTTPError: + logger.exception("Unable the remove runner on GitHub: %s", self.config.name) + # This can occur when attempting to remove a busy runner. + # The caller should retry later, after GitHub mark the runner as offline. def _add_shared_filesystem(self, path: Path) -> None: """Add the shared filesystem to the runner instance. diff --git a/tests/integration/helpers.py b/tests/integration/helpers.py index 65be90ee6..38e0a5254 100644 --- a/tests/integration/helpers.py +++ b/tests/integration/helpers.py @@ -12,6 +12,7 @@ from datetime import datetime, timezone from typing import Any, Awaitable, Callable, Union +import github import juju.version import requests import yaml @@ -397,6 +398,9 @@ def get_workflow_runs( runner_name: The runner name the workflow job is assigned to. branch: The branch the workflow is run on. """ + if branch is None: + branch = github.GithubObject.NotSet + for run in workflow.get_runs(created=f">={start_time.isoformat()}", branch=branch): latest_job: WorkflowJob = run.jobs()[0] logs = get_job_logs(job=latest_job) diff --git a/tests/integration/test_charm_one_runner.py b/tests/integration/test_charm_one_runner.py index b97086cf1..6fcb8c786 100644 --- a/tests/integration/test_charm_one_runner.py +++ b/tests/integration/test_charm_one_runner.py @@ -187,6 +187,8 @@ async def test_reconcile_runners_with_lxd_storage_pool_failure( await wait_till_num_of_runners(unit, 1) +@pytest.mark.asyncio +@pytest.mark.abort_on_fail async def test_change_runner_storage(model: Model, app: Application) -> None: """ arrange: A working application with one runners using memory as disk. diff --git a/tests/integration/test_self_hosted_runner.py b/tests/integration/test_self_hosted_runner.py index 7eb3426a7..5d6527855 100644 --- a/tests/integration/test_self_hosted_runner.py +++ b/tests/integration/test_self_hosted_runner.py @@ -8,7 +8,6 @@ import github import pytest -import requests from github.Repository import Repository from juju.application import Application from juju.model import Model @@ -18,7 +17,9 @@ from tests.integration.helpers import ( DISPATCH_TEST_WORKFLOW_FILENAME, DISPATCH_WAIT_TEST_WORKFLOW_FILENAME, + get_job_logs, get_runner_names, + get_workflow_runs, reconcile, run_in_lxd_instance, wait_till_num_of_runners, @@ -81,13 +82,10 @@ async def test_dispatch_workflow_with_dockerhub_mirror( # Unable to find the run id of the workflow that was dispatched. # Therefore, all runs after this test start should pass the conditions. - for run in workflow.get_runs(created=f">={start_time.isoformat()}"): - if start_time > run.created_at: - continue - + for run in get_workflow_runs(start_time, workflow, runner_to_be_used): + jobs = run.jobs() try: - logs_url = run.jobs()[0].logs_url() - logs = requests.get(logs_url).content.decode("utf-8") + logs = get_job_logs(jobs[0]) except github.GithubException.GithubException: continue @@ -101,7 +99,7 @@ async def test_dispatch_workflow_with_dockerhub_mirror( @pytest.mark.asyncio @pytest.mark.abort_on_fail -async def test_wait_on_busy_runner_repo_check( +async def test_flush_busy_runner( model: Model, app_runner: Application, forked_github_repository: Repository, @@ -151,6 +149,7 @@ async def test_wait_on_busy_runner_repo_check( if not runners: # if runner is not online yet. + sleep(30) continue assert len(runners) == 1, "Should not occur as GitHub enforce unique naming of runner"