Changeset - a1e0221d24e7
[Not reviewed]
22 1 25
Branko Majic (branko) - 6 years ago 2017-11-26 23:25:06
branko@majic.rs
MAR-128: Upgraded tests for ROLE_NAME role:

- Switch to new Molecule configuration.
- Updated set-up playbook to use become: yes.
- Moved some preparatory steps outside of the main playbook (eases
idempotence tests).
- Updated tests to reference the yml inventory file.
- Updated tests to use new fixture (host instead of individual ones).
- Switched to extracting hostname instead of hard-coding it in a
couple of tests.
- Fixed some linting issues.
- Updated one of the tests that depend on output of pip freeze due to
new versions of packages coming out.
- Updated hostname to include Debian version (for future expansion).
- Renamded some test data to match new hostname.
27 files changed with 420 insertions and 302 deletions:
0 comments (0 inline, 0 general)
roles/wsgi_website/meta/main.yml
Show inline comments
 
@@ -4,4 +4,4 @@ allow_duplicates: yes
 

	
 
dependencies:
 
  - common
 
  - web_server
 
\ No newline at end of file
 
  - web_server
roles/wsgi_website/molecule.yml
Show inline comments
 
deleted file
roles/wsgi_website/molecule/default/create.yml
Show inline comments
 
new file 100644
 
---
 
- name: Create
 
  hosts: localhost
 
  connection: local
 
  gather_facts: False
 
  no_log: "{{ not lookup('env', 'MOLECULE_DEBUG') | bool }}"
 
  vars:
 
    molecule_file: "{{ lookup('env', 'MOLECULE_FILE') }}"
 
    molecule_instance_config: "{{ lookup('env', 'MOLECULE_INSTANCE_CONFIG') }}"
 
    molecule_yml: "{{ lookup('file', molecule_file) | molecule_from_yaml }}"
 
  tasks:
 
    - name: Create molecule instance(s)
 
      molecule_vagrant:
 
        instance_name: "{{ item.name }}"
 
        instance_interfaces: "{{ item.interfaces | default(omit) }}"
 
        instance_raw_config_args: "{{ item.instance_raw_config_args | default(omit) }}"
 

	
 
        platform_box: "{{ item.box }}"
 
        platform_box_version: "{{ item.box_version | default(omit) }}"
 
        platform_box_url: "{{ item.box_url | default(omit) }}"
 

	
 
        provider_name: "{{ molecule_yml.driver.provider.name }}"
 
        provider_memory: "{{ item.memory | default(omit) }}"
 
        provider_cpus: "{{ item.cpus | default(omit) }}"
 
        provider_raw_config_args: "{{ item.raw_config_args | default(omit) }}"
 

	
 
        state: up
 
      register: server
 
      with_items: "{{ molecule_yml.platforms }}"
 

	
 
    # Mandatory configuration for Molecule to function.
 

	
 
    - name: Populate instance config dict
 
      set_fact:
 
        instance_conf_dict: {
 
          'instance': "{{ item.Host }}",
 
          'address': "{{ item.HostName }}",
 
          'user': "{{ item.User }}",
 
          'port': "{{ item.Port }}",
 
          'identity_file': "{{ item.IdentityFile }}", }
 
      with_items: "{{ server.results }}"
 
      register: instance_config_dict
 
      when: server.changed | bool
 

	
 
    - name: Convert instance config dict to a list
 
      set_fact:
 
        instance_conf: "{{ instance_config_dict.results | map(attribute='ansible_facts.instance_conf_dict') | list }}"
 
      when: server.changed | bool
 

	
 
    - name: Dump instance config
 
      copy:
 
        # NOTE(retr0h): Workaround for Ansible 2.2.
 
        #               https://github.com/ansible/ansible/issues/20885
 
        content: "{{ instance_conf | to_json | from_json | molecule_to_yaml | molecule_header }}"
 
        dest: "{{ molecule_instance_config }}"
 
      when: server.changed | bool
roles/wsgi_website/molecule/default/destroy.yml
Show inline comments
 
new file 100644
 
---
 

	
 
- name: Destroy
 
  hosts: localhost
 
  connection: local
 
  gather_facts: False
 
  no_log: "{{ not lookup('env', 'MOLECULE_DEBUG') | bool }}"
 
  vars:
 
    molecule_file: "{{ lookup('env', 'MOLECULE_FILE') }}"
 
    molecule_instance_config: "{{ lookup('env',' MOLECULE_INSTANCE_CONFIG') }}"
 
    molecule_yml: "{{ lookup('file', molecule_file) | molecule_from_yaml }}"
 
  tasks:
 
    - name: Destroy molecule instance(s)
 
      molecule_vagrant:
 
        instance_name: "{{ item.name }}"
 
        platform_box: "{{ item.box }}"
 
        provider_name: "{{ molecule_yml.driver.provider.name }}"
 
        force_stop: "{{ item.force_stop | default(True) }}"
 

	
 
        state: destroy
 
      register: server
 
      with_items: "{{ molecule_yml.platforms }}"
 

	
 
    # Mandatory configuration for Molecule to function.
 

	
 
    - name: Populate instance config
 
      set_fact:
 
        instance_conf: {}
 

	
 
    - name: Dump instance config
 
      copy:
 
        # NOTE(retr0h): Workaround for Ansible 2.2.
 
        #               https://github.com/ansible/ansible/issues/20885
 
        content: "{{ instance_conf | to_json | from_json | molecule_to_yaml | molecule_header }}"
 
        dest: "{{ molecule_instance_config }}"
 
      when: server.changed | bool
roles/wsgi_website/molecule/default/molecule.yml
Show inline comments
 
new file 100644
 
---
 

	
 
dependency: {}
 

	
 
driver:
 
  name: vagrant
 
  provider:
 
    name: virtualbox
 

	
 
lint:
 
  name: yamllint
 

	
 
platforms:
 

	
 
  - name: wsgi-website-jessie64
 
    groups:
 
      - wsgi-website
 
    box: debian/contrib-jessie64
 
    memory: 512
 
    cpus: 1
 

	
 
provisioner:
 
  name: ansible
 
  config_options:
 
    ssh_connection:
 
      pipelining: "True"
 
  lint:
 
    name: ansible-lint
 

	
 
scenario:
 
  name: default
 

	
 
verifier:
 
  name: testinfra
 
  lint:
 
    name: flake8
roles/wsgi_website/molecule/default/playbook.yml
Show inline comments
 
file renamed from roles/wsgi_website/playbook.yml to roles/wsgi_website/molecule/default/playbook.yml
 
---
 

	
 
- hosts: all
 
  tasks:
 

	
 
    - name: Update all caches to avoid errors due to missing remote archives
 
      apt:
 
        update_cache: yes
 
      changed_when: False
 

	
 
    - name: Set-up /etc/hosts entries
 
      lineinfile:
 
        dest: /etc/hosts
 
        line: "{{ ansible_eth0.ipv4.address }} parameters-mandatory parameters-optional.local parameters-paste-req wsgi-website"
 

	
 
    - name: Install curl for testing redirects and webpage content
 
      apt:
 
        name: curl
 
        state: installed
 

	
 
    - name: Install swaks for testing mail forwarding
 
      apt:
 
        name: swaks
 
        state: installed
 

	
 
    - name: Install Postfix for testing mail forwarding (Exim4 not covered)
 
      apt:
 
        name: postfix
 
        state: installed
 

	
 
    - name: Set-up group for an additional user
 
      group:
 
        name: user
 
        state: present
 

	
 
    - name: Set-up additional user for testing mail delivery
 
      user:
 
        name: user
 
        group: user
 
        shell: /bin/bash
 

	
 
- hosts: all
 
- hosts: wsgi-website
 
  become: yes
 
  vars:
 
    # common
 
    ca_certificates:
 
@@ -117,8 +79,8 @@
 
      https_tls_certificate: "{{ lookup('file', 'tests/data/x509/parameters-paste-req_https.cert.pem') }}"
 
      https_tls_key: "{{ lookup('file', 'tests/data/x509/parameters-paste-req_https.key.pem') }}"
 

	
 

	
 
- hosts: all
 
- hosts: wsgi-website
 
  become: yes
 
  tasks:
 
    # parameters-mandatory application
 
    - name: Set-up directories where application files are hosted at
roles/wsgi_website/molecule/default/prepare.yml
Show inline comments
 
new file 100644
 
---
 

	
 
- name: Prepare
 
  hosts: all
 
  gather_facts: False
 
  tasks:
 
    - name: Install python for Ansible
 
      raw: test -e /usr/bin/python || (apt -y update && apt install -y python-minimal)
 
      become: True
 
      changed_when: False
 

	
 
- hosts: wsgi-website
 
  become: yes
 
  tasks:
 

	
 
    - name: Update all caches to avoid errors due to missing remote archives
 
      apt:
 
        update_cache: yes
 
      changed_when: False
 

	
 
    - name: Set-up /etc/hosts entries
 
      lineinfile:
 
        dest: /etc/hosts
 
        line: "{{ ansible_eth0.ipv4.address }} parameters-mandatory parameters-optional.local parameters-paste-req wsgi-website"
 

	
 
    - name: Install curl for testing redirects and webpage content
 
      apt:
 
        name: curl
 
        state: installed
 

	
 
    - name: Install swaks for testing mail forwarding
 
      apt:
 
        name: swaks
 
        state: installed
 

	
 
    - name: Install Postfix for testing mail forwarding (Exim4 not covered)
 
      apt:
 
        name: postfix
 
        state: installed
 

	
 
    - name: Set-up group for an additional user
 
      group:
 
        name: user
 
        state: present
 

	
 
    - name: Set-up additional user for testing mail delivery
 
      user:
 
        name: user
 
        group: user
 
        shell: /bin/bash
roles/wsgi_website/molecule/default/tests/data/media_file.txt
Show inline comments
 
file renamed from roles/wsgi_website/tests/data/media_file.txt to roles/wsgi_website/molecule/default/tests/data/media_file.txt
roles/wsgi_website/molecule/default/tests/data/python/paste/config.ini
Show inline comments
 
file renamed from roles/wsgi_website/tests/data/python/paste/config.ini to roles/wsgi_website/molecule/default/tests/data/python/paste/config.ini
roles/wsgi_website/molecule/default/tests/data/python/paste/testapp.py
Show inline comments
 
file renamed from roles/wsgi_website/tests/data/python/paste/testapp.py to roles/wsgi_website/molecule/default/tests/data/python/paste/testapp.py
roles/wsgi_website/molecule/default/tests/data/python/paste/wsgi.py
Show inline comments
 
file renamed from roles/wsgi_website/tests/data/python/paste/wsgi.py to roles/wsgi_website/molecule/default/tests/data/python/paste/wsgi.py
roles/wsgi_website/molecule/default/tests/data/python/wsgi/testapp.py
Show inline comments
 
file renamed from roles/wsgi_website/tests/data/python/wsgi/testapp.py to roles/wsgi_website/molecule/default/tests/data/python/wsgi/testapp.py
roles/wsgi_website/molecule/default/tests/data/static_file.txt
Show inline comments
 
file renamed from roles/wsgi_website/tests/data/static_file.txt to roles/wsgi_website/molecule/default/tests/data/static_file.txt
roles/wsgi_website/molecule/default/tests/data/x509/ca.cert.pem
Show inline comments
 
file renamed from roles/wsgi_website/tests/data/x509/ca.cert.pem to roles/wsgi_website/molecule/default/tests/data/x509/ca.cert.pem
roles/wsgi_website/molecule/default/tests/data/x509/ca.key.pem
Show inline comments
 
file renamed from roles/wsgi_website/tests/data/x509/ca.key.pem to roles/wsgi_website/molecule/default/tests/data/x509/ca.key.pem
roles/wsgi_website/molecule/default/tests/data/x509/parameters-mandatory_https.key
Show inline comments
 
file renamed from roles/wsgi_website/tests/data/x509/parameters-mandatory_https.key to roles/wsgi_website/molecule/default/tests/data/x509/parameters-mandatory_https.key
roles/wsgi_website/molecule/default/tests/data/x509/parameters-mandatory_https.pem
Show inline comments
 
file renamed from roles/wsgi_website/tests/data/x509/parameters-mandatory_https.pem to roles/wsgi_website/molecule/default/tests/data/x509/parameters-mandatory_https.pem
roles/wsgi_website/molecule/default/tests/data/x509/parameters-optional.local_https.cert.pem
Show inline comments
 
file renamed from roles/wsgi_website/tests/data/x509/parameters-optional.local_https.cert.pem to roles/wsgi_website/molecule/default/tests/data/x509/parameters-optional.local_https.cert.pem
roles/wsgi_website/molecule/default/tests/data/x509/parameters-optional.local_https.key.pem
Show inline comments
 
file renamed from roles/wsgi_website/tests/data/x509/parameters-optional.local_https.key.pem to roles/wsgi_website/molecule/default/tests/data/x509/parameters-optional.local_https.key.pem
roles/wsgi_website/molecule/default/tests/data/x509/parameters-paste-req_https.cert.pem
Show inline comments
 
file renamed from roles/wsgi_website/tests/data/x509/parameters-paste-req_https.cert.pem to roles/wsgi_website/molecule/default/tests/data/x509/parameters-paste-req_https.cert.pem
roles/wsgi_website/molecule/default/tests/data/x509/parameters-paste-req_https.key.pem
Show inline comments
 
file renamed from roles/wsgi_website/tests/data/x509/parameters-paste-req_https.key.pem to roles/wsgi_website/molecule/default/tests/data/x509/parameters-paste-req_https.key.pem
roles/wsgi_website/molecule/default/tests/data/x509/wsgi-website_https.cert.pem
Show inline comments
 
file renamed from roles/wsgi_website/tests/data/x509/wsgi-website_https.cert.pem to roles/wsgi_website/molecule/default/tests/data/x509/wsgi-website_https.cert.pem
roles/wsgi_website/molecule/default/tests/data/x509/wsgi-website_https.key.pem
Show inline comments
 
file renamed from roles/wsgi_website/tests/data/x509/wsgi-website_https.key.pem to roles/wsgi_website/molecule/default/tests/data/x509/wsgi-website_https.key.pem
roles/wsgi_website/molecule/default/tests/test_default.py
Show inline comments
 
file renamed from roles/wsgi_website/tests/test_default.py to roles/wsgi_website/molecule/default/tests/test_default.py
 
@@ -2,11 +2,11 @@ import testinfra.utils.ansible_runner
 

	
 

	
 
testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner(
 
    '.molecule/ansible_inventory').get_hosts('all')
 
    '.molecule/ansible_inventory.yml').get_hosts('all')
 

	
 

	
 
def test_hosts_file(File):
 
    f = File('/etc/hosts')
 
def test_hosts_file(host):
 
    f = host.file('/etc/hosts')
 

	
 
    assert f.exists
 
    assert f.user == 'root'
roles/wsgi_website/molecule/default/tests/test_parameters_mandatory.py
Show inline comments
 
file renamed from roles/wsgi_website/tests/test_parameters_mandatory.py to roles/wsgi_website/molecule/default/tests/test_parameters_mandatory.py
 
@@ -5,26 +5,26 @@ import testinfra.utils.ansible_runner
 

	
 

	
 
testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner(
 
    '.molecule/ansible_inventory').get_hosts('all')
 
    '.molecule/ansible_inventory.yml').get_hosts('all')
 

	
 

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

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

	
 
    assert group.exists
 
    assert group.gid == 1003
 

	
 

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

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

	
 
    assert user.exists
 
    assert user.uid == 1003
 
@@ -34,12 +34,12 @@ def test_website_admin_user(User):
 
    assert user.home == '/var/www/parameters-mandatory'
 

	
 

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

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

	
 
    assert home.is_directory
 
    assert home.user == 'admin-parameters-mandatory'
 
@@ -47,45 +47,45 @@ def test_website_admin_home(File, Sudo):
 
    assert home.mode == 0o750
 

	
 

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

	
 
    with Sudo():
 
    with host.sudo():
 

	
 
        directory = File('/var/www/parameters-mandatory/.profile.d')
 
        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(File, Sudo):
 
def test_virtualenv_profile_configuration(host):
 
    """
 
    Tests if profile configuration file for auto-activation of virtual
 
    environment has been deployed correctly.
 
    """
 

	
 
    with Sudo():
 
    with host.sudo():
 

	
 
        config = File('/var/www/parameters-mandatory/.profile.d/virtualenv.sh')
 
        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(File, Sudo):
 
def test_environment_profile_configuration(host):
 
    """
 
    Tests if profile configuration file for setting-up environment variables has
 
    been deployed correctly.
 
    """
 

	
 
    with Sudo():
 
    with host.sudo():
 

	
 
        config = File('/var/www/parameters-mandatory/.profile.d/environment.sh')
 
        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'
 
@@ -93,22 +93,22 @@ def test_environment_profile_configuration(File, Sudo):
 
        assert config.content == ""
 

	
 

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

	
 
    env = Command("sudo -i -u admin-parameters-mandatory printenv VIRTUAL_ENV MY_ENV_VAR")
 
    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(Command, Sudo, User):
 
def test_website_application_user(host):
 
    """
 
    Tests if website application user has been created correctly.
 
    """
 

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

	
 
    assert user.exists
 
    assert user.uid == 999
 
@@ -117,28 +117,28 @@ def test_website_application_user(Command, Sudo, User):
 
    assert user.shell == '/bin/sh'
 
    assert user.home == '/var/www/parameters-mandatory'
 

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

	
 

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

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

	
 

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

	
 
    with Sudo():
 
    with host.sudo():
 

	
 
        config = File('/var/www/parameters-mandatory/.forward')
 
        config = host.file('/var/www/parameters-mandatory/.forward')
 
        assert config.is_file
 
        assert config.user == 'root'
 
        assert config.group == 'web-parameters-mandatory'
 
@@ -146,59 +146,61 @@ def test_forward_file(File, Sudo):
 
        assert config.content == "root"
 

	
 

	
 
def test_mail_forwarding(Command, File, Sudo):
 
def test_mail_forwarding(host):
 
    """
 
    Tests if mail forwarding works as expected.
 
    """
 

	
 
    send = Command('swaks --suppress-data --to web-parameters-mandatory@localhost')
 
    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 Sudo():
 
        mail_log = File('/var/log/mail.log')
 
    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@wsgi-website>, orig_to=<web-parameters-mandatory@localhost>.*status=sent" % message_id
 
        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(File, Sudo):
 
def test_python_virtualenv_created(host):
 
    """
 
    Tests if Python virtual environment has been created correctly.
 
    """
 

	
 
    with Sudo():
 
    with host.sudo():
 

	
 
        virtualenv = File("/var/www/parameters-mandatory/virtualenv")
 
        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 = File("/var/www/parameters-mandatory/virtualenv/bin/activate")
 
        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(File, Sudo):
 
def test_python_virtualenv_project_directory_config(host):
 
    """
 
    Tests if project directory configuration within virtualenv is set-up
 
    correctly.
 
    """
 

	
 
    with Sudo():
 
    with host.sudo():
 

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

	
 
        assert project.is_file
 
        assert project.user == 'admin-parameters-mandatory'
 
@@ -206,29 +208,29 @@ def test_python_virtualenv_project_directory_config(File, Sudo):
 
        assert project.mode == 0o640
 

	
 

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

	
 
    with Sudo():
 
    with host.sudo():
 

	
 
        wrapper = File("/var/www/parameters-mandatory/virtualenv/bin/exec")
 
        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 = Command("sudo -u admin-parameters-mandatory /var/www/parameters-mandatory/virtualenv/bin/exec python -c 'import gunicorn'")
 
        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(Command):
 
def test_virtualenv_packages(host):
 
    """
 
    Tests if correct packages are installed in virtualenv.
 
    """
 

	
 
    packages = Command("sudo -u admin-parameters-mandatory /var/www/parameters-mandatory/virtualenv/bin/pip freeze")
 
    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
 
@@ -236,22 +238,22 @@ gunicorn==19.7.1
 
wsgiref==0.1.2""".lower().split("\n"))
 

	
 

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

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

	
 

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

	
 
    config = File("/etc/systemd/system/parameters-mandatory.socket")
 
    config = host.file("/etc/systemd/system/parameters-mandatory.socket")
 
    assert config.is_file
 
    assert config.user == 'root'
 
    assert config.group == 'root'
 
@@ -260,29 +262,29 @@ def test_systemd_socket_configuration_file(File):
 
    assert "ListenStream=/run/wsgi/parameters-mandatory.sock" in config.content
 

	
 

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

	
 
    with Sudo():
 
    with host.sudo():
 

	
 
        socket_file = File("/run/wsgi/parameters-mandatory.sock")
 
        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 = Socket("unix:///run/wsgi/parameters-mandatory.sock")
 
        socket = host.socket("unix:///run/wsgi/parameters-mandatory.sock")
 
        assert socket.is_listening
 

	
 

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

	
 
    config = File("/etc/systemd/system/parameters-mandatory.service")
 
    config = host.file("/etc/systemd/system/parameters-mandatory.service")
 
    assert config.is_file
 
    assert config.user == 'root'
 
    assert config.group == 'root'
 
@@ -290,46 +292,46 @@ def test_systemd_service_configuration_file(File):
 
    assert "parameters-mandatory" in config.content
 

	
 

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

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

	
 
    assert service.is_enabled
 
    assert service.is_running
 

	
 

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

	
 
    with Sudo():
 
    with host.sudo():
 

	
 
        directory = File('/var/www/parameters-mandatory/htdocs')
 
        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(File, Sudo):
 
def test_nginx_tls_files(host):
 
    """
 
    Tests if TLS private key and certificate have been deployed correctly.
 
    """
 

	
 
    with Sudo():
 
    with host.sudo():
 

	
 
        tls_file = File('/etc/ssl/private/parameters-mandatory_https.key')
 
        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 = File('/etc/ssl/certs/parameters-mandatory_https.pem')
 
        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'
 
@@ -337,13 +339,13 @@ def test_nginx_tls_files(File, Sudo):
 
        assert tls_file.content == open("tests/data/x509/parameters-mandatory_https.pem", "r").read().rstrip()
 

	
 

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

	
 
    config = File('/etc/check_certificate/parameters-mandatory_https.conf')
 
    config = host.file('/etc/check_certificate/parameters-mandatory_https.conf')
 
    assert config.is_file
 
    assert config.user == 'root'
 
    assert config.group == 'root'
 
@@ -351,12 +353,12 @@ def test_certificate_validity_check_configuration(File):
 
    assert config.content == "/etc/ssl/certs/parameters-mandatory_https.pem"
 

	
 

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

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

	
 
    assert config.is_file
 
    assert config.user == 'root'
 
@@ -364,35 +366,35 @@ def test_vhost_file(File):
 
    assert config.mode == 0o640
 

	
 

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

	
 
    config = File('/etc/nginx/sites-enabled/parameters-mandatory')
 
    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(Command):
 
def test_https_enforcement(host):
 
    """
 
    Tests if HTTPS is being enforced.
 
    """
 

	
 
    https_enforcement = Command('curl -I http://parameters-mandatory/')
 
    https_enforcement = host.run('curl -I http://parameters-mandatory/')
 

	
 
    assert https_enforcement.rc == 0
 
    assert 'HTTP/1.1 301 Moved Permanently' in https_enforcement.stdout
 
    assert 'Location: https://parameters-mandatory/' in https_enforcement.stdout
 

	
 
    https_enforcement = Command('curl -I https://parameters-mandatory/')
 
    https_enforcement = host.run('curl -I https://parameters-mandatory/')
 

	
 
    assert https_enforcement.rc == 0
 
    assert 'Strict-Transport-Security: max-age=31536000; includeSubDomains' in https_enforcement.stdout
 

	
 

	
 
def test_index_page(Command):
 
def test_index_page(host):
 
    """
 
    Tests if index page is served correctly. This covers:
 

	
 
@@ -401,7 +403,7 @@ def test_index_page(Command):
 
    - Handling of proxy headers.
 
    """
 

	
 
    page = Command('curl -H "Accept-Encoding: plain" https://parameters-mandatory/')
 
    page = host.run('curl -H "Accept-Encoding: plain" https://parameters-mandatory/')
 

	
 
    assert page.rc == 0
 
    assert "This is the WSGI application at parameters-mandatory." in page.stdout
 
@@ -410,17 +412,17 @@ def test_index_page(Command):
 
    assert "Accept-Encoding: plain" in page.stdout
 

	
 

	
 
def test_static_file_serving(Command):
 
def test_static_file_serving(host):
 
    """
 
    Tests serving of static files.
 
    """
 

	
 
    page = Command('curl https://parameters-mandatory/static/static_file.txt')
 
    page = host.run('curl https://parameters-mandatory/static/static_file.txt')
 
    assert page.rc == 0
 
    assert "This is the WSGI application at parameters-mandatory." in page.stdout
 
    assert "Requested URL was: https://parameters-mandatory/static/static_file.txt" in page.stdout
 

	
 
    page = Command('curl https://parameters-mandatory/media/media_file.txt')
 
    page = host.run('curl https://parameters-mandatory/media/media_file.txt')
 
    assert page.rc == 0
 
    assert "This is the WSGI application at parameters-mandatory." in page.stdout
 
    assert "Requested URL was: https://parameters-mandatory/media/media_file.txt" in page.stdout
roles/wsgi_website/molecule/default/tests/test_parameters_optional.py
Show inline comments
 
file renamed from roles/wsgi_website/tests/test_parameters_optional.py to roles/wsgi_website/molecule/default/tests/test_parameters_optional.py
 
@@ -5,26 +5,26 @@ import testinfra.utils.ansible_runner
 

	
 

	
 
testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner(
 
    '.molecule/ansible_inventory').get_hosts('all')
 
    '.molecule/ansible_inventory.yml').get_hosts('all')
 

	
 

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

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

	
 
    assert group.exists
 
    assert group.gid == 5001
 

	
 

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

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

	
 
    assert user.exists
 
    assert user.uid == 5000
 
@@ -34,12 +34,12 @@ def test_website_admin_user(User):
 
    assert user.home == '/var/www/parameters-optional.local'
 

	
 

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

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

	
 
    assert home.is_directory
 
    assert home.user == 'admin-parameters-optional_local'
 
@@ -47,45 +47,45 @@ def test_website_admin_home(File, Sudo):
 
    assert home.mode == 0o750
 

	
 

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

	
 
    with Sudo():
 
    with host.sudo():
 

	
 
        directory = File('/var/www/parameters-optional.local/.profile.d')
 
        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(File, Sudo):
 
def test_virtualenv_profile_configuration(host):
 
    """
 
    Tests if profile configuration file for auto-activation of virtual
 
    environment has been deployed correctly.
 
    """
 

	
 
    with Sudo():
 
    with host.sudo():
 

	
 
        config = File('/var/www/parameters-optional.local/.profile.d/virtualenv.sh')
 
        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(File, Sudo):
 
def test_environment_profile_configuration(host):
 
    """
 
    Tests if profile configuration file for setting-up environment variables has
 
    been deployed correctly.
 
    """
 

	
 
    with Sudo():
 
    with host.sudo():
 

	
 
        config = File('/var/www/parameters-optional.local/.profile.d/environment.sh')
 
        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'
 
@@ -93,22 +93,22 @@ def test_environment_profile_configuration(File, Sudo):
 
        assert config.content == "export MY_ENV_VAR='My environment variable'"
 

	
 

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

	
 
    env = Command("sudo -i -u admin-parameters-optional_local printenv VIRTUAL_ENV MY_ENV_VAR")
 
    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(Command, Sudo, User):
 
def test_website_application_user(host):
 
    """
 
    Tests if website application user has been created correctly.
 
    """
 

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

	
 
    assert user.exists
 
    assert user.uid == 5001
 
@@ -117,28 +117,28 @@ def test_website_application_user(Command, Sudo, User):
 
    assert user.shell == '/bin/sh'
 
    assert user.home == '/var/www/parameters-optional.local'
 

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

	
 

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

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

	
 

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

	
 
    with Sudo():
 
    with host.sudo():
 

	
 
        config = File('/var/www/parameters-optional.local/.forward')
 
        config = host.file('/var/www/parameters-optional.local/.forward')
 
        assert config.is_file
 
        assert config.user == 'root'
 
        assert config.group == 'web-parameters-optional_local'
 
@@ -146,79 +146,81 @@ def test_forward_file(File, Sudo):
 
        assert config.content == "user"
 

	
 

	
 
def test_mail_forwarding(Command, File, Sudo):
 
def test_mail_forwarding(host):
 
    """
 
    Tests if mail forwarding works as expected.
 
    """
 

	
 
    send = Command('swaks --suppress-data --to web-parameters-optional_local@localhost')
 
    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 Sudo():
 
        mail_log = File('/var/log/mail.log')
 
    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@wsgi-website>, orig_to=<web-parameters-optional_local@localhost>.*status=sent" % message_id
 
        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(Package):
 
def test_installed_packages(host):
 
    """
 
    Tests if additional packages are installed.
 
    """
 

	
 
    assert Package('libmariadb-client-lgpl-dev-compat').is_installed
 
    assert Package('global').is_installed
 
    assert host.package('libmariadb-client-lgpl-dev-compat').is_installed
 
    assert host.package('global').is_installed
 

	
 

	
 
def test_mariadb_compat_symlink(File):
 
def test_mariadb_compat_symlink(host):
 
    """
 
    Tests if compatibility symlink is set-up for mysql_config binary if
 
    libmariadb-client-lgpl-dev-compat is installed.
 
    """
 

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

	
 

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

	
 
    with Sudo():
 
    with host.sudo():
 

	
 
        virtualenv = File("/var/www/parameters-optional.local/virtualenv")
 
        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 = File("/var/www/parameters-optional.local/virtualenv/bin/activate")
 
        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(File, Sudo):
 
def test_python_virtualenv_project_directory_config(host):
 
    """
 
    Tests if project directory configuration within virtualenv is set-up
 
    correctly.
 
    """
 

	
 
    with Sudo():
 
    with host.sudo():
 

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

	
 
        assert project.is_file
 
        assert project.user == 'admin-parameters-optional_local'
 
@@ -226,29 +228,29 @@ def test_python_virtualenv_project_directory_config(File, Sudo):
 
        assert project.mode == 0o640
 

	
 

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

	
 
    with Sudo():
 
    with host.sudo():
 

	
 
        wrapper = File("/var/www/parameters-optional.local/virtualenv/bin/exec")
 
        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 = Command("sudo -u admin-parameters-optional_local /var/www/parameters-optional.local/virtualenv/bin/exec python -c 'import gunicorn'")
 
        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(Command):
 
def test_virtualenv_packages(host):
 
    """
 
    Tests if correct packages are installed in virtualenv.
 
    """
 

	
 
    packages = Command("sudo -u admin-parameters-optional_local /var/www/parameters-optional.local/virtualenv/bin/pip freeze")
 
    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
 
@@ -256,30 +258,31 @@ dnspython==1.15.0
 
docopt==0.6.2
 
futures==3.1.0
 
gunicorn==19.7.0
 
jedi==0.10.2
 
jedi==0.11.0
 
parso==0.1.0
 
prompt-toolkit==1.0.15
 
ptpython==0.41
 
six==1.10.0
 
six==1.11.0
 
wcwidth==0.1.7
 
wsgiref==0.1.2""".lower().split("\n"))
 

	
 

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

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

	
 

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

	
 
    config = File("/etc/systemd/system/parameters-optional.local.socket")
 
    config = host.file("/etc/systemd/system/parameters-optional.local.socket")
 
    assert config.is_file
 
    assert config.user == 'root'
 
    assert config.group == 'root'
 
@@ -288,29 +291,29 @@ def test_systemd_socket_configuration_file(File):
 
    assert "ListenStream=/run/wsgi/parameters-optional.local.sock" in config.content
 

	
 

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

	
 
    with Sudo():
 
    with host.sudo():
 

	
 
        socket_file = File("/run/wsgi/parameters-optional.local.sock")
 
        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 = Socket("unix:///run/wsgi/parameters-optional.local.sock")
 
        socket = host.socket("unix:///run/wsgi/parameters-optional.local.sock")
 
        assert socket.is_listening
 

	
 

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

	
 
    config = File("/etc/systemd/system/parameters-optional.local.service")
 
    config = host.file("/etc/systemd/system/parameters-optional.local.service")
 
    assert config.is_file
 
    assert config.user == 'root'
 
    assert config.group == 'root'
 
@@ -318,46 +321,46 @@ def test_systemd_service_configuration_file(File):
 
    assert "parameters-optional.local" in config.content
 

	
 

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

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

	
 
    assert service.is_enabled
 
    assert service.is_running
 

	
 

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

	
 
    with Sudo():
 
    with host.sudo():
 

	
 
        directory = File('/var/www/parameters-optional.local/htdocs')
 
        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(File, Sudo):
 
def test_nginx_tls_files(host):
 
    """
 
    Tests if TLS private key and certificate have been deployed correctly.
 
    """
 

	
 
    with Sudo():
 
    with host.sudo():
 

	
 
        tls_file = File('/etc/ssl/private/parameters-optional.local_https.key')
 
        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 = File('/etc/ssl/certs/parameters-optional.local_https.pem')
 
        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'
 
@@ -365,13 +368,13 @@ def test_nginx_tls_files(File, Sudo):
 
        assert tls_file.content == open("tests/data/x509/parameters-optional.local_https.cert.pem", "r").read().rstrip()
 

	
 

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

	
 
    config = File('/etc/check_certificate/parameters-optional.local_https.conf')
 
    config = host.file('/etc/check_certificate/parameters-optional.local_https.conf')
 
    assert config.is_file
 
    assert config.user == 'root'
 
    assert config.group == 'root'
 
@@ -379,12 +382,12 @@ def test_certificate_validity_check_configuration(File):
 
    assert config.content == "/etc/ssl/certs/parameters-optional.local_https.pem"
 

	
 

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

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

	
 
    assert config.is_file
 
    assert config.user == 'root'
 
@@ -392,36 +395,36 @@ def test_vhost_file(File):
 
    assert config.mode == 0o640
 

	
 

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

	
 
    config = File('/etc/nginx/sites-enabled/parameters-optional.local')
 
    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(Command):
 
def test_https_enforcement(host):
 
    """
 
    Tests if HTTPS is (not) being enforced.
 
    """
 

	
 
    https_enforcement = Command('curl -I http://parameters-optional.local/')
 
    https_enforcement = host.run('curl -I http://parameters-optional.local/')
 

	
 
    assert https_enforcement.rc == 0
 
    assert 'HTTP/1.1 200 OK' in https_enforcement.stdout
 
    assert 'HTTP/1.1 301 Moved Permanently' not in https_enforcement.stdout
 
    assert 'Location: https://parameters-optional/' not in https_enforcement.stdout
 

	
 
    https_enforcement = Command('curl -I https://parameters-optional.local/')
 
    https_enforcement = host.run('curl -I https://parameters-optional.local/')
 

	
 
    assert https_enforcement.rc == 0
 
    assert 'Strict-Transport-Security' not in https_enforcement.stdout
 

	
 

	
 
def test_index_page(Command):
 
def test_index_page(host):
 
    """
 
    Tests if index page is served correctly. This covers:
 

	
 
@@ -430,7 +433,7 @@ def test_index_page(Command):
 
    - Handling of proxy headers.
 
    """
 

	
 
    page = Command('curl -H "Accept-Encoding: plain" https://parameters-optional.local/')
 
    page = host.run('curl -H "Accept-Encoding: plain" https://parameters-optional.local/')
 

	
 
    assert page.rc == 0
 
    assert "This is the WSGI application at parameters-optional.local." in page.stdout
 
@@ -439,51 +442,51 @@ def test_index_page(Command):
 
    assert "Accept-Encoding: None" in page.stdout
 

	
 

	
 
def test_static_file_serving(Command):
 
def test_static_file_serving(host):
 
    """
 
    Tests serving of static files.
 
    """
 

	
 
    page = Command('curl https://parameters-optional.local/static/static_file.txt')
 
    page = host.run('curl https://parameters-optional.local/static/static_file.txt')
 
    assert page.rc == 0
 
    assert page.stdout == open("tests/data/static_file.txt", 'r').read().rstrip()
 

	
 
    page = Command('curl https://parameters-optional.local/media/media_file.txt')
 
    page = host.run('curl https://parameters-optional.local/media/media_file.txt')
 
    assert page.rc == 0
 
    assert page.stdout == open("tests/data/media_file.txt", 'r').read().rstrip()
 

	
 

	
 
def test_additional_nginx_config(Command):
 
def test_additional_nginx_config(host):
 
    """
 
    Tests if additional Nginx configuration directives are properly deployed.
 
    """
 

	
 
    page = Command('curl https://parameters-optional.local/static/missing_static_file.txt')
 
    page = host.run('curl https://parameters-optional.local/static/missing_static_file.txt')
 
    assert page.rc == 0
 
    assert "Requested URL was: https://parameters-optional.local/my/own/error/page"
 

	
 

	
 
def test_environment_indicator(Command):
 
def test_environment_indicator(host):
 
    """
 
    Tests if environment indicator is applied correctly.
 
    """
 

	
 
    page = Command('curl https://parameters-optional.local/')
 
    page = host.run('curl https://parameters-optional.local/')
 

	
 
    assert page.rc == 0
 
    assert "<div id='website-environment' style='background-color: #ff0000; width: 100%; text-align: center; position: fixed; bottom: 5px; color: #00ff00; " \
 
        "font-weight: bold; z-index: 999999;'>parameters-optional</div></body>" in page.stdout
 

	
 

	
 
def test_nginx_rewrite_config(Command):
 
def test_nginx_rewrite_config(host):
 
    """
 
    Tests if Nginx rewrite configuration is deployed correctly.
 
    """
 

	
 
    page = Command('curl https://parameters-optional.local/rewrite1/some/path')
 
    page = host.run('curl https://parameters-optional.local/rewrite1/some/path')
 
    assert page.rc == 0
 
    assert "Requested URL was: https://parameters-optional.local/rewritten1/" in page.stdout
 

	
 
    page = Command('curl https://parameters-optional.local/rewrite2/some/other/path')
 
    page = host.run('curl https://parameters-optional.local/rewrite2/some/other/path')
 
    assert page.rc == 0
 
    assert "Requested URL was: https://parameters-optional.local/rewritten2/some/other/path" in page.stdout
roles/wsgi_website/molecule/default/tests/test_parameters_paste_req.py
Show inline comments
 
file renamed from roles/wsgi_website/tests/test_parameters_paste_req.py to roles/wsgi_website/molecule/default/tests/test_parameters_paste_req.py
 
@@ -5,26 +5,26 @@ import testinfra.utils.ansible_runner
 

	
 

	
 
testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner(
 
    '.molecule/ansible_inventory').get_hosts('all')
 
    '.molecule/ansible_inventory.yml').get_hosts('all')
 

	
 

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

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

	
 
    assert group.exists
 
    assert group.gid == 5002
 

	
 

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

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

	
 
    assert user.exists
 
    assert user.uid == 5002
 
@@ -34,12 +34,12 @@ def test_website_admin_user(User):
 
    assert user.home == '/var/www/parameters-paste-req'
 

	
 

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

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

	
 
    assert home.is_directory
 
    assert home.user == 'admin-parameters-paste-req'
 
@@ -47,52 +47,52 @@ def test_website_admin_home(File, Sudo):
 
    assert home.mode == 0o750
 

	
 

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

	
 
    with Sudo():
 
    with host.sudo():
 

	
 
        directory = File('/var/www/parameters-paste-req/.profile.d')
 
        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(File, Sudo):
 
def test_virtualenv_profile_configuration(host):
 
    """
 
    Tests if profile configuration file for auto-activation of virtual
 
    environment has been deployed correctly.
 
    """
 

	
 
    with Sudo():
 
    with host.sudo():
 

	
 
        config = File('/var/www/parameters-paste-req/.profile.d/virtualenv.sh')
 
        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(Command):
 
def test_profile_configuration(host):
 
    """
 
    Tests if profile configuration is behaving correctly (setting appropriate
 
    vars via login shell).
 
    """
 

	
 
    env = Command("sudo -i -u admin-parameters-paste-req printenv VIRTUAL_ENV MY_ENV_VAR")
 
    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(Command, Sudo, User):
 
def test_website_application_user(host):
 
    """
 
    Tests if website application user has been created correctly.
 
    """
 

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

	
 
    assert user.exists
 
    assert user.uid == 998
 
@@ -101,28 +101,28 @@ def test_website_application_user(Command, Sudo, User):
 
    assert user.shell == '/bin/sh'
 
    assert user.home == '/var/www/parameters-paste-req'
 

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

	
 

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

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

	
 

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

	
 
    with Sudo():
 
    with host.sudo():
 

	
 
        config = File('/var/www/parameters-paste-req/.forward')
 
        config = host.file('/var/www/parameters-paste-req/.forward')
 
        assert config.is_file
 
        assert config.user == 'root'
 
        assert config.group == 'web-parameters-paste-req'
 
@@ -130,59 +130,61 @@ def test_forward_file(File, Sudo):
 
        assert config.content == "root"
 

	
 

	
 
def test_mail_forwarding(Command, File, Sudo):
 
def test_mail_forwarding(host):
 
    """
 
    Tests if mail forwarding works as expected.
 
    """
 

	
 
    send = Command('swaks --suppress-data --to web-parameters-paste-req@localhost')
 
    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 Sudo():
 
        mail_log = File('/var/log/mail.log')
 
    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@wsgi-website>, orig_to=<web-parameters-paste-req@localhost>.*status=sent" % message_id
 
        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(File, Sudo):
 
def test_python_virtualenv_created(host):
 
    """
 
    Tests if Python virtual environment has been created correctly.
 
    """
 

	
 
    with Sudo():
 
    with host.sudo():
 

	
 
        virtualenv = File("/var/www/parameters-paste-req/virtualenv")
 
        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 = File("/var/www/parameters-paste-req/virtualenv/bin/activate")
 
        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(File, Sudo):
 
def test_python_virtualenv_project_directory_config(host):
 
    """
 
    Tests if project directory configuration within virtualenv is set-up
 
    correctly.
 
    """
 

	
 
    with Sudo():
 
    with host.sudo():
 

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

	
 
        assert project.is_file
 
        assert project.user == 'admin-parameters-paste-req'
 
@@ -190,29 +192,29 @@ def test_python_virtualenv_project_directory_config(File, Sudo):
 
        assert project.mode == 0o640
 

	
 

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

	
 
    with Sudo():
 
    with host.sudo():
 

	
 
        wrapper = File("/var/www/parameters-paste-req/virtualenv/bin/exec")
 
        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 = Command("sudo -u admin-parameters-paste-req /var/www/parameters-paste-req/virtualenv/bin/exec python -c 'import gunicorn'")
 
        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(Command):
 
def test_virtualenv_packages(host):
 
    """
 
    Tests if correct packages are installed in virtualenv.
 
    """
 

	
 
    packages = Command("sudo -u admin-parameters-paste-req /var/www/parameters-paste-req/virtualenv/bin/pip freeze")
 
    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
 
@@ -230,26 +232,26 @@ six==1.10.0
 
wsgiref==0.1.2""".lower().split("\n"))
 

	
 

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

	
 
    with Sudo():
 
        directory = File('/etc/pip_check_requirements_upgrades/parameters-paste-req')
 
    with host.sudo():
 
        directory = host.file('/etc/pip_check_requirements_upgrades/parameters-paste-req')
 
        assert directory.is_directory
 
        assert directory.user == 'root'
 
        assert directory.group == 'pipreqcheck'
 
        assert directory.mode == 0o750
 

	
 
        config = File('/etc/pip_check_requirements_upgrades/parameters-paste-req/wsgi_requirements.in')
 
        config = host.file('/etc/pip_check_requirements_upgrades/parameters-paste-req/wsgi_requirements.in')
 
        assert config.is_file
 
        assert config.user == 'root'
 
        assert config.group == 'pipreqcheck'
 
        assert config.mode == 0o640
 
        assert config.content == "gunicorn\nfutures"
 

	
 
        config = File('/etc/pip_check_requirements_upgrades/parameters-paste-req/wsgi_requirements.txt')
 
        config = host.file('/etc/pip_check_requirements_upgrades/parameters-paste-req/wsgi_requirements.txt')
 
        assert config.is_file
 
        assert config.user == 'root'
 
        assert config.group == 'pipreqcheck'
 
@@ -257,15 +259,15 @@ def test_wsgi_requirements_upgrade_checks(File, Sudo):
 
        assert config.content == "futures==3.1.0\ngunicorn==19.7.0"
 

	
 

	
 
def test_gunicorn_requirements_installation_file(File, Sudo):
 
def test_gunicorn_requirements_installation_file(host):
 
    """
 
    Tests if requirements file for installing Gunicorn has been deployed
 
    correctly.
 
    """
 

	
 
    with Sudo():
 
    with host.sudo():
 

	
 
        requirements = File('/var/www/parameters-paste-req/.wsgi_requirements.txt')
 
        requirements = host.file('/var/www/parameters-paste-req/.wsgi_requirements.txt')
 
        assert requirements.is_file
 
        assert requirements.user == 'admin-parameters-paste-req'
 
        assert requirements.group == 'web-parameters-paste-req'
 
@@ -273,7 +275,7 @@ def test_gunicorn_requirements_installation_file(File, Sudo):
 
        assert requirements.content == "futures==3.1.0\ngunicorn==19.7.0"
 

	
 

	
 
def test_index_page(Command):
 
def test_index_page(host):
 
    """
 
    Tests if index page is served correctly. This covers:
 

	
 
@@ -282,7 +284,7 @@ def test_index_page(Command):
 
    - Handling of proxy headers.
 
    """
 

	
 
    page = Command('curl -H "Accept-Encoding: plain" https://parameters-paste-req/')
 
    page = host.run('curl -H "Accept-Encoding: plain" https://parameters-paste-req/')
 

	
 
    assert page.rc == 0
 
    assert "This is the WSGI application at parameters-paste-req." in page.stdout
 
@@ -291,17 +293,17 @@ def test_index_page(Command):
 
    assert "Accept-Encoding: plain" in page.stdout
 

	
 

	
 
def test_static_file_serving(Command):
 
def test_static_file_serving(host):
 
    """
 
    Tests serving of static files.
 
    """
 

	
 
    page = Command('curl https://parameters-paste-req/static/static_file.txt')
 
    page = host.run('curl https://parameters-paste-req/static/static_file.txt')
 
    assert page.rc == 0
 
    assert "This is the WSGI application at parameters-paste-req." in page.stdout
 
    assert "Requested URL was: https://parameters-paste-req/static/static_file.txt" in page.stdout
 

	
 
    page = Command('curl https://parameters-paste-req/media/media_file.txt')
 
    page = host.run('curl https://parameters-paste-req/media/media_file.txt')
 
    assert page.rc == 0
 
    assert "This is the WSGI application at parameters-paste-req." in page.stdout
 
    assert "Requested URL was: https://parameters-paste-req/media/media_file.txt" in page.stdout
0 comments (0 inline, 0 general)