Blob Blame History Raw
From 919acf18b62408b6d4317e24c47986997c07d178 Mon Sep 17 00:00:00 2001
Message-Id: <919acf18b62408b6d4317e24c47986997c07d178@dist-git>
From: Osier Yang <jyang@redhat.com>
Date: Sun, 2 Feb 2014 23:09:41 +0800
Subject: [PATCH] tests: Add tests for scsi utils

https://bugzilla.redhat.com/show_bug.cgi?id=957292

This creates the basic unit tests for the scsi utils.

Signed-off-by: Osier Yang <jyang@redhat.com>
(cherry picked from commit 6fdece9a332fc668a89bde96af94e7b7cbf6750d)

Conflicts:
	tests/Makefile.am:
          * "virpcitestdata" is not backported
          * Commit 4679e942 is backported
Build failure fix:
        tests/virscsitest.c:
          * Commit eee6eb666c is not backported
Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
---
 .gitignore                                     |   1 +
 tests/Makefile.am                              |  14 ++
 tests/virscsidata/0:0:0:0/block/sda/dev        |   1 +
 tests/virscsidata/0:0:0:0/model                |   1 +
 tests/virscsidata/0:0:0:0/scsi_generic/sg0/dev |   1 +
 tests/virscsidata/0:0:0:0/vendor               |   1 +
 tests/virscsidata/1:0:0:0/block/sr0/dev        |   1 +
 tests/virscsidata/1:0:0:0/model                |   1 +
 tests/virscsidata/1:0:0:0/scsi_generic/sg1/dev |   1 +
 tests/virscsidata/1:0:0:0/vendor               |   1 +
 tests/virscsitest.c                            | 188 +++++++++++++++++++++++++
 11 files changed, 211 insertions(+)
 create mode 100644 tests/virscsidata/0:0:0:0/block/sda/dev
 create mode 100644 tests/virscsidata/0:0:0:0/model
 create mode 100644 tests/virscsidata/0:0:0:0/scsi_generic/sg0/dev
 create mode 100644 tests/virscsidata/0:0:0:0/vendor
 create mode 100644 tests/virscsidata/1:0:0:0/block/sr0/dev
 create mode 100644 tests/virscsidata/1:0:0:0/model
 create mode 100644 tests/virscsidata/1:0:0:0/scsi_generic/sg1/dev
 create mode 100644 tests/virscsidata/1:0:0:0/vendor
 create mode 100644 tests/virscsitest.c

diff --git a/tests/Makefile.am b/tests/Makefile.am
index 2ec8ffb..88b2224 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -103,6 +103,7 @@ EXTRA_DIST =		\
 	test-lib.sh	\
 	virsh-uriprecedence \
 	virfiledata \
+	virscsidata \
 	vmx2xmldata \
 	xencapsdata \
 	xmconfigdata \
@@ -212,6 +213,10 @@ if WITH_STORAGE
 test_programs += storagevolxml2argvtest
 endif
 
+if WITH_LINUX
+test_programs += virscsitest
+endif WITH_LINUX
+
 test_programs += storagevolxml2xmltest storagepoolxml2xmltest
 
 test_programs += nodedevxml2xmltest
@@ -812,6 +817,15 @@ fchosttest_SOURCES = \
        fchosttest.c testutils.h testutils.c
 fchosttest_LDADD = $(LDADDS)
 
+if WITH_LINUX
+virscsitest_SOURCES = \
+       virscsitest.c testutils.h testutils.c
+virscsitest_LDADD = $(LDADDS)
+
+else ! WITH_LINUX
+EXTRA_DIST += virscsitest.c
+endif  ! WITH_LINUX
+
 if WITH_CIL
 CILOPTFLAGS =
 CILOPTINCS =
diff --git a/tests/virscsidata/0:0:0:0/block/sda/dev b/tests/virscsidata/0:0:0:0/block/sda/dev
new file mode 100644
index 0000000..fae0a50
--- /dev/null
+++ b/tests/virscsidata/0:0:0:0/block/sda/dev
@@ -0,0 +1 @@
+8:0
diff --git a/tests/virscsidata/0:0:0:0/model b/tests/virscsidata/0:0:0:0/model
new file mode 100644
index 0000000..4d7e3ef
--- /dev/null
+++ b/tests/virscsidata/0:0:0:0/model
@@ -0,0 +1 @@
+TOSHIBA MK5061GS
diff --git a/tests/virscsidata/0:0:0:0/scsi_generic/sg0/dev b/tests/virscsidata/0:0:0:0/scsi_generic/sg0/dev
new file mode 100644
index 0000000..992e920
--- /dev/null
+++ b/tests/virscsidata/0:0:0:0/scsi_generic/sg0/dev
@@ -0,0 +1 @@
+21:0
diff --git a/tests/virscsidata/0:0:0:0/vendor b/tests/virscsidata/0:0:0:0/vendor
new file mode 100644
index 0000000..531030d
--- /dev/null
+++ b/tests/virscsidata/0:0:0:0/vendor
@@ -0,0 +1 @@
+ATA
diff --git a/tests/virscsidata/1:0:0:0/block/sr0/dev b/tests/virscsidata/1:0:0:0/block/sr0/dev
new file mode 100644
index 0000000..3d33f0f
--- /dev/null
+++ b/tests/virscsidata/1:0:0:0/block/sr0/dev
@@ -0,0 +1 @@
+11:0
diff --git a/tests/virscsidata/1:0:0:0/model b/tests/virscsidata/1:0:0:0/model
new file mode 100644
index 0000000..4d8a0e5
--- /dev/null
+++ b/tests/virscsidata/1:0:0:0/model
@@ -0,0 +1 @@
+DVDRAM GT50N
diff --git a/tests/virscsidata/1:0:0:0/scsi_generic/sg1/dev b/tests/virscsidata/1:0:0:0/scsi_generic/sg1/dev
new file mode 100644
index 0000000..bd84814
--- /dev/null
+++ b/tests/virscsidata/1:0:0:0/scsi_generic/sg1/dev
@@ -0,0 +1 @@
+21:1
diff --git a/tests/virscsidata/1:0:0:0/vendor b/tests/virscsidata/1:0:0:0/vendor
new file mode 100644
index 0000000..d0383d4
--- /dev/null
+++ b/tests/virscsidata/1:0:0:0/vendor
@@ -0,0 +1 @@
+HL-DT-ST
diff --git a/tests/virscsitest.c b/tests/virscsitest.c
new file mode 100644
index 0000000..f95898b
--- /dev/null
+++ b/tests/virscsitest.c
@@ -0,0 +1,188 @@
+/*
+ * 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: Osier Yang <jyang@redhat.com>
+ *
+ */
+
+#include <config.h>
+
+#include <stdlib.h>
+
+#include "virscsi.h"
+#include "testutils.h"
+
+#define VIR_FROM_THIS VIR_FROM_NONE
+
+#define VIR_SCSI_DATA "/virscsidata"
+
+static const char *abs_top_srcdir;
+static char *virscsi_prefix = NULL;
+
+static int
+test1(const void *data ATTRIBUTE_UNUSED)
+{
+    char *name = NULL;
+    int ret = -1;
+
+    if (!(name = virSCSIDeviceGetDevName(virscsi_prefix,
+                                         "scsi_host1", 0, 0, 0)))
+        return -1;
+
+    if (STRNEQ(name, "sr0"))
+        goto cleanup;
+
+    ret = 0;
+cleanup:
+    VIR_FREE(name);
+    return ret;
+}
+
+/*
+ * Two test devices are used, one has address "0:0:0:0", the
+ * other has address "1:0:0:0", see "virscsidata/" for more
+ * details.
+ */
+static int
+test2(const void *data ATTRIBUTE_UNUSED)
+{
+    virSCSIDeviceListPtr list = NULL;
+    virSCSIDevicePtr dev = NULL;
+    virSCSIDevicePtr dev1 = NULL;
+    bool free_dev = true;
+    bool free_dev1 = true;
+    virSCSIDevicePtr tmp = NULL;
+    char *sgname = NULL;
+    int ret = -1;
+
+    sgname = virSCSIDeviceGetSgName(virscsi_prefix,
+                                    "scsi_host1", 0, 0, 0);
+
+    if (!sgname || STRNEQ(sgname, "sg1"))
+        goto cleanup;
+
+    if (!(dev = virSCSIDeviceNew(virscsi_prefix, "scsi_host1",
+                                 0, 0, 0, false, true)))
+        goto cleanup;
+
+    if (STRNEQ_NULLABLE(virSCSIDeviceGetName(dev), "1:0:0:0") ||
+        virSCSIDeviceGetAdapter(dev) != 1 ||
+        virSCSIDeviceGetBus(dev) != 0 ||
+        virSCSIDeviceGetTarget(dev) != 0 ||
+        virSCSIDeviceGetUnit(dev) != 0 ||
+        virSCSIDeviceGetReadonly(dev) ||
+        !virSCSIDeviceGetShareable(dev))
+        goto cleanup;
+
+    if (!virSCSIDeviceIsAvailable(dev))
+        goto cleanup;
+
+    if (virSCSIDeviceSetUsedBy(dev, "fc18") < 0)
+        goto cleanup;
+
+    if (virSCSIDeviceIsAvailable(dev))
+        goto cleanup;
+
+    if (virSCSIDeviceSetUsedBy(dev, "fc20") < 0)
+        goto cleanup;
+
+    if (virSCSIDeviceIsAvailable(dev))
+        goto cleanup;
+
+    if (!(list = virSCSIDeviceListNew()))
+        goto cleanup;
+
+    if (virSCSIDeviceListAdd(list, dev) < 0)
+        goto cleanup;
+
+    /* virSCSIDeviceListDispose will take care of freeing
+     * the device.
+     */
+    free_dev = false;
+
+    if (!virSCSIDeviceListFind(list, dev))
+        goto cleanup;
+
+    virSCSIDeviceListDel(list, dev, "fc20");
+
+    if (!virSCSIDeviceListFind(list, dev))
+        goto cleanup;
+
+    if (virSCSIDeviceIsAvailable(dev))
+        goto cleanup;
+
+    if (virSCSIDeviceListCount(list) != 1)
+        goto cleanup;
+
+    if (!(dev1 = virSCSIDeviceNew(virscsi_prefix, "scsi_host0",
+                                  0, 0, 0, true, false)))
+        goto cleanup;
+
+    if (virSCSIDeviceListAdd(list, dev1) < 0)
+        goto cleanup;
+
+    /* virSCSIDeviceListDispose will take care of freeing
+     * the device.
+     */
+    free_dev1 = false;
+
+    if (virSCSIDeviceListCount(list) != 2)
+        goto cleanup;
+
+    if (!(tmp = virSCSIDeviceListSteal(list, dev1)))
+        goto cleanup;
+    virSCSIDeviceFree(tmp);
+
+    if (virSCSIDeviceListCount(list) != 1)
+        goto cleanup;
+
+    ret = 0;
+cleanup:
+    VIR_FREE(sgname);
+    if (free_dev)
+        virSCSIDeviceFree(dev);
+    if (free_dev1)
+        virSCSIDeviceFree(dev1);
+    virObjectUnref(list);
+    return ret;
+}
+
+static int
+mymain(void)
+{
+    int ret = 0;
+
+    abs_top_srcdir = getenv("abs_top_srcdir");
+    if (!abs_top_srcdir)
+        abs_top_srcdir = abs_srcdir "/..";
+
+    if (virAsprintf(&virscsi_prefix, "%s" VIR_SCSI_DATA, abs_srcdir) < 0) {
+        ret = -1;
+        goto cleanup;
+    }
+
+    if (virtTestRun("test1", 1, test1, NULL) < 0)
+        ret = -1;
+    if (virtTestRun("test2", 1, test2, NULL) < 0)
+        ret = -1;
+
+cleanup:
+    VIR_FREE(virscsi_prefix);
+    return ret;
+}
+
+VIRT_TEST_MAIN(mymain)
-- 
1.8.5.4