Skip to content

Commit 4da5999

Browse files
committed
test: Cover faulty announce responses
Signed-off-by: Ferenc Géczi <[email protected]>
1 parent e54aabf commit 4da5999

File tree

1 file changed

+135
-1
lines changed

1 file changed

+135
-1
lines changed

tests/platforms/test_host.py

Lines changed: 135 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,17 @@
77
import logging
88
import unittest
99

10+
from mock import MagicMock, patch
11+
12+
import requests
13+
1014
from instana.agent.host import HostAgent
11-
from instana.tracer import InstanaTracer
15+
from instana.fsm import Discovery
16+
from instana.log import logger
1217
from instana.options import StandardOptions
1318
from instana.recorder import StanRecorder
1419
from instana.singletons import get_agent, set_agent, get_tracer, set_tracer
20+
from instana.tracer import InstanaTracer
1521

1622

1723
class TestHost(unittest.TestCase):
@@ -80,3 +86,131 @@ def test_agent_instana_service_name(self):
8086
os.environ['INSTANA_SERVICE_NAME'] = "greycake"
8187
self.create_agent_and_setup_tracer()
8288
self.assertEqual(self.agent.options.service_name, "greycake")
89+
90+
@patch.object(requests.Session, "put")
91+
def test_announce_is_successful(self, mock_requests_session_put):
92+
test_pid = 4242
93+
test_process_name = 'test_process'
94+
test_process_args = ['-v', '-d']
95+
test_agent_uuid = '83bf1e09-ab16-4203-abf5-34ee0977023a'
96+
97+
mock_response = MagicMock()
98+
mock_response.status_code = 200
99+
mock_response.content = (
100+
'{'
101+
f' "pid": {test_pid}, '
102+
f' "agentUuid": "{test_agent_uuid}"'
103+
'}')
104+
105+
# This mocks the call to self.agent.client.put
106+
mock_requests_session_put.return_value = mock_response
107+
108+
self.create_agent_and_setup_tracer()
109+
d = Discovery(pid=test_pid,
110+
name=test_process_name, args=test_process_args)
111+
payload = self.agent.announce(d)
112+
113+
self.assertIn('pid', payload)
114+
self.assertEqual(test_pid, payload['pid'])
115+
116+
self.assertIn('agentUuid', payload)
117+
self.assertEqual(test_agent_uuid, payload['agentUuid'])
118+
119+
120+
@patch.object(requests.Session, "put")
121+
def test_announce_fails_with_non_200(self, mock_requests_session_put):
122+
test_pid = 4242
123+
test_process_name = 'test_process'
124+
test_process_args = ['-v', '-d']
125+
test_agent_uuid = '83bf1e09-ab16-4203-abf5-34ee0977023a'
126+
127+
mock_response = MagicMock()
128+
mock_response.status_code = 404
129+
mock_response.content = ''
130+
mock_requests_session_put.return_value = mock_response
131+
132+
self.create_agent_and_setup_tracer()
133+
d = Discovery(pid=test_pid,
134+
name=test_process_name, args=test_process_args)
135+
with self.assertLogs(logger, level='DEBUG') as log:
136+
payload = self.agent.announce(d)
137+
self.assertIsNone(payload)
138+
self.assertEqual(len(log.output), 1)
139+
self.assertEqual(len(log.records), 1)
140+
self.assertIn('response status code', log.output[0])
141+
self.assertIn('is NOT 200', log.output[0])
142+
143+
144+
@patch.object(requests.Session, "put")
145+
def test_announce_fails_with_non_json(self, mock_requests_session_put):
146+
test_pid = 4242
147+
test_process_name = 'test_process'
148+
test_process_args = ['-v', '-d']
149+
test_agent_uuid = '83bf1e09-ab16-4203-abf5-34ee0977023a'
150+
151+
mock_response = MagicMock()
152+
mock_response.status_code = 200
153+
mock_response.content = ''
154+
mock_requests_session_put.return_value = mock_response
155+
156+
self.create_agent_and_setup_tracer()
157+
d = Discovery(pid=test_pid,
158+
name=test_process_name, args=test_process_args)
159+
with self.assertLogs(logger, level='DEBUG') as log:
160+
payload = self.agent.announce(d)
161+
self.assertIsNone(payload)
162+
self.assertEqual(len(log.output), 1)
163+
self.assertEqual(len(log.records), 1)
164+
self.assertIn('response is not JSON', log.output[0])
165+
166+
167+
@patch.object(requests.Session, "put")
168+
def test_announce_fails_with_missing_pid(self, mock_requests_session_put):
169+
test_pid = 4242
170+
test_process_name = 'test_process'
171+
test_process_args = ['-v', '-d']
172+
test_agent_uuid = '83bf1e09-ab16-4203-abf5-34ee0977023a'
173+
174+
mock_response = MagicMock()
175+
mock_response.status_code = 200
176+
mock_response.content = (
177+
'{'
178+
f' "agentUuid": "{test_agent_uuid}"'
179+
'}')
180+
mock_requests_session_put.return_value = mock_response
181+
182+
self.create_agent_and_setup_tracer()
183+
d = Discovery(pid=test_pid,
184+
name=test_process_name, args=test_process_args)
185+
with self.assertLogs(logger, level='DEBUG') as log:
186+
payload = self.agent.announce(d)
187+
self.assertIsNone(payload)
188+
self.assertEqual(len(log.output), 1)
189+
self.assertEqual(len(log.records), 1)
190+
self.assertIn('response payload has no pid', log.output[0])
191+
192+
193+
@patch.object(requests.Session, "put")
194+
def test_announce_fails_with_missing_uuid(self, mock_requests_session_put):
195+
test_pid = 4242
196+
test_process_name = 'test_process'
197+
test_process_args = ['-v', '-d']
198+
test_agent_uuid = '83bf1e09-ab16-4203-abf5-34ee0977023a'
199+
200+
mock_response = MagicMock()
201+
mock_response.status_code = 200
202+
mock_response.content = (
203+
'{'
204+
f' "pid": {test_pid} '
205+
'}')
206+
mock_requests_session_put.return_value = mock_response
207+
208+
self.create_agent_and_setup_tracer()
209+
d = Discovery(pid=test_pid,
210+
name=test_process_name, args=test_process_args)
211+
with self.assertLogs(logger, level='DEBUG') as log:
212+
payload = self.agent.announce(d)
213+
self.assertIsNone(payload)
214+
self.assertEqual(len(log.output), 1)
215+
self.assertEqual(len(log.records), 1)
216+
self.assertIn('response payload has no agentUuid', log.output[0])

0 commit comments

Comments
 (0)