Browse Source

Allow OpenStack integration tests to reuse prepare_config_dir

Like the playbooks/services tests, OpenStack integration tests needs an
SSH key, OpenStack credentials. All the requirements for these tests can
be managed the same way.
keep-around/ff85a610bfa1ac009caa880465ad3f17d24007eb
Pierre-Louis Bonicoli 1 year ago
parent
commit
8d806cc3bb
No known key found for this signature in database GPG Key ID: 6914C4A5EDAA6DD
  1. 38
      playbooks/conftest.py
  2. 31
      tests/__init__.py
  3. 10
      tests/enough/common/test_common_service.py
  4. 15
      tests/enough/common/test_common_ssh.py
  5. 39
      tests/enough/common/test_init.py

38
playbooks/conftest.py

@ -1,10 +1,9 @@
import os
import pytest
import shutil
import textwrap
import yaml
from enough.common import Enough
from tests import make_config_dir, prepare_config_dir
def pytest_addoption(parser):
@ -29,35 +28,6 @@ def pytest_configure(config):
pass
def make_config_dir(domain, cache):
d = cache.makedir('dotenough')
os.environ['ENOUGH_DOT'] = str(d)
config_dir = f'{d}/{domain}'
if not os.path.exists(config_dir):
os.makedirs(config_dir)
return config_dir
def prepare_config_dir(domain, cache):
os.environ['ENOUGH_DOMAIN'] = domain
config_dir = make_config_dir(domain, cache)
# Reuse existent SSH key. If it doesn't exist, then a key will be
# generated by DotEnough.ensure_ssh_key.
if os.path.isfile('infrastructure_key'):
shutil.copy('infrastructure_key', f'{config_dir}/infrastructure_key')
shutil.copy('infrastructure_key.pub', f'{config_dir}/infrastructure_key.pub')
all_dir = f'{config_dir}/inventory/group_vars/all'
if not os.path.exists(all_dir):
os.makedirs(all_dir)
shutil.copyfile('inventory/group_vars/all/clouds.yml', f'{all_dir}/clouds.yml')
shutil.copyfile('inventory/group_vars/all/provision.yml', f'{all_dir}/provision.yml')
open(f'{all_dir}/certificate.yml', 'w').write(textwrap.dedent(f"""\
---
certificate_authority: letsencrypt_staging
"""))
return config_dir
def get_hosts(session, enough):
hosts = session.config.getoption("--enough-hosts").split(',')
updated = enough.service.add_vpn_hosts_if_needed(hosts)
@ -69,7 +39,8 @@ def pytest_sessionstart(session):
provider = session.config.getoption("--provider")
os.environ['ENOUGH_PROVIDER'] = provider
domain = f'{service_directory}.test'
config_dir = prepare_config_dir(domain, session.config.cache)
enough_dot_dir = session.config.cache.makedir('dotenough')
config_dir = prepare_config_dir(domain, enough_dot_dir)
if session.config.getoption("--enough-no-create"):
return
@ -123,7 +94,8 @@ def pytest_sessionfinish(session, exitstatus):
service_directory = session.config.getoption("--enough-service")
provider = session.config.getoption("--provider")
domain = f'{service_directory}.test'
config_dir = make_config_dir(domain, session.config.cache)
enough_dot_dir = session.config.cache.makedir('dotenough')
config_dir = make_config_dir(domain, enough_dot_dir)
e = Enough(config_dir, '.',
domain=domain,
driver='openstack',

31
tests/__init__.py

@ -0,0 +1,31 @@
import os
import shutil
import textwrap
def make_config_dir(domain, enough_dot_dir):
os.environ['ENOUGH_DOT'] = str(enough_dot_dir)
config_dir = f'{enough_dot_dir}/{domain}'
if not os.path.exists(config_dir):
os.makedirs(config_dir)
return config_dir
def prepare_config_dir(domain, enough_dot_dir):
os.environ['ENOUGH_DOMAIN'] = domain
config_dir = make_config_dir(domain, enough_dot_dir)
# Reuse existent SSH key. If it doesn't exist, then a key will be
# generated by DotEnough.ensure_ssh_key.
if os.path.isfile('infrastructure_key'):
shutil.copy('infrastructure_key', f'{config_dir}/infrastructure_key')
shutil.copy('infrastructure_key.pub', f'{config_dir}/infrastructure_key.pub')
all_dir = f'{config_dir}/inventory/group_vars/all'
if not os.path.exists(all_dir):
os.makedirs(all_dir)
shutil.copyfile('inventory/group_vars/all/clouds.yml', f'{all_dir}/clouds.yml')
shutil.copyfile('inventory/group_vars/all/provision.yml', f'{all_dir}/provision.yml')
open(f'{all_dir}/certificate.yml', 'w').write(textwrap.dedent(f"""\
---
certificate_authority: letsencrypt_staging
"""))
return config_dir

10
tests/enough/common/test_common_service.py

@ -1,23 +1,23 @@
import os
import pytest
import requests_mock as requests_mock_module
import shutil
from enough import settings
from enough.common import openstack
from enough.common import service
from tests import prepare_config_dir
@pytest.mark.openstack_integration
def test_openstack_create_or_update(request, tmpdir, requests_mock):
try:
shutil.copy('infrastructure_key', f'{tmpdir}/infrastructure_key')
shutil.copy('infrastructure_key.pub', f'{tmpdir}/infrastructure_key.pub')
domain = 'enough.test'
config_dir = prepare_config_dir(domain, tmpdir)
requests_mock.post(requests_mock_module.ANY, status_code=201)
requests_mock.get(requests_mock_module.ANY, status_code=200)
domain = f'enough.test'
playbook = 'tests/enough/common/test_common_service/enough-playbook.yml'
s = service.ServiceOpenStack(str(tmpdir), settings.SHARE_DIR, **{
s = service.ServiceOpenStack(config_dir, settings.SHARE_DIR, **{
'driver': 'openstack',
'playbook': os.path.abspath(playbook),
'domain': domain,

15
tests/enough/common/test_common_ssh.py

@ -1,18 +1,21 @@
import pytest
from enough import settings
from enough.common.openstack import Heat, Stack
from enough.common.ssh import SSH
from tests import prepare_config_dir
@pytest.mark.openstack_integration
def test_ssh(request, openstack_name, openstack_provider):
def test_ssh(openstack_name, openstack_provider, tmpdir):
domain = 'enough.community'
d = Heat.hostvars_to_stack(openstack_name, openstack_provider)
s = Stack(settings.CONFIG_DIR, d)
s.set_public_key('infrastructure_key.pub')
config_dir = prepare_config_dir(domain, tmpdir)
provider = openstack_provider['openstack_provider']
ssh = SSH(config_dir, domain=domain, provider=provider)
s = Stack(config_dir, d)
s.set_public_key(f'{config_dir}/infrastructure_key.pub')
s.create_or_update()
ssh = SSH(settings.CONFIG_DIR, domain='enough.community',
provider=request.config.option.provider)
r = ssh.ssh(openstack_name, ['uptime'], interactive=False)
s.delete()
assert r.returncode == 0

39
tests/enough/common/test_init.py

@ -9,26 +9,29 @@ from enough import settings
from enough.common import Enough
from enough.common.openstack import OpenStackBase, OpenStack
from tests import prepare_config_dir
@pytest.mark.openstack_integration
def test_clone_and_destroy(request, tmpdir):
test_clouds = 'inventory/group_vars/all/clouds.yml'
clone_override_dir = f'{tmpdir}/clone-override'
shutil.copytree('tests/enough/common/test_init/clone-override',
clone_override_dir)
original_domain = 'original.com'
original_config_dir = f'{tmpdir}/{original_domain}'
# TODO: swap the two instructions below once Python 3.8 is used
# and update copytree parameters:
# 1. add dirs_exist_ok=True parameter
# 2. use original_config_dir instead of f'{tmpdir}/{original_domain}
shutil.copytree('tests/enough/common/test_init/backup',
original_config_dir)
f'{tmpdir}/{original_domain}')
original_config_dir = prepare_config_dir(original_domain, tmpdir)
password = 'PASSWORD'
open(f'{original_config_dir}.pass', 'w').write(password)
shutil.copy('infrastructure_key', f'{original_config_dir}/infrastructure_key')
shutil.copy('infrastructure_key.pub', f'{original_config_dir}/infrastructure_key.pub')
original_all_dir = f'{original_config_dir}/inventory/group_vars/all'
os.makedirs(original_all_dir)
original_clouds = f'{original_all_dir}/clouds.yml'
shutil.copyfile(test_clouds, original_clouds)
assert not os.path.exists(f'{original_all_dir}/internal_network.yml')
original = Enough(original_config_dir, settings.SHARE_DIR,
@ -53,20 +56,18 @@ def test_clone_and_destroy(request, tmpdir):
def create_enough(provider, tmpdir, dotenough, **kwargs):
clouds = 'inventory/group_vars/all/clouds.yml'
enough_domain = 'enough.com'
enough_config_dir = f'{tmpdir}/{enough_domain}'
# TODO: swap the two instructions below once Python 3.8 is used
# and update copytree parameters:
# 1. add dirs_exist_ok=True parameter
# 2. use config_dir instead of f'{tmpdir}/{enough_domain}
shutil.copytree(f'tests/enough/common/test_init/{dotenough}',
enough_config_dir)
shutil.copy('infrastructure_key', f'{enough_config_dir}/infrastructure_key')
shutil.copy('infrastructure_key.pub', f'{enough_config_dir}/infrastructure_key.pub')
enough_all_dir = f'{enough_config_dir}/inventory/group_vars/all'
os.makedirs(enough_all_dir)
enough_clouds = f'{enough_all_dir}/clouds.yml'
shutil.copyfile(clouds, enough_clouds)
enough = Enough(enough_config_dir, settings.SHARE_DIR,
f'{tmpdir}/{enough_domain}')
config_dir = prepare_config_dir(enough_domain, tmpdir)
enough = Enough(config_dir, settings.SHARE_DIR,
domain=enough_domain,
driver='openstack',
provider=provider,

Loading…
Cancel
Save