Skip to content

Commit 5d3524c

Browse files
[MISC] Data-replication Jubilant test fixes (#697)
1 parent 8551dfc commit 5d3524c

File tree

2 files changed

+15
-93
lines changed

2 files changed

+15
-93
lines changed

tests/integration/high_availability/high_availability_helpers_new.py

Lines changed: 4 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@
33
# See LICENSE file for licensing details.
44

55
import json
6-
import os
76
import subprocess
87
from collections.abc import Callable
9-
from pathlib import Path
108

119
import jubilant_backports
1210
from jubilant_backports import Juju
@@ -28,18 +26,6 @@
2826
JujuAppsStatusFn = Callable[[Status, str], bool]
2927

3028

31-
def _get_juju_keys() -> tuple[str, str]:
32-
"""Get Juju public and private keys."""
33-
config_dir = Path("~/.local/share/juju")
34-
if juju_data := os.getenv("JUJU_DATA"):
35-
config_dir = Path(juju_data)
36-
37-
return (
38-
str(config_dir.expanduser().resolve() / "ssh" / "juju_id_rsa.pub"),
39-
str(config_dir.expanduser().resolve() / "ssh" / "juju_id_rsa"),
40-
)
41-
42-
4329
def check_mysql_instances_online(juju: Juju, app_name: str) -> bool:
4430
"""Checks whether all MySQL cluster instances are online.
4531
@@ -121,20 +107,16 @@ def get_app_units(juju: Juju, app_name: str) -> dict[str, UnitStatus]:
121107

122108
def scale_app_units(juju: Juju, app_name: str, num_units: int) -> None:
123109
"""Scale a given application to a number of units."""
124-
app_units = get_app_units(juju, app_name)
125-
app_units_diff = len(app_units) - num_units
110+
app_units = list(get_app_units(juju, app_name))
111+
app_units_diff = num_units - len(app_units)
126112

127-
scale_func = None
128113
if app_units_diff > 0:
129-
scale_func = juju.remove_unit
114+
juju.add_unit(app_name, num_units=app_units_diff)
130115
if app_units_diff < 0:
131-
scale_func = juju.add_unit
116+
juju.remove_unit(*app_units[app_units_diff:])
132117
if app_units_diff == 0:
133118
return
134119

135-
for _ in range(abs(app_units_diff)):
136-
scale_func(app_name, num_units=1)
137-
138120
juju.wait(
139121
ready=lambda status: len(status.apps[app_name].units) == num_units,
140122
timeout=20 * MINUTE_SECS,
@@ -206,54 +188,6 @@ def get_unit_status_log(juju: Juju, unit_name: str, log_lines: int = 0) -> list[
206188
return json.loads(output)
207189

208190

209-
# TODO:
210-
# Rely on Jubilant scp command once they make it easier
211-
# for package users to deal with temporal directories
212-
# https://github.com/canonical/jubilant/issues/201
213-
def scp_unit_file_from(
214-
juju: Juju, app_name: str, unit_name: str, source_path: str, target_path: str
215-
) -> None:
216-
"""Copy a file from a given unit."""
217-
juju_keys = _get_juju_keys()
218-
unit_address = get_unit_ip(juju, app_name, unit_name)
219-
unit_username = "ubuntu"
220-
221-
subprocess.check_output([
222-
"scp",
223-
"-B",
224-
"-o",
225-
"StrictHostKeyChecking=no",
226-
"-i",
227-
juju_keys[1],
228-
f"{unit_username}@{unit_address}:{source_path}",
229-
f"{target_path}",
230-
])
231-
232-
233-
# TODO:
234-
# Rely on Jubilant scp command once they make it easier
235-
# for package users to deal with temporal directories
236-
# https://github.com/canonical/jubilant/issues/201
237-
def scp_unit_file_into(
238-
juju: Juju, app_name: str, unit_name: str, source_path: str, target_path: str
239-
) -> None:
240-
"""Copy a file into a given unit."""
241-
juju_keys = _get_juju_keys()
242-
unit_address = get_unit_ip(juju, app_name, unit_name)
243-
unit_username = "ubuntu"
244-
245-
subprocess.check_output([
246-
"scp",
247-
"-B",
248-
"-o",
249-
"StrictHostKeyChecking=no",
250-
"-i",
251-
juju_keys[1],
252-
f"{source_path}",
253-
f"{unit_username}@{unit_address}:{target_path}",
254-
])
255-
256-
257191
def get_relation_data(juju: Juju, app_name: str, rel_name: str) -> list[dict]:
258192
"""Returns a list that contains the relation-data.
259193

tests/integration/high_availability/test_replication_logs_rotation.py

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import logging
55
import tempfile
6+
from pathlib import Path
67

78
import jubilant_backports
89
import pytest
@@ -18,8 +19,6 @@
1819
from .high_availability_helpers_new import (
1920
get_app_leader,
2021
get_unit_process_id,
21-
scp_unit_file_from,
22-
scp_unit_file_into,
2322
wait_for_apps_status,
2423
)
2524

@@ -91,7 +90,6 @@ def test_log_rotation(juju: Juju) -> None:
9190
logging.info("Writing some data to the text log files")
9291
write_unit_file(
9392
juju=juju,
94-
app_name=MYSQL_APP_NAME,
9593
unit_name=mysql_app_leader,
9694
file_path=f"{mysql_logs_path}/{log_type}.log",
9795
file_data=f"{log_type} content",
@@ -118,7 +116,6 @@ def test_log_rotation(juju: Juju) -> None:
118116
for log_type in log_types:
119117
active_log_file_data = read_unit_file(
120118
juju=juju,
121-
app_name=MYSQL_APP_NAME,
122119
unit_name=mysql_app_leader,
123120
file_path=f"{mysql_logs_path}/{log_type}.log",
124121
)
@@ -132,7 +129,6 @@ def test_log_rotation(juju: Juju) -> None:
132129
archive_log_file_name = archive_log_files_listed[0].split()[-1]
133130
archive_log_file_data = read_unit_file(
134131
juju=juju,
135-
app_name=MYSQL_APP_NAME,
136132
unit_name=mysql_app_leader,
137133
file_path=f"{archive_log_dir}/{archive_log_file_name}",
138134
)
@@ -174,12 +170,11 @@ def list_unit_files(juju: Juju, unit_name: str, file_path: str) -> list[str]:
174170
]
175171

176172

177-
def read_unit_file(juju: Juju, app_name: str, unit_name: str, file_path: str) -> str:
173+
def read_unit_file(juju: Juju, unit_name: str, file_path: str) -> str:
178174
"""Read contents from file in the provided unit.
179175
180176
Args:
181177
juju: The Juju instance
182-
app_name: The name of the app
183178
unit_name: The name of the unit to read the file from
184179
file_path: The path of the unit to read the file
185180
"""
@@ -188,42 +183,35 @@ def read_unit_file(juju: Juju, app_name: str, unit_name: str, file_path: str) ->
188183
juju.exec(f"sudo cp {file_path} {temp_path}", unit=unit_name)
189184
juju.exec(f"sudo chown ubuntu:ubuntu {temp_path}", unit=unit_name)
190185

191-
with tempfile.NamedTemporaryFile(mode="r+") as temp_file:
192-
scp_unit_file_from(
193-
juju=juju,
194-
app_name=app_name,
195-
unit_name=unit_name,
196-
source_path=temp_path,
197-
target_path=temp_file.name,
186+
with tempfile.NamedTemporaryFile(mode="r+", dir=Path.home()) as temp_file:
187+
juju.scp(
188+
f"{unit_name}:{temp_path}",
189+
f"{temp_file.name}",
198190
)
199191
contents = temp_file.read()
200192

201193
juju.exec(f"sudo rm {temp_path}", unit=unit_name)
202194
return contents
203195

204196

205-
def write_unit_file(juju: Juju, app_name: str, unit_name: str, file_path: str, file_data: str):
197+
def write_unit_file(juju: Juju, unit_name: str, file_path: str, file_data: str):
206198
"""Write content to the file in the provided unit.
207199
208200
Args:
209201
juju: The Juju instance
210-
app_name: The name of the app
211202
unit_name: The name of the unit to write the file into
212203
file_path: The path of the unit to write the file
213204
file_data: The data to write to the file.
214205
"""
215206
temp_path = "/tmp/file"
216207

217-
with tempfile.NamedTemporaryFile(mode="w") as temp_file:
208+
with tempfile.NamedTemporaryFile(mode="w", dir=Path.home()) as temp_file:
218209
temp_file.write(file_data)
219210
temp_file.flush()
220211

221-
scp_unit_file_into(
222-
juju=juju,
223-
app_name=app_name,
224-
unit_name=unit_name,
225-
source_path=temp_file.name,
226-
target_path=temp_path,
212+
juju.scp(
213+
f"{temp_file.name}",
214+
f"{unit_name}:{temp_path}",
227215
)
228216

229217
juju.exec(f"sudo mv {temp_path} {file_path}", unit=unit_name)

0 commit comments

Comments
 (0)