def test_case_insensitve_assert_host_id(self):
     hostmap = HostMap()
     hostmap.add_hostref("Test1")
     hostmap.assert_host_id("test1", "host-test1-forcedid")
     self.perform_mapping(hostmap)
     self.assertEquals(hostmap.get_hostid("test1"), "host-test1-forcedid")
     self.assertEquals(hostmap.get_hostid("Test1"), "host-test1-forcedid")
    def test_clear_mappings(self):
        hostmap = HostMap()
        self.perform_mapping(hostmap)
        self.assertEquals(len(hostmap.all_hostids()), 0)

        hostmap.add_hostref("test")

        self.perform_mapping(hostmap)
        self.assertEquals(len(hostmap.all_hostids()), 1)

        hostmap.clear_mappings()

        self.perform_mapping(hostmap)
        self.assertEquals(len(hostmap.all_hostids()), 0)
    def test_localdomain(self):
        # See ZPS-1244 for details- in short, any hostname ending in .localdomain
        # will resolve to 127.0.0.1 on our hosts, and some tripleO deployment models
        # end up with hosts self-identifying with such hostnames.
        # While this will cause other issues, at least hostmap should not conflate
        # them.
        hostmap = HostMap()
        hostmap.add_hostref("test1.localdomain")
        hostmap.add_hostref("test2.localdomain")

        self.perform_mapping(hostmap)

        # We want this to resolve to two host IDs, not be consolidated into one.
        self.assertEquals(len(hostmap.all_hostids()), 2)
    def afterSetUp(self):
        super(TestCVIM, self).afterSetUp()

        dc = self.dmd.Devices.createOrganizer(
            '/Devices/OpenStack/Infrastructure')
        dc.setZenProperty('zPythonClass',
                          'ZenPacks.zenoss.OpenStackInfrastructure.Endpoint')
        dc.setZenProperty('zOpenStackHostDeviceClass',
                          '/Server/SSH/Linux/NovaHost')
        dc.setZenProperty('zOpenStackHostLocalDomain', '')

        self.d = dc.createInstance('zenoss.OpenStackInfrastructure.testDevice')
        self.linux_dc = self.dmd.Devices.createOrganizer(
            '/Server/SSH/Linux/NovaHost')
        self.hostmap = HostMap()
    def test_short_fqdn(self):
        hostmap = HostMap()
        hostmap.add_hostref("test1")
        hostmap.add_hostref("test1.example.com")
        hostmap.add_hostref("test2")
        hostmap.add_hostref("test2.example.com")
        self.perform_mapping(hostmap)

        # These should be condensed down, if they resolve to the
        # same IP.  Otherwise, it does not assume that they are the same.
        self.assertEquals(len(hostmap.all_hostids()), 2)

        # It should use the longer name, as well.
        self.assertEquals(hostmap.get_hostid("test1"), "host-test1.example.com")
        self.assertEquals(hostmap.get_hostid("test2"), "host-test2.example.com")
    def test_mixed_fqdns(self):
        # This is replicating results seen in the QA environment where
        # one host was being identified as 2, due to a mix of fqdn and short
        # names being reported in the nova services list.  (ZPS-1709)
        hostmap = HostMap()

        hostmap.add_hostref("ip-10-111-5-173", source="nova services")
        hostmap.add_hostref("ip-10-111-5-173.zenoss.loc", source="nova services")
        hostmap.add_hostref("ip-10-111-5-173@lvm", source="cinder services")
        hostmap.add_hostref("ip-10-111-5-173.zenoss.loc@lvm", source="cinder services")
        hostmap.add_hostref("10.111.5.173", source="Nova API URL")

        self.perform_mapping(hostmap)

        # We want this to consolidated into one hostid.
        self.assertEquals(len(hostmap.all_hostids()), 1)
    def test_suffixed_names(self):
        hostmap = HostMap()
        hostmap.add_hostref("test1:somecrazysuffix_thatisreallylong")
        hostmap.add_hostref("test1.example.com")
        hostmap.add_hostref("test1")
        hostmap.add_hostref("test2.example.com")
        self.perform_mapping(hostmap)

        # These should be condensed down, if they resolve to the
        # same IP.  Otherwise, it does not assume that they are the same.
        self.assertEquals(len(hostmap.all_hostids()), 2)

        # It should use the longer name, as well, but not the crazy suffixed
        # one..
        self.assertEquals(hostmap.get_hostid("test1"), "host-test1.example.com")
        self.assertEquals(hostmap.get_hostid("test1:somecrazysuffix_thatisreallylong"), "host-test1.example.com")
    def preprocess_hosts(self, config, results):
        # spin through the collected data, pre-processing all the fields
        # that reference hosts to have consistent host IDs, so that the
        # process() method does not have to worry about hostname -> ID
        # mapping at all.

        hostmap = HostMap()
        results['hostmap'] = hostmap

        ds0 = config.datasources[0]

        # load in previously modeled mappings..
        hostmap.thaw_mappings(ds0.params['host_mappings'])

        for service in results['services']:
            if 'host' in service:
                hostmap.add_hostref(service['host'], source="nova services")

        for mapping in ds0.zOpenStackHostMapToId:
            try:
                hostref, hostid = mapping.split("=")
                hostmap.assert_host_id(hostref, hostid)
            except Exception:
                log.error("Invalid value in zOpenStackHostMapToId: %s",
                          mapping)

        for mapping in ds0.zOpenStackHostMapSame:
            try:
                hostref1, hostref2 = mapping.split("=")
                hostmap.assert_same_host(hostref1,
                                         hostref2,
                                         source='zOpenStackHostMapSame')
            except Exception:
                log.error("Invalid value in zOpenStackHostMapSame: %s",
                          mapping)

        # generate host IDs
        yield hostmap.perform_mapping()

        # replace all references to hosts with their host IDs, so
        # process() doesn't have to think about this stuff.
        for service in results['services']:
            if 'host' in service:
                service['host'] = hostmap.get_hostid(service['host'])
    def test_freeze_thaw_mappings(self):
        hostmap = HostMap()
        hostmap.add_hostref("test1")
        hostmap.add_hostref("test2")
        hostmap.add_hostref("test3")
        hostmap.assert_host_id("test2", "host-test2-forcedid")
        self.perform_mapping(hostmap)
        self.assertEquals(len(hostmap.all_hostids()), 3)

        frozen = hostmap.freeze_mappings()

        hostmap.clear_mappings()
        hostmap.thaw_mappings(frozen)
        hostmap.add_hostref("test1")
        hostmap.add_hostref("test2")
        hostmap.add_hostref("test3")
        self.perform_mapping(hostmap)
        self.assertEquals(len(hostmap.all_hostids()), 3)

        self.assertEquals(hostmap.get_hostid("test1"), "host-test1")
        self.assertEquals(hostmap.get_hostid("test2"), "host-test2-forcedid")
        self.assertEquals(hostmap.get_hostid("test3"), "host-test3")