Repository: Azure/WALinuxAgent
Pull request #2303 (2021-07-14 17:46:27)

Merge hotfix-2.3.1 to develop


Patch:
diff --git a/azurelinuxagent/common/osutil/default.py b/azurelinuxagent/common/osutil/default.py
index 066e1431..820016c1 100644
--- a/azurelinuxagent/common/osutil/default.py
+++ b/azurelinuxagent/common/osutil/default.py
@@ -1163,25 +1163,15 @@ class DefaultOSUtil(object):
     def restart_if(self, ifname, retries=3, wait=5):
         retry_limit = retries + 1
         for attempt in range(1, retry_limit):
-            try:
-                shellutil.run_command(["ifdown", ifname])
-                shellutil.run_command(["ifup", ifname])
+            return_code = shellutil.run("ifdown {0} && ifup {0}".format(ifname), expected_errors=[1] if attempt < retries else [])
+            if return_code == 0:
                 return
-            except shellutil.CommandError as cmd_err:
-                
-                msg = "failed to restart {0}: returncode={1}\n[stdout]{2}\n\n[stderr]{3}\n"\
-                    .format(ifname, cmd_err.returncode, cmd_err.stdout, cmd_err.stderr)
-                
-                if cmd_err.returncode == 1:
-                    logger.info(msg)
-                else:
-                    logger.warn(msg)
-
-                if attempt < retry_limit:
-                    logger.info("retrying in {0} seconds".format(wait))
-                    time.sleep(wait)
-                else:
-                    logger.warn("exceeded restart retries")
+            logger.warn("failed to restart {0}: return code {1}".format(ifname, return_code))
+            if attempt < retry_limit:
+                logger.info("retrying in {0} seconds".format(wait))
+                time.sleep(wait)
+            else:
+                logger.warn("exceeded restart retries")
 
     def publish_hostname(self, hostname):
         self.set_dhcp_hostname(hostname)
diff --git a/azurelinuxagent/common/osutil/ubuntu.py b/azurelinuxagent/common/osutil/ubuntu.py
index 249e1120..5a21511c 100644
--- a/azurelinuxagent/common/osutil/ubuntu.py
+++ b/azurelinuxagent/common/osutil/ubuntu.py
@@ -142,19 +142,17 @@ class UbuntuOSUtil(Ubuntu16OSUtil):
         Restart an interface by bouncing the link. systemd-networkd observes
         this event, and forces a renew of DHCP.
         """
-        retry_limit=retries+1
+        retry_limit = retries+1
         for attempt in range(1, retry_limit):
-            try:
-                shellutil.run_command(["ip", "link", "set", ifname, "down"])
-                shellutil.run_command(["ip", "link", "set", ifname, "up"])
-
-            except shellutil.CommandError as cmd_err:
-                logger.warn("failed to restart {0}: return code {1}".format(ifname, cmd_err.returncode))
-                if attempt < retry_limit:
-                    logger.info("retrying in {0} seconds".format(wait))
-                    time.sleep(wait)
-                else:
-                    logger.warn("exceeded restart retries")
+            return_code = shellutil.run("ip link set {0} down && ip link set {0} up".format(ifname))
+            if return_code == 0:
+                return
+            logger.warn("failed to restart {0}: return code {1}".format(ifname, return_code))
+            if attempt < retry_limit:
+                logger.info("retrying in {0} seconds".format(wait))
+                time.sleep(wait)
+            else:
+                logger.warn("exceeded restart retries")
 
 
 class UbuntuSnappyOSUtil(Ubuntu14OSUtil):
diff --git a/azurelinuxagent/common/version.py b/azurelinuxagent/common/version.py
index 39fdf1ba..8371b546 100644
--- a/azurelinuxagent/common/version.py
+++ b/azurelinuxagent/common/version.py
@@ -200,7 +200,7 @@ AGENT_LONG_NAME = "Azure Linux Agent"
 # IMPORTANT: Please be sure that the version is always 9.9.9.9 on the develop branch. Automation requires this, otherwise
 #            DCR may test the wrong agent version.
 #
-#            When doing a release, be sure to use the actual agent version. Current agent version: 2.3.0.2
+#            When doing a release, be sure to use the actual agent version. Current agent version: 2.4.0.0
 #
 AGENT_VERSION = '9.9.9.9'
 AGENT_LONG_VERSION = "{0}-{1}".format(AGENT_NAME, AGENT_VERSION)
diff --git a/azurelinuxagent/ga/exthandlers.py b/azurelinuxagent/ga/exthandlers.py
index ebd56767..96f42d1f 100644
--- a/azurelinuxagent/ga/exthandlers.py
+++ b/azurelinuxagent/ga/exthandlers.py
@@ -88,7 +88,6 @@ _STATUS_FILE_RETRY_DELAY = 2  # seconds
 # This is the default sequence number we use when there are no settings available for Handlers
 _DEFAULT_SEQ_NO = "0"
 
-
 class ValidHandlerStatus(object):
     transitioning = "transitioning"
     warning = "warning"


Test patch:
diff --git a/tests/common/osutil/test_default.py b/tests/common/osutil/test_default.py
index 65d7ae0f..d6eae68f 100644
--- a/tests/common/osutil/test_default.py
+++ b/tests/common/osutil/test_default.py
@@ -49,20 +49,15 @@ class TestOSUtil(AgentTestCase):
         # setup
         retries = 3
         ifname = 'dummy'
-        with patch.object(shellutil, "run_command") as run_patch:
-            run_patch.side_effect = shellutil.CommandError("ifupdown dummy", 1, "", "")
+        with patch.object(shellutil, "run") as run_patch:
+            run_patch.return_value = 1
 
             # execute
             osutil.DefaultOSUtil.restart_if(osutil.DefaultOSUtil(), ifname=ifname, retries=retries, wait=0)
 
             # assert
             self.assertEqual(run_patch.call_count, retries)
-            cmd_queue = list(args[0] for (args, _) in run_patch.call_args_list)            
-            while cmd_queue:
-                self.assertEqual(cmd_queue.pop(0), ["ifdown", ifname])
-                # We don't expect the following command to be called because 'dummy' does
-                # not exist.
-                self.assertNotEqual(cmd_queue[0] if cmd_queue else None, ["ifup", ifname])
+            self.assertEqual(run_patch.call_args_list[0][0][0], 'ifdown {0} && ifup {0}'.format(ifname))
                 
     def test_get_dvd_device_success(self):
         with patch.object(os, 'listdir', return_value=['cpu', 'cdrom0']):