From cbad7a7444858f9d1be88e82418f846abfde9676 Mon Sep 17 00:00:00 2001 From: Giampaolo Lauria Date: Tue, 19 Feb 2013 14:15:21 -0500 Subject: Add snapshot methods to fakes.py Modified Snapshots unit tests to use them Removed logging from unit tests The methods will also be used by the sample tests Fixes bug 1130313 Change-Id: Id6c9c809952acefe302432bdfc345378730f0243 --- .../openstack/compute/contrib/test_snapshots.py | 118 ++------------------- nova/tests/api/openstack/fakes.py | 22 +++- 2 files changed, 26 insertions(+), 114 deletions(-) diff --git a/nova/tests/api/openstack/compute/contrib/test_snapshots.py b/nova/tests/api/openstack/compute/contrib/test_snapshots.py index fa0c521fe..a890abe6f 100644 --- a/nova/tests/api/openstack/compute/contrib/test_snapshots.py +++ b/nova/tests/api/openstack/compute/contrib/test_snapshots.py @@ -18,71 +18,12 @@ import webob from nova.api.openstack.compute.contrib import volumes from nova import context -from nova import exception from nova.openstack.common import jsonutils -from nova.openstack.common import log as logging from nova.openstack.common import timeutils from nova import test from nova.tests.api.openstack import fakes from nova.volume import cinder -LOG = logging.getLogger(__name__) - -_last_param = {} - - -def _get_default_snapshot_param(): - return { - 'id': 123, - 'volume_id': 12, - 'status': 'available', - 'volume_size': 100, - 'created_at': None, - 'display_name': 'Default name', - 'display_description': 'Default description', - } - - -def stub_snapshot_create(self, context, volume_id, name, description): - global _last_param - snapshot = _get_default_snapshot_param() - snapshot['volume_id'] = volume_id - snapshot['display_name'] = name - snapshot['display_description'] = description - - LOG.debug(_("_create: %s"), snapshot) - _last_param = snapshot - return snapshot - - -def stub_snapshot_delete(self, context, snapshot): - global _last_param - _last_param = snapshot - - LOG.debug(_("_delete: %s"), locals()) - if snapshot['id'] != '123': - raise exception.NotFound - - -def stub_snapshot_get(self, context, snapshot_id): - global _last_param - _last_param = dict(snapshot_id=snapshot_id) - - LOG.debug(_("_get: %s"), locals()) - if snapshot_id != '123': - raise exception.NotFound - - param = _get_default_snapshot_param() - param['id'] = snapshot_id - return param - - -def stub_snapshot_get_all(self, context): - LOG.debug(_("_get_all: %s"), locals()) - param = _get_default_snapshot_param() - param['id'] = 123 - return [param] - class SnapshotApiTest(test.TestCase): def setUp(self): @@ -90,14 +31,14 @@ class SnapshotApiTest(test.TestCase): fakes.stub_out_networking(self.stubs) fakes.stub_out_rate_limiting(self.stubs) self.stubs.Set(cinder.API, "create_snapshot", - stub_snapshot_create) + fakes.stub_snapshot_create) self.stubs.Set(cinder.API, "create_snapshot_force", - stub_snapshot_create) + fakes.stub_snapshot_create) self.stubs.Set(cinder.API, "delete_snapshot", - stub_snapshot_delete) - self.stubs.Set(cinder.API, "get_snapshot", stub_snapshot_get) + fakes.stub_snapshot_delete) + self.stubs.Set(cinder.API, "get_snapshot", fakes.stub_snapshot_get) self.stubs.Set(cinder.API, "get_all_snapshots", - stub_snapshot_get_all) + fakes.stub_snapshot_get_all) self.stubs.Set(cinder.API, "get", fakes.stub_volume_get) self.flags( osapi_compute_extension=[ @@ -108,9 +49,6 @@ class SnapshotApiTest(test.TestCase): self.app = fakes.wsgi_app(init_only=('os-snapshots',)) def test_snapshot_create(self): - global _last_param - _last_param = {} - snapshot = {"volume_id": 12, "force": False, "display_name": "Snapshot Test Name", @@ -122,16 +60,8 @@ class SnapshotApiTest(test.TestCase): req.headers['content-type'] = 'application/json' resp = req.get_response(self.app) - LOG.debug(_("test_snapshot_create: param=%s"), _last_param) self.assertEqual(resp.status_int, 200) - - # Compare if parameters were correctly passed to stub - self.assertEqual(_last_param['display_name'], "Snapshot Test Name") - self.assertEqual(_last_param['display_description'], - "Snapshot Test Desc") - resp_dict = jsonutils.loads(resp.body) - LOG.debug(_("test_snapshot_create: resp_dict=%s"), resp_dict) self.assertTrue('snapshot' in resp_dict) self.assertEqual(resp_dict['snapshot']['displayName'], snapshot['display_name']) @@ -139,9 +69,6 @@ class SnapshotApiTest(test.TestCase): snapshot['display_description']) def test_snapshot_create_force(self): - global _last_param - _last_param = {} - snapshot = {"volume_id": 12, "force": True, "display_name": "Snapshot Test Name", @@ -153,16 +80,9 @@ class SnapshotApiTest(test.TestCase): req.headers['content-type'] = 'application/json' resp = req.get_response(self.app) - LOG.debug(_("test_snapshot_create_force: param=%s"), _last_param) self.assertEqual(resp.status_int, 200) - # Compare if parameters were correctly passed to stub - self.assertEqual(_last_param['display_name'], "Snapshot Test Name") - self.assertEqual(_last_param['display_description'], - "Snapshot Test Desc") - resp_dict = jsonutils.loads(resp.body) - LOG.debug(_("test_snapshot_create_force: resp_dict=%s"), resp_dict) self.assertTrue('snapshot' in resp_dict) self.assertEqual(resp_dict['snapshot']['displayName'], snapshot['display_name']) @@ -179,60 +99,41 @@ class SnapshotApiTest(test.TestCase): req.headers['content-type'] = 'application/json' resp = req.get_response(self.app) - LOG.debug(_("test_snapshot_create_force: param=%s"), _last_param) self.assertEqual(resp.status_int, 400) def test_snapshot_delete(self): - global _last_param - _last_param = {} - snapshot_id = 123 req = webob.Request.blank('/v2/fake/os-snapshots/%d' % snapshot_id) req.method = 'DELETE' resp = req.get_response(self.app) self.assertEqual(resp.status_int, 202) - self.assertEqual(str(_last_param['id']), str(snapshot_id)) def test_snapshot_delete_invalid_id(self): - global _last_param - _last_param = {} - - snapshot_id = 234 + snapshot_id = -1 req = webob.Request.blank('/v2/fake/os-snapshots/%d' % snapshot_id) req.method = 'DELETE' resp = req.get_response(self.app) self.assertEqual(resp.status_int, 404) - self.assertEqual(str(_last_param['snapshot_id']), str(snapshot_id)) def test_snapshot_show(self): - global _last_param - _last_param = {} - snapshot_id = 123 req = webob.Request.blank('/v2/fake/os-snapshots/%d' % snapshot_id) req.method = 'GET' resp = req.get_response(self.app) - LOG.debug(_("test_snapshot_show: resp=%s"), resp) self.assertEqual(resp.status_int, 200) - self.assertEqual(str(_last_param['snapshot_id']), str(snapshot_id)) - resp_dict = jsonutils.loads(resp.body) self.assertTrue('snapshot' in resp_dict) self.assertEqual(resp_dict['snapshot']['id'], str(snapshot_id)) def test_snapshot_show_invalid_id(self): - global _last_param - _last_param = {} - - snapshot_id = 234 + snapshot_id = -1 req = webob.Request.blank('/v2/fake/os-snapshots/%d' % snapshot_id) req.method = 'GET' resp = req.get_response(self.app) self.assertEqual(resp.status_int, 404) - self.assertEqual(str(_last_param['snapshot_id']), str(snapshot_id)) def test_snapshot_detail(self): req = webob.Request.blank('/v2/fake/os-snapshots/detail') @@ -241,13 +142,12 @@ class SnapshotApiTest(test.TestCase): self.assertEqual(resp.status_int, 200) resp_dict = jsonutils.loads(resp.body) - LOG.debug(_("test_snapshot_detail: resp_dict=%s"), resp_dict) self.assertTrue('snapshots' in resp_dict) resp_snapshots = resp_dict['snapshots'] - self.assertEqual(len(resp_snapshots), 1) + self.assertEqual(len(resp_snapshots), 3) resp_snapshot = resp_snapshots.pop() - self.assertEqual(resp_snapshot['id'], 123) + self.assertEqual(resp_snapshot['id'], 102) class SnapshotSerializerTest(test.TestCase): diff --git a/nova/tests/api/openstack/fakes.py b/nova/tests/api/openstack/fakes.py index beb99a3f5..7c4bed46c 100644 --- a/nova/tests/api/openstack/fakes.py +++ b/nova/tests/api/openstack/fakes.py @@ -597,11 +597,23 @@ def stub_snapshot(id, **kwargs): return snapshot -def stub_snapshot_get_all(self): +def stub_snapshot_create(self, context, volume_id, name, description): + return stub_snapshot(100, volume_id=volume_id, display_name=name, + display_description=description) + + +def stub_snapshot_delete(self, context, snapshot): + if snapshot['id'] == '-1': + raise exc.NotFound + + +def stub_snapshot_get(self, context, snapshot_id): + if snapshot_id == '-1': + raise exc.NotFound + return stub_snapshot(snapshot_id) + + +def stub_snapshot_get_all(self, context): return [stub_snapshot(100, project_id='fake'), stub_snapshot(101, project_id='superfake'), stub_snapshot(102, project_id='superduperfake')] - - -def stub_snapshot_get_all_by_project(self, context): - return [stub_snapshot(1)] -- cgit