#!/usr/bin/python
#
# $Id: test_pytraverse.py,v 1.3 2010-07-02 17:49:47 jloden Exp $
#

import unittest
from test import test_support


from pytraverse import clients
from pytraverse.debug import tvdebug
from pytraverse.error import *

TESTHOST="192.168.15.155"
TESTUSER="netvigil"
TESTPASS="netvigil"

class ProvTestCase(unittest.TestCase):
    """Unit tests for Traverse Provisioning Python API."""
    def setUp(self):
        self.BVE = clients.Provisioning(host=TESTHOST, debug=False)
        self.BVE.Login(user=TESTUSER, password=TESTPASS)

    def tearDown(self):
        try:
            self.BVE.Logout()
        except:
            pass

    def test_login(self):
        # log ourselves out first since setUp logs us in
        self.BVE.Logout()
        # test a login that should work
        self.assertTrue(self.BVE.Login(user=TESTUSER, password=TESTPASS))
        self.assertTrue(self.BVE.GetFullResponseText().startswith("OK"))
        self.BVE.Logout()
        # now a login that should fail and raise LoginFailure
        self.assertRaises(LoginFailure, self.BVE.Login, "notauser", "notapassword")

    def test_result_set(self):
        #call GetResultSet() with no previous command to ensure it's not
        #fetching data when it shouldn't
        self.assertFalse(self.BVE.GetResultSet())

    def test_ListDevice(self):
        # needs a localhost device to be defined in the test system
        self.assertTrue(self.BVE.ListDevice(devicename="localhost"))
        # make sure we're reporting at least 1 device in the result count
        self.assertTrue(self.BVE.GetResultCount() > 0)
        # make sure the list returned isn't empty
        self.assertTrue(self.BVE.GetResultSet)

    def test_UpdateDevice(self):
        self.assertTrue(self.BVE.UpdateDevice(devicename="localhost", showonsummary="true"))
        # check response code
        self.assertTrue(self.BVE.GetFullResponseText().startswith("OK"))
        self.assertTrue(self.BVE.GetResponseText().startswith("1 NetworkDevice"))

    def test_ListAction(self):
        # list all actions
        self.assertTrue(self.BVE.ListAction(actionname="*"))
        # make sure that at least one action profile is returned
        self.assertTrue(self.BVE.GetResultSet())
        self.assertTrue(self.BVE.GetResultCount() > 0)

    def test_ListDevice(self):
        # first test a device that should exist (requires "localhost" device)
        self.assertTrue(self.BVE.ListDevice(devicename="localhost"))
        self.assertTrue(self.BVE.GetResultCount() > 0)
        self.assertTrue(self.BVE.GetResultSet())
        # now test a device that should NOT exist
        # device.list returns OK even if device does not exist, so True
        self.assertTrue(self.BVE.ListDevice(devicename="notahost"))
        self.assertTrue(self.BVE.GetResultCount() == 0)
        self.assertFalse(self.BVE.GetResultSet())

    def test_ListResult(self):
        self.assertTrue(self.BVE.ListResult(devicename="localhost"))
        self.assertTrue(self.BVE.GetResultCount() > 0)
        self.assertTrue(self.BVE.GetResultSet())
        # now test a device that should NOT exist
        # result.list returns ERR if device does not exist
        self.assertFalse(self.BVE.ListResult(devicename="notahost"))
        self.assertTrue(self.BVE.GetResultCount() == 0)
        self.assertFalse(self.BVE.GetResultSet())

    def test_DeleteAction(self):
        self.assertTrue(self.BVE.DeleteAction(actionname="myaction"))

    def test_UpdateAction(self):
        self.assertTrue(self.BVE.DeleteAction(actionname="myaction"))

    def test_Logout(self):
        self.assertTrue(self.BVE.Logout())
        self.assertTrue(self.BVE.GetFullResponseText().startswith("OK 299 Logging out"))

    def test_InvalidListCmd(self):
        # not a real command, should fail
        # create a tmp function to call with assertRaises
        def tmp():
            self.BVE.ListFoo(foo="bar")
        self.assertRaises(AttributeError, tmp)

    def test_InvalidCmd(self):
        def tmp():
            self.BVE.FooBar(foo="bar")
        self.assertRaises(AttributeError, tmp)


class ISMTestCase(unittest.TestCase):
    """Unit tests for Traverse ISM Python API."""
    def setUp(self):
        self.ISM = clients.Message(host=TESTHOST, debug=False)
        self.ISM.Login(user="ismuser", password="fixme")

    def tearDown(self):
        try:
            self.ISM.Logout()
        except:
            pass

    def test_Login(self):
        # log ourselves out first since setUp logs us in
        self.ISM.Logout()
        # test a login that should work
        self.assertTrue(self.ISM.Login(user="ismuser", password="fixme"))
        self.assertTrue(self.ISM.GetFullResponseText().startswith("OK"))
        self.ISM.Logout()
        # now a login that should fail and raise LoginFailure
        self.assertRaises(LoginFailure, self.ISM.Login, "notauser", "notapassword")

    def test_InsertMessage(self):
        self.assertTrue(self.ISM.InsertMessage("localhost", "127.0.0.1", "CRITICAL", "test message from pytraverse test suite"))
        self.assertTrue(self.ISM.GetFullResponseText().startswith("OK"))

    def test_Logout(self):
        self.assertTrue(self.ISM.Logout())
        self.assertTrue(self.ISM.GetFullResponseText().startswith("OK"))


class EDFTestCase(unittest.TestCase):
    """Unit tests for Traverse ISM Python API."""
    def setUp(self):
        self.EDF = clients.ExternalData(host=TESTHOST, debug=False)
        self.EDF.Login(user="edfuser", password="fixme")

    def tearDown(self):
        try:
            self.EDF.Logout()
        except:
            pass

    def test_Login(self):
        # log ourselves out first since setUp logs us in
        self.EDF.Logout()
        # test a login that should work
        self.assertTrue(self.EDF.Login(user="edfuser", password="fixme"))
        self.assertTrue(self.EDF.GetFullResponseText().startswith("OK"))
        self.EDF.Logout()
        # now a login that should fail and raise LoginFailure
        self.assertRaises(LoginFailure, self.EDF.Login, "notauser", "notapassword")

    def test_InsertResult(self):
        self.assertTrue(self.EDF.InsertResult("localhost", "127.0.0.1", "EDF test", "", "50"))
        self.assertTrue(self.EDF.GetFullResponseText().startswith("OK"))

    def test_Logout(self):
        self.assertTrue(self.EDF.Logout())
        self.assertTrue(self.EDF.GetFullResponseText().startswith("OK"))


def test_main():
    test_suite = unittest.TestSuite()
    test_suite.addTest(unittest.makeSuite(ProvTestCase))
    test_suite.addTest(unittest.makeSuite(ISMTestCase))
    test_suite.addTest(unittest.makeSuite(EDFTestCase))
    unittest.TextTestRunner(verbosity=2).run(test_suite)
    if hasattr(test_support, "reap_children"):  # python 2.5 and >
        test_support.reap_children()

if __name__ == '__main__':
    test_main()


