def test_new_cs_vserver_execution_flow(self):
        set_module_args(
            dict(
                nitro_user='******',
                nitro_pass='******',
                nsip='192.0.2.1',
                state='present',
            ))
        from ansible_collections.ansible.misc.plugins.modules import netscaler_cs_vserver

        client_mock = Mock()

        m = Mock(return_value=client_mock)

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

        with patch.multiple(
                'ansible_collections.ansible.misc.plugins.modules.netscaler_cs_vserver',
                get_nitro_client=m,
                cs_vserver_exists=Mock(side_effect=[False, True]),
                cs_vserver_identical=Mock(side_effect=[True]),
                ensure_feature_is_enabled=Mock(return_value=True),
                ConfigProxy=config_proxy_mock,
                nitro_exception=self.MockException,
                do_state_change=Mock(return_value=Mock(errorcode=0)),
        ):
            self.module = netscaler_cs_vserver
            self.exited()
            cs_vserver_proxy_mock.assert_has_calls([call.add()])
    def test_save_config_not_called_on_state_absent(self):
        set_module_args(dict(
            nitro_user='******',
            nitro_pass='******',
            nsip='192.0.2.1',
            state='absent',
            save_config=False,
        ))
        from ansible_collections.ansible.misc.plugins.modules import netscaler_cs_action

        client_mock = Mock()

        m = Mock(return_value=client_mock)

        cs_action_proxy_mock = Mock()

        with patch.multiple(
            'ansible_collections.ansible.misc.plugins.modules.netscaler_cs_action',
            get_nitro_client=m,
            action_exists=Mock(side_effect=[True, False]),
            ensure_feature_is_enabled=Mock(return_value=True),
            ConfigProxy=Mock(return_value=cs_action_proxy_mock),
        ):
            self.module = netscaler_cs_action
            self.exited()
            self.assertNotIn(call.save_config(), client_mock.mock_calls)
    def test_absent_cs_action_failed_delete(self):
        set_module_args(dict(
            nitro_user='******',
            nitro_pass='******',
            nsip='192.0.2.1',
            state='absent',
        ))
        from ansible_collections.ansible.misc.plugins.modules import netscaler_cs_action

        client_mock = Mock()

        m = Mock(return_value=client_mock)

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

        with patch.multiple(
            'ansible_collections.ansible.misc.plugins.modules.netscaler_cs_action',
            nitro_exception=self.MockException,
            get_nitro_client=m,
            diff_list=Mock(return_value={}),
            get_immutables_intersection=Mock(return_value=[]),
            action_exists=Mock(side_effect=[True, True]),
            action_identical=Mock(side_effect=[False, False]),
            ensure_feature_is_enabled=Mock(return_value=True),
            ConfigProxy=config_proxy_mock,
        ):
            self.module = netscaler_cs_action
            result = self.failed()
            self.assertEqual(result['msg'], 'Content switching action still exists')
            self.assertTrue(result['failed'])
    def test_save_config_not_called_on_state_present(self):
        set_module_args(dict(
            nitro_user='******',
            nitro_pass='******',
            nsip='192.0.2.1',
            state='present',
            save_config=False,
        ))
        from ansible_collections.ansible.misc.plugins.modules import netscaler_ssl_certkey

        client_mock = Mock()

        m = Mock(return_value=client_mock)

        ssl_certkey_proxy_mock = Mock()

        with patch.multiple(
            'ansible_collections.ansible.misc.plugins.modules.netscaler_ssl_certkey',
            get_nitro_client=m,
            key_exists=Mock(side_effect=[False, True]),
            key_identical=Mock(side_effect=[True]),
            ConfigProxy=Mock(return_value=ssl_certkey_proxy_mock),
            nitro_exception=self.MockException,
        ):
            self.module = netscaler_ssl_certkey
            self.exited()
            self.assertNotIn(call.save_config(), client_mock.mock_calls)
    def test_present_server_update_immutable_attribute(self):
        set_module_args(dict(
            nitro_user='******',
            nitro_pass='******',
            nsip='192.0.2.1',
            state='present',
        ))
        from ansible_collections.ansible.misc.plugins.modules import netscaler_ssl_certkey

        client_mock = Mock()

        m = Mock(return_value=client_mock)

        ssl_certkey_proxy_attrs = {
            'diff_object.return_value': {},
        }
        ssl_certkey_proxy_mock = Mock()
        ssl_certkey_proxy_mock.configure_mock(**ssl_certkey_proxy_attrs)
        config_proxy_mock = Mock(return_value=ssl_certkey_proxy_mock)

        with patch.multiple(
            'ansible_collections.ansible.misc.plugins.modules.netscaler_ssl_certkey',
            nitro_exception=self.MockException,
            get_nitro_client=m,
            diff_list=Mock(return_value={}),
            get_immutables_intersection=Mock(return_value=['domain']),
            key_exists=Mock(side_effect=[True, True]),
            key_identical=Mock(side_effect=[False, False]),
            ConfigProxy=config_proxy_mock,
        ):
            self.module = netscaler_ssl_certkey
            result = self.failed()
            self.assertEqual(result['msg'], 'Cannot update immutable attributes [\'domain\']')
            self.assertTrue(result['failed'])
    def test_absent_server_failed_delete(self):
        set_module_args(
            dict(
                nitro_user='******',
                nitro_pass='******',
                nsip='192.0.2.1',
                state='absent',
            ))
        from ansible_collections.ansible.misc.plugins.modules import netscaler_server

        client_mock = Mock()

        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_collections.ansible.misc.plugins.modules.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=[True, True]),
                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 seems to be present')
            self.assertTrue(result['failed'])
    def test_save_config_called_on_state_absent(self):
        set_module_args(
            dict(
                nitro_user='******',
                nitro_pass='******',
                nsip='192.0.2.1',
                state='absent',
            ))
        from ansible_collections.ansible.misc.plugins.modules import netscaler_gslb_service

        client_mock = Mock()

        m = Mock(return_value=client_mock)

        gslb_service_proxy_mock = Mock()

        with patch.multiple(
                'ansible_collections.ansible.misc.plugins.modules.netscaler_gslb_service',
                get_nitro_client=m,
                gslb_service_exists=Mock(side_effect=[True, False]),
                nitro_exception=self.MockException,
                ensure_feature_is_enabled=Mock(),
                monkey_patch_nitro_api=Mock(),
                ConfigProxy=Mock(return_value=gslb_service_proxy_mock),
        ):
            self.module = netscaler_gslb_service
            self.exited()
            self.assertIn(call.save_config(), client_mock.mock_calls)
    def test_save_config_called_on_state_present(self):
        set_module_args(
            dict(
                nitro_user='******',
                nitro_pass='******',
                nsip='192.0.2.1',
                state='present',
            ))
        from ansible_collections.ansible.misc.plugins.modules import netscaler_lb_vserver

        client_mock = Mock()

        m = Mock(return_value=client_mock)

        lb_vserver_proxy_mock = Mock()

        with patch.multiple(
                'ansible_collections.ansible.misc.plugins.modules.netscaler_lb_vserver',
                get_nitro_client=m,
                lb_vserver_exists=Mock(side_effect=[False, True]),
                ConfigProxy=Mock(return_value=lb_vserver_proxy_mock),
                ensure_feature_is_enabled=Mock(return_value=True),
                do_state_change=Mock(return_value=Mock(errorcode=0)),
        ):
            self.module = netscaler_lb_vserver
            self.exited()
            self.assertIn(call.save_config(), client_mock.mock_calls)
    def test_create_new_lb_vserver_workflow(self):
        set_module_args(
            dict(
                nitro_user='******',
                nitro_pass='******',
                nsip='192.0.2.1',
                state='present',
                save_config=False,
            ))
        from ansible_collections.ansible.misc.plugins.modules import netscaler_lb_vserver

        lb_vserver_proxy_mock = Mock()

        with patch.multiple(
                'ansible_collections.ansible.misc.plugins.modules.netscaler_lb_vserver',
                get_nitro_client=Mock(return_value=Mock()),
                lb_vserver_exists=Mock(side_effect=[False, True]),
                lb_vserver_identical=Mock(side_effect=[True]),
                servicegroup_bindings_identical=Mock(side_effect=[True, True]),
                service_bindings_identical=Mock(side_effect=[True, True]),
                do_state_change=Mock(return_value=Mock(errorcode=0)),
                ConfigProxy=Mock(return_value=lb_vserver_proxy_mock),
                ensure_feature_is_enabled=Mock(return_value=True),
        ):
            self.module = netscaler_lb_vserver
            result = self.exited()
            lb_vserver_proxy_mock.assert_has_calls([call.add()])
            self.assertTrue(result['changed'])
    def test_servicegroup_monitor_bindings_sanity(self):
        self.set_module_state('present')
        from ansible_collections.ansible.misc.plugins.modules import netscaler_servicegroup
        servicegroup_proxy_mock = MagicMock()
        attrs = {
            'diff_object.return_value': {},
        }
        servicegroup_proxy_mock.configure_mock(**attrs)

        m = MagicMock(return_value=servicegroup_proxy_mock)
        sync_mock = Mock()

        with patch.multiple(
                'ansible_collections.ansible.misc.plugins.modules.netscaler_servicegroup',
                ConfigProxy=m,
                servicegroup_exists=Mock(side_effect=[True, True]),
                servicegroup_identical=Mock(side_effect=[True, True]),
                monitor_bindings_identical=Mock(side_effect=[False, False]),
                sync_monitor_bindings=Mock(),
                servicemembers_identical=Mock(side_effect=[True, True]),
                nitro_exception=self.MockException,
                sync_service_members=sync_mock,
                do_state_change=Mock(return_value=Mock(errorcode=0)),
        ):
            self.module = netscaler_servicegroup
            result = self.failed()
            self.assertEqual(result['msg'],
                             'Monitor bindings are not identical')
    def test_no_change_to_module_when_all_identical(self):
        self.set_module_state('present')
        from ansible_collections.ansible.misc.plugins.modules import netscaler_servicegroup
        servicegroup_proxy_mock = MagicMock()
        attrs = {
            'diff_object.return_value': {},
        }
        servicegroup_proxy_mock.configure_mock(**attrs)

        m = MagicMock(return_value=servicegroup_proxy_mock)
        servicegroup_exists_mock = Mock(side_effect=[True, True])
        servicegroup_identical_mock = Mock(side_effect=[True, True])
        monitor_bindings_identical_mock = Mock(side_effect=[True, True])

        with patch.multiple(
                'ansible_collections.ansible.misc.plugins.modules.netscaler_servicegroup',
                ConfigProxy=m,
                servicegroup_exists=servicegroup_exists_mock,
                servicegroup_identical=servicegroup_identical_mock,
                servicemembers_identical=Mock(side_effect=[True, True]),
                monitor_bindings_identical=monitor_bindings_identical_mock,
                do_state_change=Mock(return_value=Mock(errorcode=0)),
                nitro_exception=self.MockException,
        ):
            self.module = netscaler_servicegroup
            result = self.exited()
            self.assertFalse(result['changed'],
                             msg='Erroneous changed status update')
    def test_update_servicegroup_when_service_members_differ(self):
        self.set_module_state('present')
        from ansible_collections.ansible.misc.plugins.modules import netscaler_servicegroup
        servicegroup_proxy_mock = MagicMock()
        attrs = {
            'diff_object.return_value': {},
        }
        servicegroup_proxy_mock.configure_mock(**attrs)

        m = MagicMock(return_value=servicegroup_proxy_mock)
        sync_mock = Mock()

        with patch.multiple(
                'ansible_collections.ansible.misc.plugins.modules.netscaler_servicegroup',
                ConfigProxy=m,
                servicegroup_exists=Mock(side_effect=[True, True]),
                servicegroup_identical=Mock(side_effect=[True, True]),
                monitor_bindings_identical=Mock(side_effect=[True, True]),
                sync_monitor_bindings=Mock(),
                servicemembers_identical=Mock(side_effect=[False, True]),
                sync_service_members=sync_mock,
                do_state_change=Mock(return_value=Mock(errorcode=0)),
        ):
            self.module = netscaler_servicegroup
            result = self.exited()
        # poor man's assert_called_once since python3.5 does not implement that mock method
        self.assertEqual(len(sync_mock.mock_calls),
                         1,
                         msg='sync monitor bindings not called once')
        self.assertTrue(result['changed'], msg='Change not recorded')
    def test_create_non_existing_servicegroup(self):
        self.set_module_state('present')
        from ansible_collections.ansible.misc.plugins.modules import netscaler_servicegroup
        servicegroup_proxy_mock = MagicMock()
        attrs = {
            'diff_object.return_value': {},
        }
        servicegroup_proxy_mock.configure_mock(**attrs)

        m = MagicMock(return_value=servicegroup_proxy_mock)
        servicegroup_exists_mock = Mock(side_effect=[False, True])

        servicegroup_servicegroupmember_binding_mock = Mock(count=Mock(
            return_value=0))

        with patch.multiple(
                'ansible_collections.ansible.misc.plugins.modules.netscaler_servicegroup',
                ConfigProxy=m,
                servicegroup_exists=servicegroup_exists_mock,
                servicemembers_identical=Mock(side_effect=[False, True]),
                do_state_change=Mock(return_value=Mock(errorcode=0)),
                servicegroup_servicegroupmember_binding=
                servicegroup_servicegroupmember_binding_mock,
                nitro_exception=self.MockException,
        ):
            self.module = netscaler_servicegroup
            result = self.exited()
            servicegroup_proxy_mock.assert_has_calls([call.add()])
            self.assertTrue(result['changed'], msg='Change not recorded')
    def test_no_change_to_module_when_all_identical(self):
        self.set_module_state('present')
        from ansible_collections.ansible.misc.plugins.modules import netscaler_cs_policy
        cs_policy_mock = MagicMock()
        attrs = {
            'diff_object.return_value': {},
        }
        cs_policy_mock.configure_mock(**attrs)

        m = MagicMock(return_value=cs_policy_mock)
        policy_exists_mock = Mock(side_effect=[True, True])
        policy_identical_mock = Mock(side_effect=[True, True])

        with patch.multiple(
                'ansible_collections.ansible.misc.plugins.modules.netscaler_cs_policy',
                ConfigProxy=m,
                policy_exists=policy_exists_mock,
                policy_identical=policy_identical_mock,
                ensure_feature_is_enabled=Mock(),
                nitro_exception=self.MockException,
        ):
            self.module = netscaler_cs_policy
            result = self.exited()
            self.assertFalse(result['changed'],
                             msg='Erroneous changed status update')
    def test_disable_server_graceful(self):
        set_module_args(
            dict(nitro_user='******',
                 nitro_pass='******',
                 nsip='192.0.2.1',
                 state='present',
                 disabled=True,
                 graceful=True))
        from ansible_collections.ansible.misc.plugins.modules 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_collections.ansible.misc.plugins.modules.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]),
                ConfigProxy=Mock(return_value=server_proxy_mock),
                do_state_change=Mock(return_value=Mock(errorcode=0))):
            self.module = netscaler_server
            result = self.exited()
            self.assertEqual(
                d, {},
                'Graceful disable options were not discarded from the diff_list with the actual object'
            )
    def test_ssl_bindings_not_called_for_non_ssl_service(self):
        set_module_args(
            dict(
                nitro_user='******',
                nitro_pass='******',
                nsip='192.0.2.1',
                state='present',
                save_config=False,
                servicetype='HTTP',
            ))
        from ansible_collections.ansible.misc.plugins.modules import netscaler_lb_vserver

        lb_vserver_proxy_mock = Mock()
        ssl_sync_mock = Mock()

        client_mock = Mock()

        with patch.multiple(
                'ansible_collections.ansible.misc.plugins.modules.netscaler_lb_vserver',
                get_nitro_client=Mock(return_value=client_mock),
                lb_vserver_exists=Mock(side_effect=[True, True]),
                lb_vserver_identical=Mock(side_effect=[False, True]),
                servicegroup_bindings_identical=Mock(side_effect=[True, True]),
                service_bindings_identical=Mock(side_effect=[True, True]),
                ssl_certkey_bindings_identical=Mock(side_effect=[False, True]),
                ssl_certkey_bindings_sync=ssl_sync_mock,
                ConfigProxy=Mock(return_value=lb_vserver_proxy_mock),
                ensure_feature_is_enabled=Mock(return_value=True),
                do_state_change=Mock(return_value=Mock(errorcode=0)),
                get_immutables_intersection=(Mock(return_value=[])),
        ):
            self.module = netscaler_lb_vserver
            result = self.exited()
            ssl_sync_mock.assert_not_called()
            self.assertTrue(result['changed'])
    def test_absent_server_execution_flow(self):
        set_module_args(
            dict(
                nitro_user='******',
                nitro_pass='******',
                nsip='192.0.2.1',
                state='absent',
            ))
        from ansible_collections.ansible.misc.plugins.modules import netscaler_server

        client_mock = Mock()

        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_collections.ansible.misc.plugins.modules.netscaler_server',
                get_nitro_client=m,
                diff_list=Mock(return_value={}),
                get_immutables_intersection=Mock(return_value=[]),
                server_exists=Mock(side_effect=[True, False]),
                server_identical=Mock(side_effect=[False, True]),
                ConfigProxy=config_proxy_mock,
                do_state_change=Mock(return_value=Mock(errorcode=0))):
            self.module = netscaler_server
            self.exited()
            server_proxy_mock.assert_has_calls([call.delete()])
    def test_server_servicegroup_bindings_sanity_check(self):
        set_module_args(
            dict(
                nitro_user='******',
                nitro_pass='******',
                nsip='192.0.2.1',
                state='present',
            ))
        from ansible_collections.ansible.misc.plugins.modules import netscaler_lb_vserver

        lb_vserver_proxy_mock = Mock()

        client_mock = Mock()

        with patch.multiple(
                'ansible_collections.ansible.misc.plugins.modules.netscaler_lb_vserver',
                get_nitro_client=Mock(return_value=client_mock),
                lb_vserver_exists=Mock(side_effect=[True, True]),
                lb_vserver_identical=Mock(side_effect=[False, True]),
                servicegroup_bindings_identical=Mock(
                    side_effect=[False, False]),
                service_bindings_identical=Mock(side_effect=[True, True]),
                ssl_certkey_bindings_identical=Mock(
                    side_effect=[False, False]),
                ConfigProxy=Mock(return_value=lb_vserver_proxy_mock),
                ensure_feature_is_enabled=Mock(return_value=True),
                do_state_change=Mock(return_value=Mock(errorcode=0)),
                get_immutables_intersection=(Mock(return_value=[])),
                nitro_exception=self.MockException,
        ):
            self.module = netscaler_lb_vserver
            result = self.failed()
            self.assertEqual(result['msg'],
                             'servicegroup bindings are not identical')
    def test_ensure_feature_is_enabled_called(self):
        set_module_args(
            dict(
                nitro_user='******',
                nitro_pass='******',
                nsip='192.0.2.1',
                state='present',
            ))
        from ansible_collections.ansible.misc.plugins.modules import netscaler_gslb_service

        gslb_service_proxy_mock = Mock()
        ensure_feature_is_enabled_mock = Mock()
        client_mock = Mock()

        with patch.multiple(
                'ansible_collections.ansible.misc.plugins.modules.netscaler_gslb_service',
                get_nitro_client=Mock(return_value=client_mock),
                gslb_service_exists=Mock(side_effect=[False, True]),
                gslb_service_identical=Mock(side_effect=[True]),
                nitro_exception=self.MockException,
                ensure_feature_is_enabled=ensure_feature_is_enabled_mock,
                monkey_patch_nitro_api=Mock(),
                ConfigProxy=Mock(return_value=gslb_service_proxy_mock),
        ):
            self.module = netscaler_gslb_service
            self.exited()
            ensure_feature_is_enabled_mock.assert_called_with(
                client_mock, 'GSLB')
    def test_absent_state_sanity_check(self):
        set_module_args(
            dict(
                nitro_user='******',
                nitro_pass='******',
                nsip='192.0.2.1',
                state='absent',
            ))
        from ansible_collections.ansible.misc.plugins.modules import netscaler_lb_vserver

        lb_vserver_proxy_mock = Mock()

        client_mock = Mock()
        with patch.multiple(
                'ansible_collections.ansible.misc.plugins.modules.netscaler_lb_vserver',
                get_nitro_client=Mock(return_value=client_mock),
                ConfigProxy=Mock(return_value=lb_vserver_proxy_mock),
                ensure_feature_is_enabled=Mock(return_value=True),
                lb_vserver_exists=Mock(side_effect=[True, True]),
                nitro_exception=self.MockException,
        ):
            self.module = netscaler_lb_vserver
            result = self.failed()
            lb_vserver_proxy_mock.assert_has_calls([call.delete()])
            self.assertEqual(result['msg'], 'lb vserver still exists')
    def test_absent_gslb_site_execution_flow(self):
        set_module_args(
            dict(
                nitro_user='******',
                nitro_pass='******',
                nsip='192.0.2.1',
                state='absent',
            ))
        from ansible_collections.ansible.misc.plugins.modules import netscaler_gslb_service

        client_mock = Mock()

        m = Mock(return_value=client_mock)

        glsb_service_proxy_attrs = {
            'diff_object.return_value': {},
        }
        gslb_service_proxy_mock = Mock()
        gslb_service_proxy_mock.configure_mock(**glsb_service_proxy_attrs)
        config_proxy_mock = Mock(return_value=gslb_service_proxy_mock)

        with patch.multiple(
                'ansible_collections.ansible.misc.plugins.modules.netscaler_gslb_service',
                get_nitro_client=m,
                diff_list=Mock(return_value={}),
                get_immutables_intersection=Mock(return_value=[]),
                gslb_service_exists=Mock(side_effect=[True, False]),
                gslb_service_identical=Mock(side_effect=[False, True]),
                ensure_feature_is_enabled=Mock(),
                monkey_patch_nitro_api=Mock(),
                ConfigProxy=config_proxy_mock,
        ):
            self.module = netscaler_gslb_service
            self.exited()
            gslb_service_proxy_mock.assert_has_calls([call.delete()])
    def test_disabled_state_change_called(self):
        set_module_args(
            dict(
                nitro_user='******',
                nitro_pass='******',
                nsip='192.0.2.1',
                state='present',
            ))

        from ansible_collections.ansible.misc.plugins.modules import netscaler_lb_vserver

        lb_vserver_proxy_mock = Mock()

        do_state_change_mock = Mock(return_value=Mock(errorcode=0))
        client_mock = Mock()
        with patch.multiple(
                'ansible_collections.ansible.misc.plugins.modules.netscaler_lb_vserver',
                get_nitro_client=Mock(return_value=client_mock),
                ConfigProxy=Mock(return_value=lb_vserver_proxy_mock),
                ensure_feature_is_enabled=Mock(return_value=True),
                lb_vserver_exists=Mock(side_effect=[True, True]),
                nitro_exception=self.MockException,
                do_state_change=do_state_change_mock,
        ):
            self.module = netscaler_lb_vserver
            self.exited()
            self.assertTrue(len(do_state_change_mock.mock_calls) > 0,
                            msg='Did not call state change')
    def test_present_key_identical_flow(self):
        set_module_args(dict(
            nitro_user='******',
            nitro_pass='******',
            nsip='192.0.2.1',
            state='present',
        ))
        from ansible_collections.ansible.misc.plugins.modules import netscaler_ssl_certkey

        client_mock = Mock()

        m = Mock(return_value=client_mock)

        ssl_certkey_proxy_attrs = {
            'diff_object.return_value': {},
        }
        ssl_certkey_proxy_mock = Mock()
        ssl_certkey_proxy_mock.configure_mock(**ssl_certkey_proxy_attrs)
        config_proxy_mock = Mock(return_value=ssl_certkey_proxy_mock)

        with patch.multiple(
            'ansible_collections.ansible.misc.plugins.modules.netscaler_ssl_certkey',
            get_nitro_client=m,
            diff_list=Mock(return_value={}),
            get_immutables_intersection=Mock(return_value=[]),
            key_exists=Mock(side_effect=[True, True]),
            key_identical=Mock(side_effect=[True, True]),
            ConfigProxy=config_proxy_mock,
        ):
            self.module = netscaler_ssl_certkey
            self.exited()
            ssl_certkey_proxy_mock.assert_not_called()
    def test_get_immutables_failure(self):
        set_module_args(
            dict(
                nitro_user='******',
                nitro_pass='******',
                nsip='192.0.2.1',
                state='present',
            ))

        from ansible_collections.ansible.misc.plugins.modules import netscaler_lb_vserver

        lb_vserver_proxy_mock = Mock()

        client_mock = Mock()
        m = Mock(return_value=['some'])
        with patch.multiple(
                'ansible_collections.ansible.misc.plugins.modules.netscaler_lb_vserver',
                get_nitro_client=Mock(return_value=client_mock),
                ConfigProxy=Mock(return_value=lb_vserver_proxy_mock),
                ensure_feature_is_enabled=Mock(),
                lb_vserver_exists=Mock(side_effect=[True, True]),
                lb_vserver_identical=Mock(side_effect=[False]),
                do_state_change=Mock(return_value=Mock(errorcode=0)),
                get_immutables_intersection=m,
                nitro_exception=self.MockException,
        ):
            self.module = netscaler_lb_vserver
            result = self.failed()
            self.assertTrue(
                result['msg'].startswith('Cannot update immutable attributes'),
                msg='Did not handle immutables error correctly',
            )
    def test_graceful_login_error(self):
        set_module_args(dict(
            nitro_user='******',
            nitro_pass='******',
            nsip='192.0.2.1',
            state='present',
        ))
        from ansible_collections.ansible.misc.plugins.modules import netscaler_cs_action

        if sys.version_info[:2] == (2, 6):
            self.skipTest('requests library not available under python2.6')

        class MockException(Exception):
            pass
        client_mock = Mock()
        attrs = {'login.side_effect': requests.exceptions.SSLError}
        client_mock.configure_mock(**attrs)
        m = Mock(return_value=client_mock)
        with patch.multiple(
            'ansible_collections.ansible.misc.plugins.modules.netscaler_cs_action',
            get_nitro_client=m,
            nitro_exception=MockException,
        ):
            self.module = netscaler_cs_action
            result = self.failed()
            self.assertTrue(result['msg'].startswith('SSL Error'), msg='SSL Error was not handled gracefully')
    def test_save_config_not_called_on_state_absent(self):
        set_module_args(
            dict(
                nitro_user='******',
                nitro_pass='******',
                nsip='192.0.2.1',
                state='absent',
                save_config=False,
            ))
        from ansible_collections.ansible.misc.plugins.modules import netscaler_server

        client_mock = Mock()

        m = Mock(return_value=client_mock)

        server_proxy_mock = Mock()

        with patch.multiple(
                'ansible_collections.ansible.misc.plugins.modules.netscaler_server',
                get_nitro_client=m,
                server_exists=Mock(side_effect=[True, False]),
                ConfigProxy=Mock(return_value=server_proxy_mock),
                do_state_change=Mock(return_value=Mock(errorcode=0))):
            self.module = netscaler_server
            self.exited()
            self.assertNotIn(call.save_config(), client_mock.mock_calls)
    def test_absent_cs_action_noop_flow(self):
        set_module_args(dict(
            nitro_user='******',
            nitro_pass='******',
            nsip='192.0.2.1',
            state='absent',
        ))
        from ansible_collections.ansible.misc.plugins.modules import netscaler_cs_action

        client_mock = Mock()

        m = Mock(return_value=client_mock)

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

        with patch.multiple(
            'ansible_collections.ansible.misc.plugins.modules.netscaler_cs_action',
            get_nitro_client=m,
            diff_list=Mock(return_value={}),
            get_immutables_intersection=Mock(return_value=[]),
            action_exists=Mock(side_effect=[False, False]),
            action_identical=Mock(side_effect=[False, False]),
            ensure_feature_is_enabled=Mock(return_value=True),
            ConfigProxy=config_proxy_mock,
        ):
            self.module = netscaler_cs_action
            self.exited()
            cs_action_proxy_mock.assert_not_called()
    def test_do_state_change_fail(self):
        set_module_args(
            dict(
                nitro_user='******',
                nitro_pass='******',
                nsip='192.0.2.1',
                state='present',
            ))
        from ansible_collections.ansible.misc.plugins.modules import netscaler_server

        client_mock = Mock()

        m = Mock(return_value=client_mock)

        server_proxy_mock = Mock()

        with patch.multiple(
                'ansible_collections.ansible.misc.plugins.modules.netscaler_server',
                nitro_exception=self.MockException,
                get_nitro_client=m,
                server_exists=Mock(side_effect=[True, False]),
                ConfigProxy=Mock(return_value=server_proxy_mock),
                diff_list=Mock(return_value={}),
                do_state_change=Mock(return_value=Mock(
                    errorcode=1, message='Failed on purpose'))):
            self.module = netscaler_server
            result = self.failed()
            self.assertEqual(
                result['msg'],
                'Error when setting disabled state. errorcode: 1 message: Failed on purpose'
            )
    def test_graceful_nitro_exception_state_absent(self):
        set_module_args(dict(
            nitro_user='******',
            nitro_pass='******',
            nsip='192.0.2.1',
            state='absent',
        ))
        from ansible_collections.ansible.misc.plugins.modules 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_collections.ansible.misc.plugins.modules.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'
            )
    def test_update_service_when_service_differs(self):
        self.set_module_state('present')
        from ansible_collections.ansible.misc.plugins.modules import netscaler_service
        service_proxy_mock = MagicMock()
        attrs = {
            'diff_object.return_value': {},
        }
        service_proxy_mock.configure_mock(**attrs)

        m = MagicMock(return_value=service_proxy_mock)
        service_exists_mock = Mock(side_effect=[True, True])
        service_identical_mock = Mock(side_effect=[False, True])
        monitor_bindings_identical_mock = Mock(side_effect=[True, True])
        all_identical_mock = Mock(side_effect=[False])

        with patch.multiple(
                'ansible_collections.ansible.misc.plugins.modules.netscaler_service',
                ConfigProxy=m,
                service_exists=service_exists_mock,
                service_identical=service_identical_mock,
                monitor_bindings_identical=monitor_bindings_identical_mock,
                all_identical=all_identical_mock,
                do_state_change=Mock(return_value=Mock(errorcode=0)),
        ):
            self.module = netscaler_service
            result = self.exited()
            service_proxy_mock.assert_has_calls([call.update()])
            self.assertTrue(result['changed'], msg='Change not recorded')