Skip to content

Commit dd0c88e

Browse files
committed
Added first test, to be reused later
1 parent 09a7948 commit dd0c88e

5 files changed

Lines changed: 98 additions & 1 deletion

File tree

dev-requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ pre-commit
33
build
44
twine
55
mypy >= 1.1.1
6-
-r mypy-requirements.txt
6+
-r mypy-requirements.txt
7+
pytest

execution_process_metrics_collector/collector.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import psutil
2828
import re
2929
import socket
30+
import subprocess
3031
import time
3132
import sys
3233

@@ -248,6 +249,25 @@ def analyse_list_of_processes(
248249
return new_pid_pairs
249250

250251

252+
def execution_metrics_collector(
253+
cmdline: "Sequence[str]",
254+
reldatadir: "pathlib.Path",
255+
sleep_secs: "float" = 1,
256+
timestamp_format: "str" = "%Y-%m-%d %H:%M:%S",
257+
match_docker: "bool" = False,
258+
) -> "None":
259+
pop = subprocess.Popen(cmdline)
260+
process_metrics_collector(
261+
pop.pid,
262+
reldatadir,
263+
sleep_secs=sleep_secs,
264+
timestamp_format=timestamp_format,
265+
match_docker=match_docker,
266+
)
267+
# Just for completeness
268+
pop.wait()
269+
270+
251271
def process_metrics_collector(
252272
pid: "int",
253273
reldatadir: "pathlib.Path",

tests/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"""Tests for treecript (execution process metrics collector)"""

tests/core/__init__.py

Whitespace-only changes.

tests/core/test_collector.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
# SPDX-License-Identifier: GPL-3.0-or-later
5+
# execution-process-metrics-collector, a process tree metrics gatherer.
6+
# Copyright (C) 2025 Barcelona Supercomputing Center, José M. Fernández
7+
#
8+
# This program is free software: you can redistribute it and/or modify
9+
# it under the terms of the GNU General Public License as published by
10+
# the Free Software Foundation, either version 3 of the License, or
11+
# (at your option) any later version.
12+
#
13+
# This program is distributed in the hope that it will be useful, but
14+
# WITHOUT ANY WARRANTY; without even the implied warranty of
15+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16+
# General Public License for more details.
17+
#
18+
# You should have received a copy of the GNU General Public License
19+
# along with this program. If not, see <https://www.gnu.org/licenses/>.
20+
21+
import pytest
22+
import inspect
23+
import pathlib
24+
25+
from execution_process_metrics_collector.collector import execution_metrics_collector
26+
27+
from typing import (
28+
TYPE_CHECKING,
29+
)
30+
31+
if TYPE_CHECKING:
32+
from typing import (
33+
Optional,
34+
Sequence,
35+
)
36+
37+
38+
COLLECTOR_TESTBED = pytest.mark.parametrize(
39+
["command_line", "should_fail"],
40+
[
41+
(["/usr/bin/sleep", "3"], None),
42+
],
43+
)
44+
45+
46+
@COLLECTOR_TESTBED
47+
def test_collector(
48+
tmpdir: "str",
49+
command_line: "Sequence[str]",
50+
should_fail: "Optional[Sequence[str]]",
51+
) -> "None":
52+
try:
53+
execution_metrics_collector(
54+
command_line,
55+
pathlib.Path(tmpdir),
56+
match_docker=True,
57+
)
58+
except BaseException:
59+
current_frame = inspect.currentframe()
60+
if (
61+
should_fail is None
62+
or current_frame is None
63+
or current_frame.f_code.co_name not in should_fail
64+
): # type: ignore[union-attr]
65+
raise
66+
else:
67+
current_frame = inspect.currentframe()
68+
if (
69+
should_fail is not None
70+
and current_frame is not None
71+
and current_frame.f_code.co_name in should_fail
72+
): # type: ignore[union-attr]
73+
raise AssertionError(
74+
f"Method {current_frame.f_code.co_name} should have failed with command line {command_line}"
75+
) # type: ignore[union-attr]

0 commit comments

Comments
 (0)