<s> import uuid <EOL> import mock <EOL> from oslo_config import cfg <EOL> from oslo_serialization import jsonutils <EOL> from oslo_utils import timeutils <EOL> import six <EOL> import webob <EOL> from nova . api . openstack . compute . legacy_v2 import server_metadata as server_metadata_v2 <EOL> from nova . api . openstack . compute import server_metadata as server_metadata_v21 <EOL> from nova . compute import rpcapi as compute_rpcapi <EOL> from nova . compute import vm_states <EOL> import nova . db <EOL> from nova import exception <EOL> from nova import objects <EOL> from nova import test <EOL> from nova . tests . unit . api . openstack import fakes <EOL> from nova . tests . unit import fake_instance <EOL> CONF = cfg . CONF <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 fake_instance_save ( inst , ** kwargs ) : <EOL> inst . metadata = stub_server_metadata ( ) <EOL> inst . obj_reset_changes ( ) <EOL> def return_server_metadata ( context , server_id ) : <EOL> if not isinstance ( server_id , six . string_types ) or not len ( server_id ) == <NUM_LIT> : <EOL> msg = '<STR_LIT>' % server_id <EOL> raise Exception ( msg ) <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 ( CONF . quota_metadata_items ) : <EOL> metadata [ '<STR_LIT>' ] [ '<STR_LIT>' % num ] = "<STR_LIT>" <EOL> return metadata <EOL> def return_server ( context , server_id , columns_to_join = None ) : <EOL> return fake_instance . fake_db_instance ( <EOL> ** { '<STR_LIT:id>' : server_id , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : timeutils . utcnow ( ) , <EOL> '<STR_LIT>' : vm_states . ACTIVE } ) <EOL> def return_server_by_uuid ( context , server_uuid , <EOL> columns_to_join = None , use_slave = False ) : <EOL> return fake_instance . fake_db_instance ( <EOL> ** { '<STR_LIT:id>' : <NUM_LIT:1> , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : timeutils . utcnow ( ) , <EOL> '<STR_LIT>' : stub_server_metadata ( ) , <EOL> '<STR_LIT>' : vm_states . ACTIVE } ) <EOL> def return_server_nonexistent ( context , server_id , <EOL> columns_to_join = None , use_slave = False ) : <EOL> raise exception . InstanceNotFound ( instance_id = server_id ) <EOL> def fake_change_instance_metadata ( self , context , instance , diff ) : <EOL> pass <EOL> class ServerMetaDataTestV21 ( test . TestCase ) : <EOL> validation_ex = exception . ValidationError <EOL> validation_ex_large = validation_ex <EOL> def setUp ( self ) : <EOL> super ( ServerMetaDataTestV21 , self ) . setUp ( ) <EOL> fakes . stub_out_key_pair_funcs ( self . stubs ) <EOL> self . stub_out ( '<STR_LIT>' , return_server ) <EOL> self . stub_out ( '<STR_LIT>' , <EOL> return_server_by_uuid ) <EOL> self . stub_out ( '<STR_LIT>' , <EOL> return_server_metadata ) <EOL> self . stubs . Set ( compute_rpcapi . ComputeAPI , '<STR_LIT>' , <EOL> fake_change_instance_metadata ) <EOL> self . _set_up_resources ( ) <EOL> def _set_up_resources ( self ) : <EOL> self . controller = server_metadata_v21 . ServerMetadataController ( ) <EOL> self . uuid = str ( uuid . uuid4 ( ) ) <EOL> self . url = '<STR_LIT>' % self . uuid <EOL> def _get_request ( self , param_url = '<STR_LIT>' ) : <EOL> return fakes . HTTPRequestV21 . blank ( self . url + param_url ) <EOL> def test_index ( self ) : <EOL> req = self . _get_request ( ) <EOL> res_dict = self . controller . index ( req , self . uuid ) <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_nonexistent_server ( self ) : <EOL> self . stub_out ( '<STR_LIT>' , <EOL> return_server_nonexistent ) <EOL> req = self . _get_request ( ) <EOL> self . assertRaises ( webob . exc . HTTPNotFound , <EOL> self . controller . index , req , self . url ) <EOL> def test_index_no_data ( self ) : <EOL> self . stub_out ( '<STR_LIT>' , <EOL> return_empty_server_metadata ) <EOL> req = self . _get_request ( ) <EOL> res_dict = self . controller . index ( req , self . uuid ) <EOL> expected = { '<STR_LIT>' : { } } <EOL> self . assertEqual ( expected , res_dict ) <EOL> def test_show ( self ) : <EOL> req = self . _get_request ( '<STR_LIT>' ) <EOL> res_dict = self . controller . show ( req , self . uuid , '<STR_LIT>' ) <EOL> expected = { "<STR_LIT>" : { '<STR_LIT>' : '<STR_LIT>' } } <EOL> self . assertEqual ( expected , res_dict ) <EOL> def test_show_nonexistent_server ( self ) : <EOL> self . stub_out ( '<STR_LIT>' , <EOL> return_server_nonexistent ) <EOL> req = self . _get_request ( '<STR_LIT>' ) <EOL> self . assertRaises ( webob . exc . HTTPNotFound , <EOL> self . controller . show , req , self . uuid , '<STR_LIT>' ) <EOL> def test_show_meta_not_found ( self ) : <EOL> self . stub_out ( '<STR_LIT>' , <EOL> return_empty_server_metadata ) <EOL> req = self . _get_request ( '<STR_LIT>' ) <EOL> self . assertRaises ( webob . exc . HTTPNotFound , <EOL> self . controller . show , req , self . uuid , '<STR_LIT>' ) <EOL> def test_delete ( self ) : <EOL> self . stub_out ( '<STR_LIT>' , <EOL> return_server_metadata ) <EOL> self . stub_out ( '<STR_LIT>' , <EOL> delete_server_metadata ) <EOL> req = self . _get_request ( '<STR_LIT>' ) <EOL> req . method = '<STR_LIT>' <EOL> res = self . controller . delete ( req , self . uuid , '<STR_LIT>' ) <EOL> self . assertIsNone ( res ) <EOL> def test_delete_nonexistent_server ( self ) : <EOL> self . stub_out ( '<STR_LIT>' , <EOL> return_server_nonexistent ) <EOL> req = self . _get_request ( '<STR_LIT>' ) <EOL> req . method = '<STR_LIT>' <EOL> self . assertRaises ( webob . exc . HTTPNotFound , <EOL> self . controller . delete , req , self . uuid , '<STR_LIT>' ) <EOL> def test_delete_meta_not_found ( self ) : <EOL> self . stub_out ( '<STR_LIT>' , <EOL> return_empty_server_metadata ) <EOL> req = self . _get_request ( '<STR_LIT>' ) <EOL> req . method = '<STR_LIT>' <EOL> self . assertRaises ( webob . exc . HTTPNotFound , <EOL> self . controller . delete , req , self . uuid , '<STR_LIT>' ) <EOL> def test_create ( self ) : <EOL> self . stubs . Set ( objects . Instance , '<STR_LIT>' , fake_instance_save ) <EOL> req = self . _get_request ( ) <EOL> req . method = '<STR_LIT:POST>' <EOL> req . content_type = "<STR_LIT:application/json>" <EOL> body = { "<STR_LIT>" : { "<STR_LIT>" : "<STR_LIT>" } } <EOL> req . body = jsonutils . dump_as_bytes ( body ) <EOL> res_dict = self . controller . create ( req , self . uuid , body = body ) <EOL> body [ '<STR_LIT>' ] . update ( { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> } ) <EOL> self . assertEqual ( body , res_dict ) <EOL> def test_create_empty_body ( self ) : <EOL> self . stub_out ( '<STR_LIT>' , <EOL> return_create_instance_metadata ) <EOL> req = self . _get_request ( ) <EOL> req . method = '<STR_LIT:POST>' <EOL> req . headers [ "<STR_LIT>" ] = "<STR_LIT:application/json>" <EOL> self . assertRaises ( self . validation_ex , <EOL> self . controller . create , req , self . uuid , body = None ) <EOL> def test_create_item_empty_key ( self ) : <EOL> self . stub_out ( '<STR_LIT>' , <EOL> return_create_instance_metadata ) <EOL> req = self . _get_request ( '<STR_LIT>' ) <EOL> req . method = '<STR_LIT>' <EOL> body = { "<STR_LIT>" : { "<STR_LIT>" : "<STR_LIT>" } } <EOL> req . body = jsonutils . dump_as_bytes ( body ) <EOL> req . headers [ "<STR_LIT>" ] = "<STR_LIT:application/json>" <EOL> self . assertRaises ( self . validation_ex , <EOL> self . controller . create , req , self . uuid , body = body ) <EOL> def test_create_item_non_dict ( self ) : <EOL> self . stub_out ( '<STR_LIT>' , <EOL> return_create_instance_metadata ) <EOL> req = self . _get_request ( '<STR_LIT>' ) <EOL> req . method = '<STR_LIT>' <EOL> body = { "<STR_LIT>" : None } <EOL> req . body = jsonutils . dump_as_bytes ( body ) <EOL> req . headers [ "<STR_LIT>" ] = "<STR_LIT:application/json>" <EOL> self . assertRaises ( self . validation_ex , <EOL> self . controller . create , req , self . uuid , body = body ) <EOL> def test_create_item_key_too_long ( self ) : <EOL> self . stub_out ( '<STR_LIT>' , <EOL> return_create_instance_metadata ) <EOL> req = self . _get_request ( '<STR_LIT>' ) <EOL> req . method = '<STR_LIT>' <EOL> body = { "<STR_LIT>" : { ( "<STR_LIT:a>" * <NUM_LIT> ) : "<STR_LIT>" } } <EOL> req . body = jsonutils . dump_as_bytes ( body ) <EOL> req . headers [ "<STR_LIT>" ] = "<STR_LIT:application/json>" <EOL> self . assertRaises ( self . validation_ex_large , <EOL> self . controller . create , <EOL> req , self . uuid , body = body ) <EOL> def test_create_malformed_container ( self ) : <EOL> self . stub_out ( '<STR_LIT>' , <EOL> return_create_instance_metadata ) <EOL> req = fakes . HTTPRequest . blank ( self . url + '<STR_LIT>' ) <EOL> req . method = '<STR_LIT>' <EOL> body = { "<STR_LIT>" : { } } <EOL> req . body = jsonutils . dump_as_bytes ( body ) <EOL> req . headers [ "<STR_LIT>" ] = "<STR_LIT:application/json>" <EOL> self . assertRaises ( self . validation_ex , <EOL> self . controller . create , req , self . uuid , body = body ) <EOL> def test_create_malformed_data ( self ) : <EOL> self . stub_out ( '<STR_LIT>' , <EOL> return_create_instance_metadata ) <EOL> req = fakes . HTTPRequest . blank ( self . url + '<STR_LIT>' ) <EOL> req . method = '<STR_LIT>' <EOL> body = { "<STR_LIT>" : [ '<STR_LIT>' ] } <EOL> req . body = jsonutils . dump_as_bytes ( body ) <EOL> req . headers [ "<STR_LIT>" ] = "<STR_LIT:application/json>" <EOL> self . assertRaises ( self . validation_ex , <EOL> self . controller . create , req , self . uuid , body = body ) <EOL> def test_create_nonexistent_server ( self ) : <EOL> self . stub_out ( '<STR_LIT>' , <EOL> return_server_nonexistent ) <EOL> req = self . _get_request ( ) <EOL> req . method = '<STR_LIT:POST>' <EOL> body = { "<STR_LIT>" : { "<STR_LIT>" : "<STR_LIT>" } } <EOL> req . body = jsonutils . dump_as_bytes ( body ) <EOL> req . headers [ "<STR_LIT>" ] = "<STR_LIT:application/json>" <EOL> self . assertRaises ( webob . exc . HTTPNotFound , <EOL> self . controller . create , req , self . uuid , body = body ) <EOL> def test_update_metadata ( self ) : <EOL> self . stubs . Set ( objects . Instance , '<STR_LIT>' , fake_instance_save ) <EOL> req = self . _get_request ( ) <EOL> req . method = '<STR_LIT:POST>' <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 = jsonutils . dump_as_bytes ( expected ) <EOL> response = self . controller . update_all ( req , self . uuid , body = expected ) <EOL> self . assertEqual ( expected , response ) <EOL> def test_update_all ( self ) : <EOL> self . stubs . Set ( objects . Instance , '<STR_LIT>' , fake_instance_save ) <EOL> req = self . _get_request ( ) <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 = jsonutils . dump_as_bytes ( expected ) <EOL> res_dict = self . controller . update_all ( req , self . uuid , body = expected ) <EOL> self . assertEqual ( expected , res_dict ) <EOL> def test_update_all_empty_container ( self ) : <EOL> self . stubs . Set ( objects . Instance , '<STR_LIT>' , fake_instance_save ) <EOL> req = self . _get_request ( ) <EOL> req . method = '<STR_LIT>' <EOL> req . content_type = "<STR_LIT:application/json>" <EOL> expected = { '<STR_LIT>' : { } } <EOL> req . body = jsonutils . dump_as_bytes ( expected ) <EOL> res_dict = self . controller . update_all ( req , self . uuid , body = expected ) <EOL> self . assertEqual ( expected , res_dict ) <EOL> def test_update_all_empty_body_item ( self ) : <EOL> self . stub_out ( '<STR_LIT>' , <EOL> return_create_instance_metadata ) <EOL> req = fakes . HTTPRequest . blank ( self . url + '<STR_LIT>' ) <EOL> req . method = '<STR_LIT>' <EOL> req . headers [ "<STR_LIT>" ] = "<STR_LIT:application/json>" <EOL> self . assertRaises ( self . validation_ex , <EOL> self . controller . update_all , req , self . uuid , <EOL> body = None ) <EOL> def test_update_all_with_non_dict_item ( self ) : <EOL> self . stub_out ( '<STR_LIT>' , <EOL> return_create_instance_metadata ) <EOL> req = fakes . HTTPRequest . blank ( self . url + '<STR_LIT>' ) <EOL> req . method = '<STR_LIT>' <EOL> body = { "<STR_LIT>" : None } <EOL> req . body = jsonutils . dump_as_bytes ( body ) <EOL> req . headers [ "<STR_LIT>" ] = "<STR_LIT:application/json>" <EOL> self . assertRaises ( self . validation_ex , <EOL> self . controller . update_all , req , self . uuid , <EOL> body = body ) <EOL> def test_update_all_malformed_container ( self ) : <EOL> self . stub_out ( '<STR_LIT>' , <EOL> return_create_instance_metadata ) <EOL> req = self . _get_request ( ) <EOL> req . method = '<STR_LIT>' <EOL> req . content_type = "<STR_LIT:application/json>" <EOL> expected = { '<STR_LIT>' : { } } <EOL> req . body = jsonutils . dump_as_bytes ( expected ) <EOL> self . assertRaises ( self . validation_ex , <EOL> self . controller . update_all , req , self . uuid , <EOL> body = expected ) <EOL> def test_update_all_malformed_data ( self ) : <EOL> self . stub_out ( '<STR_LIT>' , <EOL> return_create_instance_metadata ) <EOL> req = self . _get_request ( ) <EOL> req . method = '<STR_LIT>' <EOL> req . content_type = "<STR_LIT:application/json>" <EOL> expected = { '<STR_LIT>' : [ '<STR_LIT>' ] } <EOL> req . body = jsonutils . dump_as_bytes ( expected ) <EOL> self . assertRaises ( self . validation_ex , <EOL> self . controller . update_all , req , self . uuid , <EOL> body = expected ) <EOL> def test_update_all_nonexistent_server ( self ) : <EOL> self . stub_out ( '<STR_LIT>' , return_server_nonexistent ) <EOL> req = self . _get_request ( ) <EOL> req . method = '<STR_LIT>' <EOL> req . content_type = "<STR_LIT:application/json>" <EOL> body = { '<STR_LIT>' : { '<STR_LIT>' : '<STR_LIT>' } } <EOL> req . body = jsonutils . dump_as_bytes ( body ) <EOL> self . assertRaises ( webob . exc . HTTPNotFound , <EOL> self . controller . update_all , req , '<STR_LIT:100>' , body = body ) <EOL> def test_update_all_non_dict ( self ) : <EOL> self . stub_out ( '<STR_LIT>' , <EOL> return_create_instance_metadata ) <EOL> req = self . _get_request ( ) <EOL> req . method = '<STR_LIT>' <EOL> body = { "<STR_LIT>" : None } <EOL> req . body = jsonutils . dump_as_bytes ( body ) <EOL> req . headers [ "<STR_LIT>" ] = "<STR_LIT:application/json>" <EOL> self . assertRaises ( self . validation_ex , self . controller . update_all , <EOL> req , self . uuid , body = body ) <EOL> def test_update_item ( self ) : <EOL> self . stubs . Set ( objects . Instance , '<STR_LIT>' , fake_instance_save ) <EOL> req = self . _get_request ( '<STR_LIT>' ) <EOL> req . method = '<STR_LIT>' <EOL> body = { "<STR_LIT>" : { "<STR_LIT>" : "<STR_LIT>" } } <EOL> req . body = jsonutils . dump_as_bytes ( body ) <EOL> req . headers [ "<STR_LIT>" ] = "<STR_LIT:application/json>" <EOL> res_dict = self . controller . update ( req , self . uuid , '<STR_LIT>' , body = body ) <EOL> expected = { "<STR_LIT>" : { '<STR_LIT>' : '<STR_LIT>' } } <EOL> self . assertEqual ( expected , res_dict ) <EOL> def test_update_item_nonexistent_server ( self ) : <EOL> self . stub_out ( '<STR_LIT>' , <EOL> return_server_nonexistent ) <EOL> req = self . _get_request ( '<STR_LIT>' ) <EOL> req . method = '<STR_LIT>' <EOL> body = { "<STR_LIT>" : { "<STR_LIT>" : "<STR_LIT>" } } <EOL> req . body = jsonutils . dump_as_bytes ( body ) <EOL> req . headers [ "<STR_LIT>" ] = "<STR_LIT:application/json>" <EOL> self . assertRaises ( webob . exc . HTTPNotFound , <EOL> self . controller . update , req , self . uuid , '<STR_LIT>' , <EOL> body = body ) <EOL> def test_update_item_empty_body ( self ) : <EOL> self . stub_out ( '<STR_LIT>' , <EOL> return_create_instance_metadata ) <EOL> req = self . _get_request ( '<STR_LIT>' ) <EOL> req . method = '<STR_LIT>' <EOL> req . headers [ "<STR_LIT>" ] = "<STR_LIT:application/json>" <EOL> self . assertRaises ( self . validation_ex , <EOL> self . controller . update , req , self . uuid , '<STR_LIT>' , <EOL> body = None ) <EOL> def test_update_malformed_container ( self ) : <EOL> self . stub_out ( '<STR_LIT>' , <EOL> return_create_instance_metadata ) <EOL> req = fakes . HTTPRequest . blank ( self . url ) <EOL> req . method = '<STR_LIT>' <EOL> expected = { '<STR_LIT>' : { } } <EOL> req . body = jsonutils . dump_as_bytes ( expected ) <EOL> req . headers [ "<STR_LIT>" ] = "<STR_LIT:application/json>" <EOL> self . assertRaises ( self . validation_ex , <EOL> self . controller . update , req , self . uuid , '<STR_LIT>' , <EOL> body = expected ) <EOL> def test_update_malformed_data ( self ) : <EOL> self . stub_out ( '<STR_LIT>' , <EOL> return_create_instance_metadata ) <EOL> req = fakes . HTTPRequest . blank ( self . url ) <EOL> req . method = '<STR_LIT>' <EOL> expected = { '<STR_LIT>' : [ '<STR_LIT>' ] } <EOL> req . body = jsonutils . dump_as_bytes ( expected ) <EOL> req . headers [ "<STR_LIT>" ] = "<STR_LIT:application/json>" <EOL> self . assertRaises ( self . validation_ex , <EOL> self . controller . update , req , self . uuid , '<STR_LIT>' , <EOL> body = expected ) <EOL> def test_update_item_empty_key ( self ) : <EOL> self . stub_out ( '<STR_LIT>' , <EOL> return_create_instance_metadata ) <EOL> req = self . _get_request ( '<STR_LIT>' ) <EOL> req . method = '<STR_LIT>' <EOL> body = { "<STR_LIT>" : { "<STR_LIT>" : "<STR_LIT>" } } <EOL> req . body = jsonutils . dump_as_bytes ( body ) <EOL> req . headers [ "<STR_LIT>" ] = "<STR_LIT:application/json>" <EOL> self . assertRaises ( self . validation_ex , <EOL> self . controller . update , req , self . uuid , '<STR_LIT>' , <EOL> body = body ) <EOL> def test_update_item_key_too_long ( self ) : <EOL> self . stub_out ( '<STR_LIT>' , <EOL> return_create_instance_metadata ) <EOL> req = self . _get_request ( '<STR_LIT>' ) <EOL> req . method = '<STR_LIT>' <EOL> body = { "<STR_LIT>" : { ( "<STR_LIT:a>" * <NUM_LIT> ) : "<STR_LIT>" } } <EOL> req . body = jsonutils . dump_as_bytes ( body ) <EOL> req . headers [ "<STR_LIT>" ] = "<STR_LIT:application/json>" <EOL> self . assertRaises ( self . validation_ex_large , <EOL> self . controller . update , <EOL> req , self . uuid , ( "<STR_LIT:a>" * <NUM_LIT> ) , body = body ) <EOL> def test_update_item_value_too_long ( self ) : <EOL> self . stub_out ( '<STR_LIT>' , <EOL> return_create_instance_metadata ) <EOL> req = self . _get_request ( '<STR_LIT>' ) <EOL> req . method = '<STR_LIT>' <EOL> body = { "<STR_LIT>" : { "<STR_LIT>" : ( "<STR_LIT:a>" * <NUM_LIT> ) } } <EOL> req . body = jsonutils . dump_as_bytes ( body ) <EOL> req . headers [ "<STR_LIT>" ] = "<STR_LIT:application/json>" <EOL> self . assertRaises ( self . validation_ex_large , <EOL> self . controller . update , <EOL> req , self . uuid , "<STR_LIT>" , body = body ) <EOL> def test_update_item_too_many_keys ( self ) : <EOL> self . stub_out ( '<STR_LIT>' , <EOL> return_create_instance_metadata ) <EOL> req = self . _get_request ( '<STR_LIT>' ) <EOL> req . method = '<STR_LIT>' <EOL> body = { "<STR_LIT>" : { "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT>" : "<STR_LIT>" } } <EOL> req . body = jsonutils . dump_as_bytes ( body ) <EOL> req . headers [ "<STR_LIT>" ] = "<STR_LIT:application/json>" <EOL> self . assertRaises ( self . validation_ex , <EOL> self . controller . update , req , self . uuid , '<STR_LIT>' , <EOL> body = body ) <EOL> def test_update_item_body_uri_mismatch ( self ) : <EOL> self . stub_out ( '<STR_LIT>' , <EOL> return_create_instance_metadata ) <EOL> req = self . _get_request ( '<STR_LIT>' ) <EOL> req . method = '<STR_LIT>' <EOL> body = { "<STR_LIT>" : { "<STR_LIT>" : "<STR_LIT>" } } <EOL> req . body = jsonutils . dump_as_bytes ( body ) <EOL> req . headers [ "<STR_LIT>" ] = "<STR_LIT:application/json>" <EOL> self . assertRaises ( webob . exc . HTTPBadRequest , <EOL> self . controller . update , req , self . uuid , '<STR_LIT>' , <EOL> body = body ) <EOL> def test_update_item_non_dict ( self ) : <EOL> self . stub_out ( '<STR_LIT>' , <EOL> return_create_instance_metadata ) <EOL> req = self . _get_request ( '<STR_LIT>' ) <EOL> req . method = '<STR_LIT>' <EOL> body = { "<STR_LIT>" : None } <EOL> req . body = jsonutils . dump_as_bytes ( body ) <EOL> req . headers [ "<STR_LIT>" ] = "<STR_LIT:application/json>" <EOL> self . assertRaises ( self . validation_ex , <EOL> self . controller . update , req , self . uuid , '<STR_LIT>' , <EOL> body = body ) <EOL> def test_update_empty_container ( self ) : <EOL> self . stub_out ( '<STR_LIT>' , <EOL> return_create_instance_metadata ) <EOL> req = fakes . HTTPRequest . blank ( self . url ) <EOL> req . method = '<STR_LIT>' <EOL> expected = { '<STR_LIT>' : { } } <EOL> req . body = jsonutils . dump_as_bytes ( expected ) <EOL> req . headers [ "<STR_LIT>" ] = "<STR_LIT:application/json>" <EOL> self . assertRaises ( self . validation_ex , <EOL> self . controller . update , req , self . uuid , '<STR_LIT>' , <EOL> body = expected ) <EOL> def test_too_many_metadata_items_on_create ( self ) : <EOL> self . stub_out ( '<STR_LIT>' , <EOL> return_create_instance_metadata ) <EOL> data = { "<STR_LIT>" : { } } <EOL> for num in range ( CONF . quota_metadata_items + <NUM_LIT:1> ) : <EOL> data [ '<STR_LIT>' ] [ '<STR_LIT>' % num ] = "<STR_LIT>" <EOL> req = self . _get_request ( ) <EOL> req . method = '<STR_LIT:POST>' <EOL> req . body = jsonutils . dump_as_bytes ( data ) <EOL> req . headers [ "<STR_LIT>" ] = "<STR_LIT:application/json>" <EOL> self . assertRaises ( webob . exc . HTTPForbidden , <EOL> self . controller . create , req , self . uuid , body = data ) <EOL> def test_invalid_metadata_items_on_create ( self ) : <EOL> self . stub_out ( '<STR_LIT>' , <EOL> return_create_instance_metadata ) <EOL> req = self . _get_request ( ) <EOL> req . method = '<STR_LIT:POST>' <EOL> req . headers [ "<STR_LIT>" ] = "<STR_LIT:application/json>" <EOL> data = { "<STR_LIT>" : { "<STR_LIT:a>" * <NUM_LIT> : "<STR_LIT>" } } <EOL> req . body = jsonutils . dump_as_bytes ( data ) <EOL> self . assertRaises ( self . validation_ex_large , <EOL> self . controller . create , req , self . uuid , body = data ) <EOL> data = { "<STR_LIT>" : { "<STR_LIT:key>" : "<STR_LIT:v>" * <NUM_LIT> } } <EOL> req . body = jsonutils . dump_as_bytes ( data ) <EOL> self . assertRaises ( self . validation_ex_large , <EOL> self . controller . create , req , self . uuid , body = data ) <EOL> data = { "<STR_LIT>" : { "<STR_LIT>" : "<STR_LIT>" } } <EOL> req . body = jsonutils . dump_as_bytes ( data ) <EOL> self . assertRaises ( self . validation_ex , <EOL> self . controller . create , req , self . uuid , body = data ) <EOL> def test_too_many_metadata_items_on_update_item ( self ) : <EOL> self . stub_out ( '<STR_LIT>' , <EOL> return_create_instance_metadata ) <EOL> data = { "<STR_LIT>" : { } } <EOL> for num in range ( CONF . quota_metadata_items + <NUM_LIT:1> ) : <EOL> data [ '<STR_LIT>' ] [ '<STR_LIT>' % num ] = "<STR_LIT>" <EOL> req = self . _get_request ( ) <EOL> req . method = '<STR_LIT>' <EOL> req . body = jsonutils . dump_as_bytes ( data ) <EOL> req . headers [ "<STR_LIT>" ] = "<STR_LIT:application/json>" <EOL> self . assertRaises ( webob . exc . HTTPForbidden , self . controller . update_all , <EOL> req , self . uuid , body = data ) <EOL> def test_invalid_metadata_items_on_update_item ( self ) : <EOL> self . stub_out ( '<STR_LIT>' , <EOL> return_create_instance_metadata ) <EOL> self . stub_out ( '<STR_LIT>' , <EOL> return_create_instance_metadata ) <EOL> data = { "<STR_LIT>" : { } } <EOL> for num in range ( CONF . quota_metadata_items + <NUM_LIT:1> ) : <EOL> data [ '<STR_LIT>' ] [ '<STR_LIT>' % num ] = "<STR_LIT>" <EOL> req = self . _get_request ( ) <EOL> req . method = '<STR_LIT>' <EOL> req . body = jsonutils . dump_as_bytes ( data ) <EOL> req . headers [ "<STR_LIT>" ] = "<STR_LIT:application/json>" <EOL> data = { "<STR_LIT>" : { "<STR_LIT:a>" * <NUM_LIT> : "<STR_LIT>" } } <EOL> req . body = jsonutils . dump_as_bytes ( data ) <EOL> self . assertRaises ( self . validation_ex_large , <EOL> self . controller . update_all , req , self . uuid , <EOL> body = data ) <EOL> data = { "<STR_LIT>" : { "<STR_LIT:key>" : "<STR_LIT:v>" * <NUM_LIT> } } <EOL> req . body = jsonutils . dump_as_bytes ( data ) <EOL> self . assertRaises ( self . validation_ex_large , <EOL> self . controller . update_all , req , self . uuid , <EOL> body = data ) <EOL> data = { "<STR_LIT>" : { "<STR_LIT>" : "<STR_LIT>" } } <EOL> req . body = jsonutils . dump_as_bytes ( data ) <EOL> self . assertRaises ( self . validation_ex , <EOL> self . controller . update_all , req , self . uuid , <EOL> body = data ) <EOL> class ServerMetaDataTestV2 ( ServerMetaDataTestV21 ) : <EOL> validation_ex = webob . exc . HTTPBadRequest <EOL> validation_ex_large = webob . exc . HTTPRequestEntityTooLarge <EOL> def _set_up_resources ( self ) : <EOL> self . controller = server_metadata_v2 . Controller ( ) <EOL> self . uuid = str ( uuid . uuid4 ( ) ) <EOL> self . url = '<STR_LIT>' % self . uuid <EOL> def _get_request ( self , param_url = '<STR_LIT>' ) : <EOL> return fakes . HTTPRequest . blank ( self . url + param_url ) <EOL> class BadStateServerMetaDataTestV21 ( test . TestCase ) : <EOL> def setUp ( self ) : <EOL> super ( BadStateServerMetaDataTestV21 , self ) . setUp ( ) <EOL> fakes . stub_out_key_pair_funcs ( self . stubs ) <EOL> self . stub_out ( '<STR_LIT>' , <EOL> return_server_metadata ) <EOL> self . stubs . Set ( compute_rpcapi . ComputeAPI , '<STR_LIT>' , <EOL> fake_change_instance_metadata ) <EOL> self . stub_out ( '<STR_LIT>' , self . _return_server_in_build ) <EOL> self . stub_out ( '<STR_LIT>' , <EOL> self . _return_server_in_build_by_uuid ) <EOL> self . stub_out ( '<STR_LIT>' , <EOL> delete_server_metadata ) <EOL> self . _set_up_resources ( ) <EOL> def _set_up_resources ( self ) : <EOL> self . controller = server_metadata_v21 . ServerMetadataController ( ) <EOL> self . uuid = str ( uuid . uuid4 ( ) ) <EOL> self . url = '<STR_LIT>' % self . uuid <EOL> def _get_request ( self , param_url = '<STR_LIT>' ) : <EOL> return fakes . HTTPRequestV21 . blank ( self . url + param_url ) <EOL> def test_invalid_state_on_delete ( self ) : <EOL> req = self . _get_request ( '<STR_LIT>' ) <EOL> req . method = '<STR_LIT>' <EOL> self . assertRaises ( webob . exc . HTTPConflict , self . controller . delete , <EOL> req , self . uuid , '<STR_LIT>' ) <EOL> def test_invalid_state_on_update_metadata ( self ) : <EOL> self . stub_out ( '<STR_LIT>' , <EOL> return_create_instance_metadata ) <EOL> req = self . _get_request ( ) <EOL> req . method = '<STR_LIT:POST>' <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 = jsonutils . dump_as_bytes ( expected ) <EOL> self . assertRaises ( webob . exc . HTTPConflict , self . controller . update_all , <EOL> req , self . uuid , body = expected ) <EOL> def _return_server_in_build ( self , context , server_id , <EOL> columns_to_join = None ) : <EOL> return fake_instance . fake_db_instance ( <EOL> ** { '<STR_LIT:id>' : server_id , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : vm_states . BUILDING } ) <EOL> def _return_server_in_build_by_uuid ( self , context , server_uuid , <EOL> columns_to_join = None , use_slave = False ) : <EOL> return fake_instance . fake_db_instance ( <EOL> ** { '<STR_LIT:id>' : <NUM_LIT:1> , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : vm_states . BUILDING } ) <EOL> @ mock . patch . object ( nova . compute . api . API , '<STR_LIT>' , <EOL> side_effect = exception . InstanceIsLocked ( instance_uuid = <NUM_LIT:0> ) ) <EOL> def test_instance_lock_update_metadata ( self , mock_update ) : <EOL> req = self . _get_request ( ) <EOL> req . method = '<STR_LIT:POST>' <EOL> req . content_type = '<STR_LIT:application/json>' <EOL> expected = { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> } <EOL> req . body = jsonutils . dump_as_bytes ( expected ) <EOL> self . assertRaises ( webob . exc . HTTPConflict , self . controller . update_all , <EOL> req , self . uuid , body = expected ) <EOL> class BadStateServerMetaDataTestV2 ( BadStateServerMetaDataTestV21 ) : <EOL> def _set_up_resources ( self ) : <EOL> self . controller = server_metadata_v2 . Controller ( ) <EOL> self . uuid = str ( uuid . uuid4 ( ) ) <EOL> self . url = '<STR_LIT>' % self . uuid <EOL> def _get_request ( self , param_url = '<STR_LIT>' ) : <EOL> return fakes . HTTPRequest . blank ( self . url + param_url ) <EOL> class ServerMetaPolicyEnforcementV21 ( test . NoDBTestCase ) : <EOL> def setUp ( self ) : <EOL> super ( ServerMetaPolicyEnforcementV21 , self ) . setUp ( ) <EOL> self . controller = server_metadata_v21 . ServerMetadataController ( ) <EOL> self . req = fakes . HTTPRequest . blank ( '<STR_LIT>' ) <EOL> def test_create_policy_failed ( self ) : <EOL> rule_name = "<STR_LIT>" <EOL> self . policy . set_rules ( { rule_name : "<STR_LIT>" } ) <EOL> exc = self . assertRaises ( <EOL> exception . PolicyNotAuthorized , <EOL> self . controller . create , self . req , fakes . FAKE_UUID , <EOL> body = { '<STR_LIT>' : { } } ) <EOL> self . assertEqual ( <EOL> "<STR_LIT>" % rule_name , <EOL> exc . format_message ( ) ) <EOL> def test_index_policy_failed ( self ) : <EOL> rule_name = "<STR_LIT>" <EOL> self . policy . set_rules ( { rule_name : "<STR_LIT>" } ) <EOL> exc = self . assertRaises ( <EOL> exception . PolicyNotAuthorized , <EOL> self . controller . index , self . req , fakes . FAKE_UUID ) <EOL> self . assertEqual ( <EOL> "<STR_LIT>" % rule_name , <EOL> exc . format_message ( ) ) <EOL> def test_update_policy_failed ( self ) : <EOL> rule_name = "<STR_LIT>" <EOL> self . policy . set_rules ( { rule_name : "<STR_LIT>" } ) <EOL> exc = self . assertRaises ( <EOL> exception . PolicyNotAuthorized , <EOL> self . controller . update , self . req , fakes . FAKE_UUID , fakes . FAKE_UUID , <EOL> body = { '<STR_LIT>' : { '<STR_LIT>' : '<STR_LIT>' } } ) <EOL> self . assertEqual ( <EOL> "<STR_LIT>" % rule_name , <EOL> exc . format_message ( ) ) <EOL> def test_update_all_policy_failed ( self ) : <EOL> rule_name = "<STR_LIT>" <EOL> self . policy . set_rules ( { rule_name : "<STR_LIT>" } ) <EOL> exc = self . assertRaises ( <EOL> exception . PolicyNotAuthorized , <EOL> self . controller . update_all , self . req , fakes . FAKE_UUID , <EOL> body = { '<STR_LIT>' : { } } ) <EOL> self . assertEqual ( <EOL> "<STR_LIT>" % rule_name , <EOL> exc . format_message ( ) ) <EOL> def test_delete_policy_failed ( self ) : <EOL> rule_name = "<STR_LIT>" <EOL> self . policy . set_rules ( { rule_name : "<STR_LIT>" } ) <EOL> exc = self . assertRaises ( <EOL> exception . PolicyNotAuthorized , <EOL> self . controller . delete , self . req , fakes . FAKE_UUID , fakes . FAKE_UUID ) <EOL> self . assertEqual ( <EOL> "<STR_LIT>" % rule_name , <EOL> exc . format_message ( ) ) <EOL> def test_show_policy_failed ( self ) : <EOL> rule_name = "<STR_LIT>" <EOL> self . policy . set_rules ( { rule_name : "<STR_LIT>" } ) <EOL> exc = self . assertRaises ( <EOL> exception . PolicyNotAuthorized , <EOL> self . controller . show , self . req , fakes . FAKE_UUID , fakes . FAKE_UUID ) <EOL> self . assertEqual ( <EOL> "<STR_LIT>" % rule_name , <EOL> exc . format_message ( ) ) </s>
