Best Python code snippet using tempest_python
base_security_groups.py
Source:base_security_groups.py
...48 def _create_security_group_rule(self, **kwargs):49 rule_create_body = self.client.create_security_group_rule(**kwargs)50 # List rules and verify created rule is in response51 rule_list_body = (52 self.client.list_security_group_rules())53 rule_list = [rule['id']54 for rule in rule_list_body['security_group_rules']]55 self.assertIn(rule_create_body['security_group_rule']['id'],56 rule_list)57 self.addCleanup(self._delete_security_group_rule,58 rule_create_body['security_group_rule']['id'])59 return rule_create_body60 def _show_security_group_rule(self, **kwargs):61 show_rule_body = self.client.show_security_group_rule(kwargs['id'])62 for key, value in kwargs.items():63 self.assertEqual(value,64 show_rule_body['security_group_rule'][key],65 "%s does not match." % key)66 def _delete_security_group_rule(self, secgroup_rule_id):67 self.client.delete_security_group_rule(secgroup_rule_id)68 rule_list_body = self.client.list_security_group_rules()69 rule_list = [rule['id']70 for rule in rule_list_body['security_group_rules']]71 self.assertNotIn(secgroup_rule_id, rule_list)72 def _test_create_show_delete_security_group_rule(self, **kwargs):73 # The security group rule is deleted by the cleanup call in74 # _create_security_group_rule.75 rule_create_body = (76 self._create_security_group_rule(**kwargs)['security_group_rule'])77 self._show_security_group_rule(78 id=rule_create_body['id'],79 protocol=rule_create_body['protocol'],80 direction=rule_create_body['direction'],...
SecurityGroupClassDef.py
Source:SecurityGroupClassDef.py
1# Security_Group class definition:2# TODO Test passing a list_Security_Group_Rules value into init3class Security_Group:4 def __init__(self,key_name='',group_name='',vpc_id='',group_id='',description='',list_Security_Group_Rules=None):5 self.key_name = key_name6 self.group_name = group_name7 print(group_name)8 self.vpc_id = vpc_id9 self.group_id = group_id10 self.description = description11 self.list_Security_Group_Rules = []12 if list_Security_Group_Rules is not None:13 [self.add_existing_rule(rule) for rule in list_Security_Group_Rules]14 15 16 # TODO Handle -2 values throughout code - don't want to write -2 to csv or try to send it to AWS17 # TODO Handle -1 ip_protocol which means that all open18 class Security_Group_Rule:19 def __init__(self,ip_protocol=-2,from_port=-2,to_port=-2,cidr_ip='',description=''):20 self.ip_protocol = ip_protocol21 if type(from_port) is str:22 try:23 self.from_port = int(from_port)24 except:25 print ('unable to initialize from_port value. setting to -2, since -1 is a valid value')26 self.from_port = -227 else:28 self.from_port = from_port29 if type(to_port) is str:30 try:31 self.to_port = int(to_port)32 except:33 print ('unable to initialize to_port value. setting to -2, since -1 is a valid value')34 self.to_port = -235 else:36 self.to_port = to_port37 self.cidr_ip = cidr_ip38 self.description = description39 40 41 # TODO: I don't think this works42 def add_existing_rule(self,security_group_rule):43 self.list_Security_Group_Rules.append(security_group_rule)44 45 def add_new_rule(self,data):46 self.list_Security_Group_Rules.append(self.Security_Group_Rule(*data))47 print (self.list_Security_Group_Rules)48 # assumes arguments have the same keys - length comparison is performed in compare_sg49 def compare_values(self,first_vars,second_vars):50 51 for key in first_vars.keys():52 if type(first_vars[key]) is not list:53 if first_vars[key] != second_vars[key]:54 return False55 return True56 57 58 # compares classes and returns true if all the values in them are identical 59 def compare_sg(self, other_security_group):60 self_values = vars(self)61 other_values = vars(other_security_group)62 63 if len(self_values) != len(other_values):64 return False65 66 if len(self.list_Security_Group_Rules) != len(other_security_group.list_Security_Group_Rules):67 return False68 69 if not self.compare_values(self_values,other_values):70 return False71 for self_rule in self.list_Security_Group_Rules:72 rule_found = False73 for other_rule in other_security_group.list_Security_Group_Rules:74 if self.compare_values(vars(self_rule),vars(other_rule)):75 rule_found = True76 break77 if not rule_found:78 return False79 return True80 ...
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!!