Changeset - d31ae08379b3
[Not reviewed]
0 3 0
Branko Majic (branko) - 10 months ago 2025-01-22 00:11:28
branko@majic.rs
MAR-233: Replace hping3 with nmap in mail_forwarder connectivity tests.
3 files changed with 11 insertions and 10 deletions:
0 comments (0 inline, 0 general)
roles/mail_forwarder/molecule/default/prepare.yml
Show inline comments
 
@@ -71,25 +71,25 @@
 
- name: Prepare, helpers
 
  hosts: clients
 
  become: true
 
  tasks:
 

	
 
    - name: Install SWAKS for testing SMTP capability
 
      ansible.builtin.apt:
 
        name: swaks
 
        state: present
 

	
 
    - name: Install tool for testing TCP connectivity
 
      ansible.builtin.apt:
 
        name: hping3
 
        name: nmap
 
        state: present
 

	
 
    - name: Deploy CA certificate
 
      ansible.builtin.copy:
 
        src: tests/data/x509/ca/level1.cert.pem
 
        dest: /usr/local/share/ca-certificates/testca.crt
 
        owner: root
 
        group: root
 
        mode: "0644"
 
      notify:
 
        - Update CA certificate cache
 

	
 
@@ -140,25 +140,25 @@
 
    - name: Deploy Postfix configuration
 
      ansible.builtin.copy:
 
        src: tests/data/main.cf
 
        dest: /etc/postfix/main.cf
 
        owner: root
 
        group: root
 
        mode: "0644"
 
      notify:
 
        - Restart Postfix
 

	
 
    - name: Install tool for testing TCP connectivity
 
      ansible.builtin.apt:
 
        name: hping3
 
        name: nmap
 
        state: present
 

	
 
    - name: Install SWAKS for testing SMTP capability
 
      ansible.builtin.apt:
 
        name: swaks
 
        state: present
 

	
 
    - name: Set-up port forwarding
 
      ansible.builtin.command: "iptables -t nat -A PREROUTING -p tcp -m tcp --dport 27 -j REDIRECT --to-ports 25"
 
      changed_when: false
 

	
 
  handlers:
roles/mail_forwarder/molecule/default/tests/test_connectivity_from_client.py
Show inline comments
 
@@ -14,15 +14,15 @@ ansible_runner = testinfra.utils.ansible_runner.AnsibleRunner(
 
@pytest.mark.parametrize("server",
 
                         sorted(
 
                             set(ansible_runner.get_hosts('all')) -
 
                             set(ansible_runner.get_hosts('helper'))))
 
def test_connectivity_from_client(host, server):
 
    """
 
    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 %s', server)
 
        assert ping.rc != 0
 
        assert "100% packet loss" in ping.stderr
 
        scan = host.run('nmap -4 -p 25 -oG - %s', server)
 
        assert scan.rc == 0
 
        assert "Ports: 25/filtered/tcp//smtp/" in scan.stdout
roles/mail_forwarder/molecule/default/tests/test_connectivity_from_relay.py
Show inline comments
 
@@ -12,43 +12,44 @@ ansible_runner = testinfra.utils.ansible_runner.AnsibleRunner(
 

	
 

	
 
@pytest.mark.parametrize("server",
 
                         ansible_runner.get_hosts('parameters-optional'))
 
def test_connectivity_from_authorised_relay(host, server):
 
    """
 
    Tests connectivity towards mail forwarder servers from authorised
 
    relay.
 
    """
 

	
 
    with host.sudo():
 

	
 
        ping = host.run('hping3 -S -p 25 -c 1 %s', server)
 
        assert ping.rc == 0
 
        scan = host.run('nmap -p 25 -oG - %s', server)
 
        assert scan.rc == 0
 
        assert "Ports: 25/open/tcp//smtp/" in scan.stdout
 

	
 

	
 
@pytest.mark.parametrize("server",
 
                         sorted(
 
                             set(ansible_runner.get_hosts('parameters-mandatory')) |
 
                             set(ansible_runner.get_hosts('parameters-no-incoming'))))
 
def test_connectivity_from_unauthorised_relay(host, server):
 
    """
 
    Tests connectivity towards mail forwarder servers from unauthorised
 
    relay.
 
    """
 

	
 
    with host.sudo():
 

	
 
        ping = host.run('hping3 -S -p 25 -c 1 %s', server)
 
        assert ping.rc != 0
 
        assert "100% packet loss" in ping.stderr
 
        scan = host.run('nmap -p 25 -oG - %s', server)
 
        assert scan.rc == 0
 
        assert "Ports: 25/filtered/tcp//smtp/" in scan.stdout
 

	
 

	
 
@pytest.mark.parametrize("server",
 
                         ansible_runner.get_hosts('parameters-optional'))
 
def test_mail_reception_from_authorised_relay(host, server):
 
    """
 
    Tests if mails can be sent from relay to servers configured to use the
 
    relay.
 
    """
 

	
 
    send = host.run('swaks --suppress-data --to root@{server} --server {server}'.format(server=server))
 
    assert send.rc == 0
0 comments (0 inline, 0 general)