From 7c5ba5f6bba11ecc96aa05e809cc723ad2c58a3f Mon Sep 17 00:00:00 2001 From: Dragomir Penev Date: Tue, 4 Jun 2024 18:14:37 +0300 Subject: [PATCH] Backout ops and fix linting --- poetry.lock | 10 ++++----- pyproject.toml | 2 +- tests/unit/test_charm.py | 29 +++++++++++++------------- tests/unit/test_db.py | 2 +- tests/unit/test_patroni.py | 8 +++---- tests/unit/test_postgresql_provider.py | 2 +- 6 files changed, 26 insertions(+), 27 deletions(-) diff --git a/poetry.lock b/poetry.lock index 56bfa02d27..e02775b1a1 100644 --- a/poetry.lock +++ b/poetry.lock @@ -1,4 +1,4 @@ -# This file is automatically @generated by Poetry 1.7.1 and should not be changed by hand. +# This file is automatically @generated by Poetry 1.8.3 and should not be changed by hand. [[package]] name = "allure-pytest" @@ -1017,13 +1017,13 @@ signedtoken = ["cryptography (>=3.0.0)", "pyjwt (>=2.0.0,<3)"] [[package]] name = "ops" -version = "2.14.0" +version = "2.12.0" description = "The Python library behind great charms" optional = false python-versions = ">=3.8" files = [ - {file = "ops-2.14.0-py3-none-any.whl", hash = "sha256:b1d7b03f87fa154bf0f2c0d688cb117d5ad8a2830966498fc66ba46987ae375d"}, - {file = "ops-2.14.0.tar.gz", hash = "sha256:23d64b5e9433176f171895fbe3f21ad1963eaa073a20ae01f0f38171c688eb1a"}, + {file = "ops-2.12.0-py3-none-any.whl", hash = "sha256:b6f7db8aa2886351d0a2527f0df6c8a34e0d9cf90ddfbb91e734f73259df8ddf"}, + {file = "ops-2.12.0.tar.gz", hash = "sha256:7d88522914728caa13aaf1689637f8b573eaf5d38b7f2b8cf135406ee6ef0fc3"}, ] [package.dependencies] @@ -2145,4 +2145,4 @@ files = [ [metadata] lock-version = "2.0" python-versions = "^3.10" -content-hash = "1f8f1ec2edcc668c07ef8c11e0e3c4b3b6d53ec5a82295fa95e8a33502ec6e98" +content-hash = "fe1a7bfc38152d87035eeecb0c6445707bdfa10558f5d9c3f1c7b3e9073eed92" diff --git a/pyproject.toml b/pyproject.toml index 6c451ec7f1..f122a50ce4 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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" diff --git a/tests/unit/test_charm.py b/tests/unit/test_charm.py index 2b7e7d2f03..949af8517b 100644 --- a/tests/unit/test_charm.py +++ b/tests/unit/test_charm.py @@ -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. @@ -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 ) @@ -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 @@ -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. @@ -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): @@ -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() @@ -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) diff --git a/tests/unit/test_db.py b/tests/unit/test_db.py index f4d9629a55..bf2c894c2d 100644 --- a/tests/unit/test_db.py +++ b/tests/unit/test_db.py @@ -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(): diff --git a/tests/unit/test_patroni.py b/tests/unit/test_patroni.py index 126a82cbe8..ddad83f6ab 100644 --- a/tests/unit/test_patroni.py +++ b/tests/unit/test_patroni.py @@ -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): diff --git a/tests/unit/test_postgresql_provider.py b/tests/unit/test_postgresql_provider.py index 38acb26b37..773386cfc5 100644 --- a/tests/unit/test_postgresql_provider.py +++ b/tests/unit/test_postgresql_provider.py @@ -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():