Repository: Azure/WALinuxAgent (python)
Issue: [2.2.0+] Cannot run waagent command inside any folder under /var/lib/waagent
Description of problem:
Cannot run any waagent command (such as: waagent -version, waagent -deprovision...) inside the any folder under /var/lib/waagent (such as: /var/lib/waagent/events)
It seems that if create a folder inside /var/lib/waagent, go into this folder, any waagent command cannot be run.

Version-Release number of selected component (if applicable):
WALinuxAgent-2.2.0+

RHEL Version: 
RHEL-7.3

How reproducible:
100%

Steps to Reproduce:
1. Prepare a RHEL-7.3 VM with WALA-2.2.0+ installed. 
2. Access to the /var/lib/waagent/events folder.
~]# cd /var/lib/waagent/events
4. Run waagent command
events]# waagent -version

Actual Result:
Raise exception. Cannot run this command.

events]# waagent -version
Traceback (most recent call last):
  File "/sbin/waagent", line 31, in <module>
    import azurelinuxagent.agent as agent
  File "/usr/lib/python2.7/site-packages/azurelinuxagent/agent.py", line 29, in <module>
    import azurelinuxagent.common.event as event
  File "/usr/lib/python2.7/site-packages/azurelinuxagent/common/event.py", line 30, in <module>
    from azurelinuxagent.common.protocol.restapi import TelemetryEventParam, \
  File "/usr/lib/python2.7/site-packages/azurelinuxagent/common/protocol/__init__.py", line 18, in <module>
    from azurelinuxagent.common.protocol.util import get_protocol_util, \
  File "/usr/lib/python2.7/site-packages/azurelinuxagent/common/protocol/util.py", line 30, in <module>
    from azurelinuxagent.common.osutil import get_osutil
  File "/usr/lib/python2.7/site-packages/azurelinuxagent/common/osutil/__init__.py", line 18, in <module>
    from azurelinuxagent.common.osutil.factory import get_osutil
  File "/usr/lib/python2.7/site-packages/azurelinuxagent/common/osutil/factory.py", line 20, in <module>
    from azurelinuxagent.common.version import DISTRO_NAME, DISTRO_VERSION, \
  File "/usr/lib/python2.7/site-packages/azurelinuxagent/common/version.py", line 87, in <module>
    CURRENT_AGENT, CURRENT_VERSION = set_current_agent()
  File "/usr/lib/python2.7/site-packages/azurelinuxagent/common/version.py", line 85, in set_current_agent
    version = AGENT_NAME_PATTERN.match(agent).group(1)
AttributeError: 'NoneType' object has no attribute 'group'

Expect Result:
Can run waagent command.
Pull request description: fix the logic for agent version
- fixes #546 

/cc @jinhyunr @brendandixon 

Patch:
diff --git a/azurelinuxagent/common/version.py b/azurelinuxagent/common/version.py
index 1099e252..1e505bdb 100644
--- a/azurelinuxagent/common/version.py
+++ b/azurelinuxagent/common/version.py
@@ -112,12 +112,13 @@ def set_current_agent():
     lib_dir = conf.get_lib_dir()
     if lib_dir[-1] != os.path.sep:
         lib_dir += os.path.sep
-    if path[:len(lib_dir)] != lib_dir:
+    agent = path[len(lib_dir):].split(os.path.sep)[0]
+    match = AGENT_NAME_PATTERN.match(agent)
+    if match:
+        version = match.group(1)
+    else:
         agent = AGENT_LONG_VERSION
         version = AGENT_VERSION
-    else:
-        agent = path[len(lib_dir):].split(os.path.sep)[0]
-        version = AGENT_NAME_PATTERN.match(agent).group(1)
     return agent, FlexibleVersion(version)
 CURRENT_AGENT, CURRENT_VERSION = set_current_agent()
 


Test patch:
diff --git a/tests/common/test_version.py b/tests/common/test_version.py
index 306ea16f..57b2e9a1 100644
--- a/tests/common/test_version.py
+++ b/tests/common/test_version.py
@@ -54,6 +54,22 @@ class TestCurrentAgentName(AgentTestCase):
         self.assertEqual(AGENT_VERSION, str(current_version))
         return
 
+    @patch("os.getcwd", return_value="/")
+    def test_extract_name_root_finds_installed(self, mock_cwd):
+        current_agent, current_version = set_current_agent()
+        self.assertEqual(AGENT_LONG_VERSION, current_agent)
+        self.assertEqual(AGENT_VERSION, str(current_version))
+        return
+
+    @patch("os.getcwd")
+    def test_extract_name_in_path_finds_installed(self, mock_cwd):
+        path = os.path.join(conf.get_lib_dir(), "events")
+        mock_cwd.return_value = path
+        current_agent, current_version = set_current_agent()
+        self.assertEqual(AGENT_LONG_VERSION, current_agent)
+        self.assertEqual(AGENT_VERSION, str(current_version))
+        return
+
     @patch("os.getcwd")
     def test_extract_name_finds_latest_agent(self, mock_cwd):
         path = os.path.join(conf.get_lib_dir(), "{0}-{1}".format(