Skip to content

Commit 6d37437

Browse files
Extended testing of group_names
1 parent fa48a25 commit 6d37437

File tree

1 file changed

+47
-15
lines changed

1 file changed

+47
-15
lines changed

test/test_modules.py

+47-15
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,15 @@
1313
import datetime
1414
import os
1515
import re
16+
import tempfile
17+
import textwrap
1618
import time
1719
from ipaddress import IPv4Address, IPv6Address, ip_address
1820

1921
import pytest
2022

2123
from testinfra.modules.socket import parse_socketspec
24+
from testinfra.utils.ansible_runner import AnsibleRunner
2225

2326
all_images = pytest.mark.testinfra_hosts(
2427
*[
@@ -329,7 +332,7 @@ def test_file(host):
329332

330333

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

357-
variables = host.ansible.get_variables()
358-
assert variables["myvar"] == "foo"
359-
assert variables["myhostvar"] == "bar"
360-
assert variables["mygroupvar"] == "qux"
361-
assert variables["inventory_hostname"] == "debian_bookworm"
362-
assert variables["group_names"] == ["all", "testgroup"]
363-
assert variables["groups"] == {
364-
"all": ["debian_bookworm"],
365-
"testgroup": ["debian_bookworm"],
366-
}
367-
368360
with pytest.raises(host.ansible.AnsibleException) as excinfo:
369361
host.ansible("command", "zzz")
370362
assert excinfo.value.result["msg"] == "Skipped. You might want to try check=False"
@@ -380,6 +372,47 @@ def test_ansible_module(host):
380372
assert result["stdout"] == "foo"
381373

382374

375+
@pytest.mark.testinfra_hosts("ansible://debian_bookworm")
376+
def test_ansible_get_variables_flat_wo_child_groups(host):
377+
"""Test AnsibleRunner.get_variables() with parent groups only"""
378+
variables = host.ansible.get_variables()
379+
assert variables["myvar"] == "foo"
380+
assert variables["myhostvar"] == "bar"
381+
assert variables["mygroupvar"] == "qux"
382+
assert variables["inventory_hostname"] == "debian_bookworm"
383+
assert variables["group_names"] == ["all", "testgroup"]
384+
assert variables["groups"] == {
385+
"all": ["debian_bookworm"],
386+
"testgroup": ["debian_bookworm"],
387+
}
388+
389+
390+
def test_ansible_get_variables_w_child_groups():
391+
"""Test AnsibleRunner.get_variables() with parent and child groups"""
392+
inventory = """
393+
host_a
394+
[toplevel1]
395+
host_b
396+
[toplevel2]
397+
host_c
398+
[toplevel3:children]
399+
toplevel1
400+
"""
401+
with tempfile.NamedTemporaryFile(encoding="ascii") as file_inventory:
402+
file_inventory.write(textwrap.dedent(inventory.strip()))
403+
file_inventory.flush()
404+
405+
get_variables = AnsibleRunner(file_inventory.name).get_variables
406+
407+
assert get_variables("host_a")["group_names"] == ["all", "ungrouped"]
408+
assert get_variables("host_b")["group_names"] == [
409+
"all",
410+
"toplevel1",
411+
"toplevel3",
412+
]
413+
assert get_variables("host_c")["group_names"] == ["all", "toplevel2"]
414+
415+
383416
@pytest.mark.testinfra_hosts(
384417
"ansible://debian_bookworm", "ansible://user@debian_bookworm"
385418
)
@@ -439,9 +472,8 @@ def test_supervisor(host, supervisorctl_path, supervisorctl_conf):
439472
)
440473
if service.status == "RUNNING":
441474
break
442-
else:
443-
assert service.status == "STARTING"
444-
time.sleep(0.5)
475+
assert service.status == "STARTING"
476+
time.sleep(0.5)
445477
else:
446478
raise RuntimeError("No running tail in supervisor")
447479

0 commit comments

Comments
 (0)