def test_disassociate_all(self): def fake_db_disassociate_all(context, id): if id == 'Trouble': raise db_exc.DBError() pass type1_ref = volume_types.create(self.ctxt, 'TypeName1') type2_ref = volume_types.create(self.ctxt, 'TypeName2') specs_id = self._create_qos_specs('QoSName') qos_specs.associate_qos_with_type(self.ctxt, specs_id, type1_ref['id']) qos_specs.associate_qos_with_type(self.ctxt, specs_id, type2_ref['id']) res = qos_specs.get_associations(self.ctxt, specs_id) self.assertEquals(len(res[specs_id].keys()), 2) qos_specs.disassociate_all(self.ctxt, specs_id) res = qos_specs.get_associations(self.ctxt, specs_id) self.assertEquals(len(res[specs_id].keys()), 0) self.stubs.Set(db, 'qos_specs_disassociate_all', fake_db_disassociate_all) self.assertRaises(exception.QoSSpecsDisassociateFailed, qos_specs.disassociate_all, self.ctxt, 'Trouble')
def test_disassociate_qos_specs(self): def fake_db_disassociate(context, id, type_id): raise db_exc.DBError() type_ref = volume_types.create(self.ctxt, 'TypeName') specs_id = self._create_qos_specs('QoSName') qos_specs.associate_qos_with_type(self.ctxt, specs_id, type_ref['id']) res = qos_specs.get_associations(self.ctxt, specs_id) self.assertEqual(1, len(res)) qos_specs.disassociate_qos_specs(self.ctxt, specs_id, type_ref['id']) res = qos_specs.get_associations(self.ctxt, specs_id) self.assertEqual(0, len(res)) self.assertRaises(exception.VolumeTypeNotFound, qos_specs.disassociate_qos_specs, self.ctxt, specs_id, 'NotFound') # Verify we can disassociate specs from volume_type even if they are # not associated with no error qos_specs.disassociate_qos_specs(self.ctxt, specs_id, type_ref['id']) qos_specs.associate_qos_with_type(self.ctxt, specs_id, type_ref['id']) self.mock_object(db, 'qos_specs_disassociate', fake_db_disassociate) self.assertRaises(exception.QoSSpecsDisassociateFailed, qos_specs.disassociate_qos_specs, self.ctxt, specs_id, type_ref['id'])
def test_disassociate_qos_specs(self): def fake_db_disassociate(context, id, type_id): if id == 'Trouble': raise db_exc.DBError() elif type_id == 'NotFound': raise exception.VolumeTypeNotFound(volume_type_id=type_id) pass type_ref = volume_types.create(self.ctxt, 'TypeName') specs_id = self._create_qos_specs('QoSName') qos_specs.associate_qos_with_type(self.ctxt, specs_id, type_ref['id']) res = qos_specs.get_associations(self.ctxt, specs_id) self.assertEquals(len(res[specs_id].keys()), 1) qos_specs.disassociate_qos_specs(self.ctxt, specs_id, type_ref['id']) res = qos_specs.get_associations(self.ctxt, specs_id) self.assertEquals(len(res[specs_id].keys()), 0) self.stubs.Set(db, 'qos_specs_disassociate', fake_db_disassociate) self.assertRaises(exception.VolumeTypeNotFound, qos_specs.disassociate_qos_specs, self.ctxt, 'specs-id', 'NotFound') self.assertRaises(exception.QoSSpecsDisassociateFailed, qos_specs.disassociate_qos_specs, self.ctxt, 'Trouble', 'id')
def test_disassociate_all(self): def fake_db_disassociate_all(context, id): if id == "Trouble": raise db_exc.DBError() pass def fake_qos_specs_get(context, id): if id == "NotFound": raise exception.QoSSpecsNotFound(specs_id=id) else: pass type1_ref = volume_types.create(self.ctxt, "TypeName1") type2_ref = volume_types.create(self.ctxt, "TypeName2") specs_id = self._create_qos_specs("QoSName") qos_specs.associate_qos_with_type(self.ctxt, specs_id, type1_ref["id"]) qos_specs.associate_qos_with_type(self.ctxt, specs_id, type2_ref["id"]) res = qos_specs.get_associations(self.ctxt, specs_id) self.assertEqual(2, len(res)) qos_specs.disassociate_all(self.ctxt, specs_id) res = qos_specs.get_associations(self.ctxt, specs_id) self.assertEqual(0, len(res)) self.mock_object(db, "qos_specs_disassociate_all", fake_db_disassociate_all) self.mock_object(qos_specs, "get_qos_specs", fake_qos_specs_get) self.assertRaises(exception.QoSSpecsDisassociateFailed, qos_specs.disassociate_all, self.ctxt, "Trouble")
def test_disassociate_all(self): def fake_db_disassociate_all(context, id): if id == 'Trouble': raise db_exc.DBError() pass def fake_qos_specs_get(context, id): if id == 'NotFound': raise exception.QoSSpecsNotFound(specs_id=id) else: pass type1_ref = volume_types.create(self.ctxt, 'TypeName1') type2_ref = volume_types.create(self.ctxt, 'TypeName2') specs_id = self._create_qos_specs('QoSName') qos_specs.associate_qos_with_type(self.ctxt, specs_id, type1_ref['id']) qos_specs.associate_qos_with_type(self.ctxt, specs_id, type2_ref['id']) res = qos_specs.get_associations(self.ctxt, specs_id) self.assertEqual(2, len(res)) qos_specs.disassociate_all(self.ctxt, specs_id) res = qos_specs.get_associations(self.ctxt, specs_id) self.assertEqual(0, len(res)) self.stubs.Set(db, 'qos_specs_disassociate_all', fake_db_disassociate_all) self.stubs.Set(qos_specs, 'get_qos_specs', fake_qos_specs_get) self.assertRaises(exception.QoSSpecsDisassociateFailed, qos_specs.disassociate_all, self.ctxt, 'Trouble')
def test_disassociate_qos_specs(self): def fake_qos_specs_get(context, id): if id == 'NotFound': raise exception.QoSSpecsNotFound(specs_id=id) else: pass def fake_db_disassociate(context, id, type_id): if id == 'Trouble': raise db_exc.DBError() elif type_id == 'NotFound': raise exception.VolumeTypeNotFound(volume_type_id=type_id) pass type_ref = volume_types.create(self.ctxt, 'TypeName') specs_id = self._create_qos_specs('QoSName') qos_specs.associate_qos_with_type(self.ctxt, specs_id, type_ref['id']) res = qos_specs.get_associations(self.ctxt, specs_id) self.assertEqual(len(res), 1) qos_specs.disassociate_qos_specs(self.ctxt, specs_id, type_ref['id']) res = qos_specs.get_associations(self.ctxt, specs_id) self.assertEqual(len(res), 0) self.stubs.Set(db, 'qos_specs_disassociate', fake_db_disassociate) self.stubs.Set(qos_specs, 'get_qos_specs', fake_qos_specs_get) self.assertRaises(exception.VolumeTypeNotFound, qos_specs.disassociate_qos_specs, self.ctxt, 'specs-id', 'NotFound') self.assertRaises(exception.QoSSpecsDisassociateFailed, qos_specs.disassociate_qos_specs, self.ctxt, 'Trouble', 'id')
def test_disassociate_qos_specs(self): def fake_db_disassociate(context, id, type_id): raise db_exc.DBError() type_ref = volume_types.create(self.ctxt, 'TypeName') specs_id = self._create_qos_specs('QoSName') qos_specs.associate_qos_with_type(self.ctxt, specs_id, type_ref['id']) res = qos_specs.get_associations(self.ctxt, specs_id) self.assertEqual(1, len(res)) qos_specs.disassociate_qos_specs(self.ctxt, specs_id, type_ref['id']) res = qos_specs.get_associations(self.ctxt, specs_id) self.assertEqual(0, len(res)) self.assertRaises(exception.VolumeTypeNotFound, qos_specs.disassociate_qos_specs, self.ctxt, specs_id, 'NotFound') # Verify we can disassociate specs from volume_type even if they are # not associated with no error qos_specs.disassociate_qos_specs(self.ctxt, specs_id, type_ref['id']) qos_specs.associate_qos_with_type(self.ctxt, specs_id, type_ref['id']) self.stubs.Set(db, 'qos_specs_disassociate', fake_db_disassociate) self.assertRaises(exception.QoSSpecsDisassociateFailed, qos_specs.disassociate_qos_specs, self.ctxt, specs_id, type_ref['id'])
def associations(self, req, id): """List all associations of given qos specs.""" context = req.environ['cinder.context'] authorize(context) LOG.debug("Get associations for qos_spec id: %s" % id) try: associates = qos_specs.get_associations(context, id) notifier_info = dict(id=id) notifier_api.notify(context, 'QoSSpecs', 'qos_specs.associations', notifier_api.INFO, notifier_info) except exception.QoSSpecsNotFound as err: notifier_err = dict(id=id, error_message=str(err)) self._notify_qos_specs_error(context, 'qos_specs.associations', notifier_err) raise webob.exc.HTTPNotFound(explanation=str(err)) except exception.CinderException as err: notifier_err = dict(id=id, error_message=str(err)) self._notify_qos_specs_error(context, 'qos_specs.associations', notifier_err) raise webob.exc.HTTPInternalServerError(explanation=str(err)) return self._view_builder.associations(req, associates)
def associations(self, req, id): """List all associations of given qos specs.""" context = req.environ['cinder.context'] authorize(context) LOG.debug("Get associations for qos_spec id: %s", id) try: associates = qos_specs.get_associations(context, id) notifier_info = dict(id=id) rpc.get_notifier('QoSSpecs').info(context, 'qos_specs.associations', notifier_info) except exception.QoSSpecsNotFound as err: notifier_err = dict(id=id, error_message=err) self._notify_qos_specs_error(context, 'qos_specs.associations', notifier_err) # Not found exception will be handled at the wsgi level raise except exception.CinderException as err: notifier_err = dict(id=id, error_message=err) self._notify_qos_specs_error(context, 'qos_specs.associations', notifier_err) raise webob.exc.HTTPInternalServerError( explanation=six.text_type(err)) return self._view_builder.associations(req, associates)
def test_get_associations(self): def fake_db_associate_get(context, id): if id == 'Trouble': raise db_exc.DBError() return [{ 'name': 'type-1', 'id': 'id-1' }, { 'name': 'type-2', 'id': 'id-2' }] self.stubs.Set(db, 'qos_specs_associations_get', fake_db_associate_get) expected1 = { 'association_type': 'volume_type', 'name': 'type-1', 'id': 'id-1' } expected2 = { 'association_type': 'volume_type', 'name': 'type-2', 'id': 'id-2' } res = qos_specs.get_associations(self.ctxt, 'specs-id') self.assertIn(expected1, res) self.assertIn(expected2, res) self.assertRaises(exception.CinderException, qos_specs.get_associations, self.ctxt, 'Trouble')
def test_get_associations(self, mock_qos_specs_associations_get): vol_types = [fake_db_get_vol_type(x) for x in range(2)] mock_qos_specs_associations_get.return_value = vol_types specs_id = self._create_qos_specs("new_spec") res = qos_specs.get_associations(self.ctxt, specs_id) for vol_type in vol_types: expected_type = {"association_type": "volume_type", "id": vol_type["id"], "name": vol_type["name"]} self.assertIn(expected_type, res) e = exception.QoSSpecsNotFound(specs_id="Trouble") mock_qos_specs_associations_get.side_effect = e self.assertRaises(exception.CinderException, qos_specs.get_associations, self.ctxt, "Trouble")
def test_associate_qos_with_type(self): def fake_qos_specs_get(context, id): if id == 'NotFound': raise exception.QoSSpecsNotFound(specs_id=id) else: pass def fake_db_associate(context, id, type_id): if id == 'Trouble': raise db_exc.DBError() elif type_id == 'NotFound': raise exception.VolumeTypeNotFound(volume_type_id=type_id) pass def fake_vol_type_qos_get(type_id): if type_id == 'Invalid': return {'qos_specs': {'id': 'Invalid'}} else: return {'qos_specs': None} type_ref = volume_types.create(self.ctxt, 'TypeName') specs_id = self._create_qos_specs('QoSName') qos_specs.associate_qos_with_type(self.ctxt, specs_id, type_ref['id']) res = qos_specs.get_associations(self.ctxt, specs_id) self.assertEqual(1, len(res)) self.assertEqual('TypeName', res[0]['name']) self.assertEqual(type_ref['id'], res[0]['id']) self.stubs.Set(db, 'qos_specs_associate', fake_db_associate) self.stubs.Set(qos_specs, 'get_qos_specs', fake_qos_specs_get) self.stubs.Set(volume_types, 'get_volume_type_qos_specs', fake_vol_type_qos_get) self.assertRaises(exception.VolumeTypeNotFound, qos_specs.associate_qos_with_type, self.ctxt, 'specs-id', 'NotFound') self.assertRaises(exception.QoSSpecsAssociateFailed, qos_specs.associate_qos_with_type, self.ctxt, 'Trouble', 'id') self.assertRaises(exception.QoSSpecsNotFound, qos_specs.associate_qos_with_type, self.ctxt, 'NotFound', 'id') self.assertRaises(exception.InvalidVolumeType, qos_specs.associate_qos_with_type, self.ctxt, 'specs-id', 'Invalid')
def test_associate_qos_with_type(self): def fake_qos_specs_get(context, id): if id == 'NotFound': raise exception.QoSSpecsNotFound(specs_id=id) else: pass def fake_db_associate(context, id, type_id): if id == 'Trouble': raise db_exc.DBError() elif type_id == 'NotFound': raise exception.VolumeTypeNotFound(volume_type_id=type_id) pass def fake_vol_type_qos_get(type_id): if type_id == 'Invalid': return {'qos_specs': {'id': 'Invalid'}} else: return {'qos_specs': None} type_ref = volume_types.create(self.ctxt, 'TypeName') specs_id = self._create_qos_specs('QoSName') qos_specs.associate_qos_with_type(self.ctxt, specs_id, type_ref['id']) res = qos_specs.get_associations(self.ctxt, specs_id) self.assertEqual(1, len(res)) self.assertEqual('TypeName', res[0]['name']) self.assertEqual(type_ref['id'], res[0]['id']) self.mock_object(db, 'qos_specs_associate', fake_db_associate) self.mock_object(qos_specs, 'get_qos_specs', fake_qos_specs_get) self.mock_object(volume_types, 'get_volume_type_qos_specs', fake_vol_type_qos_get) self.assertRaises(exception.VolumeTypeNotFound, qos_specs.associate_qos_with_type, self.ctxt, 'specs-id', 'NotFound') self.assertRaises(exception.QoSSpecsAssociateFailed, qos_specs.associate_qos_with_type, self.ctxt, 'Trouble', 'id') self.assertRaises(exception.QoSSpecsNotFound, qos_specs.associate_qos_with_type, self.ctxt, 'NotFound', 'id') self.assertRaises(exception.InvalidVolumeType, qos_specs.associate_qos_with_type, self.ctxt, 'specs-id', 'Invalid')
def test_get_associations(self, mock_qos_specs_associations_get): vol_types = [fake_db_get_vol_type(x) for x in range(2)] mock_qos_specs_associations_get.return_value = vol_types specs_id = self._create_qos_specs('new_spec') res = qos_specs.get_associations(self.ctxt, specs_id) for vol_type in vol_types: expected_type = { 'association_type': 'volume_type', 'id': vol_type['id'], 'name': vol_type['name'] } self.assertIn(expected_type, res) self.assertRaises(exception.CinderException, qos_specs.get_associations, self.ctxt, 'Trouble')
def test_get_associations(self): def fake_db_associate_get(context, id): if id == 'Trouble': raise db_exc.DBError() return [{'name': 'type-1', 'id': 'id-1'}, {'name': 'type-2', 'id': 'id-2'}] self.stubs.Set(db, 'qos_specs_associations_get', fake_db_associate_get) expected = {'specs-id': {'type-1': 'id-1', 'type-2': 'id-2'}} res = qos_specs.get_associations(self.ctxt, 'specs-id') self.assertDictMatch(res, expected) self.assertRaises(exception.CinderException, qos_specs.get_associations, self.ctxt, 'Trouble')
def test_get_associations(self, mock_qos_specs_associations_get): vol_types = [fake_db_get_vol_type(x) for x in range(2)] mock_qos_specs_associations_get.return_value = vol_types specs_id = self._create_qos_specs('new_spec') res = qos_specs.get_associations(self.ctxt, specs_id) for vol_type in vol_types: expected_type = { 'association_type': 'volume_type', 'id': vol_type['id'], 'name': vol_type['name'] } self.assertIn(expected_type, res) e = exception.QoSSpecsNotFound(specs_id='Trouble') mock_qos_specs_associations_get.side_effect = e self.assertRaises(exception.CinderException, qos_specs.get_associations, self.ctxt, 'Trouble')
def test_get_associations(self): def fake_db_associate_get(context, id): if id == 'Trouble': raise db_exc.DBError() return [{ 'name': 'type-1', 'id': 'id-1' }, { 'name': 'type-2', 'id': 'id-2' }] self.stubs.Set(db, 'qos_specs_associations_get', fake_db_associate_get) expected = {'specs-id': {'type-1': 'id-1', 'type-2': 'id-2'}} res = qos_specs.get_associations(self.ctxt, 'specs-id') self.assertDictMatch(res, expected) self.assertRaises(exception.CinderException, qos_specs.get_associations, self.ctxt, 'Trouble')
def test_associate_qos_with_type(self): def fake_qos_specs_get(context, id): if id == "NotFound": raise exception.QoSSpecsNotFound(specs_id=id) else: pass def fake_db_associate(context, id, type_id): if id == "Trouble": raise db_exc.DBError() elif type_id == "NotFound": raise exception.VolumeTypeNotFound(volume_type_id=type_id) pass def fake_vol_type_qos_get(type_id): if type_id == "Invalid": return {"qos_specs": {"id": "Invalid"}} else: return {"qos_specs": None} type_ref = volume_types.create(self.ctxt, "TypeName") specs_id = self._create_qos_specs("QoSName") qos_specs.associate_qos_with_type(self.ctxt, specs_id, type_ref["id"]) res = qos_specs.get_associations(self.ctxt, specs_id) self.assertEqual(1, len(res)) self.assertEqual("TypeName", res[0]["name"]) self.assertEqual(type_ref["id"], res[0]["id"]) self.mock_object(db, "qos_specs_associate", fake_db_associate) self.mock_object(qos_specs, "get_qos_specs", fake_qos_specs_get) self.mock_object(volume_types, "get_volume_type_qos_specs", fake_vol_type_qos_get) self.assertRaises( exception.VolumeTypeNotFound, qos_specs.associate_qos_with_type, self.ctxt, "specs-id", "NotFound" ) self.assertRaises( exception.QoSSpecsAssociateFailed, qos_specs.associate_qos_with_type, self.ctxt, "Trouble", "id" ) self.assertRaises(exception.QoSSpecsNotFound, qos_specs.associate_qos_with_type, self.ctxt, "NotFound", "id") self.assertRaises( exception.InvalidVolumeType, qos_specs.associate_qos_with_type, self.ctxt, "specs-id", "Invalid" )
def test_get_associations(self): def fake_db_associate_get(context, id): if id == 'Trouble': raise db_exc.DBError() return [{'name': 'type-1', 'id': 'id-1'}, {'name': 'type-2', 'id': 'id-2'}] self.stubs.Set(db, 'qos_specs_associations_get', fake_db_associate_get) expected1 = {'association_type': 'volume_type', 'name': 'type-1', 'id': 'id-1'} expected2 = {'association_type': 'volume_type', 'name': 'type-2', 'id': 'id-2'} res = qos_specs.get_associations(self.ctxt, 'specs-id') self.assertIn(expected1, res) self.assertIn(expected2, res) self.assertRaises(exception.CinderException, qos_specs.get_associations, self.ctxt, 'Trouble')
def test_associate_qos_with_type(self): def fake_db_associate(context, id, type_id): if id == 'Trouble': raise db_exc.DBError() elif type_id == 'NotFound': raise exception.VolumeTypeNotFound(volume_type_id=type_id) pass type_ref = volume_types.create(self.ctxt, 'TypeName') specs_id = self._create_qos_specs('QoSName') qos_specs.associate_qos_with_type(self.ctxt, specs_id, type_ref['id']) res = qos_specs.get_associations(self.ctxt, specs_id) self.assertEquals(len(res[specs_id].keys()), 1) self.assertTrue('TypeName' in res[specs_id].keys()) self.assertTrue(type_ref['id'] in res[specs_id].values()) self.stubs.Set(db, 'qos_specs_associate', fake_db_associate) self.assertRaises(exception.VolumeTypeNotFound, qos_specs.associate_qos_with_type, self.ctxt, 'specs-id', 'NotFound') self.assertRaises(exception.QoSSpecsAssociateFailed, qos_specs.associate_qos_with_type, self.ctxt, 'Trouble', 'id')