Home | History | Annotate | Download | only in cli
      1 # pylint: disable-msg=C0111
      2 #!/usr/bin/python
      3 #
      4 # Copyright 2008 Google Inc. All Rights Reserved.
      5 
      6 """Test for acl."""
      7 
      8 import unittest, sys
      9 
     10 import common
     11 from autotest_lib.cli import acl, cli_mock
     12 
     13 
     14 class acl_list_unittest(cli_mock.cli_unittest):
     15     def test_parse_list_acl(self):
     16         acl_list = acl.acl_list()
     17         afile = cli_mock.create_file('acl0\nacl3\nacl4\n')
     18         sys.argv = ['atest', 'acl0', 'acl1,acl2',
     19                     '--alist', afile.name, 'acl5', 'acl6,acl7']
     20         acl_list.parse()
     21         self.assertEqualNoOrder(['acl%s' % x for x in range(8)],
     22                                 acl_list.acls)
     23         afile.clean()
     24 
     25 
     26     def test_parse_list_user(self):
     27         acl_list = acl.acl_list()
     28         sys.argv = ['atest', '--user', 'user0']
     29         acl_list.parse()
     30         self.assertEqual('user0', acl_list.users)
     31 
     32 
     33     def test_parse_list_host(self):
     34         acl_list = acl.acl_list()
     35         sys.argv = ['atest', '--mach', 'host0']
     36         acl_list.parse()
     37         self.assertEqual('host0', acl_list.hosts)
     38 
     39 
     40     def _test_parse_bad_options(self):
     41         acl_list = acl.acl_list()
     42         self.god.mock_io()
     43         sys.exit.expect_call(1).and_raises(cli_mock.ExitException)
     44         self.assertRaises(cli_mock.ExitException, acl_list.parse)
     45         (out, err) = self.god.unmock_io()
     46         self.god.check_playback()
     47         self.assert_(err.find('usage'))
     48 
     49 
     50     def test_parse_list_acl_user(self):
     51         sys.argv = ['atest', 'acl0', '-u', 'user']
     52         self._test_parse_bad_options()
     53 
     54 
     55     def test_parse_list_acl_2users(self):
     56         sys.argv = ['atest', '-u', 'user0,user1']
     57         self._test_parse_bad_options()
     58 
     59 
     60     def test_parse_list_acl_host(self):
     61         sys.argv = ['atest', 'acl0', '--mach', 'mach']
     62         self._test_parse_bad_options()
     63 
     64 
     65     def test_parse_list_acl_2hosts(self):
     66         sys.argv = ['atest', '--mach', 'mach0,mach1']
     67         self._test_parse_bad_options()
     68 
     69 
     70     def test_parse_list_user_host(self):
     71         sys.argv = ['atest', '-u', 'user', '--mach', 'mach']
     72         self._test_parse_bad_options()
     73 
     74 
     75     def test_parse_list_all(self):
     76         sys.argv = ['atest', '-u', 'user', '--mach', 'mach', 'acl0']
     77         self._test_parse_bad_options()
     78 
     79 
     80     def test_execute_list_all_acls(self):
     81         self.run_cmd(argv=['atest', 'acl', 'list', '-v'],
     82                      rpcs=[('get_acl_groups', {}, True,
     83                            [{'id': 1L,
     84                              'name': 'Everyone',
     85                              'description': '',
     86                              'users': ['debug_user'],
     87                              'hosts': []}])],
     88                      out_words_ok=['debug_user'])
     89 
     90 
     91     def test_execute_list_acls_for_acl(self):
     92         self.run_cmd(argv=['atest', 'acl', 'list', 'acl0'],
     93                      rpcs=[('get_acl_groups', {'name__in': ['acl0']}, True,
     94                            [{'id': 1L,
     95                              'name': 'Everyone',
     96                              'description': '',
     97                              'users': ['user0'],
     98                              'hosts': []}])],
     99                      out_words_ok=['Everyone'])
    100 
    101 
    102     def test_execute_list_acls_for_user(self):
    103         self.run_cmd(argv=['atest', 'acl', 'list', '-v', '--user', 'user0'],
    104                      rpcs=[('get_acl_groups', {'users__login': 'user0'}, True,
    105                            [{'id': 1L,
    106                              'name': 'Everyone',
    107                              'description': '',
    108                              'users': ['user0'],
    109                              'hosts': []}])],
    110                      out_words_ok=['user0'])
    111 
    112 
    113     def test_execute_list_acls_for_host(self):
    114         self.run_cmd(argv=['atest', 'acl', 'list', '-m', 'host0'],
    115                      rpcs=[('get_acl_groups', {'hosts__hostname': 'host0'},
    116                             True,
    117                            [{'id': 1L,
    118                              'name': 'Everyone',
    119                              'description': '',
    120                              'users': ['user0'],
    121                              'hosts': ['host0']}])],
    122                      out_words_ok=['Everyone'],
    123                      out_words_no=['host0'])
    124 
    125 
    126     def test_execute_list_acls_for_host_verb(self):
    127         self.run_cmd(argv=['atest', 'acl', 'list', '-m', 'host0', '-v'],
    128                      rpcs=[('get_acl_groups', {'hosts__hostname': 'host0'},
    129                             True,
    130                            [{'id': 1L,
    131                              'name': 'Everyone',
    132                              'description': '',
    133                              'users': ['user0'],
    134                              'hosts': ['host0']}])],
    135                      out_words_ok=['Everyone', 'host0'])
    136 
    137 
    138 
    139 class acl_create_unittest(cli_mock.cli_unittest):
    140     def test_acl_create_parse_ok(self):
    141         acls = acl.acl_create()
    142         sys.argv = ['atest', 'acl0',
    143                     '--desc', 'my_favorite_acl']
    144         acls.parse()
    145         self.assertEqual('my_favorite_acl', acls.data['description'])
    146 
    147 
    148     def test_acl_create_parse_no_desc(self):
    149         self.god.mock_io()
    150         acls = acl.acl_create()
    151         sys.argv = ['atest', 'acl0']
    152         sys.exit.expect_call(1).and_raises(cli_mock.ExitException)
    153         self.assertRaises(cli_mock.ExitException, acls.parse)
    154         self.god.check_playback()
    155         self.god.unmock_io()
    156 
    157 
    158     def test_acl_create_parse_2_acls(self):
    159         self.god.mock_io()
    160         acls = acl.acl_create()
    161         sys.argv = ['atest', 'acl0', 'acl1',
    162                     '-desc', 'my_favorite_acl']
    163         sys.exit.expect_call(1).and_raises(cli_mock.ExitException)
    164         self.assertRaises(cli_mock.ExitException, acls.parse)
    165         self.god.check_playback()
    166         self.god.unmock_io()
    167 
    168 
    169     def test_acl_create_parse_no_option(self):
    170         self.god.mock_io()
    171         acls = acl.acl_create()
    172         sys.argv = ['atest']
    173         sys.exit.expect_call(1).and_raises(cli_mock.ExitException)
    174         self.assertRaises(cli_mock.ExitException, acls.parse)
    175         self.god.check_playback()
    176         self.god.unmock_io()
    177 
    178 
    179     def test_acl_create_acl_ok(self):
    180         self.run_cmd(argv=['atest', 'acl', 'create', 'acl0',
    181                            '--desc', 'my_favorite_acl'],
    182                      rpcs=[('add_acl_group',
    183                            {'description': 'my_favorite_acl',
    184                             'name': 'acl0'},
    185                            True,
    186                             3L)],
    187                      out_words_ok=['acl0'])
    188 
    189 
    190     def test_acl_create_duplicate_acl(self):
    191         self.run_cmd(argv=['atest', 'acl', 'create', 'acl0',
    192                            '--desc', 'my_favorite_acl'],
    193                      rpcs=[('add_acl_group',
    194                            {'description': 'my_favorite_acl',
    195                             'name': 'acl0'},
    196                            False,
    197                            'ValidationError:'
    198                            '''{'name': 'This value must be '''
    199                            '''unique (acl0)'}''')],
    200                      err_words_ok=['acl0', 'ValidationError',
    201                                    'unique'])
    202 
    203 
    204 class acl_delete_unittest(cli_mock.cli_unittest):
    205     def test_acl_delete_acl_ok(self):
    206         self.run_cmd(argv=['atest', 'acl', 'delete', 'acl0',
    207                            '--no-confirmation'],
    208                      rpcs=[('delete_acl_group', {'id': 'acl0'}, True, None)],
    209                      out_words_ok=['acl0'])
    210 
    211 
    212     def test_acl_delete_acl_does_not_exist(self):
    213         self.run_cmd(argv=['atest', 'acl', 'delete', 'acl0',
    214                            '--no-confirmation'],
    215                      rpcs=[('delete_acl_group', {'id': 'acl0'},
    216                             False,
    217                             'DoesNotExist: acl_group matching '
    218                             'query does not exist.')],
    219                      err_words_ok=['acl0', 'DoesNotExist'])
    220 
    221 
    222     def test_acl_delete_multiple_acl_ok(self):
    223         alist = cli_mock.create_file('acl2\nacl1')
    224         self.run_cmd(argv=['atest', 'acl', 'delete',
    225                            'acl0', 'acl1', '--no-confirmation',
    226                            '--alist', alist.name],
    227                      rpcs=[('delete_acl_group',
    228                            {'id': 'acl0'},
    229                            True,
    230                            None),
    231                           ('delete_acl_group',
    232                            {'id': 'acl1'},
    233                            True,
    234                            None),
    235                           ('delete_acl_group',
    236                            {'id': 'acl2'},
    237                            True,
    238                            None)],
    239                      out_words_ok=['acl0', 'acl1', 'acl2', 'Deleted'])
    240         alist.clean()
    241 
    242 
    243     def test_acl_delete_multiple_acl_bad(self):
    244         alist = cli_mock.create_file('acl2\nacl1')
    245         self.run_cmd(argv=['atest', 'acl', 'delete',
    246                            'acl0', 'acl1', '--no-confirmation',
    247                            '--alist', alist.name],
    248                      rpcs=[('delete_acl_group',
    249                            {'id': 'acl0'},
    250                            True,
    251                            None),
    252                           ('delete_acl_group',
    253                            {'id': 'acl1'},
    254                            False,
    255                            'DoesNotExist: acl_group '
    256                            'matching query does not exist.'),
    257                           ('delete_acl_group',
    258                            {'id': 'acl2'},
    259                            True,
    260                            None)],
    261                      out_words_ok=['acl0', 'acl2', 'Deleted'],
    262                      err_words_ok=['acl1', 'delete_acl_group',
    263                                    'DoesNotExist', 'acl_group',
    264                                    'matching'])
    265         alist.clean()
    266 
    267 
    268 class acl_add_unittest(cli_mock.cli_unittest):
    269     def test_acl_add_parse_no_option(self):
    270         self.god.mock_io()
    271         acls = acl.acl_add()
    272         sys.argv = ['atest']
    273         sys.exit.expect_call(1).and_raises(cli_mock.ExitException)
    274         self.assertRaises(cli_mock.ExitException, acls.parse)
    275         self.god.unmock_io()
    276         self.god.check_playback()
    277 
    278 
    279     def test_acl_add_users_hosts(self):
    280         self.run_cmd(argv=['atest', 'acl', 'add', 'acl0',
    281                            '-u', 'user0,user1', '-m', 'host0'],
    282                      rpcs=[('acl_group_add_users',
    283                            {'id': 'acl0',
    284                             'users': ['user0', 'user1']},
    285                            True,
    286                            None),
    287                           ('acl_group_add_hosts',
    288                            {'id': 'acl0',
    289                             'hosts': ['host0']},
    290                            True,
    291                            None)],
    292                      out_words_ok=['acl0', 'user0',
    293                                    'user1', 'host0'])
    294 
    295 
    296     def test_acl_add_bad_users(self):
    297         self.run_cmd(argv=['atest', 'acl', 'add', 'acl0',
    298                            '-u', 'user0,user1'],
    299                      rpcs=[('acl_group_add_users',
    300                            {'id': 'acl0',
    301                             'users': ['user0', 'user1']},
    302                             False,
    303                             'DoesNotExist: The following Users do not exist: '
    304                             'user0, user1')],
    305                      err_words_ok=['user0', 'user1'])
    306 
    307 
    308     def test_acl_add_bad_users_hosts(self):
    309         self.run_cmd(argv=['atest', 'acl', 'add', 'acl0',
    310                            '-u', 'user0,user1', '-m', 'host0,host1'],
    311                      rpcs=[('acl_group_add_users',
    312                            {'id': 'acl0',
    313                             'users': ['user0', 'user1']},
    314                             False,
    315                             'DoesNotExist: The following Users do not exist: '
    316                             'user0'),
    317                            ('acl_group_add_users',
    318                            {'id': 'acl0',
    319                             'users': ['user1']},
    320                             True,
    321                             None),
    322                            ('acl_group_add_hosts',
    323                             {'id': 'acl0',
    324                              'hosts': ['host1', 'host0']},
    325                             False,
    326                             'DoesNotExist: The following Hosts do not exist: '
    327                             'host1'),
    328                            ('acl_group_add_hosts',
    329                             {'id': 'acl0',
    330                              'hosts': ['host0']},
    331                             True,
    332                             None)],
    333                      out_words_ok=['acl0', 'user1', 'host0'],
    334                      out_words_no=['user0', 'host1'],
    335                      err_words_ok=['user0', 'host1'],
    336                      err_words_no=['user1', 'host0'])
    337 
    338 
    339 class acl_remove_unittest(cli_mock.cli_unittest):
    340     def test_acl_remove_remove_users(self):
    341         self.run_cmd(argv=['atest', 'acl', 'remove',
    342                            'acl0', '-u', 'user0,user1'],
    343                      rpcs=[('acl_group_remove_users',
    344                            {'id': 'acl0',
    345                             'users': ['user0', 'user1']},
    346                            True,
    347                            None)],
    348                      out_words_ok=['acl0', 'user0', 'user1'],
    349                      out_words_no=['host'])
    350 
    351 
    352     def test_acl_remove_remove_hosts(self):
    353         self.run_cmd(argv=['atest', 'acl', 'remove',
    354                            'acl0', '--mach', 'host0,host1'],
    355                      rpcs=[('acl_group_remove_hosts',
    356                            {'id': 'acl0',
    357                             'hosts': ['host1', 'host0']},
    358                            True,
    359                            None)],
    360                      out_words_ok=['acl0', 'host0', 'host1'],
    361                      out_words_no=['user'])
    362 
    363 
    364     def test_acl_remove_remove_both(self):
    365         self.run_cmd(argv=['atest', 'acl', 'remove',
    366                            'acl0', '--user', 'user0,user1',
    367                            '-m', 'host0,host1'],
    368                      rpcs=[('acl_group_remove_users',
    369                            {'id': 'acl0',
    370                             'users': ['user0', 'user1']},
    371                            True,
    372                            None),
    373                           ('acl_group_remove_hosts',
    374                            {'id': 'acl0',
    375                             'hosts': ['host1', 'host0']},
    376                            True,
    377                            None)],
    378                      out_words_ok=['acl0', 'user0', 'user1',
    379                                    'host0', 'host1'])
    380 
    381 
    382 if __name__ == '__main__':
    383     unittest.main()
    384