def setUp(self):
     self.switch_mock = flexmock()
     self.switch_mock.switch_descriptor = SwitchDescriptor('dell', "a_host")
     self.session_manager = SwitchSessionManager()
Example #2
0
 def setUp(self):
     self.switch_mock = flexmock()
     self.switch_mock.switch_descriptor = SwitchDescriptor('dell', "a_host")
     self.session_manager = SwitchSessionManager()
class SwitchSessionManagerTest(TestCase):
    def setUp(self):
        self.switch_mock = flexmock()
        self.switch_mock.switch_descriptor = SwitchDescriptor('dell', "a_host")
        self.session_manager = SwitchSessionManager()

    def tearDown(self):
        for timer in self.session_manager.timers.values():
            timer.cancel()

    def test_open_session_generates_with_passed_session_id(self):
        self.session_manager.session_storage = flexmock()
        self.session_manager.session_storage.should_receive('add').with_args(
            'patate', self.switch_mock.switch_descriptor
        ).once()
        self.switch_mock.should_receive('connect').once()

        assert_that(self.session_manager.open_session(self.switch_mock, 'patate'), is_('patate'))
        assert_that(self.session_manager.get_switch_for_session('patate'), is_(self.switch_mock))

    def test_open_session_with_session_that_already_exists_raises_an_exception(self):
        self.session_manager.session_storage = flexmock()
        self.session_manager.session_storage.should_receive('add').with_args(
            'patate', self.switch_mock.switch_descriptor
        ).once()
        self.switch_mock.should_receive('connect').once()

        self.session_manager.open_session(self.switch_mock, 'patate')

        with self.assertRaises(SessionAlreadyExists):
            self.session_manager.open_session(self.switch_mock, 'patate')

    def test_close_session(self):
        self.session_manager.session_storage = flexmock()
        self.session_manager.session_storage.should_receive('add').with_args(
            'patate', self.switch_mock.switch_descriptor
        ).once()
        self.switch_mock.should_receive('connect').once()

        assert_that(self.session_manager.open_session(self.switch_mock, 'patate'), is_('patate'))
        assert_that(self.session_manager.get_switch_for_session('patate'), is_(self.switch_mock))

        self.switch_mock.should_receive('disconnect').once()
        self.session_manager.session_storage.should_receive('remove').with_args('patate')

        self.session_manager.close_session('patate')

        with self.assertRaises(UnknownResource):
            self.session_manager.get_switch_for_session('patate')

    def test_close_session_catches_exception_if_remote_remove_fails(self):
        self.session_manager.session_storage = flexmock()

        self.session_manager.session_storage.should_receive('add').with_args(
            'patate', self.switch_mock.switch_descriptor
        ).once().ordered()
        self.switch_mock.should_receive('connect').once()

        self.session_manager.open_session(self.switch_mock, 'patate')

        self.switch_mock.should_receive('disconnect').once()
        self.session_manager.session_storage.should_receive('remove').with_args('patate').and_raise(NetmanException)

        self.session_manager.close_session('patate')

    def test_session_should_close_itself_after_timeout(self):
        self.session_manager.session_inactivity_timeout = 0.01

        switch_mock = Mock()

        assert_that(self.session_manager.open_session(switch_mock, 'patate'), is_('patate'))
        assert_that(self.session_manager.get_switch_for_session('patate'), is_(switch_mock))

        time.sleep(0.02)

        assert_that(switch_mock.rollback_transaction.called, is_(False))

        with self.assertRaises(UnknownResource):
            self.session_manager.get_switch_for_session('patate')

    def test_session_timeout_should_reset_on_activity(self):
        self.session_manager.session_inactivity_timeout = 0.1

        switch_mock = Mock()

        assert_that(self.session_manager.open_session(switch_mock, 'patate'), is_('patate'))
        assert_that(self.session_manager.get_switch_for_session('patate'), is_(switch_mock))

        time.sleep(0.05)

        self.session_manager.keep_alive('patate')

        time.sleep(0.05)

        assert_that(switch_mock.rollback_transaction.called, is_(False))

        self.session_manager.keep_alive('patate')

        time.sleep(0.11)

        assert_that(switch_mock.rollback_transaction.called, is_(False))

        with self.assertRaises(UnknownResource):
            self.session_manager.get_switch_for_session('patate')

    def test_commit_transaction(self):
        self.session_manager.keep_alive = Mock()
        self.session_manager.session_storage = flexmock()

        self.session_manager.session_storage.should_receive('add').with_args(
            'patate', self.switch_mock.switch_descriptor
        ).once()
        self.switch_mock.should_receive('connect').once().ordered()

        session_id = self.session_manager.open_session(self.switch_mock, 'patate')

        self.switch_mock.should_receive('commit_transaction').once().ordered()

        self.assertEquals(session_id, 'patate')

        self.session_manager.commit_session(session_id)

        self.session_manager.keep_alive.assert_called_with(session_id)

    def test_rollback_session(self):
        self.session_manager.keep_alive = Mock()
        self.session_manager.session_storage = flexmock()

        self.session_manager.session_storage.should_receive('add').with_args(
            'patate', self.switch_mock.switch_descriptor
        ).once()
        self.switch_mock.should_receive('connect').once()

        session_id = self.session_manager.open_session(self.switch_mock, 'patate')

        self.assertEquals(session_id, 'patate')

        self.switch_mock.should_receive('rollback_transaction').once().ordered()

        self.session_manager.rollback_session(session_id)

        self.session_manager.keep_alive.assert_called_with(session_id)

    def test_unknown_session(self):
        with self.assertRaises(UnknownResource):
            self.session_manager.get_switch_for_session('patate')

    def test_add_session_catches_exception_if_remote_add_fails(self):
        self.session_manager.session_storage = flexmock()
        self.session_manager.session_storage.should_receive('add').with_args(
            'patate', self.switch_mock.switch_descriptor
        ).and_raise(NetmanException)
        self.switch_mock.should_receive('connect').once().ordered()

        self.session_manager.open_session(self.switch_mock, 'patate')

    def test_start_transaction(self):
        self.session_manager.keep_alive = Mock()
        self.session_manager.session_storage = flexmock()

        self.session_manager.session_storage.should_receive('add').with_args(
            'patate', self.switch_mock.switch_descriptor
        ).once()
        self.switch_mock.should_receive('connect').once().ordered()

        session_id = self.session_manager.open_session(self.switch_mock, 'patate')

        self.switch_mock.should_receive('start_transaction').once().ordered()

        self.assertEquals(session_id, 'patate')

        self.session_manager.start_transaction(session_id)

        self.session_manager.keep_alive.assert_called_with(session_id)

    def test_end_transaction(self):
        self.session_manager.keep_alive = Mock()
        self.session_manager.session_storage = flexmock()

        self.session_manager.session_storage.should_receive('add').with_args(
            'patate', self.switch_mock.switch_descriptor
        ).once()
        self.switch_mock.should_receive('connect').once().ordered()

        session_id = self.session_manager.open_session(self.switch_mock, 'patate')

        self.switch_mock.should_receive('end_transaction').once().ordered()

        self.assertEquals(session_id, 'patate')

        self.session_manager.end_transaction(session_id)

        self.session_manager.keep_alive.assert_called_with(session_id)
Example #4
0
class SwitchSessionManagerTest(TestCase):
    def setUp(self):
        self.switch_mock = flexmock()
        self.switch_mock.switch_descriptor = SwitchDescriptor('dell', "a_host")
        self.session_manager = SwitchSessionManager()

    def tearDown(self):
        for timer in self.session_manager.timers.values():
            timer.cancel()

    def test_open_session_generates_with_passed_session_id(self):
        self.session_manager.session_storage = flexmock()
        self.session_manager.session_storage.should_receive('add').with_args(
            'patate', self.switch_mock.switch_descriptor).once()
        self.switch_mock.should_receive('connect').once()

        assert_that(
            self.session_manager.open_session(self.switch_mock, 'patate'),
            is_('patate'))
        assert_that(self.session_manager.get_switch_for_session('patate'),
                    is_(self.switch_mock))

    def test_open_session_with_session_that_already_exists_raises_an_exception(
            self):
        self.session_manager.session_storage = flexmock()
        self.session_manager.session_storage.should_receive('add').with_args(
            'patate', self.switch_mock.switch_descriptor).once()
        self.switch_mock.should_receive('connect').once()

        self.session_manager.open_session(self.switch_mock, 'patate')

        with self.assertRaises(SessionAlreadyExists):
            self.session_manager.open_session(self.switch_mock, 'patate')

    def test_close_session(self):
        self.session_manager.session_storage = flexmock()
        self.session_manager.session_storage.should_receive('add').with_args(
            'patate', self.switch_mock.switch_descriptor).once()
        self.switch_mock.should_receive('connect').once()

        assert_that(
            self.session_manager.open_session(self.switch_mock, 'patate'),
            is_('patate'))
        assert_that(self.session_manager.get_switch_for_session('patate'),
                    is_(self.switch_mock))

        self.switch_mock.should_receive('disconnect').once()
        self.session_manager.session_storage.should_receive(
            'remove').with_args('patate')

        self.session_manager.close_session('patate')

        with self.assertRaises(UnknownResource):
            self.session_manager.get_switch_for_session('patate')

    def test_close_session_catches_exception_if_remote_remove_fails(self):
        self.session_manager.session_storage = flexmock()

        self.session_manager.session_storage.should_receive('add').with_args(
            'patate', self.switch_mock.switch_descriptor).once().ordered()
        self.switch_mock.should_receive('connect').once()

        self.session_manager.open_session(self.switch_mock, 'patate')

        self.switch_mock.should_receive('disconnect').once()
        self.session_manager.session_storage.should_receive(
            'remove').with_args('patate').and_raise(NetmanException)

        self.session_manager.close_session('patate')

    def test_session_should_close_itself_after_timeout(self):
        self.session_manager.session_inactivity_timeout = 0.01

        switch_mock = Mock()

        assert_that(self.session_manager.open_session(switch_mock, 'patate'),
                    is_('patate'))
        assert_that(self.session_manager.get_switch_for_session('patate'),
                    is_(switch_mock))

        time.sleep(0.02)

        assert_that(switch_mock.rollback_transaction.called, is_(False))

        with self.assertRaises(UnknownResource):
            self.session_manager.get_switch_for_session('patate')

    def test_session_timeout_should_reset_on_activity(self):
        self.session_manager.session_inactivity_timeout = 0.1

        switch_mock = Mock()

        assert_that(self.session_manager.open_session(switch_mock, 'patate'),
                    is_('patate'))
        assert_that(self.session_manager.get_switch_for_session('patate'),
                    is_(switch_mock))

        time.sleep(0.05)

        self.session_manager.keep_alive('patate')

        time.sleep(0.05)

        assert_that(switch_mock.rollback_transaction.called, is_(False))

        self.session_manager.keep_alive('patate')

        time.sleep(0.11)

        assert_that(switch_mock.rollback_transaction.called, is_(False))

        with self.assertRaises(UnknownResource):
            self.session_manager.get_switch_for_session('patate')

    def test_commit_transaction(self):
        self.session_manager.keep_alive = Mock()
        self.session_manager.session_storage = flexmock()

        self.session_manager.session_storage.should_receive('add').with_args(
            'patate', self.switch_mock.switch_descriptor).once()
        self.switch_mock.should_receive('connect').once().ordered()

        session_id = self.session_manager.open_session(self.switch_mock,
                                                       'patate')

        self.switch_mock.should_receive('commit_transaction').once().ordered()

        self.assertEquals(session_id, 'patate')

        self.session_manager.commit_session(session_id)

        self.session_manager.keep_alive.assert_called_with(session_id)

    def test_rollback_session(self):
        self.session_manager.keep_alive = Mock()
        self.session_manager.session_storage = flexmock()

        self.session_manager.session_storage.should_receive('add').with_args(
            'patate', self.switch_mock.switch_descriptor).once()
        self.switch_mock.should_receive('connect').once()

        session_id = self.session_manager.open_session(self.switch_mock,
                                                       'patate')

        self.assertEquals(session_id, 'patate')

        self.switch_mock.should_receive(
            'rollback_transaction').once().ordered()

        self.session_manager.rollback_session(session_id)

        self.session_manager.keep_alive.assert_called_with(session_id)

    def test_unknown_session(self):
        with self.assertRaises(UnknownResource):
            self.session_manager.get_switch_for_session('patate')

    def test_add_session_catches_exception_if_remote_add_fails(self):
        self.session_manager.session_storage = flexmock()
        self.session_manager.session_storage.should_receive('add').with_args(
            'patate',
            self.switch_mock.switch_descriptor).and_raise(NetmanException)
        self.switch_mock.should_receive('connect').once().ordered()

        self.session_manager.open_session(self.switch_mock, 'patate')

    def test_start_transaction(self):
        self.session_manager.keep_alive = Mock()
        self.session_manager.session_storage = flexmock()

        self.session_manager.session_storage.should_receive('add').with_args(
            'patate', self.switch_mock.switch_descriptor).once()
        self.switch_mock.should_receive('connect').once().ordered()

        session_id = self.session_manager.open_session(self.switch_mock,
                                                       'patate')

        self.switch_mock.should_receive('start_transaction').once().ordered()

        self.assertEquals(session_id, 'patate')

        self.session_manager.start_transaction(session_id)

        self.session_manager.keep_alive.assert_called_with(session_id)

    def test_end_transaction(self):
        self.session_manager.keep_alive = Mock()
        self.session_manager.session_storage = flexmock()

        self.session_manager.session_storage.should_receive('add').with_args(
            'patate', self.switch_mock.switch_descriptor).once()
        self.switch_mock.should_receive('connect').once().ordered()

        session_id = self.session_manager.open_session(self.switch_mock,
                                                       'patate')

        self.switch_mock.should_receive('end_transaction').once().ordered()

        self.assertEquals(session_id, 'patate')

        self.session_manager.end_transaction(session_id)

        self.session_manager.keep_alive.assert_called_with(session_id)
Example #5
0
from netman.api.switch_session_api import SwitchSessionApi
from netman.core.switch_factory import SwitchFactory

app = Flask('netman')
app.url_map.converters['regex'] = RegexConverter

@app.before_request
def log_request():
    logger = getLogger("netman.api")
    logger.info("{} : {}".format(request.method, request.url))
    if logger.isEnabledFor(DEBUG):
        logging.getLogger("netman.api").debug("body : {}".format(repr(request.data) if request.data else "<<empty>>"))
        logging.getLogger("netman.api").debug("Headers : " + ", ".join(["{0}={1}".format(h[0], h[1]) for h in request.headers]))

switch_factory = SwitchFactory(MemoryStorage(), ThreadingLockFactory())
switch_session_manager = SwitchSessionManager()

NetmanApi().hook_to(app)
SwitchApi(switch_factory, switch_session_manager).hook_to(app)
SwitchSessionApi(switch_factory, switch_session_manager).hook_to(app)


if __name__ == '__main__':

    parser = argparse.ArgumentParser(description='Netman Server')
    parser.add_argument('--host', nargs='?', default="127.0.0.1")
    parser.add_argument('--port', type=int, nargs='?', default=5000)
    parser.add_argument('--session_inactivity_timeout', type=int, nargs='?')

    args = parser.parse_args()
Example #6
0
 def setUp(self):
     self.switch_mock = flexmock()
     self.session_manager = SwitchSessionManager()
Example #7
0
class SwitchSessionManagerTest(TestCase):
    def setUp(self):
        self.switch_mock = flexmock()
        self.session_manager = SwitchSessionManager()

    def tearDown(self):
        for timer in self.session_manager.timers.values():
            timer.cancel()

    def test_open_session_generates_with_passed_session_id(self):
        self.switch_mock.should_receive('connect').once().ordered()
        self.switch_mock.should_receive('start_transaction').once().ordered()
        self.switch_mock.should_receive('disconnect').never()

        assert_that(self.session_manager.open_session(self.switch_mock, 'patate'), is_('patate'))
        assert_that(self.session_manager.get_switch_for_session('patate'), is_(self.switch_mock))

    def test_open_session_with_session_that_already_exists_raises_an_exception(self):

        self.switch_mock.should_receive('connect').never()
        self.switch_mock.should_receive('start_transaction').never()
        self.switch_mock.should_receive('disconnect').never()

        self.session_manager.sessions['i_already_exist_buddy'] = 'stuff'

        with self.assertRaises(SessionAlreadyExists):
            self.session_manager.open_session(self.switch_mock, 'i_already_exist_buddy')

    def test_open_failing_session_closes_connection(self):
        self.switch_mock.should_receive('connect').once().ordered()
        self.switch_mock.should_receive('start_transaction').once().ordered().and_raise(NetmanException())
        self.switch_mock.should_receive('disconnect').once().ordered()

        with self.assertRaises(NetmanException):
            self.session_manager.open_session(self.switch_mock, 'patate')

    def test_close_session(self):

        self.switch_mock.should_receive('connect').once().ordered()
        self.switch_mock.should_receive('start_transaction').once().ordered()

        assert_that(self.session_manager.open_session(self.switch_mock, 'patate'), is_('patate'))
        assert_that(self.session_manager.get_switch_for_session('patate'), is_(self.switch_mock))

        self.switch_mock.should_receive('end_transaction').once().ordered()
        self.switch_mock.should_receive('disconnect').once().ordered()

        self.session_manager.close_session('patate')

        with self.assertRaises(UnknownResource):
            self.session_manager.get_switch_for_session('patate')

    def test_session_should_close_itself_after_timeout(self):
        self.session_manager.session_inactivity_timeout = 0.5

        switch_mock = mock.Mock()

        assert_that(self.session_manager.open_session(switch_mock, 'patate'), is_('patate'))
        assert_that(self.session_manager.get_switch_for_session('patate'), is_(switch_mock))

        switch_mock.connect.assert_called_with()
        switch_mock.start_transaction.assert_called_with()

        time.sleep(0.6)

        switch_mock.rollback_transaction.assert_called_with()
        switch_mock.end_transaction.assert_called_with()
        switch_mock.disconnect.assert_called_with()

        with self.assertRaises(UnknownResource):
            self.session_manager.get_switch_for_session('patate')

    def test_session_timeout_should_reset_on_activity(self):
        self.session_manager.session_inactivity_timeout = 1

        switch_mock = mock.Mock()

        assert_that(self.session_manager.open_session(switch_mock, 'patate'), is_('patate'))
        assert_that(self.session_manager.get_switch_for_session('patate'), is_(switch_mock))

        switch_mock.connect.assert_called_with()
        switch_mock.start_transaction.assert_called_with()

        time.sleep(0.5)

        self.session_manager.keep_alive('patate')

        time.sleep(0.5)

        assert_that(switch_mock.rollback_transaction.called, is_(False))
        assert_that(switch_mock.end_transaction.called, is_(False))
        assert_that(switch_mock.disconnect.called, is_(False))

        self.session_manager.keep_alive('patate')

        time.sleep(1.1)

        switch_mock.rollback_transaction.assert_called_with()
        switch_mock.end_transaction.assert_called_with()
        switch_mock.disconnect.assert_called_with()

        with self.assertRaises(UnknownResource):
            self.session_manager.get_switch_for_session('patate')

    def test_commit_session(self):
        self.session_manager.keep_alive = mock.Mock()

        self.switch_mock.should_receive('connect').once().ordered()
        self.switch_mock.should_receive('start_transaction').once().ordered()

        session_id = self.session_manager.open_session(self.switch_mock, 'patate')

        self.switch_mock.should_receive('commit_transaction').once().ordered()

        self.assertEquals(session_id, 'patate')

        self.session_manager.commit_session(session_id)

        self.session_manager.keep_alive.assert_called_with(session_id)

    def test_rollback_session(self):
        self.session_manager.keep_alive = mock.Mock()

        self.switch_mock.should_receive('connect').once().ordered()
        self.switch_mock.should_receive('start_transaction').once().ordered()

        session_id = self.session_manager.open_session(self.switch_mock, 'patate')

        self.assertEquals(session_id, 'patate')

        self.switch_mock.should_receive('rollback_transaction').once().ordered()

        self.session_manager.rollback_session(session_id)

        self.session_manager.keep_alive.assert_called_with(session_id)

    def test_unknown_session(self):
        with self.assertRaises(UnknownResource):
            self.session_manager.get_switch_for_session('patate')

    def test_close_session_with_error(self):

        self.switch_mock.should_receive('connect').once().ordered()
        self.switch_mock.should_receive('start_transaction').once().ordered()

        self.session_manager.open_session(self.switch_mock, 'patate')

        self.switch_mock.should_receive('end_transaction').and_raise(OperationNotCompleted()).once().ordered()
        self.switch_mock.should_receive('disconnect').once().ordered()

        with self.assertRaises(OperationNotCompleted):
            self.session_manager.close_session('patate')

        with self.assertRaises(UnknownResource):
            self.session_manager.get_switch_for_session('patate')
Example #8
0
@app.before_request
def log_request():
    logger = getLogger("netman.api")
    logger.info("{} : {}".format(request.method, request.url))
    if logger.isEnabledFor(DEBUG):
        logger.debug("body : {}".format(
            repr(request.data) if request.data else "<<empty>>"))
        logger.debug(
            "Headers : " +
            ", ".join(["{0}={1}".format(h[0], h[1]) for h in request.headers]))


lock_factory = ThreadingLockFactory()
switch_factory = FlowControlSwitchFactory(MemoryStorage(), lock_factory)
real_switch_factory = RealSwitchFactory()
switch_session_manager = SwitchSessionManager()

NetmanApi(switch_factory).hook_to(app)
SwitchApi(switch_factory, switch_session_manager).hook_to(app)
SwitchSessionApi(real_switch_factory, switch_session_manager).hook_to(app)


def load_app(session_inactivity_timeout=None):
    if session_inactivity_timeout:
        switch_session_manager.session_inactivity_timeout = session_inactivity_timeout

    return app


if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Netman Server')