Exemple #1
0
class ServerTest(unittest.TestCase):
    CORRELATE = Server.Server

    def setUp(self):
        self.host   = 'localhost'
        self.port   = 1235
        self.device = VirtualDevice(self.host, echo = False)
        self.daemon = Server.Server(self.host, self.port, self.device)
        self.device.set_prompt(self.host + ':' + str(self.port) + '> ')

    def tearDown(self):
        if self.daemon:
            self.daemon.exit()
        if self.daemon.__class__ != Server.Server:
            self.daemon.join()

    def _create_daemon(self):
        raise NotImplementedError()

    def _create_client(self):
        raise NotImplementedError()

    def _add_commands(self):
        self.device.add_command('exit', self.daemon.exit_command)
        self.device.add_command('ls',   'ok1')
        self.device.add_command('ll',   'ok2\nfoobar:1>', prompt = False)
        self.device.add_command('.+',   'Unknown command.')

    def testConstructor(self):
        # Test can not work on the abstract base.
        if self.daemon.__class__ == Server.Server:
            return
        self._create_daemon()
        self.daemon.start()
        time.sleep(1)

    def testStart(self):
        # Test can not work on the abstract base.
        if self.daemon.__class__ == Server.Server:
            return
        self._create_daemon()
        self._add_commands()
        self.daemon.start()
        time.sleep(1)

        client = self._create_client()
        client.set_prompt(re.compile(r'\w+:\d+> ?'))
        client.connect(self.host, self.port)
        client.login(Account('user', 'password'))
        client.execute('ls')
        self.assertEqual(client.response, 'ok1\n')
        client.execute('ll')
        self.assertEqual(client.response, 'ok2\n')
        client.send('exit\r')

    def testExitCommand(self):
        pass # tested in testExit()

    def testExit(self):
        # Test can not work on the abstract base.
        if self.daemon.__class__ == Server.Server:
            return
        self.testStart()
        # Since testStart() sent an "exit" command to the server,
        # it should be shutting down even without us calling
        # self.daemon.exit().
        self.daemon.join()
        self.testConstructor()
Exemple #2
0
class ServerTest(unittest.TestCase):
    CORRELATE = Server

    def setUp(self):
        self.host = 'localhost'
        self.port = 1235
        self.device = VirtualDevice(self.host, echo=False)
        self.daemon = Server(self.host, self.port, self.device)
        self.device.set_prompt(self.host + ':' + str(self.port) + '> ')

    def tearDown(self):
        if self.daemon:
            self.daemon.exit()
        if self.daemon.__class__ != Server:
            self.daemon.join()

    def _create_daemon(self):
        raise NotImplementedError()

    def _create_client(self):
        raise NotImplementedError()

    def _add_commands(self):
        self.device.add_command('exit', self.daemon.exit_command)
        self.device.add_command('ls', 'ok1')
        self.device.add_command('ll', 'ok2\nfoobar:1>', prompt=False)
        self.device.add_command('.+', 'Unknown command.')

    def testConstructor(self):
        # Test can not work on the abstract base.
        if self.daemon.__class__ == Server:
            return
        self._create_daemon()
        self.daemon.start()
        time.sleep(1)

    def testStart(self):
        # Test can not work on the abstract base.
        if self.daemon.__class__ == Server:
            return
        self._create_daemon()
        self._add_commands()
        self.daemon.start()
        time.sleep(1)

        client = self._create_client()
        client.set_prompt(re.compile(r'\w+:\d+> ?'))
        client.connect(self.host, self.port)
        client.login(Account('user', 'password'))
        client.execute('ls')
        self.assertEqual(client.response, 'ok1\n')
        client.execute('ll')
        self.assertEqual(client.response, 'ok2\n')
        client.send('exit\r')

    def testExitCommand(self):
        pass  # tested in testExit()

    def testExit(self):
        # Test can not work on the abstract base.
        if self.daemon.__class__ == Server:
            return
        self.testStart()
        # Since testStart() sent an "exit" command to the server,
        # it should be shutting down even without us calling
        # self.daemon.exit().
        self.daemon.join()
        self.testConstructor()