summaryrefslogtreecommitdiff
path: root/tests/objecteventtest.c
diff options
context:
space:
mode:
authorJovanka Gulicoska <jovanka.gulicoska@gmail.com>2016-06-15 20:15:39 +0200
committerCole Robinson <crobinso@redhat.com>2016-06-16 12:22:11 -0400
commitedc1a27a5af66314dd04f15749aac687c072bed1 (patch)
tree7dbf10624167f932bb4cbda82b5a8628e2ed006b /tests/objecteventtest.c
parentdc7b849a0cac8f9a70ac56488ce974ea41b9320b (diff)
downloadlibvirt-edc1a27a5af66314dd04f15749aac687c072bed1.tar.gz
test: implement storage lifecycle event APIs
Also includes unittests for storage pool lifecycle events API
Diffstat (limited to 'tests/objecteventtest.c')
-rw-r--r--tests/objecteventtest.c177
1 files changed, 177 insertions, 0 deletions
diff --git a/tests/objecteventtest.c b/tests/objecteventtest.c
index c77b0cd148..ac8cce3ed1 100644
--- a/tests/objecteventtest.c
+++ b/tests/objecteventtest.c
@@ -53,12 +53,21 @@ static const char networkDef[] =
" </ip>\n"
"</network>\n";
+static const char storagePoolDef[] =
+"<pool type='dir'>\n"
+" <name>P</name>\n"
+" <target>\n"
+" <path>/target-path</path>\n"
+" </target>\n"
+"</pool>\n";
+
typedef struct {
int startEvents;
int stopEvents;
int defineEvents;
int undefineEvents;
int unexpectedEvents;
+ int refreshEvents;
} lifecycleEventCounter;
static void
@@ -69,11 +78,13 @@ lifecycleEventCounter_reset(lifecycleEventCounter *counter)
counter->defineEvents = 0;
counter->undefineEvents = 0;
counter->unexpectedEvents = 0;
+ counter->refreshEvents = 0;
}
typedef struct {
virConnectPtr conn;
virNetworkPtr net;
+ virStoragePoolPtr pool;
} objecteventTest;
@@ -125,6 +136,26 @@ networkLifecycleCb(virConnectPtr conn ATTRIBUTE_UNUSED,
counter->undefineEvents++;
}
+static void
+storagePoolLifecycleCb(virConnectPtr conn ATTRIBUTE_UNUSED,
+ virStoragePoolPtr pool ATTRIBUTE_UNUSED,
+ int event,
+ int detail ATTRIBUTE_UNUSED,
+ void* opaque)
+{
+ lifecycleEventCounter *counter = opaque;
+
+ if (event == VIR_STORAGE_POOL_EVENT_STARTED)
+ counter->startEvents++;
+ else if (event == VIR_STORAGE_POOL_EVENT_STOPPED)
+ counter->stopEvents++;
+ else if (event == VIR_STORAGE_POOL_EVENT_DEFINED)
+ counter->defineEvents++;
+ else if (event == VIR_STORAGE_POOL_EVENT_UNDEFINED)
+ counter->undefineEvents++;
+ else if (event == VIR_STORAGE_POOL_EVENT_REFRESHED)
+ counter->refreshEvents++;
+}
static int
testDomainCreateXMLOld(const void *data)
@@ -523,6 +554,130 @@ testNetworkStartStopEvent(const void *data)
return ret;
}
+static int
+testStoragePoolCreateXML(const void *data)
+{
+ const objecteventTest *test = data;
+ lifecycleEventCounter counter;
+ virStoragePoolPtr pool;
+ int id;
+ int ret = 0;
+
+ lifecycleEventCounter_reset(&counter);
+
+ id = virConnectStoragePoolEventRegisterAny(test->conn, NULL,
+ VIR_STORAGE_POOL_EVENT_ID_LIFECYCLE,
+ VIR_STORAGE_POOL_EVENT_CALLBACK(&storagePoolLifecycleCb),
+ &counter, NULL);
+ pool = virStoragePoolCreateXML(test->conn, storagePoolDef, 0);
+
+ if (!pool || virEventRunDefaultImpl() < 0) {
+ ret = -1;
+ goto cleanup;
+ }
+
+ if (counter.startEvents != 1 || counter.unexpectedEvents > 0) {
+ ret = -1;
+ goto cleanup;
+ }
+
+ cleanup:
+ virConnectStoragePoolEventDeregisterAny(test->conn, id);
+ if (pool) {
+ virStoragePoolDestroy(pool);
+ virStoragePoolFree(pool);
+ }
+ return ret;
+}
+
+static int
+testStoragePoolDefine(const void *data)
+{
+ const objecteventTest *test = data;
+ lifecycleEventCounter counter;
+ virStoragePoolPtr pool;
+ int id;
+ int ret = 0;
+
+ lifecycleEventCounter_reset(&counter);
+
+ id = virConnectStoragePoolEventRegisterAny(test->conn, NULL,
+ VIR_STORAGE_POOL_EVENT_ID_LIFECYCLE,
+ VIR_STORAGE_POOL_EVENT_CALLBACK(&storagePoolLifecycleCb),
+ &counter, NULL);
+
+ /* Make sure the define event is triggered */
+ pool = virStoragePoolDefineXML(test->conn, storagePoolDef, 0);
+
+ if (!pool || virEventRunDefaultImpl() < 0) {
+ ret = -1;
+ goto cleanup;
+ }
+
+ if (counter.defineEvents != 1 || counter.unexpectedEvents > 0) {
+ ret = -1;
+ goto cleanup;
+ }
+
+ /* Make sure the undefine event is triggered */
+ virStoragePoolUndefine(pool);
+
+ if (virEventRunDefaultImpl() < 0) {
+ ret = -1;
+ goto cleanup;
+ }
+
+ if (counter.undefineEvents != 1 || counter.unexpectedEvents > 0) {
+ ret = -1;
+ goto cleanup;
+ }
+
+
+ cleanup:
+ virConnectStoragePoolEventDeregisterAny(test->conn, id);
+ if (pool)
+ virStoragePoolFree(pool);
+
+ return ret;
+}
+
+static int
+testStoragePoolStartStopEvent(const void *data)
+{
+ const objecteventTest *test = data;
+ lifecycleEventCounter counter;
+ int id;
+ int ret = 0;
+
+ if (!test->pool)
+ return -1;
+
+ lifecycleEventCounter_reset(&counter);
+
+ id = virConnectStoragePoolEventRegisterAny(test->conn, test->pool,
+ VIR_STORAGE_POOL_EVENT_ID_LIFECYCLE,
+ VIR_STORAGE_POOL_EVENT_CALLBACK(&storagePoolLifecycleCb),
+ &counter, NULL);
+ virStoragePoolCreate(test->pool, 0);
+ virStoragePoolRefresh(test->pool, 0);
+ virStoragePoolDestroy(test->pool);
+
+ if (virEventRunDefaultImpl() < 0) {
+ ret = -1;
+ goto cleanup;
+ }
+
+ if (counter.startEvents != 1 || counter.stopEvents != 1 ||
+ counter.refreshEvents != 1 || counter.unexpectedEvents > 0) {
+ ret = -1;
+ goto cleanup;
+ }
+
+ cleanup:
+ virConnectStoragePoolEventDeregisterAny(test->conn, id);
+ return ret;
+}
+
static void
timeout(int id ATTRIBUTE_UNUSED, void *opaque ATTRIBUTE_UNUSED)
{
@@ -581,6 +736,28 @@ mymain(void)
virNetworkUndefine(test.net);
virNetworkFree(test.net);
}
+
+ /* Storage pool event tests */
+ if (virTestRun("Storage pool createXML start event ",
+ testStoragePoolCreateXML, &test) < 0)
+ ret = EXIT_FAILURE;
+ if (virTestRun("Storage pool (un)define events",
+ testStoragePoolDefine, &test) < 0)
+ ret = EXIT_FAILURE;
+
+ /* Define a test storage pool */
+ if (!(test.pool = virStoragePoolDefineXML(test.conn, storagePoolDef, 0)))
+ ret = EXIT_FAILURE;
+ if (virTestRun("Storage pool start stop events ",
+ testStoragePoolStartStopEvent, &test) < 0)
+ ret = EXIT_FAILURE;
+
+ /* Cleanup */
+ if (test.pool) {
+ virStoragePoolUndefine(test.pool);
+ virStoragePoolFree(test.pool);
+ }
+
virConnectClose(test.conn);
virEventRemoveTimeout(timer);