Skip to content

Commit f6e8383

Browse files
committed
fix: Handle when the announce response has no fields
Signed-off-by: Ferenc Géczi <[email protected]>
1 parent caddc33 commit f6e8383

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

instana/agent/host.py

+4
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,10 @@ def announce(self, discovery):
202202
logger.debug("announce: response is not JSON: (%s)", raw_json)
203203
return None
204204

205+
if not hasattr(payload, 'get'):
206+
logger.debug("announce: response payload has no fields: (%s)", payload)
207+
return None
208+
205209
if not payload.get('pid'):
206210
logger.debug("announce: response payload has no pid: (%s)", payload)
207211
return None

tests/platforms/test_host.py

+22
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,28 @@ def test_announce_fails_with_non_json(self, mock_requests_session_put):
163163
self.assertEqual(len(log.records), 1)
164164
self.assertIn('response is not JSON', log.output[0])
165165

166+
@patch.object(requests.Session, "put")
167+
def test_announce_fails_with_empty_list_json(self, mock_requests_session_put):
168+
test_pid = 4242
169+
test_process_name = 'test_process'
170+
test_process_args = ['-v', '-d']
171+
test_agent_uuid = '83bf1e09-ab16-4203-abf5-34ee0977023a'
172+
173+
mock_response = MagicMock()
174+
mock_response.status_code = 200
175+
mock_response.content = '[]'
176+
mock_requests_session_put.return_value = mock_response
177+
178+
self.create_agent_and_setup_tracer()
179+
d = Discovery(pid=test_pid,
180+
name=test_process_name, args=test_process_args)
181+
with self.assertLogs(logger, level='DEBUG') as log:
182+
payload = self.agent.announce(d)
183+
self.assertIsNone(payload)
184+
self.assertEqual(len(log.output), 1)
185+
self.assertEqual(len(log.records), 1)
186+
self.assertIn('payload has no fields', log.output[0])
187+
166188

167189
@patch.object(requests.Session, "put")
168190
def test_announce_fails_with_missing_pid(self, mock_requests_session_put):

0 commit comments

Comments
 (0)