From 715435c816b51b6ec8d38453326eecd35c339fd9 Mon Sep 17 00:00:00 2001 From: Rick Harris Date: Tue, 14 May 2013 15:23:55 +0000 Subject: Use strict=True instead of `is_valid_boolstr` Oslo's `bool_from_string` learned the `strict` keyword which allows callers to detect invalid boolean values, so we can use that instead of having a new Nova-specific function. Change-Id: I61bfa4029897c7304bd54d6cdae9f9a9bc4c1f78 --- .../api/openstack/compute/contrib/test_volumes.py | 36 +++++++++++++++ nova/tests/compute/test_compute.py | 53 ++++++++++++++++++++++ nova/tests/test_utils.py | 13 ------ 3 files changed, 89 insertions(+), 13 deletions(-) (limited to 'nova/tests') diff --git a/nova/tests/api/openstack/compute/contrib/test_volumes.py b/nova/tests/api/openstack/compute/contrib/test_volumes.py index d1d7210f0..617b60311 100644 --- a/nova/tests/api/openstack/compute/contrib/test_volumes.py +++ b/nova/tests/api/openstack/compute/contrib/test_volumes.py @@ -81,6 +81,16 @@ def fake_detach_volume(self, context, instance, volume): return() +def fake_create_snapshot(self, context, volume, name, description): + return({'id': 123, + 'volume_id': 'fakeVolId', + 'status': 'available', + 'volume_size': 123, + 'created_at': '2013-01-01 00:00:01', + 'display_name': 'myVolumeName', + 'display_description': 'myVolumeDescription'}) + + def fake_get_instance_bdms(self, context, instance): return([{'id': 1, 'instance_uuid': instance['uuid'], @@ -668,3 +678,29 @@ class UnprocessableSnapshotTestCase(CommonUnprocessableEntityTestCase, resource = 'os-snapshots' entity_name = 'snapshot' controller_cls = volumes.SnapshotController + + +class CreateSnapshotTestCase(test.TestCase): + def setUp(self): + super(CreateSnapshotTestCase, self).setUp() + self.controller = volumes.SnapshotController() + self.stubs.Set(cinder.API, 'get', fake_get_volume) + self.stubs.Set(cinder.API, 'create_snapshot_force', + fake_create_snapshot) + self.stubs.Set(cinder.API, 'create_snapshot', fake_create_snapshot) + self.req = fakes.HTTPRequest.blank('/v2/fake/os-snapshots') + self.req.method = 'POST' + self.body = {'snapshot': {'volume_id': 1}} + + def test_force_true(self): + self.body['snapshot']['force'] = 'True' + self.controller.create(self.req, body=self.body) + + def test_force_false(self): + self.body['snapshot']['force'] = 'f' + self.controller.create(self.req, body=self.body) + + def test_force_invalid(self): + self.body['snapshot']['force'] = 'foo' + self.assertRaises(exception.InvalidParameterValue, + self.controller.create, self.req, body=self.body) diff --git a/nova/tests/compute/test_compute.py b/nova/tests/compute/test_compute.py index 094b348a2..22dd7bd2f 100644 --- a/nova/tests/compute/test_compute.py +++ b/nova/tests/compute/test_compute.py @@ -8851,3 +8851,56 @@ class GetAndCheckImageMetadataTest(test.TestCase): self.assertRaises(exception.ImageTooLarge, self.compute._get_and_check_image_metadata, self.context, instance) + + +class CheckConfigDriveTestCase(test.TestCase): + # NOTE(sirp): `TestCase` is far too heavyweight for this test, this should + # probably derive from a `test.FastTestCase` that omits DB and env + # handling + def setUp(self): + super(CheckConfigDriveTestCase, self).setUp() + self.compute_api = compute.API() + self.context = context.RequestContext( + 'fake_user_id', 'fake_project_id') + + self.called = called = {'show': False} + + def fake_get_remote_image_service(context, image_id): + class FakeGlance(object): + def show(self, context, image_id): + called['show'] = True + + return FakeGlance(), image_id + + self.stubs.Set(glance, 'get_remote_image_service', + fake_get_remote_image_service) + + def tearDown(self): + self.stubs.UnsetAll() + super(CheckConfigDriveTestCase, self).tearDown() + + def assertCheck(self, expected, config_drive): + self.assertEqual(expected, + self.compute_api._check_config_drive( + self.context, config_drive)) + + def test_value_is_none(self): + self.assertFalse(self.called['show']) + self.assertCheck((None, None), None) + self.assertFalse(self.called['show']) + + def test_value_is_bool_like_string(self): + self.assertCheck((None, 'True'), 'True') + self.assertCheck((None, 'yes'), 'yes') + + def test_bool_string_or_id(self): + # NOTE(sirp): '0' and '1' could be a bool value or an ID. Since there + # are many other ways to specify bools (e.g. 't', 'f'), it's better to + # treat as an ID. + self.assertCheck((0, None), 0) + self.assertCheck((1, None), 1) + self.assertCheck(('0', None), '0') + self.assertCheck(('1', None), '1') + + def test_value_is_image_id(self): + self.assertCheck((2, None), 2) diff --git a/nova/tests/test_utils.py b/nova/tests/test_utils.py index 39db03f3e..a2abedda8 100644 --- a/nova/tests/test_utils.py +++ b/nova/tests/test_utils.py @@ -360,19 +360,6 @@ class GenericUtilsTestCase(test.TestCase): h2 = hashlib.sha1(data).hexdigest() self.assertEquals(h1, h2) - def test_is_valid_boolstr(self): - self.assertTrue(utils.is_valid_boolstr('true')) - self.assertTrue(utils.is_valid_boolstr('false')) - self.assertTrue(utils.is_valid_boolstr('yes')) - self.assertTrue(utils.is_valid_boolstr('no')) - self.assertTrue(utils.is_valid_boolstr('y')) - self.assertTrue(utils.is_valid_boolstr('n')) - self.assertTrue(utils.is_valid_boolstr('1')) - self.assertTrue(utils.is_valid_boolstr('0')) - - self.assertFalse(utils.is_valid_boolstr('maybe')) - self.assertFalse(utils.is_valid_boolstr('only on tuesdays')) - def test_is_valid_ipv4(self): self.assertTrue(utils.is_valid_ipv4('127.0.0.1')) self.assertFalse(utils.is_valid_ipv4('::1')) -- cgit