Example #1
0
class ApplyDataMapTest(BaseTestCase):

    def afterSetUp(self):
        super(ApplyDataMapTest, self).afterSetUp()
        self.adm = ApplyDataMap()

    def test_updateObject_encoding(self):
        for enc in ('ascii', 'latin-1', 'utf-8', 'utf-16'):
            obj = _obj()
            obj.zCollectorDecoding = enc
            objmap = eval(enc.replace('-','')+'_objmap')
            self.adm._updateObject(obj, objmap)
            for key in objmap:
                self.assertEqual(getattr(obj, key), objmap[key].decode(enc))

    def test_applyDataMap_relmap(self):
        dmd = self.dmd
        class datamap(list):
            compname = "a/b"
            relname  = "c"

        class Device(object):

            def deviceClass(self):
                return dmd.Devices

            class dmd:
                "Used for faking sync()"
                class _p_jar:
                    @staticmethod
                    def sync():
                        pass

            def getObjByPath(self, path):
                return reduce(getattr, path.split("/"), self)
            def getId(self):
                return "testDevice"

            class a:
                class b:
                    class c:
                        "The relationship to populate"
                        @staticmethod
                        def objectIdsAll():
                            "returns the list of object ids in this relationship"
                            return []
        self.adm.applyDataMap(Device(), datamap(), datamap.relname, datamap.compname)
class ApplyDataMapTest(BaseTestCase):
    def afterSetUp(self):
        super(ApplyDataMapTest, self).afterSetUp()
        self.adm = ApplyDataMap()

    def test_updateObject_encoding(self):
        for enc in ('ascii', 'latin-1', 'utf-8', 'utf-16'):
            obj = _obj()
            obj.zCollectorDecoding = enc
            objmap = eval(enc.replace('-', '') + '_objmap')
            self.adm._updateObject(obj, objmap)
            for key in objmap:
                self.assertEqual(getattr(obj, key), objmap[key].decode(enc))

    def test_applyDataMap_relmap(self):
        dmd = self.dmd

        class datamap(list):
            compname = "a/b"
            relname = "c"

        class Device(object):
            def deviceClass(self):
                return dmd.Devices

            class dmd:
                "Used for faking sync()"

                class _p_jar:
                    @staticmethod
                    def sync():
                        pass

            def getObjByPath(self, path):
                return reduce(getattr, path.split("/"), self)

            def getId(self):
                return "testDevice"

            class a:
                class b:
                    class c:
                        "The relationship to populate"

                        @staticmethod
                        def objectIdsAll():
                            "returns the list of object ids in this relationship"
                            return []

        self.adm.applyDataMap(Device(), datamap(), datamap.relname,
                              datamap.compname)

    def test_applyDataMap_relmapException(self):
        '''test_applyDataMap_exception is mostly the same as test_applyDataMap_relmap
    	     - difference #1: compname is commented out
	     - difference #2: with self.assertRaises(AttributeError) is added
    	'''
        dmd = self.dmd

        class datamap(list):
            #compname = "a/b"
            relname = "c"

        class Device(object):
            def deviceClass(self):
                return dmd.Devices

            class dmd:
                "Used for faking sync()"

                class _p_jar:
                    @staticmethod
                    def sync():
                        pass

            def getObjByPath(self, path):
                return reduce(getattr, path.split("/"), self)

            class a:
                class b:
                    class c:
                        "The relationship to populate"

                        @staticmethod
                        def objectIdsAll():
                            "returns the list of object ids in this relationship"
                            return []

        with self.assertRaises(AttributeError) as theException:
            self.adm.applyDataMap(Device(), datamap(), datamap.relname,
                                  datamap.compname)

        self.assertEqual(theException.exception.message,
                         "type object 'datamap' has no attribute 'compname'")

    def testNoChangeAllComponentsLocked(self):
        device = self.dmd.Devices.createInstance('testDevice')
        # Create an IP interface
        device.os.addIpInterface('eth0', False)
        iface = device.os.interfaces._getOb('eth0')
        iface.lockFromDeletion()

        # Apply a RelMap with no interfaces
        relmap = RelationshipMap("interfaces", "os",
                                 "Products.ZenModel.IpInterface")
        self.assertFalse(self.adm._applyDataMap(device, relmap))

        self.assertEquals(1, len(device.os.interfaces))
class ApplyDataMapTest(BaseTestCase):

    def afterSetUp(self):
        super(ApplyDataMapTest, self).afterSetUp()
        self.adm = ApplyDataMap()

    def test_updateObject_encoding(self):
        for enc in ('ascii', 'latin-1', 'utf-8', 'utf-16'):
            obj = _obj()
            obj.zCollectorDecoding = enc
            objmap = eval(enc.replace('-','')+'_objmap')
            self.adm._updateObject(obj, objmap)
            for key, val in objmap.items():
                self.assertEqual(getattr(obj, key), val.decode(enc))

    def test_applyDataMap_relmap(self):
        dmd = self.dmd
        class datamap(list):
            compname = "a/b"
            relname  = "c"

        class Device(object):

            def deviceClass(self):
                return dmd.Devices

            def getPrimaryId(self):
                return "{}/{}".format(self.deviceClass().getPrimaryId(), self.getId())

            class dmd:
                "Used for faking sync()"
                class _p_jar:
                    @staticmethod
                    def sync():
                        pass

            def getObjByPath(self, path):
                return reduce(getattr, path.split("/"), self)

            def getId(self):
                return "testDevice"

            class a:
                class b:
                    class c:
                        "The relationship to populate"
                        @staticmethod
                        def objectIdsAll():
                            "returns the list of object ids in this relationship"
                            return []
        self.adm.applyDataMap(Device(), datamap(), datamap.relname, datamap.compname)

    def test_applyDataMap_relmapException(self):
    	'''test_applyDataMap_exception is mostly the same as test_applyDataMap_relmap
    	     - difference #1: compname is commented out
	     - difference #2: with self.assertRaises(AttributeError) is added
    	'''
        dmd = self.dmd
        class datamap(list):
            #compname = "a/b"
            relname  = "c"

        class Device(object):

            def deviceClass(self):
                return dmd.Devices

            class dmd:
                "Used for faking sync()"
                class _p_jar:
                    @staticmethod
                    def sync():
                        pass

            def getObjByPath(self, path):
                return reduce(getattr, path.split("/"), self)

            class a:
                class b:
                    class c:
                        "The relationship to populate"
                        @staticmethod
                        def objectIdsAll():
                            "returns the list of object ids in this relationship"
                            return []
 
        with self.assertRaises(AttributeError) as theException:
            self.adm.applyDataMap(Device(), datamap(), datamap.relname, datamap.compname)

        self.assertEqual(theException.exception.message, "type object 'datamap' has no attribute 'compname'")

    def testNoChangeAllComponentsLocked(self):
        device = self.dmd.Devices.createInstance('testDevice')
        # Create an IP interface
        device.os.addIpInterface('eth0', False)
        iface = device.os.interfaces._getOb('eth0')
        iface.lockFromDeletion()

        # Apply a RelMap with no interfaces
        relmap = RelationshipMap("interfaces", "os", "Products.ZenModel.IpInterface")
        self.assertFalse(self.adm._applyDataMap(device, relmap))

        self.assertEquals(1, len(device.os.interfaces))