Best Python code snippet using tempest_python
test_routers_rbac.py
Source:test_routers_rbac.py  
...256    @rbac_rule_validation.action(service="neutron",257                                 rule="delete_router",258                                 expected_error_code=404)259    @decorators.idempotent_id('c0634dd5-0467-48f7-a4ae-1014d8edb2a7')260    def test_delete_router(self):261        """Delete Router262        RBAC test for the neutron delete_router policy263        """264        router = self.create_router()265        self.rbac_utils.switch_role(self, toggle_rbac_role=True)266        self.routers_client.delete_router(router['id'])267    @rbac_rule_validation.action(service="neutron",268                                 rule="add_router_interface",269                                 expected_error_code=404)270    @decorators.idempotent_id('a0627778-d68d-4913-881b-e345360cca19')271    def test_add_router_interfaces(self):272        """Add Router Interface273        RBAC test for the neutron add_router_interface policy274        """275        network = self.create_network()276        subnet = self.create_subnet(network)277        router = self.create_router()278        self.rbac_utils.switch_role(self, toggle_rbac_role=True)279        self.routers_client.add_router_interface(280            router['id'], subnet_id=subnet['id'])...test_vpc.py
Source:test_vpc.py  
1# Copyright 20142# The Cloudscaling Group, Inc.3#4# Licensed under the Apache License, Version 2.0 (the "License");5# you may not use this file except in compliance with the License.6# You may obtain a copy of the License at7# http://www.apache.org/licenses/LICENSE-2.08#9# Unless required by applicable law or agreed to in writing, software10# distributed under the License is distributed on an "AS IS" BASIS,11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.12# See the License for the specific language governing permissions and13# limitations under the License.14import copy15import mock16from neutronclient.common import exceptions as neutron_exception17from ec2api.tests.unit import base18from ec2api.tests.unit import fakes19from ec2api.tests.unit import matchers20from ec2api.tests.unit import tools21class VpcTestCase(base.ApiTestCase):22    def test_create_vpc(self):23        self.neutron.create_router.side_effect = (24            tools.get_neutron_create('router', fakes.ID_OS_ROUTER_1))25        self.db_api.add_item.side_effect = (26            tools.get_db_api_add_item({27                'vpc': fakes.ID_EC2_VPC_1,28                'rtb': fakes.ID_EC2_ROUTE_TABLE_1,29                'sg': fakes.ID_EC2_SECURITY_GROUP_1}))30        self.set_mock_db_items(fakes.DB_VPC_1)31        self.nova.security_groups.create.return_value = (32            fakes.NovaSecurityGroup(fakes.NOVA_SECURITY_GROUP_1))33        def check_response(response):34            self.assertIn('vpc', response)35            vpc = resp['vpc']36            self.assertThat(fakes.EC2_VPC_1, matchers.DictMatches(vpc))37            self.neutron.create_router.assert_called_with({'router': {}})38            self.neutron.update_router.assert_called_once_with(39                fakes.ID_OS_ROUTER_1,40                {'router': {'name': fakes.EC2_VPC_1['vpcId']}})41            self.db_api.add_item.assert_any_call(42                mock.ANY, 'vpc',43                tools.purge_dict(fakes.DB_VPC_1,44                                 ('id', 'vpc_id', 'route_table_id')),45                project_id=None)46            self.db_api.add_item.assert_any_call(47                mock.ANY, 'rtb',48                tools.purge_dict(fakes.DB_ROUTE_TABLE_1,49                                 ('id',)),50                project_id=None)51            self.db_api.update_item.assert_called_once_with(52                mock.ANY,53                fakes.DB_VPC_1)54            self.neutron.reset_mock()55            self.db_api.reset_mock()56            self.db_api.update_item.reset_mock()57        resp = self.execute('CreateVpc', {'CidrBlock': fakes.CIDR_VPC_1})58        check_response(resp)59        resp = self.execute('CreateVpc', {'CidrBlock': fakes.CIDR_VPC_1,60                                          'instanceTenancy': 'default'})61        check_response(resp)62    def test_create_vpc_invalid_cidr(self):63        self.neutron.create_router.side_effect = (64            tools.get_neutron_create('router', fakes.ID_OS_ROUTER_1))65        self.db_api.add_item.side_effect = tools.get_db_api_add_item(66            fakes.ID_EC2_VPC_1)67        def do_check(args, error_code):68            self.assert_execution_error(error_code, 'CreateVpc', args)69            self.assertEqual(0, self.neutron.create_router.call_count)70            self.neutron.reset_mock()71            self.db_api.reset_mock()72        do_check({'CidrBlock': 'bad_cidr'}, 'InvalidParameterValue')73        do_check({'CidrBlock': '10.0.0.0/8'}, 'InvalidVpc.Range')74    def test_create_vpc_overlimit(self):75        self.neutron.create_router.side_effect = (76            neutron_exception.OverQuotaClient)77        self.db_api.add_item.side_effect = tools.get_db_api_add_item(78            fakes.ID_EC2_VPC_1)79        self.assert_execution_error('VpcLimitExceeded', 'CreateVpc',80                                    {'CidrBlock': fakes.CIDR_VPC_1})81        self.neutron.create_router.assert_called_with({'router': {}})82        self.assertEqual(0, self.db_api.add_item.call_count)83    @tools.screen_unexpected_exception_logs84    def test_create_vpc_rollback(self):85        self.neutron.create_router.side_effect = (86            tools.get_neutron_create('router', fakes.ID_OS_ROUTER_1))87        self.db_api.add_item.side_effect = (88            tools.get_db_api_add_item({89                'vpc': fakes.ID_EC2_VPC_1,90                'rtb': fakes.ID_EC2_ROUTE_TABLE_1}))91        self.neutron.update_router.side_effect = Exception()92        self.assert_execution_error(self.ANY_EXECUTE_ERROR, 'CreateVpc',93                                    {'CidrBlock': fakes.CIDR_VPC_1})94        self.neutron.delete_router.assert_called_once_with(95            fakes.ID_OS_ROUTER_1)96        self.db_api.delete_item.assert_any_call(mock.ANY, fakes.ID_EC2_VPC_1)97        self.db_api.delete_item.assert_any_call(mock.ANY,98                                                fakes.ID_EC2_ROUTE_TABLE_1)99    def test_delete_vpc(self):100        self.set_mock_db_items(fakes.DB_VPC_1, fakes.DB_ROUTE_TABLE_1,101                               fakes.DB_SECURITY_GROUP_1)102        resp = self.execute('DeleteVpc', {'VpcId': fakes.ID_EC2_VPC_1})103        self.assertEqual(True, resp['return'])104        self.neutron.delete_router.assert_called_once_with(105            fakes.ID_OS_ROUTER_1)106        self.db_api.delete_item.assert_any_call(107            mock.ANY,108            fakes.ID_EC2_VPC_1)109        self.db_api.delete_item.assert_any_call(110            mock.ANY,111            fakes.ID_EC2_ROUTE_TABLE_1)112        self.db_api.delete_item.assert_any_call(113            mock.ANY,114            fakes.ID_EC2_SECURITY_GROUP_1)115    def test_delete_vpc_not_found(self):116        self.set_mock_db_items()117        self.assert_execution_error('InvalidVpcID.NotFound', 'DeleteVpc',118                                    {'VpcId': fakes.ID_EC2_VPC_1})119        self.assertEqual(0, self.neutron.delete_router.call_count)120        self.assertEqual(0, self.db_api.delete_item.call_count)121    def test_delete_vpc_dependency_violation(self):122        def do_check():123            self.assert_execution_error('DependencyViolation', 'DeleteVpc',124                                        {'VpcId': fakes.ID_EC2_VPC_1})125            self.assertEqual(0, self.neutron.delete_router.call_count)126            self.assertEqual(0, self.db_api.delete_item.call_count)127            self.neutron.reset_mock()128            self.db_api.reset_mock()129        self.neutron.list_security_groups.return_value = (130            {'security_groups': [copy.deepcopy(fakes.OS_SECURITY_GROUP_1)]})131        self.set_mock_db_items(fakes.DB_SECURITY_GROUP_1,132                               fakes.DB_IGW_1, fakes.DB_VPC_1, )133        do_check()134        self.neutron.list_security_groups.return_value = (135            {'security_groups': [copy.deepcopy(fakes.OS_SECURITY_GROUP_1)]})136        self.set_mock_db_items(fakes.DB_SECURITY_GROUP_1,137                               fakes.DB_ROUTE_TABLE_1, fakes.DB_ROUTE_TABLE_2,138                               fakes.DB_VPC_1)139        do_check()140        self.set_mock_db_items(fakes.DB_SECURITY_GROUP_1,141                               fakes.DB_SECURITY_GROUP_2, fakes.DB_VPC_1)142        self.neutron.list_security_groups.return_value = (143            {'security_groups': [copy.deepcopy(fakes.OS_SECURITY_GROUP_1),144                                 fakes.OS_SECURITY_GROUP_2]})145        do_check()146    def test_delete_vpc_not_conststent_os_vpc(self):147        self.set_mock_db_items(fakes.DB_VPC_1, fakes.DB_ROUTE_TABLE_1)148        def check_response(resp):149            self.assertEqual(True, resp['return'])150            self.neutron.delete_router.assert_called_once_with(151                fakes.ID_OS_ROUTER_1)152            self.db_api.delete_item.assert_any_call(153                mock.ANY,154                fakes.ID_EC2_VPC_1)155            self.db_api.delete_item.assert_any_call(156                mock.ANY,157                fakes.ID_EC2_ROUTE_TABLE_1)158            self.neutron.reset_mock()159            self.db_api.reset_mock()160        self.neutron.delete_router.side_effect = neutron_exception.NotFound161        resp = self.execute('DeleteVpc', {'VpcId': fakes.ID_EC2_VPC_1})162        check_response(resp)163        self.neutron.delete_router.side_effect = neutron_exception.Conflict164        resp = self.execute('DeleteVpc', {'VpcId': fakes.ID_EC2_VPC_1})165        check_response(resp)166    @tools.screen_unexpected_exception_logs167    def test_delete_vpc_rollback(self):168        self.set_mock_db_items(fakes.DB_VPC_1, fakes.DB_ROUTE_TABLE_1)169        self.neutron.delete_router.side_effect = Exception()170        self.assert_execution_error(self.ANY_EXECUTE_ERROR, 'DeleteVpc',171                                    {'VpcId': fakes.ID_EC2_VPC_1})172        self.db_api.restore_item.assert_any_call(173            mock.ANY, 'vpc', fakes.DB_VPC_1)174        self.db_api.restore_item.assert_any_call(175            mock.ANY, 'rtb', fakes.DB_ROUTE_TABLE_1)176    def test_describe_vpcs(self):177        self.neutron.list_routers.return_value = (178            {'routers': [fakes.OS_ROUTER_1, fakes.OS_ROUTER_2]})179        self.set_mock_db_items(fakes.DB_VPC_1, fakes.DB_VPC_2)180        resp = self.execute('DescribeVpcs', {})181        self.assertThat(resp['vpcSet'],182                        matchers.ListMatches([fakes.EC2_VPC_1,183                                              fakes.EC2_VPC_2]))184        self.db_api.get_items.assert_called_once_with(mock.ANY, 'vpc')185        resp = self.execute('DescribeVpcs',186                            {'VpcId.1': fakes.ID_EC2_VPC_1})187        self.assertThat(resp['vpcSet'],188                        matchers.ListMatches([fakes.EC2_VPC_1]))189        self.db_api.get_items_by_ids.assert_called_once_with(190            mock.ANY, set([fakes.ID_EC2_VPC_1]))191        self.check_filtering(192            'DescribeVpcs', 'vpcSet',193            [('cidr', fakes.CIDR_VPC_1),194             ('dhcp-options-id', 'default'),195             ('is-default', False),196             ('state', 'available'),197             ('vpc-id', fakes.ID_EC2_VPC_1)])198        self.check_tag_support(199            'DescribeVpcs', 'vpcSet',200            fakes.ID_EC2_VPC_1, 'vpcId')201    def test_describe_vpcs_no_router(self):202        self.neutron.list_routers.return_value = {'routers': []}203        self.set_mock_db_items(fakes.DB_VPC_1)204        resp = self.execute('DescribeVpcs', {})205        self.assertThat(resp['vpcSet'],206                        matchers.ListMatches([fakes.EC2_VPC_1]))...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!!
