def test_main_param_error(self, ansible_mod_cls, perform_task_func): """ Test main() with ParameterError being raised in perform_task(). """ # Module invocation params = { 'hmc_host': 'fake-host', 'hmc_auth': dict(userid='fake-userid', password='******'), 'cpc_name': 'fake-cpc-name', 'name': 'fake-name', 'state': 'absent', 'expand_storage_groups': False, 'expand_crypto_adapters': False, 'log_file': None, } check_mode = False # Exception raised by perform_task() perform_task_exc = module_utils.ParameterError("fake message") # Prepare mocks mod_obj = ansible_mod_cls.return_value mod_obj.params = params mod_obj.check_mode = check_mode mod_obj.fail_json.configure_mock(side_effect=SystemExit(1)) mod_obj.exit_json.configure_mock(side_effect=SystemExit(0)) perform_task_func.mock.configure_mock(side_effect=perform_task_exc) # Exercise code with self.assertRaises(SystemExit) as cm: zhmc_partition.main() exit_code = cm.exception.args[0] # Assert module exit code assert(exit_code == 1) # Assert call to perform_task() assert(perform_task_func.call_args == mock.call(params, check_mode)) # Assert call to fail_json() assert(mod_obj.fail_json.call_args == mock.call(msg="ParameterError: fake message")) # Assert no call to exit_json() assert(mod_obj.exit_json.called is False)
def test_main_param_error(self, ansible_mod_cls, perform_task_func, check_mode): """ Test main() with ParameterError being raised in perform_task(). """ # Module invocation params = { 'hmc_host': 'fake-host', 'hmc_auth': dict(userid='fake-userid', password='******'), 'cpc_name': 'fake-cpc-name', 'partition_name': 'fake-partition-name', 'name': 'fake-vfunction-name', 'state': 'absent', 'log_file': None, } # Exception raised by perform_task() perform_task_exc = module_utils.ParameterError("fake message") # Prepare mocks mod_obj = ansible_mod_cls.return_value mod_obj.params = params mod_obj.check_mode = check_mode mod_obj.fail_json.configure_mock(side_effect=SystemExit(1)) mod_obj.exit_json.configure_mock(side_effect=SystemExit(0)) perform_task_func.mock.configure_mock(side_effect=perform_task_exc) # Exercise the code to be tested with pytest.raises(SystemExit) as exc_info: zhmc_virtual_function.main() exit_code = exc_info.value.args[0] # Assert module exit code assert (exit_code == 1) # Assert call to perform_task() assert (perform_task_func.call_args == mock.call(params, check_mode)) # Assert call to fail_json() assert (mod_obj.fail_json.call_args == mock.call( msg="ParameterError: fake message")) # Assert no call to exit_json() assert (mod_obj.exit_json.called is False)