Exemple #1
0
def test_discover_api_errors(instance):
    # Mock for creating the instance only
    with mock.patch.object(CloudFoundryApiCheck, "discover_api", return_value=("v3", "uaa_url")):
        check = CloudFoundryApiCheck('cloud_foundry_api', {}, [instance])
    check._http = None  # initialize the _http attribute for mocking

    with mock.patch.object(check, "_http") as http_mock, pytest.raises(RequestException):
        http_mock.get.side_effect = RequestException()
        check.discover_api()

    with mock.patch.object(check, "_http") as http_mock, pytest.raises(HTTPError):
        http_mock.get.return_value = mock.MagicMock(raise_for_status=mock.MagicMock(side_effect=HTTPError()))
        check.discover_api()

    with mock.patch.object(check, "_http") as http_mock, pytest.raises(ValueError):
        http_mock.get.return_value = mock.MagicMock(json=mock.MagicMock(side_effect=ValueError()))
        check.discover_api()

    with mock.patch.object(check, "_http") as http_mock, pytest.raises(CheckException):
        http_mock.get.return_value = mock.MagicMock(json=mock.MagicMock(return_value={"no_links": None}))
        check.discover_api()

    with mock.patch.object(check, "_http") as http_mock, pytest.raises(CheckException):
        http_mock.get.return_value = mock.MagicMock(json=mock.MagicMock(return_value={"links": {"no_uaa": None}}))
        check.discover_api()
def test_scroll_api_pages_errors(_, __, ___, aggregator, instance):
    check = CloudFoundryApiCheck('cloud_foundry_api', {}, [instance])
    check._http = None

    with mock.patch.object(check, "_http") as http_mock:
        http_mock.get.side_effect = RequestException()
        for _ in check.scroll_api_pages("", {}, {}):
            pass
        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.assert_service_check(
            "cloud_foundry_api.api.can_connect",
            status=CloudFoundryApiCheck.OK,
            count=0,
            tags=["api_url:api.sys.domain.com", "foo:bar"],
        )
        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()))
        for _ in check.scroll_api_pages("", {}, {}):
            pass
        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.assert_service_check(
            "cloud_foundry_api.api.can_connect",
            status=CloudFoundryApiCheck.OK,
            count=0,
            tags=["api_url:api.sys.domain.com", "foo:bar"],
        )
        aggregator.reset()

    with mock.patch.object(check, "_http") as http_mock:
        http_mock.get.return_value = mock.MagicMock(json=mock.MagicMock(
            side_effect=ValueError()))
        for _ in check.scroll_api_pages("", {}, {}):
            pass
        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.assert_service_check(
            "cloud_foundry_api.api.can_connect",
            status=CloudFoundryApiCheck.OK,
            count=0,
            tags=["api_url:api.sys.domain.com", "foo:bar"],
        )
        aggregator.reset()
def test_scroll_events_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_events("", {}, {})
        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_events("", {}, {})
        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_events("", {}, {})
        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_events("", {}, {})
        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
Exemple #4
0
def test_get_oauth_token_errors(_, aggregator, instance):
    check = CloudFoundryApiCheck('cloud_foundry_api', {}, [instance])
    check._http = None  # initialize the _http attribute for mocking

    with mock.patch.object(
            check, "_http") as http_mock, pytest.raises(RequestException):
        http_mock.get.side_effect = RequestException()
        check.get_oauth_token()
        aggregator.assert_service_check(
            name="cloud_foundry_api.uaa.can_authenticate",
            status=CloudFoundryApiCheck.CRITICAL,
            tags=[
                "uaa_url:uaa.sys.domain.com", "foo:bar",
                "api_url:api.sys.domain.com"
            ],
            count=1,
        )
        aggregator.reset()

    with mock.patch.object(check,
                           "_http") as http_mock, pytest.raises(HTTPError):
        http_mock.get.return_value = mock.MagicMock(
            raise_for_status=mock.MagicMock(side_effect=HTTPError()))
        check.get_oauth_token()
        aggregator.assert_service_check(
            name="cloud_foundry_api.uaa.can_authenticate",
            status=CloudFoundryApiCheck.CRITICAL,
            tags=[
                "uaa_url:uaa.sys.domain.com", "foo:bar",
                "api_url:api.sys.domain.com"
            ],
            count=1,
        )
        aggregator.reset()

    with mock.patch.object(check,
                           "_http") as http_mock, pytest.raises(ValueError):
        http_mock.get.return_value = mock.MagicMock(json=mock.MagicMock(
            side_effect=ValueError()))
        check.get_oauth_token()
        aggregator.assert_service_check(
            name="cloud_foundry_api.uaa.can_authenticate",
            status=CloudFoundryApiCheck.CRITICAL,
            tags=[
                "uaa_url:uaa.sys.domain.com", "foo:bar",
                "api_url:api.sys.domain.com"
            ],
            count=1,
        )
        aggregator.reset()
Exemple #5
0
def test_discover_api(api_info_v3, api_info_v2, instance):
    # Mock for creating the instance only
    with mock.patch.object(CloudFoundryApiCheck, "discover_api", return_value=("v3", "uaa_url")):
        check = CloudFoundryApiCheck('cloud_foundry_api', {}, [instance])
    check._http = None  # initialize the _http attribute for mocking

    # v2
    with mock.patch.object(check, "_http") as http_mock:
        http_mock.get.return_value = mock.MagicMock(json=mock.MagicMock(return_value=api_info_v2))
        api_version, uaa_url = check.discover_api()
        assert api_version == "v2"
        assert uaa_url == "https://uaa.sys.domain.com"
    # v3
    with mock.patch.object(check, "_http") as http_mock:
        http_mock.get.return_value = mock.MagicMock(json=mock.MagicMock(return_value=api_info_v3))
        api_version, uaa_url = check.discover_api()
        assert api_version == "v3"
        assert uaa_url == "https://uaa.sys.domain.com"