2017-05-05 10:23:00 +02:00
|
|
|
"""CloudStack plugin for integration tests."""
|
2019-07-12 08:46:20 +02:00
|
|
|
from __future__ import (absolute_import, division, print_function)
|
|
|
|
__metaclass__ = type
|
2017-05-05 10:23:00 +02:00
|
|
|
|
2017-05-05 14:01:27 +02:00
|
|
|
import json
|
2017-05-05 10:23:00 +02:00
|
|
|
import os
|
2017-05-05 14:01:27 +02:00
|
|
|
import re
|
2017-05-05 10:23:00 +02:00
|
|
|
import time
|
|
|
|
|
|
|
|
from lib.cloud import (
|
|
|
|
CloudProvider,
|
|
|
|
CloudEnvironment,
|
2019-03-01 03:25:49 +01:00
|
|
|
CloudEnvironmentConfig,
|
2017-05-05 10:23:00 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
from lib.util import (
|
|
|
|
find_executable,
|
|
|
|
ApplicationError,
|
|
|
|
display,
|
|
|
|
SubprocessError,
|
|
|
|
is_shippable,
|
2018-09-21 20:38:22 +02:00
|
|
|
ConfigParser,
|
2017-05-05 10:23:00 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
from lib.http import (
|
|
|
|
HttpClient,
|
2017-07-07 01:56:20 +02:00
|
|
|
HttpError,
|
2017-05-05 10:23:00 +02:00
|
|
|
urlparse,
|
|
|
|
)
|
|
|
|
|
|
|
|
from lib.docker_util import (
|
|
|
|
docker_run,
|
|
|
|
docker_rm,
|
|
|
|
docker_inspect,
|
|
|
|
docker_pull,
|
2017-05-05 14:01:27 +02:00
|
|
|
docker_network_inspect,
|
2018-12-17 22:39:15 +01:00
|
|
|
docker_exec,
|
2017-05-05 14:01:27 +02:00
|
|
|
get_docker_container_id,
|
2017-05-05 10:23:00 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class CsCloudProvider(CloudProvider):
|
|
|
|
"""CloudStack cloud provider plugin. Sets up cloud resources before delegation."""
|
|
|
|
DOCKER_SIMULATOR_NAME = 'cloudstack-sim'
|
|
|
|
|
|
|
|
def __init__(self, args):
|
|
|
|
"""
|
|
|
|
:type args: TestConfig
|
|
|
|
"""
|
2019-03-01 03:25:49 +01:00
|
|
|
super(CsCloudProvider, self).__init__(args)
|
2017-05-05 10:23:00 +02:00
|
|
|
|
2017-10-02 21:08:56 +02:00
|
|
|
# The simulator must be pinned to a specific version to guarantee CI passes with the version used.
|
2018-08-28 16:49:34 +02:00
|
|
|
self.image = 'quay.io/ansible/cloudstack-test-container:1.2.0'
|
2017-05-05 10:23:00 +02:00
|
|
|
self.container_name = ''
|
|
|
|
self.endpoint = ''
|
|
|
|
self.host = ''
|
|
|
|
self.port = 0
|
|
|
|
|
|
|
|
def filter(self, targets, exclude):
|
|
|
|
"""Filter out the cloud tests when the necessary config and resources are not available.
|
|
|
|
:type targets: tuple[TestTarget]
|
|
|
|
:type exclude: list[str]
|
|
|
|
"""
|
|
|
|
if os.path.isfile(self.config_static_path):
|
|
|
|
return
|
|
|
|
|
2017-09-16 01:08:25 +02:00
|
|
|
docker = find_executable('docker', required=False)
|
2017-05-05 10:23:00 +02:00
|
|
|
|
|
|
|
if docker:
|
|
|
|
return
|
|
|
|
|
2017-09-16 01:08:25 +02:00
|
|
|
skip = 'cloud/%s/' % self.platform
|
|
|
|
skipped = [target.name for target in targets if skip in target.aliases]
|
|
|
|
|
|
|
|
if skipped:
|
|
|
|
exclude.append(skip)
|
|
|
|
display.warning('Excluding tests marked "%s" which require the "docker" command or config (see "%s"): %s'
|
|
|
|
% (skip.rstrip('/'), self.config_template_path, ', '.join(skipped)))
|
2017-05-05 10:23:00 +02:00
|
|
|
|
|
|
|
def setup(self):
|
|
|
|
"""Setup the cloud resource before delegation and register a cleanup callback."""
|
|
|
|
super(CsCloudProvider, self).setup()
|
|
|
|
|
|
|
|
if self._use_static_config():
|
|
|
|
self._setup_static()
|
|
|
|
else:
|
|
|
|
self._setup_dynamic()
|
|
|
|
|
|
|
|
def get_remote_ssh_options(self):
|
|
|
|
"""Get any additional options needed when delegating tests to a remote instance via SSH.
|
|
|
|
:rtype: list[str]
|
|
|
|
"""
|
|
|
|
if self.managed:
|
|
|
|
return ['-R', '8888:localhost:8888']
|
|
|
|
|
|
|
|
return []
|
|
|
|
|
|
|
|
def get_docker_run_options(self):
|
|
|
|
"""Get any additional options needed when delegating tests to a docker container.
|
|
|
|
:rtype: list[str]
|
|
|
|
"""
|
|
|
|
if self.managed:
|
|
|
|
return ['--link', self.DOCKER_SIMULATOR_NAME]
|
|
|
|
|
|
|
|
return []
|
|
|
|
|
|
|
|
def cleanup(self):
|
|
|
|
"""Clean up the cloud resource and any temporary configuration files after tests complete."""
|
|
|
|
if self.container_name:
|
|
|
|
if is_shippable():
|
|
|
|
docker_rm(self.args, self.container_name)
|
|
|
|
elif not self.args.explain:
|
|
|
|
display.notice('Remember to run `docker rm -f %s` when finished testing.' % self.container_name)
|
|
|
|
|
|
|
|
super(CsCloudProvider, self).cleanup()
|
|
|
|
|
|
|
|
def _setup_static(self):
|
|
|
|
"""Configure CloudStack tests for use with static configuration."""
|
2018-09-21 20:38:22 +02:00
|
|
|
parser = ConfigParser()
|
2017-05-05 10:23:00 +02:00
|
|
|
parser.read(self.config_static_path)
|
|
|
|
|
|
|
|
self.endpoint = parser.get('cloudstack', 'endpoint')
|
|
|
|
|
|
|
|
parts = urlparse(self.endpoint)
|
|
|
|
|
|
|
|
self.host = parts.hostname
|
|
|
|
|
|
|
|
if not self.host:
|
|
|
|
raise ApplicationError('Could not determine host from endpoint: %s' % self.endpoint)
|
|
|
|
|
|
|
|
if parts.port:
|
|
|
|
self.port = parts.port
|
|
|
|
elif parts.scheme == 'http':
|
|
|
|
self.port = 80
|
|
|
|
elif parts.scheme == 'https':
|
|
|
|
self.port = 443
|
|
|
|
else:
|
|
|
|
raise ApplicationError('Could not determine port from endpoint: %s' % self.endpoint)
|
|
|
|
|
|
|
|
display.info('Read cs host "%s" and port %d from config: %s' % (self.host, self.port, self.config_static_path), verbosity=1)
|
|
|
|
|
|
|
|
self._wait_for_service()
|
|
|
|
|
|
|
|
def _setup_dynamic(self):
|
|
|
|
"""Create a CloudStack simulator using docker."""
|
|
|
|
config = self._read_config_template()
|
|
|
|
|
|
|
|
self.container_name = self.DOCKER_SIMULATOR_NAME
|
|
|
|
|
|
|
|
results = docker_inspect(self.args, self.container_name)
|
|
|
|
|
|
|
|
if results and not results[0]['State']['Running']:
|
|
|
|
docker_rm(self.args, self.container_name)
|
|
|
|
results = []
|
|
|
|
|
|
|
|
if results:
|
|
|
|
display.info('Using the existing CloudStack simulator docker container.', verbosity=1)
|
|
|
|
else:
|
|
|
|
display.info('Starting a new CloudStack simulator docker container.', verbosity=1)
|
|
|
|
docker_pull(self.args, self.image)
|
|
|
|
docker_run(self.args, self.image, ['-d', '-p', '8888:8888', '--name', self.container_name])
|
2018-12-17 22:39:15 +01:00
|
|
|
|
|
|
|
# apply work-around for OverlayFS issue
|
|
|
|
# https://github.com/docker/for-linux/issues/72#issuecomment-319904698
|
|
|
|
docker_exec(self.args, self.container_name, ['find', '/var/lib/mysql', '-type', 'f', '-exec', 'touch', '{}', ';'])
|
|
|
|
|
2017-05-09 10:15:27 +02:00
|
|
|
if not self.args.explain:
|
2018-08-28 16:49:34 +02:00
|
|
|
display.notice('The CloudStack simulator will probably be ready in 2 - 4 minutes.')
|
2017-05-05 10:23:00 +02:00
|
|
|
|
2017-05-05 14:01:27 +02:00
|
|
|
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'
|
|
|
|
|
2017-05-05 10:23:00 +02:00
|
|
|
self.port = 8888
|
|
|
|
self.endpoint = 'http://%s:%d' % (self.host, self.port)
|
|
|
|
|
|
|
|
self._wait_for_service()
|
|
|
|
|
|
|
|
if self.args.explain:
|
|
|
|
values = dict(
|
|
|
|
HOST=self.host,
|
|
|
|
PORT=str(self.port),
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
credentials = self._get_credentials()
|
|
|
|
|
|
|
|
if self.args.docker:
|
|
|
|
host = self.DOCKER_SIMULATOR_NAME
|
|
|
|
else:
|
|
|
|
host = self.host
|
|
|
|
|
|
|
|
values = dict(
|
|
|
|
HOST=host,
|
|
|
|
PORT=str(self.port),
|
|
|
|
KEY=credentials['apikey'],
|
|
|
|
SECRET=credentials['secretkey'],
|
|
|
|
)
|
|
|
|
|
|
|
|
config = self._populate_config_template(config, values)
|
|
|
|
|
|
|
|
self._write_config(config)
|
|
|
|
|
2017-05-05 14:01:27 +02:00
|
|
|
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'])
|
2018-09-21 20:38:22 +02:00
|
|
|
except Exception:
|
2017-05-05 14:01:27 +02:00
|
|
|
display.error('Failed to process the following docker network inspect output:\n%s' %
|
|
|
|
json.dumps(networks, indent=4, sort_keys=True))
|
|
|
|
raise
|
|
|
|
|
2017-05-05 10:23:00 +02:00
|
|
|
def _wait_for_service(self):
|
|
|
|
"""Wait for the CloudStack service endpoint to accept connections."""
|
|
|
|
if self.args.explain:
|
|
|
|
return
|
|
|
|
|
|
|
|
client = HttpClient(self.args, always=True)
|
|
|
|
endpoint = self.endpoint
|
|
|
|
|
2019-07-15 22:47:16 +02:00
|
|
|
for _iteration in range(1, 30):
|
2017-05-05 10:23:00 +02:00
|
|
|
display.info('Waiting for CloudStack service: %s' % endpoint, verbosity=1)
|
|
|
|
|
|
|
|
try:
|
|
|
|
client.get(endpoint)
|
|
|
|
return
|
|
|
|
except SubprocessError:
|
|
|
|
pass
|
|
|
|
|
2018-08-28 16:49:34 +02:00
|
|
|
time.sleep(10)
|
2017-05-05 10:23:00 +02:00
|
|
|
|
|
|
|
raise ApplicationError('Timeout waiting for CloudStack service.')
|
|
|
|
|
|
|
|
def _get_credentials(self):
|
|
|
|
"""Wait for the CloudStack simulator to return credentials.
|
|
|
|
:rtype: dict[str, str]
|
|
|
|
"""
|
|
|
|
client = HttpClient(self.args, always=True)
|
|
|
|
endpoint = '%s/admin.json' % self.endpoint
|
|
|
|
|
2019-07-15 22:47:16 +02:00
|
|
|
for _iteration in range(1, 30):
|
2017-05-05 10:23:00 +02:00
|
|
|
display.info('Waiting for CloudStack credentials: %s' % endpoint, verbosity=1)
|
|
|
|
|
|
|
|
response = client.get(endpoint)
|
|
|
|
|
|
|
|
if response.status_code == 200:
|
2017-07-07 01:56:20 +02:00
|
|
|
try:
|
|
|
|
return response.json()
|
|
|
|
except HttpError as ex:
|
|
|
|
display.error(ex)
|
2017-05-05 10:23:00 +02:00
|
|
|
|
2018-08-28 16:49:34 +02:00
|
|
|
time.sleep(10)
|
2017-05-05 10:23:00 +02:00
|
|
|
|
|
|
|
raise ApplicationError('Timeout waiting for CloudStack credentials.')
|
|
|
|
|
|
|
|
|
|
|
|
class CsCloudEnvironment(CloudEnvironment):
|
|
|
|
"""CloudStack cloud environment plugin. Updates integration test environment after delegation."""
|
2019-03-01 03:25:49 +01:00
|
|
|
def get_environment_config(self):
|
2017-05-05 10:23:00 +02:00
|
|
|
"""
|
2019-03-01 03:25:49 +01:00
|
|
|
:rtype: CloudEnvironmentConfig
|
2017-05-05 10:23:00 +02:00
|
|
|
"""
|
2019-03-01 03:25:49 +01:00
|
|
|
parser = ConfigParser()
|
|
|
|
parser.read(self.config_path)
|
|
|
|
|
|
|
|
config = dict(parser.items('default'))
|
|
|
|
|
|
|
|
env_vars = dict(
|
|
|
|
CLOUDSTACK_ENDPOINT=config['endpoint'],
|
|
|
|
CLOUDSTACK_KEY=config['key'],
|
|
|
|
CLOUDSTACK_SECRET=config['secret'],
|
|
|
|
CLOUDSTACK_TIMEOUT=config['timeout'],
|
2017-05-05 10:23:00 +02:00
|
|
|
)
|
|
|
|
|
2019-03-01 03:25:49 +01:00
|
|
|
ansible_vars = dict(
|
|
|
|
cs_resource_prefix=self.resource_prefix,
|
|
|
|
)
|
2017-05-05 10:23:00 +02:00
|
|
|
|
2019-03-01 03:25:49 +01:00
|
|
|
return CloudEnvironmentConfig(
|
|
|
|
env_vars=env_vars,
|
|
|
|
ansible_vars=ansible_vars,
|
|
|
|
)
|