예제 #1
0
def test_native_container_activate_only(agent):
    # Recieving an activate event for a running, pre-existing container should
    # result in the container continuing to run and the appropriate data sent
    # back in the response (like, ports, ip, inspect, etc)
    delete_container('/native_container')

    client = docker_client()
    c = client.create_container('ibuildthecloud/helloworld:latest',
                                name='native_container')
    client.start(c)
    inspect = docker_client().inspect_container(c['Id'])

    def pre(req):
        instance = req['data']['instanceHostMap']['instance']
        instance['externalId'] = c['Id']

    def post(req, resp, valid_resp):
        instance_data = resp['data']['instanceHostMap']['instance']['+data']
        docker_inspect = instance_data['dockerInspect']
        assert docker_inspect['Id'] == inspect['Id']
        assert docker_inspect['State']['Running']
        container_field_test_boiler_plate(resp)

        docker_container = instance_data['dockerContainer']
        fields = instance_data['+fields']
        trim(docker_container, fields, resp, valid_resp)

    event_test(agent, 'docker/native_container_activate',
               pre_func=pre, post_func=post)
예제 #2
0
def test_native_container_activate_not_running(agent):
    # Receiving an activate event for a pre-existing stopped container
    # that Rancher never recorded as having started should result in the
    # container staying stopped.
    delete_container('/native_container')

    client = docker_client()
    c = client.create_container('ibuildthecloud/helloworld:latest',
                                name='native_container')
    inspect = docker_client().inspect_container(c['Id'])

    def pre(req):
        instance = req['data']['instanceHostMap']['instance']
        instance['externalId'] = c['Id']

    def post(req, resp, valid_resp):
        instance_data = resp['data']['instanceHostMap']['instance']['+data']
        docker_inspect = instance_data['dockerInspect']
        assert docker_inspect['Id'] == inspect['Id']
        assert not docker_inspect['State']['Running']
        container_field_test_boiler_plate(resp)

        docker_container = instance_data['dockerContainer']
        fields = instance_data['+fields']
        trim(docker_container, fields, resp, valid_resp)

    event_test(agent, 'docker/native_container_not_running',
               pre_func=pre, post_func=post)
예제 #3
0
def test_native_container_activate_only(agent):
    # Recieving an activate event for a running, pre-existing container should
    # result in the container continuing to run and the appropriate data sent
    # back in the response (like, ports, ip, inspect, etc)
    delete_container('/native_container')

    client = docker_client()
    c = client.create_container('ibuildthecloud/helloworld:latest',
                                name='native_container')
    client.start(c)
    inspect = docker_client().inspect_container(c['Id'])

    def pre(req):
        instance = req['data']['instanceHostMap']['instance']
        instance['externalId'] = c['Id']

    def post(req, resp, valid_resp):
        instance_data = resp['data']['instanceHostMap']['instance']['+data']
        docker_inspect = instance_data['dockerInspect']
        assert docker_inspect['Id'] == inspect['Id']
        assert docker_inspect['State']['Running']
        container_field_test_boiler_plate(resp)

        docker_container = instance_data['dockerContainer']
        fields = instance_data['+fields']
        trim(docker_container, fields, resp, valid_resp)

    event_test(agent,
               'docker/native_container_activate',
               pre_func=pre,
               post_func=post)
예제 #4
0
def test_native_container_activate_no_op(agent):
    # If a container receieves a no-op activate event, it should not
    # be activated.
    test_native_container_activate_only(agent)

    c = get_container('/native_container')
    docker_client().stop(c)

    def pre(req):
        instance = req['data']['instanceHostMap']['instance']
        instance['externalId'] = c['Id']

    def post(req, resp, valid_resp):
        instance_data = resp['data']['instanceHostMap']['instance']['+data']
        instance_data['dockerContainer']['Ports'].append(
            {'Type': 'tcp', 'PrivatePort': 8080})
        instance_data['+fields']['dockerPorts'].append('8080/tcp')
        docker_inspect = instance_data['dockerInspect']
        assert not docker_inspect['State']['Running']
        container_field_test_boiler_plate(resp)

        docker_container = instance_data['dockerContainer']
        fields = instance_data['+fields']
        trim(docker_container, fields, resp, valid_resp)

    event_test(agent, 'docker/native_container_activate',
               pre_func=pre, post_func=post)
예제 #5
0
def test_native_container_activate_no_op(agent):
    # If a container receieves a no-op activate event, it should not
    # be activated.
    test_native_container_activate_only(agent)

    c = get_container('/native_container')
    docker_client().stop(c)

    def pre(req):
        instance = req['data']['instanceHostMap']['instance']
        instance['externalId'] = c['Id']

    def post(req, resp, valid_resp):
        instance_data = resp['data']['instanceHostMap']['instance']['+data']
        instance_data['dockerContainer']['Ports'].append({
            'Type': 'tcp',
            'PrivatePort': 8080
        })
        instance_data['+fields']['dockerPorts'].append('8080/tcp')
        docker_inspect = instance_data['dockerInspect']
        assert not docker_inspect['State']['Running']
        container_field_test_boiler_plate(resp)

        docker_container = instance_data['dockerContainer']
        fields = instance_data['+fields']
        trim(docker_container, fields, resp, valid_resp)

    event_test(agent,
               'docker/native_container_activate',
               pre_func=pre,
               post_func=post)
예제 #6
0
def test_native_container_activate_not_running(agent):
    # Receiving an activate event for a pre-existing stopped container
    # that Rancher never recorded as having started should result in the
    # container staying stopped.
    delete_container('/native_container')

    client = docker_client()
    c = client.create_container('ibuildthecloud/helloworld:latest',
                                name='native_container')
    inspect = docker_client().inspect_container(c['Id'])

    def pre(req):
        instance = req['data']['instanceHostMap']['instance']
        instance['externalId'] = c['Id']

    def post(req, resp, valid_resp):
        instance_data = resp['data']['instanceHostMap']['instance']['+data']
        docker_inspect = instance_data['dockerInspect']
        assert docker_inspect['Id'] == inspect['Id']
        assert not docker_inspect['State']['Running']
        container_field_test_boiler_plate(resp)

        docker_container = instance_data['dockerContainer']
        fields = instance_data['+fields']
        trim(docker_container, fields, resp, valid_resp)

    event_test(agent,
               'docker/native_container_not_running',
               pre_func=pre,
               post_func=post)
예제 #7
0
def test_inspect_by_id(agent):
    delete_container('/inspect_test')

    client = docker_client()
    c = client.create_container('ibuildthecloud/helloworld',
                                name='inspect_test')
    inspect = docker_client().inspect_container(c['Id'])

    def pre(req):
        instance_inspect = req['data']['instanceInspect']
        instance_inspect['id'] = c['Id']
        del instance_inspect['name']

    def post(req, resp, valid_resp):
        response_inspect = resp['data']['instanceInspect']

        # can't compare the inspect from go api and py api
        # TODO find a new way to assert
        assert response_inspect['Id'] == inspect['Id']
        # diff_dict(inspect, response_inspect)

    event_test(agent,
               'docker/instance_inspect',
               pre_func=pre,
               post_func=post,
               diff=False)
예제 #8
0
def test_native_container_activate_removed(agent):
    # Receiving an activate event for a pre-existing, but removed container
    # should result in the container continuing to not exist and a valid but
    # minimally populated response.
    delete_container('/native_container')
    client = docker_client()
    c = client.create_container('ibuildthecloud/helloworld',
                                name='native_container')
    delete_container('/native_container')

    def pre(req):
        instance = req['data']['instanceHostMap']['instance']
        instance['externalId'] = c['Id']

    def post(req, resp, valid_resp):
        instance_data = resp['data']['instanceHostMap']['instance']['+data']
        assert not instance_data['dockerInspect']
        assert not instance_data['dockerContainer']
        fields = instance_data['+fields']
        assert not fields['dockerIp']
        assert not fields['dockerPorts']
        # assert fields['dockerHostIp']
        assert not get_container('/native_container')

    event_test(agent, 'docker/native_container_not_running',
               pre_func=pre, post_func=post, diff=False)
예제 #9
0
def test_inspect_by_name(agent):
    delete_container('/inspect_test')

    client = docker_client()
    c = client.create_container('ibuildthecloud/helloworld',
                                name='inspect_test')
    inspect = docker_client().inspect_container(c['Id'])

    def post(req, resp):
        response_inspect = resp['data']['instanceInspect']
        # diff_dict(inspect, response_inspect)
        assert response_inspect['Id'] == inspect['Id']
        del resp['links']
        del resp['actions']

    event_test(agent, 'docker/instance_inspect', post_func=post, diff=False)
예제 #10
0
def test_pull(agent):
    client = docker_client()
    try:
        client.remove_image('tianon/true:latestrandom', force=True)
    except:
        pass

    def post(req, resp, valid_resp):
        inspect = client.inspect_image('tianon/true:latest')
        assert resp['data']['fields']['dockerImage']['Id'] == inspect['Id']
        resp['data']['fields']['dockerImage'] = {}
        del resp['links']
        del resp['actions']
        del valid_resp['resourceId']

    event_test(agent, 'docker/instance_pull', post_func=post, diff=True)

    inspect = client.inspect_image('tianon/true:latestrandom')
    assert inspect is not None

    def pre2(req):
        req['data']['instancePull']['complete'] = True

    def post2(req, resp, valid_resp):
        assert resp['data']['fields']['dockerImage']['Id'] == ''
        del resp['links']
        del resp['actions']
        del valid_resp['resourceId']

    event_test(agent, 'docker/instance_pull', pre_func=pre2,  post_func=post2,
               diff=False)

    with pytest.raises(APIError):
        client.inspect_image('tianon/true:latestrandom')
예제 #11
0
def test_network_mode_container(agent):
    delete_container('/network-container')
    delete_container('/c861f990-4472-4fa1-960f-65171b544c28')

    client = docker_client()
    c = client.create_container('ibuildthecloud/helloworld',
                                name='network-container')
    client.start(c)

    def pre(req):
        instance = req['data']['instanceHostMap']['instance']
        instance['nics'][0]['network']['kind'] = 'dockerContainer'
        instance['networkContainer'] = JsonObject({
            'uuid': 'network-container'
        })

    def post(req, resp, valid_resp):
        instance_data = resp['data']['instanceHostMap']['instance']['+data']
        docker_inspect = instance_data['dockerInspect']
        assert 'NetworkDisabled' not in docker_inspect['Config']
        assert docker_inspect['HostConfig']['NetworkMode'] == \
            'container:{}'.format(c['Id'])

        docker_container = instance_data['dockerContainer']
        fields = instance_data['+fields']
        trim(docker_container, fields, resp, valid_resp)

    event_test(agent, 'docker/instance_activate', pre_func=pre,
               post_func=post, diff=False)
예제 #12
0
def test_native_container_activate_removed(agent):
    # Receiving an activate event for a pre-existing, but removed container
    # should result in the container continuing to not exist and a valid but
    # minimally populated response.
    delete_container('/native_container')
    client = docker_client()
    c = client.create_container('ibuildthecloud/helloworld',
                                name='native_container')
    delete_container('/native_container')

    def pre(req):
        instance = req['data']['instanceHostMap']['instance']
        instance['externalId'] = c['Id']

    def post(req, resp, valid_resp):
        instance_data = resp['data']['instanceHostMap']['instance']['+data']
        assert not instance_data['dockerInspect']
        assert not instance_data['dockerContainer']
        fields = instance_data['+fields']
        assert not fields['dockerIp']
        assert not fields['dockerPorts']
        # assert fields['dockerHostIp']
        assert not get_container('/native_container')

    event_test(agent,
               'docker/native_container_not_running',
               pre_func=pre,
               post_func=post,
               diff=False)
예제 #13
0
def test_inspect_by_name(agent):
    delete_container('/inspect_test')

    client = docker_client()
    c = client.create_container('ibuildthecloud/helloworld',
                                name='inspect_test')
    inspect = docker_client().inspect_container(c['Id'])

    def post(req, resp):
        response_inspect = resp['data']['instanceInspect']
        # diff_dict(inspect, response_inspect)
        assert response_inspect['Id'] == inspect['Id']
        del resp['links']
        del resp['actions']

    event_test(agent, 'docker/instance_inspect',
               post_func=post, diff=False)
예제 #14
0
def test_native_container_remove(agent):
    test_native_container_activate_only(agent)

    c = get_container('/native_container')
    docker_client().stop(c)

    def pre(req):
        instance = req['data']['volumeStoragePoolMap']['volume']['instance']
        instance['externalId'] = c['Id']

    def post(req, resp, valid_resp):
        del resp['links']
        del resp['actions']

    event_test(agent, 'docker/native_container_volume_remove',
               pre_func=pre, post_func=post)

    # Removing a removed container. Should be error free
    event_test(agent, 'docker/native_container_volume_remove',
               pre_func=pre, post_func=post)
예제 #15
0
def test_inspect_by_id(agent):
    delete_container('/inspect_test')

    client = docker_client()
    c = client.create_container('ibuildthecloud/helloworld',
                                name='inspect_test')
    inspect = docker_client().inspect_container(c['Id'])

    def pre(req):
        instance_inspect = req['data']['instanceInspect']
        instance_inspect['id'] = c['Id']
        del instance_inspect['name']

    def post(req, resp, valid_resp):
        response_inspect = resp['data']['instanceInspect']

        # can't compare the inspect from go api and py api
        # TODO find a new way to assert
        assert response_inspect['Id'] == inspect['Id']
        # diff_dict(inspect, response_inspect)

    event_test(agent, 'docker/instance_inspect', pre_func=pre,
               post_func=post, diff=False)
예제 #16
0
def test_native_container_remove(agent):
    test_native_container_activate_only(agent)

    c = get_container('/native_container')
    docker_client().stop(c)

    def pre(req):
        instance = req['data']['volumeStoragePoolMap']['volume']['instance']
        instance['externalId'] = c['Id']

    def post(req, resp, valid_resp):
        del resp['links']
        del resp['actions']

    event_test(agent,
               'docker/native_container_volume_remove',
               pre_func=pre,
               post_func=post)

    # Removing a removed container. Should be error free
    event_test(agent,
               'docker/native_container_volume_remove',
               pre_func=pre,
               post_func=post)
예제 #17
0
def test_pull_on_create(agent):
    client = docker_client()

    event_test(agent, 'docker/instance_activate', diff=False)
    delete_container('/c861f990-4472-4fa1-960f-65171b544c28')

    client.pull('tianon/true')
    client.tag('tianon/true', 'ibuildthecloud/helloworld', 'latest',
               force=True)
    old_image = client.inspect_image('ibuildthecloud/helloworld')

    event_test(agent, 'docker/instance_activate_pull', diff=False)
    image = client.inspect_image('ibuildthecloud/helloworld')

    assert image['Id'] != old_image['Id']
    assert image['Id'] != client.inspect_image('tianon/true')['Id']
예제 #18
0
def test_pull_mode_update(agent):
    client = docker_client()

    with pytest.raises(APIError):
        client.inspect_image('garbage')

    def pre(req):
        req.data.instancePull.image.data.dockerImage.fullName = 'garbage'
        req.data.instancePull.mode = 'cached'

    def post(req, resp, valid_resp):
        assert resp['data']['fields']['dockerImage']['Id'] == ''
        del resp['links']
        del resp['actions']

    event_test(agent, 'docker/instance_pull', pre_func=pre,  post_func=post,
               diff=False)
예제 #19
0
def test_pull_on_create(agent):
    client = docker_client()

    event_test(agent, 'docker/instance_activate', diff=False)
    delete_container('/c861f990-4472-4fa1-960f-65171b544c28')

    client.pull('tianon/true')
    client.tag('tianon/true',
               'ibuildthecloud/helloworld',
               'latest',
               force=True)
    old_image = client.inspect_image('ibuildthecloud/helloworld')

    event_test(agent, 'docker/instance_activate_pull', diff=False)
    image = client.inspect_image('ibuildthecloud/helloworld')

    assert image['Id'] != old_image['Id']
    assert image['Id'] != client.inspect_image('tianon/true')['Id']
예제 #20
0
def test_pull_mode_update(agent):
    client = docker_client()

    with pytest.raises(APIError):
        client.inspect_image('garbage')

    def pre(req):
        req.data.instancePull.image.data.dockerImage.fullName = 'garbage'
        req.data.instancePull.mode = 'cached'

    def post(req, resp, valid_resp):
        assert resp['data']['fields']['dockerImage']['Id'] == ''
        del resp['links']
        del resp['actions']

    event_test(agent,
               'docker/instance_pull',
               pre_func=pre,
               post_func=post,
               diff=False)
예제 #21
0
def test_pull(agent):
    client = docker_client()
    try:
        client.remove_image('tianon/true:latestrandom', force=True)
    except:
        pass

    def post(req, resp, valid_resp):
        inspect = client.inspect_image('tianon/true:latest')
        assert resp['data']['fields']['dockerImage']['Id'] == inspect['Id']
        resp['data']['fields']['dockerImage'] = {}
        del resp['links']
        del resp['actions']
        del valid_resp['resourceId']

    event_test(agent, 'docker/instance_pull', post_func=post, diff=True)

    inspect = client.inspect_image('tianon/true:latestrandom')
    assert inspect is not None

    def pre2(req):
        req['data']['instancePull']['complete'] = True

    def post2(req, resp, valid_resp):
        assert resp['data']['fields']['dockerImage']['Id'] == ''
        del resp['links']
        del resp['actions']
        del valid_resp['resourceId']

    event_test(agent,
               'docker/instance_pull',
               pre_func=pre2,
               post_func=post2,
               diff=False)

    with pytest.raises(APIError):
        client.inspect_image('tianon/true:latestrandom')