Skip to content

Commit

Permalink
Integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
juditnovak committed May 27, 2024
1 parent 760933e commit 3430095
Show file tree
Hide file tree
Showing 7 changed files with 675 additions and 3 deletions.
24 changes: 22 additions & 2 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,30 @@ jobs:
juju-snap-channel: "3.1/stable"
libjuju-version: "3.2.2"
include:
- {tox-environments: integration-upgrades,
- {tox-environments: integration-interfaces-upgrade-from-version-1-earlier,
juju-version:
{juju-snap-channel: "3.1/stable",
juju-bootstrap-option: "3.1.6",
juju-bootstrap-option: "3.1.7",
libjuju-version: "3.2.2"}}
- {tox-environments: integration-interfaces-upgrade-from-version-2-earlier,
juju-version:
{juju-snap-channel: "3.1/stable",
juju-bootstrap-option: "3.1.7",
libjuju-version: "3.2.2"}}
- {tox-environments: integration-interfaces-upgrade-from-version-3-earlier,
juju-version:
{juju-snap-channel: "3.1/stable",
juju-bootstrap-option: "3.1.7",
libjuju-version: "3.2.2"}}
- {tox-environments: integration-interfaces-upgrade-from-version-4-earlier,
juju-version:
{juju-snap-channel: "3.1/stable",
juju-bootstrap-option: "3.1.7",
libjuju-version: "3.2.2"}}
- {tox-environments: integration-upgrades-databag-to-secrets,
juju-version:
{juju-snap-channel: "3.1/stable",
juju-bootstrap-option: "3.1.7",
libjuju-version: "3.2.2"}}
- {tox-environments: integration-secrets,
juju-version:
Expand Down
2 changes: 2 additions & 0 deletions 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.
37 changes: 37 additions & 0 deletions tests/integration/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import shutil
from datetime import datetime
from pathlib import Path
from subprocess import check_call, check_output

import pytest
from pytest_operator.plugin import OpsTest
Expand Down Expand Up @@ -153,3 +154,39 @@ async def without_errors(ops_test: OpsTest, request):
continue
if logitems[2] == "ERROR":
assert any(white in line for white in whitelist)


@pytest.fixture(scope="session")
def fetch_old_versions():
"""Fetching the previous 4 versions of the lib for upgrade tests."""
cwd = os.getcwd()
src_path = "lib/charms/data_platform_libs/v0/data_interfaces.py"
data_path = f"{cwd}/tests/integration/data/data_interfaces.py"
tmp_path = "./tmp_repo_checkout"

os.mkdir(tmp_path)
os.chdir(tmp_path)
check_call("git clone https://github.com/canonical/data-platform-libs.git", shell=True)
os.chdir("data-platform-libs")
last_commits = check_output(
"git show --pretty=format:'%h' --no-patch -15", shell=True, universal_newlines=True
).split()

versions = []
for commit in last_commits:
check_call(f"git checkout {commit}", shell=True)
version = check_output(
"grep LIBPATCH lib/charms/data_platform_libs/v0/data_interfaces.py | cut -d ' ' -f 3",
shell=True,
universal_newlines=True,
)
version = version.strip()
if version not in versions:
shutil.copyfile(src_path, f"{data_path}.v{version}")
versions.append(version)

if len(versions) == 4:
break

os.chdir(cwd)
shutil.rmtree(tmp_path)
17 changes: 17 additions & 0 deletions tests/integration/database-charm/actions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,23 @@ set-peer-relation-field:
type: string
description: Value of the field to set

set-peer-relation-field-multiple:
description: Set fields from the second-database relation multiple times
params:
component:
type: string
description: app/unit
field:
type: string
description: Relation field
value:
type: string
description: Value of the field to set
count:
type: integer
description: Number of iterations
default: 3

set-peer-secret:
description: Set fields from the second-database relation
params:
Expand Down
31 changes: 31 additions & 0 deletions tests/integration/database-charm/src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ def __init__(self, *args):
self.framework.observe(
self.on.set_peer_relation_field_action, self._on_set_peer_relation_field
)
self.framework.observe(
self.on.set_peer_relation_field_multiple_action,
self._on_set_peer_relation_field_multiple,
)
self.framework.observe(self.on.set_peer_secret_action, self._on_set_peer_secret)
self.framework.observe(
self.on.delete_peer_relation_field_action, self._on_delete_peer_relation_field
Expand Down Expand Up @@ -341,6 +345,33 @@ def _on_set_peer_relation_field(self, event: ActionEvent):
relation.id, {event.params["field"]: event.params["value"]}
)

def _on_set_peer_relation_field_multiple(self, event: ActionEvent):
"""Set requested relation field."""
component = event.params["component"]
count = event.params["count"]

# Charms should be compatible with old vesrions, to simulate rolling upgrade
for cnt in range(count):
value = event.params["value"] + f"{cnt}"
if DATA_INTERFACES_VERSION <= 17:
relation = self.model.get_relation(PEER)
if component == "app":
relation.data[self.app][event.params["field"]] = value
else:
relation.data[self.unit][event.params["field"]] = value
return

if component == "app":
relation = self.peer_relation_app.relations[0]
self.peer_relation_app.update_relation_data(
relation.id, {event.params["field"]: value}
)
else:
relation = self.peer_relation_unit.relations[0]
self.peer_relation_unit.update_relation_data(
relation.id, {event.params["field"]: value}
)

def _on_set_peer_secret(self, event: ActionEvent):
"""Set requested relation field."""
component = event.params["component"]
Expand Down
Loading

0 comments on commit 3430095

Please sign in to comment.