c401cc
From 919acf18b62408b6d4317e24c47986997c07d178 Mon Sep 17 00:00:00 2001
c401cc
Message-Id: <919acf18b62408b6d4317e24c47986997c07d178@dist-git>
c401cc
From: Osier Yang <jyang@redhat.com>
c401cc
Date: Sun, 2 Feb 2014 23:09:41 +0800
c401cc
Subject: [PATCH] tests: Add tests for scsi utils
c401cc
c401cc
https://bugzilla.redhat.com/show_bug.cgi?id=957292
c401cc
c401cc
This creates the basic unit tests for the scsi utils.
c401cc
c401cc
Signed-off-by: Osier Yang <jyang@redhat.com>
c401cc
(cherry picked from commit 6fdece9a332fc668a89bde96af94e7b7cbf6750d)
c401cc
c401cc
Conflicts:
c401cc
	tests/Makefile.am:
c401cc
          * "virpcitestdata" is not backported
c401cc
          * Commit 4679e942 is backported
c401cc
Build failure fix:
c401cc
        tests/virscsitest.c:
c401cc
          * Commit eee6eb666c is not backported
c401cc
Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
c401cc
---
c401cc
 .gitignore                                     |   1 +
c401cc
 tests/Makefile.am                              |  14 ++
c401cc
 tests/virscsidata/0:0:0:0/block/sda/dev        |   1 +
c401cc
 tests/virscsidata/0:0:0:0/model                |   1 +
c401cc
 tests/virscsidata/0:0:0:0/scsi_generic/sg0/dev |   1 +
c401cc
 tests/virscsidata/0:0:0:0/vendor               |   1 +
c401cc
 tests/virscsidata/1:0:0:0/block/sr0/dev        |   1 +
c401cc
 tests/virscsidata/1:0:0:0/model                |   1 +
c401cc
 tests/virscsidata/1:0:0:0/scsi_generic/sg1/dev |   1 +
c401cc
 tests/virscsidata/1:0:0:0/vendor               |   1 +
c401cc
 tests/virscsitest.c                            | 188 +++++++++++++++++++++++++
c401cc
 11 files changed, 211 insertions(+)
c401cc
 create mode 100644 tests/virscsidata/0:0:0:0/block/sda/dev
c401cc
 create mode 100644 tests/virscsidata/0:0:0:0/model
c401cc
 create mode 100644 tests/virscsidata/0:0:0:0/scsi_generic/sg0/dev
c401cc
 create mode 100644 tests/virscsidata/0:0:0:0/vendor
c401cc
 create mode 100644 tests/virscsidata/1:0:0:0/block/sr0/dev
c401cc
 create mode 100644 tests/virscsidata/1:0:0:0/model
c401cc
 create mode 100644 tests/virscsidata/1:0:0:0/scsi_generic/sg1/dev
c401cc
 create mode 100644 tests/virscsidata/1:0:0:0/vendor
c401cc
 create mode 100644 tests/virscsitest.c
c401cc
c401cc
diff --git a/tests/Makefile.am b/tests/Makefile.am
c401cc
index 2ec8ffb..88b2224 100644
c401cc
--- a/tests/Makefile.am
c401cc
+++ b/tests/Makefile.am
c401cc
@@ -103,6 +103,7 @@ EXTRA_DIST =		\
c401cc
 	test-lib.sh	\
c401cc
 	virsh-uriprecedence \
c401cc
 	virfiledata \
c401cc
+	virscsidata \
c401cc
 	vmx2xmldata \
c401cc
 	xencapsdata \
c401cc
 	xmconfigdata \
c401cc
@@ -212,6 +213,10 @@ if WITH_STORAGE
c401cc
 test_programs += storagevolxml2argvtest
c401cc
 endif
c401cc
 
c401cc
+if WITH_LINUX
c401cc
+test_programs += virscsitest
c401cc
+endif WITH_LINUX
c401cc
+
c401cc
 test_programs += storagevolxml2xmltest storagepoolxml2xmltest
c401cc
 
c401cc
 test_programs += nodedevxml2xmltest
c401cc
@@ -812,6 +817,15 @@ fchosttest_SOURCES = \
c401cc
        fchosttest.c testutils.h testutils.c
c401cc
 fchosttest_LDADD = $(LDADDS)
c401cc
 
c401cc
+if WITH_LINUX
c401cc
+virscsitest_SOURCES = \
c401cc
+       virscsitest.c testutils.h testutils.c
c401cc
+virscsitest_LDADD = $(LDADDS)
c401cc
+
c401cc
+else ! WITH_LINUX
c401cc
+EXTRA_DIST += virscsitest.c
c401cc
+endif  ! WITH_LINUX
c401cc
+
c401cc
 if WITH_CIL
c401cc
 CILOPTFLAGS =
c401cc
 CILOPTINCS =
c401cc
diff --git a/tests/virscsidata/0:0:0:0/block/sda/dev b/tests/virscsidata/0:0:0:0/block/sda/dev
c401cc
new file mode 100644
c401cc
index 0000000..fae0a50
c401cc
--- /dev/null
c401cc
+++ b/tests/virscsidata/0:0:0:0/block/sda/dev
c401cc
@@ -0,0 +1 @@
c401cc
+8:0
c401cc
diff --git a/tests/virscsidata/0:0:0:0/model b/tests/virscsidata/0:0:0:0/model
c401cc
new file mode 100644
c401cc
index 0000000..4d7e3ef
c401cc
--- /dev/null
c401cc
+++ b/tests/virscsidata/0:0:0:0/model
c401cc
@@ -0,0 +1 @@
c401cc
+TOSHIBA MK5061GS
c401cc
diff --git a/tests/virscsidata/0:0:0:0/scsi_generic/sg0/dev b/tests/virscsidata/0:0:0:0/scsi_generic/sg0/dev
c401cc
new file mode 100644
c401cc
index 0000000..992e920
c401cc
--- /dev/null
c401cc
+++ b/tests/virscsidata/0:0:0:0/scsi_generic/sg0/dev
c401cc
@@ -0,0 +1 @@
c401cc
+21:0
c401cc
diff --git a/tests/virscsidata/0:0:0:0/vendor b/tests/virscsidata/0:0:0:0/vendor
c401cc
new file mode 100644
c401cc
index 0000000..531030d
c401cc
--- /dev/null
c401cc
+++ b/tests/virscsidata/0:0:0:0/vendor
c401cc
@@ -0,0 +1 @@
c401cc
+ATA
c401cc
diff --git a/tests/virscsidata/1:0:0:0/block/sr0/dev b/tests/virscsidata/1:0:0:0/block/sr0/dev
c401cc
new file mode 100644
c401cc
index 0000000..3d33f0f
c401cc
--- /dev/null
c401cc
+++ b/tests/virscsidata/1:0:0:0/block/sr0/dev
c401cc
@@ -0,0 +1 @@
c401cc
+11:0
c401cc
diff --git a/tests/virscsidata/1:0:0:0/model b/tests/virscsidata/1:0:0:0/model
c401cc
new file mode 100644
c401cc
index 0000000..4d8a0e5
c401cc
--- /dev/null
c401cc
+++ b/tests/virscsidata/1:0:0:0/model
c401cc
@@ -0,0 +1 @@
c401cc
+DVDRAM GT50N
c401cc
diff --git a/tests/virscsidata/1:0:0:0/scsi_generic/sg1/dev b/tests/virscsidata/1:0:0:0/scsi_generic/sg1/dev
c401cc
new file mode 100644
c401cc
index 0000000..bd84814
c401cc
--- /dev/null
c401cc
+++ b/tests/virscsidata/1:0:0:0/scsi_generic/sg1/dev
c401cc
@@ -0,0 +1 @@
c401cc
+21:1
c401cc
diff --git a/tests/virscsidata/1:0:0:0/vendor b/tests/virscsidata/1:0:0:0/vendor
c401cc
new file mode 100644
c401cc
index 0000000..d0383d4
c401cc
--- /dev/null
c401cc
+++ b/tests/virscsidata/1:0:0:0/vendor
c401cc
@@ -0,0 +1 @@
c401cc
+HL-DT-ST
c401cc
diff --git a/tests/virscsitest.c b/tests/virscsitest.c
c401cc
new file mode 100644
c401cc
index 0000000..f95898b
c401cc
--- /dev/null
c401cc
+++ b/tests/virscsitest.c
c401cc
@@ -0,0 +1,188 @@
c401cc
+/*
c401cc
+ * Copyright (C) 2014 Red Hat, Inc.
c401cc
+ *
c401cc
+ * This library is free software; you can redistribute it and/or
c401cc
+ * modify it under the terms of the GNU Lesser General Public
c401cc
+ * License as published by the Free Software Foundation; either
c401cc
+ * version 2.1 of the License, or (at your option) any later version.
c401cc
+ *
c401cc
+ * This library is distributed in the hope that it will be useful,
c401cc
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
c401cc
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
c401cc
+ * Lesser General Public License for more details.
c401cc
+ *
c401cc
+ * You should have received a copy of the GNU Lesser General Public
c401cc
+ * License along with this library.  If not, see
c401cc
+ * <http://www.gnu.org/licenses/>.
c401cc
+ *
c401cc
+ * Author: Osier Yang <jyang@redhat.com>
c401cc
+ *
c401cc
+ */
c401cc
+
c401cc
+#include <config.h>
c401cc
+
c401cc
+#include <stdlib.h>
c401cc
+
c401cc
+#include "virscsi.h"
c401cc
+#include "testutils.h"
c401cc
+
c401cc
+#define VIR_FROM_THIS VIR_FROM_NONE
c401cc
+
c401cc
+#define VIR_SCSI_DATA "/virscsidata"
c401cc
+
c401cc
+static const char *abs_top_srcdir;
c401cc
+static char *virscsi_prefix = NULL;
c401cc
+
c401cc
+static int
c401cc
+test1(const void *data ATTRIBUTE_UNUSED)
c401cc
+{
c401cc
+    char *name = NULL;
c401cc
+    int ret = -1;
c401cc
+
c401cc
+    if (!(name = virSCSIDeviceGetDevName(virscsi_prefix,
c401cc
+                                         "scsi_host1", 0, 0, 0)))
c401cc
+        return -1;
c401cc
+
c401cc
+    if (STRNEQ(name, "sr0"))
c401cc
+        goto cleanup;
c401cc
+
c401cc
+    ret = 0;
c401cc
+cleanup:
c401cc
+    VIR_FREE(name);
c401cc
+    return ret;
c401cc
+}
c401cc
+
c401cc
+/*
c401cc
+ * Two test devices are used, one has address "0:0:0:0", the
c401cc
+ * other has address "1:0:0:0", see "virscsidata/" for more
c401cc
+ * details.
c401cc
+ */
c401cc
+static int
c401cc
+test2(const void *data ATTRIBUTE_UNUSED)
c401cc
+{
c401cc
+    virSCSIDeviceListPtr list = NULL;
c401cc
+    virSCSIDevicePtr dev = NULL;
c401cc
+    virSCSIDevicePtr dev1 = NULL;
c401cc
+    bool free_dev = true;
c401cc
+    bool free_dev1 = true;
c401cc
+    virSCSIDevicePtr tmp = NULL;
c401cc
+    char *sgname = NULL;
c401cc
+    int ret = -1;
c401cc
+
c401cc
+    sgname = virSCSIDeviceGetSgName(virscsi_prefix,
c401cc
+                                    "scsi_host1", 0, 0, 0);
c401cc
+
c401cc
+    if (!sgname || STRNEQ(sgname, "sg1"))
c401cc
+        goto cleanup;
c401cc
+
c401cc
+    if (!(dev = virSCSIDeviceNew(virscsi_prefix, "scsi_host1",
c401cc
+                                 0, 0, 0, false, true)))
c401cc
+        goto cleanup;
c401cc
+
c401cc
+    if (STRNEQ_NULLABLE(virSCSIDeviceGetName(dev), "1:0:0:0") ||
c401cc
+        virSCSIDeviceGetAdapter(dev) != 1 ||
c401cc
+        virSCSIDeviceGetBus(dev) != 0 ||
c401cc
+        virSCSIDeviceGetTarget(dev) != 0 ||
c401cc
+        virSCSIDeviceGetUnit(dev) != 0 ||
c401cc
+        virSCSIDeviceGetReadonly(dev) ||
c401cc
+        !virSCSIDeviceGetShareable(dev))
c401cc
+        goto cleanup;
c401cc
+
c401cc
+    if (!virSCSIDeviceIsAvailable(dev))
c401cc
+        goto cleanup;
c401cc
+
c401cc
+    if (virSCSIDeviceSetUsedBy(dev, "fc18") < 0)
c401cc
+        goto cleanup;
c401cc
+
c401cc
+    if (virSCSIDeviceIsAvailable(dev))
c401cc
+        goto cleanup;
c401cc
+
c401cc
+    if (virSCSIDeviceSetUsedBy(dev, "fc20") < 0)
c401cc
+        goto cleanup;
c401cc
+
c401cc
+    if (virSCSIDeviceIsAvailable(dev))
c401cc
+        goto cleanup;
c401cc
+
c401cc
+    if (!(list = virSCSIDeviceListNew()))
c401cc
+        goto cleanup;
c401cc
+
c401cc
+    if (virSCSIDeviceListAdd(list, dev) < 0)
c401cc
+        goto cleanup;
c401cc
+
c401cc
+    /* virSCSIDeviceListDispose will take care of freeing
c401cc
+     * the device.
c401cc
+     */
c401cc
+    free_dev = false;
c401cc
+
c401cc
+    if (!virSCSIDeviceListFind(list, dev))
c401cc
+        goto cleanup;
c401cc
+
c401cc
+    virSCSIDeviceListDel(list, dev, "fc20");
c401cc
+
c401cc
+    if (!virSCSIDeviceListFind(list, dev))
c401cc
+        goto cleanup;
c401cc
+
c401cc
+    if (virSCSIDeviceIsAvailable(dev))
c401cc
+        goto cleanup;
c401cc
+
c401cc
+    if (virSCSIDeviceListCount(list) != 1)
c401cc
+        goto cleanup;
c401cc
+
c401cc
+    if (!(dev1 = virSCSIDeviceNew(virscsi_prefix, "scsi_host0",
c401cc
+                                  0, 0, 0, true, false)))
c401cc
+        goto cleanup;
c401cc
+
c401cc
+    if (virSCSIDeviceListAdd(list, dev1) < 0)
c401cc
+        goto cleanup;
c401cc
+
c401cc
+    /* virSCSIDeviceListDispose will take care of freeing
c401cc
+     * the device.
c401cc
+     */
c401cc
+    free_dev1 = false;
c401cc
+
c401cc
+    if (virSCSIDeviceListCount(list) != 2)
c401cc
+        goto cleanup;
c401cc
+
c401cc
+    if (!(tmp = virSCSIDeviceListSteal(list, dev1)))
c401cc
+        goto cleanup;
c401cc
+    virSCSIDeviceFree(tmp);
c401cc
+
c401cc
+    if (virSCSIDeviceListCount(list) != 1)
c401cc
+        goto cleanup;
c401cc
+
c401cc
+    ret = 0;
c401cc
+cleanup:
c401cc
+    VIR_FREE(sgname);
c401cc
+    if (free_dev)
c401cc
+        virSCSIDeviceFree(dev);
c401cc
+    if (free_dev1)
c401cc
+        virSCSIDeviceFree(dev1);
c401cc
+    virObjectUnref(list);
c401cc
+    return ret;
c401cc
+}
c401cc
+
c401cc
+static int
c401cc
+mymain(void)
c401cc
+{
c401cc
+    int ret = 0;
c401cc
+
c401cc
+    abs_top_srcdir = getenv("abs_top_srcdir");
c401cc
+    if (!abs_top_srcdir)
c401cc
+        abs_top_srcdir = abs_srcdir "/..";
c401cc
+
c401cc
+    if (virAsprintf(&virscsi_prefix, "%s" VIR_SCSI_DATA, abs_srcdir) < 0) {
c401cc
+        ret = -1;
c401cc
+        goto cleanup;
c401cc
+    }
c401cc
+
c401cc
+    if (virtTestRun("test1", 1, test1, NULL) < 0)
c401cc
+        ret = -1;
c401cc
+    if (virtTestRun("test2", 1, test2, NULL) < 0)
c401cc
+        ret = -1;
c401cc
+
c401cc
+cleanup:
c401cc
+    VIR_FREE(virscsi_prefix);
c401cc
+    return ret;
c401cc
+}
c401cc
+
c401cc
+VIRT_TEST_MAIN(mymain)
c401cc
-- 
c401cc
1.8.5.4
c401cc