From 1f12580a64c454473de21104663262d4ad160dd2 Mon Sep 17 00:00:00 2001 From: Jovanka Gulicoska Date: Thu, 28 Jul 2016 14:02:52 +0200 Subject: [PATCH] test: implement node device lifecycle event APIs Also includes unittests for node device lifecycle events API --- src/test/test_driver.c | 48 +++++++++++++++++++++++++++ tests/objecteventtest.c | 72 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 120 insertions(+) diff --git a/src/test/test_driver.c b/src/test/test_driver.c index 672c1630ce..bc1f93d89d 100644 --- a/src/test/test_driver.c +++ b/src/test/test_driver.c @@ -51,6 +51,7 @@ #include "storage_conf.h" #include "storage_event.h" #include "node_device_conf.h" +#include "node_device_event.h" #include "virxml.h" #include "virthread.h" #include "virlog.h" @@ -5427,6 +5428,7 @@ testNodeDeviceCreateXML(virConnectPtr conn, int parent_host = -1; virNodeDevicePtr dev = NULL; virNodeDevCapsDefPtr caps; + virObjectEventPtr event = NULL; virCheckFlags(0, NULL); @@ -5470,11 +5472,16 @@ testNodeDeviceCreateXML(virConnectPtr conn, goto cleanup; virNodeDeviceObjUnlock(obj); + event = virNodeDeviceEventLifecycleNew(def->name, + VIR_NODE_DEVICE_EVENT_CREATED, + 0); + dev = virGetNodeDevice(conn, def->name); def = NULL; cleanup: testDriverUnlock(driver); virNodeDeviceDefFree(def); + testObjectEventQueue(driver, event); VIR_FREE(wwnn); VIR_FREE(wwpn); return dev; @@ -5488,6 +5495,7 @@ testNodeDeviceDestroy(virNodeDevicePtr dev) virNodeDeviceObjPtr obj = NULL; char *parent_name = NULL, *wwnn = NULL, *wwpn = NULL; int parent_host = -1; + virObjectEventPtr event = NULL; testDriverLock(driver); obj = virNodeDeviceFindByName(&driver->devs, dev->name); @@ -5521,12 +5529,17 @@ testNodeDeviceDestroy(virNodeDevicePtr dev) goto out; } + event = virNodeDeviceEventLifecycleNew(dev->name, + VIR_NODE_DEVICE_EVENT_DELETED, + 0); + virNodeDeviceObjLock(obj); virNodeDeviceObjRemove(&driver->devs, obj); out: if (obj) virNodeDeviceObjUnlock(obj); + testObjectEventQueue(driver, event); VIR_FREE(parent_name); VIR_FREE(wwnn); VIR_FREE(wwpn); @@ -5667,6 +5680,39 @@ testConnectStoragePoolEventDeregisterAny(virConnectPtr conn, return ret; } +static int +testConnectNodeDeviceEventRegisterAny(virConnectPtr conn, + virNodeDevicePtr dev, + int eventID, + virConnectNodeDeviceEventGenericCallback callback, + void *opaque, + virFreeCallback freecb) +{ + testDriverPtr driver = conn->privateData; + int ret; + + if (virNodeDeviceEventStateRegisterID(conn, driver->eventState, + dev, eventID, callback, + opaque, freecb, &ret) < 0) + ret = -1; + + return ret; +} + +static int +testConnectNodeDeviceEventDeregisterAny(virConnectPtr conn, + int callbackID) +{ + testDriverPtr driver = conn->privateData; + int ret = 0; + + if (virObjectEventStateDeregisterID(conn, driver->eventState, + callbackID) < 0) + ret = -1; + + return ret; +} + static int testConnectListAllDomains(virConnectPtr conn, virDomainPtr **domains, unsigned int flags) @@ -6809,6 +6855,8 @@ static virStorageDriver testStorageDriver = { }; static virNodeDeviceDriver testNodeDeviceDriver = { + .connectNodeDeviceEventRegisterAny = testConnectNodeDeviceEventRegisterAny, /* 2.2.0 */ + .connectNodeDeviceEventDeregisterAny = testConnectNodeDeviceEventDeregisterAny, /* 2.2.0 */ .nodeNumOfDevices = testNodeNumOfDevices, /* 0.7.2 */ .nodeListDevices = testNodeListDevices, /* 0.7.2 */ .nodeDeviceLookupByName = testNodeDeviceLookupByName, /* 0.7.2 */ diff --git a/tests/objecteventtest.c b/tests/objecteventtest.c index 5e8087bc08..d25a9e2c59 100644 --- a/tests/objecteventtest.c +++ b/tests/objecteventtest.c @@ -61,12 +61,25 @@ static const char storagePoolDef[] = " \n" "\n"; +static const char nodeDeviceDef[] = +"\n" +" test-scsi-host-vport\n" +" \n" +" \n" +" 1111222233334444\n" +" 5555666677778888\n" +" \n" +" \n" +"\n"; + typedef struct { int startEvents; int stopEvents; int defineEvents; int undefineEvents; int unexpectedEvents; + int createdEvents; + int deletedEvents; } lifecycleEventCounter; static void @@ -77,12 +90,15 @@ lifecycleEventCounter_reset(lifecycleEventCounter *counter) counter->defineEvents = 0; counter->undefineEvents = 0; counter->unexpectedEvents = 0; + counter->createdEvents = 0; + counter->deletedEvents = 0; } typedef struct { virConnectPtr conn; virNetworkPtr net; virStoragePoolPtr pool; + virNodeDevicePtr dev; } objecteventTest; @@ -163,6 +179,21 @@ storagePoolRefreshCb(virConnectPtr conn ATTRIBUTE_UNUSED, (*counter)++; } +static void +nodeDeviceLifecycleCb(virConnectPtr conn ATTRIBUTE_UNUSED, + virNodeDevicePtr dev ATTRIBUTE_UNUSED, + int event, + int detail ATTRIBUTE_UNUSED, + void* opaque) +{ + lifecycleEventCounter *counter = opaque; + + if (event == VIR_NODE_DEVICE_EVENT_CREATED) + counter->createdEvents++; + else if (event == VIR_NODE_DEVICE_EVENT_DELETED) + counter->deletedEvents++; +} + static int testDomainCreateXMLOld(const void *data) { @@ -691,6 +722,42 @@ testStoragePoolStartStopEvent(const void *data) return ret; } +static int +testNodeDeviceCreateXML(const void *data) +{ + const objecteventTest *test = data; + lifecycleEventCounter counter; + virNodeDevicePtr dev; + int id; + int ret = 0; + + lifecycleEventCounter_reset(&counter); + + id = virConnectNodeDeviceEventRegisterAny(test->conn, NULL, + VIR_NODE_DEVICE_EVENT_ID_LIFECYCLE, + VIR_NODE_DEVICE_EVENT_CALLBACK(&nodeDeviceLifecycleCb), + &counter, NULL); + dev = virNodeDeviceCreateXML(test->conn, nodeDeviceDef, 0); + virNodeDeviceDestroy(dev); + + if (!dev || virEventRunDefaultImpl() < 0) { + ret = -1; + goto cleanup; + } + + if (counter.createdEvents != 1 || counter.deletedEvents != 1 || + counter.unexpectedEvents > 0) { + ret = -1; + goto cleanup; + } + + cleanup: + virConnectNodeDeviceEventDeregisterAny(test->conn, id); + if (dev) + virNodeDeviceFree(dev); + return ret; +} + static void timeout(int id ATTRIBUTE_UNUSED, void *opaque ATTRIBUTE_UNUSED) { @@ -765,6 +832,11 @@ mymain(void) testStoragePoolStartStopEvent, &test) < 0) ret = EXIT_FAILURE; + /* Node device event tests */ + if (virTestRun("Node device createXML add event ", + testNodeDeviceCreateXML, &test) < 0) + ret = EXIT_FAILURE; + /* Cleanup */ if (test.pool) { virStoragePoolUndefine(test.pool);