Skip to content

Commit

Permalink
chore: Building charm in independent step (#139)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gmerold authored Apr 26, 2024
1 parent c59a5d8 commit 0b6f799
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 31 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ on:
jobs:
codeql:
name: CodeQL Analysis
uses: canonical/sdcore-github-workflows/.github/workflows/codeql-analysis.yml@v0.0.1
uses: canonical/sdcore-github-workflows/.github/workflows/codeql-analysis.yml@v1.0.0
2 changes: 1 addition & 1 deletion .github/workflows/dependabot_pr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ permissions:

jobs:
auto-merge:
uses: canonical/sdcore-github-workflows/.github/workflows/dependabot_pr.yaml@v0.0.1
uses: canonical/sdcore-github-workflows/.github/workflows/dependabot_pr.yaml@v1.0.0
2 changes: 1 addition & 1 deletion .github/workflows/issues.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ on:
jobs:
update:
name: Update Issue
uses: canonical/sdcore-github-workflows/.github/workflows/issues.yaml@v0.0.1
uses: canonical/sdcore-github-workflows/.github/workflows/issues.yaml@v1.0.0
secrets:
JIRA_URL: ${{ secrets.JIRA_URL }}
30 changes: 18 additions & 12 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,34 @@ on:

jobs:
check-libraries:
uses: canonical/sdcore-github-workflows/.github/workflows/check-libraries.yaml@v0.0.1
uses: canonical/sdcore-github-workflows/.github/workflows/check-libraries.yaml@v1.0.0
secrets:
CHARMCRAFT_AUTH: ${{ secrets.CHARMCRAFT_AUTH }}

lint-report:
uses: canonical/sdcore-github-workflows/.github/workflows/lint-report.yaml@v0.0.1
uses: canonical/sdcore-github-workflows/.github/workflows/lint-report.yaml@v1.0.0

terraform-check:
uses: canonical/sdcore-github-workflows/.github/workflows/terraform.yaml@v0.0.1
uses: canonical/sdcore-github-workflows/.github/workflows/terraform.yaml@v1.0.0

static-analysis:
uses: canonical/sdcore-github-workflows/.github/workflows/static-analysis.yaml@v0.0.1
uses: canonical/sdcore-github-workflows/.github/workflows/static-analysis.yaml@v1.0.0

unit-tests-with-coverage:
uses: canonical/sdcore-github-workflows/.github/workflows/unit-test.yaml@v0.0.1
uses: canonical/sdcore-github-workflows/.github/workflows/unit-test.yaml@v1.0.0

build:
needs:
- lint-report
- static-analysis
- unit-tests-with-coverage
uses: canonical/sdcore-github-workflows/.github/workflows/build.yaml@v1.0.0
secrets: inherit

integration-test:
uses: canonical/sdcore-github-workflows/.github/workflows/integration-test.yaml@v0.0.1
with:
charm-file-name: "sdcore-pcf-k8s_ubuntu-22.04-amd64.charm"
needs:
- build
uses: canonical/sdcore-github-workflows/.github/workflows/integration-test.yaml@v1.0.0

publish-charm:
name: Publish Charm
Expand All @@ -39,9 +47,8 @@ jobs:
- unit-tests-with-coverage
- integration-test
if: ${{ github.ref_name == 'main' }}
uses: canonical/sdcore-github-workflows/.github/workflows/publish-charm.yaml@v0.0.1
uses: canonical/sdcore-github-workflows/.github/workflows/publish-charm.yaml@v1.0.0
with:
charm-file-name: "sdcore-pcf-k8s_ubuntu-22.04-amd64.charm"
track-name: 1.5
secrets:
CHARMCRAFT_AUTH: ${{ secrets.CHARMCRAFT_AUTH }}
Expand All @@ -54,10 +61,9 @@ jobs:
- unit-tests-with-coverage
- integration-test
if: ${{ (github.ref_name != 'main') && (github.event_name == 'push') }}
uses: canonical/sdcore-github-workflows/.github/workflows/publish-charm.yaml@v0.0.1
uses: canonical/sdcore-github-workflows/.github/workflows/publish-charm.yaml@v1.0.0
with:
branch-name: ${{ github.ref_name }}
charm-file-name: "sdcore-pcf-k8s_ubuntu-22.04-amd64.charm"
track-name: 1.5
secrets:
CHARMCRAFT_AUTH: ${{ secrets.CHARMCRAFT_AUTH }}
2 changes: 1 addition & 1 deletion .github/workflows/promote.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ on:
jobs:
promote:
name: Promote Charm
uses: canonical/sdcore-github-workflows/.github/workflows/promote.yaml@v0.0.1
uses: canonical/sdcore-github-workflows/.github/workflows/promote.yaml@v1.0.0
with:
promotion: ${{ github.event.inputs.promotion }}
track-name: ${{ github.event.inputs.track-name }}
Expand Down
12 changes: 8 additions & 4 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,14 @@ There are some pre-configured environments
that can be used for linting and formatting code when you're preparing contributions to the charm:

```shell
tox -e lint # code style
tox -e static # static analysis
tox -e unit # unit tests
tox -e integration # integration tests
tox -e lint # code style
tox -e static # static analysis
tox -e unit # unit tests
tox -e integration -- --charm_path=PATH_TO_BUILD_CHARM # integration tests
```

```note
Integration tests require the charm to be built with `charmcraft pack` first.
```

## Build
Expand Down
38 changes: 38 additions & 0 deletions tests/integration/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/usr/bin/env python3
# Copyright 2024 Canonical Ltd.
# See LICENSE file for licensing details.

import os

import pytest


def pytest_addoption(parser: pytest.Parser) -> None:
"""Add options to the pytest command line.
This is a pytest hook that is called when the pytest command line is being parsed.
Args:
parser: The pytest command line parser.
"""
parser.addoption(
"--charm_path",
action="store",
default=None,
help="Path to the charm under test"
)


def pytest_configure(config: pytest.Config) -> None:
"""Validate the options provided by the user.
This is a pytest hook that is called after command line options have been parsed.
Args:
config: The pytest configuration object.
"""
charm_path = str(config.getoption("--charm_path"))
if not charm_path:
pytest.exit("The --charm_path option is required. Tests aborted.")
if not os.path.exists(charm_path):
pytest.exit(f"The path specified for the charm under test does not exist: {charm_path}")
22 changes: 11 additions & 11 deletions tests/integration/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ async def _deploy_grafana_agent(ops_test: OpsTest):

@pytest.fixture(scope="module")
@pytest.mark.abort_on_fail
async def build_and_deploy(ops_test: OpsTest):
"""Build the charm-under-test and deploy it."""
charm = await ops_test.build_charm(".")
async def deploy(ops_test: OpsTest, request):
"""Deploy necessary components."""
charm = Path(request.config.getoption("--charm_path")).resolve()
resources = {
"pcf-image": METADATA["resources"]["pcf-image"]["upstream-source"],
}
Expand All @@ -86,7 +86,7 @@ async def build_and_deploy(ops_test: OpsTest):

@pytest.mark.abort_on_fail
async def test_given_charm_is_built_when_deployed_then_status_is_blocked(
ops_test: OpsTest, build_and_deploy
ops_test: OpsTest, deploy
):
await ops_test.model.wait_for_idle( # type: ignore[union-attr]
apps=[APPLICATION_NAME],
Expand All @@ -96,7 +96,7 @@ async def test_given_charm_is_built_when_deployed_then_status_is_blocked(


@pytest.mark.abort_on_fail
async def test_relate_and_wait_for_active_status(ops_test: OpsTest, build_and_deploy):
async def test_relate_and_wait_for_active_status(ops_test: OpsTest, deploy):
assert ops_test.model
await ops_test.model.integrate(relation1=NRF_APP_NAME, relation2=DATABASE_APP_NAME)
await ops_test.model.integrate(relation1=NRF_APP_NAME, relation2=TLS_PROVIDER_NAME)
Expand All @@ -117,13 +117,13 @@ async def test_relate_and_wait_for_active_status(ops_test: OpsTest, build_and_de


@pytest.mark.abort_on_fail
async def test_remove_nrf_and_wait_for_blocked_status(ops_test: OpsTest, build_and_deploy):
async def test_remove_nrf_and_wait_for_blocked_status(ops_test: OpsTest, deploy):
await ops_test.model.remove_application(NRF_APP_NAME, block_until_done=True) # type: ignore[union-attr] # noqa: E501
await ops_test.model.wait_for_idle(apps=[APPLICATION_NAME], status="blocked", timeout=60) # type: ignore[union-attr] # noqa: E501


@pytest.mark.abort_on_fail
async def test_restore_nrf_and_wait_for_active_status(ops_test: OpsTest, build_and_deploy):
async def test_restore_nrf_and_wait_for_active_status(ops_test: OpsTest, deploy):
assert ops_test.model
await _deploy_nrf(ops_test)
await ops_test.model.integrate(
Expand All @@ -135,14 +135,14 @@ async def test_restore_nrf_and_wait_for_active_status(ops_test: OpsTest, build_a


@pytest.mark.abort_on_fail
async def test_remove_tls_and_wait_for_blocked_status(ops_test: OpsTest, build_and_deploy):
async def test_remove_tls_and_wait_for_blocked_status(ops_test: OpsTest, deploy):
assert ops_test.model
await ops_test.model.remove_application(TLS_PROVIDER_NAME, block_until_done=True)
await ops_test.model.wait_for_idle(apps=[APPLICATION_NAME], status="blocked", timeout=60)


@pytest.mark.abort_on_fail
async def test_restore_tls_and_wait_for_active_status(ops_test: OpsTest, build_and_deploy):
async def test_restore_tls_and_wait_for_active_status(ops_test: OpsTest, deploy):
assert ops_test.model
await _deploy_tls_provider(ops_test)
await ops_test.model.integrate(relation1=APPLICATION_NAME, relation2=TLS_PROVIDER_NAME)
Expand All @@ -153,7 +153,7 @@ async def test_restore_tls_and_wait_for_active_status(ops_test: OpsTest, build_a
reason="Bug in MongoDB: https://github.com/canonical/mongodb-k8s-operator/issues/218"
)
@pytest.mark.abort_on_fail
async def test_remove_database_and_wait_for_blocked_status(ops_test: OpsTest, build_and_deploy):
async def test_remove_database_and_wait_for_blocked_status(ops_test: OpsTest, deploy):
assert ops_test.model
await ops_test.model.remove_application(DATABASE_APP_NAME, block_until_done=True)
await ops_test.model.wait_for_idle(apps=[APPLICATION_NAME], status="blocked", timeout=60)
Expand All @@ -163,7 +163,7 @@ async def test_remove_database_and_wait_for_blocked_status(ops_test: OpsTest, bu
reason="Bug in MongoDB: https://github.com/canonical/mongodb-k8s-operator/issues/218"
)
@pytest.mark.abort_on_fail
async def test_restore_database_and_wait_for_active_status(ops_test: OpsTest, build_and_deploy):
async def test_restore_database_and_wait_for_active_status(ops_test: OpsTest, deploy):
assert ops_test.model
await _deploy_database(ops_test)
await ops_test.model.integrate(relation1=APPLICATION_NAME, relation2=DATABASE_APP_NAME)
Expand Down

0 comments on commit 0b6f799

Please sign in to comment.