<s> import json <EOL> import webob <EOL> from xml . dom import minidom <EOL> from nova import exception <EOL> from nova import flags <EOL> from nova import test <EOL> from nova . tests . api . openstack import fakes <EOL> import nova . wsgi <EOL> FLAGS = flags . FLAGS <EOL> def return_create_instance_metadata_max ( context , server_id , metadata , delete ) : <EOL> return stub_max_server_metadata ( ) <EOL> def return_create_instance_metadata ( context , server_id , metadata , delete ) : <EOL> return stub_server_metadata ( ) <EOL> def return_server_metadata ( context , server_id ) : <EOL> return stub_server_metadata ( ) <EOL> def return_empty_server_metadata ( context , server_id ) : <EOL> return { } <EOL> def delete_server_metadata ( context , server_id , key ) : <EOL> pass <EOL> def stub_server_metadata ( ) : <EOL> metadata = { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> } <EOL> return metadata <EOL> def stub_max_server_metadata ( ) : <EOL> metadata = { "<STR_LIT>" : { } } <EOL> for num in range ( FLAGS . quota_metadata_items ) : <EOL> metadata [ '<STR_LIT>' ] [ '<STR_LIT>' % num ] = "<STR_LIT>" <EOL> return metadata <EOL> def return_server ( context , server_id ) : <EOL> return { '<STR_LIT:id>' : server_id } <EOL> def return_server_nonexistant ( context , server_id ) : <EOL> raise exception . InstanceNotFound ( ) <EOL> class ServerMetaDataTest ( test . TestCase ) : <EOL> def setUp ( self ) : <EOL> super ( ServerMetaDataTest , self ) . setUp ( ) <EOL> fakes . stub_out_key_pair_funcs ( self . stubs ) <EOL> self . stubs . Set ( nova . db . api , '<STR_LIT>' , return_server ) <EOL> def test_index ( self ) : <EOL> self . stubs . Set ( nova . db . api , '<STR_LIT>' , <EOL> return_server_metadata ) <EOL> req = webob . Request . blank ( '<STR_LIT>' ) <EOL> res = req . get_response ( fakes . wsgi_app ( ) ) <EOL> self . assertEqual ( <NUM_LIT:200> , res . status_int ) <EOL> res_dict = json . loads ( res . body ) <EOL> self . assertEqual ( '<STR_LIT:application/json>' , res . headers [ '<STR_LIT:Content-Type>' ] ) <EOL> expected = { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } , <EOL> } <EOL> self . assertEqual ( expected , res_dict ) <EOL> def test_index_xml ( self ) : <EOL> self . stubs . Set ( nova . db . api , '<STR_LIT>' , <EOL> return_server_metadata ) <EOL> request = webob . Request . blank ( "<STR_LIT>" ) <EOL> request . accept = "<STR_LIT>" <EOL> response = request . get_response ( fakes . wsgi_app ( ) ) <EOL> self . assertEqual ( <NUM_LIT:200> , response . status_int ) <EOL> self . assertEqual ( "<STR_LIT>" , response . content_type ) <EOL> actual_metadata = minidom . parseString ( response . body . replace ( "<STR_LIT:U+0020>" , "<STR_LIT>" ) ) <EOL> expected_metadata = minidom . parseString ( """<STR_LIT>""" . replace ( "<STR_LIT:U+0020>" , "<STR_LIT>" ) . replace ( "<STR_LIT:\n>" , "<STR_LIT>" ) ) <EOL> self . assertEqual ( expected_metadata . toxml ( ) , actual_metadata . toxml ( ) ) <EOL> def test_index_nonexistant_server ( self ) : <EOL> self . stubs . Set ( nova . db . api , '<STR_LIT>' , return_server_nonexistant ) <EOL> req = webob . Request . blank ( '<STR_LIT>' ) <EOL> res = req . get_response ( fakes . wsgi_app ( ) ) <EOL> self . assertEqual ( <NUM_LIT> , res . status_int ) <EOL> def test_index_no_data ( self ) : <EOL> self . stubs . Set ( nova . db . api , '<STR_LIT>' , <EOL> return_empty_server_metadata ) <EOL> req = webob . Request . blank ( '<STR_LIT>' ) <EOL> res = req . get_response ( fakes . wsgi_app ( ) ) <EOL> self . assertEqual ( <NUM_LIT:200> , res . status_int ) <EOL> res_dict = json . loads ( res . body ) <EOL> expected = { '<STR_LIT>' : { } } <EOL> self . assertEqual ( expected , res_dict ) <EOL> def test_show ( self ) : <EOL> self . stubs . Set ( nova . db . api , '<STR_LIT>' , <EOL> return_server_metadata ) <EOL> req = webob . Request . blank ( '<STR_LIT>' ) <EOL> res = req . get_response ( fakes . wsgi_app ( ) ) <EOL> res_dict = json . loads ( res . body ) <EOL> self . assertEqual ( <NUM_LIT:200> , res . status_int ) <EOL> expected = { '<STR_LIT>' : { '<STR_LIT>' : '<STR_LIT>' } } <EOL> self . assertEqual ( expected , res_dict ) <EOL> def test_show_xml ( self ) : <EOL> self . stubs . Set ( nova . db . api , '<STR_LIT>' , <EOL> return_server_metadata ) <EOL> request = webob . Request . blank ( "<STR_LIT>" ) <EOL> request . accept = "<STR_LIT>" <EOL> response = request . get_response ( fakes . wsgi_app ( ) ) <EOL> self . assertEqual ( <NUM_LIT:200> , response . status_int ) <EOL> self . assertEqual ( "<STR_LIT>" , response . content_type ) <EOL> actual_metadata = minidom . parseString ( response . body . replace ( "<STR_LIT:U+0020>" , "<STR_LIT>" ) ) <EOL> expected_metadata = minidom . parseString ( """<STR_LIT>""" . replace ( "<STR_LIT:U+0020>" , "<STR_LIT>" ) . replace ( "<STR_LIT:\n>" , "<STR_LIT>" ) ) <EOL> self . assertEqual ( expected_metadata . toxml ( ) , actual_metadata . toxml ( ) ) <EOL> def test_show_nonexistant_server ( self ) : <EOL> self . stubs . Set ( nova . db . api , '<STR_LIT>' , return_server_nonexistant ) <EOL> req = webob . Request . blank ( '<STR_LIT>' ) <EOL> res = req . get_response ( fakes . wsgi_app ( ) ) <EOL> self . assertEqual ( <NUM_LIT> , res . status_int ) <EOL> def test_show_meta_not_found ( self ) : <EOL> self . stubs . Set ( nova . db . api , '<STR_LIT>' , <EOL> return_empty_server_metadata ) <EOL> req = webob . Request . blank ( '<STR_LIT>' ) <EOL> res = req . get_response ( fakes . wsgi_app ( ) ) <EOL> self . assertEqual ( <NUM_LIT> , res . status_int ) <EOL> def test_delete ( self ) : <EOL> self . stubs . Set ( nova . db . api , '<STR_LIT>' , <EOL> return_server_metadata ) <EOL> self . stubs . Set ( nova . db . api , '<STR_LIT>' , <EOL> delete_server_metadata ) <EOL> req = webob . Request . blank ( '<STR_LIT>' ) <EOL> req . method = '<STR_LIT>' <EOL> res = req . get_response ( fakes . wsgi_app ( ) ) <EOL> self . assertEqual ( <NUM_LIT> , res . status_int ) <EOL> self . assertEqual ( '<STR_LIT>' , res . body ) <EOL> def test_delete_nonexistant_server ( self ) : <EOL> self . stubs . Set ( nova . db . api , '<STR_LIT>' , return_server_nonexistant ) <EOL> req = webob . Request . blank ( '<STR_LIT>' ) <EOL> req . method = '<STR_LIT>' <EOL> res = req . get_response ( fakes . wsgi_app ( ) ) <EOL> self . assertEqual ( <NUM_LIT> , res . status_int ) <EOL> def test_delete_meta_not_found ( self ) : <EOL> self . stubs . Set ( nova . db . api , '<STR_LIT>' , <EOL> return_empty_server_metadata ) <EOL> req = webob . Request . blank ( '<STR_LIT>' ) <EOL> req . method = '<STR_LIT>' <EOL> res = req . get_response ( fakes . wsgi_app ( ) ) <EOL> self . assertEqual ( <NUM_LIT> , res . status_int ) <EOL> def test_create ( self ) : <EOL> self . stubs . Set ( nova . db . api , '<STR_LIT>' , <EOL> return_server_metadata ) <EOL> self . stubs . Set ( nova . db . api , '<STR_LIT>' , <EOL> return_create_instance_metadata ) <EOL> req = webob . Request . blank ( '<STR_LIT>' ) <EOL> req . method = '<STR_LIT:POST>' <EOL> req . content_type = "<STR_LIT:application/json>" <EOL> input = { "<STR_LIT>" : { "<STR_LIT>" : "<STR_LIT>" } } <EOL> req . body = json . dumps ( input ) <EOL> res = req . get_response ( fakes . wsgi_app ( ) ) <EOL> self . assertEqual ( <NUM_LIT:200> , res . status_int ) <EOL> res_dict = json . loads ( res . body ) <EOL> input [ '<STR_LIT>' ] . update ( { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> } ) <EOL> self . assertEqual ( input , res_dict ) <EOL> def test_create_xml ( self ) : <EOL> self . stubs . Set ( nova . db . api , '<STR_LIT>' , <EOL> return_server_metadata ) <EOL> self . stubs . Set ( nova . db . api , "<STR_LIT>" , <EOL> return_create_instance_metadata ) <EOL> req = webob . Request . blank ( "<STR_LIT>" ) <EOL> req . method = "<STR_LIT:POST>" <EOL> req . content_type = "<STR_LIT>" <EOL> req . accept = "<STR_LIT>" <EOL> request_metadata = minidom . parseString ( """<STR_LIT>""" . replace ( "<STR_LIT:U+0020>" , "<STR_LIT>" ) . replace ( "<STR_LIT:\n>" , "<STR_LIT>" ) ) <EOL> req . body = str ( request_metadata . toxml ( ) ) <EOL> response = req . get_response ( fakes . wsgi_app ( ) ) <EOL> expected_metadata = minidom . parseString ( """<STR_LIT>""" . replace ( "<STR_LIT:U+0020>" , "<STR_LIT>" ) . replace ( "<STR_LIT:\n>" , "<STR_LIT>" ) ) <EOL> self . assertEqual ( <NUM_LIT:200> , response . status_int ) <EOL> actual_metadata = minidom . parseString ( response . body ) <EOL> self . assertEqual ( expected_metadata . toxml ( ) , actual_metadata . toxml ( ) ) <EOL> def test_create_empty_body ( self ) : <EOL> self . stubs . Set ( nova . db . api , '<STR_LIT>' , <EOL> return_create_instance_metadata ) <EOL> req = webob . Request . blank ( '<STR_LIT>' ) <EOL> req . method = '<STR_LIT:POST>' <EOL> req . headers [ "<STR_LIT>" ] = "<STR_LIT:application/json>" <EOL> res = req . get_response ( fakes . wsgi_app ( ) ) <EOL> self . assertEqual ( <NUM_LIT> , res . status_int ) <EOL> def test_create_nonexistant_server ( self ) : <EOL> self . stubs . Set ( nova . db . api , '<STR_LIT>' , return_server_nonexistant ) <EOL> req = webob . Request . blank ( '<STR_LIT>' ) <EOL> req . method = '<STR_LIT:POST>' <EOL> req . body = '<STR_LIT>' <EOL> req . headers [ "<STR_LIT>" ] = "<STR_LIT:application/json>" <EOL> res = req . get_response ( fakes . wsgi_app ( ) ) <EOL> self . assertEqual ( <NUM_LIT> , res . status_int ) <EOL> def test_update_all ( self ) : <EOL> self . stubs . Set ( nova . db . api , '<STR_LIT>' , <EOL> return_create_instance_metadata ) <EOL> req = webob . Request . blank ( '<STR_LIT>' ) <EOL> req . method = '<STR_LIT>' <EOL> req . content_type = "<STR_LIT:application/json>" <EOL> expected = { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } , <EOL> } <EOL> req . body = json . dumps ( expected ) <EOL> res = req . get_response ( fakes . wsgi_app ( ) ) <EOL> self . assertEqual ( <NUM_LIT:200> , res . status_int ) <EOL> res_dict = json . loads ( res . body ) <EOL> self . assertEqual ( expected , res_dict ) <EOL> def test_update_all_empty_container ( self ) : <EOL> self . stubs . Set ( nova . db . api , '<STR_LIT>' , <EOL> return_create_instance_metadata ) <EOL> req = webob . Request . blank ( '<STR_LIT>' ) <EOL> req . method = '<STR_LIT>' <EOL> req . content_type = "<STR_LIT:application/json>" <EOL> expected = { '<STR_LIT>' : { } } <EOL> req . body = json . dumps ( expected ) <EOL> res = req . get_response ( fakes . wsgi_app ( ) ) <EOL> self . assertEqual ( <NUM_LIT:200> , res . status_int ) <EOL> res_dict = json . loads ( res . body ) <EOL> self . assertEqual ( expected , res_dict ) <EOL> def test_update_all_malformed_container ( self ) : <EOL> self . stubs . Set ( nova . db . api , '<STR_LIT>' , <EOL> return_create_instance_metadata ) <EOL> req = webob . Request . blank ( '<STR_LIT>' ) <EOL> req . method = '<STR_LIT>' <EOL> req . content_type = "<STR_LIT:application/json>" <EOL> expected = { '<STR_LIT>' : { } } <EOL> req . body = json . dumps ( expected ) <EOL> res = req . get_response ( fakes . wsgi_app ( ) ) <EOL> self . assertEqual ( <NUM_LIT> , res . status_int ) <EOL> def test_update_all_malformed_data ( self ) : <EOL> self . stubs . Set ( nova . db . api , '<STR_LIT>' , <EOL> return_create_instance_metadata ) <EOL> req = webob . Request . blank ( '<STR_LIT>' ) <EOL> req . method = '<STR_LIT>' <EOL> req . content_type = "<STR_LIT:application/json>" <EOL> expected = { '<STR_LIT>' : [ '<STR_LIT>' ] } <EOL> req . body = json . dumps ( expected ) <EOL> res = req . get_response ( fakes . wsgi_app ( ) ) <EOL> self . assertEqual ( <NUM_LIT> , res . status_int ) <EOL> def test_update_all_nonexistant_server ( self ) : <EOL> self . stubs . Set ( nova . db . api , '<STR_LIT>' , return_server_nonexistant ) <EOL> req = webob . Request . blank ( '<STR_LIT>' ) <EOL> req . method = '<STR_LIT>' <EOL> req . content_type = "<STR_LIT:application/json>" <EOL> req . body = json . dumps ( { '<STR_LIT>' : { '<STR_LIT>' : '<STR_LIT>' } } ) <EOL> res = req . get_response ( fakes . wsgi_app ( ) ) <EOL> self . assertEqual ( <NUM_LIT> , res . status_int ) <EOL> def test_update_item ( self ) : <EOL> self . stubs . Set ( nova . db . api , '<STR_LIT>' , <EOL> return_create_instance_metadata ) <EOL> req = webob . Request . blank ( '<STR_LIT>' ) <EOL> req . method = '<STR_LIT>' <EOL> req . body = '<STR_LIT>' <EOL> req . headers [ "<STR_LIT>" ] = "<STR_LIT:application/json>" <EOL> res = req . get_response ( fakes . wsgi_app ( ) ) <EOL> self . assertEqual ( <NUM_LIT:200> , res . status_int ) <EOL> self . assertEqual ( '<STR_LIT:application/json>' , res . headers [ '<STR_LIT:Content-Type>' ] ) <EOL> res_dict = json . loads ( res . body ) <EOL> expected = { '<STR_LIT>' : { '<STR_LIT>' : '<STR_LIT>' } } <EOL> self . assertEqual ( expected , res_dict ) <EOL> def test_update_item_xml ( self ) : <EOL> self . stubs . Set ( nova . db . api , '<STR_LIT>' , <EOL> return_create_instance_metadata ) <EOL> req = webob . Request . blank ( '<STR_LIT>' ) <EOL> req . method = '<STR_LIT>' <EOL> req . accept = "<STR_LIT:application/json>" <EOL> req . content_type = "<STR_LIT>" <EOL> req . body = """<STR_LIT>""" <EOL> res = req . get_response ( fakes . wsgi_app ( ) ) <EOL> self . assertEqual ( <NUM_LIT:200> , res . status_int ) <EOL> self . assertEqual ( '<STR_LIT:application/json>' , res . headers [ '<STR_LIT:Content-Type>' ] ) <EOL> res_dict = json . loads ( res . body ) <EOL> expected = { '<STR_LIT>' : { '<STR_LIT>' : '<STR_LIT>' } } <EOL> self . assertEqual ( expected , res_dict ) <EOL> def test_update_item_nonexistant_server ( self ) : <EOL> self . stubs . Set ( nova . db . api , '<STR_LIT>' , return_server_nonexistant ) <EOL> req = webob . Request . blank ( '<STR_LIT>' ) <EOL> req . method = '<STR_LIT>' <EOL> req . body = '<STR_LIT>' <EOL> req . headers [ "<STR_LIT>" ] = "<STR_LIT:application/json>" <EOL> res = req . get_response ( fakes . wsgi_app ( ) ) <EOL> self . assertEqual ( <NUM_LIT> , res . status_int ) <EOL> def test_update_item_empty_body ( self ) : <EOL> self . stubs . Set ( nova . db . api , '<STR_LIT>' , <EOL> return_create_instance_metadata ) <EOL> req = webob . Request . blank ( '<STR_LIT>' ) <EOL> req . method = '<STR_LIT>' <EOL> req . headers [ "<STR_LIT>" ] = "<STR_LIT:application/json>" <EOL> res = req . get_response ( fakes . wsgi_app ( ) ) <EOL> self . assertEqual ( <NUM_LIT> , res . status_int ) <EOL> def test_update_item_too_many_keys ( self ) : <EOL> self . stubs . Set ( nova . db . api , '<STR_LIT>' , <EOL> return_create_instance_metadata ) <EOL> req = webob . Request . blank ( '<STR_LIT>' ) <EOL> req . method = '<STR_LIT>' <EOL> req . body = '<STR_LIT>' <EOL> req . headers [ "<STR_LIT>" ] = "<STR_LIT:application/json>" <EOL> res = req . get_response ( fakes . wsgi_app ( ) ) <EOL> self . assertEqual ( <NUM_LIT> , res . status_int ) <EOL> def test_update_item_body_uri_mismatch ( self ) : <EOL> self . stubs . Set ( nova . db . api , '<STR_LIT>' , <EOL> return_create_instance_metadata ) <EOL> req = webob . Request . blank ( '<STR_LIT>' ) <EOL> req . method = '<STR_LIT>' <EOL> req . body = '<STR_LIT>' <EOL> req . headers [ "<STR_LIT>" ] = "<STR_LIT:application/json>" <EOL> res = req . get_response ( fakes . wsgi_app ( ) ) <EOL> self . assertEqual ( <NUM_LIT> , res . status_int ) <EOL> def test_too_many_metadata_items_on_create ( self ) : <EOL> self . stubs . Set ( nova . db . api , '<STR_LIT>' , <EOL> return_create_instance_metadata ) <EOL> data = { "<STR_LIT>" : { } } <EOL> for num in range ( FLAGS . quota_metadata_items + <NUM_LIT:1> ) : <EOL> data [ '<STR_LIT>' ] [ '<STR_LIT>' % num ] = "<STR_LIT>" <EOL> json_string = str ( data ) . replace ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> req = webob . Request . blank ( '<STR_LIT>' ) <EOL> req . method = '<STR_LIT:POST>' <EOL> req . body = json_string <EOL> req . headers [ "<STR_LIT>" ] = "<STR_LIT:application/json>" <EOL> res = req . get_response ( fakes . wsgi_app ( ) ) <EOL> self . assertEqual ( <NUM_LIT> , res . status_int ) <EOL> def test_too_many_metadata_items_on_update_item ( self ) : <EOL> self . stubs . Set ( nova . db . api , '<STR_LIT>' , <EOL> return_create_instance_metadata_max ) <EOL> req = webob . Request . blank ( '<STR_LIT>' ) <EOL> req . method = '<STR_LIT>' <EOL> req . body = '<STR_LIT>' <EOL> req . headers [ "<STR_LIT>" ] = "<STR_LIT:application/json>" <EOL> res = req . get_response ( fakes . wsgi_app ( ) ) <EOL> self . assertEqual ( <NUM_LIT> , res . status_int ) </s>
