Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Report message in handler heartbeat #2688

Merged
merged 7 commits into from
Oct 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions azurelinuxagent/ga/exthandlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1001,6 +1001,8 @@ def report_ext_handler_status(self, vm_status, ext_handler, goal_state_changed):
heartbeat = ext_handler_i.collect_heartbeat()
if heartbeat is not None:
handler_status.status = heartbeat.get('status')
if 'formattedMessage' in heartbeat:
handler_status.message = parse_formatted_message(heartbeat.get('formattedMessage'))
except ExtensionError as e:
ext_handler_i.set_handler_status(message=ustr(e), code=e.code)

Expand Down
28 changes: 25 additions & 3 deletions tests/ga/test_exthandlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,8 @@
from azurelinuxagent.common.utils.extensionprocessutil import TELEMETRY_MESSAGE_MAX_LEN, format_stdout_stderr, \
read_output
from azurelinuxagent.ga.exthandlers import parse_ext_status, ExtHandlerInstance, ExtCommandEnvVariable, \
ExtensionStatusError, _DEFAULT_SEQ_NO
from tests.protocol import mockwiredata
from tests.protocol.mocks import mock_wire_protocol
ExtensionStatusError, _DEFAULT_SEQ_NO, get_exthandlers_handler, ExtHandlerState
from tests.protocol.mocks import mock_wire_protocol, mockwiredata
from tests.tools import AgentTestCase, patch, mock_sleep, clear_singleton_instances


Expand Down Expand Up @@ -288,6 +287,29 @@ def test_command_extension_log_truncates_correctly(self, mock_log_dir):
with open(log_file_path) as truncated_log_file:
self.assertEqual(truncated_log_file.read(), "{second_line}\n".format(second_line=second_line))

def test_it_should_report_the_message_in_the_hearbeat(self):
def heartbeat_with_message():
return {'code': 0, 'formattedMessage': {'lang': 'en-US', 'message': 'This is a heartbeat message'},
'status': 'ready'}

with mock_wire_protocol(mockwiredata.DATA_FILE) as protocol:
with patch("azurelinuxagent.common.protocol.wire.WireProtocol.report_vm_status", return_value=None):
with patch("azurelinuxagent.ga.exthandlers.ExtHandlerInstance.collect_heartbeat",
side_effect=heartbeat_with_message):
with patch("azurelinuxagent.ga.exthandlers.ExtHandlerInstance.get_handler_state",
return_value=ExtHandlerState.Enabled):
with patch("azurelinuxagent.ga.exthandlers.ExtHandlerInstance.collect_ext_status",
return_value=None):
exthandlers_handler = get_exthandlers_handler(protocol)
exthandlers_handler.run()
vm_status = exthandlers_handler.report_ext_handlers_status()
ext_handler = vm_status.vmAgent.extensionHandlers[0]
self.assertEqual(ext_handler.message,
heartbeat_with_message().get('formattedMessage').get('message'),
"Extension handler messages don't match")
self.assertEqual(ext_handler.status, heartbeat_with_message().get('status'),
"Extension handler statuses don't match")

class LaunchCommandTestCase(AgentTestCase):
"""
Test cases for launch_command
Expand Down