def requests_get_mock(*args, **kwargs):
    url = args[0]
    # The parameter that creates the query params (kwargs) is an unordered dict,
    #   so the query params can be in any order
    if url.startswith(YARN_APPS_URL_BASE):
        query = url[len(YARN_APPS_URL_BASE):]
        if query in [
                "?states=RUNNING&applicationTypes=MAPREDUCE",
                "?applicationTypes=MAPREDUCE&states=RUNNING"
        ]:
            return MockResponse(
                file_path=os.path.join(HERE, "fixtures", "apps_metrics"))
        else:
            raise Exception(
                "Apps URL must have the two query parameters: states=RUNNING and applicationTypes=MAPREDUCE"
            )

    elif url == MR_JOBS_URL:
        return MockResponse(
            file_path=os.path.join(HERE, "fixtures", "job_metrics"))

    elif url == MR_JOB_COUNTERS_URL:
        return MockResponse(
            file_path=os.path.join(HERE, "fixtures", "job_counter_metrics"))

    elif url == MR_TASKS_URL:
        return MockResponse(
            file_path=os.path.join(HERE, "fixtures", "task_metrics"))

    elif url == CLUSTER_INFO_URL:
        return MockResponse(
            file_path=os.path.join(HERE, "fixtures", "cluster_info"))

    else:
        raise Exception("There is no mock request for {}".format(url))
Exemple #2
0
def requests_get_mock(url, *args, **kwargs):
    if url == NAME_SYSTEM_STATE_URL:
        return MockResponse(file_path=os.path.join(FIXTURE_DIR, 'hdfs_namesystem_state.json'))
    elif url == NAME_SYSTEM_URL:
        return MockResponse(file_path=os.path.join(FIXTURE_DIR, 'hdfs_namesystem.json'))
    elif url == NAME_SYSTEM_METADATA_URL:
        return MockResponse(file_path=os.path.join(FIXTURE_DIR, 'hdfs_namesystem_info.json'))
Exemple #3
0
def test_metadata_unit(datadog_agent):
    version = common._get_pdns_version()
    if version == 3:
        instance = common.CONFIG
    elif version == 4:
        instance = common.CONFIG_V4
    check = PowerDNSRecursorCheck("powerdns_recursor", {}, [instance])
    check.check_id = 'test:123'
    check.log = mock.MagicMock()

    config_obj, tags = check._get_config(instance)

    with mock.patch('requests.get', side_effect=requests.exceptions.Timeout()):
        check._collect_metadata(config_obj)
        datadog_agent.assert_metadata_count(0)
        check.log.debug.assert_called_with(
            'Error collecting PowerDNS Recursor version: %s', '')

    datadog_agent.reset()
    with mock.patch('requests.get', return_value=MockResponse()):
        check._collect_metadata(config_obj)
        datadog_agent.assert_metadata_count(0)
        check.log.debug.assert_called_with(
            "Couldn't find the PowerDNS Recursor Server version header")

    datadog_agent.reset()
    with mock.patch(
            'requests.get',
            return_value=MockResponse(headers={'Server': 'wrong_stuff'})):
        check._collect_metadata(config_obj)
        datadog_agent.assert_metadata_count(0)
        check.log.debug.assert_called_with(
            'Error while decoding PowerDNS Recursor version: %s',
            'list index out of range')
Exemple #4
0
def mocked_requests_get_sys_delta(*args, **kwargs):
    if args[0].endswith("/metadata"):
        return MockResponse(
            file_path=os.path.join(HERE, 'fixtures', 'metadata.json'))
    elif args[0].endswith("/stats"):
        return MockResponse(file_path=os.path.join(
            HERE, 'fixtures', 'stats_wrong_system_delta.json'))
    else:
        return MockResponse(status_code=404)
Exemple #5
0
def mocked_requests_get_windows(*args, **kwargs):
    if args[0].endswith("/metadata"):
        return MockResponse(
            file_path=os.path.join(HERE, 'fixtures', 'metadata.json'))
    elif args[0].endswith("/stats"):
        return MockResponse(
            file_path=os.path.join(HERE, 'fixtures', WINDOWS_STATS_FIXTURE))
    else:
        return MockResponse(status_code=404)
Exemple #6
0
def test_api__make_get_request(harbor_api):
    harbor_api.http = MagicMock()
    harbor_api.http.get = MagicMock(return_value=MockResponse(
        json_data={'json': True}))
    assert harbor_api._make_get_request('{base_url}/api/path') == {
        "json": True
    }

    harbor_api.http.get = MagicMock(return_value=MockResponse(status_code=500))
    with pytest.raises(HTTPError):
        harbor_api._make_get_request('{base_url}/api/path')
def mock_requests_get(url, *args, **kwargs):
    url_parts = url.split('/')

    if url_parts[0] != 'mocked':
        return MockResponse(status_code=404)

    path = os.path.join(common.HERE, 'fixtures', 'standalone',
                        '{}.json'.format(url_parts[1]))
    if not os.path.exists(path):
        return MockResponse(status_code=404)

    return MockResponse(file_path=path)
Exemple #8
0
 def mock_requests_get(url, *args, **kwargs):
     if url == instance['api_url'] + '/sys/leader':
         return MockResponse(
             json_data={
                 'ha_enabled': False,
                 'is_self': False,
                 'leader_address': 'bar',
                 'leader_cluster_address': '',
             })
     return requests_get(url, *args, **kwargs)
Exemple #9
0
def test_err_response(aggregator, instance):

    check = TwistlockCheck('twistlock', {}, [instance])

    with pytest.raises(Exception, match='^Error in response'):
        with mock.patch(
                'requests.get',
                return_value=MockResponse('{"err": "invalid credentials"}'),
                autospec=True):

            check.check(instance)
Exemple #10
0
 def mock_requests_get(url, *args, **kwargs):
     if url == instance['api_url'] + '/sys/leader':
         return MockResponse(
             json_data={
                 'ha_enabled': False,
                 'is_self': True,
                 'leader_address': '',
                 'leader_cluster_address': ''
             })
     elif url == instance['api_url'] + '/sys/health':
         return MockResponse(
             json_data={
                 'cluster_id': '9e25ccdb-09ea-8bd8-0521-34cf3ef7a4cc',
                 'cluster_name': 'vault-cluster-f5f44063',
                 'initialized': True,
                 'replication_dr_mode': 'disabled',
                 'replication_performance_mode': 'disabled',
                 'sealed': False,
                 'server_time_utc': 1529357080,
                 'standby': False,
                 'version': '0.10.2',
             })
     return requests_get(url, *args, **kwargs)
Exemple #11
0
    def test_service_check_500_fail(self, aggregator, dd_run_check,
                                    global_tags):
        instance = {'use_openmetrics': False}
        instance.update(INSTANCES['main'])
        c = Vault(Vault.CHECK_NAME, {}, [instance])

        with mock.patch('requests.get',
                        return_value=MockResponse(status_code=500)):
            with pytest.raises(
                    Exception,
                    match=r'^The Vault endpoint `{}.+?` returned 500$'.format(
                        re.escape(instance['api_url']))):
                dd_run_check(c, extract_message=True)

        aggregator.assert_service_check(Vault.SERVICE_CHECK_CONNECT,
                                        status=Vault.CRITICAL,
                                        tags=global_tags,
                                        count=1)
Exemple #12
0
 def mock_requests_get(url, *args, **kwargs):
     if url == instance['api_url'] + '/sys/health':
         return MockResponse(
             json_data={
                 'cluster_id': '9e25ccdb-09ea-8bd8-0521-34cf3ef7a4cc',
                 'cluster_name': 'vault-cluster-f5f44063',
                 'initialized': False,
                 'replication_dr_mode': 'secondary',
                 'replication_performance_mode': 'primary',
                 'sealed': False,
                 'server_time_utc': 1529357080,
                 'standby': True,
                 'performance_standby': False,
                 'version': '0.10.2',
             },
             status_code=200,
         )
     return requests_get(url, *args, **kwargs)
Exemple #13
0
def test_api__make_paginated_get_request(harbor_api):
    expected_result = [{'item': i} for i in range(20)]
    paginated_result = [[expected_result[i], expected_result[i + 1]]
                        for i in range(0,
                                       len(expected_result) - 1, 2)]
    values = []
    for r in paginated_result:
        values.append(
            MockResponse(json_data=r,
                         headers={
                             'link':
                             'Link: <unused_url>; rel=next; type="text/plain"'
                         }))
    values[-1].headers.pop('link')

    harbor_api.http = MagicMock()
    harbor_api.http.get = MagicMock(side_effect=values)

    assert harbor_api._make_paginated_get_request(
        '{base_url}/api/path') == expected_result
Exemple #14
0
 def mock_get(url, *args, **kwargs):
     split_url = url.split('/')
     path = split_url[-1]
     return MockResponse(file_path=os.path.join(
         HERE, 'fixtures', fixture_group, '{}.json'.format(path)))
Exemple #15
0
 def get(url: AnyStr, *_: Any, **__: Any):
     resource = url.split('/')[-1]
     return MockResponse(file_path=os.path.join(HERE, 'compose', 'fixtures', f'{resource}_metrics'))
Exemple #16
0
def mock_requests_get(url, *args, **kwargs):
    fixture = 'jmx_metrics.txt' if urlparse(
        url).port == 11001 else 'node_metrics.txt'
    return MockResponse(file_path=get_fixture_path(fixture))
Exemple #17
0
def requests_metadata_mock(*args, **kwargs):
    return MockResponse(
        file_path=os.path.join(FIXTURE_DIR, 'hdfs_datanode_info_jmx.json'))
Exemple #18
0
def mock_requests_get(url, *args, **kwargs):
    exporter_type = 'jmx' if urlparse(url).port == common.JMX_PORT else 'node'
    return MockResponse(
        file_path=common.get_metrics_fixture_path(exporter_type))
Exemple #19
0
def mocked_requests(_, *args, **kwargs):
    def match(url, *candidates_url):
        for c in candidates_url:
            if url == c.format(base_url=URL):
                return True
        return False

    if match(args[0], LOGIN_PRE_1_7_URL, LOGIN_URL):
        return MockResponse()
    elif match(args[0], HEALTH_URL) and HARBOR_VERSION >= VERSION_1_8:
        return MockResponse(json_data=HEALTH_FIXTURE)
    elif match(args[0], PING_URL):
        return MockResponse('Pong')
    elif match(args[0],
               CHARTREPO_HEALTH_URL) and HARBOR_VERSION >= VERSION_1_6:
        return MockResponse(json_data=CHARTREPO_HEALTH_FIXTURE)
    elif match(args[0], PROJECTS_URL):
        return MockResponse(json_data=PROJECTS_FIXTURE)
    elif match(args[0], REGISTRIES_PRE_1_8_URL, REGISTRIES_URL):
        if HARBOR_VERSION >= VERSION_1_8:
            return MockResponse(json_data=REGISTRIES_FIXTURE)
        return MockResponse(json_data=REGISTRIES_PRE_1_8_FIXTURE)
    elif match(args[0], REGISTRIES_PING_PRE_1_8_URL, REGISTRIES_PING_URL):
        return MockResponse()
    elif match(args[0], VOLUME_INFO_URL):
        if HARBOR_VERSION < VERSION_2_2:
            return MockResponse(json_data=VOLUME_INFO_PRE_2_2_FIXTURE)
        return MockResponse(json_data=VOLUME_INFO_FIXTURE)
    elif match(args[0], SYSTEM_INFO_URL):
        return MockResponse(json_data=SYSTEM_INFO_FIXTURE)

    return MockResponse(status_code=404)
 def mock_requests_get(url, *args, **kwargs):
     fixture = url.rsplit('/', 1)[-1]
     return MockResponse(file_path=os.path.join(os.path.dirname(
         __file__), 'fixtures', tag_condition, fixture))
Exemple #21
0
 def mock_requests_get(url, *args, **kwargs):
     if url == instance['api_url'] + '/sys/leader':
         return MockResponse(json_data={'errors': ["Vault is sealed"]},
                             status_code=503)
     return requests_get(url, *args, **kwargs)