Best Python code snippet using autotest_python
acl_unittest.py
Source:acl_unittest.py  
1# pylint: disable-msg=C01112#!/usr/bin/python3#4# Copyright 2008 Google Inc. All Rights Reserved.5"""Test for acl."""6import unittest, sys7import common8from autotest_lib.cli import acl, cli_mock9class acl_list_unittest(cli_mock.cli_unittest):10    def test_parse_list_acl(self):11        acl_list = acl.acl_list()12        afile = cli_mock.create_file('acl0\nacl3\nacl4\n')13        sys.argv = ['atest', 'acl0', 'acl1,acl2',14                    '--alist', afile.name, 'acl5', 'acl6,acl7']15        acl_list.parse()16        self.assertEqualNoOrder(['acl%s' % x for x in range(8)],17                                acl_list.acls)18        afile.clean()19    def test_parse_list_user(self):20        acl_list = acl.acl_list()21        sys.argv = ['atest', '--user', 'user0']22        acl_list.parse()23        self.assertEqual('user0', acl_list.users)24    def test_parse_list_host(self):25        acl_list = acl.acl_list()26        sys.argv = ['atest', '--mach', 'host0']27        acl_list.parse()28        self.assertEqual('host0', acl_list.hosts)29    def _test_parse_bad_options(self):30        acl_list = acl.acl_list()31        self.god.mock_io()32        sys.exit.expect_call(1).and_raises(cli_mock.ExitException)33        self.assertRaises(cli_mock.ExitException, acl_list.parse)34        (out, err) = self.god.unmock_io()35        self.god.check_playback()36        self.assert_(err.find('usage'))37    def test_parse_list_acl_user(self):38        sys.argv = ['atest', 'acl0', '-u', 'user']39        self._test_parse_bad_options()40    def test_parse_list_acl_2users(self):41        sys.argv = ['atest', '-u', 'user0,user1']42        self._test_parse_bad_options()43    def test_parse_list_acl_host(self):44        sys.argv = ['atest', 'acl0', '--mach', 'mach']45        self._test_parse_bad_options()46    def test_parse_list_acl_2hosts(self):47        sys.argv = ['atest', '--mach', 'mach0,mach1']48        self._test_parse_bad_options()49    def test_parse_list_user_host(self):50        sys.argv = ['atest', '-u', 'user', '--mach', 'mach']51        self._test_parse_bad_options()52    def test_parse_list_all(self):53        sys.argv = ['atest', '-u', 'user', '--mach', 'mach', 'acl0']54        self._test_parse_bad_options()55    def test_execute_list_all_acls(self):56        self.run_cmd(argv=['atest', 'acl', 'list', '-v'],57                     rpcs=[('get_acl_groups', {}, True,58                           [{'id': 1L,59                             'name': 'Everyone',60                             'description': '',61                             'users': ['debug_user'],62                             'hosts': []}])],63                     out_words_ok=['debug_user'])64    def test_execute_list_acls_for_acl(self):65        self.run_cmd(argv=['atest', 'acl', 'list', 'acl0'],66                     rpcs=[('get_acl_groups', {'name__in': ['acl0']}, True,67                           [{'id': 1L,68                             'name': 'Everyone',69                             'description': '',70                             'users': ['user0'],71                             'hosts': []}])],72                     out_words_ok=['Everyone'])73    def test_execute_list_acls_for_user(self):74        self.run_cmd(argv=['atest', 'acl', 'list', '-v', '--user', 'user0'],75                     rpcs=[('get_acl_groups', {'users__login': 'user0'}, True,76                           [{'id': 1L,77                             'name': 'Everyone',78                             'description': '',79                             'users': ['user0'],80                             'hosts': []}])],81                     out_words_ok=['user0'])82    def test_execute_list_acls_for_host(self):83        self.run_cmd(argv=['atest', 'acl', 'list', '-m', 'host0'],84                     rpcs=[('get_acl_groups', {'hosts__hostname': 'host0'},85                            True,86                           [{'id': 1L,87                             'name': 'Everyone',88                             'description': '',89                             'users': ['user0'],90                             'hosts': ['host0']}])],91                     out_words_ok=['Everyone'],92                     out_words_no=['host0'])93    def test_execute_list_acls_for_host_verb(self):94        self.run_cmd(argv=['atest', 'acl', 'list', '-m', 'host0', '-v'],95                     rpcs=[('get_acl_groups', {'hosts__hostname': 'host0'},96                            True,97                           [{'id': 1L,98                             'name': 'Everyone',99                             'description': '',100                             'users': ['user0'],101                             'hosts': ['host0']}])],102                     out_words_ok=['Everyone', 'host0'])103class acl_create_unittest(cli_mock.cli_unittest):104    def test_acl_create_parse_ok(self):105        acls = acl.acl_create()106        sys.argv = ['atest', 'acl0',107                    '--desc', 'my_favorite_acl']108        acls.parse()109        self.assertEqual('my_favorite_acl', acls.data['description'])110    def test_acl_create_parse_no_desc(self):111        self.god.mock_io()112        acls = acl.acl_create()113        sys.argv = ['atest', 'acl0']114        sys.exit.expect_call(1).and_raises(cli_mock.ExitException)115        self.assertRaises(cli_mock.ExitException, acls.parse)116        self.god.check_playback()117        self.god.unmock_io()118    def test_acl_create_parse_2_acls(self):119        self.god.mock_io()120        acls = acl.acl_create()121        sys.argv = ['atest', 'acl0', 'acl1',122                    '-desc', 'my_favorite_acl']123        sys.exit.expect_call(1).and_raises(cli_mock.ExitException)124        self.assertRaises(cli_mock.ExitException, acls.parse)125        self.god.check_playback()126        self.god.unmock_io()127    def test_acl_create_parse_no_option(self):128        self.god.mock_io()129        acls = acl.acl_create()130        sys.argv = ['atest']131        sys.exit.expect_call(1).and_raises(cli_mock.ExitException)132        self.assertRaises(cli_mock.ExitException, acls.parse)133        self.god.check_playback()134        self.god.unmock_io()135    def test_acl_create_acl_ok(self):136        self.run_cmd(argv=['atest', 'acl', 'create', 'acl0',137                           '--desc', 'my_favorite_acl'],138                     rpcs=[('add_acl_group',139                           {'description': 'my_favorite_acl',140                            'name': 'acl0'},141                           True,142                            3L)],143                     out_words_ok=['acl0'])144    def test_acl_create_duplicate_acl(self):145        self.run_cmd(argv=['atest', 'acl', 'create', 'acl0',146                           '--desc', 'my_favorite_acl'],147                     rpcs=[('add_acl_group',148                           {'description': 'my_favorite_acl',149                            'name': 'acl0'},150                           False,151                           'ValidationError:'152                           '''{'name': 'This value must be '''153                           '''unique (acl0)'}''')],154                     err_words_ok=['acl0', 'ValidationError',155                                   'unique'])156class acl_delete_unittest(cli_mock.cli_unittest):157    def test_acl_delete_acl_ok(self):158        self.run_cmd(argv=['atest', 'acl', 'delete', 'acl0',159                           '--no-confirmation'],160                     rpcs=[('delete_acl_group', {'id': 'acl0'}, True, None)],161                     out_words_ok=['acl0'])162    def test_acl_delete_acl_does_not_exist(self):163        self.run_cmd(argv=['atest', 'acl', 'delete', 'acl0',164                           '--no-confirmation'],165                     rpcs=[('delete_acl_group', {'id': 'acl0'},166                            False,167                            'DoesNotExist: acl_group matching '168                            'query does not exist.')],169                     err_words_ok=['acl0', 'DoesNotExist'])170    def test_acl_delete_multiple_acl_ok(self):171        alist = cli_mock.create_file('acl2\nacl1')172        self.run_cmd(argv=['atest', 'acl', 'delete',173                           'acl0', 'acl1', '--no-confirmation',174                           '--alist', alist.name],175                     rpcs=[('delete_acl_group',176                           {'id': 'acl0'},177                           True,178                           None),179                          ('delete_acl_group',180                           {'id': 'acl1'},181                           True,182                           None),183                          ('delete_acl_group',184                           {'id': 'acl2'},185                           True,186                           None)],187                     out_words_ok=['acl0', 'acl1', 'acl2', 'Deleted'])188        alist.clean()189    def test_acl_delete_multiple_acl_bad(self):190        alist = cli_mock.create_file('acl2\nacl1')191        self.run_cmd(argv=['atest', 'acl', 'delete',192                           'acl0', 'acl1', '--no-confirmation',193                           '--alist', alist.name],194                     rpcs=[('delete_acl_group',195                           {'id': 'acl0'},196                           True,197                           None),198                          ('delete_acl_group',199                           {'id': 'acl1'},200                           False,201                           'DoesNotExist: acl_group '202                           'matching query does not exist.'),203                          ('delete_acl_group',204                           {'id': 'acl2'},205                           True,206                           None)],207                     out_words_ok=['acl0', 'acl2', 'Deleted'],208                     err_words_ok=['acl1', 'delete_acl_group',209                                   'DoesNotExist', 'acl_group',210                                   'matching'])211        alist.clean()212class acl_add_unittest(cli_mock.cli_unittest):213    def test_acl_add_parse_no_option(self):214        self.god.mock_io()215        acls = acl.acl_add()216        sys.argv = ['atest']217        sys.exit.expect_call(1).and_raises(cli_mock.ExitException)218        self.assertRaises(cli_mock.ExitException, acls.parse)219        self.god.unmock_io()220        self.god.check_playback()221    def test_acl_add_users_hosts(self):222        self.run_cmd(argv=['atest', 'acl', 'add', 'acl0',223                           '-u', 'user0,user1', '-m', 'host0'],224                     rpcs=[('acl_group_add_users',225                           {'id': 'acl0',226                            'users': ['user0', 'user1']},227                           True,228                           None),229                          ('acl_group_add_hosts',230                           {'id': 'acl0',231                            'hosts': ['host0']},232                           True,233                           None)],234                     out_words_ok=['acl0', 'user0',235                                   'user1', 'host0'])236    def test_acl_add_bad_users(self):237        self.run_cmd(argv=['atest', 'acl', 'add', 'acl0',238                           '-u', 'user0,user1'],239                     rpcs=[('acl_group_add_users',240                           {'id': 'acl0',241                            'users': ['user0', 'user1']},242                            False,243                            'DoesNotExist: The following Users do not exist: '244                            'user0, user1')],245                     err_words_ok=['user0', 'user1'])246    def test_acl_add_bad_users_hosts(self):247        self.run_cmd(argv=['atest', 'acl', 'add', 'acl0',248                           '-u', 'user0,user1', '-m', 'host0,host1'],249                     rpcs=[('acl_group_add_users',250                           {'id': 'acl0',251                            'users': ['user0', 'user1']},252                            False,253                            'DoesNotExist: The following Users do not exist: '254                            'user0'),255                           ('acl_group_add_users',256                           {'id': 'acl0',257                            'users': ['user1']},258                            True,259                            None),260                           ('acl_group_add_hosts',261                            {'id': 'acl0',262                             'hosts': ['host1', 'host0']},263                            False,264                            'DoesNotExist: The following Hosts do not exist: '265                            'host1'),266                           ('acl_group_add_hosts',267                            {'id': 'acl0',268                             'hosts': ['host0']},269                            True,270                            None)],271                     out_words_ok=['acl0', 'user1', 'host0'],272                     out_words_no=['user0', 'host1'],273                     err_words_ok=['user0', 'host1'],274                     err_words_no=['user1', 'host0'])275class acl_remove_unittest(cli_mock.cli_unittest):276    def test_acl_remove_remove_users(self):277        self.run_cmd(argv=['atest', 'acl', 'remove',278                           'acl0', '-u', 'user0,user1'],279                     rpcs=[('acl_group_remove_users',280                           {'id': 'acl0',281                            'users': ['user0', 'user1']},282                           True,283                           None)],284                     out_words_ok=['acl0', 'user0', 'user1'],285                     out_words_no=['host'])286    def test_acl_remove_remove_hosts(self):287        self.run_cmd(argv=['atest', 'acl', 'remove',288                           'acl0', '--mach', 'host0,host1'],289                     rpcs=[('acl_group_remove_hosts',290                           {'id': 'acl0',291                            'hosts': ['host1', 'host0']},292                           True,293                           None)],294                     out_words_ok=['acl0', 'host0', 'host1'],295                     out_words_no=['user'])296    def test_acl_remove_remove_both(self):297        self.run_cmd(argv=['atest', 'acl', 'remove',298                           'acl0', '--user', 'user0,user1',299                           '-m', 'host0,host1'],300                     rpcs=[('acl_group_remove_users',301                           {'id': 'acl0',302                            'users': ['user0', 'user1']},303                           True,304                           None),305                          ('acl_group_remove_hosts',306                           {'id': 'acl0',307                            'hosts': ['host1', 'host0']},308                           True,309                           None)],310                     out_words_ok=['acl0', 'user0', 'user1',311                                   'host0', 'host1'])312if __name__ == '__main__':...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
