2017-06-22 19:49:58 +02:00
|
|
|
"""VMware vCenter plugin for integration tests."""
|
|
|
|
from __future__ import absolute_import, print_function
|
|
|
|
|
|
|
|
import os
|
|
|
|
|
|
|
|
from lib.cloud import (
|
|
|
|
CloudProvider,
|
|
|
|
CloudEnvironment,
|
2019-03-01 03:25:49 +01:00
|
|
|
CloudEnvironmentConfig,
|
2017-06-22 19:49:58 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
from lib.util import (
|
|
|
|
find_executable,
|
|
|
|
display,
|
|
|
|
)
|
|
|
|
|
|
|
|
from lib.docker_util import (
|
|
|
|
docker_run,
|
|
|
|
docker_rm,
|
|
|
|
docker_inspect,
|
|
|
|
docker_pull,
|
|
|
|
get_docker_container_id,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class VcenterProvider(CloudProvider):
|
|
|
|
"""VMware vcenter/esx plugin. Sets up cloud resources for tests."""
|
|
|
|
DOCKER_SIMULATOR_NAME = 'vcenter-simulator'
|
|
|
|
|
|
|
|
def __init__(self, args):
|
|
|
|
"""
|
|
|
|
:type args: TestConfig
|
|
|
|
"""
|
2019-03-01 03:25:49 +01:00
|
|
|
super(VcenterProvider, self).__init__(args)
|
2017-06-22 19:49:58 +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.
|
2017-10-16 18:04:45 +02:00
|
|
|
if os.environ.get('ANSIBLE_VCSIM_CONTAINER'):
|
|
|
|
self.image = os.environ.get('ANSIBLE_VCSIM_CONTAINER')
|
|
|
|
else:
|
2018-11-05 20:08:04 +01:00
|
|
|
self.image = 'quay.io/ansible/vcenter-test-container:1.4.0'
|
2017-06-22 19:49:58 +02:00
|
|
|
self.container_name = ''
|
|
|
|
|
|
|
|
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]
|
|
|
|
"""
|
2017-09-16 01:08:25 +02:00
|
|
|
docker = find_executable('docker', required=False)
|
2017-06-22 19:49:58 +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: %s'
|
|
|
|
% (skip.rstrip('/'), ', '.join(skipped)))
|
2017-06-22 19:49:58 +02:00
|
|
|
|
|
|
|
def setup(self):
|
|
|
|
"""Setup the cloud resource before delegation and register a cleanup callback."""
|
|
|
|
super(VcenterProvider, self).setup()
|
|
|
|
|
|
|
|
if self._use_static_config():
|
|
|
|
self._setup_static()
|
|
|
|
else:
|
|
|
|
self._setup_dynamic()
|
|
|
|
|
|
|
|
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:
|
|
|
|
docker_rm(self.args, self.container_name)
|
|
|
|
|
|
|
|
super(VcenterProvider, self).cleanup()
|
|
|
|
|
|
|
|
def _setup_dynamic(self):
|
|
|
|
"""Create a vcenter simulator using docker."""
|
|
|
|
container_id = get_docker_container_id()
|
|
|
|
|
|
|
|
if container_id:
|
|
|
|
display.info('Running in docker container: %s' % container_id, verbosity=1)
|
|
|
|
|
|
|
|
self.container_name = self.DOCKER_SIMULATOR_NAME
|
|
|
|
|
|
|
|
results = docker_inspect(self.args, self.container_name)
|
|
|
|
|
|
|
|
if results and not results[0].get('State', {}).get('Running'):
|
|
|
|
docker_rm(self.args, self.container_name)
|
|
|
|
results = []
|
|
|
|
|
|
|
|
if results:
|
|
|
|
display.info('Using the existing vCenter simulator docker container.', verbosity=1)
|
|
|
|
else:
|
|
|
|
display.info('Starting a new vCenter simulator docker container.', verbosity=1)
|
|
|
|
|
|
|
|
if not self.args.docker and not container_id:
|
|
|
|
# publish the simulator ports when not running inside docker
|
|
|
|
publish_ports = [
|
|
|
|
'-p', '80:80',
|
|
|
|
'-p', '443:443',
|
|
|
|
'-p', '8080:8080',
|
|
|
|
'-p', '8989:8989',
|
|
|
|
'-p', '5000:5000', # control port for flask app in simulator
|
|
|
|
]
|
|
|
|
else:
|
|
|
|
publish_ports = []
|
|
|
|
|
2017-10-16 18:04:45 +02:00
|
|
|
if not os.environ.get('ANSIBLE_VCSIM_CONTAINER'):
|
|
|
|
docker_pull(self.args, self.image)
|
|
|
|
|
2017-06-22 19:49:58 +02:00
|
|
|
docker_run(
|
|
|
|
self.args,
|
|
|
|
self.image,
|
|
|
|
['-d', '--name', self.container_name] + publish_ports,
|
|
|
|
)
|
|
|
|
|
|
|
|
if self.args.docker:
|
|
|
|
vcenter_host = self.DOCKER_SIMULATOR_NAME
|
|
|
|
elif container_id:
|
|
|
|
vcenter_host = self._get_simulator_address()
|
|
|
|
display.info('Found vCenter simulator container address: %s' % vcenter_host, verbosity=1)
|
|
|
|
else:
|
|
|
|
vcenter_host = 'localhost'
|
|
|
|
|
|
|
|
self._set_cloud_config('vcenter_host', vcenter_host)
|
|
|
|
|
|
|
|
def _get_simulator_address(self):
|
|
|
|
results = docker_inspect(self.args, self.container_name)
|
|
|
|
ipaddress = results[0]['NetworkSettings']['IPAddress']
|
|
|
|
return ipaddress
|
|
|
|
|
|
|
|
def _setup_static(self):
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
|
|
|
|
|
|
class VcenterEnvironment(CloudEnvironment):
|
|
|
|
"""VMware vcenter/esx environment plugin. Updates integration test environment after delegation."""
|
2019-03-01 03:25:49 +01:00
|
|
|
def get_environment_config(self):
|
2017-06-22 19:49:58 +02:00
|
|
|
"""
|
2019-03-01 03:25:49 +01:00
|
|
|
:rtype: CloudEnvironmentConfig
|
2017-06-22 19:49:58 +02:00
|
|
|
"""
|
2019-03-01 03:25:49 +01:00
|
|
|
env_vars = dict(
|
|
|
|
VCENTER_HOST=self._get_cloud_config('vcenter_host'),
|
|
|
|
)
|
|
|
|
|
|
|
|
ansible_vars = dict(
|
|
|
|
vcsim=self._get_cloud_config('vcenter_host'),
|
|
|
|
)
|
|
|
|
|
|
|
|
return CloudEnvironmentConfig(
|
|
|
|
env_vars=env_vars,
|
|
|
|
ansible_vars=ansible_vars,
|
|
|
|
)
|