def test_start_ports(): with injector({'dns-server': 'local.dns', 'dns-search-suffix': 'local'}): s = Service() s.name = 'my_service' s.ports = ['22/tcp'] flexmock(s) s.client = flexmock() s.client.should_receive('find_container_by_name').with_args( 'my_service').once().and_return(defer.succeed('123abc')) s.client.should_receive('start_container').with_args( '123abc', ticket_id=123123, config={ "PortBindings": { '22/tcp': [{}] }, 'DnsSearch': 'None.local', 'Dns': ['local.dns'] }).once().and_return(defer.succeed('boo')) s.should_receive('inspect').with_args().once().and_return( defer.succeed('baz')) r = yield s.start(ticket_id=123123) assert r == 'baz'
def test_start_volumes(): with injector({'dns-server': 'local.dns', 'dns-search-suffix': 'local'}): s = Service() s.name = 'my_service' s.volumes = [ {'local': '/base/path/foo1', 'remote': '/bar1'}, {'local': '/base/path/foo2', 'remote': '/bar2'}, {'local': '/base/path/foo3', 'remote': '/bar3'} ] flexmock(s) s.client = flexmock() s.client.should_receive('find_container_by_name').with_args('my_service').once().and_return(defer.succeed('123abc')) s.client.should_receive('start_container').with_args('123abc', ticket_id=123123, config={ "Binds": ['/base/path/foo1:/bar1', '/base/path/foo2:/bar2', '/base/path/foo3:/bar3'], 'DnsSearch': 'None.local', 'Dns': ['local.dns'] }).once().and_return(defer.succeed('boo')) s.should_receive('inspect').with_args().once().and_return(defer.succeed('baz')) r = yield s.start(ticket_id=123123) assert r == 'baz'
def test_start_volumes_from(): with injector({'dns-server': 'local.dns', 'dns-search-suffix': 'local'}): s = Service() s.name = 'my_service' s.volumes_from = ['foo', 'bar'] flexmock(s) s.client = flexmock() s.client.should_receive('find_container_by_name').with_args( 'my_service').once().and_return(defer.succeed('123abc')) s.client.should_receive('start_container').with_args( '123abc', ticket_id=123123, config={ "VolumesFrom": ['foo', 'bar'], 'DnsSearch': 'None.local', 'Dns': ['local.dns'] }).once().and_return(defer.succeed('boo')) s.should_receive('inspect').with_args().once().and_return( defer.succeed('baz')) r = yield s.start(ticket_id=123123) assert r == 'baz'
def test_start(): with injector({'dns-server': 'local.dns', 'dns-search-suffix': 'local'}): s = Service() s.name = 'my_service' flexmock(s) s.client = flexmock() s.client.should_receive('find_container_by_name').with_args('my_service').once().and_return(defer.succeed('123abc')) s.client.should_receive('start_container').with_args('123abc', ticket_id=123123, config={'DnsSearch': 'None.local', 'Dns': ['local.dns']}).once().and_return(defer.succeed('boo')) s.should_receive('inspect').with_args().once().and_return(defer.succeed('baz')) r = yield s.start(ticket_id=123123) assert r == 'baz'
def test_start_volumes(): with injector({'dns-server': 'local.dns', 'dns-search-suffix': 'local'}): s = Service() s.name = 'my_service' s.volumes = [{ 'local': '/base/path/foo1', 'remote': '/bar1' }, { 'local': '/base/path/foo2', 'remote': '/bar2' }, { 'local': '/base/path/foo3', 'remote': '/bar3' }] flexmock(s) s.client = flexmock() s.client.should_receive('find_container_by_name').with_args( 'my_service').once().and_return(defer.succeed('123abc')) s.client.should_receive('start_container').with_args( '123abc', ticket_id=123123, config={ "Binds": [ '/base/path/foo1:/bar1', '/base/path/foo2:/bar2', '/base/path/foo3:/bar3' ], 'DnsSearch': 'None.local', 'Dns': ['local.dns'] }).once().and_return(defer.succeed('boo')) s.should_receive('inspect').with_args().once().and_return( defer.succeed('baz')) r = yield s.start(ticket_id=123123) assert r == 'baz'
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 task_sync(self, ticket_id, app_name, service_name, volume): app = yield self.app_controller.get(app_name) config = yield app.load() client = yield app.get_client() s = Service(client=client) s.app_name = app_name s.name = '%s_%s_%s' % (app_name, '_rsync_', ticket_id) s.image_builder = PrebuiltImageBuilder(image='modera/rsync') s.ports = [873] if service_name: if not volume: raise VolumeNotFound('In case of service name is provided, volume name is mandatory!') services = config.get_services() service_full_name = '%s.%s' % (service_name, app_name) try: service = services[service_full_name] all_volumes = service.list_volumes() if not volume in all_volumes: raise VolumeNotFound('Volume with name %s no found!' % volume) volume_name = volume s.volumes_from = service_full_name except KeyError: raise VolumeNotFound('Service with name %s was not found!' % service_name) else: s.volumes = [{ 'local': app.config['path'], 'remote': '/volume' }] volume_name = '/volume' s.env = { 'USERNAME': ''.join(random.choice(string.ascii_lowercase + string.digits) for _ in range(32)), 'PASSWORD': ''.join( random.choice(string.ascii_lowercase + string.punctuation + string.digits) for _ in range(32)), 'ALLOW': '*' } yield s.start(ticket_id) deployment = yield app.get_deployment() if deployment.local: sync_host = 'me' else: sync_host = deployment.host print s.public_ports() defer.returnValue({ 'env': s.env, 'container': s.name, 'host': sync_host, 'port': s.public_ports()['873/tcp'][0]['HostPort'], 'volume': volume_name, 'ticket_id': ticket_id })
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 task_sync(self, ticket_id, app_name, service_name, volume): app = yield self.app_controller.get(app_name) config = yield app.load() client = yield app.get_client() s = Service(client=client) s.app_name = app_name s.name = '%s_%s_%s' % (app_name, '_rsync_', ticket_id) s.image_builder = PrebuiltImageBuilder(image='modera/rsync') s.ports = [873] if service_name: if not volume: raise VolumeNotFound( 'In case of service name is provided, volume name is mandatory!' ) services = config.get_services() service_full_name = '%s.%s' % (service_name, app_name) try: service = services[service_full_name] all_volumes = service.list_volumes() if not volume in all_volumes: raise VolumeNotFound('Volume with name %s no found!' % volume) volume_name = volume s.volumes_from = service_full_name except KeyError: raise VolumeNotFound('Service with name %s was not found!' % service_name) else: s.volumes = [{'local': app.config['path'], 'remote': '/volume'}] volume_name = '/volume' s.env = { 'USERNAME': ''.join( random.choice(string.ascii_lowercase + string.digits) for _ in range(32)), 'PASSWORD': ''.join( random.choice(string.ascii_lowercase + string.punctuation + string.digits) for _ in range(32)), 'ALLOW': '*' } yield s.start(ticket_id) deployment = yield app.get_deployment() if deployment.local: sync_host = 'me' else: sync_host = deployment.host print s.public_ports() defer.returnValue({ 'env': s.env, 'container': s.name, 'host': sync_host, 'port': s.public_ports()['873/tcp'][0]['HostPort'], 'volume': volume_name, 'ticket_id': ticket_id })