Best Python code snippet using tempest_python
test_flavors_extra_specs_negative.py
Source:test_flavors_extra_specs_negative.py  
1# Copyright 2012 OpenStack Foundation2# All Rights Reserved.3# Copyright 2013 IBM Corp.4#5#    Licensed under the Apache License, Version 2.0 (the "License"); you may6#    not use this file except in compliance with the License. You may obtain7#    a copy of the License at8#9#         http://www.apache.org/licenses/LICENSE-2.010#11#    Unless required by applicable law or agreed to in writing, software12#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT13#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the14#    License for the specific language governing permissions and limitations15#    under the License.16from tempest_lib import exceptions as lib_exc17from tempest.api.compute import base18from tempest.common.utils import data_utils19from tempest import test20class FlavorsExtraSpecsNegativeTestJSON(base.BaseV2ComputeAdminTest):21    """Negative Tests Flavor Extra Spec API extension.22    SET, UNSET, UPDATE Flavor Extra specs require admin privileges.23    """24    @classmethod25    def skip_checks(cls):26        super(FlavorsExtraSpecsNegativeTestJSON, cls).skip_checks()27        if not test.is_extension_enabled('OS-FLV-EXT-DATA', 'compute'):28            msg = "OS-FLV-EXT-DATA extension not enabled."29            raise cls.skipException(msg)30    @classmethod31    def setup_clients(cls):32        super(FlavorsExtraSpecsNegativeTestJSON, cls).setup_clients()33        cls.client = cls.os_adm.flavors_client34    @classmethod35    def resource_setup(cls):36        super(FlavorsExtraSpecsNegativeTestJSON, cls).resource_setup()37        flavor_name = data_utils.rand_name('test_flavor')38        ram = 51239        vcpus = 140        disk = 1041        ephemeral = 1042        cls.new_flavor_id = data_utils.rand_int_id(start=1000)43        swap = 102444        rxtx = 145        # Create a flavor46        cls.flavor = cls.client.create_flavor(name=flavor_name,47                                              ram=ram, vcpus=vcpus,48                                              disk=disk,49                                              id=cls.new_flavor_id,50                                              ephemeral=ephemeral,51                                              swap=swap,52                                              rxtx_factor=rxtx)['flavor']53    @classmethod54    def resource_cleanup(cls):55        cls.client.delete_flavor(cls.flavor['id'])56        cls.client.wait_for_resource_deletion(cls.flavor['id'])57        super(FlavorsExtraSpecsNegativeTestJSON, cls).resource_cleanup()58    @test.attr(type=['negative'])59    @test.idempotent_id('a00a3b81-5641-45a8-ab2b-4a8ec41e1d7d')60    def test_flavor_non_admin_set_keys(self):61        # Test to SET flavor extra spec as a user without admin privileges.62        self.assertRaises(lib_exc.Forbidden,63                          self.flavors_client.set_flavor_extra_spec,64                          self.flavor['id'],65                          key1="value1", key2="value2")66    @test.attr(type=['negative'])67    @test.idempotent_id('1ebf4ef8-759e-48fe-a801-d451d80476fb')68    def test_flavor_non_admin_update_specific_key(self):69        # non admin user is not allowed to update flavor extra spec70        body = self.client.set_flavor_extra_spec(71            self.flavor['id'], key1="value1", key2="value2")['extra_specs']72        self.assertEqual(body['key1'], 'value1')73        self.assertRaises(lib_exc.Forbidden,74                          self.flavors_client.75                          update_flavor_extra_spec,76                          self.flavor['id'],77                          'key1',78                          key1='value1_new')79    @test.attr(type=['negative'])80    @test.idempotent_id('28f12249-27c7-44c1-8810-1f382f316b11')81    def test_flavor_non_admin_unset_keys(self):82        self.client.set_flavor_extra_spec(self.flavor['id'],83                                          key1="value1", key2="value2")84        self.assertRaises(lib_exc.Forbidden,85                          self.flavors_client.unset_flavor_extra_spec,86                          self.flavor['id'],87                          'key1')88    @test.attr(type=['negative'])89    @test.idempotent_id('440b9f3f-3c7f-4293-a106-0ceda350f8de')90    def test_flavor_unset_nonexistent_key(self):91        nonexistent_key = data_utils.rand_name('flavor_key')92        self.assertRaises(lib_exc.NotFound,93                          self.client.unset_flavor_extra_spec,94                          self.flavor['id'],95                          nonexistent_key)96    @test.attr(type=['negative'])97    @test.idempotent_id('329a7be3-54b2-48be-8052-bf2ce4afd898')98    def test_flavor_get_nonexistent_key(self):99        self.assertRaises(lib_exc.NotFound,100                          self.flavors_client.show_flavor_extra_spec,101                          self.flavor['id'],102                          "nonexistent_key")103    @test.attr(type=['negative'])104    @test.idempotent_id('25b822b8-9f49-44f6-80de-d99f0482e5cb')105    def test_flavor_update_mismatch_key(self):106        # the key will be updated should be match the key in the body107        self.assertRaises(lib_exc.BadRequest,108                          self.client.update_flavor_extra_spec,109                          self.flavor['id'],110                          "key2",111                          key1="value")112    @test.attr(type=['negative'])113    @test.idempotent_id('f5889590-bf66-41cc-b4b1-6e6370cfd93f')114    def test_flavor_update_more_key(self):115        # there should be just one item in the request body116        self.assertRaises(lib_exc.BadRequest,117                          self.client.update_flavor_extra_spec,118                          self.flavor['id'],119                          "key1",120                          key1="value",...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!!
