Skip to content

Commit 61bd261

Browse files
Remove patching of private ops class. (#617)
1 parent f9032b8 commit 61bd261

File tree

6 files changed

+6
-82
lines changed

6 files changed

+6
-82
lines changed

tests/helpers.py

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,8 @@
33

44

55
from pathlib import Path
6-
from typing import Callable
7-
from unittest.mock import patch
86

97
import yaml
108

119
METADATA = yaml.safe_load(Path("./metadata.yaml").read_text())
1210
STORAGE_PATH = METADATA["storage"]["pgdata"]["location"]
13-
14-
15-
def patch_network_get(private_address="1.1.1.1") -> Callable:
16-
def network_get(*args, **kwargs) -> dict:
17-
"""Patch for the not-yet-implemented testing backend needed for `bind_address`.
18-
19-
This patch decorator can be used for cases such as:
20-
self.model.get_binding(event.relation).network.bind_address
21-
"""
22-
return {
23-
"bind-addresses": [
24-
{
25-
"addresses": [{"value": private_address}],
26-
}
27-
]
28-
}
29-
30-
return patch("ops.testing._TestingModelBackend.network_get", network_get)

tests/unit/test_backups.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
from backups import ListBackupsError
1919
from charm import PostgresqlOperatorCharm
2020
from constants import PEER
21-
from tests.helpers import patch_network_get
2221

2322
ANOTHER_CLUSTER_REPOSITORY_ERROR_MESSAGE = "the S3 repository has backups from another cluster"
2423
FAILED_TO_ACCESS_CREATE_BUCKET_ERROR_MESSAGE = (
@@ -133,7 +132,6 @@ def test_can_initialise_stanza(harness):
133132
)
134133

135134

136-
@patch_network_get(private_address="1.1.1.1")
137135
def test_can_unit_perform_backup(harness):
138136
with (
139137
patch("charm.PostgreSQLBackups._are_backup_settings_ok") as _are_backup_settings_ok,
@@ -223,7 +221,6 @@ def test_can_unit_perform_backup(harness):
223221
)
224222

225223

226-
@patch_network_get(private_address="1.1.1.1")
227224
def test_can_use_s3_repository(harness):
228225
with (
229226
patch("charm.Patroni.reload_patroni_configuration") as _reload_patroni_configuration,
@@ -733,7 +730,6 @@ def test_list_backups(harness):
733730
)
734731

735732

736-
@patch_network_get(private_address="1.1.1.1")
737733
def test_initialise_stanza(harness):
738734
with (
739735
patch("charm.Patroni.reload_patroni_configuration") as _reload_patroni_configuration,
@@ -822,7 +818,6 @@ def test_initialise_stanza(harness):
822818
tc.assertIsInstance(harness.charm.unit.status, MaintenanceStatus)
823819

824820

825-
@patch_network_get(private_address="1.1.1.1")
826821
def test_check_stanza(harness):
827822
with (
828823
patch("charm.Patroni.reload_patroni_configuration") as _reload_patroni_configuration,
@@ -1445,7 +1440,6 @@ def test_on_list_backups_action(harness):
14451440
mock_event.fail.assert_not_called()
14461441

14471442

1448-
@patch_network_get(private_address="1.1.1.1")
14491443
def test_on_restore_action(harness):
14501444
with (
14511445
patch("charm.Patroni.start_patroni") as _start_patroni,
@@ -1617,7 +1611,6 @@ def test_pre_restore_checks(harness):
16171611
mock_event.fail.assert_not_called()
16181612

16191613

1620-
@patch_network_get(private_address="1.1.1.1")
16211614
@pytest.mark.parametrize(
16221615
"tls_ca_chain_filename",
16231616
["", "/var/snap/charmed-postgresql/common/pgbackrest-tls-ca-chain.crt"],
@@ -1706,7 +1699,6 @@ def test_render_pgbackrest_conf_file(harness, tls_ca_chain_filename):
17061699
_render_file.assert_has_calls(calls)
17071700

17081701

1709-
@patch_network_get(private_address="1.1.1.1")
17101702
def test_restart_database(harness):
17111703
with (
17121704
patch("charm.Patroni.start_patroni") as _start_patroni,

tests/unit/test_charm.py

Lines changed: 6 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
)
3939
from cluster import NotReadyError, RemoveRaftMemberFailedError
4040
from constants import PEER, POSTGRESQL_SNAP_NAME, SECRET_INTERNAL_LABEL, SNAP_PACKAGES
41-
from tests.helpers import patch_network_get
4241

4342
CREATE_CLUSTER_CONF_PATH = "/etc/postgresql-common/createcluster.d/pgcharm.conf"
4443

@@ -57,7 +56,6 @@ def harness():
5756
harness.cleanup()
5857

5958

60-
@patch_network_get(private_address="1.1.1.1")
6159
def test_on_install(harness):
6260
with patch("charm.subprocess.check_call") as _check_call, patch(
6361
"charm.snap.SnapCache"
@@ -91,7 +89,6 @@ def test_on_install(harness):
9189
assert isinstance(harness.model.unit.status, WaitingStatus)
9290

9391

94-
@patch_network_get(private_address="1.1.1.1")
9592
def test_on_install_failed_to_create_home(harness):
9693
with patch("charm.subprocess.check_call") as _check_call, patch(
9794
"charm.snap.SnapCache"
@@ -123,7 +120,6 @@ def test_on_install_failed_to_create_home(harness):
123120
assert isinstance(harness.model.unit.status, WaitingStatus)
124121

125122

126-
@patch_network_get(private_address="1.1.1.1")
127123
def test_on_install_snap_failure(harness):
128124
with patch(
129125
"charm.PostgresqlOperatorCharm._install_snap_packages"
@@ -139,21 +135,19 @@ def test_on_install_snap_failure(harness):
139135
assert isinstance(harness.model.unit.status, BlockedStatus)
140136

141137

142-
@patch_network_get(private_address="1.1.1.1")
143138
def test_patroni_scrape_config_no_tls(harness):
144139
result = harness.charm.patroni_scrape_config()
145140

146141
assert result == [
147142
{
148143
"metrics_path": "/metrics",
149144
"scheme": "http",
150-
"static_configs": [{"targets": ["1.1.1.1:8008"]}],
145+
"static_configs": [{"targets": ["192.0.2.0:8008"]}],
151146
"tls_config": {"insecure_skip_verify": True},
152147
},
153148
]
154149

155150

156-
@patch_network_get(private_address="1.1.1.1")
157151
def test_patroni_scrape_config_tls(harness):
158152
with patch(
159153
"charm.PostgresqlOperatorCharm.is_tls_enabled",
@@ -166,7 +160,7 @@ def test_patroni_scrape_config_tls(harness):
166160
{
167161
"metrics_path": "/metrics",
168162
"scheme": "https",
169-
"static_configs": [{"targets": ["1.1.1.1:8008"]}],
163+
"static_configs": [{"targets": ["192.0.2.0:8008"]}],
170164
"tls_config": {"insecure_skip_verify": True},
171165
},
172166
]
@@ -206,7 +200,6 @@ def test_primary_endpoint_no_peers(harness):
206200
assert not _patroni.return_value.get_primary.called
207201

208202

209-
@patch_network_get(private_address="1.1.1.1")
210203
def test_on_leader_elected(harness):
211204
with patch(
212205
"charm.PostgresqlOperatorCharm._update_relation_endpoints", new_callable=PropertyMock
@@ -570,7 +563,6 @@ def test_enable_disable_extensions(harness, caplog):
570563
assert isinstance(harness.charm.unit.status, ActiveStatus)
571564

572565

573-
@patch_network_get(private_address="1.1.1.1")
574566
def test_on_start(harness):
575567
with (
576568
patch(
@@ -669,7 +661,6 @@ def test_on_start(harness):
669661
_restart_services_after_reboot.assert_called_once()
670662

671663

672-
@patch_network_get(private_address="1.1.1.1")
673664
def test_on_start_replica(harness):
674665
with (
675666
patch("charm.snap.SnapCache") as _snap_cache,
@@ -734,7 +725,6 @@ def test_on_start_replica(harness):
734725
assert isinstance(harness.model.unit.status, WaitingStatus)
735726

736727

737-
@patch_network_get(private_address="1.1.1.1")
738728
def test_on_start_no_patroni_member(harness):
739729
with (
740730
patch("subprocess.check_output", return_value=b"C"),
@@ -785,7 +775,6 @@ def test_on_start_after_blocked_state(harness):
785775
assert harness.model.unit.status == initial_status
786776

787777

788-
@patch_network_get(private_address="1.1.1.1")
789778
def test_on_get_password(harness):
790779
with patch("charm.PostgresqlOperatorCharm.update_config"):
791780
rel_id = harness.model.get_relation(PEER).id
@@ -820,7 +809,6 @@ def test_on_get_password(harness):
820809
mock_event.set_results.assert_called_once_with({"password": "replication-test-password"})
821810

822811

823-
@patch_network_get(private_address="1.1.1.1")
824812
def test_on_set_password(harness):
825813
with (
826814
patch("charm.PostgresqlOperatorCharm.update_config"),
@@ -883,7 +871,6 @@ def test_on_set_password(harness):
883871
)
884872

885873

886-
@patch_network_get(private_address="1.1.1.1")
887874
def test_on_update_status(harness):
888875
with (
889876
patch("charm.ClusterTopologyObserver.start_observer") as _start_observer,
@@ -992,7 +979,6 @@ def test_on_update_status(harness):
992979
_start_observer.assert_called_once()
993980

994981

995-
@patch_network_get(private_address="1.1.1.1")
996982
def test_on_update_status_after_restore_operation(harness):
997983
with (
998984
patch("charm.ClusterTopologyObserver.start_observer"),
@@ -1218,7 +1204,6 @@ def test_reboot_on_detached_storage(harness):
12181204
_check_call.assert_called_once_with(["systemctl", "reboot"])
12191205

12201206

1221-
@patch_network_get(private_address="1.1.1.1")
12221207
def test_restart(harness):
12231208
with (
12241209
patch("charm.Patroni.restart_postgresql") as _restart_postgresql,
@@ -1246,7 +1231,6 @@ def test_restart(harness):
12461231
mock_event.defer.assert_not_called()
12471232

12481233

1249-
@patch_network_get(private_address="1.1.1.1")
12501234
def test_update_config(harness):
12511235
with (
12521236
patch("subprocess.check_output", return_value=b"C"),
@@ -1457,7 +1441,6 @@ def test_validate_config_options(harness):
14571441
assert str(e.value).startswith(message)
14581442

14591443

1460-
@patch_network_get(private_address="1.1.1.1")
14611444
def test_on_peer_relation_changed(harness):
14621445
with (
14631446
patch("charm.snap.SnapCache"),
@@ -1501,7 +1484,7 @@ def test_on_peer_relation_changed(harness):
15011484
harness.update_relation_data(
15021485
rel_id,
15031486
harness.charm.app.name,
1504-
{"cluster_initialised": "True", "members_ips": '["1.1.1.1"]'},
1487+
{"cluster_initialised": "True", "members_ips": '["192.0.2.0"]'},
15051488
)
15061489
harness.set_leader()
15071490
_reconfigure_cluster.return_value = False
@@ -1515,7 +1498,7 @@ def test_on_peer_relation_changed(harness):
15151498
_reconfigure_cluster.return_value = True
15161499
_update_member_ip.return_value = False
15171500
_member_started.return_value = True
1518-
_primary_endpoint.return_value = "1.1.1.1"
1501+
_primary_endpoint.return_value = "192.0.2.0"
15191502
harness.model.unit.status = WaitingStatus("awaiting for cluster to start")
15201503
harness.charm._on_peer_relation_changed(mock_event)
15211504
mock_event.defer.assert_not_called()
@@ -1600,7 +1583,6 @@ def test_on_peer_relation_changed(harness):
16001583
_check_stanza.assert_called_once()
16011584

16021585

1603-
@patch_network_get(private_address="1.1.1.1")
16041586
def test_reconfigure_cluster(harness):
16051587
with (
16061588
patch("charm.PostgresqlOperatorCharm._add_members") as _add_members,
@@ -1684,7 +1666,6 @@ def test_update_certificate(harness):
16841666
assert harness.charm.get_secret("unit", "private-key") == private_key
16851667

16861668

1687-
@patch_network_get(private_address="1.1.1.1")
16881669
def test_update_member_ip(harness):
16891670
with (
16901671
patch("charm.PostgresqlOperatorCharm._update_certificate") as _update_certificate,
@@ -1697,7 +1678,7 @@ def test_update_member_ip(harness):
16971678
rel_id,
16981679
harness.charm.unit.name,
16991680
{
1700-
"ip": "1.1.1.1",
1681+
"ip": "192.0.2.0",
17011682
},
17021683
)
17031684
assert not (harness.charm._update_member_ip())
@@ -1717,13 +1698,12 @@ def test_update_member_ip(harness):
17171698
)
17181699
assert harness.charm._update_member_ip()
17191700
relation_data = harness.get_relation_data(rel_id, harness.charm.unit.name)
1720-
assert relation_data.get("ip") == "1.1.1.1"
1701+
assert relation_data.get("ip") == "192.0.2.0"
17211702
assert relation_data.get("ip-to-remove") == "2.2.2.2"
17221703
_stop_patroni.assert_called_once()
17231704
_update_certificate.assert_called_once()
17241705

17251706

1726-
@patch_network_get(private_address="1.1.1.1")
17271707
def test_push_tls_files_to_workload(harness):
17281708
with (
17291709
patch("charm.PostgresqlOperatorCharm.update_config") as _update_config,
@@ -1874,7 +1854,6 @@ def test_scope_obj(harness):
18741854
assert harness.charm._scope_obj("test") is None
18751855

18761856

1877-
@patch_network_get(private_address="1.1.1.1")
18781857
def test_get_secret_from_databag(harness):
18791858
"""Asserts that get_secret method can read secrets from databag.
18801859
@@ -1901,7 +1880,6 @@ def test_get_secret_from_databag(harness):
19011880
assert harness.charm.get_secret("unit", "operator_password") == "test-password"
19021881

19031882

1904-
@patch_network_get(private_address="1.1.1.1")
19051883
def test_on_get_password_secrets(harness):
19061884
with (
19071885
patch("charm.PostgresqlOperatorCharm._on_leader_elected"),
@@ -1932,7 +1910,6 @@ def test_on_get_password_secrets(harness):
19321910

19331911

19341912
@pytest.mark.parametrize("scope,field", [("app", "operator-password"), ("unit", "csr")])
1935-
@patch_network_get(private_address="1.1.1.1")
19361913
def test_get_secret_secrets(harness, scope, field):
19371914
with (
19381915
patch("charm.PostgresqlOperatorCharm._on_leader_elected"),
@@ -1944,7 +1921,6 @@ def test_get_secret_secrets(harness, scope, field):
19441921
assert harness.charm.get_secret(scope, field) == "test"
19451922

19461923

1947-
@patch_network_get(private_address="1.1.1.1")
19481924
def test_set_secret_in_databag(harness, only_without_juju_secrets):
19491925
"""Asserts that set_secret method writes to relation databag.
19501926
@@ -1979,7 +1955,6 @@ def test_set_secret_in_databag(harness, only_without_juju_secrets):
19791955

19801956

19811957
@pytest.mark.parametrize("scope,is_leader", [("app", True), ("unit", True), ("unit", False)])
1982-
@patch_network_get(private_address="1.1.1.1")
19831958
def test_set_reset_new_secret(harness, scope, is_leader):
19841959
with (
19851960
patch("charm.PostgresqlOperatorCharm._on_leader_elected"),
@@ -2001,7 +1976,6 @@ def test_set_reset_new_secret(harness, scope, is_leader):
20011976

20021977

20031978
@pytest.mark.parametrize("scope,is_leader", [("app", True), ("unit", True), ("unit", False)])
2004-
@patch_network_get(private_address="1.1.1.1")
20051979
def test_invalid_secret(harness, scope, is_leader):
20061980
with (
20071981
patch("charm.PostgresqlOperatorCharm._on_leader_elected"),
@@ -2016,7 +1990,6 @@ def test_invalid_secret(harness, scope, is_leader):
20161990
assert harness.charm.get_secret(scope, "somekey") is None
20171991

20181992

2019-
@patch_network_get(private_address="1.1.1.1")
20201993
def test_delete_password(harness, _has_secrets, caplog):
20211994
with (
20221995
patch("charm.PostgresqlOperatorCharm._on_leader_elected"),
@@ -2063,7 +2036,6 @@ def test_delete_password(harness, _has_secrets, caplog):
20632036

20642037

20652038
@pytest.mark.parametrize("scope,is_leader", [("app", True), ("unit", True), ("unit", False)])
2066-
@patch_network_get(private_address="1.1.1.1")
20672039
def test_migration_from_databag(harness, only_with_juju_secrets, scope, is_leader):
20682040
"""Check if we're moving on to use secrets when live upgrade from databag to Secrets usage.
20692041
@@ -2091,7 +2063,6 @@ def test_migration_from_databag(harness, only_with_juju_secrets, scope, is_leade
20912063

20922064

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

0 commit comments

Comments
 (0)