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