Repository: Azure/WALinuxAgent
Pull request #3004 (2023-12-27 20:24:38)

log update time for self updater
<!-- DO NOT DELETE THIS TEMPLATE -->

## Description
Logging update time(when agent will get upgrade) to match the current behavior as we are taking a dependency on this telemetry in deployment queries for detecting TEMU issue

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://app.codecov.io/gh/Azure/WALinuxAgent/pull/3004?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Azure) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Comparison is base [(`e74677b`)](https://app.codecov.io/gh/Azure/WALinuxAgent/commit/e74677b3f2a7559cfd132f2f0390d76640050a71?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Azure) 71.92% compared to head [(`d4c3517`)](https://app.codecov.io/gh/Azure/WALinuxAgent/pull/3004?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Azure) 71.93%.
> Report is 1 commits behind head on develop.


<details><summary>Additional details and impacted files</summary>


```diff
@@           Coverage Diff            @@
##           develop    #3004   +/-   ##
========================================
  Coverage    71.92%   71.93%           
========================================
  Files          109      109           
  Lines        16200    16204    +4     
  Branches      2313     2314    +1     
========================================
+ Hits         11652    11656    +4     
  Misses        4005     4005           
  Partials       543      543           
```



</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/Azure/WALinuxAgent/pull/3004?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Azure).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Azure).


Patch:
diff --git a/azurelinuxagent/ga/self_update_version_updater.py b/azurelinuxagent/ga/self_update_version_updater.py
index 2b04fd5c..2dc87bf8 100644
--- a/azurelinuxagent/ga/self_update_version_updater.py
+++ b/azurelinuxagent/ga/self_update_version_updater.py
@@ -71,7 +71,7 @@ class SelfUpdateVersionUpdater(GAVersionUpdater):
         """
         This method ensure that update is allowed only once per (hotfix/Regular) upgrade frequency
         """
-        now = datetime.datetime.now()
+        now = datetime.datetime.utcnow()
         upgrade_type = self._get_agent_upgrade_type(self._version)
         if upgrade_type == SelfUpdateType.Hotfix:
             next_update_time = self._get_next_process_time(self._last_attempted_self_update_time,
@@ -80,6 +80,12 @@ class SelfUpdateVersionUpdater(GAVersionUpdater):
             next_update_time = self._get_next_process_time(self._last_attempted_self_update_time,
                                                            conf.get_self_update_regular_frequency(), now)
 
+        if self._version > CURRENT_VERSION:
+            message = "Self-update discovered new {0} upgrade WALinuxAgent-{1}; Will upgrade on or after {2}".format(
+                upgrade_type, str(self._version), next_update_time.strftime(logger.Logger.LogTimeFormatInUTC))
+            logger.info(message)
+            add_event(op=WALAEventOperation.AgentUpgrade, message=message, log_event=False)
+
         if next_update_time <= now:
             # Update the last upgrade check time even if no new agent is available for upgrade
             self._last_attempted_self_update_time = now
@@ -92,7 +98,7 @@ class SelfUpdateVersionUpdater(GAVersionUpdater):
         the agent has not attempted to download the manifest in the last 1 hour
         If we allow update, we update the last attempted manifest download time
         """
-        now = datetime.datetime.now()
+        now = datetime.datetime.utcnow()
 
         if self._last_attempted_manifest_download_time != datetime.datetime.min:
             next_attempt_time = self._last_attempted_manifest_download_time + datetime.timedelta(
@@ -161,7 +167,7 @@ class SelfUpdateVersionUpdater(GAVersionUpdater):
         """
         This function logs the update message after we check version allowed to update.
         """
-        msg = "Self-update discovered new agent version:{0} in agent manifest for goal state {1}, will update the agent before processing the goal state.".format(
+        msg = "Self-update is ready to upgrade the new agent: {0} now before processing the goal state: {1}".format(
             str(self._version), self._gs_id)
         logger.info(msg)
         add_event(op=WALAEventOperation.AgentUpgrade, message=msg, log_event=False)


Test patch:
diff --git a/tests/ga/test_agent_update_handler.py b/tests/ga/test_agent_update_handler.py
index 6c069bfc..56162897 100644
--- a/tests/ga/test_agent_update_handler.py
+++ b/tests/ga/test_agent_update_handler.py
@@ -83,7 +83,7 @@ class TestAgentUpdate(UpdateTestCase):
 
     def _assert_update_discovered_from_agent_manifest(self, mock_telemetry, inc=1, version="9.9.9.10"):
         upgrade_event_msgs = [kwarg['message'] for _, kwarg in mock_telemetry.call_args_list if
-                              'Self-update discovered new agent version:{0} in agent manifest for goal state incarnation_{1}'.format(version, inc) in kwarg['message'] and kwarg[
+                              'Self-update is ready to upgrade the new agent: {0} now before processing the goal state: incarnation_{1}'.format(version, inc) in kwarg['message'] and kwarg[
                                   'op'] == WALAEventOperation.AgentUpgrade]
         self.assertEqual(1, len(upgrade_event_msgs),
                          "Did not find the event indicating that the new version found. Got: {0}".format(