def test_deployment_controller_new_app(): redis = yield txredisapi.Connection(dbid=2) yield redis.flushdb() eb = flexmock() ac = flexmock() def configure(binder): binder.bind(txredisapi.Connection, redis) binder.bind(EventBus, eb) binder.bind(ApplicationController, ac) with inject_services(configure): controller = DeploymentController() eb.should_receive('fire_event').once() yield controller.create('foo', 'foo.bar') r = yield controller.get('foo') assert r.apps == [] ac.should_receive('create').with_args('bar.foo', {'path': 'some/path'}, skip_validation=True).once() yield controller.new_app('foo', 'bar', {'path': 'some/path'}, skip_validation=True, skip_events=True) r = yield controller.get('foo') assert r.apps == ['bar.foo'] yield controller.remove_app('foo', 'bar') r = yield controller.get('foo') assert r.apps == []
def test_deployment_controller_publish_app(): redis = yield txredisapi.Connection(dbid=2) yield redis.flushdb() eb = flexmock() def configure(binder): binder.bind(txredisapi.Connection, redis) binder.bind(EventBus, eb) with inject_services(configure): controller = DeploymentController() eb.should_receive('fire_event').times(3) yield controller.create('foo', None) r = yield controller.get('foo') assert r.public_app is None yield controller.publish_app('foo', 'bar') r = yield controller.get('foo') assert r.public_app == 'bar.foo' yield controller.unpublish_app('foo') r = yield controller.get('foo') assert r.public_app is None
def test_list_deployments_task(): ac = flexmock() dc = flexmock() def configure(binder): binder.bind(ApplicationController, ac) binder.bind(DeploymentController, dc) with inject_services(configure): ac.should_receive('get').with_args('foo').and_return(defer.succeed(Application({'path': 'some/path'}))).once() dc.should_receive('list').and_return(defer.succeed([Deployment(public_domain='foo.bar', name='baz', apps=['foo'])])).once() ts = TaskService() r = yield ts.task_deployments(123123) assert r == [{ 'name': 'baz', 'public_domain': 'foo.bar', 'apps': [ { 'name': 'foo', 'config': {'path': 'some/path'} } ] }]
def test_init_app_task(): ac = flexmock() def configure(binder): binder.bind(ApplicationController, ac) with inject_services(configure): ac.should_receive('get').and_raise(AppDoesNotExist) ac.should_receive('create').with_args( 'foo', { 'path': 'some/path', 'deployment': None, 'source': 'foo', 'env': 'prod' }).and_return(defer.succeed(flexmock())) ac.should_receive('list').and_return( defer.succeed('result-of-list-operation')) ts = TaskService() r = yield ts.task_init(123123, 'foo', 'some/path', config='foo', env='prod') assert r is True
def test_list_deployments_task(): ac = flexmock() dc = flexmock() def configure(binder): binder.bind(ApplicationController, ac) binder.bind(DeploymentController, dc) with inject_services(configure): ac.should_receive('get').with_args('foo').and_return( defer.succeed(Application({'path': 'some/path'}))).once() dc.should_receive('list').and_return( defer.succeed([ Deployment(public_domain='foo.bar', name='baz', apps=['foo']) ])).once() ts = TaskService() r = yield ts.task_deployments(123123) assert r == [{ 'name': 'baz', 'public_domain': 'foo.bar', 'apps': [{ 'name': 'foo', 'config': { 'path': 'some/path' } }] }]
def test_set_var_task(): def timeout(): print('Can not connect to redis!') reactor.stop() redis = yield txtimeout(txredisapi.Connection(dbid=2), 2, timeout) yield redis.flushdb() def configure(binder): binder.bind(txredisapi.Connection, redis) with inject_services(configure): ts = TaskService() r = yield ts.task_list_vars(123123) assert r == {} r = yield ts.task_set_var(123123, 'boo', '123') assert r == {'boo': 123} r = yield ts.task_list_vars(123123) assert r == {'boo': 123} r = yield ts.task_set_var(123123, 'boo2', '1234') assert r == {'boo': 123, 'boo2': 1234} r = yield ts.task_rm_var(123123, 'boo') assert r == {'boo2': 1234}
def test_app_load(): def timeout(): print('Can not connect to redis!') reactor.stop() redis = yield txtimeout(txredisapi.Connection(dbid=2), 2, timeout) yield redis.flushdb() def configure(binder): binder.bind(txredisapi.Connection, redis) binder.bind(IDockerClient, DockerTwistedClient()) with inject_services(configure): app = Application({'path': os.path.realpath(os.path.dirname(__file__) + '/_files/')}, name='myapp') config = yield app.load() assert isinstance(config, YamlConfig) assert len(config.get_services()) == 1 assert config.app_name == 'myapp' service = config.get_services()['controller.myapp'] assert isinstance(service, Service) assert isinstance(service.image_builder, DockerfileImageBuilder) assert service.is_inspected()
def test_load_data(): ac = flexmock() docker = flexmock() docker.should_receive('find_container_by_name').and_return(defer.succeed(None)) def configure(binder): binder.bind(ApplicationController, ac) binder.bind(IDockerClient, docker) binder.bind('dns-search-suffix', 'foo.lh') with inject_services(configure): ac.should_receive('get').with_args('v1.baz').and_return(defer.succeed(Application({'source': 'srv: {image: bar}'}, 'v1.baz'))).once() ac.should_receive('get').with_args('v2.baz').and_return(defer.succeed(Application({'source': 'srv: {image: baz}'}, 'v2.baz'))).once() d = Deployment(public_domain='foo.bar', name='baz', apps=['v1.baz', 'v2.baz']) config = yield d.load_data(skip_validation=True) print config assert config['name'] == 'baz' assert config['public_domain'] == 'foo.bar' assert config['public_app'] == None assert len(config['apps']) == 2
def test_deployment_controller(): redis = yield txredisapi.Connection(dbid=2) yield redis.flushdb() eb = flexmock() def configure(binder): binder.bind(txredisapi.Connection, redis) binder.bind(EventBus, eb) with inject_services(configure): controller = DeploymentController() with pytest.raises(DeploymentDoesNotExist): yield controller.get('foo') eb.should_receive('fire_event').with_args( 'new-deployment', public_app=None, apps=[], name='foo', public_domain='foo.bar').once() eb.should_receive('fire_event').with_args( 'new-deployment', public_app=None, apps=[], name='boo', public_domain='other.path').once() r = yield controller.create('foo', 'foo.bar') assert isinstance(r, Deployment) assert r.public_domain == 'foo.bar' assert r.name == 'foo' r = yield controller.get('foo') assert r.public_domain == 'foo.bar' assert r.name == 'foo' r = yield controller.create('boo', 'other.path') assert isinstance(r, Deployment) assert r.public_domain == 'other.path' assert r.name == 'boo' r = yield controller.list() assert isinstance(r, list) assert len(r) == 2 for app in r: assert isinstance(app, Deployment) eb.should_receive('fire_event').with_args('remove-deployment', name='foo').once() yield controller.remove('foo') with pytest.raises(DeploymentDoesNotExist): yield controller.get('foo')
def test_inject_services(): class Boo(): pass class Baz(): pass def configure(binder): binder.bind(Boo, Baz()) with inject_services(configure): instance = inject.instance(Boo) assert isinstance(instance, Baz)
def test_app_controller(): def timeout(): print('Can not connect to redis!') reactor.stop() redis = yield txtimeout(txredisapi.Connection(dbid=2), 2, timeout) yield redis.flushdb() def configure(binder): binder.bind(txredisapi.Connection, redis) with inject_services(configure): controller = ApplicationController() with pytest.raises(AppDoesNotExist): yield controller.get('foo') r = yield controller.create('foo', {'path': 'some/path'}, skip_validation=True) assert isinstance(r, Application) assert r.name == 'foo' assert r.config['path'] == 'some/path' r = yield controller.get('foo') assert isinstance(r, Application) assert r.name == 'foo' assert r.config['path'] == 'some/path' r = yield controller.create('boo', {'path': 'other/path'}, skip_validation=True) assert isinstance(r, Application) assert r.name == 'boo' assert r.config['path'] == 'other/path' mockapp = flexmock() flexmock(Application).new_instances(mockapp) mockapp.should_receive('load').with_args(need_details=True).and_return(defer.succeed({'foo': 'bar'})) r = yield controller.list() assert isinstance(r, list) assert len(r) == 2 for app in r: assert app == {'foo': 'bar'} yield controller.remove('foo') with pytest.raises(AppDoesNotExist): yield controller.get('foo')
def test_push_task(): ac = flexmock() ac.should_receive('list').and_return(defer.succeed(['foo', 'bar'])).once() def configure(binder): binder.bind(ApplicationController, ac) with inject_services(configure): ts = TaskService() r = yield ts.task_list(123123) assert r == ['foo', 'bar']
def test_deployment_new_app_task_source(): dc = flexmock() def configure(binder): binder.bind(DeploymentController, dc) with inject_services(configure): dc.should_receive('new_app').with_args('baz', 'foo', {'source': 'foo: bar'}).and_return(defer.succeed(flexmock())).once() ts = TaskService() r = yield ts.task_deployment_new_app_source(123123, 'baz', 'foo', 'foo: bar') assert r is True
def test_init_app_task(): ac = flexmock() def configure(binder): binder.bind(ApplicationController, ac) with inject_services(configure): ac.should_receive('get').and_raise(AppDoesNotExist) ac.should_receive('create').with_args('foo', {'path': 'some/path', 'deployment': None, 'source': 'foo', 'env': 'prod'}).and_return(defer.succeed(flexmock())) ac.should_receive('list').and_return(defer.succeed('result-of-list-operation')) ts = TaskService() r = yield ts.task_init(123123, 'foo', 'some/path', config='foo', env='prod') assert r is True
def test_deployment_controller(): redis = yield txredisapi.Connection(dbid=2) yield redis.flushdb() eb = flexmock() def configure(binder): binder.bind(txredisapi.Connection, redis) binder.bind(EventBus, eb) with inject_services(configure): controller = DeploymentController() with pytest.raises(DeploymentDoesNotExist): yield controller.get('foo') eb.should_receive('fire_event').with_args('new-deployment', public_app=None, apps=[], name='foo', public_domain='foo.bar').once() eb.should_receive('fire_event').with_args('new-deployment', public_app=None, apps=[], name='boo', public_domain='other.path').once() r = yield controller.create('foo', 'foo.bar') assert isinstance(r, Deployment) assert r.public_domain == 'foo.bar' assert r.name == 'foo' r = yield controller.get('foo') assert r.public_domain == 'foo.bar' assert r.name == 'foo' r = yield controller.create('boo', 'other.path') assert isinstance(r, Deployment) assert r.public_domain == 'other.path' assert r.name == 'boo' r = yield controller.list() assert isinstance(r, list) assert len(r) == 2 for app in r: assert isinstance(app, Deployment) eb.should_receive('fire_event').with_args('remove-deployment', name='foo').once() yield controller.remove('foo') with pytest.raises(DeploymentDoesNotExist): yield controller.get('foo')
def test_deployment_new_app_task_source(): dc = flexmock() def configure(binder): binder.bind(DeploymentController, dc) with inject_services(configure): dc.should_receive('new_app').with_args('baz', 'foo', { 'source': 'foo: bar' }).and_return(defer.succeed(flexmock())).once() ts = TaskService() r = yield ts.task_deployment_new_app_source(123123, 'baz', 'foo', 'foo: bar') assert r is True
def test_register_file(): rc = yield txredisapi.Connection(dbid=2) yield rc.flushdb() def configure(binder): binder.bind(txredisapi.Connection, rc) with inject_services(configure): ts = TaskService() r = yield ts.task_register_file(None) assert r == 1 r = yield ts.task_register_file(None) assert r == 2 r = yield ts.task_register_file(None) assert r == 3
def test_deployment_controller_new_app(): redis = yield txredisapi.Connection(dbid=2) yield redis.flushdb() eb = flexmock() ac = flexmock() def configure(binder): binder.bind(txredisapi.Connection, redis) binder.bind(EventBus, eb) binder.bind(ApplicationController, ac) with inject_services(configure): controller = DeploymentController() eb.should_receive('fire_event').once() yield controller.create('foo', 'foo.bar') r = yield controller.get('foo') assert r.apps == [] ac.should_receive('create').with_args('bar.foo', { 'path': 'some/path' }, skip_validation=True).once() yield controller.new_app('foo', 'bar', {'path': 'some/path'}, skip_validation=True, skip_events=True) r = yield controller.get('foo') assert r.apps == ['bar.foo'] yield controller.remove_app('foo', 'bar') r = yield controller.get('foo') assert r.apps == []
def test_load_data(): ac = flexmock() docker = flexmock() docker.should_receive('find_container_by_name').and_return( defer.succeed(None)) def configure(binder): binder.bind(ApplicationController, ac) binder.bind(IDockerClient, docker) binder.bind('dns-search-suffix', 'foo.lh') with inject_services(configure): ac.should_receive('get').with_args('v1.baz').and_return( defer.succeed( Application({'source': 'srv: {image: bar}'}, 'v1.baz'))).once() ac.should_receive('get').with_args('v2.baz').and_return( defer.succeed( Application({'source': 'srv: {image: baz}'}, 'v2.baz'))).once() d = Deployment(public_domain='foo.bar', name='baz', apps=['v1.baz', 'v2.baz']) config = yield d.load_data(skip_validation=True) print config assert config['name'] == 'baz' assert config['public_domain'] == 'foo.bar' assert config['public_app'] == None assert len(config['apps']) == 2