def test_inherit_redefine_attribute(self): """ Héritage d'un attribut redéfini depuis un modèle d'hôte. Le template "testtpl1" ajoute l'attribut "TestAttr" valant "TestVal1". Le template "testtpl2" hérite de "testtpl1" et redéfinit "TestAttr" comme valant "TestVal2". L'hôte "localhost" importe le template "testtpl2". L'hôte "localhost" devrait donc posséder cet attribut avec la valeur définie dans "testtpl2". """ self.tpl.add_attribute("TestAttr", "TestVal1") tpl2 = HostTemplate("testtpl2") tpl2.add_parent("testtpl1") self.tpl.add_attribute("TestAttr", "TestVal2") self.hosttemplatefactory.register(tpl2) # Reload the templates self.hosttemplatefactory.load_templates() xmlfile = open(os.path.join(self.tmpdir, "localhost.xml"), "w") xmlfile.write(""" <host name="localhost" address="127.0.0.1"> <template>testtpl2</template> <group>Linux servers</group> </host> """) xmlfile.close() hosts = self.hostfactory.load() print hosts self.assertTrue(hosts['localhost'].has_key("TestAttr"), "inheritance does not work with attributes") self.assertEqual(hosts['localhost']["TestAttr"], "TestVal2", "inheritance does not work with attributes")
def test_inherit_multiple_test(self): """ Héritage de tests issus de modèles d'hôtes multiples. L'hôte hérite d'un template qui hérite lui-même de deux templates définissant chacun 1 test. On s'assure que l'hôte hérite correctement des 2 tests. """ self.tpl.add_test("Interface", {"ifname":"eth0", "label":"Label0"}) tpl2 = HostTemplate("testtpl2") tpl2.add_test("Interface", {"ifname":"eth1", "label":"Label1"}) self.hosttemplatefactory.register(tpl2) tpl3 = HostTemplate("testtpl3") tpl3.add_parent(["testtpl1", "testtpl2"]) self.hosttemplatefactory.register(tpl3) # Reload the templates self.hosttemplatefactory.load_templates() xmlfile = open(os.path.join(self.tmpdir, "localhost.xml"), "w") xmlfile.write(""" <host name="localhost" address="127.0.0.1"> <template>testtpl3</template> <group>Linux servers</group> </host> """) xmlfile.close() hosts = self.hostfactory.load() print hosts for svc in ("Interface Label0", "Interface Label1"): self.assertTrue(svc in hosts['localhost']['services'], "multiple inheritance does not work (%s)" % svc)
def test_inherit_group(self): """ Héritage d'un groupe depuis un modèle d'hôte. Le template "testtpl1" s'ajoute au groupe "Test Group". Le template "testtpl2" hérite de "testtpl1". L'hôte "localhost" importe le template "testtpl2". L'hôte "localhost" devrait donc appartenir au groupe "Test Group". """ self.tpl.add_group("Test Group") tpl2 = HostTemplate("testtpl2") tpl2.add_parent("testtpl1") self.hosttemplatefactory.register(tpl2) # Reload the templates self.hosttemplatefactory.load_templates() xmlfile = open(os.path.join(self.tmpdir, "localhost.xml"), "w") xmlfile.write(""" <host name="localhost" address="127.0.0.1"> <template>testtpl2</template> </host> """) xmlfile.close() hosts = self.hostfactory.load() print hosts self.assertTrue("Test Group" in hosts["localhost"]["otherGroups"], "inheritance does not work with groups")
def test_inherit_test(self): """ Héritage d'un test depuis un modèle d'hôte. Le template "testtpl1" ajoute le test "UpTime". Le template "testtpl2" hérite de "testtpl1". L'hôte "localhost" importe le template "testtpl2". Le test "UpTime" doit donc être appliqué à "localhost". """ self.tpl.add_test("UpTime") tpl2 = HostTemplate("testtpl2") tpl2.add_parent("testtpl1") self.hosttemplatefactory.register(tpl2) # Reload the templates self.hosttemplatefactory.load_templates() xmlfile = open(os.path.join(self.tmpdir, "localhost.xml"), "w") xmlfile.write(""" <host name="localhost" address="127.0.0.1"> <template>testtpl2</template> <group>Linux servers</group> </host> """) xmlfile.close() hosts = self.hostfactory.load() print hosts self.assertTrue("UpTime" in hosts['localhost']['services'], "inheritance does not work with tests")
def test_parent_default(self): tpl1 = HostTemplate("testtpl2") tpl1.add_parent("testtpl1") self.hosttemplatefactory.register(tpl1) self.assertTrue("default" in self.hosttemplatefactory.templates["testtpl1"]["parent"], "The \"default\" template is not automatically added as " "parent to other templates")
def test_inherit_redefine_test(self): self.tpl.add_test("Interface", {"ifname":"eth0", "label":"Label1"}) tpl2 = HostTemplate("testtpl2") tpl2.add_parent("testtpl1") tpl2.add_test("Interface", {"ifname":"eth0", "label":"Label2"}) self.hosttemplatefactory.register(tpl2) # Reload the templates self.hosttemplatefactory.load_templates() intftest = None for test in self.hosttemplatefactory.templates["testtpl2"]["tests"]: if test["name"] == "Interface": intftest = test self.assertTrue(intftest is not None, "inheritance does not work with tests") self.assertEqual(intftest["args"]["label"], "Label2", "child templates cannot redefine tests from parent templates")
def test_deepcopy(self): """ Test de la copie en profondeur If the template data from the parent is not copied with copy.deepcopy(), then the child's template data will propagate back into the parent """ self.tpl.add_attribute("TestAttr1", "TestVal") tpl2 = HostTemplate("testtpl2") tpl2.add_parent("testtpl1") tpl2.add_attribute("TestAttr2", "TestVal") self.hosttemplatefactory.register(tpl2) # Reload the templates self.hosttemplatefactory.load_templates() tpldata = self.hosttemplatefactory.templates["testtpl1"] self.failIf(tpldata["attributes"].has_key("TestAttr2"), "inheritence taints parent templates")