def test_parsing_different_command_styles(self): # test ensures that options sent to load_oslo_configuration with # different command styles generates the same output cmd_1 = ['python', 'test', '--config-dir', '.'] cmd_2 = ['python', 'test', '--config-dir=.'] opts = [ {'opt': cfg.StrOpt('region_name')} ] cfg_1 = utils.load_oslo_configuration( from_cmd=cmd_1, in_project=self.PROJECT, of_prog=self.PROG, for_opts=opts ) cfg_2 = utils.load_oslo_configuration( from_cmd=cmd_2, in_project=self.PROJECT, of_prog=self.PROG, for_opts=opts ) self.assertIsNot(cfg_1, cfg_2) self.assertEqual(cfg_1, cfg_2)
def test_just_keep_built_in_options(self): # test ensures that non built-in oslo.config options to # load_oslo_configuration is skipped cmd_1 = ['python', 'test'] cmd_2 = ['python', 'test', '--log-file', '/var/log/test/test.log'] opts = [ {'opt': cfg.StrOpt('region_name')} ] cfg_1 = utils.load_oslo_configuration( from_cmd=cmd_1, in_project=self.PROJECT, of_prog=self.PROG, for_opts=opts ) cfg_2 = utils.load_oslo_configuration( from_cmd=cmd_2, in_project=self.PROJECT, of_prog=self.PROG, for_opts=opts ) self.assertIsNot(cfg_1, cfg_2) self.assertEqual(cfg_1, cfg_2)
def test_distinct_oslo_confs_should_contain_different_opts(self): # test ensures that each instance created via load_oslo_configuration # contains different values of the same opts file_1, file_2 = self.create_tempfiles([('', ''), ('', '')]) base_dir = os.path.dirname(file_1) os.makedirs(base_dir + '/1') os.makedirs(base_dir + '/2') cmd_1 = ['python', 'test', '--config-dir', base_dir + '/1'] cmd_2 = ['python', 'test', '--config-dir', base_dir + '/2'] cmd_3 = ['python', 'test', '--config-file', file_1] cmd_4 = ['python', 'test', '--config-file', file_2] opts = [ {'opt': cfg.StrOpt('region_name')} ] cfg_1 = utils.load_oslo_configuration( from_cmd=cmd_1, in_project=self.PROJECT, of_prog=self.PROG, for_opts=opts ) cfg_2 = utils.load_oslo_configuration( from_cmd=cmd_2, in_project=self.PROJECT, of_prog=self.PROG, for_opts=opts ) cfg_3 = utils.load_oslo_configuration( from_cmd=cmd_3, in_project=self.PROJECT, of_prog=self.PROG, for_opts=opts ) cfg_4 = utils.load_oslo_configuration( from_cmd=cmd_4, in_project=self.PROJECT, of_prog=self.PROG, for_opts=opts ) self.assertIsNot(cfg_1, cfg_2) self.assertIsNot(cfg_3, cfg_4) self.assertNotEqual(cfg_1, cfg_2) self.assertNotEqual(cfg_3, cfg_4)
def test_should_create_new_oslo_conf_for_each_call(self): # test ensures that each call for load_oslo_configuration # creates new object of oslo_config.ConfigOpts cfg_1 = utils.load_oslo_configuration( from_cmd=[], in_project=self.PROJECT, of_prog=self.PROG, for_opts=[] ) cfg_2 = utils.load_oslo_configuration( from_cmd=[], in_project=self.PROJECT, of_prog=self.PROG, for_opts=[] ) self.assertIsNot(cfg_1, cfg_2)
def _find_nova_conf(nova_process): try: nova_cmd = nova_process.as_dict(['cmdline'])['cmdline'] return utils.load_oslo_configuration(from_cmd=nova_cmd, in_project='nova', for_opts=_REQUIRED_OPTS) except cfg.Error: log.exception('Failed to load nova configuration') return None
def test_distinct_oslo_confs_should_contain_different_opts(self): # test ensures that each instance created via load_oslo_configuration # contains different values of the same opts cmd_1 = ['python', 'test', '--foo', '1'] cmd_2 = ['python', 'test', '--foo', '2'] opts = [ { 'opt': cfg.IntOpt(name='foo', default=-1), 'cli': True } ] cfg_1 = utils.load_oslo_configuration( from_cmd=cmd_1, in_project=self.PROJECT, of_prog=self.PROG, for_opts=opts ) cfg_2 = utils.load_oslo_configuration( from_cmd=cmd_2, in_project=self.PROJECT, of_prog=self.PROG, for_opts=opts ) cfg_3 = utils.load_oslo_configuration( from_cmd=[], in_project=self.PROJECT, of_prog=self.PROG, for_opts=opts ) self.assertIsNot(cfg_1, cfg_2) self.assertIsNot(cfg_2, cfg_3) self.assertIsNot(cfg_1, cfg_3) self.assertNotEqual(cfg_1.foo, cfg_2.foo) self.assertNotEqual(cfg_2.foo, cfg_3.foo) self.assertNotEqual(cfg_1.foo, cfg_3.foo)
def _run_load_oslo_test(self, config_opts, opts, args): actual_args = args[2:] conf = utils.load_oslo_configuration( from_cmd=args, in_project=self.PROJECT, of_prog=self.PROG, for_opts=opts ) self.assertIsNotNone(conf) for opt in opts: config_opts.register_opt.assert_called_once_with(**opt) config_opts.assert_called_once_with( args=actual_args, project=self.PROJECT, prog=self.PROG )
def _detect(self): process_exist = utils.find_process_cmdline(Ovs.PROC_NAME) has_dependencies = self.dependencies_installed() neutron_conf = self._get_ovs_config_file() if process_exist else '' neutron_conf_exists = os.path.isfile(neutron_conf) neutron_conf_valid = (neutron_conf_exists and self._is_neutron_conf_valid(neutron_conf)) self.available = (process_exist is not None and neutron_conf_valid and has_dependencies) self.cmd = '' if process_exist: self.cmd = process_exist.as_dict(attrs=['cmdline'])['cmdline'] if not self.available: if not process_exist: log.error('OVS daemon process [%s] does not exist.', Ovs.PROC_NAME) elif not neutron_conf_exists: log.error(('OVS daemon process exists but configuration ' 'file was not found. Path to file does not exist ' 'as a process parameter or was not ' 'passed via args.')) elif not neutron_conf_valid: log.error(('OVS daemon process exists, configuration file was ' 'found but it looks like it does not contain ' 'one of following sections=%s. ' 'Check if neutron was not configured to load ' 'configuration from /etc/neutron/neutron.conf.d/.'), Ovs.REQUIRED_CONF_SECTIONS) # NOTE(trebskit) the problem with that approach is that # each setting that Ovs plugin require might be scattered # through multiple files located inside # /etc/neutron/neutron.conf.d/ # not to mention that it is still possible to have # yet another configuration file passed as neutron CLI # argument elif not has_dependencies: log.error(('OVS daemon process exists but required ' 'dependencies were not found.\n' 'Run pip install monasca-agent[ovs] ' 'to install all dependencies.')) else: for_opts = [{ 'opt': cfg.StrOpt('region', default='RegionOne'), 'group': 'service_auth' }, { 'opt': cfg.StrOpt('region_name'), 'group': 'nova' }, { 'opt': cfg.StrOpt('nova_region_name'), 'group': 'DEFAULT' }, { 'opt': cfg.StrOpt('username'), 'group': 'keystone_authtoken' }, { 'opt': cfg.StrOpt('password'), 'group': 'keystone_authtoken' }, { 'opt': cfg.StrOpt('project_name'), 'group': 'keystone_authtoken' }, { 'opt': cfg.StrOpt('auth_url'), 'group': 'keystone_authtoken' }, { 'opt': cfg.StrOpt('identity_uri'), 'group': 'keystone_authtoken' }] self.conf = utils.load_oslo_configuration(from_cmd=self.cmd, in_project='neutron', for_opts=for_opts) log.info( "\tUsing neutron configuration file {0} for detection".format( neutron_conf)) self.neutron_conf = neutron_conf
def _detect(self): process_exist = utils.find_process_cmdline(Ovs.PROC_NAME) has_dependencies = self.dependencies_installed() neutron_conf = self._get_ovs_config_file() if process_exist else '' neutron_conf_exists = os.path.isfile(neutron_conf) neutron_conf_valid = (neutron_conf_exists and self._is_neutron_conf_valid(neutron_conf)) self.available = (process_exist is not None and neutron_conf_valid and has_dependencies) self.cmd = '' if process_exist: self.cmd = process_exist.as_dict(attrs=['cmdline'])['cmdline'] if not self.available: if not process_exist: log.info('OVS daemon process [%s] does not exist.', Ovs.PROC_NAME) elif not neutron_conf_exists: log.warning(('OVS daemon process exists but configuration ' 'file was not found. Path to file does not exist ' 'as a process parameter or was not ' 'passed via args.')) elif not neutron_conf_valid: log.error(('OVS daemon process exists, configuration file was ' 'found but it looks like it does not contain ' 'one of following sections=%s. ' 'Check if neutron was not configured to load ' 'configuration from /etc/neutron/neutron.conf.d/.'), Ovs.REQUIRED_CONF_SECTIONS) # NOTE(trebskit) the problem with that approach is that # each setting that Ovs plugin require might be scattered # through multiple files located inside # /etc/neutron/neutron.conf.d/ # not to mention that it is still possible to have # yet another configuration file passed as neutron CLI # argument elif not has_dependencies: log.error(('OVS daemon process exists but required ' 'dependencies were not found.\n' 'Run pip install monasca-agent[ovs] ' 'to install all dependencies.')) else: for_opts = [{'opt': cfg.StrOpt('region', default='RegionOne'), 'group': 'service_auth'}, {'opt': cfg.StrOpt('region_name'), 'group': 'nova'}, {'opt': cfg.StrOpt('nova_region_name'), 'group': 'DEFAULT'}, {'opt': cfg.StrOpt('username'), 'group': 'keystone_authtoken'}, {'opt': cfg.StrOpt('password'), 'group': 'keystone_authtoken'}, {'opt': cfg.StrOpt('project_name'), 'group': 'keystone_authtoken'}, {'opt': cfg.StrOpt('user_domain_name'), 'group': 'keystone_authtoken'}, {'opt': cfg.StrOpt('user_domain_id'), 'group': 'keystone_authtoken'}, {'opt': cfg.StrOpt('project_domain_name'), 'group': 'keystone_authtoken'}, {'opt': cfg.StrOpt('project_domain_id'), 'group': 'keystone_authtoken'}, {'opt': cfg.StrOpt('auth_url'), 'group': 'keystone_authtoken'}, {'opt': cfg.StrOpt('identity_uri'), 'group': 'keystone_authtoken'}] self.conf = utils.load_oslo_configuration(from_cmd=self.cmd, in_project='neutron', for_opts=for_opts ) log.info("\tUsing neutron configuration file {0} for detection".format(neutron_conf)) self.neutron_conf = neutron_conf