def testInvalidAndValidConfigs(self): valid_config_name = "valid_config" with open(os.path.join(self.config_dir, "test1.conf"), "w") as f: f.write(""" [%(valid_config_name)s] type=esx server=1.2.3.4 username=admin password=password owner=owner env=env rhsm_hostname=abc [invalid_missing_owner] type=esx server=1.2.3.4 username=admin password=password env=env rhsm_hostname=abc """ % {'valid_config_name': valid_config_name}) config_manager = DestinationToSourceMapper(init_config({}, {}, config_dir=self.config_dir)) # There should be only one config, and that should be the one that is valid self.assertEqual(len(config_manager.configs), 1) self.assertEqual(config_manager.configs[0][1].name, valid_config_name)
def testCLIConfigOverridesGeneralConfigFile(self): cli_config_file_path = os.path.join(self.custom_config_dir, "my_file.conf") with open(cli_config_file_path, "w") as f: f.write(""" [valid_cli_section] server=5.5.5.5 username=admin1 password=password1 owner=owner1 rhsm_hostname=abc1 """) cli_dict = {'configs': [cli_config_file_path]} # alter the main conf file constant temporarily: virtwho.config.VW_GENERAL_CONF_PATH = os.path.join(self.general_config_file_dir, "virt-who.conf") with open(virtwho.config.VW_GENERAL_CONF_PATH, "w") as f: f.write(""" [valid_default_main_conf_file_section] server=1.2.3.4 username=admin password=password owner=owner rhsm_hostname=abc """) config_manager = DestinationToSourceMapper(init_config({}, cli_dict, config_dir=self.config_dir)) # There should be only one config, and that should be the one passed from the cli self.assertEqual(len(config_manager.configs), 1) config = config_manager.configs[0][1] self.assertEqual(config.name, "valid_cli_section") self.assertEqual(config["server"], "5.5.5.5") self.assertEqual(config["username"], "admin1") self.assertEqual(config["password"], "password1") self.assertEqual(config["owner"], "owner1") self.assertEqual(config["rhsm_hostname"], "abc1")
def testCLIConfigOverridesDefaultDirectoryConfigs(self): cli_config_file_path = os.path.join(self.custom_config_dir, "my_file.conf") with open(cli_config_file_path, "w") as f: f.write(""" [valid_cli_section] server=5.5.5.5 username=admin1 password=password1 owner=owner1 env=env1 rhsm_hostname=abc1 """) cli_dict = {'configs': [cli_config_file_path]} with open(os.path.join(self.config_dir, "test1.conf"), "w") as f: f.write(""" [valid_default_dir_section] server=1.2.3.4 username=admin password=password owner=owner env=env rhsm_hostname=abc """) config_manager = DestinationToSourceMapper(init_config({}, cli_dict, config_dir=self.config_dir)) # There should be only one config, and that should be the one passed from the cli self.assertEqual(len(config_manager.configs), 1) config = config_manager.configs[0][1] self.assertEqual(config.name, "valid_cli_section") self.assertEqual(config["server"], "5.5.5.5") self.assertEqual(config["username"], "admin1") self.assertEqual(config["password"], "password1") self.assertEqual(config["owner"], "owner1") self.assertEqual(config["env"], "env1") self.assertEqual(config["rhsm_hostname"], "abc1")
def test_read_hypervisor(self): with open(self.hypervisor_file, "w") as f: f.write(HYPERVISOR_JSON) with open(self.config_file, "w") as f: f.write(""" [test] type=fake is_hypervisor=true owner=taylor env=swift file=%s """ % self.hypervisor_file) effective_config = init_config({}, {}, config_dir=self.config_dir) manager = DestinationToSourceMapper(effective_config) self.assertEqual(len(manager.configs), 1) virt = Virt.from_config(self.logger, manager.configs[0][1], None) self.assertEqual(type(virt), FakeVirt) mapping = virt.getHostGuestMapping() self.assertTrue("hypervisors" in mapping) hypervisors = mapping["hypervisors"] self.assertEqual(len(hypervisors), 1) hypervisor = hypervisors[0] self.assertEqual(type(hypervisor), Hypervisor) self.assertEqual(hypervisor.hypervisorId, "60527517-6284-7593-6AAB-75BF2A6375EF") self.assertEqual(len(hypervisor.guestIds), 1) guest = hypervisor.guestIds[0] self.assertEqual(guest.uuid, "07ED8178-95D5-4244-BC7D-582A54A48FF8") self.assertEqual(guest.state, 1)
def test_one_source_to_many_dests(self): # This tests that there can be one source that specifies # information for different destinations and that the correct mapping # is created. config_1 = combine_dicts(TestReadingConfigs.source_options_1, TestReadingConfigs.dest_options_1) # NOTE: virt-who today does not support config sections having the same # name. Hence the only way to have one source go to multiple # destinations (without new config options) is to have two sections # with the same information but different section names config_options_2 = TestReadingConfigs.source_options_1.copy() config_options_2['name'] = 'test2' config_2 = combine_dicts(config_options_2, TestReadingConfigs.dest_options_2) expected_dest_1 = Satellite6DestinationInfo( **TestReadingConfigs.dest_options_1) expected_dest_2 = Satellite6DestinationInfo( **TestReadingConfigs.dest_options_2) expected_mapping = { expected_dest_1: [config_1['name']], expected_dest_2: [config_2['name']] # config_2['name'] == # config_1['name'] } with open(os.path.join(self.config_dir, "test1.conf"), "w") as f: f.write(TestReadingConfigs.dict_to_ini(config_1) + TestReadingConfigs.dict_to_ini(config_2)) manager = DestinationToSourceMapper(init_config({}, {}, config_dir=self.config_dir)) self.assertEqual(manager.dest_to_sources_map, expected_mapping)
def testCLIConfigOverridesGeneralConfigFileButStillReadsItsGlobalAndDefaultsSections(self): cli_config_file_path = os.path.join(self.custom_config_dir, "my_file.conf") with open(cli_config_file_path, "w") as f: f.write(""" [valid_cli_section] server=5.5.5.5 username=admin1 password=password1 owner=owner1 env=env1 rhsm_hostname=abc1 """) cli_dict = {'configs': [cli_config_file_path]} # alter the main conf file constant temporarily: virtwho.config.VW_GENERAL_CONF_PATH = os.path.join(self.general_config_file_dir, "virt-who.conf") with open(virtwho.config.VW_GENERAL_CONF_PATH, "w") as f: f.write(""" [global] interval=100 log_file=rhsm45.log [defaults] hypervisor_id=hostname [valid_default_main_conf_file_section] server=1.2.3.4 username=admin password=password owner=owner env=env rhsm_hostname=abc """) config_manager = DestinationToSourceMapper(init_config({}, cli_dict, config_dir=self.config_dir)) # There should be only one config, and that should be the one passed from the cli self.assertEqual(len(config_manager.configs), 1) config = config_manager.configs[0][1] self.assertEqual(config.name, "valid_cli_section") self.assertEqual(config["server"], "5.5.5.5") self.assertEqual(config["username"], "admin1") self.assertEqual(config["password"], "password1") self.assertEqual(config["owner"], "owner1") self.assertEqual(config["env"], "env1") self.assertEqual(config["rhsm_hostname"], "abc1") # Also, check that the default section values from the VW_GENERAL_CONF_PATH file are still read # (and used when any of the keys are missing in the virt config) self.assertEqual(config["hypervisor_id"], "hostname") # Additionally, the global section from the VW_GENERAL_CONF_PATH file should be read in self.assertEqual(config_manager.effective_config["global"]["log_file"], "rhsm45.log") self.assertEqual(config_manager.effective_config["global"]["interval"], 100)
def test_invalid_config(self): with open(os.path.join(self.config_dir, "test.conf"), "w") as f: f.write(""" Malformed configuration file """) manager = DestinationToSourceMapper(init_config({}, {}, config_dir=self.config_dir)) # If there are only invalid configurations specified, and nothing has been specified via # the command line or ENV then we should use the default # TODO Remove the default hard-coded behaviour, and allow virt-who to output a # configuration that will cause it to behave equivalently self.assertEqual(len(manager.configs), 1) self.assertEqual(manager.configs[0][0], VW_ENV_CLI_SECTION_NAME)
def testNoOptionsConfig(self): with open(os.path.join(self.config_dir, "test.conf"), "w") as f: f.write(""" [test] type=esx """) # Instantiating the DestinationToSourceMapper with an invalid config should not fail # instead we expect that the list of configs managed by the DestinationToSourceMapper does not # include the invalid one config_manager = DestinationToSourceMapper(init_config({}, {}, config_dir=self.config_dir)) # There should be no configs parsed successfully, therefore the list of configs should # be empty self.assertEqual(len(config_manager.configs), 0)
def testInvisibleConfigFile(self): with open(os.path.join(self.config_dir, ".test1.conf"), "w") as f: f.write(""" [test1] type=libvirt server=1.2.3.4 username=admin password=password owner=root """) manager = DestinationToSourceMapper(init_config({}, {}, config_dir=self.config_dir)) self.assertTrue("test1" not in [name for (name, config) in manager.configs], "Hidden config file shouldn't be read")
def test_one_source_to_one_dest(self): config_1 = combine_dicts(TestReadingConfigs.source_options_1, TestReadingConfigs.dest_options_1) expected_dest_1 = Satellite6DestinationInfo( **TestReadingConfigs.dest_options_1) expected_mapping = { expected_dest_1: [config_1['name']] } with open(os.path.join(self.config_dir, "test1.conf"), "w") as f: f.write(TestReadingConfigs.dict_to_ini(config_1)) manager = DestinationToSourceMapper(init_config({}, {}, config_dir=self.config_dir)) self.assertEqual(manager.dest_to_sources_map, expected_mapping)
def testFilterHostNew(self): with open(os.path.join(self.config_dir, "test1.conf"), "w") as f: f.write(""" [test1] type=esx server=1.2.3.4 username=admin password=password owner=root filter_hosts=12345 """) manager = DestinationToSourceMapper(init_config({}, {}, config_dir=self.config_dir)) self.assertEqual(len(manager.configs), 1) self.assertEqual(manager.configs[0][1]["filter_hosts"], ['12345'])
def testEsxDisableSimplifiedVim(self): with open(os.path.join(self.config_dir, "test1.conf"), "w") as f: f.write(""" [test1] type=esx server=1.2.3.4 username=admin password=password owner=root simplified_vim=false """) manager = DestinationToSourceMapper(init_config({}, {}, config_dir=self.config_dir)) self.assertEqual(len(manager.configs), 1) _, config = manager.configs[0] self.assertFalse(config['simplified_vim'])
def test_unreadable_config(self): filename = os.path.join(self.config_dir, "test.conf") with open(filename, "w") as f: f.write(""" [test] type=esx server=1.2.3.4 username=admin password=password owner=root """) os.chmod(filename, 0) manager = DestinationToSourceMapper(init_config({}, {}, config_dir=self.config_dir)) # There should be at least one 'env/cmdline' section self.assertEqual(len(manager.configs), 1)
def test_read_non_hypervisor_from_hypervisor(self): with open(self.hypervisor_file, "w") as f: f.write(HYPERVISOR_JSON) with open(self.config_file, "w") as f: f.write(""" [test] type=fake is_hypervisor=false file=%s """ % self.hypervisor_file) effective_config = init_config({}, {}, config_dir=self.config_dir) # This is an invalid case, the config section that is invalid should have been dropped self.assertNotIn('test', effective_config)
def testMissingOwnerOption(self): with open(os.path.join(self.config_dir, "test1.conf"), "w") as f: f.write(""" [test1] type=esx server=1.2.3.4 username=admin password=password rhsm_hostname=abc """) # Instantiating the DestinationToSourceMapper with an invalid config should not fail # instead we expect that the list of configs managed by the DestinationToSourceMapper does not # include the invalid one config_manager = DestinationToSourceMapper(init_config({}, {}, config_dir=self.config_dir)) # There should be no configs parsed successfully, therefore the list of configs should # be empty self.assertEqual(len(config_manager.configs), 0)
def test_read_hypervisor_from_non_hypervisor(self): with open(self.hypervisor_file, "w") as f: f.write(NON_HYPERVISOR_JSON) with open(self.config_file, "w") as f: f.write(""" [test] type=fake is_hypervisor=true owner=covfefe file=%s """ % self.hypervisor_file) effective_config = init_config({}, config_dir=self.config_dir) DestinationToSourceMapper(effective_config) # The 'test' section is not valid here (as the json provided will not work with # the is_hypervisor value set to true) self.assertNotIn('test', effective_config)
def test_invalid_type(self): filename = os.path.join(self.config_dir, "test.conf") with open(filename, "w") as f: f.write(""" [test] type=invalid server=1.2.3.4 username=test """) # Instantiating the DestinationToSourceMapper with an invalid config should not fail # instead we expect that the list of configs managed by the DestinationToSourceMapper does not # include the invalid one config_manager = DestinationToSourceMapper( init_config({}, {}, config_dir=self.config_dir)) # There should be no configs parsed successfully, therefore the list of configs should # be empty self.assertEqual(len(config_manager.configs), 0)
def test_unreadable_config(self): filename = os.path.join(self.config_dir, "test.conf") with open(filename, "w") as f: f.write(""" [test] type=esx server=1.2.3.4 username=admin password=password owner=root env=staging """) os.chmod(filename, 0) manager = DestinationToSourceMapper( init_config({}, {}, config_dir=self.config_dir)) # There should be at least one 'env/cmdline' section self.assertEqual(len(manager.configs), 1)
def testMissingSectionName(self): with open(os.path.join(self.config_dir, "test1.conf"), "w") as f: f.write(""" type=esx server=1.2.3.4 username=admin password=password rhsm_hostname=abc """) # Instantiating the DestinationToSourceMapper with an invalid config should not fail # instead we expect that the list of configs managed by the DestinationToSourceMapper does not # include the invalid one config_manager = DestinationToSourceMapper( init_config({}, {}, config_dir=self.config_dir)) # There should be no configs parsed successfully, therefore the list of configs should # be empty self.assertEqual(len(config_manager.configs), 0)
def test_sm_config_file(self): config_dir = tempfile.mkdtemp() self.addCleanup(shutil.rmtree, config_dir) with open(os.path.join(config_dir, "test.conf"), "w") as f: f.write(""" [test] type=libvirt owner=owner env=env rhsm_hostname=host rhsm_port=8080 rhsm_prefix=prefix rhsm_proxy_hostname=proxy_host rhsm_proxy_port=9090 rhsm_proxy_user=proxy_user rhsm_proxy_password=proxy_password rhsm_insecure=1 rhsm_username=user rhsm_password=passwd rhsm_no_proxy=filter """) config_manager = DestinationToSourceMapper( init_config({}, {}, config_dir=config_dir)) self.assertEqual(len(config_manager.configs), 1) config = dict(config_manager.configs)["test"] manager = Manager.from_config(self.logger, config) self.assertTrue(isinstance(manager, SubscriptionManager)) self.assertEqual(config['rhsm_hostname'], 'host') self.assertEqual(config['rhsm_port'], '8080') manager._connect(config) self.sm.connection.assert_called_with( username='******', password='******', host='host', ssl_port=8080, handler='prefix', proxy_hostname='proxy_host', proxy_port='9090', proxy_user='******', proxy_password='******', no_proxy='filter', insecure='1', correlation_id=manager.correlation_id)
def testCryptedPassword(self, password): password.return_value = (hexlify(Password._generate_key()), hexlify(Password._generate_key())) passwd = "TestSecretPassword!" crypted = hexlify(Password.encrypt(passwd)).decode('utf-8') filename = os.path.join(self.config_dir, "test.conf") with open(filename, "w") as f: f.write(""" [test] type=esx server=1.2.3.4 username=admin encrypted_password=%s owner=root """ % crypted) manager = DestinationToSourceMapper(init_config({}, {}, config_dir=self.config_dir)) self.assertEqual(len(manager.configs), 1) self.assertEqual(manager.configs[0][1]['password'], passwd)
def test_read_hypervisor_from_non_hypervisor(self): with open(self.hypervisor_file, "w") as f: f.write(NON_HYPERVISOR_JSON) with open(self.config_file, "w") as f: f.write(""" [test] type=fake is_hypervisor=true owner=covfefe env=covfefe file=%s """ % self.hypervisor_file) effective_config = init_config({}, {}, config_dir=self.config_dir) DestinationToSourceMapper(effective_config) # The 'test' section is not valid here (as the json provided will not work with # the is_hypervisor value set to true) self.assertNotIn('test', effective_config)
def testMultipleConfigsInFile(self): config_1 = combine_dicts(TestReadingConfigs.source_options_1, TestReadingConfigs.dest_options_1) config_2 = combine_dicts(TestReadingConfigs.source_options_2, TestReadingConfigs.dest_options_2) with open(os.path.join(self.config_dir, "test.conf"), "w") as f: f.write(TestReadingConfigs.dict_to_ini(config_1) + TestReadingConfigs.dict_to_ini(config_2)) manager = DestinationToSourceMapper(init_config({}, {}, config_dir=self.config_dir)) self.assertEqual(len(manager.configs), 2) for name, config in manager.configs: self.assertIn(config.name, [config_1["name"], config_2["name"]]) if config.name == config_1['name']: self.assert_config_contains_all(config, config_1) elif config.name == config_2['name']: self.assert_config_contains_all(config, config_2)
def test_sm_config_file(self): config_dir = tempfile.mkdtemp() self.addCleanup(shutil.rmtree, config_dir) with open(os.path.join(config_dir, "test.conf"), "w") as f: f.write(""" [test] type=libvirt owner=owner env=env rhsm_hostname=host rhsm_port=8080 rhsm_prefix=prefix rhsm_proxy_hostname=proxy_host rhsm_proxy_port=9090 rhsm_proxy_user=proxy_user rhsm_proxy_password=proxy_password rhsm_insecure=1 rhsm_username=user rhsm_password=passwd rhsm_no_proxy=filter """) config_manager = DestinationToSourceMapper(init_config({}, {}, config_dir=config_dir)) self.assertEqual(len(config_manager.configs), 1) config = dict(config_manager.configs)["test"] manager = Manager.from_config(self.logger, config) self.assertTrue(isinstance(manager, SubscriptionManager)) self.assertEqual(config['rhsm_hostname'], 'host') self.assertEqual(config['rhsm_port'], '8080') manager._connect(config) self.sm.connection.assert_called_with( username='******', password='******', host='host', ssl_port=8080, handler='prefix', proxy_hostname='proxy_host', proxy_port='9090', proxy_user='******', proxy_password='******', no_proxy='filter', insecure='1', correlation_id=manager.correlation_id)
def testQuotesInConfig(self): with open(os.path.join(self.config_dir, "test1.conf"), "w") as f: f.write(""" [test1] type=esx server="http://1.2.3.4" username='******' password=p"asswor'd owner=" root " """) manager = DestinationToSourceMapper(init_config({}, {}, config_dir=self.config_dir)) self.assertEqual(len(manager.configs), 1) config = manager.configs[0][1] self.assertEqual(config.name, "test1") self.assertEqual(config["type"], "esx") self.assertEqual(config["server"], "http://1.2.3.4") self.assertEqual(config["username"], "admin") self.assertEqual(config["password"], "p\"asswor'd") self.assertEqual(config["owner"], " root ")
def test_many_sources_to_many_dests(self): config_1 = combine_dicts(TestReadingConfigs.source_options_1, TestReadingConfigs.dest_options_1) config_2 = combine_dicts(TestReadingConfigs.source_options_2, TestReadingConfigs.dest_options_2) # Create another source config that is slightly different source_3_options = TestReadingConfigs.source_options_2.copy() source_3_options['name'] = 'test3' source_4_options = TestReadingConfigs.source_options_1.copy() source_4_options['name'] = 'test4' # Create another dest config that is slightly different dest_options_3 = TestReadingConfigs.dest_options_2.copy() dest_options_3['owner'] = 'some_cool_owner_person' config_3 = combine_dicts(source_3_options, TestReadingConfigs.dest_options_2) config_4 = combine_dicts(source_4_options, dest_options_3) expected_dest_1 = Satellite6DestinationInfo( **TestReadingConfigs.dest_options_1) expected_dest_2 = Satellite6DestinationInfo( **TestReadingConfigs.dest_options_2) expected_dest_3 = Satellite6DestinationInfo(**dest_options_3) expected_mapping = { expected_dest_1: [config_1['name']], expected_dest_2: [config_2['name'], config_3['name']], expected_dest_3: [config_4['name']] } with open(os.path.join(self.config_dir, "test1.conf"), "w") as f: f.write( TestReadingConfigs.dict_to_ini(config_1) + TestReadingConfigs.dict_to_ini(config_2) + TestReadingConfigs.dict_to_ini(config_3) + TestReadingConfigs.dict_to_ini(config_4)) manager = DestinationToSourceMapper( init_config({}, config_dir=self.config_dir)) self.assertEqual(manager.dest_to_sources_map, expected_mapping)
def testCommentedOutLineContinuationInConfig(self, logger_warn): """Test that when a config line that starts with space or tab which is followed by a '#', if we are running python2: it is treated as a continuation of the previous line, but a warning is logged for the user. If we are running python3: it is ignored as a comment, and no warning is logged. :return: """ with open(os.path.join(self.config_dir, "test1.conf"), "w") as f: f.write(""" [test1] type=esx server=http://1.2.3.4 #value username=admin password=password owner=root env=staging #filter_hosts=abc.efg.com """) manager = DestinationToSourceMapper( init_config({}, {}, config_dir=self.config_dir)) self.assertEqual(len(manager.configs), 1) config = manager.configs[0][1] self.assertEqual(config.name, "test1") if six.PY2: self.assertEqual(config["server"], "http://1.2.3.4\n#value") self.assertEqual(config["env"], 'staging\n#filter_hosts=abc.efg.com') # Check that the warning was logged twice, and it was last called for line number 10 of the conf file: self.assertTrue(logger_warn.called) self.assertEqual(logger_warn.call_count, 2) logger_warn.assert_called_with( 'A line continuation (line starts with space) that is commented out ' 'was detected in file %s, line number %s.', f.name, 10) elif six.PY3: self.assertEqual(config["server"], "http://1.2.3.4") self.assertEqual(config["env"], 'staging') self.assertFalse(logger_warn.called) self.assertEqual(logger_warn.call_count, 0)
def test_many_sources_to_many_dests(self): config_1 = combine_dicts(TestReadingConfigs.source_options_1, TestReadingConfigs.dest_options_1) config_2 = combine_dicts(TestReadingConfigs.source_options_2, TestReadingConfigs.dest_options_2) # Create another source config that is slightly different source_3_options = TestReadingConfigs.source_options_2.copy() source_3_options['name'] = 'test3' source_4_options = TestReadingConfigs.source_options_1.copy() source_4_options['name'] = 'test4' # Create another dest config that is slightly different dest_options_3 = TestReadingConfigs.dest_options_2.copy() dest_options_3['owner'] = 'some_cool_owner_person' config_3 = combine_dicts(source_3_options, TestReadingConfigs.dest_options_2) config_4 = combine_dicts(source_4_options, dest_options_3) expected_dest_1 = Satellite6DestinationInfo( **TestReadingConfigs.dest_options_1) expected_dest_2 = Satellite6DestinationInfo( **TestReadingConfigs.dest_options_2) expected_dest_3 = Satellite6DestinationInfo(**dest_options_3) expected_mapping = { expected_dest_1: [config_1['name']], expected_dest_2: [config_2['name'], config_3['name']], expected_dest_3: [config_4['name']] } with open(os.path.join(self.config_dir, "test1.conf"), "w") as f: f.write(TestReadingConfigs.dict_to_ini(config_1) + TestReadingConfigs.dict_to_ini(config_2) + TestReadingConfigs.dict_to_ini(config_3) + TestReadingConfigs.dict_to_ini(config_4)) manager = DestinationToSourceMapper(init_config({}, {}, config_dir=self.config_dir)) self.assertEqual(manager.dest_to_sources_map, expected_mapping)
def testMultipleConfigsInFile(self): config_1 = combine_dicts(TestReadingConfigs.source_options_1, TestReadingConfigs.dest_options_1) config_2 = combine_dicts(TestReadingConfigs.source_options_2, TestReadingConfigs.dest_options_2) with open(os.path.join(self.config_dir, "test.conf"), "w") as f: f.write( TestReadingConfigs.dict_to_ini(config_1) + TestReadingConfigs.dict_to_ini(config_2)) manager = DestinationToSourceMapper( init_config({}, config_dir=self.config_dir)) self.assertEqual(len(manager.configs), 2) for name, config in manager.configs: self.assertIn(config.name, [config_1["name"], config_2["name"]]) if config.name == config_1['name']: self.assert_config_contains_all(config, config_1) elif config.name == config_2['name']: self.assert_config_contains_all(config, config_2)
def testQuotesInConfig(self): with open(os.path.join(self.config_dir, "test1.conf"), "w") as f: f.write(""" [test1] type=esx server="http://1.2.3.4" username='******' password=p"asswor'd owner=" root " """) manager = DestinationToSourceMapper( init_config({}, config_dir=self.config_dir)) self.assertEqual(len(manager.configs), 1) config = manager.configs[0][1] self.assertEqual(config.name, "test1") self.assertEqual(config["type"], "esx") self.assertEqual(config["server"], "http://1.2.3.4") self.assertEqual(config["username"], "admin") self.assertEqual(config["password"], "p\"asswor'd") self.assertEqual(config["owner"], " root ")
def testCryptedPassword(self, password): password.return_value = (hexlify(Password._generate_key()), hexlify(Password._generate_key())) passwd = "TestSecretPassword!" crypted = hexlify(Password.encrypt(passwd)).decode('utf-8') filename = os.path.join(self.config_dir, "test.conf") with open(filename, "w") as f: f.write(""" [test] type=esx server=1.2.3.4 username=admin encrypted_password=%s owner=root """ % crypted) manager = DestinationToSourceMapper( init_config({}, config_dir=self.config_dir)) self.assertEqual(len(manager.configs), 1) self.assertEqual(manager.configs[0][1]['password'], passwd)
def testUnicode(self): with open(os.path.join(self.config_dir, "test1.conf"), "w") as f: f.write(""" [test1] type=esx server=http://žluťoučký servřík username=username password=password owner=здравствуйте """) manager = DestinationToSourceMapper(init_config({}, {}, config_dir=self.config_dir)) self.assertEqual(len(manager.configs), 1) config = manager.configs[0][1] self.assertEqual(config.name, "test1") self.assertEqual(config["type"], "esx") self.assertEqual(config["server"], "http://žluťoučký servřík") # Username and password can't be unicode, they has to be latin1 for HTTP Basic auth self.assertEqual(config["username"], "username") self.assertEqual(config["password"], "password") self.assertEqual(config["owner"], "здравствуйте")
def testCLIConfigOverridesGeneralConfigFile(self): cli_config_file_path = os.path.join(self.custom_config_dir, "my_file.conf") with open(cli_config_file_path, "w") as f: f.write(""" [valid_cli_section] server=5.5.5.5 username=admin1 password=password1 owner=owner1 env=env1 rhsm_hostname=abc1 """) cli_dict = {'configs': [cli_config_file_path]} # alter the main conf file constant temporarily: virtwho.config.VW_GENERAL_CONF_PATH = os.path.join( self.general_config_file_dir, "virt-who.conf") with open(virtwho.config.VW_GENERAL_CONF_PATH, "w") as f: f.write(""" [valid_default_main_conf_file_section] server=1.2.3.4 username=admin password=password owner=owner env=env rhsm_hostname=abc """) config_manager = DestinationToSourceMapper( init_config({}, cli_dict, config_dir=self.config_dir)) # There should be only one config, and that should be the one passed from the cli self.assertEqual(len(config_manager.configs), 1) config = config_manager.configs[0][1] self.assertEqual(config.name, "valid_cli_section") self.assertEqual(config["server"], "5.5.5.5") self.assertEqual(config["username"], "admin1") self.assertEqual(config["password"], "password1") self.assertEqual(config["owner"], "owner1") self.assertEqual(config["env"], "env1") self.assertEqual(config["rhsm_hostname"], "abc1")
def testUnicode(self): with open(os.path.join(self.config_dir, "test1.conf"), "w") as f: f.write(""" [test1] type=hyperv server=http://žluťoučký servřík username=username password=password owner=здравствуйте """) manager = DestinationToSourceMapper( init_config({}, config_dir=self.config_dir)) self.assertEqual(len(manager.configs), 1) config = manager.configs[0][1] self.assertEqual(config.name, "test1") self.assertEqual(config["type"], "hyperv") self.assertEqual(config["server"], "http://žluťoučký servřík") # Username and password can't be unicode, they has to be latin1 for HTTP Basic auth self.assertEqual(config["username"], "username") self.assertEqual(config["password"], "password") self.assertEqual(config["owner"], "здравствуйте")
def testLibvirtConfig(self): with open(os.path.join(self.config_dir, "test1.conf"), "w") as f: f.write(""" [test1] type=libvirt server=1.2.3.4 username=admin password=password owner=root """) manager = DestinationToSourceMapper(init_config({}, {}, config_dir=self.config_dir)) self.assertEqual(len(manager.configs), 1) config = manager.configs[0][1] self.assertEqual(config.name, "test1") self.assertEqual(config["type"], "libvirt") # The following server value is different than what is provided above as it has been # processed by the libvirt config section validation. # TODO decouple this from the libvirt config section (for testing only) self.assertEqual(config["server"], "qemu+ssh://[email protected]/system?no_tty=1") self.assertEqual(config["username"], "admin") self.assertEqual(config["password"], "password") self.assertEqual(config["owner"], "root")
def test_read_non_hypervisor(self): with open(self.hypervisor_file, "w") as f: f.write(NON_HYPERVISOR_JSON) with open(self.config_file, "w") as f: f.write(""" [test] type=fake is_hypervisor=false file=%s """ % self.hypervisor_file) effective_config = init_config({}, {}, config_dir=self.config_dir) manager = DestinationToSourceMapper(effective_config) self.assertEqual(len(manager.configs), 1) virt = Virt.from_config(self.logger, manager.configs[0][1], None) self.assertEqual(type(virt), FakeVirt) guests = virt.listDomains() self.assertEqual(len(guests), 1) guest = guests[0] self.assertEqual(guest.uuid, "9f06a84d-5f56-4e7e-be0c-937b3c1924d7") self.assertEqual(guest.state, 1)
def test_read_non_hypervisor(self): with open(self.hypervisor_file, "w") as f: f.write(NON_HYPERVISOR_JSON) with open(self.config_file, "w") as f: f.write(""" [test] type=fake is_hypervisor=false file=%s """ % self.hypervisor_file) effective_config = init_config({}, {}, config_dir=self.config_dir) manager = DestinationToSourceMapper(effective_config) self.assertEqual(len(manager.configs), 1) virt = Virt.from_config(self.logger, manager.configs[0][1], None) self.assertEqual(type(virt), FakeVirt) guests = virt.listDomains() self.assertEqual(len(guests), 1) guest = guests[0] self.assertEqual(guest.uuid, "9f06a84d-5f56-4e7e-be0c-937b3c1924d7") self.assertEqual(guest.state, 1)
def testLineContinuationInConfig(self): """ Test that when a config line that starts with space or tab, it is treated as a continuation of the previous line. :return: """ with open(os.path.join(self.config_dir, "test1.conf"), "w") as f: f.write(""" [test1] type=esx server=http://1.2.3.4 value username=admin password=password owner=root filter_hosts=abc.efg.com """) manager = DestinationToSourceMapper(init_config({}, {}, config_dir=self.config_dir)) self.assertEqual(len(manager.configs), 1) config = manager.configs[0][1] self.assertEqual(config.name, "test1") self.assertEqual(config["server"], 'http://1.2.3.4\nvalue')
def testCommentedOutLineContinuationInConfig(self, logger_warn): """Test that when a config line that starts with space or tab which is followed by a '#', if we are running python2: it is treated as a continuation of the previous line, but a warning is logged for the user. If we are running python3: it is ignored as a comment, and no warning is logged. :return: """ with open(os.path.join(self.config_dir, "test1.conf"), "w") as f: f.write(""" [test1] type=esx server=http://1.2.3.4 #value username=admin password=password owner=root env=staging #filter_hosts=abc.efg.com """) manager = DestinationToSourceMapper(init_config({}, {}, config_dir=self.config_dir)) self.assertEqual(len(manager.configs), 1) config = manager.configs[0][1] self.assertEqual(config.name, "test1") if six.PY2: self.assertEqual(config["server"], "http://1.2.3.4\n#value") self.assertEqual(config["env"], 'staging\n#filter_hosts=abc.efg.com') # Check that the warning was logged twice, and it was last called for line number 10 of the conf file: self.assertTrue(logger_warn.called) self.assertEqual(logger_warn.call_count, 2) logger_warn.assert_called_with('A line continuation (line starts with space) that is commented out ' 'was detected in file %s, line number %s.', f.name, 10) elif six.PY3: self.assertEqual(config["server"], "http://1.2.3.4") self.assertEqual(config["env"], 'staging') self.assertFalse(logger_warn.called) self.assertEqual(logger_warn.call_count, 0)
def test_two_sources_to_two_dests(self): config_1 = combine_dicts(TestReadingConfigs.source_options_1, TestReadingConfigs.dest_options_1) config_2 = combine_dicts(TestReadingConfigs.source_options_2, TestReadingConfigs.dest_options_2) expected_dest_1 = Satellite6DestinationInfo( **TestReadingConfigs.dest_options_1) expected_dest_2 = Satellite6DestinationInfo( **TestReadingConfigs.dest_options_2) expected_mapping = { expected_dest_1: [config_1['name']], expected_dest_2: [config_2['name']] } with open(os.path.join(self.config_dir, "test1.conf"), "w") as f: f.write( TestReadingConfigs.dict_to_ini(config_1) + TestReadingConfigs.dict_to_ini(config_2)) manager = DestinationToSourceMapper( init_config({}, config_dir=self.config_dir)) self.assertEqual(manager.dest_to_sources_map, expected_mapping)
def testMultipleConfigFiles(self): config_1 = combine_dicts(TestReadingConfigs.source_options_1, TestReadingConfigs.dest_options_1) config_2 = combine_dicts(TestReadingConfigs.source_options_2, TestReadingConfigs.dest_options_2) with open(os.path.join(self.config_dir, "test1.conf"), "w") as f: f.write(TestReadingConfigs.dict_to_ini(config_1)) with open(os.path.join(self.config_dir, "test2.conf"), "w") as f: f.write(TestReadingConfigs.dict_to_ini(config_2)) expected_dest_1 = Satellite6DestinationInfo( **TestReadingConfigs.dest_options_1) expected_dest_2 = Satellite6DestinationInfo( **TestReadingConfigs.dest_options_2) expected_mapping = { expected_dest_1: [config_1['name']], expected_dest_2: [config_2['name']] } manager = DestinationToSourceMapper( init_config({}, config_dir=self.config_dir)) self.assertEqual(len(manager.configs), 2) self.assertEqual(manager.dest_to_sources_map, expected_mapping) self.assertEqual(manager.dests, set([expected_dest_1, expected_dest_2])) self.assertEqual(manager.sources, set([config_1['name'], config_2['name']])) result1 = manager.configs[0][1] result2 = manager.configs[1][1] self.assertIn(result1.name, ("test1", "test2")) if result1.name == "test2": result2, result1 = result1, result2 self.assert_config_contains_all(result1, config_1) self.assert_config_contains_all(result2, config_2)
def test_many_sources_to_one_dest(self): # This tests that there can be multiple configs that specify to # report to the same destination config_1 = combine_dicts(TestReadingConfigs.source_options_1, TestReadingConfigs.dest_options_1) config_2 = combine_dicts(TestReadingConfigs.source_options_2, TestReadingConfigs.dest_options_1) expected_dest = Satellite6DestinationInfo( **TestReadingConfigs.dest_options_1) expected_mapping = {expected_dest: [config_1['name'], config_2['name']]} with open(os.path.join(self.config_dir, "test1.conf"), "w") as f: f.write(TestReadingConfigs.dict_to_ini(config_1) + TestReadingConfigs.dict_to_ini(config_2)) manager = DestinationToSourceMapper(init_config({}, {}, config_dir=self.config_dir)) self.assertEqual(manager.dests, set([expected_dest])) self.assertEqual(manager.sources, set([config_1['name'], config_2['name']])) self.assertEqual(manager.dest_to_sources_map, expected_mapping)
def testLineContinuationInConfig(self): """ Test that when a config line that starts with space or tab, it is treated as a continuation of the previous line. :return: """ with open(os.path.join(self.config_dir, "test1.conf"), "w") as f: f.write(""" [test1] type=esx server=http://1.2.3.4 value username=admin password=password owner=root filter_hosts=abc.efg.com """) manager = DestinationToSourceMapper( init_config({}, config_dir=self.config_dir)) self.assertEqual(len(manager.configs), 1) config = manager.configs[0][1] self.assertEqual(config.name, "test1") self.assertEqual(config["server"], 'http://1.2.3.4\nvalue')
def testMultipleConfigFiles(self): config_1 = combine_dicts(TestReadingConfigs.source_options_1, TestReadingConfigs.dest_options_1) config_2 = combine_dicts(TestReadingConfigs.source_options_2, TestReadingConfigs.dest_options_2) with open(os.path.join(self.config_dir, "test1.conf"), "w") as f: f.write(TestReadingConfigs.dict_to_ini(config_1)) with open(os.path.join(self.config_dir, "test2.conf"), "w") as f: f.write(TestReadingConfigs.dict_to_ini(config_2)) expected_dest_1 = Satellite6DestinationInfo( **TestReadingConfigs.dest_options_1) expected_dest_2 = Satellite6DestinationInfo( **TestReadingConfigs.dest_options_2) expected_mapping = { expected_dest_1: [config_1['name']], expected_dest_2: [config_2['name']] } manager = DestinationToSourceMapper(init_config({}, {}, config_dir=self.config_dir)) self.assertEqual(len(manager.configs), 2) self.assertEqual(manager.dest_to_sources_map, expected_mapping) self.assertEqual(manager.dests, set([expected_dest_1, expected_dest_2])) self.assertEqual(manager.sources, set([config_1['name'], config_2['name']])) result1 = manager.configs[0][1] result2 = manager.configs[1][1] self.assertIn(result1.name, ("test1", "test2")) if result1.name == "test2": result2, result1 = result1, result2 self.assert_config_contains_all(result1, config_1) self.assert_config_contains_all(result2, config_2)
def testCLIConfigOverridesDefaultDirectoryConfigs(self): cli_config_file_path = os.path.join(self.custom_config_dir, "my_file.conf") with open(cli_config_file_path, "w") as f: f.write(""" [valid_cli_section] server=5.5.5.5 username=admin1 password=password1 owner=owner1 env=env1 rhsm_hostname=abc1 """) cli_dict = {'configs': [cli_config_file_path]} with open(os.path.join(self.config_dir, "test1.conf"), "w") as f: f.write(""" [valid_default_dir_section] server=1.2.3.4 username=admin password=password owner=owner env=env rhsm_hostname=abc """) config_manager = DestinationToSourceMapper( init_config({}, cli_dict, config_dir=self.config_dir)) # There should be only one config, and that should be the one passed from the cli self.assertEqual(len(config_manager.configs), 1) config = config_manager.configs[0][1] self.assertEqual(config.name, "valid_cli_section") self.assertEqual(config["server"], "5.5.5.5") self.assertEqual(config["username"], "admin1") self.assertEqual(config["password"], "password1") self.assertEqual(config["owner"], "owner1") self.assertEqual(config["env"], "env1") self.assertEqual(config["rhsm_hostname"], "abc1")
def testInvalidAndValidConfigs(self): valid_config_name = "valid_config" with open(os.path.join(self.config_dir, "test1.conf"), "w") as f: f.write(""" [%(valid_config_name)s] type=esx server=1.2.3.4 username=admin password=password owner=owner rhsm_hostname=abc [invalid_missing_owner] type=esx server=1.2.3.4 username=admin password=password rhsm_hostname=abc """ % {'valid_config_name': valid_config_name}) config_manager = DestinationToSourceMapper( init_config({}, config_dir=self.config_dir)) # There should be only one config, and that should be the one that is valid self.assertEqual(len(config_manager.configs), 1) self.assertEqual(config_manager.configs[0][1].name, valid_config_name)
def testLibvirtConfig(self): with open(os.path.join(self.config_dir, "test1.conf"), "w") as f: f.write(""" [test1] type=libvirt server=1.2.3.4 username=admin password=password owner=root """) manager = DestinationToSourceMapper( init_config({}, config_dir=self.config_dir)) self.assertEqual(len(manager.configs), 1) config = manager.configs[0][1] self.assertEqual(config.name, "test1") self.assertEqual(config["type"], "libvirt") # The following server value is different than what is provided above as it has been # processed by the libvirt config section validation. # TODO decouple this from the libvirt config section (for testing only) self.assertEqual(config["server"], "qemu+ssh://[email protected]/system?no_tty=1") self.assertEqual(config["username"], "admin") self.assertEqual(config["password"], "password") self.assertEqual(config["owner"], "root")
def testConfigFileExtensions(self): with open(os.path.join(self.config_dir, "test1.conf"), "w") as f: f.write(""" [test1] type=esx server=1.2.3.4 username=admin password=password owner=root rhsm_username=rhsm_admin1 rhsm_password=rhsm_password1 rhsm_hostname=host1 rhsm_port=12341 rhsm_prefix=prefix1 rhsm_proxy_hostname=proxyhost1 rhsm_proxy_port=43211 rhsm_proxy_user=proxyuser1 rhsm_proxy_password=proxypass1 rhsm_insecure=1 """) with open(os.path.join(self.config_dir, "test2.conf.bk"), "w") as f: f.write(""" [test2] type=hyperv server=1.2.3.5 username=admin password=password owner=root rhsm_username=rhsm_admin2 rhsm_password=rhsm_password2 rhsm_hostname=host2 rhsm_port=12342 rhsm_prefix=prefix2 rhsm_proxy_hostname=proxyhost2 rhsm_proxy_port=43212 rhsm_proxy_user=proxyuser2 rhsm_proxy_password=proxypass2 rhsm_insecure=2 """) manager = DestinationToSourceMapper( init_config({}, config_dir=self.config_dir)) self.assertEqual(len(manager.configs), 1) name, config = manager.configs[0] self.assertEqual(name, "test1") # TODO decouple tests like these from the ConfigSections that they imply # The values used here reflect the expected output of the EsxConfigSection validation # (If in case these seem strange) self.assertEqual(config["type"], "esx") self.assertEqual(config["server"], "https://1.2.3.4") self.assertEqual(config["username"], "admin") self.assertEqual(config["password"], "password") self.assertEqual(config["owner"], "root") self.assertEqual(config["rhsm_username"], 'rhsm_admin1') self.assertEqual(config["rhsm_password"], 'rhsm_password1') self.assertEqual(config["rhsm_hostname"], 'host1') self.assertEqual(config["rhsm_port"], '12341') self.assertEqual(config["rhsm_prefix"], 'prefix1') self.assertEqual(config["rhsm_proxy_hostname"], 'proxyhost1') self.assertEqual(config["rhsm_proxy_port"], '43211') self.assertEqual(config["rhsm_proxy_user"], 'proxyuser1') self.assertEqual(config["rhsm_proxy_password"], 'proxypass1') self.assertEqual(config["rhsm_insecure"], '1')
def testConfigFileExtensions(self): with open(os.path.join(self.config_dir, "test1.conf"), "w") as f: f.write(""" [test1] type=esx server=1.2.3.4 username=admin password=password owner=root env=staging rhsm_username=rhsm_admin1 rhsm_password=rhsm_password1 rhsm_hostname=host1 rhsm_port=12341 rhsm_prefix=prefix1 rhsm_proxy_hostname=proxyhost1 rhsm_proxy_port=43211 rhsm_proxy_user=proxyuser1 rhsm_proxy_password=proxypass1 rhsm_insecure=1 """) with open(os.path.join(self.config_dir, "test2.conf.bk"), "w") as f: f.write(""" [test2] type=hyperv server=1.2.3.5 username=admin password=password owner=root env=staging rhsm_username=rhsm_admin2 rhsm_password=rhsm_password2 rhsm_hostname=host2 rhsm_port=12342 rhsm_prefix=prefix2 rhsm_proxy_hostname=proxyhost2 rhsm_proxy_port=43212 rhsm_proxy_user=proxyuser2 rhsm_proxy_password=proxypass2 rhsm_insecure=2 """) manager = DestinationToSourceMapper(init_config({}, {}, config_dir=self.config_dir)) self.assertEqual(len(manager.configs), 1) name, config = manager.configs[0] self.assertEqual(name, "test1") # TODO decouple tests like these from the ConfigSections that they imply # The values used here reflect the expected output of the EsxConfigSection validation # (If in case these seem strange) self.assertEqual(config["type"], "esx") self.assertEqual(config["server"], "https://1.2.3.4") self.assertEqual(config["username"], "admin") self.assertEqual(config["password"], "password") self.assertEqual(config["owner"], "root") self.assertEqual(config["env"], "staging") self.assertEqual(config["rhsm_username"], 'rhsm_admin1') self.assertEqual(config["rhsm_password"], 'rhsm_password1') self.assertEqual(config["rhsm_hostname"], 'host1') self.assertEqual(config["rhsm_port"], '12341') self.assertEqual(config["rhsm_prefix"], 'prefix1') self.assertEqual(config["rhsm_proxy_hostname"], 'proxyhost1') self.assertEqual(config["rhsm_proxy_port"], '43211') self.assertEqual(config["rhsm_proxy_user"], 'proxyuser1') self.assertEqual(config["rhsm_proxy_password"], 'proxypass1') self.assertEqual(config["rhsm_insecure"], '1')
def parse_options(): """ This function parses all options from command line and environment variables :return: Tuple of logger and options """ # These options are deprecated DEPRECATED_OPTIONS = ['log_per_config', 'log_dir', 'log_file', 'reporter_id', 'virt_type', 'owner', 'env', 'server', 'username', 'password', 'sat_server', 'sat_username', 'sat_password', 'sm_type'] VIRT_TYPE_OPTIONS = ['owner', 'server', 'username', 'password'] SAT_OPTION_MAP = {'sat_server': 'satellite-server', 'sat_username': '******', 'sat_password': '******'} # Read command line arguments first cli_options, errors, defaults = parse_cli_arguments() if 'version' in cli_options and cli_options['version']: print(get_version()) exit(os.EX_OK) if 'status' in cli_options and ('print' in cli_options or 'oneshot' in cli_options): print("You may not use the --print or --one-shot options with the --status option.") exit(os.EX_USAGE) if 'json' in cli_options and 'status' not in cli_options: print("The --json option must only be used with the --status option.") exit(os.EX_USAGE) # Create the effective config that virt-who will use to run effective_config = init_config(cli_options) # Ensure validation errors during effective config creation are logged errors.extend(effective_config.validation_messages) logger = log.getLogger(config=effective_config, queue=False) used_deprecated_cli_options = [] for option in DEPRECATED_OPTIONS: display_option = option if option in cli_options and not cli_options[option] == defaults[option]: if option == 'virt_type' or option == 'sm_type': display_option = cli_options[option] elif any(option in s for s in VIRT_TYPE_OPTIONS): display_option = '%s-%s' % (cli_options['virt_type'], option) elif option in SAT_OPTION_MAP: display_option = SAT_OPTION_MAP[option] used_deprecated_cli_options.append(display_option) # These two flags set the value of sm_type to the default value ('sam'), so ArgumentParser will not # include them in the cli_options list, thus we have to manually check for and add them to # the deprecated list for them to be included in the warning: if '--satellite6' in _sys.argv: used_deprecated_cli_options.append('satellite6') if '--sam' in _sys.argv: used_deprecated_cli_options.append('sam') deprecated_options_msg = ( "The following cli options: %s are deprecated and will be removed " "in the next release. Please see 'man virt-who-config' for details on adding " "a configuration section." ) if used_deprecated_cli_options: logger.warning(deprecated_options_msg % ', '.join('--' + item for item in used_deprecated_cli_options)) # Log pending errors for err in errors: method = getattr(logger, err[0]) if method is not None and err[0] == 'error': method(err[1]) return logger, effective_config
def test_basic_config(self): with open(os.path.join(self.config_dir, "test.conf"), "w") as f: f.write(TestReadingConfigs.dict_to_ini(default_config_values)) config = init_config({}, config_dir=self.config_dir) six.assertCountEqual(self, list(config.keys()), ['test', 'global']) self.assert_config_equals_default(config['test'])
def test_empty_config(self): config = init_config({}, self.config_dir) six.assertCountEqual(self, list(config.keys()), [VW_GLOBAL, VW_ENV_CLI_SECTION_NAME])
def parse_options(): """ This function parses all options from command line and environment variables :return: Tuple of logger and options """ # These options are deprecated DEPRECATED_OPTIONS = ['log_per_config', 'log_dir', 'log_file', 'reporter_id', 'virt_type', 'owner', 'env', 'server', 'username', 'password', 'sat_server', 'sat_username', 'sat_password', 'sm_type'] VIRT_TYPE_OPTIONS = ['owner', 'server', 'username', 'password'] SAT_OPTION_MAP = {'sat_server':'satellite-server', 'sat_username':'******', 'sat_password':'******'} # Read command line arguments first cli_options, errors, defaults = parse_cli_arguments() if 'version' in cli_options and cli_options['version']: print(get_version()) exit(os.EX_OK) # Read configuration env. variables env_options = read_config_env_variables() if six.PY2: # Read environments variables for virtualization backends env_options, env_errors = read_vm_backend_env_variables(env_options) errors.extend(env_errors) # Create the effective config that virt-who will use to run effective_config = init_config(env_options, cli_options) # Ensure validation errors during effective config creation are logged errors.extend(effective_config.validation_messages) logger = log.getLogger(config=effective_config, queue=False) used_deprecated_cli_options = [] for option in DEPRECATED_OPTIONS: display_option = option if option in cli_options and not cli_options[option] == defaults[option]: if option == 'virt_type' or option == 'sm_type': display_option = cli_options[option] elif any(option in s for s in VIRT_TYPE_OPTIONS): display_option = '%s-%s' % (cli_options['virt_type'], option) elif option in SAT_OPTION_MAP: display_option = SAT_OPTION_MAP[option] used_deprecated_cli_options.append(display_option) # These two flags set the value of sm_type to the default value ('sam'), so ArgumentParser will not # include them in the cli_options list, thus we have to manually check for and add them to # the deprecated list for them to be included in the warning: if '--satellite6' in _sys.argv: used_deprecated_cli_options.append('satellite6') if '--sam' in _sys.argv: used_deprecated_cli_options.append('sam') deprecated_options_msg = "The following cli options: %s are deprecated and will be removed " \ "in the next release. Please see 'man virt-who-config' for details on adding a configuration "\ "section." if used_deprecated_cli_options: logger.warning(deprecated_options_msg % ', '.join('--' + item for item in used_deprecated_cli_options)) # Log pending errors for err in errors: method = getattr(logger, err[0]) if method is not None and err[0] == 'error': method(err[1]) return logger, effective_config