config_data_test.py 41.4 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# Copyright (C) 2010  Internet Systems Consortium.
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SYSTEMS CONSORTIUM
# DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
# INTERNET SYSTEMS CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
# FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
# NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
# WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

#
17
# Tests for the ConfigData and MultiConfigData classes
18
19
20
21
22
#

import unittest
import os
from isc.config.config_data import *
23
from isc.config.module_spec import *
24
25
26
27
28
29
30

class TestConfigData(unittest.TestCase):
    def setUp(self):
        if 'CONFIG_TESTDATA_PATH' in os.environ:
            self.data_path = os.environ['CONFIG_TESTDATA_PATH']
        else:
            self.data_path = "../../../testdata"
31
32
        spec = isc.config.module_spec_from_file(self.data_path + os.sep + "spec2.spec")
        self.cd = ConfigData(spec)
33

34
35
36
37
38
39
40
41
    #def test_module_spec_from_file(self):
    #    spec = isc.config.module_spec_from_file(self.data_path + os.sep + "spec1.spec")
    #    cd = ConfigData(spec)
    #    self.assertEqual(cd.specification, spec)
    #    self.assertEqual(cd.data, {})
    #    self.assertRaises(ConfigDataError, ConfigData, 1)

    def test_check_type(self):
42
43
        config_spec = isc.config.module_spec_from_file(self.data_path + os.sep + "spec22.spec").get_config_spec()
        spec_part = find_spec_part(config_spec, "value1")
44
45
46
47
48
49
50
        check_type(spec_part, 1)
        self.assertRaises(isc.cc.data.DataTypeError, check_type, spec_part, 1.1)
        self.assertRaises(isc.cc.data.DataTypeError, check_type, spec_part, True)
        self.assertRaises(isc.cc.data.DataTypeError, check_type, spec_part, "a")
        self.assertRaises(isc.cc.data.DataTypeError, check_type, spec_part, [ 1, 2 ])
        self.assertRaises(isc.cc.data.DataTypeError, check_type, spec_part, { "a": 1 })
        
51
        spec_part = find_spec_part(config_spec, "value2")
52
53
54
55
56
57
58
        check_type(spec_part, 1.1)
        self.assertRaises(isc.cc.data.DataTypeError, check_type, spec_part, 1)
        self.assertRaises(isc.cc.data.DataTypeError, check_type, spec_part, True)
        self.assertRaises(isc.cc.data.DataTypeError, check_type, spec_part, "a")
        self.assertRaises(isc.cc.data.DataTypeError, check_type, spec_part, [ 1, 2 ])
        self.assertRaises(isc.cc.data.DataTypeError, check_type, spec_part, { "a": 1 })
        
59
        spec_part = find_spec_part(config_spec, "value3")
60
61
62
63
64
65
66
67
        check_type(spec_part, True)
        check_type(spec_part, False)
        self.assertRaises(isc.cc.data.DataTypeError, check_type, spec_part, 1)
        self.assertRaises(isc.cc.data.DataTypeError, check_type, spec_part, 1.1)
        self.assertRaises(isc.cc.data.DataTypeError, check_type, spec_part, "a")
        self.assertRaises(isc.cc.data.DataTypeError, check_type, spec_part, [ 1, 2 ])
        self.assertRaises(isc.cc.data.DataTypeError, check_type, spec_part, { "a": 1 })
        
68
        spec_part = find_spec_part(config_spec, "value4")
69
70
71
72
73
74
75
        check_type(spec_part, "asdf")
        self.assertRaises(isc.cc.data.DataTypeError, check_type, spec_part, 1)
        self.assertRaises(isc.cc.data.DataTypeError, check_type, spec_part, 1.1)
        self.assertRaises(isc.cc.data.DataTypeError, check_type, spec_part, True)
        self.assertRaises(isc.cc.data.DataTypeError, check_type, spec_part, [ 1, 2 ])
        self.assertRaises(isc.cc.data.DataTypeError, check_type, spec_part, { "a": 1 })
        
76
77
        spec_part = find_spec_part(config_spec, "value5")
        check_type(spec_part, [1, 2])
78
79
80
81
        self.assertRaises(isc.cc.data.DataTypeError, check_type, spec_part, 1)
        self.assertRaises(isc.cc.data.DataTypeError, check_type, spec_part, 1.1)
        self.assertRaises(isc.cc.data.DataTypeError, check_type, spec_part, True)
        self.assertRaises(isc.cc.data.DataTypeError, check_type, spec_part, "a")
82
        self.assertRaises(isc.cc.data.DataTypeError, check_type, spec_part, [ "a", "b" ])
83
84
        self.assertRaises(isc.cc.data.DataTypeError, check_type, spec_part, { "a": 1 })
        
85
        spec_part = find_spec_part(config_spec, "value6")
86
87
88
89
90
91
92
93
        check_type(spec_part, { "value1": "aaa", "value2": 2 })
        self.assertRaises(isc.cc.data.DataTypeError, check_type, spec_part, 1)
        self.assertRaises(isc.cc.data.DataTypeError, check_type, spec_part, 1.1)
        self.assertRaises(isc.cc.data.DataTypeError, check_type, spec_part, True)
        self.assertRaises(isc.cc.data.DataTypeError, check_type, spec_part, "a")
        self.assertRaises(isc.cc.data.DataTypeError, check_type, spec_part, [ 1, 2 ])
        #self.assertRaises(isc.cc.data.DataTypeError, check_type, spec_part, { "value1": 1 })

94
95
        self.assertRaises(isc.cc.data.DataTypeError, check_type, config_spec, 1)

96
97
98
99
100
101
102
103
104
105
106
107
    def test_convert_type(self):
        config_spec = isc.config.module_spec_from_file(self.data_path + os.sep + "spec22.spec").get_config_spec()
        spec_part = find_spec_part(config_spec, "value1")
        self.assertEqual(1, convert_type(spec_part, '1'))
        self.assertEqual(2, convert_type(spec_part, 2.1))
        self.assertEqual(2, convert_type(spec_part, '2'))
        self.assertEqual(3, convert_type(spec_part, '3'))
        self.assertEqual(1, convert_type(spec_part, True))

        self.assertRaises(isc.cc.data.DataTypeError, convert_type, spec_part, "a")
        self.assertRaises(isc.cc.data.DataTypeError, convert_type, spec_part, [ 1, 2 ])
        self.assertRaises(isc.cc.data.DataTypeError, convert_type, spec_part, { "a": 1 })
Jelte Jansen's avatar
Jelte Jansen committed
108
109
        self.assertRaises(isc.cc.data.DataTypeError, convert_type, 1, "a")
        self.assertRaises(isc.cc.data.DataTypeError, convert_type, { 'somedict': 'somevalue' }, "a")
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
        
        spec_part = find_spec_part(config_spec, "value2")
        self.assertEqual(1.1, convert_type(spec_part, '1.1'))
        self.assertEqual(123.0, convert_type(spec_part, '123'))
        self.assertEqual(1.0, convert_type(spec_part, True))
        self.assertRaises(isc.cc.data.DataTypeError, convert_type, spec_part, "a")
        self.assertRaises(isc.cc.data.DataTypeError, convert_type, spec_part, [ 1, 2 ])
        self.assertRaises(isc.cc.data.DataTypeError, convert_type, spec_part, { "a": 1 })

        spec_part = find_spec_part(config_spec, "value3")
        self.assertEqual(True, convert_type(spec_part, 'True'))
        self.assertEqual(False, convert_type(spec_part, 'False'))
        self.assertEqual(True, convert_type(spec_part, 1))
        self.assertEqual(True, convert_type(spec_part, 1.1))
        self.assertEqual(True, convert_type(spec_part, 'a'))
        self.assertEqual(True, convert_type(spec_part, [1, 2]))
        self.assertEqual(True, convert_type(spec_part, {'a' : 1}))

        spec_part = find_spec_part(config_spec, "value4")
        self.assertEqual('asdf', convert_type(spec_part, "asdf"))
        self.assertEqual('1', convert_type(spec_part, 1))
        self.assertEqual('1.1', convert_type(spec_part, 1.1))
        self.assertEqual('True', convert_type(spec_part, True))
        
        spec_part = find_spec_part(config_spec, "value5")
        self.assertEqual([1, 2], convert_type(spec_part, '1, 2'))
        self.assertEqual([1, 2, 3], convert_type(spec_part, '1 2  3'))
        self.assertEqual([1, 2, 3,4], convert_type(spec_part, '1 2  3, 4'))
        self.assertEqual([1], convert_type(spec_part, [1,]))
        self.assertEqual([1,2], convert_type(spec_part, [1,2]))
        self.assertEqual([1,2], convert_type(spec_part, ['1', '2']))

        self.assertRaises(isc.cc.data.DataTypeError, convert_type, spec_part, 1.1)
        self.assertRaises(isc.cc.data.DataTypeError, convert_type, spec_part, True)
        self.assertRaises(isc.cc.data.DataTypeError, convert_type, spec_part, "a")
        self.assertRaises(isc.cc.data.DataTypeError, convert_type, spec_part, [ "a", "b" ])
        self.assertRaises(isc.cc.data.DataTypeError, convert_type, spec_part, [ "1", "b" ])
        self.assertRaises(isc.cc.data.DataTypeError, convert_type, spec_part, { "a": 1 })

Jelte Jansen's avatar
Jelte Jansen committed
149
150
151
152
153
154
155
156
157
158
159
        spec_part = find_spec_part(config_spec, "value6")
        self.assertEqual({}, convert_type(spec_part, '{}'))
        self.assertEqual({ 'v61': 'a' }, convert_type(spec_part, '{ \'v61\': \'a\' }'))

        self.assertRaises(isc.cc.data.DataTypeError, convert_type, spec_part, 1.1)
        self.assertRaises(isc.cc.data.DataTypeError, convert_type, spec_part, True)
        self.assertRaises(isc.cc.data.DataTypeError, convert_type, spec_part, "a")
        self.assertRaises(isc.cc.data.DataTypeError, convert_type, spec_part, "1")
        self.assertRaises(isc.cc.data.DataTypeError, convert_type, spec_part, [ "a", "b" ])
        self.assertRaises(isc.cc.data.DataTypeError, convert_type, spec_part, [ "1", "b" ])
        self.assertRaises(isc.cc.data.DataTypeError, convert_type, spec_part, { "a": 1 })
160
        self.assertRaises(isc.cc.data.DataTypeError, convert_type, spec_part, "\"{ \"a\": 1 }\"")
Jelte Jansen's avatar
Jelte Jansen committed
161

162
163
164
165
166
167
168
169
170
171
172
173
174
175
        spec_part = find_spec_part(config_spec, "value7")
        self.assertEqual(['1', '2'], convert_type(spec_part, '1, 2'))
        self.assertEqual(['1', '2', '3'], convert_type(spec_part, '1 2  3'))
        self.assertEqual(['1', '2', '3','4'], convert_type(spec_part, '1 2  3, 4'))
        self.assertEqual([1], convert_type(spec_part, [1,]))
        self.assertEqual([1,2], convert_type(spec_part, [1,2]))
        self.assertEqual(['1','2'], convert_type(spec_part, ['1', '2']))

        self.assertRaises(isc.cc.data.DataTypeError, convert_type, spec_part, 1.1)
        self.assertRaises(isc.cc.data.DataTypeError, convert_type, spec_part, True)
        self.assertEqual(['a'], convert_type(spec_part, "a"))
        self.assertEqual(['a', 'b'], convert_type(spec_part, ["a", "b" ]))
        self.assertEqual([1, 'b'], convert_type(spec_part, [1, "b" ]))

176
177
178
179
    def test_find_spec_part(self):
        config_spec = self.cd.get_module_spec().get_config_spec()
        spec_part = find_spec_part(config_spec, "item1")
        self.assertEqual({'item_name': 'item1', 'item_type': 'integer', 'item_optional': False, 'item_default': 1, }, spec_part)
180
181
        spec_part = find_spec_part(config_spec, "/item1")
        self.assertEqual({'item_name': 'item1', 'item_type': 'integer', 'item_optional': False, 'item_default': 1, }, spec_part)
182
183
        self.assertRaises(isc.cc.data.DataNotFoundError, find_spec_part, config_spec, "no_such_item")
        self.assertRaises(isc.cc.data.DataNotFoundError, find_spec_part, config_spec, "no_such_item/multilevel")
184
185
        self.assertRaises(isc.cc.data.DataNotFoundError, find_spec_part, config_spec, "item6/multilevel")
        self.assertRaises(isc.cc.data.DataNotFoundError, find_spec_part, 1, "item6/multilevel")
186
187
188
        spec_part = find_spec_part(config_spec, "item6/value1")
        self.assertEqual({'item_name': 'value1', 'item_type': 'string', 'item_optional': True, 'item_default': 'default'}, spec_part)

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
    def test_find_spec_part_lists(self):
        # A few specific tests for list data
        module_spec = isc.config.module_spec_from_file(self.data_path +
                                                       os.sep +
                                                       "spec31.spec")
        config_spec = module_spec.get_config_spec()

        expected_spec_part = {'item_name': 'number',
                              'item_type': 'integer',
                              'item_default': 1,
                              'item_optional': False}

        # First a check for a correct fetch
        spec_part = find_spec_part(config_spec,
                                   "/first_list_items[0]/second_list_items[1]/"
                                   "map_element/list1[1]/list2[1]")
        self.assertEqual(expected_spec_part, spec_part)

        # Leaving out an index should fail by default
        self.assertRaises(isc.cc.data.DataNotFoundError,
                          find_spec_part, config_spec,
                          "/first_list_items[0]/second_list_items/"
                          "map_element/list1[1]/list2[1]")

        # But not for the last element
        spec_part = find_spec_part(config_spec,
                                   "/first_list_items[0]/second_list_items[1]/"
                                   "map_element/list1[1]/list2")
        self.assertEqual(expected_spec_part, spec_part)

        # And also not if strict_identifier is false (third argument)
        spec_part = find_spec_part(config_spec,
                                   "/first_list_items[0]/second_list_items/"
                                   "map_element/list1[1]/list2[1]", False)
        self.assertEqual(expected_spec_part, spec_part)


226
227
    def test_spec_name_list(self):
        name_list = spec_name_list(self.cd.get_module_spec().get_config_spec())
228
        self.assertEqual(['item1', 'item2', 'item3', 'item4', 'item5', 'item6'], name_list)
229
        name_list = spec_name_list(self.cd.get_module_spec().get_config_spec(), "", True)
230
        self.assertEqual(['item1', 'item2', 'item3', 'item4', 'item5', 'item6/value1', 'item6/value2'], name_list)
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
        spec_part = find_spec_part(self.cd.get_module_spec().get_config_spec(), "item6")
        name_list = spec_name_list(spec_part, "item6", True)
        self.assertEqual(['item6/value1', 'item6/value2'], name_list)
        spec_part = find_spec_part(self.cd.get_module_spec().get_config_spec(), "item6")
        name_list = spec_name_list(spec_part, "item6", True)
        self.assertEqual(['item6/value1', 'item6/value2'], name_list)

        config_spec = isc.config.module_spec_from_file(self.data_path + os.sep + "spec22.spec").get_config_spec()
        spec_part = find_spec_part(config_spec, "value9")
        name_list = spec_name_list(spec_part, "value9", True)
        self.assertEqual(['value9/v91', 'value9/v92/v92a', 'value9/v92/v92b'], name_list)

        name_list = spec_name_list({ "myModule": config_spec }, "", False)
        self.assertEqual(['myModule/'], name_list)
        name_list = spec_name_list({ "myModule": config_spec }, "", True)
246
        self.assertEqual(['myModule/', 'myModule/value1', 'myModule/value2', 'myModule/value3', 'myModule/value4', 'myModule/value5', 'myModule/value6/v61', 'myModule/value6/v62', 'myModule/value7', 'myModule/value8', 'myModule/value9/v91', 'myModule/value9/v92/v92a', 'myModule/value9/v92/v92b'], name_list)
247
248
249

        self.assertRaises(ConfigDataError, spec_name_list, 1)
        self.assertRaises(ConfigDataError, spec_name_list, [ 'a' ])
250

251
252
253
    def test_init(self):
        self.assertRaises(ConfigDataError, ConfigData, "asdf")
        
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
    def test_get_value(self):
        value, default = self.cd.get_value("item1")
        self.assertEqual(1, value)
        self.assertEqual(True, default)
        value, default = self.cd.get_value("item2")
        self.assertEqual(1.1, value)
        self.assertEqual(True, default)
        value, default = self.cd.get_value("item3")
        self.assertEqual(True, value)
        self.assertEqual(True, default)
        value, default = self.cd.get_value("item4")
        self.assertEqual("test", value)
        self.assertEqual(True, default)
        value, default = self.cd.get_value("item5")
        self.assertEqual(["a", "b"], value)
        self.assertEqual(True, default)
        value, default = self.cd.get_value("item6")
        self.assertEqual({}, value)
        self.assertEqual(True, default)
        self.assertRaises(isc.cc.data.DataNotFoundError, self.cd.get_value, "no_such_item")
        value, default = self.cd.get_value("item6/value2")
        self.assertEqual(None, value)
        self.assertEqual(False, default)
277
        self.assertRaises(isc.cc.data.DataNotFoundError, self.cd.get_value, "item6/no_such_item")
278

Jelte Jansen's avatar
Jelte Jansen committed
279
280
281
    def test_get_default_value(self):
        self.assertEqual(1, self.cd.get_default_value("item1"))
        self.assertEqual('default', self.cd.get_default_value("item6/value1"))
282
        self.assertEqual(None, self.cd.get_default_value("item6/value2"))
Jelte Jansen's avatar
Jelte Jansen committed
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299

        # set some local values to something else, and see if we
        # still get the default
        self.cd.set_local_config({"item1": 2, "item6": { "value1": "asdf" } })

        self.assertEqual((2, False), self.cd.get_value("item1"))
        self.assertEqual(1, self.cd.get_default_value("item1"))
        self.assertEqual(('asdf', False), self.cd.get_value("item6/value1"))
        self.assertEqual('default', self.cd.get_default_value("item6/value1"))

        self.assertRaises(isc.cc.data.DataNotFoundError,
                          self.cd.get_default_value,
                          "does_not_exist/value1")
        self.assertRaises(isc.cc.data.DataNotFoundError,
                          self.cd.get_default_value,
                          "item6/doesnotexist")

300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
    def test_set_local_config(self):
        self.cd.set_local_config({"item1": 2})
        value, default = self.cd.get_value("item1")
        self.assertEqual(2, value)
        self.assertEqual(False, default)

    def test_get_local_config(self):
        local_config = self.cd.get_local_config()
        self.assertEqual({}, local_config)
        my_config = { "item1": 2, "item2": 2.2, "item3": False, "item4": "asdf", "item5": [ "c", "d" ] }
        self.cd.set_local_config(my_config)
        self.assertEqual(my_config, self.cd.get_local_config())

    def test_get_item_list(self):
        name_list = self.cd.get_item_list()
315
        self.assertEqual(['item1', 'item2', 'item3', 'item4', 'item5', 'item6'], name_list)
316
        name_list = self.cd.get_item_list("", True)
317
        self.assertEqual(['item1', 'item2', 'item3', 'item4', 'item5', 'item6/value1', 'item6/value2'], name_list)
318
319
320
321
322
        name_list = self.cd.get_item_list("item6", False)
        self.assertEqual(['item6/value1', 'item6/value2'], name_list)

    def test_get_full_config(self):
        full_config = self.cd.get_full_config()
323
        self.assertEqual({ "item1": 1, "item2": 1.1, "item3": True, "item4": "test", "item5": ['a', 'b'], "item6/value1": 'default', 'item6/value2': None}, full_config)
324
325
326
        my_config = { "item1": 2, "item2": 2.2, "item3": False, "item4": "asdf", "item5": [ "c", "d" ] }
        self.cd.set_local_config(my_config)
        full_config = self.cd.get_full_config()
327
        self.assertEqual({ "item1": 2, "item2": 2.2, "item3": False, "item4": "asdf", "item5": [ "c", "d" ], "item6/value1": 'default', 'item6/value2': None}, full_config)
328
329
330
331
332
333
334
335
336
337
338
339
340
341

class TestMultiConfigData(unittest.TestCase):
    def setUp(self):
        if 'CONFIG_TESTDATA_PATH' in os.environ:
            self.data_path = os.environ['CONFIG_TESTDATA_PATH']
        else:
            self.data_path = "../../../testdata"
        self.mcd = MultiConfigData()
        
    def test_init(self):
        self.assertEqual({}, self.mcd._specifications)
        self.assertEqual({}, self.mcd._current_config)
        self.assertEqual({}, self.mcd._local_changes)

342
    def test_set_remove_specification(self):
343
        module_spec = isc.config.module_spec_from_file(self.data_path + os.sep + "spec1.spec")
344
        self.assertFalse(self.mcd.have_specification(module_spec.get_module_name()))
345
        self.mcd.set_specification(module_spec)
346
        self.assertTrue(self.mcd.have_specification(module_spec.get_module_name()))
347
348
        self.assert_(module_spec.get_module_name() in self.mcd._specifications)
        self.assertEquals(module_spec, self.mcd._specifications[module_spec.get_module_name()])
349
        self.assertRaises(ConfigDataError, self.mcd.set_specification, "asdf")
350
351
        self.mcd.remove_specification(module_spec.get_module_name())
        self.assertFalse(self.mcd.have_specification(module_spec.get_module_name()))
352

Jelte Jansen's avatar
Jelte Jansen committed
353
354
355
356
357
358
359
360
361
362
    def test_clear_specifications(self):
        self.assertEqual(0, len(self.mcd._specifications))
        module_spec = isc.config.module_spec_from_file(self.data_path +
                                                       os.sep +
                                                       "spec1.spec")
        self.mcd.set_specification(module_spec)
        self.assertEqual(1, len(self.mcd._specifications))
        self.mcd.clear_specifications()
        self.assertEqual(0, len(self.mcd._specifications))

363
364
365
366
367
368
369
370
371
372
373
    def test_get_module_spec(self):
        module_spec = isc.config.module_spec_from_file(self.data_path + os.sep + "spec1.spec")
        self.mcd.set_specification(module_spec)
        module_spec2 = self.mcd.get_module_spec(module_spec.get_module_name())
        self.assertEqual(module_spec, module_spec2)
        module_spec3 = self.mcd.get_module_spec("no_such_module")
        self.assertEqual(None, module_spec3)

    def test_find_spec_part(self):
        spec_part = self.mcd.find_spec_part("Spec2/item1")
        self.assertEqual(None, spec_part)
374
375
        spec_part = self.mcd.find_spec_part("/Spec2/item1")
        self.assertEqual(None, spec_part)
376
377
378
379
380
        module_spec = isc.config.module_spec_from_file(self.data_path + os.sep + "spec2.spec")
        self.mcd.set_specification(module_spec)
        spec_part = self.mcd.find_spec_part("Spec2/item1")
        self.assertEqual({'item_name': 'item1', 'item_type': 'integer', 'item_optional': False, 'item_default': 1, }, spec_part)

381
382
383
384
385
386
387
388
    def test_find_spec_part_nested(self):
        module_spec = isc.config.module_spec_from_file(self.data_path + os.sep + "spec30.spec")
        self.mcd.set_specification(module_spec)
        spec_part = self.mcd.find_spec_part("/lists/first_list_items[0]/second_list_items[1]/final_element")
        self.assertEqual({'item_name': 'final_element', 'item_type': 'string', 'item_default': 'hello', 'item_optional': False}, spec_part)
        spec_part = self.mcd.find_spec_part("/BAD_NAME/first_list_items[0]/second_list_items[1]/final_element")
        self.assertEqual(None, spec_part)

389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
    def test_find_spec_part_nested2(self):
        module_spec = isc.config.module_spec_from_file(self.data_path + os.sep + "spec31.spec")
        self.mcd.set_specification(module_spec)
        spec_part = self.mcd.find_spec_part("/lists/first_list_items[0]/second_list_items[1]/map_element/list1[1]/list2[2]")
        self.assertEqual({"item_name": "number", "item_type": "integer", "item_optional": False, "item_default": 1}, spec_part)

        spec_part = self.mcd.find_spec_part("/DOESNOTEXIST")
        self.assertEqual(None, spec_part)
        spec_part = self.mcd.find_spec_part("/lists/DOESNOTEXIST")
        self.assertEqual(None, spec_part)
        spec_part = self.mcd.find_spec_part("/lists/first_list_items[0]/DOESNOTEXIST")
        self.assertEqual(None, spec_part)
        spec_part = self.mcd.find_spec_part("/lists/first_list_items[0]/second_list_items[1]/DOESNOTEXIST")
        self.assertEqual(None, spec_part)
        spec_part = self.mcd.find_spec_part("/lists/first_list_items[0]/second_list_items[1]/map_element/DOESNOTEXIST")
        self.assertEqual(None, spec_part)
        spec_part = self.mcd.find_spec_part("/lists/first_list_items[0]/second_list_items[1]/map_element/list1[1]/DOESNOTEXIST")
        self.assertEqual(None, spec_part)
        spec_part = self.mcd.find_spec_part("/lists/first_list_items[0]/second_list_items[1]/map_element/list1[1]/list2[1]/DOESNOTEXIST")
        self.assertEqual(None, spec_part)

410
411
    def test_get_current_config(self):
        cf = { 'module1': { 'item1': 2, 'item2': True } }
412
        self.mcd._set_current_config(cf)
413
414
        self.assertEqual(cf, self.mcd.get_current_config())

415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
    def test_get_local_changes(self):
        module_spec = isc.config.module_spec_from_file(self.data_path + os.sep + "spec2.spec")
        self.mcd.set_specification(module_spec)
        local_changes = self.mcd.get_local_changes()
        self.assertEqual({}, local_changes)
        self.mcd.set_value("Spec2/item1", 2)
        local_changes = self.mcd.get_local_changes()
        self.assertEqual({"Spec2": { "item1": 2}}, local_changes)
        

    def test_clear_local_changes(self):
        module_spec = isc.config.module_spec_from_file(self.data_path + os.sep + "spec2.spec")
        self.mcd.set_specification(module_spec)
        self.mcd.set_value("Spec2/item1", 2)
        self.mcd.clear_local_changes()
        local_changes = self.mcd.get_local_changes()
        self.assertEqual({}, local_changes)
        pass

    def test_get_local_value(self):
435
436
        module_spec = isc.config.module_spec_from_file(self.data_path + os.sep + "spec2.spec")
        self.mcd.set_specification(module_spec)
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
        value = self.mcd.get_local_value("Spec2/item1")
        self.assertEqual(None, value)
        self.mcd.set_value("Spec2/item1", 2)
        value = self.mcd.get_local_value("Spec2/item1")
        self.assertEqual(2, value)

    def test_get_current_value(self):
        value = self.mcd.get_current_value("Spec2/item1")
        self.assertEqual(None, value)
        self.mcd._current_config = { "Spec2": { "item1": 3 } }
        value = self.mcd.get_current_value("Spec2/item1")
        self.assertEqual(3, value)
        pass

    def test_get_default_value(self):
        module_spec = isc.config.module_spec_from_file(self.data_path + os.sep + "spec2.spec")
        self.mcd.set_specification(module_spec)
        value = self.mcd.get_default_value("Spec2/item1")
        self.assertEqual(1, value)
456
457
        value = self.mcd.get_default_value("/Spec2/item1")
        self.assertEqual(1, value)
Jelte Jansen's avatar
Jelte Jansen committed
458
459
        value = self.mcd.get_default_value("Spec2/item5[0]")
        self.assertEqual('a', value)
460
461
        value = self.mcd.get_default_value("Spec2/item5[1]")
        self.assertEqual('b', value)
Jelte Jansen's avatar
Jelte Jansen committed
462
463
464
465
        value = self.mcd.get_default_value("Spec2/item5[5]")
        self.assertEqual(None, value)
        value = self.mcd.get_default_value("Spec2/item5[0][1]")
        self.assertEqual(None, value)
466
467
468
469
470
471
472
        value = self.mcd.get_default_value("Spec2/item6/value1")
        self.assertEqual('default', value)
        value = self.mcd.get_default_value("Spec2/item6/value2")
        self.assertEqual(None, value)
        value = self.mcd.get_default_value("Spec2/no_such_item/asdf")
        self.assertEqual(None, value)

473
474
        module_spec = isc.config.module_spec_from_file(self.data_path + os.sep + "spec32.spec")
        self.mcd.set_specification(module_spec)
475
        value = self.mcd.get_default_value("Spec32/named_set_item")
476
        self.assertEqual({ 'a': 1, 'b': 2}, value)
477
        value = self.mcd.get_default_value("Spec32/named_set_item/a")
478
        self.assertEqual(1, value)
479
        value = self.mcd.get_default_value("Spec32/named_set_item/b")
480
        self.assertEqual(2, value)
481
        value = self.mcd.get_default_value("Spec32/named_set_item/no_such_item")
482
483
        self.assertEqual(None, value)

484
485
486
487
    def test_get_value(self):
        module_spec = isc.config.module_spec_from_file(self.data_path + os.sep + "spec2.spec")
        self.mcd.set_specification(module_spec)
        self.mcd.set_value("Spec2/item1", 2)
488
489

        value, status = self.mcd.get_value("Spec2/item1")
490
491
        self.assertEqual(2, value)
        self.assertEqual(MultiConfigData.LOCAL, status)
492
493

        value, status = self.mcd.get_value("Spec2/item2")
494
495
        self.assertEqual(1.1, value)
        self.assertEqual(MultiConfigData.DEFAULT, status)
496

497
        self.mcd._current_config = { "Spec2": { "item3": False } }
498
499

        value, status = self.mcd.get_value("Spec2/item3")
500
501
        self.assertEqual(False, value)
        self.assertEqual(MultiConfigData.CURRENT, status)
502
503

        value, status = self.mcd.get_value("Spec2/no_such_item")
504
505
506
        self.assertEqual(None, value)
        self.assertEqual(MultiConfigData.NONE, status)

507
508
509
510
        value, status = self.mcd.get_value("Spec2/item5")
        self.assertEqual(['a', 'b'], value)
        self.assertEqual(MultiConfigData.DEFAULT, status)

511
512
513
514
515
516
517
518
        value, status = self.mcd.get_value("Spec2/item5[0]")
        self.assertEqual("a", value)
        self.assertEqual(MultiConfigData.DEFAULT, status)

        value, status = self.mcd.get_value("Spec2/item5[0]", False)
        self.assertEqual(None, value)
        self.assertEqual(MultiConfigData.NONE, status)

519
520
521
522
523
        value, status = self.mcd.get_value("Spec2/item5[1]")
        self.assertEqual("b", value)
        self.assertEqual(MultiConfigData.DEFAULT, status)


524
    def test_get_value_maps(self):
525
526
527
528
529
        maps = self.mcd.get_value_maps()
        self.assertEqual([], maps)
        
        module_spec = isc.config.module_spec_from_file(self.data_path + os.sep + "spec1.spec")
        self.mcd.set_specification(module_spec)
530
531
532
533
534
535
536

        expected = [{'default': False,
                     'type': 'module',
                     'name': 'Spec1',
                     'value': None,
                     'modified': False}]

537
        maps = self.mcd.get_value_maps()
538
539
540
541
542
        self.assertEqual(expected, maps)

        maps = self.mcd.get_value_maps("/")
        self.assertEqual(expected, maps)

543
544
545
546
547
548
549
        maps = self.mcd.get_value_maps('Spec2')
        self.assertEqual([], maps)
        maps = self.mcd.get_value_maps('Spec1')
        self.assertEqual([], maps)
        self.mcd.remove_specification("Spec1")
        self.mcd.remove_specification("foo")
        
550
551
552
553
        module_spec = isc.config.module_spec_from_file(self.data_path + os.sep + "spec2.spec")
        self.mcd.set_specification(module_spec)
        maps = self.mcd.get_value_maps()
        self.assertEqual([{'default': False, 'type': 'module', 'name': 'Spec2', 'value': None, 'modified': False}], maps)
554
555
556
        self.mcd._set_current_config({ "Spec2": { "item1": 2 } })
        self.mcd.set_value("Spec2/item3", False)
        maps = self.mcd.get_value_maps("/Spec2")
557
558
559
560
561
        self.assertEqual([{'default': False, 'type': 'integer', 'name': 'Spec2/item1', 'value': 2, 'modified': False},
                          {'default': True, 'type': 'real', 'name': 'Spec2/item2', 'value': 1.1, 'modified': False},
                          {'default': False, 'type': 'boolean', 'name': 'Spec2/item3', 'value': False, 'modified': True},
                          {'default': True, 'type': 'string', 'name': 'Spec2/item4', 'value': 'test', 'modified': False},
                          {'default': True, 'type': 'list', 'name': 'Spec2/item5', 'value': ['a', 'b'], 'modified': False},
562
563
                          {'default': True, 'type': 'string', 'name': 'Spec2/item6/value1', 'value': 'default', 'modified': False},
                          {'default': False, 'type': 'integer', 'name': 'Spec2/item6/value2', 'value': None, 'modified': False}], maps)
564
        maps = self.mcd.get_value_maps("Spec2")
565
566
567
568
569
        self.assertEqual([{'default': False, 'type': 'integer', 'name': 'Spec2/item1', 'value': 2, 'modified': False},
                          {'default': True, 'type': 'real', 'name': 'Spec2/item2', 'value': 1.1, 'modified': False},
                          {'default': False, 'type': 'boolean', 'name': 'Spec2/item3', 'value': False, 'modified': True},
                          {'default': True, 'type': 'string', 'name': 'Spec2/item4', 'value': 'test', 'modified': False},
                          {'default': True, 'type': 'list', 'name': 'Spec2/item5', 'value': ['a', 'b'], 'modified': False},
570
571
                          {'default': True, 'type': 'string', 'name': 'Spec2/item6/value1', 'value': 'default', 'modified': False},
                          {'default': False, 'type': 'integer', 'name': 'Spec2/item6/value2', 'value': None, 'modified': False}], maps)
572
        maps = self.mcd.get_value_maps("/Spec2/item5")
573
574
        self.assertEqual([{'default': True, 'type': 'string', 'name': 'Spec2/item5[0]', 'value': 'a', 'modified': False},
                          {'default': True, 'type': 'string', 'name': 'Spec2/item5[1]', 'value': 'b', 'modified': False}], maps)
Jelte Jansen's avatar
Jelte Jansen committed
575
        maps = self.mcd.get_value_maps("/Spec2/item5[0]")
576
        self.assertEqual([{'default': True, 'modified': False, 'name': 'Spec2/item5[0]', 'type': 'string', 'value': 'a'}], maps)
577
        maps = self.mcd.get_value_maps("/Spec2/item1")
578
        self.assertEqual([{'default': False, 'type': 'integer', 'name': 'Spec2/item1', 'value': 2, 'modified': False}], maps)
579
        maps = self.mcd.get_value_maps("/Spec2/item2")
580
        self.assertEqual([{'default': True, 'type': 'real', 'name': 'Spec2/item2', 'value': 1.1, 'modified': False}], maps)
581
        maps = self.mcd.get_value_maps("/Spec2/item3")
582
        self.assertEqual([{'default': False, 'type': 'boolean', 'name': 'Spec2/item3', 'value': False, 'modified': True}], maps)
583
        maps = self.mcd.get_value_maps("/Spec2/item4")
584
        self.assertEqual([{'default': True, 'type': 'string', 'name': 'Spec2/item4', 'value': 'test', 'modified': False}], maps)
585

586
587
        module_spec = isc.config.module_spec_from_file(self.data_path + os.sep + "spec24.spec")
        self.mcd.set_specification(module_spec)
588
589
590
591
        # optional list item that is not set should return as empty list
        maps = self.mcd.get_value_maps("/Spec24/item", 4)
        self.assertEqual([{'default': False, 'type': 'list', 'name': 'Spec24/item', 'value': [], 'modified': False}], maps)

592
593
        self.mcd._set_current_config({ "Spec24": { "item": [] } })
        maps = self.mcd.get_value_maps("/Spec24/item")
594
        self.assertEqual([{'default': False, 'modified': False, 'name': 'Spec24/item', 'type': 'list', 'value': []}], maps)
595

596
597
        module_spec = isc.config.module_spec_from_file(self.data_path + os.sep + "spec22.spec")
        self.mcd.set_specification(module_spec)
Jelte Jansen's avatar
Jelte Jansen committed
598
        expected = [{'default': True,
Jelte Jansen's avatar
Jelte Jansen committed
599
                     'modified': False,
600
                     'name': 'Spec22/value9/v91',
Jelte Jansen's avatar
Jelte Jansen committed
601
602
603
604
                     'type': 'string',
                     'value': 'def'},
                    {'default': True,
                     'modified': False,
605
606
607
608
609
610
611
612
613
                     'name': 'Spec22/value9/v92/v92a',
                     'type': 'string',
                     'value': 'Hello'
                    },
                    {'default': True,
                     'modified': False,
                     'name': 'Spec22/value9/v92/v92b',
                     'type': 'integer',
                     'value': 47806
Jelte Jansen's avatar
Jelte Jansen committed
614
615
                    }
                   ]
616
617
        maps = self.mcd.get_value_maps("/Spec22/value9")
        self.assertEqual(expected, maps)
618

619
620
621
        # A slash at the end should not produce different output
        maps = self.mcd.get_value_maps("/Spec22/value9/")
        self.assertEqual(expected, maps)
622

623
624
625
626
627
628
629
630
631
632
        # A slash at the end should not produce different output with
        # indices too
        expected2 = [{'default': True,
                      'type': 'integer',
                      'name': 'Spec22/value5[1]',
                      'value': 'b',
                      'modified': False}]
        maps = self.mcd.get_value_maps("/Spec22/value5[1]/")
        self.assertEqual(expected2, maps)

633
    def test_get_value_maps_named_set(self):
634
635
636
637
638
639
        module_spec = isc.config.module_spec_from_file(self.data_path + os.sep + "spec32.spec")
        self.mcd.set_specification(module_spec)
        maps = self.mcd.get_value_maps()
        self.assertEqual([{'default': False, 'type': 'module',
                           'name': 'Spec32', 'value': None,
                           'modified': False}], maps)
640
        maps = self.mcd.get_value_maps("/Spec32/named_set_item")
641
        self.assertEqual([{'default': True, 'type': 'integer',
642
                           'name': 'Spec32/named_set_item/a',
643
644
                           'value': 1, 'modified': False},
                          {'default': True, 'type': 'integer',
645
                           'name': 'Spec32/named_set_item/b',
646
                           'value': 2, 'modified': False}], maps)
647
        maps = self.mcd.get_value_maps("/Spec32/named_set_item/a")
648
        self.assertEqual([{'default': True, 'type': 'integer',
649
                           'name': 'Spec32/named_set_item/a',
650
                           'value': 1, 'modified': False}], maps)
651
        maps = self.mcd.get_value_maps("/Spec32/named_set_item/b")
652
        self.assertEqual([{'default': True, 'type': 'integer',
653
                           'name': 'Spec32/named_set_item/b',
654
655
                           'value': 2, 'modified': False}], maps)

656
657
658
659
    def test_set_value(self):
        module_spec = isc.config.module_spec_from_file(self.data_path + os.sep + "spec2.spec")
        self.mcd.set_specification(module_spec)
        self.mcd.set_value("Spec2/item1", 2)
660
661
        self.assertRaises(isc.cc.data.DataTypeError,
                          self.mcd.set_value, "Spec2/item1", "asdf")
662

663
664
        self.assertRaises(isc.cc.data.DataNotFoundError,
                          self.mcd.set_value, "Spec2/no_such_item", 4)
665
666
667
668
669
670
671

        self.mcd.set_value("Spec2/item5[0]", "c")
        value, status = self.mcd.get_value("Spec2/item5[0]")
        self.assertEqual(value, "c")
        self.assertEqual(MultiConfigData.LOCAL, status)

        self.assertRaises(isc.cc.data.DataTypeError, self.mcd.set_value, "Spec2/item5[a]", "asdf")
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703


    def test_unset(self):
        """
        Test the unset command works.
        """
        module_spec = isc.config.module_spec_from_file(self.data_path + os.sep + "spec2.spec")
        self.mcd.set_specification(module_spec)
        self.mcd.set_specification(module_spec)
        value, status = self.mcd.get_value("Spec2/item1")
        # This is the default first
        self.assertEqual(1, value)
        self.assertEqual(MultiConfigData.DEFAULT, status)
        # Unseting a default item does nothing.
        self.mcd.unset("Spec2/item1")
        value, status = self.mcd.get_value("Spec2/item1")
        # This should be the default
        self.assertEqual(1, value)
        self.assertEqual(MultiConfigData.DEFAULT, status)
        # Set it to something else
        self.mcd.set_value("Spec2/item1", 42)
        value, status = self.mcd.get_value("Spec2/item1")
        self.assertEqual(42, value)
        self.assertEqual(MultiConfigData.LOCAL, status)
        # Try to unset it
        self.mcd.unset("Spec2/item1")
        value, status = self.mcd.get_value("Spec2/item1")
        # This should be the default
        self.assertEqual(1, value)
        self.assertEqual(MultiConfigData.DEFAULT, status)
        # Unset a nonexisting item. Should raise.
        self.assertRaises(isc.cc.data.DataNotFoundError, self.mcd.unset, "Spec2/doesnotexist")
704
705
706
707
708
709
710
711

    def test_get_config_item_list(self):
        config_items = self.mcd.get_config_item_list()
        self.assertEqual([], config_items)
        module_spec = isc.config.module_spec_from_file(self.data_path + os.sep + "spec2.spec")
        self.mcd.set_specification(module_spec)
        config_items = self.mcd.get_config_item_list()
        self.assertEqual(['Spec2'], config_items)
712
713
714
        config_items = self.mcd.get_config_item_list(None, False)
        self.assertEqual(['Spec2'], config_items)
        config_items = self.mcd.get_config_item_list(None, True)
715
        self.assertEqual(['Spec2/item1', 'Spec2/item2', 'Spec2/item3', 'Spec2/item4', 'Spec2/item5', 'Spec2/item6/value1', 'Spec2/item6/value2'], config_items)
716
        config_items = self.mcd.get_config_item_list("Spec2", True)
717
        self.assertEqual(['Spec2/item1', 'Spec2/item2', 'Spec2/item3', 'Spec2/item4', 'Spec2/item5', 'Spec2/item6/value1', 'Spec2/item6/value2'], config_items)
718
        config_items = self.mcd.get_config_item_list("Spec2")
719
        self.assertEqual(['Spec2/item1', 'Spec2/item2', 'Spec2/item3', 'Spec2/item4', 'Spec2/item5', 'Spec2/item6'], config_items)
Jelte Jansen's avatar
Jelte Jansen committed
720
        config_items = self.mcd.get_config_item_list("/Spec2")
Jelte Jansen's avatar
Jelte Jansen committed
721
        self.assertEqual(['Spec2/item1', 'Spec2/item2', 'Spec2/item3', 'Spec2/item4', 'Spec2/item5', 'Spec2/item6'], config_items)
722
        config_items = self.mcd.get_config_item_list("Spec2", True)
723
        self.assertEqual(['Spec2/item1', 'Spec2/item2', 'Spec2/item3', 'Spec2/item4', 'Spec2/item5', 'Spec2/item6/value1', 'Spec2/item6/value2'], config_items)
724

725
726
727
728
729
    def test_is_named_set(self):
        module_spec = isc.config.module_spec_from_file(self.data_path + os.sep + "spec32.spec")
        self.mcd.set_specification(module_spec)
        spec_part = self.mcd.find_spec_part("Spec32/named_set_item")
        self.assertTrue(spec_part_is_named_set(spec_part))
730

731
    def test_get_config_item_list_named_set(self):
732
733
734
735
736
737
738
739
740
        config_items = self.mcd.get_config_item_list()
        self.assertEqual([], config_items)
        module_spec = isc.config.module_spec_from_file(self.data_path + os.sep + "spec32.spec")
        self.mcd.set_specification(module_spec)
        config_items = self.mcd.get_config_item_list()
        self.assertEqual(['Spec32'], config_items)
        config_items = self.mcd.get_config_item_list(None, False)
        self.assertEqual(['Spec32'], config_items)
        config_items = self.mcd.get_config_item_list(None, True)
741
        self.assertEqual(['Spec32/named_set_item', 'Spec32/named_set_item2'], config_items)
742
743
744
745
746
        self.mcd.set_value('Spec32/named_set_item', { "aaaa": 4, "aabb": 5, "bbbb": 6})
        config_items = self.mcd.get_config_item_list("/Spec32/named_set_item", True)
        self.assertEqual(['Spec32/named_set_item/aaaa',
                          'Spec32/named_set_item/aabb',
                          'Spec32/named_set_item/bbbb',
747
748
                         ], config_items)

749
750
751
752
753
754
755
756
757
758
759
760
761
762
    def test_set_named_set_nonlocal(self):
        # Test whether a default named set is copied to local if a subitem
        # is changed, and that other items in the set do not get lost
        module_spec = isc.config.module_spec_from_file(self.data_path + os.sep + 'spec32.spec')
        self.mcd.set_specification(module_spec)
        value, status = self.mcd.get_value('Spec32/named_set_item')
        self.assertEqual({'a': 1, 'b': 2}, value)
        self.assertEqual(MultiConfigData.DEFAULT, status)

        self.mcd.set_value('Spec32/named_set_item/b', 3)
        value, status = self.mcd.get_value('Spec32/named_set_item')
        self.assertEqual({'a': 1, 'b': 3}, value)
        self.assertEqual(MultiConfigData.LOCAL, status)

763
764
765
if __name__ == '__main__':
    unittest.main()