2018-03-28 17:16:48 +02:00
|
|
|
"""Foreman plugin for integration tests."""
|
2019-07-12 08:46:20 +02:00
|
|
|
from __future__ import (absolute_import, division, print_function)
|
|
|
|
__metaclass__ = type
|
2018-03-28 17:16:48 +02:00
|
|
|
|
|
|
|
import os
|
|
|
|
|
|
|
|
from . import (
|
|
|
|
CloudProvider,
|
|
|
|
CloudEnvironment,
|
2019-03-01 03:25:49 +01:00
|
|
|
CloudEnvironmentConfig,
|
2018-03-28 17:16:48 +02:00
|
|
|
)
|
|
|
|
|
2021-02-02 20:47:38 +01:00
|
|
|
from ..containers import (
|
|
|
|
run_support_container,
|
2018-03-28 17:16:48 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class ForemanProvider(CloudProvider):
|
|
|
|
"""Foreman plugin.
|
|
|
|
|
|
|
|
Sets up Foreman stub server for tests.
|
|
|
|
"""
|
|
|
|
|
|
|
|
DOCKER_SIMULATOR_NAME = 'foreman-stub'
|
2018-04-04 12:02:03 +02:00
|
|
|
|
2018-10-15 02:51:25 +02:00
|
|
|
DOCKER_IMAGE = 'quay.io/ansible/foreman-test-container:1.4.0'
|
2018-04-04 12:02:03 +02:00
|
|
|
"""Default image to run Foreman stub from.
|
|
|
|
|
|
|
|
The simulator must be pinned to a specific version
|
|
|
|
to guarantee CI passes with the version used.
|
2018-04-03 20:07:51 +02:00
|
|
|
|
|
|
|
It's source source itself resides at:
|
|
|
|
https://github.com/ansible/foreman-test-container
|
|
|
|
"""
|
2018-03-28 17:16:48 +02:00
|
|
|
|
|
|
|
def __init__(self, args):
|
|
|
|
"""Set up container references for provider.
|
|
|
|
|
|
|
|
:type args: TestConfig
|
|
|
|
"""
|
|
|
|
super(ForemanProvider, self).__init__(args)
|
|
|
|
|
2020-02-04 20:21:53 +01:00
|
|
|
self.__container_from_env = os.environ.get('ANSIBLE_FRMNSIM_CONTAINER')
|
2018-04-03 20:07:51 +02:00
|
|
|
"""Overrides target container, might be used for development.
|
|
|
|
|
2018-04-04 12:02:03 +02:00
|
|
|
Use ANSIBLE_FRMNSIM_CONTAINER=whatever_you_want if you want
|
|
|
|
to use other image. Omit/empty otherwise.
|
2018-04-03 20:07:51 +02:00
|
|
|
"""
|
|
|
|
|
2018-04-04 12:02:03 +02:00
|
|
|
self.image = self.__container_from_env or self.DOCKER_IMAGE
|
2018-03-28 17:16:48 +02:00
|
|
|
|
2021-02-02 20:47:38 +01:00
|
|
|
self.uses_docker = True
|
2018-03-28 17:16:48 +02:00
|
|
|
|
|
|
|
def setup(self):
|
|
|
|
"""Setup cloud resource before delegation and reg cleanup callback."""
|
|
|
|
super(ForemanProvider, self).setup()
|
|
|
|
|
|
|
|
if self._use_static_config():
|
|
|
|
self._setup_static()
|
|
|
|
else:
|
|
|
|
self._setup_dynamic()
|
|
|
|
|
|
|
|
def _setup_dynamic(self):
|
|
|
|
"""Spawn a Foreman stub within docker container."""
|
|
|
|
foreman_port = 8080
|
|
|
|
|
2021-02-02 20:47:38 +01:00
|
|
|
ports = [
|
|
|
|
foreman_port,
|
|
|
|
]
|
|
|
|
|
|
|
|
descriptor = run_support_container(
|
|
|
|
self.args,
|
|
|
|
self.platform,
|
|
|
|
self.image,
|
|
|
|
self.DOCKER_SIMULATOR_NAME,
|
|
|
|
ports,
|
|
|
|
allow_existing=True,
|
|
|
|
cleanup=True,
|
2018-03-28 17:16:48 +02:00
|
|
|
)
|
|
|
|
|
2021-02-02 20:47:38 +01:00
|
|
|
descriptor.register(self.args)
|
2018-03-28 17:16:48 +02:00
|
|
|
|
2021-02-02 20:47:38 +01:00
|
|
|
self._set_cloud_config('FOREMAN_HOST', self.DOCKER_SIMULATOR_NAME)
|
2018-03-28 17:16:48 +02:00
|
|
|
self._set_cloud_config('FOREMAN_PORT', str(foreman_port))
|
|
|
|
|
|
|
|
def _setup_static(self):
|
2021-02-02 20:47:38 +01:00
|
|
|
raise NotImplementedError()
|
2018-03-28 17:16:48 +02:00
|
|
|
|
|
|
|
|
|
|
|
class ForemanEnvironment(CloudEnvironment):
|
|
|
|
"""Foreman environment plugin.
|
|
|
|
|
|
|
|
Updates integration test environment after delegation.
|
|
|
|
"""
|
2019-03-01 03:25:49 +01:00
|
|
|
def get_environment_config(self):
|
2018-03-28 17:16:48 +02:00
|
|
|
"""
|
2019-03-01 03:25:49 +01:00
|
|
|
:rtype: CloudEnvironmentConfig
|
2018-03-28 17:16:48 +02:00
|
|
|
"""
|
2019-03-01 03:25:49 +01:00
|
|
|
env_vars = dict(
|
|
|
|
FOREMAN_HOST=self._get_cloud_config('FOREMAN_HOST'),
|
|
|
|
FOREMAN_PORT=self._get_cloud_config('FOREMAN_PORT'),
|
|
|
|
)
|
2018-03-28 17:16:48 +02:00
|
|
|
|
2019-03-01 03:25:49 +01:00
|
|
|
return CloudEnvironmentConfig(
|
|
|
|
env_vars=env_vars,
|
|
|
|
)
|