2017-06-22 19:49:58 +02:00
|
|
|
"""VMware vCenter plugin for integration tests."""
|
2019-07-12 08:46:20 +02:00
|
|
|
from __future__ import (absolute_import, division, print_function)
|
|
|
|
__metaclass__ = type
|
2017-06-22 19:49:58 +02:00
|
|
|
|
|
|
|
import os
|
|
|
|
|
2019-08-06 23:43:29 +02:00
|
|
|
from . import (
|
2017-06-22 19:49:58 +02:00
|
|
|
CloudProvider,
|
|
|
|
CloudEnvironment,
|
2019-03-01 03:25:49 +01:00
|
|
|
CloudEnvironmentConfig,
|
2017-06-22 19:49:58 +02:00
|
|
|
)
|
|
|
|
|
2019-08-06 23:43:29 +02:00
|
|
|
from ..util import (
|
2017-06-22 19:49:58 +02:00
|
|
|
find_executable,
|
|
|
|
display,
|
2019-06-04 22:49:30 +02:00
|
|
|
is_shippable,
|
|
|
|
ConfigParser,
|
2017-06-22 19:49:58 +02:00
|
|
|
)
|
|
|
|
|
2019-08-06 23:43:29 +02:00
|
|
|
from ..docker_util import (
|
2017-06-22 19:49:58 +02:00
|
|
|
docker_run,
|
|
|
|
docker_rm,
|
|
|
|
docker_inspect,
|
|
|
|
docker_pull,
|
|
|
|
get_docker_container_id,
|
|
|
|
)
|
|
|
|
|
2019-08-06 23:43:29 +02:00
|
|
|
from ..core_ci import (
|
2019-06-04 22:49:30 +02:00
|
|
|
AnsibleCoreCI,
|
|
|
|
)
|
|
|
|
|
2017-06-22 19:49:58 +02:00
|
|
|
|
|
|
|
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:
|
2019-08-09 04:26:52 +02:00
|
|
|
self.image = 'quay.io/ansible/vcenter-test-container:1.7.0'
|
2017-06-22 19:49:58 +02:00
|
|
|
self.container_name = ''
|
|
|
|
|
2019-06-30 16:00:43 +02:00
|
|
|
# VMware tests can be run on govcsim or baremetal, either BYO with a static config
|
2019-06-04 22:49:30 +02:00
|
|
|
# file or hosted in worldstream. Using an env var value of 'worldstream' with appropriate
|
|
|
|
# CI credentials will deploy a dynamic baremetal environment. The simulator is the default
|
|
|
|
# if no other config if provided.
|
2019-08-29 19:54:39 +02:00
|
|
|
self.vmware_test_platform = os.environ.get('VMWARE_TEST_PLATFORM', 'govcsim')
|
2019-06-04 22:49:30 +02:00
|
|
|
self.aci = None
|
|
|
|
self.insecure = False
|
2019-06-19 15:12:14 +02:00
|
|
|
self.proxy = None
|
2019-08-29 19:54:39 +02:00
|
|
|
self.platform = 'vcenter'
|
2019-06-04 22:49:30 +02:00
|
|
|
|
2017-06-22 19:49:58 +02:00
|
|
|
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]
|
|
|
|
"""
|
2019-09-12 10:48:06 +02:00
|
|
|
if self.vmware_test_platform == 'govcsim' or (self.vmware_test_platform == '' and not os.path.isfile(self.config_static_path)):
|
2019-06-04 22:49:30 +02:00
|
|
|
docker = find_executable('docker', required=False)
|
2017-06-22 19:49:58 +02:00
|
|
|
|
2019-06-04 22:49:30 +02:00
|
|
|
if docker:
|
|
|
|
return
|
|
|
|
|
|
|
|
skip = 'cloud/%s/' % self.platform
|
|
|
|
skipped = [target.name for target in targets if skip in target.aliases]
|
|
|
|
|
|
|
|
if skipped:
|
|
|
|
exclude.append(skip)
|
2019-09-12 10:48:06 +02:00
|
|
|
display.warning('Excluding tests marked "%s" which require the "docker" command or config (see "%s"): %s'
|
|
|
|
% (skip.rstrip('/'), self.config_template_path, ', '.join(skipped)))
|
2019-08-29 19:54:39 +02:00
|
|
|
elif self.vmware_test_platform == 'static':
|
2019-06-04 22:49:30 +02:00
|
|
|
if os.path.isfile(self.config_static_path):
|
|
|
|
return
|
|
|
|
|
2019-08-29 19:54:39 +02:00
|
|
|
super(VcenterProvider, self).filter(targets, exclude)
|
|
|
|
elif self.vmware_test_platform == 'worldstream':
|
2019-06-04 22:49:30 +02:00
|
|
|
aci = self._create_ansible_core_ci()
|
2017-06-22 19:49:58 +02:00
|
|
|
|
2019-06-04 22:49:30 +02:00
|
|
|
if os.path.isfile(aci.ci_key):
|
|
|
|
return
|
2017-09-16 01:08:25 +02:00
|
|
|
|
2019-06-04 22:49:30 +02:00
|
|
|
if is_shippable():
|
|
|
|
return
|
|
|
|
|
|
|
|
super(VcenterProvider, self).filter(targets, exclude)
|
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()
|
|
|
|
|
2019-06-04 22:49:30 +02:00
|
|
|
self._set_cloud_config('vmware_test_platform', self.vmware_test_platform)
|
2019-07-24 14:22:23 +02:00
|
|
|
if self.vmware_test_platform == 'govcsim':
|
|
|
|
self._setup_dynamic_simulator()
|
2019-08-29 19:54:39 +02:00
|
|
|
self.managed = True
|
2019-08-27 20:56:43 +02:00
|
|
|
elif self.vmware_test_platform == 'worldstream':
|
|
|
|
self._setup_dynamic_baremetal()
|
2019-08-29 19:54:39 +02:00
|
|
|
self.managed = True
|
|
|
|
elif self.vmware_test_platform == 'static':
|
|
|
|
self._use_static_config()
|
2017-06-22 19:49:58 +02:00
|
|
|
self._setup_static()
|
|
|
|
else:
|
2019-08-29 19:54:39 +02:00
|
|
|
display.error('Unknown vmware_test_platform: %s' % self.vmware_test_platform)
|
|
|
|
exit(1)
|
2017-06-22 19:49:58 +02:00
|
|
|
|
|
|
|
def get_docker_run_options(self):
|
|
|
|
"""Get any additional options needed when delegating tests to a docker container.
|
|
|
|
:rtype: list[str]
|
|
|
|
"""
|
2019-06-04 22:49:30 +02:00
|
|
|
if self.managed and self.vmware_test_platform != 'worldstream':
|
2017-06-22 19:49:58 +02:00
|
|
|
return ['--link', self.DOCKER_SIMULATOR_NAME]
|
|
|
|
|
|
|
|
return []
|
|
|
|
|
|
|
|
def cleanup(self):
|
|
|
|
"""Clean up the cloud resource and any temporary configuration files after tests complete."""
|
2019-06-04 22:49:30 +02:00
|
|
|
if self.vmware_test_platform == 'worldstream':
|
|
|
|
|
|
|
|
if self.aci:
|
|
|
|
self.aci.stop()
|
|
|
|
|
2017-06-22 19:49:58 +02:00
|
|
|
if self.container_name:
|
|
|
|
docker_rm(self.args, self.container_name)
|
|
|
|
|
|
|
|
super(VcenterProvider, self).cleanup()
|
|
|
|
|
2019-06-04 22:49:30 +02:00
|
|
|
def _setup_dynamic_simulator(self):
|
2017-06-22 19:49:58 +02:00
|
|
|
"""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:
|
2019-08-29 19:54:39 +02:00
|
|
|
vcenter_hostname = self.DOCKER_SIMULATOR_NAME
|
2017-06-22 19:49:58 +02:00
|
|
|
elif container_id:
|
2019-08-29 19:54:39 +02:00
|
|
|
vcenter_hostname = self._get_simulator_address()
|
|
|
|
display.info('Found vCenter simulator container address: %s' % vcenter_hostname, verbosity=1)
|
2017-06-22 19:49:58 +02:00
|
|
|
else:
|
2019-08-29 19:54:39 +02:00
|
|
|
vcenter_hostname = 'localhost'
|
2017-06-22 19:49:58 +02:00
|
|
|
|
2019-08-29 19:54:39 +02:00
|
|
|
self._set_cloud_config('vcenter_hostname', vcenter_hostname)
|
2017-06-22 19:49:58 +02:00
|
|
|
|
|
|
|
def _get_simulator_address(self):
|
|
|
|
results = docker_inspect(self.args, self.container_name)
|
|
|
|
ipaddress = results[0]['NetworkSettings']['IPAddress']
|
|
|
|
return ipaddress
|
|
|
|
|
2019-06-04 22:49:30 +02:00
|
|
|
def _setup_dynamic_baremetal(self):
|
|
|
|
"""Request Esxi credentials through the Ansible Core CI service."""
|
|
|
|
display.info('Provisioning %s cloud environment.' % self.platform,
|
|
|
|
verbosity=1)
|
|
|
|
|
|
|
|
config = self._read_config_template()
|
|
|
|
|
|
|
|
aci = self._create_ansible_core_ci()
|
|
|
|
|
|
|
|
if not self.args.explain:
|
|
|
|
self.aci = aci
|
2019-08-27 20:56:43 +02:00
|
|
|
aci.start()
|
|
|
|
aci.wait(iterations=160)
|
2019-06-04 22:49:30 +02:00
|
|
|
|
2019-08-27 20:56:43 +02:00
|
|
|
data = aci.get().response_json.get('data')
|
2019-08-29 19:24:25 +02:00
|
|
|
for key, value in data.items():
|
|
|
|
if key.endswith('PASSWORD'):
|
|
|
|
display.sensitive.add(value)
|
2019-08-27 20:56:43 +02:00
|
|
|
config = self._populate_config_template(config, data)
|
2019-06-04 22:49:30 +02:00
|
|
|
self._write_config(config)
|
|
|
|
|
|
|
|
def _create_ansible_core_ci(self):
|
|
|
|
"""
|
|
|
|
:rtype: AnsibleCoreCI
|
|
|
|
"""
|
|
|
|
return AnsibleCoreCI(self.args, 'vmware', 'vmware',
|
|
|
|
persist=False, stage=self.args.remote_stage,
|
|
|
|
provider='vmware')
|
|
|
|
|
2017-06-22 19:49:58 +02:00
|
|
|
def _setup_static(self):
|
2019-08-29 19:54:39 +02:00
|
|
|
if not os.path.exists(self.config_static_path):
|
|
|
|
display.error('Configuration file does not exist: %s' % self.config_static_path)
|
|
|
|
exit(1)
|
2019-06-27 23:38:33 +02:00
|
|
|
parser = ConfigParser({
|
|
|
|
'vcenter_port': '443',
|
2019-07-12 11:05:27 +02:00
|
|
|
'vmware_proxy_host': '',
|
2019-07-24 14:22:23 +02:00
|
|
|
'vmware_proxy_port': '8080'})
|
2019-06-04 22:49:30 +02:00
|
|
|
parser.read(self.config_static_path)
|
|
|
|
|
|
|
|
if parser.get('DEFAULT', 'vmware_validate_certs').lower() in ('no', 'false'):
|
|
|
|
self.insecure = True
|
2019-06-19 15:12:14 +02:00
|
|
|
proxy_host = parser.get('DEFAULT', 'vmware_proxy_host')
|
|
|
|
proxy_port = int(parser.get('DEFAULT', 'vmware_proxy_port'))
|
|
|
|
if proxy_host and proxy_port:
|
|
|
|
self.proxy = 'http://%s:%d' % (proxy_host, proxy_port)
|
2019-06-04 22:49:30 +02:00
|
|
|
|
2017-06-22 19:49:58 +02:00
|
|
|
|
|
|
|
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-08-29 19:54:39 +02:00
|
|
|
try:
|
|
|
|
# We may be in a container, so we cannot just reach VMWARE_TEST_PLATFORM,
|
|
|
|
# We do a try/except instead
|
2019-06-04 22:49:30 +02:00
|
|
|
parser = ConfigParser()
|
2019-08-29 19:54:39 +02:00
|
|
|
parser.read(self.config_path) # Worldstream and static
|
2019-06-04 22:49:30 +02:00
|
|
|
|
|
|
|
# Most of the test cases use ansible_vars, but we plan to refactor these
|
|
|
|
# to use env_vars, output both for now
|
|
|
|
env_vars = dict(
|
2019-07-31 18:53:11 +02:00
|
|
|
(key.upper(), value) for key, value in parser.items('DEFAULT', raw=True))
|
2019-06-04 22:49:30 +02:00
|
|
|
|
|
|
|
ansible_vars = dict(
|
|
|
|
resource_prefix=self.resource_prefix,
|
|
|
|
)
|
2019-07-31 18:53:11 +02:00
|
|
|
ansible_vars.update(dict(parser.items('DEFAULT', raw=True)))
|
2019-08-29 19:54:39 +02:00
|
|
|
except KeyError: # govcsim
|
2019-06-04 22:49:30 +02:00
|
|
|
env_vars = dict(
|
2019-08-29 19:54:39 +02:00
|
|
|
VCENTER_HOSTNAME=self._get_cloud_config('vcenter_hostname'),
|
|
|
|
VCENTER_USERNAME='user',
|
|
|
|
VCENTER_PASSWORD='pass',
|
2019-06-04 22:49:30 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
ansible_vars = dict(
|
2019-08-29 19:54:39 +02:00
|
|
|
vcsim=self._get_cloud_config('vcenter_hostname'),
|
2019-06-04 22:49:30 +02:00
|
|
|
)
|
2019-03-01 03:25:49 +01:00
|
|
|
|
2019-09-17 06:01:37 +02:00
|
|
|
for key, value in ansible_vars.items():
|
|
|
|
if key.endswith('_password'):
|
|
|
|
display.sensitive.add(value)
|
|
|
|
|
2019-03-01 03:25:49 +01:00
|
|
|
return CloudEnvironmentConfig(
|
|
|
|
env_vars=env_vars,
|
|
|
|
ansible_vars=ansible_vars,
|
|
|
|
)
|