Rather than have repetitive code - create/use a couple of helpers: testStoragePoolObjFindActiveByName and testStoragePoolObjFindInactiveByName This will also allow for the reduction of some cleanup path logic. Signed-off-by: John Ferlan <jferlan@xxxxxxxxxx> --- src/test/test_driver.c | 256 +++++++++++++++++-------------------------------- 1 file changed, 86 insertions(+), 170 deletions(-) diff --git a/src/test/test_driver.c b/src/test/test_driver.c index efa54ff..9918df6 100644 --- a/src/test/test_driver.c +++ b/src/test/test_driver.c @@ -4030,6 +4030,46 @@ testStoragePoolObjFindByName(testDriverPtr privconn, static virStoragePoolObjPtr +testStoragePoolObjFindActiveByName(testDriverPtr privconn, + const char *name) +{ + virStoragePoolObjPtr obj; + + if (!(obj = testStoragePoolObjFindByName(privconn, name))) + return NULL; + + if (!virStoragePoolObjIsActive(obj)) { + virReportError(VIR_ERR_OPERATION_INVALID, + _("storage pool '%s' is not active"), name); + virStoragePoolObjUnlock(obj); + return NULL; + } + + return obj; +} + + +static virStoragePoolObjPtr +testStoragePoolObjFindInactiveByName(testDriverPtr privconn, + const char *name) +{ + virStoragePoolObjPtr obj; + + if (!(obj = testStoragePoolObjFindByName(privconn, name))) + return NULL; + + if (virStoragePoolObjIsActive(obj)) { + virReportError(VIR_ERR_OPERATION_INVALID, + _("storage pool '%s' is already active"), name); + virStoragePoolObjUnlock(obj); + return NULL; + } + + return obj; +} + + +static virStoragePoolObjPtr testStoragePoolObjFindByUUID(testDriverPtr privconn, const unsigned char *uuid) { @@ -4221,32 +4261,22 @@ testStoragePoolCreate(virStoragePoolPtr pool, { testDriverPtr privconn = pool->conn->privateData; virStoragePoolObjPtr obj; - int ret = -1; virObjectEventPtr event = NULL; virCheckFlags(0, -1); - if (!(obj = testStoragePoolObjFindByName(privconn, pool->name))) - goto cleanup; - - if (virStoragePoolObjIsActive(obj)) { - virReportError(VIR_ERR_OPERATION_INVALID, - _("storage pool '%s' is already active"), pool->name); - goto cleanup; - } + if (!(obj = testStoragePoolObjFindInactiveByName(privconn, pool->name))) + return -1; obj->active = 1; event = virStoragePoolEventLifecycleNew(pool->name, pool->uuid, VIR_STORAGE_POOL_EVENT_STARTED, 0); - ret = 0; - cleanup: testObjectEventQueue(privconn, event); - if (obj) - virStoragePoolObjUnlock(obj); - return ret; + virStoragePoolObjUnlock(obj); + return 0; } @@ -4458,31 +4488,19 @@ testStoragePoolUndefine(virStoragePoolPtr pool) { testDriverPtr privconn = pool->conn->privateData; virStoragePoolObjPtr obj; - int ret = -1; virObjectEventPtr event = NULL; - if (!(obj = testStoragePoolObjFindByName(privconn, pool->name))) - goto cleanup; - - if (virStoragePoolObjIsActive(obj)) { - virReportError(VIR_ERR_OPERATION_INVALID, - _("storage pool '%s' is already active"), pool->name); - goto cleanup; - } + if (!(obj = testStoragePoolObjFindInactiveByName(privconn, pool->name))) + return -1; event = virStoragePoolEventLifecycleNew(pool->name, pool->uuid, VIR_STORAGE_POOL_EVENT_UNDEFINED, 0); virStoragePoolObjRemove(&privconn->pools, obj); - obj = NULL; - ret = 0; - cleanup: - if (obj) - virStoragePoolObjUnlock(obj); testObjectEventQueue(privconn, event); - return ret; + return 0; } @@ -4492,24 +4510,14 @@ testStoragePoolBuild(virStoragePoolPtr pool, { testDriverPtr privconn = pool->conn->privateData; virStoragePoolObjPtr obj; - int ret = -1; virCheckFlags(0, -1); - if (!(obj = testStoragePoolObjFindByName(privconn, pool->name))) - goto cleanup; - - if (virStoragePoolObjIsActive(obj)) { - virReportError(VIR_ERR_OPERATION_INVALID, - _("storage pool '%s' is already active"), pool->name); - goto cleanup; - } - ret = 0; + if (!(obj = testStoragePoolObjFindInactiveByName(privconn, pool->name))) + return -1; - cleanup: - if (obj) - virStoragePoolObjUnlock(obj); - return ret; + virStoragePoolObjUnlock(obj); + return 0; } @@ -4559,14 +4567,8 @@ testStoragePoolDestroy(virStoragePoolPtr pool) int ret = -1; virObjectEventPtr event = NULL; - if (!(obj = testStoragePoolObjFindByName(privconn, pool->name))) - goto cleanup; - - if (!virStoragePoolObjIsActive(obj)) { - virReportError(VIR_ERR_OPERATION_INVALID, - _("storage pool '%s' is not active"), pool->name); - goto cleanup; - } + if (!(obj = testStoragePoolObjFindActiveByName(privconn, pool->name))) + return -1; obj->active = 0; @@ -4603,25 +4605,14 @@ testStoragePoolDelete(virStoragePoolPtr pool, { testDriverPtr privconn = pool->conn->privateData; virStoragePoolObjPtr obj; - int ret = -1; virCheckFlags(0, -1); - if (!(obj = testStoragePoolObjFindByName(privconn, pool->name))) - goto cleanup; - - if (virStoragePoolObjIsActive(obj)) { - virReportError(VIR_ERR_OPERATION_INVALID, - _("storage pool '%s' is already active"), pool->name); - goto cleanup; - } - - ret = 0; + if (!(obj = testStoragePoolObjFindInactiveByName(privconn, pool->name))) + return -1; - cleanup: - if (obj) - virStoragePoolObjUnlock(obj); - return ret; + virStoragePoolObjUnlock(obj); + return 0; } @@ -4631,28 +4622,18 @@ testStoragePoolRefresh(virStoragePoolPtr pool, { testDriverPtr privconn = pool->conn->privateData; virStoragePoolObjPtr obj; - int ret = -1; virObjectEventPtr event = NULL; virCheckFlags(0, -1); - if (!(obj = testStoragePoolObjFindByName(privconn, pool->name))) - goto cleanup; - - if (!virStoragePoolObjIsActive(obj)) { - virReportError(VIR_ERR_OPERATION_INVALID, - _("storage pool '%s' is not active"), pool->name); - goto cleanup; - } + if (!(obj = testStoragePoolObjFindActiveByName(privconn, pool->name))) + return -1; event = virStoragePoolEventRefreshNew(pool->name, pool->uuid); - ret = 0; - cleanup: testObjectEventQueue(privconn, event); - if (obj) - virStoragePoolObjUnlock(obj); - return ret; + virStoragePoolObjUnlock(obj); + return 0; } @@ -4755,21 +4736,13 @@ testStoragePoolNumOfVolumes(virStoragePoolPtr pool) virStoragePoolObjPtr obj; int ret = -1; - if (!(obj = testStoragePoolObjFindByName(privconn, pool->name))) - goto cleanup; - - if (!virStoragePoolObjIsActive(obj)) { - virReportError(VIR_ERR_OPERATION_INVALID, - _("storage pool '%s' is not active"), pool->name); - goto cleanup; - } + if (!(obj = testStoragePoolObjFindActiveByName(privconn, pool->name))) + return -1; ret = virStoragePoolObjNumOfVolumes(&obj->volumes, pool->conn, obj->def, NULL); - cleanup: - if (obj) - virStoragePoolObjUnlock(obj); + virStoragePoolObjUnlock(obj); return ret; } @@ -4783,19 +4756,12 @@ testStoragePoolListVolumes(virStoragePoolPtr pool, virStoragePoolObjPtr obj; int n = -1; - if (!(obj = testStoragePoolObjFindByName(privconn, pool->name))) + if (!(obj = testStoragePoolObjFindActiveByName(privconn, pool->name))) return -1; - if (!virStoragePoolObjIsActive(obj)) { - virReportError(VIR_ERR_OPERATION_INVALID, - _("storage pool '%s' is not active"), pool->name); - goto cleanup; - } - n = virStoragePoolObjVolumeGetNames(&obj->volumes, pool->conn, obj->def, NULL, names, maxnames); - cleanup: virStoragePoolObjUnlock(obj); return n; } @@ -4840,14 +4806,8 @@ testStorageVolLookupByName(virStoragePoolPtr pool, virStorageVolDefPtr privvol; virStorageVolPtr ret = NULL; - if (!(obj = testStoragePoolObjFindByName(privconn, pool->name))) - goto cleanup; - - if (!virStoragePoolObjIsActive(obj)) { - virReportError(VIR_ERR_OPERATION_INVALID, - _("storage pool '%s' is not active"), pool->name); - goto cleanup; - } + if (!(obj = testStoragePoolObjFindActiveByName(privconn, pool->name))) + return NULL; privvol = virStorageVolDefFindByName(obj, name); @@ -4862,8 +4822,7 @@ testStorageVolLookupByName(virStoragePoolPtr pool, NULL, NULL); cleanup: - if (obj) - virStoragePoolObjUnlock(obj); + virStoragePoolObjUnlock(obj); return ret; } @@ -4954,14 +4913,8 @@ testStorageVolCreateXML(virStoragePoolPtr pool, virCheckFlags(0, NULL); - if (!(obj = testStoragePoolObjFindByName(privconn, pool->name))) - goto cleanup; - - if (!virStoragePoolObjIsActive(obj)) { - virReportError(VIR_ERR_OPERATION_INVALID, - _("storage pool '%s' is not active"), pool->name); - goto cleanup; - } + if (!(obj = testStoragePoolObjFindActiveByName(privconn, pool->name))) + return NULL; privvol = virStorageVolDefParseString(obj->def, xmldesc, 0); if (privvol == NULL) @@ -5002,8 +4955,7 @@ testStorageVolCreateXML(virStoragePoolPtr pool, cleanup: virStorageVolDefFree(privvol); - if (obj) - virStoragePoolObjUnlock(obj); + virStoragePoolObjUnlock(obj); return ret; } @@ -5021,14 +4973,8 @@ testStorageVolCreateXMLFrom(virStoragePoolPtr pool, virCheckFlags(0, NULL); - if (!(obj = testStoragePoolObjFindByName(privconn, pool->name))) - goto cleanup; - - if (!virStoragePoolObjIsActive(obj)) { - virReportError(VIR_ERR_OPERATION_INVALID, - _("storage pool '%s' is not active"), pool->name); - goto cleanup; - } + if (!(obj = testStoragePoolObjFindActiveByName(privconn, pool->name))) + return NULL; privvol = virStorageVolDefParseString(obj->def, xmldesc, 0); if (privvol == NULL) @@ -5078,8 +5024,7 @@ testStorageVolCreateXMLFrom(virStoragePoolPtr pool, cleanup: virStorageVolDefFree(privvol); - if (obj) - virStoragePoolObjUnlock(obj); + virStoragePoolObjUnlock(obj); return ret; } @@ -5096,8 +5041,8 @@ testStorageVolDelete(virStorageVolPtr vol, virCheckFlags(0, -1); - if (!(obj = testStoragePoolObjFindByName(privconn, vol->pool))) - goto cleanup; + if (!(obj = testStoragePoolObjFindActiveByName(privconn, vol->pool))) + return -1; privvol = virStorageVolDefFindByName(obj, vol->name); @@ -5108,13 +5053,6 @@ testStorageVolDelete(virStorageVolPtr vol, goto cleanup; } - if (!virStoragePoolObjIsActive(obj)) { - virReportError(VIR_ERR_OPERATION_INVALID, - _("storage pool '%s' is not active"), vol->pool); - goto cleanup; - } - - obj->def->allocation -= privvol->target.allocation; obj->def->available = (obj->def->capacity - obj->def->allocation); @@ -5129,8 +5067,7 @@ testStorageVolDelete(virStorageVolPtr vol, ret = 0; cleanup: - if (obj) - virStoragePoolObjUnlock(obj); + virStoragePoolObjUnlock(obj); return ret; } @@ -5159,8 +5096,8 @@ testStorageVolGetInfo(virStorageVolPtr vol, virStorageVolDefPtr privvol; int ret = -1; - if (!(obj = testStoragePoolObjFindByName(privconn, vol->pool))) - goto cleanup; + if (!(obj = testStoragePoolObjFindActiveByName(privconn, vol->pool))) + return -1; privvol = virStorageVolDefFindByName(obj, vol->name); @@ -5171,12 +5108,6 @@ testStorageVolGetInfo(virStorageVolPtr vol, goto cleanup; } - if (!virStoragePoolObjIsActive(obj)) { - virReportError(VIR_ERR_OPERATION_INVALID, - _("storage pool '%s' is not active"), vol->pool); - goto cleanup; - } - memset(info, 0, sizeof(*info)); info->type = testStorageVolumeTypeForPool(obj->def->type); info->capacity = privvol->target.capacity; @@ -5184,8 +5115,7 @@ testStorageVolGetInfo(virStorageVolPtr vol, ret = 0; cleanup: - if (obj) - virStoragePoolObjUnlock(obj); + virStoragePoolObjUnlock(obj); return ret; } @@ -5201,8 +5131,8 @@ testStorageVolGetXMLDesc(virStorageVolPtr vol, virCheckFlags(0, NULL); - if (!(obj = testStoragePoolObjFindByName(privconn, vol->pool))) - goto cleanup; + if (!(obj = testStoragePoolObjFindActiveByName(privconn, vol->pool))) + return NULL; privvol = virStorageVolDefFindByName(obj, vol->name); @@ -5213,17 +5143,10 @@ testStorageVolGetXMLDesc(virStorageVolPtr vol, goto cleanup; } - if (!virStoragePoolObjIsActive(obj)) { - virReportError(VIR_ERR_OPERATION_INVALID, - _("storage pool '%s' is not active"), vol->pool); - goto cleanup; - } - ret = virStorageVolDefFormat(obj->def, privvol); cleanup: - if (obj) - virStoragePoolObjUnlock(obj); + virStoragePoolObjUnlock(obj); return ret; } @@ -5236,8 +5159,8 @@ testStorageVolGetPath(virStorageVolPtr vol) virStorageVolDefPtr privvol; char *ret = NULL; - if (!(obj = testStoragePoolObjFindByName(privconn, vol->pool))) - goto cleanup; + if (!(obj = testStoragePoolObjFindActiveByName(privconn, vol->pool))) + return NULL; privvol = virStorageVolDefFindByName(obj, vol->name); @@ -5248,17 +5171,10 @@ testStorageVolGetPath(virStorageVolPtr vol) goto cleanup; } - if (!virStoragePoolObjIsActive(obj)) { - virReportError(VIR_ERR_OPERATION_INVALID, - _("storage pool '%s' is not active"), vol->pool); - goto cleanup; - } - ignore_value(VIR_STRDUP(ret, privvol->target.path)); cleanup: - if (obj) - virStoragePoolObjUnlock(obj); + virStoragePoolObjUnlock(obj); return ret; } -- 2.9.3 -- libvir-list mailing list libvir-list@xxxxxxxxxx https://www.redhat.com/mailman/listinfo/libvir-list