<s> import sys <EOL> import unittest <EOL> import base64 <EOL> try : <EOL> import simplejson as json <EOL> except ImportError : <EOL> import json <EOL> from libcloud . utils . py3 import httplib <EOL> from libcloud . utils . py3 import b <EOL> from libcloud . common . types import InvalidCredsError <EOL> from libcloud . compute . drivers . brightbox import BrightboxNodeDriver <EOL> from libcloud . compute . types import NodeState <EOL> from libcloud . test import MockHttp <EOL> from libcloud . test . compute import TestCaseMixin <EOL> from libcloud . test . file_fixtures import ComputeFileFixtures <EOL> from libcloud . test . secrets import BRIGHTBOX_PARAMS <EOL> USER_DATA = '<STR_LIT>' <EOL> class BrightboxTest ( unittest . TestCase , TestCaseMixin ) : <EOL> def setUp ( self ) : <EOL> BrightboxNodeDriver . connectionCls . conn_classes = ( <EOL> None , BrightboxMockHttp ) <EOL> BrightboxMockHttp . type = None <EOL> self . driver = BrightboxNodeDriver ( * BRIGHTBOX_PARAMS ) <EOL> def test_authentication ( self ) : <EOL> BrightboxMockHttp . type = '<STR_LIT>' <EOL> self . assertRaises ( InvalidCredsError , self . driver . list_nodes ) <EOL> BrightboxMockHttp . type = '<STR_LIT>' <EOL> self . assertRaises ( InvalidCredsError , self . driver . list_nodes ) <EOL> def test_invalid_api_version ( self ) : <EOL> kwargs = { '<STR_LIT>' : '<STR_LIT>' } <EOL> self . driver = BrightboxNodeDriver ( * BRIGHTBOX_PARAMS , ** kwargs ) <EOL> self . assertRaises ( Exception , self . driver . list_locations ) <EOL> def test_other_host ( self ) : <EOL> kwargs = { '<STR_LIT:host>' : '<STR_LIT>' } <EOL> self . driver = BrightboxNodeDriver ( * BRIGHTBOX_PARAMS , ** kwargs ) <EOL> locations = self . driver . list_locations ( ) <EOL> self . assertEqual ( len ( locations ) , <NUM_LIT:0> ) <EOL> def test_list_nodes ( self ) : <EOL> nodes = self . driver . list_nodes ( ) <EOL> self . assertEqual ( len ( nodes ) , <NUM_LIT:2> ) <EOL> self . assertEqual ( len ( nodes [ <NUM_LIT:0> ] . public_ips ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( len ( nodes [ <NUM_LIT:1> ] . public_ips ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( len ( nodes [ <NUM_LIT:0> ] . private_ips ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( len ( nodes [ <NUM_LIT:1> ] . private_ips ) , <NUM_LIT:1> ) <EOL> self . assertTrue ( '<STR_LIT>' in nodes [ <NUM_LIT:0> ] . public_ips ) <EOL> self . assertTrue ( '<STR_LIT>' in nodes [ <NUM_LIT:0> ] . private_ips ) <EOL> self . assertTrue ( '<STR_LIT>' in nodes [ <NUM_LIT:1> ] . private_ips ) <EOL> self . assertTrue ( <EOL> '<STR_LIT>' in nodes [ <NUM_LIT:1> ] . public_ips ) <EOL> self . assertEqual ( nodes [ <NUM_LIT:0> ] . state , NodeState . RUNNING ) <EOL> self . assertEqual ( nodes [ <NUM_LIT:1> ] . state , NodeState . RUNNING ) <EOL> def test_list_node_extras ( self ) : <EOL> nodes = self . driver . list_nodes ( ) <EOL> self . assertFalse ( nodes [ <NUM_LIT:0> ] . size is None ) <EOL> self . assertFalse ( nodes [ <NUM_LIT:1> ] . size is None ) <EOL> self . assertFalse ( nodes [ <NUM_LIT:0> ] . image is None ) <EOL> self . assertFalse ( nodes [ <NUM_LIT:1> ] . image is None ) <EOL> self . assertEqual ( nodes [ <NUM_LIT:0> ] . image . id , '<STR_LIT>' ) <EOL> self . assertEqual ( nodes [ <NUM_LIT:0> ] . size . id , '<STR_LIT>' ) <EOL> self . assertEqual ( nodes [ <NUM_LIT:1> ] . image . id , '<STR_LIT>' ) <EOL> self . assertEqual ( nodes [ <NUM_LIT:1> ] . size . id , '<STR_LIT>' ) <EOL> self . assertEqual ( nodes [ <NUM_LIT:0> ] . extra [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> self . assertEqual ( nodes [ <NUM_LIT:1> ] . extra [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> self . assertEqual ( nodes [ <NUM_LIT:0> ] . extra [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> self . assertEqual ( nodes [ <NUM_LIT:1> ] . extra [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> self . assertEqual ( nodes [ <NUM_LIT:0> ] . extra [ '<STR_LIT:status>' ] , '<STR_LIT>' ) <EOL> self . assertEqual ( nodes [ <NUM_LIT:1> ] . extra [ '<STR_LIT:status>' ] , '<STR_LIT>' ) <EOL> self . assertTrue ( '<STR_LIT>' in nodes [ <NUM_LIT:0> ] . extra ) <EOL> self . assertTrue ( '<STR_LIT>' in nodes [ <NUM_LIT:0> ] . extra ) <EOL> self . assertTrue ( '<STR_LIT>' in nodes [ <NUM_LIT:0> ] . extra ) <EOL> self . assertTrue ( '<STR_LIT>' in nodes [ <NUM_LIT:0> ] . extra ) <EOL> self . assertTrue ( '<STR_LIT>' in nodes [ <NUM_LIT:0> ] . extra ) <EOL> self . assertTrue ( '<STR_LIT>' in nodes [ <NUM_LIT:0> ] . extra ) <EOL> self . assertFalse ( '<STR_LIT>' in nodes [ <NUM_LIT:0> ] . extra ) <EOL> def test_list_sizes ( self ) : <EOL> sizes = self . driver . list_sizes ( ) <EOL> self . assertEqual ( len ( sizes ) , <NUM_LIT:7> ) <EOL> self . assertEqual ( sizes [ <NUM_LIT:0> ] . id , '<STR_LIT>' ) <EOL> self . assertEqual ( sizes [ <NUM_LIT:0> ] . name , '<STR_LIT>' ) <EOL> self . assertEqual ( sizes [ <NUM_LIT:0> ] . ram , <NUM_LIT> ) <EOL> self . assertEqual ( sizes [ <NUM_LIT:0> ] . disk , <NUM_LIT> ) <EOL> self . assertEqual ( sizes [ <NUM_LIT:0> ] . bandwidth , <NUM_LIT:0> ) <EOL> self . assertEqual ( sizes [ <NUM_LIT:0> ] . price , <NUM_LIT:0> ) <EOL> def test_list_images ( self ) : <EOL> images = self . driver . list_images ( ) <EOL> self . assertEqual ( len ( images ) , <NUM_LIT:3> ) <EOL> self . assertEqual ( images [ <NUM_LIT:0> ] . id , '<STR_LIT>' ) <EOL> self . assertEqual ( images [ <NUM_LIT:0> ] . name , '<STR_LIT>' ) <EOL> self . assertTrue ( '<STR_LIT>' in images [ <NUM_LIT:0> ] . extra ) <EOL> self . assertFalse ( '<STR_LIT>' in images [ <NUM_LIT:0> ] . extra ) <EOL> def test_list_images_extras ( self ) : <EOL> images = self . driver . list_images ( ) <EOL> extra = images [ - <NUM_LIT:1> ] . extra <EOL> self . assertEqual ( extra [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> self . assertFalse ( extra [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( extra [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> self . assertTrue ( '<STR_LIT:description>' in extra ) <EOL> self . assertEqual ( extra [ '<STR_LIT>' ] , <NUM_LIT> ) <EOL> self . assertFalse ( '<STR_LIT>' in extra ) <EOL> self . assertFalse ( extra [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( extra [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> self . assertTrue ( extra [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( extra [ '<STR_LIT:source>' ] , '<STR_LIT>' ) <EOL> self . assertEqual ( extra [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> self . assertEqual ( extra [ '<STR_LIT:status>' ] , '<STR_LIT>' ) <EOL> self . assertEqual ( extra [ '<STR_LIT:username>' ] , '<STR_LIT>' ) <EOL> self . assertEqual ( extra [ '<STR_LIT>' ] , <NUM_LIT> ) <EOL> self . assertFalse ( '<STR_LIT>' in extra ) <EOL> self . assertFalse ( '<STR_LIT>' in extra ) <EOL> def test_list_locations ( self ) : <EOL> locations = self . driver . list_locations ( ) <EOL> self . assertEqual ( locations [ <NUM_LIT:0> ] . id , '<STR_LIT>' ) <EOL> self . assertEqual ( locations [ <NUM_LIT:0> ] . name , '<STR_LIT>' ) <EOL> self . assertEqual ( locations [ <NUM_LIT:1> ] . id , '<STR_LIT>' ) <EOL> self . assertEqual ( locations [ <NUM_LIT:1> ] . name , '<STR_LIT>' ) <EOL> def test_reboot_node_response ( self ) : <EOL> node = self . driver . list_nodes ( ) [ <NUM_LIT:0> ] <EOL> self . assertRaises ( NotImplementedError , self . driver . reboot_node , [ node ] ) <EOL> def test_destroy_node ( self ) : <EOL> node = self . driver . list_nodes ( ) [ <NUM_LIT:0> ] <EOL> self . assertTrue ( self . driver . destroy_node ( node ) ) <EOL> def test_create_node ( self ) : <EOL> size = self . driver . list_sizes ( ) [ <NUM_LIT:0> ] <EOL> image = self . driver . list_images ( ) [ <NUM_LIT:0> ] <EOL> node = self . driver . create_node ( <EOL> name = '<STR_LIT>' , image = image , size = size ) <EOL> self . assertEqual ( '<STR_LIT>' , node . id ) <EOL> self . assertEqual ( '<STR_LIT>' , node . name ) <EOL> self . assertEqual ( '<STR_LIT>' , node . extra [ '<STR_LIT>' ] . name ) <EOL> def test_create_node_in_location ( self ) : <EOL> size = self . driver . list_sizes ( ) [ <NUM_LIT:0> ] <EOL> image = self . driver . list_images ( ) [ <NUM_LIT:0> ] <EOL> location = self . driver . list_locations ( ) [ <NUM_LIT:1> ] <EOL> node = self . driver . create_node ( <EOL> name = '<STR_LIT>' , image = image , size = size , location = location ) <EOL> self . assertEqual ( '<STR_LIT>' , node . id ) <EOL> self . assertEqual ( '<STR_LIT>' , node . name ) <EOL> self . assertEqual ( '<STR_LIT>' , node . extra [ '<STR_LIT>' ] . name ) <EOL> def test_create_node_with_user_data ( self ) : <EOL> size = self . driver . list_sizes ( ) [ <NUM_LIT:0> ] <EOL> image = self . driver . list_images ( ) [ <NUM_LIT:0> ] <EOL> node = self . driver . create_node ( <EOL> name = '<STR_LIT>' , image = image , size = size , ex_userdata = USER_DATA ) <EOL> decoded = base64 . b64decode ( b ( node . extra [ '<STR_LIT>' ] ) ) . decode ( '<STR_LIT:ascii>' ) <EOL> self . assertEqual ( '<STR_LIT>' , node . extra [ '<STR_LIT>' ] . name ) <EOL> self . assertEqual ( USER_DATA , decoded ) <EOL> def test_create_node_with_a_server_group ( self ) : <EOL> size = self . driver . list_sizes ( ) [ <NUM_LIT:0> ] <EOL> image = self . driver . list_images ( ) [ <NUM_LIT:0> ] <EOL> node = self . driver . create_node ( <EOL> name = '<STR_LIT>' , image = image , size = size , ex_servergroup = '<STR_LIT>' ) <EOL> self . assertEqual ( '<STR_LIT>' , node . extra [ '<STR_LIT>' ] . name ) <EOL> self . assertEqual ( len ( node . extra [ '<STR_LIT>' ] ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( node . extra [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT:id>' ] , '<STR_LIT>' ) <EOL> def test_create_node_with_a_list_of_server_groups ( self ) : <EOL> size = self . driver . list_sizes ( ) [ <NUM_LIT:0> ] <EOL> image = self . driver . list_images ( ) [ <NUM_LIT:0> ] <EOL> node = self . driver . create_node ( <EOL> name = '<STR_LIT>' , image = image , size = size , ex_servergroup = [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> self . assertEqual ( '<STR_LIT>' , node . extra [ '<STR_LIT>' ] . name ) <EOL> self . assertEqual ( len ( node . extra [ '<STR_LIT>' ] ) , <NUM_LIT:2> ) <EOL> self . assertEqual ( node . extra [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT:id>' ] , '<STR_LIT>' ) <EOL> self . assertEqual ( node . extra [ '<STR_LIT>' ] [ <NUM_LIT:1> ] [ '<STR_LIT:id>' ] , '<STR_LIT>' ) <EOL> def test_list_cloud_ips ( self ) : <EOL> cip_list = self . driver . ex_list_cloud_ips ( ) <EOL> self . assertEqual ( len ( cip_list ) , <NUM_LIT:4> ) <EOL> self . assertEqual ( cip_list [ <NUM_LIT:2> ] [ '<STR_LIT:status>' ] , '<STR_LIT>' ) <EOL> cip_check = cip_list [ <NUM_LIT:0> ] <EOL> self . assertEqual ( cip_check [ '<STR_LIT:id>' ] , '<STR_LIT>' ) <EOL> self . assertEqual ( cip_check [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> self . assertEqual ( <EOL> cip_check [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> self . assertEqual ( cip_check [ '<STR_LIT:status>' ] , '<STR_LIT>' ) <EOL> self . assertTrue ( cip_check [ '<STR_LIT>' ] is None ) <EOL> self . assertTrue ( cip_check [ '<STR_LIT>' ] is None ) <EOL> self . assertTrue ( cip_check [ '<STR_LIT>' ] is None ) <EOL> self . assertTrue ( cip_check [ '<STR_LIT>' ] is None ) <EOL> def test_create_cloud_ip ( self ) : <EOL> cip = self . driver . ex_create_cloud_ip ( ) <EOL> self . assertEqual ( cip [ '<STR_LIT:id>' ] , '<STR_LIT>' ) <EOL> self . assertEqual ( <EOL> cip [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> def test_create_cloud_ip_with_dns ( self ) : <EOL> cip = self . driver . ex_create_cloud_ip ( '<STR_LIT>' ) <EOL> self . assertEqual ( cip [ '<STR_LIT:id>' ] , '<STR_LIT>' ) <EOL> self . assertEqual ( cip [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> def test_map_cloud_ip ( self ) : <EOL> self . assertTrue ( self . driver . ex_map_cloud_ip ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> def test_unmap_cloud_ip ( self ) : <EOL> self . assertTrue ( self . driver . ex_unmap_cloud_ip ( '<STR_LIT>' ) ) <EOL> def test_update_cloud_ip ( self ) : <EOL> self . assertTrue ( <EOL> self . driver . ex_update_cloud_ip ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> def test_destroy_cloud_ip ( self ) : <EOL> self . assertTrue ( self . driver . ex_destroy_cloud_ip ( '<STR_LIT>' ) ) <EOL> class BrightboxMockHttp ( MockHttp ) : <EOL> fixtures = ComputeFileFixtures ( '<STR_LIT>' ) <EOL> def _token ( self , method , url , body , headers ) : <EOL> if method == '<STR_LIT:POST>' : <EOL> return self . response ( httplib . OK , self . fixtures . load ( '<STR_LIT>' ) ) <EOL> def _token_INVALID_CLIENT ( self , method , url , body , headers ) : <EOL> if method == '<STR_LIT:POST>' : <EOL> return self . response ( httplib . BAD_REQUEST , '<STR_LIT>' ) <EOL> def _token_UNAUTHORIZED_CLIENT ( self , method , url , body , headers ) : <EOL> if method == '<STR_LIT:POST>' : <EOL> return self . response ( httplib . UNAUTHORIZED , '<STR_LIT>' ) <EOL> def _1_0_images ( self , method , url , body , headers ) : <EOL> if method == '<STR_LIT:GET>' : <EOL> return self . response ( httplib . OK , self . fixtures . load ( '<STR_LIT>' ) ) <EOL> def _1_0_servers ( self , method , url , body , headers ) : <EOL> if method == '<STR_LIT:GET>' : <EOL> return self . response ( httplib . OK , self . fixtures . load ( '<STR_LIT>' ) ) <EOL> elif method == '<STR_LIT:POST>' : <EOL> body = json . loads ( body ) <EOL> encoded = base64 . b64encode ( b ( USER_DATA ) ) . decode ( '<STR_LIT:ascii>' ) <EOL> if '<STR_LIT>' in body and body [ '<STR_LIT>' ] != encoded : <EOL> data = '<STR_LIT>' <EOL> return self . response ( httplib . BAD_REQUEST , data ) <EOL> if body . get ( '<STR_LIT>' , '<STR_LIT>' ) == '<STR_LIT>' : <EOL> node = json . loads ( <EOL> self . fixtures . load ( '<STR_LIT>' ) ) <EOL> else : <EOL> node = json . loads ( <EOL> self . fixtures . load ( '<STR_LIT>' ) ) <EOL> node [ '<STR_LIT:name>' ] = body [ '<STR_LIT:name>' ] <EOL> if '<STR_LIT>' in body : <EOL> node [ '<STR_LIT>' ] = [ { '<STR_LIT:id>' : x } <EOL> for x in body [ '<STR_LIT>' ] ] <EOL> if '<STR_LIT>' in body : <EOL> node [ '<STR_LIT>' ] = body [ '<STR_LIT>' ] <EOL> return self . response ( httplib . ACCEPTED , json . dumps ( node ) ) <EOL> def _1_0_servers_srv_xvpn7 ( self , method , url , body , headers ) : <EOL> if method == '<STR_LIT>' : <EOL> return self . response ( httplib . ACCEPTED , '<STR_LIT>' ) <EOL> def _1_0_server_types ( self , method , url , body , headers ) : <EOL> if method == '<STR_LIT:GET>' : <EOL> return self . response ( httplib . OK , self . fixtures . load ( '<STR_LIT>' ) ) <EOL> def _1_0_zones ( self , method , url , body , headers ) : <EOL> if method == '<STR_LIT:GET>' : <EOL> if headers [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> return self . response ( httplib . OK , "<STR_LIT:{}>" ) <EOL> else : <EOL> return self . response ( httplib . OK , self . fixtures . load ( '<STR_LIT>' ) ) <EOL> def _2_0_zones ( self , method , url , body , headers ) : <EOL> data = '<STR_LIT>' <EOL> return self . response ( httplib . BAD_REQUEST , data ) <EOL> def _1_0_cloud_ips ( self , method , url , body , headers ) : <EOL> if method == '<STR_LIT:GET>' : <EOL> return self . response ( httplib . OK , self . fixtures . load ( '<STR_LIT>' ) ) <EOL> elif method == '<STR_LIT:POST>' : <EOL> if body : <EOL> body = json . loads ( body ) <EOL> node = json . loads ( self . fixtures . load ( '<STR_LIT>' ) ) <EOL> if '<STR_LIT>' in body : <EOL> node [ '<STR_LIT>' ] = body [ '<STR_LIT>' ] <EOL> return self . response ( httplib . ACCEPTED , json . dumps ( node ) ) <EOL> def _1_0_cloud_ips_cip_jsjc5 ( self , method , url , body , headers ) : <EOL> if method == '<STR_LIT>' : <EOL> return self . response ( httplib . OK , '<STR_LIT>' ) <EOL> elif method == '<STR_LIT>' : <EOL> body = json . loads ( body ) <EOL> if body . get ( '<STR_LIT>' , None ) == '<STR_LIT>' : <EOL> return self . response ( httplib . OK , '<STR_LIT>' ) <EOL> else : <EOL> return self . response ( httplib . BAD_REQUEST , '<STR_LIT>' ) <EOL> def _1_0_cloud_ips_cip_jsjc5_map ( self , method , url , body , headers ) : <EOL> if method == '<STR_LIT:POST>' : <EOL> body = json . loads ( body ) <EOL> if '<STR_LIT>' in body : <EOL> return self . response ( httplib . ACCEPTED , '<STR_LIT>' ) <EOL> else : <EOL> data = '<STR_LIT>' <EOL> return self . response ( httplib . BAD_REQUEST , data ) <EOL> def _1_0_cloud_ips_cip_jsjc5_unmap ( self , method , url , body , headers ) : <EOL> if method == '<STR_LIT:POST>' : <EOL> return self . response ( httplib . ACCEPTED , '<STR_LIT>' ) <EOL> def response ( self , status , body ) : <EOL> return ( status , body , { '<STR_LIT>' : '<STR_LIT:application/json>' } , httplib . responses [ status ] ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> sys . exit ( unittest . main ( ) ) </s>
