Skip to content

Commit

Permalink
Add basic integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yhaliaw committed Jul 6, 2023
1 parent e8439ad commit fba27ce
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 30 deletions.
18 changes: 18 additions & 0 deletions .github/workflows/integration_test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: Integration test

on:
pull_request:

jobs:
integration-tests:
name: Integration test
runs-on: ["self-hosted", "integration-runner"]
steps:
- name: Install tox
run: python3 -m pip install --user tox
- name: Run Integration tests
run: |
tox -e integration -- \
--keep-models \
--path ${{secrets.E2E_TESTING_REPO}} \
--token ${{secrets.E2E_TESTING_TOKEN}}
2 changes: 1 addition & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: Tests

on:
pull_request: {}
pull_request:

jobs:
unit-tests:
Expand Down
44 changes: 30 additions & 14 deletions tests/integration/conftest.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,48 @@
# Copyright 2023 Canonical Ltd.
# See LICENSE file for licensing details.

"""Fixtures for github runner charm integration tests."""

from pathlib import Path

import pytest
import pytest_asyncio
import yaml
from ops.model import Application
from pytest_operator.plugin import OpsTest


@pytest.fixture
def metadata():
@pytest.fixture(scope="module")
def metadata() -> dict[str, any]:
"""Metadata information of the charm"""
metadata = Path("./metadata.yaml")
data = yaml.safe_load(metadata.read_text())
return data


@pytest.fixture
def model(ops_test):
return ops_test.model
@pytest.fixture(scope="module")
def path(pytestconfig: pytest.Config) -> str:
path = pytestconfig.getoption("--path")
assert path is not None, "Please specify the --path command line option"
return path


@pytest.fixture(scope="module")
def token(pytestconfig: pytest.Config) -> str:
token = pytestconfig.getoption("--token")
assert token is not None, "Please specify the --token command line option"
return token


@pytest.fixture
def application(model, metadata):
charm_name = metadata["name"]
app = model.applications[charm_name]
return app
@pytest_asyncio.fixture(scope="module")
async def app(ops_test: OpsTest, path: str) -> Application:
charm = await ops_test.build_charm(".")

application = await ops_test.model.deploy(
charm,
series="jammy",
config={path: path, "virtual-machines": 1, "denylist": "10.0.0.0/8"},
constraints={"cores": "4", "mem": "32G", "virt-type": "virtual-machine"},
)

@pytest.fixture
def units(application):
units = application.units
return units
yield application
42 changes: 27 additions & 15 deletions tests/integration/test_charm.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,38 @@
# Copyright 2023 Canonical Ltd.
# See LICENSE file for licensing details.

import logging
"""Integration tests for github-runner charm."""

import pytest

log = logging.getLogger(__name__)


async def file_contents(unit, path):
cmd = "cat {}".format(path)
action = await unit.run(cmd)
return action.results["Stdout"]
from ops.model import ActiveStatus, BlockedStatus, Application
from pytest_operator.plugin import OpsTest


@pytest.mark.asyncio
@pytest.mark.abort_on_fail
async def test_build_and_deploy(ops_test):
my_charm = await ops_test.build_charm(".")
await ops_test.model.deploy(my_charm)
async def test_missing_config(ops_test: OpsTest, app: Application) -> None:
"""
arrange: Deploy an application without token configuration
act: Check the status the application
assert: The application is in blocked status.
"""
await ops_test.model.wait_for_idle()
assert ops_test.model.applications["github-runner"].status == BlockedStatus.name
assert (
ops_test.model.applications["github-runner"].status_message
== "Missing token or org/repo path config"
)


async def test_status(units):
assert units[0].workload_status == "blocked"
assert units[0].workload_status_message == "Missing token or org/repo path config"
@pytest.mark.asyncio
@pytest.mark.abort_on_fail
@pytest.mark.requires_secrets
async def test_config(ops_test: OpsTest, app: Application, token: str) -> None:
"""
arrange: Deploy an application without token configuration
act: Set the token configuration and wait.
assert: The application is in active status.
"""
await app.set_config({"token": token})
await ops_test.model.wait_for_idle()
assert ops_test.model.applications["github-runner"].status == ActiveStatus.name

0 comments on commit fba27ce

Please sign in to comment.