Skip to content

Commit

Permalink
add test for failed PUT log request in hostplugin
Browse files Browse the repository at this point in the history
  • Loading branch information
pgombar committed Jul 22, 2020
1 parent 455ddd5 commit 20ff1c8
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 8 deletions.
2 changes: 1 addition & 1 deletion azurelinuxagent/common/protocol/hostplugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ def put_vm_log(self, content):
else:
logger.info("HostGAPlugin: Upload VM logs succeeded")

return True
return response

def put_vm_status(self, status_blob, sas_url, config_blob_type=None):
"""
Expand Down
1 change: 0 additions & 1 deletion azurelinuxagent/common/protocol/wire.py
Original file line number Diff line number Diff line change
Expand Up @@ -1234,7 +1234,6 @@ def get_artifacts_profile(self):

def upload_logs(self, content):
host_func = lambda: self._upload_logs_through_host(content)
# propagate any exceptions up to the main thread
return self.call_hostplugin_with_container_check(host_func)

def _upload_logs_through_host(self, content):
Expand Down
31 changes: 27 additions & 4 deletions tests/protocol/test_hostplugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -538,12 +538,11 @@ def test_validate_page_blobs(self):
test_goal_state,
exp_method, exp_url, exp_data)

def test_validate_http_request_when_uploading_logs(self):
def test_validate_http_request_for_put_vm_log(self):
def http_put_handler(url, *args, **kwargs):
if self.is_host_plugin_put_logs_request(url):
http_put_handler.args, http_put_handler.kwargs = args, kwargs
return MockResponse(body=b'', status_code=200)
self.fail('The upload logs request was sent to the wrong url: {0}'.format(url))

http_put_handler.args, http_put_handler.kwargs = [], {}

Expand Down Expand Up @@ -579,8 +578,32 @@ def http_put_handler(url, *args, **kwargs):

# Special check for correlation id header value, check for pattern, not exact value
self.assertTrue("x-ms-client-correlationid" in headers.keys(), "Correlation id not found in headers!")
self.assertTrue(UUID_PATTERN.match(headers["x-ms-client-correlationid"]),
"Correlation id is not in GUID form!")
self.assertTrue(UUID_PATTERN.match(headers["x-ms-client-correlationid"]), "Correlation id is not in GUID form!")

def test_put_vm_log_should_raise_an_exception_when_request_fails(self):
def http_put_handler(url, *args, **kwargs):
if self.is_host_plugin_put_logs_request(url):
http_put_handler.args, http_put_handler.kwargs = args, kwargs
return MockResponse(body=b'Gone', status_code=410)

http_put_handler.args, http_put_handler.kwargs = [], {}

with mock_wire_protocol(DATA_FILE, http_put_handler=http_put_handler) as protocol:
test_goal_state = protocol.client.get_goal_state()

host_client = wire.HostPluginProtocol(wireserver_url,
test_goal_state.container_id,
test_goal_state.role_config_name)

self.assertFalse(host_client.is_initialized, "Host plugin should not be initialized!")

with self.assertRaises(HttpError) as context_manager:
content = b"test"
host_client.put_vm_log(content)

self.assertIsInstance(context_manager.exception, HttpError)
self.assertIn("410", context_manager.exception.message)
self.assertIn("Gone", context_manager.exception.message)

def test_validate_get_extension_artifacts(self):
with mock_wire_protocol(DATA_FILE) as protocol:
Expand Down
2 changes: 0 additions & 2 deletions tests/protocol/test_wire.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,6 @@ def test_upload_status_blob_should_use_the_host_channel_by_default(self, *_):
def http_put_handler(url, *_, **__):
if protocol.get_endpoint() in url and url.endswith('/status'):
return MockResponse(body=b'', status_code=200)
self.fail('The upload status request was sent to the wrong url: {0}'.format(url))

with mock_wire_protocol(mockwiredata.DATA_FILE, http_put_handler=http_put_handler) as protocol:
HostPluginProtocol.set_default_channel(False)
Expand Down Expand Up @@ -820,7 +819,6 @@ def test_upload_logs_should_not_refresh_plugin_when_first_attempt_succeeds(self)
def http_put_handler(url, *_, **__):
if self.is_host_plugin_put_logs_request(url):
return MockResponse(body=b'', status_code=200)
self.fail('The upload logs request was sent to the wrong url: {0}'.format(url))

with mock_wire_protocol(mockwiredata.DATA_FILE, http_put_handler=http_put_handler) as protocol:
content = b"test"
Expand Down

0 comments on commit 20ff1c8

Please sign in to comment.