Skip to content

Commit

Permalink
pytest: Add support for defining domain and realm.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
rjeffman committed Jan 12, 2023
1 parent c5ba88d commit 1343b21
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
9 changes: 7 additions & 2 deletions tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ To run the tests run:
IPA_SERVER_HOST=<ipaserver_host_or_ip> pytest
```

To use a specific domain or realm set `IPA_SERVER_DOMAIN` (defaults to 'test.local') or `IPA_SERVER_REALM` (defaults to uppercase `IPA_SERVER_DOMAIN`):

```
IPA_SERVER_DOMAIN=<ipaserver_domain> IPA_SERVER_REALM=<ipaserver_realm> pytest
```

If you need to run using a different user you can use `ANSIBLE_REMOTE_USER`
environment variable. For example:

Expand All @@ -36,7 +42,6 @@ environment variable. For example:
IPA_SSH_PASSWORD=<ipaserver_ssh_password> IPA_SERVER_HOST=<ipaserver_host_or_ip> pytest
```


To run a single test use the full path with the following format:

```
Expand All @@ -61,7 +66,7 @@ To see why tests were skipped use `-rs`. For example:
IPA_SERVER_HOST=<ipaserver_host_or_ip> 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

Expand Down
34 changes: 31 additions & 3 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import os
import socket
import pytest
import re
import subprocess
Expand All @@ -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_INET6, 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]:
Expand Down Expand Up @@ -88,21 +101,36 @@ 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}"

sshpass = get_ssh_password()
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")

Expand Down

0 comments on commit 1343b21

Please sign in to comment.