Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests: remove patching of private ops class #692

Merged
merged 1 commit into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 0 additions & 20 deletions tests/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,8 @@
# See LICENSE file for licensing details.

from pathlib import Path
from typing import Callable
from unittest.mock import patch

import yaml

METADATA = yaml.safe_load(Path("./metadata.yaml").read_text())
STORAGE_PATH = METADATA["storage"]["pgdata"]["location"]


def patch_network_get(private_address="10.1.157.116") -> Callable:
def network_get(*args, **kwargs) -> dict:
"""Patch for the not-yet-implemented testing backend needed for `bind_address`.

This patch decorator can be used for cases such as:
self.model.get_binding(event.relation).network.bind_address
"""
return {
"bind-addresses": [
{
"addresses": [{"value": private_address}],
}
]
}

return patch("ops.testing._TestingModelBackend.network_get", network_get)
18 changes: 1 addition & 17 deletions tests/unit/test_charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
from charm import PostgresqlOperatorCharm
from constants import PEER, SECRET_INTERNAL_LABEL
from patroni import NotReadyError
from tests.helpers import patch_network_get
from tests.unit.helpers import _FakeApiError

POSTGRESQL_CONTAINER = "postgresql"
Expand Down Expand Up @@ -156,23 +155,21 @@ def test_on_leader_elected(harness):
)


@patch_network_get(private_address="1.1.1.1")
def test_get_unit_ip(harness):
with (
patch("charm.PostgresqlOperatorCharm._peers", new_callable=PropertyMock) as _peers,
):
_peers.return_value.data = {sentinel.unit: {"private-address": "2.2.2.2"}}

# Current host
assert harness.charm.get_unit_ip(harness.charm.unit) == "1.1.1.1"
assert harness.charm.get_unit_ip(harness.charm.unit) == "192.0.2.0"

# Not existing unit
assert harness.charm.get_unit_ip(None) is None

assert harness.charm.get_unit_ip(sentinel.unit) == "2.2.2.2"


@patch_network_get(private_address="1.1.1.1")
def test_on_postgresql_pebble_ready(harness):
with (
patch("charm.PostgresqlOperatorCharm._set_active_status") as _set_active_status,
Expand Down Expand Up @@ -427,7 +424,6 @@ def test_on_set_password(harness):
)


@patch_network_get(private_address="1.1.1.1")
def test_on_get_primary(harness):
with patch("charm.Patroni.get_primary") as _get_primary:
mock_event = Mock()
Expand All @@ -437,7 +433,6 @@ def test_on_get_primary(harness):
mock_event.set_results.assert_called_once_with({"primary": "postgresql-k8s-1"})


@patch_network_get(private_address="1.1.1.1")
def test_fail_to_get_primary(harness):
with patch("charm.Patroni.get_primary") as _get_primary:
mock_event = Mock()
Expand All @@ -447,7 +442,6 @@ def test_fail_to_get_primary(harness):
mock_event.set_results.assert_not_called()


@patch_network_get(private_address="1.1.1.1")
def test_on_update_status(harness):
with (
patch("charm.logger") as _logger,
Expand Down Expand Up @@ -526,7 +520,6 @@ def test_on_update_status_no_connection(harness):
_get_primary.assert_not_called()


@patch_network_get(private_address="1.1.1.1")
def test_on_update_status_with_error_on_get_primary(harness):
with (
patch(
Expand Down Expand Up @@ -1102,7 +1095,6 @@ def test_scope_obj(harness):
assert harness.charm._scope_obj("test") is None


@patch_network_get(private_address="1.1.1.1")
def test_get_secret_from_databag(harness):
"""Asserts that get_secret method can read secrets from databag.

Expand All @@ -1129,7 +1121,6 @@ def test_get_secret_from_databag(harness):
assert harness.charm.get_secret("unit", "operator_password") == "test-password"


@patch_network_get(private_address="1.1.1.1")
def test_on_get_password_secrets(harness):
with patch("charm.PostgresqlOperatorCharm._on_leader_elected"):
# Create a mock event and set passwords in peer relation data.
Expand Down Expand Up @@ -1158,7 +1149,6 @@ def test_on_get_password_secrets(harness):


@pytest.mark.parametrize("scope", [("app"), ("unit")])
@patch_network_get(private_address="1.1.1.1")
def test_get_secret_secrets(harness, scope):
with patch("charm.PostgresqlOperatorCharm._on_leader_elected"):
harness.set_leader()
Expand All @@ -1168,7 +1158,6 @@ def test_get_secret_secrets(harness, scope):
assert harness.charm.get_secret(scope, "operator-password") == "test-password"


@patch_network_get(private_address="1.1.1.1")
def test_set_secret_in_databag(harness, only_without_juju_secrets):
"""Asserts that set_secret method writes to relation databag.

Expand Down Expand Up @@ -1204,7 +1193,6 @@ def test_set_secret_in_databag(harness, only_without_juju_secrets):


@pytest.mark.parametrize("scope,is_leader", [("app", True), ("unit", True), ("unit", False)])
@patch_network_get(private_address="1.1.1.1")
def test_set_reset_new_secret(harness, scope, is_leader):
"""NOTE: currently ops.testing seems to allow for non-leader to set secrets too!"""
with patch("charm.PostgresqlOperatorCharm._on_leader_elected"):
Expand All @@ -1224,7 +1212,6 @@ def test_set_reset_new_secret(harness, scope, is_leader):


@pytest.mark.parametrize("scope,is_leader", [("app", True), ("unit", True), ("unit", False)])
@patch_network_get(private_address="1.1.1.1")
def test_invalid_secret(harness, scope, is_leader):
with patch("charm.PostgresqlOperatorCharm._on_leader_elected"):
# App has to be leader, unit can be either
Expand All @@ -1237,7 +1224,6 @@ def test_invalid_secret(harness, scope, is_leader):
assert harness.charm.get_secret(scope, "somekey") is None


@patch_network_get(private_address="1.1.1.1")
def test_delete_password(harness, juju_has_secrets, caplog):
"""NOTE: currently ops.testing seems to allow for non-leader to remove secrets too!"""
with patch("charm.PostgresqlOperatorCharm._on_leader_elected"):
Expand Down Expand Up @@ -1282,7 +1268,6 @@ def test_delete_password(harness, juju_has_secrets, caplog):


@pytest.mark.parametrize("scope,is_leader", [("app", True), ("unit", True), ("unit", False)])
@patch_network_get(private_address="1.1.1.1")
def test_migration_from_databag(harness, only_with_juju_secrets, scope, is_leader):
"""Check if we're moving on to use secrets when live upgrade from databag to Secrets usage.

Expand All @@ -1308,7 +1293,6 @@ def test_migration_from_databag(harness, only_with_juju_secrets, scope, is_leade


@pytest.mark.parametrize("scope,is_leader", [("app", True), ("unit", True), ("unit", False)])
@patch_network_get(private_address="1.1.1.1")
def test_migration_from_single_secret(harness, only_with_juju_secrets, scope, is_leader):
"""Check if we're moving on to use secrets when live upgrade from databag to Secrets usage.

Expand Down
4 changes: 0 additions & 4 deletions tests/unit/test_postgresql_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

from charm import PostgresqlOperatorCharm
from constants import PEER
from tests.helpers import patch_network_get

DATABASE = "test_database"
EXTRA_USER_ROLES = "CREATEDB,CREATEROLE"
Expand Down Expand Up @@ -74,7 +73,6 @@ def request_database(_harness):
)


@patch_network_get(private_address="1.1.1.1")
def test_on_database_requested(harness):
with (
patch.object(PostgresqlOperatorCharm, "postgresql", Mock()) as postgresql_mock,
Expand Down Expand Up @@ -161,7 +159,6 @@ def test_on_database_requested(harness):
assert isinstance(harness.model.unit.status, BlockedStatus)


@patch_network_get(private_address="1.1.1.1")
def test_on_relation_departed(harness):
with patch("charm.Patroni.member_started", new_callable=PropertyMock(return_value=True)):
peer_rel_id = harness.model.get_relation(PEER).id
Expand All @@ -183,7 +180,6 @@ def test_on_relation_departed(harness):
assert "departing" not in relation_data


@patch_network_get(private_address="1.1.1.1")
def test_on_relation_broken(harness):
with harness.hooks_disabled():
harness.set_leader()
Expand Down
17 changes: 6 additions & 11 deletions tests/unit/test_postgresql_tls.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

from charm import PostgresqlOperatorCharm
from constants import PEER
from tests.helpers import patch_network_get

RELATION_NAME = "certificates"
SCOPE = "unit"
Expand Down Expand Up @@ -88,7 +87,6 @@ def test_on_set_tls_private_key(harness):
_request_certificate.assert_called_once_with("test-key")


@patch_network_get(private_address="1.1.1.1")
def test_request_certificate(harness):
with (
patch(
Expand All @@ -109,12 +107,12 @@ def test_request_certificate(harness):
generate_csr_call = call(
private_key=b"fake private key",
subject="postgresql-k8s-0.postgresql-k8s-endpoints",
sans_ip=["1.1.1.1"],
sans_ip=["192.0.2.0"],
sans_dns=[
"postgresql-k8s-0",
"postgresql-k8s-0.postgresql-k8s-endpoints",
socket.getfqdn(),
"1.1.1.1",
"192.0.2.0",
f"postgresql-k8s-primary.{harness.charm.model.name}.svc.cluster.local",
f"postgresql-k8s-replicas.{harness.charm.model.name}.svc.cluster.local",
],
Expand Down Expand Up @@ -142,12 +140,12 @@ def test_request_certificate(harness):
custom_key_generate_csr_call = call(
private_key=key.encode("utf-8"),
subject="postgresql-k8s-0.postgresql-k8s-endpoints",
sans_ip=["1.1.1.1"],
sans_ip=["192.0.2.0"],
sans_dns=[
"postgresql-k8s-0",
"postgresql-k8s-0.postgresql-k8s-endpoints",
socket.getfqdn(),
"1.1.1.1",
"192.0.2.0",
f"postgresql-k8s-primary.{harness.charm.model.name}.svc.cluster.local",
f"postgresql-k8s-replicas.{harness.charm.model.name}.svc.cluster.local",
],
Expand Down Expand Up @@ -180,7 +178,6 @@ def test_on_tls_relation_joined(harness):
_request_certificate.assert_called_once_with(None)


@patch_network_get(private_address="1.1.1.1")
def test_on_tls_relation_broken(harness):
with patch("charm.PostgresqlOperatorCharm.update_config") as _update_config:
_update_config.reset_mock()
Expand Down Expand Up @@ -219,7 +216,6 @@ def test_on_certificate_available(harness):
_defer.assert_called_once()


@patch_network_get(private_address="1.1.1.1")
def test_on_certificate_expiring(harness):
with (
patch(
Expand All @@ -241,16 +237,15 @@ def test_on_certificate_expiring(harness):
_request_certificate_renewal.assert_called_once()


@patch_network_get(private_address="1.1.1.1")
def test_get_sans(harness):
sans = harness.charm.tls._get_sans()
assert sans == {
"sans_ip": ["1.1.1.1"],
"sans_ip": ["192.0.2.0"],
"sans_dns": [
"postgresql-k8s-0",
"postgresql-k8s-0.postgresql-k8s-endpoints",
socket.getfqdn(),
"1.1.1.1",
"192.0.2.0",
"postgresql-k8s-primary.None.svc.cluster.local",
"postgresql-k8s-replicas.None.svc.cluster.local",
],
Expand Down
Loading