|
| 1 | +# Copyright 2010 New Relic, Inc. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import json |
| 16 | +import os |
| 17 | + |
| 18 | +import pytest |
| 19 | +from testing_support.mock_http_client import create_client_cls |
| 20 | +from testing_support.validators.validate_internal_metrics import validate_internal_metrics |
| 21 | + |
| 22 | +from newrelic.common.utilization import AWSUtilization |
| 23 | + |
| 24 | +CURRENT_DIR = os.path.dirname(os.path.realpath(__file__)) |
| 25 | +FIXTURE = os.path.normpath(os.path.join(CURRENT_DIR, "aws.json")) |
| 26 | + |
| 27 | +_parameters_list = ["testname", "auth_token_cls", "uri", "expected_vendors_hash", "expected_metrics"] |
| 28 | + |
| 29 | +_parameters = ",".join(_parameters_list) |
| 30 | + |
| 31 | + |
| 32 | +@classmethod |
| 33 | +def fake_token(cls): |
| 34 | + return "FakeToken" |
| 35 | + |
| 36 | + |
| 37 | +@classmethod |
| 38 | +def no_token(cls): |
| 39 | + return None |
| 40 | + |
| 41 | + |
| 42 | +def _load_tests(): |
| 43 | + with open(FIXTURE) as fh: |
| 44 | + js = fh.read() |
| 45 | + return json.loads(js) |
| 46 | + |
| 47 | + |
| 48 | +def _parametrize_test(test): |
| 49 | + return tuple([test.get(f, None) for f in _parameters_list]) |
| 50 | + |
| 51 | + |
| 52 | +# Load the tests from the JSON fixture |
| 53 | +_aws_tests = [_parametrize_test(t) for t in _load_tests()] |
| 54 | + |
| 55 | + |
| 56 | +# Order of tests: |
| 57 | +# 1. auth token fails, no cached data |
| 58 | +# 2. Auth token succeeds, but utilization data is invalid, no cached data |
| 59 | +# 3. Auth token succeeds, utilization data is valid, data is cached (check both cache and returned value) |
| 60 | +# 4. Auth token fails, but cached data is valid, return cached data |
| 61 | +# 5. Auth token succeeds, utilization data is valid, data is cached (check both cache and returned value) |
| 62 | + |
| 63 | + |
| 64 | +@pytest.mark.parametrize(_parameters, _aws_tests) |
| 65 | +def test_aws_utilization_caching(monkeypatch, testname, auth_token_cls, uri, expected_vendors_hash, expected_metrics): |
| 66 | + def _get_mock_return_value(api_result): |
| 67 | + if api_result["timeout"]: |
| 68 | + return 0, None |
| 69 | + else: |
| 70 | + body = json.dumps(api_result["response"]) |
| 71 | + return 200, body.encode("utf-8") |
| 72 | + |
| 73 | + url, api_result = uri.popitem() |
| 74 | + status, data = _get_mock_return_value(api_result) |
| 75 | + |
| 76 | + client_cls = create_client_cls(status, data, url) |
| 77 | + |
| 78 | + monkeypatch.setattr(AWSUtilization, "CLIENT_CLS", client_cls) |
| 79 | + monkeypatch.setattr(AWSUtilization, "fetchAuthToken", fake_token if auth_token_cls == "fake_token" else no_token) |
| 80 | + |
| 81 | + metrics = [] |
| 82 | + if expected_metrics: |
| 83 | + metrics = [(k, v.get("call_count")) for k, v in expected_metrics.items()] |
| 84 | + |
| 85 | + # Define function that actually runs the test |
| 86 | + |
| 87 | + @validate_internal_metrics(metrics=metrics) |
| 88 | + def _test_aws_data(): |
| 89 | + data = AWSUtilization.detect() |
| 90 | + |
| 91 | + if data: |
| 92 | + aws_vendor_hash = {"aws": data} |
| 93 | + else: |
| 94 | + aws_vendor_hash = None |
| 95 | + |
| 96 | + assert aws_vendor_hash == expected_vendors_hash |
| 97 | + if expected_vendors_hash is not None: |
| 98 | + # Check that the cached data is set to the most recent valid data |
| 99 | + assert json.loads(AWSUtilization._utilization_data.decode("utf-8")) == data |
| 100 | + |
| 101 | + _test_aws_data() |
| 102 | + |
| 103 | + assert not client_cls.FAIL |
0 commit comments