Skip to content

Commit c55cbff

Browse files
committed
Fix test_move_results
1 parent e616531 commit c55cbff

File tree

2 files changed

+75
-38
lines changed

2 files changed

+75
-38
lines changed

lib/pbench/cli/agent/commands/results/__init__.py

Lines changed: 31 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import os
2-
import sys
32
import requests
3+
import shutil
4+
import sys
45
import tempfile
56

6-
from pbench.common.logger import get_pbench_logger
77
from pbench.agent import PbenchAgentConfig
88
from pbench.agent.results import MakeResultTb, CopyResultTb
9+
from pbench.common.logger import get_pbench_logger
910

1011

1112
def move_results(ctx, _user, _prefix, _show_server):
@@ -47,9 +48,6 @@ def move_results(ctx, _user, _prefix, _show_server):
4748
)
4849
sys.exit(1)
4950

50-
runs_copied = 0
51-
failures = 0
52-
5351
try:
5452
temp_dir = tempfile.mkdtemp(
5553
dir=config.pbench_tmp, prefix="pbench-move-results."
@@ -58,42 +56,42 @@ def move_results(ctx, _user, _prefix, _show_server):
5856
logger.error("Failed to create temporary directory")
5957
sys.exit(1)
6058

61-
dirs = [
62-
_dir
63-
for _dir in next(os.walk(config.pbench_run))[1]
64-
if not _dir.startswith("tools-") and not _dir.startswith("tmp")
65-
]
59+
runs_copied = 0
60+
failures = 0
6661

67-
for _dir in dirs:
68-
result_dir = config.pbench_run / _dir
62+
for dirent in config.pbench_run.iterdir():
63+
if not dirent.is_dir():
64+
continue
65+
if dirent.name.startswith("tools-") or dirent.name == "tmp":
66+
continue
67+
result_dir = dirent
6968
mrt = MakeResultTb(result_dir, temp_dir, _user, _prefix, config, logger)
7069
result_tb_name = mrt.make_result_tb()
71-
if result_tb_name:
72-
crt = CopyResultTb(controller, result_tb_name, config, logger)
73-
copy_result = crt.copy_result_tb()
74-
try:
75-
os.remove(result_tb_name)
76-
os.remove(f"{result_tb_name}.md5")
77-
except OSError:
78-
logger.error("rm failed to remove %s and its .md5 file", result_tb_name)
79-
sys.exit(1)
80-
if not copy_result:
81-
failures += 1
82-
continue
70+
assert (
71+
result_tb_name
72+
), "Logic bomb! make_result_tb() always returns a tar ball name"
73+
crt = CopyResultTb(controller, result_tb_name, config, logger)
74+
crt.copy_result_tb()
75+
try:
76+
# We always remove the constructed tar ball, regardless of success
77+
# or failure, since we keep the result directory below on failure.
78+
os.remove(result_tb_name)
79+
os.remove(f"{result_tb_name}.md5")
80+
except OSError:
81+
logger.error("rm failed to remove %s and its .md5 file", result_tb_name)
82+
sys.exit(1)
8383

84-
try:
85-
os.remove(result_dir)
86-
except OSError:
87-
logger.error(
88-
"rm failed to remove the %s directory hierarchy", result_dir
89-
)
90-
sys.exit(1)
84+
try:
85+
shutil.rmtree(result_dir)
86+
except OSError:
87+
logger.error("rm failed to remove the %s directory hierarchy", result_dir)
88+
sys.exit(1)
9189

92-
runs_copied += 1
90+
runs_copied += 1
9391

9492
if runs_copied + failures > 0:
9593
logger.debug(
9694
"successfully moved %s runs, encountered %s failures", runs_copied, failures
9795
)
9896

99-
return failures
97+
return runs_copied, failures
Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,43 @@
1-
import os
21
import datetime
2+
import logging
3+
import os
34
import responses
45

6+
from pathlib import Path
57
from pbench.cli.agent.commands.results import move_results
68
from pbench.test.unit.agent.task.common import MockDatetime
79

810

11+
mdlog_tmpl = """[pbench]
12+
name = {}
13+
script = pbench-user-benchmark
14+
config = test-move-results
15+
date = YYYY-MM-DDTHH:MM:SS
16+
rpm-version = 0.00.0-1
17+
iterations = 1, 1
18+
19+
[tools]
20+
hosts = agent.example.com
21+
group = default
22+
23+
[tools/agent.example.com]
24+
sar = --interval=3
25+
26+
[run]
27+
controller = agent.example.com
28+
start_run = YYYY-MM-DDTHH:MM:SS.000000000
29+
end_run = YYYY-MM-DDTHH:MM:SS.000000000
30+
31+
[iterations/1]
32+
iteration_name = 1
33+
user_script = sleep
34+
"""
35+
36+
937
class TestMoveResults:
1038
@staticmethod
1139
@responses.activate
12-
def test_move_results(monkeypatch):
40+
def test_move_results(monkeypatch, caplog, pytestconfig):
1341
monkeypatch.setenv("full_hostname", "localhost")
1442
monkeypatch.setattr(datetime, "datetime", MockDatetime)
1543

@@ -21,14 +49,25 @@ def test_move_results(monkeypatch):
2149
)
2250
responses.add(
2351
responses.PUT,
24-
"http://pbench.example.com/api/v1/upload/ctrl/controller",
52+
"http://pbench.example.com/api/v1/upload/ctrl/localhost",
2553
status=200,
2654
)
2755

2856
ctx = {"args": {"config": os.environ["_PBENCH_AGENT_CONFIG"]}}
2957

58+
TMP = pytestconfig.cache.get("TMP", None)
59+
pbrun = Path(TMP) / "var" / "lib" / "pbench-agent"
60+
run_name = "pbench-user-benchmark_test-move-results_YYYY.MM.DDTHH.MM.SS"
61+
run_dir = pbrun / run_name
62+
run_dir.mkdir()
63+
mlog = run_dir / "metadata.log"
64+
mlog.write_text(mdlog_tmpl.format(run_name))
65+
66+
caplog.set_level(logging.DEBUG)
67+
3068
try:
31-
move_results(ctx, "pbench", "", True)
69+
runs_copied, failures = move_results(ctx, "pbench", "", True)
3270
except SystemExit:
3371
assert False
34-
assert True
72+
assert failures == 0, f"Unexpected failures, {failures}"
73+
assert runs_copied == 1, f"Unexpected runs_copied, {runs_copied}"

0 commit comments

Comments
 (0)