Use requests-mock instead of httpretty

Replace the use of httpretty with requests-mock and replace the copied
fixtures from keystoneclient with the provided fixture objects.

Change-Id: Id60e75a8c21d392ef80a9c8e4478e32c956054c6
This commit is contained in:
Jamie Lennox 2015-11-21 19:47:45 +11:00
parent 65f2b18c33
commit 0faf28c137
3 changed files with 35 additions and 126 deletions

View File

@ -1,81 +0,0 @@
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import json
import uuid
from keystoneclient.fixture import v2 as ks_v2_fixture
from keystoneclient.fixture import v3 as ks_v3_fixture
# these are copied from python-keystoneclient tests
BASE_HOST = 'http://keystone.example.com'
BASE_URL = "%s:5000/" % BASE_HOST
UPDATED = '2013-03-06T00:00:00Z'
V2_URL = "%sv2.0" % BASE_URL
V2_DESCRIBED_BY_HTML = {'href': 'http://docs.openstack.org/api/'
'openstack-identity-service/2.0/content/',
'rel': 'describedby',
'type': 'text/html'}
V2_DESCRIBED_BY_PDF = {'href': 'http://docs.openstack.org/api/openstack-ident'
'ity-service/2.0/identity-dev-guide-2.0.pdf',
'rel': 'describedby',
'type': 'application/pdf'}
V2_VERSION = {'id': 'v2.0',
'links': [{'href': V2_URL, 'rel': 'self'},
V2_DESCRIBED_BY_HTML, V2_DESCRIBED_BY_PDF],
'status': 'stable',
'updated': UPDATED}
V3_URL = "%sv3" % BASE_URL
V3_MEDIA_TYPES = [{'base': 'application/json',
'type': 'application/vnd.openstack.identity-v3+json'},
{'base': 'application/xml',
'type': 'application/vnd.openstack.identity-v3+xml'}]
V3_VERSION = {'id': 'v3.0',
'links': [{'href': V3_URL, 'rel': 'self'}],
'media-types': V3_MEDIA_TYPES,
'status': 'stable',
'updated': UPDATED}
TOKENID = uuid.uuid4().hex
def _create_version_list(versions):
return json.dumps({'versions': {'values': versions}})
def _create_single_version(version):
return json.dumps({'version': version})
V3_VERSION_LIST = _create_version_list([V3_VERSION, V2_VERSION])
V2_VERSION_LIST = _create_version_list([V2_VERSION])
V3_VERSION_ENTRY = _create_single_version(V3_VERSION)
V2_VERSION_ENTRY = _create_single_version(V2_VERSION)
def keystone_request_callback(request, uri, headers):
response_headers = {"content-type": "application/json"}
token_id = TOKENID
if uri == BASE_URL:
return (200, headers, V3_VERSION_LIST)
elif uri == BASE_URL + "/v2.0":
v2_token = ks_v2_fixture.Token(token_id)
return (200, response_headers, json.dumps(v2_token))
elif uri == BASE_URL + "/v3":
v3_token = ks_v3_fixture.Token()
response_headers["X-Subject-Token"] = token_id
return (201, response_headers, json.dumps(v3_token))

View File

@ -10,44 +10,45 @@
# 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 json
import os
import re import re
import sys import sys
import uuid
import fixtures import fixtures
import httplib2 import httplib2
import httpretty
from keystoneclient import exceptions as keystone_exc from keystoneclient import exceptions as keystone_exc
from keystoneclient.fixture import v2 as ks_v2_fixture from keystoneclient import fixture as ks_fixture
from keystoneclient.fixture import v3 as ks_v3_fixture
import mock import mock
import requests_mock
import six import six
import testtools import testtools
from testtools import matchers from testtools import matchers
from ironicclient import exc from ironicclient import exc
from ironicclient import shell as ironic_shell from ironicclient import shell as ironic_shell
from ironicclient.tests.unit import keystone_client_fixtures
from ironicclient.tests.unit import utils from ironicclient.tests.unit import utils
BASE_URL = 'http://no.where:5000'
V2_URL = BASE_URL + '/v2.0'
V3_URL = BASE_URL + '/v3'
FAKE_ENV = {'OS_USERNAME': 'username', FAKE_ENV = {'OS_USERNAME': 'username',
'OS_PASSWORD': 'password', 'OS_PASSWORD': 'password',
'OS_TENANT_NAME': 'tenant_name', 'OS_TENANT_NAME': 'tenant_name',
'OS_AUTH_URL': 'http://no.where/v2.0/'} 'OS_AUTH_URL': V2_URL}
FAKE_ENV_KEYSTONE_V2 = { FAKE_ENV_KEYSTONE_V2 = {
'OS_USERNAME': 'username', 'OS_USERNAME': 'username',
'OS_PASSWORD': 'password', 'OS_PASSWORD': 'password',
'OS_TENANT_NAME': 'tenant_name', 'OS_TENANT_NAME': 'tenant_name',
'OS_AUTH_URL': keystone_client_fixtures.BASE_URL, 'OS_AUTH_URL': V2_URL
} }
FAKE_ENV_KEYSTONE_V3 = { FAKE_ENV_KEYSTONE_V3 = {
'OS_USERNAME': 'username', 'OS_USERNAME': 'username',
'OS_PASSWORD': 'password', 'OS_PASSWORD': 'password',
'OS_TENANT_NAME': 'tenant_name', 'OS_TENANT_NAME': 'tenant_name',
'OS_AUTH_URL': keystone_client_fixtures.BASE_URL, 'OS_AUTH_URL': V3_URL,
'OS_USER_DOMAIN_ID': 'default', 'OS_USER_DOMAIN_ID': 'default',
'OS_PROJECT_DOMAIN_ID': 'default', 'OS_PROJECT_DOMAIN_ID': 'default',
} }
@ -192,8 +193,6 @@ class ShellTest(utils.BaseTestCase):
class TestCase(testtools.TestCase): class TestCase(testtools.TestCase):
tokenid = keystone_client_fixtures.TOKENID
def set_fake_env(self, fake_env): def set_fake_env(self, fake_env):
client_env = ('OS_USERNAME', 'OS_PASSWORD', 'OS_TENANT_ID', client_env = ('OS_USERNAME', 'OS_PASSWORD', 'OS_TENANT_ID',
'OS_TENANT_NAME', 'OS_AUTH_URL', 'OS_REGION_NAME', 'OS_TENANT_NAME', 'OS_AUTH_URL', 'OS_REGION_NAME',
@ -215,41 +214,34 @@ class TestCase(testtools.TestCase):
msg, expected_regexp.pattern, text) msg, expected_regexp.pattern, text)
raise self.failureException(msg) raise self.failureException(msg)
def register_keystone_v2_token_fixture(self): def register_keystone_v2_token_fixture(self, request_mocker):
v2_token = ks_v2_fixture.Token(token_id=self.tokenid) v2_token = ks_fixture.V2Token()
service = v2_token.add_service('baremetal') service = v2_token.add_service('baremetal')
service.add_endpoint('http://ironic.example.com', region='RegionOne') service.add_endpoint('http://ironic.example.com', region='RegionOne')
httpretty.register_uri( request_mocker.post('%s/tokens' % V2_URL,
httpretty.POST, json=v2_token)
'%s/tokens' % (keystone_client_fixtures.V2_URL),
body=json.dumps(v2_token))
def register_keystone_v3_token_fixture(self): def register_keystone_v3_token_fixture(self, request_mocker):
v3_token = ks_v3_fixture.Token() v3_token = ks_fixture.V3Token()
service = v3_token.add_service('baremetal') service = v3_token.add_service('baremetal')
service.add_standard_endpoints(public='http://ironic.example.com') service.add_standard_endpoints(public='http://ironic.example.com')
httpretty.register_uri( request_mocker.post(
httpretty.POST, '%s/auth/tokens' % V3_URL,
'%s/auth/tokens' % (keystone_client_fixtures.V3_URL), json=v3_token,
body=json.dumps(v3_token), headers={'X-Subject-Token': uuid.uuid4().hex})
adding_headers={'X-Subject-Token': self.tokenid})
def register_keystone_auth_fixture(self): def register_keystone_auth_fixture(self, request_mocker):
self.register_keystone_v2_token_fixture() self.register_keystone_v2_token_fixture(request_mocker)
self.register_keystone_v3_token_fixture() self.register_keystone_v3_token_fixture(request_mocker)
httpretty.register_uri(
httpretty.GET, request_mocker.get(V2_URL, json=ks_fixture.V2Discovery(V2_URL))
keystone_client_fixtures.BASE_URL, request_mocker.get(V3_URL, json=ks_fixture.V3Discovery(V3_URL))
body=keystone_client_fixtures.keystone_request_callback) request_mocker.get(BASE_URL, json=ks_fixture.DiscoveryList(BASE_URL))
class ShellTestNoMox(TestCase): class ShellTestNoMox(TestCase):
def setUp(self): def setUp(self):
super(ShellTestNoMox, self).setUp() super(ShellTestNoMox, self).setUp()
# httpretty doesn't work as expected if http proxy environment
# variable is set.
os.environ = dict((k, v) for (k, v) in os.environ.items()
if k.lower() not in ('http_proxy', 'https_proxy'))
self.set_fake_env(FAKE_ENV_KEYSTONE_V2) self.set_fake_env(FAKE_ENV_KEYSTONE_V2)
def shell(self, argstr): def shell(self, argstr):
@ -269,9 +261,9 @@ class ShellTestNoMox(TestCase):
return out return out
@httpretty.activate @requests_mock.mock()
def test_node_list(self): def test_node_list(self, request_mocker):
self.register_keystone_auth_fixture() self.register_keystone_auth_fixture(request_mocker)
resp_dict = {"nodes": [ resp_dict = {"nodes": [
{"instance_uuid": "null", {"instance_uuid": "null",
"uuid": "351a82d6-9f04-4c36-b79a-a38b9e98ff71", "uuid": "351a82d6-9f04-4c36-b79a-a38b9e98ff71",
@ -295,12 +287,10 @@ class ShellTestNoMox(TestCase):
"maintenance": "false", "maintenance": "false",
"provision_state": "null", "provision_state": "null",
"power_state": "power off"}]} "power_state": "power off"}]}
httpretty.register_uri( headers = {'Content-Type': 'application/json; charset=UTF-8'}
httpretty.GET, request_mocker.get('http://ironic.example.com/v1/nodes',
'http://ironic.example.com/v1/nodes', headers=headers,
status=200, json=resp_dict)
content_type='application/json; charset=UTF-8',
body=json.dumps(resp_dict))
event_list_text = self.shell('node-list') event_list_text = self.shell('node-list')

View File

@ -6,7 +6,7 @@ coverage>=3.6
discover discover
doc8 # Apache-2.0 doc8 # Apache-2.0
fixtures>=1.3.1 fixtures>=1.3.1
httpretty<0.8.7,>=0.8.4 requests-mock>=0.6.0 # Apache-2.0
mock>=1.2 mock>=1.2
Babel>=1.3 Babel>=1.3
oslosphinx!=3.4.0,>=2.5.0 # Apache-2.0 oslosphinx!=3.4.0,>=2.5.0 # Apache-2.0