<s> import time <EOL> from eucaops import Eucaops <EOL> from eucaops import ELBops <EOL> from boto . ec2 . elb import LoadBalancer <EOL> from eutester . eutestcase import EutesterTestCase <EOL> import os <EOL> import random <EOL> class LoadBalancing ( EutesterTestCase ) : <EOL> def __init__ ( self , extra_args = None ) : <EOL> self . setuptestcase ( ) <EOL> self . setup_parser ( ) <EOL> if extra_args : <EOL> for arg in extra_args : <EOL> self . parser . add_argument ( arg ) <EOL> self . get_args ( ) <EOL> if self . args . region : <EOL> self . tester = ELBops ( credpath = self . args . credpath , region = self . args . region ) <EOL> else : <EOL> self . tester = Eucaops ( credpath = self . args . credpath , config_file = self . args . config , password = self . args . password ) <EOL> self . test_hash = str ( int ( time . time ( ) ) ) <EOL> self . group = self . tester . add_group ( group_name = "<STR_LIT>" + self . test_hash ) <EOL> self . tester . authorize_group_by_name ( group_name = self . group . name ) <EOL> self . tester . authorize_group_by_name ( group_name = self . group . name , port = - <NUM_LIT:1> , protocol = "<STR_LIT>" ) <EOL> self . tester . authorize_group_by_name ( group_name = self . group . name , port = <NUM_LIT> , protocol = "<STR_LIT>" ) <EOL> self . keypair = self . tester . add_keypair ( "<STR_LIT>" + self . test_hash ) <EOL> self . keypath = '<STR_LIT>' % ( os . curdir , self . keypair . name ) <EOL> self . user_data = "<STR_LIT>" <EOL> self . image = self . args . emi <EOL> if not self . image : <EOL> self . image = self . tester . get_emi ( ) <EOL> zones = self . tester . ec2 . get_all_zones ( ) <EOL> self . zone = random . choice ( zones ) . name <EOL> self . load_balancer_port = <NUM_LIT> <EOL> self . load_balancer = self . tester . create_load_balancer ( zones = [ self . zone ] , <EOL> name = "<STR_LIT>" + self . test_hash , <EOL> load_balancer_port = self . load_balancer_port ) <EOL> assert isinstance ( self . load_balancer , LoadBalancer ) <EOL> self . count = <NUM_LIT:2> <EOL> ( self . web_servers ) = self . tester . create_as_webservers ( name = self . test_hash , <EOL> keypair = self . keypair . name , <EOL> group = self . group . name , <EOL> zone = self . zone , <EOL> image = self . image . id , <EOL> count = self . count , <EOL> user_data = self . user_data , <EOL> load_balancer = self . load_balancer . name ) <EOL> self . asg = self . tester . describe_as_group ( name = "<STR_LIT>" + self . test_hash ) <EOL> assert self . tester . wait_for_result ( self . tester . wait_for_lb_instances , True , timeout = <NUM_LIT> , <EOL> lb = self . load_balancer . name , number = self . count ) <EOL> def clean_method ( self ) : <EOL> self . tester . cleanup_artifacts ( ) <EOL> if self . tester . test_resources [ "<STR_LIT>" ] : <EOL> for group in self . tester . test_resources [ "<STR_LIT>" ] : <EOL> self . tester . wait_for_result ( self . tester . gracefully_delete_group , True , timeout = <NUM_LIT> , group = group ) <EOL> def GenerateRequests ( self ) : <EOL> """<STR_LIT>""" <EOL> dns = self . tester . service_manager . get_enabled_dns ( ) <EOL> lb_ip = dns . resolve ( self . load_balancer . dns_name ) <EOL> lb_url = "<STR_LIT>" . format ( lb_ip , self . load_balancer_port ) <EOL> self . tester . generate_http_requests ( url = lb_url , count = <NUM_LIT:1000> ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> testcase = LoadBalancing ( ) <EOL> list = testcase . args . tests or [ "<STR_LIT>" ] <EOL> unit_list = [ ] <EOL> for test in list : <EOL> unit_list . append ( testcase . create_testunit_by_name ( test ) ) <EOL> result = testcase . run_test_case_list ( unit_list , clean_on_exit = True ) <EOL> exit ( result ) </s>
