<s> import sys <EOL> import unittest <EOL> from libcloud . utils . py3 import httplib <EOL> from libcloud . utils . py3 import urlparse <EOL> from libcloud . common . types import LibcloudError <EOL> from libcloud . compute . base import Node <EOL> from libcloud . compute . drivers . dummy import DummyNodeDriver <EOL> from libcloud . loadbalancer . base import LoadBalancer , Member , Algorithm <EOL> from libcloud . loadbalancer . drivers . gogrid import GoGridLBDriver <EOL> from libcloud . test import MockHttpTestCase <EOL> from libcloud . test . file_fixtures import LoadBalancerFileFixtures <EOL> class GoGridTests ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> GoGridLBDriver . connectionCls . conn_classes = ( None , <EOL> GoGridLBMockHttp ) <EOL> GoGridLBMockHttp . type = None <EOL> self . driver = GoGridLBDriver ( '<STR_LIT:user>' , '<STR_LIT:key>' ) <EOL> def test_list_supported_algorithms ( self ) : <EOL> algorithms = self . driver . list_supported_algorithms ( ) <EOL> self . assertTrue ( Algorithm . ROUND_ROBIN in algorithms ) <EOL> self . assertTrue ( Algorithm . LEAST_CONNECTIONS in algorithms ) <EOL> def test_list_protocols ( self ) : <EOL> protocols = self . driver . list_protocols ( ) <EOL> self . assertEqual ( len ( protocols ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( protocols [ <NUM_LIT:0> ] , '<STR_LIT:http>' ) <EOL> def test_list_balancers ( self ) : <EOL> balancers = self . driver . list_balancers ( ) <EOL> self . assertEqual ( len ( balancers ) , <NUM_LIT:2> ) <EOL> self . assertEqual ( balancers [ <NUM_LIT:0> ] . name , "<STR_LIT:foo>" ) <EOL> self . assertEqual ( balancers [ <NUM_LIT:0> ] . id , "<STR_LIT>" ) <EOL> self . assertEqual ( balancers [ <NUM_LIT:1> ] . name , "<STR_LIT:bar>" ) <EOL> self . assertEqual ( balancers [ <NUM_LIT:1> ] . id , "<STR_LIT>" ) <EOL> def test_create_balancer ( self ) : <EOL> balancer = self . driver . create_balancer ( name = '<STR_LIT>' , <EOL> port = <NUM_LIT> , <EOL> protocol = '<STR_LIT:http>' , <EOL> algorithm = Algorithm . ROUND_ROBIN , <EOL> members = ( <EOL> Member ( <EOL> None , '<STR_LIT>' , <NUM_LIT> ) , <EOL> Member ( None , '<STR_LIT>' , <NUM_LIT> ) ) <EOL> ) <EOL> self . assertEqual ( balancer . name , '<STR_LIT>' ) <EOL> self . assertEqual ( balancer . id , '<STR_LIT>' ) <EOL> def test_create_balancer_UNEXPECTED_ERROR ( self ) : <EOL> GoGridLBMockHttp . type = '<STR_LIT>' <EOL> try : <EOL> self . driver . create_balancer ( name = '<STR_LIT>' , <EOL> port = <NUM_LIT> , <EOL> protocol = '<STR_LIT:http>' , <EOL> algorithm = Algorithm . ROUND_ROBIN , <EOL> members = ( Member ( None , '<STR_LIT>' , <NUM_LIT> ) , <EOL> Member ( None , '<STR_LIT>' , <NUM_LIT> ) ) <EOL> ) <EOL> except LibcloudError : <EOL> e = sys . exc_info ( ) [ <NUM_LIT:1> ] <EOL> self . assertTrue ( <EOL> str ( e ) . find ( '<STR_LIT>' ) != - <NUM_LIT:1> ) <EOL> else : <EOL> self . fail ( '<STR_LIT>' ) <EOL> def test_destroy_balancer ( self ) : <EOL> balancer = self . driver . list_balancers ( ) [ <NUM_LIT:0> ] <EOL> ret1 = self . driver . destroy_balancer ( balancer ) <EOL> ret2 = balancer . destroy ( ) <EOL> self . assertTrue ( ret1 ) <EOL> self . assertTrue ( ret2 ) <EOL> def test_get_balancer ( self ) : <EOL> balancer = self . driver . get_balancer ( balancer_id = '<STR_LIT>' ) <EOL> self . assertEqual ( balancer . name , '<STR_LIT>' ) <EOL> self . assertEqual ( balancer . id , '<STR_LIT>' ) <EOL> def test_balancer_list_members ( self ) : <EOL> balancer = self . driver . get_balancer ( balancer_id = '<STR_LIT>' ) <EOL> members1 = self . driver . balancer_list_members ( balancer = balancer ) <EOL> members2 = balancer . list_members ( ) <EOL> expected_members = set ( [ '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ] ) <EOL> self . assertEqual ( len ( members1 ) , <NUM_LIT:3> ) <EOL> self . assertEqual ( len ( members2 ) , <NUM_LIT:3> ) <EOL> self . assertEqual ( expected_members , <EOL> set ( [ "<STR_LIT>" % ( member . ip , member . port ) for member in members1 ] ) ) <EOL> self . assertEqual ( members1 [ <NUM_LIT:0> ] . balancer , balancer ) <EOL> def test_balancer_attach_compute_node ( self ) : <EOL> balancer = LoadBalancer ( <NUM_LIT> , None , None , None , None , self . driver ) <EOL> node = Node ( id = '<STR_LIT:1>' , name = '<STR_LIT:test>' , state = None , public_ips = [ '<STR_LIT>' ] , <EOL> private_ips = [ ] , driver = DummyNodeDriver ) <EOL> member1 = self . driver . balancer_attach_compute_node ( balancer , node ) <EOL> member2 = balancer . attach_compute_node ( node ) <EOL> self . assertEqual ( member1 . ip , '<STR_LIT>' ) <EOL> self . assertEqual ( member1 . port , <NUM_LIT> ) <EOL> self . assertEqual ( member2 . ip , '<STR_LIT>' ) <EOL> self . assertEqual ( member2 . port , <NUM_LIT> ) <EOL> def test_balancer_attach_member ( self ) : <EOL> balancer = LoadBalancer ( <NUM_LIT> , None , None , None , None , self . driver ) <EOL> member = Member ( None , ip = '<STR_LIT>' , port = '<STR_LIT>' ) <EOL> member1 = self . driver . balancer_attach_member ( balancer , member = member ) <EOL> member2 = balancer . attach_member ( member = member ) <EOL> self . assertEqual ( member1 . ip , '<STR_LIT>' ) <EOL> self . assertEqual ( member1 . port , <NUM_LIT> ) <EOL> self . assertEqual ( member2 . ip , '<STR_LIT>' ) <EOL> self . assertEqual ( member2 . port , <NUM_LIT> ) <EOL> def test_balancer_detach_member ( self ) : <EOL> balancer = LoadBalancer ( <NUM_LIT> , None , None , None , None , self . driver ) <EOL> member = self . driver . balancer_list_members ( balancer ) [ <NUM_LIT:0> ] <EOL> ret1 = self . driver . balancer_detach_member ( balancer , member ) <EOL> ret2 = balancer . detach_member ( member ) <EOL> self . assertTrue ( ret1 ) <EOL> self . assertTrue ( ret2 ) <EOL> class GoGridLBMockHttp ( MockHttpTestCase ) : <EOL> fixtures = LoadBalancerFileFixtures ( '<STR_LIT>' ) <EOL> def _api_grid_loadbalancer_list ( self , method , url , body , headers ) : <EOL> body = self . fixtures . load ( '<STR_LIT>' ) <EOL> return ( httplib . OK , body , { } , httplib . responses [ httplib . OK ] ) <EOL> def _api_grid_ip_list ( self , method , url , body , headers ) : <EOL> body = self . fixtures . load ( '<STR_LIT>' ) <EOL> return ( httplib . OK , body , { } , httplib . responses [ httplib . OK ] ) <EOL> def _api_grid_loadbalancer_add ( self , method , url , body , headers ) : <EOL> query = urlparse . urlparse ( url ) . query <EOL> self . assertTrue ( query . find ( '<STR_LIT>' ) != - <NUM_LIT:1> ) <EOL> body = self . fixtures . load ( '<STR_LIT>' ) <EOL> return ( httplib . OK , body , { } , httplib . responses [ httplib . OK ] ) <EOL> def _api_grid_ip_list_UNEXPECTED_ERROR ( self , method , url , body , headers ) : <EOL> return self . _api_grid_ip_list ( method , url , body , headers ) <EOL> def _api_grid_loadbalancer_add_UNEXPECTED_ERROR ( self , method , url , body , headers ) : <EOL> body = self . fixtures . load ( '<STR_LIT>' ) <EOL> return ( httplib . INTERNAL_SERVER_ERROR , body , { } , httplib . responses [ httplib . OK ] ) <EOL> def _api_grid_loadbalancer_delete ( self , method , url , body , headers ) : <EOL> body = self . fixtures . load ( '<STR_LIT>' ) <EOL> return ( httplib . OK , body , { } , httplib . responses [ httplib . OK ] ) <EOL> def _api_grid_loadbalancer_get ( self , method , url , body , headers ) : <EOL> body = self . fixtures . load ( '<STR_LIT>' ) <EOL> return ( httplib . OK , body , { } , httplib . responses [ httplib . OK ] ) <EOL> def _api_grid_loadbalancer_edit ( self , method , url , body , headers ) : <EOL> body = self . fixtures . load ( '<STR_LIT>' ) <EOL> return ( httplib . OK , body , { } , httplib . responses [ httplib . OK ] ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> sys . exit ( unittest . main ( ) ) </s>
