Skip to content

Commit 609a52b

Browse files
committed
Tests; Improved test speed by 6x
1 parent 9b5fb0b commit 609a52b

9 files changed

+210
-80
lines changed

runner.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -881,8 +881,6 @@ def setup_services(self):
881881
health = 'healthy' # default because some containers have no health
882882
max_waiting_time = config['measurement']['boot']['wait_time_dependencies']
883883
while time_waited < max_waiting_time:
884-
# TODO: Check health status instead if `healthcheck` is enabled (https://github.com/green-coding-berlin/green-metrics-tool/issues/423)
885-
# This waiting loop is actually a pre-work for the upcoming health check. For the check if the container is "running", as implemented here, the waiting loop is not needed.
886884
status_output = subprocess.check_output(
887885
["docker", "container", "inspect", "-f", "{{.State.Status}}", dependent_container],
888886
stderr=subprocess.STDOUT,
@@ -904,7 +902,7 @@ def setup_services(self):
904902
health = health_output.strip()
905903
if health == '<nil>':
906904
raise RuntimeError(f"Health check for dependent_container '{dependent_container}' was requested, but container has no healthcheck implemented!")
907-
print(f"Health of container '{dependent_container}': {health}.")
905+
print(f"Health of container '{dependent_container}': {health}")
908906
elif condition == 'service_started':
909907
pass
910908
else:
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
name: Test depends_on
3+
author: Arne Tarara <[email protected]>
4+
description: test
5+
6+
services:
7+
test-container-1:
8+
image: alpine
9+
depends_on:
10+
test-container-2:
11+
condition: service_completed_successfully
12+
test-container-2:
13+
image: alpine
14+
15+
flow:
16+
- name: dummy
17+
container: test-container-1
18+
commands:
19+
- type: console
20+
command: pwd

tests/data/usage_scenarios/depends_on_error_unsupported_long_form.yml renamed to tests/data/usage_scenarios/depends_on_long_form.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
name: Test depends_on
2+
name: Test depends_on long_form
33
author: David Kopp
44
description: test
55

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
---
2+
name: Test depends_on
3+
author: David Kopp
4+
description: test
5+
6+
services:
7+
test-container-1:
8+
image: alpine
9+
depends_on:
10+
test-container-2:
11+
condition: service_healthy
12+
test-container-2:
13+
image: alpine
14+
healthcheck:
15+
test: ls
16+
interval: 1s
17+
18+
flow:
19+
- name: dummy
20+
container: test-container-1
21+
commands:
22+
- type: console
23+
command: pwd
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
name: Test depends_on
3+
author: David Kopp
4+
description: test
5+
6+
services:
7+
test-container-1:
8+
image: alpine
9+
depends_on:
10+
test-container-2:
11+
condition: service_healthy
12+
test-container-2:
13+
image: alpine
14+
15+
flow:
16+
- name: dummy
17+
container: test-container-1
18+
commands:
19+
- type: console
20+
command: pwd

tests/smoke_test.py

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,11 @@ def setup_module(module):
3939
err = io.StringIO()
4040
GlobalConfig(config_name='test-config.yml').config
4141
with redirect_stdout(out), redirect_stderr(err):
42-
uri = os.path.abspath(os.path.join(
43-
CURRENT_DIR, 'stress-application/'))
42+
uri = os.path.abspath(os.path.join(CURRENT_DIR, 'stress-application/'))
4443
subprocess.run(['docker', 'compose', '-f', uri+'/compose.yml', 'build'], check=True)
4544

4645
# Run the application
47-
runner = Runner(name=RUN_NAME, uri=uri, uri_type='folder', dev_repeat_run=True, skip_system_checks=True)
46+
runner = Runner(name=RUN_NAME, uri=uri, uri_type='folder', dev_no_build=True, dev_no_sleeps=True, dev_no_metrics=False, skip_system_checks=True)
4847
runner.run()
4948

5049
#pylint: disable=global-statement
@@ -87,6 +86,26 @@ def test_db_rows_are_written_and_presented():
8786
if 'NetworkConnectionsProxyContainerProvider' in metric_providers:
8887
metric_providers.remove('NetworkConnectionsProxyContainerProvider')
8988

89+
90+
if 'PowermetricsProvider' in metric_providers:
91+
# The problem here is that the powermetrics provider splits up the output of powermetrics and acts like
92+
# there are loads of providers. This makes a lot easier in showing and processing the data but is
93+
# not std behavior. That is also why we need to patch the imported check down below.
94+
pm_additional_list = [
95+
'cpu_time_powermetrics_vm',
96+
'disk_io_bytesread_powermetrics_vm',
97+
'disk_io_byteswritten_powermetrics_vm',
98+
'energy_impact_powermetrics_vm',
99+
'cores_energy_powermetrics_component',
100+
'cpu_energy_powermetrics_component',
101+
'gpu_energy_powermetrics_component',
102+
'ane_energy_powermetrics_component',
103+
]
104+
105+
metric_providers.extend([utils.get_pascal_case(i) + 'Provider' for i in pm_additional_list])
106+
107+
do_check = True
108+
90109
for d in data:
91110
d_provider = utils.get_pascal_case(d[0]) + 'Provider'
92111
d_count = d[1]
@@ -96,11 +115,20 @@ def test_db_rows_are_written_and_presented():
96115
## Assert the number of rows for that provider is at least 1
97116
assert d_count > 0
98117

99-
## Assert the information printed to std.out matches what's in the db
100-
match = re.search(rf"Imported \S* (\d+) \S* metrics from\s*{d_provider}", run_stdout)
101-
assert match is not None
102-
assert int(match.group(1)) == d_count
103-
104-
## Assert that all the providers in the config are represented
105-
metric_providers.remove(d_provider)
106-
assert len(metric_providers) == 0
118+
if do_check:
119+
if 'PowermetricsProvider' in metric_providers:
120+
## Assert the information printed to std.out matches what's in the db
121+
match = re.search(r"Imported \S* (\d+) \S* metrics from PowermetricsProvider", run_stdout, re.MULTILINE)
122+
assert match is not None
123+
do_check = False
124+
else:
125+
## Assert the information printed to std.out matches what's in the db
126+
match = re.search(rf"Imported \S* (\d+) \S* metrics from\s*{d_provider}", run_stdout)
127+
assert match is not None
128+
assert int(match.group(1)) == d_count
129+
130+
## Assert that all the providers in the config are represented
131+
metric_providers.remove(d_provider)
132+
133+
if not 'PowermetricsProvider' in metric_providers:
134+
assert len(metric_providers) == 0

tests/test_functions.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ def replace_include_in_usage_scenario(usage_scenario_path, docker_compose_filena
3535

3636
def setup_runner(usage_scenario, docker_compose=None, uri='default', uri_type='folder', branch=None,
3737
debug_mode=False, allow_unsafe=False, no_file_cleanup=False,
38-
skip_unsafe=False, verbose_provider_boot=False, dir_name=None, dev_repeat_run=True, skip_system_checks=True,
39-
dry_run=False):
38+
skip_unsafe=False, verbose_provider_boot=False, dir_name=None, dev_no_build=False, skip_system_checks=True,
39+
dev_no_sleeps=True, dev_no_metrics=True):
4040
usage_scenario_path = os.path.join(CURRENT_DIR, 'data/usage_scenarios/', usage_scenario)
4141
if docker_compose is not None:
4242
docker_compose_path = os.path.join(CURRENT_DIR, 'data/docker-compose-files/', docker_compose)
@@ -53,8 +53,8 @@ def setup_runner(usage_scenario, docker_compose=None, uri='default', uri_type='f
5353

5454
return Runner(name=RUN_NAME, uri=uri, uri_type=uri_type, filename=usage_scenario, branch=branch,
5555
debug_mode=debug_mode, allow_unsafe=allow_unsafe, no_file_cleanup=no_file_cleanup,
56-
skip_unsafe=skip_unsafe, verbose_provider_boot=verbose_provider_boot, dev_repeat_run=dev_repeat_run,
57-
skip_system_checks=skip_system_checks, dry_run=dry_run)
56+
skip_unsafe=skip_unsafe, verbose_provider_boot=verbose_provider_boot, dev_no_build=dev_no_build,
57+
skip_system_checks=skip_system_checks, dev_no_sleeps=dev_no_sleeps, dev_no_metrics=dev_no_metrics)
5858

5959
# This function runs the runner up to and *including* the specified step
6060
# remember to catch in try:finally and do cleanup when calling this!

0 commit comments

Comments
 (0)