<s> """<STR_LIT>""" <EOL> import celery <EOL> import copy <EOL> import mock <EOL> import os <EOL> import simplejson as json <EOL> import unittest2 <EOL> os . environ [ '<STR_LIT>' ] = '<STR_LIT:true>' <EOL> from compass . utils import setting_wrapper as setting <EOL> reload ( setting ) <EOL> from compass . api import login_manager <EOL> from compass . db . api import adapter_holder as adapter_api <EOL> from compass . db . api import cluster as cluster_api <EOL> from compass . db . api import database <EOL> from compass . db . api import host as host_api <EOL> from compass . db . api import metadata_holder as metadata_api <EOL> from compass . db . api import user as user_api <EOL> from compass . db . models import User <EOL> from compass . utils import flags <EOL> from compass . utils import logsetting <EOL> from compass . utils import util <EOL> class ApiTestCase ( unittest2 . TestCase ) : <EOL> """<STR_LIT>""" <EOL> CLUSTER_NAME = "<STR_LIT>" <EOL> SWITCH_IP_ADDRESS = '<STR_LIT>' <EOL> SWITCH_CREDENTIAL = { '<STR_LIT:version>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } <EOL> USER_CREDENTIALS = { <EOL> '<STR_LIT:email>' : setting . COMPASS_ADMIN_EMAIL , <EOL> '<STR_LIT:password>' : setting . COMPASS_ADMIN_PASSWORD <EOL> } <EOL> def setUp ( self ) : <EOL> super ( ApiTestCase , self ) . setUp ( ) <EOL> os . environ [ '<STR_LIT>' ] = '<STR_LIT:true>' <EOL> os . environ [ '<STR_LIT>' ] = os . path . join ( <EOL> os . path . dirname ( os . path . abspath ( __file__ ) ) , <EOL> '<STR_LIT:data>' <EOL> ) <EOL> reload ( setting ) <EOL> database . init ( '<STR_LIT>' ) <EOL> database . create_db ( ) <EOL> adapter_api . load_adapters ( force_reload = True ) <EOL> metadata_api . load_metadatas ( force_reload = True ) <EOL> adapter_api . load_flavors ( force_reload = True ) <EOL> from compass . api import api as compass_api <EOL> application = compass_api . app <EOL> self . test_client = application . test_client ( ) <EOL> celery . current_app . send_task = mock . Mock ( ) <EOL> from compass . tasks import client as celery_client <EOL> celery_client . celery . send_task = mock . Mock ( ) <EOL> url = '<STR_LIT>' <EOL> data = self . USER_CREDENTIALS <EOL> request_data = json . dumps ( data ) <EOL> return_value = self . test_client . post ( <EOL> url , <EOL> data = request_data , <EOL> ) <EOL> resp = return_value . get_data ( ) <EOL> resp = json . loads ( resp ) <EOL> self . token = resp [ '<STR_LIT>' ] <EOL> adapter_name , adapter_id , os_id , flavor_id = ( <EOL> self . _get_adapter_info ( ) <EOL> ) <EOL> url = '<STR_LIT>' <EOL> data = { } <EOL> data [ '<STR_LIT:name>' ] = '<STR_LIT>' <EOL> data [ '<STR_LIT>' ] = adapter_id <EOL> data [ '<STR_LIT>' ] = os_id <EOL> self . os_id = os_id <EOL> data [ '<STR_LIT>' ] = flavor_id <EOL> self . post ( url , data ) <EOL> data = { } <EOL> data [ '<STR_LIT:name>' ] = '<STR_LIT>' <EOL> data [ '<STR_LIT>' ] = adapter_id <EOL> data [ '<STR_LIT>' ] = os_id <EOL> self . flavor_id = flavor_id <EOL> data [ '<STR_LIT>' ] = flavor_id <EOL> self . post ( url , data ) <EOL> url = '<STR_LIT>' <EOL> datas = [ <EOL> { <EOL> '<STR_LIT>' : self . SWITCH_IP_ADDRESS , <EOL> '<STR_LIT>' : { <EOL> "<STR_LIT:version>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:state>' : '<STR_LIT>' <EOL> } , <EOL> { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : { <EOL> "<STR_LIT:version>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:state>' : '<STR_LIT>' <EOL> } <EOL> ] <EOL> for data in datas : <EOL> self . post ( url , data ) <EOL> def get ( self , url ) : <EOL> return self . test_client . get ( <EOL> url , headers = { <EOL> setting . USER_AUTH_HEADER_NAME : self . token <EOL> } <EOL> ) <EOL> def post ( self , url , data ) : <EOL> return self . test_client . post ( url , data = json . dumps ( data ) ) <EOL> def put ( self , url , data ) : <EOL> return self . test_client . put ( url , data = json . dumps ( data ) ) <EOL> def delete ( self , url ) : <EOL> return self . test_client . delete ( url ) <EOL> def tearDown ( self ) : <EOL> database . drop_db ( ) <EOL> reload ( setting ) <EOL> super ( ApiTestCase , self ) . tearDown ( ) <EOL> def _get_adapter_info ( self ) : <EOL> adapter_name = None <EOL> adapter_id = None <EOL> os_id = None <EOL> flavor_id = None <EOL> adapters = self . get ( <EOL> '<STR_LIT>' <EOL> ) . get_data ( ) <EOL> adapters = json . loads ( adapters ) <EOL> for adapter in adapters : <EOL> if adapter [ '<STR_LIT>' ] : <EOL> adapter_name = adapter [ '<STR_LIT:name>' ] <EOL> adapter_id = adapter [ '<STR_LIT:id>' ] <EOL> os_id = adapter [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT>' ] <EOL> for flavor in adapter [ '<STR_LIT>' ] : <EOL> flavor_id = flavor [ '<STR_LIT:id>' ] <EOL> break <EOL> if not adapter_name : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> if not adapter_id : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> if not os_id : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> if not flavor_id : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> return ( adapter_name , adapter_id , os_id , flavor_id ) <EOL> class TestAuth ( ApiTestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> super ( TestAuth , self ) . setUp ( ) <EOL> def tearDown ( self ) : <EOL> super ( TestAuth , self ) . tearDown ( ) <EOL> def test_login_logout ( self ) : <EOL> url = '<STR_LIT>' <EOL> data = self . USER_CREDENTIALS <EOL> request_data = json . dumps ( data ) <EOL> return_value = self . test_client . post ( <EOL> url , <EOL> data = request_data , <EOL> follow_redirects = True <EOL> ) <EOL> self . assertEqual ( return_value . status_code , <NUM_LIT:200> ) <EOL> url = '<STR_LIT>' <EOL> return_value = self . test_client . post ( <EOL> url , <EOL> follow_redirects = True <EOL> ) <EOL> self . assertEqual ( return_value . status_code , <NUM_LIT:200> ) <EOL> def test_login_failed ( self ) : <EOL> url = '<STR_LIT>' <EOL> data_list = [ <EOL> { "<STR_LIT:email>" : "<STR_LIT>" , "<STR_LIT:password>" : "<STR_LIT>" } , <EOL> { "<STR_LIT:email>" : "<STR_LIT>" , "<STR_LIT:password>" : "<STR_LIT>" } <EOL> ] <EOL> for data in data_list : <EOL> return_value = self . test_client . post ( <EOL> url , <EOL> data = data , <EOL> follow_redirects = True <EOL> ) <EOL> self . assertEqual ( return_value . status_code , <NUM_LIT> ) <EOL> self . assertIn ( '<STR_LIT>' , return_value . get_data ( ) ) <EOL> User . query . filter_by ( <EOL> email = "<STR_LIT>" <EOL> ) . update ( { "<STR_LIT>" : False } ) <EOL> request_data = json . dumps ( self . USER_CREDENTIALS ) <EOL> return_value = self . test_client . post ( <EOL> url , <EOL> data = request_data , <EOL> follow_redirects = True <EOL> ) <EOL> class TestUserAPI ( ApiTestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> super ( TestUserAPI , self ) . setUp ( ) <EOL> def tearDown ( self ) : <EOL> super ( TestUserAPI , self ) . tearDown ( ) <EOL> def test_list_users ( self ) : <EOL> url = '<STR_LIT>' <EOL> return_value = self . get ( url ) <EOL> resp = json . loads ( return_value . get_data ( ) ) <EOL> count = len ( resp ) <EOL> self . assertEqual ( count , <NUM_LIT:1> ) <EOL> self . assertEqual ( return_value . status_code , <NUM_LIT:200> ) <EOL> class TestClusterAPI ( ApiTestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> super ( TestClusterAPI , self ) . setUp ( ) <EOL> url = '<STR_LIT>' <EOL> data = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:port>' : '<STR_LIT:1>' , <EOL> '<STR_LIT>' : [ <NUM_LIT> ] <EOL> } <EOL> self . post ( url , data ) <EOL> url = '<STR_LIT>' <EOL> data = { <EOL> '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : <NUM_LIT:1> <EOL> } <EOL> self . post ( url , data ) <EOL> def tearDown ( self ) : <EOL> super ( TestClusterAPI , self ) . tearDown ( ) <EOL> def test_list_clusters ( self ) : <EOL> url = '<STR_LIT>' <EOL> return_value = self . test_client . get ( url ) <EOL> self . assertEqual ( return_value . status_code , <NUM_LIT:200> ) <EOL> resp = json . loads ( return_value . get_data ( ) ) <EOL> expected = [ <EOL> { '<STR_LIT:name>' : '<STR_LIT>' } , <EOL> { '<STR_LIT:name>' : '<STR_LIT>' } <EOL> ] <EOL> for i , v in enumerate ( resp ) : <EOL> self . assertTrue ( <EOL> all ( item in resp [ i ] . items ( ) for item in expected [ i ] . items ( ) ) <EOL> ) <EOL> url = '<STR_LIT>' <EOL> return_value = json . loads ( <EOL> self . test_client . get ( url ) . get_data ( ) <EOL> ) <EOL> self . assertEqual ( [ ] , return_value ) <EOL> def test_show_cluster ( self ) : <EOL> url = '<STR_LIT>' <EOL> return_value = self . get ( url ) <EOL> self . assertEqual ( return_value . status_code , <NUM_LIT:200> ) <EOL> resp = json . loads ( return_value . get_data ( ) ) <EOL> self . assertEqual ( resp [ '<STR_LIT:name>' ] , '<STR_LIT>' ) <EOL> url = '<STR_LIT>' <EOL> return_value = self . get ( url ) <EOL> self . assertEqual ( return_value . status_code , <NUM_LIT> ) <EOL> def test_add_cluster ( self ) : <EOL> url = '<STR_LIT>' <EOL> adapter_name , adapter_id , os_id , flavor_id = ( <EOL> self . _get_adapter_info ( ) <EOL> ) <EOL> data = { } <EOL> data [ '<STR_LIT:name>' ] = '<STR_LIT>' <EOL> data [ '<STR_LIT>' ] = adapter_id <EOL> data [ '<STR_LIT>' ] = os_id <EOL> data [ '<STR_LIT>' ] = flavor_id <EOL> return_value = self . post ( url , data ) <EOL> resp = json . loads ( return_value . get_data ( ) ) <EOL> self . assertEqual ( return_value . status_code , <NUM_LIT:200> ) <EOL> self . assertEqual ( resp [ '<STR_LIT:name>' ] , '<STR_LIT>' ) <EOL> return_value = self . post ( url , data ) <EOL> self . assertEqual ( return_value . status_code , <NUM_LIT> ) <EOL> data = { } <EOL> data [ '<STR_LIT:name>' ] = '<STR_LIT>' <EOL> data [ '<STR_LIT>' ] = <NUM_LIT:9> <EOL> data [ '<STR_LIT>' ] = <NUM_LIT:1> <EOL> data [ '<STR_LIT>' ] = flavor_id <EOL> return_value = self . post ( url , data ) <EOL> self . assertEqual ( return_value . status_code , <NUM_LIT> ) <EOL> data = { } <EOL> data [ '<STR_LIT:name>' ] = '<STR_LIT>' <EOL> data [ '<STR_LIT>' ] = adapter_id <EOL> data [ '<STR_LIT>' ] = <NUM_LIT:1> <EOL> data [ '<STR_LIT>' ] = <NUM_LIT:1> <EOL> return_value = self . post ( url , data ) <EOL> self . assertEqual ( return_value . status_code , <NUM_LIT> ) <EOL> def test_update_cluster ( self ) : <EOL> url = '<STR_LIT>' <EOL> data = { <EOL> '<STR_LIT:name>' : '<STR_LIT>' <EOL> } <EOL> return_value = self . put ( url , data ) <EOL> self . assertEqual ( return_value . status_code , <NUM_LIT:200> ) <EOL> self . assertEqual ( <EOL> json . loads ( return_value . get_data ( ) ) [ '<STR_LIT:name>' ] , <EOL> '<STR_LIT>' <EOL> ) <EOL> url = '<STR_LIT>' <EOL> data = { <EOL> '<STR_LIT:name>' : '<STR_LIT>' <EOL> } <EOL> return_value = self . put ( url , data ) <EOL> self . assertEqual ( return_value . status_code , <NUM_LIT> ) <EOL> url = '<STR_LIT>' <EOL> data = { <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> return_value = self . put ( url , data ) <EOL> self . assertEqual ( return_value . status_code , <NUM_LIT> ) <EOL> def test_delete_cluster_not_editable ( self ) : <EOL> self . user_object = ( <EOL> user_api . get_user_object ( <EOL> setting . COMPASS_ADMIN_EMAIL <EOL> ) <EOL> ) <EOL> cluster_api . update_cluster_state ( <EOL> <NUM_LIT:1> , <EOL> state = '<STR_LIT>' , <EOL> user = self . user_object , <EOL> ) <EOL> url = '<STR_LIT>' <EOL> return_value = self . delete ( url ) <EOL> self . assertEqual ( return_value . status_code , <NUM_LIT> ) <EOL> def test_delete_cluster ( self ) : <EOL> url = '<STR_LIT>' <EOL> return_value = self . delete ( url ) <EOL> self . assertEqual ( return_value . status_code , <NUM_LIT:200> ) <EOL> def test_list_cluster_hosts ( self ) : <EOL> url = '<STR_LIT>' <EOL> return_value = self . get ( url ) <EOL> resp = json . loads ( return_value . get_data ( ) ) <EOL> count = len ( resp ) <EOL> self . assertEqual ( count , <NUM_LIT:1> ) <EOL> self . assertEqual ( return_value . status_code , <NUM_LIT:200> ) <EOL> url = '<STR_LIT>' <EOL> return_value = self . get ( url ) <EOL> self . assertEqual ( return_value . status_code , <NUM_LIT> ) <EOL> def test_show_cluster_host ( self ) : <EOL> url = '<STR_LIT>' <EOL> return_value = self . get ( url ) <EOL> resp = json . loads ( return_value . get_data ( ) ) <EOL> self . assertEqual ( resp [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> url = '<STR_LIT>' <EOL> return_value = self . get ( url ) <EOL> self . assertEqual ( return_value . status_code , <NUM_LIT> ) <EOL> def test_add_cluster_host ( self ) : <EOL> url = '<STR_LIT>' <EOL> data = { <EOL> '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : <NUM_LIT:1> <EOL> } <EOL> return_value = self . post ( url , data ) <EOL> resp = json . loads ( return_value . get_data ( ) ) <EOL> self . assertEqual ( return_value . status_code , <NUM_LIT:200> ) <EOL> self . assertEqual ( resp [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> data = { <EOL> '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : <NUM_LIT:1> <EOL> } <EOL> return_value = self . post ( url , data ) <EOL> self . assertEqual ( return_value . status_code , <NUM_LIT> ) <EOL> def test_update_cluster_host ( self ) : <EOL> url = '<STR_LIT>' <EOL> data = { <EOL> '<STR_LIT>' : [ <EOL> '<STR_LIT>' <EOL> ] <EOL> } <EOL> return_value = self . put ( url , data ) <EOL> self . assertEqual ( return_value . status_code , <NUM_LIT:200> ) <EOL> def test_delete_cluster_host ( self ) : <EOL> url = '<STR_LIT>' <EOL> return_value = self . delete ( url ) <EOL> self . assertEqual ( return_value . status_code , <NUM_LIT:200> ) <EOL> url = '<STR_LIT>' <EOL> return_value = self . delete ( url ) <EOL> self . assertEqual ( return_value . status_code , <NUM_LIT> ) <EOL> class TestSubnetAPI ( ApiTestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> super ( TestSubnetAPI , self ) . setUp ( ) <EOL> url = '<STR_LIT>' <EOL> data = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:name>' : '<STR_LIT>' <EOL> } <EOL> self . post ( url , data ) <EOL> def tearDown ( self ) : <EOL> super ( TestSubnetAPI , self ) . tearDown ( ) <EOL> def test_list_subnets ( self ) : <EOL> url = '<STR_LIT>' <EOL> return_value = self . get ( url ) <EOL> resp = json . loads ( return_value . get_data ( ) ) <EOL> count = len ( resp ) <EOL> self . assertEqual ( count , <NUM_LIT:1> ) <EOL> self . assertEqual ( return_value . status_code , <NUM_LIT:200> ) <EOL> url = '<STR_LIT>' <EOL> return_value = self . get ( url ) <EOL> resp = json . loads ( return_value . get_data ( ) ) <EOL> self . assertEqual ( resp , [ ] ) <EOL> def test_show_subnet ( self ) : <EOL> url = '<STR_LIT>' <EOL> return_value = self . get ( url ) <EOL> resp = json . loads ( return_value . get_data ( ) ) <EOL> self . assertEqual ( return_value . status_code , <NUM_LIT:200> ) <EOL> self . assertEqual ( resp [ '<STR_LIT:name>' ] , '<STR_LIT>' ) <EOL> url = '<STR_LIT>' <EOL> return_value = self . get ( url ) <EOL> self . assertEqual ( return_value . status_code , <NUM_LIT> ) <EOL> def test_add_subnet ( self ) : <EOL> url = '<STR_LIT>' <EOL> data = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:name>' : '<STR_LIT>' <EOL> } <EOL> return_value = self . post ( url , data ) <EOL> self . assertEqual ( return_value . status_code , <NUM_LIT> ) <EOL> data = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:name>' : '<STR_LIT>' <EOL> } <EOL> return_value = self . post ( url , data ) <EOL> self . assertEqual ( return_value . status_code , <NUM_LIT> ) <EOL> def test_update_subnet ( self ) : <EOL> url = '<STR_LIT>' <EOL> data = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:name>' : '<STR_LIT>' <EOL> } <EOL> return_value = self . put ( url , data ) <EOL> resp = json . loads ( return_value . get_data ( ) ) <EOL> self . assertEqual ( return_value . status_code , <NUM_LIT:200> ) <EOL> self . assertTrue ( item in data . items ( ) for item in resp . items ( ) ) <EOL> url = '<STR_LIT>' <EOL> data = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:name>' : '<STR_LIT>' <EOL> } <EOL> return_value = self . put ( url , data ) <EOL> self . assertEqual ( return_value . status_code , <NUM_LIT> ) <EOL> url = '<STR_LIT>' <EOL> data = { <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> return_value = self . put ( url , data ) <EOL> self . assertEqual ( return_value . status_code , <NUM_LIT> ) <EOL> def test_delete_subnet ( self ) : <EOL> url = '<STR_LIT>' <EOL> return_value = self . delete ( url ) <EOL> self . assertEqual ( return_value . status_code , <NUM_LIT:200> ) <EOL> url = '<STR_LIT>' <EOL> return_value = self . delete ( url ) <EOL> self . assertEqual ( return_value . status_code , <NUM_LIT> ) <EOL> class TestSwitchAPI ( ApiTestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> super ( TestSwitchAPI , self ) . setUp ( ) <EOL> def tearDown ( self ) : <EOL> super ( TestSwitchAPI , self ) . tearDown ( ) <EOL> def test_list_switches ( self ) : <EOL> url = '<STR_LIT>' <EOL> return_value = self . get ( url ) <EOL> resp = json . loads ( return_value . get_data ( ) ) <EOL> count = len ( resp ) <EOL> self . assertEqual ( count , <NUM_LIT:2> ) <EOL> self . assertEqual ( return_value . status_code , <NUM_LIT:200> ) <EOL> url = '<STR_LIT>' <EOL> return_value = self . get ( url ) <EOL> self . assertEqual ( return_value . status_code , <NUM_LIT:200> ) <EOL> url = '<STR_LIT>' <EOL> return_value = json . loads ( self . get ( url ) . get_data ( ) ) <EOL> self . assertEqual ( [ ] , return_value ) <EOL> def test_show_switch ( self ) : <EOL> url = '<STR_LIT>' <EOL> return_value = self . get ( url ) <EOL> resp = json . loads ( return_value . get_data ( ) ) <EOL> self . assertEqual ( return_value . status_code , <NUM_LIT:200> ) <EOL> self . assertEqual ( resp [ '<STR_LIT>' ] , self . SWITCH_IP_ADDRESS ) <EOL> url = '<STR_LIT>' <EOL> return_value = self . get ( url ) <EOL> self . assertEqual ( return_value . status_code , <NUM_LIT> ) <EOL> def test_add_switch ( self ) : <EOL> url = '<STR_LIT>' <EOL> data = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : { <EOL> "<STR_LIT:version>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:state>' : '<STR_LIT>' <EOL> } <EOL> return_value = self . post ( url , data ) <EOL> resp = json . loads ( return_value . get_data ( ) ) <EOL> self . assertEqual ( return_value . status_code , <NUM_LIT:200> ) <EOL> self . assertEqual ( resp [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> data = { <EOL> '<STR_LIT>' : self . SWITCH_IP_ADDRESS , <EOL> '<STR_LIT>' : { <EOL> "<STR_LIT:version>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:state>' : '<STR_LIT>' <EOL> } <EOL> return_value = self . post ( url , data ) <EOL> self . assertEqual ( return_value . status_code , <NUM_LIT> ) <EOL> data = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> return_value = self . post ( url , data ) <EOL> self . assertEqual ( return_value . status_code , <NUM_LIT> ) <EOL> def test_add_switches ( self ) : <EOL> url = '<STR_LIT>' <EOL> data = [ <EOL> { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : { <EOL> "<STR_LIT:version>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } <EOL> } , { <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> ] <EOL> return_value = self . post ( url , data ) <EOL> resp = json . loads ( return_value . get_data ( ) ) <EOL> success = [ ] <EOL> fail = [ ] <EOL> for item in resp [ '<STR_LIT>' ] : <EOL> success . append ( item [ '<STR_LIT>' ] ) <EOL> for item in resp [ '<STR_LIT>' ] : <EOL> fail . append ( item [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( return_value . status_code , <NUM_LIT:200> ) <EOL> self . assertIn ( '<STR_LIT>' , success ) <EOL> self . assertIn ( '<STR_LIT>' , fail ) <EOL> def test_update_switch ( self ) : <EOL> url = '<STR_LIT>' <EOL> data = { <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> return_value = self . put ( url , data ) <EOL> resp = json . loads ( return_value . get_data ( ) ) <EOL> self . assertEqual ( return_value . status_code , <NUM_LIT:200> ) <EOL> self . assertEqual ( resp [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> url = '<STR_LIT>' <EOL> return_value = self . put ( url , data ) <EOL> self . assertEqual ( return_value . status_code , <NUM_LIT> ) <EOL> url = '<STR_LIT>' <EOL> data = { <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> return_value = self . put ( url , data ) <EOL> self . assertEqual ( return_value . status_code , <NUM_LIT> ) <EOL> def test_delete_switch ( self ) : <EOL> url = '<STR_LIT>' <EOL> return_value = self . delete ( url ) <EOL> self . assertEqual ( return_value . status_code , <NUM_LIT:200> ) <EOL> url = '<STR_LIT>' <EOL> return_value = self . delete ( url ) <EOL> self . assertEqual ( return_value . status_code , <NUM_LIT> ) <EOL> class TestAdapterAPI ( ApiTestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> super ( TestAdapterAPI , self ) . setUp ( ) <EOL> self . adapter_name = None <EOL> self . adapter_id = None <EOL> url = '<STR_LIT>' <EOL> adapters = json . loads ( self . get ( url ) . get_data ( ) ) <EOL> for adapter in adapters : <EOL> if adapter [ '<STR_LIT>' ] : <EOL> self . adapter_name = adapter [ '<STR_LIT:name>' ] <EOL> self . adapter_id = adapter [ '<STR_LIT:id>' ] <EOL> def tearDown ( self ) : <EOL> super ( TestAdapterAPI , self ) . tearDown ( ) <EOL> def test_list_adapters ( self ) : <EOL> url = '<STR_LIT>' <EOL> return_value = self . get ( url ) <EOL> resp = json . loads ( return_value . get_data ( ) ) <EOL> count = len ( resp ) <EOL> self . assertEqual ( count , <NUM_LIT:3> ) <EOL> self . assertEqual ( return_value . status_code , <NUM_LIT:200> ) <EOL> url = '<STR_LIT>' <EOL> return_value = json . loads ( self . get ( url ) . get_data ( ) ) <EOL> self . assertEqual ( [ ] , return_value ) <EOL> def test_show_adapter ( self ) : <EOL> url = '<STR_LIT>' % self . adapter_id <EOL> return_value = self . get ( url ) <EOL> resp = json . loads ( return_value . get_data ( ) ) <EOL> self . assertEqual ( return_value . status_code , <NUM_LIT:200> ) <EOL> self . assertEqual ( resp [ '<STR_LIT:name>' ] , self . adapter_name ) <EOL> url = '<STR_LIT>' <EOL> return_value = self . get ( url ) <EOL> self . assertEqual ( return_value . status_code , <NUM_LIT> ) <EOL> class TestHostAPI ( ApiTestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> super ( TestHostAPI , self ) . setUp ( ) <EOL> url = '<STR_LIT>' <EOL> datas = [ <EOL> { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:port>' : '<STR_LIT:1>' , <EOL> '<STR_LIT:location>' : '<STR_LIT>' <EOL> } , <EOL> { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:port>' : '<STR_LIT:1>' , <EOL> '<STR_LIT:location>' : '<STR_LIT>' <EOL> } <EOL> ] <EOL> machine_id = { } <EOL> for i , data in enumerate ( datas ) : <EOL> return_value = self . post ( url , data ) <EOL> resp = json . loads ( return_value . get_data ( ) ) <EOL> machine_id [ i ] = resp [ '<STR_LIT>' ] <EOL> url = '<STR_LIT>' <EOL> datas = [ <EOL> { <EOL> '<STR_LIT>' : machine_id [ <NUM_LIT:0> ] , <EOL> '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : True <EOL> } , <EOL> { <EOL> '<STR_LIT>' : machine_id [ <NUM_LIT:1> ] , <EOL> '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : True <EOL> } <EOL> ] <EOL> for data in datas : <EOL> self . post ( url , data ) <EOL> url = '<STR_LIT>' <EOL> data = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:name>' : '<STR_LIT>' <EOL> } <EOL> self . post ( url , data ) <EOL> url = '<STR_LIT>' <EOL> datas = [ <EOL> { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : False <EOL> } , <EOL> { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : False <EOL> } <EOL> ] <EOL> for data in datas : <EOL> self . post ( url , data ) <EOL> def tearDown ( self ) : <EOL> super ( TestHostAPI , self ) . tearDown ( ) <EOL> def test_list_hosts ( self ) : <EOL> url = '<STR_LIT>' <EOL> return_value = self . get ( url ) <EOL> resp = json . loads ( return_value . get_data ( ) ) <EOL> count = len ( resp ) <EOL> self . assertEqual ( count , <NUM_LIT:2> ) <EOL> self . assertEqual ( return_value . status_code , <NUM_LIT:200> ) <EOL> url = '<STR_LIT>' <EOL> return_value = self . get ( url ) <EOL> resp = json . loads ( return_value . get_data ( ) ) <EOL> self . assertEqual ( [ ] , resp ) <EOL> def test_show_host ( self ) : <EOL> url = '<STR_LIT>' <EOL> return_value = self . get ( url ) <EOL> resp = json . loads ( return_value . get_data ( ) ) <EOL> self . assertEqual ( return_value . status_code , <NUM_LIT:200> ) <EOL> self . assertEqual ( resp [ '<STR_LIT:name>' ] , '<STR_LIT>' ) <EOL> url = '<STR_LIT>' <EOL> return_value = self . get ( url ) <EOL> self . assertEqual ( return_value . status_code , <NUM_LIT> ) <EOL> def test_list_machines_or_hosts ( self ) : <EOL> url = '<STR_LIT>' <EOL> return_value = self . get ( url ) <EOL> self . assertEqual ( return_value . status_code , <NUM_LIT:200> ) <EOL> def test_show_machine_or_host ( self ) : <EOL> url = '<STR_LIT>' <EOL> return_value = self . get ( url ) <EOL> self . assertEqual ( return_value . status_code , <NUM_LIT:200> ) <EOL> def test_update_host ( self ) : <EOL> url = '<STR_LIT>' <EOL> data = { <EOL> '<STR_LIT:name>' : '<STR_LIT>' <EOL> } <EOL> return_value = self . put ( url , data ) <EOL> resp = json . loads ( return_value . get_data ( ) ) <EOL> self . assertEqual ( return_value . status_code , <NUM_LIT:200> ) <EOL> self . assertEqual ( resp [ '<STR_LIT:name>' ] , '<STR_LIT>' ) <EOL> data = { <EOL> '<STR_LIT:location>' : '<STR_LIT>' <EOL> } <EOL> return_value = self . put ( url , data ) <EOL> self . assertEqual ( return_value . status_code , <NUM_LIT> ) <EOL> self . assertIn ( '<STR_LIT>' , return_value . get_data ( ) ) <EOL> url = '<STR_LIT>' <EOL> data = { <EOL> '<STR_LIT:name>' : '<STR_LIT:host>' <EOL> } <EOL> return_value = self . put ( url , data ) <EOL> self . assertEqual ( return_value . status_code , <NUM_LIT> ) <EOL> def test_delete_host ( self ) : <EOL> url = '<STR_LIT>' <EOL> return_value = self . delete ( url ) <EOL> self . assertEqual ( return_value . status_code , <NUM_LIT:200> ) <EOL> url = '<STR_LIT>' <EOL> return_value = self . delete ( url ) <EOL> self . assertEqual ( return_value . status_code , <NUM_LIT> ) <EOL> def test_list_host_networks ( self ) : <EOL> url = '<STR_LIT>' <EOL> return_value = self . get ( url ) <EOL> resp = json . loads ( return_value . get_data ( ) ) <EOL> count = len ( resp ) <EOL> self . assertEqual ( count , <NUM_LIT:2> ) <EOL> self . assertEqual ( return_value . status_code , <NUM_LIT:200> ) <EOL> def test_show_host_network ( self ) : <EOL> url = '<STR_LIT>' <EOL> return_value = self . get ( url ) <EOL> resp = json . loads ( return_value . get_data ( ) ) <EOL> self . assertEqual ( return_value . status_code , <NUM_LIT:200> ) <EOL> self . assertEqual ( resp [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> class TestSwitchMachines ( ApiTestCase ) : <EOL> def setUp ( self ) : <EOL> super ( TestSwitchMachines , self ) . setUp ( ) <EOL> url = '<STR_LIT>' <EOL> datas = [ <EOL> { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:port>' : '<STR_LIT:1>' , <EOL> '<STR_LIT>' : [ <NUM_LIT> ] <EOL> } , <EOL> { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:port>' : '<STR_LIT:1>' , <EOL> '<STR_LIT>' : [ <NUM_LIT:1> ] <EOL> } <EOL> ] <EOL> for data in datas : <EOL> self . post ( url , data ) <EOL> def tearDown ( self ) : <EOL> super ( TestSwitchMachines , self ) . tearDown ( ) <EOL> def test_list_switch_machines ( self ) : <EOL> url = '<STR_LIT>' <EOL> return_value = self . get ( url ) <EOL> resp = json . loads ( return_value . get_data ( ) ) <EOL> count = len ( resp ) <EOL> self . assertEqual ( count , <NUM_LIT:2> ) <EOL> self . assertEqual ( return_value . status_code , <NUM_LIT:200> ) <EOL> url = '<STR_LIT>' <EOL> return_value = self . get ( url ) <EOL> self . assertEqual ( return_value . status_code , <NUM_LIT> ) <EOL> def test_add_switch_machine ( self ) : <EOL> url = '<STR_LIT>' <EOL> data = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:port>' : '<STR_LIT:1>' , <EOL> '<STR_LIT>' : [ <NUM_LIT:10> ] <EOL> } <EOL> return_value = self . post ( url , data ) <EOL> resp = json . loads ( return_value . get_data ( ) ) <EOL> self . assertEqual ( return_value . status_code , <NUM_LIT:200> ) <EOL> self . assertEqual ( resp [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> url = '<STR_LIT>' <EOL> data = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:port>' : '<STR_LIT:1>' , <EOL> '<STR_LIT>' : [ <NUM_LIT> ] <EOL> } <EOL> return_value = self . post ( url , data ) <EOL> self . assertEqual ( return_value . status_code , <NUM_LIT> ) <EOL> url = '<STR_LIT>' <EOL> data = { <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> return_value = self . post ( url , data ) <EOL> self . assertEqual ( return_value . status_code , <NUM_LIT> ) <EOL> def test_add_switch_machines ( self ) : <EOL> url = '<STR_LIT>' <EOL> return_value = self . get ( url ) <EOL> url = '<STR_LIT>' <EOL> data = [ { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:port>" : "<STR_LIT:100>" <EOL> } , { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:port>" : "<STR_LIT>" <EOL> } , { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:port>" : "<STR_LIT>" <EOL> } , { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:port>" : "<STR_LIT>" <EOL> } , { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:port>" : "<STR_LIT>" <EOL> } , { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:port>" : "<STR_LIT:100>" <EOL> } ] <EOL> return_value = self . post ( url , data ) <EOL> expected = [ { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:port>' : '<STR_LIT:100>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } , { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:port>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } , { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:port>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } ] <EOL> expect_duplicate = [ { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:port>' : '<STR_LIT>' } ] <EOL> expect_failed = [ <EOL> { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:port>' : '<STR_LIT:100>' } , <EOL> { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:port>' : '<STR_LIT>' } <EOL> ] <EOL> resp = json . loads ( return_value . get_data ( ) ) <EOL> res = [ ] <EOL> res_du = [ ] <EOL> res_fail = [ ] <EOL> for k , v in resp . items ( ) : <EOL> if k == '<STR_LIT>' : <EOL> for item in v : <EOL> res . append ( item ) <EOL> if k == '<STR_LIT>' : <EOL> for item in v : <EOL> res_du . append ( item ) <EOL> if k == '<STR_LIT>' : <EOL> for item in v : <EOL> res_fail . append ( item ) <EOL> self . assertEqual ( len ( res ) , len ( expected ) ) <EOL> for i , v in enumerate ( res ) : <EOL> self . assertDictContainsSubset ( <EOL> expected [ i ] , res [ i ] <EOL> ) <EOL> self . assertEqual ( len ( res_fail ) , len ( expect_failed ) ) <EOL> for i , v in enumerate ( res_fail ) : <EOL> self . assertDictContainsSubset ( <EOL> expect_failed [ i ] , res_fail [ i ] <EOL> ) <EOL> self . assertEqual ( len ( res_du ) , len ( expect_duplicate ) ) <EOL> for i , v in enumerate ( res_du ) : <EOL> self . assertDictContainsSubset ( <EOL> expect_duplicate [ i ] , res_du [ i ] <EOL> ) <EOL> def test_show_switch_machine ( self ) : <EOL> url = '<STR_LIT>' <EOL> return_value = self . get ( url ) <EOL> resp = json . loads ( return_value . get_data ( ) ) <EOL> self . assertEqual ( return_value . status_code , <NUM_LIT:200> ) <EOL> self . assertEqual ( resp [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> url = '<STR_LIT>' <EOL> return_value = self . get ( url ) <EOL> self . assertEqual ( return_value . status_code , <NUM_LIT> ) <EOL> def test_update_switch_machine ( self ) : <EOL> url = '<STR_LIT>' <EOL> data = { <EOL> '<STR_LIT:port>' : '<STR_LIT:100>' <EOL> } <EOL> return_value = self . put ( url , data ) <EOL> resp = json . loads ( return_value . get_data ( ) ) <EOL> self . assertEqual ( return_value . status_code , <NUM_LIT:200> ) <EOL> self . assertEqual ( resp [ '<STR_LIT:port>' ] , '<STR_LIT:100>' ) <EOL> url = '<STR_LIT>' <EOL> data = { <EOL> '<STR_LIT:port>' : '<STR_LIT>' <EOL> } <EOL> return_value = self . put ( url , data ) <EOL> self . assertEqual ( return_value . status_code , <NUM_LIT> ) <EOL> url = '<STR_LIT>' <EOL> data = { <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> return_value = self . put ( url , data ) <EOL> self . assertEqual ( return_value . status_code , <NUM_LIT> ) <EOL> self . assertIn ( '<STR_LIT>' , return_value . get_data ( ) ) <EOL> def test_delete_switch_machine ( self ) : <EOL> url = '<STR_LIT>' <EOL> return_value = self . delete ( url ) <EOL> self . assertEqual ( return_value . status_code , <NUM_LIT:200> ) <EOL> class TestMetadataAPI ( ApiTestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> super ( TestMetadataAPI , self ) . setUp ( ) <EOL> def tearDown ( self ) : <EOL> super ( TestMetadataAPI , self ) . tearDown ( ) <EOL> def test_get_os_ui_metadata ( self ) : <EOL> url = '<STR_LIT>' % self . os_id <EOL> return_value = self . get ( url ) <EOL> self . assertEqual ( return_value . status_code , <NUM_LIT:200> ) <EOL> self . assertIn ( '<STR_LIT>' , return_value . get_data ( ) ) <EOL> def test_get_flavor_ui_metadata ( self ) : <EOL> url = '<STR_LIT>' % self . flavor_id <EOL> return_value = self . get ( url ) <EOL> self . assertEqual ( return_value . status_code , <NUM_LIT:200> ) <EOL> self . assertIn ( '<STR_LIT>' , return_value . get_data ( ) ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> flags . init ( ) <EOL> logsetting . init ( ) <EOL> unittest2 . main ( ) </s>
