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: don't patch private ops class #519

Merged
merged 1 commit into from
Sep 19, 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
26 changes: 0 additions & 26 deletions tests/unit/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,3 @@
# See LICENSE file for licensing details.

"""Helper functions for writing tests."""

from typing import Callable
from unittest.mock import patch


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": [
{
"mac-address": "",
"interface-name": "",
"addresses": [{"hostname": "", "value": private_address, "cidr": ""}],
}
],
"bind-address": private_address,
"egress-subnets": ["10.152.183.65/32"],
"ingress-addresses": ["10.152.183.65"],
}

return patch("ops.testing._TestingModelBackend.network_get", network_get)
3 changes: 0 additions & 3 deletions tests/unit/test_async_replication.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
SERVER_CONFIG_PASSWORD_KEY,
)

from .helpers import patch_network_get


@patch("charms.rolling_ops.v0.rollingops.RollingOpsManager._on_process_locks")
class TestAsyncRelation(unittest.TestCase):
Expand Down Expand Up @@ -292,7 +290,6 @@ def test_consumer_created_user_data(self, _mysql, _, _unit_initialized):
self.harness.get_relation_data(async_relation_id, self.charm.app.name),
)

@patch_network_get(private_address="1.1.1.1")
@patch("ops.framework.EventBase.defer")
@patch(
"charms.mysql.v0.async_replication.MySQLAsyncReplicationConsumer.returning_cluster",
Expand Down
24 changes: 0 additions & 24 deletions tests/unit/test_backups.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@
from charm import MySQLOperatorCharm
from lib.charms.mysql.v0.backups import S3_INTEGRATOR_RELATION_NAME

from .helpers import patch_network_get


class TestMySQLBackups(unittest.TestCase):
def setUp(self):
Expand Down Expand Up @@ -150,7 +148,6 @@ def test_on_list_backups_failure(self, _list_backups_in_s3_path, _retrieve_s3_pa
event.set_results.assert_not_called()
event.fail.assert_called_once_with("Missing relation with S3 integrator charm")

@patch_network_get(private_address="1.1.1.1")
@patch("charm.MySQLOperatorCharm._on_update_status")
@patch("datetime.datetime")
@patch(
Expand Down Expand Up @@ -205,7 +202,6 @@ def test_on_create_backup(
event.set_results.assert_called_once_with({"backup-id": "2023-03-07%13:43:15Z"})
event.fail.assert_not_called()

@patch_network_get(private_address="1.1.1.1")
@patch("datetime.datetime")
@patch(
"charms.mysql.v0.backups.MySQLBackups._retrieve_s3_parameters",
Expand Down Expand Up @@ -313,7 +309,6 @@ def test_on_create_backup_failure(
event.fail.assert_called_once_with("Missing relation with S3 integrator charm")
self.assertTrue(isinstance(self.harness.model.unit.status, ActiveStatus))

@patch_network_get(private_address="1.1.1.1")
@patch("mysql_vm_helpers.MySQL.offline_mode_and_hidden_instance_exists", return_value=False)
@patch("mysql_vm_helpers.MySQL.get_member_state", return_value=("online", "replica"))
def test_can_unit_perform_backup(
Expand All @@ -326,7 +321,6 @@ def test_can_unit_perform_backup(
self.assertTrue(success)
self.assertIsNone(error_message)

@patch_network_get(private_address="1.1.1.1")
@patch("mysql_vm_helpers.MySQL.offline_mode_and_hidden_instance_exists", return_value=False)
@patch("mysql_vm_helpers.MySQL.get_member_state")
@patch("python_hosts.Hosts.write")
Expand Down Expand Up @@ -378,7 +372,6 @@ def test_can_unit_perform_backup_failure(
self.assertFalse(success)
self.assertEqual(error_message, "Unit is waiting to start or restart")

@patch_network_get(private_address="1.1.1.1")
@patch("mysql_vm_helpers.MySQL.set_instance_option")
@patch("mysql_vm_helpers.MySQL.set_instance_offline_mode")
@patch("python_hosts.Hosts.write")
Expand All @@ -403,7 +396,6 @@ def test_pre_backup(
self.assertTrue(success)
self.assertIsNone(error_message)

@patch_network_get(private_address="1.1.1.1")
@patch("mysql_vm_helpers.MySQL.set_instance_option")
@patch("mysql_vm_helpers.MySQL.set_instance_offline_mode")
def test_pre_backup_failure(
Expand Down Expand Up @@ -431,7 +423,6 @@ def test_pre_backup_failure(
self.assertFalse(success)
self.assertEqual(error_message, "Error setting instance option tag:_hidden")

@patch_network_get(private_address="1.1.1.1")
@patch("mysql_vm_helpers.MySQL.execute_backup_commands", return_value=("stdout", "stderr"))
@patch("charms.mysql.v0.backups.MySQLBackups._upload_logs_to_s3")
def test_backup(
Expand All @@ -452,7 +443,6 @@ def test_backup(
self.assertIsNone(error_message)
_upload_logs_to_s3.assert_called_once_with("stdout", "", "/path.backup.log", s3_params)

@patch_network_get(private_address="1.1.1.1")
@patch("mysql_vm_helpers.MySQL.execute_backup_commands", return_value=("stdout", "stderr"))
@patch("charms.mysql.v0.backups.MySQLBackups._upload_logs_to_s3")
def test_backup_failure(
Expand Down Expand Up @@ -486,7 +476,6 @@ def test_backup_failure(
"", "failure backup", "/path.backup.log", s3_params
)

@patch_network_get(private_address="1.1.1.1")
@patch("mysql_vm_helpers.MySQL.delete_temp_backup_directory")
@patch("mysql_vm_helpers.MySQL.set_instance_offline_mode")
@patch("mysql_vm_helpers.MySQL.set_instance_option")
Expand All @@ -501,7 +490,6 @@ def test_post_backup(
self.assertTrue(success)
self.assertIsNone(error_message)

@patch_network_get(private_address="1.1.1.1")
@patch("mysql_vm_helpers.MySQL.delete_temp_backup_directory")
@patch("mysql_vm_helpers.MySQL.set_instance_offline_mode")
@patch("mysql_vm_helpers.MySQL.set_instance_option")
Expand Down Expand Up @@ -535,7 +523,6 @@ def test_post_backup_failure(
self.assertFalse(success)
self.assertEqual(error_message, "Error deleting temp backup directory")

@patch_network_get(private_address="1.1.1.1")
@patch("mysql_vm_helpers.MySQL.is_server_connectable", return_value=True)
@patch("charm.MySQLOperatorCharm.is_unit_busy", return_value=False)
def test_pre_restore_checks(
Expand All @@ -548,7 +535,6 @@ def test_pre_restore_checks(

self.assertTrue(self.mysql_backups._pre_restore_checks(event))

@patch_network_get(private_address="1.1.1.1")
@patch("mysql_vm_helpers.MySQL.is_server_connectable", return_value=True)
@patch("charm.MySQLOperatorCharm.is_unit_busy", return_value=False)
@patch("python_hosts.Hosts.write")
Expand Down Expand Up @@ -593,7 +579,6 @@ def test_pre_restore_checks_failure(

self.assertFalse(self.mysql_backups._pre_restore_checks(event))

@patch_network_get(private_address="1.1.1.1")
@patch("charm.MySQLOperatorCharm._on_update_status")
@patch("charms.mysql.v0.backups.MySQLBackups._pre_restore_checks", return_value=True)
@patch(
Expand Down Expand Up @@ -637,7 +622,6 @@ def test_on_restore(
self.assertEqual(event.set_results.call_count, 1)
self.assertEqual(event.fail.call_count, 0)

@patch_network_get(private_address="1.1.1.1")
@patch("charms.mysql.v0.backups.MySQLBackups._pre_restore_checks", return_value=True)
@patch(
"charms.mysql.v0.backups.MySQLBackups._retrieve_s3_parameters",
Expand Down Expand Up @@ -750,7 +734,6 @@ def test_on_restore_failure(
event.set_results.assert_not_called()
event.fail.assert_not_called()

@patch_network_get(private_address="1.1.1.1")
@patch("mysql_vm_helpers.MySQL.set_instance_offline_mode")
@patch("mysql_vm_helpers.MySQL.is_mysqld_running", return_value=True)
@patch("mysql_vm_helpers.MySQL.kill_client_sessions")
Expand All @@ -768,7 +751,6 @@ def test_pre_restore(
_kill_client_sessions.assert_called_once()
_set_instance_offline_mode.assert_called_once()

@patch_network_get(private_address="1.1.1.1")
@patch("mysql_vm_helpers.MySQL.set_instance_offline_mode")
@patch("mysql_vm_helpers.MySQL.is_mysqld_running", return_value=True)
@patch("mysql_vm_helpers.MySQL.kill_client_sessions")
Expand All @@ -784,7 +766,6 @@ def test_pre_restore_failure(
self.assertFalse(success)
self.assertEqual(error, "Failed to stop mysqld")

@patch_network_get(private_address="1.1.1.1")
@patch(
"mysql_vm_helpers.MySQL.retrieve_backup_with_xbcloud",
return_value=("", "", "test/backup/location"),
Expand Down Expand Up @@ -813,7 +794,6 @@ def test_restore(
self.assertTrue(recoverable)
self.assertEqual(error, "")

@patch_network_get(private_address="1.1.1.1")
@patch(
"mysql_vm_helpers.MySQL.retrieve_backup_with_xbcloud",
return_value=("", "", "test/backup/location"),
Expand Down Expand Up @@ -869,7 +849,6 @@ def test_restore_failure(
self.assertTrue(recoverable)
self.assertEqual(error, "Failed to retrieve backup test-backup-id")

@patch_network_get(private_address="1.1.1.1")
@patch("mysql_vm_helpers.MySQL.start_mysqld")
@patch("mysql_vm_helpers.MySQL.delete_temp_restore_directory")
@patch("mysql_vm_helpers.MySQL.delete_temp_backup_directory")
Expand All @@ -880,7 +859,6 @@ def test_clean_data_dir_and_start_mysqld(self, ___, __, _):
self.assertTrue(success)
self.assertEqual(error, "")

@patch_network_get(private_address="1.1.1.1")
@patch("mysql_vm_helpers.MySQL.start_mysqld")
@patch("mysql_vm_helpers.MySQL.delete_temp_restore_directory")
@patch("mysql_vm_helpers.MySQL.delete_temp_backup_directory")
Expand Down Expand Up @@ -909,7 +887,6 @@ def test_clean_data_dir_and_start_mysqld_failure(
self.assertFalse(success)
self.assertEqual(error, "Failed to delete the temp restore directory")

@patch_network_get(private_address="1.1.1.1")
@patch(
"charms.mysql.v0.backups.MySQLBackups._clean_data_dir_and_start_mysqld",
return_value=(True, None),
Expand Down Expand Up @@ -944,7 +921,6 @@ def test_post_restore(
_initialize_juju_units_operations_table.assert_called_once()
_rescan_cluster.assert_called_once()

@patch_network_get(private_address="1.1.1.1")
@patch(
"charms.mysql.v0.backups.MySQLBackups._clean_data_dir_and_start_mysqld",
return_value=(True, None),
Expand Down
7 changes: 0 additions & 7 deletions tests/unit/test_charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
MySQLResetRootPasswordAndStartMySQLDError,
)

from .helpers import patch_network_get


class TestCharm(unittest.TestCase):
def setUp(self):
Expand All @@ -39,7 +37,6 @@ def setUp(self):
self.harness.add_relation_unit(self.peer_relation_id, "mysql/1")
self.harness.add_relation("restart", "restart")

@patch_network_get(private_address="1.1.1.1")
@patch("upgrade.MySQLVMUpgrade.cluster_state", return_value="idle")
@patch("socket.getfqdn", return_value="test-hostname")
@patch("socket.gethostbyname", return_value="")
Expand Down Expand Up @@ -130,7 +127,6 @@ def test_on_leader_elected_sets_mysql_passwords_secret(self):
for key in expected_peer_relation_databag_keys:
self.assertTrue(self.harness.charm.get_secret("app", key).isalnum())

@patch_network_get(private_address="1.1.1.1")
def test_on_leader_elected_sets_config_cluster_name_in_peer_databag(self):
# ensure that the peer relation databag is empty
peer_relation_databag = self.harness.get_relation_data(
Expand All @@ -149,7 +145,6 @@ def test_on_leader_elected_sets_config_cluster_name_in_peer_databag(self):

self.assertEqual(peer_relation_databag["cluster-name"], "test-cluster")

@patch_network_get(private_address="1.1.1.1")
def test_on_config_changed_sets_random_cluster_name_in_peer_databag(self):
# ensure that the peer relation databag is empty
peer_relation_databag = self.harness.get_relation_data(
Expand Down Expand Up @@ -194,7 +189,6 @@ def test_on_start(
self.assertEqual(self.charm.unit_peer_data["member-role"], "secondary")
self.assertEqual(self.charm.unit_peer_data["member-state"], "waiting")

@patch_network_get(private_address="1.1.1.1")
@patch("charm.MySQLOperatorCharm._can_start", return_value=True)
@patch("charm.MySQLOperatorCharm.create_cluster")
@patch("charm.MySQLOperatorCharm.workload_initialise")
Expand Down Expand Up @@ -250,7 +244,6 @@ def test_on_start_exceptions(
self.charm.on.start.emit()
self.assertTrue(isinstance(self.harness.model.unit.status, BlockedStatus))

@patch_network_get(private_address="1.1.1.1")
@patch(
"charm.MySQLOperatorCharm.cluster_initialized",
new_callable=PropertyMock(return_value=True),
Expand Down
8 changes: 0 additions & 8 deletions tests/unit/test_charm_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
from ops.testing import Harness
from parameterized import parameterized

from .helpers import patch_network_get


class TestCharmBase(unittest.TestCase):
@patch.multiple(MySQLCharmBase, __abstractmethods__=set())
Expand All @@ -27,7 +25,6 @@ def setUp(self):
self.peer_relation_id = self.harness.add_relation("database-peers", "mysql")
self.harness.add_relation_unit(self.peer_relation_id, "mysql/1")

@patch_network_get(private_address="1.1.1.1")
def test_get_secret_databag(self):
self.harness.set_leader()

Expand All @@ -46,7 +43,6 @@ def test_get_secret_databag(self):
assert self.charm.get_secret("unit", "password") == "test-password"

@pytest.mark.usefixtures("without_juju_secrets")
@patch_network_get(private_address="1.1.1.1")
@patch("charm.MySQLOperatorCharm._on_leader_elected")
def test_set_secret_databag(self, _):
self.harness.set_leader()
Expand All @@ -72,7 +68,6 @@ def test_set_secret_databag(self, _):
)

@parameterized.expand([("app"), ("unit")])
@patch_network_get(private_address="1.1.1.1")
@pytest.mark.usefixtures("with_juju_secrets")
def test_set_secret(self, scope):
self.harness.set_leader()
Expand All @@ -93,7 +88,6 @@ def test_set_secret(self, scope):
self.charm.set_secret("not-a-scope", "password", "test") # type: ignore

@parameterized.expand([("app", True), ("unit", True), ("unit", False)])
@patch_network_get(private_address="1.1.1.1")
@pytest.mark.usefixtures("with_juju_secrets")
def test_set_reset_new_secret(self, scope, is_leader):
"""NOTE: currently ops.testing seems to allow for non-leader to set secrets too!"""
Expand All @@ -112,7 +106,6 @@ def test_set_reset_new_secret(self, scope, is_leader):
assert self.harness.charm.get_secret(scope, "new-secret2") == "blablabla"

@parameterized.expand([("app", True), ("unit", True), ("unit", False)])
@patch_network_get(private_address="1.1.1.1")
@pytest.mark.usefixtures("with_juju_secrets")
def test_invalid_secret(self, scope, is_leader):
# App has to be leader, unit can be either
Expand All @@ -129,7 +122,6 @@ def test_invalid_secret(self, scope, is_leader):
("unit", True, "key"),
("unit", False, "key"),
])
@patch_network_get(private_address="1.1.1.1")
@pytest.mark.usefixtures("with_juju_secrets")
def test_migration_from_databag(self, scope, is_leader, password_key):
"""Check if we're moving on to use secrets when live upgrade from databag to Secrets."""
Expand Down
3 changes: 0 additions & 3 deletions tests/unit/test_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
from charm import MySQLOperatorCharm
from constants import DB_RELATION_NAME

from .helpers import patch_network_get


class TestDatabase(unittest.TestCase):
def setUp(self):
Expand All @@ -23,7 +21,6 @@ def setUp(self):
self.harness.add_relation_unit(self.database_relation_id, "app/0")
self.charm = self.harness.charm

@patch_network_get(private_address="1.1.1.1")
@patch("charm.MySQLOperatorCharm.unit_initialized", new_callable=PropertyMock)
@patch("charm.MySQLOperatorCharm.cluster_initialized", new_callable=PropertyMock)
@patch("mysql_vm_helpers.MySQL.get_mysql_version", return_value="8.0.29-0ubuntu0.20.04.3")
Expand Down
4 changes: 0 additions & 4 deletions tests/unit/test_db_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@

from charm import MySQLOperatorCharm

from .helpers import patch_network_get


@patch("charms.rolling_ops.v0.rollingops.RollingOpsManager._on_process_locks")
class TestDBRouter(unittest.TestCase):
Expand All @@ -29,7 +27,6 @@ def setUp(self):
self.harness.add_relation_unit(self.db_router_relation_id, "app/0")
self.charm = self.harness.charm

@patch_network_get(private_address="1.1.1.1")
@patch("relations.db_router.DBRouterRelation._on_leader_elected")
@patch("charm.MySQLOperatorCharm.unit_initialized", return_value=True)
@patch("relations.db_router.generate_random_password", return_value="super_secure_password")
Expand Down Expand Up @@ -120,7 +117,6 @@ def test_db_router_relation_changed(
},
)

@patch_network_get(private_address="1.1.1.1")
@patch("relations.db_router.DBRouterRelation._on_leader_elected")
@patch("charm.MySQLOperatorCharm.unit_initialized", return_value=True)
@patch("relations.db_router.generate_random_password", return_value="super_secure_password")
Expand Down
Loading
Loading