def test_instance_activate_need_pull_image(agent, responses): try: docker_client().remove_image('ibuildthecloud/helloworld:latest') except APIError: pass test_instance_only_activate(agent, responses)
def test_image_activate(agent, responses): try: docker_client().remove_image('ibuildthecloud/helloworld:latest') except APIError: pass def post(req, resp): del resp['data']['+data']['dockerImage']['VirtualSize'] event_test(agent, 'docker/image_activate', post_func=post)
def host_data(mocker): mocker.patch.object(platform, 'system', return_value='Linux') mocker.patch('os.getloadavg', return_value=(1.60693359375, 1.73193359375, 1.79248046875)) mocker.patch.object(CpuCollector, '_get_cpuinfo_data', return_value=cpuinfo_data()) mocker.patch.object(MemoryCollector, '_get_meminfo_data', return_value=meminfo_data()) mocker.patch.object(CadvisorAPIClient, 'get_containers', return_value=cadvisor_stats_data()) mocker.patch.object(Client, 'version', return_value=docker_client_version_data()) host = HostInfo(docker_client()) data = host.collect_data() assert isinstance(data, dict) os.getloadavg.assert_called_once_with() CpuCollector._get_cpuinfo_data.assert_called_once_with() MemoryCollector._get_meminfo_data.assert_called_once_with() CadvisorAPIClient.get_containers.assert_called_with() Client.version.assert_called_once_with() return data
def test_instance_activate_links_no_service(agent, responses): _delete_container('/c861f990-4472-4fa1-960f-65171b544c28') _delete_container('/target_redis') _delete_container('/target_mysql') client = docker_client() c = client.create_container('ibuildthecloud/helloworld', ports=['3307/udp', '3306/tcp'], name='target_mysql') client.start(c, port_bindings={ '3307/udp': ('127.0.0.2', 12346), '3306/tcp': ('127.0.0.2', 12345) }) c = client.create_container('ibuildthecloud/helloworld', name='target_redis') client.start(c) def post(req, resp): id = resp['data']['instanceHostMap']['instance'] id = id['+data']['dockerContainer']['Id'] inspect = docker_client().inspect_container(id) instance_activate_common_validation(resp) assert set( ['/target_mysql:/c861f990-4472-4fa1-960f-65171b544c28/mysql', '/target_redis:/c861f990-4472-4fa1-960f-65171b544c28/' 'redis']) == set(inspect['HostConfig']['Links']) event_test(agent, 'docker/instance_activate_links_no_service', post_func=post)
def get_container(name): client = docker_client() for c in client.containers(all=True): for container_name in c['Names']: if name == container_name: return c return None
def post(req, resp): del resp['data']['instance']['+data']['dockerInspect'] docker_container = resp['data']['instance']['+data']['dockerContainer'] fields = resp['data']['instance']['+data']['+fields'] id = docker_container['Id'] del docker_container['Created'] del docker_container['Id'] del docker_container['Status'] docker_container = _sort_ports(docker_container) del docker_container['Ports'][0]['PublicPort'] del docker_container['Ports'][1]['PublicPort'] del fields['dockerIp'] assert fields['dockerPorts']['8080/tcp'] is not None assert fields['dockerPorts']['12201/udp'] is not None fields['dockerPorts']['8080/tcp'] = '1234' fields['dockerPorts']['12201/udp'] = '5678' inspect = docker_client().inspect_container(id) port = Config.api_proxy_listen_port() assert 'CATTLE_CONFIG_URL={0}'.format(Config.config_url()) in \ inspect['Config']['Env'] assert 'CATTLE_CONFIG_URL_SCHEME=https' not in inspect['Config']['Env'] assert 'CATTLE_CONFIG_URL_PATH=/a/path' not in inspect['Config']['Env'] assert 'CATTLE_CONFIG_URL_PORT={0}'.format(port) not in \ inspect['Config']['Env'] assert 'ENV1=value1' in inspect['Config']['Env']
def full_mock_hostinfo_obj(mocker): mocker.patch.object(platform, 'system', return_value='Linux') mocker.patch.object(platform, 'release', return_value='3.19.0-28-generic') mocker.patch('os.getloadavg', return_value=(1.60693359375, 1.73193359375, 1.79248046875)) mocker.patch.object(CpuCollector, '_get_cpuinfo_data', return_value=cpuinfo_data()) mocker.patch.object(MemoryCollector, '_get_meminfo_data', return_value=meminfo_data()) mocker.patch.object(CadvisorAPIClient, 'get_containers', return_value=cadvisor_stats_data()) mocker.patch.object(CadvisorAPIClient, 'get_machine_stats', return_value=cadvisor_machine_stats_data()) mocker.patch.object(Client, 'version', return_value=docker_client_version_data()) mocker.patch.object(Client, 'info', return_value=docker_devicemapper_override()) return HostInfo(docker_client())
def _get_container(name): client = docker_client() for c in client.containers(all=True): for container_name in c['Names']: if name == container_name: return c return None
def post(req, resp): id = resp['data']['instanceHostMap']['instance'] id = id['+data']['dockerContainer']['Id'] inspect = docker_client().inspect_container(id) instance_activate_common_validation(resp) env = inspect['Config']['Env'] assert 'MYSQL_NAME=/cattle/mysql' in env assert 'MYSQL_PORT=udp://127.0.0.2:12346' in env assert 'MYSQL_PORT_3307_UDP=udp://127.0.0.2:12346' in env assert 'MYSQL_PORT_3307_UDP_ADDR=127.0.0.2' in env assert 'MYSQL_PORT_3307_UDP_PORT=12346' in env assert 'MYSQL_PORT_3307_UDP_PROTO=udp' in env assert 'MYSQL_PORT_3306_TCP=tcp://127.0.0.1:12345' in env assert 'MYSQL_PORT_3306_TCP_ADDR=127.0.0.1' in env assert 'MYSQL_PORT_3306_TCP_PORT=12345' in env assert 'MYSQL_PORT_3306_TCP_PROTO=tcp' in env assert 'REDIS_NAME=/cattle/redis' in env assert 'REDIS_PORT=udp://127.0.0.1:23456' in env assert 'REDIS_PORT_26_UDP=udp://127.0.0.1:23456' in env assert 'REDIS_PORT_26_UDP_ADDR=127.0.0.1' in env assert 'REDIS_PORT_26_UDP_PORT=23456' in env assert 'REDIS_PORT_26_UDP_PROTO=udp' in env
def post(req, resp): id = resp['data']['instanceHostMap']['instance'] id = id['+data']['dockerContainer']['Id'] inspect = docker_client().inspect_container(id) instance_activate_common_validation(resp) assert set( ['/target_mysql:/c861f990-4472-4fa1-960f-65171b544c28/mysql', '/target_redis:/c861f990-4472-4fa1-960f-65171b544c28/' 'redis']) == set(inspect['HostConfig']['Links'])
def _delete_container(name): client = docker_client() for c in client.containers(all=True): for container_name in c['Names']: if name == container_name: try: client.kill(c) except: pass client.remove_container(c)
def test_image_list(): c = docker_client() images = c.images(all=True) if len(images) == 0: c.pull('busybox') images = c.images(all=True) assert 'Id' in images[0] assert 'ID' not in images[0]
def post(req, resp): id = resp['data']['instanceHostMap']['instance'] id = id['+data']['dockerContainer']['Id'] inspect = docker_client().inspect_container(id) instance_activate_common_validation(resp) port = Config.api_proxy_listen_port() assert 'CATTLE_CONFIG_URL_SCHEME=https' in inspect['Config']['Env'] assert 'CATTLE_CONFIG_URL_PATH=/a/path' in inspect['Config']['Env'] assert 'CATTLE_CONFIG_URL_PORT={0}'.format(port) in \ inspect['Config']['Env']
def test_collect_data_osinfo(host_data): expected_osinfo_keys = [ 'operatingSystem', 'dockerVersion', 'kernelVersion' ] assert sorted(host_data['osInfo'].keys()) == \ sorted(expected_osinfo_keys) assert host_data['osInfo']['dockerVersion'] == \ 'Docker version 1.6.0, build 4749651' operating_system = docker_client().info().get("OperatingSystem") assert host_data['osInfo']['operatingSystem'] == operating_system
def test_collect_data_osinfo(host_data): expected_osinfo_keys = ['operatingSystem', 'dockerVersion', 'kernelVersion'] assert sorted(host_data['osInfo'].keys()) == \ sorted(expected_osinfo_keys) assert host_data['osInfo']['dockerVersion'] == \ 'Docker version 1.6.0, build 4749651' operating_system = docker_client().info().get("OperatingSystem") assert host_data['osInfo']['operatingSystem'] == operating_system
def delete_container(name): client = docker_client() for c in client.containers(all=True): for container_name in c['Names']: if name == container_name: try: client.kill(c) except: pass for i in range(10): if client.inspect_container(c['Id'])['State']['Pid'] == 0: break time.sleep(0.5) client.remove_container(c) remove_state_file(c)
def test_volume_purge(agent, responses): _delete_container('/c-c861f990-4472-4fa1-960f-65171b544c28') _delete_container('/target_volumes_from') client = docker_client() c = client.create_container('ibuildthecloud/helloworld', volumes=['/volumes_from_path'], name='target_volumes_from') client.start(c) # TODO Figure out a better way to test this. Because purging a volume # means removing it from disk, we run into trouble testing when # boot2docker is in the picture because the locally running agent cannot # see inside the b2d vm. We do currently test this functionality fully # in the integration test suite. event_test(agent, 'docker/volume_purge')
def test_instance_activate_volumes(agent, responses): _delete_container('/c-c861f990-4472-4fa1-960f-65171b544c28') _delete_container('/target_volumes_from') client = docker_client() c = client.create_container('ibuildthecloud/helloworld', volumes=['/volumes_from_path'], name='target_volumes_from') client.start(c) def post(req, resp): inspect = resp['data']['instance']['+data']['dockerInspect'] assert inspect['Volumes']['/host/proc'] == '/proc' assert inspect['Volumes']['/host/sys'] == '/sys' assert inspect['Volumes']['/random'] is not None assert inspect['Volumes']['/volumes_from_path'] is not None assert len(inspect['Volumes']) == 4 assert inspect['VolumesRW'] == { '/host/proc': True, '/host/sys': False, '/random': True, '/volumes_from_path': True, } assert set(['/sys:/host/sys:ro', '/proc:/host/proc:rw']) == set( inspect['HostConfig']['Binds']) del resp['data']['instance']['+data']['dockerInspect'] docker_container = resp['data']['instance']['+data']['dockerContainer'] fields = resp['data']['instance']['+data']['+fields'] del docker_container['Created'] del docker_container['Id'] del docker_container['Status'] docker_container = _sort_ports(docker_container) del docker_container['Ports'][0]['PublicPort'] del docker_container['Ports'][1]['PublicPort'] del fields['dockerIp'] assert fields['dockerPorts']['8080/tcp'] is not None assert fields['dockerPorts']['12201/udp'] is not None fields['dockerPorts']['8080/tcp'] = '1234' fields['dockerPorts']['12201/udp'] = '5678' event_test(agent, 'docker/instance_activate_volumes', post_func=post)
def test_instance_activate_links_no_service(agent, responses): _delete_container('/c861f990-4472-4fa1-960f-65171b544c28') _delete_container('/target_redis') _delete_container('/target_mysql') client = docker_client() c = client.create_container('ibuildthecloud/helloworld', ports=['3307/udp', '3306/tcp'], name='target_mysql') client.start(c, port_bindings={ '3307/udp': ('127.0.0.2', 12346), '3306/tcp': ('127.0.0.2', 12345) }) c = client.create_container('ibuildthecloud/helloworld', name='target_redis') client.start(c) def post(req, resp): del resp['data']['instance']['+data']['dockerInspect'] docker_container = resp['data']['instance']['+data']['dockerContainer'] id = docker_container['Id'] fields = resp['data']['instance']['+data']['+fields'] del docker_container['Created'] del docker_container['Id'] del docker_container['Status'] docker_container = _sort_ports(docker_container) del docker_container['Ports'][0]['PublicPort'] del docker_container['Ports'][1]['PublicPort'] del fields['dockerIp'] assert fields['dockerPorts']['8080/tcp'] is not None assert fields['dockerPorts']['12201/udp'] is not None fields['dockerPorts']['8080/tcp'] = '1234' fields['dockerPorts']['12201/udp'] = '5678' inspect = docker_client().inspect_container(id) assert set( ['/target_mysql:/c861f990-4472-4fa1-960f-65171b544c28/mysql', '/target_redis:/c861f990-4472-4fa1-960f-65171b544c28/' 'redis']) == set(inspect['HostConfig']['Links']) event_test(agent, 'docker/instance_activate_links_no_service', post_func=post)
def docker_devicemapper_override(): data = docker_client().info() data['Driver'] = "devicemapper" data['DriverStatus'] = \ [['Pool Name', 'docker-8:1-130861-pool'], ['Pool Blocksize', '65.54 kB'], ['Backing Filesystem', 'extfs'], ['Data file', '/dev/loop0'], ['Metadata file', '/dev/loop1'], ['Data Space Used', '2.661 GB'], ['Data Space Total', '107.4 GB'], ['Data Space Available', '16.8 GB'], ['Metadata Space Used', '2.683 MB'], ['Metadata Space Total', '2.147 GB'], ['Metadata Space Available', '2.145 GB'], ['Udev Sync Supported', 'false'], ['Deferred Removal Enabled', 'false'], ['Data loop file', '/mnt/sda1/var/lib/docker/devicemapper/devicemapper/data'], ['Metadata loop file', '/mnt/sda1/var/lib/docker/devicemapper/devicemapper/metadata'], ['Library Version', '1.02.82-git (2013-10-04)']] return data
def post(req, resp): del resp['data']['instance']['+data']['dockerInspect'] docker_container = resp['data']['instance']['+data']['dockerContainer'] id = docker_container['Id'] fields = resp['data']['instance']['+data']['+fields'] del docker_container['Created'] del docker_container['Id'] del docker_container['Status'] docker_container = _sort_ports(docker_container) del docker_container['Ports'][0]['PublicPort'] del docker_container['Ports'][1]['PublicPort'] del fields['dockerIp'] assert fields['dockerPorts']['8080/tcp'] is not None assert fields['dockerPorts']['12201/udp'] is not None fields['dockerPorts']['8080/tcp'] = '1234' fields['dockerPorts']['12201/udp'] = '5678' inspect = docker_client().inspect_container(id) env = inspect['Config']['Env'] assert 'MYSQL_NAME=/cattle/mysql' in env assert 'MYSQL_PORT=udp://127.0.0.2:12346' in env assert 'MYSQL_PORT_3307_UDP=udp://127.0.0.2:12346' in env assert 'MYSQL_PORT_3307_UDP_ADDR=127.0.0.2' in env assert 'MYSQL_PORT_3307_UDP_PORT=12346' in env assert 'MYSQL_PORT_3307_UDP_PROTO=udp' in env assert 'MYSQL_PORT_3306_TCP=tcp://127.0.0.1:12345' in env assert 'MYSQL_PORT_3306_TCP_ADDR=127.0.0.1' in env assert 'MYSQL_PORT_3306_TCP_PORT=12345' in env assert 'MYSQL_PORT_3306_TCP_PROTO=tcp' in env assert 'REDIS_NAME=/cattle/redis' in env assert 'REDIS_PORT=udp://127.0.0.1:23456' in env assert 'REDIS_PORT_26_UDP=udp://127.0.0.1:23456' in env assert 'REDIS_PORT_26_UDP_ADDR=127.0.0.1' in env assert 'REDIS_PORT_26_UDP_PORT=23456' in env assert 'REDIS_PORT_26_UDP_PROTO=udp' in env
def post(req, resp): del resp['data']['instance']['+data']['dockerInspect'] docker_container = resp['data']['instance']['+data']['dockerContainer'] id = docker_container['Id'] fields = resp['data']['instance']['+data']['+fields'] del docker_container['Created'] del docker_container['Id'] del docker_container['Status'] docker_container = _sort_ports(docker_container) del docker_container['Ports'][0]['PublicPort'] del docker_container['Ports'][1]['PublicPort'] del fields['dockerIp'] assert fields['dockerPorts']['8080/tcp'] is not None assert fields['dockerPorts']['12201/udp'] is not None fields['dockerPorts']['8080/tcp'] = '1234' fields['dockerPorts']['12201/udp'] = '5678' inspect = docker_client().inspect_container(id) assert set( ['/target_mysql:/c861f990-4472-4fa1-960f-65171b544c28/mysql', '/target_redis:/c861f990-4472-4fa1-960f-65171b544c28/' 'redis']) == set(inspect['HostConfig']['Links'])
def delete_container(name): client = docker_client() for c in client.containers(all=True): found = False labels = c.get('Labels', {}) if labels.get('io.rancher.container.uuid', None) == name[1:]: found = True for container_name in c['Names']: if name == container_name: found = True break if found: try: client.kill(c) except: pass for i in range(10): if client.inspect_container(c['Id'])['State']['Pid'] == 0: break time.sleep(0.5) client.remove_container(c) remove_state_file(c)
def test_instance_activate_volumes(agent, responses): _delete_container('/c-c861f990-4472-4fa1-960f-65171b544c28') _delete_container('/target_volumes_from') client = docker_client() c = client.create_container('ibuildthecloud/helloworld', volumes=['/volumes_from_path'], name='target_volumes_from') client.start(c) def post(req, resp): instance_data = resp['data']['instanceHostMap']['instance']['+data'] inspect = instance_data['dockerInspect'] assert inspect['Volumes']['/host/proc'] == '/proc' assert inspect['Volumes']['/host/sys'] == '/sys' assert inspect['Volumes']['/random'] is not None assert inspect['Volumes']['/volumes_from_path'] is not None assert len(inspect['Volumes']) == 4 assert inspect['VolumesRW'] == { '/host/proc': True, '/host/sys': False, '/random': True, '/volumes_from_path': True, } assert set(['/sys:/host/sys:ro', '/proc:/host/proc:rw']) == set( inspect['HostConfig']['Binds']) instance_activate_common_validation(resp) event_test(agent, 'docker/instance_activate_volumes', post_func=post)
def cadvisor_docker_root(): from cattle.plugins.docker import docker_client return docker_client().info().get("DockerRootDir", None)
def delete_volume(name): client = docker_client(version=DockerConfig.storage_api_version()) try: client.remove_volume(name) except: pass
def newer_than(version): client = docker_client() ver = client.version()['ApiVersion'] return compare_version(version, ver) >= 0