How to use acl_group_add_hosts method in autotest

Best Python code snippet using autotest_python

action_common_unittest.py

Source:action_common_unittest.py Github

copy

Full Screen

1#!/usr/bin/python2#3# Copyright 2008 Google Inc. All Rights Reserved.4"""Tests for action_common."""5import unittest6import sys7import copy8try:9 import autotest.common as common10except ImportError:11 import common12from autotest.cli import cli_mock, action_common, rpc13#14# List action15#16class atest_list_unittest(cli_mock.cli_unittest):17 def test_check_for_wilcard_none(self):18 orig_filters = {'name__in': ['item0', 'item1']}19 orig_checks = {'name__in': ['item0', 'item1']}20 mytest = action_common.atest_list()21 filters = copy.deepcopy(orig_filters)22 checks = copy.deepcopy(orig_checks)23 mytest.check_for_wildcard(filters, checks)24 self.assertEqual(filters, orig_filters)25 self.assertEqual(checks, orig_checks)26 def test_check_for_wilcard_none_list(self):27 orig_filters = {'name__in': ['item0']}28 orig_checks = {'name__in': ['item0']}29 mytest = action_common.atest_list()30 filters = copy.deepcopy(orig_filters)31 checks = copy.deepcopy(orig_checks)32 mytest.check_for_wildcard(filters, checks)33 self.assertEqual(filters, orig_filters)34 self.assertEqual(checks, orig_checks)35 def test_check_for_wilcard_one_list(self):36 filters = {'something__in': ['item*']}37 checks = {'something__in': ['item*']}38 mytest = action_common.atest_list()39 mytest.check_for_wildcard(filters, checks)40 self.assertEqual(filters, {'something__startswith': 'item'})41 self.assertEqual(checks, {'something__startswith': None})42 def test_check_for_wilcard_one_string(self):43 filters = {'something__name': 'item*'}44 checks = {'something__name': 'item*'}45 mytest = action_common.atest_list()46 mytest.check_for_wildcard(filters, checks)47 self.assertEqual(filters, {'something__name__startswith': 'item'})48 self.assertEqual(checks, {'something__name__startswith': None})49 def test_check_for_wilcard_one_string_login(self):50 filters = {'something__login': 'item*'}51 checks = {'something__login': 'item*'}52 mytest = action_common.atest_list()53 mytest.check_for_wildcard(filters, checks)54 self.assertEqual(filters, {'something__login__startswith': 'item'})55 self.assertEqual(checks, {'something__login__startswith': None})56 def test_check_for_wilcard_two(self):57 orig_filters = {'something__in': ['item0*', 'item1*']}58 orig_checks = {'something__in': ['item0*', 'item1*']}59 mytest = action_common.atest_list()60 filters = copy.deepcopy(orig_filters)61 checks = copy.deepcopy(orig_checks)62 self.god.stub_function(sys, 'exit')63 sys.exit.expect_call(1).and_raises(cli_mock.ExitException)64 self.god.mock_io()65 self.assertRaises(cli_mock.ExitException,66 mytest.check_for_wildcard, filters, checks)67 (out, err) = self.god.unmock_io()68 self.god.check_playback()69 self.assertEqual(filters, orig_filters)70 self.assertEqual(checks, orig_checks)71 def _atest_list_execute(self, filters={}, check_results={}):72 values = [{u'id': 180,73 u'platform': 0,74 u'name': u'label0',75 u'invalid': 0,76 u'kernel_config': u''},77 {u'id': 338,78 u'platform': 0,79 u'name': u'label1',80 u'invalid': 0,81 u'kernel_config': u''}]82 mytest = action_common.atest_list()83 mytest.afe = rpc.afe_comm()84 self.mock_rpcs([('get_labels',85 filters,86 True,87 values)])88 self.god.mock_io()89 self.assertEqual(values,90 mytest.execute(op='get_labels',91 filters=filters,92 check_results=check_results))93 errors = mytest.failed94 (out, err) = self.god.unmock_io()95 self.god.check_playback()96 return (out, err, errors)97 def test_atest_list_execute_no_filters(self):98 self._atest_list_execute()99 def test_atest_list_execute_filters_all_good(self):100 filters = {}101 check_results = {}102 filters['name__in'] = ['label0', 'label1']103 check_results['name__in'] = 'name'104 (out, err, errors) = self._atest_list_execute(filters, check_results)105 self.assertEqual(err, '')106 def test_atest_list_execute_filters_good_and_bad(self):107 filters = {}108 check_results = {}109 filters['name__in'] = ['label0', 'label1', 'label2']110 check_results['name__in'] = 'name'111 (out, err, errors) = self._atest_list_execute(filters, check_results)112 K = errors.keys()[0]113 V = errors.values()[0].keys()[0]114 self.assertTrue('Unknown' in K)115 self.assertTrue('label2' in V)116 def test_atest_list_execute_items_good_and_bad_no_check(self):117 filters = {}118 check_results = {}119 filters['name__in'] = ['label0', 'label1', 'label2']120 check_results['name__in'] = None121 (out, err, errors) = self._atest_list_execute(filters, check_results)122 self.assertEqual(err, '')123 def test_atest_list_execute_filters_wildcard(self):124 filters = {}125 check_results = {}126 filters['name__in'] = ['label*']127 check_results['name__in'] = 'name'128 values = [{u'id': 180,129 u'platform': False,130 u'name': u'label0',131 u'invalid': False,132 u'kernel_config': u''},133 {u'id': 338,134 u'platform': False,135 u'name': u'label1',136 u'invalid': False,137 u'kernel_config': u''}]138 mytest = action_common.atest_list()139 mytest.afe = rpc.afe_comm()140 self.mock_rpcs([('get_labels', {'name__startswith': 'label'},141 True, values)])142 self.god.mock_io()143 self.assertEqual(values,144 mytest.execute(op='get_labels',145 filters=filters,146 check_results=check_results))147 (out, err) = self.god.unmock_io()148 self.god.check_playback()149 self.assertEqual(err, '')150#151# Creation & Deletion of a topic (ACL, label, user)152#153class atest_create_or_delete_unittest(cli_mock.cli_unittest):154 def _create_cr_del(self, items):155 def _items():156 return items157 crdel = action_common.atest_create_or_delete()158 crdel.afe = rpc.afe_comm()159 crdel.topic = crdel.usage_topic = 'label'160 crdel.op_action = 'add'161 crdel.get_items = _items162 crdel.data['platform'] = False163 crdel.data_item_key = 'name'164 return crdel165 def test_execute_create_one_topic(self):166 acr = self._create_cr_del(['label0'])167 self.mock_rpcs([('add_label',168 {'name': 'label0', 'platform': False},169 True, 42)])170 ret = acr.execute()171 self.god.check_playback()172 self.assert_(['label0'], ret)173 def test_execute_create_two_topics(self):174 acr = self._create_cr_del(['label0', 'label1'])175 self.mock_rpcs([('add_label',176 {'name': 'label0', 'platform': False},177 True, 42),178 ('add_label',179 {'name': 'label1', 'platform': False},180 True, 43)])181 ret = acr.execute()182 self.god.check_playback()183 self.assertEqualNoOrder(['label0', 'label1'], ret)184 def test_execute_create_error(self):185 acr = self._create_cr_del(['label0'])186 self.mock_rpcs([('add_label',187 {'name': 'label0', 'platform': False},188 False,189 '''ValidationError:190 {'name': 'This value must be unique (label0)'}''')])191 ret = acr.execute()192 self.god.check_playback()193 self.assertEqualNoOrder([], ret)194#195# Adding or Removing users or hosts from a topic(ACL or label)196#197class atest_add_or_remove_unittest(cli_mock.cli_unittest):198 def _create_add_remove(self, items, users=None, hosts=None):199 def _items():200 return [items]201 addrm = action_common.atest_add_or_remove()202 addrm.afe = rpc.afe_comm()203 if users:204 addrm.users = users205 if hosts:206 addrm.hosts = hosts207 addrm.topic = 'acl_group'208 addrm.msg_topic = 'ACL'209 addrm.op_action = 'add'210 addrm.msg_done = 'Added to'211 addrm.get_items = _items212 return addrm213 def test__add_remove_uh_to_topic(self):214 acl_addrm = self._create_add_remove('acl0',215 users=['user0', 'user1'])216 self.mock_rpcs([('acl_group_add_users',217 {'id': 'acl0',218 'users': ['user0', 'user1']},219 True,220 None)])221 acl_addrm._add_remove_uh_to_topic('acl0', 'users')222 self.god.check_playback()223 def test__add_remove_uh_to_topic_raise(self):224 acl_addrm = self._create_add_remove('acl0',225 users=['user0', 'user1'])226 self.assertRaises(AttributeError,227 acl_addrm._add_remove_uh_to_topic,228 'acl0', 'hosts')229 def test_execute_add_or_remove_uh_to_topic_acl_users(self):230 acl_addrm = self._create_add_remove('acl0',231 users=['user0', 'user1'])232 self.mock_rpcs([('acl_group_add_users',233 {'id': 'acl0',234 'users': ['user0', 'user1']},235 True,236 None)])237 execute_result = acl_addrm.execute()238 self.god.check_playback()239 self.assertEqualNoOrder(['acl0'], execute_result['users'])240 self.assertEqual([], execute_result['hosts'])241 def test_execute_add_or_remove_uh_to_topic_acl_users_hosts(self):242 acl_addrm = self._create_add_remove('acl0',243 users=['user0', 'user1'],244 hosts=['host0', 'host1'])245 self.mock_rpcs([('acl_group_add_users',246 {'id': 'acl0',247 'users': ['user0', 'user1']},248 True,249 None),250 ('acl_group_add_hosts',251 {'id': 'acl0',252 'hosts': ['host0', 'host1']},253 True,254 None)])255 execute_result = acl_addrm.execute()256 self.god.check_playback()257 self.assertEqualNoOrder(['acl0'], execute_result['users'])258 self.assertEqualNoOrder(['acl0'], execute_result['hosts'])259 def test_execute_add_or_remove_uh_to_topic_acl_bad_users(self):260 acl_addrm = self._create_add_remove('acl0',261 users=['user0', 'user1'])262 self.mock_rpcs([('acl_group_add_users',263 {'id': 'acl0',264 'users': ['user0', 'user1']},265 False,266 'DoesNotExist: The following users do not exist: '267 'user0, user1')])268 execute_result = acl_addrm.execute()269 self.god.check_playback()270 self.assertEqual([], execute_result['users'])271 self.assertEqual([], execute_result['hosts'])272 self.assertOutput(acl_addrm, execute_result,273 err_words_ok=['DoesNotExist',274 'acl_group_add_users',275 'user0', 'user1'],276 err_words_no=['acl_group_add_hosts'])277 def test_execute_add_or_remove_uh_to_topic_acl_bad_users_partial(self):278 acl_addrm = self._create_add_remove('acl0',279 users=['user0', 'user1'])280 self.mock_rpcs([('acl_group_add_users',281 {'id': 'acl0',282 'users': ['user0', 'user1']},283 False,284 'DoesNotExist: The following users do not exist: '285 'user0'),286 ('acl_group_add_users',287 {'id': 'acl0',288 'users': ['user1']},289 True,290 None)])291 execute_result = acl_addrm.execute()292 self.god.check_playback()293 self.assertEqual(['acl0'], execute_result['users'])294 self.assertEqual([], execute_result['hosts'])295 self.assertOutput(acl_addrm, execute_result,296 out_words_ok=["Added to ACL 'acl0'", 'user1'],297 err_words_ok=['DoesNotExist',298 'acl_group_add_users',299 'user0'],300 err_words_no=['acl_group_add_hosts'])301 def test_execute_add_or_remove_uh_to_topic_acl_bad_u_partial_kill(self):302 acl_addrm = self._create_add_remove('acl0',303 users=['user0', 'user1'])304 acl_addrm.kill_on_failure = True305 self.mock_rpcs([('acl_group_add_users',306 {'id': 'acl0',307 'users': ['user0', 'user1']},308 False,309 'DoesNotExist: The following users do not exist: '310 'user0')])311 sys.exit.expect_call(1).and_raises(cli_mock.ExitException)312 self.god.mock_io()313 self.assertRaises(cli_mock.ExitException, acl_addrm.execute)314 (out, err) = self.god.unmock_io()315 self.god.check_playback()316 self._check_output(out=out, err=err,317 err_words_ok=['DoesNotExist',318 'acl_group_add_users',319 'user0'],320 err_words_no=['acl_group_add_hosts'])321 def test_execute_add_or_remove_uh_to_topic_acl_bad_users_good_hosts(self):322 acl_addrm = self._create_add_remove('acl0',323 users=['user0', 'user1'],324 hosts=['host0', 'host1'])325 self.mock_rpcs([('acl_group_add_users',326 {'id': 'acl0',327 'users': ['user0', 'user1']},328 False,329 'DoesNotExist: The following users do not exist: '330 'user0, user1'),331 ('acl_group_add_hosts',332 {'id': 'acl0',333 'hosts': ['host0', 'host1']},334 True,335 None)])336 execute_result = acl_addrm.execute()337 self.god.check_playback()338 self.assertEqual([], execute_result['users'])339 self.assertEqual(['acl0'], execute_result['hosts'])340 self.assertOutput(acl_addrm, execute_result,341 out_words_ok=["Added to ACL 'acl0' hosts:",342 "host0", "host1"],343 err_words_ok=['DoesNotExist',344 'acl_group_add_users',345 'user0', 'user1'],346 err_words_no=['acl_group_add_hosts'])347 def test_execute_add_or_remove_uh_to_topic_acl_good_users_bad_hosts(self):348 acl_addrm = self._create_add_remove('acl0 with space',349 users=['user0', 'user1'],350 hosts=['host0', 'host1'])351 self.mock_rpcs([('acl_group_add_users',352 {'id': 'acl0 with space',353 'users': ['user0', 'user1']},354 True,355 None),356 ('acl_group_add_hosts',357 {'id': 'acl0 with space',358 'hosts': ['host0', 'host1']},359 False,360 'DoesNotExist: The following hosts do not exist: '361 'host0, host1')])362 execute_result = acl_addrm.execute()363 self.god.check_playback()364 self.assertEqual(['acl0 with space'], execute_result['users'])365 self.assertEqual([], execute_result['hosts'])366 self.assertOutput(acl_addrm, execute_result,367 out_words_ok=["Added to ACL 'acl0 with space' users:",368 "user0", "user1"],369 err_words_ok=['DoesNotExist',370 'acl_group_add_hosts',371 'host0', 'host1'],372 err_words_no=['acl_group_add_users'])373 def test_exe_add_or_remove_uh_to_topic_acl_good_u_bad_hosts_partial(self):374 acl_addrm = self._create_add_remove('acl0',375 users=['user0', 'user1'],376 hosts=['host0', 'host1'])377 self.mock_rpcs([('acl_group_add_users',378 {'id': 'acl0',379 'users': ['user0', 'user1']},380 True,381 None),382 ('acl_group_add_hosts',383 {'id': 'acl0',384 'hosts': ['host0', 'host1']},385 False,386 'DoesNotExist: The following hosts do not exist: '387 'host1'),388 ('acl_group_add_hosts',389 {'id': 'acl0',390 'hosts': ['host0']},391 True,392 None)])393 execute_result = acl_addrm.execute()394 self.god.check_playback()395 self.assertEqual(['acl0'], execute_result['users'])396 self.assertEqual(['acl0'], execute_result['hosts'])397 self.assertOutput(acl_addrm, execute_result,398 out_words_ok=["Added to ACL 'acl0' users:",399 "user0", "user1", "host0"],400 err_words_ok=['DoesNotExist',401 'acl_group_add_hosts',402 'host1'],403 err_words_no=['acl_group_add_users'])404 def test_execute_add_or_remove_uh_to_topic_acl_bad_users_bad_hosts(self):405 acl_addrm = self._create_add_remove('acl0',406 users=['user0', 'user1'],407 hosts=['host0', 'host1'])408 self.mock_rpcs([('acl_group_add_users',409 {'id': 'acl0',410 'users': ['user0', 'user1']},411 False,412 'DoesNotExist: The following users do not exist: '413 'user0, user1'),414 ('acl_group_add_hosts',415 {'id': 'acl0',416 'hosts': ['host0', 'host1']},417 False,418 'DoesNotExist: The following hosts do not exist: '419 'host0, host1')])420 execute_result = acl_addrm.execute()421 self.god.check_playback()422 self.assertEqual([], execute_result['users'])423 self.assertEqual([], execute_result['hosts'])424 self.assertOutput(acl_addrm, execute_result,425 err_words_ok=['DoesNotExist',426 'acl_group_add_hosts',427 'host0', 'host1',428 'acl_group_add_users',429 'user0', 'user1'])430 def test_execute_add_or_remove_uh_to_topic_acl_bad_u_bad_h_partial(self):431 acl_addrm = self._create_add_remove('acl0',432 users=['user0', 'user1'],433 hosts=['host0', 'host1'])434 self.mock_rpcs([('acl_group_add_users',435 {'id': 'acl0',436 'users': ['user0', 'user1']},437 False,438 'DoesNotExist: The following users do not exist: '439 'user0'),440 ('acl_group_add_users',441 {'id': 'acl0',442 'users': ['user1']},443 True,444 None),445 ('acl_group_add_hosts',446 {'id': 'acl0',447 'hosts': ['host0', 'host1']},448 False,449 'DoesNotExist: The following hosts do not exist: '450 'host1'),451 ('acl_group_add_hosts',452 {'id': 'acl0',453 'hosts': ['host0']},454 True,455 None)])456 execute_result = acl_addrm.execute()457 self.god.check_playback()458 self.assertEqual(['acl0'], execute_result['users'])459 self.assertEqual(['acl0'], execute_result['hosts'])460 self.assertOutput(acl_addrm, execute_result,461 out_words_ok=["Added to ACL 'acl0' user:",462 "Added to ACL 'acl0' host:",463 'user1', 'host0'],464 err_words_ok=['DoesNotExist',465 'acl_group_add_hosts',466 'host1',467 'acl_group_add_users',468 'user0'])469 def test_execute_add_or_remove_to_topic_bad_acl_uh(self):470 acl_addrm = self._create_add_remove('acl0',471 users=['user0', 'user1'],472 hosts=['host0', 'host1'])473 self.mock_rpcs([('acl_group_add_users',474 {'id': 'acl0',475 'users': ['user0', 'user1']},476 False,477 'DoesNotExist: acl_group matching '478 'query does not exist.'),479 ('acl_group_add_hosts',480 {'id': 'acl0',481 'hosts': ['host0', 'host1']},482 False,483 'DoesNotExist: acl_group matching '484 'query does not exist.')])485 execute_result = acl_addrm.execute()486 self.god.check_playback()487 self.assertEqual([], execute_result['users'])488 self.assertEqual([], execute_result['hosts'])489 self.assertOutput(acl_addrm, execute_result,490 err_words_ok=['DoesNotExist',491 'acl_group_add_hosts',492 'acl_group_add_users'])493if __name__ == '__main__':...

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run autotest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful