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_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')
    def test_absent_operation_no_change(self):
        self.set_module_state('absent')
        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, False])

        with patch.multiple(
                'ansible_collections.ansible.misc.plugins.modules.netscaler_servicegroup',
                ConfigProxy=m,
                servicegroup_exists=servicegroup_exists_mock,
        ):
            self.module = netscaler_servicegroup
            result = self.exited()
            servicegroup_proxy_mock.assert_not_called()
            self.assertFalse(result['changed'],
                             msg='Changed status not set correctly')
    def test_immutables_changed(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)

        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=[False, True]),
                get_immutables_intersection=Mock(return_value=['some']),
                nitro_exception=self.MockException,
        ):
            self.module = netscaler_servicegroup
            result = self.failed()
            self.assertTrue(
                result['msg'].startswith('Cannot update immutable attributes'))
    def test_create_non_existing_cs_policy(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=[False, True])

        with patch.multiple(
                'ansible_collections.ansible.misc.plugins.modules.netscaler_cs_policy',
                ConfigProxy=m,
                policy_exists=policy_exists_mock,
                nitro_exception=self.MockException,
                ensure_feature_is_enabled=Mock(),
        ):
            self.module = netscaler_cs_policy
            result = self.exited()
            cs_policy_mock.assert_has_calls([call.add()])
            self.assertTrue(result['changed'], msg='Change not recorded')
    def test_absent_operation_no_change(self):
        self.set_module_state('absent')
        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=[False, False])

        with patch.multiple(
                'ansible_collections.ansible.misc.plugins.modules.netscaler_cs_policy',
                ConfigProxy=m,
                policy_exists=policy_exists_mock,
                nitro_exception=self.MockException,
                ensure_feature_is_enabled=Mock(),
        ):
            self.module = netscaler_cs_policy
            result = self.exited()
            cs_policy_mock.assert_not_called()
            self.assertFalse(result['changed'],
                             msg='Changed status not set correctly')