forked from freeipa/ipa-docker-test-runner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_config.py
201 lines (163 loc) · 4.85 KB
/
test_config.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# Author: Martin Babinsky <martbab@gmail.com>
# See LICENSE file for license
"""
Tests for loading, validating, and saving the configuration
"""
from copy import deepcopy
from collections import OrderedDict
import os
import pytest
from ipadocker import config, constants
# TODO: this could be more sane
CONFIG_DATA = {
'valid_config': {
'data': deepcopy(constants.DEFAULT_CONFIG),
},
'extra_section': {
'data': deepcopy(constants.DEFAULT_CONFIG),
'raises': {
'type': config.UnknownOption,
'path': ()
},
},
'extra_subsection': {
'data': deepcopy(constants.DEFAULT_CONFIG),
'raises': {
'type': config.UnknownOption,
'path': ('host',)
}
},
'invalid_value': {
'data': deepcopy(constants.DEFAULT_CONFIG),
'raises': {
'type': config.InvalidValueType,
'path': ()
}
},
'invalid_nested_value': {
'data': deepcopy(constants.DEFAULT_CONFIG),
'raises': {
'type': config.InvalidValueType,
'path': ('container',)
}
}
}
CONFIG_DATA['extra_section']['data'].update({'extra_section': 'extra_value'})
CONFIG_DATA['extra_subsection']['data']['host'].update(
{'extra_subsection': 'extra_value'})
CONFIG_DATA['invalid_value']['data']['git_repo'] = 42
CONFIG_DATA['invalid_nested_value']['data']['container']['detach'] = 42
@pytest.fixture(params=[data for data in CONFIG_DATA.values()])
def config_dict(request):
"""
Yield individual config data from CONFIG_DATA
"""
return request.param
def test_config_validation(config_dict):
"""
test that the config validation works as intended
"""
if 'raises' in config_dict:
with pytest.raises(config_dict['raises']['type']) as e:
config.validate_config(config_dict['data'],
constants.DEFAULT_CONFIG)
assert e.value.path == config_dict['raises']['path']
else:
config.validate_config(config_dict['data'], constants.DEFAULT_CONFIG)
def test_config_overrides():
overrides = {
'git_repo': 'custom/repo',
'container': {
'image': 'custom-image'
},
'host': {
'tmpfs': ['/var/tmp']
},
'server': {
'domain': 'example.org'
}
}
test_config = config.IPADockerConfig(overrides)
for key, value in overrides.items():
if isinstance(value, dict):
for key2, value2 in overrides[key].items():
assert test_config[key][key2] == value2
else:
assert test_config[key] == value
IPA_RUN_TESTS_CONFIGS = {
('--verbose', '--ignore', 'test_integration'): OrderedDict(
(
('verbose', True),
('ignore', ['test_integration'])
)
),
('--ignore', 'test_integration', '--ignore', 'test_webui'): OrderedDict(
(
('verbose', False),
('ignore', ['test_integration', 'test_webui'])
)
),
}
@pytest.fixture(params=IPA_RUN_TESTS_CONFIGS.items())
def ipa_run_tests_config(request):
"""
yield data mocking 'tests' section in config along with the expected list
of CLI params
"""
return request.param[0], {'tests': request.param[1]}
def test_run_test_config_parsing(ipa_run_tests_config):
expected, test_config = ipa_run_tests_config
run_tests_args = config.get_ipa_run_tests_options(test_config)
assert run_tests_args == list(expected)
@pytest.fixture()
def ipaconfig(request):
"""
Default config
"""
return config.IPADockerConfig()
def test_write_config(ipaconfig):
"""
test that we can write the default config to file
"""
config_file = 'test.yaml'
with open('test.yaml', 'w') as f:
ipaconfig.write_config(f)
try:
os.unlink(config_file)
except OSError:
pass
NESTED_MAPPING = {
'key1': 'value1',
'key2': {
'nested_key1': 42,
'nested_key2': False
},
'key3': {
'nested_key3': {
'nested_key4': 'value2',
'nested_key5': 'value3'
},
'nested_key6': None
}
}
FLAT_MAPPING = {
'key1': 'value1',
'key2_nested_key1': 42,
'key2_nested_key2': False,
'key3_nested_key3_nested_key4': 'value2',
'key3_nested_key3_nested_key5': 'value3',
'key3_nested_key6': None
}
def test_flattened_mapping():
"""
Tests that the `flatten_mapping` function does what it advertises
"""
flat_mapping = config.flatten_mapping(NESTED_MAPPING)
assert flat_mapping == FLAT_MAPPING
def test_deepened_mapping():
"""
Tests that the `deepen_mapping` function does its job
"""
nested_mapping = config.deepen_mapping(FLAT_MAPPING,
reference=NESTED_MAPPING)
assert nested_mapping == NESTED_MAPPING