def test_dispatch_with_ignore_saved_search_errors_true(self):
        """
        Test dispatch method to get value None in case of flag ignore_saved_search_errors=True
        """

        path = '/servicesNS/%s/%s/saved/searches/%s/dispatch' % (
            "admin", "search", "component")
        helper = SplunkHelper(FakeInstanceConfig())

        # Mock the post response of request_session
        helper.requests_session.post = mock.MagicMock()
        helper.requests_session.post.return_value = MockResponse({
            "reason":
            "Not Found",
            "status_code":
            404,
            "url":
            path
        })

        res = helper.dispatch(
            mocked_saved_search(), "admin", "search",
            helper.instance_config.ignore_saved_search_errors, None)

        self.assertEqual(res, None)
Exemple #2
0
    def test_splunk_dispatch(self):

        username = "******"
        appname = "myapp"
        instance_config = SplunkInstanceConfig(
            {
                'url': 'dummy',
                'authentication': {
                    'basic_auth': {
                        'username': "******",
                        'password': "******"
                    }
                }
            }, {}, {
                'default_request_timeout_seconds': 5,
                'default_search_max_retry_count': 3,
                'default_search_seconds_between_retries': 1,
                'default_verify_ssl_certificate': False,
                'default_batch_size': 1000,
                'default_saved_searches_parallel': 3,
                'default_unique_key_fields': ["_bkt", "_cd"],
                'default_app': 'default',
                'default_parameters': {}
            })

        splunk_helper = SplunkHelper(instance_config)
        saved_search = SplunkSavedSearch(instance_config, {
            "name": "search",
            "parameters": {}
        })
        params = {"key1": "val1", "key2": "val2"}

        def _mocked_do_post(*args, **kwargs):
            self.assertEquals(args,
                              ('/servicesNS/%s/%s/saved/searches/%s/dispatch' %
                               (username, appname, quote(saved_search.name)),
                               params, 5, 'true'))

            class MockedResponse():
                def json(self):
                    return {"sid": "zesid"}

            return MockedResponse()

        setattr(splunk_helper, "_do_post", _mocked_do_post)

        res = splunk_helper.dispatch(saved_search, username, appname, 'true',
                                     params)
        self.assertEquals(res, "zesid")