fchosttest.c 10.3 KB
Newer Older
O
Osier Yang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * Copyright (C) 2013 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/>.
 *
 */

#include <config.h>

22
#include "virlog.h"
23
#include "virstring.h"
24
#include "virvhba.h"
O
Osier Yang 已提交
25 26
#include "testutils.h"

27 28
#define VIR_FROM_THIS VIR_FROM_NONE

29 30
VIR_LOG_INIT("tests.fchosttest");

31 32 33
static char *fchost_prefix;

#define TEST_FC_HOST_PREFIX fchost_prefix
O
Osier Yang 已提交
34
#define TEST_FC_HOST_NUM 5
35
#define TEST_FC_HOST_NUM_NO_FAB 6
O
Osier Yang 已提交
36

37 38 39 40 41 42 43 44 45 46
/* virNodeDeviceCreateXML using "<parent>" to find the vport capable HBA */
static const char test7_xml[] =
"<device>"
"  <parent>scsi_host1</parent>"
"  <capability type='scsi_host'>"
"    <capability type='fc_host'>"
"    </capability>"
"  </capability>"
"</device>";

47 48 49 50 51 52 53 54 55
/* virNodeDeviceCreateXML without "<parent>" to find the vport capable HBA */
static const char test8_xml[] =
"<device>"
"  <capability type='scsi_host'>"
"    <capability type='fc_host'>"
"    </capability>"
"  </capability>"
"</device>";

56 57 58 59 60 61 62 63 64 65 66
/* virNodeDeviceCreateXML using "<parent wwnn='%s' wwpn='%s'/>" to find
 * the vport capable HBA */
static const char test9_xml[] =
"<device>"
"  <parent wwnn='2000000012341234' wwpn='1000000012341234'/>"
"  <capability type='scsi_host'>"
"    <capability type='fc_host'>"
"    </capability>"
"  </capability>"
"</device>";

67 68 69 70 71 72 73 74 75 76 77
/* virNodeDeviceCreateXML using "<parent fabric_wwn='%s'/>" to find the
 * vport capable HBA */
static const char test10_xml[] =
"<device>"
"  <parent fabric_wwn='2000000043214321'/>"
"  <capability type='scsi_host'>"
"    <capability type='fc_host'>"
"    </capability>"
"  </capability>"
"</device>";

78 79 80 81 82 83 84 85 86 87 88 89 90
/* virStoragePoolCreateXML using parent='%s' to find the vport capable HBA */
static const char test11_xml[] =
"<pool type='scsi'>"
"  <name>vhba_pool</name>"
"  <source>"
"    <adapter type='fc_host' parent='scsi_host1' wwnn='20000000c9831b4b' wwpn='10000000c9831b4b'/>"
"  </source>"
"  <target>"
"    <path>/dev/disk/by-path</path>"
"  </target>"
"</pool>";


91
/* Test virIsVHBACapable */
O
Osier Yang 已提交
92 93 94
static int
test1(const void *data ATTRIBUTE_UNUSED)
{
95 96
    if (virVHBAPathExists(TEST_FC_HOST_PREFIX, TEST_FC_HOST_NUM) &&
        virVHBAPathExists(TEST_FC_HOST_PREFIX, TEST_FC_HOST_NUM_NO_FAB))
O
Osier Yang 已提交
97 98 99 100 101
        return 0;

    return -1;
}

102
/* Test virVHBAIsVportCapable */
O
Osier Yang 已提交
103 104 105
static int
test2(const void *data ATTRIBUTE_UNUSED)
{
106
    if (virVHBAIsVportCapable(TEST_FC_HOST_PREFIX, TEST_FC_HOST_NUM))
O
Osier Yang 已提交
107 108 109 110 111
        return 0;

    return -1;
}

112
/* Test virVHBAGetConfig */
O
Osier Yang 已提交
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
static int
test3(const void *data ATTRIBUTE_UNUSED)
{
    const char *expect_wwnn = "2001001b32a9da4e";
    const char *expect_wwpn = "2101001b32a9da4e";
    const char *expect_fabric_wwn = "2001000dec9877c1";
    const char *expect_max_vports = "127";
    const char *expect_vports = "0";
    char *wwnn = NULL;
    char *wwpn = NULL;
    char *fabric_wwn = NULL;
    char *max_vports = NULL;
    char *vports = NULL;
    int ret = -1;

128 129
    if (!(wwnn = virVHBAGetConfig(TEST_FC_HOST_PREFIX, TEST_FC_HOST_NUM,
                                  "node_name")))
O
Osier Yang 已提交
130 131
        return -1;

132 133
    if (!(wwpn = virVHBAGetConfig(TEST_FC_HOST_PREFIX, TEST_FC_HOST_NUM,
                                  "port_name")))
O
Osier Yang 已提交
134 135
        goto cleanup;

136 137
    if (!(fabric_wwn = virVHBAGetConfig(TEST_FC_HOST_PREFIX, TEST_FC_HOST_NUM,
                                        "fabric_name")))
O
Osier Yang 已提交
138 139
        goto cleanup;

140 141
    if (!(max_vports = virVHBAGetConfig(TEST_FC_HOST_PREFIX, TEST_FC_HOST_NUM,
                                        "max_npiv_vports")))
O
Osier Yang 已提交
142 143 144
        goto cleanup;


145 146
    if (!(vports = virVHBAGetConfig(TEST_FC_HOST_PREFIX, TEST_FC_HOST_NUM,
                                    "npiv_vports_inuse")))
O
Osier Yang 已提交
147 148 149 150 151 152 153 154 155 156
        goto cleanup;

    if (STRNEQ(expect_wwnn, wwnn) ||
        STRNEQ(expect_wwpn, wwpn) ||
        STRNEQ(expect_fabric_wwn, fabric_wwn) ||
        STRNEQ(expect_max_vports, max_vports) ||
        STRNEQ(expect_vports, vports))
        goto cleanup;

    ret = 0;
157
 cleanup:
O
Osier Yang 已提交
158 159 160 161 162 163 164 165
    VIR_FREE(wwnn);
    VIR_FREE(wwpn);
    VIR_FREE(fabric_wwn);
    VIR_FREE(max_vports);
    VIR_FREE(vports);
    return ret;
}

166
/* Test virVHBAGetHostByWWN */
O
Osier Yang 已提交
167 168 169 170 171 172 173
static int
test4(const void *data ATTRIBUTE_UNUSED)
{
    const char *expect_hostname = "host5";
    char *hostname = NULL;
    int ret = -1;

174 175 176
    if (!(hostname = virVHBAGetHostByWWN(TEST_FC_HOST_PREFIX,
                                         "2001001b32a9da4e",
                                         "2101001b32a9da4e")))
O
Osier Yang 已提交
177 178 179 180 181 182
        return -1;

    if (STRNEQ(hostname, expect_hostname))
        goto cleanup;

    ret = 0;
183
 cleanup:
O
Osier Yang 已提交
184 185 186 187
    VIR_FREE(hostname);
    return ret;
}

188 189 190 191
/* Test virVHBAFindVportHost
 *
 * NB: host4 is not Online, so it should not be found
 */
O
Osier Yang 已提交
192 193 194 195 196 197 198
static int
test5(const void *data ATTRIBUTE_UNUSED)
{
    const char *expect_hostname = "host5";
    char *hostname = NULL;
    int ret = -1;

199
    if (!(hostname = virVHBAFindVportHost(TEST_FC_HOST_PREFIX)))
O
Osier Yang 已提交
200 201 202 203 204 205
        return -1;

    if (STRNEQ(hostname, expect_hostname))
        goto cleanup;

    ret = 0;
206
 cleanup:
O
Osier Yang 已提交
207 208 209 210
    VIR_FREE(hostname);
    return ret;
}

211
/* Test virVHBAGetConfig fabric name optional */
212 213 214 215 216 217 218
static int
test6(const void *data ATTRIBUTE_UNUSED)
{
    const char *expect_wwnn = "2002001b32a9da4e";
    const char *expect_wwpn = "2102001b32a9da4e";
    char *wwnn = NULL;
    char *wwpn = NULL;
J
John Ferlan 已提交
219
    char *fabric_wwn = NULL;
220 221
    int ret = -1;

222 223
    if (!(wwnn = virVHBAGetConfig(TEST_FC_HOST_PREFIX, TEST_FC_HOST_NUM_NO_FAB,
                                  "node_name")))
224 225
        return -1;

226 227
    if (!(wwpn = virVHBAGetConfig(TEST_FC_HOST_PREFIX, TEST_FC_HOST_NUM_NO_FAB,
                                  "port_name")))
228 229
        goto cleanup;

230 231 232
    if ((fabric_wwn = virVHBAGetConfig(TEST_FC_HOST_PREFIX,
                                       TEST_FC_HOST_NUM_NO_FAB,
                                       "fabric_name")))
233 234 235 236 237 238 239 240 241 242
        goto cleanup;

    if (STRNEQ(expect_wwnn, wwnn) ||
        STRNEQ(expect_wwpn, wwpn))
        goto cleanup;

    ret = 0;
 cleanup:
    VIR_FREE(wwnn);
    VIR_FREE(wwpn);
J
John Ferlan 已提交
243
    VIR_FREE(fabric_wwn);
244 245 246
    return ret;
}

247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290


/* Test manageVHBAByNodeDevice
 *  - Test both virNodeDeviceCreateXML and virNodeDeviceDestroy
 *  - Create a node device vHBA allowing usage of various different
 *    methods based on the input data/xml argument.
 *  - Be sure that it's possible to destroy the node device as well.
 */
static int
manageVHBAByNodeDevice(const void *data)
{
    const char *expect_hostname = "scsi_host12";
    virConnectPtr conn = NULL;
    virNodeDevicePtr dev = NULL;
    int ret = -1;
    const char *vhba = data;

    if (!(conn = virConnectOpen("test:///default")))
        return -1;

    if (!(dev = virNodeDeviceCreateXML(conn, vhba, 0)))
        goto cleanup;

    if (virNodeDeviceDestroy(dev) < 0)
        goto cleanup;

    if (STRNEQ(virNodeDeviceGetName(dev), expect_hostname)) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "Expected hostname: '%s' got: '%s'",
                       expect_hostname, virNodeDeviceGetName(dev));
        goto cleanup;
    }

    ret = 0;

 cleanup:
    if (dev)
        virNodeDeviceFree(dev);
    if (conn)
        virConnectClose(conn);
    return ret;
}


291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
/* Test manageVHBAByStoragePool
 *  - Test both virStoragePoolCreateXML and virStoragePoolDestroy
 *  - Create a storage pool vHBA allowing usage of various different
 *    methods based on the input data/xml argument.
 *  - Be sure that it's possible to destroy the storage pool as well.
 */
static int
manageVHBAByStoragePool(const void *data)
{
    const char *expect_hostname = "scsi_host12";
    virConnectPtr conn = NULL;
    virStoragePoolPtr pool = NULL;
    virNodeDevicePtr dev = NULL;
    int ret = -1;
    const char *vhba = data;

    if (!(conn = virConnectOpen("test:///default")))
        return -1;

    if (!(pool = virStoragePoolCreateXML(conn, vhba, 0)))
        goto cleanup;

    if (!(dev = virNodeDeviceLookupByName(conn, expect_hostname))) {
        VIR_DEBUG("Failed to find expected_hostname '%s'", expect_hostname);
        ignore_value(virStoragePoolDestroy(pool));
        goto cleanup;
    }
P
Pavel Hrdina 已提交
318
    virNodeDeviceFree(dev);
319 320 321 322 323 324 325

    if (virStoragePoolDestroy(pool) < 0)
        goto cleanup;

    if ((dev = virNodeDeviceLookupByName(conn, expect_hostname))) {
        VIR_DEBUG("Found expected_hostname '%s' after destroy",
                  expect_hostname);
P
Pavel Hrdina 已提交
326
        virNodeDeviceFree(dev);
327 328 329 330 331 332 333 334 335 336 337 338 339 340
        goto cleanup;
    }

    ret = 0;

 cleanup:
    if (pool)
        virStoragePoolFree(pool);
    if (conn)
        virConnectClose(conn);
    return ret;
}


O
Osier Yang 已提交
341 342 343 344 345
static int
mymain(void)
{
    int ret = 0;

346 347 348 349 350 351
    if (virAsprintf(&fchost_prefix, "%s/%s", abs_srcdir,
                    "fchostdata/fc_host/") < 0) {
        ret = -1;
        goto cleanup;
    }

352
    if (virTestRun("virVHBAPathExists", test1, NULL) < 0)
O
Osier Yang 已提交
353
        ret = -1;
354
    if (virTestRun("virVHBAIsVportCapable", test2, NULL) < 0)
O
Osier Yang 已提交
355
        ret = -1;
356
    if (virTestRun("virVHBAGetConfig", test3, NULL) < 0)
O
Osier Yang 已提交
357
        ret = -1;
358
    if (virTestRun("virVHBAGetHostByWWN", test4, NULL) < 0)
O
Osier Yang 已提交
359
        ret = -1;
360
    if (virTestRun("virVHBAFindVportHost", test5, NULL) < 0)
O
Osier Yang 已提交
361
        ret = -1;
362
    if (virTestRun("virVHBAGetConfig-empty-fabric_wwn", test6, NULL) < 0)
363
        ret = -1;
364 365 366
    if (virTestRun("manageVHBAByNodeDevice-by-parent", manageVHBAByNodeDevice,
                   test7_xml) < 0)
        ret = -1;
367 368 369
    if (virTestRun("manageVHBAByNodeDevice-no-parent", manageVHBAByNodeDevice,
                   test8_xml) < 0)
        ret = -1;
370 371 372
    if (virTestRun("manageVHBAByNodeDevice-parent-wwn", manageVHBAByNodeDevice,
                   test9_xml) < 0)
        ret = -1;
373 374 375
    if (virTestRun("manageVHBAByNodeDevice-parent-fabric-wwn",
                   manageVHBAByNodeDevice, test10_xml) < 0)
        ret = -1;
376 377 378
    if (virTestRun("manageVHBAByStoragePool-by-parent", manageVHBAByStoragePool,
                   test11_xml) < 0)
        ret = -1;
O
Osier Yang 已提交
379

380
 cleanup:
381
    VIR_FREE(fchost_prefix);
O
Osier Yang 已提交
382 383 384
    return ret;
}

385
VIR_TEST_MAIN_PRELOAD(mymain, VIR_TEST_MOCK("virrandom"))