Skip to content

Commit

Permalink
fix: Handle when the announce response has no fields
Browse files Browse the repository at this point in the history
Signed-off-by: Ferenc Géczi <ferenc.geczi@ibm.com>
  • Loading branch information
Ferenc- committed Feb 15, 2023
1 parent caddc33 commit f6e8383
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
4 changes: 4 additions & 0 deletions instana/agent/host.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,10 @@ def announce(self, discovery):
logger.debug("announce: response is not JSON: (%s)", raw_json)
return None

if not hasattr(payload, 'get'):
logger.debug("announce: response payload has no fields: (%s)", payload)
return None

if not payload.get('pid'):
logger.debug("announce: response payload has no pid: (%s)", payload)
return None
Expand Down
22 changes: 22 additions & 0 deletions tests/platforms/test_host.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,28 @@ def test_announce_fails_with_non_json(self, mock_requests_session_put):
self.assertEqual(len(log.records), 1)
self.assertIn('response is not JSON', log.output[0])

@patch.object(requests.Session, "put")
def test_announce_fails_with_empty_list_json(self, mock_requests_session_put):
test_pid = 4242
test_process_name = 'test_process'
test_process_args = ['-v', '-d']
test_agent_uuid = '83bf1e09-ab16-4203-abf5-34ee0977023a'

mock_response = MagicMock()
mock_response.status_code = 200
mock_response.content = '[]'
mock_requests_session_put.return_value = mock_response

self.create_agent_and_setup_tracer()
d = Discovery(pid=test_pid,
name=test_process_name, args=test_process_args)
with self.assertLogs(logger, level='DEBUG') as log:
payload = self.agent.announce(d)
self.assertIsNone(payload)
self.assertEqual(len(log.output), 1)
self.assertEqual(len(log.records), 1)
self.assertIn('payload has no fields', log.output[0])


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

0 comments on commit f6e8383

Please sign in to comment.