Replace httpretty with requests_mock

The httpretty library has poor Py3 support, currently the only
working version is 0.8.0, all later minor versions are pinned.

Requests-mock library provides mocking layer for requests package. It is
written in such a way that it is fairly simple to convert existing
httpretty test suites to requests-mock. The library is hosted at Stackforge
and gated as usual OpenStack project.

Change-Id: If7f4baefb53976571efc47dda17a7b543112d555
This commit is contained in:
Ilya Shakhat
2014-08-11 17:38:55 +04:00
parent fa5642f155
commit 9d8ab0df56
5 changed files with 69 additions and 67 deletions

View File

@@ -18,9 +18,9 @@ import json
import uuid
import fixtures
import httpretty
from mox3 import mox
import requests
import requests_mock
import six
import testtools
@@ -138,24 +138,24 @@ def get_response(status_code, headers=None):
return response
def setup_keystone_v2():
def setup_keystone_v2(mrequests):
v2_token = ks_v2_fixture.Token(token_id=TOKENID)
service = v2_token.add_service('network')
service.add_endpoint(PUBLIC_ENDPOINT_URL, region=REGION)
httpretty.register_uri(httpretty.POST,
mrequests.register_uri('POST',
'%s/tokens' % (V2_URL),
body=json.dumps(v2_token))
text=json.dumps(v2_token))
auth_session = session.Session()
auth_plugin = ks_v2_auth.Password(V2_URL, 'xx', 'xx')
return auth_session, auth_plugin
def setup_keystone_v3():
httpretty.register_uri(httpretty.GET,
def setup_keystone_v3(mrequests):
mrequests.register_uri('GET',
V3_URL,
body=V3_VERSION_ENTRY)
text=V3_VERSION_ENTRY)
v3_token = ks_v3_fixture.Token()
service = v3_token.add_service('network')
@@ -164,10 +164,10 @@ def setup_keystone_v3():
internal=INTERNAL_ENDPOINT_URL,
region=REGION)
httpretty.register_uri(httpretty.POST,
mrequests.register_uri('POST',
'%s/auth/tokens' % (V3_URL),
body=json.dumps(v3_token),
adding_headers={'X-Subject-Token': TOKENID})
text=json.dumps(v3_token),
headers={'X-Subject-Token': TOKENID})
auth_session = session.Session()
auth_plugin = ks_v3_auth.Password(V3_URL,
@@ -247,9 +247,9 @@ class CLITestAuthKeystone(testtools.TestCase):
'endpoint_url': self.client.endpoint_url}
self.assertEqual(client_.get_auth_info(), expected)
@httpretty.activate
def test_get_token(self):
auth_session, auth_plugin = setup_keystone_v2()
@requests_mock.Mocker()
def test_get_token(self, mrequests):
auth_session, auth_plugin = setup_keystone_v2(mrequests)
self.client = client.construct_http_client(
username=USERNAME,
@@ -403,9 +403,9 @@ class CLITestAuthKeystone(testtools.TestCase):
self.mox.ReplayAll()
self.client.do_request('/resource', 'GET')
@httpretty.activate
def test_endpoint_type(self):
auth_session, auth_plugin = setup_keystone_v3()
@requests_mock.Mocker()
def test_endpoint_type(self, mrequests):
auth_session, auth_plugin = setup_keystone_v3(mrequests)
# Test default behavior is to choose public.
self.client = client.construct_http_client(
@@ -518,9 +518,9 @@ class TestKeystoneClientVersions(testtools.TestCase):
self.addCleanup(self.mox.VerifyAll)
self.addCleanup(self.mox.UnsetStubs)
@httpretty.activate
def test_v2_auth(self):
auth_session, auth_plugin = setup_keystone_v2()
@requests_mock.Mocker()
def test_v2_auth(self, mrequests):
auth_session, auth_plugin = setup_keystone_v2(mrequests)
res200 = get_response(200)
self.client = client.construct_http_client(
@@ -542,9 +542,9 @@ class TestKeystoneClientVersions(testtools.TestCase):
self.mox.ReplayAll()
self.client.do_request('/resource', 'GET')
@httpretty.activate
def test_v3_auth(self):
auth_session, auth_plugin = setup_keystone_v3()
@requests_mock.Mocker()
def test_v3_auth(self, mrequests):
auth_session, auth_plugin = setup_keystone_v3(mrequests)
res200 = get_response(200)
self.client = client.construct_http_client(

View File

@@ -16,6 +16,7 @@
import abc
from mox3 import mox
import requests_mock
import six
import testtools
@@ -81,8 +82,9 @@ class TestHTTPClientMixin(object):
class TestSessionClient(TestHTTPClientMixin, testtools.TestCase):
def initialize(self):
session, auth = test_auth.setup_keystone_v2()
@requests_mock.Mocker()
def initialize(self, mrequests):
session, auth = test_auth.setup_keystone_v2(mrequests)
return [client.SessionClient,
client.SessionClient(session=session, auth=auth)]

View File

@@ -20,8 +20,8 @@ import re
import sys
import fixtures
import httpretty
from mox3 import mox
import requests_mock
import six
import testtools
from testtools import matchers
@@ -134,12 +134,12 @@ class ShellTest(testtools.TestCase):
self.assertEqual('You must provide a service URL via '
'either --os-url or env[OS_URL]', stderr.strip())
@httpretty.activate
def test_auth(self):
@requests_mock.Mocker()
def test_auth(self, mrequests):
# emulate Keystone version discovery
httpretty.register_uri(httpretty.GET,
mrequests.register_uri('GET',
auth.V3_URL,
body=auth.V3_VERSION_ENTRY)
text=auth.V3_VERSION_ENTRY)
neutron_shell = openstack_shell.NeutronShell('2.0')
self.addCleanup(self.mox.UnsetStubs)
@@ -169,12 +169,12 @@ class ShellTest(testtools.TestCase):
neutron_shell.run(cmdline.split())
self.mox.VerifyAll()
@httpretty.activate
def test_auth_cert_and_key(self):
@requests_mock.Mocker()
def test_auth_cert_and_key(self, mrequests):
# emulate Keystone version discovery
httpretty.register_uri(httpretty.GET,
mrequests.register_uri('GET',
auth.V3_URL,
body=auth.V3_VERSION_ENTRY)
text=auth.V3_VERSION_ENTRY)
neutron_shell = openstack_shell.NeutronShell('2.0')
self.addCleanup(self.mox.UnsetStubs)
@@ -205,12 +205,12 @@ class ShellTest(testtools.TestCase):
neutron_shell.run(cmdline.split())
self.mox.VerifyAll()
@httpretty.activate
def test_v2_auth(self):
@requests_mock.Mocker()
def test_v2_auth(self, mrequests):
# emulate Keystone version discovery
httpretty.register_uri(httpretty.GET,
mrequests.register_uri('GET',
auth.V2_URL,
body=auth.V2_VERSION_ENTRY)
text=auth.V2_VERSION_ENTRY)
neutron_shell = openstack_shell.NeutronShell('2.0')
self.addCleanup(self.mox.UnsetStubs)
@@ -240,12 +240,12 @@ class ShellTest(testtools.TestCase):
neutron_shell.run(cmdline.split())
self.mox.VerifyAll()
@httpretty.activate
def test_failed_auth_version_discovery_v3_auth_url(self):
@requests_mock.Mocker()
def test_failed_auth_version_discovery_v3_auth_url(self, mrequests):
# emulate Keystone version discovery
httpretty.register_uri(httpretty.GET,
mrequests.register_uri('GET',
auth.V3_URL,
status=405)
status_code=405)
neutron_shell = openstack_shell.NeutronShell('2.0')
self.addCleanup(self.mox.UnsetStubs)
@@ -276,12 +276,12 @@ class ShellTest(testtools.TestCase):
neutron_shell.run(cmdline.split())
self.mox.VerifyAll()
@httpretty.activate
def test_failed_auth_version_discovery_v2_auth_url(self):
@requests_mock.Mocker()
def test_failed_auth_version_discovery_v2_auth_url(self, mrequests):
# emulate Keystone version discovery
httpretty.register_uri(httpretty.GET,
mrequests.register_uri('GET',
auth.V2_URL,
status=405)
status_code=405)
neutron_shell = openstack_shell.NeutronShell('2.0')
self.addCleanup(self.mox.UnsetStubs)
@@ -311,12 +311,12 @@ class ShellTest(testtools.TestCase):
neutron_shell.run(cmdline.split())
self.mox.VerifyAll()
@httpretty.activate
def test_auth_version_discovery_v3(self):
@requests_mock.Mocker()
def test_auth_version_discovery_v3(self, mrequests):
# emulate Keystone version discovery
httpretty.register_uri(httpretty.GET,
mrequests.register_uri('GET',
auth.BASE_URL,
body=auth.V3_VERSION_LIST)
text=auth.V3_VERSION_LIST)
neutron_shell = openstack_shell.NeutronShell('2.0')
self.addCleanup(self.mox.UnsetStubs)
@@ -347,12 +347,12 @@ class ShellTest(testtools.TestCase):
neutron_shell.run(cmdline.split())
self.mox.VerifyAll()
@httpretty.activate
def test_auth_version_discovery_v2(self):
@requests_mock.Mocker()
def test_auth_version_discovery_v2(self, mrequests):
# emulate Keystone version discovery
httpretty.register_uri(httpretty.GET,
mrequests.register_uri('GET',
auth.BASE_URL,
body=auth.V3_VERSION_LIST)
text=auth.V3_VERSION_LIST)
neutron_shell = openstack_shell.NeutronShell('2.0')
self.addCleanup(self.mox.UnsetStubs)
@@ -382,12 +382,12 @@ class ShellTest(testtools.TestCase):
neutron_shell.run(cmdline.split())
self.mox.VerifyAll()
@httpretty.activate
def test_insecure_auth(self):
@requests_mock.Mocker()
def test_insecure_auth(self, mrequests):
# emulate Keystone version discovery
httpretty.register_uri(httpretty.GET,
mrequests.register_uri('GET',
auth.V2_URL,
body=auth.V2_VERSION_ENTRY)
text=auth.V2_VERSION_ENTRY)
neutron_shell = openstack_shell.NeutronShell('2.0')
self.addCleanup(self.mox.UnsetStubs)

View File

@@ -17,8 +17,8 @@ import fixtures
import requests
import testtools
import httpretty
from mox3 import mox
import requests_mock
from neutronclient.client import HTTPClient
from neutronclient.common.clientmanager import ClientManager
@@ -43,12 +43,12 @@ class TestSSL(testtools.TestCase):
self.mox = mox.Mox()
self.addCleanup(self.mox.UnsetStubs)
@httpretty.activate
def test_ca_cert_passed(self):
@requests_mock.Mocker()
def test_ca_cert_passed(self, mrequests):
# emulate Keystone version discovery
httpretty.register_uri(httpretty.GET,
mrequests.register_uri('GET',
auth.V3_URL,
body=auth.V3_VERSION_ENTRY)
text=auth.V3_VERSION_ENTRY)
self.mox.StubOutWithMock(ClientManager, '__init__')
self.mox.StubOutWithMock(openstack_shell.NeutronShell, 'interact')
@@ -87,13 +87,13 @@ class TestSSL(testtools.TestCase):
openstack_shell.NeutronShell('2.0').run(cmdline.split())
self.mox.VerifyAll()
@httpretty.activate
def test_ca_cert_passed_as_env_var(self):
@requests_mock.Mocker()
def test_ca_cert_passed_as_env_var(self, mrequests):
# emulate Keystone version discovery
httpretty.register_uri(httpretty.GET,
mrequests.register_uri('GET',
auth.V3_URL,
body=auth.V3_VERSION_ENTRY)
text=auth.V3_VERSION_ENTRY)
self.useFixture(fixtures.EnvironmentVariable('OS_CACERT', CA_CERT))

View File

@@ -7,11 +7,11 @@ cliff-tablib>=1.0
coverage>=3.6
discover
fixtures>=0.3.14
httpretty>=0.8.0,!=0.8.1,!=0.8.2,!=0.8.3
mox3>=0.7.0
oslosphinx>=2.2.0.0a2
oslotest>=1.1.0.0a2
python-subunit>=0.0.18
requests-mock>=0.4.0 # Apache-2.0
sphinx>=1.1.2,!=1.2.0,<1.3
testrepository>=0.0.18
testtools>=0.9.34