[libvirt] [PATCH 7/7] Add a test for virStorageBackendISCSIScanTargets

Ján Tomko jtomko at redhat.com
Wed Mar 12 13:08:17 UTC 2014


Check the iscsiadm output parsing by reading it from a file.
---
 src/storage/storage_backend_iscsi.c         |  2 +-
 src/storage/storage_backend_iscsipriv.h     |  5 ++
 tests/storageiscsidata/iscsiadm_sendtargets |  6 +++
 tests/storageiscsitest.c                    | 71 +++++++++++++++++++++++++++++
 4 files changed, 83 insertions(+), 1 deletion(-)
 create mode 100644 tests/storageiscsidata/iscsiadm_sendtargets

diff --git a/src/storage/storage_backend_iscsi.c b/src/storage/storage_backend_iscsi.c
index 143412d..316fb1b 100644
--- a/src/storage/storage_backend_iscsi.c
+++ b/src/storage/storage_backend_iscsi.c
@@ -470,7 +470,7 @@ virStorageBackendISCSITargetAutologin(const char *portal,
 }
 
 
-static int
+int
 virStorageBackendISCSIScanTargets(const char *portal,
                                   const char *initiatoriqn,
                                   size_t *ntargetsret,
diff --git a/src/storage/storage_backend_iscsipriv.h b/src/storage/storage_backend_iscsipriv.h
index 25a2509..897bfb4 100644
--- a/src/storage/storage_backend_iscsipriv.h
+++ b/src/storage/storage_backend_iscsipriv.h
@@ -27,5 +27,10 @@ char *
 virStorageBackendISCSISession(virStoragePoolObjPtr pool,
                               bool probe);
 
+int
+virStorageBackendISCSIScanTargets(const char *portal,
+                                  const char *initiatoriqn,
+                                  size_t *ntargetsret,
+                                  char ***targetsret);
 
 #endif /* __VIR_STORAGE_BACKEND_ISCSIPRIV_H__ */
diff --git a/tests/storageiscsidata/iscsiadm_sendtargets b/tests/storageiscsidata/iscsiadm_sendtargets
new file mode 100644
index 0000000..acea8ee
--- /dev/null
+++ b/tests/storageiscsidata/iscsiadm_sendtargets
@@ -0,0 +1,6 @@
+10.20.30.40:3260,1 iqn.2004-06.example:example1:iscsi.test
+10.20.30.40:3260,1 iqn.2005-05.example:example1:iscsi.hello
+10.20.30.40:3260,1 iqn.2006-04.example:example1:iscsi.world
+10.20.30.40:3260,1 iqn.2007-04.example:example1:iscsi.foo
+10.20.30.40:3260,1 iqn.2008-04.example:example1:iscsi.bar
+10.20.30.40:3260,1 iqn.2009-04.example:example1:iscsi.seven
diff --git a/tests/storageiscsitest.c b/tests/storageiscsitest.c
index 212f524..304bb12 100644
--- a/tests/storageiscsitest.c
+++ b/tests/storageiscsitest.c
@@ -86,6 +86,60 @@ cleanup:
     return ret;
 }
 
+struct testScanTargetsInfo {
+    const char *fake_cmd_output;
+    const char *portal;
+    const char **expected_targets;
+    size_t nexpected;
+};
+
+static int
+testISCSIScanTargets(const void *data)
+{
+    const struct testScanTargetsInfo *info = data;
+    size_t ntargets = 0;
+    char **targets = NULL;
+    char *cmdout = NULL;
+    int ret = -1;
+    size_t i;
+
+    if (virAsprintf(&cmdout, "%s/storageiscsidata/%s", abs_srcdir,
+                    info->fake_cmd_output) < 0)
+        goto cleanup;
+
+    virCommandSetMockOutputFile(cmdout);
+
+    if (virStorageBackendISCSIScanTargets(info->portal, NULL,
+                                          &ntargets, &targets) < 0)
+        goto cleanup;
+
+    if (info->nexpected != ntargets) {
+        virReportError(VIR_ERR_INTERNAL_ERROR,
+                       "Expected %zu targets, got %zu",
+                       info->nexpected, ntargets);
+        goto cleanup;
+    }
+
+    for (i = 0; i < ntargets; i++) {
+        if (STRNEQ(info->expected_targets[i], targets[i])) {
+            virReportError(VIR_ERR_INTERNAL_ERROR,
+                           "Expected target '%s', got '%s'",
+                           info->expected_targets[i], targets[i]);
+            goto cleanup;
+        }
+    }
+
+    ret = 0;
+
+cleanup:
+    virCommandSetMockOutputFile(NULL);
+    for (i = 0; i < ntargets; i++)
+        VIR_FREE(targets[i]);
+    VIR_FREE(targets);
+    VIR_FREE(cmdout);
+    return ret;
+}
+
 static int
 mymain(void)
 {
@@ -107,6 +161,23 @@ mymain(void)
     DO_SESSION_TEST("iqn.2009-04.example:example1:iscsi.seven", "7");
     DO_SESSION_TEST("iqn.2009-04.example:example1:iscsi.eight", NULL);
 
+    const char *targets[] = {
+        "iqn.2004-06.example:example1:iscsi.test",
+        "iqn.2005-05.example:example1:iscsi.hello",
+        "iqn.2006-04.example:example1:iscsi.world",
+        "iqn.2007-04.example:example1:iscsi.foo",
+        "iqn.2008-04.example:example1:iscsi.bar",
+        "iqn.2009-04.example:example1:iscsi.seven"
+    };
+    struct testScanTargetsInfo infoTargets = {
+        .fake_cmd_output = "iscsiadm_sendtargets",
+        .portal = "10.20.30.45:3260,1",
+        .expected_targets = targets,
+        .nexpected = ARRAY_CARDINALITY(targets),
+    };
+    if (virtTestRun("ISCSI scan targets", testISCSIScanTargets, &infoTargets) < 0)
+        rv = -1;
+
     if (rv < 0)
         return EXIT_FAILURE;
     return EXIT_SUCCESS;
-- 
1.8.3.2




More information about the libvir-list mailing list