Changeset - fb5e4e372902
[Not reviewed]
14 1 17
Branko Majic (branko) - 6 years ago 2017-11-26 17:10:15
branko@majic.rs
MAR-128: Upgraded tests for mail_forwarder 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.
- Rearranged IP addresses for test instances a bit.
- Expanded on available hosts for testing all OS/paramter
combinations.
- Switched to using a more resilient way to populate /etc/hosts during
prepartion phase.
- Expanded connectivity tests to include all the introduced hosts.
21 files changed with 466 insertions and 249 deletions:
0 comments (0 inline, 0 general)
roles/mail_forwarder/meta/main.yml
Show inline comments
 
---
 

	
 
dependencies:
 
  - common
 
\ No newline at end of file
 
  - common
roles/mail_forwarder/molecule.yml
Show inline comments
 
deleted file
roles/mail_forwarder/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/mail_forwarder/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/mail_forwarder/molecule/default/molecule.yml
Show inline comments
 
new file 100644
 
---
 

	
 
dependency: {}
 

	
 
driver:
 
  name: vagrant
 
  provider:
 
    name: virtualbox
 

	
 
lint:
 
  name: yamllint
 

	
 
platforms:
 

	
 
  - name: mail-server
 
    groups:
 
      - mail-servers
 
    box: debian/contrib-jessie64
 
    memory: 256
 
    cpus: 1
 
    interfaces:
 
      - auto_config: true
 
        ip: 10.31.127.10
 
        network_name: private_network
 
        type: static
 

	
 
  - name: client1
 
    groups:
 
      - clients
 
    box: debian/contrib-jessie64
 
    memory: 256
 
    cpus: 1
 
    interfaces:
 
      - auto_config: true
 
        ip: 10.31.127.11
 
        network_name: private_network
 
        type: static
 

	
 
  - name: parameters-mandatory-jessie64
 
    groups:
 
      - parameters-mandatory
 
    box: debian/contrib-jessie64
 
    memory: 256
 
    cpus: 1
 
    interfaces:
 
      - auto_config: true
 
        ip: 10.31.127.20
 
        network_name: private_network
 
        type: static
 

	
 
  - name: parameters-optional-jessie64
 
    groups:
 
      - parameters-optional
 
    box: debian/contrib-jessie64
 
    memory: 256
 
    cpus: 1
 
    interfaces:
 
      - auto_config: true
 
        ip: 10.31.127.21
 
        network_name: private_network
 
        type: static
 

	
 
  - name: parameters-no-incoming-jessie64
 
    groups:
 
      - parameters-no-incoming
 
    box: debian/contrib-jessie64
 
    memory: 256
 
    cpus: 1
 
    interfaces:
 
      - auto_config: true
 
        ip: 10.31.127.22
 
        network_name: private_network
 
        type: static
 

	
 
  - name: parameters-mandatory-stretch64
 
    groups:
 
      - parameters-mandatory
 
    box: debian/contrib-stretch64
 
    memory: 256
 
    cpus: 1
 
    interfaces:
 
      - auto_config: true
 
        ip: 10.31.127.30
 
        network_name: private_network
 
        type: static
 

	
 
  - name: parameters-optional-stretch64
 
    groups:
 
      - parameters-optional
 
    box: debian/contrib-stretch64
 
    memory: 256
 
    cpus: 1
 
    interfaces:
 
      - auto_config: true
 
        ip: 10.31.127.31
 
        network_name: private_network
 
        type: static
 

	
 
  - name: parameters-no-incoming-stretch64
 
    groups:
 
      - parameters-no-incoming
 
    box: debian/contrib-stretch64
 
    memory: 256
 
    cpus: 1
 
    interfaces:
 
      - auto_config: true
 
        ip: 10.31.127.32
 
        network_name: private_network
 
        type: static
 

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

	
 
scenario:
 
  name: default
 

	
 
verifier:
 
  name: testinfra
 
  lint:
 
    name: flake8
roles/mail_forwarder/molecule/default/playbook.yml
Show inline comments
 
new file 100644
 
---
 

	
 
- hosts: parameters-mandatory
 
  become: yes
 
  roles:
 
    - role: mail_forwarder
 

	
 
      # Global common parameters.
 
      tls_certificate_dir: tests/data/x509/
 

	
 
- hosts: parameters-optional
 
  become: yes
 
  roles:
 
    - role: mail_forwarder
 
      local_mail_aliases:
 
        root: "root testuser"
 
      smtp_from_relay_allowed: True
 
      smtp_relay_host: mail-server
 
      smtp_relay_host_port: 27
 
      smtp_relay_truststore: "{{ lookup('file', 'tests/data/x509/ca.cert.pem') }}"
 

	
 
      # common
 
      ca_certificates:
 
        testca: "{{ lookup('file', 'tests/data/x509/ca.cert.pem') }}"
 

	
 
- hosts: parameters-no-incoming
 
  become: yes
 
  roles:
 
    - role: mail_forwarder
 
      smtp_relay_host: mail-server
 
      smtp_from_relay_allowed: False
 
      smtp_relay_truststore: "{{ lookup('file', 'tests/data/x509/ca.cert.pem') }}"
 

	
 
      # common
 
      ca_certificates:
 
        testca: "{{ lookup('file', 'tests/data/x509/ca.cert.pem') }}"
roles/mail_forwarder/molecule/default/prepare.yml
Show inline comments
 
file renamed from roles/mail_forwarder/playbook.yml to roles/mail_forwarder/molecule/default/prepare.yml
 
---
 

	
 
- 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: all
 
  become: yes
 
  tasks:
 

	
 
    - name: Update all caches to avoid errors due to missing remote archives
 
@@ -9,20 +19,30 @@
 
      changed_when: False
 

	
 
- hosts: all
 
  become: yes
 
  tasks:
 

	
 
    - name: Set-up /etc/hosts entries
 
    - name: Set-up the hosts file
 
      lineinfile:
 
        dest: /etc/hosts
 
        path: /etc/hosts
 
        regexp: "^{{ item.key }}"
 
        line: "{{ item.key }} {{ item.value }}"
 
        owner: root
 
        group: root
 
        mode: 0644
 
        state: present
 
      with_dict:
 
        10.31.127.10: "mail-server domain1"
 
        10.31.127.20: "client1"
 
        10.31.127.30: "parameters-mandatory"
 
        10.31.127.31: "parameters-optional"
 
        10.31.127.32: "parameters-no-incoming"
 

	
 
- hosts: client1
 
        10.31.127.11: "client1"
 
        10.31.127.20: "parameters-mandatory-jessie64"
 
        10.31.127.21: "parameters-optional-jessie64"
 
        10.31.127.22: "parameters-no-incoming-jessie64"
 
        10.31.127.30: "parameters-mandatory-stretch64"
 
        10.31.127.31: "parameters-optional-stretch64"
 
        10.31.127.32: "parameters-no-incoming-stretch64"
 

	
 
- hosts: clients
 
  become: yes
 
  tasks:
 

	
 
    - name: Install SWAKS for testing SMTP capability
 
@@ -50,7 +70,8 @@
 
    - name: Update CA certificate cache
 
      command: /usr/sbin/update-ca-certificates --fresh
 

	
 
- hosts: mail-server
 
- hosts: mail-servers
 
  become: yes
 
  tasks:
 

	
 
    - name: Deploy CA certificate
 
@@ -114,39 +135,8 @@
 
        name: postfix
 
        state: restarted
 

	
 
- hosts: parameters-mandatory
 
  roles:
 
    - role: mail_forwarder
 

	
 
      # Global common parameters.
 
      tls_certificate_dir: tests/data/x509/
 

	
 
- hosts: parameters-optional
 
  roles:
 
    - role: mail_forwarder
 
      local_mail_aliases:
 
        root: "root testuser"
 
      smtp_from_relay_allowed: True
 
      smtp_relay_host: mail-server
 
      smtp_relay_host_port: 27
 
      smtp_relay_truststore: "{{ lookup('file', 'tests/data/x509/ca.cert.pem') }}"
 

	
 
      # common
 
      ca_certificates:
 
        testca: "{{ lookup('file', 'tests/data/x509/ca.cert.pem') }}"
 

	
 
- hosts: parameters-no-incoming
 
  roles:
 
    - role: mail_forwarder
 
      smtp_relay_host: mail-server
 
      smtp_from_relay_allowed: False
 
      smtp_relay_truststore: "{{ lookup('file', 'tests/data/x509/ca.cert.pem') }}"
 

	
 
      # common
 
      ca_certificates:
 
        testca: "{{ lookup('file', 'tests/data/x509/ca.cert.pem') }}"
 

	
 
- hosts: parameters-optional
 
  become: yes
 
  tasks:
 

	
 
    - name: Create additional group for testing local aliases
roles/mail_forwarder/molecule/default/tests/data/main.cf
Show inline comments
 
file renamed from roles/mail_forwarder/tests/data/main.cf to roles/mail_forwarder/molecule/default/tests/data/main.cf
roles/mail_forwarder/molecule/default/tests/data/x509/ca.cert.pem
Show inline comments
 
file renamed from roles/mail_forwarder/tests/data/x509/ca.cert.pem to roles/mail_forwarder/molecule/default/tests/data/x509/ca.cert.pem
roles/mail_forwarder/molecule/default/tests/data/x509/ca.key.pem
Show inline comments
 
file renamed from roles/mail_forwarder/tests/data/x509/ca.key.pem to roles/mail_forwarder/molecule/default/tests/data/x509/ca.key.pem
roles/mail_forwarder/molecule/default/tests/data/x509/mail-server_smtp.cert.pem
Show inline comments
 
file renamed from roles/mail_forwarder/tests/data/x509/mail-server_smtp.cert.pem to roles/mail_forwarder/molecule/default/tests/data/x509/mail-server_smtp.cert.pem
roles/mail_forwarder/molecule/default/tests/data/x509/mail-server_smtp.key.pem
Show inline comments
 
file renamed from roles/mail_forwarder/tests/data/x509/mail-server_smtp.key.pem to roles/mail_forwarder/molecule/default/tests/data/x509/mail-server_smtp.key.pem
roles/mail_forwarder/molecule/default/tests/data/x509/truststore.pem
Show inline comments
 
file renamed from roles/mail_forwarder/tests/data/x509/truststore.pem to roles/mail_forwarder/molecule/default/tests/data/x509/truststore.pem
roles/mail_forwarder/molecule/default/tests/test_connectivity_from_client.py
Show inline comments
 
new file 100644
 
import testinfra.utils.ansible_runner
 

	
 

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

	
 

	
 
def test_connectivity_from_client(host):
 
    """
 
    Tests connectivity towards mail forwarder servers from client
 
    (non-relay). Connectivity should fail for both.
 
    """
 

	
 
    with host.sudo():
 

	
 
        ping = host.run('hping3 -S -p 25 -c 1 parameters-mandatory-jessie64')
 
        assert ping.rc != 0
 
        assert "100% packet loss" in ping.stderr
 

	
 
        ping = host.run('hping3 -S -p 25 -c 1 parameters-optional-jessie64')
 
        assert ping.rc != 0
 
        assert "100% packet loss" in ping.stderr
 

	
 
        ping = host.run('hping3 -S -p 25 -c 1 parameters-no-incoming-jessie64')
 
        assert "100% packet loss" in ping.stderr
 
        assert ping.rc != 0
 

	
 
        ping = host.run('hping3 -S -p 25 -c 1 parameters-mandatory-stretch64')
 
        assert ping.rc != 0
 
        assert "100% packet loss" in ping.stderr
 

	
 
        ping = host.run('hping3 -S -p 25 -c 1 parameters-optional-stretch64')
 
        assert ping.rc != 0
 
        assert "100% packet loss" in ping.stderr
 

	
 
        ping = host.run('hping3 -S -p 25 -c 1 parameters-no-incoming-stretch64')
 
        assert "100% packet loss" in ping.stderr
 
        assert ping.rc != 0
roles/mail_forwarder/molecule/default/tests/test_connectivity_from_relay.py
Show inline comments
 
new file 100644
 
import testinfra.utils.ansible_runner
 

	
 

	
 
testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner(
 
    '.molecule/ansible_inventory.yml').get_hosts('mail-server')
 

	
 

	
 
def test_connectivity_from_relay(host):
 
    """
 
    Tests connectivity towards mail forwarder servers from relay. Connection
 
    towards parameters-mandatory should fail.
 
    """
 

	
 
    with host.sudo():
 

	
 
        ping = host.run('hping3 -S -p 25 -c 1 parameters-mandatory-jessie64')
 
        assert ping.rc != 0
 
        assert "100% packet loss" in ping.stderr
 

	
 
        ping = host.run('hping3 -S -p 25 -c 1 parameters-optional-jessie64')
 
        assert ping.rc == 0
 

	
 
        ping = host.run('hping3 -S -p 25 -c 1 parameters-no-incoming-jessie64')
 
        assert "100% packet loss" in ping.stderr
 
        assert ping.rc != 0
 

	
 
        ping = host.run('hping3 -S -p 25 -c 1 parameters-mandatory-stretch64')
 
        assert ping.rc != 0
 
        assert "100% packet loss" in ping.stderr
 

	
 
        ping = host.run('hping3 -S -p 25 -c 1 parameters-optional-stretch64')
 
        assert ping.rc == 0
 

	
 
        ping = host.run('hping3 -S -p 25 -c 1 parameters-no-incoming-stretch64')
 
        assert "100% packet loss" in ping.stderr
 
        assert ping.rc != 0
 

	
 

	
 
def test_mail_reception_from_relay(host):
 
    """
 
    Tests if mails can be sent from relay to servers configured to use the
 
    relay.
 
    """
 

	
 
    send = host.run('swaks --suppress-data --to root@parameters-optional-jessie64 --server parameters-optional-jessie64')
 
    assert send.rc == 0
 

	
 
    send = host.run('swaks --suppress-data --to root@parameters-optional-stretch64 --server parameters-optional-stretch64')
 
    assert send.rc == 0
 

	
 

	
 
def test_open_relay(host):
 
    """
 
    Tests if mail forwarder behaves as open relay.
 
    """
 

	
 
    no_recipients_accepted = 24
 

	
 
    send = host.run('swaks --suppress-data --to root@client1 --server parameters-optional-jessie64')
 
    assert send.rc == no_recipients_accepted
 
    assert "Relay access denied" in send.stdout
 

	
 
    send = host.run('swaks --suppress-data --to root@client1 --server parameters-optional-stretch64')
 
    assert send.rc == no_recipients_accepted
 
    assert "Relay access denied" in send.stdout
roles/mail_forwarder/molecule/default/tests/test_default.py
Show inline comments
 
file renamed from roles/mail_forwarder/tests/test_default.py to roles/mail_forwarder/molecule/default/tests/test_default.py
 
@@ -2,33 +2,33 @@ import testinfra.utils.ansible_runner
 

	
 

	
 
testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner(
 
    '.molecule/ansible_inventory').get_hosts(['parameters-mandatory', 'parameters-optional'])
 
    '.molecule/ansible_inventory.yml').get_hosts(['parameters-mandatory', 'parameters-optional', 'parameters-no-incoming'])
 

	
 

	
 
def test_installed_packages(Package):
 
def test_installed_packages(host):
 
    """
 
    Tests if the necessary packages have been installed.
 
    """
 

	
 
    assert Package('postfix').is_installed
 
    assert Package('procmail').is_installed
 
    assert Package('swaks').is_installed
 
    assert host.package('postfix').is_installed
 
    assert host.package('procmail').is_installed
 
    assert host.package('swaks').is_installed
 

	
 

	
 
def test_removed_packages(Package):
 
def test_removed_packages(host):
 
    """
 
    Tests if certain packages have been removed from the system.
 
    """
 

	
 
    assert not Package('exim4').is_installed
 
    assert not host.package('exim4').is_installed
 

	
 

	
 
def test_smtp_relay_truststore_file(File):
 
def test_smtp_relay_truststore_file(host):
 
    """
 
    Tests if SMTP relay truststore has correct permissions
 
    """
 

	
 
    truststore = File('/etc/ssl/certs/smtp_relay_truststore.pem')
 
    truststore = host.file('/etc/ssl/certs/smtp_relay_truststore.pem')
 

	
 
    assert truststore.is_file
 
    assert truststore.user == 'root'
 
@@ -36,12 +36,12 @@ def test_smtp_relay_truststore_file(File):
 
    assert truststore.mode == 0o644
 

	
 

	
 
def test_smtp_mailname(File):
 
def test_smtp_mailname(host):
 
    """
 
    Tests if SMTP mailname configuration file has correct permissions.
 
    """
 

	
 
    mailname = File('/etc/mailname')
 
    mailname = host.file('/etc/mailname')
 

	
 
    assert mailname.is_file
 
    assert mailname.user == 'root'
 
@@ -49,35 +49,35 @@ def test_smtp_mailname(File):
 
    assert mailname.mode == 0o644
 

	
 

	
 
def test_postfix_main_cf_file(File):
 
def test_postfix_main_cf_file(host):
 
    """
 
    Tests Postfix main configuration file permissions.
 
    """
 

	
 
    config = File('/etc/postfix/main.cf')
 
    config = host.file('/etc/postfix/main.cf')
 
    assert config.is_file
 
    assert config.user == 'root'
 
    assert config.group == 'root'
 
    assert config.mode == 0o644
 

	
 

	
 
def test_services(Service):
 
def test_services(host):
 
    """
 
    Tests if all the necessary services are enabled and running.
 
    """
 

	
 
    service = Service('postfix')
 
    service = host.service('postfix')
 
    assert service.is_running
 
    assert service.is_enabled
 

	
 

	
 
def test_firewall_configuration_file(File, Sudo):
 
def test_firewall_configuration_file(host):
 
    """
 
    Tests if firewall configuration file has correct permissions.
 
    """
 

	
 
    with Sudo():
 
        config = File('/etc/ferm/conf.d/20-mail.conf')
 
    with host.sudo():
 
        config = host.file('/etc/ferm/conf.d/20-mail.conf')
 
        assert config.is_file
 
        assert config.user == 'root'
 
        assert config.group == 'root'
roles/mail_forwarder/molecule/default/tests/test_mandatory.py
Show inline comments
 
file renamed from roles/mail_forwarder/tests/test_mandatory.py to roles/mail_forwarder/molecule/default/tests/test_mandatory.py
 
@@ -6,39 +6,42 @@ import testinfra.utils.ansible_runner
 

	
 

	
 
testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner(
 
    '.molecule/ansible_inventory').get_hosts('parameters-mandatory')
 
    '.molecule/ansible_inventory.yml').get_hosts('parameters-mandatory')
 

	
 

	
 
def test_smtp_relay_truststore_file(File):
 
def test_smtp_relay_truststore_file(host):
 
    """
 
    Tests if SMTP relay truststore has correct content.
 
    """
 

	
 
    truststore = File('/etc/ssl/certs/smtp_relay_truststore.pem')
 
    truststore = host.file('/etc/ssl/certs/smtp_relay_truststore.pem')
 

	
 
    assert truststore.content == open("tests/data/x509/truststore.pem", "r").read().rstrip()
 

	
 

	
 
def test_smtp_mailname(File):
 
def test_smtp_mailname(host):
 
    """
 
    Tests if SMTP mailname configuration file has correct content.
 
    """
 

	
 
    mailname = File('/etc/mailname')
 
    hostname = host.run('hostname').stdout
 

	
 
    assert mailname.content == "parameters-mandatory"
 
    mailname = host.file('/etc/mailname')
 

	
 
    assert mailname.content == hostname
 

	
 
def test_postfix_main_cf_file_content(File):
 

	
 
def test_postfix_main_cf_file_content(host):
 
    """
 
    Tests if the Postfix main configuration file content is correct.
 
    """
 

	
 
    config = File('/etc/postfix/main.cf')
 
    hostname = host.run('hostname').stdout
 
    config = host.file('/etc/postfix/main.cf')
 
    config_lines = config.content.split("\n")
 

	
 
    assert "myhostname = parameters-mandatory" in config_lines
 
    assert "mydestination = parameters-mandatory, parameters-mandatory, localhost.localdomain, localhost" in config_lines
 
    assert "myhostname = %s" % hostname in config_lines
 
    assert "mydestination = %s, %s, localhost.localdomain, localhost" % (hostname, hostname) in config_lines
 
    assert "relayhost = " in config_lines
 
    assert "mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128" in config_lines
 
    assert "smtp_tls_security_level" not in config.content
 
@@ -46,21 +49,21 @@ def test_postfix_main_cf_file_content(File):
 
    assert "smtp_host_lookup = dns, native" in config_lines
 

	
 

	
 
def test_direct_mail_sending(Command, File, Sudo):
 
def test_direct_mail_sending(host):
 
    """
 
    Tests if mails are sent correctly directly without relay if relay has not
 
    been configured.
 
    """
 

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

	
 
    # Wait for a little while for message to be processed.
 
    time.sleep(5)
 

	
 
    with Sudo():
 
        mail_log = File('/var/log/mail.log')
 
    with host.sudo():
 
        mail_log = host.file('/var/log/mail.log')
 
        # Pattern used to verify the mail was sent directly on default port.
 
        pattern = "%s: to=<root@domain1>, relay=domain1\[[^]]*\]:25.*status=sent" % message_id
 

	
roles/mail_forwarder/molecule/default/tests/test_optional.py
Show inline comments
 
file renamed from roles/mail_forwarder/tests/test_optional.py to roles/mail_forwarder/molecule/default/tests/test_optional.py
 
@@ -6,39 +6,42 @@ import testinfra.utils.ansible_runner
 

	
 

	
 
testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner(
 
    '.molecule/ansible_inventory').get_hosts('parameters-optional')
 
    '.molecule/ansible_inventory.yml').get_hosts('parameters-optional')
 

	
 

	
 
def test_smtp_relay_truststore_file(File):
 
def test_smtp_relay_truststore_file(host):
 
    """
 
    Tests if SMTP relay truststore has correct content.
 
    """
 

	
 
    truststore = File('/etc/ssl/certs/smtp_relay_truststore.pem')
 
    truststore = host.file('/etc/ssl/certs/smtp_relay_truststore.pem')
 

	
 
    assert truststore.content == open("tests/data/x509/ca.cert.pem", "r").read().rstrip()
 

	
 

	
 
def test_smtp_mailname(File):
 
def test_smtp_mailname(host):
 
    """
 
    Tests if SMTP mailname has been configured correctly.
 
    """
 

	
 
    mailname = File('/etc/mailname')
 
    hostname = host.run('hostname').stdout
 

	
 
    assert mailname.content == "parameters-optional"
 
    mailname = host.file('/etc/mailname')
 

	
 
    assert mailname.content == "%s" % hostname
 

	
 
def test_postfix_main_cf_file_content(File):
 

	
 
def test_postfix_main_cf_file_content(host):
 
    """
 
    Tests if the Postfix main configuration file content is correct.
 
    """
 

	
 
    config = File('/etc/postfix/main.cf')
 
    hostname = host.run('hostname').stdout
 
    config = host.file('/etc/postfix/main.cf')
 
    config_lines = config.content.split("\n")
 

	
 
    assert "myhostname = parameters-optional" in config_lines
 
    assert "mydestination = parameters-optional, parameters-optional, localhost.localdomain, localhost" in config_lines
 
    assert "myhostname = %s" % hostname in config_lines
 
    assert "mydestination = %s, %s, localhost.localdomain, localhost" % (hostname, hostname) in config_lines
 
    assert "relayhost = mail-server:27" in config_lines
 
    assert "mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128" in config_lines
 
    assert "smtp_tls_security_level=verify" in config_lines
 
@@ -46,41 +49,42 @@ def test_postfix_main_cf_file_content(File):
 
    assert "smtp_host_lookup = dns, native" in config_lines
 

	
 

	
 
def test_local_aliases(Command, File, Sudo):
 
def test_local_aliases(host):
 
    """
 
    Tests if local aliases are configured correctly.
 
    """
 

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

	
 
    # Wait for a little while for message to be processed.
 
    time.sleep(5)
 

	
 
    with Sudo():
 
        mail_log = File('/var/log/mail.log')
 
        pattern1 = "%s: to=<root@parameters-optional>, orig_to=<root@localhost>.*status=sent" % message_id
 
        pattern2 = "%s: to=<testuser@parameters-optional>, orig_to=<root@localhost>.*status=sent" % message_id
 
    with host.sudo():
 
        mail_log = host.file('/var/log/mail.log')
 
        pattern1 = "%s: to=<root@%s>, orig_to=<root@localhost>.*status=sent" % (message_id, hostname)
 
        pattern2 = "%s: to=<testuser@%s>, orig_to=<root@localhost>.*status=sent" % (message_id, hostname)
 

	
 
        assert re.search(pattern1, mail_log.content) is not None
 
        assert re.search(pattern2, mail_log.content) is not None
 

	
 

	
 
def test_relay_mail_sending(Command, File, Sudo):
 
def test_relay_mail_sending(host):
 
    """
 
    Tests if mails are sent correctly via relay if relay has been configured.
 
    """
 

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

	
 
    # Wait for a little while for message to be processed.
 
    time.sleep(5)
 

	
 
    with Sudo():
 
        mail_log = File('/var/log/mail.log')
 
    with host.sudo():
 
        mail_log = host.file('/var/log/mail.log')
 
        # Pattern used to verify the mail was sent over relay on designated
 
        # port.
 
        pattern = r"%s: to=<root@domain1>, relay=mail-server\[[^]]*\]:27.*status=sent" % message_id
 
@@ -88,26 +92,26 @@ def test_relay_mail_sending(Command, File, Sudo):
 
        assert re.search(pattern, mail_log.content) is not None
 

	
 

	
 
def test_tls_enforced_towards_relay_mail_server(Command, File, Sudo):
 
def test_tls_enforced_towards_relay_mail_server(host):
 
    """
 
    Tests if TLS verification is enfoced towards the relay mail server.
 
    """
 

	
 
    with Sudo():
 
    with host.sudo():
 
        # Replace the relayhost with name that is not present in relay's
 
        # certificate.
 
        command = Command("sed -i -e s#relayhost\\ =\\ mail-server#relayhost\\ =\\ domain1# /etc/postfix/main.cf")
 
        command = host.run("sed -i -e s#relayhost\\ =\\ mail-server#relayhost\\ =\\ domain1# /etc/postfix/main.cf")
 
        assert command.rc == 0
 
        command = Command("service postfix restart")
 
        command = host.run("service postfix restart")
 
        assert command.rc == 0
 

	
 
        # Try to send out an e-mail
 
        send = Command('swaks --suppress-data --to root@domain1 --server localhost')
 
        send = host.run('swaks --suppress-data --to root@domain1 --server localhost')
 

	
 
        # Restore correct relay name in the configuration file.
 
        command = Command("sed -i -e s#relayhost\\ =\\ domain1#relayhost\\ =\\ mail-server# /etc/postfix/main.cf")
 
        command = host.run("sed -i -e s#relayhost\\ =\\ domain1#relayhost\\ =\\ mail-server# /etc/postfix/main.cf")
 
        assert command.rc == 0
 
        command = Command("service postfix restart")
 
        command = host.run("service postfix restart")
 
        assert command.rc == 0
 

	
 
        # Finally check the results.
 
@@ -117,8 +121,8 @@ def test_tls_enforced_towards_relay_mail_server(Command, File, Sudo):
 
        # Wait for a little while for message to be processed.
 
        time.sleep(5)
 

	
 
        with Sudo():
 
            mail_log = File('/var/log/mail.log')
 
        with host.sudo():
 
            mail_log = host.file('/var/log/mail.log')
 
            pattern = "%s: to=<root@domain1>, relay=domain1.*status=deferred \(Server certificate not verified\)" % message_id
 

	
 
            assert re.search(pattern, mail_log.content) is not None
roles/mail_forwarder/molecule/default/tests/test_smtp_relay_host_port.py
Show inline comments
 
file renamed from roles/mail_forwarder/tests/test_smtp_relay_host_port.py to roles/mail_forwarder/molecule/default/tests/test_smtp_relay_host_port.py
 
@@ -6,19 +6,20 @@ import testinfra.utils.ansible_runner
 

	
 

	
 
testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner(
 
    '.molecule/ansible_inventory').get_hosts('parameters-no-incoming')
 
    '.molecule/ansible_inventory.yml').get_hosts('parameters-no-incoming')
 

	
 

	
 
def test_postfix_main_cf_file_content(File):
 
def test_postfix_main_cf_file_content(host):
 
    """
 
    Tests if the Postfix main configuration file content is correct.
 
    """
 

	
 
    config = File('/etc/postfix/main.cf')
 
    hostname = host.run('hostname').stdout
 
    config = host.file('/etc/postfix/main.cf')
 
    config_lines = config.content.split("\n")
 

	
 
    assert "myhostname = parameters-no-incoming" in config_lines
 
    assert "mydestination = parameters-no-incoming, parameters-no-incoming, localhost.localdomain, localhost" in config_lines
 
    assert "myhostname = %s" % hostname in config_lines
 
    assert "mydestination = %s, %s, localhost.localdomain, localhost" % (hostname, hostname) in config_lines
 
    assert "relayhost = mail-server" in config_lines
 
    assert "mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128" in config_lines
 
    assert "smtp_tls_security_level=verify" in config_lines
 
@@ -26,20 +27,20 @@ def test_postfix_main_cf_file_content(File):
 
    assert "smtp_host_lookup = dns, native" in config_lines
 

	
 

	
 
def test_relay_mail_sending(Command, File, Sudo):
 
def test_relay_mail_sending(host):
 
    """
 
    Tests if mails are sent correctly via relay if relay has been configured.
 
    """
 

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

	
 
    # Wait for a little while for message to be processed.
 
    time.sleep(5)
 

	
 
    with Sudo():
 
        mail_log = File('/var/log/mail.log')
 
    with host.sudo():
 
        mail_log = host.file('/var/log/mail.log')
 
        # Pattern used to verify the mail was sent over relay on default port.
 
        pattern = r"%s: to=<root@domain1>, relay=mail-server\[[^]]*\]:25.*status=sent" % message_id
 

	
roles/mail_forwarder/tests/test_connectivity_from_client.py
Show inline comments
 
deleted file
roles/mail_forwarder/tests/test_connectivity_from_relay.py
Show inline comments
 
deleted file
0 comments (0 inline, 0 general)