Skip to content

Commit

Permalink
fix test helper handling of rc (#8387)
Browse files Browse the repository at this point in the history
* fix test helper handling of rc

* fix side_effect logic for rc != 0

* fix side_effect func + sanity tests

* fix ignore files

* fix code

* revamp the generator for run_command calls returns in testcase

* remove unused import

* Update tests/sanity/ignore-2.18.txt

Co-authored-by: Felix Fontein <felix@fontein.de>

* Update tests/sanity/ignore-2.17.txt

Co-authored-by: Felix Fontein <felix@fontein.de>

---------

Co-authored-by: Felix Fontein <felix@fontein.de>
  • Loading branch information
russoz and felixfontein authored May 24, 2024
1 parent 4792e21 commit da2c87c
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 8 deletions.
1 change: 1 addition & 0 deletions tests/sanity/ignore-2.17.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ plugins/modules/udm_user.py import-3.12 # Uses deprecated stdlib library 'crypt
plugins/modules/xfconf.py validate-modules:return-syntax-error
plugins/module_utils/univention_umc.py pylint:use-yield-from # suggested construct does not work with Python 2
tests/unit/compat/mock.py pylint:use-yield-from # suggested construct does not work with Python 2
tests/unit/plugins/modules/helper.py pylint:use-yield-from # suggested construct does not work with Python 2
tests/unit/plugins/modules/test_gio_mime.yaml no-smart-quotes
1 change: 1 addition & 0 deletions tests/sanity/ignore-2.18.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ plugins/modules/udm_user.py import-3.12 # Uses deprecated stdlib library 'crypt
plugins/modules/xfconf.py validate-modules:return-syntax-error
plugins/module_utils/univention_umc.py pylint:use-yield-from # suggested construct does not work with Python 2
tests/unit/compat/mock.py pylint:use-yield-from # suggested construct does not work with Python 2
tests/unit/plugins/modules/helper.py pylint:use-yield-from # suggested construct does not work with Python 2
tests/unit/plugins/modules/test_gio_mime.yaml no-smart-quotes
20 changes: 14 additions & 6 deletions tests/unit/plugins/modules/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import sys
import json
from collections import namedtuple
from itertools import chain, repeat

import pytest
import yaml
Expand Down Expand Up @@ -76,12 +75,21 @@ def __init__(self, *args, **kwargs):
self.mock_run_cmd = self._make_mock_run_cmd()

def _make_mock_run_cmd(self):
call_results = [(x.rc, x.out, x.err) for x in self.run_cmd_calls]
error_call_results = (123,
"OUT: testcase has not enough run_command calls",
"ERR: testcase has not enough run_command calls")
def _results():
for result in [(x.rc, x.out, x.err) for x in self.run_cmd_calls]:
yield result
raise Exception("testcase has not enough run_command calls")

results = _results()

def side_effect(self_, **kwargs):
result = next(results)
if kwargs.get("check_rc", False) and result[0] != 0:
raise Exception("rc = {0}".format(result[0]))
return result

mock_run_command = self.mocker.patch('ansible.module_utils.basic.AnsibleModule.run_command',
side_effect=chain(call_results, repeat(error_call_results)))
side_effect=side_effect)
return mock_run_command

def check_results(self, results):
Expand Down
2 changes: 0 additions & 2 deletions tests/unit/plugins/modules/test_django_command.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@
settings: whatever.settings
output:
failed: true
flags:
xfail: not seem to be failing as it should
run_command_calls:
- command: [/testbin/python, -m, django, check, --no-color, --settings=whatever.settings, babaloo, yaba, daba, doo]
environ: *env-def
Expand Down

0 comments on commit da2c87c

Please sign in to comment.