mirror of
				https://github.com/ansible-collections/community.general.git
				synced 2025-10-25 05:23:58 -07:00 
			
		
		
		
	[PR #9918/8ab8010b backport][stable-10] Use more unit test utils from community.internal_test_tools (#9919)
Use more unit test utils from community.internal_test_tools (#9918)
* Make conftest's patch_ansible_module use the context manager from .utils.
* Fix test dependencies.
* Use module mock utils from community.internal_test_tools.
* Use DataDictLoader from community.internal_test_tools.
* Use trust util from community.internal_test_tools.
(cherry picked from commit 8ab8010b6d)
Co-authored-by: Felix Fontein <felix@fontein.de>
	
	
This commit is contained in:
		
					parent
					
						
							
								7ba3d84004
							
						
					
				
			
			
				commit
				
					
						57dcd31c82
					
				
			
		
					 94 changed files with 217 additions and 267 deletions
				
			
		|  | @ -5,45 +5,44 @@ | |||
| from __future__ import (absolute_import, division, print_function) | ||||
| __metaclass__ = type | ||||
| 
 | ||||
| import contextlib as _contextlib | ||||
| import json | ||||
| 
 | ||||
| import pytest | ||||
| 
 | ||||
| from ansible.module_utils.six import string_types | ||||
| from ansible.module_utils.common.text.converters import to_bytes | ||||
| from ansible.module_utils.common._collections_compat import MutableMapping | ||||
| 
 | ||||
| from ansible_collections.community.general.plugins.module_utils import deps | ||||
| from ansible_collections.community.internal_test_tools.tests.unit.plugins.modules.utils import set_module_args as _set_module_args | ||||
| 
 | ||||
| 
 | ||||
| def fix_ansible_args(args): | ||||
| def _fix_ansible_args(args): | ||||
|     if isinstance(args, string_types): | ||||
|         return args | ||||
|         # This should be deprecated! | ||||
|         return json.loads(args) | ||||
| 
 | ||||
|     if isinstance(args, MutableMapping): | ||||
|         if 'ANSIBLE_MODULE_ARGS' not in args: | ||||
|             args = {'ANSIBLE_MODULE_ARGS': args} | ||||
|         if '_ansible_remote_tmp' not in args['ANSIBLE_MODULE_ARGS']: | ||||
|             args['ANSIBLE_MODULE_ARGS']['_ansible_remote_tmp'] = '/tmp' | ||||
|         if '_ansible_keep_remote_files' not in args['ANSIBLE_MODULE_ARGS']: | ||||
|             args['ANSIBLE_MODULE_ARGS']['_ansible_keep_remote_files'] = False | ||||
|         args = json.dumps(args) | ||||
|         return args | ||||
| 
 | ||||
|     else: | ||||
|         raise Exception('Malformed data to the patch_ansible_module pytest fixture') | ||||
|     raise Exception('Malformed data to the patch_ansible_module pytest fixture') | ||||
| 
 | ||||
| 
 | ||||
| @pytest.fixture | ||||
| def patch_ansible_module(request, mocker): | ||||
|     if hasattr(request, "param"): | ||||
|         args = fix_ansible_args(request.param) | ||||
|         mocker.patch('ansible.module_utils.basic._ANSIBLE_ARGS', to_bytes(args)) | ||||
|     else: | ||||
|         def _patch(args): | ||||
|             args = fix_ansible_args(args) | ||||
|             mocker.patch('ansible.module_utils.basic._ANSIBLE_ARGS', to_bytes(args)) | ||||
|         return _patch | ||||
| def patch_ansible_module(request): | ||||
|     args = _fix_ansible_args(request.param) | ||||
|     with _set_module_args(args): | ||||
|         yield | ||||
| 
 | ||||
| 
 | ||||
| @pytest.fixture | ||||
| def patch_ansible_module_uthelper(request): | ||||
|     @_contextlib.contextmanager | ||||
|     def _patch(args): | ||||
|         args = _fix_ansible_args(args) | ||||
|         with _set_module_args(args): | ||||
|             yield | ||||
|     return _patch | ||||
| 
 | ||||
| 
 | ||||
| @pytest.fixture(autouse=True) | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue