<s> import time <EOL> import unittest <EOL> from nova . log import logging <EOL> from nova . tests . integrated import integrated_helpers <EOL> from nova . tests . integrated . api import client <EOL> LOG = logging . getLogger ( '<STR_LIT>' ) <EOL> class ServersTest ( integrated_helpers . _IntegratedTestBase ) : <EOL> def _wait_for_creation ( self , server ) : <EOL> retries = <NUM_LIT:0> <EOL> while server [ '<STR_LIT:status>' ] == '<STR_LIT>' : <EOL> time . sleep ( <NUM_LIT:1> ) <EOL> server = self . api . get_server ( server [ '<STR_LIT:id>' ] ) <EOL> print server <EOL> retries = retries + <NUM_LIT:1> <EOL> if retries > <NUM_LIT:5> : <EOL> break <EOL> return server <EOL> def test_get_servers ( self ) : <EOL> """<STR_LIT>""" <EOL> servers = self . api . get_servers ( ) <EOL> for server in servers : <EOL> LOG . debug ( "<STR_LIT>" % server ) <EOL> def test_create_and_delete_server ( self ) : <EOL> """<STR_LIT>""" <EOL> self . flags ( stub_network = True ) <EOL> server = { } <EOL> good_server = self . _build_minimal_create_server_request ( ) <EOL> post = { '<STR_LIT>' : server } <EOL> self . assertRaises ( client . OpenStackApiException , <EOL> self . api . post_server , post ) <EOL> server [ '<STR_LIT>' ] = self . get_invalid_image ( ) <EOL> self . assertRaises ( client . OpenStackApiException , <EOL> self . api . post_server , post ) <EOL> server [ '<STR_LIT>' ] = good_server . get ( '<STR_LIT>' ) <EOL> server [ '<STR_LIT>' ] = good_server . get ( '<STR_LIT>' ) <EOL> self . assertRaises ( client . OpenStackApiException , <EOL> self . api . post_server , post ) <EOL> server [ '<STR_LIT>' ] = good_server . get ( '<STR_LIT>' ) <EOL> server [ '<STR_LIT>' ] = good_server . get ( '<STR_LIT>' ) <EOL> self . assertRaises ( client . OpenStackApiException , <EOL> self . api . post_server , post ) <EOL> server [ '<STR_LIT:name>' ] = good_server [ '<STR_LIT:name>' ] <EOL> created_server = self . api . post_server ( post ) <EOL> LOG . debug ( "<STR_LIT>" % created_server ) <EOL> self . assertTrue ( created_server [ '<STR_LIT:id>' ] ) <EOL> created_server_id = created_server [ '<STR_LIT:id>' ] <EOL> found_server = self . api . get_server ( created_server_id ) <EOL> self . assertEqual ( created_server_id , found_server [ '<STR_LIT:id>' ] ) <EOL> servers = self . api . get_servers ( ) <EOL> server_ids = [ server [ '<STR_LIT:id>' ] for server in servers ] <EOL> self . assertTrue ( created_server_id in server_ids ) <EOL> found_server = self . _wait_for_creation ( found_server ) <EOL> self . assertEqual ( '<STR_LIT>' , found_server [ '<STR_LIT:status>' ] ) <EOL> servers = self . api . get_servers ( detail = True ) <EOL> for server in servers : <EOL> self . assertTrue ( "<STR_LIT:image>" in server ) <EOL> self . assertTrue ( "<STR_LIT>" in server ) <EOL> self . _delete_server ( created_server_id ) <EOL> def _delete_server ( self , server_id ) : <EOL> self . api . delete_server ( server_id ) <EOL> for _retries in range ( <NUM_LIT:5> ) : <EOL> try : <EOL> found_server = self . api . get_server ( server_id ) <EOL> except client . OpenStackApiNotFoundException : <EOL> found_server = None <EOL> LOG . debug ( "<STR_LIT>" ) <EOL> break <EOL> LOG . debug ( "<STR_LIT>" % found_server ) <EOL> time . sleep ( <NUM_LIT:1> ) <EOL> self . assertFalse ( found_server ) <EOL> def test_create_server_with_metadata ( self ) : <EOL> """<STR_LIT>""" <EOL> server = self . _build_minimal_create_server_request ( ) <EOL> metadata = { } <EOL> for i in range ( <NUM_LIT:30> ) : <EOL> metadata [ '<STR_LIT>' % i ] = '<STR_LIT>' % i <EOL> server [ '<STR_LIT>' ] = metadata <EOL> post = { '<STR_LIT>' : server } <EOL> created_server = self . api . post_server ( post ) <EOL> LOG . debug ( "<STR_LIT>" % created_server ) <EOL> self . assertTrue ( created_server [ '<STR_LIT:id>' ] ) <EOL> created_server_id = created_server [ '<STR_LIT:id>' ] <EOL> self . assertEqual ( metadata , created_server . get ( '<STR_LIT>' ) ) <EOL> found_server = self . api . get_server ( created_server_id ) <EOL> self . assertEqual ( created_server_id , found_server [ '<STR_LIT:id>' ] ) <EOL> self . assertEqual ( metadata , found_server . get ( '<STR_LIT>' ) ) <EOL> servers = self . api . get_servers ( detail = True ) <EOL> server_map = dict ( ( server [ '<STR_LIT:id>' ] , server ) for server in servers ) <EOL> found_server = server_map . get ( created_server_id ) <EOL> self . assertTrue ( found_server ) <EOL> self . assertEqual ( metadata , found_server . get ( '<STR_LIT>' ) ) <EOL> servers = self . api . get_servers ( detail = False ) <EOL> server_map = dict ( ( server [ '<STR_LIT:id>' ] , server ) for server in servers ) <EOL> found_server = server_map . get ( created_server_id ) <EOL> self . assertTrue ( found_server ) <EOL> self . assertFalse ( found_server . get ( '<STR_LIT>' ) ) <EOL> self . _delete_server ( created_server_id ) <EOL> def test_create_and_rebuild_server ( self ) : <EOL> """<STR_LIT>""" <EOL> self . flags ( stub_network = True ) <EOL> server = self . _build_minimal_create_server_request ( ) <EOL> server_post = { '<STR_LIT>' : server } <EOL> created_server = self . api . post_server ( server_post ) <EOL> LOG . debug ( "<STR_LIT>" % created_server ) <EOL> self . assertTrue ( created_server [ '<STR_LIT:id>' ] ) <EOL> created_server_id = created_server [ '<STR_LIT:id>' ] <EOL> created_server = self . _wait_for_creation ( created_server ) <EOL> post = { } <EOL> post [ '<STR_LIT>' ] = { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:name>" : "<STR_LIT>" , <EOL> } <EOL> self . api . post_server_action ( created_server_id , post ) <EOL> LOG . debug ( "<STR_LIT>" % created_server ) <EOL> self . assertTrue ( created_server [ '<STR_LIT:id>' ] ) <EOL> found_server = self . api . get_server ( created_server_id ) <EOL> self . assertEqual ( created_server_id , found_server [ '<STR_LIT:id>' ] ) <EOL> self . assertEqual ( { } , found_server . get ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( '<STR_LIT>' , found_server . get ( '<STR_LIT:name>' ) ) <EOL> self . assertEqual ( '<STR_LIT:3>' , found_server . get ( '<STR_LIT:image>' ) [ '<STR_LIT:id>' ] ) <EOL> self . _delete_server ( created_server_id ) <EOL> def test_create_and_rebuild_server_with_metadata ( self ) : <EOL> """<STR_LIT>""" <EOL> self . flags ( stub_network = True ) <EOL> server = self . _build_minimal_create_server_request ( ) <EOL> server_post = { '<STR_LIT>' : server } <EOL> created_server = self . api . post_server ( server_post ) <EOL> LOG . debug ( "<STR_LIT>" % created_server ) <EOL> self . assertTrue ( created_server [ '<STR_LIT:id>' ] ) <EOL> created_server_id = created_server [ '<STR_LIT:id>' ] <EOL> created_server = self . _wait_for_creation ( created_server ) <EOL> post = { } <EOL> post [ '<STR_LIT>' ] = { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:name>" : "<STR_LIT>" , <EOL> } <EOL> metadata = { } <EOL> for i in range ( <NUM_LIT:30> ) : <EOL> metadata [ '<STR_LIT>' % i ] = '<STR_LIT>' % i <EOL> post [ '<STR_LIT>' ] [ '<STR_LIT>' ] = metadata <EOL> self . api . post_server_action ( created_server_id , post ) <EOL> LOG . debug ( "<STR_LIT>" % created_server ) <EOL> self . assertTrue ( created_server [ '<STR_LIT:id>' ] ) <EOL> found_server = self . api . get_server ( created_server_id ) <EOL> self . assertEqual ( created_server_id , found_server [ '<STR_LIT:id>' ] ) <EOL> self . assertEqual ( metadata , found_server . get ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( '<STR_LIT>' , found_server . get ( '<STR_LIT:name>' ) ) <EOL> self . _delete_server ( created_server_id ) <EOL> def test_create_and_rebuild_server_with_metadata_removal ( self ) : <EOL> """<STR_LIT>""" <EOL> self . flags ( stub_network = True ) <EOL> server = self . _build_minimal_create_server_request ( ) <EOL> server_post = { '<STR_LIT>' : server } <EOL> metadata = { } <EOL> for i in range ( <NUM_LIT:30> ) : <EOL> metadata [ '<STR_LIT>' % i ] = '<STR_LIT>' % i <EOL> server_post [ '<STR_LIT>' ] [ '<STR_LIT>' ] = metadata <EOL> created_server = self . api . post_server ( server_post ) <EOL> LOG . debug ( "<STR_LIT>" % created_server ) <EOL> self . assertTrue ( created_server [ '<STR_LIT:id>' ] ) <EOL> created_server_id = created_server [ '<STR_LIT:id>' ] <EOL> created_server = self . _wait_for_creation ( created_server ) <EOL> post = { } <EOL> post [ '<STR_LIT>' ] = { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:name>" : "<STR_LIT>" , <EOL> } <EOL> metadata = { } <EOL> post [ '<STR_LIT>' ] [ '<STR_LIT>' ] = metadata <EOL> self . api . post_server_action ( created_server_id , post ) <EOL> LOG . debug ( "<STR_LIT>" % created_server ) <EOL> self . assertTrue ( created_server [ '<STR_LIT:id>' ] ) <EOL> found_server = self . api . get_server ( created_server_id ) <EOL> self . assertEqual ( created_server_id , found_server [ '<STR_LIT:id>' ] ) <EOL> self . assertEqual ( metadata , found_server . get ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( '<STR_LIT>' , found_server . get ( '<STR_LIT:name>' ) ) <EOL> self . _delete_server ( created_server_id ) <EOL> def test_rename_server ( self ) : <EOL> """<STR_LIT>""" <EOL> server = self . _build_minimal_create_server_request ( ) <EOL> created_server = self . api . post_server ( { '<STR_LIT>' : server } ) <EOL> LOG . debug ( "<STR_LIT>" % created_server ) <EOL> server_id = created_server [ '<STR_LIT:id>' ] <EOL> self . assertTrue ( server_id ) <EOL> self . api . put_server ( server_id , { '<STR_LIT>' : { '<STR_LIT:name>' : '<STR_LIT>' } } ) <EOL> created_server = self . api . get_server ( server_id ) <EOL> self . assertEqual ( created_server [ '<STR_LIT:name>' ] , '<STR_LIT>' ) <EOL> self . _delete_server ( server_id ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> unittest . main ( ) </s>
