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)
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)
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)
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)
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)
def test_inspect_not_found(agent): delete_container('/inspect_test') def post(req, resp): assert "Id" not in resp['data']['instanceInspect'] assert "Name" not in resp['data']['instanceInspect'] event_test(agent, 'docker/instance_inspect', post_func=post, diff=False)
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']
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)
def test_network_mode_none(agent): delete_container('/c861f990-4472-4fa1-960f-65171b544c28') def pre(req): instance = req['data']['instanceHostMap']['instance'] instance['nics'][0]['network']['kind'] = 'dockerNone' instance['hostname'] = 'nameisset' def post(req, resp, valid_resp): instance_data = resp['data']['instanceHostMap']['instance']['+data'] docker_inspect = instance_data['dockerInspect'] assert docker_inspect['Config']['NetworkDisabled'] assert docker_inspect['HostConfig']['NetworkMode'] == 'none' assert docker_inspect['Config']['Hostname'] == 'nameisset' 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)
def test_network_mode_bridge(agent): delete_container('/c861f990-4472-4fa1-960f-65171b544c28') def pre(req): instance = req['data']['instanceHostMap']['instance'] instance['nics'][0]['network']['kind'] = 'dockerBridge' def post(req, resp, valid_resp): instance_data = resp['data']['instanceHostMap']['instance']['+data'] docker_inspect = instance_data['dockerInspect'] docker_data = instance_data['dockerContainer'] assert 'NetworkDisabled' not in docker_inspect['Config'] assert len(docker_data['Ports']) == 1 assert docker_data['Ports'][0]["PublicPort"] == 100 docker_container = instance_data['dockerContainer'] fields = instance_data['+fields'] trim(docker_container, fields, resp, valid_resp) event_test(agent, 'docker/instance_activate_bridge', pre_func=pre, post_func=post, diff=False)