Skip to content

Commit

Permalink
Cache charmcraft pack container, skip unstable tests except on schedu…
Browse files Browse the repository at this point in the history
…le (#27)
  • Loading branch information
carlcsaposs-canonical authored Feb 15, 2023
1 parent 8607528 commit d4816a0
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 83 deletions.
82 changes: 45 additions & 37 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
name: integration and other tests
# Copyright 2022 Canonical Ltd.
# See LICENSE file for licensing details.
name: Tests

on:
workflow_call:
pull_request:
schedule:
- cron: '53 0 * * *' # Daily at 00:53 UTC
# Triggered on push to branch "main" by .github/workflows/release.yaml
workflow_call:

jobs:
lint:
Expand All @@ -11,68 +17,70 @@ jobs:
- name: Checkout
uses: actions/checkout@v3
- name: Install tox
# TODO: Consider replacing with custom image on self-hosted runner OR pinning version
run: python3 -m pip install tox
- name: Run linters
run: tox -e lint
run: tox run -e lint

unit-test:
name: Unit tests
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Install tox
# TODO: Consider replacing with custom image on self-hosted runner OR pinning version
run: python3 -m pip install tox
- name: Run tests
run: tox -e unit
integration-test-microk8s:
name: Integration tests (microk8s)
needs:
- lint
- unit-test
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Setup operator environment
uses: charmed-kubernetes/actions-operator@main
with:
provider: microk8s
# This is needed until https://bugs.launchpad.net/juju/+bug/1977582 is fixed
channel: 1.23/stable
bootstrap-options: "--agent-version 2.9.29"
- name: Install tox
run: python3 -m pip install tox
- name: Run integration tests
# set a predictable model name so it can be consumed by charm-logdump-action
run: sg microk8s -c "tox -e integration -- --model testing"
- name: Dump logs
uses: canonical/charm-logdump-action@main
if: failure()
with:
# TEMPLATE-TODO: Replace the application name
app: operator-template
model: testing
run: tox run -e unit

build:
name: Build charms
uses: canonical/data-platform-workflows/.github/workflows/build_charms_with_cache.yaml@v1

integration-test-database-microk8s:
name: Integration tests for database relations (microk8s)
integration-test:
strategy:
fail-fast: false
matrix:
tox-environments:
- integration
- integration-database
name: ${{ matrix.tox-environments }}
needs:
- lint
- unit-test
- build
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Setup operator environment
# TODO: Replace with custom image on self-hosted runner
uses: charmed-kubernetes/actions-operator@main
with:
provider: microk8s
# This is needed until https://bugs.launchpad.net/juju/+bug/1977582 is fixed
bootstrap-options: "--agent-version 2.9.29"
- name: Install tox
run: python3 -m pip install tox
- name: Download packed charm(s)
uses: actions/download-artifact@v3
with:
name: ${{ needs.build.outputs.artifact-name }}
- name: Select tests
id: select-tests
run: |
if [ "${{ github.event_name }}" == "schedule" ]
then
echo Running unstable and stable tests
echo "mark_expression=" >> $GITHUB_OUTPUT
else
echo Skipping unstable tests
echo "mark_expression=not unstable" >> $GITHUB_OUTPUT
fi
- name: Run integration tests
# set a predictable model name so it can be consumed by charm-logdump-action
run: sg microk8s -c "tox -e integration-database -- --model testing"
run: sg microk8s -c "tox run -e ${{ matrix.tox-environments }} -- -m '${{ steps.select-tests.outputs.mark_expression }}' --model testing"
env:
CI_PACKED_CHARMS: ${{ needs.build.outputs.charms }}
- name: Dump logs
uses: canonical/charm-logdump-action@main
if: failure()
Expand Down
16 changes: 8 additions & 8 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,21 @@ this operator.

## Developing

You can use the environments created by `tox` for development:
You can create an environment for development with `tox`:

```shell
tox --notest -e unit
source .tox/unit/bin/activate
tox devenv -e integration
source venv/bin/activate
```

### Testing

```shell
tox -e fmt # update your code according to linting rules
tox -e lint # code style
tox -e unit # unit tests
tox -e integration # integration tests
tox # runs 'lint' and 'unit' environments
tox run -e format # update your code according to linting rules
tox run -e lint # code style
tox run -e unit # unit tests
tox run -e integration # integration tests
tox # runs 'lint' and 'unit' environments
```

## Build charm
Expand Down
4 changes: 1 addition & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ show_missing = true
minversion = "6.0"
log_cli_level = "INFO"
asyncio_mode = "auto"
markers = [
"database_tests",
]
markers = ["unstable"]

# Formatting tools configuration
[tool.black]
Expand Down
2 changes: 2 additions & 0 deletions tests/integration/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Copyright 2023 Canonical Ltd.
# See LICENSE file for licensing details.
28 changes: 28 additions & 0 deletions tests/integration/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/usr/bin/env python3
# Copyright 2022 Canonical Ltd.
# See LICENSE file for licensing details.

import json
import os
from pathlib import Path

import pytest
from pytest_operator.plugin import OpsTest


@pytest.fixture
def ops_test(ops_test: OpsTest) -> OpsTest:
if os.environ.get("CI") == "true":
# Running in GitHub Actions; skip build step
# (GitHub Actions uses a separate, cached build step. See .github/workflows/ci.yaml)
packed_charms = json.loads(os.environ["CI_PACKED_CHARMS"])

async def build_charm(charm_path, bases_index: int = None) -> Path:
for charm in packed_charms:
if Path(charm_path) == Path(charm["directory_path"]):
if bases_index is None or bases_index == charm["bases_index"]:
return charm["file_path"]
raise ValueError(f"Unable to find .charm file for {bases_index=} at {charm_path=}")

ops_test.build_charm = build_charm
return ops_test
7 changes: 3 additions & 4 deletions tests/integration/test_charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@

import pytest
import yaml
from helpers import (
from pytest_operator.plugin import OpsTest

from .helpers import (
execute_queries_on_unit,
get_inserted_data_by_application,
get_server_config_credentials,
get_unit_address,
scale_application,
)
from pytest_operator.plugin import OpsTest

logger = logging.getLogger(__name__)

Expand All @@ -28,9 +29,7 @@
SLOW_TIMEOUT = 15 * 60


@pytest.mark.order(1)
@pytest.mark.abort_on_fail
@pytest.mark.database_tests
async def test_database_relation(ops_test: OpsTest):
"""Test the database relation."""
# Build and deploy applications
Expand Down
53 changes: 22 additions & 31 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
# Copyright 2022 Canonical Ltd.
# Copyright 2021 Canonical Ltd.
# See LICENSE file for licensing details.

[tox]
skipsdist=True
no_package = True
skip_missing_interpreters = True
envlist = lint, unit
env_list = lint, unit

[vars]
src_path = {toxinidir}/src/
tst_path = {toxinidir}/tests/
;lib_path = {toxinidir}/lib/charms/operator_name_with_underscores
all_path = {[vars]src_path} {[vars]tst_path}
src_path = {tox_root}/src
tests_path = {tox_root}/tests
;lib_path = {tox_root}/lib/charms/operator_name_with_underscores
all_path = {[vars]src_path} {[vars]tests_path}

[testenv]
setenv =
PYTHONPATH = {toxinidir}:{toxinidir}/lib:{[vars]src_path}
set_env =
PYTHONPATH = {tox_root}/lib:{[vars]src_path}
PYTHONBREAKPOINT=ipdb.set_trace
PY_COLORS=1
passenv =
pass_env =
PYTHONPATH
CHARM_BUILD_DIR
MODEL_SETTINGS

[testenv:fmt]
[testenv:format]
description = Apply coding style standards to code
deps =
black
Expand All @@ -46,9 +46,9 @@ deps =
commands =
# uncomment the following line if this charm owns a lib
# codespell {[vars]lib_path}
codespell {toxinidir} --skip {toxinidir}/.git --skip {toxinidir}/.tox \
--skip {toxinidir}/build --skip {toxinidir}/lib --skip {toxinidir}/venv \
--skip {toxinidir}/.mypy_cache --skip {toxinidir}/icon.svg
codespell {tox_root} --skip {tox_root}/.git --skip {tox_root}/.tox \
--skip {tox_root}/build --skip {tox_root}/lib --skip {tox_root}/venv \
--skip {tox_root}/.mypy_cache --skip {tox_root}/icon.svg
# pflake8 wrapper supports config from pyproject.toml
pflake8 {[vars]all_path}
isort --check-only --diff {[vars]all_path}
Expand All @@ -61,32 +61,23 @@ deps =
mysql-connector-python==8.0.30
pytest
coverage[toml]
-r{toxinidir}/requirements.txt
-r {tox_root}/requirements.txt
commands =
coverage run --source={[vars]src_path} \
-m pytest --ignore={[vars]tst_path}integration -v --tb native -s {posargs}
-m pytest -v --tb native -s {posargs} {[vars]tests_path}/unit
coverage report

[testenv:integration]
description = Run integration tests
deps =
juju==2.9.38.1
mysql-connector-python
pytest
pytest-operator
pytest-order
-r{toxinidir}/requirements.txt
commands =
pytest -v --tb native --ignore={[vars]tst_path}unit --log-cli-level=INFO -s {posargs}

[testenv:integration-database]
description = Run integration tests for the database relation
pass_env =
{[testenv]pass_env}
CI
CI_PACKED_CHARMS
deps =
juju==2.9.38.1
mysql-connector-python
pytest
pytest-operator
pytest-order
-r{toxinidir}/requirements.txt
-r {tox_root}/requirements.txt
commands =
pytest -v --tb native --ignore={[vars]tst_path}unit --log-cli-level=INFO -s {posargs} -m "database_tests"
pytest -v --tb native --log-cli-level=INFO -s {posargs} {[vars]tests_path}/integration/test_charm.py

0 comments on commit d4816a0

Please sign in to comment.