cinder/cinder/tests/test_volume_transfer.py
Zhiteng Huang 7e95b05b93 Fix possible race condition for accept transfer
Accept transfer API workflow is currently like this:

  call volume_api.accept_transfer()
    |
    --- RPC cast to volume manager
          |
          --- volume manager calls volume driver accept_transfer()

  update volume's DB record

Given the non-blocking nature of RPC cast, what happens in volume
manager and volume driver can happen in parallel with the DB update.
If volume driver relies on original DB record to do things, then
DB record shouldn't be updated until volume driver finishes its job.

So this patch change volume RPC API accept_transfer() from cast
to call to make sure the workflow is in serialized manner.  Also
elevated the context when volume manager tries to update the DB
record when driver has done accept_transfer().

Change-Id: Ieae52e167aa02967338e0be5d78d570d682faa7a
Closes-bug: #1357432
2014-09-08 08:54:52 -07:00

141 lines
5.8 KiB
Python

# Copyright (c) 2013 OpenStack Foundation
# 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.
"""Unit Tests for volume transfers."""
import datetime
from cinder import context
from cinder import db
from cinder import exception
from cinder.openstack.common import log as logging
from cinder import test
from cinder.tests import utils
from cinder.transfer import api as transfer_api
LOG = logging.getLogger(__name__)
class VolumeTransferTestCase(test.TestCase):
"""Test cases for volume transfer code."""
def setUp(self):
super(VolumeTransferTestCase, self).setUp()
self.ctxt = context.RequestContext(user_id='user_id',
project_id='project_id')
self.updated_at = datetime.datetime(1, 1, 1, 1, 1, 1)
def test_transfer_volume_create_delete(self):
tx_api = transfer_api.API()
utils.create_volume(self.ctxt, id='1',
updated_at=self.updated_at)
response = tx_api.create(self.ctxt, '1', 'Description')
volume = db.volume_get(self.ctxt, '1')
self.assertEqual('awaiting-transfer', volume['status'],
'Unexpected state')
tx_api.delete(self.ctxt, response['id'])
volume = db.volume_get(self.ctxt, '1')
self.assertEqual('available', volume['status'], 'Unexpected state')
def test_transfer_invalid_volume(self):
tx_api = transfer_api.API()
utils.create_volume(self.ctxt, id='1', status='in-use',
updated_at=self.updated_at)
self.assertRaises(exception.InvalidVolume,
tx_api.create,
self.ctxt, '1', 'Description')
volume = db.volume_get(self.ctxt, '1')
self.assertEqual('in-use', volume['status'], 'Unexpected state')
def test_transfer_accept(self):
svc = self.start_service('volume', host='test_host')
tx_api = transfer_api.API()
utils.create_volume(self.ctxt, id='1',
updated_at=self.updated_at)
transfer = tx_api.create(self.ctxt, '1', 'Description')
volume = db.volume_get(self.ctxt, '1')
self.assertEqual('awaiting-transfer', volume['status'],
'Unexpected state')
self.assertRaises(exception.TransferNotFound,
tx_api.accept,
self.ctxt, '2', transfer['auth_key'])
self.assertRaises(exception.InvalidAuthKey,
tx_api.accept,
self.ctxt, transfer['id'], 'wrong')
db.volume_update(self.ctxt, '1', {'status': 'wrong'})
self.assertRaises(exception.InvalidVolume,
tx_api.accept,
self.ctxt, transfer['id'], transfer['auth_key'])
db.volume_update(self.ctxt, '1', {'status': 'awaiting-transfer'})
self.ctxt.user_id = 'new_user_id'
self.ctxt.project_id = 'new_project_id'
response = tx_api.accept(self.ctxt,
transfer['id'],
transfer['auth_key'])
volume = db.volume_get(self.ctxt, '1')
self.assertEqual(volume['project_id'], 'new_project_id',
'Unexpected project id')
self.assertEqual(volume['user_id'], 'new_user_id',
'Unexpected user id')
self.assertEqual(volume['id'], response['volume_id'],
'Unexpected volume id in response.')
self.assertEqual(transfer['id'], response['id'],
'Unexpected transfer id in response.')
svc.stop()
def test_transfer_get(self):
tx_api = transfer_api.API()
volume = utils.create_volume(self.ctxt, id='1',
updated_at=self.updated_at)
transfer = tx_api.create(self.ctxt, volume['id'], 'Description')
t = tx_api.get(self.ctxt, transfer['id'])
self.assertEqual(t['id'], transfer['id'], 'Unexpected transfer id')
ts = tx_api.get_all(self.ctxt)
self.assertEqual(len(ts), 1, 'Unexpected number of transfers.')
nctxt = context.RequestContext(user_id='new_user_id',
project_id='new_project_id')
utils.create_volume(nctxt, id='2', updated_at=self.updated_at)
self.assertRaises(exception.TransferNotFound,
tx_api.get,
nctxt,
transfer['id'])
ts = tx_api.get_all(nctxt)
self.assertEqual(len(ts), 0, 'Unexpected transfers listed.')
def test_delete_transfer_with_deleted_volume(self):
#create a volume
volume = utils.create_volume(self.ctxt, id='1',
updated_at=self.updated_at)
#create a transfer
tx_api = transfer_api.API()
transfer = tx_api.create(self.ctxt, volume['id'], 'Description')
t = tx_api.get(self.ctxt, transfer['id'])
self.assertEqual(t['id'], transfer['id'], 'Unexpected transfer id')
#force delete volume
db.volume_destroy(context.get_admin_context(), volume['id'])
#Make sure transfer has been deleted.
self.assertRaises(exception.TransferNotFound,
tx_api.get,
self.ctxt,
transfer['id'])