diff --git a/test/runner/lib/cloud/cs.py b/test/runner/lib/cloud/cs.py index b751548606b..99edca18b00 100644 --- a/test/runner/lib/cloud/cs.py +++ b/test/runner/lib/cloud/cs.py @@ -1,7 +1,9 @@ """CloudStack plugin for integration tests.""" from __future__ import absolute_import, print_function +import json import os +import re import time from lib.cloud import ( @@ -27,6 +29,8 @@ from lib.docker_util import ( docker_rm, docker_inspect, docker_pull, + docker_network_inspect, + get_docker_container_id, ) try: @@ -152,7 +156,15 @@ class CsCloudProvider(CloudProvider): docker_run(self.args, self.image, ['-d', '-p', '8888:8888', '--name', self.container_name]) display.notice('The CloudStack simulator will probably be ready in 5 - 10 minutes.') - self.host = 'localhost' + container_id = get_docker_container_id() + + if container_id: + display.info('Running in docker container: %s' % container_id, verbosity=1) + self.host = self._get_simulator_address() + display.info('Found CloudStack simulator container address: %s' % self.host, verbosity=1) + else: + self.host = 'localhost' + self.port = 8888 self.endpoint = 'http://%s:%d' % (self.host, self.port) @@ -182,6 +194,19 @@ class CsCloudProvider(CloudProvider): self._write_config(config) + def _get_simulator_address(self): + networks = docker_network_inspect(self.args, 'bridge') + + try: + bridge = [network for network in networks if network['Name'] == 'bridge'][0] + containers = bridge['Containers'] + container = [containers[container] for container in containers if containers[container]['Name'] == self.DOCKER_SIMULATOR_NAME][0] + return re.sub(r'/[0-9]+$', '', container['IPv4Address']) + except: + display.error('Failed to process the following docker network inspect output:\n%s' % + json.dumps(networks, indent=4, sort_keys=True)) + raise + def _wait_for_service(self): """Wait for the CloudStack service endpoint to accept connections.""" if self.args.explain: @@ -190,7 +215,7 @@ class CsCloudProvider(CloudProvider): client = HttpClient(self.args, always=True) endpoint = self.endpoint - for _ in range(1, 90): + for _ in range(1, 30): display.info('Waiting for CloudStack service: %s' % endpoint, verbosity=1) try: @@ -199,7 +224,7 @@ class CsCloudProvider(CloudProvider): except SubprocessError: pass - time.sleep(10) + time.sleep(30) raise ApplicationError('Timeout waiting for CloudStack service.') @@ -210,7 +235,7 @@ class CsCloudProvider(CloudProvider): client = HttpClient(self.args, always=True) endpoint = '%s/admin.json' % self.endpoint - for _ in range(1, 90): + for _ in range(1, 30): display.info('Waiting for CloudStack credentials: %s' % endpoint, verbosity=1) response = client.get(endpoint) @@ -218,7 +243,7 @@ class CsCloudProvider(CloudProvider): if response.status_code == 200: return response.json() - time.sleep(10) + time.sleep(30) raise ApplicationError('Timeout waiting for CloudStack credentials.') diff --git a/test/runner/lib/delegation.py b/test/runner/lib/delegation.py index e90e20f970a..d991fd6c035 100644 --- a/test/runner/lib/delegation.py +++ b/test/runner/lib/delegation.py @@ -195,10 +195,11 @@ def delegate_docker(args, exclude, require): '--env', 'HTTPTESTER=1', ] - cloud_platforms = get_cloud_providers(args) + if isinstance(args, TestConfig): + cloud_platforms = get_cloud_providers(args) - for cloud_platform in cloud_platforms: - test_options += cloud_platform.get_docker_run_options() + for cloud_platform in cloud_platforms: + test_options += cloud_platform.get_docker_run_options() test_id, _ = docker_run(args, test_image, options=test_options) @@ -268,10 +269,11 @@ def delegate_remote(args, exclude, require): ssh_options = [] - cloud_platforms = get_cloud_providers(args) + if isinstance(args, TestConfig): + cloud_platforms = get_cloud_providers(args) - for cloud_platform in cloud_platforms: - ssh_options += cloud_platform.get_remote_ssh_options() + for cloud_platform in cloud_platforms: + ssh_options += cloud_platform.get_remote_ssh_options() try: manage.ssh(cmd, ssh_options) diff --git a/test/runner/lib/docker_util.py b/test/runner/lib/docker_util.py index d93507d2d05..4bd2609b3db 100644 --- a/test/runner/lib/docker_util.py +++ b/test/runner/lib/docker_util.py @@ -21,6 +21,30 @@ from lib.util import ( BUFFER_SIZE = 256 * 256 +def get_docker_container_id(): + """ + :rtype: str | None + """ + path = '/proc/self/cgroup' + + if not os.path.exists(path): + return None + + with open(path) as cgroup_fd: + contents = cgroup_fd.read() + + paths = [line.split(':')[2] for line in contents.splitlines()] + container_ids = set(path.split('/')[2] for path in paths if path.startswith('/docker/')) + + if not container_ids: + return None + + if len(container_ids) == 1: + return container_ids.pop() + + raise ApplicationError('Found multiple container_id candidates: %s\n%s' % (sorted(container_ids), contents)) + + def docker_pull(args, image): """ :type args: EnvironmentConfig @@ -115,6 +139,25 @@ def docker_inspect(args, container_id): raise ex # pylint: disable=locally-disabled, raising-bad-type +def docker_network_inspect(args, network): + """ + :type args: EnvironmentConfig + :type network: str + :rtype: list[dict] + """ + if args.explain: + return [] + + try: + stdout, _ = docker_command(args, ['network', 'inspect', network], capture=True) + return json.loads(stdout) + except SubprocessError as ex: + try: + return json.loads(ex.stdout) + except: + raise ex # pylint: disable=locally-disabled, raising-bad-type + + def docker_exec(args, container_id, cmd, options=None, capture=False, stdin=None, stdout=None): """ :type args: EnvironmentConfig