예제 #1
0
def test_aws(monkeypatch, testname, uri, expected_vendors_hash,
             expected_metrics):

    # Generate mock responses for InsecureHttpClient

    def _get_mock_return_value(api_result):
        if api_result['timeout']:
            return 0, None
        else:
            body = json.dumps(api_result['response'])
            return 200, body.encode('utf-8')

    @classmethod
    def fake_token(cls):
        return "FakeToken"

    url, api_result = uri.popitem()
    status, data = _get_mock_return_value(api_result)

    client_cls = create_client_cls(status, data, url)

    monkeypatch.setattr(AWSUtilization, "CLIENT_CLS", client_cls)
    monkeypatch.setattr(AWSUtilization, "fetchAuthToken", fake_token)

    metrics = []
    if expected_metrics:
        metrics = [(k, v.get('call_count'))
                   for k, v in expected_metrics.items()]

    # Define function that actually runs the test

    @validate_internal_metrics(metrics=metrics)
    def _test_aws_data():

        data = AWSUtilization.detect()

        if data:
            aws_vendor_hash = {'aws': data}
        else:
            aws_vendor_hash = None

        assert aws_vendor_hash == expected_vendors_hash

    _test_aws_data()

    assert not client_cls.FAIL
    def _test_utilization_data():

        data = _get_response_body_for_test(test)
        client_cls = create_client_cls(200, data)
        monkeypatch.setattr(CommonUtilization, "CLIENT_CLS", client_cls)

        with UpdatedSettings() as settings:
            # Ignoring docker will ensure that there is nothing extra in the
            # gathered utilization data
            monkeypatch.setattr(settings.utilization, "detect_docker", False)

            local_config, = AgentProtocol._connect_payload(
                '', [], [], settings)
            util_output = local_config['utilization']
            expected_output = test['expected_output_json']

            # The agent does not record full_hostname and it's not required
            expected_output.pop("full_hostname")

            assert expected_output == util_output
예제 #3
0
def test_gcp(monkeypatch, testname, uri, expected_vendors_hash,
             expected_metrics):

    # Generate mock responses for HttpClient

    def _get_mock_return_value(api_result):
        if api_result['timeout']:
            return 0, None
        else:
            body = json.dumps(api_result['response'])
            return 200, body.encode('utf-8')

    url, api_result = uri.popitem()
    status, data = _get_mock_return_value(api_result)

    client_cls = create_client_cls(status, data, url)

    monkeypatch.setattr(GCPUtilization, "CLIENT_CLS", client_cls)

    metrics = []
    if expected_metrics:
        metrics = [(k, v.get('call_count'))
                   for k, v in expected_metrics.items()]

    # Define function that actually runs the test

    @validate_internal_metrics(metrics=metrics)
    def _test_gcp_data():

        data = GCPUtilization.detect()

        if data:
            gcp_vendor_hash = {'gcp': data}
        else:
            gcp_vendor_hash = None

        assert gcp_vendor_hash == expected_vendors_hash

    _test_gcp_data()

    assert not client_cls.FAIL