Best Python code snippet using tempest_python
test_server_actions.py
Source:test_server_actions.py
...187 raise Exception('hurr durr')188 self.stubs.Set(nova.compute.api.API, 'resize', resize_mock)189 res = req.get_response(fakes.wsgi_app())190 self.assertEqual(res.status_int, 500)191 def test_confirm_resize_server(self):192 req = self.webreq('/1/action', 'POST', dict(confirmResize=None))193 self.resize_called = False194 def confirm_resize_mock(*args):195 self.resize_called = True196 self.stubs.Set(nova.compute.api.API, 'confirm_resize',197 confirm_resize_mock)198 res = req.get_response(fakes.wsgi_app())199 self.assertEqual(res.status_int, 204)200 self.assertEqual(self.resize_called, True)201 def test_confirm_resize_server_fails(self):202 req = self.webreq('/1/action', 'POST', dict(confirmResize=None))203 def confirm_resize_mock(*args):204 raise Exception('hurr durr')205 self.stubs.Set(nova.compute.api.API, 'confirm_resize',206 confirm_resize_mock)207 res = req.get_response(fakes.wsgi_app())208 self.assertEqual(res.status_int, 400)209 def test_confirm_resize_migration_not_found(self):210 req = self.webreq('/1/action', 'POST', dict(confirmResize=None))211 def confirm_resize_mock(*args):212 raise exception.MigrationNotFoundByStatus(instance_id=1,213 status='finished')214 self.stubs.Set(nova.compute.api.API,215 'confirm_resize',216 confirm_resize_mock)217 res = req.get_response(fakes.wsgi_app())218 self.assertEqual(res.status_int, 400)219 def test_revert_resize_server(self):220 req = self.webreq('/1/action', 'POST', dict(revertResize=None))221 self.resize_called = False222 def revert_resize_mock(*args):223 self.resize_called = True224 self.stubs.Set(nova.compute.api.API, 'revert_resize',225 revert_resize_mock)226 res = req.get_response(fakes.wsgi_app())227 self.assertEqual(res.status_int, 202)228 self.assertEqual(self.resize_called, True)229 def test_revert_resize_server_fails(self):230 req = self.webreq('/1/action', 'POST', dict(revertResize=None))231 def revert_resize_mock(*args):232 raise Exception('hurr durr')233 self.stubs.Set(nova.compute.api.API, 'revert_resize',234 revert_resize_mock)235 res = req.get_response(fakes.wsgi_app())236 self.assertEqual(res.status_int, 400)237 def test_migrate_server(self):238 """This is basically the same as resize, only we provide the `migrate`239 attribute in the body's dict.240 """241 req = self.webreq('/1/migrate', 'POST')242 self.resize_called = False243 def resize_mock(*args):244 self.resize_called = True245 self.stubs.Set(nova.compute.api.API, 'resize', resize_mock)246 res = req.get_response(fakes.wsgi_app())247 self.assertEqual(res.status_int, 202)248 self.assertEqual(self.resize_called, True)249 def test_revert_resize_migration_not_found(self):250 req = self.webreq('/1/action', 'POST', dict(revertResize=None))251 def revert_resize_mock(*args):252 raise exception.MigrationNotFoundByStatus(instance_id=1,253 status='finished')254 self.stubs.Set(nova.compute.api.API,255 'revert_resize',256 revert_resize_mock)257 res = req.get_response(fakes.wsgi_app())258 self.assertEqual(res.status_int, 400)259 def test_create_backup(self):260 """The happy path for creating backups"""261 self.flags(allow_admin_api=True)262 body = {263 'createBackup': {264 'name': 'Backup 1',265 'backup_type': 'daily',266 'rotation': 1,267 },268 }269 req = webob.Request.blank('/v1.0/servers/1/action')270 req.method = 'POST'271 req.body = json.dumps(body)272 req.headers["content-type"] = "application/json"273 response = req.get_response(fakes.wsgi_app())274 self.assertEqual(202, response.status_int)275 self.assertTrue(response.headers['Location'])276 def test_create_backup_admin_api_off(self):277 """The happy path for creating backups"""278 self.flags(allow_admin_api=False)279 body = {280 'createBackup': {281 'name': 'Backup 1',282 'backup_type': 'daily',283 'rotation': 1,284 },285 }286 req = webob.Request.blank('/v1.0/servers/1/action')287 req.method = 'POST'288 req.body = json.dumps(body)289 req.headers["content-type"] = "application/json"290 response = req.get_response(fakes.wsgi_app())291 self.assertEqual(400, response.status_int)292 def test_create_backup_with_metadata(self):293 self.flags(allow_admin_api=True)294 body = {295 'createBackup': {296 'name': 'Backup 1',297 'backup_type': 'daily',298 'rotation': 1,299 'metadata': {'123': 'asdf'},300 },301 }302 req = webob.Request.blank('/v1.0/servers/1/action')303 req.method = 'POST'304 req.body = json.dumps(body)305 req.headers["content-type"] = "application/json"306 response = req.get_response(fakes.wsgi_app())307 self.assertEqual(202, response.status_int)308 self.assertTrue(response.headers['Location'])309 def test_create_backup_with_too_much_metadata(self):310 self.flags(allow_admin_api=True)311 body = {312 'createBackup': {313 'name': 'Backup 1',314 'backup_type': 'daily',315 'rotation': 1,316 'metadata': {'123': 'asdf'},317 },318 }319 for num in range(FLAGS.quota_metadata_items + 1):320 body['createBackup']['metadata']['foo%i' % num] = "bar"321 req = webob.Request.blank('/v1.0/servers/1/action')322 req.method = 'POST'323 req.body = json.dumps(body)324 req.headers["content-type"] = "application/json"325 response = req.get_response(fakes.wsgi_app())326 self.assertEqual(413, response.status_int)327 def test_create_backup_no_name(self):328 """Name is required for backups"""329 self.flags(allow_admin_api=True)330 body = {331 'createBackup': {332 'backup_type': 'daily',333 'rotation': 1,334 },335 }336 req = webob.Request.blank('/v1.0/images')337 req.method = 'POST'338 req.body = json.dumps(body)339 req.headers["content-type"] = "application/json"340 response = req.get_response(fakes.wsgi_app())341 self.assertEqual(400, response.status_int)342 def test_create_backup_no_rotation(self):343 """Rotation is required for backup requests"""344 self.flags(allow_admin_api=True)345 body = {346 'createBackup': {347 'name': 'Backup 1',348 'backup_type': 'daily',349 },350 }351 req = webob.Request.blank('/v1.0/images')352 req.method = 'POST'353 req.body = json.dumps(body)354 req.headers["content-type"] = "application/json"355 response = req.get_response(fakes.wsgi_app())356 self.assertEqual(400, response.status_int)357 def test_create_backup_no_backup_type(self):358 """Backup Type (daily or weekly) is required for backup requests"""359 self.flags(allow_admin_api=True)360 body = {361 'createBackup': {362 'name': 'Backup 1',363 'rotation': 1,364 },365 }366 req = webob.Request.blank('/v1.0/images')367 req.method = 'POST'368 req.body = json.dumps(body)369 req.headers["content-type"] = "application/json"370 response = req.get_response(fakes.wsgi_app())371 self.assertEqual(400, response.status_int)372 def test_create_backup_bad_entity(self):373 self.flags(allow_admin_api=True)374 body = {'createBackup': 'go'}375 req = webob.Request.blank('/v1.0/images')376 req.method = 'POST'377 req.body = json.dumps(body)378 req.headers["content-type"] = "application/json"379 response = req.get_response(fakes.wsgi_app())380 self.assertEqual(400, response.status_int)381class ServerActionsTestV11(test.TestCase):382 def setUp(self):383 self.maxDiff = None384 super(ServerActionsTestV11, self).setUp()385 self.stubs = stubout.StubOutForTesting()386 fakes.stub_out_auth(self.stubs)387 self.stubs.Set(nova.db.api, 'instance_get', return_server_by_id)388 self.stubs.Set(nova.db.api, 'instance_update', instance_update)389 fakes.stub_out_glance(self.stubs)390 fakes.stub_out_compute_api_snapshot(self.stubs)391 service_class = 'nova.image.glance.GlanceImageService'392 self.service = utils.import_object(service_class)393 self.context = context.RequestContext(1, None)394 self.service.delete_all()395 self.sent_to_glance = {}396 fakes.stub_out_glance_add_image(self.stubs, self.sent_to_glance)397 self.flags(allow_instance_snapshots=True)398 def tearDown(self):399 self.stubs.UnsetAll()400 def test_server_bad_body(self):401 body = {}402 req = webob.Request.blank('/v1.1/fake/servers/1/action')403 req.method = 'POST'404 req.content_type = 'application/json'405 req.body = json.dumps(body)406 res = req.get_response(fakes.wsgi_app())407 self.assertEqual(res.status_int, 400)408 def test_server_unknown_action(self):409 body = {'sockTheFox': {'fakekey': '1234'}}410 req = webob.Request.blank('/v1.1/fake/servers/1/action')411 req.method = 'POST'412 req.content_type = 'application/json'413 req.body = json.dumps(body)414 res = req.get_response(fakes.wsgi_app())415 self.assertEqual(res.status_int, 400)416 def test_server_change_password(self):417 mock_method = MockSetAdminPassword()418 self.stubs.Set(nova.compute.api.API, 'set_admin_password', mock_method)419 body = {'changePassword': {'adminPass': '1234pass'}}420 req = webob.Request.blank('/v1.1/fake/servers/1/action')421 req.method = 'POST'422 req.content_type = 'application/json'423 req.body = json.dumps(body)424 res = req.get_response(fakes.wsgi_app())425 self.assertEqual(res.status_int, 202)426 self.assertEqual(mock_method.instance_id, '1')427 self.assertEqual(mock_method.password, '1234pass')428 def test_server_change_password_xml(self):429 mock_method = MockSetAdminPassword()430 self.stubs.Set(nova.compute.api.API, 'set_admin_password', mock_method)431 req = webob.Request.blank('/v1.1/fake/servers/1/action')432 req.method = 'POST'433 req.content_type = "application/xml"434 req.body = """<?xml version="1.0" encoding="UTF-8"?>435 <changePassword436 xmlns="http://docs.openstack.org/compute/api/v1.1"437 adminPass="1234pass"/>"""438 res = req.get_response(fakes.wsgi_app())439 self.assertEqual(res.status_int, 202)440 self.assertEqual(mock_method.instance_id, '1')441 self.assertEqual(mock_method.password, '1234pass')442 def test_server_change_password_not_a_string(self):443 body = {'changePassword': {'adminPass': 1234}}444 req = webob.Request.blank('/v1.1/fake/servers/1/action')445 req.method = 'POST'446 req.content_type = 'application/json'447 req.body = json.dumps(body)448 res = req.get_response(fakes.wsgi_app())449 self.assertEqual(res.status_int, 400)450 def test_server_change_password_bad_request(self):451 body = {'changePassword': {'pass': '12345'}}452 req = webob.Request.blank('/v1.1/fake/servers/1/action')453 req.method = 'POST'454 req.content_type = 'application/json'455 req.body = json.dumps(body)456 res = req.get_response(fakes.wsgi_app())457 self.assertEqual(res.status_int, 400)458 def test_server_change_password_empty_string(self):459 body = {'changePassword': {'adminPass': ''}}460 req = webob.Request.blank('/v1.1/fake/servers/1/action')461 req.method = 'POST'462 req.content_type = 'application/json'463 req.body = json.dumps(body)464 res = req.get_response(fakes.wsgi_app())465 self.assertEqual(res.status_int, 400)466 def test_server_change_password_none(self):467 body = {'changePassword': {'adminPass': None}}468 req = webob.Request.blank('/v1.1/fake/servers/1/action')469 req.method = 'POST'470 req.content_type = 'application/json'471 req.body = json.dumps(body)472 res = req.get_response(fakes.wsgi_app())473 self.assertEqual(res.status_int, 400)474 def test_server_reboot_hard(self):475 body = dict(reboot=dict(type="HARD"))476 req = webob.Request.blank('/v1.1/fake/servers/1/action')477 req.method = 'POST'478 req.content_type = 'application/json'479 req.body = json.dumps(body)480 res = req.get_response(fakes.wsgi_app())481 self.assertEqual(res.status_int, 202)482 def test_server_reboot_soft(self):483 body = dict(reboot=dict(type="SOFT"))484 req = webob.Request.blank('/v1.1/fake/servers/1/action')485 req.method = 'POST'486 req.content_type = 'application/json'487 req.body = json.dumps(body)488 res = req.get_response(fakes.wsgi_app())489 self.assertEqual(res.status_int, 202)490 def test_server_reboot_incorrect_type(self):491 body = dict(reboot=dict(type="NOT_A_TYPE"))492 req = webob.Request.blank('/v1.1/fake/servers/1/action')493 req.method = 'POST'494 req.content_type = 'application/json'495 req.body = json.dumps(body)496 res = req.get_response(fakes.wsgi_app())497 self.assertEqual(res.status_int, 400)498 def test_server_reboot_missing_type(self):499 body = dict(reboot=dict())500 req = webob.Request.blank('/v1.1/fake/servers/1/action')501 req.method = 'POST'502 req.content_type = 'application/json'503 req.body = json.dumps(body)504 res = req.get_response(fakes.wsgi_app())505 self.assertEqual(res.status_int, 400)506 def test_server_rebuild_accepted_minimum(self):507 new_return_server = return_server_with_attributes(image_ref='2')508 self.stubs.Set(nova.db.api, 'instance_get', new_return_server)509 body = {510 "rebuild": {511 "imageRef": "http://localhost/images/2",512 },513 }514 req = webob.Request.blank('/v1.1/fake/servers/1/action')515 req.method = 'POST'516 req.content_type = 'application/json'517 req.body = json.dumps(body)518 res = req.get_response(fakes.wsgi_app())519 self.assertEqual(res.status_int, 202)520 body = json.loads(res.body)521 self.assertEqual(body['server']['image']['id'], '2')522 self.assertEqual(len(body['server']['adminPass']), 16)523 def test_server_rebuild_rejected_when_building(self):524 body = {525 "rebuild": {526 "imageRef": "http://localhost/images/2",527 },528 }529 state = vm_states.BUILDING530 new_return_server = return_server_with_state(state)531 self.stubs.Set(nova.db.api, 'instance_get', new_return_server)532 self.stubs.Set(nova.db, 'instance_get_by_uuid',533 return_server_with_uuid_and_state(state))534 req = webob.Request.blank('/v1.1/fake/servers/1/action')535 req.method = 'POST'536 req.content_type = 'application/json'537 req.body = json.dumps(body)538 res = req.get_response(fakes.wsgi_app())539 self.assertEqual(res.status_int, 409)540 def test_server_rebuild_accepted_with_metadata(self):541 metadata = {'new': 'metadata'}542 new_return_server = return_server_with_attributes(metadata=metadata)543 self.stubs.Set(nova.db.api, 'instance_get', new_return_server)544 body = {545 "rebuild": {546 "imageRef": "http://localhost/images/2",547 "metadata": metadata,548 },549 }550 req = webob.Request.blank('/v1.1/fake/servers/1/action')551 req.method = 'POST'552 req.content_type = 'application/json'553 req.body = json.dumps(body)554 res = req.get_response(fakes.wsgi_app())555 self.assertEqual(res.status_int, 202)556 body = json.loads(res.body)557 self.assertEqual(body['server']['metadata'], metadata)558 def test_server_rebuild_accepted_with_bad_metadata(self):559 body = {560 "rebuild": {561 "imageRef": "http://localhost/images/2",562 "metadata": "stack",563 },564 }565 req = webob.Request.blank('/v1.1/fake/servers/1/action')566 req.method = 'POST'567 req.content_type = 'application/json'568 req.body = json.dumps(body)569 res = req.get_response(fakes.wsgi_app())570 self.assertEqual(res.status_int, 400)571 def test_server_rebuild_bad_entity(self):572 body = {573 "rebuild": {574 "imageId": 2,575 },576 }577 req = webob.Request.blank('/v1.1/fake/servers/1/action')578 req.method = 'POST'579 req.content_type = 'application/json'580 req.body = json.dumps(body)581 res = req.get_response(fakes.wsgi_app())582 self.assertEqual(res.status_int, 400)583 def test_server_rebuild_bad_personality(self):584 body = {585 "rebuild": {586 "imageRef": "http://localhost/images/2",587 "personality": [{588 "path": "/path/to/file",589 "contents": "INVALID b64",590 }]591 },592 }593 req = webob.Request.blank('/v1.1/fake/servers/1/action')594 req.method = 'POST'595 req.content_type = 'application/json'596 req.body = json.dumps(body)597 res = req.get_response(fakes.wsgi_app())598 self.assertEqual(res.status_int, 400)599 def test_server_rebuild_personality(self):600 body = {601 "rebuild": {602 "imageRef": "http://localhost/images/2",603 "personality": [{604 "path": "/path/to/file",605 "contents": base64.b64encode("Test String"),606 }]607 },608 }609 req = webob.Request.blank('/v1.1/fake/servers/1/action')610 req.method = 'POST'611 req.content_type = 'application/json'612 req.body = json.dumps(body)613 res = req.get_response(fakes.wsgi_app())614 self.assertEqual(res.status_int, 202)615 body = json.loads(res.body)616 self.assertTrue('personality' not in body['server'])617 def test_server_rebuild_admin_pass(self):618 new_return_server = return_server_with_attributes(image_ref='2')619 self.stubs.Set(nova.db.api, 'instance_get', new_return_server)620 body = {621 "rebuild": {622 "imageRef": "http://localhost/images/2",623 "adminPass": "asdf",624 },625 }626 req = webob.Request.blank('/v1.1/fake/servers/1/action')627 req.method = 'POST'628 req.content_type = 'application/json'629 req.body = json.dumps(body)630 res = req.get_response(fakes.wsgi_app())631 self.assertEqual(res.status_int, 202)632 body = json.loads(res.body)633 self.assertEqual(body['server']['image']['id'], '2')634 self.assertEqual(body['server']['adminPass'], 'asdf')635 def test_server_rebuild_server_not_found(self):636 def server_not_found(self, instance_id):637 raise exception.InstanceNotFound(instance_id=instance_id)638 self.stubs.Set(nova.db.api, 'instance_get', server_not_found)639 body = {640 "rebuild": {641 "imageRef": "http://localhost/images/2",642 },643 }644 req = webob.Request.blank('/v1.1/fake/servers/1/action')645 req.method = 'POST'646 req.content_type = 'application/json'647 req.body = json.dumps(body)648 res = req.get_response(fakes.wsgi_app())649 self.assertEqual(res.status_int, 404)650 def test_resize_server(self):651 req = webob.Request.blank('/v1.1/fake/servers/1/action')652 req.content_type = 'application/json'653 req.method = 'POST'654 body_dict = dict(resize=dict(flavorRef="http://localhost/3"))655 req.body = json.dumps(body_dict)656 self.resize_called = False657 def resize_mock(*args):658 self.resize_called = True659 self.stubs.Set(nova.compute.api.API, 'resize', resize_mock)660 res = req.get_response(fakes.wsgi_app())661 self.assertEqual(res.status_int, 202)662 self.assertEqual(self.resize_called, True)663 def test_resize_server_no_flavor(self):664 req = webob.Request.blank('/v1.1/fake/servers/1/action')665 req.content_type = 'application/json'666 req.method = 'POST'667 body_dict = dict(resize=dict())668 req.body = json.dumps(body_dict)669 res = req.get_response(fakes.wsgi_app())670 self.assertEqual(res.status_int, 400)671 def test_resize_server_no_flavor_ref(self):672 req = webob.Request.blank('/v1.1/fake/servers/1/action')673 req.content_type = 'application/json'674 req.method = 'POST'675 body_dict = dict(resize=dict(flavorRef=None))676 req.body = json.dumps(body_dict)677 res = req.get_response(fakes.wsgi_app())678 self.assertEqual(res.status_int, 400)679 def test_confirm_resize_server(self):680 req = webob.Request.blank('/v1.1/fake/servers/1/action')681 req.content_type = 'application/json'682 req.method = 'POST'683 body_dict = dict(confirmResize=None)684 req.body = json.dumps(body_dict)685 self.confirm_resize_called = False686 def cr_mock(*args):687 self.confirm_resize_called = True688 self.stubs.Set(nova.compute.api.API, 'confirm_resize', cr_mock)689 res = req.get_response(fakes.wsgi_app())690 self.assertEqual(res.status_int, 204)691 self.assertEqual(self.confirm_resize_called, True)692 def test_revert_resize_server(self):693 req = webob.Request.blank('/v1.1/fake/servers/1/action')...
test_server_actions_rbac.py
Source:test_server_actions_rbac.py
...119 def _test_revert_resize_server(self):120 self.servers_client.revert_resize_server(self.server_id)121 waiters.wait_for_server_status(122 self.os_admin.servers_client, self.server_id, 'ACTIVE')123 def _test_confirm_resize_server(self):124 self.servers_client.confirm_resize_server(self.server_id)125 waiters.wait_for_server_status(126 self.os_admin.servers_client, self.server_id, 'ACTIVE')127 @rbac_rule_validation.action(128 service="nova",129 rule="os_compute_api:servers:stop")130 @decorators.idempotent_id('ab4a17d2-166f-4a6d-9944-f17baa576cf2')131 def test_stop_server(self):132 self.rbac_utils.switch_role(self, toggle_rbac_role=True)133 self._test_stop_server()134 @test.attr(type='slow')135 @rbac_rule_validation.action(136 service="nova",137 rule="os_compute_api:servers:start")138 @decorators.idempotent_id('8876bfa9-4d10-406e-a335-a57e451abb12')139 def test_start_server(self):140 self._test_stop_server()141 self.rbac_utils.switch_role(self, toggle_rbac_role=True)142 self._test_start_server()143 @test.attr(type='slow')144 @rbac_rule_validation.action(145 service="nova",146 rule="os_compute_api:servers:resize")147 @decorators.idempotent_id('0546fbdd-2d8f-4ce8-ac00-f1e2129d0765')148 @testtools.skipUnless(CONF.compute_feature_enabled.resize,149 'Resize is not available.')150 def test_resize_server(self):151 self.rbac_utils.switch_role(self, toggle_rbac_role=True)152 self._test_resize_server(self.flavor_ref_alt)153 @test.attr(type='slow')154 @rbac_rule_validation.action(155 service="nova",156 rule="os_compute_api:servers:revert_resize")157 @decorators.idempotent_id('d41b64b8-a72d-414a-a4c5-94e1eb5e5a96')158 @testtools.skipUnless(CONF.compute_feature_enabled.resize,159 'Resize is not available.')160 def test_revert_resize_server(self):161 self._test_resize_server(self.flavor_ref_alt)162 self.rbac_utils.switch_role(self, toggle_rbac_role=True)163 self._test_revert_resize_server()164 @test.attr(type='slow')165 @rbac_rule_validation.action(166 service="nova",167 rule="os_compute_api:servers:confirm_resize")168 @decorators.idempotent_id('f51620cb-dfcb-4e5d-b421-2e0edaa1316e')169 @testtools.skipUnless(CONF.compute_feature_enabled.resize,170 'Resize is not available.')171 def test_confirm_resize_server(self):172 self._test_resize_server(self.flavor_ref_alt)173 self.rbac_utils.switch_role(self, toggle_rbac_role=True)174 self.addCleanup(175 lambda: (self._test_resize_server(self.flavor_ref),176 self._test_confirm_resize_server())177 )178 self._test_confirm_resize_server()179 @rbac_rule_validation.action(180 service="nova",181 rule="os_compute_api:servers:rebuild")182 @decorators.idempotent_id('54b1a30b-c96c-472c-9c83-ccaf6ec7e20b')183 def test_rebuild_server(self):184 self.rbac_utils.switch_role(self, toggle_rbac_role=True)185 self.servers_client.rebuild_server(self.server_id, self.image_ref)186 waiters.wait_for_server_status(187 self.os_admin.servers_client, self.server_id, 'ACTIVE')188 @rbac_rule_validation.action(189 service="nova",190 rule="os_compute_api:servers:reboot")191 @decorators.idempotent_id('19f27856-56e1-44f8-8615-7257f6b85cbb')192 def test_reboot_server(self):...
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!!