summaryrefslogtreecommitdiff
path: root/nova/tests/unit/compute/test_provider_config.py
blob: 384d4650542b03a4ce9438d7cdf8a70f05e95b64 (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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

import copy
import ddt
import fixtures
import importlib.metadata
import microversion_parse
import os
from unittest import mock

from oslo_utils.fixture import uuidsentinel
from oslotest import base
from packaging import version

from nova.compute import provider_config
from nova import exception as nova_exc


class SchemaValidationMixin(base.BaseTestCase):
    """This class provides the basic methods for running schema validation test
    cases. It can be used along with ddt.file_data to test a specific schema
    version using tests defined in yaml files. See SchemaValidationTestCasesV1
    for an example of how this was done for schema version 1.

    Because decorators can only access class properties of the class they are
    defined in (even when overriding values in the subclass), the decorators
    need to be placed in the subclass. This is why there are test_ functions in
    the subclass that call the run_test_ methods in this class. This should
    keep things simple as more schema versions are added.
    """

    def setUp(self):
        super(SchemaValidationMixin, self).setUp()
        self.mock_load_yaml = self.useFixture(
            fixtures.MockPatchObject(
                provider_config, '_load_yaml_file')).mock
        self.mock_LOG = self.useFixture(
            fixtures.MockPatchObject(
                provider_config, 'LOG')).mock

    def set_config(self, config=None):
        data = config or {}
        self.mock_load_yaml.return_value = data
        return data

    def run_test_validation_errors(self, config, expected_messages):
        self.set_config(config=config)

        actual_msg = self.assertRaises(
            nova_exc.ProviderConfigException,
            provider_config._parse_provider_yaml, 'test_path').message

        for msg in expected_messages:
            self.assertIn(msg, actual_msg)

    def run_test_validation_success(self, config):
        reference = self.set_config(config=config)

        actual = provider_config._parse_provider_yaml('test_path')

        self.assertEqual(reference, actual)

    def run_schema_version_matching(
            self, min_schema_version, max_schema_version):
        # note _load_yaml_file is mocked so the value is not important
        # however it may appear in logs messages so changing it could
        # result in tests failing unless the expected_messages field
        # is updated in the test data.
        path = 'test_path'

        # test exactly min and max versions are supported
        self.set_config(config={
            'meta': {'schema_version': str(min_schema_version)}})
        provider_config._parse_provider_yaml(path)
        self.set_config(config={
            'meta': {'schema_version': str(max_schema_version)}})
        provider_config._parse_provider_yaml(path)

        self.mock_LOG.warning.assert_not_called()

        # test max major+1 raises
        higher_major = microversion_parse.Version(
            major=max_schema_version.major + 1, minor=max_schema_version.minor)
        self.set_config(config={'meta': {'schema_version': str(higher_major)}})

        self.assertRaises(nova_exc.ProviderConfigException,
                          provider_config._parse_provider_yaml, path)

        # test max major with max minor+1 is logged
        higher_minor = microversion_parse.Version(
            major=max_schema_version.major, minor=max_schema_version.minor + 1)
        expected_log_call = (
            "Provider config file [%(path)s] is at schema version "
            "%(schema_version)s. Nova supports the major version, but "
            "not the minor. Some fields may be ignored." % {
            "path": path, "schema_version": higher_minor})
        self.set_config(config={'meta': {'schema_version': str(higher_minor)}})

        provider_config._parse_provider_yaml(path)

        self.mock_LOG.warning.assert_called_once_with(expected_log_call)


@ddt.ddt
class SchemaValidationTestCasesV1(SchemaValidationMixin):
    MIN_SCHEMA_VERSION = microversion_parse.Version(1, 0)
    MAX_SCHEMA_VERSION = microversion_parse.Version(1, 0)

    @ddt.unpack
    @ddt.file_data('provider_config_data/v1/validation_error_test_data.yaml')
    def test_validation_errors(self, config, expected_messages):
        # TODO(stephenfin): Drop this once we no longer support jsonschema 3.x
        jsonschema_version = importlib.metadata.version('jsonschema')
        if version.parse(jsonschema_version) < version.parse('4.0.0'):
            if expected_messages == [
                "should not be valid under {}",
                "validating 'not' in schema['properties']['__source_file']",
            ]:
                expected_messages = [
                    "{} is not allowed for",
                    "validating 'not' in schema['properties']['__source_file']",  # noqa: E501
                ]
        self.run_test_validation_errors(config, expected_messages)

    @ddt.unpack
    @ddt.file_data('provider_config_data/v1/validation_success_test_data.yaml')
    def test_validation_success(self, config):
        self.run_test_validation_success(config)

    def test_schema_version_matching(self):
        self.run_schema_version_matching(self.MIN_SCHEMA_VERSION,
                                         self.MAX_SCHEMA_VERSION)


@ddt.ddt
class ValidateProviderConfigTestCases(base.BaseTestCase):
    @ddt.unpack
    @ddt.file_data('provider_config_data/validate_provider_good_config.yaml')
    def test__validate_provider_good_config(self, sample):
        provider_config._validate_provider_config(sample, "fake_path")

    @ddt.unpack
    @ddt.file_data('provider_config_data/validate_provider_bad_config.yaml')
    def test__validate_provider_bad_config(self, sample, expected_messages):
        actual_msg = self.assertRaises(
            nova_exc.ProviderConfigException,
            provider_config._validate_provider_config,
            sample, 'fake_path').message

        self.assertIn(actual_msg, expected_messages)

    @mock.patch.object(provider_config, 'LOG')
    def test__validate_provider_config_one_noop_provider(self, mock_log):
        expected = {
            "providers": [
                {
                    "identification": {"name": "NAME1"},
                    "inventories": {
                        "additional": [
                            {"CUSTOM_RESOURCE_CLASS": {}}
                        ]
                    }
                },
                {
                    "identification": {"name": "NAME_453764"},
                    "inventories": {
                        "additional": []
                    },
                    "traits": {
                        "additional": []
                    }
                }
            ]
        }
        data = copy.deepcopy(expected)

        valid = provider_config._validate_provider_config(data, "fake_path")

        mock_log.warning.assert_called_once_with(
            "Provider NAME_453764 defined in "
            "fake_path has no additional "
            "inventories or traits and will be ignored."
        )
        # assert that _validate_provider_config does not mutate inputs
        self.assertEqual(expected, data)
        # assert that the first entry in the returned tuple is the full set
        # of providers not a copy and is equal to the expected providers.
        self.assertIs(data['providers'][0], valid[0])
        self.assertEqual(expected['providers'][0], valid[0])


class GetProviderConfigsTestCases(base.BaseTestCase):
    @mock.patch.object(provider_config, 'glob')
    def test_get_provider_configs_one_file(self, mock_glob):
        expected = {
            "$COMPUTE_NODE": {
                "__source_file": "example_provider.yaml",
                "identification": {
                    "name": "$COMPUTE_NODE"
                },
                "inventories": {
                    "additional": [
                        {
                            "CUSTOM_EXAMPLE_RESOURCE_CLASS": {
                                "total": 100,
                                "reserved": 0,
                                "min_unit": 1,
                                "max_unit": 10,
                                "step_size": 1,
                                "allocation_ratio": 1.0
                            }
                        }
                    ]
                },
                "traits": {
                    "additional": [
                        "CUSTOM_TRAIT_ONE",
                        "CUSTOM_TRAIT2"
                    ]
                }
            }
        }

        example_file = os.path.join(
            os.path.dirname(os.path.realpath(__file__)),
            'provider_config_data/v1/example_provider.yaml')
        mock_glob.glob.return_value = [example_file]

        actual = provider_config.get_provider_configs('path')

        self.assertEqual(expected, actual)
        mock_glob.glob.assert_called_with('path/*.yaml')

    @mock.patch.object(provider_config, 'glob')
    @mock.patch.object(provider_config, '_parse_provider_yaml')
    def test_get_provider_configs_one_file_uuid_conflict(
            self, mock_parser, mock_glob):
        # one config file with conflicting identification
        providers = [
            {"__source_file": "file1.yaml",
             "identification": {
                 "uuid": uuidsentinel.uuid1
             },
             "inventories": {
                 "additional": [
                     {
                         "CUSTOM_EXAMPLE_RESOURCE_CLASS1": {
                             "total": 100,
                             "reserved": 0,
                             "min_unit": 1,
                             "max_unit": 10,
                             "step_size": 1,
                             "allocation_ratio": 1
                         }
                     }
                 ]
             },
             "traits": {
                 "additional": [
                     "CUSTOM_TRAIT1"
                 ]
             }
             },
            {"__source_file": "file1.yaml",
             "identification": {
                 "uuid": uuidsentinel.uuid1
             },
             "inventories": {
                 "additional": [
                     {
                         "CUSTOM_EXAMPLE_RESOURCE_CLASS2": {
                             "total": 100,
                             "reserved": 0,
                             "min_unit": 1,
                             "max_unit": 10,
                             "step_size": 1,
                             "allocation_ratio": 1
                         }
                     }
                 ]
             },
             "traits": {
                 "additional": [
                     "CUSTOM_TRAIT2"
                 ]
             }
             }
        ]
        mock_parser.side_effect = [{"providers": providers}]
        mock_glob.glob.return_value = ['file1.yaml']

        # test that correct error is raised and message matches
        error = self.assertRaises(nova_exc.ProviderConfigException,
                                  provider_config.get_provider_configs,
                                  'dummy_path').kwargs['error']
        self.assertEqual("Provider %s has multiple definitions in source "
                         "file(s): ['file1.yaml']." % uuidsentinel.uuid1,
                         error)

    @mock.patch.object(provider_config, 'glob')
    @mock.patch.object(provider_config, '_parse_provider_yaml')
    def test_get_provider_configs_two_files(self, mock_parser, mock_glob):
        expected = {
            "EXAMPLE_RESOURCE_PROVIDER1": {
                "__source_file": "file1.yaml",
                "identification": {
                    "name": "EXAMPLE_RESOURCE_PROVIDER1"
                },
                "inventories": {
                    "additional": [
                        {
                            "CUSTOM_EXAMPLE_RESOURCE_CLASS1": {
                                "total": 100,
                                "reserved": 0,
                                "min_unit": 1,
                                "max_unit": 10,
                                "step_size": 1,
                                "allocation_ratio": 1
                            }
                        }
                    ]
                },
                "traits": {
                    "additional": [
                        "CUSTOM_TRAIT1"
                    ]
                }
            },
            "EXAMPLE_RESOURCE_PROVIDER2": {
                "__source_file": "file2.yaml",
                "identification": {
                    "name": "EXAMPLE_RESOURCE_PROVIDER2"
                },
                "inventories": {
                    "additional": [
                        {
                            "CUSTOM_EXAMPLE_RESOURCE_CLASS2": {
                                "total": 100,
                                "reserved": 0,
                                "min_unit": 1,
                                "max_unit": 10,
                                "step_size": 1,
                                "allocation_ratio": 1
                            }
                        }
                    ]
                },
                "traits": {
                    "additional": [
                        "CUSTOM_TRAIT2"
                    ]
                }
            }
        }
        mock_parser.side_effect = [
            {"providers": [provider]} for provider in expected.values()]
        mock_glob_return = ['file1.yaml', 'file2.yaml']
        mock_glob.glob.return_value = mock_glob_return
        dummy_path = 'dummy_path'

        actual = provider_config.get_provider_configs(dummy_path)

        mock_glob.glob.assert_called_once_with(os.path.join(dummy_path,
                                                            '*.yaml'))
        mock_parser.assert_has_calls([mock.call(param)
                                      for param in mock_glob_return])
        self.assertEqual(expected, actual)

    @mock.patch.object(provider_config, 'glob')
    @mock.patch.object(provider_config, '_parse_provider_yaml')
    def test_get_provider_configs_two_files_name_conflict(self, mock_parser,
                                                          mock_glob):
        # two config files with conflicting identification
        configs = {
            "EXAMPLE_RESOURCE_PROVIDER1": {
                "__source_file": "file1.yaml",
                "identification": {
                    "name": "EXAMPLE_RESOURCE_PROVIDER1"
                },
                "inventories": {
                    "additional": [
                        {
                            "CUSTOM_EXAMPLE_RESOURCE_CLASS1": {
                                "total": 100,
                                "reserved": 0,
                                "min_unit": 1,
                                "max_unit": 10,
                                "step_size": 1,
                                "allocation_ratio": 1
                            }
                        }
                    ]
                },
                "traits": {
                    "additional": [
                        "CUSTOM_TRAIT1"
                    ]
                }
            },
            "EXAMPLE_RESOURCE_PROVIDER2": {
                "__source_file": "file2.yaml",
                "identification": {
                    "name": "EXAMPLE_RESOURCE_PROVIDER1"
                },
                "inventories": {
                    "additional": [
                        {
                            "CUSTOM_EXAMPLE_RESOURCE_CLASS1": {
                                "total": 100,
                                "reserved": 0,
                                "min_unit": 1,
                                "max_unit": 10,
                                "step_size": 1,
                                "allocation_ratio": 1
                            }
                        }
                    ]
                },
                "traits": {
                    "additional": [
                        "CUSTOM_TRAIT1"
                    ]
                }
            }
        }
        mock_parser.side_effect = [{"providers": [configs[provider]]}
                                   for provider in configs]
        mock_glob.glob.return_value = ['file1.yaml', 'file2.yaml']

        # test that correct error is raised and message matches
        error = self.assertRaises(nova_exc.ProviderConfigException,
                                  provider_config.get_provider_configs,
                                  'dummy_path').kwargs['error']
        self.assertEqual("Provider EXAMPLE_RESOURCE_PROVIDER1 has multiple "
                         "definitions in source file(s): "
                         "['file1.yaml', 'file2.yaml'].", error)

    @mock.patch.object(provider_config, 'LOG')
    def test_get_provider_configs_no_configs(self, mock_log):
        path = "invalid_path!@#"
        actual = provider_config.get_provider_configs(path)

        self.assertEqual({}, actual)
        mock_log.info.assert_called_once_with(
            "No provider configs found in %s. If files are present, "
            "ensure the Nova process has access.", path)