summaryrefslogtreecommitdiffstats
path: root/testing/mozharness/test/test_base_config.py
blob: 42ec7a641dd83884f034856e764364e9a3bf9c4d (plain)
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
import os
import unittest

JSON_TYPE = None
try:
    import simplejson as json
    assert json
except ImportError:
    import json
    JSON_TYPE = 'json'
else:
    JSON_TYPE = 'simplejson'

import mozharness.base.config as config
from copy import deepcopy

MH_DIR = os.path.dirname(os.path.dirname(__file__))


class TestParseConfigFile(unittest.TestCase):
    def _get_json_config(self, filename=os.path.join(MH_DIR, "configs", "test", "test.json"),
                         output='dict'):
        fh = open(filename)
        contents = json.load(fh)
        fh.close()
        if 'output' == 'dict':
            return dict(contents)
        else:
            return contents

    def _get_python_config(self, filename=os.path.join(MH_DIR, "configs", "test", "test.py"),
                           output='dict'):
        global_dict = {}
        local_dict = {}
        execfile(filename, global_dict, local_dict)
        return local_dict['config']

    def test_json_config(self):
        c = config.BaseConfig(initial_config_file='test/test.json')
        content_dict = self._get_json_config()
        for key in content_dict.keys():
            self.assertEqual(content_dict[key], c._config[key])

    def test_python_config(self):
        c = config.BaseConfig(initial_config_file='test/test.py')
        config_dict = self._get_python_config()
        for key in config_dict.keys():
            self.assertEqual(config_dict[key], c._config[key])

    def test_illegal_config(self):
        self.assertRaises(IOError, config.parse_config_file, "this_file_does_not_exist.py", search_path="yadda")

    def test_illegal_suffix(self):
        self.assertRaises(RuntimeError, config.parse_config_file, "test/test.illegal_suffix")

    def test_malformed_json(self):
        if JSON_TYPE == 'simplejson':
            self.assertRaises(json.decoder.JSONDecodeError, config.parse_config_file, "test/test_malformed.json")
        else:
            self.assertRaises(ValueError, config.parse_config_file, "test/test_malformed.json")

    def test_malformed_python(self):
        self.assertRaises(SyntaxError, config.parse_config_file, "test/test_malformed.py")

    def test_multiple_config_files_override_string(self):
        c = config.BaseConfig(initial_config_file='test/test.py')
        c.parse_args(['--cfg', 'test/test_override.py,test/test_override2.py'])
        self.assertEqual(c._config['override_string'], 'yay')

    def test_multiple_config_files_override_list(self):
        c = config.BaseConfig(initial_config_file='test/test.py')
        c.parse_args(['--cfg', 'test/test_override.py,test/test_override2.py'])
        self.assertEqual(c._config['override_list'], ['yay', 'worked'])

    def test_multiple_config_files_override_dict(self):
        c = config.BaseConfig(initial_config_file='test/test.py')
        c.parse_args(['--cfg', 'test/test_override.py,test/test_override2.py'])
        self.assertEqual(c._config['override_dict'], {'yay': 'worked'})

    def test_multiple_config_files_keep_string(self):
        c = config.BaseConfig(initial_config_file='test/test.py')
        c.parse_args(['--cfg', 'test/test_override.py,test/test_override2.py'])
        self.assertEqual(c._config['keep_string'], "don't change me")

    def test_optional_config_files_override_value(self):
        c = config.BaseConfig(initial_config_file='test/test.py')
        c.parse_args(['--cfg', 'test/test_override.py,test/test_override2.py',
                      '--opt-cfg', 'test/test_optional.py'])
        self.assertEqual(c._config['opt_override'], "new stuff")

    def test_optional_config_files_missing_config(self):
        c = config.BaseConfig(initial_config_file='test/test.py')
        c.parse_args(['--cfg', 'test/test_override.py,test/test_override2.py',
                      '--opt-cfg', 'test/test_optional.py,does_not_exist.py'])
        self.assertEqual(c._config['opt_override'], "new stuff")

    def test_optional_config_files_keep_string(self):
        c = config.BaseConfig(initial_config_file='test/test.py')
        c.parse_args(['--cfg', 'test/test_override.py,test/test_override2.py',
                      '--opt-cfg', 'test/test_optional.py'])
        self.assertEqual(c._config['keep_string'], "don't change me")


class TestReadOnlyDict(unittest.TestCase):
    control_dict = {
        'b': '2',
        'c': {'d': '4'},
        'e': ['f', 'g'],
        'e': ['f', 'g', {'turtles': ['turtle1']}],
        'd': {
            'turtles': ['turtle1']
        }
    }

    def get_unlocked_ROD(self):
        r = config.ReadOnlyDict(self.control_dict)
        return r

    def get_locked_ROD(self):
        r = config.ReadOnlyDict(self.control_dict)
        r.lock()
        return r

    def test_create_ROD(self):
        r = self.get_unlocked_ROD()
        self.assertEqual(r, self.control_dict,
                         msg="can't transfer dict to ReadOnlyDict")

    def test_pop_item(self):
        r = self.get_unlocked_ROD()
        r.popitem()
        self.assertEqual(len(r), len(self.control_dict) - 1,
                         msg="can't popitem() ReadOnlyDict when unlocked")

    def test_pop(self):
        r = self.get_unlocked_ROD()
        r.pop('e')
        self.assertEqual(len(r), len(self.control_dict) - 1,
                         msg="can't pop() ReadOnlyDict when unlocked")

    def test_set(self):
        r = self.get_unlocked_ROD()
        r['e'] = 'yarrr'
        self.assertEqual(r['e'], 'yarrr',
                         msg="can't set var in ReadOnlyDict when unlocked")

    def test_del(self):
        r = self.get_unlocked_ROD()
        del r['e']
        self.assertEqual(len(r), len(self.control_dict) - 1,
                         msg="can't del in ReadOnlyDict when unlocked")

    def test_clear(self):
        r = self.get_unlocked_ROD()
        r.clear()
        self.assertEqual(r, {},
                         msg="can't clear() ReadOnlyDict when unlocked")

    def test_set_default(self):
        r = self.get_unlocked_ROD()
        for key in self.control_dict.keys():
            r.setdefault(key, self.control_dict[key])
        self.assertEqual(r, self.control_dict,
                         msg="can't setdefault() ReadOnlyDict when unlocked")

    def test_locked_set(self):
        r = self.get_locked_ROD()
        # TODO use |with self.assertRaises(AssertionError):| if/when we're
        # all on 2.7.
        try:
            r['e'] = 2
        except:
            pass
        else:
            self.assertEqual(0, 1, msg="can set r['e'] when locked")

    def test_locked_del(self):
        r = self.get_locked_ROD()
        try:
            del r['e']
        except:
            pass
        else:
            self.assertEqual(0, 1, "can del r['e'] when locked")

    def test_locked_popitem(self):
        r = self.get_locked_ROD()
        self.assertRaises(AssertionError, r.popitem)

    def test_locked_update(self):
        r = self.get_locked_ROD()
        self.assertRaises(AssertionError, r.update, {})

    def test_locked_set_default(self):
        r = self.get_locked_ROD()
        self.assertRaises(AssertionError, r.setdefault, {})

    def test_locked_pop(self):
        r = self.get_locked_ROD()
        self.assertRaises(AssertionError, r.pop)

    def test_locked_clear(self):
        r = self.get_locked_ROD()
        self.assertRaises(AssertionError, r.clear)

    def test_locked_second_level_dict_pop(self):
        r = self.get_locked_ROD()
        self.assertRaises(AssertionError, r['c'].update, {})

    def test_locked_second_level_list_pop(self):
        r = self.get_locked_ROD()
        with self.assertRaises(AttributeError):
            r['e'].pop()

    def test_locked_third_level_mutate(self):
        r = self.get_locked_ROD()
        with self.assertRaises(AttributeError):
            r['d']['turtles'].append('turtle2')

    def test_locked_object_in_tuple_mutate(self):
        r = self.get_locked_ROD()
        with self.assertRaises(AttributeError):
            r['e'][2]['turtles'].append('turtle2')

    def test_locked_second_level_dict_pop2(self):
        r = self.get_locked_ROD()
        self.assertRaises(AssertionError, r['c'].update, {})

    def test_locked_second_level_list_pop2(self):
        r = self.get_locked_ROD()
        with self.assertRaises(AttributeError):
            r['e'].pop()

    def test_locked_third_level_mutate2(self):
        r = self.get_locked_ROD()
        with self.assertRaises(AttributeError):
            r['d']['turtles'].append('turtle2')

    def test_locked_object_in_tuple_mutate2(self):
        r = self.get_locked_ROD()
        with self.assertRaises(AttributeError):
            r['e'][2]['turtles'].append('turtle2')

    def test_locked_deepcopy_set(self):
        r = self.get_locked_ROD()
        c = deepcopy(r)
        c['e'] = 'hey'
        self.assertEqual(c['e'], 'hey', "can't set var in ROD after deepcopy")


class TestActions(unittest.TestCase):
    all_actions = ['a', 'b', 'c', 'd', 'e']
    default_actions = ['b', 'c', 'd']

    def test_verify_actions(self):
        c = config.BaseConfig(initial_config_file='test/test.json')
        try:
            c.verify_actions(['not_a_real_action'])
        except:
            pass
        else:
            self.assertEqual(0, 1, msg="verify_actions() didn't die on invalid action")
        c = config.BaseConfig(initial_config_file='test/test.json')
        returned_actions = c.verify_actions(c.all_actions)
        self.assertEqual(c.all_actions, returned_actions,
                         msg="returned actions from verify_actions() changed")

    def test_default_actions(self):
        c = config.BaseConfig(default_actions=self.default_actions,
                              all_actions=self.all_actions,
                              initial_config_file='test/test.json')
        self.assertEqual(self.default_actions, c.get_actions(),
                         msg="default_actions broken")

    def test_no_action1(self):
        c = config.BaseConfig(default_actions=self.default_actions,
                              all_actions=self.all_actions,
                              initial_config_file='test/test.json')
        c.parse_args(args=['foo', '--no-action', 'a'])
        self.assertEqual(self.default_actions, c.get_actions(),
                         msg="--no-ACTION broken")

    def test_no_action2(self):
        c = config.BaseConfig(default_actions=self.default_actions,
                              all_actions=self.all_actions,
                              initial_config_file='test/test.json')
        c.parse_args(args=['foo', '--no-c'])
        self.assertEqual(['b', 'd'], c.get_actions(),
                         msg="--no-ACTION broken")

    def test_add_action(self):
        c = config.BaseConfig(default_actions=self.default_actions,
                              all_actions=self.all_actions,
                              initial_config_file='test/test.json')
        c.parse_args(args=['foo', '--add-action', 'e'])
        self.assertEqual(['b', 'c', 'd', 'e'], c.get_actions(),
                         msg="--add-action ACTION broken")

    def test_only_action(self):
        c = config.BaseConfig(default_actions=self.default_actions,
                              all_actions=self.all_actions,
                              initial_config_file='test/test.json')
        c.parse_args(args=['foo', '--a', '--e'])
        self.assertEqual(['a', 'e'], c.get_actions(),
                         msg="--ACTION broken")

if __name__ == '__main__':
    unittest.main()