Changeset - fc2c40c98e0c
[Not reviewed]
0 7 0
Branko Majic (branko) - 5 months ago 2023-12-01 15:44:45
branko@majic.rs
MAR-189: Refactored web_server tests to be less hostname-dependent:

- Use the inventory name (alongisde distribution version) in places
where it's possible and makes sense to do so.
- Rearrange the IP allocation a bit to make more sense.
- Drop architecture information from the hostnames.
- Make the connectivity test operate over a matrix of input
parameters.
- Replace singular use of wget for testing with curl.
7 files changed with 35 insertions and 37 deletions:
0 comments (0 inline, 0 general)
roles/web_server/molecule/default/group_vars/parameters-optional.yml
Show inline comments
 
@@ -3,7 +3,7 @@
 
default_https_tls_certificate: "{{ lookup('file', 'tests/data/x509/server/{{ inventory_hostname }}_https.cert.pem') }}"
 
default_https_tls_key: "{{ lookup('file', 'tests/data/x509/server/{{ inventory_hostname }}_https.key.pem') }}"
 
web_default_title: "Optional Welcome"
 
web_default_message: "Welcome to parameters-optional, default virtual host."
 
web_default_message: "Welcome to default virtual host."
 
web_server_tls_protocols:
 
  - TLSv1.1
 
  - TLSv1.2
roles/web_server/molecule/default/molecule.yml
Show inline comments
 
@@ -14,20 +14,17 @@ lint:
 

	
 
platforms:
 

	
 
  - name: client1-buster
 
    groups:
 
      - client
 
      - buster
 
  - name: client
 
    box: debian/contrib-buster64
 
    memory: 256
 
    cpus: 1
 
    interfaces:
 
      - auto_config: true
 
        ip: 192.168.56.20
 
        ip: 192.168.56.11
 
        network_name: private_network
 
        type: static
 

	
 
  - name: parameters-mandatory-buster64
 
  - name: parameters-mandatory-buster
 
    groups:
 
      - parameters-mandatory
 
      - buster
 
@@ -36,11 +33,11 @@ platforms:
 
    cpus: 1
 
    interfaces:
 
      - auto_config: true
 
        ip: 192.168.56.30
 
        ip: 192.168.56.21
 
        network_name: private_network
 
        type: static
 

	
 
  - name: parameters-optional-buster64
 
  - name: parameters-optional-buster
 
    groups:
 
      - parameters-optional
 
      - buster
 
@@ -49,7 +46,7 @@ platforms:
 
    cpus: 1
 
    interfaces:
 
      - auto_config: true
 
        ip: 192.168.56.31
 
        ip: 192.168.56.22
 
        network_name: private_network
 
        type: static
 

	
roles/web_server/molecule/default/prepare.yml
Show inline comments
 
@@ -23,10 +23,10 @@
 
          - "{{ item.name }}"
 
          - "{{ item.fqdn }}"
 
      with_items:
 
        - name: parameters-mandatory-buster64_https
 
          fqdn: parameters-mandatory
 
        - name: parameters-optional-buster64_https
 
          fqdn: parameters-optional
 
        - name: parameters-mandatory-buster_https
 
          fqdn: parameters-mandatory-buster
 
        - name: parameters-optional-buster_https
 
          fqdn: parameters-optional-buster
 

	
 
    - name: Set-up link to generated X.509 material
 
      file:
 
@@ -59,7 +59,7 @@
 
          - nmap
 
        state: present
 

	
 
- hosts: buster
 
- hosts: all
 
  become: true
 
  tasks:
 

	
 
@@ -73,9 +73,9 @@
 
        mode: 0644
 
        state: present
 
      with_dict:
 
        192.168.56.20: "client1"
 
        192.168.56.30: "parameters-mandatory"
 
        192.168.56.31: "parameters-optional"
 
        192.168.56.11: "client"
 
        192.168.56.21: "parameters-mandatory-buster"
 
        192.168.56.22: "parameters-optional-buster"
 

	
 
    - name: Install curl for testing redirects and webpage content
 
      apt:
roles/web_server/molecule/default/tests/test_client.py
Show inline comments
 
import os
 

	
 
import pytest
 

	
 
import testinfra.utils.ansible_runner
 

	
 

	
 
@@ -7,17 +9,16 @@ testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner(
 
    os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('client')
 

	
 

	
 
def test_connectivity(host):
 
@pytest.mark.parametrize('server', testinfra.utils.ansible_runner.AnsibleRunner(
 
    os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('parameters-*')
 
)
 
@pytest.mark.parametrize('port', [80, 443])
 
def test_connectivity(host, server, port):
 
    """
 
    Tests connectivity to the web server (ports that should be reachable).
 
    """
 

	
 
    with host.sudo():
 

	
 
        for server in ["parameters-mandatory",
 
                       "parameters-optional"]:
 
            # HTTP, HTTPS.
 
            for port in [80, 443]:
 

	
 
                ping = host.run('hping3 -S -p %s -c 1 %s', str(port), server)
 
                assert ping.rc == 0
 
        ping = host.run('hping3 -S -p %s -c 1 %s', str(port), server)
 
        assert ping.rc == 0
roles/web_server/molecule/default/tests/test_default.py
Show inline comments
 
@@ -343,10 +343,9 @@ def test_tls_enabled(host):
 
    Tests if TLS has been enabled.
 
    """
 

	
 
    hostname = host.run('hostname').stdout.strip()
 
    fqdn = hostname[:hostname.rfind('-')]
 
    hostname = host.ansible.get_variables()['inventory_hostname']
 

	
 
    tls = host.run('wget -q -O - https://%s/', fqdn)
 
    tls = host.run('curl https://%s/', hostname)
 
    assert tls.rc == 0
 

	
 

	
 
@@ -355,16 +354,15 @@ def test_https_enforcement(host):
 
    Tests if HTTPS is being enforced.
 
    """
 

	
 
    hostname = host.run('hostname').stdout.strip()
 
    fqdn = hostname[:hostname.rfind('-')]
 
    hostname = host.ansible.get_variables()['inventory_hostname']
 

	
 
    https_enforcement = host.run('curl -I http://%s/', fqdn)
 
    https_enforcement = host.run('curl -I http://%s/', hostname)
 

	
 
    assert https_enforcement.rc == 0
 
    assert 'HTTP/1.1 301 Moved Permanently' in https_enforcement.stdout
 
    assert 'Location: https://%s/' % fqdn in https_enforcement.stdout
 
    assert 'Location: https://%s/' % hostname in https_enforcement.stdout
 

	
 
    https_enforcement = host.run('curl -I https://%s/', fqdn)
 
    https_enforcement = host.run('curl -I https://%s/', hostname)
 

	
 
    assert https_enforcement.rc == 0
 
    assert 'Strict-Transport-Security: max-age=31536000; includeSubDomains' in https_enforcement.stdout
roles/web_server/molecule/default/tests/test_mandatory.py
Show inline comments
 
@@ -53,7 +53,8 @@ def test_default_vhost_index_page(host):
 
    Tests content of default vhost index page.
 
    """
 

	
 
    page = host.run('curl https://parameters-mandatory/')
 
    hostname = host.ansible.get_variables()['inventory_hostname']
 
    page = host.run('curl https://%s/', hostname)
 

	
 
    assert page.rc == 0
 
    assert "<title>Welcome</title>" in page.stdout
roles/web_server/molecule/default/tests/test_optional.py
Show inline comments
 
@@ -56,9 +56,10 @@ def test_default_vhost_index_page(host):
 
    Tests content of default vhost index page.
 
    """
 

	
 
    page = host.run('curl https://parameters-optional/')
 
    hostname = host.ansible.get_variables()['inventory_hostname']
 
    page = host.run('curl https://%s/', hostname)
 

	
 
    assert page.rc == 0
 
    assert "<title>Optional Welcome</title>" in page.stdout
 
    assert "<h1>Optional Welcome</h1>" in page.stdout
 
    assert "<p>Welcome to parameters-optional, default virtual host.</p>" in page.stdout
 
    assert "<p>Welcome to default virtual host.</p>" in page.stdout
0 comments (0 inline, 0 general)