From f6bd1ff55982b3ad02dd728294ede90556bdb389 2024-02-10 13:17:27 From: Branko Majic Date: 2024-02-10 13:17:27 Subject: [PATCH] MAR-191: Make tests for allowed/disallowed connections more generic: - Use dynamic inventory to retrive the list of hosts instead of hard-coding release names in the tests. --- diff --git a/roles/common/molecule/default/tests/test_maintenance_from_allowed_client.py b/roles/common/molecule/default/tests/test_maintenance_from_allowed_client.py index 509c02dc56c8370ede0755198165b43ca626f335..b1f95b21bca54a9b388ec354f1e4f55101c91c78 100644 --- a/roles/common/molecule/default/tests/test_maintenance_from_allowed_client.py +++ b/roles/common/molecule/default/tests/test_maintenance_from_allowed_client.py @@ -8,38 +8,32 @@ import testinfra.utils.ansible_runner testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner( os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('client-allowed') +parameters_mandatory_hosts = testinfra.utils.ansible_runner.AnsibleRunner( + os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('parameters-mandatory') -@pytest.mark.parametrize("platform", [ - "buster", - "bullseye" -]) -def test_ssh_connectivity(host, platform): +parameters_optional_hosts = testinfra.utils.ansible_runner.AnsibleRunner( + os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('parameters-optional') + + +@pytest.mark.parametrize("target_host", parameters_mandatory_hosts + parameters_optional_hosts) +def test_ssh_connectivity(host, target_host): """ Test if SSH server is reachable. """ with host.sudo(): - ping = host.run('hping3 -S -p 22 -c 1 %s', 'parameters-mandatory-%s' % platform) - assert ping.rc == 0 - - ping = host.run('hping3 -S -p 22 -c 1 %s', 'parameters-optional-%s' % platform) + ping = host.run('hping3 -S -p 22 -c 1 %s', target_host) assert ping.rc == 0 -@pytest.mark.parametrize("platform", [ - "buster", - "bullseye" -]) -def test_http_connectivity(host, platform): +@pytest.mark.parametrize("target_host", parameters_mandatory_hosts + parameters_optional_hosts) +def test_http_connectivity(host, target_host): """ Test if HTTP server is reachable. """ with host.sudo(): - ping = host.run('hping3 -S -p 80 -c 1 %s', 'parameters-mandatory-%s' % platform) - assert ping.rc == 0 - - ping = host.run('hping3 -S -p 80 -c 1 %s', 'parameters-optional-%s' % platform) + ping = host.run('hping3 -S -p 80 -c 1 %s', target_host) assert ping.rc == 0 diff --git a/roles/common/molecule/default/tests/test_maintenance_from_disallowed_client.py b/roles/common/molecule/default/tests/test_maintenance_from_disallowed_client.py index 5e6979aad57c6886f1dfe12aad418749a7cf9cee..ac7376367f4b1818eda5530b6f2e5979ad412a1a 100644 --- a/roles/common/molecule/default/tests/test_maintenance_from_disallowed_client.py +++ b/roles/common/molecule/default/tests/test_maintenance_from_disallowed_client.py @@ -8,38 +8,44 @@ import testinfra.utils.ansible_runner testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner( os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('client-disallowed') +parameters_mandatory_hosts = testinfra.utils.ansible_runner.AnsibleRunner( + os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('parameters-mandatory') -@pytest.mark.parametrize("platform", [ - "buster", - "bullseye" -]) -def test_ssh_connectivity(host, platform): +parameters_optional_hosts = testinfra.utils.ansible_runner.AnsibleRunner( + os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('parameters-optional') + + +@pytest.mark.parametrize("target_host", parameters_mandatory_hosts + parameters_optional_hosts) +def test_ssh_connectivity(host, target_host): """ Test if SSH server is reachable. """ with host.sudo(): - ping = host.run('hping3 -S -p 22 -c 1 %s', 'parameters-mandatory-%s' % platform) - assert ping.rc == 0 - - ping = host.run('hping3 -S -p 22 -c 1 %s', 'parameters-optional-%s' % platform) + ping = host.run('hping3 -S -p 22 -c 1 %s', '%s' % target_host) assert ping.rc == 0 -@pytest.mark.parametrize("platform", [ - "buster", - "bullseye" -]) -def test_http_connectivity(host, platform): +@pytest.mark.parametrize("target_host", parameters_mandatory_hosts) +def test_http_connectivity_allowed(host, target_host): """ Test if HTTP server is reachable. """ with host.sudo(): - ping = host.run('hping3 -S -p 80 -c 1 %s', 'parameters-mandatory-%s' % platform) + ping = host.run('hping3 -S -p 80 -c 1 %s', target_host) assert ping.rc == 0 - ping = host.run('hping3 -S -p 80 -c 1 %s', 'parameters-optional-%s' % platform) - assert ping.rc != 0 + +@pytest.mark.parametrize("target_host", parameters_optional_hosts) +def test_http_connectivity_disallowed(host, target_host): + """ + Test if HTTP server is reachable. + """ + + with host.sudo(): + + ping = host.run('hping3 -S -p 80 -c 1 %s', target_host) + assert ping.rc == 1