From 386004bed3dc3c29c37571befcd9102a3ad3604f Mon Sep 17 00:00:00 2001 From: Rafael Guterres Jeffman Date: Wed, 30 Nov 2022 19:57:41 -0300 Subject: [PATCH] pytest: Add support for defining domain and realm. If running ansible-freeipa tests using pytest against an ipaserver with a domain that was not 'test.local', or did not have a zone for that domain, some tests would fail as the 'ipaserver_domain' was set to 'test.local' and some tests rely on that value to setup variables. By allowing a user to define a different domain or realm, all tests will succeed if the domain is set correctly, enhancing development experience and allowing, if needed, in the future, tests that require different domains or realms. --- tests/README.md | 9 +++++++-- tests/utils.py | 34 +++++++++++++++++++++++++++++++--- 2 files changed, 38 insertions(+), 5 deletions(-) diff --git a/tests/README.md b/tests/README.md index a08068bf30..5bb1ab80c2 100644 --- a/tests/README.md +++ b/tests/README.md @@ -22,6 +22,12 @@ To run the tests run: IPA_SERVER_HOST= pytest ``` +The default the domain used is `test.local`. if `IPA_SERVER_HOST` is provided, and is not an IP address, the default domain in inferred from it. The default realm in the uppercase version of the default domain. To use a specific domain or realm, set `IPA_SERVER_DOMAIN` or `IPA_SERVER_REALM`: + +``` +IPA_SERVER_DOMAIN= IPA_SERVER_REALM= pytest +``` + If you need to run using a different user you can use `ANSIBLE_REMOTE_USER` environment variable. For example: @@ -36,7 +42,6 @@ environment variable. For example: IPA_SSH_PASSWORD= IPA_SERVER_HOST= pytest ``` - To run a single test use the full path with the following format: ``` @@ -61,7 +66,7 @@ To see why tests were skipped use `-rs`. For example: IPA_SERVER_HOST= pytest -rs ``` -For a complete list of options check `pytest --help`. +For a complete list of `pytest` options check `pytest --help`. ### Disabling and enabling playbook tests diff --git a/tests/utils.py b/tests/utils.py index eb64bef582..bda08b1fab 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -20,6 +20,7 @@ # along with this program. If not, see . import os +import socket import pytest import re import subprocess @@ -32,6 +33,18 @@ SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__)) +def is_ip_addr(ipaddr): + """Test if given IPA_SERVER_HOST is an IP address.""" + try: + socket.inet_pton(socket.AF_INET, ipaddr) + except socket.error: + try: + socket.inet_pton(socket.AF_INET, ipaddr) + except socket.error: + return False + return True + + def get_docker_env(): docker_env = os.getenv("RUN_TESTS_IN_DOCKER", None) if docker_env in ["1", "True", "true", "yes", True]: @@ -88,8 +101,17 @@ def get_enabled_test(group_name, test_name): def get_inventory_content(): """Create the content of an inventory file for a test run.""" ipa_server_host = get_server_host() - container_engine = get_docker_env() + + if ( + ipa_server_host + and container_engine is None + and not is_ip_addr(ipa_server_host) + ): + default_domain = ipa_server_host.split(".", 1)[-1] + else: + default_domain = "test.local" + if container_engine is not None: ipa_server_host += f" ansible_connection={container_engine}" @@ -97,12 +119,18 @@ def get_inventory_content(): if sshpass: ipa_server_host += " ansible_ssh_pass=%s" % sshpass + ipaserver_domain = os.environ.get("IPA_SERVER_DOMAIN", default_domain) + ipaserver_realm = os.environ.get( + "IPA_SERVER_REALM", + ipaserver_domain.upper() + ) + lines = [ "[ipaserver]", ipa_server_host, "[ipaserver:vars]", - "ipaserver_domain=test.local", - "ipaserver_realm=TEST.LOCAL", + "ipaserver_domain=%s" % ipaserver_domain, + "ipaserver_realm=%s" % ipaserver_realm, ] return "\n".join(lines).encode("utf8")