mirror of
				https://github.com/ansible-collections/community.general.git
				synced 2025-10-24 13:04:00 -07:00 
			
		
		
		
	[PR #7196/40cad3e7 backport][stable-7] gconftool2: using yaml-specified unit tests (#7198)
gconftool2: using yaml-specified unit tests (#7196)
* gconftool2: using yaml-specified unit tests
* gconftool2_info: using yaml-specified unit tests
* adjust code for skip and xfail
(cherry picked from commit 40cad3e7a9)
Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com>
	
	
This commit is contained in:
		
					parent
					
						
							
								16499072ff
							
						
					
				
			
			
				commit
				
					
						3785b656d6
					
				
			
		
					 6 changed files with 215 additions and 274 deletions
				
			
		|  | @ -6,96 +6,26 @@ | |||
| from __future__ import (absolute_import, division, print_function) | ||||
| __metaclass__ = type | ||||
| 
 | ||||
| import json | ||||
| 
 | ||||
| from ansible_collections.community.general.plugins.modules import gconftool2_info | ||||
| 
 | ||||
| import pytest | ||||
| 
 | ||||
| 
 | ||||
| @pytest.fixture | ||||
| def patch_gconftool2_info(mocker): | ||||
|     """ | ||||
|     Function used for mocking some parts of redhat_subscription module | ||||
|     """ | ||||
|     mocker.patch('ansible_collections.community.general.plugins.module_utils.mh.module_helper.AnsibleModule.get_bin_path', | ||||
|                  return_value='/testbin/gconftool-2') | ||||
| from ansible_collections.community.general.plugins.modules import gconftool2_info | ||||
| from .cmd_runner_test_utils import CmdRunnerTestHelper | ||||
| 
 | ||||
| 
 | ||||
| TEST_CASES = [ | ||||
|     [ | ||||
|         {'key': '/desktop/gnome/background/picture_filename'}, | ||||
|         { | ||||
|             'id': 'test_simple_element_get', | ||||
|             'run_command.calls': [ | ||||
|                 ( | ||||
|                     # Calling of following command will be asserted | ||||
|                     ['/testbin/gconftool-2', '--get', '/desktop/gnome/background/picture_filename'], | ||||
|                     # Was return code checked? | ||||
|                     {'environ_update': {'LANGUAGE': 'C', 'LC_ALL': 'C'}, 'check_rc': True}, | ||||
|                     # Mock of returned code, stdout and stderr | ||||
|                     (0, '100\n', '',), | ||||
|                 ), | ||||
|             ], | ||||
|             'value': '100', | ||||
|         } | ||||
|     ], | ||||
|     [ | ||||
|         {'key': '/desktop/gnome/background/picture_filename'}, | ||||
|         { | ||||
|             'id': 'test_simple_element_get_not_found', | ||||
|             'run_command.calls': [ | ||||
|                 ( | ||||
|                     # Calling of following command will be asserted | ||||
|                     ['/testbin/gconftool-2', '--get', '/desktop/gnome/background/picture_filename'], | ||||
|                     # Was return code checked? | ||||
|                     {'environ_update': {'LANGUAGE': 'C', 'LC_ALL': 'C'}, 'check_rc': True}, | ||||
|                     # Mock of returned code, stdout and stderr | ||||
|                     (0, '', "No value set for `/desktop/gnome/background/picture_filename'\n",), | ||||
|                 ), | ||||
|             ], | ||||
|             'value': None, | ||||
|         } | ||||
|     ], | ||||
| ] | ||||
| TEST_CASES_IDS = [item[1]['id'] for item in TEST_CASES] | ||||
| with open("tests/unit/plugins/modules/test_gconftool2_info.yaml", "r") as TEST_CASES: | ||||
|     helper = CmdRunnerTestHelper(gconftool2_info.main, test_cases=TEST_CASES) | ||||
|     patch_bin = helper.cmd_fixture | ||||
| 
 | ||||
| 
 | ||||
| @pytest.mark.parametrize('patch_ansible_module, testcase', | ||||
|                          TEST_CASES, | ||||
|                          ids=TEST_CASES_IDS, | ||||
|                          helper.testcases_params, ids=helper.testcases_ids, | ||||
|                          indirect=['patch_ansible_module']) | ||||
| @pytest.mark.usefixtures('patch_ansible_module') | ||||
| def test_gconftool2_info(mocker, capfd, patch_gconftool2_info, testcase): | ||||
| def test_module(mocker, capfd, patch_bin, testcase): | ||||
|     """ | ||||
|     Run unit tests for test cases listen in TEST_CASES | ||||
|     Run unit tests for test cases listed in TEST_CASES | ||||
|     """ | ||||
| 
 | ||||
|     # Mock function used for running commands first | ||||
|     call_results = [item[2] for item in testcase['run_command.calls']] | ||||
|     mock_run_command = mocker.patch( | ||||
|         'ansible_collections.community.general.plugins.module_utils.mh.module_helper.AnsibleModule.run_command', | ||||
|         side_effect=call_results) | ||||
| 
 | ||||
|     # Try to run test case | ||||
|     with pytest.raises(SystemExit): | ||||
|         gconftool2_info.main() | ||||
| 
 | ||||
|     out, err = capfd.readouterr() | ||||
|     results = json.loads(out) | ||||
|     print("testcase =\n%s" % testcase) | ||||
|     print("results =\n%s" % results) | ||||
| 
 | ||||
|     for conditional_test_result in ('value',): | ||||
|         if conditional_test_result in testcase: | ||||
|             assert conditional_test_result in results, "'{0}' not found in {1}".format(conditional_test_result, results) | ||||
|             assert results[conditional_test_result] == testcase[conditional_test_result], \ | ||||
|                 "'{0}': '{1}' != '{2}'".format(conditional_test_result, results[conditional_test_result], testcase[conditional_test_result]) | ||||
| 
 | ||||
|     assert mock_run_command.call_count == len(testcase['run_command.calls']) | ||||
|     if mock_run_command.call_count: | ||||
|         call_args_list = [(item[0][0], item[1]) for item in mock_run_command.call_args_list] | ||||
|         expected_call_args_list = [(item[0], item[1]) for item in testcase['run_command.calls']] | ||||
|         print("call args list =\n%s" % call_args_list) | ||||
|         print("expected args list =\n%s" % expected_call_args_list) | ||||
|         assert call_args_list == expected_call_args_list | ||||
|     with helper(testcase, mocker, capfd) as testcase_context: | ||||
|         testcase_context.run() | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue