예제 #1
0
 def init_effective_config(self):
     """
     This method is executed before each unit test
     """
     self.effective_config = EffectiveConfig()
     self.config_section = CustomConfigSection(CUSTOM_SECTION_NAME,
                                               self.effective_config)
     # Fill config section with some values
     for key, value in CUSTOM_SECTION_VALUES.items():
         self.config_section[key] = value
     self.effective_config[CUSTOM_SECTION_NAME] = self.config_section
class TestLibvirtEffectiveConfig(TestBase):
    """
    Test reading config section from text using EffectiveConfig
    """

    def __init__(self, *args, **kwargs):
        super(TestLibvirtEffectiveConfig, self).__init__(*args, **kwargs)
        self.effective_config = None

    def init_effective_config(self):
        """
        This method is executed before each unit test
        """
        self.effective_config = EffectiveConfig()

    def test_read_sam_effective_config_from_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(LIBVIRT_SECTION_TEXT)
        conf = parse_file(os.path.join(config_dir, "test.conf"))
        self.init_effective_config()
        conf_values = conf.pop(MY_SECTION_NAME)
        self.effective_config[MY_SECTION_NAME] = ConfigSection.from_dict(
            conf_values,
            MY_SECTION_NAME,
            self.effective_config
        )
        self.assertEqual(type(self.effective_config[MY_SECTION_NAME]), LibvirtdConfigSection)
        self.assertEqual(self.effective_config[MY_SECTION_NAME]['server'], 'ssh://192.168.122.10')
        validate_messages = self.effective_config.validate()
        self.assertIsNotNone(validate_messages)
        del self.effective_config[MY_SECTION_NAME]

    def test_read_sat5_effective_config_from_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(LIBVIRT_SAT5_SECTION_TEXT)
        conf = parse_file(os.path.join(config_dir, "test.conf"))
        self.init_effective_config()
        conf_values = conf.pop(MY_SAT5_SECTION_NAME)
        self.effective_config[MY_SAT5_SECTION_NAME] = ConfigSection.from_dict(
            conf_values,
            MY_SAT5_SECTION_NAME,
            self.effective_config
        )
        self.assertEqual(type(self.effective_config[MY_SAT5_SECTION_NAME]), LibvirtdConfigSection)
        validate_messages = self.effective_config.validate()
        self.assertEqual(self.effective_config[MY_SAT5_SECTION_NAME]['sat_server'], 'https://sat5.company.com/XMLRPC')
        self.assertEqual(self.effective_config[MY_SAT5_SECTION_NAME]['sm_type'], 'satellite')
        self.assertIsNotNone(validate_messages)
        del self.effective_config[MY_SAT5_SECTION_NAME]
예제 #3
0
class TestLibvirtEffectiveConfig(TestBase):
    """
    Test reading config section from text using EffectiveConfig
    """
    def __init__(self, *args, **kwargs):
        super(TestLibvirtEffectiveConfig, self).__init__(*args, **kwargs)
        self.effective_config = None

    def init_effective_config(self):
        """
        This method is executed before each unit test
        """
        self.effective_config = EffectiveConfig()

    def test_read_sam_effective_config_from_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(LIBVIRT_SECTION_TEXT)
        conf = parse_file(os.path.join(config_dir, "test.conf"))
        self.init_effective_config()
        conf_values = conf.pop(MY_SECTION_NAME)
        self.effective_config[MY_SECTION_NAME] = ConfigSection.from_dict(
            conf_values, MY_SECTION_NAME, self.effective_config)
        self.assertEqual(type(self.effective_config[MY_SECTION_NAME]),
                         LibvirtdConfigSection)
        self.assertEqual(self.effective_config[MY_SECTION_NAME]['server'],
                         'ssh://192.168.122.10')
        validate_messages = self.effective_config.validate()
        self.assertIsNotNone(validate_messages)
        del self.effective_config[MY_SECTION_NAME]

    def test_read_sat5_effective_config_from_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(LIBVIRT_SAT5_SECTION_TEXT)
        conf = parse_file(os.path.join(config_dir, "test.conf"))
        self.init_effective_config()
        conf_values = conf.pop(MY_SAT5_SECTION_NAME)
        self.effective_config[MY_SAT5_SECTION_NAME] = ConfigSection.from_dict(
            conf_values, MY_SAT5_SECTION_NAME, self.effective_config)
        self.assertEqual(type(self.effective_config[MY_SAT5_SECTION_NAME]),
                         LibvirtdConfigSection)
        validate_messages = self.effective_config.validate()
        self.assertEqual(
            self.effective_config[MY_SAT5_SECTION_NAME]['sat_server'],
            'https://sat5.company.com/XMLRPC')
        self.assertEqual(
            self.effective_config[MY_SAT5_SECTION_NAME]['sm_type'],
            'satellite')
        self.assertIsNotNone(validate_messages)
        del self.effective_config[MY_SAT5_SECTION_NAME]
예제 #4
0
 def test_effective_config_filter_params(self):
     effective_config = EffectiveConfig()
     desired_params = ['foo', 'bar', 'foo.bar']
     values = {
         'foo': 'Foo',
         'bar': 'Bar',
         'bar.foo': 'Bar Foo',
         'pub': 'Pub Foo'
     }
     matching, non_matching = effective_config.filter_parameters(
         desired_params, values)
     expected_matching = {'foo': 'Foo', 'bar': 'Bar'}
     expected_non_matching = {'bar.foo': 'Bar Foo', 'pub': 'Pub Foo'}
     self.assertEqual(matching, expected_matching)
     self.assertEqual(non_matching, expected_non_matching)
예제 #5
0
    def filter_hosts(self, filter_something, filter_type=''):
        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=esx
server=does.not.exist
username=username
password=password
owner=owner
env=env
{filter_something}
{filter_type}
""".format(filter_something=filter_something, filter_type=filter_type))
        conf = parse_file(os.path.join(config_dir, "test.conf"))
        test_conf_values = conf.pop('test')
        effective_config = EffectiveConfig()
        effective_config['test'] = VirtConfigSection.from_dict(
            test_conf_values,
            'test',
            effective_config
        )
        effective_config.validate()
        config_manager = DestinationToSourceMapper(effective_config)
        self.assertEqual(len(config_manager.configs), 1)
        config = config_manager.configs[0][1]

        included_hypervisor = Hypervisor('12345', guestIds=[
            Guest('guest-2', xvirt.CONFIG_TYPE, Guest.STATE_RUNNING),
        ])
        excluded_hypervisor = Hypervisor('00000', guestIds=[
            Guest('guest-1', xvirt.CONFIG_TYPE, Guest.STATE_RUNNING),
        ])

        assoc = {
            'hypervisors': [
                excluded_hypervisor,
                included_hypervisor,
            ]
        }

        report = HostGuestAssociationReport(config, assoc)
        assert report.association == {
            'hypervisors': [
                included_hypervisor
            ]
        }
예제 #6
0
    def test_satellite_config_file(self):
        # Username and password are required for a valid sat5 destination
        with open(os.path.join(self.config_dir, "test.conf"), "w") as f:
            f.write("""
[test]
type=libvirt
sat_server=sat.example.com
sat_username=sat_username
sat_password=sat_password
""")
        conf = parse_file(os.path.join(self.config_dir, "test.conf"))
        effective_config = EffectiveConfig()
        conf_values = conf.pop("test")
        effective_config["test"] = ConfigSection.from_dict(
            conf_values,
            "test",
            effective_config
        )
        config_manager = DestinationToSourceMapper(effective_config)
        self.assertEqual(len(config_manager.configs), 1)
        # There should only be one destination detected
        self.assertEqual(len(config_manager.dests), 1)
        # Which should be a Satellite5DestinationInfo
        dest_info = config_manager.dests.pop()
        self.assertTrue(isinstance(dest_info, Satellite5DestinationInfo), 'The destination info '
                                                                          'we got was not of the '
                                                                          'expected type')
        manager = Manager.fromInfo(self.logger, effective_config, dest_info)
        self.assertTrue(isinstance(manager, Satellite))
        self.assertEqual(dest_info.sat_server, 'sat.example.com')
예제 #7
0
 def test_read_effective_config_from_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(CONFIG_SECTION_TEXT)
     conf = parse_file(os.path.join(config_dir, "test.conf"))
     effective_config = EffectiveConfig()
     conf_values = conf.pop(CUSTOM_SECTION_NAME)
     effective_config[CUSTOM_SECTION_NAME] = ConfigSection.from_dict(
         conf_values, CUSTOM_SECTION_NAME, effective_config)
     self.assertEqual(type(effective_config[CUSTOM_SECTION_NAME]),
                      CustomConfigSection)
     self.assertEqual(effective_config[CUSTOM_SECTION_NAME]['my_str'],
                      'foo')
     validate_messages = effective_config.validate()
     self.assertEqual(validate_messages, [])
예제 #8
0
    def filter_hosts(self, filter_something, filter_type=''):
        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=esx
server=does.not.exist
username=username
password=password
owner=owner
env=env
{filter_something}
{filter_type}
""".format(filter_something=filter_something, filter_type=filter_type))
        conf = parse_file(os.path.join(config_dir, "test.conf"))
        test_conf_values = conf.pop('test')
        effective_config = EffectiveConfig()
        effective_config['test'] = VirtConfigSection.from_dict(
            test_conf_values, 'test', effective_config)
        effective_config.validate()
        config_manager = DestinationToSourceMapper(effective_config)
        self.assertEqual(len(config_manager.configs), 1)
        config = config_manager.configs[0][1]

        included_hypervisor = Hypervisor('12345',
                                         guestIds=[
                                             Guest('guest-2',
                                                   xvirt.CONFIG_TYPE,
                                                   Guest.STATE_RUNNING),
                                         ])
        excluded_hypervisor = Hypervisor('00000',
                                         guestIds=[
                                             Guest('guest-1',
                                                   xvirt.CONFIG_TYPE,
                                                   Guest.STATE_RUNNING),
                                         ])

        assoc = {
            'hypervisors': [
                excluded_hypervisor,
                included_hypervisor,
            ]
        }

        report = HostGuestAssociationReport(config, assoc)
        assert report.association == {'hypervisors': [included_hypervisor]}
예제 #9
0
    def test_sm_config_override(self, initConfig, HTTPSConnection,
                                RhsmProxyHTTPSConnection):
        """Test if overriding options from rhsm.conf works."""

        conn = MagicMock()
        conn.getresponse.return_value.status = 200
        conn.getresponse.return_value.read.return_value = '{"result": "ok"}'
        HTTPSConnection.return_value = conn
        RhsmProxyHTTPSConnection.return_value = conn

        def config_get(section, key):
            return {
                'server/proxy_hostname': 'proxy.server.test',
                'rhsm/consumerCertDir': '',
                'server/hostname': 'server.test',
                'server/port': '8081',
                'server/prefix': 'old_prefix',
            }.get('%s/%s' % (section, key), None)

        initConfig.return_value.get.side_effect = config_get
        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
rhsm_hostname=host
rhsm_port=8080
rhsm_prefix=/prefix
rhsm_proxy_hostname=
rhsm_proxy_port=8443
rhsm_insecure=1
rhsm_username=user
rhsm_password=passwd
""")

        conf = parse_file(os.path.join(config_dir, "test.conf"))
        effective_config = EffectiveConfig()
        conf_values = conf.pop("test")
        effective_config["test"] = VirtConfigSection.from_dict(
            conf_values, "test", effective_config)
        config_manager = DestinationToSourceMapper(effective_config)
        self.assertEqual(len(config_manager.configs), 1)
        config = config_manager.configs[0]
        manager = Manager.fromOptions(self.logger, Mock(), config)
        self.assertTrue(isinstance(manager, SubscriptionManager))
        self.assertEqual(config.rhsm_hostname, 'host')
        self.assertEqual(config.rhsm_port, '8080')

        manager._connect(config)
        self.assertFalse(RhsmProxyHTTPSConnection.called,
                         "It shouldn't use proxy")
        self.assertTrue(HTTPSConnection.called)
        conn.request.assert_called_with('GET',
                                        '/prefix/status/',
                                        body=ANY,
                                        headers=ANY)
 def init_effective_config(self):
     """
     This method is executed before each unit test
     """
     self.effective_config = EffectiveConfig()
예제 #11
0
 def init_effective_config(self):
     """
     This method is executed before each unit test
     """
     self.effective_config = EffectiveConfig()
예제 #12
0
class TestEffectiveConfig(TestBase):
    """
    Class used for testing EffectiveConfig
    """
    def __init__(self, *args, **kwargs):
        super(TestEffectiveConfig, self).__init__(*args, **kwargs)
        self.effective_config = None
        self.config_section = None

    def init_effective_config(self):
        """
        This method is executed before each unit test
        """
        self.effective_config = EffectiveConfig()
        self.config_section = CustomConfigSection(CUSTOM_SECTION_NAME,
                                                  self.effective_config)
        # Fill config section with some values
        for key, value in CUSTOM_SECTION_VALUES.items():
            self.config_section[key] = value
        self.effective_config[CUSTOM_SECTION_NAME] = self.config_section

    def test_effective_config_validate(self):
        self.init_effective_config()
        validate_messages = self.effective_config.validate()
        self.assertEqual(validate_messages, [])

    def test_effective_config_is_valid(self):
        self.init_effective_config()
        self.effective_config.validate()
        is_valid = self.effective_config.is_valid()
        self.assertEqual(is_valid, True)

    def test_effective_config_virt_sections(self):
        self.init_effective_config()
        self.effective_config.validate()
        virt_sections = self.effective_config.virt_sections()
        self.assertEqual(len(virt_sections), 1)
        self.assertEqual(virt_sections[0][0], CUSTOM_SECTION_NAME)

    def test_effective_config_is_value_default(self):
        self.init_effective_config()
        self.effective_config.validate()
        result = self.effective_config.is_default(CUSTOM_SECTION_NAME,
                                                  'my_str')
        self.assertEqual(result, False)
        result = self.effective_config.is_default(CUSTOM_SECTION_NAME,
                                                  'another_str')
        self.assertEqual(result, True)

    def test_effective_config_items(self):
        self.init_effective_config()
        self.effective_config.validate()
        for key, item in self.effective_config.items():
            self.assertEqual(key, CUSTOM_SECTION_NAME)
            self.assertEqual(type(item), CustomConfigSection)

    def test_effective_config_del_item(self):
        self.init_effective_config()
        self.effective_config.validate()
        self.assertEqual(len(self.effective_config), 1)
        del self.effective_config[CUSTOM_SECTION_NAME]
        self.assertEqual(len(self.effective_config), 0)

    def test_effective_config_filter_params(self):
        effective_config = EffectiveConfig()
        desired_params = ['foo', 'bar', 'foo.bar']
        values = {
            'foo': 'Foo',
            'bar': 'Bar',
            'bar.foo': 'Bar Foo',
            'pub': 'Pub Foo'
        }
        matching, non_matching = effective_config.filter_parameters(
            desired_params, values)
        expected_matching = {'foo': 'Foo', 'bar': 'Bar'}
        expected_non_matching = {'bar.foo': 'Bar Foo', 'pub': 'Pub Foo'}
        self.assertEqual(matching, expected_matching)
        self.assertEqual(non_matching, expected_non_matching)

    def test_read_effective_config_from_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(CONFIG_SECTION_TEXT)
        conf = parse_file(os.path.join(config_dir, "test.conf"))
        effective_config = EffectiveConfig()
        conf_values = conf.pop(CUSTOM_SECTION_NAME)
        effective_config[CUSTOM_SECTION_NAME] = ConfigSection.from_dict(
            conf_values, CUSTOM_SECTION_NAME, effective_config)
        self.assertEqual(type(effective_config[CUSTOM_SECTION_NAME]),
                         CustomConfigSection)
        self.assertEqual(effective_config[CUSTOM_SECTION_NAME]['my_str'],
                         'foo')
        validate_messages = effective_config.validate()
        self.assertEqual(validate_messages, [])