Convert to requests-mock

We've had some trouble with httpretty in the past and so are moving to
requests-mock. There should be no functionality change in this patch,
simply a transition to a newer library.

Examples: 
 - Python 2/3 inconsistencies
 - Breaking compatibility between releases
 - Incorrect package dependency specifications
 - Problems with distro packaging around tests 
 - *can* introduce a maintained state between tests.

Change-Id: I666a5c7e6747f0c5c2dc96336774fd0fcd3f5907
This commit is contained in:
Jamie Lennox 2014-07-10 15:14:52 +10:00
parent 77991e571b
commit caf9f799ef
25 changed files with 496 additions and 537 deletions

View File

@ -10,9 +10,6 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import httpretty
from novaclient.openstack.common import jsonutils
from novaclient.tests.fixture_data import base from novaclient.tests.fixture_data import base
@ -35,9 +32,9 @@ class Fixture(base.Fixture):
} }
} }
httpretty.register_uri(httpretty.POST, self.url(), self.requests.register_uri('POST', self.url(),
body=jsonutils.dumps(post_os_agents), json=post_os_agents,
content_type='application/json') headers=self.json_headers)
put_os_agents_1 = { put_os_agents_1 = {
"agent": { "agent": {
@ -48,10 +45,10 @@ class Fixture(base.Fixture):
} }
} }
httpretty.register_uri(httpretty.PUT, self.url(1), self.requests.register_uri('PUT', self.url(1),
body=jsonutils.dumps(put_os_agents_1), json=put_os_agents_1,
content_type='application/json') headers=self.json_headers)
httpretty.register_uri(httpretty.DELETE, self.url(1), self.requests.register_uri('DELETE', self.url(1),
content_type='application/json', headers=self.json_headers,
status=202) status_code=202)

View File

@ -10,9 +10,6 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import httpretty
from novaclient.openstack.common import jsonutils
from novaclient.tests.fixture_data import base from novaclient.tests.fixture_data import base
@ -32,21 +29,24 @@ class Fixture(base.Fixture):
'availability_zone': 'nova1'}, 'availability_zone': 'nova1'},
]} ]}
httpretty.register_uri(httpretty.GET, self.url(), self.requests.register_uri('GET', self.url(),
body=jsonutils.dumps(get_os_aggregates), json=get_os_aggregates,
content_type='application/json') headers=self.json_headers)
r = jsonutils.dumps({'aggregate': get_os_aggregates['aggregates'][0]}) get_aggregates_1 = {'aggregate': get_os_aggregates['aggregates'][0]}
httpretty.register_uri(httpretty.POST, self.url(), body=r, self.requests.register_uri('POST', self.url(),
content_type='application/json') json=get_aggregates_1,
headers=self.json_headers)
for agg_id in (1, 2): for agg_id in (1, 2):
for method in (httpretty.GET, httpretty.PUT): for method in ('GET', 'PUT'):
httpretty.register_uri(method, self.url(agg_id), body=r, self.requests.register_uri(method, self.url(agg_id),
content_type='application/json') json=get_aggregates_1,
headers=self.json_headers)
httpretty.register_uri(httpretty.POST, self.url(agg_id, 'action'), self.requests.register_uri('POST', self.url(agg_id, 'action'),
body=r, content_type='application/json') json=get_aggregates_1,
headers=self.json_headers)
httpretty.register_uri(httpretty.DELETE, self.url(1), status=202) self.requests.register_uri('DELETE', self.url(1), status_code=202)

View File

@ -10,9 +10,6 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import httpretty
from novaclient.openstack.common import jsonutils
from novaclient.tests.fixture_data import base from novaclient.tests.fixture_data import base
@ -41,9 +38,10 @@ class V1(base.Fixture):
} }
] ]
} }
httpretty.register_uri(httpretty.GET, self.url(),
body=jsonutils.dumps(get_os_availability_zone), self.requests.register_uri('GET', self.url(),
content_type='application/json') json=get_os_availability_zone,
headers=self.json_headers)
get_os_zone_detail = { get_os_zone_detail = {
self.zone_info_key: [ self.zone_info_key: [
@ -88,9 +86,9 @@ class V1(base.Fixture):
] ]
} }
httpretty.register_uri(httpretty.GET, self.url('detail'), self.requests.register_uri('GET', self.url('detail'),
body=jsonutils.dumps(get_os_zone_detail), json=get_os_zone_detail,
content_type='application/json') headers=self.json_headers)
class V3(V1): class V3(V1):

View File

@ -19,9 +19,11 @@ COMPUTE_URL = 'http://compute.host'
class Fixture(fixtures.Fixture): class Fixture(fixtures.Fixture):
base_url = None base_url = None
json_headers = {'Content-Type': 'application/json'}
def __init__(self, compute_url=COMPUTE_URL): def __init__(self, requests, compute_url=COMPUTE_URL):
super(Fixture, self).__init__() super(Fixture, self).__init__()
self.requests = requests
self.compute_url = compute_url self.compute_url = compute_url
def url(self, *args, **kwargs): def url(self, *args, **kwargs):

View File

@ -10,9 +10,6 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import httpretty
from novaclient.openstack.common import jsonutils
from novaclient.tests.fixture_data import base from novaclient.tests.fixture_data import base
@ -43,9 +40,9 @@ class Fixture(base.Fixture):
'data': 'foo' 'data': 'foo'
} }
} }
httpretty.register_uri(httpretty.GET, self.url('root'), self.requests.register_uri('GET', self.url('root'),
body=jsonutils.dumps(get_os_certificate), json=get_os_certificate,
content_type='application/json') headers=self.json_headers)
post_os_certificates = { post_os_certificates = {
'certificate': { 'certificate': {
@ -53,6 +50,6 @@ class Fixture(base.Fixture):
'data': 'bar' 'data': 'bar'
} }
} }
httpretty.register_uri(httpretty.POST, self.url(), self.requests.register_uri('POST', self.url(),
body=jsonutils.dumps(post_os_certificates), json=post_os_certificates,
content_type='application/json') headers=self.json_headers)

View File

@ -11,11 +11,9 @@
# under the License. # under the License.
import fixtures import fixtures
import httpretty
from keystoneclient.auth.identity import v2 from keystoneclient.auth.identity import v2
from keystoneclient import session from keystoneclient import session
from novaclient.openstack.common import jsonutils
from novaclient.v1_1 import client as v1_1client from novaclient.v1_1 import client as v1_1client
from novaclient.v3 import client as v3client from novaclient.v3 import client as v3client
@ -25,11 +23,13 @@ COMPUTE_URL = 'http://compute.host'
class V1(fixtures.Fixture): class V1(fixtures.Fixture):
def __init__(self, compute_url=COMPUTE_URL, identity_url=IDENTITY_URL): def __init__(self, requests,
compute_url=COMPUTE_URL, identity_url=IDENTITY_URL):
super(V1, self).__init__() super(V1, self).__init__()
self.identity_url = identity_url self.identity_url = identity_url
self.compute_url = compute_url self.compute_url = compute_url
self.client = None self.client = None
self.requests = requests
self.token = { self.token = {
'access': { 'access': {
@ -86,13 +86,12 @@ class V1(fixtures.Fixture):
def setUp(self): def setUp(self):
super(V1, self).setUp() super(V1, self).setUp()
httpretty.enable()
self.addCleanup(httpretty.disable)
auth_url = '%s/tokens' % self.identity_url auth_url = '%s/tokens' % self.identity_url
httpretty.register_uri(httpretty.POST, auth_url, headers = {'X-Content-Type': 'application/json'}
body=jsonutils.dumps(self.token), self.requests.register_uri('POST', auth_url,
content_type='application/json') json=self.token,
headers=headers)
self.client = self.new_client() self.client = self.new_client()
def new_client(self): def new_client(self):

View File

@ -10,9 +10,6 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import httpretty
from novaclient.openstack.common import jsonutils
from novaclient.tests.fixture_data import base from novaclient.tests.fixture_data import base
@ -24,17 +21,17 @@ class Fixture(base.Fixture):
super(Fixture, self).setUp() super(Fixture, self).setUp()
get_os_cloudpipe = {'cloudpipes': [{'project_id': 1}]} get_os_cloudpipe = {'cloudpipes': [{'project_id': 1}]}
httpretty.register_uri(httpretty.GET, self.url(), self.requests.register_uri('GET', self.url(),
body=jsonutils.dumps(get_os_cloudpipe), json=get_os_cloudpipe,
content_type='application/json') headers=self.json_headers)
instance_id = '9d5824aa-20e6-4b9f-b967-76a699fc51fd' instance_id = '9d5824aa-20e6-4b9f-b967-76a699fc51fd'
post_os_cloudpipe = {'instance_id': instance_id} post_os_cloudpipe = {'instance_id': instance_id}
httpretty.register_uri(httpretty.POST, self.url(), self.requests.register_uri('POST', self.url(),
body=jsonutils.dumps(post_os_cloudpipe), json=post_os_cloudpipe,
content_type='application/json', headers=self.json_headers,
status=202) status_code=202)
httpretty.register_uri(httpretty.PUT, self.url('configure-project'), self.requests.register_uri('PUT', self.url('configure-project'),
content_type='application/json', headers=self.json_headers,
status=202) status_code=202)

View File

@ -10,9 +10,6 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import httpretty
from novaclient.openstack.common import jsonutils
from novaclient.tests.fixture_data import base from novaclient.tests.fixture_data import base
@ -31,11 +28,12 @@ class Fixture(base.Fixture):
'host': 'bar' 'host': 'bar'
} }
} }
httpretty.register_uri(httpretty.GET, self.url('192.168.1.1'),
body=jsonutils.dumps(get_os_fixed_ips),
content_type='application/json')
httpretty.register_uri(httpretty.POST, self.requests.register_uri('GET', self.url('192.168.1.1'),
self.url('192.168.1.1', 'action'), json=get_os_fixed_ips,
content_type='application/json', headers=self.json_headers)
status=202)
self.requests.register_uri('POST',
self.url('192.168.1.1', 'action'),
headers=self.json_headers,
status_code=202)

View File

@ -10,8 +10,6 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import httpretty
from novaclient.openstack.common import jsonutils from novaclient.openstack.common import jsonutils
from novaclient.tests import fakes from novaclient.tests import fakes
from novaclient.tests.fixture_data import base from novaclient.tests.fixture_data import base
@ -28,29 +26,28 @@ class FloatingFixture(base.Fixture):
{'id': 2, 'fixed_ip': '10.0.0.2', 'ip': '11.0.0.2'}] {'id': 2, 'fixed_ip': '10.0.0.2', 'ip': '11.0.0.2'}]
get_os_floating_ips = {'floating_ips': floating_ips} get_os_floating_ips = {'floating_ips': floating_ips}
httpretty.register_uri(httpretty.GET, self.url(), self.requests.register_uri('GET', self.url(),
body=jsonutils.dumps(get_os_floating_ips), json=get_os_floating_ips,
content_type='application/json') headers=self.json_headers)
for ip in floating_ips: for ip in floating_ips:
get_os_floating_ip = {'floating_ip': ip} get_os_floating_ip = {'floating_ip': ip}
httpretty.register_uri(httpretty.GET, self.url(ip['id']), self.requests.register_uri('GET', self.url(ip['id']),
body=jsonutils.dumps(get_os_floating_ip), json=get_os_floating_ip,
content_type='application/json') headers=self.json_headers)
httpretty.register_uri(httpretty.DELETE, self.url(ip['id']), self.requests.register_uri('DELETE', self.url(ip['id']),
content_type='application/json', headers=self.json_headers,
status=204) status_code=204)
def post_os_floating_ips(request, url, headers): def post_os_floating_ips(request, context):
body = jsonutils.loads(request.body.decode('utf-8')) body = jsonutils.loads(request.body)
ip = floating_ips[0].copy() ip = floating_ips[0].copy()
ip['pool'] = body.get('pool') ip['pool'] = body.get('pool')
ip = jsonutils.dumps({'floating_ip': ip}) return {'floating_ip': ip}
return 200, headers, ip self.requests.register_uri('POST', self.url(),
httpretty.register_uri(httpretty.POST, self.url(), json=post_os_floating_ips,
body=post_os_floating_ips, headers=self.json_headers)
content_type='application/json')
class DNSFixture(base.Fixture): class DNSFixture(base.Fixture):
@ -66,10 +63,10 @@ class DNSFixture(base.Fixture):
{'domain': 'example.com'} {'domain': 'example.com'}
] ]
} }
httpretty.register_uri(httpretty.GET, self.url(), self.requests.register_uri('GET', self.url(),
body=jsonutils.dumps(get_os_floating_ip_dns), json=get_os_floating_ip_dns,
content_type='application/json', headers=self.json_headers,
status=205) status_code=205)
get_dns_testdomain_entries_testname = { get_dns_testdomain_entries_testname = {
'dns_entry': { 'dns_entry': {
@ -80,31 +77,30 @@ class DNSFixture(base.Fixture):
} }
} }
url = self.url('testdomain', 'entries', 'testname') url = self.url('testdomain', 'entries', 'testname')
body = jsonutils.dumps(get_dns_testdomain_entries_testname) self.requests.register_uri('GET', url,
httpretty.register_uri(httpretty.GET, url, json=get_dns_testdomain_entries_testname,
body=body, headers=self.json_headers,
content_type='application/json', status_code=205)
status=205)
httpretty.register_uri(httpretty.DELETE, self.url('testdomain'), self.requests.register_uri('DELETE', self.url('testdomain'))
status=200)
url = self.url('testdomain', 'entries', 'testname') url = self.url('testdomain', 'entries', 'testname')
httpretty.register_uri(httpretty.DELETE, url, status=200) self.requests.register_uri('DELETE', url)
def put_dns_testdomain_entries_testname(request, url, headers): def put_dns_testdomain_entries_testname(request, context):
body = jsonutils.loads(request.body.decode('utf-8')) body = jsonutils.loads(request.body)
fakes.assert_has_keys(body['dns_entry'], fakes.assert_has_keys(body['dns_entry'],
required=['ip', 'dns_type']) required=['ip', 'dns_type'])
return 205, headers, request.body context.status_code = 205
httpretty.register_uri(httpretty.PUT, url, return request.body
body=put_dns_testdomain_entries_testname, self.requests.register_uri('PUT', url,
content_type='application/json') body=put_dns_testdomain_entries_testname,
headers=self.json_headers)
url = self.url('testdomain', 'entries') url = self.url('testdomain', 'entries')
httpretty.register_uri(httpretty.GET, url, status=404) self.requests.register_uri('GET', url, status_code=404)
get_os_floating_ip_dns_testdomain_entries = { get_os_floating_ip_dns_testdomain = {
'dns_entries': [ 'dns_entries': [
{ {
'dns_entry': { 'dns_entry': {
@ -124,14 +120,13 @@ class DNSFixture(base.Fixture):
}, },
] ]
} }
body = jsonutils.dumps(get_os_floating_ip_dns_testdomain_entries) self.requests.register_uri('GET', url + '?ip=1.2.3.4',
httpretty.register_uri(httpretty.GET, url + '?ip=1.2.3.4', json=get_os_floating_ip_dns_testdomain,
body=body, status_code=205,
status=205, headers=self.json_headers)
content_type='application/json')
def put_os_floating_ip_dns_testdomain(request, url, headers): def put_os_floating_ip_dns_testdomain(request, context):
body = jsonutils.loads(request.body.decode('utf-8')) body = jsonutils.loads(request.body)
if body['domain_entry']['scope'] == 'private': if body['domain_entry']['scope'] == 'private':
fakes.assert_has_keys(body['domain_entry'], fakes.assert_has_keys(body['domain_entry'],
required=['availability_zone', 'scope']) required=['availability_zone', 'scope'])
@ -142,11 +137,12 @@ class DNSFixture(base.Fixture):
fakes.assert_has_keys(body['domain_entry'], fakes.assert_has_keys(body['domain_entry'],
required=['project', 'scope']) required=['project', 'scope'])
headers['Content-Type'] = 'application/json' return request.body
return (205, headers, request.body)
httpretty.register_uri(httpretty.PUT, self.url('testdomain'), self.requests.register_uri('PUT', self.url('testdomain'),
body=put_os_floating_ip_dns_testdomain) body=put_os_floating_ip_dns_testdomain,
status_code=205,
headers=self.json_headers)
class BulkFixture(base.Fixture): class BulkFixture(base.Fixture):
@ -162,40 +158,38 @@ class BulkFixture(base.Fixture):
{'id': 2, 'fixed_ip': '10.0.0.2', 'ip': '11.0.0.2'}, {'id': 2, 'fixed_ip': '10.0.0.2', 'ip': '11.0.0.2'},
] ]
} }
httpretty.register_uri(httpretty.GET, self.url(), self.requests.register_uri('GET', self.url(),
body=jsonutils.dumps(get_os_floating_ips_bulk), json=get_os_floating_ips_bulk,
content_type='application/json') headers=self.json_headers)
httpretty.register_uri(httpretty.GET, self.url('testHost'), self.requests.register_uri('GET', self.url('testHost'),
body=jsonutils.dumps(get_os_floating_ips_bulk), json=get_os_floating_ips_bulk,
content_type='application/json') headers=self.json_headers)
def put_os_floating_ips_bulk_delete(request, url, headers): def put_os_floating_ips_bulk_delete(request, context):
body = jsonutils.loads(request.body.decode('utf-8')) body = jsonutils.loads(request.body)
ip_range = body.get('ip_range') ip_range = body.get('ip_range')
data = {'floating_ips_bulk_delete': ip_range} return {'floating_ips_bulk_delete': ip_range}
return 200, headers, jsonutils.dumps(data)
httpretty.register_uri(httpretty.PUT, self.url('delete'), self.requests.register_uri('PUT', self.url('delete'),
body=put_os_floating_ips_bulk_delete, json=put_os_floating_ips_bulk_delete,
content_type='application/json') headers=self.json_headers)
def post_os_floating_ips_bulk(request, url, headers): def post_os_floating_ips_bulk(request, context):
body = jsonutils.loads(request.body.decode('utf-8')) body = jsonutils.loads(request.body)
params = body.get('floating_ips_bulk_create') params = body.get('floating_ips_bulk_create')
pool = params.get('pool', 'defaultPool') pool = params.get('pool', 'defaultPool')
interface = params.get('interface', 'defaultInterface') interface = params.get('interface', 'defaultInterface')
data = { return {
'floating_ips_bulk_create': { 'floating_ips_bulk_create': {
'ip_range': '192.168.1.0/30', 'ip_range': '192.168.1.0/30',
'pool': pool, 'pool': pool,
'interface': interface 'interface': interface
} }
} }
return 200, headers, jsonutils.dumps(data)
httpretty.register_uri(httpretty.POST, self.url(), self.requests.register_uri('POST', self.url(),
body=post_os_floating_ips_bulk, json=post_os_floating_ips_bulk,
content_type='application/json') headers=self.json_headers)
class PoolsFixture(base.Fixture): class PoolsFixture(base.Fixture):
@ -211,6 +205,6 @@ class PoolsFixture(base.Fixture):
{'name': 'bar'} {'name': 'bar'}
] ]
} }
httpretty.register_uri(httpretty.GET, self.url(), self.requests.register_uri('GET', self.url(),
body=jsonutils.dumps(get_os_floating_ip_pools), json=get_os_floating_ip_pools,
content_type='application/json') headers=self.json_headers)

View File

@ -10,9 +10,6 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import httpretty
from novaclient.openstack.common import jsonutils
from novaclient.tests.fixture_data import base from novaclient.tests.fixture_data import base
@ -30,9 +27,9 @@ class Fixture(base.Fixture):
"alive": True, "alive": True,
} }
} }
httpretty.register_uri(httpretty.GET, self.url(1), self.requests.register_uri('GET', self.url(1),
body=jsonutils.dumps(get_os_fping_1), json=get_os_fping_1,
content_type='application/json') headers=self.json_headers)
get_os_fping = { get_os_fping = {
'servers': [ 'servers': [
@ -44,6 +41,6 @@ class Fixture(base.Fixture):
}, },
] ]
} }
httpretty.register_uri(httpretty.GET, self.url(), self.requests.register_uri('GET', self.url(),
body=jsonutils.dumps(get_os_fping), json=get_os_fping,
content_type='application/json') headers=self.json_headers)

View File

@ -10,7 +10,6 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import httpretty
from six.moves.urllib import parse from six.moves.urllib import parse
from novaclient.openstack.common import jsonutils from novaclient.openstack.common import jsonutils
@ -36,12 +35,15 @@ class BaseFixture(base.Fixture):
'cpu': 1, 'memory_mb': 2048, 'disk_gb': 30}} 'cpu': 1, 'memory_mb': 2048, 'disk_gb': 30}}
] ]
} }
httpretty.register_uri(httpretty.GET, self.url('host'),
body=jsonutils.dumps(get_os_hosts_host),
content_type='application/json')
def get_os_hosts(request, url, headers): headers = {'Content-Type': 'application/json'}
host, query = parse.splitquery(url)
self.requests.register_uri('GET', self.url('host'),
json=get_os_hosts_host,
headers=headers)
def get_os_hosts(request, context):
host, query = parse.splitquery(request.url)
zone = 'nova1' zone = 'nova1'
if query: if query:
@ -51,7 +53,7 @@ class BaseFixture(base.Fixture):
except Exception: except Exception:
pass pass
data = { return {
'hosts': [ 'hosts': [
{ {
'host': 'host1', 'host': 'host1',
@ -65,56 +67,52 @@ class BaseFixture(base.Fixture):
} }
] ]
} }
return 200, headers, jsonutils.dumps(data)
httpretty.register_uri(httpretty.GET, self.url(), self.requests.register_uri('GET', self.url(),
body=get_os_hosts, json=get_os_hosts,
content_type='application/json') headers=headers)
get_os_hosts_sample_host = { get_os_hosts_sample_host = {
'host': [ 'host': [
{'resource': {'host': 'sample_host'}} {'resource': {'host': 'sample_host'}}
], ],
} }
httpretty.register_uri(httpretty.GET, self.url('sample_host'), self.requests.register_uri('GET', self.url('sample_host'),
body=jsonutils.dumps(get_os_hosts_sample_host), json=get_os_hosts_sample_host,
content_type='application/json') headers=headers)
httpretty.register_uri(httpretty.PUT, self.url('sample_host', 1), self.requests.register_uri('PUT', self.url('sample_host', 1),
body=jsonutils.dumps(self.put_host_1()), json=self.put_host_1(),
content_type='application/json') headers=headers)
httpretty.register_uri(httpretty.PUT, self.url('sample_host', 2), self.requests.register_uri('PUT', self.url('sample_host', 2),
body=jsonutils.dumps(self.put_host_2()), json=self.put_host_2(),
content_type='application/json') headers=headers)
httpretty.register_uri(httpretty.PUT, self.url('sample_host', 3), self.requests.register_uri('PUT', self.url('sample_host', 3),
body=jsonutils.dumps(self.put_host_3()), json=self.put_host_3(),
content_type='application/json') headers=headers)
url = self.url('sample_host', 'reboot') self.requests.register_uri('GET', self.url('sample_host', 'reboot'),
httpretty.register_uri(httpretty.GET, url, json=self.get_host_reboot(),
body=jsonutils.dumps(self.get_host_reboot()), headers=headers)
content_type='application/json')
url = self.url('sample_host', 'startup') self.requests.register_uri('GET', self.url('sample_host', 'startup'),
httpretty.register_uri(httpretty.GET, url, json=self.get_host_startup(),
body=jsonutils.dumps(self.get_host_startup()), headers=headers)
content_type='application/json')
url = self.url('sample_host', 'shutdown') self.requests.register_uri('GET', self.url('sample_host', 'shutdown'),
httpretty.register_uri(httpretty.GET, url, json=self.get_host_shutdown(),
body=jsonutils.dumps(self.get_host_shutdown()), headers=headers)
content_type='application/json')
def put_os_hosts_sample_host(request, url, headers): def put_os_hosts_sample_host(request, context):
result = {'host': 'dummy'} result = {'host': 'dummy'}
result.update(jsonutils.loads(request.body.decode('utf-8'))) result.update(jsonutils.loads(request.body))
return 200, headers, jsonutils.dumps(result) return result
httpretty.register_uri(httpretty.PUT, self.url('sample_host'), self.requests.register_uri('PUT', self.url('sample_host'),
body=put_os_hosts_sample_host, json=put_os_hosts_sample_host,
content_type='application/json') headers=headers)
class V1(BaseFixture): class V1(BaseFixture):

View File

@ -10,9 +10,6 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import httpretty
from novaclient.openstack.common import jsonutils
from novaclient.tests.fixture_data import base from novaclient.tests.fixture_data import base
@ -30,9 +27,11 @@ class V1(base.Fixture):
] ]
} }
httpretty.register_uri(httpretty.GET, self.url(), self.headers = {'Content-Type': 'application/json'}
body=jsonutils.dumps(get_os_hypervisors),
content_type='application/json') self.requests.register_uri('GET', self.url(),
json=get_os_hypervisors,
headers=self.headers)
get_os_hypervisors_detail = { get_os_hypervisors_detail = {
'hypervisors': [ 'hypervisors': [
@ -83,9 +82,9 @@ class V1(base.Fixture):
] ]
} }
httpretty.register_uri(httpretty.GET, self.url('detail'), self.requests.register_uri('GET', self.url('detail'),
body=jsonutils.dumps(get_os_hypervisors_detail), json=get_os_hypervisors_detail,
content_type='application/json') headers=self.headers)
get_os_hypervisors_stats = { get_os_hypervisors_stats = {
'hypervisor_statistics': { 'hypervisor_statistics': {
@ -104,9 +103,9 @@ class V1(base.Fixture):
} }
} }
httpretty.register_uri(httpretty.GET, self.url('statistics'), self.requests.register_uri('GET', self.url('statistics'),
body=jsonutils.dumps(get_os_hypervisors_stats), json=get_os_hypervisors_stats,
content_type='application/json') headers=self.headers)
get_os_hypervisors_search = { get_os_hypervisors_search = {
'hypervisors': [ 'hypervisors': [
@ -115,9 +114,9 @@ class V1(base.Fixture):
] ]
} }
httpretty.register_uri(httpretty.GET, self.url('hyper', 'search'), self.requests.register_uri('GET', self.url('hyper', 'search'),
body=jsonutils.dumps(get_os_hypervisors_search), json=get_os_hypervisors_search,
content_type='application/json') headers=self.headers)
get_hyper_server = { get_hyper_server = {
'hypervisors': [ 'hypervisors': [
@ -140,9 +139,9 @@ class V1(base.Fixture):
] ]
} }
httpretty.register_uri(httpretty.GET, self.url('hyper', 'servers'), self.requests.register_uri('GET', self.url('hyper', 'servers'),
body=jsonutils.dumps(get_hyper_server), json=get_hyper_server,
content_type='application/json') headers=self.headers)
get_os_hypervisors_1234 = { get_os_hypervisors_1234 = {
'hypervisor': { 'hypervisor': {
@ -166,9 +165,9 @@ class V1(base.Fixture):
} }
} }
httpretty.register_uri(httpretty.GET, self.url(1234), self.requests.register_uri('GET', self.url(1234),
body=jsonutils.dumps(get_os_hypervisors_1234), json=get_os_hypervisors_1234,
content_type='application/json') headers=self.headers)
get_os_hypervisors_uptime = { get_os_hypervisors_uptime = {
'hypervisor': { 'hypervisor': {
@ -178,9 +177,9 @@ class V1(base.Fixture):
} }
} }
httpretty.register_uri(httpretty.GET, self.url(1234, 'uptime'), self.requests.register_uri('GET', self.url(1234, 'uptime'),
body=jsonutils.dumps(get_os_hypervisors_uptime), json=get_os_hypervisors_uptime,
content_type='application/json') headers=self.headers)
class V3(V1): class V3(V1):
@ -195,10 +194,10 @@ class V3(V1):
] ]
} }
httpretty.register_uri(httpretty.GET, self.requests.register_uri('GET',
self.url('search', query='hyper'), self.url('search', query='hyper'),
body=jsonutils.dumps(get_os_hypervisors_search), json=get_os_hypervisors_search,
content_type='application/json') headers=self.headers)
get_1234_servers = { get_1234_servers = {
'hypervisor': { 'hypervisor': {
@ -211,6 +210,6 @@ class V3(V1):
}, },
} }
httpretty.register_uri(httpretty.GET, self.url(1234, 'servers'), self.requests.register_uri('GET', self.url(1234, 'servers'),
body=jsonutils.dumps(get_1234_servers), json=get_1234_servers,
content_type='application/json') headers=self.headers)

View File

@ -10,8 +10,6 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import httpretty
from novaclient.openstack.common import jsonutils from novaclient.openstack.common import jsonutils
from novaclient.tests import fakes from novaclient.tests import fakes
from novaclient.tests.fixture_data import base from novaclient.tests.fixture_data import base
@ -31,9 +29,11 @@ class V1(base.Fixture):
] ]
} }
httpretty.register_uri(httpretty.GET, self.url(), headers = {'Content-Type': 'application/json'}
body=jsonutils.dumps(get_images),
content_type='application/json') self.requests.register_uri('GET', self.url(),
json=get_images,
headers=headers)
image_1 = { image_1 = {
'id': 1, 'id': 1,
@ -58,60 +58,53 @@ class V1(base.Fixture):
"links": {}, "links": {},
} }
get_images_detail = {'images': [image_1, image_2]} self.requests.register_uri('GET', self.url('detail'),
json={'images': [image_1, image_2]},
headers=headers)
httpretty.register_uri(httpretty.GET, self.url('detail'), self.requests.register_uri('GET', self.url(1),
body=jsonutils.dumps(get_images_detail), json={'image': image_1},
content_type='application/json') headers=headers)
get_images_1 = {'image': image_1} self.requests.register_uri('GET', self.url(2),
json={'image': image_2},
headers=headers)
httpretty.register_uri(httpretty.GET, self.url(1), self.requests.register_uri('GET', self.url(456),
body=jsonutils.dumps(get_images_1), json={'image': image_2},
content_type='application/json') headers=headers)
get_images_2 = {'image': image_2} def post_images(request, context):
body = jsonutils.loads(request.body)
httpretty.register_uri(httpretty.GET, self.url(2),
body=jsonutils.dumps(get_images_2),
content_type='application/json')
httpretty.register_uri(httpretty.GET, self.url(456),
body=jsonutils.dumps(get_images_2),
content_type='application/json')
def post_images(request, url, headers):
body = jsonutils.loads(request.body.decode('utf-8'))
assert list(body) == ['image'] assert list(body) == ['image']
fakes.assert_has_keys(body['image'], required=['serverId', 'name']) fakes.assert_has_keys(body['image'], required=['serverId', 'name'])
return 202, headers, jsonutils.dumps(images_1) return images_1
httpretty.register_uri(httpretty.POST, self.url(), self.requests.register_uri('POST', self.url(),
body=post_images, json=post_images,
content_type='application/json') headers=headers,
status_code=202)
def post_images_1_metadata(request, url, headers): def post_images_1_metadata(request, context):
body = jsonutils.loads(request.body.decode('utf-8')) body = jsonutils.loads(request.body)
assert list(body) == ['metadata'] assert list(body) == ['metadata']
fakes.assert_has_keys(body['metadata'], required=['test_key']) fakes.assert_has_keys(body['metadata'], required=['test_key'])
data = jsonutils.dumps({'metadata': image_1['metadata']}) return {'metadata': image_1['metadata']}
return 200, headers, data
httpretty.register_uri(httpretty.POST, self.url(1, 'metadata'), self.requests.register_uri('POST', self.url(1, 'metadata'),
body=post_images_1_metadata, json=post_images_1_metadata,
content_type='application/json') headers=headers)
for u in (1, 2, '1/metadata/test_key'): for u in (1, 2, '1/metadata/test_key'):
httpretty.register_uri(httpretty.DELETE, self.url(u), self.requests.register_uri('DELETE', self.url(u), status_code=204)
status=204)
httpretty.register_uri(httpretty.HEAD, self.url(1), status=200, image_headers = {'x-image-meta-id': '1',
x_image_meta_id=1, 'x-image-meta-name': 'CentOS 5.2',
x_image_meta_name='CentOS 5.2', 'x-image-meta-updated': '2010-10-10T12:00:00Z',
x_image_meta_updated='2010-10-10T12:00:00Z', 'x-image-meta-created': '2010-10-10T12:00:00Z',
x_image_meta_created='2010-10-10T12:00:00Z', 'x-image-meta-status': 'ACTIVE',
x_image_meta_status='ACTIVE', 'x-image-meta-property-test-key': 'test_value'}
x_image_meta_property_test_key='test_value') self.requests.register_uri('HEAD', self.url(1), headers=image_headers)
class V3(V1): class V3(V1):

View File

@ -10,9 +10,6 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import httpretty
from novaclient.openstack.common import jsonutils from novaclient.openstack.common import jsonutils
from novaclient.tests import fakes from novaclient.tests import fakes
from novaclient.tests.fixture_data import base from novaclient.tests.fixture_data import base
@ -26,25 +23,27 @@ class V1(base.Fixture):
super(V1, self).setUp() super(V1, self).setUp()
keypair = {'fingerprint': 'FAKE_KEYPAIR', 'name': 'test'} keypair = {'fingerprint': 'FAKE_KEYPAIR', 'name': 'test'}
httpretty.register_uri(httpretty.GET, self.url(), headers = {'Content-Type': 'application/json'}
body=jsonutils.dumps({'keypairs': [keypair]}),
content_type='application/json')
httpretty.register_uri(httpretty.GET, self.url('test'), self.requests.register_uri('GET', self.url(),
body=jsonutils.dumps({'keypair': keypair}), json={'keypairs': [keypair]},
content_type='application/json') headers=headers)
httpretty.register_uri(httpretty.DELETE, self.url('test'), status=202) self.requests.register_uri('GET', self.url('test'),
json={'keypair': keypair},
headers=headers)
def post_os_keypairs(request, url, headers): self.requests.register_uri('DELETE', self.url('test'), status_code=202)
body = jsonutils.loads(request.body.decode('utf-8'))
def post_os_keypairs(request, context):
body = jsonutils.loads(request.body)
assert list(body) == ['keypair'] assert list(body) == ['keypair']
fakes.assert_has_keys(body['keypair'], required=['name']) fakes.assert_has_keys(body['keypair'], required=['name'])
return 202, headers, jsonutils.dumps({'keypair': keypair}) return {'keypair': keypair}
httpretty.register_uri(httpretty.POST, self.url(), self.requests.register_uri('POST', self.url(),
body=post_os_keypairs, json=post_os_keypairs,
content_type='application/json') headers=headers)
class V3(V1): class V3(V1):

View File

@ -10,9 +10,6 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import httpretty
from novaclient.openstack.common import jsonutils
from novaclient.tests.fixture_data import base from novaclient.tests.fixture_data import base
@ -77,6 +74,7 @@ class Fixture(base.Fixture):
}, },
} }
httpretty.register_uri(httpretty.GET, self.url(), headers = {'Content-Type': 'application/json'}
body=jsonutils.dumps(get_limits), self.requests.register_uri('GET', self.url(),
content_type='application/json') json=get_limits,
headers=headers)

View File

@ -10,8 +10,6 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import httpretty
from novaclient.openstack.common import jsonutils from novaclient.openstack.common import jsonutils
from novaclient.tests.fixture_data import base from novaclient.tests.fixture_data import base
@ -34,29 +32,30 @@ class Fixture(base.Fixture):
] ]
} }
httpretty.register_uri(httpretty.GET, self.url(), headers = {'Content-Type': 'application/json'}
body=jsonutils.dumps(get_os_networks),
content_type='application/json')
def post_os_networks(request, url, headers): self.requests.register_uri('GET', self.url(),
body = jsonutils.loads(request.body.decode('utf-8')) json=get_os_networks,
data = jsonutils.dumps({'network': body}) headers=headers)
return 202, headers, data
httpretty.register_uri(httpretty.POST, self.url(), def post_os_networks(request, context):
body=post_os_networks, body = jsonutils.loads(request.body)
content_type='application/json') return {'network': body}
self.requests.register_uri("POST", self.url(),
json=post_os_networks,
headers=headers)
get_os_networks_1 = {'network': {"label": "1", "cidr": "10.0.0.0/24"}} get_os_networks_1 = {'network': {"label": "1", "cidr": "10.0.0.0/24"}}
httpretty.register_uri(httpretty.GET, self.url(1), self.requests.register_uri('GET', self.url(1),
body=jsonutils.dumps(get_os_networks_1), json=get_os_networks_1,
content_type='application/json') headers=headers)
httpretty.register_uri(httpretty.DELETE, self.requests.register_uri('DELETE',
self.url('networkdelete'), self.url('networkdelete'),
stauts=202) status_code=202)
for u in ('add', 'networkdisassociate/action', 'networktest/action', for u in ('add', 'networkdisassociate/action', 'networktest/action',
'1/action', '2/action'): '1/action', '2/action'):
httpretty.register_uri(httpretty.POST, self.url(u), stauts=202) self.requests.register_uri('POST', self.url(u), status_code=202)

View File

@ -10,9 +10,6 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import httpretty
from novaclient.openstack.common import jsonutils
from novaclient.tests.fixture_data import base from novaclient.tests.fixture_data import base
@ -25,32 +22,32 @@ class V1(base.Fixture):
uuid = '97f4c221-bff4-4578-b030-0df4ef119353' uuid = '97f4c221-bff4-4578-b030-0df4ef119353'
uuid2 = '97f4c221bff44578b0300df4ef119353' uuid2 = '97f4c221bff44578b0300df4ef119353'
test_json = jsonutils.dumps({'quota_set': self.test_quota('test')}) test_json = {'quota_set': self.test_quota('test')}
self.headers = {'Content-Type': 'application/json'}
for u in ('test', 'tenant-id', 'tenant-id/defaults', for u in ('test', 'tenant-id', 'tenant-id/defaults',
'%s/defaults' % uuid2): '%s/defaults' % uuid2):
httpretty.register_uri(httpretty.GET, self.url(u), self.requests.register_uri('GET', self.url(u),
body=test_json, json=test_json,
content_type='application/json') headers=self.headers)
quota_json = jsonutils.dumps({'quota_set': self.test_quota(uuid)}) self.requests.register_uri('PUT', self.url(uuid),
httpretty.register_uri(httpretty.PUT, self.url(uuid), json={'quota_set': self.test_quota(uuid)},
body=quota_json, headers=self.headers)
content_type='application/json')
httpretty.register_uri(httpretty.GET, self.url(uuid),
body=quota_json,
content_type='application/json')
quota_json2 = jsonutils.dumps({'quota_set': self.test_quota(uuid2)}) self.requests.register_uri('GET', self.url(uuid),
httpretty.register_uri(httpretty.PUT, self.url(uuid2), json={'quota_set': self.test_quota(uuid)},
body=quota_json2, headers=self.headers)
content_type='application/json')
httpretty.register_uri(httpretty.GET, self.url(uuid2), self.requests.register_uri('PUT', self.url(uuid2),
body=quota_json2, json={'quota_set': self.test_quota(uuid2)},
content_type='application/json') headers=self.headers)
self.requests.register_uri('GET', self.url(uuid2),
json={'quota_set': self.test_quota(uuid2)},
headers=self.headers)
for u in ('test', uuid2): for u in ('test', uuid2):
httpretty.register_uri(httpretty.DELETE, self.url(u), status=202) self.requests.register_uri('DELETE', self.url(u), status_code=202)
def test_quota(self, tenant_id='test'): def test_quota(self, tenant_id='test'):
return { return {
@ -82,6 +79,6 @@ class V3(V1):
} }
} }
httpretty.register_uri(httpretty.GET, self.url('test', 'detail'), self.requests.register_uri('GET', self.url('test', 'detail'),
body=jsonutils.dumps(get_detail), json=get_detail,
content_type='application/json') headers=self.headers)

View File

@ -10,8 +10,6 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import httpretty
from novaclient.openstack.common import jsonutils from novaclient.openstack.common import jsonutils
from novaclient.tests import fakes from novaclient.tests import fakes
from novaclient.tests.fixture_data import base from novaclient.tests.fixture_data import base
@ -34,24 +32,26 @@ class Fixture(base.Fixture):
'cidr': '10.0.0.0/8' 'cidr': '10.0.0.0/8'
} }
get_rules = {'security_group_rules': [rule]} headers = {'Content-Type': 'application/json'}
httpretty.register_uri(httpretty.GET, self.url(),
body=jsonutils.dumps(get_rules), self.requests.register_uri('GET', self.url(),
content_type='application/json') json={'security_group_rules': [rule]},
headers=headers)
for u in (1, 11, 12): for u in (1, 11, 12):
httpretty.register_uri(httpretty.DELETE, self.url(u), status=202) self.requests.register_uri('DELETE', self.url(u), status_code=202)
def post_rules(request, url, headers): def post_rules(request, context):
body = jsonutils.loads(request.body.decode('utf-8')) body = jsonutils.loads(request.body)
assert list(body) == ['security_group_rule'] assert list(body) == ['security_group_rule']
fakes.assert_has_keys(body['security_group_rule'], fakes.assert_has_keys(body['security_group_rule'],
required=['parent_group_id'], required=['parent_group_id'],
optional=['group_id', 'ip_protocol', optional=['group_id', 'ip_protocol',
'from_port', 'to_port', 'cidr']) 'from_port', 'to_port', 'cidr'])
return 202, headers, jsonutils.dumps({'security_group_rule': rule}) return {'security_group_rule': rule}
httpretty.register_uri(httpretty.POST, self.url(), self.requests.register_uri('POST', self.url(),
body=post_rules, json=post_rules,
content_type='application/json') headers=headers,
status_code=202)

View File

@ -10,9 +10,6 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import httpretty
from novaclient.openstack.common import jsonutils from novaclient.openstack.common import jsonutils
from novaclient.tests import fakes from novaclient.tests import fakes
from novaclient.tests.fixture_data import base from novaclient.tests.fixture_data import base
@ -64,36 +61,39 @@ class Fixture(base.Fixture):
} }
get_groups = {'security_groups': [security_group_1, security_group_2]} get_groups = {'security_groups': [security_group_1, security_group_2]}
httpretty.register_uri(httpretty.GET, self.url(), headers = {'Content-Type': 'application/json'}
body=jsonutils.dumps(get_groups),
content_type='application/json') self.requests.register_uri('GET', self.url(),
json=get_groups,
headers=headers)
get_group_1 = {'security_group': security_group_1} get_group_1 = {'security_group': security_group_1}
httpretty.register_uri(httpretty.GET, self.url(1), self.requests.register_uri('GET', self.url(1),
body=jsonutils.dumps(get_group_1), json=get_group_1,
content_type='application/json') headers=headers)
httpretty.register_uri(httpretty.DELETE, self.url(1), status=202) self.requests.register_uri('DELETE', self.url(1), status_code=202)
def post_os_security_groups(request, url, headers): def post_os_security_groups(request, context):
body = jsonutils.loads(request.body.decode('utf-8')) body = jsonutils.loads(request.body)
assert list(body) == ['security_group'] assert list(body) == ['security_group']
fakes.assert_has_keys(body['security_group'], fakes.assert_has_keys(body['security_group'],
required=['name', 'description']) required=['name', 'description'])
r = jsonutils.dumps({'security_group': security_group_1}) return {'security_group': security_group_1}
return 202, headers, r
httpretty.register_uri(httpretty.POST, self.url(), self.requests.register_uri('POST', self.url(),
body=post_os_security_groups, json=post_os_security_groups,
content_type='application/json') headers=headers,
status_code=202)
def put_os_security_groups_1(request, url, headers): def put_os_security_groups_1(request, context):
body = jsonutils.loads(request.body.decode('utf-8')) body = jsonutils.loads(request.body)
assert list(body) == ['security_group'] assert list(body) == ['security_group']
fakes.assert_has_keys(body['security_group'], fakes.assert_has_keys(body['security_group'],
required=['name', 'description']) required=['name', 'description'])
return 205, headers, request.body return body
httpretty.register_uri(httpretty.PUT, self.url(1), self.requests.register_uri('PUT', self.url(1),
body=put_os_security_groups_1, json=put_os_security_groups_1,
content_type='application/json') headers=headers,
status_code=205)

View File

@ -10,9 +10,6 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import httpretty
from novaclient.openstack.common import jsonutils from novaclient.openstack.common import jsonutils
from novaclient.tests.fixture_data import base from novaclient.tests.fixture_data import base
@ -54,22 +51,23 @@ class Fixture(base.Fixture):
} }
] ]
get_server_groups = {'server_groups': server_groups} headers = {'Content-Type': 'application/json'}
httpretty.register_uri(httpretty.GET, self.url(),
body=jsonutils.dumps(get_server_groups), self.requests.register_uri('GET', self.url(),
content_type='application/json') json={'server_groups': server_groups},
headers=headers)
server = server_groups[0] server = server_groups[0]
server_json = jsonutils.dumps({'server_group': server}) server_j = jsonutils.dumps({'server_group': server})
def _register(method, *args): def _register(method, *args):
httpretty.register_uri(method, self.url(*args), body=server_json) self.requests.register_uri(method, self.url(*args), text=server_j)
_register(httpretty.POST) _register('POST')
_register(httpretty.POST, server['id']) _register('POST', server['id'])
_register(httpretty.GET, server['id']) _register('GET', server['id'])
_register(httpretty.PUT, server['id']) _register('PUT', server['id'])
_register(httpretty.POST, server['id'], '/action') _register('POST', server['id'], '/action')
httpretty.register_uri(httpretty.DELETE, self.url(server['id']), self.requests.register_uri('DELETE', self.url(server['id']),
status=202) status_code=202)

View File

@ -10,8 +10,6 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import httpretty
from novaclient.openstack.common import jsonutils from novaclient.openstack.common import jsonutils
from novaclient.tests import fakes from novaclient.tests import fakes
from novaclient.tests.fixture_data import base from novaclient.tests.fixture_data import base
@ -31,9 +29,9 @@ class Base(base.Fixture):
] ]
} }
httpretty.register_uri(httpretty.GET, self.url(), self.requests.register_uri('GET', self.url(),
body=jsonutils.dumps(get_servers), json=get_servers,
content_type='application/json') headers=self.json_headers)
self.server_1234 = { self.server_1234 = {
"id": 1234, "id": 1234,
@ -151,48 +149,47 @@ class Base(base.Fixture):
servers = [self.server_1234, self.server_5678, self.server_9012] servers = [self.server_1234, self.server_5678, self.server_9012]
get_servers_detail = {"servers": servers} get_servers_detail = {"servers": servers}
httpretty.register_uri(httpretty.GET, self.url('detail'), self.requests.register_uri('GET', self.url('detail'),
body=jsonutils.dumps(get_servers_detail), json=get_servers_detail,
content_type='application/json') headers=self.json_headers)
self.server_1235 = self.server_1234.copy() self.server_1235 = self.server_1234.copy()
self.server_1235['id'] = 1235 self.server_1235['id'] = 1235
self.server_1235['status'] = 'error' self.server_1235['status'] = 'error'
self.server_1235['fault'] = {'message': 'something went wrong!'} self.server_1235['fault'] = {'message': 'something went wrong!'}
servers.append(self.server_1235)
for s in servers: for s in servers + [self.server_1235]:
httpretty.register_uri(httpretty.GET, self.url(s['id']), self.requests.register_uri('GET', self.url(s['id']),
body=jsonutils.dumps({'server': s}), json={'server': s},
content_type='application/json') headers=self.json_headers)
for s in (1234, 5678): for s in (1234, 5678):
httpretty.register_uri(httpretty.DELETE, self.url(s), status=202) self.requests.register_uri('DELETE', self.url(s), status_code=202)
for k in ('test_key', 'key1', 'key2'): for k in ('test_key', 'key1', 'key2'):
httpretty.register_uri(httpretty.DELETE, self.requests.register_uri('DELETE',
self.url(1234, 'metadata', k), self.url(1234, 'metadata', k),
status=204) status_code=204)
metadata1 = jsonutils.dumps({'metadata': {'test_key': 'test_value'}}) metadata1 = {'metadata': {'test_key': 'test_value'}}
httpretty.register_uri(httpretty.POST, self.url(1234, 'metadata'), self.requests.register_uri('POST', self.url(1234, 'metadata'),
body=metadata1, status=200, json=metadata1,
content_type='application/json') headers=self.json_headers)
httpretty.register_uri(httpretty.PUT, self.requests.register_uri('PUT',
self.url(1234, 'metadata', 'test_key'), self.url(1234, 'metadata', 'test_key'),
body=metadata1, status=200, json=metadata1,
content_type='application/json') headers=self.json_headers)
self.diagnostic = jsonutils.dumps({'data': 'Fake diagnostics'}) self.diagnostic = {'data': 'Fake diagnostics'}
metadata2 = jsonutils.dumps({'metadata': {'key1': 'val1'}}) metadata2 = {'metadata': {'key1': 'val1'}}
for u in ('uuid1', 'uuid2', 'uuid3', 'uuid4'): for u in ('uuid1', 'uuid2', 'uuid3', 'uuid4'):
httpretty.register_uri(httpretty.POST, self.url(u, 'metadata'), self.requests.register_uri('POST', self.url(u, 'metadata'),
body=metadata2, status=204) json=metadata2, status_code=204)
httpretty.register_uri(httpretty.DELETE, self.requests.register_uri('DELETE',
self.url(u, 'metadata', 'key1'), self.url(u, 'metadata', 'key1'),
body=self.diagnostic, json=self.diagnostic,
content_type='application/json') headers=self.json_headers)
get_security_groups = { get_security_groups = {
"security_groups": [{ "security_groups": [{
@ -203,18 +200,17 @@ class Base(base.Fixture):
'rules': []}] 'rules': []}]
} }
httpretty.register_uri(httpretty.GET, self.requests.register_uri('GET',
self.url('1234', 'os-security-groups'), self.url('1234', 'os-security-groups'),
body=jsonutils.dumps(get_security_groups), json=get_security_groups)
status=200)
httpretty.register_uri(httpretty.POST, self.url(), self.requests.register_uri('POST', self.url(),
body=self.post_servers, json=self.post_servers,
content_type='application/json') headers=self.json_headers)
httpretty.register_uri(httpretty.POST, self.url('1234', 'action'), self.requests.register_uri('POST', self.url('1234', 'action'),
body=self.post_servers_1234_action, json=self.post_servers_1234_action,
content_type='application/json') headers=self.json_headers)
get_os_interface = { get_os_interface = {
"interfaceAttachments": [ "interfaceAttachments": [
@ -235,30 +231,31 @@ class Base(base.Fixture):
] ]
} }
httpretty.register_uri(httpretty.GET, self.requests.register_uri('GET',
self.url('1234', 'os-interface'), self.url('1234', 'os-interface'),
body=jsonutils.dumps(get_os_interface), json=get_os_interface,
content_type='application/json') headers=self.json_headers)
interface_data = {'interfaceAttachment': {}} interface_data = {'interfaceAttachment': {}}
httpretty.register_uri(httpretty.POST, self.requests.register_uri('POST',
self.url('1234', 'os-interface'), self.url('1234', 'os-interface'),
body=jsonutils.dumps(interface_data), json=interface_data,
content_type='application/json') headers=self.json_headers)
def put_servers_1234(request, url, headers): def put_servers_1234(request, context):
body = jsonutils.loads(request.body.decode('utf-8')) body = jsonutils.loads(request.body)
assert list(body) == ['server'] assert list(body) == ['server']
fakes.assert_has_keys(body['server'], fakes.assert_has_keys(body['server'],
optional=['name', 'adminPass']) optional=['name', 'adminPass'])
return 204, headers, request.body return request.body
httpretty.register_uri(httpretty.PUT, self.url(1234), self.requests.register_uri('PUT', self.url(1234),
body=put_servers_1234, body=put_servers_1234,
content_type='application/json') status_code=204,
headers=self.json_headers)
def post_os_volumes_boot(request, url, headers): def post_os_volumes_boot(request, context):
body = jsonutils.loads(request.body.decode('utf-8')) body = jsonutils.loads(request.body)
assert (set(body.keys()) <= assert (set(body.keys()) <=
set(['server', 'os:scheduler_hints'])) set(['server', 'os:scheduler_hints']))
@ -277,23 +274,24 @@ class Base(base.Fixture):
msg = "found extra keys: 'block_device_mapping'" msg = "found extra keys: 'block_device_mapping'"
raise AssertionError(msg) raise AssertionError(msg)
return 202, headers, jsonutils.dumps({'server': self.server_9012}) return {'server': self.server_9012}
# NOTE(jamielennox): hack to make os_volumes mock go to the right place # NOTE(jamielennox): hack to make os_volumes mock go to the right place
base_url = self.base_url base_url = self.base_url
self.base_url = None self.base_url = None
httpretty.register_uri(httpretty.POST, self.url('os-volumes_boot'), self.requests.register_uri('POST', self.url('os-volumes_boot'),
body=post_os_volumes_boot, json=post_os_volumes_boot,
content_type='application/json') status_code=202,
headers=self.json_headers)
self.base_url = base_url self.base_url = base_url
# #
# Server password # Server password
# #
httpretty.register_uri(httpretty.DELETE, self.requests.register_uri('DELETE',
self.url(1234, 'os-server-password'), self.url(1234, 'os-server-password'),
status=202) status_code=202)
class V1(Base): class V1(Base):
@ -306,29 +304,28 @@ class V1(Base):
# #
add = self.server_1234['addresses'] add = self.server_1234['addresses']
httpretty.register_uri(httpretty.GET, self.url(1234, 'ips'), self.requests.register_uri('GET', self.url(1234, 'ips'),
jsonutils.dumps({'addresses': add}), json={'addresses': add},
content_type='application/json') headers=self.json_headers)
httpretty.register_uri(httpretty.GET, self.url(1234, 'ips', 'public'), self.requests.register_uri('GET', self.url(1234, 'ips', 'public'),
jsonutils.dumps({'public': add['public']}), json={'public': add['public']},
content_type='application/json') headers=self.json_headers)
httpretty.register_uri(httpretty.GET, self.url(1234, 'ips', 'private'), self.requests.register_uri('GET', self.url(1234, 'ips', 'private'),
jsonutils.dumps({'private': add['private']}), json={'private': add['private']},
content_type='application/json') headers=self.json_headers)
httpretty.register_uri(httpretty.DELETE, self.requests.register_uri('DELETE',
self.url(1234, 'ips', 'public', '1.2.3.4'), self.url(1234, 'ips', 'public', '1.2.3.4'),
status=202) status_code=202)
httpretty.register_uri(httpretty.GET, self.requests.register_uri('GET',
self.url('1234', 'diagnostics'), self.url('1234', 'diagnostics'),
body=self.diagnostic, json=self.diagnostic)
status=200)
httpretty.register_uri(httpretty.DELETE, self.requests.register_uri('DELETE',
self.url('1234', 'os-interface', 'port-id')) self.url('1234', 'os-interface', 'port-id'))
# Testing with the following password and key # Testing with the following password and key
# #
@ -351,12 +348,13 @@ class V1(Base):
'/y5a6Z3/AoJZYGG7IH5WN88UROU3B9JZGFB2qtPLQTOvDMZLUhoPRIJeHiVSlo1N' '/y5a6Z3/AoJZYGG7IH5WN88UROU3B9JZGFB2qtPLQTOvDMZLUhoPRIJeHiVSlo1N'
'tI2/++UsXVg3ow6ItqCJGgdNuGG5JB+bslDHWPxROpesEIHdczk46HCpHQN8f1sk' 'tI2/++UsXVg3ow6ItqCJGgdNuGG5JB+bslDHWPxROpesEIHdczk46HCpHQN8f1sk'
'Hi/fmZZNQQqj1Ijq0caOIw=='} 'Hi/fmZZNQQqj1Ijq0caOIw=='}
httpretty.register_uri(httpretty.GET, self.requests.register_uri('GET',
self.url(1234, 'os-server-password'), self.url(1234, 'os-server-password'),
jsonutils.dumps(get_server_password)) json=get_server_password)
def post_servers(self, request, url, headers): def post_servers(self, request, context):
body = jsonutils.loads(request.body.decode('utf-8')) body = jsonutils.loads(request.body)
context.status_code = 202
assert (set(body.keys()) <= assert (set(body.keys()) <=
set(['server', 'os:scheduler_hints'])) set(['server', 'os:scheduler_hints']))
fakes.assert_has_keys(body['server'], fakes.assert_has_keys(body['server'],
@ -370,12 +368,12 @@ class V1(Base):
else: else:
body = self.server_1234 body = self.server_1234
return 202, headers, jsonutils.dumps({'server': body}) return {'server': body}
def post_servers_1234_action(self, request, url, headers): def post_servers_1234_action(self, request, context):
_body = '' _body = ''
body = jsonutils.loads(request.body.decode('utf-8')) body = jsonutils.loads(request.body)
resp = 202 context.status_code = 202
assert len(body.keys()) == 1 assert len(body.keys()) == 1
action = list(body)[0] action = list(body)[0]
if action == 'reboot': if action == 'reboot':
@ -393,7 +391,8 @@ class V1(Base):
elif action == 'confirmResize': elif action == 'confirmResize':
assert body[action] is None assert body[action] is None
# This one method returns a different response code # This one method returns a different response code
return 204, headers, '' context.status_code = 204
return None
elif action == 'revertResize': elif action == 'revertResize':
assert body[action] is None assert body[action] is None
elif action == 'migrate': elif action == 'migrate':
@ -445,12 +444,13 @@ class V1(Base):
assert list(body[action]) == ['address'] assert list(body[action]) == ['address']
elif action == 'createImage': elif action == 'createImage':
assert set(body[action].keys()) == set(['name', 'metadata']) assert set(body[action].keys()) == set(['name', 'metadata'])
headers['location'] = "http://blah/images/456" context.headers['location'] = "http://blah/images/456"
elif action == 'changePassword': elif action == 'changePassword':
assert list(body[action]) == ['adminPass'] assert list(body[action]) == ['adminPass']
elif action == 'os-getConsoleOutput': elif action == 'os-getConsoleOutput':
assert list(body[action]) == ['length'] assert list(body[action]) == ['length']
return 202, headers, jsonutils.dumps({'output': 'foo'}) context.status_code = 202
return {'output': 'foo'}
elif action == 'os-getVNCConsole': elif action == 'os-getVNCConsole':
assert list(body[action]) == ['type'] assert list(body[action]) == ['type']
elif action == 'os-getSPICEConsole': elif action == 'os-getSPICEConsole':
@ -480,7 +480,7 @@ class V1(Base):
assert set(keys) == set(['host', 'onSharedStorage']) assert set(keys) == set(['host', 'onSharedStorage'])
else: else:
raise AssertionError("Unexpected server action: %s" % action) raise AssertionError("Unexpected server action: %s" % action)
return resp, headers, jsonutils.dumps({'server': _body}) return {'server': _body}
class V3(Base): class V3(Base):
@ -507,32 +507,30 @@ class V3(Base):
] ]
} }
httpretty.register_uri(httpretty.GET, self.requests.register_uri('GET',
self.url('1234', 'os-attach-interfaces'), self.url('1234', 'os-attach-interfaces'),
body=jsonutils.dumps(get_interfaces), json=get_interfaces,
content_type='application/json') headers=self.json_headers)
attach_body = {'interface_attachment': {}} attach_body = {'interface_attachment': {}}
httpretty.register_uri(httpretty.POST, self.requests.register_uri('POST',
self.url('1234', 'os-attach-interfaces'), self.url('1234', 'os-attach-interfaces'),
body=jsonutils.dumps(attach_body), json=attach_body,
content_type='application/json') headers=self.json_headers)
httpretty.register_uri(httpretty.GET, self.requests.register_uri('GET',
self.url('1234', 'os-server-diagnostics'), self.url('1234', 'os-server-diagnostics'),
body=self.diagnostic, json=self.diagnostic)
status=200)
httpretty.register_uri(httpretty.DELETE, url = self.url('1234', 'os-attach-interfaces', 'port-id')
self.url('1234', 'os-attach-interfaces', self.requests.register_uri('DELETE', url)
'port-id'))
httpretty.register_uri(httpretty.GET, self.requests.register_uri('GET',
self.url(1234, 'os-server-password'), self.url(1234, 'os-server-password'),
jsonutils.dumps({'password': ''})) json={'password': ''})
def post_servers(self, request, url, headers): def post_servers(self, request, context):
body = jsonutils.loads(request.body.decode('utf-8')) body = jsonutils.loads(request.body)
assert set(body.keys()) <= set(['server']) assert set(body.keys()) <= set(['server'])
fakes.assert_has_keys(body['server'], fakes.assert_has_keys(body['server'],
required=['name', 'image_ref', 'flavor_ref'], required=['name', 'image_ref', 'flavor_ref'],
@ -543,10 +541,11 @@ class V3(Base):
else: else:
body = self.server_1234 body = self.server_1234
return 202, headers, jsonutils.dumps({'server': body}) context.status_code = 202
return {'server': body}
def post_servers_1234_action(self, request, url, headers): def post_servers_1234_action(self, request, context):
resp = 202 context.status_code = 202
body_is_none_list = [ body_is_none_list = [
'revert_resize', 'migrate', 'stop', 'start', 'force_delete', 'revert_resize', 'migrate', 'stop', 'start', 'force_delete',
'restore', 'pause', 'unpause', 'lock', 'unlock', 'unrescue', 'restore', 'pause', 'unpause', 'lock', 'unlock', 'unrescue',
@ -577,7 +576,7 @@ class V3(Base):
'detach': ['volume_id'], 'detach': ['volume_id'],
'swap_volume_attachment': ['old_volume_id', 'new_volume_id']} 'swap_volume_attachment': ['old_volume_id', 'new_volume_id']}
body = jsonutils.loads(request.body.decode('utf-8')) body = jsonutils.loads(request.body)
assert len(body.keys()) == 1 assert len(body.keys()) == 1
action = list(body)[0] action = list(body)[0]
_body = body_return_map.get(action, '') _body = body_return_map.get(action, '')
@ -598,13 +597,13 @@ class V3(Base):
assert body[action]['type'] in ['HARD', 'SOFT'] assert body[action]['type'] in ['HARD', 'SOFT']
elif action == 'confirm_resize': elif action == 'confirm_resize':
# This one method returns a different response code # This one method returns a different response code
resp = 204 context.status_code = 204
elif action == 'create_image': elif action == 'create_image':
headers['location'] = "http://blah/images/456" context.headers['location'] = "http://blah/images/456"
if action not in set.union(set(body_is_none_list), if action not in set.union(set(body_is_none_list),
set(body_params_check_exact.keys()), set(body_params_check_exact.keys()),
set(body_param_check_exists.keys())): set(body_param_check_exists.keys())):
raise AssertionError("Unexpected server action: %s" % action) raise AssertionError("Unexpected server action: %s" % action)
return resp, headers, jsonutils.dumps(_body) return _body

View File

@ -14,8 +14,8 @@
import os import os
import fixtures import fixtures
import httpretty
import requests import requests
from requests_mock.contrib import fixture as requests_mock_fixture
import six import six
import testscenarios import testscenarios
import testtools import testtools
@ -52,24 +52,26 @@ class FixturedTestCase(testscenarios.TestWithScenarios, TestCase):
def setUp(self): def setUp(self):
super(FixturedTestCase, self).setUp() super(FixturedTestCase, self).setUp()
httpretty.reset() self.requests = self.useFixture(requests_mock_fixture.Fixture())
self.data_fixture = None self.data_fixture = None
self.client_fixture = None self.client_fixture = None
self.cs = None self.cs = None
if self.client_fixture_class: if self.client_fixture_class:
self.client_fixture = self.useFixture(self.client_fixture_class()) fix = self.client_fixture_class(self.requests)
self.client_fixture = self.useFixture(fix)
self.cs = self.client_fixture.client self.cs = self.client_fixture.client
if self.data_fixture_class: if self.data_fixture_class:
self.data_fixture = self.useFixture(self.data_fixture_class()) fix = self.data_fixture_class(self.requests)
self.data_fixture = self.useFixture(fix)
def assert_called(self, method, path, body=None): def assert_called(self, method, path, body=None):
self.assertEqual(httpretty.last_request().method, method) self.assertEqual(self.requests.last_request.method, method)
self.assertEqual(httpretty.last_request().path, path) self.assertEqual(self.requests.last_request.path_url, path)
if body: if body:
req_data = httpretty.last_request().body req_data = self.requests.last_request.body
if isinstance(req_data, six.binary_type): if isinstance(req_data, six.binary_type):
req_data = req_data.decode('utf-8') req_data = req_data.decode('utf-8')
if not isinstance(body, six.string_types): if not isinstance(body, six.string_types):

View File

@ -13,9 +13,6 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import httpretty
from novaclient.openstack.common import jsonutils
from novaclient.tests.fixture_data import agents as data from novaclient.tests.fixture_data import agents as data
from novaclient.tests.fixture_data import client from novaclient.tests.fixture_data import client
from novaclient.tests import utils from novaclient.tests import utils
@ -53,9 +50,10 @@ class AgentsTest(utils.FixturedTestCase):
] ]
} }
httpretty.register_uri(httpretty.GET, self.data_fixture.url(), headers = {'Content-Type': 'application/json'}
body=jsonutils.dumps(get_os_agents), self.requests.register_uri('GET', self.data_fixture.url(),
content_type='application/json') json=get_os_agents,
headers=headers)
def test_list_agents(self): def test_list_agents(self):
self.stub_hypervisors() self.stub_hypervisors()

View File

@ -12,11 +12,11 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import httpretty
import mock import mock
import six import six
from novaclient import exceptions from novaclient import exceptions
from novaclient.openstack.common import jsonutils
from novaclient.tests.fixture_data import client from novaclient.tests.fixture_data import client
from novaclient.tests.fixture_data import floatingips from novaclient.tests.fixture_data import floatingips
from novaclient.tests.fixture_data import servers as data from novaclient.tests.fixture_data import servers as data
@ -31,7 +31,7 @@ class ServersTest(utils.FixturedTestCase):
def setUp(self): def setUp(self):
super(ServersTest, self).setUp() super(ServersTest, self).setUp()
self.useFixture(floatingips.FloatingFixture()) self.useFixture(floatingips.FloatingFixture(self.requests))
def test_list_servers(self): def test_list_servers(self):
sl = self.cs.servers.list() sl = self.cs.servers.list()
@ -191,7 +191,7 @@ class ServersTest(utils.FixturedTestCase):
self.assertIsInstance(s, servers.Server) self.assertIsInstance(s, servers.Server)
# verify disk config param was used in the request: # verify disk config param was used in the request:
body = httpretty.last_request().parsed_body body = jsonutils.loads(self.requests.last_request.body)
server = body['server'] server = body['server']
self.assertTrue('OS-DCF:diskConfig' in server) self.assertTrue('OS-DCF:diskConfig' in server)
self.assertEqual(disk_config, server['OS-DCF:diskConfig']) self.assertEqual(disk_config, server['OS-DCF:diskConfig'])
@ -280,7 +280,7 @@ class ServersTest(utils.FixturedTestCase):
self.assert_called('POST', '/servers/1234/action') self.assert_called('POST', '/servers/1234/action')
# verify disk config param was used in the request: # verify disk config param was used in the request:
body = httpretty.last_request().parsed_body body = jsonutils.loads(self.requests.last_request.body)
d = body[operation] d = body[operation]
self.assertTrue('OS-DCF:diskConfig' in d) self.assertTrue('OS-DCF:diskConfig' in d)
@ -296,7 +296,7 @@ class ServersTest(utils.FixturedTestCase):
s = self.cs.servers.get(1234) s = self.cs.servers.get(1234)
s.rebuild(image=1, preserve_ephemeral=True) s.rebuild(image=1, preserve_ephemeral=True)
self.assert_called('POST', '/servers/1234/action') self.assert_called('POST', '/servers/1234/action')
body = httpretty.last_request().parsed_body body = jsonutils.loads(self.requests.last_request.body)
d = body['rebuild'] d = body['rebuild']
self.assertIn('preserve_ephemeral', d) self.assertIn('preserve_ephemeral', d)
self.assertEqual(d['preserve_ephemeral'], True) self.assertEqual(d['preserve_ephemeral'], True)
@ -305,7 +305,7 @@ class ServersTest(utils.FixturedTestCase):
files = {'/etc/passwd': 'some data'} files = {'/etc/passwd': 'some data'}
s = self.cs.servers.get(1234) s = self.cs.servers.get(1234)
s.rebuild(image=1, name='new', meta={'foo': 'bar'}, files=files) s.rebuild(image=1, name='new', meta={'foo': 'bar'}, files=files)
body = httpretty.last_request().parsed_body body = jsonutils.loads(self.requests.last_request.body)
d = body['rebuild'] d = body['rebuild']
self.assertEqual('new', d['name']) self.assertEqual('new', d['name'])
self.assertEqual({'foo': 'bar'}, d['metadata']) self.assertEqual({'foo': 'bar'}, d['metadata'])

View File

@ -3,9 +3,9 @@ hacking>=0.9.2,<0.10
coverage>=3.6 coverage>=3.6
discover discover
fixtures>=0.3.14 fixtures>=0.3.14
httpretty>=0.8.0,!=0.8.1,!=0.8.2
keyring>=2.1,!=3.3 keyring>=2.1,!=3.3
mock>=1.0 mock>=1.0
requests-mock>=0.4.0
sphinx>=1.1.2,!=1.2.0,<1.3 sphinx>=1.1.2,!=1.2.0,<1.3
python-keystoneclient>=0.10.0 python-keystoneclient>=0.10.0
oslosphinx oslosphinx