<s> import mock <EOL> import webob . exc <EOL> from nova . api . openstack . compute import agents as agents_v21 <EOL> from nova . api . openstack . compute . legacy_v2 . contrib import agents as agents_v2 <EOL> from nova import db <EOL> from nova . db . sqlalchemy import models <EOL> from nova import exception <EOL> from nova import test <EOL> from nova . tests . unit . api . openstack import fakes <EOL> fake_agents_list = [ { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:version>' : '<STR_LIT>' , <EOL> '<STR_LIT:url>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:id>' : <NUM_LIT:1> } , <EOL> { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:version>' : '<STR_LIT>' , <EOL> '<STR_LIT:url>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:id>' : <NUM_LIT:2> } , <EOL> { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:version>' : '<STR_LIT>' , <EOL> '<STR_LIT:url>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:id>' : <NUM_LIT:3> } , <EOL> { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:version>' : '<STR_LIT>' , <EOL> '<STR_LIT:url>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:id>' : <NUM_LIT:4> } , <EOL> ] <EOL> def fake_agent_build_get_all ( context , hypervisor ) : <EOL> agent_build_all = [ ] <EOL> for agent in fake_agents_list : <EOL> if hypervisor and hypervisor != agent [ '<STR_LIT>' ] : <EOL> continue <EOL> agent_build_ref = models . AgentBuild ( ) <EOL> agent_build_ref . update ( agent ) <EOL> agent_build_all . append ( agent_build_ref ) <EOL> return agent_build_all <EOL> def fake_agent_build_update ( context , agent_build_id , values ) : <EOL> pass <EOL> def fake_agent_build_destroy ( context , agent_update_id ) : <EOL> pass <EOL> def fake_agent_build_create ( context , values ) : <EOL> values [ '<STR_LIT:id>' ] = <NUM_LIT:1> <EOL> agent_build_ref = models . AgentBuild ( ) <EOL> agent_build_ref . update ( values ) <EOL> return agent_build_ref <EOL> class AgentsTestV21 ( test . NoDBTestCase ) : <EOL> controller = agents_v21 . AgentController ( ) <EOL> validation_error = exception . ValidationError <EOL> def setUp ( self ) : <EOL> super ( AgentsTestV21 , self ) . setUp ( ) <EOL> self . stub_out ( "<STR_LIT>" , fake_agent_build_get_all ) <EOL> self . stub_out ( "<STR_LIT>" , fake_agent_build_update ) <EOL> self . stub_out ( "<STR_LIT>" , fake_agent_build_destroy ) <EOL> self . stub_out ( "<STR_LIT>" , fake_agent_build_create ) <EOL> self . req = self . _get_http_request ( ) <EOL> def _get_http_request ( self ) : <EOL> return fakes . HTTPRequest . blank ( '<STR_LIT>' ) <EOL> def test_agents_create ( self ) : <EOL> body = { '<STR_LIT>' : { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:version>' : '<STR_LIT>' , <EOL> '<STR_LIT:url>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } } <EOL> response = { '<STR_LIT>' : { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:version>' : '<STR_LIT>' , <EOL> '<STR_LIT:url>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : <NUM_LIT:1> } } <EOL> res_dict = self . controller . create ( self . req , body = body ) <EOL> self . assertEqual ( res_dict , response ) <EOL> def _test_agents_create_key_error ( self , key ) : <EOL> body = { '<STR_LIT>' : { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:version>' : '<STR_LIT>' , <EOL> '<STR_LIT:url>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } } <EOL> body [ '<STR_LIT>' ] . pop ( key ) <EOL> self . assertRaises ( self . validation_error , <EOL> self . controller . create , self . req , body = body ) <EOL> def test_agents_create_without_hypervisor ( self ) : <EOL> self . _test_agents_create_key_error ( '<STR_LIT>' ) <EOL> def test_agents_create_without_os ( self ) : <EOL> self . _test_agents_create_key_error ( '<STR_LIT>' ) <EOL> def test_agents_create_without_architecture ( self ) : <EOL> self . _test_agents_create_key_error ( '<STR_LIT>' ) <EOL> def test_agents_create_without_version ( self ) : <EOL> self . _test_agents_create_key_error ( '<STR_LIT:version>' ) <EOL> def test_agents_create_without_url ( self ) : <EOL> self . _test_agents_create_key_error ( '<STR_LIT:url>' ) <EOL> def test_agents_create_without_md5hash ( self ) : <EOL> self . _test_agents_create_key_error ( '<STR_LIT>' ) <EOL> def test_agents_create_with_wrong_type ( self ) : <EOL> body = { '<STR_LIT>' : None } <EOL> self . assertRaises ( self . validation_error , <EOL> self . controller . create , self . req , body = body ) <EOL> def test_agents_create_with_empty_type ( self ) : <EOL> body = { } <EOL> self . assertRaises ( self . validation_error , <EOL> self . controller . create , self . req , body = body ) <EOL> def test_agents_create_with_existed_agent ( self ) : <EOL> def fake_agent_build_create_with_exited_agent ( context , values ) : <EOL> raise exception . AgentBuildExists ( ** values ) <EOL> self . stub_out ( '<STR_LIT>' , <EOL> fake_agent_build_create_with_exited_agent ) <EOL> body = { '<STR_LIT>' : { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:version>' : '<STR_LIT>' , <EOL> '<STR_LIT:url>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } } <EOL> self . assertRaises ( webob . exc . HTTPConflict , self . controller . create , <EOL> self . req , body = body ) <EOL> def _test_agents_create_with_invalid_length ( self , key ) : <EOL> body = { '<STR_LIT>' : { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:version>' : '<STR_LIT>' , <EOL> '<STR_LIT:url>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } } <EOL> body [ '<STR_LIT>' ] [ key ] = '<STR_LIT:x>' * <NUM_LIT> <EOL> self . assertRaises ( self . validation_error , <EOL> self . controller . create , self . req , body = body ) <EOL> def test_agents_create_with_invalid_length_hypervisor ( self ) : <EOL> self . _test_agents_create_with_invalid_length ( '<STR_LIT>' ) <EOL> def test_agents_create_with_invalid_length_os ( self ) : <EOL> self . _test_agents_create_with_invalid_length ( '<STR_LIT>' ) <EOL> def test_agents_create_with_invalid_length_architecture ( self ) : <EOL> self . _test_agents_create_with_invalid_length ( '<STR_LIT>' ) <EOL> def test_agents_create_with_invalid_length_version ( self ) : <EOL> self . _test_agents_create_with_invalid_length ( '<STR_LIT:version>' ) <EOL> def test_agents_create_with_invalid_length_url ( self ) : <EOL> self . _test_agents_create_with_invalid_length ( '<STR_LIT:url>' ) <EOL> def test_agents_create_with_invalid_length_md5hash ( self ) : <EOL> self . _test_agents_create_with_invalid_length ( '<STR_LIT>' ) <EOL> def test_agents_delete ( self ) : <EOL> self . controller . delete ( self . req , <NUM_LIT:1> ) <EOL> def test_agents_delete_with_id_not_found ( self ) : <EOL> with mock . patch . object ( db , '<STR_LIT>' , <EOL> side_effect = exception . AgentBuildNotFound ( id = <NUM_LIT:1> ) ) : <EOL> self . assertRaises ( webob . exc . HTTPNotFound , <EOL> self . controller . delete , self . req , <NUM_LIT:1> ) <EOL> def test_agents_delete_string_id ( self ) : <EOL> self . assertRaises ( webob . exc . HTTPBadRequest , <EOL> self . controller . delete , self . req , '<STR_LIT>' ) <EOL> def test_agents_list ( self ) : <EOL> res_dict = self . controller . index ( self . req ) <EOL> agents_list = [ { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:version>' : '<STR_LIT>' , <EOL> '<STR_LIT:url>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : <NUM_LIT:1> } , <EOL> { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:version>' : '<STR_LIT>' , <EOL> '<STR_LIT:url>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : <NUM_LIT:2> } , <EOL> { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:version>' : '<STR_LIT>' , <EOL> '<STR_LIT:url>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : <NUM_LIT:3> } , <EOL> { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:version>' : '<STR_LIT>' , <EOL> '<STR_LIT:url>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : <NUM_LIT:4> } , <EOL> ] <EOL> self . assertEqual ( res_dict , { '<STR_LIT>' : agents_list } ) <EOL> def test_agents_list_with_hypervisor ( self ) : <EOL> req = fakes . HTTPRequest . blank ( '<STR_LIT>' , use_admin_context = True , <EOL> query_string = '<STR_LIT>' ) <EOL> res_dict = self . controller . index ( req ) <EOL> response = [ { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:version>' : '<STR_LIT>' , <EOL> '<STR_LIT:url>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : <NUM_LIT:1> } , <EOL> { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:version>' : '<STR_LIT>' , <EOL> '<STR_LIT:url>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : <NUM_LIT:2> } , <EOL> ] <EOL> self . assertEqual ( res_dict , { '<STR_LIT>' : response } ) <EOL> def test_agents_update ( self ) : <EOL> body = { '<STR_LIT>' : { '<STR_LIT:version>' : '<STR_LIT>' , <EOL> '<STR_LIT:url>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } } <EOL> response = { '<STR_LIT>' : { '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT:version>' : '<STR_LIT>' , <EOL> '<STR_LIT:url>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } } <EOL> res_dict = self . controller . update ( self . req , <NUM_LIT:1> , body = body ) <EOL> self . assertEqual ( res_dict , response ) <EOL> def _test_agents_update_key_error ( self , key ) : <EOL> body = { '<STR_LIT>' : { '<STR_LIT:version>' : '<STR_LIT>' , <EOL> '<STR_LIT:url>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } } <EOL> body [ '<STR_LIT>' ] . pop ( key ) <EOL> self . assertRaises ( self . validation_error , <EOL> self . controller . update , self . req , <NUM_LIT:1> , body = body ) <EOL> def test_agents_update_without_version ( self ) : <EOL> self . _test_agents_update_key_error ( '<STR_LIT:version>' ) <EOL> def test_agents_update_without_url ( self ) : <EOL> self . _test_agents_update_key_error ( '<STR_LIT:url>' ) <EOL> def test_agents_update_without_md5hash ( self ) : <EOL> self . _test_agents_update_key_error ( '<STR_LIT>' ) <EOL> def test_agents_update_with_wrong_type ( self ) : <EOL> body = { '<STR_LIT>' : None } <EOL> self . assertRaises ( self . validation_error , <EOL> self . controller . update , self . req , <NUM_LIT:1> , body = body ) <EOL> def test_agents_update_with_empty ( self ) : <EOL> body = { } <EOL> self . assertRaises ( self . validation_error , <EOL> self . controller . update , self . req , <NUM_LIT:1> , body = body ) <EOL> def test_agents_update_value_error ( self ) : <EOL> body = { '<STR_LIT>' : { '<STR_LIT:version>' : '<STR_LIT>' , <EOL> '<STR_LIT:url>' : <NUM_LIT> , <EOL> '<STR_LIT>' : '<STR_LIT>' } } <EOL> self . assertRaises ( self . validation_error , <EOL> self . controller . update , self . req , <NUM_LIT:1> , body = body ) <EOL> def test_agents_update_with_string_id ( self ) : <EOL> body = { '<STR_LIT>' : { '<STR_LIT:version>' : '<STR_LIT>' , <EOL> '<STR_LIT:url>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } } <EOL> self . assertRaises ( webob . exc . HTTPBadRequest , <EOL> self . controller . update , self . req , <EOL> '<STR_LIT>' , body = body ) <EOL> def _test_agents_update_with_invalid_length ( self , key ) : <EOL> body = { '<STR_LIT>' : { '<STR_LIT:version>' : '<STR_LIT>' , <EOL> '<STR_LIT:url>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } } <EOL> body [ '<STR_LIT>' ] [ key ] = '<STR_LIT:x>' * <NUM_LIT> <EOL> self . assertRaises ( self . validation_error , <EOL> self . controller . update , self . req , <NUM_LIT:1> , body = body ) <EOL> def test_agents_update_with_invalid_length_version ( self ) : <EOL> self . _test_agents_update_with_invalid_length ( '<STR_LIT:version>' ) <EOL> def test_agents_update_with_invalid_length_url ( self ) : <EOL> self . _test_agents_update_with_invalid_length ( '<STR_LIT:url>' ) <EOL> def test_agents_update_with_invalid_length_md5hash ( self ) : <EOL> self . _test_agents_update_with_invalid_length ( '<STR_LIT>' ) <EOL> def test_agents_update_with_id_not_found ( self ) : <EOL> with mock . patch . object ( db , '<STR_LIT>' , <EOL> side_effect = exception . AgentBuildNotFound ( id = <NUM_LIT:1> ) ) : <EOL> body = { '<STR_LIT>' : { '<STR_LIT:version>' : '<STR_LIT>' , <EOL> '<STR_LIT:url>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } } <EOL> self . assertRaises ( webob . exc . HTTPNotFound , <EOL> self . controller . update , self . req , <NUM_LIT:1> , body = body ) <EOL> class AgentsTestV2 ( AgentsTestV21 ) : <EOL> controller = agents_v2 . AgentController ( ) <EOL> validation_error = webob . exc . HTTPBadRequest <EOL> def setUp ( self ) : <EOL> super ( AgentsTestV2 , self ) . setUp ( ) <EOL> self . non_admin_req = fakes . HTTPRequest . blank ( '<STR_LIT>' ) <EOL> def _get_http_request ( self ) : <EOL> return fakes . HTTPRequest . blank ( '<STR_LIT>' , use_admin_context = True ) <EOL> def test_agents_update_with_non_admin ( self ) : <EOL> self . assertRaises ( exception . AdminRequired , self . controller . update , <EOL> self . non_admin_req , fakes . FAKE_UUID , body = { } ) <EOL> def test_agents_create_with_non_admin ( self ) : <EOL> self . assertRaises ( exception . AdminRequired , self . controller . create , <EOL> self . non_admin_req , body = { } ) <EOL> def test_agents_delete_with_non_admin ( self ) : <EOL> self . assertRaises ( exception . AdminRequired , self . controller . delete , <EOL> self . non_admin_req , fakes . FAKE_UUID ) <EOL> def test_agents_index_with_non_admin ( self ) : <EOL> self . assertRaises ( exception . AdminRequired , self . controller . index , <EOL> self . non_admin_req ) <EOL> class AgentsPolicyEnforcementV21 ( test . NoDBTestCase ) : <EOL> def setUp ( self ) : <EOL> super ( AgentsPolicyEnforcementV21 , self ) . setUp ( ) <EOL> self . controller = agents_v21 . AgentController ( ) <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 , <EOL> body = { '<STR_LIT>' : { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:version>' : '<STR_LIT>' , <EOL> '<STR_LIT:url>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<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 ) <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 ) <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 , <EOL> body = { '<STR_LIT>' : { '<STR_LIT:version>' : '<STR_LIT>' , <EOL> '<STR_LIT:url>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } } ) <EOL> self . assertEqual ( <EOL> "<STR_LIT>" % rule_name , <EOL> exc . format_message ( ) ) </s>
