Пример #1
0
    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)
Пример #2
0
    def test_main_success(self, ansible_mod_cls, perform_task_func,
                          check_mode):
        """
        Test main() with all required module parameters.
        """

        # 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,
        }

        # Return values of perform_task()
        perform_task_changed = True
        perform_task_result = {}

        # 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.return_value = (perform_task_changed,
                                          perform_task_result)

        # 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 == 0)

        # Assert call to AnsibleModule()
        expected_argument_spec = dict(
            hmc_host=dict(required=True, type='str'),
            hmc_auth=dict(
                required=True,
                type='dict',
                options=dict(
                    userid=dict(required=True, type='str'),
                    password=dict(required=True, type='str', no_log=True),
                    ca_certs=dict(required=False, type='str', default=None),
                    verify=dict(required=False, type='bool', default=True),
                ),
            ),
            cpc_name=dict(required=True, type='str'),
            partition_name=dict(required=True, type='str'),
            name=dict(required=True, type='str'),
            state=dict(required=True,
                       type='str',
                       choices=['absent', 'present']),
            properties=dict(required=False, type='dict', default={}),
            log_file=dict(required=False, type='str', default=None),
            _faked_session=dict(required=False, type='raw'),
        )
        assert (ansible_mod_cls.call_args == mock.call(
            argument_spec=expected_argument_spec, supports_check_mode=True))

        # Assert call to perform_task()
        assert (perform_task_func.call_args == mock.call(params, check_mode))

        # Assert call to exit_json()
        assert (mod_obj.exit_json.call_args == mock.call(
            changed=perform_task_changed,
            virtual_function=perform_task_result))

        # Assert no call to fail_json()
        assert (mod_obj.fail_json.called is False)