Skip to content

Commit

Permalink
Backout ops and fix linting
Browse files Browse the repository at this point in the history
  • Loading branch information
dragomirp committed Jun 4, 2024
1 parent 046d99a commit 7c5ba5f
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 27 deletions.
10 changes: 5 additions & 5 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ repository = "https://github.com/canonical/postgresql-k8s-operator"

[tool.poetry.dependencies]
python = "^3.10"
ops = "^2.14.0"
ops = "^2.12.0"
cryptography = "^42.0.7"
boto3 = "^1.34.118"
pgconnstr = "^1.0.1"
Expand Down
29 changes: 14 additions & 15 deletions tests/unit/test_charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def test_on_leader_elected(harness):
Endpoints, name=f"patroni-{harness.charm.app.name}", namespace=harness.charm.model.name
)
_client.return_value.patch.assert_not_called()
tc.assertIn("cluster_initialised", harness.get_relation_data(rel_id, harness.charm.app))
assert "cluster_initialised" in harness.get_relation_data(rel_id, harness.charm.app)

# Trigger a new leader election and check that the password is still the same, and that the charm
# fixes the missing "leader" key in the endpoint annotations.
Expand Down Expand Up @@ -397,8 +397,9 @@ def test_on_update_status_with_error_on_get_primary(harness):

with tc.assertLogs("charm", "ERROR") as logs:
harness.charm.on.update_status.emit()
tc.assertIn(
"ERROR:charm:failed to get primary with error RetryError[fake error]", logs.output
assert (
"ERROR:charm:failed to get primary with error RetryError[fake error]"
in logs.output
)


Expand Down Expand Up @@ -694,7 +695,7 @@ def test_on_stop(harness):
)
_client.return_value.list.assert_not_called()
_client.return_value.apply.assert_not_called()
tc.assertIn("failed to get first pod info", "".join(logs.output))
assert "failed to get first pod info" in "".join(logs.output)

# Test when the charm fails to get the k8s resources created by the charm and Patroni.
_client.return_value.get.side_effect = None
Expand All @@ -708,9 +709,8 @@ def test_on_stop(harness):
labels={"app.juju.is/created-by": harness.charm.app.name},
)
_client.return_value.apply.assert_not_called()
tc.assertIn(
"failed to get the k8s resources created by the charm and Patroni",
"".join(logs.output),
assert "failed to get the k8s resources created by the charm and Patroni" in "".join(
logs.output
)

# Test when the charm fails to patch a k8s resource.
Expand All @@ -724,8 +724,8 @@ def test_on_stop(harness):
_client.return_value.apply.side_effect = [None, _FakeApiError]
with tc.assertLogs("charm", "ERROR") as logs:
harness.charm.on.stop.emit()
tc.assertEqual(_client.return_value.apply.call_count, 2)
tc.assertIn("failed to patch k8s MagicMock", "".join(logs.output))
assert _client.return_value.apply.call_count == 2
assert "failed to patch k8s MagicMock" in "".join(logs.output)


def test_client_relations(harness):
Expand Down Expand Up @@ -1433,11 +1433,10 @@ def test_handle_postgresql_restart_need(harness):

harness.charm._handle_postgresql_restart_need()
_reload_patroni_configuration.assert_called_once()
(
tc.assertIn("tls", harness.get_relation_data(rel_id, harness.charm.unit))
if values[0]
else tc.assertNotIn("tls", harness.get_relation_data(rel_id, harness.charm.unit))
)
if values[0]:
assert "tls" in harness.get_relation_data(rel_id, harness.charm.unit)
else:
assert "tls" not in harness.get_relation_data(rel_id, harness.charm.unit)
if (values[0] != values[1]) or values[2]:
_generate_metrics_jobs.assert_called_once_with(values[0])
_restart.assert_called_once()
Expand Down Expand Up @@ -1501,4 +1500,4 @@ def test_set_active_status(harness):
_get_primary.side_effect = values[0]
_get_primary.return_value = None
harness.charm._set_active_status()
tc.assertIsInstance(harness.charm.unit.status, MaintenanceStatus)
assert isinstance(harness.charm.unit.status, MaintenanceStatus)
2 changes: 1 addition & 1 deletion tests/unit/test_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ def test_on_relation_departed(harness):
event.relation.data = {harness.charm.app: {}, harness.charm.unit: {}}
event.departing_unit = harness.charm.unit
harness.charm.legacy_db_relation._on_relation_departed(event)
tc.assertIn("departing", harness.get_relation_data(peer_rel_id, harness.charm.unit))
assert "departing" in harness.get_relation_data(peer_rel_id, harness.charm.unit)

# Test when this unit is departing the relation (due to the relation being broken between the apps).
with harness.hooks_disabled():
Expand Down
8 changes: 4 additions & 4 deletions tests/unit/test_patroni.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,10 +234,10 @@ def test_render_patroni_yml_file(harness, patroni):

# Also, ensure the right parameters are in the expected content
# (as it was already validated with the above render file call).
tc.assertIn("ssl: on", expected_content_with_tls)
tc.assertIn("ssl_ca_file: /var/lib/postgresql/data/ca.pem", expected_content_with_tls)
tc.assertIn("ssl_cert_file: /var/lib/postgresql/data/cert.pem", expected_content_with_tls)
tc.assertIn("ssl_key_file: /var/lib/postgresql/data/key.pem", expected_content_with_tls)
assert "ssl: on" in expected_content_with_tls
assert "ssl_ca_file: /var/lib/postgresql/data/ca.pem" in expected_content_with_tls
assert "ssl_cert_file: /var/lib/postgresql/data/cert.pem" in expected_content_with_tls
assert "ssl_key_file: /var/lib/postgresql/data/key.pem" in expected_content_with_tls


def test_primary_endpoint_ready(harness, patroni):
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_postgresql_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ def test_on_relation_departed(harness):
event.relation.data = {harness.charm.app: {}, harness.charm.unit: {}}
event.departing_unit = harness.charm.unit
harness.charm.postgresql_client_relation._on_relation_departed(event)
tc.assertIn("departing", harness.get_relation_data(peer_rel_id, harness.charm.unit))
assert "departing" in harness.get_relation_data(peer_rel_id, harness.charm.unit)

# Test when this unit is departing the relation (due to the relation being broken between the apps).
with harness.hooks_disabled():
Expand Down

0 comments on commit 7c5ba5f

Please sign in to comment.