Skip to content

Commit b609a7a

Browse files
ArneTRdan-mm
andauthored
Env vars numbers tests (#546)
* Added tests and changed test logic to also check for valid results and include all env vars * expanded usage scenario unallowed vars unit tests to check each case * run usage scenario env var unit tests with dry-run to save time --------- Co-authored-by: dan-mm <[email protected]>
1 parent 695a3c1 commit b609a7a

7 files changed

+109
-9
lines changed

tests/data/usage_scenarios/env_vars_stress.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ services:
1111
build: .
1212
environment:
1313
TESTALLOWED: 'alpha-num123_'
14+
TEST1_ALLOWED: 'alpha-key-num123_'
1415
flow:
1516
- name: Stress
1617
container: test-container

tests/data/usage_scenarios/env_vars_stress_unallowed.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ services:
1010
build: .
1111
environment:
1212
TESTALLOWED: 'alpha-num123_'
13+
TEST1_ALLOWED: 'alpha-key-num123_'
1314
TESTBACKTICK: '`'
1415
TESTDOLLAR: '$'
1516
TESTPARENTHESIS: '()'
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
name: Test Stress
3+
author: Dan Mateas
4+
description: test
5+
6+
services:
7+
test-container:
8+
type: container
9+
image: gcb_stress
10+
build: .
11+
environment:
12+
TESTALLOWED: 'alpha-num123_'
13+
TEST1_ALLOWED: 'alpha-key-num123_'
14+
TESTBACKTICK: '`'
15+
16+
flow:
17+
- name: Stress
18+
container: test-container
19+
commands:
20+
- type: console
21+
command: stress-ng -c 1 -t 1 -q
22+
note: Starting Stress
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
name: Test Stress
3+
author: Dan Mateas
4+
description: test
5+
6+
services:
7+
test-container:
8+
type: container
9+
image: gcb_stress
10+
build: .
11+
environment:
12+
TESTALLOWED: 'alpha-num123_'
13+
TEST1_ALLOWED: 'alpha-key-num123_'
14+
TESTDOLLAR: '$'
15+
16+
flow:
17+
- name: Stress
18+
container: test-container
19+
commands:
20+
- type: console
21+
command: stress-ng -c 1 -t 1 -q
22+
note: Starting Stress
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
name: Test Stress
3+
author: Dan Mateas
4+
description: test
5+
6+
services:
7+
test-container:
8+
type: container
9+
image: gcb_stress
10+
build: .
11+
environment:
12+
TESTALLOWED: 'alpha-num123_'
13+
TEST1_ALLOWED: 'alpha-key-num123_'
14+
TESTPARENTHESIS: '()'
15+
16+
flow:
17+
- name: Stress
18+
container: test-container
19+
commands:
20+
- type: console
21+
command: stress-ng -c 1 -t 1 -q
22+
note: Starting Stress

tests/test_functions.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +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):
38+
skip_unsafe=False, verbose_provider_boot=False, dir_name=None, dev_repeat_run=True, skip_system_checks=True,
39+
dry_run=False):
3940
usage_scenario_path = os.path.join(CURRENT_DIR, 'data/usage_scenarios/', usage_scenario)
4041
if docker_compose is not None:
4142
docker_compose_path = os.path.join(CURRENT_DIR, 'data/docker-compose-files/', docker_compose)
@@ -53,7 +54,7 @@ def setup_runner(usage_scenario, docker_compose=None, uri='default', uri_type='f
5354
return Runner(name=RUN_NAME, uri=uri, uri_type=uri_type, filename=usage_scenario, branch=branch,
5455
debug_mode=debug_mode, allow_unsafe=allow_unsafe, no_file_cleanup=no_file_cleanup,
5556
skip_unsafe=skip_unsafe, verbose_provider_boot=verbose_provider_boot, dev_repeat_run=dev_repeat_run,
56-
skip_system_checks=skip_system_checks)
57+
skip_system_checks=skip_system_checks, dry_run=dry_run)
5758

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

tests/test_usage_scenario.py

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def get_env_vars(runner):
5959

6060
ps = subprocess.run(
6161
['docker', 'exec', 'test-container', '/bin/sh',
62-
'-c', 'echo $TESTALLOWED'],
62+
'-c', 'env'],
6363
check=True,
6464
stderr=subprocess.PIPE,
6565
stdout=subprocess.PIPE,
@@ -70,23 +70,54 @@ def get_env_vars(runner):
7070
Tests.cleanup(runner)
7171
return env_var_output
7272

73-
def test_env_variable_no_skip_or_allow():
74-
runner = Tests.setup_runner(usage_scenario='env_vars_stress_unallowed.yml')
73+
def test_env_variable_with_incorrect_envs_no_flags_backtick():
74+
runner = Tests.setup_runner(usage_scenario='env_vars_stress_unallowed_backtick.yml', dry_run=True)
7575
with pytest.raises(RuntimeError) as e:
7676
get_env_vars(runner)
7777
expected_exception = 'Docker container setup environment var value had wrong format.'
7878
assert expected_exception in str(e.value), \
7979
Tests.assertion_info(f"Exception: {expected_exception}", str(e.value))
8080

81+
def test_env_variable_with_incorrect_envs_no_flags_dollar():
82+
runner = Tests.setup_runner(usage_scenario='env_vars_stress_unallowed_dollar.yml', dry_run=True)
83+
with pytest.raises(RuntimeError) as e:
84+
get_env_vars(runner)
85+
expected_exception = 'Docker container setup environment var value had wrong format.'
86+
assert expected_exception in str(e.value), \
87+
Tests.assertion_info(f"Exception: {expected_exception}", str(e.value))
88+
89+
def test_env_variable_with_incorrect_envs_no_flags_paren():
90+
runner = Tests.setup_runner(usage_scenario='env_vars_stress_unallowed_paren.yml', dry_run=True)
91+
with pytest.raises(RuntimeError) as e:
92+
get_env_vars(runner)
93+
expected_exception = 'Docker container setup environment var value had wrong format.'
94+
assert expected_exception in str(e.value), \
95+
Tests.assertion_info(f"Exception: {expected_exception}", str(e.value))
96+
97+
def test_env_variable_unsafe_false():
98+
runner = Tests.setup_runner(usage_scenario='env_vars_stress.yml', skip_unsafe=False, dry_run=True)
99+
env_var_output = get_env_vars(runner)
100+
print("Env var output is ", env_var_output)
101+
assert 'TESTALLOWED=alpha-num123_' in env_var_output, Tests.assertion_info('TESTALLOWED=alpha-num123_', env_var_output)
102+
assert 'TEST1_ALLOWED=alpha-key-num123_' in env_var_output, Tests.assertion_info('TEST1_ALLOWED=alpha-key-num123_', env_var_output)
103+
81104
def test_env_variable_skip_unsafe_true():
82-
runner = Tests.setup_runner(usage_scenario='env_vars_stress.yml', skip_unsafe=True)
105+
runner = Tests.setup_runner(usage_scenario='env_vars_stress_unallowed.yml', skip_unsafe=True, dry_run=True)
83106
env_var_output = get_env_vars(runner)
84-
assert env_var_output == 'alpha-num123_\n', Tests.assertion_info('alpha-num123_', env_var_output)
107+
assert 'TESTALLOWED=alpha-num123_' in env_var_output, Tests.assertion_info('TESTALLOWED=alpha-num123_', env_var_output)
108+
assert 'TEST1_ALLOWED=alpha-key-num123_' in env_var_output, Tests.assertion_info('TEST1_ALLOWED=alpha-key-num123_', env_var_output)
109+
assert 'TESTBACKTICK' not in env_var_output, Tests.assertion_info('TESTBACKTICK', env_var_output)
110+
assert 'TESTDOLLAR' not in env_var_output, Tests.assertion_info('TESTDOLLAR', env_var_output)
111+
assert 'TESTPARENTHESIS' not in env_var_output, Tests.assertion_info('TESTPARENTHESIS', env_var_output)
85112

86113
def test_env_variable_allow_unsafe_true():
87-
runner = Tests.setup_runner(usage_scenario='env_vars_stress.yml', allow_unsafe=True)
114+
runner = Tests.setup_runner(usage_scenario='env_vars_stress_unallowed.yml', allow_unsafe=True, dry_run=True)
88115
env_var_output = get_env_vars(runner)
89-
assert env_var_output == 'alpha-num123_\n', Tests.assertion_info('alpha-num123_', env_var_output)
116+
assert 'TESTALLOWED=alpha-num123_' in env_var_output, Tests.assertion_info('TESTALLOWED=alpha-num123_', env_var_output)
117+
assert 'TEST1_ALLOWED=alpha-key-num123_' in env_var_output, Tests.assertion_info('TEST1_ALLOWED=alpha-key-num123_', env_var_output)
118+
assert 'TESTBACKTICK' in env_var_output, Tests.assertion_info('TESTBACKTICK', env_var_output)
119+
assert 'TESTDOLLAR' in env_var_output, Tests.assertion_info('TESTDOLLAR', env_var_output)
120+
assert 'TESTPARENTHESIS' in env_var_output, Tests.assertion_info('TESTPARENTHESIS', env_var_output)
90121

91122
# ports: [int:int] (optional)
92123
# Docker container portmapping on host OS to be used with --allow-unsafe flag.

0 commit comments

Comments
 (0)