Skip to content

Commit 9ee788b

Browse files
Move new data-replication tests to old files
1 parent 99141ed commit 9ee788b

File tree

22 files changed

+645
-1397
lines changed

22 files changed

+645
-1397
lines changed

tests/integration/helpers.py

Lines changed: 0 additions & 137 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import secrets
88
import string
99
import subprocess
10-
import tempfile
1110

1211
import juju.unit
1312
import yaml
@@ -830,142 +829,6 @@ async def get_cluster_status(unit: Unit, cluster_set: bool | None = False) -> di
830829
return result.get("status", {})
831830

832831

833-
async def delete_file_or_directory_in_unit(ops_test: OpsTest, unit_name: str, path: str) -> bool:
834-
"""Delete a file in the provided unit.
835-
836-
Args:
837-
ops_test: The ops test framework
838-
unit_name: The name unit on which to delete the file from
839-
path: The path of file or directory to delete
840-
841-
Returns:
842-
boolean indicating success
843-
"""
844-
if path.strip() in ["/", "."]:
845-
return
846-
847-
try:
848-
return_code, _, _ = await ops_test.juju(
849-
"ssh", unit_name, "sudo", "find", path, "-maxdepth", "1", "-delete"
850-
)
851-
852-
return return_code == 0
853-
except Exception:
854-
return False
855-
856-
857-
async def write_content_to_file_in_unit(
858-
ops_test: OpsTest, unit: Unit, path: str, content: str
859-
) -> None:
860-
"""Write content to the file in the provided unit.
861-
862-
Args:
863-
ops_test: The ops test framework
864-
unit: THe unit in which to write to file in
865-
path: The path at which to write the content to
866-
content: The content to write to the file.
867-
"""
868-
with tempfile.NamedTemporaryFile(mode="w") as temp_file:
869-
temp_file.write(content)
870-
temp_file.flush()
871-
872-
await unit.scp_to(temp_file.name, "/tmp/file")
873-
874-
return_code, _, _ = await ops_test.juju("ssh", unit.name, "sudo", "mv", "/tmp/file", path)
875-
assert return_code == 0
876-
877-
return_code, _, _ = await ops_test.juju(
878-
"ssh", unit.name, "sudo", "chown", "snap_daemon:root", path
879-
)
880-
assert return_code == 0
881-
882-
883-
async def read_contents_from_file_in_unit(ops_test: OpsTest, unit: Unit, path: str) -> str:
884-
"""Read contents from file in the provided unit.
885-
886-
Args:
887-
ops_test: The ops test framework
888-
unit: The unit in which to read file from
889-
path: The path from which to read content from
890-
891-
Returns:
892-
the contents of the file
893-
"""
894-
return_code, _, _ = await ops_test.juju("ssh", unit.name, "sudo", "cp", path, "/tmp/file")
895-
assert return_code == 0
896-
897-
return_code, _, _ = await ops_test.juju(
898-
"ssh", unit.name, "sudo", "chown", "ubuntu:ubuntu", "/tmp/file"
899-
)
900-
assert return_code == 0
901-
902-
with tempfile.NamedTemporaryFile(mode="r+") as temp_file:
903-
await unit.scp_from("/tmp/file", temp_file.name)
904-
905-
temp_file.seek(0)
906-
907-
contents = ""
908-
for line in temp_file:
909-
contents += line
910-
contents += "\n"
911-
912-
return_code, _, _ = await ops_test.juju("ssh", unit.name, "sudo", "rm", "/tmp/file")
913-
assert return_code == 0
914-
915-
return contents
916-
917-
918-
async def ls_la_in_unit(ops_test: OpsTest, unit_name: str, directory: str) -> list[str]:
919-
"""Returns the output of ls -la in unit.
920-
921-
Args:
922-
ops_test: The ops test framework
923-
unit_name: The name of unit in which to run ls -la
924-
path: The path from which to run ls -la
925-
926-
Returns:
927-
a list of files returned by ls -la
928-
"""
929-
return_code, output, _ = await ops_test.juju("ssh", unit_name, "sudo", "ls", "-la", directory)
930-
assert return_code == 0
931-
932-
ls_output = output.split("\n")[1:]
933-
934-
return [
935-
line.strip("\r")
936-
for line in ls_output
937-
if len(line.strip()) > 0 and line.split()[-1] not in [".", ".."]
938-
]
939-
940-
941-
async def stop_running_flush_mysql_cronjobs(ops_test: OpsTest, unit_name: str) -> None:
942-
"""Stop running any logrotate jobs that may have been triggered by cron.
943-
944-
Args:
945-
ops_test: The ops test object passed into every test case
946-
unit_name: The name of the unit to be tested
947-
"""
948-
# send TERM signal to mysql daemon, which trigger shutdown process
949-
await ops_test.juju(
950-
"ssh",
951-
unit_name,
952-
"sudo",
953-
"pkill",
954-
"-15",
955-
"-f",
956-
"logrotate -f /etc/logrotate.d/flush_mysql_logs",
957-
)
958-
959-
# hold execution until process is stopped
960-
try:
961-
for attempt in Retrying(stop=stop_after_attempt(45), wait=wait_fixed(2)):
962-
with attempt:
963-
if await get_process_pid(ops_test, unit_name, "logrotate"):
964-
raise Exception
965-
except RetryError as e:
966-
raise Exception("Failed to stop the flush_mysql_logs logrotate process.") from e
967-
968-
969832
def get_unit_by_index(app_name: str, units: list, index: int):
970833
"""Get unit by index.
971834
Lines changed: 57 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,72 @@
11
# Copyright 2022 Canonical Ltd.
22
# See LICENSE file for licensing details.
33

4-
54
import logging
6-
from pathlib import Path
75

6+
import jubilant_backports
87
import pytest
9-
import yaml
10-
from pytest_operator.plugin import OpsTest
11-
12-
from .high_availability_helpers import (
13-
clean_up_database_and_table,
14-
ensure_all_units_continuous_writes_incrementing,
15-
get_application_name,
16-
insert_data_into_mysql_and_validate_replication,
8+
from jubilant_backports import Juju
9+
10+
from ..helpers import generate_random_string
11+
from .high_availability_helpers_new import (
12+
check_mysql_units_writes_increment,
13+
insert_mysql_test_data,
14+
remove_mysql_test_data,
15+
verify_mysql_test_data,
16+
wait_for_apps_status,
1717
)
1818

19-
logger = logging.getLogger(__name__)
19+
MYSQL_APP_NAME = "mysql"
20+
MYSQL_TEST_APP_NAME = "mysql-test-app"
21+
22+
MINUTE_SECS = 60
2023

21-
METADATA = yaml.safe_load(Path("./metadata.yaml").read_text())
22-
APP_NAME = METADATA["name"]
23-
ANOTHER_APP_NAME = f"second{APP_NAME}"
24-
TIMEOUT = 17 * 60
24+
logging.getLogger("jubilant.wait").setLevel(logging.WARNING)
2525

2626

2727
@pytest.mark.abort_on_fail
28-
async def test_consistent_data_replication_across_cluster(
29-
ops_test: OpsTest, highly_available_cluster
30-
) -> None:
31-
"""Confirm that data is replicated from the primary node to all the replicas."""
32-
mysql_application_name = get_application_name(ops_test, "mysql")
28+
def test_deploy_highly_available_cluster(juju: Juju, charm: str) -> None:
29+
"""Simple test to ensure that the MySQL and application charms get deployed."""
30+
logging.info("Deploying MySQL cluster")
31+
juju.deploy(
32+
charm=charm,
33+
app=MYSQL_APP_NAME,
34+
base="ubuntu@22.04",
35+
config={"profile": "testing"},
36+
num_units=3,
37+
)
38+
juju.deploy(
39+
charm=MYSQL_TEST_APP_NAME,
40+
app=MYSQL_TEST_APP_NAME,
41+
base="ubuntu@22.04",
42+
channel="latest/edge",
43+
config={"sleep_interval": 500},
44+
num_units=1,
45+
)
46+
47+
juju.integrate(
48+
f"{MYSQL_APP_NAME}:database",
49+
f"{MYSQL_TEST_APP_NAME}:database",
50+
)
3351

34-
# assert that there are 3 units in the mysql cluster
35-
assert len(ops_test.model.applications[mysql_application_name].units) == 3
52+
logging.info("Wait for applications to become active")
53+
juju.wait(
54+
ready=wait_for_apps_status(
55+
jubilant_backports.all_active, MYSQL_APP_NAME, MYSQL_TEST_APP_NAME
56+
),
57+
error=jubilant_backports.any_blocked,
58+
timeout=20 * MINUTE_SECS,
59+
)
60+
61+
62+
@pytest.mark.abort_on_fail
63+
async def test_consistent_data_replication_across_cluster(juju: Juju) -> None:
64+
"""Confirm that data is replicated from the primary node to all the replicas."""
65+
table_name = "data"
66+
table_value = generate_random_string(255)
3667

37-
database_name, table_name = "test-check-consistency", "data"
38-
await insert_data_into_mysql_and_validate_replication(ops_test, database_name, table_name)
39-
await clean_up_database_and_table(ops_test, database_name, table_name)
68+
await insert_mysql_test_data(juju, MYSQL_APP_NAME, table_name, table_value)
69+
await verify_mysql_test_data(juju, MYSQL_APP_NAME, table_name, table_value)
70+
await remove_mysql_test_data(juju, MYSQL_APP_NAME, table_name)
4071

41-
await ensure_all_units_continuous_writes_incrementing(ops_test)
72+
await check_mysql_units_writes_increment(juju, MYSQL_APP_NAME)

tests/integration/high_availability/test_replication_data_consistency_new.py

Lines changed: 0 additions & 72 deletions
This file was deleted.

0 commit comments

Comments
 (0)