def test_graceful_no_connection_error(self):

        if sys.version_info[:2] == (2, 6):
            self.skipTest('requests library not available under python2.6')
        set_module_args(dict(
            nitro_user='******',
            nitro_pass='******',
            nsip='192.0.2.1',
        ))
        from ansible_collections.misc.not_a_real_collection.plugins.modules import netscaler_save_config

        class MockException(Exception):
            pass
        client_mock = Mock()
        attrs = {'login.side_effect': requests.exceptions.ConnectionError}
        client_mock.configure_mock(**attrs)
        m = Mock(return_value=client_mock)
        with patch.multiple(
            'ansible_collections.misc.not_a_real_collection.plugins.modules.netscaler_save_config',
            get_nitro_client=m,
            nitro_exception=MockException,
        ):
            self.module = netscaler_save_config
            result = self.failed()
            self.assertTrue(result['msg'].startswith('Connection error'), msg='Connection error was not handled gracefully')
Esempio n. 2
0
    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.misc.not_a_real_collection.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.misc.not_a_real_collection.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()])
Esempio n. 3
0
    def test_absent_cs_vserver_noop_flow(self):
        set_module_args(
            dict(
                nitro_user='******',
                nitro_pass='******',
                nsip='192.0.2.1',
                state='absent',
            ))
        from ansible_collections.misc.not_a_real_collection.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.misc.not_a_real_collection.plugins.modules.netscaler_cs_vserver',
                get_nitro_client=m,
                diff_list=Mock(return_value={}),
                get_immutables_intersection=Mock(return_value=[]),
                cs_vserver_exists=Mock(side_effect=[False, False]),
                cs_vserver_identical=Mock(side_effect=[False, False]),
                ensure_feature_is_enabled=Mock(return_value=True),
                ConfigProxy=config_proxy_mock,
        ):
            self.module = netscaler_cs_vserver
            self.exited()
            cs_vserver_proxy_mock.assert_not_called()
Esempio n. 4
0
    def test_absent_gslb_site_failed_delete(self):
        set_module_args(dict(
            nitro_user='******',
            nitro_pass='******',
            nsip='192.0.2.1',
            state='absent',
        ))
        from ansible_collections.misc.not_a_real_collection.plugins.modules import netscaler_gslb_site

        client_mock = Mock()

        m = Mock(return_value=client_mock)

        glsb_site_proxy_attrs = {
            'diff_object.return_value': {},
        }
        gslb_site_proxy_mock = Mock()
        gslb_site_proxy_mock.configure_mock(**glsb_site_proxy_attrs)
        config_proxy_mock = Mock(return_value=gslb_site_proxy_mock)

        with patch.multiple(
            'ansible_collections.misc.not_a_real_collection.plugins.modules.netscaler_gslb_site',
            nitro_exception=self.MockException,
            get_nitro_client=m,
            diff_list=Mock(return_value={}),
            get_immutables_intersection=Mock(return_value=[]),
            gslb_site_exists=Mock(side_effect=[True, True]),
            gslb_site_identical=Mock(side_effect=[False, False]),
            ensure_feature_is_enabled=Mock(),
            ConfigProxy=config_proxy_mock,
        ):
            self.module = netscaler_gslb_site
            result = self.failed()
            self.assertEqual(result['msg'], 'GSLB site still exists')
            self.assertTrue(result['failed'])
    def test_present_gslb_service_identical_flow(self):
        set_module_args(dict(
            nitro_user='******',
            nitro_pass='******',
            nsip='192.0.2.1',
            state='present',
        ))
        from ansible_collections.misc.not_a_real_collection.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.misc.not_a_real_collection.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, True]),
            gslb_service_identical=Mock(side_effect=[True, True]),
            nitro_exception=self.MockException,
            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_not_called()
Esempio n. 6
0
    def test_present_server_failed_create(self):
        set_module_args(
            dict(
                nitro_user='******',
                nitro_pass='******',
                nsip='192.0.2.1',
                state='present',
            ))
        from ansible_collections.misc.not_a_real_collection.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.misc.not_a_real_collection.plugins.modules.netscaler_ssl_certkey',
                nitro_exception=self.MockException,
                get_nitro_client=m,
                diff_list=Mock(return_value={}),
                get_immutables_intersection=Mock(return_value=[]),
                key_exists=Mock(side_effect=[False, False]),
                key_identical=Mock(side_effect=[False, False]),
                ConfigProxy=config_proxy_mock,
        ):
            self.module = netscaler_ssl_certkey
            result = self.failed()
            self.assertEqual(result['msg'], 'SSL certkey does not exist')
            self.assertTrue(result['failed'])
Esempio n. 7
0
    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.misc.not_a_real_collection.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.misc.not_a_real_collection.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()
Esempio n. 8
0
    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.misc.not_a_real_collection.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.misc.not_a_real_collection.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'])
Esempio n. 9
0
    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.misc.not_a_real_collection.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.misc.not_a_real_collection.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()])
Esempio n. 10
0
 def test_ensure_feature_is_enabled(self):
     client = Mock()
     attrs = {'get_enabled_features.return_value': ['GSLB']}
     client.configure_mock(**attrs)
     ensure_feature_is_enabled(client, 'GSLB')
     ensure_feature_is_enabled(client, 'LB')
     client.enable_features.assert_called_once_with('LB')
    def test_graceful_no_connection_error(self):

        if sys.version_info[:2] == (2, 6):
            self.skipTest('requests library not available under python2.6')
        self.set_module_state('present')
        from ansible_collections.misc.not_a_real_collection.plugins.modules import netscaler_cs_policy

        client_mock = Mock()
        attrs = {'login.side_effect': requests.exceptions.ConnectionError}
        client_mock.configure_mock(**attrs)
        m = Mock(return_value=client_mock)
        with patch.multiple(
            'ansible_collections.misc.not_a_real_collection.plugins.modules.netscaler_cs_policy',
            get_nitro_client=m,
            nitro_exception=self.MockException,
        ):
            self.module = netscaler_cs_policy
            result = self.failed()
            self.assertTrue(result['msg'].startswith('Connection error'), msg='Connection error was not handled gracefully')
Esempio n. 12
0
    def test_cs_vserver_ssl_called(self):
        set_module_args(
            dict(
                nitro_user='******',
                nitro_pass='******',
                nsip='192.0.2.1',
                state='present',
                servicetype='SSL',
            ))
        from ansible_collections.misc.not_a_real_collection.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)
        ssl_certkey_bindings_sync_mock = Mock()

        with patch.multiple(
                'ansible_collections.misc.not_a_real_collection.plugins.modules.netscaler_cs_vserver',
                nitro_exception=self.MockException,
                get_nitro_client=m,
                diff_list=Mock(return_value={}),
                get_immutables_intersection=Mock(return_value=[]),
                cs_vserver_exists=Mock(side_effect=[True, True]),
                cs_vserver_identical=Mock(side_effect=[False, True]),
                ensure_feature_is_enabled=Mock(return_value=True),
                ssl_certkey_bindings_identical=Mock(side_effect=[False, True]),
                ssl_certkey_bindings_sync=ssl_certkey_bindings_sync_mock,
                do_state_change=Mock(return_value=Mock(errorcode=0)),
                ConfigProxy=config_proxy_mock,
        ):
            self.module = netscaler_cs_vserver
            result = self.exited()
            self.assertTrue(result['changed'])
            self.assertTrue(ssl_certkey_bindings_sync_mock.called)
Esempio n. 13
0
    def test_present_gslb_vserver_service_bindings_error_flow(self):
        set_module_args(
            dict(
                nitro_user='******',
                nitro_pass='******',
                nsip='192.0.2.1',
                state='present',
            ))
        from ansible_collections.misc.not_a_real_collection.plugins.modules import netscaler_gslb_vserver

        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.misc.not_a_real_collection.plugins.modules.netscaler_gslb_vserver',
                get_nitro_client=m,
                diff_list=Mock(return_value={}),
                get_immutables_intersection=Mock(return_value=[]),
                gslb_vserver_exists=Mock(side_effect=[True, True]),
                gslb_vserver_identical=Mock(side_effect=[True, True, True]),
                service_bindings_identical=Mock(
                    side_effect=[False, False, False]),
                do_state_change=Mock(return_value=Mock(errorcode=0)),
                nitro_exception=self.MockException,
                ensure_feature_is_enabled=Mock(),
                ConfigProxy=config_proxy_mock,
        ):
            self.module = netscaler_gslb_vserver
            result = self.failed()
            self.assertEqual(result['msg'],
                             'Service bindings differ from configured')
            self.assertTrue(result['failed'])
Esempio n. 14
0
    def test_present_cs_vserver_update_immutable_attribute(self):
        set_module_args(
            dict(
                nitro_user='******',
                nitro_pass='******',
                nsip='192.0.2.1',
                state='present',
            ))
        from ansible_collections.misc.not_a_real_collection.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.misc.not_a_real_collection.plugins.modules.netscaler_cs_vserver',
                nitro_exception=self.MockException,
                get_nitro_client=m,
                diff_list=Mock(return_value={}),
                get_immutables_intersection=Mock(return_value=['domain']),
                cs_vserver_exists=Mock(side_effect=[True, True]),
                cs_vserver_identical=Mock(side_effect=[False, False]),
                ensure_feature_is_enabled=Mock(return_value=True),
                ConfigProxy=config_proxy_mock,
        ):
            self.module = netscaler_cs_vserver
            result = self.failed()
            self.assertEqual(
                result['msg'],
                'Cannot update immutable attributes [\'domain\']')
            self.assertTrue(result['failed'])