Changeset - 5ccc26477822
[Not reviewed]
0 4 0
Branko Majic (branko) - 6 years ago 2018-07-24 16:49:23
branko@majic.rs
MAR-131: Refactor tests for wsgi_website to reduce code duplication through test parametrisation.
4 files changed with 530 insertions and 976 deletions:
0 comments (0 inline, 0 general)
roles/wsgi_website/molecule/default/tests/test_default.py
Show inline comments
 
import os
 
import re
 
import time
 

	
 
import pytest
 
import testinfra.utils.ansible_runner
 

	
 

	
 
testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner(
 
    os.environ['MOLECULE_INVENTORY_FILE']).get_hosts(['all'])
 

	
 

	
 
def test_hosts_file(host):
 
    f = host.file('/etc/hosts')
 
@pytest.mark.parametrize("expected_group, expected_group_id", [
 
    ('web-parameters-mandatory', 1003),
 
    ('web-parameters-optional_local', 5001),
 
    ('web-parameters-paste-req', 5002),
 
])
 
def test_website_group(host, expected_group, expected_group_id):
 
    """
 
    Tests if website group has been created correctly.
 
    """
 

	
 
    assert f.exists
 
    assert f.user == 'root'
 
    assert f.group == 'root'
 
    group = host.group(expected_group)
 

	
 
    assert group.exists
 
    assert group.gid == expected_group_id
 

	
 

	
 
@pytest.mark.parametrize("fqdn, expected_group, expected_admin, expected_user_id", [
 
    ('parameters-mandatory', 'web-parameters-mandatory', 'admin-parameters-mandatory', 1003),
 
    ('parameters-optional.local', 'web-parameters-optional_local', 'admin-parameters-optional_local', 5000),
 
    ('parameters-paste-req', 'web-parameters-paste-req', 'admin-parameters-paste-req', 5002)
 
])
 
def test_website_admin_user(host, fqdn, expected_group, expected_admin, expected_user_id):
 

	
 
    user = host.user(expected_admin)
 

	
 
    assert user.exists
 
    assert user.uid == expected_user_id
 
    assert user.group == expected_group
 
    assert user.groups == [expected_group]
 
    assert user.shell == '/bin/bash'
 
    assert user.home == '/var/www/' + fqdn
 

	
 

	
 
@pytest.mark.parametrize("home, expected_owner, expected_group", [
 
    ('/var/www/parameters-mandatory', 'admin-parameters-mandatory', 'web-parameters-mandatory'),
 
    ('/var/www/parameters-optional.local', 'admin-parameters-optional_local', 'web-parameters-optional_local'),
 
    ('/var/www/parameters-paste-req', 'admin-parameters-paste-req', 'web-parameters-paste-req'),
 
])
 
def test_website_admin_home(host, home, expected_owner, expected_group):
 
    """
 
    Tests if permissions on website admin home directory are correct.
 
    """
 

	
 
    home = host.file(home)
 

	
 
    assert home.is_directory
 
    assert home.user == expected_owner
 
    assert home.group == expected_group
 
    assert home.mode == 0o750
 

	
 

	
 
@pytest.mark.parametrize("profile_dir, expected_owner, expected_group", [
 
    ('/var/www/parameters-mandatory/.profile.d', 'admin-parameters-mandatory', 'web-parameters-mandatory'),
 
    ('/var/www/parameters-optional.local/.profile.d', 'admin-parameters-optional_local', 'web-parameters-optional_local'),
 
    ('/var/www/parameters-paste-req/.profile.d', 'admin-parameters-paste-req', 'web-parameters-paste-req'),
 
])
 
def test_home_profile_directory(host, profile_dir, expected_owner, expected_group):
 
    """
 
    Tests if profile directory has been set-up correctly for the website
 
    administrator/application user.
 
    """
 

	
 
    with host.sudo():
 

	
 
        directory = host.file(profile_dir)
 
        assert directory.is_directory
 
        assert directory.user == expected_owner
 
        assert directory.group == expected_group
 
        assert directory.mode == 0o750
 

	
 

	
 
@pytest.mark.parametrize("profile_file, expected_group", [
 
    ('/var/www/parameters-mandatory/.profile.d/virtualenv.sh', 'web-parameters-mandatory'),
 
    ('/var/www/parameters-optional.local/.profile.d/virtualenv.sh', 'web-parameters-optional_local'),
 
    ('/var/www/parameters-paste-req/.profile.d/virtualenv.sh', 'web-parameters-paste-req'),
 
])
 
def test_virtualenv_profile_configuration(host, profile_file, expected_group):
 
    """
 
    Tests if profile configuration file for auto-activation of virtual
 
    environment has been deployed correctly.
 
    """
 

	
 
    with host.sudo():
 

	
 
        config = host.file(profile_file)
 
        assert config.is_file
 
        assert config.user == 'root'
 
        assert config.group == expected_group
 
        assert config.mode == 0o640
 

	
 

	
 
@pytest.mark.parametrize("profile_file, expected_group, expected_content", [
 
    ('/var/www/parameters-mandatory/.profile.d/environment.sh', 'web-parameters-mandatory', ''),
 
    ('/var/www/parameters-optional.local/.profile.d/environment.sh', 'web-parameters-optional_local', "export MY_ENV_VAR='My environment variable'"),
 
    ('/var/www/parameters-paste-req/.profile.d/environment.sh', 'web-parameters-paste-req', ''),
 
])
 
def test_environment_profile_configuration(host, profile_file, expected_group, expected_content):
 
    """
 
    Tests if profile configuration file for setting-up environment variables has
 
    been deployed correctly.
 
    """
 

	
 
    with host.sudo():
 

	
 
        config = host.file(profile_file)
 
        assert config.is_file
 
        assert config.user == 'root'
 
        assert config.group == expected_group
 
        assert config.mode == 0o640
 
        assert config.content == expected_content
 

	
 

	
 
@pytest.mark.parametrize("admin_user, expected_virtualenv_path", [
 
    ('admin-parameters-mandatory', '/var/www/parameters-mandatory/virtualenv'),
 
    ('admin-parameters-optional_local', '/var/www/parameters-optional.local/virtualenv\nMy environment variable'),
 
    ('admin-parameters-paste-req', '/var/www/parameters-paste-req/virtualenv'),
 
])
 
def test_profile_configuration(host, admin_user, expected_virtualenv_path):
 
    """
 
    Tests if profile configuration is behaving correctly (setting appropriate
 
    vars via login shell).
 
    """
 

	
 
    env = host.run("sudo -i -u " + admin_user + " printenv VIRTUAL_ENV MY_ENV_VAR")
 
    assert env.stdout == expected_virtualenv_path
 

	
 

	
 
@pytest.mark.parametrize("app_user, expected_uid, expected_group, expected_home", [
 
    ('web-parameters-mandatory', 999, 'web-parameters-mandatory', '/var/www/parameters-mandatory'),
 
    ('web-parameters-optional_local', 5001, 'web-parameters-optional_local', '/var/www/parameters-optional.local'),
 
    ('web-parameters-paste-req', 998, 'web-parameters-paste-req', '/var/www/parameters-paste-req'),
 
])
 
def test_website_application_user(host, app_user, expected_uid, expected_group, expected_home):
 
    """
 
    Tests if website application user has been created correctly.
 
    """
 

	
 
    user = host.user(app_user)
 

	
 
    assert user.exists
 
    assert user.uid == expected_uid
 
    assert user.group == expected_group
 
    assert user.groups == [expected_group]
 
    assert user.shell == '/bin/sh'
 
    assert user.home == expected_home
 

	
 
    with host.sudo():
 
        umask = host.run("su -l " + app_user + " -c 'bash -c umask'")
 
        assert umask.stdout == '0007'
 

	
 

	
 
@pytest.mark.parametrize("expected_group", [
 
    'web-parameters-mandatory',
 
    'web-parameters-optional_local',
 
    'web-parameters-paste-req'
 
])
 
def test_nginx_user(host, expected_group):
 
    """
 
    Tests if web server user has been added to website group.
 
    """
 

	
 
    user = host.user('www-data')
 
    assert expected_group in user.groups
 

	
 

	
 
@pytest.mark.parametrize("forward_file, expected_group, expected_content", [
 
    ('/var/www/parameters-mandatory/.forward', 'web-parameters-mandatory', 'root'),
 
    ('/var/www/parameters-optional.local/.forward', 'web-parameters-optional_local', 'user'),
 
    ('/var/www/parameters-paste-req/.forward', 'web-parameters-paste-req', 'root'),
 
])
 
def test_forward_file(host, forward_file, expected_group, expected_content):
 
    """
 
    Tests if the forward file has correct permissions and content.
 
    """
 

	
 
    with host.sudo():
 

	
 
        config = host.file(forward_file)
 

	
 
        assert config.is_file
 
        assert config.user == 'root'
 
        assert config.group == expected_group
 
        assert config.mode == 0o640
 
        assert config.content == expected_content
 

	
 

	
 
@pytest.mark.parametrize("original_destination, expected_destination_user", [
 
    ('web-parameters-mandatory@localhost', 'vagrant'),
 
    ('web-parameters-optional_local@localhost', 'user'),
 
    ('web-parameters-paste-req@localhost', 'vagrant'),
 
])
 
def test_mail_forwarding(host, original_destination, expected_destination_user):
 
    """
 
    Tests if mail forwarding works as expected.
 
    """
 

	
 
    hostname = host.run('hostname').stdout
 

	
 
    send = host.run('swaks --suppress-data --to ' + original_destination)
 
    assert send.rc == 0
 
    message_id = re.search('Ok: queued as (.*)', send.stdout).group(1)
 

	
 
    # Sleep for a couple of seconds so the mail can get delivered.
 
    time.sleep(5)
 

	
 
    with host.sudo():
 
        mail_log = host.file('/var/log/mail.log')
 

	
 
        # First extract message ID of forwarded mail.
 
        pattern = "%s: to=<%s>.*status=sent \(forwarded as ([^)]*)\)" % (message_id, original_destination)
 
        message_id = re.search(pattern, mail_log.content).group(1)
 

	
 
        # Now try to determine where the forward ended-up at.
 
        pattern = "%s: to=<%s@%s>, orig_to=<%s>.*status=sent" % (message_id, expected_destination_user, hostname, original_destination)
 
        assert re.search(pattern, mail_log.content) is not None
 

	
 

	
 
@pytest.mark.parametrize("virtualenv_dir, expected_owner, expected_group", [
 
    ('/var/www/parameters-mandatory/virtualenv', 'admin-parameters-mandatory', 'web-parameters-mandatory'),
 
    ('/var/www/parameters-optional.local/virtualenv', 'admin-parameters-optional_local', 'web-parameters-optional_local'),
 
    ('/var/www/parameters-paste-req/virtualenv', 'admin-parameters-paste-req', 'web-parameters-paste-req'),
 
])
 
def test_python_virtualenv_created(host, virtualenv_dir, expected_owner, expected_group):
 
    """
 
    Tests if Python virtual environment has been created correctly.
 
    """
 

	
 
    with host.sudo():
 

	
 
        virtualenv = host.file(virtualenv_dir)
 
        assert virtualenv.is_directory
 
        assert virtualenv.user == expected_owner
 
        assert virtualenv.group == expected_group
 
        assert virtualenv.mode == 0o2750
 

	
 
        virtualenv_activate = host.file(virtualenv_dir + "/bin/activate")
 
        assert virtualenv_activate.is_file
 
        assert virtualenv_activate.user == expected_owner
 
        assert virtualenv_activate.group == expected_group
 
        assert virtualenv_activate.mode == 0o644
 

	
 

	
 
@pytest.mark.parametrize("project_file, expected_owner, expected_group", [
 
    ('/var/www/parameters-mandatory/virtualenv/.project', 'admin-parameters-mandatory', 'web-parameters-mandatory'),
 
    ('/var/www/parameters-optional.local/virtualenv/.project', 'admin-parameters-optional_local', 'web-parameters-optional_local'), 
 
    ('/var/www/parameters-paste-req/virtualenv/.project', 'admin-parameters-paste-req', 'web-parameters-paste-req'),
 
])
 
def test_python_virtualenv_project_directory_config(host, project_file, expected_owner, expected_group):
 
    """
 
    Tests if project directory configuration within virtualenv is set-up
 
    correctly.
 
    """
 

	
 
    with host.sudo():
 

	
 
        project = host.file(project_file)
 

	
 
        assert project.is_file
 
        assert project.user == expected_owner
 
        assert project.group == expected_group
 
        assert project.mode == 0o640
 

	
 

	
 
@pytest.mark.parametrize("wrapper_script, expected_owner, expected_group", [
 
    ('/var/www/parameters-mandatory/virtualenv/bin/exec', 'admin-parameters-mandatory', 'web-parameters-mandatory'),
 
    ('/var/www/parameters-optional.local/virtualenv/bin/exec', 'admin-parameters-optional_local', 'web-parameters-optional_local'),
 
    ('/var/www/parameters-paste-req/virtualenv/bin/exec', 'admin-parameters-paste-req', 'web-parameters-paste-req'),
 
])
 
def test_python_virtualenv_wrapper_script(host, wrapper_script, expected_owner, expected_group):
 
    """
 
    Tests if Python virtualenv wrapper script is functioning correctly.
 
    """
 

	
 
    with host.sudo():
 

	
 
        wrapper = host.file(wrapper_script)
 
        assert wrapper.is_file
 
        assert wrapper.user == expected_owner
 
        assert wrapper.group == expected_group
 
        assert wrapper.mode == 0o750
 

	
 
        command = host.run("sudo -u %s %s python -c 'import gunicorn'" % (expected_owner, wrapper_script))
 
        assert command.rc == 0
 

	
 

	
 
@pytest.mark.parametrize("admin_user, pip_path, expected_packages",  [
 
    ('admin-parameters-mandatory', '/var/www/parameters-mandatory/virtualenv/bin/pip', [
 
        "argparse==1.2.1",
 
        "futures==3.1.1",
 
        "gunicorn==19.7.1",
 
        "wsgiref==0.1.2"
 
    ]),
 
    ('admin-parameters-optional_local', '/var/www/parameters-optional.local/virtualenv/bin/pip', [
 
        "Pygments==2.2.0",
 
        "argparse==1.2.1",
 
        "dnspython==1.15.0",
 
        "docopt==0.6.2",
 
        "futures==3.1.0",
 
        "gunicorn==19.7.0",
 
        "jedi==0.12.1",
 
        "parso==0.3.1",
 
        "prompt-toolkit==1.0.15",
 
        "ptpython==0.41",
 
        "six==1.11.0",
 
        "wcwidth==0.1.7",
 
        "wsgiref==0.1.2"
 
    ]),
 
    ('admin-parameters-paste-req', '/var/www/parameters-paste-req/virtualenv/bin/pip', [
 
        "Flask==0.12.2",
 
        "Jinja2==2.9.6",
 
        "MarkupSafe==1.0",
 
        "Paste==2.0.3",
 
        "PasteDeploy==1.5.2",
 
        "PasteScript==2.0.2",
 
        "Werkzeug==0.12.2",
 
        "argparse==1.2.1",
 
        "click==6.7",
 
        "futures==3.1.0",
 
        "gunicorn==19.7.0",
 
        "itsdangerous==0.24",
 
        "six==1.10.0",
 
        "wsgiref==0.1.2",
 
    ]),
 
])
 
def test_virtualenv_packages(host, admin_user, pip_path, expected_packages):
 
    """
 
    Tests if correct packages are installed in virtualenv.
 
    """
 

	
 
    packages = host.run("sudo -u %s %s freeze" % (admin_user, pip_path))
 

	
 
    # Normalise package names and order.
 
    expected_packages = sorted([p.lower() for p in expected_packages])
 
    actual_packages = sorted(packages.stdout.lower().split("\n"))
 

	
 
    assert actual_packages == expected_packages
 

	
 

	
 
@pytest.mark.parametrize('check_config_dir', [
 
    '/etc/pip_check_requirements_upgrades/parameters-mandatory',
 
    '/etc/pip_check_requirements_upgrades/parameters-optional.local',
 
])
 
def test_wsgi_requirements_upgrade_checks(host, check_config_dir):
 
    """
 
    Tests if Python requirements files for upgrade checks are set-up
 
    correctly (absent when not configured).
 
    """
 

	
 
    with host.sudo():
 
        directory = host.file(check_config_dir)
 
        assert not directory.exists
 

	
 

	
 
@pytest.mark.parametrize("config_file, expected_website_name, expected_socket_file_path", [
 
    ('/etc/systemd/system/parameters-mandatory.socket', 'parameters-mandatory', '/run/wsgi/parameters-mandatory.sock'),
 
    ('/etc/systemd/system/parameters-optional.local.socket', 'parameters-optional.local', '/run/wsgi/parameters-optional.local.sock'),
 
    ('/etc/systemd/system/parameters-paste-req.socket', 'parameters-paste-req', '/run/wsgi/parameters-paste-req.sock'),
 
])
 
def test_systemd_socket_configuration_file(host, config_file, expected_website_name, expected_socket_file_path):
 
    """
 
    Tests if systemd socket configuration file has been set-up correctly.
 
    """
 

	
 
    config = host.file(config_file)
 
    assert config.is_file
 
    assert config.user == 'root'
 
    assert config.group == 'root'
 
    assert config.mode == 0o644
 
    assert "Socket for website %s" % expected_website_name in config.content
 
    assert "ListenStream=%s" % expected_socket_file_path in config.content
 

	
 

	
 
@pytest.mark.parametrize("socket_file_path", [
 
    '/run/wsgi/parameters-mandatory.sock',
 
    '/run/wsgi/parameters-optional.local.sock',
 
    '/run/wsgi/parameters-paste-req.sock',
 
])
 
def test_systemd_socket(host, socket_file_path):
 
    """
 
    Tests if systemd socket has correct permissions and is available.
 
    """
 

	
 
    with host.sudo():
 

	
 
        socket_file = host.file(socket_file_path)
 
        assert socket_file.is_socket
 
        assert socket_file.user == 'www-data'
 
        assert socket_file.group == 'www-data'
 
        assert socket_file.mode == 0o660
 

	
 
        socket = host.socket("unix://%s" % socket_file_path)
 
        assert socket.is_listening
 

	
 

	
 
@pytest.mark.parametrize("service_file, expected_fqdn", [
 
    ('/etc/systemd/system/parameters-mandatory.service', 'parameters-mandatory'),
 
    ('/etc/systemd/system/parameters-optional.local.service', 'parameters-optional.local'),
 
    ('/etc/systemd/system/parameters-paste-req.service', 'parameters-paste-req'),
 
])
 
def test_systemd_service_configuration_file(host, service_file, expected_fqdn):
 
    """
 
    Tests if systemd service configuration file has been set-up correctly.
 
    """
 

	
 
    config = host.file(service_file)
 
    assert config.is_file
 
    assert config.user == 'root'
 
    assert config.group == 'root'
 
    assert config.mode == 0o644
 
    assert expected_fqdn in config.content
 

	
 

	
 
@pytest.mark.parametrize("service_name", [
 
    'parameters-mandatory',
 
    'parameters-optional.local',
 
    'parameters-paste-req',
 
])
 
def test_systemd_service(host, service_name):
 
    """
 
    Tests if the systemd service is enabled at boot and running.
 
    """
 

	
 
    service = host.service(service_name)
 

	
 
    assert service.is_enabled
 
    assert service.is_running
 

	
 

	
 
@pytest.mark.parametrize("directory_path, expected_owner, expected_group", [
 
    ('/var/www/parameters-mandatory/htdocs', 'admin-parameters-mandatory', 'web-parameters-mandatory'),
 
    ('/var/www/parameters-optional.local/htdocs', 'admin-parameters-optional_local', 'web-parameters-optional_local'),
 
    ('/var/www/parameters-paste-req/htdocs', 'admin-parameters-paste-req', 'web-parameters-paste-req'),
 
])
 
def test_static_file_directory(host, directory_path, expected_owner, expected_group):
 
    """
 
    Tests if directory for serving static files has been created correctly.
 
    """
 

	
 
    with host.sudo():
 

	
 
        directory = host.file(directory_path)
 
        assert directory.is_directory
 
        assert directory.user == expected_owner
 
        assert directory.group == expected_group
 
        assert directory.mode == 0o2750
 

	
 

	
 
@pytest.mark.parametrize("private_key_path, certificate_path, expected_private_key, expected_certificate", [
 
    ('/etc/ssl/private/parameters-mandatory_https.key', '/etc/ssl/certs/parameters-mandatory_https.pem',
 
     'tests/data/x509/parameters-mandatory_https.key', 'tests/data/x509/parameters-mandatory_https.pem'),
 
    ('/etc/ssl/private/parameters-optional.local_https.key', '/etc/ssl/certs/parameters-optional.local_https.pem',
 
     'tests/data/x509/parameters-optional.local_https.key.pem', 'tests/data/x509/parameters-optional.local_https.cert.pem'),
 
    ('/etc/ssl/private/parameters-paste-req_https.key', '/etc/ssl/certs/parameters-paste-req_https.pem',
 
     'tests/data/x509/parameters-paste-req_https.key.pem', 'tests/data/x509/parameters-paste-req_https.cert.pem'),
 
])
 
def test_nginx_tls_files(host, private_key_path, certificate_path, expected_private_key, expected_certificate):
 
    """
 
    Tests if TLS private key and certificate have been deployed correctly.
 
    """
 

	
 
    with host.sudo():
 

	
 
        tls_file = host.file(private_key_path)
 
        assert tls_file.is_file
 
        assert tls_file.user == 'root'
 
        assert tls_file.group == 'root'
 
        assert tls_file.mode == 0o640
 
        assert tls_file.content == open(expected_private_key, "r").read().rstrip()
 

	
 
        tls_file = host.file(certificate_path)
 
        assert tls_file.is_file
 
        assert tls_file.user == 'root'
 
        assert tls_file.group == 'root'
 
        assert tls_file.mode == 0o644
 
        assert tls_file.content == open(expected_certificate, "r").read().rstrip()
 

	
 

	
 
@pytest.mark.parametrize("config_file_path, expected_content", [
 
    ('/etc/check_certificate/parameters-mandatory_https.conf', '/etc/ssl/certs/parameters-mandatory_https.pem'),
 
    ('/etc/check_certificate/parameters-optional.local_https.conf', '/etc/ssl/certs/parameters-optional.local_https.pem'),
 
    ('/etc/check_certificate/parameters-paste-req_https.conf', '/etc/ssl/certs/parameters-paste-req_https.pem'),
 
])
 
def test_certificate_validity_check_configuration(host, config_file_path, expected_content):
 
    """
 
    Tests if certificate validity check configuration file has been deployed
 
    correctly.
 
    """
 

	
 
    config = host.file(config_file_path)
 
    assert config.is_file
 
    assert config.user == 'root'
 
    assert config.group == 'root'
 
    assert config.mode == 0o644
 
    assert config.content == expected_content
 

	
 

	
 
@pytest.mark.parametrize("config_file", [
 
    '/etc/nginx/sites-available/parameters-mandatory',
 
    '/etc/nginx/sites-available/parameters-optional.local',
 
    '/etc/nginx/sites-available/parameters-paste-req',
 
])
 
def test_vhost_file(host, config_file):
 
    """
 
    Tests permissions of vhost configuration file.
 
    """
 

	
 
    config = host.file(config_file)
 

	
 
    assert config.is_file
 
    assert config.user == 'root'
 
    assert config.group == 'root'
 
    assert config.mode == 0o640
 

	
 

	
 
@pytest.mark.parametrize("config_file, expected_content", [
 
    ('/etc/nginx/sites-enabled/parameters-mandatory', '/etc/nginx/sites-available/parameters-mandatory'),
 
    ('/etc/nginx/sites-enabled/parameters-optional.local', '/etc/nginx/sites-available/parameters-optional.local'),
 
    ('/etc/nginx/sites-enabled/parameters-paste-req', '/etc/nginx/sites-available/parameters-paste-req'),
 
])
 
def test_website_enabled(host, config_file, expected_content):
 
    """
 
    Tests if website has been enabled.
 
    """
 

	
 
    config = host.file(config_file)
 

	
 
    assert config.is_symlink
 
    assert config.linked_to == expected_content
roles/wsgi_website/molecule/default/tests/test_parameters_mandatory.py
Show inline comments
 
@@ -6,381 +6,12 @@ import testinfra.utils.ansible_runner
 

	
 

	
 
testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner(
 
    os.environ['MOLECULE_INVENTORY_FILE']).get_hosts(['all'])
 

	
 

	
 
def test_website_group(host):
 
    """
 
    Tests if website group has been created correctly.
 
    """
 

	
 
    group = host.group('web-parameters-mandatory')
 

	
 
    assert group.exists
 
    assert group.gid == 1003
 

	
 

	
 
def test_website_admin_user(host):
 
    """
 
    Tests if website administrator user has been created correctly.
 
    """
 

	
 
    user = host.user('admin-parameters-mandatory')
 

	
 
    assert user.exists
 
    assert user.uid == 1003
 
    assert user.group == 'web-parameters-mandatory'
 
    assert user.groups == ['web-parameters-mandatory']
 
    assert user.shell == '/bin/bash'
 
    assert user.home == '/var/www/parameters-mandatory'
 

	
 

	
 
def test_website_admin_home(host):
 
    """
 
    Tests if permissions on website admin home directory are correct.
 
    """
 

	
 
    home = host.file('/var/www/parameters-mandatory')
 

	
 
    assert home.is_directory
 
    assert home.user == 'admin-parameters-mandatory'
 
    assert home.group == 'web-parameters-mandatory'
 
    assert home.mode == 0o750
 

	
 

	
 
def test_home_profile_directory(host):
 
    """
 
    Tests if profile directory has been set-up correctly for the website
 
    administrator/application user.
 
    """
 

	
 
    with host.sudo():
 

	
 
        directory = host.file('/var/www/parameters-mandatory/.profile.d')
 
        assert directory.is_directory
 
        assert directory.user == 'admin-parameters-mandatory'
 
        assert directory.group == 'web-parameters-mandatory'
 
        assert directory.mode == 0o750
 

	
 

	
 
def test_virtualenv_profile_configuration(host):
 
    """
 
    Tests if profile configuration file for auto-activation of virtual
 
    environment has been deployed correctly.
 
    """
 

	
 
    with host.sudo():
 

	
 
        config = host.file('/var/www/parameters-mandatory/.profile.d/virtualenv.sh')
 
        assert config.is_file
 
        assert config.user == 'root'
 
        assert config.group == 'web-parameters-mandatory'
 
        assert config.mode == 0o640
 

	
 

	
 
def test_environment_profile_configuration(host):
 
    """
 
    Tests if profile configuration file for setting-up environment variables has
 
    been deployed correctly.
 
    """
 

	
 
    with host.sudo():
 

	
 
        config = host.file('/var/www/parameters-mandatory/.profile.d/environment.sh')
 
        assert config.is_file
 
        assert config.user == 'root'
 
        assert config.group == 'web-parameters-mandatory'
 
        assert config.mode == 0o640
 
        assert config.content == ""
 

	
 

	
 
def test_profile_configuration(host):
 
    """
 
    Tests if profile configuration is behaving correctly (setting appropriate
 
    vars via login shell).
 
    """
 

	
 
    env = host.run("sudo -i -u admin-parameters-mandatory printenv VIRTUAL_ENV MY_ENV_VAR")
 
    assert env.stdout == "/var/www/parameters-mandatory/virtualenv"
 

	
 

	
 
def test_website_application_user(host):
 
    """
 
    Tests if website application user has been created correctly.
 
    """
 

	
 
    user = host.user('web-parameters-mandatory')
 

	
 
    assert user.exists
 
    assert user.uid == 999
 
    assert user.group == 'web-parameters-mandatory'
 
    assert user.groups == ['web-parameters-mandatory']
 
    assert user.shell == '/bin/sh'
 
    assert user.home == '/var/www/parameters-mandatory'
 

	
 
    with host.sudo():
 
        umask = host.run("su -l web-parameters-mandatory -c 'bash -c umask'")
 
        assert umask.stdout == '0007'
 

	
 

	
 
def test_nginx_user(host):
 
    """
 
    Tests if web server user has been added to website group.
 
    """
 

	
 
    user = host.user('www-data')
 
    assert 'web-parameters-mandatory' in user.groups
 

	
 

	
 
def test_forward_file(host):
 
    """
 
    Tests if the forward file has correct permissions and content.
 
    """
 

	
 
    with host.sudo():
 

	
 
        config = host.file('/var/www/parameters-mandatory/.forward')
 
        assert config.is_file
 
        assert config.user == 'root'
 
        assert config.group == 'web-parameters-mandatory'
 
        assert config.mode == 0o640
 
        assert config.content == "root"
 

	
 

	
 
def test_mail_forwarding(host):
 
    """
 
    Tests if mail forwarding works as expected.
 
    """
 

	
 
    hostname = host.run('hostname').stdout
 

	
 
    send = host.run('swaks --suppress-data --to web-parameters-mandatory@localhost')
 
    assert send.rc == 0
 
    message_id = re.search('Ok: queued as (.*)', send.stdout).group(1)
 

	
 
    # Sleep for a couple of seconds so the mail can get delivered.
 
    time.sleep(5)
 

	
 
    with host.sudo():
 
        mail_log = host.file('/var/log/mail.log')
 

	
 
        # First extract message ID of forwarded mail.
 
        pattern = "%s: to=<web-parameters-mandatory@localhost>.*status=sent \(forwarded as ([^)]*)\)" % message_id
 
        message_id = re.search(pattern, mail_log.content).group(1)
 

	
 
        # Now try to determine where the forward ended-up at.
 
        pattern = "%s: to=<vagrant@%s>, orig_to=<web-parameters-mandatory@localhost>.*status=sent" % (message_id, hostname)
 
        assert re.search(pattern, mail_log.content) is not None
 

	
 

	
 
def test_python_virtualenv_created(host):
 
    """
 
    Tests if Python virtual environment has been created correctly.
 
    """
 

	
 
    with host.sudo():
 

	
 
        virtualenv = host.file("/var/www/parameters-mandatory/virtualenv")
 
        assert virtualenv.is_directory
 
        assert virtualenv.user == 'admin-parameters-mandatory'
 
        assert virtualenv.group == 'web-parameters-mandatory'
 
        assert virtualenv.mode == 0o2750
 

	
 
        virtualenv_activate = host.file("/var/www/parameters-mandatory/virtualenv/bin/activate")
 
        assert virtualenv_activate.is_file
 
        assert virtualenv_activate.user == 'admin-parameters-mandatory'
 
        assert virtualenv_activate.group == 'web-parameters-mandatory'
 
        assert virtualenv_activate.mode == 0o644
 

	
 

	
 
def test_python_virtualenv_project_directory_config(host):
 
    """
 
    Tests if project directory configuration within virtualenv is set-up
 
    correctly.
 
    """
 

	
 
    with host.sudo():
 

	
 
        project = host.file("/var/www/parameters-mandatory/virtualenv/.project")
 

	
 
        assert project.is_file
 
        assert project.user == 'admin-parameters-mandatory'
 
        assert project.group == 'web-parameters-mandatory'
 
        assert project.mode == 0o640
 

	
 

	
 
def test_python_virtualenv_wrapper_script(host):
 
    """
 
    Tests if Python virtualenv wrapper script is functioning correctly.
 
    """
 

	
 
    with host.sudo():
 

	
 
        wrapper = host.file("/var/www/parameters-mandatory/virtualenv/bin/exec")
 
        assert wrapper.is_file
 
        assert wrapper.user == 'admin-parameters-mandatory'
 
        assert wrapper.group == 'web-parameters-mandatory'
 
        assert wrapper.mode == 0o750
 

	
 
        command = host.run("sudo -u admin-parameters-mandatory /var/www/parameters-mandatory/virtualenv/bin/exec python -c 'import gunicorn'")
 
        assert command.rc == 0
 

	
 

	
 
def test_virtualenv_packages(host):
 
    """
 
    Tests if correct packages are installed in virtualenv.
 
    """
 

	
 
    packages = host.run("sudo -u admin-parameters-mandatory /var/www/parameters-mandatory/virtualenv/bin/pip freeze")
 

	
 
    assert sorted(packages.stdout.lower().split("\n")) == sorted("""argparse==1.2.1
 
futures==3.1.1
 
gunicorn==19.7.1
 
wsgiref==0.1.2""".lower().split("\n"))
 

	
 

	
 
def test_wsgi_requirements_upgrade_checks(host):
 
    """
 
    Tests if Python requirements files for upgrade checks are set-up correctly.
 
    """
 

	
 
    with host.sudo():
 
        directory = host.file('/etc/pip_check_requirements_upgrades/parameters-mandatory')
 
        assert not directory.exists
 

	
 

	
 
def test_systemd_socket_configuration_file(host):
 
    """
 
    Tests if systemd socket configuration file has been set-up correctly.
 
    """
 

	
 
    config = host.file("/etc/systemd/system/parameters-mandatory.socket")
 
    assert config.is_file
 
    assert config.user == 'root'
 
    assert config.group == 'root'
 
    assert config.mode == 0o644
 
    assert "Socket for website parameters-mandatory" in config.content
 
    assert "ListenStream=/run/wsgi/parameters-mandatory.sock" in config.content
 

	
 

	
 
def test_systemd_socket(host):
 
    """
 
    Tests if systemd socket has correct permissions and is available.
 
    """
 

	
 
    with host.sudo():
 

	
 
        socket_file = host.file("/run/wsgi/parameters-mandatory.sock")
 
        assert socket_file.is_socket
 
        assert socket_file.user == 'www-data'
 
        assert socket_file.group == 'www-data'
 
        assert socket_file.mode == 0o660
 

	
 
        socket = host.socket("unix:///run/wsgi/parameters-mandatory.sock")
 
        assert socket.is_listening
 

	
 

	
 
def test_systemd_service_configuration_file(host):
 
    """
 
    Tests if systemd service configuration file has been set-up correctly.
 
    """
 

	
 
    config = host.file("/etc/systemd/system/parameters-mandatory.service")
 
    assert config.is_file
 
    assert config.user == 'root'
 
    assert config.group == 'root'
 
    assert config.mode == 0o644
 
    assert "parameters-mandatory" in config.content
 

	
 

	
 
def test_systemd_service(host):
 
    """
 
    Tests if the systemd service is enabled at boot and running.
 
    """
 

	
 
    service = host.service('parameters-mandatory')
 

	
 
    assert service.is_enabled
 
    assert service.is_running
 

	
 

	
 
def test_static_file_directory(host):
 
    """
 
    Tests if directory for serving static files has been created correctly.
 
    """
 

	
 
    with host.sudo():
 

	
 
        directory = host.file('/var/www/parameters-mandatory/htdocs')
 
        assert directory.is_directory
 
        assert directory.user == 'admin-parameters-mandatory'
 
        assert directory.group == 'web-parameters-mandatory'
 
        assert directory.mode == 0o2750
 

	
 

	
 
def test_nginx_tls_files(host):
 
    """
 
    Tests if TLS private key and certificate have been deployed correctly.
 
    """
 

	
 
    with host.sudo():
 

	
 
        tls_file = host.file('/etc/ssl/private/parameters-mandatory_https.key')
 
        assert tls_file.is_file
 
        assert tls_file.user == 'root'
 
        assert tls_file.group == 'root'
 
        assert tls_file.mode == 0o640
 
        assert tls_file.content == open("tests/data/x509/parameters-mandatory_https.key", "r").read().rstrip()
 

	
 
        tls_file = host.file('/etc/ssl/certs/parameters-mandatory_https.pem')
 
        assert tls_file.is_file
 
        assert tls_file.user == 'root'
 
        assert tls_file.group == 'root'
 
        assert tls_file.mode == 0o644
 
        assert tls_file.content == open("tests/data/x509/parameters-mandatory_https.pem", "r").read().rstrip()
 

	
 

	
 
def test_certificate_validity_check_configuration(host):
 
    """
 
    Tests if certificate validity check configuration file has been deployed
 
    correctly.
 
    """
 

	
 
    config = host.file('/etc/check_certificate/parameters-mandatory_https.conf')
 
    assert config.is_file
 
    assert config.user == 'root'
 
    assert config.group == 'root'
 
    assert config.mode == 0o644
 
    assert config.content == "/etc/ssl/certs/parameters-mandatory_https.pem"
 

	
 

	
 
def test_vhost_file(host):
 
    """
 
    Tests permissions of vhost configuration file.
 
    """
 

	
 
    config = host.file('/etc/nginx/sites-available/parameters-mandatory')
 

	
 
    assert config.is_file
 
    assert config.user == 'root'
 
    assert config.group == 'root'
 
    assert config.mode == 0o640
 

	
 

	
 
def test_website_enabled(host):
 
    """
 
    Tests if website has been enabled.
 
    """
 

	
 
    config = host.file('/etc/nginx/sites-enabled/parameters-mandatory')
 

	
 
    assert config.is_symlink
 
    assert config.linked_to == '/etc/nginx/sites-available/parameters-mandatory'
 

	
 

	
 
def test_https_enforcement(host):
 
    """
 
    Tests if HTTPS is being enforced.
 
    """
 

	
 
    https_enforcement = host.run('curl -I http://parameters-mandatory/')
roles/wsgi_website/molecule/default/tests/test_parameters_optional.py
Show inline comments
 
@@ -6,176 +6,12 @@ import testinfra.utils.ansible_runner
 

	
 

	
 
testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner(
 
    os.environ['MOLECULE_INVENTORY_FILE']).get_hosts(['all'])
 

	
 

	
 
def test_website_group(host):
 
    """
 
    Tests if website group has been created correctly.
 
    """
 

	
 
    group = host.group('web-parameters-optional_local')
 

	
 
    assert group.exists
 
    assert group.gid == 5001
 

	
 

	
 
def test_website_admin_user(host):
 
    """
 
    Tests if website administrator user has been created correctly.
 
    """
 

	
 
    user = host.user('admin-parameters-optional_local')
 

	
 
    assert user.exists
 
    assert user.uid == 5000
 
    assert user.group == 'web-parameters-optional_local'
 
    assert user.groups == ['web-parameters-optional_local']
 
    assert user.shell == '/bin/bash'
 
    assert user.home == '/var/www/parameters-optional.local'
 

	
 

	
 
def test_website_admin_home(host):
 
    """
 
    Tests if permissions on website admin home directory are correct.
 
    """
 

	
 
    home = host.file('/var/www/parameters-optional.local')
 

	
 
    assert home.is_directory
 
    assert home.user == 'admin-parameters-optional_local'
 
    assert home.group == 'web-parameters-optional_local'
 
    assert home.mode == 0o750
 

	
 

	
 
def test_home_profile_directory(host):
 
    """
 
    Tests if profile directory has been set-up correctly for the website
 
    administrator/application user.
 
    """
 

	
 
    with host.sudo():
 

	
 
        directory = host.file('/var/www/parameters-optional.local/.profile.d')
 
        assert directory.is_directory
 
        assert directory.user == 'admin-parameters-optional_local'
 
        assert directory.group == 'web-parameters-optional_local'
 
        assert directory.mode == 0o750
 

	
 

	
 
def test_virtualenv_profile_configuration(host):
 
    """
 
    Tests if profile configuration file for auto-activation of virtual
 
    environment has been deployed correctly.
 
    """
 

	
 
    with host.sudo():
 

	
 
        config = host.file('/var/www/parameters-optional.local/.profile.d/virtualenv.sh')
 
        assert config.is_file
 
        assert config.user == 'root'
 
        assert config.group == 'web-parameters-optional_local'
 
        assert config.mode == 0o640
 

	
 

	
 
def test_environment_profile_configuration(host):
 
    """
 
    Tests if profile configuration file for setting-up environment variables has
 
    been deployed correctly.
 
    """
 

	
 
    with host.sudo():
 

	
 
        config = host.file('/var/www/parameters-optional.local/.profile.d/environment.sh')
 
        assert config.is_file
 
        assert config.user == 'root'
 
        assert config.group == 'web-parameters-optional_local'
 
        assert config.mode == 0o640
 
        assert config.content == "export MY_ENV_VAR='My environment variable'"
 

	
 

	
 
def test_profile_configuration(host):
 
    """
 
    Tests if profile configuration is behaving correctly (setting appropriate
 
    vars via login shell).
 
    """
 

	
 
    env = host.run("sudo -i -u admin-parameters-optional_local printenv VIRTUAL_ENV MY_ENV_VAR")
 
    assert env.stdout == "/var/www/parameters-optional.local/virtualenv\nMy environment variable"
 

	
 

	
 
def test_website_application_user(host):
 
    """
 
    Tests if website application user has been created correctly.
 
    """
 

	
 
    user = host.user('web-parameters-optional_local')
 

	
 
    assert user.exists
 
    assert user.uid == 5001
 
    assert user.group == 'web-parameters-optional_local'
 
    assert user.groups == ['web-parameters-optional_local']
 
    assert user.shell == '/bin/sh'
 
    assert user.home == '/var/www/parameters-optional.local'
 

	
 
    with host.sudo():
 
        umask = host.run("su -l web-parameters-optional_local -c 'bash -c umask'")
 
        assert umask.stdout == '0007'
 

	
 

	
 
def test_nginx_user(host):
 
    """
 
    Tests if web server user has been added to website group.
 
    """
 

	
 
    user = host.user('www-data')
 
    assert 'web-parameters-optional_local' in user.groups
 

	
 

	
 
def test_forward_file(host):
 
    """
 
    Tests if the forward file has correct permissions and content.
 
    """
 

	
 
    with host.sudo():
 

	
 
        config = host.file('/var/www/parameters-optional.local/.forward')
 
        assert config.is_file
 
        assert config.user == 'root'
 
        assert config.group == 'web-parameters-optional_local'
 
        assert config.mode == 0o640
 
        assert config.content == "user"
 

	
 

	
 
def test_mail_forwarding(host):
 
    """
 
    Tests if mail forwarding works as expected.
 
    """
 

	
 
    hostname = host.run('hostname').stdout
 

	
 
    send = host.run('swaks --suppress-data --to web-parameters-optional_local@localhost')
 
    assert send.rc == 0
 
    message_id = re.search('Ok: queued as (.*)', send.stdout).group(1)
 

	
 
    # Sleep for a couple of seconds so the mail can get delivered.
 
    time.sleep(5)
 

	
 
    with host.sudo():
 
        mail_log = host.file('/var/log/mail.log')
 

	
 
        # First extract message ID of forwarded mail.
 
        pattern = "%s: to=<web-parameters-optional_local@localhost>.*status=sent \(forwarded as ([^)]*)\)" % message_id
 
        message_id = re.search(pattern, mail_log.content).group(1)
 

	
 
        # Now try to determine where the forward ended-up at.
 
        pattern = "%s: to=<user@%s>, orig_to=<web-parameters-optional_local@localhost>.*status=sent" % (message_id, hostname)
 
        assert re.search(pattern, mail_log.content) is not None
 

	
 

	
 
def test_installed_packages(host):
 
    """
 
    Tests if additional packages are installed.
 
    """
 

	
 
    assert host.package('libmariadb-client-lgpl-dev-compat').is_installed
 
@@ -190,226 +26,12 @@ def test_mariadb_compat_symlink(host):
 

	
 
    link = host.file('/usr/bin/mysql_config')
 
    assert link.is_symlink
 
    assert link.linked_to == "/usr/bin/mariadb_config"
 

	
 

	
 
def test_python_virtualenv_created(host):
 
    """
 
    Tests if Python virtual environment has been created correctly.
 
    """
 

	
 
    with host.sudo():
 

	
 
        virtualenv = host.file("/var/www/parameters-optional.local/virtualenv")
 
        assert virtualenv.is_directory
 
        assert virtualenv.user == 'admin-parameters-optional_local'
 
        assert virtualenv.group == 'web-parameters-optional_local'
 
        assert virtualenv.mode == 0o2750
 

	
 
        virtualenv_activate = host.file("/var/www/parameters-optional.local/virtualenv/bin/activate")
 
        assert virtualenv_activate.is_file
 
        assert virtualenv_activate.user == 'admin-parameters-optional_local'
 
        assert virtualenv_activate.group == 'web-parameters-optional_local'
 
        assert virtualenv_activate.mode == 0o644
 

	
 

	
 
def test_python_virtualenv_project_directory_config(host):
 
    """
 
    Tests if project directory configuration within virtualenv is set-up
 
    correctly.
 
    """
 

	
 
    with host.sudo():
 

	
 
        project = host.file("/var/www/parameters-optional.local/virtualenv/.project")
 

	
 
        assert project.is_file
 
        assert project.user == 'admin-parameters-optional_local'
 
        assert project.group == 'web-parameters-optional_local'
 
        assert project.mode == 0o640
 

	
 

	
 
def test_python_virtualenv_wrapper_script(host):
 
    """
 
    Tests if Python virtualenv wrapper script is functioning correctly.
 
    """
 

	
 
    with host.sudo():
 

	
 
        wrapper = host.file("/var/www/parameters-optional.local/virtualenv/bin/exec")
 
        assert wrapper.is_file
 
        assert wrapper.user == 'admin-parameters-optional_local'
 
        assert wrapper.group == 'web-parameters-optional_local'
 
        assert wrapper.mode == 0o750
 

	
 
        command = host.run("sudo -u admin-parameters-optional_local /var/www/parameters-optional.local/virtualenv/bin/exec python -c 'import gunicorn'")
 
        assert command.rc == 0
 

	
 

	
 
def test_virtualenv_packages(host):
 
    """
 
    Tests if correct packages are installed in virtualenv.
 
    """
 

	
 
    packages = host.run("sudo -u admin-parameters-optional_local /var/www/parameters-optional.local/virtualenv/bin/pip freeze")
 

	
 
    assert sorted(packages.stdout.lower().split("\n")) == sorted("""Pygments==2.2.0
 
argparse==1.2.1
 
dnspython==1.15.0
 
docopt==0.6.2
 
futures==3.1.0
 
gunicorn==19.7.0
 
jedi==0.12.0
 
parso==0.2.1
 
prompt-toolkit==1.0.15
 
ptpython==0.41
 
six==1.11.0
 
wcwidth==0.1.7
 
wsgiref==0.1.2""".lower().split("\n"))
 

	
 

	
 
def test_wsgi_requirements_upgrade_checks(host):
 
    """
 
    Tests if Python requirements files for upgrade checks are set-up correctly.
 
    """
 

	
 
    with host.sudo():
 
        directory = host.file('/etc/pip_check_requirements_upgrades/parameters-optional.local')
 
        assert not directory.exists
 

	
 

	
 
def test_systemd_socket_configuration_file(host):
 
    """
 
    Tests if systemd socket configuration file has been set-up correctly.
 
    """
 

	
 
    config = host.file("/etc/systemd/system/parameters-optional.local.socket")
 
    assert config.is_file
 
    assert config.user == 'root'
 
    assert config.group == 'root'
 
    assert config.mode == 0o644
 
    assert "Socket for website parameters-optional.local" in config.content
 
    assert "ListenStream=/run/wsgi/parameters-optional.local.sock" in config.content
 

	
 

	
 
def test_systemd_socket(host):
 
    """
 
    Tests if systemd socket has correct permissions and is available.
 
    """
 

	
 
    with host.sudo():
 

	
 
        socket_file = host.file("/run/wsgi/parameters-optional.local.sock")
 
        assert socket_file.is_socket
 
        assert socket_file.user == 'www-data'
 
        assert socket_file.group == 'www-data'
 
        assert socket_file.mode == 0o660
 

	
 
        socket = host.socket("unix:///run/wsgi/parameters-optional.local.sock")
 
        assert socket.is_listening
 

	
 

	
 
def test_systemd_service_configuration_file(host):
 
    """
 
    Tests if systemd service configuration file has been set-up correctly.
 
    """
 

	
 
    config = host.file("/etc/systemd/system/parameters-optional.local.service")
 
    assert config.is_file
 
    assert config.user == 'root'
 
    assert config.group == 'root'
 
    assert config.mode == 0o644
 
    assert "parameters-optional.local" in config.content
 

	
 

	
 
def test_systemd_service(host):
 
    """
 
    Tests if the systemd service is enabled at boot and running.
 
    """
 

	
 
    service = host.service('parameters-optional.local')
 

	
 
    assert service.is_enabled
 
    assert service.is_running
 

	
 

	
 
def test_static_file_directory(host):
 
    """
 
    Tests if directory for serving static files has been created correctly.
 
    """
 

	
 
    with host.sudo():
 

	
 
        directory = host.file('/var/www/parameters-optional.local/htdocs')
 
        assert directory.is_directory
 
        assert directory.user == 'admin-parameters-optional_local'
 
        assert directory.group == 'web-parameters-optional_local'
 
        assert directory.mode == 0o2750
 

	
 

	
 
def test_nginx_tls_files(host):
 
    """
 
    Tests if TLS private key and certificate have been deployed correctly.
 
    """
 

	
 
    with host.sudo():
 

	
 
        tls_file = host.file('/etc/ssl/private/parameters-optional.local_https.key')
 
        assert tls_file.is_file
 
        assert tls_file.user == 'root'
 
        assert tls_file.group == 'root'
 
        assert tls_file.mode == 0o640
 
        assert tls_file.content == open("tests/data/x509/parameters-optional.local_https.key.pem", "r").read().rstrip()
 

	
 
        tls_file = host.file('/etc/ssl/certs/parameters-optional.local_https.pem')
 
        assert tls_file.is_file
 
        assert tls_file.user == 'root'
 
        assert tls_file.group == 'root'
 
        assert tls_file.mode == 0o644
 
        assert tls_file.content == open("tests/data/x509/parameters-optional.local_https.cert.pem", "r").read().rstrip()
 

	
 

	
 
def test_certificate_validity_check_configuration(host):
 
    """
 
    Tests if certificate validity check configuration file has been deployed
 
    correctly.
 
    """
 

	
 
    config = host.file('/etc/check_certificate/parameters-optional.local_https.conf')
 
    assert config.is_file
 
    assert config.user == 'root'
 
    assert config.group == 'root'
 
    assert config.mode == 0o644
 
    assert config.content == "/etc/ssl/certs/parameters-optional.local_https.pem"
 

	
 

	
 
def test_vhost_file(host):
 
    """
 
    Tests permissions of vhost configuration file.
 
    """
 

	
 
    config = host.file('/etc/nginx/sites-available/parameters-optional.local')
 

	
 
    assert config.is_file
 
    assert config.user == 'root'
 
    assert config.group == 'root'
 
    assert config.mode == 0o640
 

	
 

	
 
def test_website_enabled(host):
 
    """
 
    Tests if website has been enabled.
 
    """
 

	
 
    config = host.file('/etc/nginx/sites-enabled/parameters-optional.local')
 

	
 
    assert config.is_symlink
 
    assert config.linked_to == '/etc/nginx/sites-available/parameters-optional.local'
 

	
 

	
 
def test_https_enforcement(host):
 
    """
 
    Tests if HTTPS is (not) being enforced.
 
    """
 

	
 
    https_enforcement = host.run('curl -I http://parameters-optional.local/')
roles/wsgi_website/molecule/default/tests/test_parameters_paste_req.py
Show inline comments
 
@@ -6,236 +6,12 @@ import testinfra.utils.ansible_runner
 

	
 

	
 
testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner(
 
    os.environ['MOLECULE_INVENTORY_FILE']).get_hosts(['all'])
 

	
 

	
 
def test_website_group(host):
 
    """
 
    Tests if website group has been created correctly.
 
    """
 

	
 
    group = host.group('web-parameters-paste-req')
 

	
 
    assert group.exists
 
    assert group.gid == 5002
 

	
 

	
 
def test_website_admin_user(host):
 
    """
 
    Tests if website administrator user has been created correctly.
 
    """
 

	
 
    user = host.user('admin-parameters-paste-req')
 

	
 
    assert user.exists
 
    assert user.uid == 5002
 
    assert user.group == 'web-parameters-paste-req'
 
    assert user.groups == ['web-parameters-paste-req']
 
    assert user.shell == '/bin/bash'
 
    assert user.home == '/var/www/parameters-paste-req'
 

	
 

	
 
def test_website_admin_home(host):
 
    """
 
    Tests if permissions on website admin home directory are correct.
 
    """
 

	
 
    home = host.file('/var/www/parameters-paste-req')
 

	
 
    assert home.is_directory
 
    assert home.user == 'admin-parameters-paste-req'
 
    assert home.group == 'web-parameters-paste-req'
 
    assert home.mode == 0o750
 

	
 

	
 
def test_home_profile_directory(host):
 
    """
 
    Tests if profile directory has been set-up correctly for the website
 
    administrator/application user.
 
    """
 

	
 
    with host.sudo():
 

	
 
        directory = host.file('/var/www/parameters-paste-req/.profile.d')
 
        assert directory.is_directory
 
        assert directory.user == 'admin-parameters-paste-req'
 
        assert directory.group == 'web-parameters-paste-req'
 
        assert directory.mode == 0o750
 

	
 

	
 
def test_virtualenv_profile_configuration(host):
 
    """
 
    Tests if profile configuration file for auto-activation of virtual
 
    environment has been deployed correctly.
 
    """
 

	
 
    with host.sudo():
 

	
 
        config = host.file('/var/www/parameters-paste-req/.profile.d/virtualenv.sh')
 
        assert config.is_file
 
        assert config.user == 'root'
 
        assert config.group == 'web-parameters-paste-req'
 
        assert config.mode == 0o640
 

	
 

	
 
def test_profile_configuration(host):
 
    """
 
    Tests if profile configuration is behaving correctly (setting appropriate
 
    vars via login shell).
 
    """
 

	
 
    env = host.run("sudo -i -u admin-parameters-paste-req printenv VIRTUAL_ENV MY_ENV_VAR")
 
    assert env.stdout == "/var/www/parameters-paste-req/virtualenv"
 

	
 

	
 
def test_website_application_user(host):
 
    """
 
    Tests if website application user has been created correctly.
 
    """
 

	
 
    user = host.user('web-parameters-paste-req')
 

	
 
    assert user.exists
 
    assert user.uid == 998
 
    assert user.group == 'web-parameters-paste-req'
 
    assert user.groups == ['web-parameters-paste-req']
 
    assert user.shell == '/bin/sh'
 
    assert user.home == '/var/www/parameters-paste-req'
 

	
 
    with host.sudo():
 
        umask = host.run("su -l web-parameters-paste-req -c 'bash -c umask'")
 
        assert umask.stdout == '0007'
 

	
 

	
 
def test_nginx_user(host):
 
    """
 
    Tests if web server user has been added to website group.
 
    """
 

	
 
    user = host.user('www-data')
 
    assert 'web-parameters-paste-req' in user.groups
 

	
 

	
 
def test_forward_file(host):
 
    """
 
    Tests if the forward file has correct permissions and content.
 
    """
 

	
 
    with host.sudo():
 

	
 
        config = host.file('/var/www/parameters-paste-req/.forward')
 
        assert config.is_file
 
        assert config.user == 'root'
 
        assert config.group == 'web-parameters-paste-req'
 
        assert config.mode == 0o640
 
        assert config.content == "root"
 

	
 

	
 
def test_mail_forwarding(host):
 
    """
 
    Tests if mail forwarding works as expected.
 
    """
 

	
 
    hostname = host.run('hostname').stdout
 

	
 
    send = host.run('swaks --suppress-data --to web-parameters-paste-req@localhost')
 
    assert send.rc == 0
 
    message_id = re.search('Ok: queued as (.*)', send.stdout).group(1)
 

	
 
    # Sleep for a couple of seconds so the mail can get delivered.
 
    time.sleep(5)
 

	
 
    with host.sudo():
 
        mail_log = host.file('/var/log/mail.log')
 

	
 
        # First extract message ID of forwarded mail.
 
        pattern = "%s: to=<web-parameters-paste-req@localhost>.*status=sent \(forwarded as ([^)]*)\)" % message_id
 
        message_id = re.search(pattern, mail_log.content).group(1)
 

	
 
        # Now try to determine where the forward ended-up at.
 
        pattern = "%s: to=<vagrant@%s>, orig_to=<web-parameters-paste-req@localhost>.*status=sent" % (message_id, hostname)
 
        assert re.search(pattern, mail_log.content) is not None
 

	
 

	
 
def test_python_virtualenv_created(host):
 
    """
 
    Tests if Python virtual environment has been created correctly.
 
    """
 

	
 
    with host.sudo():
 

	
 
        virtualenv = host.file("/var/www/parameters-paste-req/virtualenv")
 
        assert virtualenv.is_directory
 
        assert virtualenv.user == 'admin-parameters-paste-req'
 
        assert virtualenv.group == 'web-parameters-paste-req'
 
        assert virtualenv.mode == 0o2750
 

	
 
        virtualenv_activate = host.file("/var/www/parameters-paste-req/virtualenv/bin/activate")
 
        assert virtualenv_activate.is_file
 
        assert virtualenv_activate.user == 'admin-parameters-paste-req'
 
        assert virtualenv_activate.group == 'web-parameters-paste-req'
 
        assert virtualenv_activate.mode == 0o644
 

	
 

	
 
def test_python_virtualenv_project_directory_config(host):
 
    """
 
    Tests if project directory configuration within virtualenv is set-up
 
    correctly.
 
    """
 

	
 
    with host.sudo():
 

	
 
        project = host.file("/var/www/parameters-paste-req/virtualenv/.project")
 

	
 
        assert project.is_file
 
        assert project.user == 'admin-parameters-paste-req'
 
        assert project.group == 'web-parameters-paste-req'
 
        assert project.mode == 0o640
 

	
 

	
 
def test_python_virtualenv_wrapper_script(host):
 
    """
 
    Tests if Python virtualenv wrapper script is functioning correctly.
 
    """
 

	
 
    with host.sudo():
 

	
 
        wrapper = host.file("/var/www/parameters-paste-req/virtualenv/bin/exec")
 
        assert wrapper.is_file
 
        assert wrapper.user == 'admin-parameters-paste-req'
 
        assert wrapper.group == 'web-parameters-paste-req'
 
        assert wrapper.mode == 0o750
 

	
 
        command = host.run("sudo -u admin-parameters-paste-req /var/www/parameters-paste-req/virtualenv/bin/exec python -c 'import gunicorn'")
 
        assert command.rc == 0
 

	
 

	
 
def test_virtualenv_packages(host):
 
    """
 
    Tests if correct packages are installed in virtualenv.
 
    """
 

	
 
    packages = host.run("sudo -u admin-parameters-paste-req /var/www/parameters-paste-req/virtualenv/bin/pip freeze")
 

	
 
    assert sorted(packages.stdout.lower().split("\n")) == sorted("""Flask==0.12.2
 
Jinja2==2.9.6
 
MarkupSafe==1.0
 
Paste==2.0.3
 
PasteDeploy==1.5.2
 
PasteScript==2.0.2
 
Werkzeug==0.12.2
 
argparse==1.2.1
 
click==6.7
 
futures==3.1.0
 
gunicorn==19.7.0
 
itsdangerous==0.24
 
six==1.10.0
 
wsgiref==0.1.2""".lower().split("\n"))
 

	
 

	
 
def test_wsgi_requirements_upgrade_checks(host):
 
    """
 
    Tests if Python requirements files for upgrade checks are set-up correctly.
 
    """
 

	
 
    with host.sudo():
0 comments (0 inline, 0 general)