Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not report restart_if warnings as errors #1498

Merged
merged 1 commit into from
Mar 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion azurelinuxagent/common/osutil/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -1116,7 +1116,7 @@ def set_dhcp_hostname(self, hostname):
def restart_if(self, ifname, retries=3, wait=5):
retry_limit=retries+1
for attempt in range(1, retry_limit):
return_code=shellutil.run("ifdown {0} && ifup {0}".format(ifname))
return_code=shellutil.run("ifdown {0} && ifup {0}".format(ifname), expected_errors=[1] if attempt < retries else [])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: add spacing around return_code=shellutil.run(...)

if return_code == 0:
return
logger.warn("failed to restart {0}: return code {1}".format(ifname, return_code))
Expand Down
4 changes: 2 additions & 2 deletions azurelinuxagent/common/utils/shellutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,13 @@ def has_command(cmd):
"""
return not run(cmd, False)

def run(cmd, chk_err=True):
def run(cmd, chk_err=True, expected_errors=[]):
"""
Calls run_get_output on 'cmd', returning only the return code.
If chk_err=True then errors will be reported in the log.
If chk_err=False then errors will be suppressed from the log.
"""
retcode, out = run_get_output(cmd, chk_err)
retcode, out = run_get_output(cmd, chk_err=chk_err, expected_errors=expected_errors)
return retcode


Expand Down
43 changes: 38 additions & 5 deletions tests/utils/test_shell_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,32 @@
import azurelinuxagent.common.utils.shellutil as shellutil
import test

class TestrunCmd(AgentTestCase):

class ShellQuoteTestCase(AgentTestCase):
def test_shellquote(self):
self.assertEqual("\'foo\'", shellutil.quote("foo"))
self.assertEqual("\'foo bar\'", shellutil.quote("foo bar"))
self.assertEqual("'foo'\\''bar'", shellutil.quote("foo\'bar"))


class RunTestCase(AgentTestCase):
def test_it_should_return_the_exit_code_of_the_command(self):
exit_code = shellutil.run("exit 123")
self.assertEquals(123, exit_code)

def test_it_should_be_a_pass_thru_to_run_get_output(self):
with patch.object(shellutil, "run_get_output", return_value=(0, "")) as mock_run_get_output:
shellutil.run("echo hello word!", chk_err=False, expected_errors=[1, 2, 3])

self.assertEquals(mock_run_get_output.call_count, 1)

args, kwargs = mock_run_get_output.call_args
self.assertEquals(args[0], "echo hello word!")
self.assertEquals(kwargs["chk_err"], False)
self.assertEquals(kwargs["expected_errors"], [1, 2, 3])


class RunGetOutputTestCase(AgentTestCase):
def test_run_get_output(self):
output = shellutil.run_get_output(u"ls /")
self.assertNotEquals(None, output)
Expand All @@ -35,10 +60,18 @@ def test_run_get_output(self):
err = shellutil.run_get_output(u"ls 我")
self.assertNotEquals(0, err[0])

def test_shellquote(self):
self.assertEqual("\'foo\'", shellutil.quote("foo"))
self.assertEqual("\'foo bar\'", shellutil.quote("foo bar"))
self.assertEqual("'foo'\\''bar'", shellutil.quote("foo\'bar"))
def test_it_should_log_the_command(self):
command = "echo hello world!"

with patch("azurelinuxagent.common.utils.shellutil.logger", autospec=True) as mock_logger:
shellutil.run_get_output(command)

self.assertEquals(mock_logger.verbose.call_count, 1)

args, kwargs = mock_logger.verbose.call_args
command_in_message = args[1]
self.assertEqual(command_in_message, command)


def test_it_should_log_command_failures_as_errors(self):
return_code = 99
Expand Down