From 63f9ea120580bb2f37ca97b6514caba3f552061b Mon Sep 17 00:00:00 2001 From: Cleber Rosa Date: Wed, 11 Apr 2018 16:09:09 -0400 Subject: [PATCH] selftests/unit/test_utils_process.py: fix broken test logic on sudo tests The sudo related test are currently broken in the following aspects: 1) it doesn't require TRUE_CMD 2) the monkey patching of find_command() produces and adverse effect, and makes the binary we look for ("sudo") become "true" 3) the expected results reflect the wrong sudo binary Let's fix those aspects, making sure the process.py library looks for sudo only, and that it prepends that (the found sudo binary) to the generated command. Signed-off-by: Cleber Rosa --- selftests/unit/test_utils_process.py | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/selftests/unit/test_utils_process.py b/selftests/unit/test_utils_process.py index a5f431ab..41df6a76 100644 --- a/selftests/unit/test_utils_process.py +++ b/selftests/unit/test_utils_process.py @@ -125,15 +125,14 @@ class TestProcessRun(unittest.TestCase): p = process.SubProcess(cmd='ls -l') self.assertEqual(p.cmd, expected_command) - @unittest.skipUnless(TRUE_CMD, - '"true" binary not available') @mock.patch.object(path, 'find_command', - mock.Mock(return_value=TRUE_CMD)) + mock.Mock(return_value='/bin/sudo')) @mock.patch.object(os, 'getuid', mock.Mock(return_value=1000)) def test_subprocess_sudo(self): - expected_command = '%s -n ls -l' % TRUE_CMD + expected_command = '/bin/sudo -n ls -l' p = process.SubProcess(cmd='ls -l', sudo=True) + path.find_command.assert_called_once_with('sudo') self.assertEqual(p.cmd, expected_command) @mock.patch.object(path, 'find_command', mock_fail_find_cmd) @@ -153,14 +152,13 @@ class TestProcessRun(unittest.TestCase): p = process.SubProcess(cmd='ls -l', sudo=True) self.assertEqual(p.cmd, expected_command) - @unittest.skipUnless(TRUE_CMD, - '"true" binary not available') @mock.patch.object(path, 'find_command', - mock.Mock(return_value=TRUE_CMD)) + mock.Mock(return_value='/bin/sudo')) @mock.patch.object(os, 'getuid', mock.Mock(return_value=1000)) def test_subprocess_sudo_shell(self): - expected_command = '%s -n -s ls -l' % TRUE_CMD + expected_command = '/bin/sudo -n -s ls -l' p = process.SubProcess(cmd='ls -l', sudo=True, shell=True) + path.find_command.assert_called_once_with('sudo') self.assertEqual(p.cmd, expected_command) @mock.patch.object(path, 'find_command', mock_fail_find_cmd) @@ -200,14 +198,13 @@ class TestProcessRun(unittest.TestCase): p = process.run(cmd='ls -l', ignore_status=True) self.assertEqual(p.command, expected_command) - @unittest.skipUnless(TRUE_CMD, - '"true" binary not available') @mock.patch.object(path, 'find_command', - mock.Mock(return_value=TRUE_CMD)) + mock.Mock(return_value='/bin/sudo')) @mock.patch.object(os, 'getuid', mock.Mock(return_value=1000)) def test_run_sudo(self): - expected_command = '%s -n ls -l' % TRUE_CMD + expected_command = '/bin/sudo -n ls -l' p = process.run(cmd='ls -l', sudo=True, ignore_status=True) + path.find_command.assert_called_once_with('sudo') self.assertEqual(p.command, expected_command) @mock.patch.object(path, 'find_command', mock_fail_find_cmd) @@ -227,14 +224,13 @@ class TestProcessRun(unittest.TestCase): p = process.run(cmd='ls -l', sudo=True, ignore_status=True) self.assertEqual(p.command, expected_command) - @unittest.skipUnless(TRUE_CMD, - '"true" binary not available') @mock.patch.object(path, 'find_command', - mock.Mock(return_value=TRUE_CMD)) + mock.Mock(return_value='/bin/sudo')) @mock.patch.object(os, 'getuid', mock.Mock(return_value=1000)) def test_run_sudo_shell(self): - expected_command = '%s -n -s ls -l' % TRUE_CMD + expected_command = '/bin/sudo -n -s ls -l' p = process.run(cmd='ls -l', sudo=True, shell=True, ignore_status=True) + path.find_command.assert_called_once_with('sudo') self.assertEqual(p.command, expected_command) @mock.patch.object(path, 'find_command', mock_fail_find_cmd) -- GitLab