class DiscoveratorBaseTest(object): testmib = None def setUp(self): setup_db() self.tmpdir = setup_tmpdir() testfactory = TestFactory(confdir=settings["vigiconf"].get("confdir")) self.disc = Discoverator(testfactory, group="Test") self.disc.testfactory.load_hclasses_checks() walkfile = os.path.join(TESTDATADIR, "discoverator", self.testmib) self.disc.scanfile(walkfile) self.disc.detect() self.testnames = [ t["name"] for t in self.disc.tests ] def tearDown(self): shutil.rmtree(self.tmpdir) teardown_db() def test_hostname(self): expected = socket.getfqdn("localhost") self.assertEqual(self.disc.hostname, expected, "Hostname not detected correctly (got %s)" % self.disc.hostname) def test_ipaddr(self): self.assertEqual(self.disc.ipaddr, "127.0.0.1", "IP not detected correcty (got %s)" % self.disc.ipaddr)
def setUp(self): setup_db() self.tmpdir = setup_tmpdir() testfactory = TestFactory(confdir=settings["vigiconf"].get("confdir")) self.disc = Discoverator(testfactory, group="Test") self.disc.testfactory.load_hclasses_checks() self.testnames = [ t["name"] for t in self.disc.tests ]
def setUp(self): setup_db() self.tmpdir = setup_tmpdir() testfactory = TestFactory(confdir=settings["vigiconf"].get("confdir")) self.disc = Discoverator(testfactory, group="Test") self.disc.testfactory.load_hclasses_checks() walkfile = os.path.join(TESTDATADIR, "discoverator", self.testmib) self.disc.scanfile(walkfile)
def discover(args): from vigilo.vigiconf.lib.confclasses.test import TestFactory from vigilo.vigiconf.discoverator import Discoverator, indent from vigilo.vigiconf.discoverator import DiscoveratorError from vigilo.vigiconf.discoverator import SnmpwalkNotInstalled testfactory = TestFactory(confdir=settings["vigiconf"].get("confdir")) discoverator = Discoverator(testfactory, args.group) discoverator.testfactory.load_hclasses_checks() if len(args.target) > 1: args.output.write("<hosts>\n") for target in args.target: try: discoverator.scan(target, args.community, args.version) except SnmpwalkNotInstalled: raise except DiscoveratorError, e: # On ne fait que logguer l'erreur pour générer quand même ce # qu'on a pu détecter jusqu'ici (cas du timeout) LOGGER.error(e.value) discoverator.detect(args.test) elements = discoverator.declaration() indent(elements) args.output.write(ET.tostring(elements, encoding="utf8"))
class DiscoveratorSpecificTest(unittest.TestCase): testmib = "linux.walk" def setUp(self): setup_db() self.tmpdir = setup_tmpdir() testfactory = TestFactory(confdir=settings["vigiconf"].get("confdir")) self.disc = Discoverator(testfactory, group="Test") self.disc.testfactory.load_hclasses_checks() walkfile = os.path.join(TESTDATADIR, "discoverator", self.testmib) self.disc.scanfile(walkfile) def tearDown(self): shutil.rmtree(self.tmpdir) teardown_db() def test_multiple_test_detection(self): """Test the simple test detection on Linux This uses the test's detect_oid() method""" self.disc.detect(["Swap", "Partition", "Interface"]) self.testnames = [ t["name"] for t in self.disc.tests ] for test in [ "Swap", "Partition", "Interface" ]: self.assertTrue(test in self.testnames, "Test %s is not detected" % test) def test_single_test_detection(self): """Test the simple test detection on Linux This uses the test's detect_oid() method""" self.disc.detect([ "Partition" ]) self.testnames = [ t["name"] for t in self.disc.tests ] self.assertTrue("Partition" in self.testnames, "Test %s is not detected" % "Partition") self.assertFalse("Interface" in self.testnames, "Test %s is detected" % "Interface")
class TestDiscoveratorBasics(unittest.TestCase): def setUp(self): setup_db() self.tmpdir = setup_tmpdir() testfactory = TestFactory(confdir=settings["vigiconf"].get("confdir")) self.disc = Discoverator(testfactory, group="Test") self.disc.testfactory.load_hclasses_checks() self.testnames = [ t["name"] for t in self.disc.tests ] def tearDown(self): shutil.rmtree(self.tmpdir) teardown_db() def test_snmp_novalue(self): tmpfile = os.path.join(self.tmpdir, "test.walk") walkfile = open(tmpfile, "w") walkfile.write(".1.3.6.1.2.1.2.2.1.6.1 = \n") walkfile.close() self.disc._get_snmp_command = lambda c, v, h: ["cat", tmpfile] try: self.disc.scanhost("test", "public", "v2c") except ValueError: self.fail("Discoverator chokes on empty SNMP values") def test_snmp_empty(self): """Discoverator should handle empty walks""" tmpfile = os.path.join(self.tmpdir, "test.walk") open(tmpfile, "w").close() self.disc._get_snmp_command = lambda c, v, h: ["cat", tmpfile] try: self.disc.scanhost("test", "public", "v2c") except (ValueError, TypeError): self.fail("Discoverator chokes on empty SNMP walks") print self.disc.oids self.assertEqual(len(self.disc.oids), 0) def test_wrapped_line(self): """Discoverator : valeur multiligne sans guillemets""" tmpfile = os.path.join(self.tmpdir, "test.walk") walkfile = open(tmpfile, "w") walkfile.write(".1.42 = First line\nSecond line\nThird line\n") walkfile.close() self.disc._get_snmp_command = lambda c, v, h: ["cat", tmpfile] self.disc.scanhost("test", "public", "v2c") self.assertEqual(self.disc.oids[".1.42"], "First line\nSecond line\nThird line\n") def test_wrapped_line2(self): """Discoverator : valeur multiligne entre guillemets""" tmpfile = os.path.join(self.tmpdir, "test.walk") walkfile = open(tmpfile, "w") walkfile.write(".1.42 = \"First line\nSecond line\nThird line\n\"") walkfile.close() self.disc._get_snmp_command = lambda c, v, h: ["cat", tmpfile] self.disc.scanhost("test", "public", "v2c") self.assertEqual(self.disc.oids[".1.42"], "First line\nSecond line\nThird line\n") def test_detect_hclasses_wrapped_line(self): self.disc.oids[".1.3.6.1.2.1.1.1.0"] = "l1\nTest HClass\nl3\n" self.disc.testfactory.tests["faketest"] = {"test_hclass": None} self.disc.testfactory.hclasschecks["test_hclass"] = { "sysdescr": ".*Test HClass.*"} self.disc.find_hclasses_sysdescr() print self.disc.hclasses self.assertTrue("test_hclass" in self.disc.hclasses) def test_detect_homonyms(self): """ Détection des tests avec homonymie. Si plusieurs classes d'hôtes fournissent le même test avec une méthode de détection, chacune de ces méthodes de détection doit être appelée. """ class FakeTest(Test): oids = [".1.3.6.1.2.1.1.1.0"] class FakeTest2(FakeTest): pass # On fait en sorte que les 2 tests soient vus # comme ayant le même nom. FakeTest2.__name__ = FakeTest.__name__ self.disc.oids[".1.3.6.1.2.1.1.1.0"] = "" self.disc.testfactory.tests["faketest"] = { "testclass1": FakeTest, "testclass2": FakeTest2, } self.disc.find_tests() self.disc.find_hclasses() self.assertTrue("testclass1" in self.disc.hclasses, str(self.disc.hclasses)) self.assertTrue("testclass2" in self.disc.hclasses, str(self.disc.hclasses)) # Il ne doit y avoir qu'un seul test dans le résultat self.disc.deduplicate_tests() decl = self.disc.declaration() testlines = [] for elem in decl: if elem.tag != "test": continue testlines.append(ET.tostring(elem)) self.assertEqual(testlines, ['<test name="FakeTest" />'])