Example #1
0
 def test_duplicate(self):
     content = 'root:x:0:0:root:/root:/bin/bash'
     content2 = 'root:x:1:0:root:/root:/bin/bash'
     arr = {}
     mngids.parse(content, arr)
     with self.assertRaises(KeyError):
         mngids.parse(content2, arr)
Example #2
0
 def test_duplicate(self):
     content = 'root:x:0:0:root:/root:/bin/bash'
     content2 = 'root:x:1:0:root:/root:/bin/bash'
     arr = {}
     mngids.parse(content, arr)
     with self.assertRaises(KeyError):
         mngids.parse(content2, arr)
Example #3
0
 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)
Example #4
0
 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)
Example #5
0
    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')
Example #6
0
 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)
Example #7
0
 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)
Example #8
0
 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)
Example #9
0
 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)
Example #10
0
 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')
Example #11
0
 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')
Example #12
0
 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')
Example #13
0
 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')
Example #14
0
 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')
Example #15
0
 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')
Example #16
0
    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')
Example #17
0
 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')
Example #18
0
 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')
Example #19
0
 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')
Example #20
0
 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')
Example #21
0
 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)
Example #22
0
 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)
Example #23
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')
Example #24
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')
Example #25
0
 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)
Example #26
0
 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')
Example #27
0
 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)
Example #28
0
 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')
Example #29
0
    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')
Example #30
0
    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')
Example #31
0
 def test_comment(self):
     content = '#root:x:0:0:root:/root:/bin/bash'
     arr = {}
     mngids.parse(content, arr)
     self.assertEquals(arr, {})
Example #32
0
 def test_parse_group(self):
     arr = {}
     mngids.parse(GROUP, arr, True)
     self.assertEquals(arr['root'], ('0', ''))
Example #33
0
 def test_parse(self):
     content = 'root:x:0:0:root:/root:/bin/bash'
     arr = {}
     mngids.parse(content, arr)
     self.assertEquals(arr['root'], ('0', '0'))
Example #34
0
 def test_parse(self):
     content = 'root:x:0:0:root:/root:/bin/bash'
     arr = {}
     mngids.parse(content, arr)
     self.assertEquals(arr['root'], ('0', '0'))
Example #35
0
 def test_comment(self):
     content = '#root:x:0:0:root:/root:/bin/bash'
     arr = {}
     mngids.parse(content, arr)
     self.assertEquals(arr, {})
Example #36
0
 def test_parse_group(self):
     arr = {}
     mngids.parse(GROUP, arr, True)
     self.assertEquals(arr['root'], ('0', ''))