Best Python code snippet using autotest_python
test_fw_mgr.py
Source:test_fw_mgr.py
...118 'prepare_fabric_fw') as prep_fab:119 fw_rule_data = self._fill_rule_data()120 self.fw_mgr.fw_rule_create(fw_rule_data, None)121 parent = mock.MagicMock()122 parent.attach_mock(prep_fab, 'prepare_fabric_fw')123 parent.attach_mock(self.create_fw_dev_mock, 'create_fw_device')124 parent.attach_mock(self.update_fw_db_mock,125 'update_fw_db_final_result')126 parent.attach_mock(self.update_fw_dev_mock,127 'update_fw_db_dev_status')128 prep_fab.assert_not_called()129 self.update_fw_db_mock.assert_not_called()130 self.create_fw_dev_mock.assert_not_called()131 self.update_fw_dev_mock.assert_not_called()132 def test_fw_rule_create(self):133 """Test FW rule create. """134 self._test_fw_rule_create()135 def _fill_policy_data(self, rule_dict):136 fw_policy_dict = {'firewall_policy': {137 'tenant_id': self.tenant_id, 'id': self.policy_id,138 'name': 'PolicyA', 'firewall_rules': rule_dict}}139 # Fill the above with Policy rules by calling the fill rule fn140 return fw_policy_dict141 def _test_fw_policy_create(self, with_rule=False):142 with mock.patch.object(self.fw_mgr.fabric,143 'prepare_fabric_fw') as prep_fab:144 fw_rule_data = dict()145 if with_rule:146 fw_rule_data = []147 fw_rule_data.append(self.rule_id)148 fw_policy_data = self._fill_policy_data(fw_rule_data)149 self.fw_mgr.fw_policy_create(fw_policy_data, None)150 parent = mock.MagicMock()151 parent.attach_mock(prep_fab, 'prepare_fabric_fw')152 parent.attach_mock(self.create_fw_dev_mock, 'create_fw_device')153 parent.attach_mock(self.update_fw_db_mock,154 'update_fw_db_final_result')155 parent.attach_mock(self.update_fw_dev_mock,156 'update_fw_db_dev_status')157 prep_fab.assert_not_called()158 self.update_fw_db_mock.assert_not_called()159 self.create_fw_dev_mock.assert_not_called()160 self.update_fw_dev_mock.assert_not_called()161 def test_fw_policy_create(self):162 """Test FW policy create. """163 self._test_fw_policy_create()164 def _fill_fw_data(self, rule_dict):165 return {'firewall': {'tenant_id': self.tenant_id,166 'id': self.fw_id,167 'firewall_policy_id': self.policy_id,168 'admin_state_up': True, 'name': self.fw_name,169 'firewall_rules': rule_dict,170 'router_ids': [self.router_id]}}171 def _fill_fw_delete_data(self):172 fw_dict = {}173 fw_dict['firewall_id'] = self.fw_id174 return fw_dict175 def _prepare_result_fw_dict(self):176 return {'rules': {self.rule_id: self.rule_dict},177 'tenant_name': self.tenant_name,178 'tenant_id': self.tenant_id, 'fw_id': self.fw_id,179 'fw_name': self.fw_name,180 'firewall_policy_id': self.policy_id,181 'fw_type': self.fw_type, 'router_id': self.router_id}182 def _test_fw_create(self, with_rule=True):183 res_fw_dict = self._prepare_result_fw_dict()184 with mock.patch('networking_cisco.apps.saf.server.'185 'dfa_openstack_helper.DfaNeutronHelper'),\186 mock.patch.object(self.fw_mgr.fabric,187 'prepare_fabric_fw') as prep_fab,\188 mock.patch('networking_cisco.apps.saf.db.dfa_db_models.'189 'DfaDBMixin.get_project_name',190 return_value=self.tenant_name):191 FakeClass.set_return(dev_mgr.DeviceMgr,192 'is_device_virtual', False)193 self.fw_mgr.os_helper.get_rtr_name.return_value = (194 'Cisco_TenantEdge_TenantA')195 fw_rule_data = dict()196 if with_rule:197 fw_rule_data = self._fill_rule_data()198 fw_data = self._fill_fw_data(fw_rule_data)199 self._test_fw_rule_create()200 self._test_fw_policy_create(with_rule=True)201 parent = mock.MagicMock()202 parent.attach_mock(prep_fab, 'prepare_fabric_fw')203 parent.attach_mock(self.create_fw_dev_mock, 'create_fw_device')204 parent.attach_mock(self.update_fw_db_mock,205 'update_fw_db_final_result')206 parent.attach_mock(self.update_fw_dev_mock,207 'update_fw_db_dev_status')208 self.fw_mgr.fw_create(fw_data, None)209 expected_calls = [210 mock.call.prepare_fabric_fw(self.tenant_id, res_fw_dict, False,211 'FAB_CREATE_PEND'),212 mock.call.update_fw_db_final_result(res_fw_dict.get('fw_id'),213 'FAB_CREATE_DONE'),214 mock.call.create_fw_device(self.tenant_id,215 res_fw_dict.get('fw_id'), res_fw_dict),216 mock.call.update_fw_db_dev_status(res_fw_dict.get('fw_id'),217 'SUCCESS')]218 parent.assert_has_calls(expected_calls, any_order=True)219 # any_order should be False,in which case it didn't work TODO(padkrish)220 def test_fw_create(self):221 """Test FW create. """222 self._test_fw_create()223 def _test_fw_create_fabric_error(self, with_rule=True):224 """Internal function to test the error case for fabric create.225 The fabric module is mocked to return an error for prepare fabric.226 """227 res_fw_dict = self._prepare_result_fw_dict()228 with mock.patch('networking_cisco.apps.saf.server.'229 'dfa_openstack_helper.DfaNeutronHelper'),\230 mock.patch('networking_cisco.apps.saf.db.dfa_db_models.'231 'DfaDBMixin.get_project_name',232 return_value=self.tenant_name),\233 mock.patch.object(self.fw_mgr.fabric, 'prepare_fabric_fw',234 return_value=False) as prep_fab:235 FakeClass.set_return(dev_mgr.DeviceMgr, 'is_device_virtual', False)236 self.fw_mgr.os_helper.get_rtr_name.return_value = (237 'Cisco_TenantEdge_TenantA')238 fw_rule_data = dict()239 if with_rule:240 fw_rule_data = self._fill_rule_data()241 fw_data = self._fill_fw_data(fw_rule_data)242 self._test_fw_rule_create()243 self._test_fw_policy_create(with_rule=True)244 parent = mock.MagicMock()245 parent.attach_mock(prep_fab, 'prepare_fabric_fw')246 parent.attach_mock(self.create_fw_dev_mock, 'create_fw_device')247 parent.attach_mock(self.update_fw_db_mock,248 'update_fw_db_final_result')249 parent.attach_mock(self.update_fw_dev_mock,250 'update_fw_db_dev_status')251 self.fw_mgr.fw_create(fw_data, None)252 expected_calls = [mock.call.prepare_fabric_fw(self.tenant_id,253 res_fw_dict, False,254 'FAB_CREATE_PEND')]255 parent.assert_has_calls(expected_calls, any_order=True)256 self.update_fw_db_mock.assert_not_called()257 self.create_fw_dev_mock.assert_not_called()258 self.update_fw_dev_mock.assert_not_called()259 def test_fw_create_fabric_error(self):260 """Test FW create. """261 self._test_fw_create_fabric_error()262 def _test_fw_create_device_error(self, with_rule=True):263 """Internal function to test the error case for device setup.264 The device driver module is mocked to return an error for device265 configuration.266 """267 res_fw_dict = self._prepare_result_fw_dict()268 with mock.patch('networking_cisco.apps.saf.server.'269 'dfa_openstack_helper.DfaNeutronHelper'),\270 mock.patch('networking_cisco.apps.saf.db.dfa_db_models.'271 'DfaDBMixin.get_project_name',272 return_value=self.tenant_name),\273 mock.patch.object(self.fw_mgr.fabric,274 'prepare_fabric_fw') as prep_fab,\275 mock.patch.object(self.fw_mgr, 'create_fw_device',276 return_value=False) as create_fw_dev:277 FakeClass.set_return(dev_mgr.DeviceMgr, 'is_device_virtual', False)278 self.fw_mgr.os_helper.get_rtr_name.return_value = (279 'Cisco_TenantEdge_TenantA')280 fw_rule_data = dict()281 if with_rule:282 fw_rule_data = self._fill_rule_data()283 fw_data = self._fill_fw_data(fw_rule_data)284 self._test_fw_rule_create()285 self._test_fw_policy_create(with_rule=True)286 parent = mock.MagicMock()287 parent.attach_mock(prep_fab, 'prepare_fabric_fw')288 parent.attach_mock(create_fw_dev, 'create_fw_device')289 parent.attach_mock(self.update_fw_db_mock,290 'update_fw_db_final_result')291 parent.attach_mock(self.update_fw_dev_mock,292 'update_fw_db_dev_status')293 self.fw_mgr.fw_create(fw_data, None)294 expected_calls = [295 mock.call.prepare_fabric_fw(self.tenant_id, res_fw_dict, False,296 'FAB_CREATE_PEND'),297 mock.call.update_fw_db_final_result(res_fw_dict.get('fw_id'),298 'FAB_CREATE_DONE'),299 mock.call.create_fw_device(self.tenant_id,300 res_fw_dict.get('fw_id'), res_fw_dict)]301 parent.assert_has_calls(expected_calls, any_order=True)302 self.update_fw_dev_mock.assert_not_called()303 def test_fw_create_device_error(self):304 """Test FW create. """305 self._test_fw_create_device_error()306 def _test_fw_delete(self, with_rule=True):307 res_fw_dict = self._prepare_result_fw_dict()308 with mock.patch('networking_cisco.apps.saf.server.'309 'dfa_openstack_helper.DfaNeutronHelper'),\310 mock.patch.object(self.fw_mgr,311 'delete_fw_device') as delete_fw_dev,\312 mock.patch.object(self.fw_mgr.fabric,313 'delete_fabric_fw') as del_fab,\314 mock.patch.object(self.fw_mgr, 'delete_fw') as del_fw,\315 mock.patch('networking_cisco.apps.saf.db.dfa_db_models.'316 'DfaDBMixin.get_project_name',317 return_value=self.tenant_name):318 FakeClass.set_return(dev_mgr.DeviceMgr, 'is_device_virtual',319 False)320 self.fw_mgr.os_helper.get_rtr_name.return_value = (321 'Cisco_TenantEdge_TenantA')322 fw_rule_data = dict()323 if with_rule:324 fw_rule_data = self._fill_rule_data()325 fw_data = self._fill_fw_data(fw_rule_data)326 self._test_fw_rule_create()327 self._test_fw_policy_create(with_rule=True)328 self._test_fw_create(with_rule=True)329 parent = mock.MagicMock()330 parent.attach_mock(del_fab, 'delete_fabric_fw')331 parent.attach_mock(delete_fw_dev, 'delete_fw_device')332 parent.attach_mock(self.update_fw_db_mock,333 'update_fw_db_final_result')334 parent.attach_mock(self.update_fw_dev_mock,335 'update_fw_db_dev_status')336 parent.attach_mock(del_fw, 'delete_fw')337 fw_data = self._fill_fw_delete_data()338 self.fw_mgr.fw_delete(fw_data, None)339 expected_calls = [340 mock.call.update_fw_db_final_result(res_fw_dict.get('fw_id'),341 'FAB_DELETE_PEND'),342 mock.call.delete_fw_device(self.tenant_id,343 res_fw_dict.get('fw_id'), res_fw_dict),344 mock.call.update_fw_db_dev_status(res_fw_dict.get('fw_id'), ''),345 mock.call.delete_fabric_fw(self.tenant_id, res_fw_dict, False,346 'FAB_DELETE_PEND'),347 mock.call.update_fw_db_final_result(res_fw_dict.get('fw_id'),348 'FAB_DELETE_DONE'),349 mock.call.delete_fw(res_fw_dict.get('fw_id'))]350 parent.assert_has_calls(expected_calls, any_order=True)351 def test_fw_delete(self):352 """Test FW delete. """353 self._test_fw_delete()354 def _test_fw_delete_dev_error(self, with_rule=True):355 """Internal function to test the error case for device cleanup.356 The device driver module is mocked to return an error for device357 configuration cleanup.358 """359 res_fw_dict = self._prepare_result_fw_dict()360 with mock.patch('networking_cisco.apps.saf.server.'361 'dfa_openstack_helper.DfaNeutronHelper'),\362 mock.patch.object(self.fw_mgr, 'delete_fw_device',363 return_value=False) as delete_fw_dev,\364 mock.patch.object(self.fw_mgr.fabric,365 'delete_fabric_fw') as del_fab,\366 mock.patch.object(self.fw_mgr, 'delete_fw') as del_fw,\367 mock.patch('networking_cisco.apps.saf.db.dfa_db_models.'368 'DfaDBMixin.get_project_name',369 return_value=self.tenant_name):370 FakeClass.set_return(dev_mgr.DeviceMgr, 'is_device_virtual', False)371 self.fw_mgr.os_helper.get_rtr_name.return_value = (372 'Cisco_TenantEdge_TenantA')373 fw_rule_data = dict()374 if with_rule:375 fw_rule_data = self._fill_rule_data()376 fw_data = self._fill_fw_data(fw_rule_data)377 self._test_fw_rule_create()378 self._test_fw_policy_create(with_rule=True)379 self._test_fw_create(with_rule=True)380 parent = mock.MagicMock()381 parent.attach_mock(del_fab, 'delete_fabric_fw')382 parent.attach_mock(delete_fw_dev, 'delete_fw_device')383 parent.attach_mock(self.update_fw_db_mock,384 'update_fw_db_final_result')385 parent.attach_mock(self.update_fw_dev_mock,386 'update_fw_db_dev_status')387 parent.attach_mock(del_fw, 'delete_fw')388 fw_data = self._fill_fw_delete_data()389 self.fw_mgr.fw_delete(fw_data, None)390 expected_calls = [391 mock.call.update_fw_db_final_result(res_fw_dict.get('fw_id'),392 'FAB_DELETE_PEND'),393 mock.call.delete_fw_device(self.tenant_id,394 res_fw_dict.get('fw_id'), res_fw_dict)]395 parent.assert_has_calls(expected_calls, any_order=True)396 del_fab.assert_not_called()397 del_fw.assert_not_called()398 def test_fw_delete_dev_error(self):399 """Test FW delete. """400 self._test_fw_delete_dev_error()401 def _test_fw_delete_fab_error(self, with_rule=True):402 """Internal function to test the error case for fabric cleanup.403 The fabric module is mocked to return an error for delete the fabric404 configuration.405 """406 res_fw_dict = self._prepare_result_fw_dict()407 with mock.patch('networking_cisco.apps.saf.server.'408 'dfa_openstack_helper.DfaNeutronHelper'),\409 mock.patch.object(self.fw_mgr,410 'delete_fw_device') as delete_fw_dev,\411 mock.patch.object(self.fw_mgr.fabric, 'delete_fabric_fw',412 return_value=False) as del_fab,\413 mock.patch.object(self.fw_mgr, 'delete_fw') as del_fw,\414 mock.patch('networking_cisco.apps.saf.db.dfa_db_models.'415 'DfaDBMixin.get_project_name',416 return_value=self.tenant_name):417 FakeClass.set_return(dev_mgr.DeviceMgr, 'is_device_virtual', False)418 self.fw_mgr.os_helper.get_rtr_name.return_value = (419 'Cisco_TenantEdge_TenantA')420 fw_rule_data = dict()421 if with_rule:422 fw_rule_data = self._fill_rule_data()423 fw_data = self._fill_fw_data(fw_rule_data)424 self._test_fw_rule_create()425 self._test_fw_policy_create(with_rule=True)426 self._test_fw_create(with_rule=True)427 parent = mock.MagicMock()428 parent.attach_mock(del_fab, 'delete_fabric_fw')429 parent.attach_mock(delete_fw_dev, 'delete_fw_device')430 parent.attach_mock(self.update_fw_db_mock,431 'update_fw_db_final_result')432 parent.attach_mock(self.update_fw_dev_mock,433 'update_fw_db_dev_status')434 parent.attach_mock(del_fw, 'delete_fw')435 fw_data = self._fill_fw_delete_data()436 self.fw_mgr.fw_delete(fw_data, None)437 expected_calls = [438 mock.call.update_fw_db_final_result(res_fw_dict.get('fw_id'),439 'FAB_DELETE_PEND'),440 mock.call.delete_fw_device(self.tenant_id,441 res_fw_dict.get('fw_id'), res_fw_dict),442 mock.call.update_fw_db_dev_status(res_fw_dict.get('fw_id'), ''),443 mock.call.delete_fabric_fw(self.tenant_id, res_fw_dict, False,444 'FAB_DELETE_PEND')]445 parent.assert_has_calls(expected_calls, any_order=True)446 del_fw.assert_not_called()447 def test_fw_delete_fab_error(self):448 """Test FW delete. """...
test_execution_managers.py
Source:test_execution_managers.py
...22class TestStandardExecutionManager(unittest.TestCase):23 def test_run(self):24 logger = MagicMock()25 logger.msg_proc = MagicMock()26 logger.msg_proc.attach_mock(MagicMock(), "emit")27 exec_manager = ProcessExecutionManager(logger, "python", "-c", "print('hello')")28 ret = exec_manager.run_until_complete()29 logger.msg_proc.emit.assert_called_once()30 logger.msg_proc.emit.assert_called_with("hello")31 self.assertEqual(ret, 0)32 def test_run_with_error(self):33 logger = MagicMock()34 logger.msg_proc_error = MagicMock()35 logger.msg_proc_error.attach_mock(MagicMock(), "emit")36 exec_manager = ProcessExecutionManager(logger, "python", "-c", "gibberish")37 ret = exec_manager.run_until_complete()38 logger.msg_proc_error.emit.assert_called_with("NameError: name 'gibberish' is not defined")39 self.assertEqual(ret, 1)40 def test_run_unknown_program(self):41 logger = MagicMock()42 logger.msg_proc = MagicMock()43 logger.msg_proc_error = MagicMock()44 logger.msg_standard_execution = MagicMock()45 logger.msg_proc.attach_mock(MagicMock(), "emit")46 logger.msg_proc_error.attach_mock(MagicMock(), "emit")47 logger.msg_standard_execution.attach_mock(MagicMock(), "emit")48 exec_manager = ProcessExecutionManager(logger, "unknown_program")49 ret = exec_manager.run_until_complete()50 logger.msg_proc.emit.assert_not_called()51 logger.msg_proc_error.emit.assert_not_called()52 logger.msg_standard_execution.emit.assert_called_once()53 self.assertEqual(ret, 1)54 def test_run_with_workdir(self):55 logger = MagicMock()56 logger.msg_proc = MagicMock()57 logger.msg_proc.attach_mock(MagicMock(), "emit")58 logger.msg_proc_error = MagicMock()59 logger.msg_proc_error.attach_mock(MagicMock(), "emit")60 with TemporaryDirectory() as workdir:61 exec_manager = ProcessExecutionManager(62 logger, "python", "-c", "import os; print(os.getcwd())", workdir=workdir63 )64 ret = exec_manager.run_until_complete()65 logger.msg_proc.emit.assert_called_once()66 logger.msg_proc.emit.assert_called_with(f"{workdir}")67 self.assertEqual(ret, 0)68class TestPersistentExecutionManager(unittest.TestCase):69 def test_reuse_process(self):70 logger = MagicMock()71 logger.msg_warning = MagicMock()72 exec_mngr1 = PythonPersistentExecutionManager(73 logger, ["python"], ['print("hello")'], "alias", group_id="SomeGroup"...
test_synchronizer.py
Source:test_synchronizer.py
...51 dvs_synchronizer,52 pi_synchronizer,53):54 order_checker = mock.Mock()55 order_checker.attach_mock(database, "database")56 order_checker.attach_mock(dvs_synchronizer, "dvs_synchronizer")57 order_checker.attach_mock(pi_synchronizer, "pi_synchronizer")58 order_checker.attach_mock(vm_synchronizer, "vm_synchronizer")59 order_checker.attach_mock(dpg_synchronizer, "dpg_synchronizer")60 order_checker.attach_mock(vpg_synchronizer, "vpg_synchronizer")61 order_checker.attach_mock(vmi_synchronizer, "vmi_synchronizer")62 synchronizer.sync()63 expected_order_calls = [64 mock.call.database.clear_database(),65 mock.call.dvs_synchronizer.sync(),66 mock.call.pi_synchronizer.sync(),67 mock.call.dpg_synchronizer.sync_create(),68 mock.call.vm_synchronizer.sync(),69 mock.call.vpg_synchronizer.sync_create(),70 mock.call.vmi_synchronizer.sync_create(),71 mock.call.vmi_synchronizer.sync_delete(),72 mock.call.vpg_synchronizer.sync_delete(),73 mock.call.dpg_synchronizer.sync_delete(),74 ]75 assert order_checker.mock_calls == expected_order_calls
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!!