Use requests module for HTTP/HTTPS

* Implement correct certificate verification
* Add --os-cacert
* Rework tests for requests

Pinned requests module to < 1.0 as 1.0.2 is now current in pipi
as of 17Dec2012.

Blueprint: tls-verify

Change-Id: I9a25a94c8dfcaf483c4c8328439809d65cf10b38
This commit is contained in:
Dean Troyer 2012-12-18 14:05:29 -06:00
parent ac2ed549e5
commit aa1df04bad
12 changed files with 440 additions and 346 deletions

View File

@ -9,11 +9,12 @@ OpenStack Client interface. Handles the REST calls and responses.
import logging
import os
import sys
import time
import urlparse
import httplib2
import pkg_resources
import requests
try:
import json
@ -46,41 +47,14 @@ def get_auth_system_url(auth_system):
raise exceptions.AuthSystemNotFound(auth_system)
def _get_proxy_info():
"""Work around httplib2 proxying bug.
Full details of the bug here:
http://code.google.com/p/httplib2/issues/detail?id=228
Basically, in the case of plain old http with httplib2>=0.7.5 we
want to ensure that PROXY_TYPE_HTTP_NO_TUNNEL is used.
"""
def get_proxy_info(method):
pi = httplib2.ProxyInfo.from_environment(method)
if pi is None or method != 'http':
return pi
# We can't rely on httplib2.socks being available
# PROXY_TYPE_HTTP_NO_TUNNEL was introduced in 0.7.5
if not (hasattr(httplib2, 'socks') and
hasattr(httplib2.socks, 'PROXY_TYPE_HTTP_NO_TUNNEL')):
return pi
pi.proxy_type = httplib2.socks.PROXY_TYPE_HTTP_NO_TUNNEL
return pi
# 0.7.3 introduced configuring proxy from the environment
if not hasattr(httplib2.ProxyInfo, 'from_environment'):
return None
return get_proxy_info
class HTTPClient(httplib2.Http):
class HTTPClient(object):
USER_AGENT = 'python-novaclient'
requests_config = {
'danger_mode': False,
}
def __init__(self, user, password, projectid, auth_url=None,
insecure=False, timeout=None, proxy_tenant_id=None,
proxy_token=None, region_name=None,
@ -88,9 +62,8 @@ class HTTPClient(httplib2.Http):
service_name=None, volume_service_name=None,
timings=False, bypass_url=None,
os_cache=False, no_cache=True,
http_log_debug=False, auth_system='keystone'):
super(HTTPClient, self).__init__(timeout=timeout,
proxy_info=_get_proxy_info())
http_log_debug=False, auth_system='keystone',
cacert=None):
self.user = user
self.password = password
self.projectid = projectid
@ -118,9 +91,13 @@ class HTTPClient(httplib2.Http):
self.proxy_tenant_id = proxy_tenant_id
self.used_keyring = False
# httplib2 overrides
self.force_exception_to_status_code = True
self.disable_ssl_certificate_validation = insecure
if insecure:
self.verify_cert = False
else:
if cacert:
self.verify_cert = cacert
else:
self.verify_cert = True
self.auth_system = auth_system
@ -129,6 +106,7 @@ class HTTPClient(httplib2.Http):
ch = logging.StreamHandler()
self._logger.setLevel(logging.DEBUG)
self._logger.addHandler(ch)
self.requests_config['verbose'] = sys.stderr
def use_token_cache(self, use_it):
self.os_cache = use_it
@ -167,40 +145,53 @@ class HTTPClient(httplib2.Http):
string_parts.append(" -d '%s'" % (kwargs['body']))
self._logger.debug("\nREQ: %s\n" % "".join(string_parts))
def http_log_resp(self, resp, body):
def http_log_resp(self, resp):
if not self.http_log_debug:
return
self._logger.debug("RESP:%s %s\n", resp, body)
self._logger.debug(
"RESP: [%s] %s\nRESP BODY: %s\n",
resp.status_code,
resp.headers,
resp.text)
def request(self, *args, **kwargs):
def request(self, url, method, **kwargs):
kwargs.setdefault('headers', kwargs.get('headers', {}))
kwargs['headers']['User-Agent'] = self.USER_AGENT
kwargs['headers']['Accept'] = 'application/json'
if 'body' in kwargs:
kwargs['headers']['Content-Type'] = 'application/json'
kwargs['body'] = json.dumps(kwargs['body'])
kwargs['data'] = json.dumps(kwargs['body'])
del kwargs['body']
self.http_log_req(args, kwargs)
resp, body = super(HTTPClient, self).request(*args, **kwargs)
self.http_log_resp(resp, body)
self.http_log_req((url, method,), kwargs)
resp = requests.request(
method,
url,
verify=self.verify_cert,
config=self.requests_config,
**kwargs)
self.http_log_resp(resp)
if body:
if resp.text:
# TODO(dtroyer): verify the note below in a requests context
# NOTE(alaski): Because force_exceptions_to_status_code=True
# httplib2 returns a connection refused event as a 400 response.
# To determine if it is a bad request or refused connection we need
# to check the body. httplib2 tests check for 'Connection refused'
# or 'actively refused' in the body, so that's what we'll do.
if resp.status == 400:
if 'Connection refused' in body or 'actively refused' in body:
raise exceptions.ConnectionRefused(body)
if resp.status_code == 400:
if ('Connection refused' in resp.text or
'actively refused' in resp.text):
raise exceptions.ConnectionRefused(resp.text)
try:
body = json.loads(body)
body = json.loads(resp.text)
except ValueError:
pass
body = None
else:
body = None
if resp.status >= 400:
if resp.status_code >= 400:
raise exceptions.from_response(resp, body)
return resp, body
@ -254,7 +245,7 @@ class HTTPClient(httplib2.Http):
We may get redirected to another site, fail or actually get
back a service catalog with a token and our endpoints."""
if resp.status == 200: # content must always present
if resp.status_code == 200: # content must always present
try:
self.auth_url = url
self.service_catalog = \
@ -281,8 +272,8 @@ class HTTPClient(httplib2.Http):
print "Could not find any suitable endpoint. Correct region?"
raise
elif resp.status == 305:
return resp['location']
elif resp.status_code == 305:
return resp.headers['location']
else:
raise exceptions.from_response(resp, body)
@ -407,16 +398,16 @@ class HTTPClient(httplib2.Http):
headers['X-Auth-Project-Id'] = self.projectid
resp, body = self._time_request(url, 'GET', headers=headers)
if resp.status in (200, 204): # in some cases we get No Content
if resp.status_code in (200, 204): # in some cases we get No Content
try:
mgmt_header = 'x-server-management-url'
self.management_url = resp[mgmt_header].rstrip('/')
self.auth_token = resp['x-auth-token']
self.management_url = resp.headers[mgmt_header].rstrip('/')
self.auth_token = resp.headers['x-auth-token']
self.auth_url = url
except KeyError:
except (KeyError, TypeError):
raise exceptions.AuthorizationFailure()
elif resp.status == 305:
return resp['location']
elif resp.status_code == 305:
return resp.headers['location']
else:
raise exceptions.from_response(resp, body)
@ -444,13 +435,11 @@ class HTTPClient(httplib2.Http):
token_url = url + "/tokens"
# Make sure we follow redirects when trying to reach Keystone
tmp_follow_all_redirects = self.follow_all_redirects
self.follow_all_redirects = True
try:
resp, body = self._time_request(token_url, "POST", body=body)
finally:
self.follow_all_redirects = tmp_follow_all_redirects
resp, body = self._time_request(
token_url,
"POST",
body=body,
allow_redirects=True)
return self._extract_service_catalog(url, resp, body)

View File

@ -143,16 +143,19 @@ _code_map = dict((c.http_status, c) for c in [BadRequest, Unauthorized,
def from_response(response, body):
"""
Return an instance of an ClientException or subclass
based on an httplib2 response.
based on an requests response.
Usage::
resp, body = http.request(...)
if resp.status != 200:
raise exception_from_response(resp, body)
resp, body = requests.request(...)
if resp.status_code != 200:
raise exception_from_response(resp, rest.text)
"""
cls = _code_map.get(response.status, ClientException)
request_id = response.get('x-compute-request-id')
cls = _code_map.get(response.status_code, ClientException)
if response.headers:
request_id = response.headers.get('x-compute-request-id')
else:
request_id = None
if body:
message = "n/a"
details = "n/a"
@ -160,7 +163,7 @@ def from_response(response, body):
error = body[body.keys()[0]]
message = error.get('message', None)
details = error.get('details', None)
return cls(code=response.status, message=message, details=details,
return cls(code=response.status_code, message=message, details=details,
request_id=request_id)
else:
return cls(code=response.status, request_id=request_id)
return cls(code=response.status_code, request_id=request_id)

View File

@ -20,7 +20,6 @@ Command-line interface to the OpenStack Nova API.
import argparse
import glob
import httplib2
import imp
import itertools
import os
@ -196,6 +195,13 @@ class OpenStackComputeShell(object):
parser.add_argument('--os_compute_api_version',
help=argparse.SUPPRESS)
parser.add_argument('--os-cacert',
metavar='<ca-certificate>',
default=utils.env('OS_CACERT', default=None),
help='Specify a CA bundle file to use in '
'verifying a TLS (https) server certificate. '
'Defaults to env[OS_CACERT]')
parser.add_argument('--insecure',
default=utils.env('NOVACLIENT_INSECURE', default=False),
action='store_true',
@ -349,8 +355,6 @@ class OpenStackComputeShell(object):
logger.setLevel(logging.DEBUG)
logger.addHandler(streamhandler)
httplib2.debuglevel = 1
def main(self, argv):
# Parse args once to find version and debug settings
parser = self.get_base_parser()
@ -393,7 +397,7 @@ class OpenStackComputeShell(object):
os_region_name, os_auth_system, endpoint_type, insecure,
service_type, service_name, volume_service_name,
username, apikey, projectid, url, region_name,
bypass_url, os_cache) = (
bypass_url, os_cache, cacert) = (
args.os_username, args.os_password,
args.os_tenant_name, args.os_auth_url,
args.os_region_name, args.os_auth_system,
@ -401,7 +405,8 @@ class OpenStackComputeShell(object):
args.service_name, args.volume_service_name,
args.username, args.apikey, args.projectid,
args.url, args.region_name,
args.bypass_url, args.os_cache)
args.bypass_url, args.os_cache,
args.os_cacert)
if not endpoint_type:
endpoint_type = DEFAULT_NOVA_ENDPOINT_TYPE
@ -472,7 +477,8 @@ class OpenStackComputeShell(object):
service_name=service_name, auth_system=os_auth_system,
volume_service_name=volume_service_name,
timings=args.timings, bypass_url=bypass_url,
os_cache=os_cache, http_log_debug=options.debug)
os_cache=os_cache, http_log_debug=options.debug,
cacert=cacert)
try:
if not utils.isunauthenticated(args.func):

View File

@ -56,7 +56,8 @@ class Client(object):
service_type='compute', service_name=None,
volume_service_name=None, timings=False,
bypass_url=None, os_cache=False, no_cache=True,
http_log_debug=False, auth_system='keystone'):
http_log_debug=False, auth_system='keystone',
cacert=None):
# FIXME(comstud): Rename the api_key argument above when we
# know it's not being used as keyword argument
password = api_key
@ -122,7 +123,8 @@ class Client(object):
timings=timings,
bypass_url=bypass_url,
os_cache=self.os_cache,
http_log_debug=http_log_debug)
http_log_debug=http_log_debug,
cacert=cacert)
def set_management_url(self, url):
self.client.set_management_url(url)

View File

@ -611,7 +611,8 @@ class ServerManager(local_base.BootingManagerWithFind):
:param meta: Metadata to give newly-created image entity
"""
body = {'name': image_name, 'metadata': metadata or {}}
location = self._action('createImage', server, body)[0]['location']
resp = self._action('createImage', server, body)[0]
location = resp.headers['location']
image_uuid = location.split('/')[-1]
return image_uuid

View File

@ -13,9 +13,9 @@
# License for the specific language governing permissions and limitations
# under the License.
import httplib2
import mock
import pkg_resources
import requests
try:
import json
@ -52,12 +52,11 @@ def mock_http_request(resp=None):
},
}
auth_response = httplib2.Response({
"status": 200,
"body": json.dumps(resp),
auth_response = utils.TestResponse({
"status_code": 200,
"text": json.dumps(resp),
})
return mock.Mock(return_value=(auth_response,
json.dumps(resp)))
return mock.Mock(return_value=(auth_response))
def requested_headers(cs):
@ -86,7 +85,7 @@ class AuthPluginTest(utils.TestCase):
@mock.patch.object(pkg_resources, "iter_entry_points",
mock_iter_entry_points)
@mock.patch.object(httplib2.Http, "request", mock_request)
@mock.patch.object(requests, "request", mock_request)
def test_auth_call():
cs = client.Client("username", "password", "project_id",
"auth_url/v2.0", auth_system="fake")
@ -95,9 +94,13 @@ class AuthPluginTest(utils.TestCase):
headers = requested_headers(cs)
token_url = cs.client.auth_url + "/tokens"
mock_request.assert_called_with(token_url, "POST",
headers=headers,
body='{"fake": "me"}')
mock_request.assert_called_with(
"POST",
token_url,
headers=headers,
data='{"fake": "me"}',
allow_redirects=True,
**self.TEST_REQUEST_BASE)
test_auth_call()
@ -109,7 +112,7 @@ class AuthPluginTest(utils.TestCase):
@mock.patch.object(pkg_resources, "iter_entry_points",
mock_iter_entry_points)
@mock.patch.object(httplib2.Http, "request", mock_request)
@mock.patch.object(requests, "request", mock_request)
def test_auth_call():
cs = client.Client("username", "password", "project_id",
"auth_url/v2.0", auth_system="notexists")
@ -146,7 +149,7 @@ class AuthPluginTest(utils.TestCase):
@mock.patch.object(pkg_resources, "iter_entry_points",
mock_iter_entry_points)
@mock.patch.object(httplib2.Http, "request", mock_request)
@mock.patch.object(requests, "request", mock_request)
def test_auth_call():
cs = client.Client("username", "password", "project_id",
auth_system="fakewithauthurl")

View File

@ -1,32 +1,28 @@
import httplib2
import mock
import requests
from novaclient import client
from novaclient import exceptions
from tests import utils
fake_response = httplib2.Response({"status": 200})
fake_body = '{"hi": "there"}'
mock_request = mock.Mock(return_value=(fake_response, fake_body))
fake_response = utils.TestResponse({
"status_code": 200,
"text": '{"hi": "there"}',
})
mock_request = mock.Mock(return_value=(fake_response))
refused_response = httplib2.Response({"status": 400})
refused_body = '[Errno 111] Connection refused'
refused_mock_request = mock.Mock(
return_value=(
refused_response,
refused_body,
)
)
refused_response = utils.TestResponse({
"status_code": 400,
"text": '[Errno 111] Connection refused',
})
refused_mock_request = mock.Mock(return_value=(refused_response))
bad_req_response = httplib2.Response({"status": 400})
bad_req_body = ''
bad_req_mock_request = mock.Mock(
return_value=(
bad_req_response,
bad_req_body,
)
)
bad_req_response = utils.TestResponse({
"status_code": 400,
"text": '',
})
bad_req_mock_request = mock.Mock(return_value=(bad_req_response))
def get_client():
@ -47,7 +43,7 @@ class ClientTest(utils.TestCase):
def test_get(self):
cl = get_authed_client()
@mock.patch.object(httplib2.Http, "request", mock_request)
@mock.patch.object(requests, "request", mock_request)
@mock.patch('time.time', mock.Mock(return_value=1234))
def test_get_call():
resp, body = cl.get("/hi")
@ -56,8 +52,11 @@ class ClientTest(utils.TestCase):
"User-Agent": cl.USER_AGENT,
'Accept': 'application/json',
}
mock_request.assert_called_with("http://example.com/hi",
"GET", headers=headers)
mock_request.assert_called_with(
"GET",
"http://example.com/hi",
headers=headers,
**self.TEST_REQUEST_BASE)
# Automatic JSON parsing
self.assertEqual(body, {"hi": "there"})
@ -66,7 +65,7 @@ class ClientTest(utils.TestCase):
def test_post(self):
cl = get_authed_client()
@mock.patch.object(httplib2.Http, "request", mock_request)
@mock.patch.object(requests, "request", mock_request)
def test_post_call():
cl.post("/hi", body=[1, 2, 3])
headers = {
@ -76,8 +75,12 @@ class ClientTest(utils.TestCase):
'Accept': 'application/json',
"User-Agent": cl.USER_AGENT
}
mock_request.assert_called_with("http://example.com/hi", "POST",
headers=headers, body='[1, 2, 3]')
mock_request.assert_called_with(
"POST",
"http://example.com/hi",
headers=headers,
data='[1, 2, 3]',
**self.TEST_REQUEST_BASE)
test_post_call()
@ -85,7 +88,7 @@ class ClientTest(utils.TestCase):
cl = get_client()
# response must not have x-server-management-url header
@mock.patch.object(httplib2.Http, "request", mock_request)
@mock.patch.object(requests, "request", mock_request)
def test_auth_call():
self.assertRaises(exceptions.AuthorizationFailure, cl.authenticate)
@ -94,7 +97,7 @@ class ClientTest(utils.TestCase):
def test_connection_refused(self):
cl = get_client()
@mock.patch.object(httplib2.Http, "request", refused_mock_request)
@mock.patch.object(requests, "request", refused_mock_request)
def test_refused_call():
self.assertRaises(exceptions.ConnectionRefused, cl.get, "/hi")
@ -103,7 +106,7 @@ class ClientTest(utils.TestCase):
def test_bad_request(self):
cl = get_client()
@mock.patch.object(httplib2.Http, "request", bad_req_mock_request)
@mock.patch.object(requests, "request", bad_req_mock_request)
def test_refused_call():
self.assertRaises(exceptions.BadRequest, cl.get, "/hi")

View File

@ -1,6 +1,5 @@
import cStringIO
import os
import httplib2
import sys
from novaclient import exceptions
@ -44,11 +43,6 @@ class ShellTest(utils.TestCase):
def test_help_unknown_command(self):
self.assertRaises(exceptions.CommandError, self.shell, 'help foofoo')
def test_debug(self):
httplib2.debuglevel = 0
self.shell('--debug help')
assert httplib2.debuglevel == 1
def test_help(self):
required = [
'^usage: ',

View File

@ -1,5 +1,34 @@
import requests
import unittest2
class TestCase(unittest2.TestCase):
pass
TEST_REQUEST_BASE = {
'config': {'danger_mode': False},
'verify': True,
}
class TestResponse(requests.Response):
"""
Class used to wrap requests.Response and provide some
convenience to initialize with a dict
"""
def __init__(self, data):
self._text = None
super(TestResponse, self)
if isinstance(data, dict):
self.status_code = data.get('status_code', None)
self.headers = data.get('headers', None)
# Fake the text attribute to streamline Response creation
self._text = data.get('text', None)
else:
self.status_code = data
def __eq__(self, other):
return self.__dict__ == other.__dict__
@property
def text(self):
return self._text

View File

@ -14,12 +14,14 @@
# limitations under the License.
from datetime import datetime
import httplib2
import urlparse
import requests
from novaclient import client as base_client
from novaclient.v1_1 import client
from tests import fakes
from tests import utils
class FakeClient(fakes.FakeClient, client.Client):
@ -63,11 +65,17 @@ class FakeHTTPClient(base_client.HTTPClient):
# Note the call
self.callstack.append((method, url, kwargs.get('body', None)))
status, body = getattr(self, callback)(**kwargs)
if hasattr(status, 'items'):
return httplib2.Response(status), body
if 'body' in kwargs:
b = kwargs['body']
else:
return httplib2.Response({"status": status}), body
b = ''
status, headers, body = getattr(self, callback)(**kwargs)
r = utils.TestResponse({
"status_code": status,
"text": body,
"headers": headers,
})
return r, body
#
# agents
@ -75,7 +83,7 @@ class FakeHTTPClient(base_client.HTTPClient):
def get_os_agents(self, **kw):
hypervisor = kw.get('hypervisor', 'kvm')
return (200, {'agents':
return (200, {}, {'agents':
[{'hypervisor': hypervisor,
'os': 'win',
'architecture': 'x86',
@ -93,7 +101,7 @@ class FakeHTTPClient(base_client.HTTPClient):
]})
def post_os_agents(self, body):
return (200, {'agent': {
return (200, {}, {'agent': {
'url': '/xxx/xxx/xxx',
'hypervisor': body['agent']['hypervisor'],
'md5hash': 'add6bb58e139be103324d04d82d8f546',
@ -103,10 +111,10 @@ class FakeHTTPClient(base_client.HTTPClient):
'id': 1}})
def delete_os_agents_1(self, **kw):
return (202, None)
return (202, {}, None)
def put_os_agents_1(self, body, **kw):
return (200, {"agent": {
return (200, {}, {"agent": {
"url": "/yyy/yyyy/yyyy",
"version": "8.0",
"md5hash": "add6bb58e139be103324d04d82d8f546",
@ -155,7 +163,7 @@ class FakeHTTPClient(base_client.HTTPClient):
"updated": "2011-11-03T00:00:00+00:00"
},
]
return (200, {
return (200, {}, {
"extensions": exts,
})
@ -164,7 +172,7 @@ class FakeHTTPClient(base_client.HTTPClient):
#
def get_limits(self, **kw):
return (200, {"limits": {
return (200, {}, {"limits": {
"rate": [
{
"uri": "*",
@ -222,13 +230,13 @@ class FakeHTTPClient(base_client.HTTPClient):
#
def get_servers(self, **kw):
return (200, {"servers": [
return (200, {}, {"servers": [
{'id': 1234, 'name': 'sample-server'},
{'id': 5678, 'name': 'sample-server2'}
]})
def get_servers_detail(self, **kw):
return (200, {"servers": [
return (200, {}, {"servers": [
{
"id": 1234,
"name": "sample-server",
@ -331,52 +339,52 @@ class FakeHTTPClient(base_client.HTTPClient):
if 'personality' in body['server']:
for pfile in body['server']['personality']:
fakes.assert_has_keys(pfile, required=['path', 'contents'])
return (202, self.get_servers_1234()[1])
return (202, {}, self.get_servers_1234()[2])
def post_os_volumes_boot(self, body, **kw):
assert set(body.keys()) <= set(['server', 'os:scheduler_hints'])
fakes.assert_has_keys(body['server'],
required=['name', 'block_device_mapping', 'flavorRef'],
optional=['imageRef'])
return (202, self.get_servers_9012()[1])
return (202, {}, self.get_servers_9012()[2])
def get_servers_1234(self, **kw):
r = {'server': self.get_servers_detail()[1]['servers'][0]}
return (200, r)
r = {'server': self.get_servers_detail()[2]['servers'][0]}
return (200, {}, r)
def get_servers_5678(self, **kw):
r = {'server': self.get_servers_detail()[1]['servers'][1]}
return (200, r)
r = {'server': self.get_servers_detail()[2]['servers'][1]}
return (200, {}, r)
def get_servers_9012(self, **kw):
r = {'server': self.get_servers_detail()[1]['servers'][2]}
return (200, r)
r = {'server': self.get_servers_detail()[2]['servers'][2]}
return (200, {}, r)
def put_servers_1234(self, body, **kw):
assert body.keys() == ['server']
fakes.assert_has_keys(body['server'], optional=['name', 'adminPass'])
return (204, None)
return (204, {}, None)
def delete_servers_1234(self, **kw):
return (202, None)
return (202, {}, None)
def delete_servers_1234_metadata_test_key(self, **kw):
return (204, None)
return (204, {}, None)
def delete_servers_1234_metadata_key1(self, **kw):
return (204, None)
return (204, {}, None)
def delete_servers_1234_metadata_key2(self, **kw):
return (204, None)
return (204, {}, None)
def post_servers_1234_metadata(self, **kw):
return (204, {'metadata': {'test_key': 'test_value'}})
return (204, {}, {'metadata': {'test_key': 'test_value'}})
def get_servers_1234_diagnostics(self, **kw):
return (200, {'data': 'Fake diagnostics'})
return (200, {}, {'data': 'Fake diagnostics'})
def get_servers_1234_actions(self, **kw):
return (200, {'actions': [
return (200, {}, {'actions': [
{
'action': 'rebuild',
'error': None,
@ -394,25 +402,26 @@ class FakeHTTPClient(base_client.HTTPClient):
#
def get_servers_1234_ips(self, **kw):
return (200, {'addresses':
return (200, {}, {'addresses':
self.get_servers_1234()[1]['server']['addresses']})
def get_servers_1234_ips_public(self, **kw):
return (200, {'public':
return (200, {}, {'public':
self.get_servers_1234_ips()[1]['addresses']['public']})
def get_servers_1234_ips_private(self, **kw):
return (200, {'private':
return (200, {}, {'private':
self.get_servers_1234_ips()[1]['addresses']['private']})
def delete_servers_1234_ips_public_1_2_3_4(self, **kw):
return (202, None)
return (202, {}, None)
#
# Server actions
#
def post_servers_1234_action(self, body, **kw):
_headers = None
_body = None
resp = 202
assert len(body.keys()) == 1
@ -425,13 +434,13 @@ class FakeHTTPClient(base_client.HTTPClient):
if 'adminPass' in keys:
keys.remove('adminPass')
assert keys == ['imageRef']
_body = self.get_servers_1234()[1]
_body = self.get_servers_1234()[2]
elif action == 'resize':
assert body[action].keys() == ['flavorRef']
elif action == 'confirmResize':
assert body[action] is None
# This one method returns a different response code
return (204, None)
return (204, {}, None)
elif action == 'revertResize':
assert body[action] is None
elif action == 'migrate':
@ -458,12 +467,12 @@ class FakeHTTPClient(base_client.HTTPClient):
assert body[action].keys() == ['address']
elif action == 'createImage':
assert set(body[action].keys()) == set(['name', 'metadata'])
resp = dict(status=202, location="http://blah/images/456")
_headers = dict(location="http://blah/images/456")
elif action == 'changePassword':
assert body[action].keys() == ['adminPass']
elif action == 'os-getConsoleOutput':
assert body[action].keys() == ['length']
return (202, {'output': 'foo'})
return (202, {}, {'output': 'foo'})
elif action == 'os-getVNCConsole':
assert body[action].keys() == ['type']
elif action == 'os-migrateLive':
@ -482,36 +491,42 @@ class FakeHTTPClient(base_client.HTTPClient):
'rotation'])
else:
raise AssertionError("Unexpected server action: %s" % action)
return (resp, _body)
return (resp, _headers, _body)
#
# Cloudpipe
#
def get_os_cloudpipe(self, **kw):
return (200, {'cloudpipes': [
{'project_id':1}
]})
return (
200,
{},
{'cloudpipes': [{'project_id':1}]}
)
def post_os_cloudpipe(self, **ks):
return (202, {'instance_id': '9d5824aa-20e6-4b9f-b967-76a699fc51fd'})
return (
202,
{},
{'instance_id': '9d5824aa-20e6-4b9f-b967-76a699fc51fd'}
)
def put_os_cloudpipe_configure_project(self, **kw):
return (202, None)
return (202, {}, None)
#
# Flavors
#
def get_flavors(self, **kw):
return (200, {'flavors': [
return (200, {}, {'flavors': [
{'id': 1, 'name': '256 MB Server'},
{'id': 2, 'name': '512 MB Server'},
{'id': 'aa1', 'name': '128 MB Server'}
]})
def get_flavors_detail(self, **kw):
return (200, {'flavors': [
return (200, {}, {'flavors': [
{'id': 1, 'name': '256 MB Server', 'ram': 256, 'disk': 10,
'OS-FLV-EXT-DATA:ephemeral': 10,
'os-flavor-access:is_public': True,
@ -527,39 +542,65 @@ class FakeHTTPClient(base_client.HTTPClient):
]})
def get_flavors_1(self, **kw):
return (200, {'flavor': self.get_flavors_detail()[1]['flavors'][0]})
return (
200,
{},
{'flavor': self.get_flavors_detail()[2]['flavors'][0]}
)
def get_flavors_2(self, **kw):
return (200, {'flavor': self.get_flavors_detail()[1]['flavors'][1]})
return (
200,
{},
{'flavor': self.get_flavors_detail()[2]['flavors'][1]}
)
def get_flavors_3(self, **kw):
# Diablo has no ephemeral
return (200, {'flavor': {'id': 3, 'name': '256 MB Server',
'ram': 256, 'disk': 10}})
return (
200,
{},
{'flavor': {
'id': 3,
'name': '256 MB Server',
'ram': 256,
'disk': 10,
}},
)
def get_flavors_aa1(self, **kw):
# Aplhanumeric flavor id are allowed.
return (200, {'flavor': self.get_flavors_detail()[1]['flavors'][2]})
return (
200,
{},
{'flavor': self.get_flavors_detail()[2]['flavors'][2]}
)
def delete_flavors_flavordelete(self, **kw):
return (202, None)
return (202, {}, None)
def delete_flavors_2(self, **kw):
return (202, None)
return (202, {}, None)
def post_flavors(self, body, **kw):
return (202, {'flavor': self.get_flavors_detail()[1]['flavors'][0]})
return (
202,
{},
{'flavor': self.get_flavors_detail()[2]['flavors'][0]}
)
def get_flavors_1_os_extra_specs(self, **kw):
return (200,
{},
{'extra_specs': {"k1": "v1"}})
def get_flavors_2_os_extra_specs(self, **kw):
return (200,
{},
{'extra_specs': {"k2": "v2"}})
def get_flavors_aa1_os_extra_specs(self, **kw):
return (200,
return (200, {},
{'extra_specs': {"k3": "v3"}})
def post_flavors_1_os_extra_specs(self, body, **kw):
@ -567,69 +608,78 @@ class FakeHTTPClient(base_client.HTTPClient):
fakes.assert_has_keys(body['extra_specs'],
required=['k1'])
return (200,
{},
{'extra_specs': {"k1": "v1"}})
def delete_flavors_1_os_extra_specs_k1(self, **kw):
return (204, None)
return (204, {}, None)
#
# Flavor access
#
def get_flavors_1_os_flavor_access(self, **kw):
return (404, None)
return (404, {}, None)
def get_flavors_2_os_flavor_access(self, **kw):
return (200, {'flavor_access': [
return (200, {}, {'flavor_access': [
{'flavor_id': '2', 'tenant_id': 'proj1'},
{'flavor_id': '2', 'tenant_id': 'proj2'}
]})
def post_flavors_2_action(self, body, **kw):
return (202, self.get_flavors_2_os_flavor_access()[1])
return (202, {}, self.get_flavors_2_os_flavor_access()[2])
#
# Floating ips
#
def get_os_floating_ip_pools(self):
return (200, {'floating_ip_pools': [{'name': 'foo', 'name': 'bar'}]})
return (
200,
{},
{'floating_ip_pools': [{'name': 'foo', 'name': 'bar'}]}
)
def get_os_floating_ips(self, **kw):
return (200, {'floating_ips': [
{'id': 1, 'fixed_ip': '10.0.0.1', 'ip': '11.0.0.1'},
{'id': 2, 'fixed_ip': '10.0.0.2', 'ip': '11.0.0.2'},
]})
return (
200,
{},
{'floating_ips': [
{'id': 1, 'fixed_ip': '10.0.0.1', 'ip': '11.0.0.1'},
{'id': 2, 'fixed_ip': '10.0.0.2', 'ip': '11.0.0.2'},
]},
)
def get_os_floating_ips_1(self, **kw):
return (200, {'floating_ip':
return (200, {}, {'floating_ip':
{'id': 1, 'fixed_ip': '10.0.0.1', 'ip': '11.0.0.1'}
})
def post_os_floating_ips(self, body, **kw):
return (202, self.get_os_floating_ips_1()[1])
return (202, {}, self.get_os_floating_ips_1()[1])
def post_os_floating_ips(self, body):
if body.get('pool'):
return (200, {'floating_ip':
return (200, {}, {'floating_ip':
{'id': 1, 'fixed_ip': '10.0.0.1', 'ip': '11.0.0.1',
'pool': 'nova'}})
else:
return (200, {'floating_ip':
return (200, {}, {'floating_ip':
{'id': 1, 'fixed_ip': '10.0.0.1', 'ip': '11.0.0.1',
'pool': None}})
def delete_os_floating_ips_1(self, **kw):
return (204, None)
return (204, {}, None)
def get_os_floating_ip_dns(self, **kw):
return (205, {'domain_entries':
[{'domain': 'example.org'},
{'domain': 'example.com'}]})
return (205, {}, {'domain_entries':
[{'domain': 'example.org'},
{'domain': 'example.com'}]})
def get_os_floating_ip_dns_testdomain_entries(self, **kw):
if kw.get('ip'):
return (205, {'dns_entries':
return (205, {}, {'dns_entries':
[{'dns_entry':
{'ip': kw.get('ip'),
'name': "host1",
@ -641,10 +691,10 @@ class FakeHTTPClient(base_client.HTTPClient):
'type': "A",
'domain': 'testdomain'}}]})
else:
return (404, None)
return (404, {}, None)
def get_os_floating_ip_dns_testdomain_entries_testname(self, **kw):
return (205, {'dns_entry':
return (205, {}, {'dns_entry':
{'ip': "10.10.10.10",
'name': 'testname',
'type': "A",
@ -661,27 +711,27 @@ class FakeHTTPClient(base_client.HTTPClient):
else:
fakes.assert_has_keys(body['domain_entry'],
required=['project', 'scope'])
return (205, None)
return (205, {}, None)
def put_os_floating_ip_dns_testdomain_entries_testname(self, body, **kw):
fakes.assert_has_keys(body['dns_entry'],
required=['ip', 'dns_type'])
return (205, None)
return (205, {}, None)
def delete_os_floating_ip_dns_testdomain(self, **kw):
return (200, None)
return (200, {}, None)
def delete_os_floating_ip_dns_testdomain_entries_testname(self, **kw):
return (200, None)
return (200, {}, None)
def get_os_floating_ips_bulk(self, **kw):
return (200, {'floating_ip_info': [
return (200, {}, {'floating_ip_info': [
{'id': 1, 'fixed_ip': '10.0.0.1', 'ip': '11.0.0.1'},
{'id': 2, 'fixed_ip': '10.0.0.2', 'ip': '11.0.0.2'},
]})
def get_os_floating_ips_bulk_testHost(self, **kw):
return (200, {'floating_ip_info': [
return (200, {}, {'floating_ip_info': [
{'id': 1, 'fixed_ip': '10.0.0.1', 'ip': '11.0.0.1'},
{'id': 2, 'fixed_ip': '10.0.0.2', 'ip': '11.0.0.2'},
]})
@ -690,26 +740,26 @@ class FakeHTTPClient(base_client.HTTPClient):
params = kw.get('body').get('floating_ips_bulk_create')
pool = params.get('pool', 'defaultPool')
interface = params.get('interface', 'defaultInterface')
return (200, {'floating_ips_bulk_create':
return (200, {}, {'floating_ips_bulk_create':
{'ip_range': '192.168.1.0/30',
'pool': pool,
'interface': interface}})
def put_os_floating_ips_bulk_delete(self, **kw):
ip_range = kw.get('body').get('ip_range')
return (200, {'floating_ips_bulk_delete': ip_range})
return (200, {}, {'floating_ips_bulk_delete': ip_range})
#
# Images
#
def get_images(self, **kw):
return (200, {'images': [
return (200, {}, {'images': [
{'id': 1, 'name': 'CentOS 5.2'},
{'id': 2, 'name': 'My Server Backup'}
]})
def get_images_detail(self, **kw):
return (200, {'images': [
return (200, {}, {'images': [
{
'id': 1,
'name': 'CentOS 5.2',
@ -734,52 +784,53 @@ class FakeHTTPClient(base_client.HTTPClient):
]})
def get_images_1(self, **kw):
return (200, {'image': self.get_images_detail()[1]['images'][0]})
return (200, {}, {'image': self.get_images_detail()[2]['images'][0]})
def get_images_2(self, **kw):
return (200, {'image': self.get_images_detail()[1]['images'][1]})
return (200, {}, {'image': self.get_images_detail()[2]['images'][1]})
def post_images(self, body, **kw):
assert body.keys() == ['image']
fakes.assert_has_keys(body['image'], required=['serverId', 'name'])
return (202, self.get_images_1()[1])
return (202, {}, self.get_images_1()[2])
def post_images_1_metadata(self, body, **kw):
assert body.keys() == ['metadata']
fakes.assert_has_keys(body['metadata'],
required=['test_key'])
return (200,
{'metadata': self.get_images_1()[1]['image']['metadata']})
{},
{'metadata': self.get_images_1()[2]['image']['metadata']})
def delete_images_1(self, **kw):
return (204, None)
return (204, {}, None)
def delete_images_1_metadata_test_key(self, **kw):
return (204, None)
return (204, {}, None)
#
# Keypairs
#
def get_os_keypairs(self, *kw):
return (200, {"keypairs": [
return (200, {}, {"keypairs": [
{'fingerprint': 'FAKE_KEYPAIR', 'name': 'test'}
]})
def delete_os_keypairs_test(self, **kw):
return (202, None)
return (202, {}, None)
def post_os_keypairs(self, body, **kw):
assert body.keys() == ['keypair']
fakes.assert_has_keys(body['keypair'],
required=['name'])
r = {'keypair': self.get_os_keypairs()[1]['keypairs'][0]}
return (202, r)
r = {'keypair': self.get_os_keypairs()[2]['keypairs'][0]}
return (202, {}, r)
#
# Virtual Interfaces
#
def get_servers_1234_os_virtual_interfaces(self, **kw):
return (200, {"virtual_interfaces": [
return (200, {}, {"virtual_interfaces": [
{'id': 'fakeid', 'mac_address': 'fakemac'}
]})
@ -788,7 +839,7 @@ class FakeHTTPClient(base_client.HTTPClient):
#
def get_os_quota_sets_test(self, **kw):
return (200, {'quota_set': {
return (200, {}, {'quota_set': {
'tenant_id': 'test',
'metadata_items': [],
'injected_file_content_bytes': 1,
@ -805,7 +856,7 @@ class FakeHTTPClient(base_client.HTTPClient):
'security_group_rules': 1}})
def get_os_quota_sets_test_defaults(self):
return (200, {'quota_set': {
return (200, {}, {'quota_set': {
'tenant_id': 'test',
'metadata_items': [],
'injected_file_content_bytes': 1,
@ -825,7 +876,7 @@ class FakeHTTPClient(base_client.HTTPClient):
assert body.keys() == ['quota_set']
fakes.assert_has_keys(body['quota_set'],
required=['tenant_id'])
return (200, {'quota_set': {
return (200, {}, {'quota_set': {
'tenant_id': 'test',
'metadata_items': [],
'injected_file_content_bytes': 1,
@ -846,7 +897,7 @@ class FakeHTTPClient(base_client.HTTPClient):
#
def get_os_quota_class_sets_test(self, **kw):
return (200, {'quota_class_set': {
return (200, {}, {'quota_class_set': {
'class_name': 'test',
'metadata_items': [],
'injected_file_content_bytes': 1,
@ -866,7 +917,7 @@ class FakeHTTPClient(base_client.HTTPClient):
assert body.keys() == ['quota_class_set']
fakes.assert_has_keys(body['quota_class_set'],
required=['class_name'])
return (200, {'quota_class_set': {
return (200, {}, {'quota_class_set': {
'class_name': 'test',
'metadata_items': [],
'injected_file_content_bytes': 1,
@ -886,39 +937,39 @@ class FakeHTTPClient(base_client.HTTPClient):
# Security Groups
#
def get_os_security_groups(self, **kw):
return (200, {"security_groups": [
return (200, {}, {"security_groups": [
{'id': 1, 'name': 'test', 'description': 'FAKE_SECURITY_GROUP',
'tenant_id': '4ffc664c198e435e9853f2538fbcd7a7'}
]})
def get_os_security_groups_1(self, **kw):
return (200, {"security_group":
return (200, {}, {"security_group":
{'id': 1, 'name': 'test', 'description': 'FAKE_SECURITY_GROUP'}
})
def delete_os_security_groups_1(self, **kw):
return (202, None)
return (202, {}, None)
def post_os_security_groups(self, body, **kw):
assert body.keys() == ['security_group']
fakes.assert_has_keys(body['security_group'],
required=['name', 'description'])
r = {'security_group':
self.get_os_security_groups()[1]['security_groups'][0]}
return (202, r)
self.get_os_security_groups()[2]['security_groups'][0]}
return (202, {}, r)
#
# Security Group Rules
#
def get_os_security_group_rules(self, **kw):
return (200, {"security_group_rules": [
return (200, {}, {"security_group_rules": [
{'id': 1, 'parent_group_id': 1, 'group_id': 2,
'ip_protocol': 'TCP', 'from_port': '22', 'to_port': 22,
'cidr': '10.0.0.0/8'}
]})
def delete_os_security_group_rules_1(self, **kw):
return (202, None)
return (202, {}, None)
def post_os_security_group_rules(self, body, **kw):
assert body.keys() == ['security_group_rule']
@ -927,14 +978,14 @@ class FakeHTTPClient(base_client.HTTPClient):
optional=['group_id', 'ip_protocol', 'from_port',
'to_port', 'cidr'])
r = {'security_group_rule':
self.get_os_security_group_rules()[1]['security_group_rules'][0]}
return (202, r)
self.get_os_security_group_rules()[2]['security_group_rules'][0]}
return (202, {}, r)
#
# Tenant Usage
#
def get_os_simple_tenant_usage(self, **kw):
return (200, {u'tenant_usages': [{
return (200, {}, {u'tenant_usages': [{
u'total_memory_mb_usage': 25451.762807466665,
u'total_vcpus_usage': 49.71047423333333,
u'total_hours': 49.71047423333333,
@ -952,7 +1003,7 @@ class FakeHTTPClient(base_client.HTTPClient):
u'total_local_gb_usage': 0.0}]})
def get_os_simple_tenant_usage_tenantfoo(self, **kw):
return (200, {u'tenant_usage': {
return (200, {}, {u'tenant_usage': {
u'total_memory_mb_usage': 25451.762807466665,
u'total_vcpus_usage': 49.71047423333333,
u'total_hours': 49.71047423333333,
@ -973,16 +1024,24 @@ class FakeHTTPClient(base_client.HTTPClient):
# Certificates
#
def get_os_certificates_root(self, **kw):
return (200, {'certificate': {'private_key': None, 'data': 'foo'}})
return (
200,
{},
{'certificate': {'private_key': None, 'data': 'foo'}}
)
def post_os_certificates(self, **kw):
return (200, {'certificate': {'private_key': 'foo', 'data': 'bar'}})
return (
200,
{},
{'certificate': {'private_key': 'foo', 'data': 'bar'}}
)
#
# Aggregates
#
def get_os_aggregates(self, *kw):
return (200, {"aggregates": [
return (200, {}, {"aggregates": [
{'id':'1',
'name': 'test',
'availability_zone': 'nova1'},
@ -992,8 +1051,8 @@ class FakeHTTPClient(base_client.HTTPClient):
]})
def _return_aggregate(self):
r = {'aggregate': self.get_os_aggregates()[1]['aggregates'][0]}
return (200, r)
r = {'aggregate': self.get_os_aggregates()[2]['aggregates'][0]}
return (200, {}, r)
def get_os_aggregates_1(self, **kw):
return self._return_aggregate()
@ -1014,7 +1073,7 @@ class FakeHTTPClient(base_client.HTTPClient):
return self._return_aggregate()
def delete_os_aggregates_1(self, **kw):
return (202, None)
return (202, {}, None)
#
# Services
@ -1022,7 +1081,7 @@ class FakeHTTPClient(base_client.HTTPClient):
def get_os_services(self, **kw):
host = kw.get('host', 'host1')
service = kw.get('service', 'nova-compute')
return (200, {'services':
return (200, {}, {'services':
[{'binary': service,
'host': host,
'zone': 'nova',
@ -1038,31 +1097,31 @@ class FakeHTTPClient(base_client.HTTPClient):
]})
def put_os_services_enable(self, body, **kw):
return (200, {'host': body['host'], 'service': body['service'],
return (200, {}, {'host': body['host'], 'service': body['service'],
'disabled': False})
def put_os_services_disable(self, body, **kw):
return (200, {'host': body['host'], 'service': body['service'],
return (200, {}, {'host': body['host'], 'service': body['service'],
'disabled': True})
#
# Fixed IPs
#
def get_os_fixed_ips_192_168_1_1(self, *kw):
return (200, {"fixed_ip":
return (200, {}, {"fixed_ip":
{'cidr': '192.168.1.0/24',
'address': '192.168.1.1',
'hostname': 'foo',
'host': 'bar'}})
def post_os_fixed_ips_192_168_1_1_action(self, body, **kw):
return (202, None)
return (202, {}, None)
#
# Hosts
#
def get_os_hosts_host(self, *kw):
return (200, {'host':
return (200, {}, {'host':
[{'resource': {'project': '(total)', 'host': 'dummy',
'cpu': 16, 'memory_mb': 32234, 'disk_gb': 128}},
{'resource': {'project': '(used_now)', 'host': 'dummy',
@ -1074,7 +1133,7 @@ class FakeHTTPClient(base_client.HTTPClient):
def get_os_hosts(self, **kw):
zone = kw.get('zone', 'nova1')
return (200, {'hosts':
return (200, {}, {'hosts':
[{'host': 'host1',
'service': 'nova-compute',
'zone': zone},
@ -1083,46 +1142,46 @@ class FakeHTTPClient(base_client.HTTPClient):
'zone': zone}]})
def get_os_hosts_sample_host(self, *kw):
return (200, {'host': [{'resource': {'host': 'sample_host'}}], })
return (200, {}, {'host': [{'resource': {'host': 'sample_host'}}], })
def put_os_hosts_sample_host_1(self, body, **kw):
return (200, {'host': 'sample-host_1',
return (200, {}, {'host': 'sample-host_1',
'status': 'enabled'})
def put_os_hosts_sample_host_2(self, body, **kw):
return (200, {'host': 'sample-host_2',
return (200, {}, {'host': 'sample-host_2',
'maintenance_mode': 'on_maintenance'})
def put_os_hosts_sample_host_3(self, body, **kw):
return (200, {'host': 'sample-host_3',
return (200, {}, {'host': 'sample-host_3',
'status': 'enabled',
'maintenance_mode': 'on_maintenance'})
def get_os_hosts_sample_host_startup(self, **kw):
return (200, {'host': 'sample_host',
return (200, {}, {'host': 'sample_host',
'power_action': 'startup'})
def get_os_hosts_sample_host_reboot(self, **kw):
return (200, {'host': 'sample_host',
return (200, {}, {'host': 'sample_host',
'power_action': 'reboot'})
def get_os_hosts_sample_host_shutdown(self, **kw):
return (200, {'host': 'sample_host',
return (200, {}, {'host': 'sample_host',
'power_action': 'shutdown'})
def put_os_hosts_sample_host(self, body, **kw):
result = {'host': 'dummy'}
result.update(body)
return (200, result)
return (200, {}, result)
def get_os_hypervisors(self, **kw):
return (200, {"hypervisors": [
return (200, {}, {"hypervisors": [
{'id': 1234, 'hypervisor_hostname': 'hyper1'},
{'id': 5678, 'hypervisor_hostname': 'hyper2'},
]})
def get_os_hypervisors_detail(self, **kw):
return (200, {"hypervisors": [
return (200, {}, {"hypervisors": [
{'id': 1234,
'service': {'id': 1, 'host': 'compute1'},
'vcpus': 4,
@ -1160,7 +1219,7 @@ class FakeHTTPClient(base_client.HTTPClient):
]})
def get_os_hypervisors_statistics(self, **kw):
return (200, {"hypervisor_statistics": {
return (200, {}, {"hypervisor_statistics": {
'count': 2,
'vcpus': 8,
'memory_mb': 20 * 1024,
@ -1176,13 +1235,13 @@ class FakeHTTPClient(base_client.HTTPClient):
}})
def get_os_hypervisors_hyper_search(self, **kw):
return (200, {'hypervisors': [
return (200, {}, {'hypervisors': [
{'id': 1234, 'hypervisor_hostname': 'hyper1'},
{'id': 5678, 'hypervisor_hostname': 'hyper2'}
]})
def get_os_hypervisors_hyper_servers(self, **kw):
return (200, {'hypervisors': [
return (200, {}, {'hypervisors': [
{'id': 1234,
'hypervisor_hostname': 'hyper1',
'servers': [
@ -1198,7 +1257,7 @@ class FakeHTTPClient(base_client.HTTPClient):
]})
def get_os_hypervisors_1234(self, **kw):
return (200, {'hypervisor':
return (200, {}, {'hypervisor':
{'id': 1234,
'service': {'id': 1, 'host': 'compute1'},
'vcpus': 4,
@ -1218,37 +1277,37 @@ class FakeHTTPClient(base_client.HTTPClient):
'disk_available_least': 100}})
def get_os_hypervisors_1234_uptime(self, **kw):
return (200, {'hypervisor':
return (200, {}, {'hypervisor':
{'id': 1234,
'hypervisor_hostname': "hyper1",
'uptime': "fake uptime"}})
def get_os_networks(self, **kw):
return (200, {'networks': [{"label": "1", "cidr": "10.0.0.0/24",
return (200, {}, {'networks': [{"label": "1", "cidr": "10.0.0.0/24",
'project_id': '4ffc664c198e435e9853f2538fbcd7a7',
'id': '1'}]})
def get_os_networks_1(self, **kw):
return (200, {'network': {"label": "1", "cidr": "10.0.0.0/24"}})
return (200, {}, {'network': {"label": "1", "cidr": "10.0.0.0/24"}})
def post_os_networks(self, **kw):
return (202, {'network': kw})
return (202, {}, {'network': kw})
def post_os_networks_1_action(self, **kw):
return (202, None)
return (202, {}, None)
def delete_os_networks_networkdelete(self, **kw):
return (202, None)
return (202, {}, None)
def post_os_networks_add(self, **kw):
return (202, None)
return (202, {}, None)
def post_os_networks_networkdisassociate_action(self, **kw):
return (202, None)
return (202, {}, None)
def get_os_fping(self, **kw):
return (
200, {
200, {}, {
'servers': [
{
"id": "1",
@ -1266,7 +1325,7 @@ class FakeHTTPClient(base_client.HTTPClient):
def get_os_fping_1(self, **kw):
return (
200, {
200, {}, {
'server': {
"id": "1",
"project_id": "fake-project",
@ -1276,21 +1335,21 @@ class FakeHTTPClient(base_client.HTTPClient):
)
def post_os_networks(self, **kw):
return (202, {'network': kw})
return (202, {}, {'network': kw})
def post_os_networks_1_action(self, **kw):
return (202, None)
return (202, {}, None)
def post_os_networks_networktest_action(self, **kw):
return (202, None)
return (202, {}, None)
def post_os_networks_2_action(self, **kw):
return (202, None)
return (202, {}, None)
def post_os_coverage_action(self, body, **kw):
if 'report' not in body:
return (200, None)
return (200, {}, None)
else:
return (200, {
return (200, {}, {
'path': '/tmp/tmpdir/' + body['report']['file']
})

View File

@ -1,20 +1,14 @@
import httplib2
import copy
import json
import mock
import requests
from novaclient.v1_1 import client
from novaclient import exceptions
from tests import utils
def to_http_response(resp_dict):
"""Converts dict of response attributes to httplib response."""
resp = httplib2.Response(resp_dict)
for k, v in resp_dict['headers'].items():
resp[k] = v
return resp
class AuthenticateAgainstKeystoneTests(utils.TestCase):
def test_authenticate_success(self):
cs = client.Client("username", "password", "project_id",
@ -40,15 +34,14 @@ class AuthenticateAgainstKeystoneTests(utils.TestCase):
],
},
}
auth_response = httplib2.Response({
"status": 200,
"body": json.dumps(resp),
})
auth_response = utils.TestResponse({
"status_code": 200,
"text": json.dumps(resp),
})
mock_request = mock.Mock(return_value=(auth_response,
json.dumps(resp)))
mock_request = mock.Mock(return_value=(auth_response))
@mock.patch.object(httplib2.Http, "request", mock_request)
@mock.patch.object(requests, "request", mock_request)
def test_auth_call():
cs.client.authenticate()
headers = {
@ -67,9 +60,13 @@ class AuthenticateAgainstKeystoneTests(utils.TestCase):
}
token_url = cs.client.auth_url + "/tokens"
mock_request.assert_called_with(token_url, "POST",
headers=headers,
body=json.dumps(body))
mock_request.assert_called_with(
"POST",
token_url,
headers=headers,
data=json.dumps(body),
allow_redirects=True,
**self.TEST_REQUEST_BASE)
endpoints = resp["access"]["serviceCatalog"][0]['endpoints']
public_url = endpoints[0]["publicURL"].rstrip('/')
@ -83,15 +80,14 @@ class AuthenticateAgainstKeystoneTests(utils.TestCase):
cs = client.Client("username", "password", "project_id",
"auth_url/v2.0")
resp = {"unauthorized": {"message": "Unauthorized", "code": "401"}}
auth_response = httplib2.Response({
"status": 401,
"body": json.dumps(resp),
})
auth_response = utils.TestResponse({
"status_code": 401,
"text": json.dumps(resp),
})
mock_request = mock.Mock(return_value=(auth_response,
json.dumps(resp)))
mock_request = mock.Mock(return_value=(auth_response))
@mock.patch.object(httplib2.Http, "request", mock_request)
@mock.patch.object(requests, "request", mock_request)
def test_auth_call():
self.assertRaises(exceptions.Unauthorized, cs.client.authenticate)
@ -124,29 +120,28 @@ class AuthenticateAgainstKeystoneTests(utils.TestCase):
correct_response = json.dumps(dict_correct_response)
dict_responses = [
{"headers": {'location':'http://127.0.0.1:5001'},
"status": 305,
"body": "Use proxy"},
"status_code": 305,
"text": "Use proxy"},
# Configured on admin port, nova redirects to v2.0 port.
# When trying to connect on it, keystone auth succeed by v1.0
# protocol (through headers) but tokens are being returned in
# body (looks like keystone bug). Leaved for compatibility.
{"headers": {},
"status": 200,
"body": correct_response},
"status_code": 200,
"text": correct_response},
{"headers": {},
"status": 200,
"body": correct_response}
"status_code": 200,
"text": correct_response}
]
responses = [(to_http_response(resp), resp['body']) \
for resp in dict_responses]
responses = [(utils.TestResponse(resp)) for resp in dict_responses]
def side_effect(*args, **kwargs):
return responses.pop(0)
mock_request = mock.Mock(side_effect=side_effect)
@mock.patch.object(httplib2.Http, "request", mock_request)
@mock.patch.object(requests, "request", mock_request)
def test_auth_call():
cs.client.authenticate()
headers = {
@ -165,9 +160,14 @@ class AuthenticateAgainstKeystoneTests(utils.TestCase):
}
token_url = cs.client.auth_url + "/tokens"
mock_request.assert_called_with(token_url, "POST",
headers=headers,
body=json.dumps(body))
kwargs = copy.copy(self.TEST_REQUEST_BASE)
kwargs['headers'] = headers
kwargs['data'] = json.dumps(body)
mock_request.assert_called_with(
"POST",
token_url,
allow_redirects=True,
**kwargs)
resp = dict_correct_response
endpoints = resp["access"]["serviceCatalog"][0]['endpoints']
@ -214,15 +214,14 @@ class AuthenticateAgainstKeystoneTests(utils.TestCase):
],
},
}
auth_response = httplib2.Response({
"status": 200,
"body": json.dumps(resp),
})
auth_response = utils.TestResponse({
"status_code": 200,
"text": json.dumps(resp),
})
mock_request = mock.Mock(return_value=(auth_response,
json.dumps(resp)))
mock_request = mock.Mock(return_value=(auth_response))
@mock.patch.object(httplib2.Http, "request", mock_request)
@mock.patch.object(requests, "request", mock_request)
def test_auth_call():
self.assertRaises(exceptions.AmbiguousEndpoints,
cs.client.authenticate)
@ -234,14 +233,16 @@ class AuthenticationTests(utils.TestCase):
def test_authenticate_success(self):
cs = client.Client("username", "password", "project_id", "auth_url")
management_url = 'https://localhost/v1.1/443470'
auth_response = httplib2.Response({
'status': 204,
'x-server-management-url': management_url,
'x-auth-token': '1b751d74-de0c-46ae-84f0-915744b582d1',
auth_response = utils.TestResponse({
'status_code': 204,
'headers': {
'x-server-management-url': management_url,
'x-auth-token': '1b751d74-de0c-46ae-84f0-915744b582d1',
},
})
mock_request = mock.Mock(return_value=(auth_response, None))
mock_request = mock.Mock(return_value=(auth_response))
@mock.patch.object(httplib2.Http, "request", mock_request)
@mock.patch.object(requests, "request", mock_request)
def test_auth_call():
cs.client.authenticate()
headers = {
@ -251,21 +252,25 @@ class AuthenticationTests(utils.TestCase):
'X-Auth-Project-Id': 'project_id',
'User-Agent': cs.client.USER_AGENT
}
mock_request.assert_called_with(cs.client.auth_url, 'GET',
headers=headers)
mock_request.assert_called_with(
"GET",
cs.client.auth_url,
headers=headers,
**self.TEST_REQUEST_BASE)
self.assertEqual(cs.client.management_url,
auth_response['x-server-management-url'])
auth_response.headers['x-server-management-url'])
self.assertEqual(cs.client.auth_token,
auth_response['x-auth-token'])
auth_response.headers['x-auth-token'])
test_auth_call()
def test_authenticate_failure(self):
cs = client.Client("username", "password", "project_id", "auth_url")
auth_response = httplib2.Response({'status': 401})
mock_request = mock.Mock(return_value=(auth_response, None))
auth_response = utils.TestResponse({'status_code': 401})
mock_request = mock.Mock(return_value=(auth_response))
@mock.patch.object(httplib2.Http, "request", mock_request)
@mock.patch.object(requests, "request", mock_request)
def test_auth_call():
self.assertRaises(exceptions.Unauthorized, cs.client.authenticate)

View File

@ -1,5 +1,5 @@
argparse
httplib2
iso8601>=0.1.4
prettytable>=0.6,<0.7
requests<1.0
simplejson