Beispiel #1
0
def test_generate_config_volumes():

    redis = flexmock()
    redis.should_receive('hgetall').and_return([])

    fake_inject({txredisapi.Connection: redis})

    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'
    }]

    config = yield s._generate_config('foo')
    assert config == {
        "Hostname": 'my_service',
        "Image": 'foo',

        # Volumes are not passed as of Docker 1.6 version
        # "Volumes": {
        #     "/bar1": {},
        #     "/bar2": {},
        #     "/bar3": {},
        # }
    }
Beispiel #2
0
def test_create():

    redis = flexmock()
    redis.should_receive('hgetall').and_return([])

    fake_inject({txredisapi.Connection: redis})

    s = Service()
    s.name = 'my_service'
    flexmock(s)

    s.image_builder = flexmock()
    s.image_builder.should_receive('build_image').with_args(ticket_id=123123, service=s).ordered().once()\
        .and_return(defer.succeed('boo'))

    s.client = flexmock()
    s.client.should_receive('create_container').with_args(
        {
            "Hostname": 'my_service',
            "Image": 'boo'
        },
        'my_service',
        ticket_id=123123).ordered().once().and_return('magic')

    s.should_receive('inspect').with_args().ordered().once().and_return(
        'magic')

    r = yield s.create(ticket_id=123123)

    assert r == 'magic'
Beispiel #3
0
def test_generate_config_volumes():

    redis = flexmock()
    redis.should_receive('hgetall').and_return([])

    fake_inject({
        txredisapi.Connection: redis
    })

    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'}
    ]

    config = yield s._generate_config('foo')
    assert config == {
        "Hostname": 'my_service',
        "Image": 'foo',

        # Volumes are not passed as of Docker 1.6 version
        # "Volumes": {
        #     "/bar1": {},
        #     "/bar2": {},
        #     "/bar3": {},
        # }
    }
Beispiel #4
0
def test_create():

    redis = flexmock()
    redis.should_receive('hgetall').and_return([])

    fake_inject({
        txredisapi.Connection: redis
    })

    s = Service()
    s.name = 'my_service'
    flexmock(s)

    s.image_builder = flexmock()
    s.image_builder.should_receive('build_image').with_args(ticket_id=123123, service=s).ordered().once()\
        .and_return(defer.succeed('boo'))

    s.client = flexmock()
    s.client.should_receive('create_container').with_args({
        "Hostname": 'my_service',
        "Image": 'boo'
    }, 'my_service', ticket_id=123123).ordered().once().and_return('magic')

    s.should_receive('inspect').with_args().ordered().once().and_return('magic')

    r = yield s.create(ticket_id=123123)

    assert r == 'magic'
Beispiel #5
0
def test_generate_config():

    redis = flexmock()
    redis.should_receive('hgetall').and_return([])

    fake_inject({txredisapi.Connection: redis})

    s = Service()
    s.name = 'my_service'

    config = yield s._generate_config('foo')

    assert config == {"Hostname": 'my_service', "Image": 'foo'}
Beispiel #6
0
def test_generate_config_env():

    redis = flexmock()
    redis.should_receive('hgetall').and_return({})

    fake_inject({txredisapi.Connection: redis})

    s = Service()
    s.name = 'my_service'
    s.env = {'FOO': 'bar', 'BAZ': 'foo'}

    config = yield s._generate_config('foo')
    assert config == {
        "Hostname": 'my_service',
        "Image": 'foo',
        "Env": ['FOO=bar', 'BAZ=foo']
    }
Beispiel #7
0
def test_generate_config():

    redis = flexmock()
    redis.should_receive('hgetall').and_return([])

    fake_inject({
        txredisapi.Connection: redis
    })

    s = Service()
    s.name = 'my_service'

    config = yield s._generate_config('foo')

    assert config == {
        "Hostname": 'my_service',
        "Image": 'foo'
    }
Beispiel #8
0
def test_generate_config_env():

    redis = flexmock()
    redis.should_receive('hgetall').and_return({})

    fake_inject({
        txredisapi.Connection: redis
    })

    s = Service()
    s.name = 'my_service'
    s.env = {'FOO': 'bar', 'BAZ': 'foo'}

    config = yield s._generate_config('foo')
    assert config == {
        "Hostname": 'my_service',
        "Image": 'foo',
        "Env": ['FOO=bar', 'BAZ=foo']
    }
Beispiel #9
0
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)
Beispiel #10
0
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)