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

DPE-5385 fix/database requested wait container #500

Merged
merged 2 commits into from
Sep 5, 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
5 changes: 5 additions & 0 deletions src/relations/mysql_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ def _on_database_requested(self, event: DatabaseRequestedEvent) -> None:
"""Handle the `database-requested` event."""
if not self.charm.unit.is_leader():
return
container = self.charm.unit.get_container(CONTAINER_NAME)
if not container.can_connect():
logger.debug("Container is not ready")
event.defer()
return
# check if cluster is ready and if not, defer
if not self.charm.cluster_initialized:
logger.debug("Waiting cluster to be initialized")
Expand Down
10 changes: 5 additions & 5 deletions tests/unit/test_charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,25 +208,25 @@ def test_pebble_ready_set_data(
mock_write_conf,
mock_conf,
mock_join,
_cluster_intialized,
_unit_intialized,
_cluster_initialized,
_unit_initialized,
):
mock_mysql.is_data_dir_initialised.return_value = False
mock_mysql.get_member_state.return_value = ("online", "primary")
self.harness.set_can_connect("mysql", True)
self.harness.set_leader()

mock_mysql.cluster_metadata_exists.return_value = False
_cluster_intialized.return_value = False
_unit_intialized.return_value = False
_cluster_initialized.return_value = False
_unit_initialized.return_value = False

# test on leader
self.harness.set_leader(is_leader=True)
self.harness.container_pebble_ready("mysql")
self.assertEqual(self.charm.unit_peer_data["member-state"], "online")
self.assertEqual(self.charm.unit_peer_data["member-role"], "primary")

_cluster_intialized.return_value = True
_cluster_initialized.return_value = True

# test on non leader
self.harness.set_leader(is_leader=False)
Expand Down
16 changes: 8 additions & 8 deletions tests/unit/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,36 +36,36 @@ def _check_valid_values(_harness, field: str, accepted_values: list, is_long_fie
assert _harness.charm.config[field] == value if not is_long_field else int(value)


def _check_invalid_values(_harness, field: str, erroneus_values: list) -> None:
def _check_invalid_values(_harness, field: str, erroneous_values: list) -> None:
"""Check the incorrectness of the passed values for a field."""
for value in erroneus_values:
for value in erroneous_values:
_harness.update_config({field: value})
with pytest.raises(ValueError):
_ = _harness.charm.config[field]


def test_profile_limit_values(harness) -> None:
"""Check that integer fields are parsed correctly."""
erroneus_values = [599, 10**7, -354343]
_check_invalid_values(harness, "profile-limit-memory", erroneus_values)
erroneous_values = [599, 10**7, -354343]
_check_invalid_values(harness, "profile-limit-memory", erroneous_values)

valid_values = [600, 1000, 35000]
_check_valid_values(harness, "profile-limit-memory", valid_values)


def test_profile_values(harness) -> None:
"""Test profile values."""
erroneus_values = ["prod", "Test", "foo", "bar"]
_check_invalid_values(harness, "profile", erroneus_values)
erroneous_values = ["prod", "Test", "foo", "bar"]
_check_invalid_values(harness, "profile", erroneous_values)

accepted_values = ["production", "testing"]
_check_valid_values(harness, "profile", accepted_values)


def test_cluster_name_values(harness) -> None:
"""Test cluster name values."""
erroneus_values = [64 * "a", "1-cluster", "cluster$"]
_check_invalid_values(harness, "cluster-name", erroneus_values)
erroneous_values = [64 * "a", "1-cluster", "cluster$"]
_check_invalid_values(harness, "cluster-name", erroneous_values)

accepted_values = ["c1", "cluster_name", "cluster.name", "Cluster-name", 63 * "c"]
_check_valid_values(harness, "cluster-name", accepted_values)
1 change: 1 addition & 0 deletions tests/unit/test_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ def test_database_requested(
):
# run start-up events to enable usage of the helper class
self.harness.set_leader(True)
self.harness.container_pebble_ready("mysql")
self.charm.on.config_changed.emit()

# confirm that the relation databag is empty
Expand Down
Loading