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
  • Loading branch information
carlcsaposs-canonical authored Feb 24, 2023
1 parent 9735d16 commit 120bf1d
Show file tree
Hide file tree
Showing 19 changed files with 200 additions and 194 deletions.
92 changes: 85 additions & 7 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,90 @@
name: Charmed Operator 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:
uses: ./.github/workflows/lint.yaml
unit:
uses: ./.github/workflows/unit.yaml
integration:
uses: ./.github/workflows/integration.yaml
name: Lint
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 linters
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 run -e unit

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

integration-test:
strategy:
fail-fast: false
matrix:
tox-environments:
- integration
- ha-integration
- tls-integration
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: 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 run -e ${{ matrix.tox-environments }} -- --model testing -m '${{ steps.select-tests.outputs.mark_expression }}'"
env:
CI_PACKED_CHARMS: ${{ needs.build.outputs.charms }}
- name: Dump logs
uses: canonical/charm-logdump-action@main
if: failure()
with:
app: mongodb-k8s
model: testing
77 changes: 0 additions & 77 deletions .github/workflows/integration.yaml

This file was deleted.

16 changes: 0 additions & 16 deletions .github/workflows/lint.yaml

This file was deleted.

16 changes: 0 additions & 16 deletions .github/workflows/unit.yaml

This file was deleted.

10 changes: 5 additions & 5 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ export PATH=$PATH:$HOME/.local/bin
### 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 'fmt', 'lint' and 'unit' environments
tox run -e fmt # 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 'fmt', 'lint' and 'unit' environments
```

## Build charm
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ show_missing = true
[tool.pytest.ini_options]
minversion = "6.0"
log_cli_level = "INFO"
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(scope="module")
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
2 changes: 2 additions & 0 deletions tests/integration/ha_tests/__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.
8 changes: 1 addition & 7 deletions tests/integration/ha_tests/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,7 @@
wait_fixed,
)

from tests.integration.helpers import (
APP_NAME,
get_mongo_cmd,
get_password,
mongodb_uri,
primary_host,
)
from ..helpers import APP_NAME, get_mongo_cmd, get_password, mongodb_uri, primary_host

METADATA = yaml.safe_load(Path("./metadata.yaml").read_text())
MONGODB_CONTAINER_NAME = "mongod"
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/ha_tests/test_ha.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
import pytest_asyncio
from pytest_operator.plugin import OpsTest

from tests.integration.ha_tests.helpers import (
from ..helpers import APP_NAME
from .helpers import (
ANOTHER_DATABASE_APP_NAME,
MONGOD_PROCESS_NAME,
MONGODB_CONTAINER_NAME,
Expand Down Expand Up @@ -43,7 +44,6 @@
verify_writes,
wait_until_unit_in_status,
)
from tests.integration.helpers import APP_NAME

MEDIAN_REELECTION_TIME = 12

Expand Down
11 changes: 6 additions & 5 deletions tests/integration/test_charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@
import time

import pytest
from helpers import (
from lightkube import AsyncClient
from lightkube.resources.core_v1 import Pod
from pymongo import MongoClient
from pytest_operator.plugin import OpsTest

from .helpers import (
APP_NAME,
METADATA,
TEST_DOCUMENTS,
Expand All @@ -19,10 +24,6 @@
run_mongo_op,
secondary_mongo_uris_with_sync_delay,
)
from lightkube import AsyncClient
from lightkube.resources.core_v1 import Pod
from pymongo import MongoClient
from pytest_operator.plugin import OpsTest

logger = logging.getLogger(__name__)

Expand Down
2 changes: 2 additions & 0 deletions tests/integration/tls_tests/__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.
2 changes: 1 addition & 1 deletion tests/integration/tls_tests/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from pytest_operator.plugin import OpsTest
from tenacity import RetryError, Retrying, stop_after_attempt, wait_exponential

from tests.integration.helpers import get_mongo_cmd, get_password
from ..helpers import get_mongo_cmd, get_password

logger = logging.getLogger(__name__)

Expand Down
7 changes: 1 addition & 6 deletions tests/integration/tls_tests/test_tls.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,7 @@
import pytest
from pytest_operator.plugin import OpsTest

from tests.integration.tls_tests.helpers import (
METADATA,
check_tls,
time_file_created,
time_process_started,
)
from .helpers import METADATA, check_tls, time_file_created, time_process_started

TLS_CERTIFICATES_APP_NAME = "tls-certificates-operator"
DATABASE_APP_NAME = "mongodb-k8s"
Expand Down
5 changes: 3 additions & 2 deletions tests/unit/test_charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
)

from charm import MongoDBCharm, NotReadyError
from tests.unit.helpers import patch_network_get

from .helpers import patch_network_get

PYMONGO_EXCEPTIONS = [
(ConnectionFailure("error message"), ConnectionFailure),
Expand Down Expand Up @@ -447,7 +448,7 @@ def test_reconfigure_not_already_initialised(self, connection, defer):

@patch("ops.framework.EventBase.defer")
@patch("charm.MongoDBConnection")
@patch("lib.charms.mongodb.v0.mongodb.MongoClient")
@patch("charms.mongodb.v0.mongodb.MongoClient")
def test_reconfigure_get_members_failure(self, client, connection, defer):
"""Tests reconfigure does not execute when unable to get the replica set members.
Expand Down
Loading

0 comments on commit 120bf1d

Please sign in to comment.