def test_retype_volume_not_capable_to_replica(self): elevated = context.get_admin_context() db.volume_type_create(elevated, {'name': 'old', 'extra_specs': {}}) old_vol_type = db.volume_type_get_by_name(elevated, 'old') new_extra_specs = {'replication_enabled': '<is> True'} db.volume_type_create(elevated, { 'name': 'new', 'extra_specs': new_extra_specs }) new_vol_type = db.volume_type_get_by_name(elevated, 'new') volume = tests_utils.create_volume(self.context, size=1, host=CONF.host, status='available', volume_type_id=old_vol_type['id'], replication_status='not-capable') host_obj = {'host': 'newhost', 'capabilities': {}} with mock.patch.object(self.volume, 'migrate_volume') as migrate_volume: migrate_volume.return_value = True self.volume.retype(self.context, volume, new_vol_type['id'], host_obj, migration_policy='on-demand')
def test_create_encryption_type_exists(self): self.stubs.Set(db, "volume_type_encryption_get", return_volume_type_encryption) volume_type = {"id": "fake_type_id", "name": "fake_type"} db.volume_type_create(context.get_admin_context(), volume_type) body = { "encryption": { "cipher": "cipher", "control_location": "front-end", "key_size": 128, "provider": "fake_provider", "volume_type_id": volume_type["id"], } } # Try to create encryption specs for a volume type # that already has them. res = self._get_response( volume_type, req_method="POST", req_body=json.dumps(body), req_headers="application/json" ) res_dict = json.loads(res.body) expected = { "badRequest": {"code": 400, "message": ("Volume type encryption for type " "fake_type_id already exists.")} } self.assertEqual(expected, res_dict) db.volume_type_destroy(context.get_admin_context(), volume_type["id"])
def test_create_encryption_type_exists(self): self.stubs.Set(db, 'volume_type_encryption_get', return_volume_type_encryption) volume_type = { 'id': 'fake_type_id', 'name': 'fake_type', } db.volume_type_create(context.get_admin_context(), volume_type) body = {"encryption": {'cipher': 'cipher', 'control_location': 'front-end', 'key_size': 128, 'provider': 'fake_provider', 'volume_type_id': volume_type['id']}} # Try to create encryption specs for a volume type # that already has them. res = self._get_response(volume_type, req_method='POST', req_body=json.dumps(body), req_headers='application/json') res_dict = json.loads(res.body) expected = { 'badRequest': { 'code': 400, 'message': ('Volume type encryption for type ' 'fake_type_id already exists.') } } self.assertEqual(expected, res_dict) db.volume_type_destroy(context.get_admin_context(), volume_type['id'])
def test_volume_create_with_type(self): vol_type = CONF.default_volume_type db.volume_type_create(context.get_admin_context(), dict(name=vol_type, extra_specs={})) db_vol_type = db.volume_type_get_by_name(context.get_admin_context(), vol_type) vol = {"size": 100, "display_name": "Volume Test Name", "display_description": "Volume Test Desc", "availability_zone": "zone1:host1", "volume_type": "FakeTypeName"} body = {"volume": vol} req = fakes.HTTPRequest.blank('/v1/volumes') # Raise 404 when type name isn't valid self.assertRaises(webob.exc.HTTPNotFound, self.controller.create, req, body) # Use correct volume type name vol.update(dict(volume_type=CONF.default_volume_type)) body.update(dict(volume=vol)) res_dict = self.controller.create(req, body) self.assertIn('id', res_dict['volume']) self.assertEqual(len(res_dict), 1) self.assertEqual(res_dict['volume']['volume_type'], db_vol_type['name']) # Use correct volume type id vol.update(dict(volume_type=db_vol_type['id'])) body.update(dict(volume=vol)) res_dict = self.controller.create(req, body) self.assertIn('id', res_dict['volume']) self.assertEqual(len(res_dict), 1) self.assertEqual(res_dict['volume']['volume_type'], db_vol_type['name'])
def setUp(self): super(ImageVolumeTestCases, self).setUp() db.volume_type_create(self.context, v2_fakes.fake_default_type_get( fake.VOLUME_TYPE2_ID)) self.vol_type = db.volume_type_get_by_name(self.context, 'vol_type_name')
def test_volume_create_with_type(self): vol_type = FLAGS.default_volume_type db.volume_type_create(context.get_admin_context(), dict(name=vol_type, extra_specs={})) db_vol_type = db.volume_type_get_by_name(context.get_admin_context(), vol_type) vol = { "size": 100, "name": "Volume Test Name", "display_description": "Volume Test Desc", "availability_zone": "zone1:host1", "volume_type": db_vol_type["name"], } body = {"volume": vol} req = fakes.HTTPRequest.blank("/v2/volumes") res_dict = self.controller.create(req, body) volume_id = res_dict["volume"]["id"] self.assertEquals(len(res_dict), 1) self.stubs.Set( volume_api.API, "get_all", lambda *args, **kwargs: [stubs.stub_volume(volume_id, volume_type={"name": vol_type})], ) req = fakes.HTTPRequest.blank("/v2/volumes/detail") res_dict = self.controller.detail(req)
def setUp(self, *args, **kwargs): super(SnapshotTestCase, self).setUp() db.volume_type_create(self.context, v2_fakes.fake_default_type_get( fake.VOLUME_TYPE2_ID)) self.vol_type = db.volume_type_get_by_name(self.context, 'vol_type_name')
def test_create_consistencygroup_json(self, mock_validate): group_id = "1" # Create volume type vol_type = 'test' db.volume_type_create(context.get_admin_context(), {'name': vol_type, 'extra_specs': {}}) body = {"consistencygroup": {"name": "cg1", "volume_types": vol_type, "description": "Consistency Group 1", }} req = webob.Request.blank('/v2/fake/consistencygroups') req.method = 'POST' req.headers['Content-Type'] = 'application/json' req.body = json.dumps(body) res = req.get_response(fakes.wsgi_app()) res_dict = json.loads(res.body) self.assertEqual(202, res.status_int) self.assertIn('id', res_dict['consistencygroup']) self.assertTrue(mock_validate.called) group_id = res_dict['consistencygroup']['id'] cg = objects.ConsistencyGroup.get_by_id(context.get_admin_context(), group_id) cg.destroy()
def test_create_consistencygroup_json(self): group_id = "1" # Create volume type vol_type = 'test' db.volume_type_create(context.get_admin_context(), { 'name': vol_type, 'extra_specs': {} }) body = { "consistencygroup": { "name": "cg1", "volume_types": vol_type, "description": "Consistency Group 1", } } req = webob.Request.blank('/v2/fake/consistencygroups') req.method = 'POST' req.headers['Content-Type'] = 'application/json' req.body = json.dumps(body) res = req.get_response(fakes.wsgi_app()) res_dict = json.loads(res.body) self.assertEqual(202, res.status_int) self.assertIn('id', res_dict['consistencygroup']) db.consistencygroup_destroy(context.get_admin_context(), group_id)
def create(context, name, extra_specs={}): """Creates volume types.""" try: db.volume_type_create(context, dict(name=name, extra_specs=extra_specs)) except exception.DBError, e: LOG.exception(_('DB error: %s') % e) raise exception.VolumeTypeCreateFailed(name=name, extra_specs=extra_specs)
def _create_type_and_encryption(self, volume_type, body=None): if body is None: body = {"encryption": stub_volume_type_encryption()} db.volume_type_create(context.get_admin_context(), volume_type) return self._get_response(volume_type, req_method='POST', req_body=json.dumps(body), req_headers='application/json')
def test_delete(self): volume_type = { 'id': 'fake_type_id', 'name': 'fake_type', } db.volume_type_create(context.get_admin_context(), volume_type) # Test that before create, there's nothing with a get res = self._get_response(volume_type, req_method='GET', req_headers='application/json', url='/v2/fake/types/%s/encryption') self.assertEqual(200, res.status_code) res_dict = json.loads(res.body) self.assertEqual({}, res_dict) body = { "encryption": { 'cipher': 'cipher', 'key_size': 128, 'control_location': 'front-end', 'provider': 'fake_provider', 'volume_type_id': volume_type['id'] } } # Create, and test that get returns something res = self._get_response(volume_type, req_method='POST', req_body=json.dumps(body), req_headers='application/json') res_dict = json.loads(res.body) res = self._get_response(volume_type, req_method='GET', req_headers='application/json', url='/v2/fake/types/%s/encryption') self.assertEqual(200, res.status_code) res_dict = json.loads(res.body) self.assertEqual(volume_type['id'], res_dict['volume_type_id']) # Delete, and test that get returns nothing res = self._get_response(volume_type, req_method='DELETE', req_headers='application/json', url='/v2/fake/types/%s/encryption/provider') self.assertEqual(202, res.status_code) self.assertEqual(0, len(res.body)) res = self._get_response(volume_type, req_method='GET', req_headers='application/json', url='/v2/fake/types/%s/encryption') self.assertEqual(200, res.status_code) res_dict = json.loads(res.body) self.assertEqual({}, res_dict) db.volume_type_destroy(context.get_admin_context(), volume_type['id'])
def _create(self, cipher, control_location, key_size, provider): volume_type = { 'id': 'fake_type_id', 'name': 'fake_type', } db.volume_type_create(context.get_admin_context(), volume_type) body = { "encryption": { 'cipher': cipher, 'control_location': control_location, 'key_size': key_size, 'provider': provider, 'volume_type_id': volume_type['id'] } } self.assertEqual(len(test_notifier.NOTIFICATIONS), 0) res = self._get_response(volume_type) res_dict = json.loads(res.body) self.assertEqual(200, res.status_code) # Confirm that volume type has no encryption information # before create. self.assertEqual('{}', res.body) # Create encryption specs for the volume type # with the defined body. res = self._get_response(volume_type, req_method='POST', req_body=json.dumps(body), req_headers='application/json') res_dict = json.loads(res.body) self.assertEqual(len(test_notifier.NOTIFICATIONS), 1) # check response self.assertIn('encryption', res_dict) self.assertEqual(cipher, res_dict['encryption']['cipher']) self.assertEqual(control_location, res_dict['encryption']['control_location']) self.assertEqual(key_size, res_dict['encryption']['key_size']) self.assertEqual(provider, res_dict['encryption']['provider']) self.assertEqual(volume_type['id'], res_dict['encryption']['volume_type_id']) # check database encryption = db.volume_type_encryption_get(context.get_admin_context(), volume_type['id']) self.assertIsNotNone(encryption) self.assertEqual(cipher, encryption['cipher']) self.assertEqual(key_size, encryption['key_size']) self.assertEqual(provider, encryption['provider']) self.assertEqual(volume_type['id'], encryption['volume_type_id']) db.volume_type_destroy(context.get_admin_context(), volume_type['id'])
def test_create_volume_with_volume_type(self): """Test volume creation with default volume type.""" def fake_reserve(context, expire=None, project_id=None, **deltas): return ["RESERVATION"] def fake_commit(context, reservations, project_id=None): pass def fake_rollback(context, reservations, project_id=None): pass self.stubs.Set(QUOTAS, "reserve", fake_reserve) self.stubs.Set(QUOTAS, "commit", fake_commit) self.stubs.Set(QUOTAS, "rollback", fake_rollback) volume_api = cinder.volume.api.API() # Create volume with default volume type while default # volume type doesn't exist, volume_type_id should be NULL volume = volume_api.create(self.context, 1, 'name', 'description') self.assertEquals(volume['volume_type_id'], None) # Create default volume type vol_type = fake_flags.def_vol_type db.volume_type_create(context.get_admin_context(), dict(name=vol_type, extra_specs={})) db_vol_type = db.volume_type_get_by_name(context.get_admin_context(), vol_type) # Create volume with default volume type volume = volume_api.create(self.context, 1, 'name', 'description') self.assertEquals(volume['volume_type_id'], db_vol_type.get('id')) # Create volume with specific volume type vol_type = 'test' db.volume_type_create(context.get_admin_context(), dict(name=vol_type, extra_specs={})) db_vol_type = db.volume_type_get_by_name(context.get_admin_context(), vol_type) volume = volume_api.create(self.context, 1, 'name', 'description', volume_type=db_vol_type) self.assertEquals(volume['volume_type_id'], db_vol_type.get('id'))
def _encryption_create_bad_body(self, body, msg="Create body is not valid."): volume_type = {"id": "fake_type_id", "name": "fake_type"} db.volume_type_create(context.get_admin_context(), volume_type) res = self._get_response( volume_type, req_method="POST", req_body=json.dumps(body), req_headers="application/json" ) res_dict = json.loads(res.body) expected = {"badRequest": {"code": 400, "message": (msg)}} self.assertEqual(expected, res_dict) db.volume_type_destroy(context.get_admin_context(), volume_type["id"])
def test_show_provider(self): self.stubs.Set(db, "volume_type_encryption_get", return_volume_type_encryption) volume_type = {"id": "fake_type_id", "name": "fake_type"} db.volume_type_create(context.get_admin_context(), volume_type) res = self._get_response(volume_type, url="/v2/fake/types/%s/encryption/provider") res_dict = json.loads(res.body) self.assertEqual(200, res.status_code) self.assertEqual("fake_provider", res_dict["provider"]) db.volume_type_destroy(context.get_admin_context(), volume_type["id"])
def _create(self, cipher, control_location, key_size, provider): volume_type = self._default_volume_type db.volume_type_create(context.get_admin_context(), volume_type) body = { "encryption": { 'cipher': cipher, 'control_location': control_location, 'key_size': key_size, 'provider': provider, 'volume_type_id': volume_type['id'] } } self.assertEqual(0, len(self.notifier.notifications)) res = self._get_response(volume_type) res_dict = jsonutils.loads(res.body) self.assertEqual(http_client.OK, res.status_code) # Confirm that volume type has no encryption information # before create. self.assertEqual(b'{}', res.body) # Create encryption specs for the volume type # with the defined body. res = self._get_response(volume_type, req_method='POST', req_body=jsonutils.dump_as_bytes(body), req_headers='application/json') res_dict = jsonutils.loads(res.body) self.assertEqual(1, len(self.notifier.notifications)) # check response self.assertIn('encryption', res_dict) self.assertEqual(cipher, res_dict['encryption']['cipher']) self.assertEqual(control_location, res_dict['encryption']['control_location']) self.assertEqual(key_size, res_dict['encryption']['key_size']) self.assertEqual(provider, res_dict['encryption']['provider']) self.assertEqual(volume_type['id'], res_dict['encryption']['volume_type_id']) # check database encryption = db.volume_type_encryption_get(context.get_admin_context(), volume_type['id']) self.assertIsNotNone(encryption) self.assertEqual(cipher, encryption['cipher']) self.assertEqual(key_size, encryption['key_size']) self.assertEqual(provider, encryption['provider']) self.assertEqual(volume_type['id'], encryption['volume_type_id']) db.volume_type_destroy(context.get_admin_context(), volume_type['id'])
def test_delete(self): volume_type = {"id": "fake_type_id", "name": "fake_type"} db.volume_type_create(context.get_admin_context(), volume_type) # Test that before create, there's nothing with a get res = self._get_response( volume_type, req_method="GET", req_headers="application/json", url="/v2/fake/types/%s/encryption" ) self.assertEqual(200, res.status_code) res_dict = json.loads(res.body) self.assertEqual({}, res_dict) body = { "encryption": { "cipher": "cipher", "key_size": 128, "control_location": "front-end", "provider": "fake_provider", "volume_type_id": volume_type["id"], } } # Create, and test that get returns something res = self._get_response( volume_type, req_method="POST", req_body=json.dumps(body), req_headers="application/json" ) res_dict = json.loads(res.body) res = self._get_response( volume_type, req_method="GET", req_headers="application/json", url="/v2/fake/types/%s/encryption" ) self.assertEqual(200, res.status_code) res_dict = json.loads(res.body) self.assertEqual(volume_type["id"], res_dict["volume_type_id"]) # Delete, and test that get returns nothing res = self._get_response( volume_type, req_method="DELETE", req_headers="application/json", url="/v2/fake/types/%s/encryption/provider", ) self.assertEqual(202, res.status_code) self.assertEqual(0, len(res.body)) res = self._get_response( volume_type, req_method="GET", req_headers="application/json", url="/v2/fake/types/%s/encryption" ) self.assertEqual(200, res.status_code) res_dict = json.loads(res.body) self.assertEqual({}, res_dict) db.volume_type_destroy(context.get_admin_context(), volume_type["id"])
def test_show_item_not_found(self): self.stubs.Set(db, "volume_type_encryption_get", return_volume_type_encryption) volume_type = {"id": "fake_type_id", "name": "fake_type"} db.volume_type_create(context.get_admin_context(), volume_type) res = self._get_response(volume_type, url="/v2/fake/types/%s/encryption/fake") res_dict = json.loads(res.body) self.assertEqual(404, res.status_code) expected = {"itemNotFound": {"code": 404, "message": ("The resource could not be found.")}} self.assertEqual(expected, res_dict) db.volume_type_destroy(context.get_admin_context(), volume_type["id"])
def _encryption_create_bad_body(self, body): volume_type = self._default_volume_type db.volume_type_create(context.get_admin_context(), volume_type) res = self._get_response(volume_type, req_method='POST', req_body=jsonutils.dump_as_bytes(body), req_headers='application/json') res_dict = jsonutils.loads(res.body) self.assertEqual(HTTPStatus.BAD_REQUEST, res_dict['badRequest']['code']) db.volume_type_destroy(context.get_admin_context(), volume_type['id'])
def _encryption_create_bad_body(self, body): volume_type = self._default_volume_type db.volume_type_create(context.get_admin_context(), volume_type) res = self._get_response(volume_type, req_method='POST', req_body=jsonutils.dump_as_bytes(body), req_headers='application/json') res_dict = jsonutils.loads(res.body) self.assertEqual(http_client.BAD_REQUEST, res_dict['badRequest']['code']) db.volume_type_destroy(context.get_admin_context(), volume_type['id'])
def _create(self, cipher, control_location, key_size, provider): volume_type = { 'id': 'fake_type_id', 'name': 'fake_type', } db.volume_type_create(context.get_admin_context(), volume_type) body = {"encryption": {'cipher': cipher, 'control_location': control_location, 'key_size': key_size, 'provider': provider, 'volume_type_id': volume_type['id']}} self.assertEqual(len(test_notifier.NOTIFICATIONS), 0) res = self._get_response(volume_type) res_dict = json.loads(res.body) self.assertEqual(200, res.status_code) # Confirm that volume type has no encryption information # before create. self.assertEqual('{}', res.body) # Create encryption specs for the volume type # with the defined body. res = self._get_response(volume_type, req_method='POST', req_body=json.dumps(body), req_headers='application/json') res_dict = json.loads(res.body) self.assertEqual(len(test_notifier.NOTIFICATIONS), 1) # check response self.assertIn('encryption', res_dict) self.assertEqual(cipher, res_dict['encryption']['cipher']) self.assertEqual(control_location, res_dict['encryption']['control_location']) self.assertEqual(key_size, res_dict['encryption']['key_size']) self.assertEqual(provider, res_dict['encryption']['provider']) self.assertEqual(volume_type['id'], res_dict['encryption']['volume_type_id']) # check database encryption = db.volume_type_encryption_get(context.get_admin_context(), volume_type['id']) self.assertIsNotNone(encryption) self.assertEqual(cipher, encryption['cipher']) self.assertEqual(key_size, encryption['key_size']) self.assertEqual(provider, encryption['provider']) self.assertEqual(volume_type['id'], encryption['volume_type_id']) db.volume_type_destroy(context.get_admin_context(), volume_type['id'])
def test_index(self): self.stubs.Set(db, "volume_type_encryption_get", return_volume_type_encryption) volume_type = {"id": "fake_type_id", "name": "fake_type"} db.volume_type_create(context.get_admin_context(), volume_type) res = self._get_response(volume_type) self.assertEqual(200, res.status_code) res_dict = json.loads(res.body) expected = stub_volume_type_encryption() self.assertEqual(expected, res_dict) db.volume_type_destroy(context.get_admin_context(), volume_type["id"])
def test_restore_encrypted_vol_to_none_type_source_type_available(self): fields = ["encryption_key_id"] container = {} db.volume_type_create(self.ctxt, {"id": "enc_vol_type_id", "name": "enc_vol_type"}) enc_vol_id = self._create_encrypted_volume_db_entry(str(uuid.uuid4()), "enc_vol_type_id", True) undef_vol_id = self._create_encrypted_volume_db_entry(str(uuid.uuid4()), None, False) self.bak_meta_api._save_vol_base_meta(container, enc_vol_id) self.bak_meta_api._restore_vol_base_meta( container[self.bak_meta_api.TYPE_TAG_VOL_BASE_META], undef_vol_id, fields ) self.assertEqual( db.volume_get(self.ctxt, undef_vol_id)["volume_type_id"], db.volume_get(self.ctxt, enc_vol_id)["volume_type_id"], )
def setUp(self): super(VolumeTypeExtraSpecsTestCase, self).setUp() self.context = context.get_admin_context() self.vol_type1 = dict(name="TEST: Regular volume test") self.vol_type1_specs = dict(vol_extra1="value1", vol_extra2="value2", vol_extra3=3) self.vol_type1["extra_specs"] = self.vol_type1_specs ref = db.volume_type_create(self.context, self.vol_type1) self.volume_type1_id = ref.id for k, v in self.vol_type1_specs.iteritems(): self.vol_type1_specs[k] = str(v) self.vol_type2_noextra = dict(name="TEST: Volume type without extra") ref = db.volume_type_create(self.context, self.vol_type2_noextra) self.vol_type2_id = ref.id
def _setup_volume_types(): spec_dict = _create_min_max_size_dict(2, 4) sized_vol_type_dict = { 'name': 'limit_type', 'extra_specs': spec_dict } db.volume_type_create(self.context, sized_vol_type_dict) self.sized_vol_type = db.volume_type_get_by_name( self.context, sized_vol_type_dict['name']) unsized_vol_type_dict = {'name': 'unsized_type', 'extra_specs': {}} db.volume_type_create(context.get_admin_context(), unsized_vol_type_dict) self.unsized_vol_type = db.volume_type_get_by_name( self.context, unsized_vol_type_dict['name'])
def test_delete(self): volume_type = { 'id': 'fake_type_id', 'name': 'fake_type', } db.volume_type_create(context.get_admin_context(), volume_type) # Test that before create, there's nothing with a get res = self._get_response(volume_type, req_method='GET', req_headers='application/json', url='/v2/fake/types/%s/encryption') self.assertEqual(200, res.status_code) res_dict = json.loads(res.body) self.assertEqual({}, res_dict) body = {"encryption": {'cipher': 'cipher', 'key_size': 128, 'control_location': 'front-end', 'provider': 'fake_provider', 'volume_type_id': volume_type['id']}} # Create, and test that get returns something res = self._get_response(volume_type, req_method='POST', req_body=json.dumps(body), req_headers='application/json') res_dict = json.loads(res.body) res = self._get_response(volume_type, req_method='GET', req_headers='application/json', url='/v2/fake/types/%s/encryption') self.assertEqual(200, res.status_code) res_dict = json.loads(res.body) self.assertEqual(volume_type['id'], res_dict['volume_type_id']) # Delete, and test that get returns nothing res = self._get_response(volume_type, req_method='DELETE', req_headers='application/json', url='/v2/fake/types/%s/encryption/provider') self.assertEqual(202, res.status_code) self.assertEqual(0, len(res.body)) res = self._get_response(volume_type, req_method='GET', req_headers='application/json', url='/v2/fake/types/%s/encryption') self.assertEqual(200, res.status_code) res_dict = json.loads(res.body) self.assertEqual({}, res_dict) db.volume_type_destroy(context.get_admin_context(), volume_type['id'])
def _create(self, cipher, control_location, key_size, provider): volume_type = {"id": "fake_type_id", "name": "fake_type"} db.volume_type_create(context.get_admin_context(), volume_type) body = { "encryption": { "cipher": cipher, "control_location": control_location, "key_size": key_size, "provider": provider, "volume_type_id": volume_type["id"], } } self.assertEqual(len(test_notifier.NOTIFICATIONS), 0) res = self._get_response(volume_type) res_dict = json.loads(res.body) self.assertEqual(200, res.status_code) # Confirm that volume type has no encryption information # before create. self.assertEqual("{}", res.body) # Create encryption specs for the volume type # with the defined body. res = self._get_response( volume_type, req_method="POST", req_body=json.dumps(body), req_headers="application/json" ) res_dict = json.loads(res.body) self.assertEqual(len(test_notifier.NOTIFICATIONS), 1) # check response self.assertIn("encryption", res_dict) self.assertEqual(cipher, res_dict["encryption"]["cipher"]) self.assertEqual(control_location, res_dict["encryption"]["control_location"]) self.assertEqual(key_size, res_dict["encryption"]["key_size"]) self.assertEqual(provider, res_dict["encryption"]["provider"]) self.assertEqual(volume_type["id"], res_dict["encryption"]["volume_type_id"]) # check database encryption = db.volume_type_encryption_get(context.get_admin_context(), volume_type["id"]) self.assertIsNotNone(encryption) self.assertEqual(cipher, encryption["cipher"]) self.assertEqual(key_size, encryption["key_size"]) self.assertEqual(provider, encryption["provider"]) self.assertEqual(volume_type["id"], encryption["volume_type_id"]) db.volume_type_destroy(context.get_admin_context(), volume_type["id"])
def _encryption_create_bad_body(self, body, msg='Create body is not valid.'): volume_type = self._default_volume_type db.volume_type_create(context.get_admin_context(), volume_type) res = self._get_response(volume_type, req_method='POST', req_body=jsonutils.dump_as_bytes(body), req_headers='application/json') res_dict = jsonutils.loads(res.body) expected = {'badRequest': {'code': 400, 'message': (msg)}} self.assertEqual(expected, res_dict) db.volume_type_destroy(context.get_admin_context(), volume_type['id'])
def test_create_volume_with_consistencygroup_invalid_type(self): """Test volume creation with ConsistencyGroup & invalid volume type.""" vol_type = db.volume_type_create( context.get_admin_context(), dict(name=conf_fixture.def_vol_type, extra_specs={})) vol_type = objects.VolumeType.get_by_id(self.context, vol_type.id) cg = objects.ConsistencyGroup(self.context, id=fake.CONSISTENCY_GROUP_ID, name='cg1', volume_type_id=vol_type.id) fake_type = fake_volume.fake_volume_type_obj(self.context, id=fake.VOLUME_TYPE_ID, name='fake') vol_api = cinder.volume.api.API() # Volume type must be provided when creating a volume in a # consistency group. self.assertRaises(exception.InvalidInput, vol_api.create, self.context, 1, 'vol1', 'volume 1', consistencygroup=cg) # Volume type must be valid. self.assertRaises(exception.InvalidInput, vol_api.create, self.context, 1, 'vol1', 'volume 1', volume_type=fake_type, consistencygroup=cg)
def test_volume_create_with_type(self): vol_type = db.volume_type_create( context.get_admin_context(), dict(name=FLAGS.default_volume_type, extra_specs={})) db_vol_type = db.volume_type_get(context.get_admin_context(), vol_type.id) vol = { "size": 100, "name": "Volume Test Name", "description": "Volume Test Desc", "availability_zone": "zone1:host1", "volume_type": db_vol_type['id'], } body = {"volume": vol} req = fakes.HTTPRequest.blank('/v2/volumes') res_dict = self.controller.create(req, body) volume_id = res_dict['volume']['id'] self.assertEquals(len(res_dict), 1) self.stubs.Set( volume_api.API, 'get_all', lambda *args, **kwargs: [stubs.stub_volume(volume_id, volume_type={'name': vol_type})]) req = fakes.HTTPRequest.blank('/v2/volumes/detail') res_dict = self.controller.detail(req)
def test_create_xml(self): volume_type = {"id": "fake_type_id", "name": "fake_type"} db.volume_type_create(context.get_admin_context(), volume_type) ctxt = context.RequestContext("fake", "fake", is_admin=True) req = webob.Request.blank("/v2/fake/types/%s/encryption" % volume_type["id"]) req.method = "POST" req.body = '<encryption provider="test_provider" ' 'cipher="cipher" control_location="front-end" />' req.headers["Content-Type"] = "application/xml" req.headers["Accept"] = "application/xml" res = req.get_response(fakes.wsgi_app(fake_auth_context=ctxt)) self.assertEqual(res.status_int, 200) db.volume_type_destroy(context.get_admin_context(), volume_type["id"])
def test_copy_volume_to_image_instance_deleted(self): # During uploading volume to image if instance is deleted, # volume should be in available status. self.image_meta['id'] = 'a440c04b-79fa-479c-bed1-0b816eaec379' # Creating volume testdata self.volume_attrs['instance_uuid'] = 'b21f957d-a72f-4b93-b5a5-' \ '45b1161abb02' volume_type_id = db.volume_type_create( self.context, { 'name': 'test', 'extra_specs': { 'image_service:store_id': 'fake_store' } }).get('id') self.volume_attrs['volume_type_id'] = volume_type_id db.volume_create(self.context, self.volume_attrs) method = 'volume_update_status_based_on_attachment' with mock.patch.object(db, method, wraps=getattr(db, method)) as mock_update: # Start test self.volume.copy_volume_to_image(self.context, self.volume_id, self.image_meta) # Check 'volume_update_status_after_copy_volume_to_image' # is called 1 time self.assertEqual(1, mock_update.call_count) # Check volume status has changed to available because # instance is deleted volume = db.volume_get(self.context, self.volume_id) self.assertEqual('available', volume['status'])
def test_create_volume_with_group_invalid_type(self): """Test volume creation with group & invalid volume type.""" vol_type = db.volume_type_create( context.get_admin_context(), dict(name=conf_fixture.def_vol_type, extra_specs={}) ) db_vol_type = db.volume_type_get(context.get_admin_context(), vol_type.id) grp = tests_utils.create_group( self.context, availability_zone=CONF.storage_availability_zone, status=fields.GroupStatus.AVAILABLE, volume_type_ids=[db_vol_type['id']], group_type_id=fake.GROUP_TYPE_ID, host=CONF.host) fake_type = { 'id': '9999', 'name': 'fake', } # Volume type must be provided when creating a volume in a # group. self.assertRaises(exception.InvalidInput, self.volume_api.create, self.context, 1, 'vol1', 'volume 1', group=grp) # Volume type must be valid. self.assertRaises(exception.InvalidInput, self.volume_api.create, self.context, 1, 'vol1', 'volume 1', volume_type=fake_type, group=grp)
def test_create_group_json(self, mock_validate): # Create volume types and group type vol_type = 'test' vol_type_id = db.volume_type_create( self.ctxt, {'name': vol_type, 'extra_specs': {}}).get('id') grp_type = 'grp_type' grp_type_id = db.group_type_create( self.ctxt, {'name': grp_type, 'group_specs': {}}).get('id') body = {"group": {"name": "group1", "volume_types": [vol_type_id], "group_type": grp_type_id, "description": "Group 1", }} req = fakes.HTTPRequest.blank('/v3/%s/groups' % fake.PROJECT_ID, version=GROUP_MICRO_VERSION) res_dict = self.controller.create(req, body) self.assertEqual(1, len(res_dict)) self.assertIn('id', res_dict['group']) self.assertTrue(mock_validate.called) group_id = res_dict['group']['id'] objects.Group.get_by_id(self.ctxt, group_id)
def _create_volume_with_type(self, status='available', display_description='displaydesc', **kwargs): self.stubs.UnsetAll() admin_ctxt = context.get_admin_context() vol_type = db.volume_type_create(admin_ctxt, {'name': 'vol_name'}) self.addCleanup(db.volume_type_destroy, admin_ctxt, vol_type.id) volume = utils.create_volume(self.context, volume_type_id=vol_type.id, status=status, display_description=display_description, **kwargs) self.addCleanup(db.volume_destroy, admin_ctxt, volume.id) expected = { 'os-volume_upload_image': { 'id': volume.id, 'updated_at': 'DONTCARE', 'status': 'uploading', 'display_description': 'displaydesc', 'size': 1, 'volume_type': 'DONTCARE', 'image_id': fake.IMAGE_ID, 'container_format': 'bare', 'disk_format': 'raw', 'image_name': 'image_name' } } return volume, expected
def test_create_volume_with_consistencygroup_invalid_type(self): """Test volume creation with ConsistencyGroup & invalid volume type.""" vol_type = db.volume_type_create( context.get_admin_context(), dict(name=conf_fixture.def_vol_type, extra_specs={}) ) db_vol_type = db.volume_type_get(context.get_admin_context(), vol_type.id) cg = { 'id': '1', 'name': 'cg1', 'volume_type_id': db_vol_type['id'], } fake_type = { 'id': '9999', 'name': 'fake', } vol_api = cinder.volume.api.API() # Volume type must be provided when creating a volume in a # consistency group. self.assertRaises(exception.InvalidInput, vol_api.create, self.context, 1, 'vol1', 'volume 1', consistencygroup=cg) # Volume type must be valid. self.assertRaises(exception.InvalidInput, vol_api.create, self.context, 1, 'vol1', 'volume 1', volume_type=fake_type, consistencygroup=cg)
def test_create_group_json(self, use_group_type_name, mock_validate): # Create volume types and group type vol_type = 'test' vol_type_id = db.volume_type_create(self.ctxt, { 'name': vol_type, 'extra_specs': {} }).get('id') grp_type_name = 'test_grp_type' grp_type = db.group_type_create(self.ctxt, { 'name': grp_type_name, 'group_specs': {} }).get('id') if use_group_type_name: grp_type = grp_type_name body = { "group": { "name": "group1", "volume_types": [vol_type_id], "group_type": grp_type, "description": "Group 1", } } req = fakes.HTTPRequest.blank('/v3/%s/groups' % fake.PROJECT_ID, version=GROUP_MICRO_VERSION) res_dict = self.controller.create(req, body) self.assertEqual(1, len(res_dict)) self.assertIn('id', res_dict['group']) self.assertTrue(mock_validate.called) group_id = res_dict['group']['id'] objects.Group.get_by_id(self.ctxt, group_id)
def test_volume_create_with_type(self): vol_type = db.volume_type_create( context.get_admin_context(), dict(name=CONF.default_volume_type, extra_specs={}) ) db_vol_type = db.volume_type_get(context.get_admin_context(), vol_type.id) vol = { "size": 100, "name": "Volume Test Name", "description": "Volume Test Desc", "availability_zone": "zone1:host1", "volume_type": db_vol_type['id'], } body = {"volume": vol} req = fakes.HTTPRequest.blank('/v2/volumes') res_dict = self.controller.create(req, body) volume_id = res_dict['volume']['id'] self.assertEquals(len(res_dict), 1) self.stubs.Set(volume_api.API, 'get_all', lambda *args, **kwargs: [stubs.stub_volume(volume_id, volume_type={'name': vol_type})]) req = fakes.HTTPRequest.blank('/v2/volumes/detail') res_dict = self.controller.detail(req)
def test_volume_type_delete(self): volume_type = db.volume_type_create(self.ctxt, {'name': 'fake volume type'}) volume_types.destroy(self.ctxt, volume_type['id']) self.assertRaises(exception.VolumeTypeNotFound, volume_types.get_by_name_or_id, self.ctxt, volume_type['id'])
def setUp(self): super(VolumeTypeExtraSpecsTestCase, self).setUp() self.context = context.get_admin_context() self.vol_type1 = dict(name="TEST: Regular volume test") self.vol_type1_specs = dict(vol_extra1="value1", vol_extra2="value2", vol_extra3=3) self.vol_type1['extra_specs'] = self.vol_type1_specs ref = db.volume_type_create(self.context, self.vol_type1) self.volume_type1_id = ref.id for k, v in self.vol_type1_specs.iteritems(): self.vol_type1_specs[k] = str(v) self.vol_type2_noextra = dict(name="TEST: Volume type without extra") ref = db.volume_type_create(self.context, self.vol_type2_noextra) self.vol_type2_id = ref.id
def _create(self, cipher, control_location, key_size, provider): volume_type = self._default_volume_type db.volume_type_create(context.get_admin_context(), volume_type) body = {"encryption": {'cipher': cipher, 'control_location': control_location, 'key_size': key_size, 'provider': provider, 'volume_type_id': volume_type['id']}} self.assertEqual(0, len(self.notifier.notifications)) res = self._get_response(volume_type) res_dict = jsonutils.loads(res.body) self.assertEqual(http_client.OK, res.status_code) # Confirm that volume type has no encryption information # before create. self.assertEqual(b'{}', res.body) # Create encryption specs for the volume type # with the defined body. res = self._get_response(volume_type, req_method='POST', req_body=jsonutils.dump_as_bytes(body), req_headers='application/json') res_dict = jsonutils.loads(res.body) self.assertEqual(1, len(self.notifier.notifications)) # check response self.assertIn('encryption', res_dict) self.assertEqual(cipher, res_dict['encryption']['cipher']) self.assertEqual(control_location, res_dict['encryption']['control_location']) self.assertEqual(key_size, res_dict['encryption']['key_size']) self.assertEqual(provider, res_dict['encryption']['provider']) self.assertEqual(volume_type['id'], res_dict['encryption']['volume_type_id']) # check database encryption = db.volume_type_encryption_get(context.get_admin_context(), volume_type['id']) self.assertIsNotNone(encryption) self.assertEqual(cipher, encryption['cipher']) self.assertEqual(key_size, encryption['key_size']) self.assertEqual(provider, encryption['provider']) self.assertEqual(volume_type['id'], encryption['volume_type_id']) db.volume_type_destroy(context.get_admin_context(), volume_type['id'])
def test_show_provider(self): self.stubs.Set(db, 'volume_type_encryption_get', return_volume_type_encryption) volume_type = { 'id': 'fake_type_id', 'name': 'fake_type', } db.volume_type_create(context.get_admin_context(), volume_type) res = self._get_response(volume_type, url='/v2/fake/types/%s/encryption/provider') res_dict = json.loads(res.body) self.assertEqual(200, res.status_code) self.assertEqual('fake_provider', res_dict['provider']) db.volume_type_destroy(context.get_admin_context(), volume_type['id'])
def test_restore_encrypted_vol_to_none_type_source_type_available(self): fields = ['encryption_key_id'] container = {} db.volume_type_create(self.ctxt, {'id': 'enc_vol_type_id', 'name': 'enc_vol_type'}) enc_vol_id = self._create_encrypted_volume_db_entry(str(uuid.uuid4()), 'enc_vol_type_id', True) undef_vol_id = self._create_encrypted_volume_db_entry( str(uuid.uuid4()), None, False) self.bak_meta_api._save_vol_base_meta(container, enc_vol_id) self.bak_meta_api._restore_vol_base_meta( container[self.bak_meta_api.TYPE_TAG_VOL_BASE_META], undef_vol_id, fields) self.assertEqual( db.volume_get(self.ctxt, undef_vol_id)['volume_type_id'], db.volume_get(self.ctxt, enc_vol_id)['volume_type_id'])
def test_manage_volume_raise_driver_exception(self, mock_execute, mock_driver_get_size): elevated = context.get_admin_context() project_id = self.context.project_id db.volume_type_create(elevated, {'name': 'type1', 'extra_specs': {}}) vol_type = db.volume_type_get_by_name(elevated, 'type1') # create source volume self.volume_params['volume_type_id'] = vol_type['id'] self.volume_params['status'] = 'managing' test_vol = tests_utils.create_volume(self.context, **self.volume_params) mock_execute.side_effect = exception.VolumeBackendAPIException( data="volume driver got exception") mock_driver_get_size.return_value = 1 # Set quota usage reserve_opts = {'volumes': 1, 'gigabytes': 1} reservations = QUOTAS.reserve(self.context, project_id=project_id, **reserve_opts) QUOTAS.commit(self.context, reservations) usage = db.quota_usage_get(self.context, project_id, 'volumes') volumes_in_use = usage.in_use usage = db.quota_usage_get(self.context, project_id, 'gigabytes') gigabytes_in_use = usage.in_use self.assertRaises(exception.VolumeBackendAPIException, self.volume.manage_existing, self.context, test_vol, 'volume_ref') # check volume status volume = objects.Volume.get_by_id(context.get_admin_context(), test_vol.id) self.assertEqual('error_managing', volume.status) # Delete this volume with 'error_managing_deleting' status in c-vol. test_vol.status = 'error_managing_deleting' test_vol.save() self.volume.delete_volume(self.context, test_vol) ctxt = context.get_admin_context(read_deleted='yes') volume = objects.Volume.get_by_id(ctxt, test_vol.id) self.assertEqual('deleted', volume.status) # Get in_use number after deleting error_managing volume usage = db.quota_usage_get(self.context, project_id, 'volumes') volumes_in_use_new = usage.in_use self.assertEqual(volumes_in_use, volumes_in_use_new) usage = db.quota_usage_get(self.context, project_id, 'gigabytes') gigabytes_in_use_new = usage.in_use self.assertEqual(gigabytes_in_use, gigabytes_in_use_new)
def test_volume_create_with_type(self): vol_type = CONF.default_volume_type db.volume_type_create(context.get_admin_context(), dict(name=vol_type, extra_specs={})) db_vol_type = db.volume_type_get_by_name(context.get_admin_context(), vol_type) vol = {"size": 100, "display_name": "Volume Test Name", "display_description": "Volume Test Desc", "availability_zone": "zone1:host1", "volume_type": db_vol_type['name'], } body = {"volume": vol} req = fakes.HTTPRequest.blank('/v1/volumes') res_dict = self.controller.create(req, body) self.assertEquals(res_dict['volume']['volume_type'], db_vol_type['name'])
def create(context, name, extra_specs={}): """Creates volume types.""" try: type_ref = db.volume_type_create(context, dict(name=name, extra_specs=extra_specs)) except db_exc.DBError as e: LOG.exception(_("DB error: %s") % e) raise exception.VolumeTypeCreateFailed(name=name, extra_specs=extra_specs) return type_ref
def create_volume_type(ctxt, testcase_instance=None, **kwargs): vol_type = db.volume_type_create(ctxt, kwargs) # If we get a TestCase instance we add cleanup if testcase_instance: testcase_instance.addCleanup(db.volume_type_destroy, ctxt, vol_type.id) return vol_type
def test_index(self): self.stubs.Set(db, 'volume_type_encryption_get', return_volume_type_encryption) volume_type = { 'id': 'fake_type_id', 'name': 'fake_type', } db.volume_type_create(context.get_admin_context(), volume_type) res = self._get_response(volume_type) self.assertEqual(200, res.status_code) res_dict = json.loads(res.body) expected = stub_volume_type_encryption() self.assertEqual(expected, res_dict) db.volume_type_destroy(context.get_admin_context(), volume_type['id'])
def test_volume_type_delete_with_volume_in_use(self): volume_type = db.volume_type_create(self.ctxt, {'name': 'fake volume type'}) volume = db.volume_create(self.ctxt, {'volume_type_id': volume_type['id']}) self.assertRaises(exception.VolumeTypeInUse, volume_types.destroy, self.ctxt, volume_type['id']) db.volume_destroy(self.ctxt, volume['id']) volume_types.destroy(self.ctxt, volume_type['id'])