From 2053143c25fc7e4829e7a5a77bdc16b147fbf513 Mon Sep 17 00:00:00 2001 From: Allan Feldman Date: Mon, 24 Aug 2020 11:25:48 -0400 Subject: [PATCH] Update assertions to check payload types. --- tests/agent_features/test_serverless_mode.py | 4 ++++ tests/agent_unittests/test_agent_protocol.py | 2 +- tests/agent_unittests/test_http_client.py | 8 ++++---- .../validators/validate_serverless_data.py | 4 ++++ 4 files changed, 13 insertions(+), 5 deletions(-) diff --git a/tests/agent_features/test_serverless_mode.py b/tests/agent_features/test_serverless_mode.py index 2c93615736..75b5f0075d 100644 --- a/tests/agent_features/test_serverless_mode.py +++ b/tests/agent_features/test_serverless_mode.py @@ -12,6 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. +import json import pytest from newrelic.api.application import application_instance @@ -69,6 +70,9 @@ def _test(): # Validate that something is printed to stdout assert out + # Verify that the payload is loadable JSON + payload = json.loads(out) + def test_no_cat_headers(serverless_application): @background_task( diff --git a/tests/agent_unittests/test_agent_protocol.py b/tests/agent_unittests/test_agent_protocol.py index 475a76af89..1daa783c3d 100644 --- a/tests/agent_unittests/test_agent_protocol.py +++ b/tests/agent_unittests/test_agent_protocol.py @@ -532,7 +532,7 @@ def test_serverless_protocol_finalize(capsys): assert payload[:2] == [1, "NR_LAMBDA_MONITORING"] data = serverless_payload_decode(payload[2]) - assert data["data"] == {"metric_data": "[1,2,3]"} + assert data["data"] == {"metric_data": [1,2,3]} assert data["metadata"]["foo"] == "bar" assert data["metadata"]["agent_version"] != "x" diff --git a/tests/agent_unittests/test_http_client.py b/tests/agent_unittests/test_http_client.py index 359a3d2ea5..da22587f96 100644 --- a/tests/agent_unittests/test_http_client.py +++ b/tests/agent_unittests/test_http_client.py @@ -542,24 +542,24 @@ def test_serverless_mode_client(): for method in methods: params = {"method": method} status, data = client.send_request( - params=params, payload=method.encode("utf-8") + params=params, payload=json.dumps({"method": method}).encode("utf-8"), ) assert status == 200 assert json.loads(data.decode("utf-8")) # Verify that missing methods aren't captured - status, _ = client.send_request(payload=b"*") + status, _ = client.send_request(payload=b"{}") assert status == 400 # Verify that invalid methods aren't captured - status, _ = client.send_request(params={"method": "foo"}, payload=b"*") + status, _ = client.send_request(params={"method": "foo"}, payload=b"{}") assert status == 400 payloads = client.finalize() assert len(payloads) == len(methods) for method in methods: - assert payloads[method] == method.encode("utf-8") + assert payloads[method] == {"method": method} @pytest.mark.parametrize( diff --git a/tests/testing_support/validators/validate_serverless_data.py b/tests/testing_support/validators/validate_serverless_data.py index 1da9239546..ec3cc5ee56 100644 --- a/tests/testing_support/validators/validate_serverless_data.py +++ b/tests/testing_support/validators/validate_serverless_data.py @@ -12,6 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. + from newrelic.common.object_wrapper import ( transient_function_wrapper, function_wrapper) @@ -40,6 +41,9 @@ def _validate(): for method in expected_methods: assert method in payload + # Verify the method is not a byte string + assert isinstance(payload[method], (dict, list)) + for method in forgone_methods: assert method not in payload