def test_parsecmdline_addgroup_non_exist(self): cmd = 'addgroup root'.split(' ') content = 'user:x:1000:' gids = {} mngids.parse(content, gids) with self.assertRaises(KeyError): mngids.parse_cmdline(cmd, {}, gids)
def test_parsecmdline_addgroup_with_missing_arg_value(self): cmd = 'groupadd --gid root'.split(' ') content = 'root:x:1:' gids = {} mngids.parse(content, gids) with self.assertRaises(KeyError): mngids.parse_cmdline(cmd, {}, gids)
def test_parsecmdline_addgroup_without_name(self): cmd = 'addgroup -g 1000 -b /root'.split(' ') content = 'root:x:1:' gids = {} mngids.parse(content, gids) with self.assertRaises(KeyError): mngids.parse_cmdline(cmd, {}, gids)
def test_parsecmdline_addgroup_non_exist(self): cmd = 'addgroup root'.split(' ') content = 'user:x:1000:' gids = {} mngids.parse(content, gids) mngids.parse_cmdline(cmd, {}, gids) self.assertEquals(cmd[1], '--gid') self.assertEquals(cmd[2], '1001')
def test_parsecmdline_addgroup_path(self): cmd = './addgroup root'.split(' ') content = 'root:x:1:' gids = {} mngids.parse(content, gids, True) mngids.parse_cmdline(cmd, {}, gids) self.assertEquals(cmd[1], '--gid') self.assertEquals(cmd[2], '1')
def test_parsecmdline_addgroup_noaction(self): cmd = 'addgroup --gid 2 root'.split(' ') content = 'root:x:1:' gids = {} mngids.parse(content, gids, True) l = len(cmd) mngids.parse_cmdline(cmd, {}, gids) self.assertEquals(len(cmd), l) self.assertEquals(cmd[2], '1')
def test_parsecmdline(self): cmd = 'adduser root'.split(' ') content = 'root:x:0:1:root:/root:/bin/bash' uids = {} mngids.parse(content, uids) mngids.parse_cmdline(cmd, uids, {}) self.assertEquals(cmd[1], '--uid') self.assertEquals(cmd[2], '0') self.assertEquals(cmd[3], '--gid') self.assertEquals(cmd[4], '1')
def test_parsecmdline_noaction1(self): cmd = 'adduser --uid 2 root'.split(' ') l = len(cmd) content = 'root:x:0:1:root:/root:/bin/bash' uids = {} mngids.parse(content, uids) mngids.parse_cmdline(cmd, uids, {}) self.assertEquals(cmd[1], '--gid') self.assertEquals(cmd[2], '1') self.assertEquals(cmd[4], '0') self.assertEquals(len(cmd), l + 2)
def test_parsecmdline(self): cmd = 'adduser root'.split(' ') content = 'root:x:0:0:root:/root:/bin/bash' uids = {} mngids.parse(content, uids) orig = mngids.call_addgroup mngids.call_addgroup = lambda x: x mngids.parse_cmdline(cmd, uids, {}) mngids.call_addgroup = orig self.assertEquals(cmd[1], '--uid') self.assertEquals(cmd[2], '0')
def test_parsecmdline_group(self): cmd = 'adduser --gid nogroup root'.split(' ') passwd = 'root:x:0:1:root:/root:/bin/bash' group = 'nogroup:x:65534:' uids = {} gids = {} mngids.parse(passwd, uids) mngids.parse(group, gids, True) mngids.parse_cmdline(cmd, uids, gids) self.assertEquals(cmd[1], '--uid') self.assertEquals(cmd[2], '0') self.assertEquals(cmd[3], '--gid') self.assertEquals(cmd[4], '65534')
def test_calls(self): passwd = 'jenkins:x:1000:1000::/home/jenkins:/bin/bash' group = 'jenkins:x:1000:' uids = {'john': ('1001', '1001')} gids = {'john': ('1001', '')} mngids.parse(passwd, uids) mngids.parse(group, gids, True) cmd = ['useradd', 'john'] orig = mngids.call_addgroup mngids.call_addgroup = lambda x: x mngids.parse_cmdline(cmd, uids, gids) mngids.call_addgroup = orig self.assertEquals(cmd[1], '--uid') self.assertEquals(cmd[2], '1001') cmd = ['useradd', 'john', '-u', '1001'] orig = mngids.call_addgroup mngids.call_addgroup = lambda x: x mngids.parse_cmdline(cmd, uids, gids) mngids.call_addgroup = orig self.assertEquals(cmd[1], '--gid') self.assertEquals(cmd[2], '1001') self.assertEquals(cmd[4], '-u') self.assertEquals(cmd[5], '1001') cmd = ['useradd', 'john', '-u', '1001', '-g', '1001'] mngids.parse_cmdline(cmd, uids, gids) self.assertEquals(cmd[2], '-u') self.assertEquals(cmd[3], '1001') self.assertEquals(cmd[4], '-g') self.assertEquals(cmd[5], '1001') cmd = ['useradd', 'john', '-u', '1001', '-g', '1002'] with self.assertRaises(KeyError): # KeyError: "mngids.py: 1002 not found (--gid) in [('1000', ''), ('1001', '')]" mngids.parse_cmdline(cmd, uids, gids) uids = {'john': ('1001', '1002')} gids = {'john': ('1002', '')} mngids.parse(passwd, uids) mngids.parse(group, gids, True) cmd = ['useradd', 'john', '-u', '1001'] orig = mngids.call_addgroup mngids.call_addgroup = lambda x: x mngids.parse_cmdline(cmd, uids, gids) mngids.call_addgroup = orig self.assertEquals(cmd[1], '--gid') self.assertEquals(cmd[2], '1002') self.assertEquals(cmd[4], '-u') self.assertEquals(cmd[5], '1001')
def test_parsecmdline_noaction(self): cmd = 'adduser --gid root root'.split(' ') l = len(cmd) passwd = 'root:x:0:1:root:/root:/bin/bash' group = 'root:x:1:' uids = {} gids = {} mngids.parse(passwd, uids) mngids.parse(group, gids, True) mngids.parse_cmdline(cmd, uids, gids) self.assertEquals(cmd[1], '--uid') self.assertEquals(cmd[2], '0') self.assertEquals(cmd[4], '1') self.assertEquals(len(cmd), l + 2)
def test_parsecmdline_wrong_order(self): cmd = ['useradd', 'jenkins', '--shell', '/bin/bash', '--gid', 'cloud-users', '--comment', 'eNovance Jenkins User', '-m'] passwd = 'jenkins:x:1000:1000::/home/jenkins:/bin/bash' group = 'cloud-users:x:1000:' uids = {} gids = {} mngids.parse(passwd, uids) mngids.parse(group, gids, True) mngids.parse_cmdline(cmd, uids, gids) self.assertEquals(cmd[1], '--uid') self.assertEquals(cmd[2], '1000') self.assertEquals(cmd[7], '1000')
def test_parsecmdline_wrong_order(self): cmd = [ 'useradd', 'jenkins', '--shell', '/bin/bash', '--gid', 'cloud-users', '--comment', 'eNovance Jenkins User', '-m' ] passwd = 'jenkins:x:1000:1000::/home/jenkins:/bin/bash' group = 'cloud-users:x:1000:' uids = {} gids = {} mngids.parse(passwd, uids) mngids.parse(group, gids, True) mngids.parse_cmdline(cmd, uids, gids) self.assertEquals(cmd[1], '--uid') self.assertEquals(cmd[2], '1000') self.assertEquals(cmd[7], '1000')
def test_parsecmdline_find_gu_name(self): # Be sure parse_cmdline is able to find username or groupname passwd = 'jenkins:x:1000:1000::/home/jenkins:/bin/bash' group = 'cloud-users:x:1000:' uids = {} gids = {} mngids.parse(passwd, uids) mngids.parse(group, gids, True) cmd = [ 'useradd', 'jenkins', '--shell', '/bin/bash', '--gid', 'cloud-users', '--comment', 'eNovance Jenkins User', '-m' ] mngids.parse_cmdline(cmd, uids, gids) self.assertEquals(cmd[1], '--uid') self.assertEquals(cmd[2], '1000') self.assertEquals(cmd[7], '1000') cmd = [ 'useradd', '--shell', '/bin/bash', '--gid', 'cloud-users', '--comment', 'eNovance Jenkins User', '-m', 'jenkins' ] mngids.parse_cmdline(cmd, uids, gids) self.assertEquals(cmd[1], '--uid') self.assertEquals(cmd[2], '1000') self.assertEquals(cmd[6], '1000') cmd = [ 'useradd', '--shell', '/bin/bash', '--gid', 'cloud-users', 'jenkins', '--comment', 'eNovance Jenkins User', '-m' ] mngids.parse_cmdline(cmd, uids, gids) self.assertEquals(cmd[1], '--uid') self.assertEquals(cmd[2], '1000') self.assertEquals(cmd[6], '1000') cmd = ['groupadd', 'cloud-users'] mngids.parse_cmdline(cmd, uids, gids) self.assertEquals(cmd[1], '--gid') self.assertEquals(cmd[2], '1000') # 1010 is replaced by what we have in gids (usually provided in ids.tables cmd = ['groupadd', '--gid', '1010', 'cloud-users', '-o'] mngids.parse_cmdline(cmd, uids, gids) self.assertEquals(cmd[1], '--gid') self.assertEquals(cmd[2], '1000') cmd = ['groupadd', '--gid', '1010', '-f', 'cloud-users', '-o'] mngids.parse_cmdline(cmd, uids, gids) self.assertEquals(cmd[1], '--gid') self.assertEquals(cmd[2], '1000') cmd = [ 'useradd', '--shell', '/bin/bash', '--gid', 'cloud-users', '-f', '10', 'jenkins', '--comment', 'eNovance Jenkins User', '-m' ] mngids.parse_cmdline(cmd, uids, gids) self.assertEquals(cmd[1], '--uid') self.assertEquals(cmd[2], '1000') self.assertEquals(cmd[6], '1000')
def test_parsecmdline_find_gu_name(self): # Be sure parse_cmdline is able to find username or groupname passwd = 'jenkins:x:1000:1000::/home/jenkins:/bin/bash' group = 'cloud-users:x:1000:' uids = {} gids = {} mngids.parse(passwd, uids) mngids.parse(group, gids, True) cmd = ['useradd', 'jenkins', '--shell', '/bin/bash', '--gid', 'cloud-users', '--comment', 'eNovance Jenkins User', '-m'] mngids.parse_cmdline(cmd, uids, gids) self.assertEquals(cmd[1], '--uid') self.assertEquals(cmd[2], '1000') self.assertEquals(cmd[7], '1000') cmd = ['useradd', '--shell', '/bin/bash', '--gid', 'cloud-users', '--comment', 'eNovance Jenkins User', '-m', 'jenkins'] mngids.parse_cmdline(cmd, uids, gids) self.assertEquals(cmd[1], '--uid') self.assertEquals(cmd[2], '1000') self.assertEquals(cmd[6], '1000') cmd = ['useradd', '--shell', '/bin/bash', '--gid', 'cloud-users', 'jenkins', '--comment', 'eNovance Jenkins User', '-m'] mngids.parse_cmdline(cmd, uids, gids) self.assertEquals(cmd[1], '--uid') self.assertEquals(cmd[2], '1000') self.assertEquals(cmd[6], '1000') cmd = ['groupadd', 'cloud-users'] mngids.parse_cmdline(cmd, uids, gids) self.assertEquals(cmd[1], '--gid') self.assertEquals(cmd[2], '1000') # 1010 is replaced by what we have in gids (usually provided in ids.tables cmd = ['groupadd', '--gid', '1010', 'cloud-users', '-o'] mngids.parse_cmdline(cmd, uids, gids) self.assertEquals(cmd[1], '--gid') self.assertEquals(cmd[2], '1000') cmd = ['groupadd', '--gid', '1010', '-f', 'cloud-users', '-o'] mngids.parse_cmdline(cmd, uids, gids) self.assertEquals(cmd[1], '--gid') self.assertEquals(cmd[2], '1000') cmd = ['useradd', '--shell', '/bin/bash', '--gid', 'cloud-users', '-f', '10', 'jenkins', '--comment', 'eNovance Jenkins User', '-m'] mngids.parse_cmdline(cmd, uids, gids) self.assertEquals(cmd[1], '--uid') self.assertEquals(cmd[2], '1000') self.assertEquals(cmd[6], '1000')