Skip to content

Commit 3f312cc

Browse files
Passing the success_exit_codes to facts (#755)
* Passing the success_exit_codes to facts. * Fixed existing tests. * Added test for sucess_codes. * Minor fix from code review.
1 parent 76a9a56 commit 3f312cc

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

pyinfra/api/facts.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ def get_facts(
176176
shell_executable = state.config.SHELL
177177
use_sudo_password = state.config.USE_SUDO_PASSWORD
178178
env = state.config.ENV
179+
success_exit_codes = [0]
179180

180181
# Facts can override the shell (winrm powershell vs cmd support)
181182
if fact.shell_executable:
@@ -196,6 +197,7 @@ def get_facts(
196197
ignore_errors = current_global_kwargs.get('ignore_errors', ignore_errors)
197198
timeout = current_global_kwargs.get('timeout', timeout)
198199
env = current_global_kwargs.get('env', env)
200+
success_exit_codes = current_global_kwargs.get('success_exit_codes', success_exit_codes)
199201

200202
# Make a hash which keeps facts unique - but usable cross-deploy/threads.
201203
# Locks are used to maintain order.
@@ -243,6 +245,7 @@ def get_facts(
243245
sudo=sudo,
244246
sudo_user=sudo_user,
245247
use_sudo_password=use_sudo_password,
248+
success_exit_codes=success_exit_codes,
246249
su_user=su_user,
247250
timeout=timeout,
248251
env=env,

tests/test_api/test_api_facts.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ def test_get_fact(self):
3333
print_input=False,
3434
print_output=False,
3535
shell_executable='sh',
36+
success_exit_codes=[0],
3637
su_user=None,
3738
sudo=False,
3839
sudo_user=None,
@@ -72,6 +73,7 @@ def test_get_fact_current_op_meta(self):
7273
print_input=False,
7374
print_output=False,
7475
shell_executable='sh',
76+
success_exit_codes=[0],
7577
su_user='someuser',
7678
sudo=True,
7779
sudo_user='someuser',
@@ -104,6 +106,7 @@ def test_get_fact_error(self):
104106
print_input=False,
105107
print_output=False,
106108
shell_executable='sh',
109+
success_exit_codes=[0],
107110
su_user=None,
108111
sudo=False,
109112
sudo_user=None,
@@ -143,6 +146,39 @@ def test_get_fact_error_ignore(self):
143146
print_input=False,
144147
print_output=False,
145148
shell_executable='sh',
149+
success_exit_codes=[0],
150+
su_user=None,
151+
sudo=False,
152+
sudo_user=None,
153+
timeout=None,
154+
env={},
155+
use_sudo_password=False,
156+
return_combined_output=True,
157+
)
158+
159+
def test_get_fact_with_list_of_success_codes(self):
160+
inventory = make_inventory(hosts=('anotherhost',))
161+
state = State(inventory, Config())
162+
163+
anotherhost = inventory.get_host('anotherhost')
164+
165+
connect_all(state)
166+
167+
exit_codes = [1, 2, 3]
168+
with patch('pyinfra.api.connectors.ssh.run_shell_command') as fake_run_command:
169+
fake_run_command.return_value = MagicMock(), [('stdout', 'some-output')]
170+
fact_data = get_facts(state, 'command', ('yes',), {'success_exit_codes': exit_codes})
171+
172+
assert fact_data == {anotherhost: 'some-output'}
173+
174+
fake_run_command.assert_called_with(
175+
state,
176+
anotherhost,
177+
'yes',
178+
print_input=False,
179+
print_output=False,
180+
shell_executable='sh',
181+
success_exit_codes=exit_codes,
146182
su_user=None,
147183
sudo=False,
148184
sudo_user=None,

0 commit comments

Comments
 (0)