summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJamie Lennox <jamielennox@redhat.com>2014-07-11 09:01:07 +1000
committerJamie Lennox <jamielennox@redhat.com>2014-08-25 16:15:26 +1000
commit8f23885ca3a55c9ef61fcf6284ca140c4a1c0b9a (patch)
tree1cec3acff2ccf013ab41c2e5e4efc03df76e992a
parent44f842b1b99b6454075ee1cdc391650221a29bad (diff)
downloadpython-cinderclient-8f23885ca3a55c9ef61fcf6284ca140c4a1c0b9a.tar.gz
Convert snapshot tests to requests-mock
Blueprint: http-mock-testing Change-Id: I06eae25cda6de9c266d34947471675fd95359739
-rw-r--r--cinderclient/tests/fixture_data/base.py38
-rw-r--r--cinderclient/tests/fixture_data/client.py64
-rw-r--r--cinderclient/tests/fixture_data/snapshots.py56
-rw-r--r--cinderclient/tests/utils.py39
-rw-r--r--cinderclient/tests/v1/test_snapshot_actions.py25
-rw-r--r--cinderclient/tests/v2/test_snapshot_actions.py25
6 files changed, 223 insertions, 24 deletions
diff --git a/cinderclient/tests/fixture_data/base.py b/cinderclient/tests/fixture_data/base.py
new file mode 100644
index 0000000..9406daf
--- /dev/null
+++ b/cinderclient/tests/fixture_data/base.py
@@ -0,0 +1,38 @@
+# 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 fixtures
+
+IDENTITY_URL = 'http://identityserver:5000/v2.0'
+VOLUME_URL = 'http://volume.host'
+
+
+class Fixture(fixtures.Fixture):
+
+ base_url = None
+ json_headers = {'Content-Type': 'application/json'}
+
+ def __init__(self, requests,
+ volume_url=VOLUME_URL,
+ identity_url=IDENTITY_URL):
+ super(Fixture, self).__init__()
+ self.requests = requests
+ self.volume_url = volume_url
+ self.identity_url = identity_url
+
+ def url(self, *args):
+ url_args = [self.volume_url]
+
+ if self.base_url:
+ url_args.append(self.base_url)
+
+ return '/'.join(str(a).strip('/') for a in tuple(url_args) + args)
diff --git a/cinderclient/tests/fixture_data/client.py b/cinderclient/tests/fixture_data/client.py
new file mode 100644
index 0000000..47bbb64
--- /dev/null
+++ b/cinderclient/tests/fixture_data/client.py
@@ -0,0 +1,64 @@
+# 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.
+
+from keystoneclient import fixture
+
+from cinderclient.tests.fixture_data import base
+from cinderclient.v1 import client as v1client
+from cinderclient.v2 import client as v2client
+
+
+class Base(base.Fixture):
+
+ def __init__(self, *args, **kwargs):
+ super(Base, self).__init__(*args, **kwargs)
+
+ self.token = fixture.V2Token()
+ self.token.set_scope()
+
+ def setUp(self):
+ super(Base, self).setUp()
+
+ auth_url = '%s/tokens' % self.identity_url
+ self.requests.register_uri('POST', auth_url,
+ json=self.token,
+ headers=self.json_headers)
+
+
+class V1(Base):
+
+ def __init__(self, *args, **kwargs):
+ super(V1, self).__init__(*args, **kwargs)
+
+ svc = self.token.add_service('volume')
+ svc.add_endpoint(self.volume_url)
+
+ def new_client(self):
+ return v1client.Client(username='xx',
+ api_key='xx',
+ project_id='xx',
+ auth_url=self.identity_url)
+
+
+class V2(Base):
+
+ def __init__(self, *args, **kwargs):
+ super(V2, self).__init__(*args, **kwargs)
+
+ svc = self.token.add_service('volumev2')
+ svc.add_endpoint(self.volume_url)
+
+ def new_client(self):
+ return v2client.Client(username='xx',
+ api_key='xx',
+ project_id='xx',
+ auth_url=self.identity_url)
diff --git a/cinderclient/tests/fixture_data/snapshots.py b/cinderclient/tests/fixture_data/snapshots.py
new file mode 100644
index 0000000..c379639
--- /dev/null
+++ b/cinderclient/tests/fixture_data/snapshots.py
@@ -0,0 +1,56 @@
+# 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
+
+from cinderclient.tests.fixture_data import base
+
+
+def _stub_snapshot(**kwargs):
+ snapshot = {
+ "created_at": "2012-08-28T16:30:31.000000",
+ "display_description": None,
+ "display_name": None,
+ "id": '11111111-1111-1111-1111-111111111111',
+ "size": 1,
+ "status": "available",
+ "volume_id": '00000000-0000-0000-0000-000000000000',
+ }
+ snapshot.update(kwargs)
+ return snapshot
+
+
+class Fixture(base.Fixture):
+
+ base_url = 'snapshots'
+
+ def setUp(self):
+ super(Fixture, self).setUp()
+
+ snapshot_1234 = _stub_snapshot(id='1234')
+ self.requests.register_uri('GET', self.url('1234'),
+ json={'snapshot': snapshot_1234})
+
+ def action_1234(request, context):
+ return ''
+ body = json.loads(request.body.decode('utf-8'))
+ assert len(list(body)) == 1
+ action = list(body)[0]
+ if action == 'os-reset_status':
+ assert 'status' in body['os-reset_status']
+ elif action == 'os-update_snapshot_status':
+ assert 'status' in body['os-update_snapshot_status']
+ else:
+ raise AssertionError("Unexpected action: %s" % action)
+ return ''
+ self.requests.register_uri('POST', self.url('1234', 'action'),
+ text=action_1234, status_code=202)
diff --git a/cinderclient/tests/utils.py b/cinderclient/tests/utils.py
index d6c48ce..25e7ee0 100644
--- a/cinderclient/tests/utils.py
+++ b/cinderclient/tests/utils.py
@@ -11,10 +11,13 @@
# See the License for the specific language governing permissions and
# limitations under the License.
+import json
import os
import fixtures
import requests
+from requests_mock.contrib import fixture as requests_mock_fixture
+import six
import testtools
@@ -35,6 +38,42 @@ class TestCase(testtools.TestCase):
self.useFixture(fixtures.MonkeyPatch('sys.stderr', stderr))
+class FixturedTestCase(TestCase):
+
+ client_fixture_class = None
+ data_fixture_class = None
+
+ def setUp(self):
+ super(FixturedTestCase, self).setUp()
+
+ self.requests = self.useFixture(requests_mock_fixture.Fixture())
+ self.data_fixture = None
+ self.client_fixture = None
+ self.cs = None
+
+ if self.client_fixture_class:
+ fix = self.client_fixture_class(self.requests)
+ self.client_fixture = self.useFixture(fix)
+ self.cs = self.client_fixture.new_client()
+
+ if 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):
+ self.assertEqual(self.requests.last_request.method, method)
+ self.assertEqual(self.requests.last_request.path_url, path)
+
+ if body:
+ req_data = self.requests.last_request.body
+ if isinstance(req_data, six.binary_type):
+ req_data = req_data.decode('utf-8')
+ if not isinstance(body, six.string_types):
+ # json load if the input body to match against is not a string
+ req_data = json.loads(req_data)
+ self.assertEqual(req_data, body)
+
+
class TestResponse(requests.Response):
"""Class used to wrap requests.Response and provide some
convenience to initialize with a dict.
diff --git a/cinderclient/tests/v1/test_snapshot_actions.py b/cinderclient/tests/v1/test_snapshot_actions.py
index 70b14e1..c9bb81e 100644
--- a/cinderclient/tests/v1/test_snapshot_actions.py
+++ b/cinderclient/tests/v1/test_snapshot_actions.py
@@ -14,22 +14,23 @@
# under the License.
from cinderclient.tests import utils
-from cinderclient.tests.v1 import fakes
+from cinderclient.tests.fixture_data import client
+from cinderclient.tests.fixture_data import snapshots
-cs = fakes.FakeClient()
+class SnapshotActionsTest(utils.FixturedTestCase):
+ client_fixture_class = client.V1
+ data_fixture_class = snapshots.Fixture
-class SnapshotActionsTest(utils.TestCase):
def test_update_snapshot_status(self):
- s = cs.volume_snapshots.get('1234')
- cs.volume_snapshots.update_snapshot_status(s,
- {'status': 'available'})
- cs.assert_called('POST', '/snapshots/1234/action')
+ s = self.cs.volume_snapshots.get('1234')
+ stat = {'status': 'available'}
+ self.cs.volume_snapshots.update_snapshot_status(s, stat)
+ self.assert_called('POST', '/snapshots/1234/action')
def test_update_snapshot_status_with_progress(self):
- s = cs.volume_snapshots.get('1234')
- cs.volume_snapshots.update_snapshot_status(s,
- {'status': 'available',
- 'progress': '73%'})
- cs.assert_called('POST', '/snapshots/1234/action')
+ s = self.cs.volume_snapshots.get('1234')
+ stat = {'status': 'available', 'progress': '73%'}
+ self.cs.volume_snapshots.update_snapshot_status(s, stat)
+ self.assert_called('POST', '/snapshots/1234/action')
diff --git a/cinderclient/tests/v2/test_snapshot_actions.py b/cinderclient/tests/v2/test_snapshot_actions.py
index f70cc8f..f220541 100644
--- a/cinderclient/tests/v2/test_snapshot_actions.py
+++ b/cinderclient/tests/v2/test_snapshot_actions.py
@@ -14,22 +14,23 @@
# under the License.
from cinderclient.tests import utils
-from cinderclient.tests.v2 import fakes
+from cinderclient.tests.fixture_data import client
+from cinderclient.tests.fixture_data import snapshots
-cs = fakes.FakeClient()
+class SnapshotActionsTest(utils.FixturedTestCase):
+ client_fixture_class = client.V2
+ data_fixture_class = snapshots.Fixture
-class SnapshotActionsTest(utils.TestCase):
def test_update_snapshot_status(self):
- s = cs.volume_snapshots.get('1234')
- cs.volume_snapshots.update_snapshot_status(s,
- {'status': 'available'})
- cs.assert_called('POST', '/snapshots/1234/action')
+ s = self.cs.volume_snapshots.get('1234')
+ stat = {'status': 'available'}
+ self.cs.volume_snapshots.update_snapshot_status(s, stat)
+ self.assert_called('POST', '/snapshots/1234/action')
def test_update_snapshot_status_with_progress(self):
- s = cs.volume_snapshots.get('1234')
- cs.volume_snapshots.update_snapshot_status(s,
- {'status': 'available',
- 'progress': '73%'})
- cs.assert_called('POST', '/snapshots/1234/action')
+ s = self.cs.volume_snapshots.get('1234')
+ stat = {'status': 'available', 'progress': '73%'}
+ self.cs.volume_snapshots.update_snapshot_status(s, stat)
+ self.assert_called('POST', '/snapshots/1234/action')