def test_events(): inject.clear() rc = yield redis.Connection(dbid=2) yield rc.flushdb() eb = EventBus(rc) yield eb.connect() test_events.test = None def boo(pattern, message): assert message == 'hoho' assert pattern == 'foo' test_events.test = message eb.on('foo', boo) yield eb.fire_event('foo', 'hoho') def check_results(): assert test_events.test == 'hoho' reactor.callLater(50, check_results)
def test_task_terminate(): #----------------------------------- # preparations #----------------------------------- # cleanup a bit inject.clear() rc = yield redis.Connection(dbid=2) eb = EventBus(rc) yield eb.connect() def my_config(binder): binder.bind(redis.Connection, rc) binder.bind(EventBus, eb) binder.bind('settings', None) inject.configure(my_config) yield rc.flushdb() api = inject.instance(ApiRpcServer) #----------------------------------- # Test itself #----------------------------------- # this will emulate some long-running process task_defered = defer.Deferred() # this is mock that will execute our long-running process task = flexmock() task.should_receive('foo').with_args( int, 123, 'test').once().and_return(task_defered) # register our task api.tasks['baz'] = task.foo # start server -> real server on tcp port server = Server(port=9987, no_ssl=True) server.bind() # real client connecton here client = Client(port=9987, no_ssl=True) yield client.connect() # client calls a task task = Task('baz') yield client.call(task, 123, 'test') yield sleep(0.1) assert task.id > 0 assert task.name == 'baz' assert task.is_running is True # now client terminates the task yield sleep(0.1) client.terminate_task(task.id) yield sleep(0.1) assert task.is_running is False #----------------------------------- # Cleanup #----------------------------------- client.shutdown() server.shutdown() yield sleep(0.1)
def test_tasks(): #----------------------------------- # preparations #----------------------------------- # cleanup a bit inject.clear() rc = yield redis.Connection(dbid=2) eb = EventBus(rc) yield eb.connect() def my_config(binder): binder.bind(redis.Connection, rc) binder.bind(EventBus, eb) binder.bind('settings', None) inject.configure(my_config) yield rc.flushdb() api = inject.instance(ApiRpcServer) #----------------------------------- # Test itself #----------------------------------- # this will emulate some long-running process task_defered = defer.Deferred() # this is mock that will execute our long-running process task = flexmock() task.should_receive('foo').with_args( int, 123, 'test').once().and_return(task_defered) # register our task api.tasks['baz'] = task.foo # start server -> real server on tcp port server = Server(port=9997, no_ssl=True) server.bind() # real client connecton here client = Client(port=9997, no_ssl=True) yield client.connect() # client calls a task task = Task('baz') yield client.call(task, 123, 'test') yield sleep(0.1) assert task.id > 0 assert task.name == 'baz' assert task.is_running is True assert len(server.rpc_server.tasks_running) == 1 assert server.rpc_server.tasks_running[task.id]['name'] == 'baz' assert len(server.rpc_server.task_list()) == 1 # no data should be on client yield sleep(0.1) assert task.data == [] assert task.response is None # now server sends some progress yield server.clients[0].send_event('task.progress.%s' % task.id, 'nami-nami') # and client should receive this data yield sleep(0.1) assert task.data == ['nami-nami'] assert task.is_running is True assert task.response is None # now our long-running process stopped and returned some result yield task_defered.callback('this is respnse') # and client should recieve this resul yield sleep(0.1) assert task.data == ['nami-nami'] assert task.is_running == False assert task.response == 'this is respnse' assert len(server.rpc_server.tasks_running) == 0 assert len(server.rpc_server.task_list()) == 0 #----------------------------------- # Cleanup #----------------------------------- client.shutdown() server.shutdown() yield sleep(0.1)
def run_server(redis): from mcloud.events import EventBus from mcloud.remote import ApiRpcServer, Server from mcloud.tasks import TaskService log.msg('Running server') eb = EventBus(redis) log.msg('Connecting event bus') yield eb.connect() log.msg('Configuring injector.') plugins_loaded = [] def my_config(binder): binder.bind(txredisapi.Connection, redis) binder.bind(EventBus, eb) binder.bind('settings', settings) binder.bind('host-ip', resolve_host_ip()) binder.bind('dns-search-suffix', settings.dns_search_suffix) binder.bind('plugins', plugins_loaded) # Configure a shared injector. inject.configure(my_config) api = inject.instance(ApiRpcServer) tasks = inject.instance(TaskService) api.tasks = tasks.collect_tasks() log.msg('Starting rpc listener on port %d' % settings.websocket_port) server = Server(port=settings.websocket_port) server.bind() # load plugins for ep in pkg_resources.iter_entry_points(group='mcloud_plugins'): try: plugin_class = ep.load() log.msg('=' * 80) log.msg('Loading plugin %s' % plugin_class) log.msg('-' * 80) yield verifyClass(IMcloudPlugin, plugin_class) plugin = plugin_class() yield plugin.setup() plugins_loaded.append(plugin) print "Loaded %s - OK" % plugin_class except Exception as e: print '!-' * 40 print e.__class__.__name__ print e print(traceback.format_exc()) print '!-' * 40 # reactor.stop() log.msg('=' * 80) log.msg('-' * 80) log.msg('All plugins loaded.') log.msg('=' * 80) deployment_controller = inject.instance(DeploymentController) yield deployment_controller.configure_docker_machine() log.msg('Started.')
def test_service_api(): from twisted.python import log redis = yield txredisapi.Connection(dbid=2) yield redis.flushdb() yield redis.set('mcloud-ticket-id', 123122) eb = EventBus(redis) yield eb.connect() redis = flexmock() redis.should_receive('hgetall').and_return({}) redis.should_receive('hget').and_return(None) fake_inject({ EventBus: eb, 'dns-server': 'local.dns', 'dns-search-suffix': 'local', IDockerClient: DockerTwistedClient(), txredisapi.Connection: redis }) name = 'test.foo' s = Service(image_builder=DockerfileImageBuilder( os.path.join(os.path.dirname(__file__), '_files/ct_bash')), name=name, client=inject.instance(IDockerClient)) class Printer(object): def publish(self, *args): print args s.client.message_publisher = Printer() yield s.inspect() if s.is_created(): yield s.stop(ticket_id=123123) yield s.destroy(ticket_id=123123) assert not s.is_created() assert not s.is_running() yield s.create(ticket_id=123123) assert s.is_created() assert not s.is_running() yield s.start(ticket_id=123123) assert s.is_created() assert s.is_running() yield s.stop(ticket_id=123123) assert s.is_created() assert not s.is_running() yield s.destroy(ticket_id=123123) assert not s.is_created() assert not s.is_running() # # # # @pytest.inlineCallbacks # def test_volume_snapshot(): # from twisted.python import log # log.startLogging(sys.stdout) # # redis = yield txredisapi.Connection(dbid=2) # yield redis.flushdb() # # yield redis.set('mcloud-ticket-id', 123122) # # eb = EventBus(redis) # yield eb.connect() # # with injector({EventBus: eb, 'dns-server': 'local.dns', 'dns-search-suffix': 'local', IDockerClient: DockerTwistedClient()}): # # name = 'test.foo' # # s = Service( # image_builder=DockerfileImageBuilder(os.path.join(os.path.dirname(__file__), '_files/ct_bash')), # name=name, # volumes=[ # {'local': os.path.join(os.path.dirname(__file__), '_files/boo'), 'remote': '/var/foo'} # ] # ) # # yield s.create(ticket_id=123123) # yield s.start(ticket_id=123123) # # assert s.is_created() # assert s.is_running() # # # # yield s.destroy(ticket_id=123123)
def test_service_api(): from twisted.python import log redis = yield txredisapi.Connection(dbid=2) yield redis.flushdb() yield redis.set('mcloud-ticket-id', 123122) eb = EventBus(redis) yield eb.connect() redis = flexmock() redis.should_receive('hgetall').and_return({}) redis.should_receive('hget').and_return(None) fake_inject({ EventBus: eb, 'dns-server': 'local.dns', 'dns-search-suffix': 'local', IDockerClient: DockerTwistedClient(), txredisapi.Connection: redis }) name = 'test.foo' s = Service( image_builder=DockerfileImageBuilder(os.path.join(os.path.dirname(__file__), '_files/ct_bash')), name=name, client=inject.instance(IDockerClient) ) class Printer(object): def publish(self, *args): print args s.client.message_publisher = Printer() yield s.inspect() if s.is_created(): yield s.stop(ticket_id=123123) yield s.destroy(ticket_id=123123) assert not s.is_created() assert not s.is_running() yield s.create(ticket_id=123123) assert s.is_created() assert not s.is_running() yield s.start(ticket_id=123123) assert s.is_created() assert s.is_running() yield s.stop(ticket_id=123123) assert s.is_created() assert not s.is_running() yield s.destroy(ticket_id=123123) assert not s.is_created() assert not s.is_running() # # # # @pytest.inlineCallbacks # def test_volume_snapshot(): # from twisted.python import log # log.startLogging(sys.stdout) # # redis = yield txredisapi.Connection(dbid=2) # yield redis.flushdb() # # yield redis.set('mcloud-ticket-id', 123122) # # eb = EventBus(redis) # yield eb.connect() # # with injector({EventBus: eb, 'dns-server': 'local.dns', 'dns-search-suffix': 'local', IDockerClient: DockerTwistedClient()}): # # name = 'test.foo' # # s = Service( # image_builder=DockerfileImageBuilder(os.path.join(os.path.dirname(__file__), '_files/ct_bash')), # name=name, # volumes=[ # {'local': os.path.join(os.path.dirname(__file__), '_files/boo'), 'remote': '/var/foo'} # ] # ) # # yield s.create(ticket_id=123123) # yield s.start(ticket_id=123123) # # assert s.is_created() # assert s.is_running() # # # # yield s.destroy(ticket_id=123123)
def test_task_terminate(): #----------------------------------- # preparations #----------------------------------- # cleanup a bit inject.clear() rc = yield redis.Connection(dbid=2) eb = EventBus(rc) yield eb.connect() def my_config(binder): binder.bind(redis.Connection, rc) binder.bind(EventBus, eb) binder.bind('settings', None) inject.configure(my_config) yield rc.flushdb() api = inject.instance(ApiRpcServer) #----------------------------------- # Test itself #----------------------------------- # this will emulate some long-running process task_defered = defer.Deferred() # this is mock that will execute our long-running process task = flexmock() task.should_receive('foo').with_args(int, 123, 'test').once().and_return(task_defered) # register our task api.tasks['baz'] = task.foo # start server -> real server on tcp port server = Server(port=9987, no_ssl=True) server.bind() # real client connecton here client = Client(port=9987, no_ssl=True) yield client.connect() # client calls a task task = Task('baz') yield client.call(task, 123, 'test') yield sleep(0.1) assert task.id > 0 assert task.name == 'baz' assert task.is_running is True # now client terminates the task yield sleep(0.1) client.terminate_task(task.id) yield sleep(0.1) assert task.is_running is False #----------------------------------- # Cleanup #----------------------------------- client.shutdown() server.shutdown() yield sleep(0.1)
def test_tasks(): #----------------------------------- # preparations #----------------------------------- # cleanup a bit inject.clear() rc = yield redis.Connection(dbid=2) eb = EventBus(rc) yield eb.connect() def my_config(binder): binder.bind(redis.Connection, rc) binder.bind(EventBus, eb) binder.bind('settings', None) inject.configure(my_config) yield rc.flushdb() api = inject.instance(ApiRpcServer) #----------------------------------- # Test itself #----------------------------------- # this will emulate some long-running process task_defered = defer.Deferred() # this is mock that will execute our long-running process task = flexmock() task.should_receive('foo').with_args(int, 123, 'test').once().and_return(task_defered) # register our task api.tasks['baz'] = task.foo # start server -> real server on tcp port server = Server(port=9997, no_ssl=True) server.bind() # real client connecton here client = Client(port=9997, no_ssl=True) yield client.connect() # client calls a task task = Task('baz') yield client.call(task, 123, 'test') yield sleep(0.1) assert task.id > 0 assert task.name == 'baz' assert task.is_running is True assert len(server.rpc_server.tasks_running) == 1 assert server.rpc_server.tasks_running[task.id]['name'] == 'baz' assert len(server.rpc_server.task_list()) == 1 # no data should be on client yield sleep(0.1) assert task.data == [] assert task.response is None # now server sends some progress yield server.clients[0].send_event('task.progress.%s' % task.id, 'nami-nami') # and client should receive this data yield sleep(0.1) assert task.data == ['nami-nami'] assert task.is_running is True assert task.response is None # now our long-running process stopped and returned some result yield task_defered.callback('this is respnse') # and client should recieve this resul yield sleep(0.1) assert task.data == ['nami-nami'] assert task.is_running == False assert task.response == 'this is respnse' assert len(server.rpc_server.tasks_running) == 0 assert len(server.rpc_server.task_list()) == 0 #----------------------------------- # Cleanup #----------------------------------- client.shutdown() server.shutdown() yield sleep(0.1)