Best Python code snippet using tempest_python
test_security_groups.py
Source:test_security_groups.py  
...25        super(SecGroupTest, cls).resource_setup()26        if not test.is_extension_enabled('security-group', 'network'):27            msg = "security-group extension not enabled."28            raise cls.skipException(msg)29    def _create_verify_security_group_rule(self, sg_id, direction,30                                           ethertype, protocol,31                                           port_range_min,32                                           port_range_max,33                                           remote_group_id=None,34                                           remote_ip_prefix=None):35        # Create Security Group rule with the input params and validate36        # that SG rule is created with the same parameters.37        rule_create_body = self.client.create_security_group_rule(38            security_group_id=sg_id,39            direction=direction,40            ethertype=ethertype,41            protocol=protocol,42            port_range_min=port_range_min,43            port_range_max=port_range_max,44            remote_group_id=remote_group_id,45            remote_ip_prefix=remote_ip_prefix46        )47        sec_group_rule = rule_create_body['security_group_rule']48        self.addCleanup(self._delete_security_group_rule,49                        sec_group_rule['id'])50        expected = {'direction': direction, 'protocol': protocol,51                    'ethertype': ethertype, 'port_range_min': port_range_min,52                    'port_range_max': port_range_max,53                    'remote_group_id': remote_group_id,54                    'remote_ip_prefix': remote_ip_prefix}55        for key, value in six.iteritems(expected):56            self.assertEqual(value, sec_group_rule[key],57                             "Field %s of the created security group "58                             "rule does not match with %s." %59                             (key, value))60    @test.attr(type='smoke')61    @test.idempotent_id('e30abd17-fef9-4739-8617-dc26da88e686')62    def test_list_security_groups(self):63        # Verify the that security group belonging to tenant exist in list64        body = self.client.list_security_groups()65        security_groups = body['security_groups']66        found = None67        for n in security_groups:68            if (n['name'] == 'default'):69                found = n['id']70        msg = "Security-group list doesn't contain default security-group"71        self.assertIsNotNone(found, msg)72    @test.attr(type='smoke')73    @test.idempotent_id('bfd128e5-3c92-44b6-9d66-7fe29d22c802')74    def test_create_list_update_show_delete_security_group(self):75        group_create_body, name = self._create_security_group()76        # List security groups and verify if created group is there in response77        list_body = self.client.list_security_groups()78        secgroup_list = list()79        for secgroup in list_body['security_groups']:80            secgroup_list.append(secgroup['id'])81        self.assertIn(group_create_body['security_group']['id'], secgroup_list)82        # Update the security group83        new_name = data_utils.rand_name('security-')84        new_description = data_utils.rand_name('security-description')85        update_body = self.client.update_security_group(86            group_create_body['security_group']['id'],87            name=new_name,88            description=new_description)89        # Verify if security group is updated90        self.assertEqual(update_body['security_group']['name'], new_name)91        self.assertEqual(update_body['security_group']['description'],92                         new_description)93        # Show details of the updated security group94        show_body = self.client.show_security_group(95            group_create_body['security_group']['id'])96        self.assertEqual(show_body['security_group']['name'], new_name)97        self.assertEqual(show_body['security_group']['description'],98                         new_description)99    @test.attr(type='smoke')100    @test.idempotent_id('cfb99e0e-7410-4a3d-8a0c-959a63ee77e9')101    def test_create_show_delete_security_group_rule(self):102        group_create_body, _ = self._create_security_group()103        # Create rules for each protocol104        protocols = ['tcp', 'udp', 'icmp']105        for protocol in protocols:106            rule_create_body = self.client.create_security_group_rule(107                security_group_id=group_create_body['security_group']['id'],108                protocol=protocol,109                direction='ingress',110                ethertype=self.ethertype111            )112            # Show details of the created security rule113            show_rule_body = self.client.show_security_group_rule(114                rule_create_body['security_group_rule']['id']115            )116            create_dict = rule_create_body['security_group_rule']117            for key, value in six.iteritems(create_dict):118                self.assertEqual(value,119                                 show_rule_body['security_group_rule'][key],120                                 "%s does not match." % key)121            # List rules and verify created rule is in response122            rule_list_body = self.client.list_security_group_rules()123            rule_list = [rule['id']124                         for rule in rule_list_body['security_group_rules']]125            self.assertIn(rule_create_body['security_group_rule']['id'],126                          rule_list)127    @test.attr(type='smoke')128    @test.idempotent_id('87dfbcf9-1849-43ea-b1e4-efa3eeae9f71')129    def test_create_security_group_rule_with_additional_args(self):130        """Verify security group rule with additional arguments works.131        direction:ingress, ethertype:[IPv4/IPv6],132        protocol:tcp, port_range_min:77, port_range_max:77133        """134        group_create_body, _ = self._create_security_group()135        sg_id = group_create_body['security_group']['id']136        direction = 'ingress'137        protocol = 'tcp'138        port_range_min = 77139        port_range_max = 77140        self._create_verify_security_group_rule(sg_id, direction,141                                                self.ethertype, protocol,142                                                port_range_min,143                                                port_range_max)144    @test.attr(type='smoke')145    @test.idempotent_id('c9463db8-b44d-4f52-b6c0-8dbda99f26ce')146    def test_create_security_group_rule_with_icmp_type_code(self):147        """Verify security group rule for icmp protocol works.148        Specify icmp type (port_range_min) and icmp code149        (port_range_max) with different values. A separate testcase150        is added for icmp protocol as icmp validation would be151        different from tcp/udp.152        """153        group_create_body, _ = self._create_security_group()154        sg_id = group_create_body['security_group']['id']155        direction = 'ingress'156        protocol = 'icmp'157        icmp_type_codes = [(3, 2), (3, 0), (8, 0), (0, 0), (11, None)]158        for icmp_type, icmp_code in icmp_type_codes:159            self._create_verify_security_group_rule(sg_id, direction,160                                                    self.ethertype, protocol,161                                                    icmp_type, icmp_code)162    @test.attr(type='smoke')163    @test.idempotent_id('c2ed2deb-7a0c-44d8-8b4c-a5825b5c310b')164    def test_create_security_group_rule_with_remote_group_id(self):165        # Verify creating security group rule with remote_group_id works166        sg1_body, _ = self._create_security_group()167        sg2_body, _ = self._create_security_group()168        sg_id = sg1_body['security_group']['id']169        direction = 'ingress'170        protocol = 'udp'171        port_range_min = 50172        port_range_max = 55173        remote_id = sg2_body['security_group']['id']174        self._create_verify_security_group_rule(sg_id, direction,175                                                self.ethertype, protocol,176                                                port_range_min,177                                                port_range_max,178                                                remote_group_id=remote_id)179    @test.attr(type='smoke')180    @test.idempotent_id('16459776-5da2-4634-bce4-4b55ee3ec188')181    def test_create_security_group_rule_with_remote_ip_prefix(self):182        # Verify creating security group rule with remote_ip_prefix works183        sg1_body, _ = self._create_security_group()184        sg_id = sg1_body['security_group']['id']185        direction = 'ingress'186        protocol = 'tcp'187        port_range_min = 76188        port_range_max = 77189        ip_prefix = self._tenant_network_cidr190        self._create_verify_security_group_rule(sg_id, direction,191                                                self.ethertype, protocol,192                                                port_range_min,193                                                port_range_max,194                                                remote_ip_prefix=ip_prefix)195    @test.attr(type='smoke')196    @test.idempotent_id('0a307599-6655-4220-bebc-fd70c64f2290')197    def test_create_security_group_rule_with_protocol_integer_value(self):198        # Verify creating security group rule with the199        # protocol as integer value200        # arguments : "protocol": 17201        group_create_body, _ = self._create_security_group()202        direction = 'ingress'203        protocol = 17204        security_group_id = group_create_body['security_group']['id']...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!!
