Skip to content

Commit 6be9758

Browse files
committed
Fix agent side test_move_results
While working on PR #1948, refactoring the common logging infrastructure, the tests in `test_move_results` were failing for odd reasons. Looking at them a bit more closely it became apparent the test was not actually invoking the API's PUT method. This commit creates a test that allows the API to be invoked, fixing the move results code to work correctly.
1 parent 4746adb commit 6be9758

File tree

2 files changed

+84
-38
lines changed

2 files changed

+84
-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: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,45 @@
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+
# Template for a mocked "metadata.log" file; the caller needs to
12+
# provide variables for the name, script, config, and date values.
13+
mdlog_tmpl = """[pbench]
14+
name = {name}
15+
script = {script}
16+
config = {config}
17+
date = {date}
18+
rpm-version = 0.00.0-1
19+
iterations = 1, 1
20+
21+
[tools]
22+
hosts = agent.example.com
23+
group = default
24+
25+
[tools/agent.example.com]
26+
sar = --interval=3
27+
28+
[run]
29+
controller = agent.example.com
30+
start_run = YYYY-MM-DDTHH:MM:SS.000000000
31+
end_run = YYYY-MM-DDTHH:MM:SS.000000000
32+
33+
[iterations/1]
34+
iteration_name = 1
35+
user_script = sleep
36+
"""
37+
38+
939
class TestMoveResults:
1040
@staticmethod
1141
@responses.activate
12-
def test_move_results(monkeypatch):
42+
def test_move_results(monkeypatch, caplog, pytestconfig):
1343
monkeypatch.setenv("_pbench_full_hostname", "localhost")
1444
monkeypatch.setattr(datetime, "datetime", MockDatetime)
1545

@@ -21,14 +51,32 @@ def test_move_results(monkeypatch):
2151
)
2252
responses.add(
2353
responses.PUT,
24-
"http://pbench.example.com/api/v1/upload/ctrl/controller",
54+
"http://pbench.example.com/api/v1/upload/ctrl/localhost",
2555
status=200,
2656
)
2757

2858
ctx = {"args": {"config": os.environ["_PBENCH_AGENT_CONFIG"]}}
2959

60+
# In order for a pbench tar ball to be moved/copied to a pbench-server
61+
# the run directory has to have one file in it, a "metadata.log" file.
62+
# We make a run directory and populate it with our test specific
63+
# information.
64+
TMP = pytestconfig.cache.get("TMP", None)
65+
pbrun = Path(TMP) / "var" / "lib" / "pbench-agent"
66+
script = "pbench-user-benchmark"
67+
config = "test-move-results"
68+
date = "YYYY.MM.DDTHH.MM.SS"
69+
name = "{script}_{config}_{date}"
70+
run_dir = pbrun / name
71+
run_dir.mkdir()
72+
mlog = run_dir / "metadata.log"
73+
mlog.write_text(mdlog_tmpl.format(**locals()))
74+
75+
caplog.set_level(logging.DEBUG)
76+
3077
try:
31-
move_results(ctx, "pbench", "", True)
78+
runs_copied, failures = move_results(ctx, "pbench", "", True)
3279
except SystemExit:
3380
assert False
34-
assert True
81+
assert failures == 0, f"Unexpected failures, {failures}"
82+
assert runs_copied == 1, f"Unexpected runs_copied, {runs_copied}"

0 commit comments

Comments
 (0)