2 * Copyright (C) 2014 Red Hat, Inc.
3 * Copyright (C) 2013 SUSE LINUX Products GmbH, Nuernberg, Germany.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; If not, see
17 * <http://www.gnu.org/licenses/>.
24 #include "testutils.h"
26 #define VIR_FROM_THIS VIR_FROM_NONE
29 static const char domainDef
[] =
30 "<domain type='test'>"
31 " <name>test-domain</name>"
32 " <uuid>77a6fc12-07b5-9415-8abb-a803613f2a40</uuid>"
33 " <memory>8388608</memory>"
34 " <currentMemory>2097152</currentMemory>"
41 static const char networkDef
[] =
43 " <name>test</name>\n"
44 " <bridge name=\"virbr0\"/>\n"
46 " <ip address=\"192.168.122.1\" netmask=\"255.255.255.0\">\n"
48 " <range start=\"192.168.122.2\" end=\"192.168.122.254\"/>\n"
53 static const char storagePoolDef
[] =
57 " <path>/target-path</path>\n"
61 static const char nodeDeviceDef
[] =
63 " <parent>scsi_host1</parent>\n"
64 " <capability type='scsi_host'>\n"
65 " <capability type='fc_host'>\n"
66 " <wwpn>1000000023452345</wwpn>\n"
67 " <wwnn>2000000023452345</wwnn>\n"
80 } lifecycleEventCounter
;
86 virStoragePoolPtr pool
;
92 domainLifecycleCb(virConnectPtr conn G_GNUC_UNUSED
,
93 virDomainPtr dom G_GNUC_UNUSED
,
95 int detail G_GNUC_UNUSED
,
98 lifecycleEventCounter
*counter
= opaque
;
101 case VIR_DOMAIN_EVENT_STARTED
:
102 counter
->startEvents
++;
104 case VIR_DOMAIN_EVENT_STOPPED
:
105 counter
->stopEvents
++;
107 case VIR_DOMAIN_EVENT_DEFINED
:
108 counter
->defineEvents
++;
110 case VIR_DOMAIN_EVENT_UNDEFINED
:
111 counter
->undefineEvents
++;
114 /* Ignore other events */
121 networkLifecycleCb(virConnectPtr conn G_GNUC_UNUSED
,
122 virNetworkPtr net G_GNUC_UNUSED
,
124 int detail G_GNUC_UNUSED
,
127 lifecycleEventCounter
*counter
= opaque
;
129 if (event
== VIR_NETWORK_EVENT_STARTED
)
130 counter
->startEvents
++;
131 else if (event
== VIR_NETWORK_EVENT_STOPPED
)
132 counter
->stopEvents
++;
133 else if (event
== VIR_NETWORK_EVENT_DEFINED
)
134 counter
->defineEvents
++;
135 else if (event
== VIR_NETWORK_EVENT_UNDEFINED
)
136 counter
->undefineEvents
++;
140 storagePoolLifecycleCb(virConnectPtr conn G_GNUC_UNUSED
,
141 virStoragePoolPtr pool G_GNUC_UNUSED
,
143 int detail G_GNUC_UNUSED
,
146 lifecycleEventCounter
*counter
= opaque
;
148 if (event
== VIR_STORAGE_POOL_EVENT_STARTED
)
149 counter
->startEvents
++;
150 else if (event
== VIR_STORAGE_POOL_EVENT_STOPPED
)
151 counter
->stopEvents
++;
152 else if (event
== VIR_STORAGE_POOL_EVENT_DEFINED
)
153 counter
->defineEvents
++;
154 else if (event
== VIR_STORAGE_POOL_EVENT_UNDEFINED
)
155 counter
->undefineEvents
++;
156 else if (event
== VIR_STORAGE_POOL_EVENT_CREATED
)
157 counter
->createdEvents
++;
158 else if (event
== VIR_STORAGE_POOL_EVENT_DELETED
)
159 counter
->deletedEvents
++;
163 storagePoolRefreshCb(virConnectPtr conn G_GNUC_UNUSED
,
164 virStoragePoolPtr pool G_GNUC_UNUSED
,
167 int *counter
= opaque
;
173 nodeDeviceLifecycleCb(virConnectPtr conn G_GNUC_UNUSED
,
174 virNodeDevicePtr dev G_GNUC_UNUSED
,
176 int detail G_GNUC_UNUSED
,
179 lifecycleEventCounter
*counter
= opaque
;
181 if (event
== VIR_NODE_DEVICE_EVENT_CREATED
)
182 counter
->createdEvents
++;
183 else if (event
== VIR_NODE_DEVICE_EVENT_DELETED
)
184 counter
->deletedEvents
++;
188 testDomainCreateXMLOld(const void *data
)
190 const objecteventTest
*test
= data
;
191 lifecycleEventCounter counter
= { 0 };
192 virDomainPtr dom
= NULL
;
194 bool registered
= false;
196 if (virConnectDomainEventRegister(test
->conn
,
198 &counter
, NULL
) != 0)
201 dom
= virDomainCreateXML(test
->conn
, domainDef
, 0);
203 if (dom
== NULL
|| virEventRunDefaultImpl() < 0)
206 if (counter
.startEvents
!= 1 || counter
.unexpectedEvents
> 0)
209 if (virConnectDomainEventDeregister(test
->conn
, domainLifecycleCb
) != 0)
216 virConnectDomainEventDeregister(test
->conn
, domainLifecycleCb
);
218 virDomainDestroy(dom
);
226 testDomainCreateXMLNew(const void *data
)
228 const objecteventTest
*test
= data
;
229 lifecycleEventCounter counter
= { 0 };
230 int eventId
= VIR_DOMAIN_EVENT_ID_LIFECYCLE
;
231 virDomainPtr dom
= NULL
;
235 id
= virConnectDomainEventRegisterAny(test
->conn
, NULL
, eventId
,
236 VIR_DOMAIN_EVENT_CALLBACK(&domainLifecycleCb
),
240 dom
= virDomainCreateXML(test
->conn
, domainDef
, 0);
242 if (dom
== NULL
|| virEventRunDefaultImpl() < 0)
245 if (counter
.startEvents
!= 1 || counter
.unexpectedEvents
> 0)
248 if (virConnectDomainEventDeregisterAny(test
->conn
, id
) != 0)
255 virConnectDomainEventDeregisterAny(test
->conn
, id
);
257 virDomainDestroy(dom
);
265 testDomainCreateXMLMixed(const void *data
)
267 const objecteventTest
*test
= data
;
268 lifecycleEventCounter counter
= { 0 };
273 bool registered
= false;
275 /* Fun with mixing old and new API, also with global and
276 * per-domain. Handler should be fired three times, once for each
278 dom
= virDomainDefineXML(test
->conn
, domainDef
);
282 id1
= virConnectDomainEventRegisterAny(test
->conn
, dom
,
283 VIR_DOMAIN_EVENT_ID_LIFECYCLE
,
284 VIR_DOMAIN_EVENT_CALLBACK(&domainLifecycleCb
),
288 if (virConnectDomainEventRegister(test
->conn
,
290 &counter
, NULL
) != 0)
293 id2
= virConnectDomainEventRegisterAny(test
->conn
, NULL
,
294 VIR_DOMAIN_EVENT_ID_LIFECYCLE
,
295 VIR_DOMAIN_EVENT_CALLBACK(&domainLifecycleCb
),
300 virDomainUndefine(dom
);
301 virDomainDestroy(dom
);
304 dom
= virDomainCreateXML(test
->conn
, domainDef
, 0);
305 if (dom
== NULL
|| virEventRunDefaultImpl() < 0)
308 if (counter
.startEvents
!= 3 || counter
.unexpectedEvents
> 0)
311 if (virConnectDomainEventDeregister(test
->conn
, domainLifecycleCb
) != 0)
314 if (virConnectDomainEventDeregisterAny(test
->conn
, id1
) != 0)
317 if (virConnectDomainEventDeregisterAny(test
->conn
, id2
) != 0)
324 virConnectDomainEventDeregisterAny(test
->conn
, id1
);
326 virConnectDomainEventDeregisterAny(test
->conn
, id2
);
328 virConnectDomainEventDeregister(test
->conn
, domainLifecycleCb
);
330 virDomainUndefine(dom
);
331 virDomainDestroy(dom
);
340 testDomainDefine(const void *data
)
342 const objecteventTest
*test
= data
;
343 lifecycleEventCounter counter
= { 0 };
344 int eventId
= VIR_DOMAIN_EVENT_ID_LIFECYCLE
;
345 virDomainPtr dom
= NULL
;
349 id
= virConnectDomainEventRegisterAny(test
->conn
, NULL
, eventId
,
350 VIR_DOMAIN_EVENT_CALLBACK(&domainLifecycleCb
),
353 /* Make sure the define event is triggered */
354 dom
= virDomainDefineXML(test
->conn
, domainDef
);
356 if (dom
== NULL
|| virEventRunDefaultImpl() < 0) {
360 if (counter
.defineEvents
!= 1 || counter
.unexpectedEvents
> 0) {
364 /* Make sure the undefine event is triggered */
365 virDomainUndefine(dom
);
367 if (virEventRunDefaultImpl() < 0) {
371 if (counter
.undefineEvents
!= 1 || counter
.unexpectedEvents
> 0) {
377 virConnectDomainEventDeregisterAny(test
->conn
, id
);
385 testDomainStartStopEvent(const void *data
)
387 const objecteventTest
*test
= data
;
388 lifecycleEventCounter counter
= { 0 };
389 int eventId
= VIR_DOMAIN_EVENT_ID_LIFECYCLE
;
393 virConnectPtr conn2
= NULL
;
394 virDomainPtr dom2
= NULL
;
396 dom
= virDomainLookupByName(test
->conn
, "test");
400 id
= virConnectDomainEventRegisterAny(test
->conn
, dom
, eventId
,
401 VIR_DOMAIN_EVENT_CALLBACK(&domainLifecycleCb
),
404 /* Test domain is started */
405 virDomainDestroy(dom
);
406 if (virDomainCreate(dom
) < 0)
409 if (virEventRunDefaultImpl() < 0)
412 if (counter
.startEvents
!= 1 || counter
.stopEvents
!= 1 ||
413 counter
.unexpectedEvents
> 0)
416 /* Repeat the test, but this time, trigger the events via an
417 * alternate connection. */
418 if (!(conn2
= virConnectOpen("test:///default")))
420 if (!(dom2
= virDomainLookupByName(conn2
, "test")))
423 if (virDomainDestroy(dom2
) < 0)
425 if (virDomainCreate(dom2
) < 0)
428 if (virEventRunDefaultImpl() < 0)
431 if (counter
.startEvents
!= 2 || counter
.stopEvents
!= 2 ||
432 counter
.unexpectedEvents
> 0)
437 virConnectDomainEventDeregisterAny(test
->conn
, id
);
442 virConnectClose(conn2
);
448 testNetworkCreateXML(const void *data
)
450 const objecteventTest
*test
= data
;
451 lifecycleEventCounter counter
= { 0 };
456 id
= virConnectNetworkEventRegisterAny(test
->conn
, NULL
,
457 VIR_NETWORK_EVENT_ID_LIFECYCLE
,
458 VIR_NETWORK_EVENT_CALLBACK(&networkLifecycleCb
),
460 net
= virNetworkCreateXML(test
->conn
, networkDef
);
462 if (!net
|| virEventRunDefaultImpl() < 0) {
466 if (counter
.startEvents
!= 1 || counter
.unexpectedEvents
> 0) {
472 virConnectNetworkEventDeregisterAny(test
->conn
, id
);
474 virNetworkDestroy(net
);
481 testNetworkDefine(const void *data
)
483 const objecteventTest
*test
= data
;
484 lifecycleEventCounter counter
= { 0 };
489 id
= virConnectNetworkEventRegisterAny(test
->conn
, NULL
,
490 VIR_NETWORK_EVENT_ID_LIFECYCLE
,
491 VIR_NETWORK_EVENT_CALLBACK(&networkLifecycleCb
),
494 /* Make sure the define event is triggered */
495 net
= virNetworkDefineXML(test
->conn
, networkDef
);
497 if (!net
|| virEventRunDefaultImpl() < 0) {
501 if (counter
.defineEvents
!= 1 || counter
.unexpectedEvents
> 0) {
505 /* Make sure the undefine event is triggered */
506 virNetworkUndefine(net
);
508 if (virEventRunDefaultImpl() < 0) {
512 if (counter
.undefineEvents
!= 1 || counter
.unexpectedEvents
> 0) {
519 virConnectNetworkEventDeregisterAny(test
->conn
, id
);
527 testNetworkStartStopEvent(const void *data
)
529 const objecteventTest
*test
= data
;
530 lifecycleEventCounter counter
= { 0 };
537 id
= virConnectNetworkEventRegisterAny(test
->conn
, test
->net
,
538 VIR_NETWORK_EVENT_ID_LIFECYCLE
,
539 VIR_NETWORK_EVENT_CALLBACK(&networkLifecycleCb
),
541 virNetworkCreate(test
->net
);
542 virNetworkDestroy(test
->net
);
544 if (virEventRunDefaultImpl() < 0) {
548 if (counter
.startEvents
!= 1 || counter
.stopEvents
!= 1 ||
549 counter
.unexpectedEvents
> 0) {
555 virConnectNetworkEventDeregisterAny(test
->conn
, id
);
561 testStoragePoolCreateXML(const void *data
)
563 const objecteventTest
*test
= data
;
564 lifecycleEventCounter counter
= { 0 };
565 virStoragePoolPtr pool
;
569 id
= virConnectStoragePoolEventRegisterAny(test
->conn
, NULL
,
570 VIR_STORAGE_POOL_EVENT_ID_LIFECYCLE
,
571 VIR_STORAGE_POOL_EVENT_CALLBACK(&storagePoolLifecycleCb
),
573 pool
= virStoragePoolCreateXML(test
->conn
, storagePoolDef
, 0);
575 if (!pool
|| virEventRunDefaultImpl() < 0) {
579 if (counter
.startEvents
!= 1 || counter
.unexpectedEvents
> 0) {
585 virConnectStoragePoolEventDeregisterAny(test
->conn
, id
);
587 virStoragePoolDestroy(pool
);
588 virStoragePoolFree(pool
);
594 testStoragePoolDefine(const void *data
)
596 const objecteventTest
*test
= data
;
597 lifecycleEventCounter counter
= { 0 };
598 virStoragePoolPtr pool
;
602 id
= virConnectStoragePoolEventRegisterAny(test
->conn
, NULL
,
603 VIR_STORAGE_POOL_EVENT_ID_LIFECYCLE
,
604 VIR_STORAGE_POOL_EVENT_CALLBACK(&storagePoolLifecycleCb
),
607 /* Make sure the define event is triggered */
608 pool
= virStoragePoolDefineXML(test
->conn
, storagePoolDef
, 0);
610 if (!pool
|| virEventRunDefaultImpl() < 0) {
614 if (counter
.defineEvents
!= 1 || counter
.unexpectedEvents
> 0) {
618 /* Make sure the undefine event is triggered */
619 virStoragePoolUndefine(pool
);
621 if (virEventRunDefaultImpl() < 0) {
625 if (counter
.undefineEvents
!= 1 || counter
.unexpectedEvents
> 0) {
631 virConnectStoragePoolEventDeregisterAny(test
->conn
, id
);
633 virStoragePoolFree(pool
);
639 testStoragePoolStartStopEvent(const void *data
)
641 const objecteventTest
*test
= data
;
642 lifecycleEventCounter counter
= { 0 };
643 int refreshCounter
= 0;
650 id1
= virConnectStoragePoolEventRegisterAny(test
->conn
, test
->pool
,
651 VIR_STORAGE_POOL_EVENT_ID_LIFECYCLE
,
652 VIR_STORAGE_POOL_EVENT_CALLBACK(&storagePoolLifecycleCb
),
654 id2
= virConnectStoragePoolEventRegisterAny(test
->conn
, test
->pool
,
655 VIR_STORAGE_POOL_EVENT_ID_REFRESH
,
656 VIR_STORAGE_POOL_EVENT_CALLBACK(&storagePoolRefreshCb
),
657 &refreshCounter
, NULL
);
658 virStoragePoolCreate(test
->pool
, 0);
659 virStoragePoolRefresh(test
->pool
, 0);
660 virStoragePoolDestroy(test
->pool
);
662 if (virEventRunDefaultImpl() < 0) {
666 if (counter
.startEvents
!= 1 || counter
.stopEvents
!= 1 ||
667 refreshCounter
!= 1 || counter
.unexpectedEvents
> 0) {
673 virConnectStoragePoolEventDeregisterAny(test
->conn
, id1
);
674 virConnectStoragePoolEventDeregisterAny(test
->conn
, id2
);
679 testStoragePoolBuild(const void *data
)
681 const objecteventTest
*test
= data
;
682 lifecycleEventCounter counter
= { 0 };
686 id
= virConnectStoragePoolEventRegisterAny(test
->conn
, NULL
,
687 VIR_STORAGE_POOL_EVENT_ID_LIFECYCLE
,
688 VIR_STORAGE_POOL_EVENT_CALLBACK(&storagePoolLifecycleCb
),
691 virStoragePoolBuild(test
->pool
, 0);
693 if (virEventRunDefaultImpl() < 0) {
697 if (counter
.createdEvents
!= 1) {
703 virConnectStoragePoolEventDeregisterAny(test
->conn
, id
);
708 testStoragePoolDelete(const void *data
)
710 const objecteventTest
*test
= data
;
711 lifecycleEventCounter counter
= { 0 };
715 id
= virConnectStoragePoolEventRegisterAny(test
->conn
, NULL
,
716 VIR_STORAGE_POOL_EVENT_ID_LIFECYCLE
,
717 VIR_STORAGE_POOL_EVENT_CALLBACK(&storagePoolLifecycleCb
),
720 virStoragePoolDelete(test
->pool
, 0);
722 if (virEventRunDefaultImpl() < 0) {
726 if (counter
.deletedEvents
!= 1) {
732 virConnectStoragePoolEventDeregisterAny(test
->conn
, id
);
736 testNodeDeviceCreateXML(const void *data
)
738 const objecteventTest
*test
= data
;
739 lifecycleEventCounter counter
= { 0 };
740 virNodeDevicePtr dev
;
744 id
= virConnectNodeDeviceEventRegisterAny(test
->conn
, NULL
,
745 VIR_NODE_DEVICE_EVENT_ID_LIFECYCLE
,
746 VIR_NODE_DEVICE_EVENT_CALLBACK(&nodeDeviceLifecycleCb
),
748 dev
= virNodeDeviceCreateXML(test
->conn
, nodeDeviceDef
, 0);
749 virNodeDeviceDestroy(dev
);
751 if (!dev
|| virEventRunDefaultImpl() < 0) {
755 if (counter
.createdEvents
!= 1 || counter
.deletedEvents
!= 1 ||
756 counter
.unexpectedEvents
> 0) {
762 virConnectNodeDeviceEventDeregisterAny(test
->conn
, id
);
764 virNodeDeviceFree(dev
);
769 timeout(int id G_GNUC_UNUSED
, void *opaque G_GNUC_UNUSED
)
771 fputs("test taking too long; giving up", stderr
);
778 objecteventTest test
= { 0 };
779 int ret
= EXIT_SUCCESS
;
782 virEventRegisterDefaultImpl();
784 /* Set up a timer to abort this test if it takes 10 seconds. */
785 if ((timer
= virEventAddTimeout(10 * 1000, timeout
, NULL
, NULL
)) < 0)
788 if (!(test
.conn
= virConnectOpen("test:///default")))
791 virTestQuiesceLibvirtErrors(false);
793 /* Domain event tests */
794 if (virTestRun("Domain createXML start event (old API)",
795 testDomainCreateXMLOld
, &test
) < 0)
797 if (virTestRun("Domain createXML start event (new API)",
798 testDomainCreateXMLNew
, &test
) < 0)
800 if (virTestRun("Domain createXML start event (both API)",
801 testDomainCreateXMLMixed
, &test
) < 0)
803 if (virTestRun("Domain (un)define events", testDomainDefine
, &test
) < 0)
805 if (virTestRun("Domain start stop events", testDomainStartStopEvent
, &test
) < 0)
808 /* Network event tests */
809 /* Tests requiring the test network not to be set up */
810 if (virTestRun("Network createXML start event ", testNetworkCreateXML
, &test
) < 0)
812 if (virTestRun("Network (un)define events", testNetworkDefine
, &test
) < 0)
815 /* Define a test network */
816 if (!(test
.net
= virNetworkDefineXML(test
.conn
, networkDef
)))
818 if (virTestRun("Network start stop events ", testNetworkStartStopEvent
, &test
) < 0)
823 virNetworkUndefine(test
.net
);
824 virNetworkFree(test
.net
);
827 /* Storage pool event tests */
828 if (virTestRun("Storage pool createXML start event ",
829 testStoragePoolCreateXML
, &test
) < 0)
831 if (virTestRun("Storage pool (un)define events",
832 testStoragePoolDefine
, &test
) < 0)
835 /* Define a test storage pool */
836 if (!(test
.pool
= virStoragePoolDefineXML(test
.conn
, storagePoolDef
, 0)))
838 if (virTestRun("Storage pool start stop events ",
839 testStoragePoolStartStopEvent
, &test
) < 0)
841 /* Storage pool build and delete events */
842 if (virTestRun("Storage pool build event ",
843 testStoragePoolBuild
, &test
) < 0)
845 if (virTestRun("Storage pool delete event ",
846 testStoragePoolDelete
, &test
) < 0)
849 /* Node device event tests */
850 if (virTestRun("Node device createXML add event ",
851 testNodeDeviceCreateXML
, &test
) < 0)
856 virStoragePoolUndefine(test
.pool
);
857 virStoragePoolFree(test
.pool
);
860 virConnectClose(test
.conn
);
861 virEventRemoveTimeout(timer
);
866 VIR_TEST_MAIN(mymain
)