Repository: Azure/WALinuxAgent
Pull request #2688 (2022-10-21 20:19:52)

Report message in handler heartbeat
<!-- DO NOT DELETE THIS TEMPLATE -->

## Description
This PR updates the agent to report the extension handler heartbeat message. Previously, any message in heartbeat.log was ignored. 

Issue # <!-- if any -->
<!--
Please add an informative description that covers that changes made by the pull request. 
This checklist is used to make sure that common issues in a pull request are addressed.
This will expedite the process of getting your pull request merged and avoid extra work on your part to fix issues discovered during the review process.
-->

---

### PR information
- [ ] The title of the PR is clear and informative.
- [ ] There are a small number of commits, each of which has an informative message. This means that previously merged commits do not appear in the history of the PR. For information on cleaning up the commits in your pull request, [see this page](https://github.com/Azure/azure-powershell/blob/master/documentation/development-docs/cleaning-up-commits.md).
- [ ] If applicable, the PR references the bug/issue that it fixes in the description.
- [ ] New Unit tests were added for the changes made

### Quality of Code and Contribution Guidelines
- [ ] I have read the [contribution guidelines](https://github.com/Azure/WALinuxAgent/blob/master/.github/CONTRIBUTING.md).

codecov[bot]: # [Codecov](https://codecov.io/gh/Azure/WALinuxAgent/pull/2688?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Azure) Report
> Merging [#2688](https://codecov.io/gh/Azure/WALinuxAgent/pull/2688?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Azure) (a586586) into [develop](https://codecov.io/gh/Azure/WALinuxAgent/commit/527443c105fd9bbbad99f03905f2bf9efdb4a8b5?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Azure) (527443c) will **decrease** coverage by `0.00%`.
> The diff coverage is `0.00%`.

```diff
@@             Coverage Diff             @@
##           develop    #2688      +/-   ##
===========================================
- Coverage    72.06%   72.05%   -0.01%     
===========================================
  Files          103      103              
  Lines        15702    15703       +1     
  Branches      2237     2237              
===========================================
  Hits         11315    11315              
- Misses        3872     3873       +1     
  Partials       515      515              
```


| [Impacted Files](https://codecov.io/gh/Azure/WALinuxAgent/pull/2688?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Azure) | Coverage Δ | |
|---|---|---|
| [azurelinuxagent/ga/exthandlers.py](https://codecov.io/gh/Azure/WALinuxAgent/pull/2688/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Azure#diff-YXp1cmVsaW51eGFnZW50L2dhL2V4dGhhbmRsZXJzLnB5) | `85.60% <0.00%> (-0.07%)` | :arrow_down: |

Help us with your feedback. Take ten seconds to tell us [how you rate us](https://about.codecov.io/nps?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Azure). Have a feature suggestion? [Share it here.](https://app.codecov.io/gh/feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Azure)


Patch:
diff --git a/azurelinuxagent/ga/exthandlers.py b/azurelinuxagent/ga/exthandlers.py
index 974ab19f..0aa4ed93 100644
--- a/azurelinuxagent/ga/exthandlers.py
+++ b/azurelinuxagent/ga/exthandlers.py
@@ -1001,6 +1001,8 @@ class ExtHandlersHandler(object):
                     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)
 


Test patch:
diff --git a/tests/ga/test_exthandlers.py b/tests/ga/test_exthandlers.py
index 69079cf7..67b07717 100644
--- a/tests/ga/test_exthandlers.py
+++ b/tests/ga/test_exthandlers.py
@@ -31,9 +31,8 @@ from azurelinuxagent.common.utils import fileutil
 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
 
 
@@ -288,6 +287,29 @@ class TestExtHandlers(AgentTestCase):
         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