[libvirt] [PATCH 6/7] Add test for virStorageBackendISCSISession

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


Parse iscsiadm output with and without the recently introduced
flashnode info. [1]

This should check that commits like 57e17a7 (fixing [2]) do
not break iscsiadm output parsing.

[1] https://github.com/mikechristie/open-iscsi/commit/181af9a
[2] https://bugzilla.redhat.com/show_bug.cgi?id=1067173
---
 src/Makefile.am                                  |   3 +-
 src/storage/storage_backend_iscsi.c              |   4 +-
 src/storage/storage_backend_iscsipriv.h          |  31 ++++++
 tests/Makefile.am                                |  14 ++-
 tests/storageiscsidata/iscsiadm_session          |   6 ++
 tests/storageiscsidata/iscsiadm_session_nonflash |   6 ++
 tests/storageiscsitest.c                         | 115 +++++++++++++++++++++++
 7 files changed, 174 insertions(+), 5 deletions(-)
 create mode 100644 src/storage/storage_backend_iscsipriv.h
 create mode 100644 tests/storageiscsidata/iscsiadm_session
 create mode 100644 tests/storageiscsidata/iscsiadm_session_nonflash
 create mode 100644 tests/storageiscsitest.c

diff --git a/src/Makefile.am b/src/Makefile.am
index 25b33a7..055af83 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -823,7 +823,8 @@ STORAGE_DRIVER_LVM_SOURCES =					\
 		storage/storage_backend_logical.c
 
 STORAGE_DRIVER_ISCSI_SOURCES =					\
-		storage/storage_backend_iscsi.h storage/storage_backend_iscsi.c
+		storage/storage_backend_iscsi.h storage/storage_backend_iscsi.c \
+		storage/storage_backend_iscsipriv.h
 
 STORAGE_DRIVER_SCSI_SOURCES =					\
 		storage/storage_backend_scsi.h storage/storage_backend_scsi.c
diff --git a/src/storage/storage_backend_iscsi.c b/src/storage/storage_backend_iscsi.c
index 531d044..143412d 100644
--- a/src/storage/storage_backend_iscsi.c
+++ b/src/storage/storage_backend_iscsi.c
@@ -36,7 +36,7 @@
 #include "driver.h"
 #include "virerror.h"
 #include "storage_backend_scsi.h"
-#include "storage_backend_iscsi.h"
+#include "storage_backend_iscsipriv.h"
 #include "viralloc.h"
 #include "virlog.h"
 #include "virfile.h"
@@ -90,7 +90,7 @@ virStorageBackendISCSIExtractSession(virStoragePoolObjPtr pool,
     return 0;
 }
 
-static char *
+char *
 virStorageBackendISCSISession(virStoragePoolObjPtr pool,
                               bool probe)
 {
diff --git a/src/storage/storage_backend_iscsipriv.h b/src/storage/storage_backend_iscsipriv.h
new file mode 100644
index 0000000..25a2509
--- /dev/null
+++ b/src/storage/storage_backend_iscsipriv.h
@@ -0,0 +1,31 @@
+/*
+ * storage_backend_iscsipriv.h: functions for thesting the iscsi backend
+ *
+ * Copyright (C) 2014 Red Hat, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library.  If not, see
+ * <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef __VIR_STORAGE_BACKEND_ISCSIPRIV_H__
+# define __VIR_STORAGE_BACKEND_ISCSIPRIV_H__
+
+# include "storage_backend_iscsi.h"
+
+char *
+virStorageBackendISCSISession(virStoragePoolObjPtr pool,
+                              bool probe);
+
+
+#endif /* __VIR_STORAGE_BACKEND_ISCSIPRIV_H__ */
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 3267ad3..b4147ed 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -105,6 +105,7 @@ EXTRA_DIST =		\
 	storagepoolschematest \
 	storagepoolxml2xmlin \
 	storagepoolxml2xmlout \
+	storageiscsidata \
 	storagevolxml2argvdata \
 	storagevolschemadata \
 	storagevolschematest \
@@ -247,7 +248,8 @@ endif WITH_STORAGE_SHEEPDOG
 test_programs += nwfilterxml2xmltest
 
 if WITH_STORAGE
-test_programs += storagevolxml2argvtest
+test_programs += storagevolxml2argvtest \
+	storageiscsitest
 endif WITH_STORAGE
 
 if WITH_LINUX
@@ -642,8 +644,16 @@ storagevolxml2argvtest_SOURCES = \
     testutils.c testutils.h
 storagevolxml2argvtest_LDADD = \
 	../src/libvirt_driver_storage_impl.la $(LDADDS)
+
+storageiscsitest_SOURCES = \
+	storageiscsitest.c \
+	testutils.c testutils.h
+storageiscsitest_LDADD = \
+	../src/libvirt_driver_storage_impl.la $(LDADDS)
+
 else ! WITH_STORAGE
-EXTRA_DIST += storagevolxml2argvtest.c
+EXTRA_DIST += storagevolxml2argvtest.c \
+	storageiscsitest.c
 endif ! WITH_STORAGE
 
 storagevolxml2xmltest_SOURCES = \
diff --git a/tests/storageiscsidata/iscsiadm_session b/tests/storageiscsidata/iscsiadm_session
new file mode 100644
index 0000000..52425be
--- /dev/null
+++ b/tests/storageiscsidata/iscsiadm_session
@@ -0,0 +1,6 @@
+tcp: [1] 10.20.30.40:3260,1 iqn.2004-06.example:example1:iscsi.test (non-flash)
+tcp: [2] 10.20.30.41:3260,1 iqn.2005-05.example:example1:iscsi.hello (non-flash)
+tcp: [3] 10.20.30.42:3260,1 iqn.2006-04.example:example1:iscsi.world (non-flash)
+tcp: [5] 10.20.30.43:3260,1 iqn.2007-04.example:example1:iscsi.foo (non-flash)
+tcp: [6] 10.20.30.44:3260,1 iqn.2008-04.example:example1:iscsi.bar (non-flash)
+tcp: [7] 10.20.30.45:3260,1 iqn.2009-04.example:example1:iscsi.seven (non-flash)
diff --git a/tests/storageiscsidata/iscsiadm_session_nonflash b/tests/storageiscsidata/iscsiadm_session_nonflash
new file mode 100644
index 0000000..52425be
--- /dev/null
+++ b/tests/storageiscsidata/iscsiadm_session_nonflash
@@ -0,0 +1,6 @@
+tcp: [1] 10.20.30.40:3260,1 iqn.2004-06.example:example1:iscsi.test (non-flash)
+tcp: [2] 10.20.30.41:3260,1 iqn.2005-05.example:example1:iscsi.hello (non-flash)
+tcp: [3] 10.20.30.42:3260,1 iqn.2006-04.example:example1:iscsi.world (non-flash)
+tcp: [5] 10.20.30.43:3260,1 iqn.2007-04.example:example1:iscsi.foo (non-flash)
+tcp: [6] 10.20.30.44:3260,1 iqn.2008-04.example:example1:iscsi.bar (non-flash)
+tcp: [7] 10.20.30.45:3260,1 iqn.2009-04.example:example1:iscsi.seven (non-flash)
diff --git a/tests/storageiscsitest.c b/tests/storageiscsitest.c
new file mode 100644
index 0000000..212f524
--- /dev/null
+++ b/tests/storageiscsitest.c
@@ -0,0 +1,115 @@
+/*
+ * Copyright (C) 2014 Red Hat, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library.  If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * Author: Jan Tomko <jtomko at redhat.com>
+ */
+
+#include <config.h>
+#include <stdlib.h>
+
+#include "testutils.h"
+#include "storage_conf.h"
+#include "storage/storage_backend_iscsipriv.h"
+#include "vircommandpriv.h"
+
+#define VIR_FROM_THIS VIR_FROM_NONE
+
+struct testSessionInfo {
+    const char *device_path;
+    const char *fake_cmd_output;
+    const char *expected_session;
+};
+
+static int
+testISCSISession(const void *data)
+{
+    const struct testSessionInfo *info = data;
+    char *cmdout = NULL;
+    char *tmp = NULL;
+    int ret = -1;
+    char *actual_session = NULL;
+
+    /* virStorageBackendISCSISession requires virStoragePoolObjPtr,
+     * but it only uses obj.def->source.devices[0].path */
+    virStoragePoolObj obj = { .def = NULL };
+    virStoragePoolSourceDevice srcdev = { .path = NULL };
+
+    if (VIR_ALLOC(obj.def) < 0)
+        goto cleanup;
+
+    if (VIR_STRDUP(srcdev.path, info->device_path) < 0)
+        goto cleanup;
+
+    if (VIR_APPEND_ELEMENT(obj.def->source.devices,
+                           obj.def->source.ndevice,
+                           srcdev) < 0)
+        goto cleanup;
+
+    if (virAsprintf(&cmdout, "%s/storageiscsidata/%s", abs_srcdir,
+                    info->fake_cmd_output) < 0)
+        goto cleanup;
+
+    virCommandSetMockOutputFile(cmdout);
+
+    actual_session = virStorageBackendISCSISession(&obj, true);
+
+    if (STRNEQ_NULLABLE(actual_session, info->expected_session)) {
+        virReportError(VIR_ERR_INTERNAL_ERROR,
+                       "Expected session: '%s' got: '%s'",
+                       NULLSTR(info->expected_session),
+                       NULLSTR(actual_session));
+    } else {
+        ret = 0;
+    }
+
+cleanup:
+    virCommandSetMockOutputFile(NULL);
+    virStoragePoolSourceDeviceClear(&srcdev);
+    virStoragePoolDefFree(obj.def);
+    VIR_FREE(cmdout);
+    VIR_FREE(tmp);
+    VIR_FREE(actual_session);
+    return ret;
+}
+
+static int
+mymain(void)
+{
+    int rv = 0;
+
+#define DO_SESSION_TEST(name, session)                                      \
+    do {                                                                    \
+        struct testSessionInfo info = {name, "iscsiadm_session", session};  \
+        if (virtTestRun("ISCSI session test" name,                          \
+                        testISCSISession, &info) < 0)                       \
+            rv = -1;                                                        \
+        info.fake_cmd_output = "iscsiadm_session_nonflash";                 \
+        if (virtTestRun("ISCSI (non-flash) session test" name,              \
+                        testISCSISession, &info) < 0)                       \
+            rv = -1;                                                        \
+    } while (0)
+
+    DO_SESSION_TEST("iqn.2004-06.example:example1:iscsi.test", "1");
+    DO_SESSION_TEST("iqn.2009-04.example:example1:iscsi.seven", "7");
+    DO_SESSION_TEST("iqn.2009-04.example:example1:iscsi.eight", NULL);
+
+    if (rv < 0)
+        return EXIT_FAILURE;
+    return EXIT_SUCCESS;
+}
+
+VIRT_TEST_MAIN(mymain)
-- 
1.8.3.2




More information about the libvir-list mailing list