2020-06-17 23:06:07 +02:00
|
|
|
"""Galaxy (ansible-galaxy) plugin for integration tests."""
|
|
|
|
from __future__ import (absolute_import, division, print_function)
|
|
|
|
__metaclass__ = type
|
|
|
|
|
|
|
|
import os
|
|
|
|
import tempfile
|
|
|
|
|
|
|
|
from . import (
|
|
|
|
CloudProvider,
|
|
|
|
CloudEnvironment,
|
|
|
|
CloudEnvironmentConfig,
|
|
|
|
)
|
|
|
|
|
|
|
|
from ..util import (
|
|
|
|
find_executable,
|
|
|
|
display,
|
|
|
|
)
|
|
|
|
|
|
|
|
from ..docker_util import (
|
|
|
|
docker_command,
|
|
|
|
docker_run,
|
|
|
|
docker_start,
|
|
|
|
docker_rm,
|
|
|
|
docker_inspect,
|
|
|
|
docker_pull,
|
|
|
|
get_docker_container_id,
|
2020-10-23 03:45:03 +02:00
|
|
|
get_docker_hostname,
|
|
|
|
get_docker_container_ip,
|
|
|
|
get_docker_preferred_network_name,
|
|
|
|
is_docker_user_defined_network,
|
2020-06-17 23:06:07 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2020-07-01 22:20:03 +02:00
|
|
|
# We add BasicAuthentication, to make the tasks that deal with
|
|
|
|
# direct API access easier to deal with across galaxy_ng and pulp
|
2020-06-17 23:06:07 +02:00
|
|
|
SETTINGS = b'''
|
2020-07-01 22:20:03 +02:00
|
|
|
CONTENT_ORIGIN = 'http://ansible-ci-pulp:80'
|
|
|
|
ANSIBLE_API_HOSTNAME = 'http://ansible-ci-pulp:80'
|
|
|
|
ANSIBLE_CONTENT_HOSTNAME = 'http://ansible-ci-pulp:80/pulp/content'
|
|
|
|
TOKEN_AUTH_DISABLED = True
|
|
|
|
GALAXY_REQUIRE_CONTENT_APPROVAL = False
|
|
|
|
GALAXY_AUTHENTICATION_CLASSES = [
|
|
|
|
"rest_framework.authentication.SessionAuthentication",
|
|
|
|
"rest_framework.authentication.TokenAuthentication",
|
|
|
|
"rest_framework.authentication.BasicAuthentication",
|
|
|
|
]
|
2020-06-17 23:06:07 +02:00
|
|
|
'''
|
|
|
|
|
|
|
|
SET_ADMIN_PASSWORD = b'''#!/usr/bin/execlineb -S0
|
|
|
|
foreground {
|
|
|
|
redirfd -w 1 /dev/null
|
|
|
|
redirfd -w 2 /dev/null
|
|
|
|
export DJANGO_SETTINGS_MODULE pulpcore.app.settings
|
|
|
|
export PULP_CONTENT_ORIGIN localhost
|
|
|
|
s6-setuidgid postgres
|
2020-07-01 22:20:03 +02:00
|
|
|
if { /usr/local/bin/django-admin reset-admin-password --password password }
|
|
|
|
if { /usr/local/bin/pulpcore-manager create-group system:partner-engineers --users admin }
|
2020-06-17 23:06:07 +02:00
|
|
|
}
|
|
|
|
'''
|
|
|
|
|
2020-10-28 00:17:40 +01:00
|
|
|
# There are 2 overrides here:
|
|
|
|
# 1. Change the gunicorn bind address from 127.0.0.1 to 0.0.0.0 now that Galaxy NG does not allow us to access the
|
|
|
|
# Pulp API through it.
|
|
|
|
# 2. Grant access allowing us to DELETE a namespace in Galaxy NG. This is as CI deletes and recreates repos and
|
|
|
|
# distributions in Pulp which now breaks the namespace in Galaxy NG. Recreating it is the "simple" fix to get it
|
|
|
|
# working again.
|
|
|
|
# These may not be needed in the future, especially if 1 becomes configurable by an env var but for now they must be
|
|
|
|
# done.
|
|
|
|
OVERRIDES = b'''#!/usr/bin/execlineb -S0
|
|
|
|
foreground {
|
|
|
|
sed -i "0,/\\"127.0.0.1:24817\\"/s//\\"0.0.0.0:24817\\"/" /etc/services.d/pulpcore-api/run
|
|
|
|
}
|
|
|
|
|
|
|
|
# This sed calls changes the first occurrence to "allow" which is conveniently the delete operation for a namespace.
|
|
|
|
# https://github.com/ansible/galaxy_ng/blob/master/galaxy_ng/app/access_control/statements/standalone.py#L9-L11.
|
|
|
|
backtick NG_PREFIX { python -c "import galaxy_ng; print(galaxy_ng.__path__[0], end='')" }
|
|
|
|
importas ng_prefix NG_PREFIX
|
|
|
|
foreground {
|
|
|
|
sed -i "0,/\\"effect\\": \\"deny\\"/s//\\"effect\\": \\"allow\\"/" ${ng_prefix}/app/access_control/statements/standalone.py
|
|
|
|
}'''
|
|
|
|
|
2020-06-17 23:06:07 +02:00
|
|
|
|
|
|
|
class GalaxyProvider(CloudProvider):
|
|
|
|
"""Galaxy plugin.
|
|
|
|
|
2020-07-01 22:20:03 +02:00
|
|
|
Sets up pulp (ansible-galaxy) servers for tests.
|
2020-06-17 23:06:07 +02:00
|
|
|
|
|
|
|
The pulp source itself resides at: https://github.com/pulp/pulp-oci-images
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, args):
|
|
|
|
"""
|
|
|
|
:type args: TestConfig
|
|
|
|
"""
|
|
|
|
super(GalaxyProvider, self).__init__(args)
|
|
|
|
|
|
|
|
self.pulp = os.environ.get(
|
|
|
|
'ANSIBLE_PULP_CONTAINER',
|
2020-11-04 01:07:38 +01:00
|
|
|
'docker.io/pulp/pulp-galaxy-ng@sha256:263282d364e2c996f5e0740e85bbfb685bcc602bce1ab2c4f5af702fe46f9b20'
|
2020-06-17 23:06:07 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
self.containers = []
|
|
|
|
|
|
|
|
def filter(self, targets, exclude):
|
|
|
|
"""Filter out the tests with the necessary config and res unavailable.
|
|
|
|
|
|
|
|
:type targets: tuple[TestTarget]
|
|
|
|
:type exclude: list[str]
|
|
|
|
"""
|
|
|
|
docker_cmd = 'docker'
|
|
|
|
docker = find_executable(docker_cmd, required=False)
|
|
|
|
|
|
|
|
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)
|
|
|
|
display.warning('Excluding tests marked "%s" which require the "%s" command: %s'
|
|
|
|
% (skip.rstrip('/'), docker_cmd, ', '.join(skipped)))
|
|
|
|
|
|
|
|
def setup(self):
|
|
|
|
"""Setup cloud resource before delegation and reg cleanup callback."""
|
|
|
|
super(GalaxyProvider, self).setup()
|
|
|
|
|
|
|
|
container_id = get_docker_container_id()
|
|
|
|
|
2020-07-01 22:20:03 +02:00
|
|
|
p_results = docker_inspect(self.args, 'ansible-ci-pulp')
|
2020-06-17 23:06:07 +02:00
|
|
|
|
|
|
|
if p_results and not p_results[0].get('State', {}).get('Running'):
|
2020-07-01 22:20:03 +02:00
|
|
|
docker_rm(self.args, 'ansible-ci-pulp')
|
2020-06-17 23:06:07 +02:00
|
|
|
p_results = []
|
|
|
|
|
2020-07-01 22:20:03 +02:00
|
|
|
display.info('%s ansible-ci-pulp docker container.'
|
2020-06-17 23:06:07 +02:00
|
|
|
% ('Using the existing' if p_results else 'Starting a new'),
|
|
|
|
verbosity=1)
|
|
|
|
|
2020-10-28 00:17:40 +01:00
|
|
|
galaxy_port = 80
|
|
|
|
pulp_port = 24817
|
2020-06-17 23:06:07 +02:00
|
|
|
|
|
|
|
if not p_results:
|
|
|
|
if self.args.docker or container_id:
|
|
|
|
publish_ports = []
|
|
|
|
else:
|
|
|
|
# publish the simulator ports when not running inside docker
|
|
|
|
publish_ports = [
|
2020-10-28 00:17:40 +01:00
|
|
|
'-p', ':'.join((str(galaxy_port),) * 2),
|
2020-07-01 22:20:03 +02:00
|
|
|
'-p', ':'.join((str(pulp_port),) * 2),
|
2020-06-17 23:06:07 +02:00
|
|
|
]
|
|
|
|
|
|
|
|
docker_pull(self.args, self.pulp)
|
|
|
|
|
|
|
|
# Create the container, don't run it, we need to inject configs before it starts
|
|
|
|
stdout, _dummy = docker_run(
|
|
|
|
self.args,
|
|
|
|
self.pulp,
|
2020-07-01 22:20:03 +02:00
|
|
|
['--name', 'ansible-ci-pulp'] + publish_ports,
|
2020-06-17 23:06:07 +02:00
|
|
|
create_only=True
|
|
|
|
)
|
|
|
|
|
|
|
|
pulp_id = stdout.strip()
|
|
|
|
|
2020-10-28 00:17:40 +01:00
|
|
|
injected_files = {
|
|
|
|
'/etc/pulp/settings.py': SETTINGS,
|
|
|
|
'/etc/cont-init.d/111-postgres': SET_ADMIN_PASSWORD,
|
|
|
|
'/etc/cont-init.d/000-ansible-test-overrides': OVERRIDES,
|
|
|
|
}
|
|
|
|
for path, content in injected_files.items():
|
|
|
|
with tempfile.NamedTemporaryFile() as temp_fd:
|
|
|
|
temp_fd.write(content)
|
|
|
|
temp_fd.flush()
|
|
|
|
docker_command(self.args, ['cp', temp_fd.name, '%s:%s' % (pulp_id, path)])
|
2020-06-17 23:06:07 +02:00
|
|
|
|
|
|
|
# Start the container
|
2020-07-01 22:20:03 +02:00
|
|
|
docker_start(self.args, 'ansible-ci-pulp', [])
|
2020-06-17 23:06:07 +02:00
|
|
|
|
2020-07-01 22:20:03 +02:00
|
|
|
self.containers.append('ansible-ci-pulp')
|
2020-06-17 23:06:07 +02:00
|
|
|
|
|
|
|
if self.args.docker:
|
2020-07-01 22:20:03 +02:00
|
|
|
pulp_host = 'ansible-ci-pulp'
|
2020-06-17 23:06:07 +02:00
|
|
|
elif container_id:
|
2020-07-01 22:20:03 +02:00
|
|
|
pulp_host = self._get_simulator_address('ansible-ci-pulp')
|
2020-06-17 23:06:07 +02:00
|
|
|
display.info('Found Galaxy simulator container address: %s' % pulp_host, verbosity=1)
|
|
|
|
else:
|
2020-10-23 03:45:03 +02:00
|
|
|
pulp_host = get_docker_hostname()
|
2020-06-17 23:06:07 +02:00
|
|
|
|
|
|
|
self._set_cloud_config('PULP_HOST', pulp_host)
|
|
|
|
self._set_cloud_config('PULP_PORT', str(pulp_port))
|
2020-10-28 00:17:40 +01:00
|
|
|
self._set_cloud_config('GALAXY_PORT', str(galaxy_port))
|
2020-06-17 23:06:07 +02:00
|
|
|
self._set_cloud_config('PULP_USER', 'admin')
|
|
|
|
self._set_cloud_config('PULP_PASSWORD', 'password')
|
|
|
|
|
|
|
|
def get_docker_run_options(self):
|
|
|
|
"""Get additional options needed when delegating tests to a container.
|
|
|
|
|
|
|
|
:rtype: list[str]
|
|
|
|
"""
|
2020-10-23 03:45:03 +02:00
|
|
|
network = get_docker_preferred_network_name(self.args)
|
|
|
|
|
|
|
|
if not is_docker_user_defined_network(network):
|
|
|
|
return ['--link', 'ansible-ci-pulp']
|
|
|
|
|
|
|
|
return []
|
2020-06-17 23:06:07 +02:00
|
|
|
|
|
|
|
def cleanup(self):
|
|
|
|
"""Clean up the resource and temporary configs files after tests."""
|
|
|
|
for container_name in self.containers:
|
|
|
|
docker_rm(self.args, container_name)
|
|
|
|
|
|
|
|
super(GalaxyProvider, self).cleanup()
|
|
|
|
|
|
|
|
def _get_simulator_address(self, container_name):
|
2020-10-23 03:45:03 +02:00
|
|
|
return get_docker_container_ip(self.args, container_name)
|
2020-06-17 23:06:07 +02:00
|
|
|
|
|
|
|
|
|
|
|
class GalaxyEnvironment(CloudEnvironment):
|
|
|
|
"""Galaxy environment plugin.
|
|
|
|
|
|
|
|
Updates integration test environment after delegation.
|
|
|
|
"""
|
|
|
|
def get_environment_config(self):
|
|
|
|
"""
|
|
|
|
:rtype: CloudEnvironmentConfig
|
|
|
|
"""
|
|
|
|
pulp_user = self._get_cloud_config('PULP_USER')
|
|
|
|
pulp_password = self._get_cloud_config('PULP_PASSWORD')
|
|
|
|
pulp_host = self._get_cloud_config('PULP_HOST')
|
2020-10-28 00:17:40 +01:00
|
|
|
galaxy_port = self._get_cloud_config('GALAXY_PORT')
|
2020-06-17 23:06:07 +02:00
|
|
|
pulp_port = self._get_cloud_config('PULP_PORT')
|
|
|
|
|
|
|
|
return CloudEnvironmentConfig(
|
|
|
|
ansible_vars=dict(
|
|
|
|
pulp_user=pulp_user,
|
|
|
|
pulp_password=pulp_password,
|
2020-10-28 00:17:40 +01:00
|
|
|
pulp_v2_server='http://%s:%s/pulp_ansible/galaxy/published/api/' % (pulp_host, pulp_port),
|
|
|
|
pulp_v3_server='http://%s:%s/pulp_ansible/galaxy/published/api/' % (pulp_host, pulp_port),
|
2020-06-17 23:06:07 +02:00
|
|
|
pulp_api='http://%s:%s' % (pulp_host, pulp_port),
|
2020-10-28 00:17:40 +01:00
|
|
|
galaxy_ng_server='http://%s:%s/api/galaxy/' % (pulp_host, galaxy_port),
|
2020-06-17 23:06:07 +02:00
|
|
|
),
|
|
|
|
env_vars=dict(
|
|
|
|
PULP_USER=pulp_user,
|
|
|
|
PULP_PASSWORD=pulp_password,
|
2020-10-28 00:17:40 +01:00
|
|
|
PULP_V2_SERVER='http://%s:%s/pulp_ansible/galaxy/published/api/' % (pulp_host, pulp_port),
|
|
|
|
PULP_V3_SERVER='http://%s:%s/pulp_ansible/galaxy/published/api/' % (pulp_host, pulp_port),
|
|
|
|
GALAXY_NG_SERVER='http://%s:%s/api/galaxy/' % (pulp_host, galaxy_port),
|
2020-06-17 23:06:07 +02:00
|
|
|
),
|
|
|
|
)
|