def test_scroll_pages(http_mock, _, __, aggregator, instance, events_v3_p1, events_v3_p2): events_res_p1 = mock.MagicMock() events_res_p2 = mock.MagicMock() events_res_p1.json.return_value = events_v3_p1 events_res_p2.json.return_value = events_v3_p2 http_mock.get.side_effect = (events_res_p1, events_res_p2) check = CloudFoundryApiCheck('cloud_foundry_api', {}, [instance]) with mock.patch.object(check, "log") as log_mock: dd_events = check.scroll_pages("url", {"param": "foo"}, {"header": "bar"}, []) expected_calls = [ (("url",), ({"params": {"param": "foo", "page": 1}, "headers": {"header": "bar"}})), (("url",), ({"params": {"param": "foo", "page": 2}, "headers": {"header": "bar"}})), ] assert http_mock.get.call_args_list == expected_calls # Only 3 events collected, the fourth one is too old assert len(dd_events) == 3 # The bad event is skipped log_mock.exception.assert_called_once() assert "Could not parse event" in log_mock.exception.call_args[0][0] # On second call, we don't collect any event, and don't go to the second page http_mock.reset_mock() # reset_mock doesn't reset side_effect or return_value, so manually reassign it http_mock.get.side_effect = (events_res_p1, events_res_p2) dd_events = check.scroll_pages("url", {"param": "foo"}, {"header": "bar"}, []) expected_calls = [(("url",), ({"params": {"param": "foo", "page": 1}, "headers": {"header": "bar"}}))] assert http_mock.get.call_args_list == expected_calls assert dd_events == {} aggregator.assert_service_check( name="cloud_foundry_api.api.can_connect", status=CloudFoundryApiCheck.OK, tags=["api_url:api.sys.domain.com", "foo:bar"], count=2, )
def test_scroll_pages_errors(_, __, aggregator, instance, events_v3_p1): check = CloudFoundryApiCheck('cloud_foundry_api', {}, [instance]) check._http = None # initialize the _http attribute for mocking with mock.patch.object(check, "_http") as http_mock: http_mock.get.side_effect = RequestException() check.scroll_pages("", {}, {}, []) aggregator.assert_service_check( name="cloud_foundry_api.api.can_connect", status=CloudFoundryApiCheck.CRITICAL, tags=["api_url:api.sys.domain.com", "foo:bar"], count=1, ) aggregator.reset() with mock.patch.object(check, "_http") as http_mock: http_mock.get.return_value = mock.MagicMock( raise_for_status=mock.MagicMock(side_effect=HTTPError())) check.scroll_pages("", {}, {}, []) aggregator.assert_service_check( name="cloud_foundry_api.api.can_connect", status=CloudFoundryApiCheck.CRITICAL, tags=["api_url:api.sys.domain.com", "foo:bar"], count=1, ) aggregator.reset() with mock.patch.object(check, "_http") as http_mock: http_mock.get.return_value = mock.MagicMock(json=mock.MagicMock( side_effect=ValueError())) check.scroll_pages("", {}, {}, []) aggregator.assert_service_check( name="cloud_foundry_api.api.can_connect", status=CloudFoundryApiCheck.CRITICAL, tags=["api_url:api.sys.domain.com", "foo:bar"], count=1, ) aggregator.reset() # Getting an error in the middle of pagination still sends a critical service check, # but returns the events already gathered with mock.patch.object(check, "_http") as http_mock: events_res_p1 = mock.MagicMock() events_res_p1.json.return_value = events_v3_p1 http_mock.get.side_effect = (events_res_p1, RequestException()) dd_events = check.scroll_pages("", {}, {}, []) aggregator.assert_service_check( name="cloud_foundry_api.api.can_connect", status=CloudFoundryApiCheck.CRITICAL, tags=["api_url:api.sys.domain.com", "foo:bar"], count=1, ) assert len(dd_events) == 2