Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Copyright 2018 Fujitsu Corporation
- #
- # 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 fixtures
- import mock
- from oslotest import base
- import testscenarios
- from oslo_config import cfg
- from oslo_config import fixture as config_fixture
- from oslo_config import generator
- from oslo_config import migrator
- load_tests = testscenarios.load_tests_apply_scenarios
- scenario_1 = {
- 'name': 'scenario 1',
- 'purpose': '''
- This scenario is to migrate old config to new config that there are not
- any option deprecated by new options.
- ''',
- 'old_content_file': '''[DEFAULT]
- foo_default = value_foo
- number1 = 10
- [group1]
- bar_group1 = value_bar
- number2 = 20
- ''',
- 'expected': '''[DEFAULT]
- # Lists configuration groups that provide more details for accessing
- # configuration settings from locations other than local files. (list
- # value)
- config_source =
- # foo option in DEFAULT section (string value)
- foo_default = value_foo
- # number1 option (integer value)
- number1 = 10
- [group1]
- # bar option in group1 section (string value)
- bar_group1 = value_bar
- # number2 option (integer value)
- number2 = 20
- '''
- }
- scenario_2 = {
- 'name': 'scenario 2',
- 'purpose': '''
- "deprecated_coo_default" option in DEFAULT section which will be
- deprecated in new release and it is replaced by "coo_default" in the same
- section.
- ''',
- 'old_content_file': '''[DEFAULT]
- deprecated_coo_default = value_foo
- number1 = 10
- [group1]
- bar_group1 = value_bar
- number2 = 20
- ''',
- 'expected': '''[DEFAULT]
- # Lists configuration groups that provide more details for accessing
- # configuration settings from locations other than local files. (list
- # value)
- config_source =
- # This option is to replace deprecated_coo_default option. (string
- # value)
- # Deprecated group/name - [DEFAULT]/deprecated_coo_default
- coo_default = value_foo
- # number1 option (integer value)
- number1 = 10
- [group1]
- # bar option in group1 section (string value)
- bar_group1 = value_bar
- # number2 option (integer value)
- number2 = 20
- '''
- }
- scenario_3 = {
- 'name': 'scenario 3',
- 'purpose': '''
- "deprecated_coo_default" option in DEFAULT section which will be
- deprecated in new release and it is replaced by "coo_default" in the same
- section.
- "deprecated_number4" in group1 section will be deprecated in new replease and
- "number4" is to replace it.
- ''',
- 'old_content_file': '''[DEFAULT]
- deprecated_coo_default = value_foo
- number1 = 10
- [group1]
- bar_group1 = value_bar
- deprecated_number4 = 20
- ''',
- 'expected': '''[DEFAULT]
- # Lists configuration groups that provide more details for accessing
- # configuration settings from locations other than local files. (list
- # value)
- config_source =
- # This option is to replace deprecated_coo_default option. (string
- # value)
- # Deprecated group/name - [DEFAULT]/deprecated_coo_default
- coo_default = value_foo
- # number1 option (integer value)
- number1 = 10
- # This option will be replaced in the next cycle. (integer value)
- # Deprecated group/name - [group1]/deprecated_number4
- number4 = 20
- [group1]
- # bar option in group1 section (string value)
- bar_group1 = value_bar
- '''
- }
- scenario_4 = {
- 'name': 'scenario 4',
- 'purpose': '''
- "deprecated_choices_opt" in group1 section will be deprecated in new
- release by "choices_opt", and its value will be updated as well.
- "a" --> 'a_update",
- "b" --> "b_update",
- "c" --> "c_update".
- ''',
- 'old_content_file': '''[DEFAULT]
- [group1]
- deprecated_choices_opt = a
- ''',
- 'expected': '''[DEFAULT]
- # Lists configuration groups that provide more details for accessing
- # configuration settings from locations other than local files. (list
- # value)
- config_source =
- [group2]
- # This option is to replace deprecated_choices_opt (string value)
- # Possible values:
- # a_update - <No description provided>
- # b - <No description provided>
- # c_update - <No description provided>
- # Deprecated group/name - [group1]/deprecated_choices_opt
- choices_opt = a_update
- '''
- }
- scenario_5 = {
- 'name': 'scenario 5',
- 'purpose': '''
- In case, users configure options wrong then they need to be checked and write
- the configuration file with #.
- Some wrong options including:
- - wrong_option in group2
- - wrong_wrong in group4
- ''',
- 'old_content_file': '''[DEFAULT]
- [group2]
- multi_opt = 10
- multi_opt = 20
- wrong_option = foo
- [group4]
- wrong_wrong = bar
- ''',
- 'expected': '''[DEFAULT]
- # Lists configuration groups that provide more details for accessing
- # configuration settings from locations other than local files. (list
- # value)
- config_source =
- [group2]
- # multiple strings (multi valued)
- multi_opt = 10
- multi_opt = 20
- '''
- }
- scenario_6 = {
- 'name': 'scenario 6',
- 'purpose': '''
- This scenario includes testing the following cases: deprecated option,
- change value option and wrong option.
- ''',
- 'old_content_file': '''[DEFAULT]
- deprecated_coo_default = value_foo
- [group1]
- deprecated_number4 = 20
- number1 = 1
- deprecated_choices_opt = b
- wrong = vanduc
- ''',
- 'expected': '''[DEFAULT]
- # This option is to replace deprecated_choices_opt (string value)
- # Possible values:
- # a_update - <No description provided>
- # b - <No description provided>
- # c_update - <No description provided>
- # Deprecated group/name - [group1]/deprecated_choices_opt
- choices_opt = b
- # Lists configuration groups that provide more details for accessing
- # configuration settings from locations other than local files. (list
- # value)
- config_source =
- # This option is to replace deprecated_coo_default option. (string
- # value)
- # Deprecated group/name - [DEFAULT]/deprecated_coo_default
- coo_default = value_foo
- [group1]
- # number1 option (integer value)
- number1 = 1
- [group2]
- # This option will be replaced in the next cycle. (integer value)
- # Deprecated group/name - [group1]/deprecated_number4
- number4 = 20
- '''
- }
- scenario_7 = {
- 'name': 'scenario 7',
- 'purpose': '''
- This scenario is to migrate old config to new config that use multiple
- namespaces
- ''',
- 'old_content_file': '''[DEFAULT]
- deprecated_coo_default = value_foo
- port_opt = 80
- [group1]
- deprecated_number4 = 20
- number1 = 1
- deprecated_choices_opt = b
- wrong = xyz
- ''',
- 'expected': '''[DEFAULT]
- # This option is to replace deprecated_choices_opt (string value)
- # Possible values:
- # a_update - <No description provided>
- # b - <No description provided>
- # c_update - <No description provided>
- # Deprecated group/name - [group1]/deprecated_choices_opt
- choices_opt = b
- # Lists configuration groups that provide more details for accessing
- # configuration settings from locations other than local files. (list
- # value)
- config_source =
- # This option is to replace deprecated_coo_default option. (string
- # value)
- # Deprecated group/name - [DEFAULT]/deprecated_coo_default
- coo_default = value_foo
- # a port (port value)
- # Minimum value: 0
- # Maximum value: 65535
- port_opt = 80
- [group1]
- # number1 option (integer value)
- number1 = 1
- [group2]
- # This option will be replaced in the next cycle. (integer value)
- # Deprecated group/name - [group1]/deprecated_number4
- number4 = 20
- '''
- }
- scenario_8 = {
- 'name': 'scenario 8',
- 'purpose': '''
- Remove options that are not in any namespace that user input.
- ''',
- 'old_content_file': '''[DEFAULT]
- deprecated_coo_default = value_foo
- port_opt = 80
- ''',
- 'expected': '''\
- [DEFAULT]
- # Lists configuration groups that provide more details for accessing
- # configuration settings from locations other than local files. (list
- # value)
- config_source =
- '''
- }
- scenario_9 = {
- 'name': 'scenario 9',
- 'purpose': '''
- This scenario is to test name option with '-' instead '_'
- Ex: cfg.StrOpt('log-file')
- ''',
- 'old_content_file': '''[DEFAULT]
- deprecated_coo_default = value_foo
- log_file = /var/log/
- [group1]
- deprecated_number4 = 20
- number1 = 1
- wrong = xyz
- ''',
- 'expected': '''[DEFAULT]
- # Lists configuration groups that provide more details for accessing
- # configuration settings from locations other than local files. (list
- # value)
- config_source =
- # This option is to replace deprecated_coo_default option. (string
- # value)
- # Deprecated group/name - [DEFAULT]/deprecated_coo_default
- coo_default = value_foo
- [group1]
- # log file (string value)
- # Deprecated group/name - [DEFAULT]/log_file
- log_file = /var/log/
- # number1 option (integer value)
- number1 = 1
- [group2]
- # This option will be replaced in the next cycle. (integer value)
- # Deprecated group/name - [group1]/deprecated_number4
- number4 = 20
- '''
- }
- scenario_10 = {
- 'name': 'scenario 10',
- 'purpose': '''
- This scenario includes testing the following cases: deprecated option,
- change value option and wrong option.
- ''',
- 'old_content_file': '''[DEFAULT]
- deprecated_coo_default = value_foo
- choices_opt1 = a
- notification_driver = noop
- notification_driver = log
- notification_driver = test
- deprecated_list_opt = 1,2,3
- [group1]
- deprecated_number4 = 20
- number1 = 1
- wrong = vanduc
- ''',
- 'expected': '''[DEFAULT]
- # a choice opt (string value)
- # Possible values:
- # a_update - <No description provided>
- # b - <No description provided>
- # c_update - <No description provided>
- choices_opt1 = a_update
- # Lists configuration groups that provide more details for accessing
- # configuration settings from locations other than local files. (list
- # value)
- config_source =
- # This option is to replace deprecated_coo_default option. (string
- # value)
- # Deprecated group/name - [DEFAULT]/deprecated_coo_default
- coo_default = value_foo
- # a list (list value)
- # Deprecated group/name - [DEFAULT]/deprecated_list_opt
- list_opt = 1,2,3
- [group1]
- # The Drivers(s) to handle sending notifications. Possible values are
- # messaging, messagingv2, routing, log, test, noop (multi valued)
- # Deprecated group/name - [DEFAULT]/notification_driver
- driver = noop
- driver = log
- driver = test
- # number1 option (integer value)
- number1 = 1
- [group2]
- # This option will be replaced in the next cycle. (integer value)
- # Deprecated group/name - [group1]/deprecated_number4
- number4 = 20
- '''
- }
- class MigratorTestCase(base.BaseTestCase):
- groups = {
- 'group1': cfg.OptGroup(name='group1', title='Group 1'),
- 'group2': cfg.OptGroup(name='group2', title='Group 2'),
- 'group3': cfg.OptGroup(name='group3', title='Group 3',
- help='Group3 group'),
- }
- opts = {
- 'foo_default': cfg.StrOpt('foo_default', help='foo option in DEFAULT '
- 'section'),
- 'bar_group1': cfg.StrOpt('bar_group1', help='bar option in group1 '
- 'section'),
- 'kaka_group2': cfg.StrOpt('kaka_group2', help='kaka option in group2 '
- 'section'),
- 'foo-bar': cfg.StrOpt('foo-bar', help='foobar'),
- 'number1': cfg.IntOpt('number1', default=10, help='number1 option'),
- 'number2': cfg.IntOpt('number2', default=20, help='number2 option'),
- 'number3': cfg.IntOpt('number3', help='This option is to replace '
- 'deprecated_number3'),
- 'bool_opt': cfg.BoolOpt('bool_opt',
- default=False,
- help='a boolean'),
- 'list_opt': cfg.ListOpt('list_opt',
- deprecated_name='deprecated_list_opt',
- default=['1', '2', '3'],
- help='a list'),
- 'deprecated_list_opt': cfg.ListOpt('deprecated_list_opt',
- default=['1', '2', '3'],
- help='a list'),
- 'dict_opt': cfg.DictOpt('dict_opt',
- default={'1': 'yes', '2': 'no'},
- help='a dict'),
- 'port_opt': cfg.PortOpt('port_opt',
- default=80,
- help='a port'),
- 'hostname_opt': cfg.HostnameOpt('hostname_opt',
- default='compute01.nova.site1',
- help='a hostname'),
- 'uri_opt': cfg.URIOpt('uri_opt',
- default='http://example.com',
- help='a URI'),
- 'multi_opt': cfg.MultiStrOpt('multi_opt',
- default=['1', '2', '3'],
- help='multiple strings'),
- 'coo_default': cfg.StrOpt('coo_default',
- deprecated_name='deprecated_coo_default',
- help='This option is to replace '
- 'deprecated_coo_default option.'),
- 'deprecated_coo_default': cfg.StrOpt('deprecated_coo_default',
- help='This option will be '
- 'replaced in the next '
- 'cycle.'),
- 'number4': cfg.IntOpt('number4',
- deprecated_name='deprecated_number4',
- deprecated_group='group1',
- help='This option will be replaced '
- 'in the next cycle.'),
- 'deprecated_number4': cfg.IntOpt('deprecated_number4',
- help='This option is to replace '
- 'number4 in group1 section.'),
- 'choices_opt': cfg.StrOpt('choices_opt',
- deprecated_name='deprecated_choices_opt',
- deprecated_group='group1',
- choices=('a_update', 'b', 'c_update'),
- upgrade_value={'a': 'a_update',
- 'c': 'c_update'},
- help='This option is to replace '
- 'deprecated_choices_opt'),
- 'deprecated_choices_opt': cfg.StrOpt('deprecated_choices_opt',
- choices=('a', 'b', 'c'),
- help='This option will be '
- 'replaced in the next '
- 'cycle.'),
- 'choices-opt1': cfg.StrOpt('choices-opt1',
- choices=('a_update', 'b', 'c_update'),
- upgrade_value={'a': 'a_update',
- 'c': 'c_update'},
- help='a choice opt'),
- 'log-file': cfg.StrOpt('log-file',
- deprecated_name='log-file',
- deprecated_group='DEFAULT',
- help='log file'),
- 'driver': cfg.MultiStrOpt('driver',
- default=[],
- deprecated_name='notification_driver',
- deprecated_group='DEFAULT',
- help='The Drivers(s) to handle sending notifications. '
- 'Possible values are messaging, messagingv2, '
- 'routing, log, test, noop'),
- 'notification_driver': cfg.MultiStrOpt('notification_driver',
- default=[],
- help='The Drivers(s) to handle sending notifications. '
- 'Possible values are messaging, messagingv2, '
- 'routing, log, test, noop'),
- }
- content_scenarios = [
- (scenario_1['name'], {
- 'name_scenario': scenario_1['name'],
- 'opts': [
- ('namespace1', [(None, [opts['foo_default'],
- opts['number1']]),
- (groups['group1'], [opts['bar_group1'],
- opts['number2']])])],
- 'expected': scenario_1['expected'],
- 'old_content_file': scenario_1['old_content_file']}),
- (scenario_2['name'], {
- 'name_scenario': scenario_2['name'],
- 'opts': [
- ('namespace1', [(None, [opts['coo_default'],
- opts['number1']]),
- (groups['group1'], [opts['bar_group1'],
- opts['number2']])])],
- 'expected': scenario_2['expected'],
- 'old_content_file': scenario_2['old_content_file']}),
- (scenario_3['name'], {
- 'name_scenario': scenario_3['name'],
- 'opts': [
- ('namespace1', [(None, [opts['coo_default'],
- opts['number1'],
- opts['number4']]),
- (groups['group1'], [opts['bar_group1']])])],
- 'expected': scenario_3['expected'],
- 'old_content_file': scenario_3['old_content_file']}),
- (scenario_4['name'], {
- 'name_scenario': scenario_4['name'],
- 'opts': [
- ('namespace1', [(groups['group2'],
- [opts['choices_opt']])])],
- 'expected': scenario_4['expected'],
- 'old_content_file': scenario_4['old_content_file']}),
- (scenario_5['name'], {
- 'name_scenario': scenario_5['name'],
- 'opts': [
- ('namespace1', [(groups['group2'], [opts['multi_opt']])])],
- 'expected': scenario_5['expected'],
- 'old_content_file': scenario_5['old_content_file'],
- 'log_warning': True}),
- (scenario_6['name'], {
- 'name_scenario': scenario_6['name'],
- 'opts': [
- ('namespace1', [(None, [opts['coo_default'],
- opts['choices_opt']]),
- (groups['group1'], [opts['number1']]),
- (groups['group2'], [opts['number4']]),
- (groups['group3'], [opts['number2']]), ])],
- 'expected': scenario_6['expected'],
- 'old_content_file': scenario_6['old_content_file'],
- 'log_warning': True}),
- (scenario_7['name'], {
- 'name_scenario': scenario_7['name'],
- 'opts': [
- ('namespace1', [(None, [opts['coo_default'],
- opts['choices_opt']]),
- (groups['group1'], [opts['number1']]), ]),
- ('namespace2', [(None, [opts['port_opt']]),
- (groups['group2'], [opts['number4']]), ])],
- 'expected': scenario_7['expected'],
- 'old_content_file': scenario_7['old_content_file'],
- 'log_warning': True}),
- (scenario_8['name'], {
- 'name_scenario': scenario_8['name'],
- 'opts': [
- ('namespace1', [(None, [opts['uri_opt'],
- opts['hostname_opt']])]),
- ('namespace2', [(groups['group2'], [opts['bool_opt']]), ])],
- 'expected': scenario_8['expected'],
- 'old_content_file': scenario_8['old_content_file'],
- 'log_warning': True}),
- (scenario_9['name'], {
- 'name_scenario': scenario_9['name'],
- 'opts': [
- ('namespace1', [(None, [opts['coo_default']]),
- (groups['group1'], [opts['number1']]), ]),
- ('namespace2', [(groups['group1'], [opts['log-file']]),
- (groups['group2'], [opts['number4']]), ])],
- 'expected': scenario_9['expected'],
- 'old_content_file': scenario_9['old_content_file'],
- 'log_warning': True}),
- (scenario_10['name'], {
- 'name_scenario': scenario_10['name'],
- 'opts': [
- ('namespace1', [(None, [opts['coo_default'],
- opts['choices-opt1'],
- opts['list_opt']]),
- (groups['group1'], [opts['number1'],
- opts['driver']]),
- (groups['group2'], [opts['number4']]),
- (groups['group3'], [opts['number2']]), ])],
- 'expected': scenario_10['expected'],
- 'old_content_file': scenario_10['old_content_file'],
- 'log_warning': True}),
- ]
- output_file_scenarios = [
- ('output_file',
- {'output_file': 'new_configuration.conf',
- 'old_config_file': 'old_configuration.conf'}),
- ]
- def setUp(self):
- super(MigratorTestCase, self).setUp()
- self.conf = cfg.ConfigOpts()
- self.config_fixture = config_fixture.Config(self.conf)
- self.config = self.config_fixture.config
- self.useFixture(self.config_fixture)
- self.tempdir = self.useFixture(fixtures.TempDir())
- @classmethod
- def generate_scenarios(cls):
- cls.scenarios = testscenarios.multiply_scenarios(
- cls.content_scenarios,
- cls.output_file_scenarios)
- @mock.patch.object(generator, '_get_raw_opts_loaders')
- @mock.patch.object(migrator, 'LOG')
- def test_migrator(self, mock_log, raw_opts_loader):
- migrator.register_cli_opts(self.conf)
- # Writing old configuration file
- old_config_file = self.tempdir.join(self.old_config_file)
- with open(old_config_file, 'w') as f:
- f.write(self.old_content_file)
- self.config(input_file=old_config_file)
- namespaces = [i[0] for i in self.opts]
- self.config(namespace=namespaces)
- for group in self.groups.values():
- self.conf.register_group(group)
- wrap_width = getattr(self, 'wrap_width', None)
- if wrap_width is not None:
- self.config(wrap_width=wrap_width)
- output_file = self.tempdir.join(self.output_file)
- self.config(output_file=output_file)
- raw_opts_loader.return_value = [
- (ns, lambda opts=opts: opts)
- for ns, opts in self.opts
- ]
- migrator.migrator(self.conf)
- log_warning = getattr(self, 'log_warning', False)
- if log_warning:
- mock_log.warning.assert_called()
- else:
- mock_log.warning.assert_not_called()
- with open(output_file, 'r') as f:
- actual = f.read()
- self.assertEqual(self.expected, actual)
- MigratorTestCase.generate_scenarios()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement