[libvirt] [PATCH 4/9] test: implement node device lifecycle event APIs

Cole Robinson crobinso at redhat.com
Tue Jul 26 18:28:23 UTC 2016


On 07/20/2016 09:50 AM, Jovanka Gulicoska wrote:
> Also includes unittests for node device lifecycle events API
> ---
>  src/test/test_driver.c  | 49 +++++++++++++++++++++++++++++++++
>  tests/objecteventtest.c | 72 +++++++++++++++++++++++++++++++++++++++++++++++++
>  2 files changed, 121 insertions(+)
> 
> diff --git a/src/test/test_driver.c b/src/test/test_driver.c
> index 672c163..8233d74 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"
> @@ -504,6 +505,7 @@ static const char *defaultPoolSourcesNetFSXML =
>  "  </source>\n"
>  "</sources>\n";
>  
> +

Stray whitespace change, please drop this

>  static const unsigned long long defaultPoolCap = (100 * 1024 * 1024 * 1024ull);
>  static const unsigned long long defaultPoolAlloc;
>  
> @@ -5427,6 +5429,7 @@ testNodeDeviceCreateXML(virConnectPtr conn,
>      int parent_host = -1;
>      virNodeDevicePtr dev = NULL;
>      virNodeDevCapsDefPtr caps;
> +    virObjectEventPtr event = NULL;
>  
>      virCheckFlags(0, NULL);
>  
> @@ -5470,11 +5473,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 +5496,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 +5530,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 +5681,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 +6856,8 @@ static virStorageDriver testStorageDriver = {
>  };
>  
>  static virNodeDeviceDriver testNodeDeviceDriver = {
> +    .connectNodeDeviceEventRegisterAny = testConnectNodeDeviceEventRegisterAny, /* 2.1.0 */
> +    .connectNodeDeviceEventDeregisterAny = testConnectNodeDeviceEventDeregisterAny, /* 2.1.0 */
>      .nodeNumOfDevices = testNodeNumOfDevices, /* 0.7.2 */
>      .nodeListDevices = testNodeListDevices, /* 0.7.2 */
>      .nodeDeviceLookupByName = testNodeDeviceLookupByName, /* 0.7.2 */

These versions will need to be updated to 2.2.0

> diff --git a/tests/objecteventtest.c b/tests/objecteventtest.c
> index 5e8087b..d25a9e2 100644
> --- a/tests/objecteventtest.c
> +++ b/tests/objecteventtest.c
> @@ -61,12 +61,25 @@ static const char storagePoolDef[] =
>  "  </target>\n"
>  "</pool>\n";
>  
> +static const char nodeDeviceDef[] =
> +"<device>\n"
> +"  <parent>test-scsi-host-vport</parent>\n"
> +"  <capability type='scsi_host'>\n"
> +"    <capability type='fc_host'>\n"
> +"      <wwpn>1111222233334444</wwpn>\n"
> +"      <wwnn>5555666677778888</wwnn>\n"
> +"    </capability>\n"
> +"  </capability>\n"
> +"</device>\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);
> 

Rest looks good to me

Thanks,
Cole




More information about the libvir-list mailing list