Skip to content

Commit 0db8f9c

Browse files
authored
Allow overwrite of str in usage scenario (#1248)
* Making tests faster by running only to needed mark * when merging YML the str keys should be taken preferred from usage_scenario.yml * Added tests for using strs from usage_scenario.yml on merge-include [skip ci]
1 parent fdd81d3 commit 0db8f9c

File tree

5 files changed

+59
-5
lines changed

5 files changed

+59
-5
lines changed

lib/scenario_runner.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,7 @@ def include(self, node):
316316
nodes = self.construct_sequence(node)
317317
else:
318318
raise ValueError("We don't support Mapping Nodes to date")
319+
319320
try:
320321
usage_scenario_dir = os.path.split(usage_scenario_file)[0]
321322
filename = runner_join_paths(usage_scenario_dir, nodes[0], force_path_as_root=True)
@@ -356,6 +357,7 @@ def recursive_lookup(k, d):
356357

357358
# We can use load here as the Loader extends SafeLoader
358359
yml_obj = yaml.load(usage_scenario, Loader)
360+
359361
# Now that we have parsed the yml file we need to check for the special case in which we have a
360362
# compose-file key. In this case we merge the data we find under this key but overwrite it with
361363
# the data from the including file.
@@ -369,7 +371,7 @@ def merge_dicts(dict1, dict2):
369371
else:
370372
dict1[k] = v
371373
return dict1
372-
return dict1
374+
return dict2
373375

374376
new_dict = {}
375377
if 'compose-file' in yml_obj.keys():
@@ -383,6 +385,7 @@ def merge_dicts(dict1, dict2):
383385

384386
yml_obj.update(new_dict)
385387

388+
386389
# If a service is defined as None we remove it. This is so we can have a compose file that starts
387390
# all the various services but we can disable them in the usage_scenario. This is quite useful when
388391
# creating benchmarking scripts and you want to have all options in the compose but not in each benchmark.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
name: Name as is
3+
author: Author as is
4+
description: Description as is
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
name: Name overwritten
3+
author: Author overwritten
4+
5+
compose-file: !include include_example.yml
6+
7+
flow:
8+
- name: dummy
9+
container: test-container-1
10+
commands:
11+
- type: console
12+
command: pwd
13+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
compose-file: !include include_example.yml
3+
4+
name: Name overwritten
5+
author: Author overwritten
6+
7+
flow:
8+
- name: dummy
9+
container: test-container-1
10+
commands:
11+
- type: console
12+
command: pwd
13+

tests/test_usage_scenario.py

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ def test_compose_include_not_same_dir():
155155

156156
with redirect_stdout(out), redirect_stderr(err), pytest.raises(ValueError) as e:
157157
with Tests.RunUntilManager(runner) as context:
158-
context.run_until('setup_services')
158+
context.run_until('import_metric_providers')
159159
assert str(e.value).startswith('Included compose file "../compose.yml" may only be in the same directory as the usage_scenario file as otherwise relative context_paths and volume_paths cannot be mapped anymore') , \
160160
Tests.assertion_info('Root directory escape', str(e.value))
161161

@@ -167,7 +167,7 @@ def test_context_include():
167167

168168
with redirect_stdout(out), redirect_stderr(err):
169169
with Tests.RunUntilManager(runner) as context:
170-
context.run_until('setup_services')
170+
context.run_until('import_metric_providers')
171171
# will not throw an exception
172172

173173
def test_context_include_escape():
@@ -183,12 +183,33 @@ def test_context_include_escape():
183183
assert str(e.value).startswith('../../../../../../ must not be in folder above root repo folder') , \
184184
Tests.assertion_info('Root directory escape', str(e.value))
185185

186+
def test_include_overwrites_string_values():
187+
runner = ScenarioRunner(uri=GMT_DIR, uri_type='folder', filename='tests/data/usage_scenarios/overwrite_string_from_include.yml', skip_system_checks=True, dev_no_metrics=True, dev_no_phase_stats=True, dev_no_sleeps=True, dev_cache_build=False)
188+
189+
with Tests.RunUntilManager(runner) as context:
190+
context.run_until('import_metric_providers')
191+
192+
assert runner._usage_scenario['name'] == 'Name overwritten'
193+
assert runner._usage_scenario['author'] == 'Author overwritten'
194+
assert runner._usage_scenario['description'] == 'Description as is'
195+
196+
def test_include_overwrites_string_values_even_if_top_include():
197+
runner = ScenarioRunner(uri=GMT_DIR, uri_type='folder', filename='tests/data/usage_scenarios/overwrite_string_from_include_even_if_top.yml', skip_system_checks=True, dev_no_metrics=True, dev_no_phase_stats=True, dev_no_sleeps=True, dev_cache_build=False)
198+
199+
with Tests.RunUntilManager(runner) as context:
200+
context.run_until('import_metric_providers')
201+
202+
assert runner._usage_scenario['name'] == 'Name overwritten'
203+
assert runner._usage_scenario['author'] == 'Author overwritten'
204+
assert runner._usage_scenario['description'] == 'Description as is'
205+
206+
186207
def test_unsupported_compose():
187208
runner = ScenarioRunner(uri=GMT_DIR, uri_type='folder', filename='tests/data/usage_scenarios/unsupported_compose.yml', skip_system_checks=True, dev_no_metrics=True, dev_no_phase_stats=True, dev_no_sleeps=True, dev_cache_build=False)
188209

189210
with pytest.raises(SchemaError) as e:
190211
with Tests.RunUntilManager(runner) as context:
191-
context.run_until('setup_services')
212+
context.run_until('import_metric_providers')
192213
assert str(e.value) == 'Your compose file does contain a key that GMT does not support - Please check if the container will still run as intended. If you want to ignore this error you can add the attribute `ignore-unsupported-compose: true` to your usage_scenario.yml\nError: ["Wrong key \'blkio_config\' in {\'image\': \'alpine\', \'blkio_config\': {\'weight\': 300}}"]'
193214

194215
def test_skip_unsupported_compose():
@@ -468,7 +489,7 @@ def test_depends_on_healthcheck_error_max_waiting_time():
468489
def test_network_created():
469490
runner = ScenarioRunner(uri=GMT_DIR, uri_type='folder', filename='tests/data/usage_scenarios/network_stress.yml', skip_system_checks=True, dev_no_metrics=True, dev_no_phase_stats=True, dev_no_sleeps=True, dev_cache_build=True)
470491
with Tests.RunUntilManager(runner) as context:
471-
context.run_until('setup_services')
492+
context.run_until('setup_networks')
472493
ps = subprocess.run(
473494
['docker', 'network', 'ls'],
474495
check=True,

0 commit comments

Comments
 (0)