diff --git a/changelogs/fragments/ansible_test_os_system.yml b/changelogs/fragments/ansible_test_os_system.yml new file mode 100644 index 00000000000..cb23751655f --- /dev/null +++ b/changelogs/fragments/ansible_test_os_system.yml @@ -0,0 +1,3 @@ +--- +minor_changes: + - ansible-test - add a rule for os.system call (https://github.com/ansible/ansible/issues/86250). diff --git a/test/lib/ansible_test/_util/controller/sanity/validate-modules/validate_modules/constants.py b/test/lib/ansible_test/_util/controller/sanity/validate-modules/validate_modules/constants.py index 18cbf665347..c769c9a70c6 100644 --- a/test/lib/ansible_test/_util/controller/sanity/validate-modules/validate_modules/constants.py +++ b/test/lib/ansible_test/_util/controller/sanity/validate-modules/validate_modules/constants.py @@ -60,6 +60,7 @@ REJECTLIST_IMPORTS = { } SUBPROCESS_REGEX = re.compile(r'subprocess\.Po.*') OS_CALL_REGEX = re.compile(r'os\.call.*') +OS_SYSTEM_REGEX = re.compile(r'os\.system.*') PLUGINS_WITH_RETURN_VALUES = ('module', ) diff --git a/test/lib/ansible_test/_util/controller/sanity/validate-modules/validate_modules/main.py b/test/lib/ansible_test/_util/controller/sanity/validate-modules/validate_modules/main.py index e9a15377f02..80506a6aba1 100644 --- a/test/lib/ansible_test/_util/controller/sanity/validate-modules/validate_modules/main.py +++ b/test/lib/ansible_test/_util/controller/sanity/validate-modules/validate_modules/main.py @@ -93,6 +93,7 @@ from .constants import ( REJECTLIST_IMPORTS, SUBPROCESS_REGEX, OS_CALL_REGEX, + OS_SYSTEM_REGEX, PLUGINS_WITH_RETURN_VALUES, PLUGINS_WITH_EXAMPLES, PLUGINS_WITH_YAML_EXAMPLES, @@ -483,6 +484,19 @@ class ModuleValidator(Validator): column=(os_call_match.span()[0] + 1) ) + def _check_for_os_system(self): + if 'os.system' in self.text: + for line_no, line in enumerate(self.text.splitlines()): + os_system_match = OS_SYSTEM_REGEX.search(line) + if os_system_match: + self.reporter.error( + path=self.object_path, + code='use-run-command-not-os-system', + msg=('os.system() call found. Should be module.run_command'), + line=(line_no + 1), + column=(os_system_match.span()[0] + 1) + ) + def _find_rejectlist_imports(self): for child in self.ast.body: names = [] @@ -2448,6 +2462,7 @@ class ModuleValidator(Validator): if self.plugin_type == 'module': self._check_for_subprocess() self._check_for_os_call() + self._check_for_os_system() if self._powershell_module(): self._validate_ps_replacers()