Fix parameters of assertEqual are misplaced

Many assertEqual sentences don't follow assertEqual(expected, actual),
These misplaces have 2 impacts:
1, giving confusing messages when some tests failed.
2, mislead other developers, new test modules may follow these wrong pattern.

This patch fix all of them.

Change-Id: I4adfc98d6eecd5ea117d725775b2476b237212d9
Closes-Bug: #1604213
This commit is contained in:
yan.haifeng 2016-08-01 11:46:55 +08:00 committed by Haifeng.Yan
parent 37c2a041d3
commit a46b731c8a
2 changed files with 32 additions and 32 deletions

@ -89,11 +89,11 @@ id: CommandFilter, /usr/bin/id, nobody
# Should run as 'nobody' # Should run as 'nobody'
code, out, err = self.execute(['id', '-u']) code, out, err = self.execute(['id', '-u'])
self.assertEqual(out, '%s\n' % pwd.getpwnam('nobody').pw_uid) self.assertEqual('%s\n' % pwd.getpwnam('nobody').pw_uid, out)
# Should run as 'root' # Should run as 'root'
code, out, err = self.execute(['sh', '-c', 'id -u']) code, out, err = self.execute(['sh', '-c', 'id -u'])
self.assertEqual(out, '0\n') self.assertEqual('0\n', out)
class RootwrapTest(_FunctionalBase, testtools.TestCase): class RootwrapTest(_FunctionalBase, testtools.TestCase):

@ -41,8 +41,8 @@ class RootwrapLoaderTestCase(testtools.TestCase):
filtermatch = wrapper.match_filter(filterlist, privsep) filtermatch = wrapper.match_filter(filterlist, privsep)
self.assertIsNotNone(filtermatch) self.assertIsNotNone(filtermatch)
self.assertEqual(filtermatch.get_command(privsep), self.assertEqual(["/fake/privsep-helper", "--context", "foo"],
["/fake/privsep-helper", "--context", "foo"]) filtermatch.get_command(privsep))
class RootwrapTestCase(testtools.TestCase): class RootwrapTestCase(testtools.TestCase):
@ -91,8 +91,8 @@ class RootwrapTestCase(testtools.TestCase):
usercmd = ["ls", "/root"] usercmd = ["ls", "/root"]
filtermatch = wrapper.match_filter(self.filters, usercmd) filtermatch = wrapper.match_filter(self.filters, usercmd)
self.assertFalse(filtermatch is None) self.assertFalse(filtermatch is None)
self.assertEqual(filtermatch.get_command(usercmd), self.assertEqual(["/bin/ls", "/root"],
["/bin/ls", "/root"]) filtermatch.get_command(usercmd))
def test_RegExpFilter_reject(self): def test_RegExpFilter_reject(self):
usercmd = ["ls", "root"] usercmd = ["ls", "root"]
@ -114,10 +114,10 @@ class RootwrapTestCase(testtools.TestCase):
f = filters.EnvFilter("env", "root", config_file_arg + '=A', f = filters.EnvFilter("env", "root", config_file_arg + '=A',
'NETWORK_ID=', "/usr/bin/dnsmasq") 'NETWORK_ID=', "/usr/bin/dnsmasq")
self.assertTrue(f.match(usercmd)) self.assertTrue(f.match(usercmd))
self.assertEqual(f.get_command(usercmd), ['/usr/bin/dnsmasq', 'foo']) self.assertEqual(['/usr/bin/dnsmasq', 'foo'], f.get_command(usercmd))
env = f.get_environment(usercmd) env = f.get_environment(usercmd)
self.assertEqual(env.get(config_file_arg), 'A') self.assertEqual('A', env.get(config_file_arg))
self.assertEqual(env.get('NETWORK_ID'), 'foobar') self.assertEqual('foobar', env.get('NETWORK_ID'))
def test_EnvFilter(self): def test_EnvFilter(self):
envset = ['A=/some/thing', 'B=somethingelse'] envset = ['A=/some/thing', 'B=somethingelse']
@ -151,11 +151,11 @@ class RootwrapTestCase(testtools.TestCase):
self.assertFalse(f.match(envcmd[1:])) self.assertFalse(f.match(envcmd[1:]))
# ensure that the env command is stripped when executing # ensure that the env command is stripped when executing
self.assertEqual(f.exec_args(usercmd), realcmd) self.assertEqual(realcmd, f.exec_args(usercmd))
env = f.get_environment(usercmd) env = f.get_environment(usercmd)
# check that environment variables are set # check that environment variables are set
self.assertEqual(env.get('A'), '/some/thing') self.assertEqual('/some/thing', env.get('A'))
self.assertEqual(env.get('B'), 'somethingelse') self.assertEqual('somethingelse', env.get('B'))
self.assertFalse('sleep' in env.keys()) self.assertFalse('sleep' in env.keys())
def test_EnvFilter_without_leading_env(self): def test_EnvFilter_without_leading_env(self):
@ -168,13 +168,13 @@ class RootwrapTestCase(testtools.TestCase):
# accept without leading env # accept without leading env
self.assertTrue(f.match(envset + ["sleep"])) self.assertTrue(f.match(envset + ["sleep"]))
self.assertEqual(f.get_command(envcmd + realcmd), realcmd) self.assertEqual(realcmd, f.get_command(envcmd + realcmd))
self.assertEqual(f.get_command(envset + realcmd), realcmd) self.assertEqual(realcmd, f.get_command(envset + realcmd))
env = f.get_environment(envset + realcmd) env = f.get_environment(envset + realcmd)
# check that environment variables are set # check that environment variables are set
self.assertEqual(env.get('A'), '/some/thing') self.assertEqual('/some/thing', env.get('A'))
self.assertEqual(env.get('B'), 'somethingelse') self.assertEqual('somethingelse', env.get('B'))
self.assertFalse('sleep' in env.keys()) self.assertFalse('sleep' in env.keys())
def test_KillFilter(self): def test_KillFilter(self):
@ -288,7 +288,7 @@ class RootwrapTestCase(testtools.TestCase):
usercmd = ['cat', '/bad/file'] usercmd = ['cat', '/bad/file']
self.assertFalse(f.match(['cat', '/bad/file'])) self.assertFalse(f.match(['cat', '/bad/file']))
usercmd = ['cat', goodfn] usercmd = ['cat', goodfn]
self.assertEqual(f.get_command(usercmd), ['/bin/cat', goodfn]) self.assertEqual(['/bin/cat', goodfn], f.get_command(usercmd))
self.assertTrue(f.match(usercmd)) self.assertTrue(f.match(usercmd))
def test_IpFilter_non_netns(self): def test_IpFilter_non_netns(self):
@ -429,22 +429,22 @@ class RootwrapTestCase(testtools.TestCase):
# Check default values # Check default values
raw.set('DEFAULT', 'filters_path', '/a,/b') raw.set('DEFAULT', 'filters_path', '/a,/b')
config = wrapper.RootwrapConfig(raw) config = wrapper.RootwrapConfig(raw)
self.assertEqual(config.filters_path, ['/a', '/b']) self.assertEqual(['/a', '/b'], config.filters_path)
self.assertEqual(config.exec_dirs, os.environ["PATH"].split(':')) self.assertEqual(os.environ["PATH"].split(':'), config.exec_dirs)
with fixtures.EnvironmentVariable("PATH"): with fixtures.EnvironmentVariable("PATH"):
c = wrapper.RootwrapConfig(raw) c = wrapper.RootwrapConfig(raw)
self.assertEqual(c.exec_dirs, []) self.assertEqual([], c.exec_dirs)
self.assertFalse(config.use_syslog) self.assertFalse(config.use_syslog)
self.assertEqual(config.syslog_log_facility, self.assertEqual(logging.handlers.SysLogHandler.LOG_SYSLOG,
logging.handlers.SysLogHandler.LOG_SYSLOG) config.syslog_log_facility)
self.assertEqual(config.syslog_log_level, logging.ERROR) self.assertEqual(logging.ERROR, config.syslog_log_level)
# Check general values # Check general values
raw.set('DEFAULT', 'exec_dirs', '/a,/x') raw.set('DEFAULT', 'exec_dirs', '/a,/x')
config = wrapper.RootwrapConfig(raw) config = wrapper.RootwrapConfig(raw)
self.assertEqual(config.exec_dirs, ['/a', '/x']) self.assertEqual(['/a', '/x'], config.exec_dirs)
raw.set('DEFAULT', 'use_syslog', 'oui') raw.set('DEFAULT', 'use_syslog', 'oui')
self.assertRaises(ValueError, wrapper.RootwrapConfig, raw) self.assertRaises(ValueError, wrapper.RootwrapConfig, raw)
@ -456,23 +456,23 @@ class RootwrapTestCase(testtools.TestCase):
self.assertRaises(ValueError, wrapper.RootwrapConfig, raw) self.assertRaises(ValueError, wrapper.RootwrapConfig, raw)
raw.set('DEFAULT', 'syslog_log_facility', 'local0') raw.set('DEFAULT', 'syslog_log_facility', 'local0')
config = wrapper.RootwrapConfig(raw) config = wrapper.RootwrapConfig(raw)
self.assertEqual(config.syslog_log_facility, self.assertEqual(logging.handlers.SysLogHandler.LOG_LOCAL0,
logging.handlers.SysLogHandler.LOG_LOCAL0) config.syslog_log_facility)
raw.set('DEFAULT', 'syslog_log_facility', 'LOG_AUTH') raw.set('DEFAULT', 'syslog_log_facility', 'LOG_AUTH')
config = wrapper.RootwrapConfig(raw) config = wrapper.RootwrapConfig(raw)
self.assertEqual(config.syslog_log_facility, self.assertEqual(logging.handlers.SysLogHandler.LOG_AUTH,
logging.handlers.SysLogHandler.LOG_AUTH) config.syslog_log_facility)
raw.set('DEFAULT', 'syslog_log_level', 'bar') raw.set('DEFAULT', 'syslog_log_level', 'bar')
self.assertRaises(ValueError, wrapper.RootwrapConfig, raw) self.assertRaises(ValueError, wrapper.RootwrapConfig, raw)
raw.set('DEFAULT', 'syslog_log_level', 'INFO') raw.set('DEFAULT', 'syslog_log_level', 'INFO')
config = wrapper.RootwrapConfig(raw) config = wrapper.RootwrapConfig(raw)
self.assertEqual(config.syslog_log_level, logging.INFO) self.assertEqual(logging.INFO, config.syslog_log_level)
def test_getlogin(self): def test_getlogin(self):
with mock.patch('os.getlogin') as os_getlogin: with mock.patch('os.getlogin') as os_getlogin:
os_getlogin.return_value = 'foo' os_getlogin.return_value = 'foo'
self.assertEqual(wrapper._getlogin(), 'foo') self.assertEqual('foo', wrapper._getlogin())
def test_getlogin_bad(self): def test_getlogin_bad(self):
with mock.patch('os.getenv') as os_getenv: with mock.patch('os.getenv') as os_getenv:
@ -480,9 +480,9 @@ class RootwrapTestCase(testtools.TestCase):
os_getenv.side_effect = [None, None, 'bar'] os_getenv.side_effect = [None, None, 'bar']
os_getlogin.side_effect = OSError( os_getlogin.side_effect = OSError(
'[Errno 22] Invalid argument') '[Errno 22] Invalid argument')
self.assertEqual(wrapper._getlogin(), 'bar') self.assertEqual('bar', wrapper._getlogin())
os_getlogin.assert_called_once_with() os_getlogin.assert_called_once_with()
self.assertEqual(os_getenv.call_count, 3) self.assertEqual(3, os_getenv.call_count)
class PathFilterTestCase(testtools.TestCase): class PathFilterTestCase(testtools.TestCase):