Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extended testing of group_names #771

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 47 additions & 15 deletions test/test_modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@
import datetime
import os
import re
import tempfile
import textwrap
import time
from ipaddress import IPv4Address, IPv6Address, ip_address

import pytest

from testinfra.modules.socket import parse_socketspec
from testinfra.utils.ansible_runner import AnsibleRunner

all_images = pytest.mark.testinfra_hosts(
*[
Expand Down Expand Up @@ -329,7 +332,7 @@ def test_file(host):


def test_ansible_unavailable(host):
expected = "Ansible module is only available with " "ansible connection backend"
expected = "Ansible module is only available with ansible connection backend"
with pytest.raises(RuntimeError) as excinfo:
host.ansible("setup")
assert expected in str(excinfo.value)
Expand All @@ -354,17 +357,6 @@ def test_ansible_module(host):
assert passwd["state"] in ("file", "hard")
assert passwd["uid"] == 0

variables = host.ansible.get_variables()
assert variables["myvar"] == "foo"
assert variables["myhostvar"] == "bar"
assert variables["mygroupvar"] == "qux"
assert variables["inventory_hostname"] == "debian_bookworm"
assert variables["group_names"] == ["all", "testgroup"]
assert variables["groups"] == {
"all": ["debian_bookworm"],
"testgroup": ["debian_bookworm"],
}

with pytest.raises(host.ansible.AnsibleException) as excinfo:
host.ansible("command", "zzz")
assert excinfo.value.result["msg"] == "Skipped. You might want to try check=False"
Expand All @@ -380,6 +372,47 @@ def test_ansible_module(host):
assert result["stdout"] == "foo"


@pytest.mark.testinfra_hosts("ansible://debian_bookworm")
def test_ansible_get_variables_flat_wo_child_groups(host):
"""Test AnsibleRunner.get_variables() with parent groups only"""
variables = host.ansible.get_variables()
assert variables["myvar"] == "foo"
assert variables["myhostvar"] == "bar"
assert variables["mygroupvar"] == "qux"
assert variables["inventory_hostname"] == "debian_bookworm"
assert variables["group_names"] == ["all", "testgroup"]
assert variables["groups"] == {
"all": ["debian_bookworm"],
"testgroup": ["debian_bookworm"],
}


def test_ansible_get_variables_w_child_groups():
"""Test AnsibleRunner.get_variables() with parent and child groups"""
inventory = """
host_a
[toplevel1]
host_b
[toplevel2]
host_c
[toplevel3:children]
toplevel1
"""
with tempfile.NamedTemporaryFile(mode="wt", encoding="ascii") as file_inventory:
file_inventory.write(textwrap.dedent(inventory.strip()))
file_inventory.flush()

get_variables = AnsibleRunner(file_inventory.name).get_variables

assert get_variables("host_a")["group_names"] == ["all", "ungrouped"]
assert get_variables("host_b")["group_names"] == [
"all",
"toplevel1",
"toplevel3",
]
assert get_variables("host_c")["group_names"] == ["all", "toplevel2"]


@pytest.mark.testinfra_hosts(
"ansible://debian_bookworm", "ansible://user@debian_bookworm"
)
Expand Down Expand Up @@ -439,9 +472,8 @@ def test_supervisor(host, supervisorctl_path, supervisorctl_conf):
)
if service.status == "RUNNING":
break
else:
assert service.status == "STARTING"
time.sleep(0.5)
assert service.status == "STARTING"
time.sleep(0.5)
else:
raise RuntimeError("No running tail in supervisor")

Expand Down