def test_migrate_volume_with_snap(self): expected_status = 400 host = 'test2' ctx = context.RequestContext('admin', 'fake', True) volume = self._migrate_volume_prep() db.snapshot_create(ctx, {'volume_id': volume['id']}) self._migrate_volume_exec(ctx, volume, host, expected_status)
def test_migrate_volume_with_snap(self): expected_status = 400 host = "test2" ctx = context.RequestContext("admin", "fake", True) volume = self._migrate_volume_prep() db.snapshot_create(ctx, {"volume_id": volume["id"]}) self._migrate_volume_exec(ctx, volume, host, expected_status)
def test_snapshot_data_get_for_project(self): actual = db.snapshot_data_get_for_project(self.ctxt, "project1") self.assertEqual(actual, (0, 0)) db.volume_create(self.ctxt, {"id": 1, "project_id": "project1", "size": 42}) db.snapshot_create(self.ctxt, {"id": 1, "volume_id": 1, "project_id": "project1", "volume_size": 42}) actual = db.snapshot_data_get_for_project(self.ctxt, "project1") self.assertEqual(actual, (1, 42))
def test_snapshot_metadata_get(self): metadata = {'a': 'b', 'c': 'd'} db.volume_create(self.ctxt, {'id': 1}) db.snapshot_create(self.ctxt, {'id': 1, 'volume_id': 1, 'metadata': metadata}) self.assertEquals(metadata, db.snapshot_metadata_get(self.ctxt, 1))
def test_snapshot_metadata_delete(self): metadata = {"a": "1", "c": "2"} should_be = {"a": "1"} db.volume_create(self.ctxt, {"id": 1}) db.snapshot_create(self.ctxt, {"id": 1, "volume_id": 1, "metadata": metadata}) db.snapshot_metadata_delete(self.ctxt, 1, "c") self.assertEqual(should_be, db.snapshot_metadata_get(self.ctxt, 1))
def test_snapshot_metadata_update_delete(self): metadata1 = {"a": "1", "c": "2"} metadata2 = {"a": "3", "d": "5"} should_be = metadata2 db.volume_create(self.ctxt, {"id": 1}) db.snapshot_create(self.ctxt, {"id": 1, "volume_id": 1, "metadata": metadata1}) db_meta = db.snapshot_metadata_update(self.ctxt, 1, metadata2, True) self.assertEqual(should_be, db_meta)
def test_snapshot_metadata_delete(self): metadata = {'a': '1', 'c': '2'} should_be = {'a': '1'} db.volume_create(self.ctxt, {'id': 1}) db.snapshot_create(self.ctxt, {'id': 1, 'volume_id': 1, 'metadata': metadata}) db.snapshot_metadata_delete(self.ctxt, 1, 'c') self.assertEquals(should_be, db.snapshot_metadata_get(self.ctxt, 1))
def test_snapshot_metadata_update(self): metadata1 = {'a': '1', 'c': '2'} metadata2 = {'a': '3', 'd': '5'} should_be = {'a': '3', 'c': '2', 'd': '5'} db.volume_create(self.ctxt, {'id': 1}) db.snapshot_create(self.ctxt, {'id': 1, 'volume_id': 1, 'metadata': metadata1}) db_meta = db.snapshot_metadata_update(self.ctxt, 1, metadata2, False) self.assertEqual(should_be, db_meta)
def test_snapshot_metadata_update_delete(self): metadata1 = {'a': '1', 'c': '2'} metadata2 = {'a': '3', 'd': '5'} should_be = metadata2 db.volume_create(self.ctxt, {'id': 1}) db.snapshot_create(self.ctxt, {'id': 1, 'volume_id': 1, 'metadata': metadata1}) db.snapshot_metadata_update(self.ctxt, 1, metadata2, True) self.assertEquals(should_be, db.snapshot_metadata_get(self.ctxt, 1))
def test_vol_glance_metadata_copy_to_snapshot(self): ctxt = context.get_admin_context() db.volume_create(ctxt, {"id": 1}) db.snapshot_create(ctxt, {"id": 100, "volume_id": 1}) db.volume_glance_metadata_create(ctxt, 1, "key1", "value1") db.volume_glance_metadata_copy_to_snapshot(ctxt, 100, 1) expected_meta = {"snapshot_id": "100", "key": "key1", "value": "value1"} for meta in db.volume_snapshot_glance_metadata_get(ctxt, 100): for (key, value) in expected_meta.items(): self.assertEqual(meta[key], value)
def test_vol_glance_metadata_copy_to_snapshot(self): ctxt = context.get_admin_context() db.volume_create(ctxt, {'id': 1}) db.snapshot_create(ctxt, {'id': 100, 'volume_id': 1}) db.volume_glance_metadata_create(ctxt, 1, 'key1', 'value1') db.volume_glance_metadata_copy_to_snapshot(ctxt, 100, 1) expected_meta = {'snapshot_id': '100', 'key': 'key1', 'value': 'value1'} for meta in db.volume_snapshot_glance_metadata_get(ctxt, 100): for (key, value) in expected_meta.items(): self.assertEqual(value, meta[key])
def test_force_delete_snapshot(self): self.stubs.Set(os.path, 'exists', lambda x: True) self.stubs.Set(volutils, 'clear_volume', lambda a, b, volume_clear=CONF.volume_clear, volume_clear_size=CONF.volume_clear_size: None) # admin context ctx = context.RequestContext('admin', 'fake', True) # current status is creating volume = db.volume_create(ctx, {'host': 'test', 'size': 1}) snapshot = db.snapshot_create(ctx, { 'status': 'creating', 'volume_size': 1, 'volume_id': volume['id'] }) path = '/v2/fake/snapshots/%s/action' % snapshot['id'] req = webob.Request.blank(path) req.method = 'POST' req.headers['content-type'] = 'application/json' req.body = jsonutils.dumps({'os-force_delete': {}}) # attach admin context to request req.environ['cinder.context'] = ctx # start service to handle rpc.cast for 'delete snapshot' svc = self.start_service('volume', host='test') # make request resp = req.get_response(app()) # request is accepted self.assertEqual(resp.status_int, 202) # snapshot is deleted self.assertRaises(exception.NotFound, db.snapshot_get, ctx, snapshot['id']) # cleanup svc.stop()
def test_invalid_status_for_snapshot(self): # admin context ctx = context.RequestContext('admin', 'fake', True) # snapshot in 'available' volume = db.volume_create( ctx, { 'status': 'available', 'host': 'test', 'provider_location': '', 'size': 1 }) snapshot = db.snapshot_create(ctx, { 'status': 'available', 'volume_id': volume['id'] }) req = webob.Request.blank('/v2/fake/snapshots/%s/action' % snapshot['id']) req.method = 'POST' req.headers['content-type'] = 'application/json' # 'attaching' is not a valid status for snapshots req.body = jsonutils.dumps( {'os-reset_status': { 'status': 'attaching' }}) # attach admin context to request req.environ['cinder.context'] = ctx resp = req.get_response(app()) # request is accepted self.assertEqual(resp.status_int, 400) snapshot = db.snapshot_get(ctx, snapshot['id']) # status is still 'available' self.assertEqual(snapshot['status'], 'available')
def create(self): if self.obj_attr_is_set('id'): raise exception.ObjectActionError(action='create', reason=_('already created')) updates = self.cinder_obj_get_changes() if 'volume' in updates: raise exception.ObjectActionError(action='create', reason=_('volume assigned')) if 'cgsnapshot' in updates: raise exception.ObjectActionError(action='create', reason=_('cgsnapshot assigned')) if 'cluster' in updates: raise exception.ObjectActionError(action='create', reason=_('cluster assigned')) if 'group_snapshot' in updates: raise exception.ObjectActionError( action='create', reason=_('group_snapshot assigned')) if ('volume_type_id' not in updates or updates['volume_type_id'] is None): updates['volume_type_id'] = ( volume_types.get_default_volume_type()['id']) db_snapshot = db.snapshot_create(self._context, updates) self._from_db_object(self._context, self, db_snapshot)
def setUp(self): super(VolumeRpcAPITestCase, self).setUp() self.context = context.get_admin_context() vol = {} vol['host'] = 'fake_host' vol['availability_zone'] = CONF.storage_availability_zone vol['status'] = "available" vol['attach_status'] = "detached" vol['metadata'] = {"test_key": "test_val"} volume = db.volume_create(self.context, vol) snpshot = { 'id': 1, 'volume_id': 'fake_id', 'status': "creating", 'progress': '0%', 'volume_size': 0, 'display_name': 'fake_name', 'display_description': 'fake_description'} snapshot = db.snapshot_create(self.context, snpshot) self.fake_volume = jsonutils.to_primitive(volume) self.fake_volume_metadata = volume["volume_metadata"] self.fake_snapshot = jsonutils.to_primitive(snapshot) self.fake_snapshot_obj = fake_snapshot.fake_snapshot_obj(self.context, **snpshot) self.fake_reservations = ["RESERVATION"]
def setUp(self): super(VolumeRpcAPITestCase, self).setUp() self.context = context.get_admin_context() vol = {} vol["host"] = "fake_host" vol["availability_zone"] = CONF.storage_availability_zone vol["status"] = "available" vol["attach_status"] = "detached" vol["metadata"] = {"test_key": "test_val"} volume = db.volume_create(self.context, vol) snpshot = { "id": 1, "volume_id": "fake_id", "status": "creating", "progress": "0%", "volume_size": 0, "display_name": "fake_name", "display_description": "fake_description", } snapshot = db.snapshot_create(self.context, snpshot) self.fake_volume = jsonutils.to_primitive(volume) self.fake_volume_metadata = volume["volume_metadata"] self.fake_snapshot = jsonutils.to_primitive(snapshot) self.fake_snapshot_obj = fake_snapshot.fake_snapshot_obj(self.context, **snpshot) self.fake_reservations = ["RESERVATION"]
def test_force_delete_snapshot(self): self.stubs.Set(os.path, 'exists', lambda x: True) # admin context ctx = context.RequestContext('admin', 'fake', True) # current status is creating volume = db.volume_create(ctx, {'host': 'test'}) snapshot = db.snapshot_create(ctx, {'status': 'creating', 'volume_size': 1, 'volume_id': volume['id']}) path = '/v2/fake/snapshots/%s/action' % snapshot['id'] req = webob.Request.blank(path) req.method = 'POST' req.headers['content-type'] = 'application/json' req.body = jsonutils.dumps({'os-force_delete': {}}) # attach admin context to request req.environ['cinder.context'] = ctx # start service to handle rpc.cast for 'delete snapshot' svc = self.start_service('volume', host='test') # make request resp = req.get_response(app()) # request is accepted self.assertEqual(resp.status_int, 202) # snapshot is deleted self.assertRaises(exception.NotFound, db.snapshot_get, ctx, snapshot['id']) # cleanup svc.stop()
def setUp(self): super(VolumeRpcAPITestCase, self).setUp() self.context = context.get_admin_context() vol = {} vol['host'] = 'fake_host' vol['availability_zone'] = CONF.storage_availability_zone vol['status'] = "available" vol['attach_status'] = "detached" vol['metadata'] = {"test_key": "test_val"} volume = db.volume_create(self.context, vol) snpshot = { 'id': 1, 'volume_id': 'fake_id', 'status': "creating", 'progress': '0%', 'volume_size': 0, 'display_name': 'fake_name', 'display_description': 'fake_description'} snapshot = db.snapshot_create(self.context, snpshot) source_group = tests_utils.create_consistencygroup( self.context, availability_zone=CONF.storage_availability_zone, volume_type='type1,type2', host='fakehost@fakedrv#fakepool') cgsnapshot = tests_utils.create_cgsnapshot( self.context, consistencygroup_id=source_group['id']) group = tests_utils.create_consistencygroup( self.context, availability_zone=CONF.storage_availability_zone, volume_type='type1,type2', host='fakehost@fakedrv#fakepool', cgsnapshot_id=cgsnapshot['id']) group2 = tests_utils.create_consistencygroup( self.context, availability_zone=CONF.storage_availability_zone, volume_type='type1,type2', host='fakehost@fakedrv#fakepool', source_cgid=source_group['id']) group = objects.ConsistencyGroup.get_by_id(self.context, group.id) group2 = objects.ConsistencyGroup.get_by_id(self.context, group2.id) self.fake_volume = jsonutils.to_primitive(volume) self.fake_volume_obj = fake_volume.fake_volume_obj(self.context, **vol) self.fake_volume_metadata = volume["volume_metadata"] self.fake_snapshot = jsonutils.to_primitive(snapshot) self.fake_snapshot_obj = fake_snapshot.fake_snapshot_obj(self.context, **snpshot) self.fake_reservations = ["RESERVATION"] self.fake_cg = group self.fake_cg2 = group2 self.fake_src_cg = jsonutils.to_primitive(source_group) self.fake_cgsnap = jsonutils.to_primitive(cgsnapshot)
def setUp(self): super(VolumeRpcAPITestCase, self).setUp() self.context = context.get_admin_context() vol = {} vol["host"] = "fake_host" vol["availability_zone"] = CONF.storage_availability_zone vol["status"] = "available" vol["attach_status"] = "detached" vol["metadata"] = {"test_key": "test_val"} volume = db.volume_create(self.context, vol) snpshot = { "id": 1, "volume_id": "fake_id", "status": "creating", "progress": "0%", "volume_size": 0, "display_name": "fake_name", "display_description": "fake_description", } snapshot = db.snapshot_create(self.context, snpshot) source_group = tests_utils.create_consistencygroup( self.context, availability_zone=CONF.storage_availability_zone, volume_type="type1,type2", host="fakehost@fakedrv#fakepool", ) cgsnapshot = tests_utils.create_cgsnapshot(self.context, consistencygroup_id=source_group["id"]) group = tests_utils.create_consistencygroup( self.context, availability_zone=CONF.storage_availability_zone, volume_type="type1,type2", host="fakehost@fakedrv#fakepool", cgsnapshot_id=cgsnapshot["id"], ) group2 = tests_utils.create_consistencygroup( self.context, availability_zone=CONF.storage_availability_zone, volume_type="type1,type2", host="fakehost@fakedrv#fakepool", source_cgid=source_group["id"], ) group = objects.ConsistencyGroup.get_by_id(self.context, group.id) group2 = objects.ConsistencyGroup.get_by_id(self.context, group2.id) self.fake_volume = jsonutils.to_primitive(volume) self.fake_volume_obj = fake_volume.fake_volume_obj(self.context, **vol) self.fake_volume_metadata = volume["volume_metadata"] self.fake_snapshot = jsonutils.to_primitive(snapshot) self.fake_snapshot_obj = fake_snapshot.fake_snapshot_obj(self.context, **snpshot) self.fake_reservations = ["RESERVATION"] self.fake_cg = group self.fake_cg2 = group2 self.fake_src_cg = jsonutils.to_primitive(source_group) self.fake_cgsnap = jsonutils.to_primitive(cgsnapshot)
def setUp(self): super(VolumeRpcAPITestCase, self).setUp() self.context = context.get_admin_context() vol = {} vol['host'] = 'fake_host' vol['availability_zone'] = CONF.storage_availability_zone vol['status'] = "available" vol['attach_status'] = "detached" vol['metadata'] = {"test_key": "test_val"} volume = db.volume_create(self.context, vol) snpshot = { 'id': 1, 'volume_id': 'fake_id', 'status': "creating", 'progress': '0%', 'volume_size': 0, 'display_name': 'fake_name', 'display_description': 'fake_description'} snapshot = db.snapshot_create(self.context, snpshot) source_group = tests_utils.create_consistencygroup( self.context, availability_zone=CONF.storage_availability_zone, volume_type='type1,type2', host='fakehost@fakedrv#fakepool') cgsnapshot = tests_utils.create_cgsnapshot( self.context, consistencygroup_id=source_group['id']) group = tests_utils.create_consistencygroup( self.context, availability_zone=CONF.storage_availability_zone, volume_type='type1,type2', host='fakehost@fakedrv#fakepool', cgsnapshot_id=cgsnapshot['id']) group2 = tests_utils.create_consistencygroup( self.context, availability_zone=CONF.storage_availability_zone, volume_type='type1,type2', host='fakehost@fakedrv#fakepool', source_cgid=source_group['id']) group = objects.ConsistencyGroup.get_by_id(self.context, group.id) group2 = objects.ConsistencyGroup.get_by_id(self.context, group2.id) self.fake_volume = jsonutils.to_primitive(volume) self.fake_volume_metadata = volume["volume_metadata"] self.fake_snapshot = jsonutils.to_primitive(snapshot) self.fake_snapshot_obj = fake_snapshot.fake_snapshot_obj(self.context, **snpshot) self.fake_reservations = ["RESERVATION"] self.fake_cg = group self.fake_cg2 = group2 self.fake_src_cg = jsonutils.to_primitive(source_group) self.fake_cgsnap = jsonutils.to_primitive(cgsnapshot)
def _create_snapshot(volume_id, size="0"): """Create a snapshot object.""" snap = {} snap["volume_size"] = size snap["user_id"] = "fake" snap["project_id"] = "fake" snap["volume_id"] = volume_id snap["status"] = "creating" return db.snapshot_create(context.get_admin_context(), snap)
def _create_snapshot(volume_id, size='0'): """Create a snapshot object.""" snap = {} snap['volume_size'] = size snap['user_id'] = 'fake' snap['project_id'] = 'fake' snap['volume_id'] = volume_id snap['status'] = "creating" return db.snapshot_create(context.get_admin_context(), snap)
def test_invalid_status_for_snapshot(self): ctx = context.RequestContext("admin", "fake", True) volume = db.volume_create(ctx, {"status": "available", "host": "test", "provider_location": "", "size": 1}) snapshot = db.snapshot_create(ctx, {"status": "available", "volume_id": volume["id"]}) resp = self._issue_snapshot_reset(ctx, snapshot, {"status": "attaching"}) self.assertEqual(resp.status_int, 400) snapshot = db.snapshot_get(ctx, snapshot["id"]) self.assertEqual(snapshot["status"], "available")
def create(self): if self.obj_attr_is_set("id"): raise exception.ObjectActionError(action="create", reason=_("already created")) updates = self.obj_get_changes() if "volume" in updates: raise exception.ObjectActionError(action="create", reason=_("volume assigned")) db_snapshot = db.snapshot_create(self._context, updates) self._from_db_object(self._context, self, db_snapshot)
def test_volume_glance_metadata_copy_to_volume(self): vol1 = db.volume_create(self.ctxt, {}) vol2 = db.volume_create(self.ctxt, {}) db.volume_glance_metadata_create(self.ctxt, vol1["id"], "m1", "v1") snapshot = db.snapshot_create(self.ctxt, {"volume_id": vol1["id"]}) db.volume_glance_metadata_copy_to_snapshot(self.ctxt, snapshot["id"], vol1["id"]) db.volume_glance_metadata_copy_to_volume(self.ctxt, vol2["id"], snapshot["id"]) metadata = db.volume_glance_metadata_get(self.ctxt, vol2["id"]) metadata = {m["key"]: m["value"] for m in metadata} self.assertEqual(metadata, {"m1": "v1"})
def test_snapshot_data_get_for_project(self): actual = db.snapshot_data_get_for_project(self.ctxt, 'project1') self.assertEqual(actual, (0, 0)) db.volume_create(self.ctxt, {'id': 1, 'project_id': 'project1', 'size': 42}) snapshot = db.snapshot_create(self.ctxt, {'id': 1, 'volume_id': 1, 'project_id': 'project1', 'volume_size': 42}) actual = db.snapshot_data_get_for_project(self.ctxt, 'project1') self.assertEqual(actual, (1, 42))
def test_volume_glance_metadata_copy_to_volume(self): vol1 = db.volume_create(self.ctxt, {}) vol2 = db.volume_create(self.ctxt, {}) db.volume_glance_metadata_create(self.ctxt, vol1['id'], 'm1', 'v1') snapshot = db.snapshot_create(self.ctxt, {'volume_id': vol1['id']}) db.volume_glance_metadata_copy_to_snapshot(self.ctxt, snapshot['id'], vol1['id']) db.volume_glance_metadata_copy_to_volume(self.ctxt, vol2['id'], snapshot['id']) metadata = db.volume_glance_metadata_get(self.ctxt, vol2['id']) metadata = dict([(m['key'], m['value']) for m in metadata]) self.assertEqual({'m1': 'v1'}, metadata)
def create(self): if self.obj_attr_is_set('id'): raise exception.ObjectActionError(action='create', reason=_('already created')) updates = self.cinder_obj_get_changes() if 'volume' in updates: raise exception.ObjectActionError(action='create', reason=_('volume assigned')) db_snapshot = db.snapshot_create(self._context, updates) self._from_db_object(self._context, self, db_snapshot)
def test_volume_glance_metadata_copy_to_volume(self): vol1 = db.volume_create(self.ctxt, {}) vol2 = db.volume_create(self.ctxt, {}) db.volume_glance_metadata_create(self.ctxt, vol1['id'], 'm1', 'v1') snapshot = db.snapshot_create(self.ctxt, {'volume_id': vol1['id']}) db.volume_glance_metadata_copy_to_snapshot(self.ctxt, snapshot['id'], vol1['id']) db.volume_glance_metadata_copy_to_volume(self.ctxt, vol2['id'], snapshot['id']) metadata = db.volume_glance_metadata_get(self.ctxt, vol2['id']) metadata = dict([(m['key'], m['value']) for m in metadata]) self.assertEqual(metadata, {'m1': 'v1'})
def create_snapshot( ctxt, volume_id, display_name="test_snapshot", display_description="this is a test snapshot", status="creating" ): vol = db.volume_get(ctxt, volume_id) snap = {} snap["volume_id"] = volume_id snap["user_id"] = ctxt.user_id snap["project_id"] = ctxt.project_id snap["status"] = status snap["volume_size"] = vol["size"] snap["display_name"] = display_name snap["display_description"] = display_description return db.snapshot_create(ctxt, snap)
def test_snapshot_reset_status(self): ctx = context.RequestContext('admin', 'fake', True) volume = db.volume_create(ctx, {'status': 'available', 'host': 'test', 'provider_location': '', 'size': 1}) snapshot = db.snapshot_create(ctx, {'status': 'error_deleting', 'volume_id': volume['id']}) resp = self._issue_snapshot_reset(ctx, snapshot, {'status': 'error'}) self.assertEqual(resp.status_int, 202) snapshot = db.snapshot_get(ctx, snapshot['id']) self.assertEqual(snapshot['status'], 'error')
def test_invalid_status_for_snapshot(self): ctx = context.RequestContext('admin', 'fake', True) volume = db.volume_create(ctx, {'status': 'available', 'host': 'test', 'provider_location': '', 'size': 1}) snapshot = db.snapshot_create(ctx, {'status': 'available', 'volume_id': volume['id']}) resp = self._issue_snapshot_reset(ctx, snapshot, {'status': 'attaching'}) self.assertEqual(400, resp.status_int) snapshot = db.snapshot_get(ctx, snapshot['id']) self.assertEqual('available', snapshot['status'])
def test_invalid_status_for_snapshot(self): ctx = context.RequestContext('admin', 'fake', True) volume = db.volume_create(ctx, {'status': 'available', 'host': 'test', 'provider_location': '', 'size': 1}) snapshot = db.snapshot_create(ctx, {'status': 'available', 'volume_id': volume['id']}) resp = self._issue_snapshot_reset(ctx, snapshot, {'status': 'attaching'}) self.assertEqual(resp.status_int, 400) snapshot = db.snapshot_get(ctx, snapshot['id']) self.assertEqual(snapshot['status'], 'available')
def create_snapshot(ctxt, volume_id, display_name='test_snapshot', display_description='this is a test snapshot', status='creating'): vol = db.volume_get(ctxt, volume_id) snap = {} snap['volume_id'] = volume_id snap['user_id'] = ctxt.user_id snap['project_id'] = ctxt.project_id snap['status'] = status snap['volume_size'] = vol['size'] snap['display_name'] = display_name snap['display_description'] = display_description return db.snapshot_create(ctxt, snap)
def test_force_delete_snapshot(self): self.stubs.Set(volutils, 'clear_volume', lambda a, b, volume_clear=CONF.volume_clear, volume_clear_size=CONF.volume_clear_size: None) # admin context ctx = context.RequestContext('admin', 'fake', True) # current status is creating volume = db.volume_create(ctx, {'host': 'test', 'size': 1}) snapshot = db.snapshot_create(ctx, {'status': 'creating', 'volume_size': 1, 'volume_id': volume['id']}) path = '/v2/fake/snapshots/%s/action' % snapshot['id'] req = webob.Request.blank(path) req.method = 'POST' req.headers['content-type'] = 'application/json' req.body = jsonutils.dumps({'os-force_delete': {}}) # attach admin context to request req.environ['cinder.context'] = ctx # start service to handle rpc.cast for 'delete snapshot' svc = self.start_service('volume', host='test') cast_as_call.mock_cast_as_call(svc.manager.scheduler_rpcapi.client) # NOTE(flaper87): Instead fo patch `os.path.exists` # create a fake path for the snapshot that should # be deleted and let the check pass def local_path(volume, vg=None): tfile = tempfile.mkstemp(suffix='-cow', dir=self.tempdir) # NOTE(flaper87): Strip `-cow` since it'll be added # later in the happy path. return tfile[1].strip('-cow') self.stubs.Set(svc.manager.driver, "local_path", local_path) # make request resp = req.get_response(app()) # NOTE(flaper87): Since we're using a nested service # lets make sure we yield the control over the service # thread so it can process the recent calls. time.sleep(0.6) # Request is accepted self.assertEqual(resp.status_int, 202) # snapshot is deleted self.assertRaises(exception.NotFound, db.snapshot_get, ctx, snapshot['id']) # cleanup svc.stop()
def test_snapshot_data_get_for_project(self): actual = db.snapshot_data_get_for_project(self.ctxt, 'project1') self.assertEqual(actual, (0, 0)) db.volume_create(self.ctxt, { 'id': 1, 'project_id': 'project1', 'size': 42 }) snapshot = db.snapshot_create(self.ctxt, { 'id': 1, 'volume_id': 1, 'project_id': 'project1', 'volume_size': 42 }) actual = db.snapshot_data_get_for_project(self.ctxt, 'project1') self.assertEqual(actual, (1, 42))
def test_invalid_status_for_snapshot(self): # admin context ctx = context.RequestContext("admin", "fake", True) # snapshot in 'available' volume = db.volume_create(ctx, {}) snapshot = db.snapshot_create(ctx, {"status": "available", "volume_id": volume["id"]}) req = webob.Request.blank("/v1/fake/snapshots/%s/action" % snapshot["id"]) req.method = "POST" req.headers["content-type"] = "application/json" # 'attaching' is not a valid status for snapshots req.body = jsonutils.dumps({"os-reset_status": {"status": "attaching"}}) # attach admin context to request req.environ["cinder.context"] = ctx resp = req.get_response(app()) # request is accepted self.assertEquals(resp.status_int, 400) snapshot = db.snapshot_get(ctx, snapshot["id"]) # status is still 'available' self.assertEquals(snapshot["status"], "available")
def test_update_progress(self): ctx = context.RequestContext('admin', 'fake', True) # snapshot in 'error_deleting' volume = db.volume_create(ctx, {}) snapshot = db.snapshot_create(ctx, {'volume_id': volume['id']}) req = webob.Request.blank('/v2/fake/snapshots/%s/action' % snapshot['id']) req.method = 'POST' req.headers['content-type'] = 'application/json' # request status of 'error' req.body = jsonutils.dumps({'os-update_progress': 'progress!'}) # attach admin context to request req.environ['cinder.context'] = ctx resp = req.get_response(app()) # request is accepted self.assertEquals(resp.status_int, 202) snapshot = db.snapshot_get(ctx, snapshot['id']) # status changed to 'error' self.assertEquals(snapshot['progress'], 'progress!')
def test_update_progress(self): ctx = context.RequestContext('admin', 'fake', True) # snapshot in 'error_deleting' volume = db.volume_create(ctx, {}) snapshot = db.snapshot_create(ctx, {'volume_id': volume['id']}) req = webob.Request.blank('/v2/fake/snapshots/%s/action' % snapshot['id']) req.method = 'POST' req.headers['content-type'] = 'application/json' # request status of 'error' req.body = jsonutils.dumps({'os-update_progress': 'progress!'}) # attach admin context to request req.environ['cinder.context'] = ctx resp = req.get_response(app()) # request is accepted self.assertEqual(resp.status_int, 202) snapshot = db.snapshot_get(ctx, snapshot['id']) # status changed to 'error' self.assertEqual(snapshot['progress'], 'progress!')
def setUp(self): self.context = context.get_admin_context() vol = {} vol['host'] = 'fake_host' vol['availability_zone'] = FLAGS.storage_availability_zone vol['status'] = "available" vol['attach_status'] = "detached" volume = db.volume_create(self.context, vol) snpshot = { 'volume_id': 'fake_id', 'status': "creating", 'progress': '0%', 'volume_size': 0, 'display_name': 'fake_name', 'display_description': 'fake_description'} snapshot = db.snapshot_create(self.context, snpshot) self.fake_volume = jsonutils.to_primitive(volume) self.fake_snapshot = jsonutils.to_primitive(snapshot) super(VolumeRpcAPITestCase, self).setUp()
def test_snapshot_get_all(self): db.volume_create(self.ctxt, {'id': 1}) snapshot = db.snapshot_create(self.ctxt, {'id': 1, 'volume_id': 1}) self._assertEqualListsOfObjects([snapshot], db.snapshot_get_all(self.ctxt), ignored_keys=['metadata', 'volume'])
def test_snapshot_get_active_by_window(self): # Find all all snapshots valid within a timeframe window. vol = db.volume_create(self.context, {'id': 1}) try: # Not in window db.snapshot_create( self.context, { 'id': 1, 'host': 'devstack', 'volume_id': 1, 'created_at': datetime.datetime(1, 1, 1, 1, 1, 1), 'deleted': True, 'status': 'deleted', 'deleted_at': datetime.datetime(1, 2, 1, 1, 1, 1), }) except exception.SnapshotNotFound: pass try: # In - deleted in window db.snapshot_create( self.context, { 'id': 2, 'host': 'devstack', 'volume_id': 1, 'created_at': datetime.datetime(1, 1, 1, 1, 1, 1), 'deleted': True, 'status': 'deleted', 'deleted_at': datetime.datetime(1, 3, 10, 1, 1, 1), }) except exception.SnapshotNotFound: pass try: # In - deleted after window db.snapshot_create( self.context, { 'id': 3, 'host': 'devstack', 'volume_id': 1, 'created_at': datetime.datetime(1, 1, 1, 1, 1, 1), 'deleted': True, 'status': 'deleted', 'deleted_at': datetime.datetime(1, 5, 1, 1, 1, 1), }) except exception.SnapshotNotFound: pass # In - created in window db.snapshot_create( self.context, { 'id': 4, 'host': 'devstack', 'volume_id': 1, 'created_at': datetime.datetime(1, 3, 10, 1, 1, 1), }) # Not of window. db.snapshot_create( self.context, { 'id': 5, 'host': 'devstack', 'volume_id': 1, 'created_at': datetime.datetime(1, 5, 1, 1, 1, 1), }) snapshots = db.snapshot_get_active_by_window( self.context, datetime.datetime(1, 3, 1, 1, 1, 1), datetime.datetime(1, 4, 1, 1, 1, 1)) self.assertEqual(len(snapshots), 3) self.assertEqual(snapshots[0].id, u'2') self.assertEqual(snapshots[1].id, u'3') self.assertEqual(snapshots[2].id, u'4')
def test_volume_snapshot_glance_metadata_get_nonexistent(self): vol = db.volume_create(self.ctxt, {}) snapshot = db.snapshot_create(self.ctxt, {'volume_id': vol['id']}) self.assertRaises(exception.GlanceMetadataNotFound, db.volume_snapshot_glance_metadata_get, self.ctxt, snapshot['id'])