Example #1
0
    def testAutologin(self):
        from Exscript.util.decorator import autologin

        job = FakeJob()
        host = job.data["host"]
        conn = job.data["conn"] = FakeConnection()

        # Test simple login.
        decor = autologin(flush=False)
        bound = decor(self.autologin_cb)
        result = bound(job, host, conn, "one", "two", three=3)
        self.assertEqual(result, 123)

        # Monkey patch the fake connection such that the login fails.
        conn = FakeConnection()
        data = Value("i", 0)

        def fail(data, *args, **kwargs):
            data.value += 1
            raise LoginFailure("intended login failure")

        conn.login = partial(fail, data)

        # Test retry functionality.
        decor = autologin(flush=False, attempts=5)
        bound = decor(self.autologin_cb)
        job = FakeJob()
        job.data["conn"] = conn
        self.assertRaises(LoginFailure, bound, job, host, conn, "one", "two", three=3)
        self.assertEqual(data.value, 5)
Example #2
0
    def testAutologin(self):
        from Exscript.util.decorator import autologin
        job  = FakeJob()
        host = job.data['host']
        conn = job.data['conn'] = FakeConnection()

        # Test simple login.
        decor  = autologin(flush = False)
        bound  = decor(self.autologin_cb)
        result = bound(job, host, conn, 'one', 'two', three = 3)
        self.assertEqual(result, 123)

        # Monkey patch the fake connection such that the login fails.
        conn = FakeConnection()
        data = Value('i', 0)
        def fail(data, *args, **kwargs):
            data.value += 1
            raise LoginFailure('intended login failure')
        conn.login = partial(fail, data)

        # Test retry functionality.
        decor = autologin(flush = False, attempts = 5)
        bound = decor(self.autologin_cb)
        job   = FakeJob()
        job.data['conn'] = conn
        self.assertRaises(LoginFailure,
                          bound,
                          job,
                          host,
                          conn,
                          'one',
                          'two',
                          three = 3)
        self.assertEqual(data.value, 5)
Example #3
0
    def testAutoauthenticate(self):
        from Exscript.util.decorator import autoauthenticate
        job  = FakeJob()
        host = job.data['host']
        conn = job.data['conn'] = FakeConnection()

        # Test simple authentication.
        decor  = autoauthenticate(flush = False)
        bound  = decor(self.autoauthenticate_cb)
        result = bound(job, host, conn, 'one', 'two', three = 3)
        self.assertEqual(result, 123)

        # Monkey patch the fake connection such that the login fails.
        conn = FakeConnection()
        data = Value('i', 0)
        def fail(data, *args, **kwargs):
            data.value += 1
            raise LoginFailure('intended login failure')
        conn.authenticate = partial(fail, data)

        # Test retry functionality.
        decor = autoauthenticate(flush = False, attempts = 5)
        bound = decor(self.autoauthenticate_cb)
        job   = FakeJob()
        job.data['conn'] = conn
        self.assertRaises(LoginFailure,
                          bound,
                          job,
                          host,
                          conn,
                          'one',
                          'two',
                          three = 3)
        self.assertEqual(data.value, 5)
Example #4
0
    def testGetSucceededActions(self):
        self.assertEqual(self.logger.get_succeeded_actions(), 0)

        job1 = FakeJob()
        job2 = FakeJob()
        self.assertEqual(self.logger.get_succeeded_actions(), 0)

        self.logger.add_log(id(job1), job1.name, 1)
        self.logger.add_log(id(job2), job2.name, 1)
        self.assertEqual(self.logger.get_succeeded_actions(), 0)

        self.logger.log_succeeded(id(job1))
        self.assertEqual(self.logger.get_succeeded_actions(), 1)

        try:
            raise FakeError()
        except FakeError:
            self.logger.log_aborted(id(job2), sys.exc_info())
        self.assertEqual(self.logger.get_succeeded_actions(), 1)
Example #5
0
    def testOsFunctionMapper(self):
        from Exscript.util.decorator import os_function_mapper

        cb_map = {"ios": self.ios_cb, "junos": self.junos_cb}
        mapper = os_function_mapper(cb_map)
        job = FakeJob()

        # Test with 'ios'.
        job.data["conn"] = FakeConnection(os="ios")
        result = mapper(job)
        self.assertEqual(result, "hello ios")

        # Test with 'junos'.
        job.data["conn"] = FakeConnection(os="junos")
        result = mapper(job)
        self.assertEqual(result, "hello junos")

        # Test with unsupported OS.
        job.data["conn"] = FakeConnection(os="unknown")
        self.assertRaises(Exception, mapper, job)
Example #6
0
    def testGetSucceededLogs(self):
        self.assertEqual(count(self.logger.get_succeeded_logs()), 0)

        job = FakeJob()
        self.assertEqual(count(self.logger.get_succeeded_logs()), 0)

        self.logger.add_log(id(job), job.name, 1)
        self.assertEqual(count(self.logger.get_succeeded_logs()), 0)

        self.logger.log(id(job), 'hello world')
        self.assertEqual(count(self.logger.get_succeeded_logs()), 0)

        self.logger.log_succeeded(id(job))
        self.assertEqual(count(self.logger.get_aborted_logs()), 0)
        self.assertEqual(count(self.logger.get_succeeded_logs()), 1)
        self.assertIsInstance(nth(self.logger.get_succeeded_logs(), 0), Log)
Example #7
0
    def testGetAbortedLogs(self):
        self.assertEqual(count(self.logger.get_aborted_logs()), 0)

        job = FakeJob()
        self.assertEqual(count(self.logger.get_aborted_logs()), 0)

        self.logger.add_log(id(job), job.name, 1)
        self.assertEqual(count(self.logger.get_aborted_logs()), 0)

        self.logger.log(id(job), 'hello world')
        self.assertEqual(count(self.logger.get_aborted_logs()), 0)

        try:
            raise FakeError()
        except FakeError:
            self.logger.log_aborted(id(job), sys.exc_info())
        self.assertEqual(count(self.logger.get_succeeded_logs()), 0)
        self.assertEqual(count(self.logger.get_aborted_logs()), 1)
        self.assertIsInstance(nth(self.logger.get_aborted_logs(), 0), Log)
Example #8
0
    def testOsFunctionMapper(self):
        from Exscript.util.decorator import os_function_mapper
        cb_map = {'ios': self.ios_cb, 'junos': self.junos_cb}
        mapper = os_function_mapper(cb_map)
        job    = FakeJob()
        host   = object()

        # Test with 'ios'.
        conn   = FakeConnection(os = 'ios')
        result = mapper(job, host, conn)
        self.assertEqual(result, 'hello ios')

        # Test with 'junos'.
        conn   = FakeConnection(os = 'junos')
        result = mapper(job, host, conn)
        self.assertEqual(result, 'hello junos')

        # Test with unsupported OS.
        conn = FakeConnection(os = 'unknown')
        self.assertRaises(Exception, mapper, job, host, conn)
Example #9
0
 def setUp(self):
     self.logger = Logger()
     self.job = FakeJob('fake')
Example #10
0
 def testBind(self):
     from Exscript.util.decorator import bind
     bound  = bind(self.bind_cb, 'one', 'two', three = 3)
     result = bound(FakeJob())
     self.assert_(result == 123, result)