Skip to content

Commit 2be7159

Browse files
committed
Add entity guid testing.
1 parent d7d98c7 commit 2be7159

File tree

2 files changed

+46
-28
lines changed

2 files changed

+46
-28
lines changed

newrelic/core/agent_control_health.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,6 @@ def write_to_health_file(self):
220220
status_time_unix_nano = time.time_ns()
221221
service_metadata = get_linking_metadata()
222222
entity_guid = service_metadata.get("entity.guid", "") if service_metadata else ""
223-
224223
try:
225224
health_dir_path = self.health_delivery_location
226225
if health_dir_path is None:

tests/agent_features/test_agent_control_health_check.py

Lines changed: 46 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,27 @@
2222
from testing_support.fixtures import initialize_agent
2323
from testing_support.http_client_recorder import HttpClientRecorder
2424

25+
from newrelic.common.object_wrapper import transient_function_wrapper
2526
from newrelic.config import _reset_configuration_done, initialize
2627
from newrelic.core.agent_control_health import HealthStatus, agent_control_health_instance
2728
from newrelic.core.agent_protocol import AgentProtocol
2829
from newrelic.core.application import Application
2930
from newrelic.core.config import finalize_application_settings, global_settings
3031
from newrelic.network.exceptions import DiscardDataForRequest
3132

32-
def mock_get_service_linking_metadata():
33-
"""Mock function that returns static entity GUID."""
34-
metadata["entity.guid"] = "ABC123"
33+
34+
@transient_function_wrapper("newrelic.api.time_trace", "get_service_linking_metadata")
35+
def _wrap_get_service_linking_metadata(wrapped, instance, args, kwargs):
36+
metadata = {"entity.type": "SERVICE"}
37+
38+
# Set hardcoded values for testing so we can verify the correct entity guid was written to the health file
39+
metadata["entity.name"] = "test-app"
40+
metadata["entity.guid"] = "mock-entity-guid-12345"
41+
metadata["hostname"] = "test-hostname"
42+
3543
return metadata
3644

45+
3746
def get_health_file_contents(tmp_path):
3847
# Grab the file we just wrote to and read its contents
3948
health_file = list(Path(tmp_path).iterdir())[0]
@@ -94,6 +103,7 @@ def test_agent_control_not_enabled(monkeypatch, tmp_path):
94103
assert not agent_control_health_instance().health_check_enabled
95104

96105

106+
@_wrap_get_service_linking_metadata
97107
def test_write_to_file_healthy_status(monkeypatch, tmp_path):
98108
# Setup expected env vars to run agent control health check
99109
monkeypatch.setenv("NEW_RELIC_AGENT_CONTROL_ENABLED", "True")
@@ -108,12 +118,14 @@ def test_write_to_file_healthy_status(monkeypatch, tmp_path):
108118
contents = get_health_file_contents(tmp_path)
109119

110120
# Assert on contents of health file
111-
assert len(contents) == 4
112-
assert contents[0] == "healthy: True\n"
113-
assert contents[1] == "status: Healthy\n"
114-
assert int(re.search(r"status_time_unix_nano: (\d+)", contents[3]).group(1)) > 0
121+
assert len(contents) == 5
122+
assert contents[0] == "entity_guid: mock-entity-guid-12345\n"
123+
assert contents[1] == "healthy: True\n"
124+
assert contents[2] == "status: Healthy\n"
125+
assert int(re.search(r"status_time_unix_nano: (\d+)", contents[4]).group(1)) > 0
115126

116127

128+
@_wrap_get_service_linking_metadata
117129
def test_write_to_file_unhealthy_status(monkeypatch, tmp_path):
118130
# Setup expected env vars to run agent control health check
119131
monkeypatch.setenv("NEW_RELIC_AGENT_CONTROL_ENABLED", "True")
@@ -130,14 +142,16 @@ def test_write_to_file_unhealthy_status(monkeypatch, tmp_path):
130142
contents = get_health_file_contents(tmp_path)
131143

132144
# Assert on contents of health file
133-
assert len(contents) == 5
134-
assert contents[0] == "healthy: False\n"
135-
assert contents[1] == "status: Invalid license key (HTTP status code 401)\n"
136-
assert contents[2] == "start_time_unix_nano: 1234567890\n"
137-
assert int(re.search(r"status_time_unix_nano: (\d+)", contents[3]).group(1)) > 0
138-
assert contents[4] == "last_error: NR-APM-001\n"
145+
assert len(contents) == 6
146+
assert contents[0] == "entity_guid: mock-entity-guid-12345\n"
147+
assert contents[1] == "healthy: False\n"
148+
assert contents[2] == "status: Invalid license key (HTTP status code 401)\n"
149+
assert contents[3] == "start_time_unix_nano: 1234567890\n"
150+
assert int(re.search(r"status_time_unix_nano: (\d+)", contents[4]).group(1)) > 0
151+
assert contents[5] == "last_error: NR-APM-001\n"
139152

140153

154+
@_wrap_get_service_linking_metadata
141155
def test_no_override_on_unhealthy_shutdown(monkeypatch, tmp_path):
142156
# Setup expected env vars to run agent control health check
143157
monkeypatch.setenv("NEW_RELIC_AGENT_CONTROL_ENABLED", "True")
@@ -156,10 +170,11 @@ def test_no_override_on_unhealthy_shutdown(monkeypatch, tmp_path):
156170
contents = get_health_file_contents(tmp_path)
157171

158172
# Assert on contents of health file
159-
assert len(contents) == 5
160-
assert contents[0] == "healthy: False\n"
161-
assert contents[1] == "status: Invalid license key (HTTP status code 401)\n"
162-
assert contents[4] == "last_error: NR-APM-001\n"
173+
assert len(contents) == 6
174+
assert contents[0] == "entity_guid: mock-entity-guid-12345\n"
175+
assert contents[1] == "healthy: False\n"
176+
assert contents[2] == "status: Invalid license key (HTTP status code 401)\n"
177+
assert contents[5] == "last_error: NR-APM-001\n"
163178

164179

165180
def test_health_check_running_threads(monkeypatch, tmp_path):
@@ -190,6 +205,7 @@ def test_health_check_running_threads(monkeypatch, tmp_path):
190205
assert running_threads[1].name == "Agent-Control-Health-Main-Thread"
191206

192207

208+
@_wrap_get_service_linking_metadata
193209
def test_proxy_error_status(monkeypatch, tmp_path):
194210
# Setup expected env vars to run agent control health check
195211
monkeypatch.setenv("NEW_RELIC_AGENT_CONTROL_ENABLED", "True")
@@ -214,10 +230,11 @@ def test_proxy_error_status(monkeypatch, tmp_path):
214230
contents = get_health_file_contents(tmp_path)
215231

216232
# Assert on contents of health file
217-
assert len(contents) == 5
218-
assert contents[0] == "healthy: False\n"
219-
assert contents[1] == "status: HTTP Proxy configuration error; response code 407\n"
220-
assert contents[4] == "last_error: NR-APM-007\n"
233+
assert len(contents) == 6
234+
assert contents[0] == "entity_guid: mock-entity-guid-12345\n"
235+
assert contents[1] == "healthy: False\n"
236+
assert contents[2] == "status: HTTP Proxy configuration error; response code 407\n"
237+
assert contents[5] == "last_error: NR-APM-007\n"
221238

222239

223240
def test_multiple_activations_running_threads(monkeypatch, tmp_path):
@@ -270,10 +287,11 @@ def test_update_to_healthy(monkeypatch, tmp_path):
270287
contents = get_health_file_contents(tmp_path)
271288

272289
# Assert on contents of health file
273-
assert contents[0] == "healthy: True\n"
274-
assert contents[1] == "status: Healthy\n"
290+
assert contents[1] == "healthy: True\n"
291+
assert contents[2] == "status: Healthy\n"
275292

276293

294+
@_wrap_get_service_linking_metadata
277295
def test_max_app_name_status(monkeypatch, tmp_path):
278296
# Setup expected env vars to run agent control health check
279297
monkeypatch.setenv("NEW_RELIC_AGENT_CONTROL_ENABLED", "True")
@@ -289,7 +307,8 @@ def test_max_app_name_status(monkeypatch, tmp_path):
289307
contents = get_health_file_contents(tmp_path)
290308

291309
# Assert on contents of health file
292-
assert len(contents) == 5
293-
assert contents[0] == "healthy: False\n"
294-
assert contents[1] == "status: The maximum number of configured app names (3) exceeded\n"
295-
assert contents[4] == "last_error: NR-APM-006\n"
310+
assert len(contents) == 6
311+
assert contents[0] == "entity_guid: mock-entity-guid-12345\n"
312+
assert contents[1] == "healthy: False\n"
313+
assert contents[2] == "status: The maximum number of configured app names (3) exceeded\n"
314+
assert contents[5] == "last_error: NR-APM-006\n"

0 commit comments

Comments
 (0)