Browse Source

icinga: transition from molecule to pytest

keep-around/be27015c20574507418ba259d6bbe1783e6a264b
Loïc Dachary 1 year ago
committed by Loic Dachary
parent
commit
e8eaa71f44
Signed by: dachary GPG Key ID: 992D23B392F9E4F2
  1. 1
      conftest.py
  2. 14
      molecule/icinga/conftest.py
  3. 1
      molecule/icinga/create.yml
  4. 1
      molecule/icinga/destroy.yml
  5. 42
      molecule/icinga/molecule.yml
  6. 2
      molecule/icinga/tests/test_icinga_checks.py
  7. 13
      molecule/icinga/tests/test_icingaweb.py
  8. 12
      molecule/icinga/tests/test_letsencrypt.py
  9. 13
      tests/icinga_helper.py
  10. 2
      tox.ini

1
conftest.py

@ -6,3 +6,4 @@ from tests.icinga_helper import IcingaHelper
def pytest_configure(config):
IcingaHelper.set_ansible_inventory(config.getoption("--ansible-inventory"))
IcingaHelper.set_ssh_identity_file(config.getoption("--ssh-identity-file"))

14
molecule/icinga/conftest.py

@ -0,0 +1,14 @@
def pytest_addoption(parser):
parser.addoption(
"--enough-hosts",
action="store",
default="bind-host,icinga-host,packages-host,website-host",
help="list of hosts"
)
parser.addoption(
"--enough-service-directory",
action="store",
default="icinga",
help="service directory"
)

1
molecule/icinga/create.yml

@ -1 +0,0 @@
../infrastructure/create.yml

1
molecule/icinga/destroy.yml

@ -1 +0,0 @@
../infrastructure/destroy.yml

42
molecule/icinga/molecule.yml

@ -1,42 +0,0 @@
---
driver:
name: delegated
lint:
name: yamllint
platforms:
- name: bind-host
- name: icinga-host
- name: website-host
- name: packages-host
provisioner:
name: ansible
options:
i: ../../development-inventory
limit: bind-host,icinga-host,website-host,packages-host,localhost
lint:
name: ansible-lint
env:
ANSIBLE_ROLES_PATH: roles:../infrastructure/roles:../firewall/roles:../postfix/roles:../bind/roles:../icinga/roles:../backup/roles:../misc/roles:../packages/roles:../jdauphant.nginx/roles:../enough-nginx/roles:../certificate/roles
inventory:
links:
group_vars: ../../inventory/group_vars
host_vars: ../../inventory/host_vars
scenario:
name: icinga
test_sequence:
- destroy
- create
- converge
- verify
- destroy
verifier:
name: testinfra
options:
v: True
x: True
# s: True
# k: test_icinga_ntp_time
env:
PYTHONPATH: ../..
lint:
name: flake8

2
molecule/icinga/tests/test_icinga_checks.py

@ -1,6 +1,6 @@
from tests.icinga_helper import IcingaHelper
testinfra_hosts = ['icinga-host']
testinfra_hosts = ['ansible://icinga-host']
class TestChecks(IcingaHelper):

13
molecule/icinga/tests/test_icingaweb.py

@ -3,21 +3,22 @@ import requests
from tests.infrastructure import get_driver
import yaml
testinfra_hosts = ['bind-host']
testinfra_hosts = ['ansible://bind-host']
def get_address():
vars_dir = '../../inventory/group_vars/all'
def get_address(inventory):
vars_dir = f'{inventory}/group_vars/all'
return 'icinga.' + yaml.load(
open(vars_dir + '/domain.yml'))['domain']
def test_icingaweb2_login_screen(host):
def test_icingaweb2_login_screen(host, pytestconfig):
if get_driver() == 'docker':
pytest.skip("no letsencrypt when running docker")
proto_srv = "https://{address}".format(address=get_address())
address = get_address(pytestconfig.getoption("--ansible-inventory"))
proto_srv = f"https://{address}"
s = requests.Session()
s.verify = '../../certs'
s.verify = 'certs'
r = s.get(proto_srv+'/icingaweb2/authentication/login', timeout=5)
cookies = dict(r.cookies)
r = s.get(proto_srv+'/icingaweb2/authentication/login?_checkCookie=1',

12
molecule/icinga/tests/test_letsencrypt.py

@ -3,25 +3,25 @@ import requests
from tests.infrastructure import get_driver
import yaml
testinfra_hosts = ['icinga-host']
testinfra_hosts = ['ansible://icinga-host']
def get_address():
vars_dir = '../../inventory/group_vars/all'
def get_address(inventory):
vars_dir = f'{inventory}/group_vars/all'
return 'icinga.' + yaml.load(
open(vars_dir + '/domain.yml'))['domain']
def test_icingaweb2_login_screen(host):
def test_icingaweb2_login_screen(host, pytestconfig):
if get_driver() == 'docker':
pytest.skip("no letsencrypt when running docker")
address = get_address()
address = get_address(pytestconfig.getoption("--ansible-inventory"))
print(f'https://{address}/icingaweb2/authentication/login')
s = requests.Session()
r = s.get(f'http://{address}/icingaweb2/authentication/login',
timeout=5, allow_redirects=False)
r.status_code = 302
s.verify = '../../certs'
s.verify = 'certs'
r = s.get(f'https://{address}/icingaweb2/authentication/login',
timeout=5)
cookies = dict(r.cookies)

13
tests/icinga_helper.py

@ -16,8 +16,15 @@ class IcingaHelper(object):
def set_ansible_inventory(inventory):
IcingaHelper.inventory = inventory
# set by ../conftest.py
@staticmethod
def set_ssh_identity_file(ssh_identity_file):
IcingaHelper.ssh_identity_file = ssh_identity_file
def get_auth(self):
host = testinfra.get_host('ansible://icinga-host', ansible_inventory=self.inventory)
host = testinfra.get_host('ansible://icinga-host',
ssh_identity_file=self.ssh_identity_file,
ansible_inventory=self.inventory)
with host.sudo():
f = host.file("/etc/icinga2/conf.d/api-users.conf")
return (
@ -31,7 +38,9 @@ class IcingaHelper(object):
return 'icinga.' + yaml.load(
open(vars_dir + '/domain.yml'))['domain']
else:
host = testinfra.get_host('ansible://icinga-host', ansible_inventory=self.inventory)
host = testinfra.get_host('ansible://icinga-host',
ssh_identity_file=self.ssh_identity_file,
ansible_inventory=self.inventory)
return host.ansible.get_variables()['ansible_host']
def get_client(self):

2
tox.ini

@ -19,7 +19,7 @@ commands = coverage run --source=enough {envbindir}/py.test -vvv --durations 10
[testenv:flake8]
commands = flake8 {posargs}
[testenv:{infrastructure,bind,authorized_keys,backup,certificate,postfix}]
[testenv:{infrastructure,bind,authorized_keys,backup,certificate,postfix,icinga}]
passenv =
ENOUGH_API_TOKEN
PYTEST_ADDOPTS

Loading…
Cancel
Save