13
13
import datetime
14
14
import os
15
15
import re
16
+ import tempfile
17
+ import textwrap
16
18
import time
17
19
from ipaddress import IPv4Address , IPv6Address , ip_address
18
20
19
21
import pytest
20
22
21
23
from testinfra .modules .socket import parse_socketspec
24
+ from testinfra .utils .ansible_runner import AnsibleRunner
22
25
23
26
all_images = pytest .mark .testinfra_hosts (
24
27
* [
@@ -329,7 +332,7 @@ def test_file(host):
329
332
330
333
331
334
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"
333
336
with pytest .raises (RuntimeError ) as excinfo :
334
337
host .ansible ("setup" )
335
338
assert expected in str (excinfo .value )
@@ -354,17 +357,6 @@ def test_ansible_module(host):
354
357
assert passwd ["state" ] in ("file" , "hard" )
355
358
assert passwd ["uid" ] == 0
356
359
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
-
368
360
with pytest .raises (host .ansible .AnsibleException ) as excinfo :
369
361
host .ansible ("command" , "zzz" )
370
362
assert excinfo .value .result ["msg" ] == "Skipped. You might want to try check=False"
@@ -380,6 +372,47 @@ def test_ansible_module(host):
380
372
assert result ["stdout" ] == "foo"
381
373
382
374
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
+
383
416
@pytest .mark .testinfra_hosts (
384
417
"ansible://debian_bookworm" , "ansible://user@debian_bookworm"
385
418
)
@@ -439,9 +472,8 @@ def test_supervisor(host, supervisorctl_path, supervisorctl_conf):
439
472
)
440
473
if service .status == "RUNNING" :
441
474
break
442
- else :
443
- assert service .status == "STARTING"
444
- time .sleep (0.5 )
475
+ assert service .status == "STARTING"
476
+ time .sleep (0.5 )
445
477
else :
446
478
raise RuntimeError ("No running tail in supervisor" )
447
479
0 commit comments