1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Fix ansible-test cloud integration bugs.

This commit is contained in:
Matt Clay 2017-05-05 20:01:27 +08:00
parent 17e07a27b2
commit 0573b8bcd2
3 changed files with 81 additions and 11 deletions

View file

@ -1,7 +1,9 @@
"""CloudStack plugin for integration tests.""" """CloudStack plugin for integration tests."""
from __future__ import absolute_import, print_function from __future__ import absolute_import, print_function
import json
import os import os
import re
import time import time
from lib.cloud import ( from lib.cloud import (
@ -27,6 +29,8 @@ from lib.docker_util import (
docker_rm, docker_rm,
docker_inspect, docker_inspect,
docker_pull, docker_pull,
docker_network_inspect,
get_docker_container_id,
) )
try: try:
@ -152,7 +156,15 @@ class CsCloudProvider(CloudProvider):
docker_run(self.args, self.image, ['-d', '-p', '8888:8888', '--name', self.container_name]) 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.') 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.port = 8888
self.endpoint = 'http://%s:%d' % (self.host, self.port) self.endpoint = 'http://%s:%d' % (self.host, self.port)
@ -182,6 +194,19 @@ class CsCloudProvider(CloudProvider):
self._write_config(config) 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): def _wait_for_service(self):
"""Wait for the CloudStack service endpoint to accept connections.""" """Wait for the CloudStack service endpoint to accept connections."""
if self.args.explain: if self.args.explain:
@ -190,7 +215,7 @@ class CsCloudProvider(CloudProvider):
client = HttpClient(self.args, always=True) client = HttpClient(self.args, always=True)
endpoint = self.endpoint endpoint = self.endpoint
for _ in range(1, 90): for _ in range(1, 30):
display.info('Waiting for CloudStack service: %s' % endpoint, verbosity=1) display.info('Waiting for CloudStack service: %s' % endpoint, verbosity=1)
try: try:
@ -199,7 +224,7 @@ class CsCloudProvider(CloudProvider):
except SubprocessError: except SubprocessError:
pass pass
time.sleep(10) time.sleep(30)
raise ApplicationError('Timeout waiting for CloudStack service.') raise ApplicationError('Timeout waiting for CloudStack service.')
@ -210,7 +235,7 @@ class CsCloudProvider(CloudProvider):
client = HttpClient(self.args, always=True) client = HttpClient(self.args, always=True)
endpoint = '%s/admin.json' % self.endpoint 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) display.info('Waiting for CloudStack credentials: %s' % endpoint, verbosity=1)
response = client.get(endpoint) response = client.get(endpoint)
@ -218,7 +243,7 @@ class CsCloudProvider(CloudProvider):
if response.status_code == 200: if response.status_code == 200:
return response.json() return response.json()
time.sleep(10) time.sleep(30)
raise ApplicationError('Timeout waiting for CloudStack credentials.') raise ApplicationError('Timeout waiting for CloudStack credentials.')

View file

@ -195,10 +195,11 @@ def delegate_docker(args, exclude, require):
'--env', 'HTTPTESTER=1', '--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: for cloud_platform in cloud_platforms:
test_options += cloud_platform.get_docker_run_options() test_options += cloud_platform.get_docker_run_options()
test_id, _ = docker_run(args, test_image, options=test_options) test_id, _ = docker_run(args, test_image, options=test_options)
@ -268,10 +269,11 @@ def delegate_remote(args, exclude, require):
ssh_options = [] ssh_options = []
cloud_platforms = get_cloud_providers(args) if isinstance(args, TestConfig):
cloud_platforms = get_cloud_providers(args)
for cloud_platform in cloud_platforms: for cloud_platform in cloud_platforms:
ssh_options += cloud_platform.get_remote_ssh_options() ssh_options += cloud_platform.get_remote_ssh_options()
try: try:
manage.ssh(cmd, ssh_options) manage.ssh(cmd, ssh_options)

View file

@ -21,6 +21,30 @@ from lib.util import (
BUFFER_SIZE = 256 * 256 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): def docker_pull(args, image):
""" """
:type args: EnvironmentConfig :type args: EnvironmentConfig
@ -115,6 +139,25 @@ def docker_inspect(args, container_id):
raise ex # pylint: disable=locally-disabled, raising-bad-type 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): def docker_exec(args, container_id, cmd, options=None, capture=False, stdin=None, stdout=None):
""" """
:type args: EnvironmentConfig :type args: EnvironmentConfig