<s> import json <EOL> import webob <EOL> import xml . dom . minidom as minidom <EOL> from nova . api . openstack import flavors <EOL> import nova . db . api <EOL> from nova import exception <EOL> from nova import test <EOL> from nova . tests . api . openstack import fakes <EOL> from nova import wsgi <EOL> def stub_flavor ( flavorid , name , memory_mb = "<STR_LIT>" , local_gb = "<STR_LIT>" ) : <EOL> return { <EOL> "<STR_LIT>" : str ( flavorid ) , <EOL> "<STR_LIT:name>" : name , <EOL> "<STR_LIT>" : memory_mb , <EOL> "<STR_LIT>" : local_gb , <EOL> } <EOL> def return_instance_type_by_flavor_id ( context , flavorid ) : <EOL> return stub_flavor ( flavorid , "<STR_LIT>" % ( flavorid , ) ) <EOL> def return_instance_types ( context , num = <NUM_LIT:2> ) : <EOL> instance_types = { } <EOL> for i in xrange ( <NUM_LIT:1> , num + <NUM_LIT:1> ) : <EOL> name = "<STR_LIT>" % ( i , ) <EOL> instance_types [ name ] = stub_flavor ( i , name ) <EOL> return instance_types <EOL> def return_instance_type_not_found ( context , flavor_id ) : <EOL> raise exception . InstanceTypeNotFound ( flavor_id = flavor_id ) <EOL> class FlavorsTest ( test . TestCase ) : <EOL> def setUp ( self ) : <EOL> super ( FlavorsTest , self ) . setUp ( ) <EOL> fakes . stub_out_networking ( self . stubs ) <EOL> fakes . stub_out_rate_limiting ( self . stubs ) <EOL> self . stubs . Set ( nova . db . api , "<STR_LIT>" , <EOL> return_instance_types ) <EOL> self . stubs . Set ( nova . db . api , "<STR_LIT>" , <EOL> return_instance_type_by_flavor_id ) <EOL> def tearDown ( self ) : <EOL> self . stubs . UnsetAll ( ) <EOL> super ( FlavorsTest , self ) . tearDown ( ) <EOL> def test_get_flavor_list_v1_0 ( self ) : <EOL> req = webob . Request . blank ( '<STR_LIT>' ) <EOL> res = req . get_response ( fakes . wsgi_app ( ) ) <EOL> self . assertEqual ( res . status_int , <NUM_LIT:200> ) <EOL> flavors = json . loads ( res . body ) [ "<STR_LIT>" ] <EOL> expected = [ <EOL> { <EOL> "<STR_LIT:id>" : "<STR_LIT:1>" , <EOL> "<STR_LIT:name>" : "<STR_LIT>" , <EOL> } , <EOL> { <EOL> "<STR_LIT:id>" : "<STR_LIT:2>" , <EOL> "<STR_LIT:name>" : "<STR_LIT>" , <EOL> } , <EOL> ] <EOL> self . assertEqual ( flavors , expected ) <EOL> def test_get_empty_flavor_list_v1_0 ( self ) : <EOL> def _return_empty ( self ) : <EOL> return { } <EOL> self . stubs . Set ( nova . db . api , "<STR_LIT>" , <EOL> _return_empty ) <EOL> req = webob . Request . blank ( '<STR_LIT>' ) <EOL> res = req . get_response ( fakes . wsgi_app ( ) ) <EOL> self . assertEqual ( res . status_int , <NUM_LIT:200> ) <EOL> flavors = json . loads ( res . body ) [ "<STR_LIT>" ] <EOL> expected = [ ] <EOL> self . assertEqual ( flavors , expected ) <EOL> def test_get_flavor_list_detail_v1_0 ( self ) : <EOL> req = webob . Request . blank ( '<STR_LIT>' ) <EOL> res = req . get_response ( fakes . wsgi_app ( ) ) <EOL> self . assertEqual ( res . status_int , <NUM_LIT:200> ) <EOL> flavors = json . loads ( res . body ) [ "<STR_LIT>" ] <EOL> expected = [ <EOL> { <EOL> "<STR_LIT:id>" : "<STR_LIT:1>" , <EOL> "<STR_LIT:name>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> } , <EOL> { <EOL> "<STR_LIT:id>" : "<STR_LIT:2>" , <EOL> "<STR_LIT:name>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> } , <EOL> ] <EOL> self . assertEqual ( flavors , expected ) <EOL> def test_get_flavor_by_id_v1_0 ( self ) : <EOL> req = webob . Request . blank ( '<STR_LIT>' ) <EOL> res = req . get_response ( fakes . wsgi_app ( ) ) <EOL> self . assertEqual ( res . status_int , <NUM_LIT:200> ) <EOL> flavor = json . loads ( res . body ) [ "<STR_LIT>" ] <EOL> expected = { <EOL> "<STR_LIT:id>" : "<STR_LIT>" , <EOL> "<STR_LIT:name>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> } <EOL> self . assertEqual ( flavor , expected ) <EOL> def test_get_flavor_by_invalid_id ( self ) : <EOL> self . stubs . Set ( nova . db . api , "<STR_LIT>" , <EOL> return_instance_type_not_found ) <EOL> req = webob . Request . blank ( '<STR_LIT>' ) <EOL> res = req . get_response ( fakes . wsgi_app ( ) ) <EOL> self . assertEqual ( res . status_int , <NUM_LIT> ) <EOL> def test_get_flavor_by_id_v1_1 ( self ) : <EOL> req = webob . Request . blank ( '<STR_LIT>' ) <EOL> req . environ [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> res = req . get_response ( fakes . wsgi_app ( ) ) <EOL> self . assertEqual ( res . status_int , <NUM_LIT:200> ) <EOL> flavor = json . loads ( res . body ) <EOL> expected = { <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT:id>" : "<STR_LIT>" , <EOL> "<STR_LIT:name>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : [ <EOL> { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> } , <EOL> { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> } , <EOL> ] , <EOL> } , <EOL> } <EOL> self . assertEqual ( flavor , expected ) <EOL> def test_get_flavor_list_v1_1 ( self ) : <EOL> req = webob . Request . blank ( '<STR_LIT>' ) <EOL> req . environ [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> res = req . get_response ( fakes . wsgi_app ( ) ) <EOL> self . assertEqual ( res . status_int , <NUM_LIT:200> ) <EOL> flavor = json . loads ( res . body ) <EOL> expected = { <EOL> "<STR_LIT>" : [ <EOL> { <EOL> "<STR_LIT:id>" : "<STR_LIT:1>" , <EOL> "<STR_LIT:name>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : [ <EOL> { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> } , <EOL> { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> } , <EOL> ] , <EOL> } , <EOL> { <EOL> "<STR_LIT:id>" : "<STR_LIT:2>" , <EOL> "<STR_LIT:name>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : [ <EOL> { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> } , <EOL> { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> } , <EOL> ] , <EOL> } , <EOL> ] , <EOL> } <EOL> self . assertEqual ( flavor , expected ) <EOL> def test_get_flavor_list_detail_v1_1 ( self ) : <EOL> req = webob . Request . blank ( '<STR_LIT>' ) <EOL> req . environ [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> res = req . get_response ( fakes . wsgi_app ( ) ) <EOL> self . assertEqual ( res . status_int , <NUM_LIT:200> ) <EOL> flavor = json . loads ( res . body ) <EOL> expected = { <EOL> "<STR_LIT>" : [ <EOL> { <EOL> "<STR_LIT:id>" : "<STR_LIT:1>" , <EOL> "<STR_LIT:name>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : [ <EOL> { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> } , <EOL> { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> } , <EOL> ] , <EOL> } , <EOL> { <EOL> "<STR_LIT:id>" : "<STR_LIT:2>" , <EOL> "<STR_LIT:name>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : [ <EOL> { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> } , <EOL> { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> } , <EOL> ] , <EOL> } , <EOL> ] , <EOL> } <EOL> self . assertEqual ( flavor , expected ) <EOL> def test_get_empty_flavor_list_v1_1 ( self ) : <EOL> def _return_empty ( self ) : <EOL> return { } <EOL> self . stubs . Set ( nova . db . api , "<STR_LIT>" , _return_empty ) <EOL> req = webob . Request . blank ( '<STR_LIT>' ) <EOL> res = req . get_response ( fakes . wsgi_app ( ) ) <EOL> self . assertEqual ( res . status_int , <NUM_LIT:200> ) <EOL> flavors = json . loads ( res . body ) [ "<STR_LIT>" ] <EOL> expected = [ ] <EOL> self . assertEqual ( flavors , expected ) <EOL> class FlavorsXMLSerializationTest ( test . TestCase ) : <EOL> def test_show ( self ) : <EOL> serializer = flavors . FlavorXMLSerializer ( ) <EOL> input = { <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT:id>" : "<STR_LIT>" , <EOL> "<STR_LIT:name>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : [ <EOL> { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> } , <EOL> { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> } , <EOL> ] , <EOL> } , <EOL> } <EOL> output = serializer . serialize ( input , '<STR_LIT>' ) <EOL> actual = minidom . parseString ( output . replace ( "<STR_LIT:U+0020>" , "<STR_LIT>" ) ) <EOL> expected = minidom . parseString ( """<STR_LIT>""" . replace ( "<STR_LIT:U+0020>" , "<STR_LIT>" ) ) <EOL> self . assertEqual ( expected . toxml ( ) , actual . toxml ( ) ) <EOL> def test_show_handles_integers ( self ) : <EOL> serializer = flavors . FlavorXMLSerializer ( ) <EOL> input = { <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT:id>" : <NUM_LIT:12> , <EOL> "<STR_LIT:name>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : <NUM_LIT> , <EOL> "<STR_LIT>" : <NUM_LIT:10> , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : [ <EOL> { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> } , <EOL> { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> } , <EOL> ] , <EOL> } , <EOL> } <EOL> output = serializer . serialize ( input , '<STR_LIT>' ) <EOL> actual = minidom . parseString ( output . replace ( "<STR_LIT:U+0020>" , "<STR_LIT>" ) ) <EOL> expected = minidom . parseString ( """<STR_LIT>""" . replace ( "<STR_LIT:U+0020>" , "<STR_LIT>" ) ) <EOL> self . assertEqual ( expected . toxml ( ) , actual . toxml ( ) ) <EOL> def test_detail ( self ) : <EOL> serializer = flavors . FlavorXMLSerializer ( ) <EOL> input = { <EOL> "<STR_LIT>" : [ <EOL> { <EOL> "<STR_LIT:id>" : "<STR_LIT>" , <EOL> "<STR_LIT:name>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : [ <EOL> { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> } , <EOL> { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> } , <EOL> ] , <EOL> } , <EOL> { <EOL> "<STR_LIT:id>" : "<STR_LIT>" , <EOL> "<STR_LIT:name>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : [ <EOL> { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> } , <EOL> { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> } , <EOL> ] , <EOL> } , <EOL> ] , <EOL> } <EOL> output = serializer . serialize ( input , '<STR_LIT>' ) <EOL> actual = minidom . parseString ( output . replace ( "<STR_LIT:U+0020>" , "<STR_LIT>" ) ) <EOL> expected = minidom . parseString ( """<STR_LIT>""" . replace ( "<STR_LIT:U+0020>" , "<STR_LIT>" ) % locals ( ) ) <EOL> self . assertEqual ( expected . toxml ( ) , actual . toxml ( ) ) <EOL> def test_index ( self ) : <EOL> serializer = flavors . FlavorXMLSerializer ( ) <EOL> input = { <EOL> "<STR_LIT>" : [ <EOL> { <EOL> "<STR_LIT:id>" : "<STR_LIT>" , <EOL> "<STR_LIT:name>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : [ <EOL> { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> } , <EOL> { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> } , <EOL> ] , <EOL> } , <EOL> { <EOL> "<STR_LIT:id>" : "<STR_LIT>" , <EOL> "<STR_LIT:name>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : [ <EOL> { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> } , <EOL> { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> } , <EOL> ] , <EOL> } , <EOL> ] , <EOL> } <EOL> output = serializer . serialize ( input , '<STR_LIT:index>' ) <EOL> actual = minidom . parseString ( output . replace ( "<STR_LIT:U+0020>" , "<STR_LIT>" ) ) <EOL> expected = minidom . parseString ( """<STR_LIT>""" . replace ( "<STR_LIT:U+0020>" , "<STR_LIT>" ) % locals ( ) ) <EOL> self . assertEqual ( expected . toxml ( ) , actual . toxml ( ) ) <EOL> def test_index_empty ( self ) : <EOL> serializer = flavors . FlavorXMLSerializer ( ) <EOL> input = { <EOL> "<STR_LIT>" : [ ] , <EOL> } <EOL> output = serializer . serialize ( input , '<STR_LIT:index>' ) <EOL> actual = minidom . parseString ( output . replace ( "<STR_LIT:U+0020>" , "<STR_LIT>" ) ) <EOL> expected = minidom . parseString ( """<STR_LIT>""" . replace ( "<STR_LIT:U+0020>" , "<STR_LIT>" ) % locals ( ) ) <EOL> self . assertEqual ( expected . toxml ( ) , actual . toxml ( ) ) </s>
