Skip to content

Commit

Permalink
test: fix & separate tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yanksyoon committed Apr 10, 2024
1 parent 69d22bf commit a324e45
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 38 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/integration_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
pre-run-script: scripts/pre-integration-test.sh
provider: lxd
test-tox-env: integration-juju2.9
modules: '["test_charm_fork_repo", "test_charm_no_runner", "test_charm_scheduled_events", "test_charm_one_runner", "test_charm_metrics_success", "test_charm_metrics_failure", "test_self_hosted_runner", "test_charm_with_proxy", "test_charm_with_juju_storage", "test_debug_ssh"]'
modules: '["test_charm_base_image", "test_charm_fork_repo", "test_charm_no_runner", "test_charm_scheduled_events", "test_charm_one_runner", "test_charm_metrics_success", "test_charm_metrics_failure", "test_self_hosted_runner", "test_charm_with_proxy", "test_charm_with_juju_storage", "test_debug_ssh"]'
integration-tests:
name: Integration test with juju 3.1
uses: canonical/operator-workflows/.github/workflows/integration_test.yaml@main
Expand All @@ -25,7 +25,7 @@ jobs:
pre-run-script: scripts/pre-integration-test.sh
provider: lxd
test-tox-env: integration-juju3.1
modules: '["test_charm_fork_repo", "test_charm_no_runner", "test_charm_scheduled_events", "test_charm_one_runner", "test_charm_metrics_success", "test_charm_metrics_failure", "test_self_hosted_runner", "test_charm_with_proxy", "test_charm_with_juju_storage", "test_debug_ssh"]'
modules: '["test_charm_base_image", "test_charm_fork_repo", "test_charm_no_runner", "test_charm_scheduled_events", "test_charm_one_runner", "test_charm_metrics_success", "test_charm_metrics_failure", "test_self_hosted_runner", "test_charm_with_proxy", "test_charm_with_juju_storage", "test_debug_ssh"]'
# openstack tests use microstack, whose setup is kind of special
# - due to the huge resource requirements, we use self-hosted runners for these tests
# - microstack requires juju 3.2 and microk8s 1.26
Expand All @@ -42,6 +42,6 @@ jobs:
channel: 1.26-strict/stable
microk8s-addons: "dns ingress hostpath-storage"
test-tox-env: integration-juju3.2
modules: '["test_openstack"]'
modules: '["test_openstack_base_image", "test_openstack"]'
self-hosted-runner: true
self-hosted-runner-label: two-xlarge
56 changes: 56 additions & 0 deletions tests/integration/test_charm_base_image.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Copyright 2024 Canonical Ltd.
# See LICENSE file for licensing details.

"""Integration tests for github-runner charm containing one runner."""

from github.Branch import Branch
from github.Repository import Repository
from github.WorkflowRun import WorkflowRun
from juju.application import Application
from juju.model import Model

from charm_state import BASE_IMAGE_CONFIG_NAME
from tests.integration.helpers import (
DISPATCH_E2E_TEST_RUN_WORKFLOW_FILENAME,
dispatch_workflow,
ensure_charm_has_runner,
get_runner_name,
run_in_lxd_instance,
)


async def test_runner_base_image(
model: Model,
app_no_runner: Application,
github_repository: Repository,
test_github_branch: Branch,
) -> None:
"""
arrange: A runner with noble as base image.
act: Dispatch a workflow.
assert: A runner is created with noble OS base and the workflow job is successfully run.
"""
await app_no_runner.set_config(
{
BASE_IMAGE_CONFIG_NAME: "noble",
}
)
await ensure_charm_has_runner(app_no_runner, model)

unit = app_no_runner.units[0]
runner_name = await get_runner_name(unit)
code, stdout, stderr = await run_in_lxd_instance(unit, runner_name, "lsb_release -a")
assert code == 0, f"Unable to get release name, {stdout} {stderr}"
assert "noble" in str(stdout)

workflow = await dispatch_workflow(
app=app_no_runner,
branch=test_github_branch,
github_repository=github_repository,
conclusion="success",
workflow_id_or_name=DISPATCH_E2E_TEST_RUN_WORKFLOW_FILENAME,
dispatch_input={"runner-tag": app_no_runner.name},
)

workflow_run: WorkflowRun = workflow.get_runs()[0]
assert workflow_run.status == "success"
35 changes: 0 additions & 35 deletions tests/integration/test_charm_one_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,12 @@

import pytest
import pytest_asyncio
from github.Branch import Branch
from github.Repository import Repository
from github.WorkflowRun import WorkflowRun
from juju.application import Application
from juju.model import Model

from charm import GithubRunnerCharm
from charm_state import (
BASE_IMAGE_CONFIG_NAME,
RUNNER_STORAGE_CONFIG_NAME,
TOKEN_CONFIG_NAME,
VIRTUAL_MACHINES_CONFIG_NAME,
Expand All @@ -23,9 +20,7 @@
VM_MEMORY_CONFIG_NAME,
)
from tests.integration.helpers import (
DISPATCH_E2E_TEST_RUN_WORKFLOW_FILENAME,
assert_resource_lxd_profile,
dispatch_workflow,
ensure_charm_has_runner,
get_runner_names,
reconcile,
Expand Down Expand Up @@ -310,33 +305,3 @@ async def test_token_config_changed_insufficient_perms(
await model.wait_for_idle()

await wait_till_num_of_runners(unit, num=0)


async def test_runner_base_image(
model: Model,
app_no_runner: Application,
github_repository: Repository,
test_github_branch: Branch,
) -> None:
"""
arrange: A runner with noble as base image.
act: Dispatch a workflow.
assert: A runner should work with the different images.
"""
await app_no_runner.set_config(
{
BASE_IMAGE_CONFIG_NAME: "noble",
}
)
await ensure_charm_has_runner(app_no_runner, model)
workflow = await dispatch_workflow(
app=app_no_runner,
branch=test_github_branch,
github_repository=github_repository,
conclusion="success",
workflow_id_or_name=DISPATCH_E2E_TEST_RUN_WORKFLOW_FILENAME,
dispatch_input={"runner-tag": app_no_runner.name},
)

workflow_run: WorkflowRun = workflow.get_runs()[0]
assert workflow_run.status == "success"
55 changes: 55 additions & 0 deletions tests/integration/test_openstack_base_image.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Copyright 2024 Canonical Ltd.
# See LICENSE file for licensing details.

"""Integration tests for OpenStack integration."""

import openstack.connection
from github.Branch import Branch
from github.Repository import Repository
from github.WorkflowRun import WorkflowRun
from juju.application import Application
from juju.model import Model
from openstack.compute.v2.server import Server

from charm_state import BASE_IMAGE_CONFIG_NAME
from tests.integration.helpers import DISPATCH_E2E_TEST_RUN_WORKFLOW_FILENAME, dispatch_workflow


async def test_noble_base_image(
model: Model,
app_openstack_runner: Application,
openstack_connection: openstack.connection.Connection,
github_repository: Repository,
test_github_branch: Branch,
) -> None:
"""
arrange: A runner with noble as base image.
act: Dispatch a workflow.
assert: A runner should work with the different base image.
"""
await app_openstack_runner.set_config(
{
BASE_IMAGE_CONFIG_NAME: "noble",
}
)
await model.wait_for_idle(apps=[app_openstack_runner.name], status="blocked", timeout=40 * 60)

# 1. when the e2e_test_run workflow is created.
workflow = await dispatch_workflow(
app=app_openstack_runner,
branch=test_github_branch,
github_repository=github_repository,
conclusion="success",
workflow_id_or_name=DISPATCH_E2E_TEST_RUN_WORKFLOW_FILENAME,
dispatch_input={"runner-tag": app_openstack_runner.name},
)
# 1. the workflow run completes successfully.
workflow_run: WorkflowRun = workflow.get_runs()[0]
assert workflow_run.status == "success"

# 2. when the servers are listed.
servers = openstack_connection.list_servers(detailed=True)
assert len(servers) == 1, f"Unexpected number of servers: {len(servers)}"
server: Server = servers[0]
# 2. a server with image name containing noble is created.
assert "noble" in server.image.name

0 comments on commit a324e45

Please sign in to comment.