class ConnectorMetroTest(unittest.TestCase):

    def setUp(self):
        self.tmpdir = setup_tmpdir()
        self.basedir = os.path.join(self.tmpdir, "deploy")
        self.host = Host(conf.hostsConf, "dummy.xml", "testserver1",
                         "192.168.1.1", "Servers")
        self.host.add_perfdata("dummy", "dummy")

    def tearDown(self):
        """Call after every test case."""
        shutil.rmtree(self.tmpdir)

    def test_add_missing_servers(self):
        app = ConnectorMetro()
        ventilation = {"testserver1":
                {"connector-metro": "localhost"}
            }
        app.generate(ventilation)
        self.assertTrue("localhost" in app.servers)
        self.assertEqual(app.servers["localhost"].name, "localhost")
        self.assertEqual(app.actions, {"localhost": ["stop", "start"]})

    def test_generated(self):
        app = ConnectorMetro()
        ventilation = {"testserver1":
                {"connector-metro": "localhost"}
            }
        app.generate(ventilation)
        db_path = os.path.join(self.basedir, "localhost",
                               "connector-metro.db")
        self.assertTrue(os.path.exists(db_path))
        db = sqlite3.connect(db_path)
        cur = db.cursor()
        cur.execute("SELECT * FROM perfdatasource")
        pds = cur.fetchall()
        cur.execute("SELECT * FROM rra")
        rra = cur.fetchall()
        cur.execute("SELECT * FROM pdsrra")
        pdsrra = cur.fetchall()
        cur.close()
        db.close()
        self.assertEqual(pds, [
            (1, 'dummy', 'testserver1', 'GAUGE', 300, 600, None,
             None, 1.0, None, None, None, u'localhost') ])
        self.assertEqual(rra, [
            (1, 'AVERAGE', 0.5, 1, 600),  (2, 'AVERAGE', 0.5, 6,   700),
            (3, 'AVERAGE', 0.5, 24, 775), (4, 'AVERAGE', 0.5, 288, 732)
            ])
        self.assertEqual(pdsrra, [
            (1, 1, 0),
            (1, 2, 1),
            (1, 3, 2),
            (1, 4, 3),
            ])
class VigiRRDTest(unittest.TestCase):

    def setUp(self):
        self.tmpdir = setup_tmpdir()
        os.mkdir(os.path.join(self.tmpdir, "deploy"))
        self.host = Host(conf.hostsConf, "dummy.xml", "testserver1",
                         "192.168.1.1", "Servers")
        self.vigirrd = VigiRRD()
        ventilation = {"testserver1": {"vigirrd": "sup.example.com"}}
        self.generator = self.vigirrd.generator(self.vigirrd, ventilation)

    def tearDown(self):
        shutil.rmtree(self.tmpdir)

    def test_graph_order(self):
        for i in range(7):
            self.host.add_perfdata("test_ds_%d" % i, "dummy")
        self.host.add_graph("test graph 1",
                [ "test_ds_%d" % i for i in range(7) ],
                "lines", "dummy")
        self.host.add_graph("test graph 2",
                [ "test_ds_%d" % i for i in range(6, -1, -1) ],
                "lines", "dummy")
        self.generator.generate()
        db = sqlite3.connect(os.path.join(self.tmpdir, "deploy",
                                          "sup.example.com", "vigirrd.db"))
        c = db.cursor()
        c.execute("""SELECT pds.name FROM perfdatasource pds
                     JOIN graphperfdatasource gpds
                       ON pds.idperfdatasource = gpds.idperfdatasource
                     JOIN graph g
                       ON g.idgraph = gpds.idgraph
                     WHERE g.name = ? ORDER BY gpds.`order`""",
                  ("test graph 1", ))
        ds_in_graph_1 = [ r[0] for r in c.fetchall() ]
        self.assertEqual(ds_in_graph_1, [ "test_ds_%d" % i for i in range(7) ])
        c.execute("""SELECT pds.name FROM perfdatasource pds
                     JOIN graphperfdatasource gpds
                       ON pds.idperfdatasource = gpds.idperfdatasource
                     JOIN graph g
                       ON g.idgraph = gpds.idgraph
                     WHERE g.name = ? ORDER BY gpds.`order`""",
                  ("test graph 2", ))
        ds_in_graph_2 = [ r[0] for r in c.fetchall() ]
        self.assertEqual(ds_in_graph_2,
                         [ "test_ds_%d" % i for i in range(6, -1, -1) ])