How to use the ansible.compat.tests.mock.Mock function in ansible

To help you get started, we’ve selected a few ansible examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github F5Networks / f5-ansible / test / units / modules / network / f5 / test_bigip_sys_global.py View on Github external
))

        # Configure the parameters that would be returned by querying the
        # remote device
        current = ApiParameters(params=load_fixture('load_sys_global_settings.json'))

        module = AnsibleModule(
            argument_spec=self.spec.argument_spec,
            supports_check_mode=self.spec.supports_check_mode
        )
        mm = ModuleManager(module=module)

        # Override methods to force specific logic in the module to happen
        mm.exists = Mock(return_value=False)
        mm.read_current_from_device = Mock(return_value=current)
        mm.update_on_device = Mock(return_value=True)

        results = mm.exec_module()
        assert results['changed'] is True
github citrix / citrix-adc-ansible-modules / test / units / test_netscaler_server.py View on Github external
nitro_pass='pass',
            nsip='1.1.1.1',
            state='present',
        ))
        from ansible.modules.network.netscaler import netscaler_server

        client_mock = Mock()

        m = Mock(return_value=client_mock)

        server_proxy_mock = Mock()

        with patch.multiple(
            'ansible.modules.network.netscaler.netscaler_server',
            get_nitro_client=m,
            server_exists=Mock(side_effect=[False, True]),
            ConfigProxy=Mock(return_value=server_proxy_mock),
            diff_list=Mock(return_value={}),
            do_state_change=Mock(return_value=Mock(errorcode=0))
        ):
            self.module = netscaler_server
            self.exited()
            self.assertIn(call.save_config(), client_mock.mock_calls)
github citrix / citrix-adc-ansible-modules / test / units / test_netscaler_server.py View on Github external
def test_disable_server_graceful(self):
        set_module_args(dict(
            nitro_user='user',
            nitro_pass='pass',
            nsip='1.1.1.1',
            state='present',
            disabled=True,
            graceful=True
        ))
        from ansible.modules.network.netscaler import netscaler_server

        client_mock = Mock()

        m = Mock(return_value=client_mock)

        server_proxy_mock = Mock()

        d = {
            'graceful': True,
            'delay': 20,
        }
        with patch.multiple(
            'ansible.modules.network.netscaler.netscaler_server',
            nitro_exception=self.MockException,
            get_nitro_client=m,
            diff_list=Mock(return_value=d),
            get_immutables_intersection=Mock(return_value=[]),
            server_exists=Mock(side_effect=[True, True]),
github ansible / ansible / test / units / modules / network / f5 / test_bigip_selfip.py View on Github external
traffic_group='traffic-group-local-only',
            vlan='net1',
            password='password',
            server='localhost',
            user='admin'
        ))

        module = AnsibleModule(
            argument_spec=self.spec.argument_spec,
            supports_check_mode=self.spec.supports_check_mode
        )
        mm = ModuleManager(module=module)

        # Override methods to force specific logic in the module to happen
        mm.exists = Mock(side_effect=[False, True])
        mm.create_on_device = Mock(return_value=True)

        results = mm.exec_module()

        assert results['changed'] is True
github F5Networks / f5-ansible / test / unit / test_bigip_software.py View on Github external
with patch.multiple(Parameters, **to_patch) as patched:
            patched['_check_active_volume'].return_value = True
            patched['_volume_exists_on_device'].return_value = True

            mm = RemoteManager(module=module)
            mm.exit_json = Mock(return_value=True)
            mm.list_volumes_on_device = Mock(
                return_value=self.loaded_volumes_2
            )
            mm.list_hotfixes_on_device = Mock(
                return_value=self.loaded_hotfixes
            )
            mm.list_images_on_device = Mock(return_value=self.loaded_images)
            mm.wait_for_software_install_on_device = Mock(return_value=True)
            mm.run_command_on_device = Mock(side_effect=cmd_side_effect_md5_ok)
            mm.install_image_on_device = Mock(return_value=True)
            mm.wait_for_images = Mock(return_value=True)

            results = mm.exec_module()
            assert results['changed'] is True
            assert results['force'] is False
            assert results['reuse_inactive_volume'] is False
            assert results['remote_src'] is True
            assert results['software'] == \
                'http://fake.com/BIGIP-12.1.2.0.0.249.iso'
            assert results['software_md5sum'] == \
                'http://fake.com/BIGIP-12.1.2.0.0.249.iso.md5'
            assert results['state'] == 'installed'
            assert results['version'] == '12.1.2'
            assert results['build'] == '0.0.249'
            assert results['volume'] == 'HD1.4'
github citrix / citrix-adc-ansible-modules / test / units / test_netscaler_cs_action.py View on Github external
def test_graceful_nitro_exception_state_present(self):
        set_module_args(dict(
            nitro_user='user',
            nitro_pass='pass',
            nsip='1.1.1.1',
            state='present',
        ))
        from ansible.modules.network.netscaler import netscaler_cs_action

        class MockException(Exception):
            def __init__(self, *args, **kwargs):
                self.errorcode = 0
                self.message = ''

        m = Mock(side_effect=MockException)
        with patch.multiple(
            'ansible.modules.network.netscaler.netscaler_cs_action',
            action_exists=m,
            ensure_feature_is_enabled=Mock(return_value=True),
            nitro_exception=MockException
        ):
            self.module = netscaler_cs_action
            result = self.failed()
            self.assertTrue(
                result['msg'].startswith('nitro exception'),
                msg='Nitro exception not caught on operation absent'
            )
github ansible / ansible / test / units / modules / network / f5 / test_bigip_pool_member.py View on Github external
reuse_nodes=True,
            fqdn_auto_populate=False,
            password='password',
            server='localhost',
            user='admin'
        ))

        current_node = NodeApiParameters(params=load_fixture('load_net_node_with_fqdn.json'))
        module = AnsibleModule(
            argument_spec=self.spec.argument_spec,
            supports_check_mode=self.spec.supports_check_mode
        )
        mm = ModuleManager(module=module)

        # Override methods to force specific logic in the module to happen
        mm.exists = Mock(return_value=False)
        mm.create_on_device = Mock(return_value=True)
        mm.read_current_node_from_device = Mock(return_value=current_node)

        results = mm.exec_module()

        assert results['changed'] is True
        assert results['fqdn_auto_populate'] is True
        assert results['fqdn'] == 'foo.bar.com'
        assert results['state'] == 'present'
github F5Networks / f5-ansible / test / unit / test_bigip_software.py View on Github external
patched['_check_active_volume'].return_value = True
            patched['_volume_exists_on_device'].return_value = True

            mm = LocalManager(module=module)
            mm.exit_json = Mock(return_value=False)
            mm.list_volumes_on_device = Mock(
                return_value=self.loaded_volumes_2
            )
            mm.image_exists_on_device = Mock(return_value=False)
            mm.install_image_on_device = Mock(return_value=True)
            mm.list_images_on_device = Mock(return_value=self.loaded_images)
            mm.list_hotfixes_on_device = Mock(
                return_value=self.loaded_hotfixes
            )
            mm.wait_for_software_install_on_device = Mock(return_value=True)
            mm.upload_to_device = Mock(return_value=True)
            mm.wait_for_images = Mock(return_value=True)

            results = mm.exec_module()
            assert results['changed'] is True
            assert results['force'] is False
            assert results['remote_src'] is False
            assert results['reuse_inactive_volume'] is False
            assert results['state'] == 'installed'
            assert results['volume'] == 'HD1.2'
            assert results['software'] == self.iso
github citrix / citrix-adc-ansible-modules / test / units / test_netscaler_server.py View on Github external
m = Mock(return_value=client_mock)

        server_proxy_attrs = {
            'diff_object.return_value': {},
        }
        server_proxy_mock = Mock()
        server_proxy_mock.configure_mock(**server_proxy_attrs)
        config_proxy_mock = Mock(return_value=server_proxy_mock)

        with patch.multiple(
            'ansible.modules.network.netscaler.netscaler_server',
            nitro_exception=self.MockException,
            get_nitro_client=m,
            diff_list=Mock(return_value={}),
            get_immutables_intersection=Mock(return_value=[]),
            server_exists=Mock(side_effect=[False, False]),
            server_identical=Mock(side_effect=[False, False]),
            ConfigProxy=config_proxy_mock,
            do_state_change=Mock(return_value=Mock(errorcode=0))
        ):
            self.module = netscaler_server
            result = self.failed()
            self.assertEqual(result['msg'], 'Server does not seem to exist')
            self.assertTrue(result['failed'])
github F5Networks / f5-ansible / test / unit / test_bigip_software.py View on Github external
password='password',
            user='admin',
        ))

        module = AnsibleModule(
            argument_spec=self.spec.argument_spec,
            supports_check_mode=self.spec.supports_check_mode
        )

        # Override methods to force specific logic in the module to happen
        mm = RemoteManager(module=module)
        mm.exit_json = Mock(return_value=False)
        mm.list_hotfixes_on_device = Mock(return_value=self.loaded_hotfixes)
        mm.list_images_on_device = Mock(return_value=self.loaded_images)
        mm.run_command_on_device = Mock(side_effect=cmd_side_effect_md5_fail)
        mm.exists = Mock(return_value=False)
        msg = 'ISO download failed from remote host.'

        with pytest.raises(F5ModuleError) as err:
            mm.exec_module()

        assert err.value.message == msg