Repository: Azure/WALinuxAgent (python)

Issue:
Add User-Agent String to Storage Request
Please include the agent name and version in all package requests.

Pull request description:
add User-Agent to HTTP requests
Closes #1027

Patch:
diff --git a/azurelinuxagent/common/event.py b/azurelinuxagent/common/event.py
index 7fc084d9..84a439f5 100644
--- a/azurelinuxagent/common/event.py
+++ b/azurelinuxagent/common/event.py
@@ -254,11 +254,7 @@ __event_logger__ = EventLogger()
 
 
 def elapsed_milliseconds(utc_start):
-    now = datetime.utcnow()
-    if now < utc_start:
-        return 0
-
-    d = now - utc_start
+    d = datetime.utcnow() - utc_start
     return int(((d.days * 24 * 60 * 60 + d.seconds) * 1000) + \
                     (d.microseconds / 1000.0))
 
diff --git a/azurelinuxagent/common/utils/restutil.py b/azurelinuxagent/common/utils/restutil.py
index 807be29c..c521f63a 100644
--- a/azurelinuxagent/common/utils/restutil.py
+++ b/azurelinuxagent/common/utils/restutil.py
@@ -28,8 +28,7 @@ import azurelinuxagent.common.utils.textutil as textutil
 
 from azurelinuxagent.common.exception import HttpError, ResourceGoneError
 from azurelinuxagent.common.future import httpclient, urlparse, ustr
-from azurelinuxagent.common.version import PY_VERSION_MAJOR
-
+from azurelinuxagent.common.version import PY_VERSION_MAJOR, AGENT_NAME, GOAL_STATE_AGENT_VERSION
 
 SECURE_WARNING_EMITTED = False
 
@@ -78,6 +77,7 @@ RETRY_EXCEPTIONS = [
 
 HTTP_PROXY_ENV = "http_proxy"
 HTTPS_PROXY_ENV = "https_proxy"
+HTTP_USER_AGENT = "{0}/{1}".format(AGENT_NAME, GOAL_STATE_AGENT_VERSION)
 
 DEFAULT_PROTOCOL_ENDPOINT='168.63.129.16'
 HOST_PLUGIN_PORT = 32526
@@ -175,11 +175,13 @@ def _http_request(method, host, rel_uri, port=None, data=None, secure=False,
     if port is None:
         port = 443 if secure else 80
 
+    if 'User-Agent' not in headers:
+        headers['User-Agent'] = HTTP_USER_AGENT
+
     if use_proxy:
         conn_host, conn_port = proxy_host, proxy_port
         scheme = "https" if secure else "http"
         url = "{0}://{1}:{2}{3}".format(scheme, host, port, rel_uri)
-
     else:
         conn_host, conn_port = host, port
         url = rel_uri
@@ -190,7 +192,6 @@ def _http_request(method, host, rel_uri, port=None, data=None, secure=False,
                                         timeout=10)
         if use_proxy:
             conn.set_tunnel(host, port)
-
     else:
         conn = httpclient.HTTPConnection(conn_host,
                                         conn_port,


Test patch:
diff --git a/tests/common/test_event.py b/tests/common/test_event.py
index 4d9afeff..01bcd7b9 100644
--- a/tests/common/test_event.py
+++ b/tests/common/test_event.py
@@ -17,10 +17,14 @@
 
 from __future__ import print_function
 
-from datetime import datetime, timedelta
+from datetime import datetime
+
+import azurelinuxagent.common.event as event
+import azurelinuxagent.common.logger as logger
 
 from azurelinuxagent.common.event import add_event, \
-    WALAEventOperation, elapsed_milliseconds
+                                    mark_event_status, should_emit_event, \
+                                    WALAEventOperation
 from azurelinuxagent.common.future import ustr
 from azurelinuxagent.common.version import CURRENT_VERSION
 
@@ -213,7 +217,3 @@ class TestEvent(AgentTestCase):
         with open(last_event) as last_fh:
             last_event_text = last_fh.read()
             self.assertTrue('last event' in last_event_text)
-
-    def test_elapsed_milliseconds(self):
-        utc_start = datetime.utcnow() + timedelta(days=1)
-        self.assertEqual(0, elapsed_milliseconds(utc_start))
diff --git a/tests/utils/test_rest_util.py b/tests/utils/test_rest_util.py
index bde0c3d0..4f993227 100644
--- a/tests/utils/test_rest_util.py
+++ b/tests/utils/test_rest_util.py
@@ -15,13 +15,11 @@
 # Requires Python 2.4+ and Openssl 1.0+
 #
 
-import os
-import unittest
-
 from azurelinuxagent.common.exception import HttpError, \
-                                            ProtocolError, \
                                             ResourceGoneError
+
 import azurelinuxagent.common.utils.restutil as restutil
+from azurelinuxagent.common.utils.restutil import HTTP_USER_AGENT
 
 from azurelinuxagent.common.future import httpclient, ustr
 
@@ -197,7 +195,7 @@ class TestHttpOperations(AgentTestCase):
         ])
         HTTPSConnection.assert_not_called()
         mock_conn.request.assert_has_calls([
-            call(method="GET", url="/bar", body=None, headers={})
+            call(method="GET", url="/bar", body=None, headers={'User-Agent': HTTP_USER_AGENT})
         ])
         mock_conn.getresponse.assert_called_once()
         self.assertNotEquals(None, resp)
@@ -220,7 +218,7 @@ class TestHttpOperations(AgentTestCase):
             call("foo", 443, timeout=10)
         ])
         mock_conn.request.assert_has_calls([
-            call(method="GET", url="/bar", body=None, headers={})
+            call(method="GET", url="/bar", body=None, headers={'User-Agent': HTTP_USER_AGENT})
         ])
         mock_conn.getresponse.assert_called_once()
         self.assertNotEquals(None, resp)
@@ -244,7 +242,7 @@ class TestHttpOperations(AgentTestCase):
         ])
         HTTPSConnection.assert_not_called()
         mock_conn.request.assert_has_calls([
-            call(method="GET", url="http://foo:80/bar", body=None, headers={})
+            call(method="GET", url="http://foo:80/bar", body=None, headers={'User-Agent': HTTP_USER_AGENT})
         ])
         mock_conn.getresponse.assert_called_once()
         self.assertNotEquals(None, resp)
@@ -269,7 +267,7 @@ class TestHttpOperations(AgentTestCase):
             call("foo.bar", 23333, timeout=10)
         ])
         mock_conn.request.assert_has_calls([
-            call(method="GET", url="https://foo:443/bar", body=None, headers={})
+            call(method="GET", url="https://foo:443/bar", body=None, headers={'User-Agent': HTTP_USER_AGENT})
         ])
         mock_conn.getresponse.assert_called_once()
         self.assertNotEquals(None, resp)