Repository: Azure/WALinuxAgent
Pull request #1576 (2019-07-09 23:37:52)

Emit the 'Download succeeded' event when the actual download happens.
<!-- DO NOT DELETE THIS TEMPLATE -->

## Description
Emit the 'Download succeeded' event when the actual download happens.

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).
- [ ] Except for special cases involving multiple contributors, the PR is started from a fork of the main repository, not a branch.
- [ ] If applicable, the PR references the bug/issue that it fixes in the description.
- [ ] New Unit tests were added for the changes made and Travis.CI is passing.

### Quality of Code and Contribution Guidelines
- [ ] I have read the [contribution guidelines](https://github.com/Azure/WALinuxAgent/blob/master/.github/CONTRIBUTING.md).

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/azure/walinuxagent/1576)
<!-- Reviewable:end -->


codecov[bot]: # [Codecov](https://codecov.io/gh/Azure/WALinuxAgent/pull/1576?src=pr&el=h1) Report
> Merging [#1576](https://codecov.io/gh/Azure/WALinuxAgent/pull/1576?src=pr&el=desc) into [develop](https://codecov.io/gh/Azure/WALinuxAgent/commit/48c79b2fe2624994ea24b687fd4c8b83bcf58afc?src=pr&el=desc) will **not change** coverage.
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/Azure/WALinuxAgent/pull/1576/graphs/tree.svg?width=650&token=ae90E2KYCw&height=150&src=pr)](https://codecov.io/gh/Azure/WALinuxAgent/pull/1576?src=pr&el=tree)

```diff
@@           Coverage Diff            @@
##           develop    #1576   +/-   ##
========================================
  Coverage    64.55%   64.55%           
========================================
  Files           78       78           
  Lines        11101    11101           
  Branches      1569     1569           
========================================
  Hits          7166     7166           
  Misses        3595     3595           
  Partials       340      340
```


| [Impacted Files](https://codecov.io/gh/Azure/WALinuxAgent/pull/1576?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [azurelinuxagent/ga/exthandlers.py](https://codecov.io/gh/Azure/WALinuxAgent/pull/1576/diff?src=pr&el=tree#diff-YXp1cmVsaW51eGFnZW50L2dhL2V4dGhhbmRsZXJzLnB5) | `81.75% <100%> (ø)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/Azure/WALinuxAgent/pull/1576?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/Azure/WALinuxAgent/pull/1576?src=pr&el=footer). Last update [48c79b2...1c22db4](https://codecov.io/gh/Azure/WALinuxAgent/pull/1576?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/azurelinuxagent/ga/exthandlers.py b/azurelinuxagent/ga/exthandlers.py
index 73b271b7..6a1bd697 100644
--- a/azurelinuxagent/ga/exthandlers.py
+++ b/azurelinuxagent/ga/exthandlers.py
@@ -830,10 +830,10 @@ class ExtHandlerInstance(object):
                 raise ExtensionDownloadError("Failed to download extension",
                                              code=ExtensionErrorCodes.PluginManifestDownloadError)
 
-        self.pkg_file = destination
+            duration = elapsed_milliseconds(begin_utc)
+            self.report_event(message="Download succeeded", duration=duration)
 
-        duration = elapsed_milliseconds(begin_utc)
-        self.report_event(message="Download succeeded", duration=duration)
+        self.pkg_file = destination
 
     def initialize(self):
         self.logger.info("Initializing extension {0}".format(self.get_full_name()))


Test patch:
diff --git a/tests/ga/test_exthandlers_download_extension.py b/tests/ga/test_exthandlers_download_extension.py
index cbaa1890..fb71a582 100644
--- a/tests/ga/test_exthandlers_download_extension.py
+++ b/tests/ga/test_exthandlers_download_extension.py
@@ -101,10 +101,12 @@ class DownloadExtensionTestCase(AgentTestCase):
             return True
 
         with patch("azurelinuxagent.common.protocol.wire.WireProtocol.download_ext_handler_pkg", side_effect=download_ext_handler_pkg) as mock_download_ext_handler_pkg:
-            self.ext_handler_instance.download()
+            with patch("azurelinuxagent.ga.exthandlers.ExtHandlerInstance.report_event") as mock_report_event:
+                self.ext_handler_instance.download()
 
         # first download attempt should succeed
         mock_download_ext_handler_pkg.assert_called_once()
+        mock_report_event.assert_called_once()
 
         self._assert_download_and_expand_succeeded()
 
@@ -112,9 +114,11 @@ class DownloadExtensionTestCase(AgentTestCase):
         DownloadExtensionTestCase._create_zip_file(self._get_extension_package_file())
 
         with patch("azurelinuxagent.common.protocol.wire.WireProtocol.download_ext_handler_pkg") as mock_download_ext_handler_pkg:
-            self.ext_handler_instance.download()
+            with patch("azurelinuxagent.ga.exthandlers.ExtHandlerInstance.report_event") as mock_report_event:
+                self.ext_handler_instance.download()
 
         mock_download_ext_handler_pkg.assert_not_called()
+        mock_report_event.assert_not_called()
 
         self.assertTrue(os.path.exists(self._get_extension_command_file()), "The extension package was not expanded to the expected location")