Skip to content

Commit 6a11b8a

Browse files
Parallelize tests (#34)
* Change charm database user * Fix tests user * Add password rotation action * Rework secrets management * Add unit tests * Add constants * Update part of the code to correct actions * Implement password rotation * Improve messages * Improve get password test * Fix comment and remove unneeded else block * Add set password unit test * Improve unit test * Change helper function * Add copyright notice * Improve username retrieval * Add initial code for password rotation test * Fix comment * Add test checks * Small fix * Add retry to helper function * Fix lint problems * Improve integration test * Add pytest mark * Add separate jobs for different tests * Add separate encironments for different tests * Define asyncio mode * Add pytest marks * Fix test name * Remove duplicate job * Squashed commit of the following: commit db8dbd8 Author: Marcelo Henrique Neppel <marcelo.neppel@canonical.com> Date: Fri Aug 26 16:15:15 2022 -0300 Add pytest mark commit b6f2d8a Author: Marcelo Henrique Neppel <marcelo.neppel@canonical.com> Date: Thu Aug 25 14:57:20 2022 -0300 Improve integration test commit 16dce63 Author: Marcelo Henrique Neppel <marcelo.neppel@canonical.com> Date: Thu Aug 25 13:50:42 2022 -0300 Fix lint problems commit 14ec0f0 Author: Marcelo Henrique Neppel <marcelo.neppel@canonical.com> Date: Thu Aug 25 13:48:51 2022 -0300 Add retry to helper function commit 4bc38b8 Author: Marcelo Henrique Neppel <marcelo.neppel@canonical.com> Date: Thu Aug 25 11:56:41 2022 -0300 Small fix commit 34421e5 Author: Marcelo Henrique Neppel <marcelo.neppel@canonical.com> Date: Thu Aug 25 11:47:51 2022 -0300 Add test checks commit 7c75e45 Author: Marcelo Henrique Neppel <marcelo.neppel@canonical.com> Date: Thu Aug 25 09:08:04 2022 -0300 Fix comment commit 8988aa3 Author: Marcelo Henrique Neppel <marcelo.neppel@canonical.com> Date: Thu Aug 25 09:07:22 2022 -0300 Add initial code for password rotation test commit ef2bd31 Author: Marcelo Henrique Neppel <marcelo.neppel@canonical.com> Date: Thu Aug 25 09:06:50 2022 -0300 Improve username retrieval commit be497bc Author: Marcelo Henrique Neppel <marcelo.neppel@canonical.com> Date: Thu Aug 25 08:31:03 2022 -0300 Add copyright notice commit a728a20 Author: Marcelo Henrique Neppel <marcelo.neppel@canonical.com> Date: Thu Aug 25 08:24:32 2022 -0300 Change helper function commit 71f86ac Author: Marcelo Henrique Neppel <marcelo.neppel@canonical.com> Date: Wed Aug 24 16:34:58 2022 -0300 Improve unit test commit 0d7ea40 Author: Marcelo Henrique Neppel <marcelo.neppel@canonical.com> Date: Wed Aug 24 16:32:52 2022 -0300 Add set password unit test commit c92fdbc Author: Marcelo Henrique Neppel <marcelo.neppel@canonical.com> Date: Wed Aug 24 15:42:47 2022 -0300 Fix comment and remove unneeded else block commit 4b7a534 Author: Marcelo Henrique Neppel <marcelo.neppel@canonical.com> Date: Wed Aug 24 13:59:35 2022 -0300 Improve get password test commit 9c9b211 Author: Marcelo Henrique Neppel <marcelo.neppel@canonical.com> Date: Wed Aug 24 13:54:54 2022 -0300 Improve messages commit f7024e4 Author: Marcelo Henrique Neppel <marcelo.neppel@canonical.com> Date: Wed Aug 24 13:46:00 2022 -0300 Implement password rotation commit b8a3fd2 Merge: 4735754 a336356 Author: Marcelo Henrique Neppel <marcelo.neppel@canonical.com> Date: Wed Aug 24 08:36:35 2022 -0300 Merge branch 'main' into password-rotation commit 4735754 Author: Marcelo Henrique Neppel <marcelo.neppel@canonical.com> Date: Tue Aug 23 16:22:37 2022 -0300 Update part of the code to correct actions commit e345432 Merge: ca8f525 36640b2 Author: Marcelo Henrique Neppel <marcelo.neppel@canonical.com> Date: Tue Aug 23 16:15:32 2022 -0300 Merge branch 'rework-secrets' into password-rotation commit 36640b2 Author: Marcelo Henrique Neppel <marcelo.neppel@canonical.com> Date: Tue Aug 23 11:44:53 2022 -0300 Add constants commit 05b707e Author: Marcelo Henrique Neppel <marcelo.neppel@canonical.com> Date: Tue Aug 23 11:38:44 2022 -0300 Add unit tests commit fc17a5e Author: Marcelo Henrique Neppel <marcelo.neppel@canonical.com> Date: Tue Aug 23 10:48:48 2022 -0300 Rework secrets management commit ca8f525 Merge: 4cf20fd 3e1ece8 Author: Marcelo Henrique Neppel <marcelo.neppel@canonical.com> Date: Mon Aug 22 14:54:17 2022 -0300 Merge branch 'main' into password-rotation commit 4cf20fd Merge: d325da6 62c636f Author: Marcelo Henrique Neppel <marcelo.neppel@canonical.com> Date: Wed Aug 17 16:31:52 2022 -0300 Merge branch 'main' into password-rotation commit d325da6 Author: Marcelo Henrique Neppel <marcelo.neppel@canonical.com> Date: Wed Aug 17 16:27:28 2022 -0300 Add password rotation action commit dd395f5 Author: Marcelo Henrique Neppel <marcelo.neppel@canonical.com> Date: Tue Aug 16 10:57:19 2022 -0300 Fix tests user commit fe8bd8b Author: Marcelo Henrique Neppel <marcelo.neppel@canonical.com> Date: Tue Aug 16 10:48:45 2022 -0300 Change charm database user * Improve pytest marks * Register pytest markers * Fix pytest mark
1 parent 5a4aa0f commit 6a11b8a

File tree

9 files changed

+227
-20
lines changed

9 files changed

+227
-20
lines changed

.github/workflows/ci.yaml

Lines changed: 65 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ jobs:
1313
run: python3 -m pip install tox
1414
- name: Run linters
1515
run: tox -e lint
16+
1617
unit-test:
1718
name: Unit tests
1819
runs-on: ubuntu-latest
@@ -23,8 +24,69 @@ jobs:
2324
run: python -m pip install tox
2425
- name: Run tests
2526
run: tox -e unit
26-
integration-test-microk8s:
27-
name: Integration tests (microk8s)
27+
28+
integration-test-microk8s-charm:
29+
name: Integration tests for charm deployment (microk8s)
30+
runs-on: ubuntu-latest
31+
steps:
32+
- name: Checkout
33+
uses: actions/checkout@v2
34+
- name: Setup operator environment
35+
uses: charmed-kubernetes/actions-operator@main
36+
with:
37+
provider: microk8s
38+
# This is needed until https://bugs.launchpad.net/juju/+bug/1977582 is fixed.
39+
bootstrap-options: "--agent-version 2.9.29"
40+
- name: Run integration tests
41+
run: tox -e charm-integration
42+
43+
integration-test-microk8s-database-relation:
44+
name: Integration tests for database relation (microk8s)
45+
runs-on: ubuntu-latest
46+
steps:
47+
- name: Checkout
48+
uses: actions/checkout@v2
49+
- name: Setup operator environment
50+
uses: charmed-kubernetes/actions-operator@main
51+
with:
52+
provider: microk8s
53+
# This is needed until https://bugs.launchpad.net/juju/+bug/1977582 is fixed.
54+
bootstrap-options: "--agent-version 2.9.29"
55+
- name: Run integration tests
56+
run: tox -e database-relation-integration
57+
58+
integration-test-microk8s-db-relation:
59+
name: Integration tests for db relation (microk8s)
60+
runs-on: ubuntu-latest
61+
steps:
62+
- name: Checkout
63+
uses: actions/checkout@v2
64+
- name: Setup operator environment
65+
uses: charmed-kubernetes/actions-operator@main
66+
with:
67+
provider: microk8s
68+
# This is needed until https://bugs.launchpad.net/juju/+bug/1977582 is fixed.
69+
bootstrap-options: "--agent-version 2.9.29"
70+
- name: Run integration tests
71+
run: tox -e db-relation-integration
72+
73+
integration-test-microk8s-db-admin-relation:
74+
name: Integration tests for db-admin relation (microk8s)
75+
runs-on: ubuntu-latest
76+
steps:
77+
- name: Checkout
78+
uses: actions/checkout@v2
79+
- name: Setup operator environment
80+
uses: charmed-kubernetes/actions-operator@main
81+
with:
82+
provider: microk8s
83+
# This is needed until https://bugs.launchpad.net/juju/+bug/1977582 is fixed.
84+
bootstrap-options: "--agent-version 2.9.29"
85+
- name: Run integration tests
86+
run: tox -e db-admin-relation-integration
87+
88+
integration-test-microk8s-password-rotation:
89+
name: Integration tests for password rotation (microk8s)
2890
runs-on: ubuntu-latest
2991
steps:
3092
- name: Checkout
@@ -36,4 +98,4 @@ jobs:
3698
# This is needed until https://bugs.launchpad.net/juju/+bug/1977582 is fixed.
3799
bootstrap-options: "--agent-version 2.9.29"
38100
- name: Run integration tests
39-
run: tox -e integration
101+
run: tox -e password-rotation-integration

.github/workflows/release.yaml

Lines changed: 68 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ jobs:
4242
- name: Run tests
4343
run: tox -e unit
4444

45-
integration-test:
46-
name: Integration tests
45+
integration-test-charm:
46+
name: Integration tests for charm deployment
4747
runs-on: ubuntu-latest
4848
steps:
4949
- name: Checkout
@@ -55,15 +55,79 @@ jobs:
5555
# This is needed until https://bugs.launchpad.net/juju/+bug/1977582 is fixed.
5656
bootstrap-options: "--agent-version 2.9.29"
5757
- name: Run integration tests
58-
run: tox -e integration
58+
run: tox -e charm-integration
59+
60+
integration-test-database-relation:
61+
name: Integration tests for database relation
62+
runs-on: ubuntu-latest
63+
steps:
64+
- name: Checkout
65+
uses: actions/checkout@v2
66+
- name: Setup operator environment
67+
uses: charmed-kubernetes/actions-operator@main
68+
with:
69+
provider: microk8s
70+
# This is needed until https://bugs.launchpad.net/juju/+bug/1977582 is fixed.
71+
bootstrap-options: "--agent-version 2.9.29"
72+
- name: Run integration tests
73+
run: tox -e database-relation-integration
74+
75+
integration-test-db-relation:
76+
name: Integration tests for db relation
77+
runs-on: ubuntu-latest
78+
steps:
79+
- name: Checkout
80+
uses: actions/checkout@v2
81+
- name: Setup operator environment
82+
uses: charmed-kubernetes/actions-operator@main
83+
with:
84+
provider: microk8s
85+
# This is needed until https://bugs.launchpad.net/juju/+bug/1977582 is fixed.
86+
bootstrap-options: "--agent-version 2.9.29"
87+
- name: Run integration tests
88+
run: tox -e db-relation-integration
89+
90+
integration-test-db-admin-relation:
91+
name: Integration tests for db-admin relation
92+
runs-on: ubuntu-latest
93+
steps:
94+
- name: Checkout
95+
uses: actions/checkout@v2
96+
- name: Setup operator environment
97+
uses: charmed-kubernetes/actions-operator@main
98+
with:
99+
provider: microk8s
100+
# This is needed until https://bugs.launchpad.net/juju/+bug/1977582 is fixed.
101+
bootstrap-options: "--agent-version 2.9.29"
102+
- name: Run integration tests
103+
run: tox -e db-admin-relation-integration
104+
105+
integration-test-password-rotation:
106+
name: Integration tests for password rotation
107+
runs-on: ubuntu-latest
108+
steps:
109+
- name: Checkout
110+
uses: actions/checkout@v2
111+
- name: Setup operator environment
112+
uses: charmed-kubernetes/actions-operator@main
113+
with:
114+
provider: microk8s
115+
# This is needed until https://bugs.launchpad.net/juju/+bug/1977582 is fixed.
116+
bootstrap-options: "--agent-version 2.9.29"
117+
- name: Run integration tests
118+
run: tox -e password-rotation-integration
59119

60120
release-to-charmhub:
61121
name: Release to CharmHub
62122
needs:
63123
- lib-check
64124
- lint
65125
- unit-test
66-
- integration-test
126+
- integration-test-charm
127+
- integration-test-database-relation
128+
- integration-test-db-relation
129+
- integration-test-db-admin-relation
130+
- integration-test-password-rotation
67131
runs-on: ubuntu-latest
68132
steps:
69133
- name: Checkout

pyproject.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ show_missing = true
1111
[tool.pytest.ini_options]
1212
minversion = "6.0"
1313
log_cli_level = "INFO"
14+
asyncio_mode = "auto"
15+
markers = [
16+
"charm_tests",
17+
"database_relation_tests",
18+
"db_relation_tests",
19+
"db_admin_relation_tests",
20+
"password_rotation_tests",
21+
]
1422

1523
# Formatting tools configuration
1624
[tool.black]

tests/integration/new_relations/test_new_relations.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232

3333
@pytest.mark.abort_on_fail
34+
@pytest.mark.database_relation_tests
3435
async def test_deploy_charms(ops_test: OpsTest, application_charm, database_charm):
3536
"""Deploy both charms (application and database) to use in the tests."""
3637
# Deploy both charms (multiple units for each application to test that later they correctly
@@ -68,7 +69,7 @@ async def test_deploy_charms(ops_test: OpsTest, application_charm, database_char
6869
await ops_test.model.wait_for_idle(apps=APP_NAMES, status="active", wait_for_units=1)
6970

7071

71-
@pytest.mark.abort_on_fail
72+
@pytest.mark.database_relation_tests
7273
async def test_database_relation_with_charm_libraries(ops_test: OpsTest):
7374
"""Test basic functionality of database relation interface."""
7475
# Relate the charms and wait for them exchanging some connection data.
@@ -122,6 +123,7 @@ async def test_database_relation_with_charm_libraries(ops_test: OpsTest):
122123
cursor.execute("DROP TABLE test;")
123124

124125

126+
@pytest.mark.database_relation_tests
125127
async def test_user_with_extra_roles(ops_test: OpsTest):
126128
"""Test superuser actions and the request for more permissions."""
127129
# Get the connection string to connect to the database.
@@ -142,6 +144,7 @@ async def test_user_with_extra_roles(ops_test: OpsTest):
142144
connection.close()
143145

144146

147+
@pytest.mark.database_relation_tests
145148
async def test_two_applications_doesnt_share_the_same_relation_data(
146149
ops_test: OpsTest, application_charm
147150
):
@@ -176,6 +179,7 @@ async def test_two_applications_doesnt_share_the_same_relation_data(
176179
assert application_connection_string != another_application_connection_string
177180

178181

182+
@pytest.mark.database_relation_tests
179183
async def test_an_application_can_connect_to_multiple_database_clusters(
180184
ops_test: OpsTest, database_charm
181185
):
@@ -208,6 +212,7 @@ async def test_an_application_can_connect_to_multiple_database_clusters(
208212
assert application_connection_string != another_application_connection_string
209213

210214

215+
@pytest.mark.database_relation_tests
211216
async def test_an_application_can_connect_to_multiple_aliased_database_clusters(
212217
ops_test: OpsTest, database_charm
213218
):
@@ -243,6 +248,7 @@ async def test_an_application_can_connect_to_multiple_aliased_database_clusters(
243248
assert application_connection_string != another_application_connection_string
244249

245250

251+
@pytest.mark.database_relation_tests
246252
async def test_an_application_can_request_multiple_databases(ops_test: OpsTest, application_charm):
247253
"""Test that an application can request additional databases using the same interface."""
248254
# Relate the charms using another relation and wait for them exchanging some connection data.
@@ -263,6 +269,7 @@ async def test_an_application_can_request_multiple_databases(ops_test: OpsTest,
263269
assert first_database_connection_string != second_database_connection_string
264270

265271

272+
@pytest.mark.database_relation_tests
266273
async def test_no_read_only_endpoint_in_standalone_cluster(ops_test: OpsTest):
267274
"""Test that there is no read-only endpoint in a standalone cluster."""
268275
async with ops_test.fast_forward():
@@ -280,6 +287,7 @@ async def test_no_read_only_endpoint_in_standalone_cluster(ops_test: OpsTest):
280287
)
281288

282289

290+
@pytest.mark.database_relation_tests
283291
async def test_read_only_endpoint_in_scaled_up_cluster(ops_test: OpsTest):
284292
"""Test that there is read-only endpoint in a scaled up cluster."""
285293
async with ops_test.fast_forward():

tests/integration/test_charm.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
# See LICENSE file for licensing details.
44

55
import logging
6-
import os
76

87
import psycopg2
98
import pytest
@@ -31,11 +30,9 @@
3130
UNIT_IDS = [0, 1, 2]
3231

3332

34-
@pytest.mark.skipif(
35-
os.environ.get("PYTEST_SKIP_DEPLOY", False),
36-
reason="skipping deploy, model expected to be provided.",
37-
)
3833
@pytest.mark.abort_on_fail
34+
@pytest.mark.charm_tests
35+
@pytest.mark.skip_if_deployed
3936
async def test_build_and_deploy(ops_test: OpsTest):
4037
"""Build the charm-under-test and deploy it.
4138
@@ -54,7 +51,7 @@ async def test_build_and_deploy(ops_test: OpsTest):
5451
assert ops_test.model.applications[APP_NAME].units[unit_id].workload_status == "active"
5552

5653

57-
@pytest.mark.charm
54+
@pytest.mark.charm_tests
5855
async def test_application_created_required_resources(ops_test: OpsTest) -> None:
5956
# Compare the k8s resources that the charm and Patroni should create with
6057
# the currently created k8s resources.
@@ -64,6 +61,7 @@ async def test_application_created_required_resources(ops_test: OpsTest) -> None
6461
assert set(existing_resources) == set(expected_resources)
6562

6663

64+
@pytest.mark.charm_tests
6765
@pytest.mark.parametrize("unit_id", UNIT_IDS)
6866
async def test_labels_consistency_across_pods(ops_test: OpsTest, unit_id: int) -> None:
6967
model = ops_test.model.info
@@ -75,6 +73,7 @@ async def test_labels_consistency_across_pods(ops_test: OpsTest, unit_id: int) -
7573
assert pod.metadata.labels["cluster-name"] == f"patroni-{APP_NAME}"
7674

7775

76+
@pytest.mark.charm_tests
7877
@pytest.mark.parametrize("unit_id", UNIT_IDS)
7978
async def test_database_is_up(ops_test: OpsTest, unit_id: int):
8079
# Query Patroni REST API and check the status that indicates
@@ -84,6 +83,7 @@ async def test_database_is_up(ops_test: OpsTest, unit_id: int):
8483
assert result.status_code == 200
8584

8685

86+
@pytest.mark.charm_tests
8787
@pytest.mark.parametrize("unit_id", UNIT_IDS)
8888
async def test_settings_are_correct(ops_test: OpsTest, unit_id: int):
8989
password = await get_password(ops_test)
@@ -123,6 +123,7 @@ async def test_settings_are_correct(ops_test: OpsTest, unit_id: int):
123123
assert settings["postgresql"]["use_pg_rewind"]
124124

125125

126+
@pytest.mark.charm_tests
126127
async def test_cluster_is_stable_after_leader_deletion(ops_test: OpsTest) -> None:
127128
"""Tests that the cluster maintains a primary after the primary is deleted."""
128129
# Find the current primary unit.
@@ -146,6 +147,7 @@ async def test_cluster_is_stable_after_leader_deletion(ops_test: OpsTest) -> Non
146147
assert await get_primary(ops_test, other_unit_id) != "None"
147148

148149

150+
@pytest.mark.charm_tests
149151
async def test_scale_down_and_up(ops_test: OpsTest):
150152
"""Test data is replicated to new units after a scale up."""
151153
# Ensure the initial number of units in the application.
@@ -171,6 +173,7 @@ async def test_scale_down_and_up(ops_test: OpsTest):
171173
await scale_application(ops_test, APP_NAME, initial_scale)
172174

173175

176+
@pytest.mark.charm_tests
174177
async def test_persist_data_through_graceful_restart(ops_test: OpsTest):
175178
"""Test data persists through a graceful restart."""
176179
primary = await get_primary(ops_test)
@@ -199,6 +202,7 @@ async def test_persist_data_through_graceful_restart(ops_test: OpsTest):
199202
connection.cursor().execute("SELECT * FROM gracetest;")
200203

201204

205+
@pytest.mark.charm_tests
202206
async def test_persist_data_through_failure(ops_test: OpsTest):
203207
"""Test data persists through a failure."""
204208
primary = await get_primary(ops_test)
@@ -239,6 +243,7 @@ async def test_persist_data_through_failure(ops_test: OpsTest):
239243
connection.cursor().execute("SELECT * FROM failtest;")
240244

241245

246+
@pytest.mark.charm_tests
242247
async def test_automatic_failover_after_leader_issue(ops_test: OpsTest) -> None:
243248
"""Tests that an automatic failover is triggered after an issue happens in the leader."""
244249
# Find the current primary unit.
@@ -256,6 +261,7 @@ async def test_automatic_failover_after_leader_issue(ops_test: OpsTest) -> None:
256261
assert await get_primary(ops_test) != "None"
257262

258263

264+
@pytest.mark.charm_tests
259265
async def test_application_removal(ops_test: OpsTest) -> None:
260266
# Remove the application to trigger some hooks (like peer relation departed).
261267
await ops_test.model.applications[APP_NAME].remove()
@@ -281,7 +287,7 @@ async def test_application_removal(ops_test: OpsTest) -> None:
281287
assert APP_NAME not in ops_test.model.applications
282288

283289

284-
@pytest.mark.charm
290+
@pytest.mark.charm_tests
285291
async def test_redeploy_charm_same_model(ops_test: OpsTest):
286292
"""Redeploy the charm in the same model to test that it works."""
287293
charm = await ops_test.build_charm(".")

tests/integration/test_db.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env python3
22
# Copyright 2022 Canonical Ltd.
33
# See LICENSE file for licensing details.
4-
4+
import pytest as pytest
55
from pytest_operator.plugin import OpsTest
66

77
from tests.helpers import METADATA
@@ -18,6 +18,7 @@
1818
DATABASE_UNITS = 3
1919

2020

21+
@pytest.mark.db_relation_tests
2122
async def test_finos_waltz_db(ops_test: OpsTest) -> None:
2223
"""Deploy Finos Waltz to test the 'db' relation.
2324

0 commit comments

Comments
 (0)