Skip to content

Commit

Permalink
Refactor test to use helper functions
Browse files Browse the repository at this point in the history
  • Loading branch information
yhaliaw committed Jan 30, 2024
1 parent e48b355 commit 6fdc6ce
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 12 deletions.
6 changes: 3 additions & 3 deletions src-docs/runner.py.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ The configuration values for creating a single runner instance.
## <kbd>class</kbd> `Runner`
Single instance of GitHub self-hosted runner.

<a href="../src/runner.py#L102"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>
<a href="../src/runner.py#L103"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>

### <kbd>function</kbd> `__init__`

Expand Down Expand Up @@ -67,7 +67,7 @@ Construct the runner instance.

---

<a href="../src/runner.py#L132"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>
<a href="../src/runner.py#L133"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>

### <kbd>function</kbd> `create`

Expand All @@ -91,7 +91,7 @@ Create the runner instance on LXD and register it on GitHub.

---

<a href="../src/runner.py#L168"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>
<a href="../src/runner.py#L169"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>

### <kbd>function</kbd> `remove`

Expand Down
8 changes: 7 additions & 1 deletion src/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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.
Expand Down
4 changes: 4 additions & 0 deletions tests/integration/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions tests/integration/test_charm_one_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
15 changes: 7 additions & 8 deletions tests/integration/test_self_hosted_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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

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

0 comments on commit 6fdc6ce

Please sign in to comment.