Skip to content

Commit 28c7fa7

Browse files
committed
tests: update tests to allow facts to call other facts
1 parent 1c51fed commit 28c7fa7

File tree

5 files changed

+22
-5
lines changed

5 files changed

+22
-5
lines changed

tests/facts/server.Mounts/mounts.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
"33 29 8:1 / /boot rw,relatime - vfat /dev/sda1 rw,lazytime,fmask=0022,dmask=0022,codepage=437,iocharset=iso8859-1,shortname=mixed,utf8,errors=remount-ro",
66
"408 35 0:352 / /mnt/overlay/overlay,dir\\040with\\040spaces\\040and\\040\\134backslashes ro,relatime shared:27 - overlay none ro,lowerdir=lower\\054dir,upperdir=upper\\054dir,workdir=work\\054dir"
77
],
8+
"facts": {
9+
"server.Kernel": "Linux"
10+
},
811
"fact": {
912
"/": {
1013
"device": "/dev/sdb1",

tests/operations/server.mount/remount_options.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"options": ["rw", "noatime"]
55
},
66
"facts": {
7+
"server.Kernel": "Linux",
78
"server.Mounts": {
89
"/data": {
910
"options": ["rw"]

tests/test_facts.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66

77
from pyinfra.api import StringCommand
88
from pyinfra.api.facts import ShortFactBase
9+
from pyinfra.context import ctx_host, ctx_state
910
from pyinfra_cli.util import json_encode
1011

11-
from .util import YamlTest, get_command_string
12+
from .util import FakeState, YamlTest, create_host, get_command_string
1213

1314
# show full diff on json
1415
TestCase.maxDiff = None
@@ -35,7 +36,18 @@ class TestTests(TestCase, metaclass=YamlTest):
3536
yaml_test_dir = path.join("tests", "facts", folder_name)
3637
yaml_test_prefix = "test_{0}_".format(fact.name)
3738

39+
@classmethod
40+
def setUpClass(cls):
41+
# Create a global fake state that attach to context state
42+
cls.state = FakeState()
43+
3844
def yaml_test_function(self, test_name, test_data, fact=fact):
45+
host = create_host(self.state, facts=test_data.get("facts", {}))
46+
with ctx_state.use(self.state):
47+
with ctx_host.use(host):
48+
self._test_fn(test_name, test_data, fact)
49+
50+
def _test_fn(self, test_name, test_data, fact):
3951
short_fact = None
4052

4153
if isinstance(fact, ShortFactBase):

tests/test_operations.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ def yaml_test_function(self, test_name, test_data):
111111
op_test_name = "{0}/{1}.json".format(arg, test_name)
112112

113113
# Create a host with this tests facts and attach to context host
114-
host = create_host(facts=test_data.get("facts", {}))
114+
host = create_host(self.state, facts=test_data.get("facts", {}))
115115

116116
allowed_exception = test_data.get("exception")
117117

tests/util.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,8 @@ class FakeHost:
171171
current_deploy_kwargs = None
172172
current_deploy_data = None
173173

174-
def __init__(self, name, facts, data):
174+
def __init__(self, state, name, facts, data):
175+
self.state = state
175176
self.name = name
176177
self.fact = FakeFacts(facts)
177178
self.data = data
@@ -368,7 +369,7 @@ def walk(self, dirname, topdown=True, onerror=None, followlinks=False):
368369
yield recursive_return
369370

370371

371-
def create_host(name=None, facts=None, data=None):
372+
def create_host(state, name=None, facts=None, data=None):
372373
"""
373374
Creates a FakeHost object with attached fact data.
374375
"""
@@ -380,7 +381,7 @@ def create_host(name=None, facts=None, data=None):
380381
for name, fact_data in facts.items():
381382
real_facts[name] = fact_data
382383

383-
return FakeHost(name, facts=real_facts, data=data)
384+
return FakeHost(state, name, facts=real_facts, data=data)
384385

385386

386387
class YamlTest(type):

0 commit comments

Comments
 (0)