Repository: Azure/WALinuxAgent (python)

Issue:
Record OS boot time for Failed Provisions Too
The OS boot time is recorded in the Provision event **only** in the case of a successful provision.  The OS boot time should be recorded in the case of a failed provision too.

Pull request:
track uptime even when provision fails
Closes #1245

Patch:
diff --git a/azurelinuxagent/pa/provision/cloudinit.py b/azurelinuxagent/pa/provision/cloudinit.py
index 9609d7da..3f3cdb04 100644
--- a/azurelinuxagent/pa/provision/cloudinit.py
+++ b/azurelinuxagent/pa/provision/cloudinit.py
@@ -69,9 +69,10 @@ class CloudInitProvisionHandler(ProvisionHandler):
                 duration=elapsed_milliseconds(utc_start))
 
         except ProvisionError as e:
-            logger.error("Provisioning failed: {0}", ustr(e))
+            msg = "Provisioning with cloud-init failed: {0} ({1}s)".format(ustr(e), self._get_uptime_seconds())
+            logger.error(msg)
             self.report_not_ready("ProvisioningFailed", ustr(e))
-            self.report_event(ustr(e))
+            self.report_event(msg)
             return
 
     def wait_for_ovfenv(self, max_retry=1800, sleep_time=1):
diff --git a/azurelinuxagent/pa/provision/default.py b/azurelinuxagent/pa/provision/default.py
index a6e50824..0eb0823c 100644
--- a/azurelinuxagent/pa/provision/default.py
+++ b/azurelinuxagent/pa/provision/default.py
@@ -98,9 +98,10 @@ class ProvisionHandler(object):
             logger.info("Provisioning complete")
 
         except (ProtocolError, ProvisionError) as e:
+            msg = "Provisioning failed: {0} ({1}s)".format(ustr(e), self._get_uptime_seconds())
+            logger.error(msg)
             self.report_not_ready("ProvisioningFailed", ustr(e))
-            self.report_event(ustr(e), is_success=False)
-            logger.error("Provisioning failed: {0}", ustr(e))
+            self.report_event(msg, is_success=False)
             return
 
     @staticmethod


Test patch:
diff --git a/tests/pa/test_provision.py b/tests/pa/test_provision.py
index 0335bc9c..27f75266 100644
--- a/tests/pa/test_provision.py
+++ b/tests/pa/test_provision.py
@@ -268,8 +268,8 @@ class TestProvision(AgentTestCase):
         fileutil.write_file(ovfenv_file, ovfenv_data)
 
         ph.run()
-        ph.report_event.assert_called_once_with(
-            '[ProvisionError] --unit-test--', is_success=False)
+        positional_args, kw_args = ph.report_event.call_args_list[0]
+        self.assertTrue(re.match(r'Provisioning failed: \[ProvisionError\] --unit-test-- \(\d+\.\d+s\)', positional_args[0]) is not None)
 
     @patch('azurelinuxagent.pa.provision.default.ProvisionHandler.write_agent_disabled')
     @distros()