Skip to content

Commit 5413d50

Browse files
authored
System check providers running (#619)
* Tests run_until must be guard-claused with cleanup routine * System check for providers running * Simplified tinyproxy object creation * Generalized check_system for running metric providers by asking each one separately * Disabling the broken test_check_system * Restructured returncode checking in check_system
1 parent 32575a8 commit 5413d50

File tree

5 files changed

+44
-10
lines changed

5 files changed

+44
-10
lines changed

metric_providers/base.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,17 @@ def __init__(
4646

4747
# this is the default function that will be overridden in the children
4848
def check_system(self):
49-
pass
49+
cmd = ['pgrep', '-f', self._metric_provider_executable]
50+
result = subprocess.run(cmd,
51+
stdout=subprocess.PIPE,
52+
stderr=subprocess.PIPE,
53+
check=False, encoding='UTF-8')
54+
if result.returncode == 1:
55+
return True
56+
if result.returncode == 0:
57+
raise MetricProviderConfigurationError(f"Another instance of the {self._metric_name} metrics provider is already running on the system!\nPlease close it before running the Green Metrics Tool.")
58+
# implicit else
59+
raise subprocess.CalledProcessError(result.stderr, cmd)
5060

5161
# implemented as getter function and not direct access, so it can be overloaded
5262
# some child classes might not actually have _ps attribute set

metric_providers/cpu/frequency/sysfs/core/provider.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ def __init__(self, resolution):
1414
)
1515

1616
def check_system(self):
17+
super().check_system()
18+
1719
file_path = "/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq"
1820
if os.path.exists(file_path):
1921
try:

metric_providers/network/connections/proxy/container/provider.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,24 @@
1515

1616
class NetworkConnectionsProxyContainerProvider(BaseMetricProvider):
1717
def __init__(self, *, host_ip=None):
18+
19+
tinyproxy_path = subprocess.getoutput('which tinyproxy')
20+
1821
super().__init__(
1922
metric_name='network_connections_proxy_container_dockerproxy',
2023
metrics={},
2124
resolution=None,
2225
unit=None,
2326
current_dir=os.path.dirname(os.path.abspath(__file__)),
27+
metric_provider_executable=f"{tinyproxy_path}"
2428
)
2529

2630
self._conf_file = f"{CURRENT_DIR}/proxy_conf.conf"
27-
self._filename = f"{self._tmp_folder}/proxy.log"
31+
self._extra_switches = ['-d', '-c', self._conf_file]
2832
self._host_ip = host_ip
2933

30-
tinyproxy_path = subprocess.getoutput('which tinyproxy')
31-
self._metric_provider_executable = f"{tinyproxy_path} -d -c {self._conf_file} > {self._filename}"
32-
33-
3434
def check_system(self):
35+
super().check_system()
3536

3637
output = subprocess.check_output(['tinyproxy', '-v'], stderr=subprocess.STDOUT, text=True)
3738
version_string = output.strip().split()[1].split('-')[0]

metric_providers/powermetrics/provider.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@ def __init__(self, resolution):
3434
self._filename]
3535

3636
def check_system(self):
37+
# no call to super().check_system() as we have different logic of finding the process
3738
if self.is_powermetrics_running():
38-
raise MetricProviderConfigurationError('Another instance of powermetrics is already running on the system!\nPlease close it before running the Green Metrics Tool.')
39+
raise MetricProviderConfigurationError(f"Another instance of the {self._metric_name} metrics provider is already running on the system!\nPlease close it before running the Green Metrics Tool.")
3940

4041
def is_powermetrics_running(self):
4142
ps = subprocess.run(['pgrep', '-qx', 'powermetrics'], check=False)

tests/test_runner.py

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from shutil import copy2
44

55
import pytest
6+
import re
67

78
CURRENT_DIR = os.path.dirname(os.path.abspath(__file__))
89
TEST_DATA_CONFIG_DIR = os.path.join(CURRENT_DIR, "data", "config_files")
@@ -11,16 +12,15 @@
1112
from runner import Runner
1213
from lib.global_config import GlobalConfig
1314
from lib.system_checks import ConfigurationCheckError
14-
15+
from tests import test_functions as Tests
1516

1617
test_data = [
1718
("two_psu_providers.yml", True, does_not_raise()),
1819
("two_psu_providers.yml", False, pytest.raises(ConfigurationCheckError)),
1920
]
2021

2122
@pytest.mark.parametrize("config_file,skip_system_checks,expectation", test_data)
22-
def test_check_system(config_file, skip_system_checks, expectation):
23-
23+
def disable_test_check_system(config_file, skip_system_checks, expectation):
2424
runner = Runner("foo", "baz", "bar", skip_system_checks=skip_system_checks)
2525
copy2(os.path.join(TEST_DATA_CONFIG_DIR, config_file), os.path.join(REPO_ROOT, config_file))
2626
GlobalConfig().override_config(config_name=config_file)
@@ -29,3 +29,23 @@ def test_check_system(config_file, skip_system_checks, expectation):
2929
runner.check_system()
3030
finally:
3131
os.remove(os.path.join(REPO_ROOT, config_file))
32+
33+
def test_reporters_still_running():
34+
runner = Tests.setup_runner(usage_scenario='basic_stress.yml', skip_unsafe=True, dry_run=True)
35+
36+
runner2 = Tests.setup_runner(usage_scenario='basic_stress.yml', skip_unsafe=True, dry_run=True)
37+
38+
runner.check_system('start') # should not fail
39+
40+
try:
41+
Tests.run_until(runner, 'setup_services')
42+
43+
with pytest.raises(Exception) as e:
44+
runner2.import_metric_providers()
45+
46+
expected_error = r'Another instance of the \w+ metrics provider is already running on the system!\nPlease close it before running the Green Metrics Tool.'
47+
assert re.match(expected_error, str(e.value)), Tests.assertion_info(expected_error, str(e.value))
48+
49+
finally:
50+
Tests.cleanup(runner)
51+
Tests.cleanup(runner2)

0 commit comments

Comments
 (0)