test_driver.c 145.1 KB
Newer Older
1 2 3
/*
 * test.c: A "mock" hypervisor for use by application unit tests
 *
4
 * Copyright (C) 2006-2009 Red Hat, Inc.
5
 * Copyright (C) 2006 Daniel P. Berrange
6
 *
7 8 9 10 11 12 13 14 15 16 17 18 19
 * 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, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
20 21 22 23
 *
 * Daniel Berrange <berrange@redhat.com>
 */

24
#include <config.h>
25

26 27 28
#include <stdio.h>
#include <string.h>
#include <sys/time.h>
29 30
#include <fcntl.h>
#include <unistd.h>
31
#include <sys/stat.h>
C
Cole Robinson 已提交
32
#include <libxml/xmlsave.h>
33

34 35

#include "virterror_internal.h"
36
#include "datatypes.h"
37
#include "test_driver.h"
38
#include "buf.h"
39
#include "util.h"
40
#include "uuid.h"
41
#include "capabilities.h"
42
#include "memory.h"
43
#include "network_conf.h"
L
Laine Stump 已提交
44
#include "interface_conf.h"
45
#include "domain_conf.h"
46 47
#include "domain_event.h"
#include "event.h"
C
Cole Robinson 已提交
48
#include "storage_conf.h"
49
#include "node_device_conf.h"
50
#include "xml.h"
51
#include "threads.h"
52
#include "logging.h"
53

54 55
#define VIR_FROM_THIS VIR_FROM_TEST

56 57 58 59 60 61 62 63 64
/* Driver specific info to carry with a domain */
struct _testDomainObjPrivate {
    virVcpuInfoPtr vcpu_infos;

    unsigned char *cpumaps;
};
typedef struct _testDomainObjPrivate testDomainObjPrivate;
typedef struct _testDomainObjPrivate *testDomainObjPrivatePtr;

65 66 67 68 69 70 71 72 73 74 75
#define MAX_CPUS 128

struct _testCell {
    unsigned long mem;
    int numCpus;
    int cpus[MAX_CPUS];
};
typedef struct _testCell testCell;
typedef struct _testCell *testCellPtr;

#define MAX_CELLS 128
76

77
struct _testConn {
78
    virMutex lock;
79

80 81
    char path[PATH_MAX];
    int nextDomID;
82
    virCapsPtr caps;
83
    virNodeInfo nodeInfo;
84
    virDomainObjList domains;
85
    virNetworkObjList networks;
L
Laine Stump 已提交
86
    virInterfaceObjList ifaces;
C
Cole Robinson 已提交
87
    virStoragePoolObjList pools;
88
    virNodeDeviceObjList devs;
89 90
    int numCells;
    testCell cells[MAX_CELLS];
91 92 93 94 95 96 97


    /* An array of callbacks */
    virDomainEventCallbackListPtr domainEventCallbacks;
    virDomainEventQueuePtr domainEventQueue;
    int domainEventTimer;
    int domainEventDispatching;
98 99 100
};
typedef struct _testConn testConn;
typedef struct _testConn *testConnPtr;
101

102
#define TEST_MODEL "i686"
103
#define TEST_MODEL_WORDSIZE 32
104
#define TEST_EMULATOR "/usr/bin/test-hv"
105

106
static const virNodeInfo defaultNodeInfo = {
107
    TEST_MODEL,
108 109 110 111 112 113 114
    1024*1024*3, /* 3 GB */
    16,
    1400,
    2,
    2,
    2,
    2,
115 116
};

117

118
#define testError(conn, code, fmt...)                               \
119
        virReportErrorHelper(conn, VIR_FROM_TEST, code, __FILE__, \
120
                               __FUNCTION__, __LINE__, fmt)
121

122 123 124 125 126 127
static int testClose(virConnectPtr conn);
static void testDomainEventFlush(int timer, void *opaque);
static void testDomainEventQueue(testConnPtr driver,
                                 virDomainEventPtr event);


128 129
static void testDriverLock(testConnPtr driver)
{
130
    virMutexLock(&driver->lock);
131 132 133 134
}

static void testDriverUnlock(testConnPtr driver)
{
135
    virMutexUnlock(&driver->lock);
136 137
}

138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
static void *testDomainObjPrivateAlloc(void)
{
    testDomainObjPrivatePtr priv;

    if (VIR_ALLOC(priv) < 0)
        return NULL;

    return priv;
}

static void testDomainObjPrivateFree(void *data)
{
    testDomainObjPrivatePtr priv = data;

    VIR_FREE(priv->cpumaps);
    VIR_FREE(priv);
}


157 158
static virCapsPtr
testBuildCapabilities(virConnectPtr conn) {
159
    testConnPtr privconn = conn->privateData;
160 161 162 163
    virCapsPtr caps;
    virCapsGuestPtr guest;
    const char *const guest_types[] = { "hvm", "xen" };
    int i;
164

165 166
    if ((caps = virCapabilitiesNew(TEST_MODEL, 0, 0)) == NULL)
        goto no_memory;
167

168 169 170 171
    if (virCapabilitiesAddHostFeature(caps, "pae") < 0)
        goto no_memory;
    if (virCapabilitiesAddHostFeature(caps ,"nonpae") < 0)
        goto no_memory;
172

173 174 175 176
    for (i = 0; i < privconn->numCells; i++) {
        if (virCapabilitiesAddHostNUMACell(caps, i, privconn->cells[i].numCpus,
                                           privconn->cells[i].cpus) < 0)
            goto no_memory;
177 178
    }

179 180 181 182 183 184 185 186 187 188
    for (i = 0; i < ARRAY_CARDINALITY(guest_types) ; i++) {
        if ((guest = virCapabilitiesAddGuest(caps,
                                             guest_types[i],
                                             TEST_MODEL,
                                             TEST_MODEL_WORDSIZE,
                                             TEST_EMULATOR,
                                             NULL,
                                             0,
                                             NULL)) == NULL)
            goto no_memory;
189

190 191 192 193 194 195 196
        if (virCapabilitiesAddGuestDomain(guest,
                                          "test",
                                          NULL,
                                          NULL,
                                          0,
                                          NULL) == NULL)
            goto no_memory;
197

198 199 200 201
        if (virCapabilitiesAddGuestFeature(guest, "pae", 1, 1) == NULL)
            goto no_memory;
        if (virCapabilitiesAddGuestFeature(guest ,"nonpae", 1, 1) == NULL)
            goto no_memory;
202 203
    }

204 205 206
    caps->privateDataAllocFunc = testDomainObjPrivateAlloc;
    caps->privateDataFreeFunc = testDomainObjPrivateFree;

207
    return caps;
208

209
no_memory:
210
    virReportOOMError(conn);
211 212
    virCapabilitiesFree(caps);
    return NULL;
213 214
}

215

216 217 218 219 220 221 222 223 224 225
static const char *defaultDomainXML =
"<domain type='test'>"
"  <name>test</name>"
"  <memory>8388608</memory>"
"  <currentMemory>2097152</currentMemory>"
"  <vcpu>2</vcpu>"
"  <os>"
"    <type>hvm</type>"
"  </os>"
"</domain>";
226 227


228 229 230 231 232 233 234 235 236 237 238
static const char *defaultNetworkXML =
"<network>"
"  <name>default</name>"
"  <bridge name='virbr0' />"
"  <forward/>"
"  <ip address='192.168.122.1' netmask='255.255.255.0'>"
"    <dhcp>"
"      <range start='192.168.122.2' end='192.168.122.254' />"
"    </dhcp>"
"  </ip>"
"</network>";
239

L
Laine Stump 已提交
240 241 242 243 244 245 246 247 248 249 250
static const char *defaultInterfaceXML =
"<interface type=\"ethernet\" name=\"eth1\">"
"  <start mode=\"onboot\"/>"
"  <mac address=\"aa:bb:cc:dd:ee:ff\"/>"
"  <mtu size=\"1492\"/>"
"  <protocol family=\"ipv4\">"
"    <ip address=\"192.168.0.5\" prefix=\"24\"/>"
"    <route gateway=\"192.168.0.1\"/>"
"  </protocol>"
"</interface>";

C
Cole Robinson 已提交
251 252 253 254 255 256 257 258
static const char *defaultPoolXML =
"<pool type='dir'>"
"  <name>default-pool</name>"
"  <target>"
"    <path>/default-pool</path>"
"  </target>"
"</pool>";

259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
static const char *defaultPoolSourcesLogicalXML =
"<sources>\n"
"  <source>\n"
"    <device path='/dev/sda20'/>\n"
"    <name>testvg1</name>\n"
"    <format type='lvm2'/>\n"
"  </source>\n"
"  <source>\n"
"    <device path='/dev/sda21'/>\n"
"    <name>testvg2</name>\n"
"    <format type='lvm2'/>\n"
"  </source>\n"
"</sources>\n";

static const char *defaultPoolSourcesNetFSXML =
"<sources>\n"
"  <source>\n"
"    <host name='%s'/>\n"
"    <dir path='/testshare'/>\n"
"    <format type='nfs'/>\n"
"  </source>\n"
"</sources>\n";

282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
static const char *defaultNodeXML =
"<device>"
"  <name>computer</name>"
"  <capability type='system'>"
"    <hardware>"
"      <vendor>Libvirt</vendor>"
"      <version>Test driver</version>"
"      <serial>123456</serial>"
"      <uuid>11111111-2222-3333-4444-555555555555</uuid>"
"    </hardware>"
"    <firmware>"
"      <vendor>Libvirt</vendor>"
"      <version>Test Driver</version>"
"      <release_date>01/22/2007</release_date>"
"    </firmware>"
"  </capability>"
"</device>";

300
static const unsigned long long defaultPoolCap = (100 * 1024 * 1024 * 1024ull);
C
Cole Robinson 已提交
301 302
static const unsigned long long defaultPoolAlloc = 0;

303 304 305
static int testStoragePoolObjSetDefaults(virConnectPtr conn,
                                         virStoragePoolObjPtr pool);
static int testNodeGetInfo(virConnectPtr conn, virNodeInfoPtr info);
306

307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
static char *
testDomainGenerateIfname(virConnectPtr conn,
                         virDomainDefPtr domdef) {
    int maxif = 1024;
    int ifctr, i;

    for (ifctr = 0; ifctr < maxif; ++ifctr) {
        char *ifname;
        int found = 0;

        if (virAsprintf(&ifname, "testnet%d", ifctr) < 0) {
            virReportOOMError(conn);
            return NULL;
        }

        /* Generate network interface names */
        for (i = 0 ; i < domdef->nnets ; i++) {
            if (domdef->nets[i]->ifname &&
                STREQ (domdef->nets[i]->ifname, ifname)) {
                found = 1;
                break;
            }
        }

        if (!found)
            return ifname;
    }

    testError(conn, VIR_ERR_INTERNAL_ERROR,
              _("Exceeded max iface limit %d"), maxif);
    return NULL;
}

340 341 342
static int
testDomainGenerateIfnames(virConnectPtr conn,
                          virDomainDefPtr domdef)
343 344 345 346 347 348 349 350 351 352
{
    int i = 0;

    for (i = 0; i < domdef->nnets; i++) {
        char *ifname;
        if (domdef->nets[i]->ifname)
            continue;

        ifname = testDomainGenerateIfname(conn, domdef);
        if (!ifname)
353
            return -1;
354 355 356 357

        domdef->nets[i]->ifname = ifname;
    }

358
    return 0;
359 360
}

361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
/* Helper to update info for a single VCPU */
static int
testDomainUpdateVCPU(virConnectPtr conn ATTRIBUTE_UNUSED,
                     virDomainObjPtr dom,
                     int vcpu,
                     int maplen,
                     int maxcpu)
{
    testDomainObjPrivatePtr privdata = dom->privateData;
    virVcpuInfoPtr info = &privdata->vcpu_infos[vcpu];
    unsigned char *cpumap = VIR_GET_CPUMAP(privdata->cpumaps, maplen, vcpu);
    int j;

    memset(info, 0, sizeof(virVcpuInfo));
    memset(cpumap, 0, maplen);

    info->number    = vcpu;
    info->state     = VIR_VCPU_RUNNING;
    info->cpuTime   = 5000000;
    info->cpu       = 0;

    if (dom->def->cpumask) {
        for (j = 0; j < maxcpu && j < VIR_DOMAIN_CPUMASK_LEN; ++j) {
            if (dom->def->cpumask[j]) {
                VIR_USE_CPU(cpumap, j);
                info->cpu = j;
            }
        }
    } else {
        for (j = 0; j < maxcpu; ++j) {
            if ((j % 3) == 0) {
                /* Mark of every third CPU as usable */
                VIR_USE_CPU(cpumap, j);
                info->cpu = j;
            }
        }
    }

    return 0;
}

/*
 * Update domain VCPU amount and info
 *
 * @conn: virConnectPtr
 * @dom : domain needing updates
 * @nvcpus: New amount of vcpus for the domain
 * @clear_all: If true, rebuild info for ALL vcpus, not just newly added vcpus
 */
static int
testDomainUpdateVCPUs(virConnectPtr conn,
                      virDomainObjPtr dom,
                      int nvcpus,
                      unsigned int clear_all)
{
    testConnPtr privconn = conn->privateData;
    testDomainObjPrivatePtr privdata = dom->privateData;
    int i, ret = -1;
    int cpumaplen, maxcpu;

    maxcpu  = VIR_NODEINFO_MAXCPUS(privconn->nodeInfo);
    cpumaplen = VIR_CPU_MAPLEN(maxcpu);

    if (VIR_REALLOC_N(privdata->vcpu_infos, nvcpus) < 0) {
        virReportOOMError(conn);
        goto cleanup;
    }

    if (VIR_REALLOC_N(privdata->cpumaps, nvcpus * cpumaplen) < 0) {
        virReportOOMError(conn);
        goto cleanup;
    }

    /* Set running VCPU and cpumap state */
    if (clear_all) {
        for (i = 0; i < nvcpus; ++i)
            if (testDomainUpdateVCPU(conn, dom, i, cpumaplen, maxcpu) < 0)
                goto cleanup;

    } else if (nvcpus > dom->def->vcpus) {
        /* VCPU amount has grown, populate info for the new vcpus */
        for (i = dom->def->vcpus; i < nvcpus; ++i)
            if (testDomainUpdateVCPU(conn, dom, i, cpumaplen, maxcpu) < 0)
                goto cleanup;
    }

    ret = 0;
cleanup:
    return ret;
}

/* Set up domain runtime state */
453 454 455 456 457
static int
testDomainStartState(virConnectPtr conn,
                     virDomainObjPtr dom)
{
    testConnPtr privconn = conn->privateData;
458
    int ret = -1;
459

460 461 462 463
    if (testDomainUpdateVCPUs(conn, dom, dom->def->vcpus, 1) < 0)
        goto cleanup;

    /* Set typical run state */
464 465 466
    dom->state = VIR_DOMAIN_RUNNING;
    dom->def->id = privconn->nextDomID++;

467 468 469
    ret = 0;
cleanup:
    return ret;
470
}
471

472 473 474 475 476 477 478 479 480
static void
testDomainShutdownState(virDomainPtr domain,
                        virDomainObjPtr privdom)
{
    privdom->state = VIR_DOMAIN_SHUTOFF;
    privdom->def->id = -1;
    domain->id = -1;
}

481
static int testOpenDefault(virConnectPtr conn) {
482 483
    int u;
    struct timeval tv;
484
    testConnPtr privconn;
485 486 487 488
    virDomainDefPtr domdef = NULL;
    virDomainObjPtr domobj = NULL;
    virNetworkDefPtr netdef = NULL;
    virNetworkObjPtr netobj = NULL;
L
Laine Stump 已提交
489 490
    virInterfaceDefPtr interfacedef = NULL;
    virInterfaceObjPtr interfaceobj = NULL;
C
Cole Robinson 已提交
491 492
    virStoragePoolDefPtr pooldef = NULL;
    virStoragePoolObjPtr poolobj = NULL;
493 494
    virNodeDeviceDefPtr nodedef = NULL;
    virNodeDeviceObjPtr nodeobj = NULL;
495

496
    if (VIR_ALLOC(privconn) < 0) {
497
        virReportOOMError(conn);
498 499
        return VIR_DRV_OPEN_ERROR;
    }
500 501 502 503 504 505 506
    if (virMutexInit(&privconn->lock) < 0) {
        testError(conn, VIR_ERR_INTERNAL_ERROR,
                  "%s", _("cannot initialize mutex"));
        VIR_FREE(privconn);
        return VIR_DRV_OPEN_ERROR;
    }

507
    testDriverLock(privconn);
508
    conn->privateData = privconn;
509 510

    if (gettimeofday(&tv, NULL) < 0) {
511 512
        virReportSystemError(conn, errno,
                             "%s", _("getting time of day"));
513
        goto error;
514 515
    }

516 517 518
    if (virDomainObjListInit(&privconn->domains) < 0)
        goto error;

519
    memmove(&privconn->nodeInfo, &defaultNodeInfo, sizeof(defaultNodeInfo));
520

521 522 523 524 525 526 527 528 529 530
    // Numa setup
    privconn->numCells = 2;
    for (u = 0; u < 2; ++u) {
        privconn->cells[u].numCpus = 8;
        privconn->cells[u].mem = (u + 1) * 2048 * 1024;
    }
    for (u = 0 ; u < 16 ; u++) {
        privconn->cells[u % 2].cpus[(u / 2)] = u;
    }

531 532 533 534 535
    if (!(privconn->caps = testBuildCapabilities(conn)))
        goto error;

    privconn->nextDomID = 1;

536 537 538
    if (!(domdef = virDomainDefParseString(conn, privconn->caps,
                                           defaultDomainXML,
                                           VIR_DOMAIN_XML_INACTIVE)))
539
        goto error;
540
    if (testDomainGenerateIfnames(conn, domdef) < 0)
541
        goto error;
542 543
    if (!(domobj = virDomainAssignDef(conn, privconn->caps,
                                      &privconn->domains, domdef)))
544 545
        goto error;
    domdef = NULL;
546 547 548 549 550 551

    if (testDomainStartState(conn, domobj) < 0) {
        virDomainObjUnlock(domobj);
        goto error;
    }

552
    domobj->persistent = 1;
553
    virDomainObjUnlock(domobj);
554 555 556 557 558 559 560 561 562

    if (!(netdef = virNetworkDefParseString(conn, defaultNetworkXML)))
        goto error;
    if (!(netobj = virNetworkAssignDef(conn, &privconn->networks, netdef))) {
        virNetworkDefFree(netdef);
        goto error;
    }
    netobj->active = 1;
    netobj->persistent = 1;
563
    virNetworkObjUnlock(netobj);
564

L
Laine Stump 已提交
565 566 567 568 569 570 571 572 573
    if (!(interfacedef = virInterfaceDefParseString(conn, defaultInterfaceXML)))
        goto error;
    if (!(interfaceobj = virInterfaceAssignDef(conn, &privconn->ifaces, interfacedef))) {
        virInterfaceDefFree(interfacedef);
        goto error;
    }
    interfaceobj->active = 1;
    virInterfaceObjUnlock(interfaceobj);

574
    if (!(pooldef = virStoragePoolDefParseString(conn, defaultPoolXML)))
C
Cole Robinson 已提交
575 576 577 578 579 580 581
        goto error;

    if (!(poolobj = virStoragePoolObjAssignDef(conn, &privconn->pools,
                                               pooldef))) {
        virStoragePoolDefFree(pooldef);
        goto error;
    }
582

583
    if (testStoragePoolObjSetDefaults(conn, poolobj) == -1) {
584
        virStoragePoolObjUnlock(poolobj);
C
Cole Robinson 已提交
585
        goto error;
586
    }
C
Cole Robinson 已提交
587
    poolobj->active = 1;
588
    virStoragePoolObjUnlock(poolobj);
C
Cole Robinson 已提交
589

590 591 592 593 594 595 596 597 598 599
    /* Init default node device */
    if (!(nodedef = virNodeDeviceDefParseString(conn, defaultNodeXML, 0)))
        goto error;
    if (!(nodeobj = virNodeDeviceAssignDef(conn, &privconn->devs,
                                           nodedef))) {
        virNodeDeviceDefFree(nodedef);
        goto error;
    }
    virNodeDeviceObjUnlock(nodeobj);

600
    testDriverUnlock(privconn);
601

602 603 604
    return VIR_DRV_OPEN_SUCCESS;

error:
605
    virDomainObjListDeinit(&privconn->domains);
606
    virNetworkObjListFree(&privconn->networks);
L
Laine Stump 已提交
607
    virInterfaceObjListFree(&privconn->ifaces);
C
Cole Robinson 已提交
608
    virStoragePoolObjListFree(&privconn->pools);
609
    virNodeDeviceObjListFree(&privconn->devs);
610
    virCapabilitiesFree(privconn->caps);
611
    testDriverUnlock(privconn);
612
    conn->privateData = NULL;
613
    VIR_FREE(privconn);
614
    virDomainDefFree(domdef);
615
    return VIR_DRV_OPEN_ERROR;
616 617 618 619
}


static char *testBuildFilename(const char *relativeTo,
620 621 622 623 624 625 626 627
                               const char *filename) {
    char *offset;
    int baseLen;
    if (!filename || filename[0] == '\0')
        return (NULL);
    if (filename[0] == '/')
        return strdup(filename);

628
    offset = strrchr(relativeTo, '/');
629
    if ((baseLen = (offset-relativeTo+1))) {
630
        char *absFile;
C
Chris Lalancette 已提交
631 632
        int totalLen = baseLen + strlen(filename) + 1;
        if (VIR_ALLOC_N(absFile, totalLen) < 0)
633
            return NULL;
C
Chris Lalancette 已提交
634 635 636 637
        if (virStrncpy(absFile, relativeTo, baseLen, totalLen) == NULL) {
            VIR_FREE(absFile);
            return NULL;
        }
638 639 640 641 642
        strcat(absFile, filename);
        return absFile;
    } else {
        return strdup(filename);
    }
643 644
}

645 646 647 648 649 650 651 652 653
static int testOpenVolumesForPool(virConnectPtr conn,
                                  xmlDocPtr xml,
                                  xmlXPathContextPtr ctxt,
                                  const char *file,
                                  virStoragePoolObjPtr pool,
                                  int poolidx) {
    char *vol_xpath;
    int i, ret, func_ret = -1;
    xmlNodePtr *vols = NULL;
654
    virStorageVolDefPtr def = NULL;
655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725

    /* Find storage volumes */
    if (virAsprintf(&vol_xpath, "/node/pool[%d]/volume", poolidx) < 0) {
        virReportOOMError(NULL);
        goto error;
    }

    ret = virXPathNodeSet(conn, vol_xpath, ctxt, &vols);
    VIR_FREE(vol_xpath);
    if (ret < 0) {
        testError(NULL, VIR_ERR_XML_ERROR,
                  _("node vol list for pool '%s'"), pool->def->name);
        goto error;
    }

    for (i = 0 ; i < ret ; i++) {
        char *relFile = virXMLPropString(vols[i], "file");
        if (relFile != NULL) {
            char *absFile = testBuildFilename(file, relFile);
            VIR_FREE(relFile);
            if (!absFile) {
                testError(NULL, VIR_ERR_INTERNAL_ERROR, "%s",
                          _("resolving volume filename"));
                goto error;
            }

            def = virStorageVolDefParseFile(conn, pool->def, absFile);
            VIR_FREE(absFile);
            if (!def)
                goto error;
        } else {
            if ((def = virStorageVolDefParseNode(conn, pool->def, xml,
                                                 vols[i])) == NULL) {
                goto error;
            }
        }

        if (VIR_REALLOC_N(pool->volumes.objs,
                          pool->volumes.count+1) < 0) {
            virReportOOMError(conn);
            goto error;
        }

        if (virAsprintf(&def->target.path, "%s/%s",
                        pool->def->target.path,
                        def->name) == -1) {
            virReportOOMError(conn);
            goto error;
        }

        def->key = strdup(def->target.path);
        if (def->key == NULL) {
            virReportOOMError(conn);
            goto error;
        }

        pool->def->allocation += def->allocation;
        pool->def->available = (pool->def->capacity -
                                pool->def->allocation);

        pool->volumes.objs[pool->volumes.count++] = def;
        def = NULL;
    }

    func_ret = 0;
error:
    virStorageVolDefFree(def);
    VIR_FREE(vols);
    return func_ret;
}

726
static int testOpenFromFile(virConnectPtr conn,
727
                            const char *file) {
728
    int fd = -1, i, ret;
729 730
    long l;
    char *str;
731
    xmlDocPtr xml = NULL;
732
    xmlNodePtr root = NULL;
733 734
    xmlNodePtr *domains = NULL, *networks = NULL, *ifaces = NULL,
               *pools = NULL, *devs = NULL;
735 736
    xmlXPathContextPtr ctxt = NULL;
    virNodeInfoPtr nodeInfo;
737
    virNetworkObjPtr net;
L
Laine Stump 已提交
738
    virInterfaceObjPtr iface;
739
    virDomainObjPtr dom;
740 741
    testConnPtr privconn;
    if (VIR_ALLOC(privconn) < 0) {
742
        virReportOOMError(conn);
743 744
        return VIR_DRV_OPEN_ERROR;
    }
745 746 747 748 749 750 751
    if (virMutexInit(&privconn->lock) < 0) {
        testError(conn, VIR_ERR_INTERNAL_ERROR,
                  "%s", _("cannot initialize mutex"));
        VIR_FREE(privconn);
        return VIR_DRV_OPEN_ERROR;
    }

752
    testDriverLock(privconn);
753
    conn->privateData = privconn;
754

755 756 757
    if (virDomainObjListInit(&privconn->domains) < 0)
        goto error;

758 759
    if (!(privconn->caps = testBuildCapabilities(conn)))
        goto error;
760 761

    if ((fd = open(file, O_RDONLY)) < 0) {
762 763 764
        virReportSystemError(NULL, errno,
                             _("loading host definition file '%s'"),
                             file);
765
        goto error;
766 767
    }

768 769 770
    if (!(xml = xmlReadFd(fd, file, NULL,
                          XML_PARSE_NOENT | XML_PARSE_NONET |
                          XML_PARSE_NOERROR | XML_PARSE_NOWARNING))) {
771 772
        testError(NULL, VIR_ERR_INTERNAL_ERROR,
                  _("Invalid XML in file '%s'"), file);
773
        goto error;
774
    }
775 776
    close(fd);
    fd = -1;
777

778 779
    root = xmlDocGetRootElement(xml);
    if ((root == NULL) || (!xmlStrEqual(root->name, BAD_CAST "node"))) {
780 781
        testError(NULL, VIR_ERR_XML_ERROR, "%s",
                  _("Root element is not 'node'"));
782
        goto error;
783 784
    }

785 786
    ctxt = xmlXPathNewContext(xml);
    if (ctxt == NULL) {
C
Cole Robinson 已提交
787 788
        testError(NULL, VIR_ERR_INTERNAL_ERROR, "%s",
                  _("creating xpath context"));
789
        goto error;
790
    }
791

792
    privconn->nextDomID = 1;
793
    privconn->numCells = 0;
C
Chris Lalancette 已提交
794 795 796 797 798
    if (virStrcpyStatic(privconn->path, file) == NULL) {
        testError(NULL, VIR_ERR_INTERNAL_ERROR,
                  _("Path %s too big for destination"), file);
        goto error;
    }
799 800 801
    memmove(&privconn->nodeInfo, &defaultNodeInfo, sizeof(defaultNodeInfo));

    nodeInfo = &privconn->nodeInfo;
802
    ret = virXPathLong(conn, "string(/node/cpu/nodes[1])", ctxt, &l);
803 804 805
    if (ret == 0) {
        nodeInfo->nodes = l;
    } else if (ret == -2) {
J
Jim Meyering 已提交
806
        testError(NULL, VIR_ERR_XML_ERROR, "%s", _("node cpu numa nodes"));
807
        goto error;
808
    }
809

810
    ret = virXPathLong(conn, "string(/node/cpu/sockets[1])", ctxt, &l);
811 812 813
    if (ret == 0) {
        nodeInfo->sockets = l;
    } else if (ret == -2) {
J
Jim Meyering 已提交
814
        testError(NULL, VIR_ERR_XML_ERROR, "%s", _("node cpu sockets"));
815
        goto error;
816
    }
817

818
    ret = virXPathLong(conn, "string(/node/cpu/cores[1])", ctxt, &l);
819 820 821
    if (ret == 0) {
        nodeInfo->cores = l;
    } else if (ret == -2) {
J
Jim Meyering 已提交
822
        testError(NULL, VIR_ERR_XML_ERROR, "%s", _("node cpu cores"));
823
        goto error;
824 825
    }

826
    ret = virXPathLong(conn, "string(/node/cpu/threads[1])", ctxt, &l);
827 828 829
    if (ret == 0) {
        nodeInfo->threads = l;
    } else if (ret == -2) {
J
Jim Meyering 已提交
830
        testError(NULL, VIR_ERR_XML_ERROR, "%s", _("node cpu threads"));
831
        goto error;
832
    }
833

834
    nodeInfo->cpus = nodeInfo->cores * nodeInfo->threads * nodeInfo->sockets * nodeInfo->nodes;
835
    ret = virXPathLong(conn, "string(/node/cpu/active[1])", ctxt, &l);
836 837
    if (ret == 0) {
        if (l < nodeInfo->cpus) {
838 839
            nodeInfo->cpus = l;
        }
840
    } else if (ret == -2) {
J
Jim Meyering 已提交
841
        testError(NULL, VIR_ERR_XML_ERROR, "%s", _("node active cpu"));
842
        goto error;
843
    }
844
    ret = virXPathLong(conn, "string(/node/cpu/mhz[1])", ctxt, &l);
845 846 847
    if (ret == 0) {
        nodeInfo->mhz = l;
    } else if (ret == -2) {
J
Jim Meyering 已提交
848
        testError(NULL, VIR_ERR_XML_ERROR, "%s", _("node cpu mhz"));
849
        goto error;
850 851
    }

852
    str = virXPathString(conn, "string(/node/cpu/model[1])", ctxt);
853
    if (str != NULL) {
C
Chris Lalancette 已提交
854 855 856 857 858 859
        if (virStrcpyStatic(nodeInfo->model, str) == NULL) {
            testError(NULL, VIR_ERR_INTERNAL_ERROR,
                      _("Model %s too big for destination"), str);
            VIR_FREE(str);
            goto error;
        }
860
        VIR_FREE(str);
861 862
    }

863
    ret = virXPathLong(conn, "string(/node/memory[1])", ctxt, &l);
864 865 866
    if (ret == 0) {
        nodeInfo->memory = l;
    } else if (ret == -2) {
J
Jim Meyering 已提交
867
        testError(NULL, VIR_ERR_XML_ERROR, "%s", _("node memory"));
868
        goto error;
869
    }
870

871
    ret = virXPathNodeSet(conn, "/node/domain", ctxt, &domains);
872
    if (ret < 0) {
J
Jim Meyering 已提交
873
        testError(NULL, VIR_ERR_XML_ERROR, "%s", _("node domain list"));
874
        goto error;
875
    }
876

877
    for (i = 0 ; i < ret ; i++) {
878 879 880 881 882 883
        virDomainDefPtr def;
        char *relFile = virXMLPropString(domains[i], "file");
        if (relFile != NULL) {
            char *absFile = testBuildFilename(file, relFile);
            VIR_FREE(relFile);
            if (!absFile) {
J
Jim Meyering 已提交
884
                testError(NULL, VIR_ERR_INTERNAL_ERROR, "%s", _("resolving domain filename"));
885 886
                goto error;
            }
887 888
            def = virDomainDefParseFile(conn, privconn->caps, absFile,
                                        VIR_DOMAIN_XML_INACTIVE);
889
            VIR_FREE(absFile);
890 891 892
            if (!def)
                goto error;
        } else {
893 894
            if ((def = virDomainDefParseNode(conn, privconn->caps, xml, domains[i],
                                   VIR_DOMAIN_XML_INACTIVE)) == NULL)
895 896 897
                goto error;
        }

898
        if (testDomainGenerateIfnames(conn, def) < 0 ||
899 900
            !(dom = virDomainAssignDef(conn, privconn->caps,
                                       &privconn->domains, def))) {
901
            virDomainDefFree(def);
902 903
            goto error;
        }
904

905 906 907 908 909
        if (testDomainStartState(conn, dom) < 0) {
            virDomainObjUnlock(dom);
            goto error;
        }

910
        dom->persistent = 1;
911
        virDomainObjUnlock(dom);
912
    }
913
    VIR_FREE(domains);
914

915
    ret = virXPathNodeSet(conn, "/node/network", ctxt, &networks);
916
    if (ret < 0) {
J
Jim Meyering 已提交
917
        testError(NULL, VIR_ERR_XML_ERROR, "%s", _("node network list"));
918 919 920 921 922 923 924 925
        goto error;
    }
    for (i = 0 ; i < ret ; i++) {
        virNetworkDefPtr def;
        char *relFile = virXMLPropString(networks[i], "file");
        if (relFile != NULL) {
            char *absFile = testBuildFilename(file, relFile);
            VIR_FREE(relFile);
926
            if (!absFile) {
J
Jim Meyering 已提交
927
                testError(NULL, VIR_ERR_INTERNAL_ERROR, "%s", _("resolving network filename"));
928 929
                goto error;
            }
930 931

            def = virNetworkDefParseFile(conn, absFile);
932
            VIR_FREE(absFile);
933 934 935 936 937
            if (!def)
                goto error;
        } else {
            if ((def = virNetworkDefParseNode(conn, xml, networks[i])) == NULL)
                goto error;
938
        }
939 940 941 942
        if (!(net = virNetworkAssignDef(conn, &privconn->networks,
                                        def))) {
            virNetworkDefFree(def);
            goto error;
943
        }
944
        net->persistent = 1;
945
        net->active = 1;
946
        virNetworkObjUnlock(net);
947
    }
948
    VIR_FREE(networks);
949

L
Laine Stump 已提交
950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971
    /* Parse interface definitions */
    ret = virXPathNodeSet(conn, "/node/interface", ctxt, &ifaces);
    if (ret < 0) {
        testError(NULL, VIR_ERR_XML_ERROR, "%s", _("node interface list"));
        goto error;
    }
    for (i = 0 ; i < ret ; i++) {
        virInterfaceDefPtr def;
        char *relFile = virXMLPropString(ifaces[i], "file");
        if (relFile != NULL) {
            char *absFile = testBuildFilename(file, relFile);
            VIR_FREE(relFile);
            if (!absFile) {
                testError(NULL, VIR_ERR_INTERNAL_ERROR, "%s", _("resolving interface filename"));
                goto error;
            }

            def = virInterfaceDefParseFile(conn, absFile);
            VIR_FREE(absFile);
            if (!def)
                goto error;
        } else {
972
            if ((def = virInterfaceDefParseNode(conn, xml, ifaces[i])) == NULL)
L
Laine Stump 已提交
973 974
                goto error;
        }
975

L
Laine Stump 已提交
976 977 978 979
        if (!(iface = virInterfaceAssignDef(conn, &privconn->ifaces, def))) {
            virInterfaceDefFree(def);
            goto error;
        }
980 981

        iface->active = 1;
L
Laine Stump 已提交
982 983 984 985
        virInterfaceObjUnlock(iface);
    }
    VIR_FREE(ifaces);

C
Cole Robinson 已提交
986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004
    /* Parse Storage Pool list */
    ret = virXPathNodeSet(conn, "/node/pool", ctxt, &pools);
    if (ret < 0) {
        testError(NULL, VIR_ERR_XML_ERROR, "%s", _("node pool list"));
        goto error;
    }
    for (i = 0 ; i < ret ; i++) {
        virStoragePoolDefPtr def;
        virStoragePoolObjPtr pool;
        char *relFile = virXMLPropString(pools[i], "file");
        if (relFile != NULL) {
            char *absFile = testBuildFilename(file, relFile);
            VIR_FREE(relFile);
            if (!absFile) {
                testError(NULL, VIR_ERR_INTERNAL_ERROR, "%s",
                          _("resolving pool filename"));
                goto error;
            }

1005
            def = virStoragePoolDefParseFile(conn, absFile);
C
Cole Robinson 已提交
1006 1007 1008 1009
            VIR_FREE(absFile);
            if (!def)
                goto error;
        } else {
1010 1011
            if ((def = virStoragePoolDefParseNode(conn, xml,
                                                  pools[i])) == NULL) {
C
Cole Robinson 已提交
1012 1013 1014 1015 1016 1017 1018 1019 1020 1021
                goto error;
            }
        }

        if (!(pool = virStoragePoolObjAssignDef(conn, &privconn->pools,
                                                def))) {
            virStoragePoolDefFree(def);
            goto error;
        }

1022
        if (testStoragePoolObjSetDefaults(conn, pool) == -1) {
1023
            virStoragePoolObjUnlock(pool);
C
Cole Robinson 已提交
1024
            goto error;
1025
        }
C
Cole Robinson 已提交
1026
        pool->active = 1;
1027 1028 1029 1030 1031 1032 1033

        /* Find storage volumes */
        if (testOpenVolumesForPool(conn, xml, ctxt, file, pool, i+1) < 0) {
            virStoragePoolObjUnlock(pool);
            goto error;
        }

1034
        virStoragePoolObjUnlock(pool);
C
Cole Robinson 已提交
1035
    }
1036
    VIR_FREE(pools);
C
Cole Robinson 已提交
1037

1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074
    ret = virXPathNodeSet(conn, "/node/device", ctxt, &devs);
    if (ret < 0) {
        testError(NULL, VIR_ERR_XML_ERROR, "%s", _("node device list"));
        goto error;
    }
    for (i = 0 ; i < ret ; i++) {
        virNodeDeviceDefPtr def;
        virNodeDeviceObjPtr dev;
        char *relFile = virXMLPropString(devs[i], "file");

        if (relFile != NULL) {
            char *absFile = testBuildFilename(file, relFile);
            VIR_FREE(relFile);

            if (!absFile) {
                testError(NULL, VIR_ERR_INTERNAL_ERROR, "%s",
                          _("resolving device filename"));
                goto error;
            }

            def = virNodeDeviceDefParseFile(conn, absFile, 0);
            VIR_FREE(absFile);
            if (!def)
                goto error;
        } else {
            if ((def = virNodeDeviceDefParseNode(conn, xml, devs[i], 0)) == NULL)
                goto error;
        }
        if (!(dev = virNodeDeviceAssignDef(conn, &privconn->devs, def))) {
            virNodeDeviceDefFree(def);
            goto error;
        }
        virNodeDeviceObjUnlock(dev);
    }
    VIR_FREE(devs);


J
Jim Meyering 已提交
1075
    xmlXPathFreeContext(ctxt);
1076
    xmlFreeDoc(xml);
1077
    testDriverUnlock(privconn);
1078

1079
    return (0);
1080 1081

 error:
J
Jim Meyering 已提交
1082
    xmlXPathFreeContext(ctxt);
1083
    xmlFreeDoc(xml);
1084 1085
    VIR_FREE(domains);
    VIR_FREE(networks);
L
Laine Stump 已提交
1086
    VIR_FREE(ifaces);
C
Cole Robinson 已提交
1087
    VIR_FREE(pools);
1088 1089
    if (fd != -1)
        close(fd);
1090
    virDomainObjListDeinit(&privconn->domains);
1091
    virNetworkObjListFree(&privconn->networks);
L
Laine Stump 已提交
1092
    virInterfaceObjListFree(&privconn->ifaces);
C
Cole Robinson 已提交
1093
    virStoragePoolObjListFree(&privconn->pools);
1094
    testDriverUnlock(privconn);
1095
    VIR_FREE(privconn);
1096
    conn->privateData = NULL;
1097
    return VIR_DRV_OPEN_ERROR;
1098 1099
}

1100

1101
static virDrvOpenStatus testOpen(virConnectPtr conn,
1102
                    virConnectAuthPtr auth ATTRIBUTE_UNUSED,
1103
                    int flags ATTRIBUTE_UNUSED)
1104
{
1105
    int ret;
1106

1107
    if (!conn->uri)
1108
        return VIR_DRV_OPEN_DECLINED;
1109

1110
    if (!conn->uri->scheme || STRNEQ(conn->uri->scheme, "test"))
1111
        return VIR_DRV_OPEN_DECLINED;
1112

1113
    /* Remote driver should handle these. */
1114
    if (conn->uri->server)
1115 1116
        return VIR_DRV_OPEN_DECLINED;

1117
    /* From this point on, the connection is for us. */
1118 1119 1120
    if (!conn->uri->path
        || conn->uri->path[0] == '\0'
        || (conn->uri->path[0] == '/' && conn->uri->path[1] == '\0')) {
1121
        testError (NULL, VIR_ERR_INVALID_ARG,
J
Jim Meyering 已提交
1122
                   "%s", _("testOpen: supply a path or use test:///default"));
1123 1124
        return VIR_DRV_OPEN_ERROR;
    }
1125

1126
    if (STREQ(conn->uri->path, "/default"))
1127 1128
        ret = testOpenDefault(conn);
    else
1129
        ret = testOpenFromFile(conn,
1130
                               conn->uri->path);
1131

1132 1133
    if (ret == VIR_DRV_OPEN_SUCCESS) {
        testConnPtr privconn = conn->privateData;
1134
        testDriverLock(privconn);
1135 1136 1137 1138
        /* Init callback list */
        if (VIR_ALLOC(privconn->domainEventCallbacks) < 0 ||
            !(privconn->domainEventQueue = virDomainEventQueueNew())) {
            virReportOOMError(NULL);
1139
            testDriverUnlock(privconn);
1140 1141 1142 1143 1144 1145 1146 1147
            testClose(conn);
            return VIR_DRV_OPEN_ERROR;
        }

        if ((privconn->domainEventTimer =
             virEventAddTimeout(-1, testDomainEventFlush, privconn, NULL)) < 0)
            DEBUG0("virEventAddTimeout failed: No addTimeoutImpl defined. "
                   "continuing without events.");
1148
        testDriverUnlock(privconn);
1149 1150
    }

1151
    return (ret);
1152 1153
}

1154
static int testClose(virConnectPtr conn)
1155
{
1156
    testConnPtr privconn = conn->privateData;
1157
    testDriverLock(privconn);
1158
    virCapabilitiesFree(privconn->caps);
1159
    virDomainObjListDeinit(&privconn->domains);
1160
    virNetworkObjListFree(&privconn->networks);
L
Laine Stump 已提交
1161
    virInterfaceObjListFree(&privconn->ifaces);
C
Cole Robinson 已提交
1162
    virStoragePoolObjListFree(&privconn->pools);
1163 1164 1165 1166 1167 1168 1169

    virDomainEventCallbackListFree(privconn->domainEventCallbacks);
    virDomainEventQueueFree(privconn->domainEventQueue);

    if (privconn->domainEventTimer != -1)
        virEventRemoveTimeout(privconn->domainEventTimer);

1170
    testDriverUnlock(privconn);
1171
    virMutexDestroy(&privconn->lock);
1172

1173
    VIR_FREE (privconn);
1174
    conn->privateData = NULL;
1175
    return 0;
1176 1177
}

1178 1179
static int testGetVersion(virConnectPtr conn ATTRIBUTE_UNUSED,
                          unsigned long *hvVer)
1180
{
1181 1182
    *hvVer = 2;
    return (0);
1183 1184
}

1185 1186 1187 1188 1189 1190 1191 1192
static int testGetMaxVCPUs(virConnectPtr conn ATTRIBUTE_UNUSED,
                           const char *type ATTRIBUTE_UNUSED)
{
    return 32;
}

static int testNodeGetInfo(virConnectPtr conn,
                           virNodeInfoPtr info)
1193
{
1194
    testConnPtr privconn = conn->privateData;
1195
    testDriverLock(privconn);
1196
    memcpy(info, &privconn->nodeInfo, sizeof(virNodeInfo));
1197
    testDriverUnlock(privconn);
1198
    return (0);
1199 1200
}

1201
static char *testGetCapabilities (virConnectPtr conn)
1202
{
1203
    testConnPtr privconn = conn->privateData;
1204
    char *xml;
1205
    testDriverLock(privconn);
1206
    if ((xml = virCapabilitiesFormatXML(privconn->caps)) == NULL)
1207
        virReportOOMError(conn);
1208
    testDriverUnlock(privconn);
1209
    return xml;
1210 1211
}

1212
static int testNumOfDomains(virConnectPtr conn)
1213
{
1214
    testConnPtr privconn = conn->privateData;
1215
    int count;
1216

1217
    testDriverLock(privconn);
1218
    count = virDomainObjListNumOfDomains(&privconn->domains, 1);
1219
    testDriverUnlock(privconn);
1220

1221
    return count;
1222 1223
}

1224
static virDomainPtr
1225
testDomainCreateXML(virConnectPtr conn, const char *xml,
1226
                      unsigned int flags ATTRIBUTE_UNUSED)
1227
{
1228
    testConnPtr privconn = conn->privateData;
1229
    virDomainPtr ret = NULL;
1230
    virDomainDefPtr def;
1231
    virDomainObjPtr dom = NULL;
1232
    virDomainEventPtr event = NULL;
1233

1234
    testDriverLock(privconn);
1235 1236
    if ((def = virDomainDefParseString(conn, privconn->caps, xml,
                                       VIR_DOMAIN_XML_INACTIVE)) == NULL)
1237
        goto cleanup;
1238

1239
    if (testDomainGenerateIfnames(conn, def) < 0)
1240
        goto cleanup;
1241 1242
    if (!(dom = virDomainAssignDef(conn, privconn->caps,
                                   &privconn->domains, def)))
1243 1244
        goto cleanup;
    def = NULL;
1245 1246 1247

    if (testDomainStartState(conn, dom) < 0)
        goto cleanup;
1248

1249 1250 1251 1252
    event = virDomainEventNewFromObj(dom,
                                     VIR_DOMAIN_EVENT_STARTED,
                                     VIR_DOMAIN_EVENT_STARTED_BOOTED);

1253
    ret = virGetDomain(conn, dom->def->name, dom->def->uuid);
1254
    if (ret)
1255
        ret->id = dom->def->id;
1256 1257

cleanup:
1258 1259
    if (dom)
        virDomainObjUnlock(dom);
1260 1261
    if (event)
        testDomainEventQueue(privconn, event);
1262 1263
    if (def)
        virDomainDefFree(def);
1264
    testDriverUnlock(privconn);
1265
    return ret;
1266 1267 1268
}


1269 1270
static virDomainPtr testLookupDomainByID(virConnectPtr conn,
                                         int id)
1271
{
1272
    testConnPtr privconn = conn->privateData;
1273 1274
    virDomainPtr ret = NULL;
    virDomainObjPtr dom;
1275

1276 1277 1278 1279 1280
    testDriverLock(privconn);
    dom = virDomainFindByID(&privconn->domains, id);
    testDriverUnlock(privconn);

    if (dom == NULL) {
1281
        testError (conn, VIR_ERR_NO_DOMAIN, NULL);
1282
        goto cleanup;
1283 1284
    }

1285
    ret = virGetDomain(conn, dom->def->name, dom->def->uuid);
1286 1287 1288 1289
    if (ret)
        ret->id = dom->def->id;

cleanup:
1290 1291
    if (dom)
        virDomainObjUnlock(dom);
1292
    return ret;
1293 1294
}

1295 1296
static virDomainPtr testLookupDomainByUUID(virConnectPtr conn,
                                           const unsigned char *uuid)
1297
{
1298
    testConnPtr privconn = conn->privateData;
1299 1300
    virDomainPtr ret = NULL;
    virDomainObjPtr dom ;
1301

1302 1303 1304 1305 1306
    testDriverLock(privconn);
    dom = virDomainFindByUUID(&privconn->domains, uuid);
    testDriverUnlock(privconn);

    if (dom == NULL) {
1307
        testError (conn, VIR_ERR_NO_DOMAIN, NULL);
1308
        goto cleanup;
1309
    }
1310

1311
    ret = virGetDomain(conn, dom->def->name, dom->def->uuid);
1312 1313 1314 1315
    if (ret)
        ret->id = dom->def->id;

cleanup:
1316 1317
    if (dom)
        virDomainObjUnlock(dom);
1318
    return ret;
1319 1320
}

1321 1322
static virDomainPtr testLookupDomainByName(virConnectPtr conn,
                                           const char *name)
1323
{
1324
    testConnPtr privconn = conn->privateData;
1325 1326
    virDomainPtr ret = NULL;
    virDomainObjPtr dom;
1327

1328 1329 1330 1331 1332
    testDriverLock(privconn);
    dom = virDomainFindByName(&privconn->domains, name);
    testDriverUnlock(privconn);

    if (dom == NULL) {
1333
        testError (conn, VIR_ERR_NO_DOMAIN, NULL);
1334
        goto cleanup;
1335
    }
1336

1337
    ret = virGetDomain(conn, dom->def->name, dom->def->uuid);
1338 1339 1340 1341
    if (ret)
        ret->id = dom->def->id;

cleanup:
1342 1343
    if (dom)
        virDomainObjUnlock(dom);
1344
    return ret;
1345 1346
}

1347 1348 1349
static int testListDomains (virConnectPtr conn,
                            int *ids,
                            int maxids)
1350
{
1351
    testConnPtr privconn = conn->privateData;
1352
    int n;
1353

1354
    testDriverLock(privconn);
1355
    n = virDomainObjListGetActiveIDs(&privconn->domains, ids, maxids);
1356
    testDriverUnlock(privconn);
1357

1358
    return n;
1359 1360
}

1361
static int testDestroyDomain (virDomainPtr domain)
1362
{
1363 1364
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
1365
    virDomainEventPtr event = NULL;
1366
    int ret = -1;
1367

1368
    testDriverLock(privconn);
1369 1370 1371 1372 1373
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);

    if (privdom == NULL) {
        testError(domain->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
1374
        goto cleanup;
1375
    }
1376

1377
    testDomainShutdownState(domain, privdom);
1378 1379 1380
    event = virDomainEventNewFromObj(privdom,
                                     VIR_DOMAIN_EVENT_STOPPED,
                                     VIR_DOMAIN_EVENT_STOPPED_DESTROYED);
1381

1382 1383 1384
    if (!privdom->persistent) {
        virDomainRemoveInactive(&privconn->domains,
                                privdom);
1385
        privdom = NULL;
1386
    }
1387 1388 1389

    ret = 0;
cleanup:
1390 1391
    if (privdom)
        virDomainObjUnlock(privdom);
1392 1393
    if (event)
        testDomainEventQueue(privconn, event);
1394
    testDriverUnlock(privconn);
1395
    return ret;
1396 1397
}

1398
static int testResumeDomain (virDomainPtr domain)
1399
{
1400 1401
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
1402
    virDomainEventPtr event = NULL;
1403
    int ret = -1;
1404

1405
    testDriverLock(privconn);
1406 1407
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
1408
    testDriverUnlock(privconn);
1409 1410 1411

    if (privdom == NULL) {
        testError(domain->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
1412
        goto cleanup;
1413
    }
1414

1415
    if (privdom->state != VIR_DOMAIN_PAUSED) {
1416 1417 1418
        testError(domain->conn,
                  VIR_ERR_INTERNAL_ERROR, _("domain '%s' not paused"),
                  domain->name);
1419
        goto cleanup;
1420
    }
1421

1422
    privdom->state = VIR_DOMAIN_RUNNING;
1423 1424 1425
    event = virDomainEventNewFromObj(privdom,
                                     VIR_DOMAIN_EVENT_RESUMED,
                                     VIR_DOMAIN_EVENT_RESUMED_UNPAUSED);
1426 1427 1428
    ret = 0;

cleanup:
1429 1430
    if (privdom)
        virDomainObjUnlock(privdom);
1431 1432 1433 1434 1435
    if (event) {
        testDriverLock(privconn);
        testDomainEventQueue(privconn, event);
        testDriverUnlock(privconn);
    }
1436
    return ret;
1437 1438
}

1439
static int testPauseDomain (virDomainPtr domain)
1440
{
1441 1442
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
1443
    virDomainEventPtr event = NULL;
1444
    int ret = -1;
1445

1446
    testDriverLock(privconn);
1447 1448
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
1449
    testDriverUnlock(privconn);
1450 1451 1452

    if (privdom == NULL) {
        testError(domain->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
1453
        goto cleanup;
1454
    }
1455

1456 1457
    if (privdom->state == VIR_DOMAIN_SHUTOFF ||
        privdom->state == VIR_DOMAIN_PAUSED) {
1458 1459 1460
        testError(domain->conn,
                  VIR_ERR_INTERNAL_ERROR, _("domain '%s' not running"),
                  domain->name);
1461
        goto cleanup;
1462
    }
1463

1464
    privdom->state = VIR_DOMAIN_PAUSED;
1465 1466 1467
    event = virDomainEventNewFromObj(privdom,
                                     VIR_DOMAIN_EVENT_SUSPENDED,
                                     VIR_DOMAIN_EVENT_SUSPENDED_PAUSED);
1468 1469 1470
    ret = 0;

cleanup:
1471 1472
    if (privdom)
        virDomainObjUnlock(privdom);
1473 1474 1475 1476 1477 1478

    if (event) {
        testDriverLock(privconn);
        testDomainEventQueue(privconn, event);
        testDriverUnlock(privconn);
    }
1479
    return ret;
1480 1481
}

1482
static int testShutdownDomain (virDomainPtr domain)
1483
{
1484 1485
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
1486
    virDomainEventPtr event = NULL;
1487
    int ret = -1;
1488

1489
    testDriverLock(privconn);
1490 1491 1492 1493 1494
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);

    if (privdom == NULL) {
        testError(domain->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
1495
        goto cleanup;
1496
    }
1497

1498
    if (privdom->state == VIR_DOMAIN_SHUTOFF) {
1499 1500
        testError(domain->conn, VIR_ERR_INTERNAL_ERROR,
                  _("domain '%s' not running"), domain->name);
1501
        goto cleanup;
1502
    }
1503

1504
    testDomainShutdownState(domain, privdom);
1505 1506 1507
    event = virDomainEventNewFromObj(privdom,
                                     VIR_DOMAIN_EVENT_STOPPED,
                                     VIR_DOMAIN_EVENT_STOPPED_SHUTDOWN);
1508

1509 1510 1511 1512 1513
    if (!privdom->persistent) {
        virDomainRemoveInactive(&privconn->domains,
                                privdom);
        privdom = NULL;
    }
1514

1515
    ret = 0;
1516
cleanup:
1517 1518
    if (privdom)
        virDomainObjUnlock(privdom);
1519 1520
    if (event)
        testDomainEventQueue(privconn, event);
1521
    testDriverUnlock(privconn);
1522
    return ret;
1523 1524 1525
}

/* Similar behaviour as shutdown */
1526 1527
static int testRebootDomain (virDomainPtr domain,
                             unsigned int action ATTRIBUTE_UNUSED)
1528
{
1529 1530
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
1531
    virDomainEventPtr event = NULL;
1532
    int ret = -1;
1533

1534
    testDriverLock(privconn);
1535 1536 1537 1538 1539
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);

    if (privdom == NULL) {
        testError(domain->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
1540
        goto cleanup;
1541
    }
1542

1543 1544 1545 1546
    privdom->state = VIR_DOMAIN_SHUTDOWN;
    switch (privdom->def->onReboot) {
    case VIR_DOMAIN_LIFECYCLE_DESTROY:
        privdom->state = VIR_DOMAIN_SHUTOFF;
1547 1548
        break;

1549 1550
    case VIR_DOMAIN_LIFECYCLE_RESTART:
        privdom->state = VIR_DOMAIN_RUNNING;
1551 1552
        break;

1553 1554
    case VIR_DOMAIN_LIFECYCLE_PRESERVE:
        privdom->state = VIR_DOMAIN_SHUTOFF;
1555 1556
        break;

1557 1558
    case VIR_DOMAIN_LIFECYCLE_RESTART_RENAME:
        privdom->state = VIR_DOMAIN_RUNNING;
1559
        break;
1560

1561
    default:
1562
        privdom->state = VIR_DOMAIN_SHUTOFF;
1563 1564
        break;
    }
1565

1566
    if (privdom->state == VIR_DOMAIN_SHUTOFF) {
1567
        testDomainShutdownState(domain, privdom);
1568 1569 1570
        event = virDomainEventNewFromObj(privdom,
                                         VIR_DOMAIN_EVENT_STOPPED,
                                         VIR_DOMAIN_EVENT_STOPPED_SHUTDOWN);
1571

1572 1573 1574 1575 1576
        if (!privdom->persistent) {
            virDomainRemoveInactive(&privconn->domains,
                                    privdom);
            privdom = NULL;
        }
1577 1578
    }

1579 1580
    ret = 0;
cleanup:
1581 1582
    if (privdom)
        virDomainObjUnlock(privdom);
1583 1584
    if (event)
        testDomainEventQueue(privconn, event);
1585
    testDriverUnlock(privconn);
1586
    return ret;
1587 1588
}

1589 1590
static int testGetDomainInfo (virDomainPtr domain,
                              virDomainInfoPtr info)
1591
{
1592
    testConnPtr privconn = domain->conn->privateData;
1593
    struct timeval tv;
1594
    virDomainObjPtr privdom;
1595
    int ret = -1;
1596

1597
    testDriverLock(privconn);
1598 1599
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
1600
    testDriverUnlock(privconn);
1601 1602 1603

    if (privdom == NULL) {
        testError(domain->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
1604
        goto cleanup;
1605
    }
1606 1607

    if (gettimeofday(&tv, NULL) < 0) {
1608
        testError(domain->conn, VIR_ERR_INTERNAL_ERROR,
J
Jim Meyering 已提交
1609
                  "%s", _("getting time of day"));
1610
        goto cleanup;
1611 1612
    }

1613 1614 1615 1616 1617
    info->state = privdom->state;
    info->memory = privdom->def->memory;
    info->maxMem = privdom->def->maxmem;
    info->nrVirtCpu = privdom->def->vcpus;
    info->cpuTime = ((tv.tv_sec * 1000ll * 1000ll  * 1000ll) + (tv.tv_usec * 1000ll));
1618 1619 1620
    ret = 0;

cleanup:
1621 1622
    if (privdom)
        virDomainObjUnlock(privdom);
1623
    return ret;
1624 1625
}

1626 1627 1628 1629 1630
#define TEST_SAVE_MAGIC "TestGuestMagic"

static int testDomainSave(virDomainPtr domain,
                          const char *path)
{
1631
    testConnPtr privconn = domain->conn->privateData;
1632 1633 1634
    char *xml = NULL;
    int fd = -1;
    int len;
1635
    virDomainObjPtr privdom;
1636
    virDomainEventPtr event = NULL;
1637
    int ret = -1;
1638

1639
    testDriverLock(privconn);
1640 1641 1642 1643 1644
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);

    if (privdom == NULL) {
        testError(domain->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
1645
        goto cleanup;
1646
    }
1647

C
Cole Robinson 已提交
1648 1649 1650 1651
    xml = virDomainDefFormat(domain->conn,
                             privdom->def,
                             VIR_DOMAIN_XML_SECURE);

1652
    if (xml == NULL) {
1653 1654 1655
        virReportSystemError(domain->conn, errno,
                             _("saving domain '%s' failed to allocate space for metadata"),
                             domain->name);
1656
        goto cleanup;
1657
    }
1658 1659

    if ((fd = open(path, O_CREAT|O_TRUNC|O_WRONLY, S_IRUSR|S_IWUSR)) < 0) {
1660 1661 1662
        virReportSystemError(domain->conn, errno,
                             _("saving domain '%s' to '%s': open failed"),
                             domain->name, path);
1663
        goto cleanup;
1664
    }
1665
    len = strlen(xml);
1666
    if (safewrite(fd, TEST_SAVE_MAGIC, sizeof(TEST_SAVE_MAGIC)) < 0) {
1667 1668 1669
        virReportSystemError(domain->conn, errno,
                             _("saving domain '%s' to '%s': write failed"),
                             domain->name, path);
1670
        goto cleanup;
1671
    }
1672
    if (safewrite(fd, (char*)&len, sizeof(len)) < 0) {
1673 1674 1675
        virReportSystemError(domain->conn, errno,
                             _("saving domain '%s' to '%s': write failed"),
                             domain->name, path);
1676
        goto cleanup;
1677
    }
1678
    if (safewrite(fd, xml, len) < 0) {
1679 1680 1681
        virReportSystemError(domain->conn, errno,
                             _("saving domain '%s' to '%s': write failed"),
                             domain->name, path);
1682
        goto cleanup;
1683
    }
1684

1685
    if (close(fd) < 0) {
1686 1687 1688
        virReportSystemError(domain->conn, errno,
                             _("saving domain '%s' to '%s': write failed"),
                             domain->name, path);
1689
        goto cleanup;
1690
    }
1691 1692
    fd = -1;

1693
    testDomainShutdownState(domain, privdom);
1694 1695 1696
    event = virDomainEventNewFromObj(privdom,
                                     VIR_DOMAIN_EVENT_STOPPED,
                                     VIR_DOMAIN_EVENT_STOPPED_SAVED);
1697

1698 1699 1700
    if (!privdom->persistent) {
        virDomainRemoveInactive(&privconn->domains,
                                privdom);
1701
        privdom = NULL;
1702
    }
1703

1704
    ret = 0;
1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715
cleanup:
    VIR_FREE(xml);

    /* Don't report failure in close or unlink, because
     * in either case we're already in a failure scenario
     * and have reported a earlier error */
    if (ret != 0) {
        if (fd != -1)
            close(fd);
        unlink(path);
    }
1716 1717
    if (privdom)
        virDomainObjUnlock(privdom);
1718 1719
    if (event)
        testDomainEventQueue(privconn, event);
1720
    testDriverUnlock(privconn);
1721
    return ret;
1722 1723
}

1724 1725
static int testDomainRestore(virConnectPtr conn,
                             const char *path)
1726
{
1727
    testConnPtr privconn = conn->privateData;
1728
    char *xml = NULL;
1729
    char magic[15];
1730 1731 1732
    int fd = -1;
    int len;
    virDomainDefPtr def = NULL;
1733
    virDomainObjPtr dom = NULL;
1734
    virDomainEventPtr event = NULL;
1735
    int ret = -1;
1736 1737

    if ((fd = open(path, O_RDONLY)) < 0) {
1738 1739 1740
        virReportSystemError(conn, errno,
                             _("cannot read domain image '%s'"),
                             path);
1741
        goto cleanup;
1742
    }
1743 1744 1745 1746
    if (saferead(fd, magic, sizeof(magic)) != sizeof(magic)) {
        virReportSystemError(conn, errno,
                             _("incomplete save header in '%s'"),
                             path);
1747
        goto cleanup;
1748
    }
1749
    if (memcmp(magic, TEST_SAVE_MAGIC, sizeof(magic))) {
1750
        testError(conn, VIR_ERR_INTERNAL_ERROR,
J
Jim Meyering 已提交
1751
                  "%s", _("mismatched header magic"));
1752
        goto cleanup;
1753
    }
1754 1755 1756 1757
    if (saferead(fd, (char*)&len, sizeof(len)) != sizeof(len)) {
        virReportSystemError(conn, errno,
                             _("failed to read metadata length in '%s'"),
                             path);
1758
        goto cleanup;
1759 1760
    }
    if (len < 1 || len > 8192) {
1761
        testError(conn, VIR_ERR_INTERNAL_ERROR,
J
Jim Meyering 已提交
1762
                  "%s", _("length of metadata out of range"));
1763
        goto cleanup;
1764
    }
1765
    if (VIR_ALLOC_N(xml, len+1) < 0) {
1766
        virReportOOMError(conn);
1767
        goto cleanup;
1768
    }
1769 1770 1771
    if (saferead(fd, xml, len) != len) {
        virReportSystemError(conn, errno,
                             _("incomplete metdata in '%s'"), path);
1772
        goto cleanup;
1773 1774
    }
    xml[len] = '\0';
1775

1776
    testDriverLock(privconn);
1777 1778
    def = virDomainDefParseString(conn, privconn->caps, xml,
                                  VIR_DOMAIN_XML_INACTIVE);
1779
    if (!def)
1780
        goto cleanup;
1781

1782
    if (testDomainGenerateIfnames(conn, def) < 0)
1783
        goto cleanup;
1784 1785
    if (!(dom = virDomainAssignDef(conn, privconn->caps,
                                   &privconn->domains, def)))
1786 1787
        goto cleanup;
    def = NULL;
1788

1789 1790 1791
    if (testDomainStartState(conn, dom) < 0)
        goto cleanup;

1792 1793 1794
    event = virDomainEventNewFromObj(dom,
                                     VIR_DOMAIN_EVENT_STARTED,
                                     VIR_DOMAIN_EVENT_STARTED_RESTORED);
1795
    ret = 0;
1796 1797 1798 1799 1800 1801

cleanup:
    virDomainDefFree(def);
    VIR_FREE(xml);
    if (fd != -1)
        close(fd);
1802 1803
    if (dom)
        virDomainObjUnlock(dom);
1804 1805
    if (event)
        testDomainEventQueue(privconn, event);
1806
    testDriverUnlock(privconn);
1807
    return ret;
1808 1809
}

1810 1811 1812
static int testDomainCoreDump(virDomainPtr domain,
                              const char *to,
                              int flags ATTRIBUTE_UNUSED)
1813
{
1814
    testConnPtr privconn = domain->conn->privateData;
1815
    int fd = -1;
1816
    virDomainObjPtr privdom;
1817
    virDomainEventPtr event = NULL;
1818
    int ret = -1;
1819

1820
    testDriverLock(privconn);
1821 1822 1823 1824 1825
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);

    if (privdom == NULL) {
        testError(domain->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
1826
        goto cleanup;
1827
    }
1828 1829

    if ((fd = open(to, O_CREAT|O_TRUNC|O_WRONLY, S_IRUSR|S_IWUSR)) < 0) {
1830 1831 1832
        virReportSystemError(domain->conn, errno,
                             _("domain '%s' coredump: failed to open %s"),
                             domain->name, to);
1833
        goto cleanup;
1834
    }
1835
    if (safewrite(fd, TEST_SAVE_MAGIC, sizeof(TEST_SAVE_MAGIC)) < 0) {
1836 1837 1838
        virReportSystemError(domain->conn, errno,
                             _("domain '%s' coredump: failed to write header to %s"),
                             domain->name, to);
1839
        goto cleanup;
1840
    }
1841
    if (close(fd) < 0) {
1842 1843 1844
        virReportSystemError(domain->conn, errno,
                             _("domain '%s' coredump: write failed: %s"),
                             domain->name, to);
1845
        goto cleanup;
1846
    }
1847 1848

    testDomainShutdownState(domain, privdom);
1849 1850 1851
    event = virDomainEventNewFromObj(privdom,
                                     VIR_DOMAIN_EVENT_STOPPED,
                                     VIR_DOMAIN_EVENT_STOPPED_CRASHED);
1852

1853 1854 1855
    if (!privdom->persistent) {
        virDomainRemoveInactive(&privconn->domains,
                                privdom);
1856
        privdom = NULL;
1857
    }
1858

1859
    ret = 0;
1860 1861 1862
cleanup:
    if (fd != -1)
        close(fd);
1863 1864
    if (privdom)
        virDomainObjUnlock(privdom);
1865 1866
    if (event)
        testDomainEventQueue(privconn, event);
1867
    testDriverUnlock(privconn);
1868
    return ret;
1869 1870
}

1871 1872 1873
static char *testGetOSType(virDomainPtr dom) {
    char *ret = strdup("linux");
    if (!ret)
1874
        virReportOOMError(dom->conn);
1875
    return ret;
1876 1877 1878
}

static unsigned long testGetMaxMemory(virDomainPtr domain) {
1879 1880
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
1881
    unsigned long ret = 0;
1882

1883
    testDriverLock(privconn);
1884 1885
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
1886
    testDriverUnlock(privconn);
1887 1888 1889

    if (privdom == NULL) {
        testError(domain->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
1890
        goto cleanup;
1891
    }
1892

1893 1894 1895
    ret = privdom->def->maxmem;

cleanup:
1896 1897
    if (privdom)
        virDomainObjUnlock(privdom);
1898
    return ret;
1899 1900 1901 1902 1903
}

static int testSetMaxMemory(virDomainPtr domain,
                            unsigned long memory)
{
1904 1905
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
1906
    int ret = -1;
1907

1908
    testDriverLock(privconn);
1909 1910
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
1911
    testDriverUnlock(privconn);
1912 1913 1914

    if (privdom == NULL) {
        testError(domain->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
1915
        goto cleanup;
1916
    }
1917 1918

    /* XXX validate not over host memory wrt to other domains */
1919
    privdom->def->maxmem = memory;
1920 1921 1922
    ret = 0;

cleanup:
1923 1924
    if (privdom)
        virDomainObjUnlock(privdom);
1925
    return ret;
1926 1927
}

1928 1929 1930
static int testSetMemory(virDomainPtr domain,
                         unsigned long memory)
{
1931 1932
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
1933
    int ret = -1;
1934

1935
    testDriverLock(privconn);
1936 1937
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
1938
    testDriverUnlock(privconn);
1939 1940 1941

    if (privdom == NULL) {
        testError(domain->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
1942
        goto cleanup;
1943
    }
1944

1945
    if (memory > privdom->def->maxmem) {
1946
        testError(domain->conn,
1947
                  VIR_ERR_INVALID_ARG, __FUNCTION__);
1948
        goto cleanup;
1949
    }
1950

1951
    privdom->def->memory = memory;
1952 1953 1954
    ret = 0;

cleanup:
1955 1956
    if (privdom)
        virDomainObjUnlock(privdom);
1957
    return ret;
1958 1959
}

C
Cole Robinson 已提交
1960 1961 1962 1963 1964
static int testDomainGetMaxVcpus(virDomainPtr domain)
{
    return testGetMaxVCPUs(domain->conn, "test");
}

1965 1966
static int testSetVcpus(virDomainPtr domain,
                        unsigned int nrCpus) {
1967 1968
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
C
Cole Robinson 已提交
1969 1970 1971 1972 1973 1974
    int ret = -1, maxvcpus;

    /* Do this first before locking */
    maxvcpus = testDomainGetMaxVcpus(domain);
    if (maxvcpus < 0)
        goto cleanup;
1975

1976 1977 1978 1979 1980 1981
    testDriverLock(privconn);
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
    testDriverUnlock(privconn);

    if (privdom == NULL) {
1982
        testError(domain->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
1983
        goto cleanup;
1984
    }
1985

C
Cole Robinson 已提交
1986 1987 1988 1989 1990 1991
    if (!virDomainObjIsActive(privdom)) {
        testError(domain->conn, VIR_ERR_OPERATION_INVALID,
                  "%s", _("cannot hotplug vcpus for an inactive domain"));
        goto cleanup;
    }

1992
    /* We allow more cpus in guest than host */
C
Cole Robinson 已提交
1993 1994 1995 1996
    if (nrCpus > maxvcpus) {
        testError(domain->conn, VIR_ERR_INVALID_ARG,
                  "requested cpu amount exceeds maximum (%d > %d)",
                  nrCpus, maxvcpus);
1997
        goto cleanup;
1998
    }
1999

2000 2001 2002 2003
    /* Update VCPU state for the running domain */
    if (testDomainUpdateVCPUs(domain->conn, privdom, nrCpus, 0) < 0)
        goto cleanup;

2004
    privdom->def->vcpus = nrCpus;
2005 2006 2007
    ret = 0;

cleanup:
2008 2009
    if (privdom)
        virDomainObjUnlock(privdom);
2010
    return ret;
2011 2012
}

C
Cole Robinson 已提交
2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100
static int testDomainGetVcpus(virDomainPtr domain,
                              virVcpuInfoPtr info,
                              int maxinfo,
                              unsigned char *cpumaps,
                              int maplen)
{
    testConnPtr privconn = domain->conn->privateData;
    testDomainObjPrivatePtr privdomdata;
    virDomainObjPtr privdom;
    int i, v, maxcpu, hostcpus;
    int ret = -1;
    struct timeval tv;
    unsigned long long statbase;

    testDriverLock(privconn);
    privdom = virDomainFindByName(&privconn->domains, domain->name);
    testDriverUnlock(privconn);

    if (privdom == NULL) {
        testError(domain->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
        goto cleanup;
    }

    if (!virDomainObjIsActive(privdom)) {
        testError(domain->conn, VIR_ERR_OPERATION_INVALID,
                  "%s",_("cannot list vcpus for an inactive domain"));
        goto cleanup;
    }

    privdomdata = privdom->privateData;

    if (gettimeofday(&tv, NULL) < 0) {
        virReportSystemError(domain->conn, errno,
                             "%s", _("getting time of day"));
        goto cleanup;
    }

    statbase = (tv.tv_sec * 1000UL * 1000UL) + tv.tv_usec;


    hostcpus = VIR_NODEINFO_MAXCPUS(privconn->nodeInfo);
    maxcpu = maplen * 8;
    if (maxcpu > hostcpus)
        maxcpu = hostcpus;

    /* Clamp to actual number of vcpus */
    if (maxinfo > privdom->def->vcpus)
        maxinfo = privdom->def->vcpus;

    /* Populate virVcpuInfo structures */
    if (info != NULL) {
        memset(info, 0, sizeof(*info) * maxinfo);

        for (i = 0 ; i < maxinfo ; i++) {
            virVcpuInfo privinfo = privdomdata->vcpu_infos[i];

            info[i].number = privinfo.number;
            info[i].state = privinfo.state;
            info[i].cpu = privinfo.cpu;

            /* Fake an increasing cpu time value */
            info[i].cpuTime = statbase / 10;
        }
    }

    /* Populate cpumaps */
    if (cpumaps != NULL) {
        int privmaplen = VIR_CPU_MAPLEN(hostcpus);
        memset(cpumaps, 0, maplen * maxinfo);

        for (v = 0 ; v < maxinfo ; v++) {
            unsigned char *cpumap = VIR_GET_CPUMAP(cpumaps, maplen, v);

            for (i = 0 ; i < maxcpu ; i++) {
                if (VIR_CPU_USABLE(privdomdata->cpumaps, privmaplen, v, i)) {
                    VIR_USE_CPU(cpumap, i);
                }
            }
        }
    }

    ret = maxinfo;
cleanup:
    if (privdom)
        virDomainObjUnlock(privdom);
    return ret;
}

C
Cole Robinson 已提交
2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157
static int testDomainPinVcpu(virDomainPtr domain,
                             unsigned int vcpu,
                             unsigned char *cpumap,
                             int maplen)
{
    testConnPtr privconn = domain->conn->privateData;
    testDomainObjPrivatePtr privdomdata;
    virDomainObjPtr privdom;
    unsigned char *privcpumap;
    int i, maxcpu, hostcpus, privmaplen;
    int ret = -1;

    testDriverLock(privconn);
    privdom = virDomainFindByName(&privconn->domains, domain->name);
    testDriverUnlock(privconn);

    if (privdom == NULL) {
        testError(domain->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
        goto cleanup;
    }

    if (!virDomainObjIsActive(privdom)) {
        testError(domain->conn, VIR_ERR_OPERATION_INVALID,
                  "%s",_("cannot pin vcpus on an inactive domain"));
        goto cleanup;
    }

    if (vcpu > privdom->def->vcpus) {
        testError(domain->conn, VIR_ERR_INVALID_ARG, "%s",
                  _("requested vcpu is higher than allocated vcpus"));
        goto cleanup;
    }

    privdomdata = privdom->privateData;
    hostcpus = VIR_NODEINFO_MAXCPUS(privconn->nodeInfo);
    privmaplen = VIR_CPU_MAPLEN(hostcpus);

    maxcpu = maplen * 8;
    if (maxcpu > hostcpus)
        maxcpu = hostcpus;

    privcpumap = VIR_GET_CPUMAP(privdomdata->cpumaps, privmaplen, vcpu);
    memset(privcpumap, 0, privmaplen);

    for (i = 0 ; i < maxcpu ; i++) {
        if (VIR_CPU_USABLE(cpumap, maplen, 0, i)) {
            VIR_USE_CPU(privcpumap, i);
        }
    }

    ret = 0;
cleanup:
    if (privdom)
        virDomainObjUnlock(privdom);
    return ret;
}

2158
static char *testDomainDumpXML(virDomainPtr domain, int flags)
2159
{
2160
    testConnPtr privconn = domain->conn->privateData;
2161
    virDomainDefPtr def;
2162
    virDomainObjPtr privdom;
2163 2164
    char *ret = NULL;

2165 2166 2167 2168 2169 2170
    testDriverLock(privconn);
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
    testDriverUnlock(privconn);

    if (privdom == NULL) {
2171
        testError(domain->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
2172
        goto cleanup;
2173
    }
2174

2175 2176
    def = (flags & VIR_DOMAIN_XML_INACTIVE) &&
        privdom->newDef ? privdom->newDef : privdom->def;
2177

2178 2179 2180 2181 2182
    ret = virDomainDefFormat(domain->conn,
                             def,
                             flags);

cleanup:
2183 2184
    if (privdom)
        virDomainObjUnlock(privdom);
2185
    return ret;
2186
}
2187

2188
static int testNumOfDefinedDomains(virConnectPtr conn) {
2189
    testConnPtr privconn = conn->privateData;
2190
    int count;
2191

2192
    testDriverLock(privconn);
2193
    count = virDomainObjListNumOfDomains(&privconn->domains, 0);
2194
    testDriverUnlock(privconn);
2195

2196
    return count;
2197 2198
}

2199 2200 2201
static int testListDefinedDomains(virConnectPtr conn,
                                  char **const names,
                                  int maxnames) {
2202

2203
    testConnPtr privconn = conn->privateData;
2204
    int n;
2205

2206
    testDriverLock(privconn);
2207
    memset(names, 0, sizeof(*names)*maxnames);
2208
    n = virDomainObjListGetInactiveNames(&privconn->domains, names, maxnames);
2209
    testDriverUnlock(privconn);
2210

2211
    return n;
2212 2213
}

2214
static virDomainPtr testDomainDefineXML(virConnectPtr conn,
2215
                                        const char *xml) {
2216
    testConnPtr privconn = conn->privateData;
2217
    virDomainPtr ret = NULL;
2218
    virDomainDefPtr def;
2219
    virDomainObjPtr dom = NULL;
2220
    virDomainEventPtr event = NULL;
2221

2222
    testDriverLock(privconn);
2223 2224
    if ((def = virDomainDefParseString(conn, privconn->caps, xml,
                                       VIR_DOMAIN_XML_INACTIVE)) == NULL)
2225
        goto cleanup;
2226

2227 2228
    if (testDomainGenerateIfnames(conn, def) < 0)
        goto cleanup;
2229 2230
    if (!(dom = virDomainAssignDef(conn, privconn->caps,
                                   &privconn->domains, def)))
2231
        goto cleanup;
2232
    def = NULL;
2233
    dom->persistent = 1;
2234

2235 2236 2237
    event = virDomainEventNewFromObj(dom,
                                     VIR_DOMAIN_EVENT_DEFINED,
                                     VIR_DOMAIN_EVENT_DEFINED_ADDED);
2238

2239
    ret = virGetDomain(conn, dom->def->name, dom->def->uuid);
2240
    if (ret)
2241
        ret->id = dom->def->id;
2242 2243 2244

cleanup:
    virDomainDefFree(def);
2245 2246
    if (dom)
        virDomainObjUnlock(dom);
2247 2248
    if (event)
        testDomainEventQueue(privconn, event);
2249
    testDriverUnlock(privconn);
2250
    return ret;
2251 2252
}

2253 2254 2255
static int testNodeGetCellsFreeMemory(virConnectPtr conn,
                                      unsigned long long *freemems,
                                      int startCell, int maxCells) {
2256
    testConnPtr privconn = conn->privateData;
2257
    int i, j;
2258
    int ret = -1;
2259

2260
    testDriverLock(privconn);
2261
    if (startCell > privconn->numCells) {
2262
        testError(conn, VIR_ERR_INVALID_ARG,
J
Jim Meyering 已提交
2263
                  "%s", _("Range exceeds available cells"));
2264
        goto cleanup;
2265 2266 2267 2268 2269 2270 2271
    }

    for (i = startCell, j = 0;
         (i < privconn->numCells && j < maxCells) ;
         ++i, ++j) {
        freemems[j] = privconn->cells[i].mem;
    }
2272
    ret = j;
2273

2274
cleanup:
2275
    testDriverUnlock(privconn);
2276
    return ret;
2277 2278 2279
}


2280
static int testDomainCreate(virDomainPtr domain) {
2281 2282
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
2283
    virDomainEventPtr event = NULL;
2284
    int ret = -1;
2285

2286
    testDriverLock(privconn);
2287 2288 2289 2290 2291
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);

    if (privdom == NULL) {
        testError(domain->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
2292
        goto cleanup;
2293
    }
2294

2295
    if (privdom->state != VIR_DOMAIN_SHUTOFF) {
2296 2297
        testError(domain->conn, VIR_ERR_INTERNAL_ERROR,
                  _("Domain '%s' is already running"), domain->name);
2298
        goto cleanup;
2299 2300
    }

2301 2302 2303 2304
    if (testDomainStartState(domain->conn, privdom) < 0)
        goto cleanup;
    domain->id = privdom->def->id;

2305 2306 2307
    event = virDomainEventNewFromObj(privdom,
                                     VIR_DOMAIN_EVENT_STARTED,
                                     VIR_DOMAIN_EVENT_STARTED_BOOTED);
2308
    ret = 0;
2309

2310
cleanup:
2311 2312
    if (privdom)
        virDomainObjUnlock(privdom);
2313 2314
    if (event)
        testDomainEventQueue(privconn, event);
2315
    testDriverUnlock(privconn);
2316
    return ret;
2317 2318 2319
}

static int testDomainUndefine(virDomainPtr domain) {
2320 2321
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
2322
    virDomainEventPtr event = NULL;
2323
    int ret = -1;
2324

2325
    testDriverLock(privconn);
2326 2327 2328 2329 2330
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);

    if (privdom == NULL) {
        testError(domain->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
2331
        goto cleanup;
2332
    }
2333

2334
    if (privdom->state != VIR_DOMAIN_SHUTOFF) {
2335 2336
        testError(domain->conn, VIR_ERR_INTERNAL_ERROR,
                  _("Domain '%s' is still running"), domain->name);
2337
        goto cleanup;
2338 2339
    }

2340
    privdom->state = VIR_DOMAIN_SHUTOFF;
2341 2342 2343
    event = virDomainEventNewFromObj(privdom,
                                     VIR_DOMAIN_EVENT_UNDEFINED,
                                     VIR_DOMAIN_EVENT_UNDEFINED_REMOVED);
2344 2345
    virDomainRemoveInactive(&privconn->domains,
                            privdom);
2346
    privdom = NULL;
2347
    ret = 0;
2348

2349
cleanup:
2350 2351
    if (privdom)
        virDomainObjUnlock(privdom);
2352 2353
    if (event)
        testDomainEventQueue(privconn, event);
2354
    testDriverUnlock(privconn);
2355
    return ret;
2356 2357
}

2358 2359 2360
static int testDomainGetAutostart(virDomainPtr domain,
                                  int *autostart)
{
2361 2362
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
2363
    int ret = -1;
2364

2365
    testDriverLock(privconn);
2366 2367
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
2368
    testDriverUnlock(privconn);
2369 2370 2371

    if (privdom == NULL) {
        testError(domain->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
2372
        goto cleanup;
2373 2374
    }

2375
    *autostart = privdom->autostart;
2376 2377 2378
    ret = 0;

cleanup:
2379 2380
    if (privdom)
        virDomainObjUnlock(privdom);
2381
    return ret;
2382 2383 2384 2385 2386 2387
}


static int testDomainSetAutostart(virDomainPtr domain,
                                  int autostart)
{
2388 2389
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
2390
    int ret = -1;
2391

2392
    testDriverLock(privconn);
2393 2394
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
2395
    testDriverUnlock(privconn);
2396 2397 2398

    if (privdom == NULL) {
        testError(domain->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
2399
        goto cleanup;
2400 2401
    }

2402
    privdom->autostart = autostart ? 1 : 0;
2403 2404 2405
    ret = 0;

cleanup:
2406 2407
    if (privdom)
        virDomainObjUnlock(privdom);
2408
    return ret;
2409
}
2410

2411 2412 2413
static char *testDomainGetSchedulerType(virDomainPtr domain,
                                        int *nparams)
{
2414 2415
    char *type = NULL;

2416 2417
    *nparams = 1;
    type = strdup("fair");
2418
    if (!type)
2419
        virReportOOMError(domain->conn);
2420

2421 2422 2423 2424 2425 2426 2427
    return type;
}

static int testDomainGetSchedulerParams(virDomainPtr domain,
                                        virSchedParameterPtr params,
                                        int *nparams)
{
2428 2429
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
2430
    int ret = -1;
2431

2432
    testDriverLock(privconn);
2433 2434
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
2435
    testDriverUnlock(privconn);
2436 2437 2438

    if (privdom == NULL) {
        testError(domain->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
2439
        goto cleanup;
2440 2441
    }

2442
    if (*nparams != 1) {
2443
        testError(domain->conn, VIR_ERR_INVALID_ARG, "nparams");
2444
        goto cleanup;
2445
    }
2446 2447
    strcpy(params[0].field, "weight");
    params[0].type = VIR_DOMAIN_SCHED_FIELD_UINT;
2448 2449 2450
    /* XXX */
    /*params[0].value.ui = privdom->weight;*/
    params[0].value.ui = 50;
2451 2452 2453
    ret = 0;

cleanup:
2454 2455
    if (privdom)
        virDomainObjUnlock(privdom);
2456
    return ret;
2457
}
2458 2459


2460 2461 2462 2463
static int testDomainSetSchedulerParams(virDomainPtr domain,
                                        virSchedParameterPtr params,
                                        int nparams)
{
2464 2465
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
2466
    int ret = -1;
2467

2468
    testDriverLock(privconn);
2469 2470
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
2471
    testDriverUnlock(privconn);
2472 2473 2474

    if (privdom == NULL) {
        testError(domain->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
2475
        goto cleanup;
2476 2477
    }

2478
    if (nparams != 1) {
2479
        testError(domain->conn, VIR_ERR_INVALID_ARG, "nparams");
2480
        goto cleanup;
2481
    }
2482
    if (STRNEQ(params[0].field, "weight")) {
2483
        testError(domain->conn, VIR_ERR_INVALID_ARG, "field");
2484
        goto cleanup;
2485 2486
    }
    if (params[0].type != VIR_DOMAIN_SCHED_FIELD_UINT) {
2487
        testError(domain->conn, VIR_ERR_INVALID_ARG, "type");
2488
        goto cleanup;
2489
    }
2490 2491
    /* XXX */
    /*privdom->weight = params[0].value.ui;*/
2492 2493 2494
    ret = 0;

cleanup:
2495 2496
    if (privdom)
        virDomainObjUnlock(privdom);
2497
    return ret;
2498 2499
}

2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611
static int testDomainBlockStats(virDomainPtr domain,
                                const char *path,
                                struct _virDomainBlockStats *stats)
{
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
    struct timeval tv;
    unsigned long long statbase;
    int i, found = 0, ret = -1;

    testDriverLock(privconn);
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
    testDriverUnlock(privconn);

    if (privdom == NULL) {
        testError(domain->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
        goto error;
    }

    for (i = 0 ; i < privdom->def->ndisks ; i++) {
        if (STREQ(path, privdom->def->disks[i]->dst)) {
            found = 1;
            break;
        }
    }

    if (!found) {
        testError(domain->conn, VIR_ERR_INVALID_ARG,
                  _("invalid path: %s"), path);
        goto error;
    }

    if (gettimeofday(&tv, NULL) < 0) {
        virReportSystemError(domain->conn, errno,
                             "%s", _("getting time of day"));
        goto error;
    }

    /* No significance to these numbers, just enough to mix it up*/
    statbase = (tv.tv_sec * 1000UL * 1000UL) + tv.tv_usec;
    stats->rd_req = statbase / 10;
    stats->rd_bytes = statbase / 20;
    stats->wr_req = statbase / 30;
    stats->wr_bytes = statbase / 40;
    stats->errs = tv.tv_sec / 2;

    ret = 0;
error:
    if (privdom)
        virDomainObjUnlock(privdom);
    return ret;
}

static int testDomainInterfaceStats(virDomainPtr domain,
                                    const char *path,
                                    struct _virDomainInterfaceStats *stats)
{
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
    struct timeval tv;
    unsigned long long statbase;
    int i, found = 0, ret = -1;

    testDriverLock(privconn);
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
    testDriverUnlock(privconn);

    if (privdom == NULL) {
        testError(domain->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
        goto error;
    }

    for (i = 0 ; i < privdom->def->nnets ; i++) {
        if (privdom->def->nets[i]->ifname &&
            STREQ (privdom->def->nets[i]->ifname, path)) {
            found = 1;
            break;
        }
    }

    if (!found) {
        testError(domain->conn, VIR_ERR_INVALID_ARG,
                  _("invalid path, '%s' is not a known interface"), path);
        goto error;
    }

    if (gettimeofday(&tv, NULL) < 0) {
        virReportSystemError(domain->conn, errno,
                             "%s", _("getting time of day"));
        goto error;
    }

    /* No significance to these numbers, just enough to mix it up*/
    statbase = (tv.tv_sec * 1000UL * 1000UL) + tv.tv_usec;
    stats->rx_bytes = statbase / 10;
    stats->rx_packets = statbase / 100;
    stats->rx_errs = tv.tv_sec / 1;
    stats->rx_drop = tv.tv_sec / 2;
    stats->tx_bytes = statbase / 20;
    stats->tx_packets = statbase / 110;
    stats->tx_errs = tv.tv_sec / 3;
    stats->tx_drop = tv.tv_sec / 4;

    ret = 0;
error:
    if (privdom)
        virDomainObjUnlock(privdom);
    return ret;
}

2612
static virDrvOpenStatus testOpenNetwork(virConnectPtr conn,
2613
                                        virConnectAuthPtr auth ATTRIBUTE_UNUSED,
2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630
                                        int flags ATTRIBUTE_UNUSED) {
    if (STRNEQ(conn->driver->name, "Test"))
        return VIR_DRV_OPEN_DECLINED;

    conn->networkPrivateData = conn->privateData;
    return VIR_DRV_OPEN_SUCCESS;
}

static int testCloseNetwork(virConnectPtr conn) {
    conn->networkPrivateData = NULL;
    return 0;
}


static virNetworkPtr testLookupNetworkByUUID(virConnectPtr conn,
                                           const unsigned char *uuid)
{
2631 2632
    testConnPtr privconn = conn->privateData;
    virNetworkObjPtr net;
2633
    virNetworkPtr ret = NULL;
2634

2635 2636 2637 2638 2639
    testDriverLock(privconn);
    net = virNetworkFindByUUID(&privconn->networks, uuid);
    testDriverUnlock(privconn);

    if (net == NULL) {
2640
        testError (conn, VIR_ERR_NO_NETWORK, NULL);
2641
        goto cleanup;
2642 2643
    }

2644 2645 2646
    ret = virGetNetwork(conn, net->def->name, net->def->uuid);

cleanup:
2647 2648
    if (net)
        virNetworkObjUnlock(net);
2649
    return ret;
2650
}
2651

2652
static virNetworkPtr testLookupNetworkByName(virConnectPtr conn,
2653
                                             const char *name)
2654
{
2655
    testConnPtr privconn = conn->privateData;
2656 2657
    virNetworkObjPtr net;
    virNetworkPtr ret = NULL;
2658

2659 2660 2661 2662 2663
    testDriverLock(privconn);
    net = virNetworkFindByName(&privconn->networks, name);
    testDriverUnlock(privconn);

    if (net == NULL) {
2664
        testError (conn, VIR_ERR_NO_NETWORK, NULL);
2665
        goto cleanup;
2666 2667
    }

2668 2669 2670
    ret = virGetNetwork(conn, net->def->name, net->def->uuid);

cleanup:
2671 2672
    if (net)
        virNetworkObjUnlock(net);
2673
    return ret;
2674 2675 2676 2677
}


static int testNumNetworks(virConnectPtr conn) {
2678
    testConnPtr privconn = conn->privateData;
2679
    int numActive = 0, i;
2680

2681 2682 2683
    testDriverLock(privconn);
    for (i = 0 ; i < privconn->networks.count ; i++) {
        virNetworkObjLock(privconn->networks.objs[i]);
D
Daniel P. Berrange 已提交
2684
        if (virNetworkObjIsActive(privconn->networks.objs[i]))
2685
            numActive++;
2686 2687 2688
        virNetworkObjUnlock(privconn->networks.objs[i]);
    }
    testDriverUnlock(privconn);
2689

2690
    return numActive;
2691 2692 2693
}

static int testListNetworks(virConnectPtr conn, char **const names, int nnames) {
2694
    testConnPtr privconn = conn->privateData;
2695
    int n = 0, i;
2696

2697
    testDriverLock(privconn);
2698
    memset(names, 0, sizeof(*names)*nnames);
2699 2700
    for (i = 0 ; i < privconn->networks.count && n < nnames ; i++) {
        virNetworkObjLock(privconn->networks.objs[i]);
D
Daniel P. Berrange 已提交
2701
        if (virNetworkObjIsActive(privconn->networks.objs[i]) &&
2702 2703
            !(names[n++] = strdup(privconn->networks.objs[i]->def->name))) {
            virNetworkObjUnlock(privconn->networks.objs[i]);
2704
            goto no_memory;
2705 2706 2707 2708
        }
        virNetworkObjUnlock(privconn->networks.objs[i]);
    }
    testDriverUnlock(privconn);
2709

2710 2711 2712
    return n;

no_memory:
2713
    virReportOOMError(conn);
2714 2715
    for (n = 0 ; n < nnames ; n++)
        VIR_FREE(names[n]);
2716
    testDriverUnlock(privconn);
2717
    return -1;
2718 2719 2720
}

static int testNumDefinedNetworks(virConnectPtr conn) {
2721
    testConnPtr privconn = conn->privateData;
2722
    int numInactive = 0, i;
2723

2724 2725 2726
    testDriverLock(privconn);
    for (i = 0 ; i < privconn->networks.count ; i++) {
        virNetworkObjLock(privconn->networks.objs[i]);
D
Daniel P. Berrange 已提交
2727
        if (!virNetworkObjIsActive(privconn->networks.objs[i]))
2728
            numInactive++;
2729 2730 2731
        virNetworkObjUnlock(privconn->networks.objs[i]);
    }
    testDriverUnlock(privconn);
2732

2733
    return numInactive;
2734 2735 2736
}

static int testListDefinedNetworks(virConnectPtr conn, char **const names, int nnames) {
2737
    testConnPtr privconn = conn->privateData;
2738
    int n = 0, i;
2739

2740
    testDriverLock(privconn);
2741
    memset(names, 0, sizeof(*names)*nnames);
2742 2743
    for (i = 0 ; i < privconn->networks.count && n < nnames ; i++) {
        virNetworkObjLock(privconn->networks.objs[i]);
D
Daniel P. Berrange 已提交
2744
        if (!virNetworkObjIsActive(privconn->networks.objs[i]) &&
2745 2746
            !(names[n++] = strdup(privconn->networks.objs[i]->def->name))) {
            virNetworkObjUnlock(privconn->networks.objs[i]);
2747
            goto no_memory;
2748 2749 2750 2751
        }
        virNetworkObjUnlock(privconn->networks.objs[i]);
    }
    testDriverUnlock(privconn);
2752

2753 2754 2755
    return n;

no_memory:
2756
    virReportOOMError(conn);
2757 2758
    for (n = 0 ; n < nnames ; n++)
        VIR_FREE(names[n]);
2759
    testDriverUnlock(privconn);
2760
    return -1;
2761 2762 2763
}

static virNetworkPtr testNetworkCreate(virConnectPtr conn, const char *xml) {
2764
    testConnPtr privconn = conn->privateData;
2765
    virNetworkDefPtr def;
2766
    virNetworkObjPtr net = NULL;
2767
    virNetworkPtr ret = NULL;
2768

2769
    testDriverLock(privconn);
2770
    if ((def = virNetworkDefParseString(conn, xml)) == NULL)
2771
        goto cleanup;
2772

2773
    if ((net = virNetworkAssignDef(conn, &privconn->networks, def)) == NULL)
2774 2775
        goto cleanup;
    def = NULL;
2776
    net->active = 1;
2777

2778
    ret = virGetNetwork(conn, net->def->name, net->def->uuid);
2779

2780 2781
cleanup:
    virNetworkDefFree(def);
2782 2783 2784
    if (net)
        virNetworkObjUnlock(net);
    testDriverUnlock(privconn);
2785
    return ret;
2786 2787 2788
}

static virNetworkPtr testNetworkDefine(virConnectPtr conn, const char *xml) {
2789
    testConnPtr privconn = conn->privateData;
2790
    virNetworkDefPtr def;
2791
    virNetworkObjPtr net = NULL;
2792
    virNetworkPtr ret = NULL;
2793

2794
    testDriverLock(privconn);
2795
    if ((def = virNetworkDefParseString(conn, xml)) == NULL)
2796
        goto cleanup;
2797

2798
    if ((net = virNetworkAssignDef(conn, &privconn->networks, def)) == NULL)
2799 2800
        goto cleanup;
    def = NULL;
2801
    net->persistent = 1;
2802

2803
    ret = virGetNetwork(conn, net->def->name, net->def->uuid);
2804 2805 2806

cleanup:
    virNetworkDefFree(def);
2807 2808 2809
    if (net)
        virNetworkObjUnlock(net);
    testDriverUnlock(privconn);
2810
    return ret;
2811 2812 2813
}

static int testNetworkUndefine(virNetworkPtr network) {
2814 2815
    testConnPtr privconn = network->conn->privateData;
    virNetworkObjPtr privnet;
2816
    int ret = -1;
2817

2818
    testDriverLock(privconn);
2819 2820 2821 2822 2823
    privnet = virNetworkFindByName(&privconn->networks,
                                   network->name);

    if (privnet == NULL) {
        testError(network->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
2824
        goto cleanup;
2825
    }
2826

D
Daniel P. Berrange 已提交
2827
    if (virNetworkObjIsActive(privnet)) {
2828 2829
        testError(network->conn, VIR_ERR_INTERNAL_ERROR,
                  _("Network '%s' is still running"), network->name);
2830
        goto cleanup;
2831 2832
    }

2833 2834
    virNetworkRemoveInactive(&privconn->networks,
                             privnet);
2835
    privnet = NULL;
2836
    ret = 0;
2837

2838
cleanup:
2839 2840 2841
    if (privnet)
        virNetworkObjUnlock(privnet);
    testDriverUnlock(privconn);
2842
    return ret;
2843 2844 2845
}

static int testNetworkStart(virNetworkPtr network) {
2846 2847
    testConnPtr privconn = network->conn->privateData;
    virNetworkObjPtr privnet;
2848
    int ret = -1;
2849

2850
    testDriverLock(privconn);
2851 2852
    privnet = virNetworkFindByName(&privconn->networks,
                                   network->name);
2853
    testDriverUnlock(privconn);
2854 2855 2856

    if (privnet == NULL) {
        testError(network->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
2857
        goto cleanup;
2858
    }
2859

D
Daniel P. Berrange 已提交
2860
    if (virNetworkObjIsActive(privnet)) {
2861 2862
        testError(network->conn, VIR_ERR_INTERNAL_ERROR,
                  _("Network '%s' is already running"), network->name);
2863
        goto cleanup;
2864 2865
    }

2866
    privnet->active = 1;
2867
    ret = 0;
2868

2869
cleanup:
2870 2871
    if (privnet)
        virNetworkObjUnlock(privnet);
2872
    return ret;
2873 2874 2875
}

static int testNetworkDestroy(virNetworkPtr network) {
2876 2877
    testConnPtr privconn = network->conn->privateData;
    virNetworkObjPtr privnet;
2878
    int ret = -1;
2879

2880
    testDriverLock(privconn);
2881 2882 2883 2884 2885
    privnet = virNetworkFindByName(&privconn->networks,
                                   network->name);

    if (privnet == NULL) {
        testError(network->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
2886
        goto cleanup;
2887
    }
2888

2889 2890 2891 2892
    privnet->active = 0;
    if (!privnet->persistent) {
        virNetworkRemoveInactive(&privconn->networks,
                                 privnet);
2893
        privnet = NULL;
2894
    }
2895 2896 2897
    ret = 0;

cleanup:
2898 2899 2900
    if (privnet)
        virNetworkObjUnlock(privnet);
    testDriverUnlock(privconn);
2901
    return ret;
2902 2903 2904
}

static char *testNetworkDumpXML(virNetworkPtr network, int flags ATTRIBUTE_UNUSED) {
2905 2906
    testConnPtr privconn = network->conn->privateData;
    virNetworkObjPtr privnet;
2907
    char *ret = NULL;
2908

2909
    testDriverLock(privconn);
2910 2911
    privnet = virNetworkFindByName(&privconn->networks,
                                   network->name);
2912
    testDriverUnlock(privconn);
2913 2914 2915

    if (privnet == NULL) {
        testError(network->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
2916
        goto cleanup;
2917
    }
2918

2919 2920 2921
    ret = virNetworkDefFormat(network->conn, privnet->def);

cleanup:
2922 2923
    if (privnet)
        virNetworkObjUnlock(privnet);
2924
    return ret;
2925 2926 2927
}

static char *testNetworkGetBridgeName(virNetworkPtr network) {
2928
    testConnPtr privconn = network->conn->privateData;
2929
    char *bridge = NULL;
2930 2931
    virNetworkObjPtr privnet;

2932
    testDriverLock(privconn);
2933 2934
    privnet = virNetworkFindByName(&privconn->networks,
                                   network->name);
2935
    testDriverUnlock(privconn);
2936 2937 2938

    if (privnet == NULL) {
        testError(network->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
2939
        goto cleanup;
2940 2941
    }

2942 2943 2944 2945 2946 2947 2948 2949
    if (!(privnet->def->bridge)) {
        testError(network->conn, VIR_ERR_INTERNAL_ERROR,
                  _("network '%s' does not have a bridge name."),
                  privnet->def->name);
        goto cleanup;
    }

    if (!(bridge = strdup(privnet->def->bridge))) {
2950
        virReportOOMError(network->conn);
2951
        goto cleanup;
2952
    }
2953 2954

cleanup:
2955 2956
    if (privnet)
        virNetworkObjUnlock(privnet);
2957 2958 2959 2960 2961
    return bridge;
}

static int testNetworkGetAutostart(virNetworkPtr network,
                                   int *autostart) {
2962 2963
    testConnPtr privconn = network->conn->privateData;
    virNetworkObjPtr privnet;
2964
    int ret = -1;
2965

2966
    testDriverLock(privconn);
2967 2968
    privnet = virNetworkFindByName(&privconn->networks,
                                   network->name);
2969
    testDriverUnlock(privconn);
2970 2971 2972

    if (privnet == NULL) {
        testError(network->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
2973
        goto cleanup;
2974 2975
    }

2976
    *autostart = privnet->autostart;
2977 2978 2979
    ret = 0;

cleanup:
2980 2981
    if (privnet)
        virNetworkObjUnlock(privnet);
2982
    return ret;
2983 2984 2985 2986
}

static int testNetworkSetAutostart(virNetworkPtr network,
                                   int autostart) {
2987 2988
    testConnPtr privconn = network->conn->privateData;
    virNetworkObjPtr privnet;
2989
    int ret = -1;
2990

2991
    testDriverLock(privconn);
2992 2993
    privnet = virNetworkFindByName(&privconn->networks,
                                   network->name);
2994
    testDriverUnlock(privconn);
2995 2996 2997

    if (privnet == NULL) {
        testError(network->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
2998
        goto cleanup;
2999 3000
    }

3001
    privnet->autostart = autostart ? 1 : 0;
3002 3003 3004
    ret = 0;

cleanup:
3005 3006
    if (privnet)
        virNetworkObjUnlock(privnet);
3007
    return ret;
3008
}
3009

C
Cole Robinson 已提交
3010

L
Laine Stump 已提交
3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040
/*
 * Physical host interface routines
 */

static virDrvOpenStatus testOpenInterface(virConnectPtr conn,
                                          virConnectAuthPtr auth ATTRIBUTE_UNUSED,
                                          int flags ATTRIBUTE_UNUSED)
{
    if (STRNEQ(conn->driver->name, "Test"))
        return VIR_DRV_OPEN_DECLINED;

    conn->interfacePrivateData = conn->privateData;
    return VIR_DRV_OPEN_SUCCESS;
}

static int testCloseInterface(virConnectPtr conn)
{
    conn->interfacePrivateData = NULL;
    return 0;
}


static int testNumOfInterfaces(virConnectPtr conn)
{
    testConnPtr privconn = conn->privateData;
    int i, count = 0;

    testDriverLock(privconn);
    for (i = 0 ; (i < privconn->ifaces.count); i++) {
        virInterfaceObjLock(privconn->ifaces.objs[i]);
D
Daniel P. Berrange 已提交
3041
        if (virInterfaceObjIsActive(privconn->ifaces.objs[i])) {
L
Laine Stump 已提交
3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058
            count++;
        }
        virInterfaceObjUnlock(privconn->ifaces.objs[i]);
    }
    testDriverUnlock(privconn);
    return count;
}

static int testListInterfaces(virConnectPtr conn, char **const names, int nnames)
{
    testConnPtr privconn = conn->privateData;
    int n = 0, i;

    testDriverLock(privconn);
    memset(names, 0, sizeof(*names)*nnames);
    for (i = 0 ; (i < privconn->ifaces.count) && (n < nnames); i++) {
        virInterfaceObjLock(privconn->ifaces.objs[i]);
D
Daniel P. Berrange 已提交
3059
        if (virInterfaceObjIsActive(privconn->ifaces.objs[i])) {
L
Laine Stump 已提交
3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086
            if (!(names[n++] = strdup(privconn->ifaces.objs[i]->def->name))) {
                virInterfaceObjUnlock(privconn->ifaces.objs[i]);
                goto no_memory;
            }
        }
        virInterfaceObjUnlock(privconn->ifaces.objs[i]);
    }
    testDriverUnlock(privconn);

    return n;

no_memory:
    virReportOOMError(conn);
    for (n = 0 ; n < nnames ; n++)
        VIR_FREE(names[n]);
    testDriverUnlock(privconn);
    return -1;
}

static int testNumOfDefinedInterfaces(virConnectPtr conn)
{
    testConnPtr privconn = conn->privateData;
    int i, count = 0;

    testDriverLock(privconn);
    for (i = 0 ; i < privconn->ifaces.count; i++) {
        virInterfaceObjLock(privconn->ifaces.objs[i]);
D
Daniel P. Berrange 已提交
3087
        if (!virInterfaceObjIsActive(privconn->ifaces.objs[i])) {
L
Laine Stump 已提交
3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104
            count++;
        }
        virInterfaceObjUnlock(privconn->ifaces.objs[i]);
    }
    testDriverUnlock(privconn);
    return count;
}

static int testListDefinedInterfaces(virConnectPtr conn, char **const names, int nnames)
{
    testConnPtr privconn = conn->privateData;
    int n = 0, i;

    testDriverLock(privconn);
    memset(names, 0, sizeof(*names)*nnames);
    for (i = 0 ; (i < privconn->ifaces.count) && (n < nnames); i++) {
        virInterfaceObjLock(privconn->ifaces.objs[i]);
D
Daniel P. Berrange 已提交
3105
        if (!virInterfaceObjIsActive(privconn->ifaces.objs[i])) {
L
Laine Stump 已提交
3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318
            if (!(names[n++] = strdup(privconn->ifaces.objs[i]->def->name))) {
                virInterfaceObjUnlock(privconn->ifaces.objs[i]);
                goto no_memory;
            }
        }
        virInterfaceObjUnlock(privconn->ifaces.objs[i]);
    }
    testDriverUnlock(privconn);

    return n;

no_memory:
    virReportOOMError(conn);
    for (n = 0 ; n < nnames ; n++)
        VIR_FREE(names[n]);
    testDriverUnlock(privconn);
    return -1;
}

static virInterfacePtr testLookupInterfaceByName(virConnectPtr conn,
                                                 const char *name)
{
    testConnPtr privconn = conn->privateData;
    virInterfaceObjPtr iface;
    virInterfacePtr ret = NULL;

    testDriverLock(privconn);
    iface = virInterfaceFindByName(&privconn->ifaces, name);
    testDriverUnlock(privconn);

    if (iface == NULL) {
        testError (conn, VIR_ERR_NO_INTERFACE, NULL);
        goto cleanup;
    }

    ret = virGetInterface(conn, iface->def->name, iface->def->mac);

cleanup:
    if (iface)
        virInterfaceObjUnlock(iface);
    return ret;
}

static virInterfacePtr testLookupInterfaceByMACString(virConnectPtr conn,
                                                      const char *mac)
{
    testConnPtr privconn = conn->privateData;
    virInterfaceObjPtr iface;
    int ifacect;
    virInterfacePtr ret = NULL;

    testDriverLock(privconn);
    ifacect = virInterfaceFindByMACString(&privconn->ifaces, mac, &iface, 1);
    testDriverUnlock(privconn);

    if (ifacect == 0) {
        testError (conn, VIR_ERR_NO_INTERFACE, NULL);
        goto cleanup;
    }

    if (ifacect > 1) {
        testError (conn, VIR_ERR_MULTIPLE_INTERFACES, NULL);
        goto cleanup;
    }

    ret = virGetInterface(conn, iface->def->name, iface->def->mac);

cleanup:
    if (iface)
        virInterfaceObjUnlock(iface);
    return ret;
}

static char *testInterfaceGetXMLDesc(virInterfacePtr iface,
                                     unsigned int flags ATTRIBUTE_UNUSED)
{
    testConnPtr privconn = iface->conn->privateData;
    virInterfaceObjPtr privinterface;
    char *ret = NULL;

    testDriverLock(privconn);
    privinterface = virInterfaceFindByName(&privconn->ifaces,
                                           iface->name);
    testDriverUnlock(privconn);

    if (privinterface == NULL) {
        testError(iface->conn, VIR_ERR_NO_INTERFACE, __FUNCTION__);
        goto cleanup;
    }

    ret = virInterfaceDefFormat(iface->conn, privinterface->def);

cleanup:
    if (privinterface)
        virInterfaceObjUnlock(privinterface);
    return ret;
}


static virInterfacePtr testInterfaceDefineXML(virConnectPtr conn, const char *xmlStr,
                                              unsigned int flags ATTRIBUTE_UNUSED)
{
    testConnPtr privconn = conn->privateData;
    virInterfaceDefPtr def;
    virInterfaceObjPtr iface = NULL;
    virInterfacePtr ret = NULL;

    testDriverLock(privconn);
    if ((def = virInterfaceDefParseString(conn, xmlStr)) == NULL)
        goto cleanup;

    if ((iface = virInterfaceAssignDef(conn, &privconn->ifaces, def)) == NULL)
        goto cleanup;
    def = NULL;

    ret = virGetInterface(conn, iface->def->name, iface->def->mac);

cleanup:
    virInterfaceDefFree(def);
    if (iface)
        virInterfaceObjUnlock(iface);
    testDriverUnlock(privconn);
    return ret;
}

static int testInterfaceUndefine(virInterfacePtr iface)
{
    testConnPtr privconn = iface->conn->privateData;
    virInterfaceObjPtr privinterface;
    int ret = -1;

    testDriverLock(privconn);
    privinterface = virInterfaceFindByName(&privconn->ifaces,
                                           iface->name);

    if (privinterface == NULL) {
        testError (iface->conn, VIR_ERR_NO_INTERFACE, NULL);
        goto cleanup;
    }

    virInterfaceRemove(&privconn->ifaces,
                       privinterface);
    ret = 0;

cleanup:
    testDriverUnlock(privconn);
    return ret;
}

static int testInterfaceCreate(virInterfacePtr iface,
                               unsigned int flags ATTRIBUTE_UNUSED)
{
    testConnPtr privconn = iface->conn->privateData;
    virInterfaceObjPtr privinterface;
    int ret = -1;

    testDriverLock(privconn);
    privinterface = virInterfaceFindByName(&privconn->ifaces,
                                           iface->name);

    if (privinterface == NULL) {
        testError (iface->conn, VIR_ERR_NO_INTERFACE, NULL);
        goto cleanup;
    }

    if (privinterface->active != 0) {
        testError (iface->conn, VIR_ERR_OPERATION_INVALID, NULL);
        goto cleanup;
    }

    privinterface->active = 1;
    ret = 0;

cleanup:
    if (privinterface)
        virInterfaceObjUnlock(privinterface);
    testDriverUnlock(privconn);
    return ret;
}

static int testInterfaceDestroy(virInterfacePtr iface,
                                unsigned int flags ATTRIBUTE_UNUSED)
{
    testConnPtr privconn = iface->conn->privateData;
    virInterfaceObjPtr privinterface;
    int ret = -1;

    testDriverLock(privconn);
    privinterface = virInterfaceFindByName(&privconn->ifaces,
                                           iface->name);

    if (privinterface == NULL) {
        testError (iface->conn, VIR_ERR_NO_INTERFACE, NULL);
        goto cleanup;
    }

    if (privinterface->active == 0) {
        testError (iface->conn, VIR_ERR_OPERATION_INVALID, NULL);
        goto cleanup;
    }

    privinterface->active = 0;
    ret = 0;

cleanup:
    if (privinterface)
        virInterfaceObjUnlock(privinterface);
    testDriverUnlock(privconn);
    return ret;
}



C
Cole Robinson 已提交
3319 3320 3321 3322
/*
 * Storage Driver routines
 */

3323 3324
static int testStoragePoolObjSetDefaults(virConnectPtr conn,
                                         virStoragePoolObjPtr pool) {
C
Cole Robinson 已提交
3325 3326 3327 3328 3329 3330 3331

    pool->def->capacity = defaultPoolCap;
    pool->def->allocation = defaultPoolAlloc;
    pool->def->available = defaultPoolCap - defaultPoolAlloc;

    pool->configFile = strdup("\0");
    if (!pool->configFile) {
3332
        virReportOOMError(conn);
C
Cole Robinson 已提交
3333 3334 3335 3336 3337 3338
        return -1;
    }

    return 0;
}

3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353
static virDrvOpenStatus testStorageOpen(virConnectPtr conn,
                                        virConnectAuthPtr auth ATTRIBUTE_UNUSED,
                                        int flags ATTRIBUTE_UNUSED) {
    if (STRNEQ(conn->driver->name, "Test"))
        return VIR_DRV_OPEN_DECLINED;

    conn->storagePrivateData = conn->privateData;
    return VIR_DRV_OPEN_SUCCESS;
}

static int testStorageClose(virConnectPtr conn) {
    conn->storagePrivateData = NULL;
    return 0;
}

C
Cole Robinson 已提交
3354 3355 3356
static virStoragePoolPtr
testStoragePoolLookupByUUID(virConnectPtr conn,
                            const unsigned char *uuid) {
3357
    testConnPtr privconn = conn->privateData;
3358 3359
    virStoragePoolObjPtr pool;
    virStoragePoolPtr ret = NULL;
C
Cole Robinson 已提交
3360

3361
    testDriverLock(privconn);
3362
    pool = virStoragePoolObjFindByUUID(&privconn->pools, uuid);
3363
    testDriverUnlock(privconn);
3364 3365

    if (pool == NULL) {
C
Cole Robinson 已提交
3366
        testError (conn, VIR_ERR_NO_STORAGE_POOL, NULL);
3367
        goto cleanup;
C
Cole Robinson 已提交
3368 3369
    }

3370 3371 3372
    ret = virGetStoragePool(conn, pool->def->name, pool->def->uuid);

cleanup:
3373 3374
    if (pool)
        virStoragePoolObjUnlock(pool);
3375
    return ret;
C
Cole Robinson 已提交
3376 3377 3378 3379 3380
}

static virStoragePoolPtr
testStoragePoolLookupByName(virConnectPtr conn,
                            const char *name) {
3381
    testConnPtr privconn = conn->privateData;
3382 3383
    virStoragePoolObjPtr pool;
    virStoragePoolPtr ret = NULL;
C
Cole Robinson 已提交
3384

3385
    testDriverLock(privconn);
3386
    pool = virStoragePoolObjFindByName(&privconn->pools, name);
3387
    testDriverUnlock(privconn);
3388 3389

    if (pool == NULL) {
C
Cole Robinson 已提交
3390
        testError (conn, VIR_ERR_NO_STORAGE_POOL, NULL);
3391
        goto cleanup;
C
Cole Robinson 已提交
3392 3393
    }

3394 3395 3396
    ret = virGetStoragePool(conn, pool->def->name, pool->def->uuid);

cleanup:
3397 3398
    if (pool)
        virStoragePoolObjUnlock(pool);
3399
    return ret;
C
Cole Robinson 已提交
3400 3401 3402 3403 3404 3405 3406 3407 3408
}

static virStoragePoolPtr
testStoragePoolLookupByVolume(virStorageVolPtr vol) {
    return testStoragePoolLookupByName(vol->conn, vol->pool);
}

static int
testStorageNumPools(virConnectPtr conn) {
3409
    testConnPtr privconn = conn->privateData;
C
Cole Robinson 已提交
3410 3411
    int numActive = 0, i;

3412
    testDriverLock(privconn);
C
Cole Robinson 已提交
3413 3414 3415
    for (i = 0 ; i < privconn->pools.count ; i++)
        if (virStoragePoolObjIsActive(privconn->pools.objs[i]))
            numActive++;
3416
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
3417 3418 3419 3420 3421 3422 3423 3424

    return numActive;
}

static int
testStorageListPools(virConnectPtr conn,
                     char **const names,
                     int nnames) {
3425
    testConnPtr privconn = conn->privateData;
C
Cole Robinson 已提交
3426 3427
    int n = 0, i;

3428
    testDriverLock(privconn);
C
Cole Robinson 已提交
3429
    memset(names, 0, sizeof(*names)*nnames);
3430 3431
    for (i = 0 ; i < privconn->pools.count && n < nnames ; i++) {
        virStoragePoolObjLock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
3432
        if (virStoragePoolObjIsActive(privconn->pools.objs[i]) &&
3433 3434
            !(names[n++] = strdup(privconn->pools.objs[i]->def->name))) {
            virStoragePoolObjUnlock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
3435
            goto no_memory;
3436 3437 3438 3439
        }
        virStoragePoolObjUnlock(privconn->pools.objs[i]);
    }
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
3440 3441 3442 3443

    return n;

no_memory:
3444
    virReportOOMError(conn);
C
Cole Robinson 已提交
3445 3446
    for (n = 0 ; n < nnames ; n++)
        VIR_FREE(names[n]);
3447
    testDriverUnlock(privconn);
3448
    return -1;
C
Cole Robinson 已提交
3449 3450 3451 3452
}

static int
testStorageNumDefinedPools(virConnectPtr conn) {
3453
    testConnPtr privconn = conn->privateData;
C
Cole Robinson 已提交
3454 3455
    int numInactive = 0, i;

3456 3457 3458
    testDriverLock(privconn);
    for (i = 0 ; i < privconn->pools.count ; i++) {
        virStoragePoolObjLock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
3459 3460
        if (!virStoragePoolObjIsActive(privconn->pools.objs[i]))
            numInactive++;
3461 3462 3463
        virStoragePoolObjUnlock(privconn->pools.objs[i]);
    }
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
3464 3465 3466 3467 3468 3469 3470 3471

    return numInactive;
}

static int
testStorageListDefinedPools(virConnectPtr conn,
                            char **const names,
                            int nnames) {
3472
    testConnPtr privconn = conn->privateData;
C
Cole Robinson 已提交
3473 3474
    int n = 0, i;

3475
    testDriverLock(privconn);
C
Cole Robinson 已提交
3476
    memset(names, 0, sizeof(*names)*nnames);
3477 3478
    for (i = 0 ; i < privconn->pools.count && n < nnames ; i++) {
        virStoragePoolObjLock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
3479
        if (!virStoragePoolObjIsActive(privconn->pools.objs[i]) &&
3480 3481
            !(names[n++] = strdup(privconn->pools.objs[i]->def->name))) {
            virStoragePoolObjUnlock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
3482
            goto no_memory;
3483 3484 3485 3486
        }
        virStoragePoolObjUnlock(privconn->pools.objs[i]);
    }
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
3487 3488 3489 3490

    return n;

no_memory:
3491
    virReportOOMError(conn);
C
Cole Robinson 已提交
3492 3493
    for (n = 0 ; n < nnames ; n++)
        VIR_FREE(names[n]);
3494
    testDriverUnlock(privconn);
3495
    return -1;
C
Cole Robinson 已提交
3496 3497 3498 3499
}


static int
3500
testStoragePoolStart(virStoragePoolPtr pool,
C
Cole Robinson 已提交
3501
                     unsigned int flags ATTRIBUTE_UNUSED) {
3502 3503
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
3504
    int ret = -1;
3505

3506
    testDriverLock(privconn);
3507 3508
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
3509
    testDriverUnlock(privconn);
3510 3511 3512

    if (privpool == NULL) {
        testError(pool->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
3513
        goto cleanup;
3514 3515
    }

3516 3517 3518 3519 3520
    if (virStoragePoolObjIsActive(privpool)) {
        testError(pool->conn, VIR_ERR_INTERNAL_ERROR,
                  _("storage pool '%s' is already active"), pool->name);
        goto cleanup;
    }
C
Cole Robinson 已提交
3521 3522

    privpool->active = 1;
3523
    ret = 0;
C
Cole Robinson 已提交
3524

3525
cleanup:
3526 3527
    if (privpool)
        virStoragePoolObjUnlock(privpool);
3528
    return ret;
C
Cole Robinson 已提交
3529 3530 3531
}

static char *
3532 3533 3534
testStorageFindPoolSources(virConnectPtr conn,
                           const char *type,
                           const char *srcSpec,
C
Cole Robinson 已提交
3535 3536
                           unsigned int flags ATTRIBUTE_UNUSED)
{
3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581
    virStoragePoolSourcePtr source = NULL;
    int pool_type;
    char *ret = NULL;

    pool_type = virStoragePoolTypeFromString(type);
    if (!pool_type) {
        testError(conn, VIR_ERR_INTERNAL_ERROR,
                  _("unknown storage pool type %s"), type);
        goto cleanup;
    }

    if (srcSpec) {
        source = virStoragePoolDefParseSourceString(conn, srcSpec, pool_type);
        if (!source)
            goto cleanup;
    }

    switch (pool_type) {

    case VIR_STORAGE_POOL_LOGICAL:
        ret = strdup(defaultPoolSourcesLogicalXML);
        if (!ret)
            virReportOOMError(conn);
        break;

    case VIR_STORAGE_POOL_NETFS:
        if (!source || !source->host.name) {
            testError(conn, VIR_ERR_INVALID_ARG,
                      "%s", "hostname must be specified for netfs sources");
            goto cleanup;
        }

        if (virAsprintf(&ret, defaultPoolSourcesNetFSXML,
                        source->host.name) < 0)
            virReportOOMError(conn);
        break;

    default:
        testError(conn, VIR_ERR_NO_SUPPORT,
                  _("pool type '%s' does not support source discovery"), type);
    }

cleanup:
    virStoragePoolSourceFree(source);
    return ret;
C
Cole Robinson 已提交
3582 3583 3584 3585 3586 3587 3588
}


static virStoragePoolPtr
testStoragePoolCreate(virConnectPtr conn,
                      const char *xml,
                      unsigned int flags ATTRIBUTE_UNUSED) {
3589
    testConnPtr privconn = conn->privateData;
C
Cole Robinson 已提交
3590
    virStoragePoolDefPtr def;
3591
    virStoragePoolObjPtr pool = NULL;
3592
    virStoragePoolPtr ret = NULL;
C
Cole Robinson 已提交
3593

3594
    testDriverLock(privconn);
3595
    if (!(def = virStoragePoolDefParseString(conn, xml)))
3596
        goto cleanup;
C
Cole Robinson 已提交
3597

3598 3599 3600 3601
    pool = virStoragePoolObjFindByUUID(&privconn->pools, def->uuid);
    if (!pool)
        pool = virStoragePoolObjFindByName(&privconn->pools, def->name);
    if (pool) {
C
Cole Robinson 已提交
3602 3603
        testError(conn, VIR_ERR_INTERNAL_ERROR,
                  "%s", _("storage pool already exists"));
3604
        goto cleanup;
C
Cole Robinson 已提交
3605 3606
    }

3607
    if (!(pool = virStoragePoolObjAssignDef(conn, &privconn->pools, def)))
3608
        goto cleanup;
3609
    def = NULL;
C
Cole Robinson 已提交
3610

3611
    if (testStoragePoolObjSetDefaults(conn, pool) == -1) {
C
Cole Robinson 已提交
3612
        virStoragePoolObjRemove(&privconn->pools, pool);
3613 3614
        pool = NULL;
        goto cleanup;
C
Cole Robinson 已提交
3615 3616 3617
    }
    pool->active = 1;

3618 3619 3620 3621
    ret = virGetStoragePool(conn, pool->def->name, pool->def->uuid);

cleanup:
    virStoragePoolDefFree(def);
3622 3623 3624
    if (pool)
        virStoragePoolObjUnlock(pool);
    testDriverUnlock(privconn);
3625
    return ret;
C
Cole Robinson 已提交
3626 3627 3628 3629 3630 3631
}

static virStoragePoolPtr
testStoragePoolDefine(virConnectPtr conn,
                      const char *xml,
                      unsigned int flags ATTRIBUTE_UNUSED) {
3632
    testConnPtr privconn = conn->privateData;
C
Cole Robinson 已提交
3633
    virStoragePoolDefPtr def;
3634
    virStoragePoolObjPtr pool = NULL;
3635
    virStoragePoolPtr ret = NULL;
C
Cole Robinson 已提交
3636

3637
    testDriverLock(privconn);
3638
    if (!(def = virStoragePoolDefParseString(conn, xml)))
3639
        goto cleanup;
C
Cole Robinson 已提交
3640 3641 3642 3643 3644

    def->capacity = defaultPoolCap;
    def->allocation = defaultPoolAlloc;
    def->available = defaultPoolCap - defaultPoolAlloc;

3645
    if (!(pool = virStoragePoolObjAssignDef(conn, &privconn->pools, def)))
3646 3647
        goto cleanup;
    def = NULL;
C
Cole Robinson 已提交
3648

3649
    if (testStoragePoolObjSetDefaults(conn, pool) == -1) {
C
Cole Robinson 已提交
3650
        virStoragePoolObjRemove(&privconn->pools, pool);
3651 3652
        pool = NULL;
        goto cleanup;
C
Cole Robinson 已提交
3653 3654
    }

3655 3656 3657 3658
    ret = virGetStoragePool(conn, pool->def->name, pool->def->uuid);

cleanup:
    virStoragePoolDefFree(def);
3659 3660 3661
    if (pool)
        virStoragePoolObjUnlock(pool);
    testDriverUnlock(privconn);
3662
    return ret;
C
Cole Robinson 已提交
3663 3664 3665
}

static int
3666 3667 3668
testStoragePoolUndefine(virStoragePoolPtr pool) {
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
3669
    int ret = -1;
3670

3671
    testDriverLock(privconn);
3672 3673 3674 3675 3676
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);

    if (privpool == NULL) {
        testError(pool->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
3677
        goto cleanup;
3678 3679
    }

3680 3681 3682 3683 3684
    if (virStoragePoolObjIsActive(privpool)) {
        testError(pool->conn, VIR_ERR_INTERNAL_ERROR,
                  _("storage pool '%s' is already active"), pool->name);
        goto cleanup;
    }
C
Cole Robinson 已提交
3685 3686

    virStoragePoolObjRemove(&privconn->pools, privpool);
3687
    ret = 0;
C
Cole Robinson 已提交
3688

3689
cleanup:
3690 3691 3692
    if (privpool)
        virStoragePoolObjUnlock(privpool);
    testDriverUnlock(privconn);
3693
    return ret;
C
Cole Robinson 已提交
3694 3695 3696
}

static int
3697
testStoragePoolBuild(virStoragePoolPtr pool,
C
Cole Robinson 已提交
3698
                     unsigned int flags ATTRIBUTE_UNUSED) {
3699 3700
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
3701
    int ret = -1;
3702

3703
    testDriverLock(privconn);
3704 3705
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
3706
    testDriverUnlock(privconn);
3707 3708 3709

    if (privpool == NULL) {
        testError(pool->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
3710
        goto cleanup;
3711 3712
    }

3713 3714 3715 3716 3717
    if (virStoragePoolObjIsActive(privpool)) {
        testError(pool->conn, VIR_ERR_INTERNAL_ERROR,
                  _("storage pool '%s' is already active"), pool->name);
        goto cleanup;
    }
3718
    ret = 0;
C
Cole Robinson 已提交
3719

3720
cleanup:
3721 3722
    if (privpool)
        virStoragePoolObjUnlock(privpool);
3723
    return ret;
C
Cole Robinson 已提交
3724 3725 3726 3727
}


static int
3728 3729 3730
testStoragePoolDestroy(virStoragePoolPtr pool) {
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
3731
    int ret = -1;
3732

3733
    testDriverLock(privconn);
3734 3735 3736 3737 3738
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);

    if (privpool == NULL) {
        testError(pool->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
3739
        goto cleanup;
3740 3741 3742 3743 3744
    }

    if (!virStoragePoolObjIsActive(privpool)) {
        testError(pool->conn, VIR_ERR_INTERNAL_ERROR,
                  _("storage pool '%s' is not active"), pool->name);
3745
        goto cleanup;
3746
    }
C
Cole Robinson 已提交
3747 3748 3749

    privpool->active = 0;

3750
    if (privpool->configFile == NULL) {
C
Cole Robinson 已提交
3751
        virStoragePoolObjRemove(&privconn->pools, privpool);
3752 3753
        privpool = NULL;
    }
3754
    ret = 0;
C
Cole Robinson 已提交
3755

3756
cleanup:
3757 3758 3759
    if (privpool)
        virStoragePoolObjUnlock(privpool);
    testDriverUnlock(privconn);
3760
    return ret;
C
Cole Robinson 已提交
3761 3762 3763 3764
}


static int
3765
testStoragePoolDelete(virStoragePoolPtr pool,
C
Cole Robinson 已提交
3766
                      unsigned int flags ATTRIBUTE_UNUSED) {
3767 3768
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
3769
    int ret = -1;
3770

3771
    testDriverLock(privconn);
3772 3773
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
3774
    testDriverUnlock(privconn);
3775 3776 3777

    if (privpool == NULL) {
        testError(pool->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
3778 3779 3780 3781 3782 3783 3784
        goto cleanup;
    }

    if (virStoragePoolObjIsActive(privpool)) {
        testError(pool->conn, VIR_ERR_INTERNAL_ERROR,
                  _("storage pool '%s' is already active"), pool->name);
        goto cleanup;
3785 3786
    }

3787
    ret = 0;
C
Cole Robinson 已提交
3788

3789
cleanup:
3790 3791
    if (privpool)
        virStoragePoolObjUnlock(privpool);
3792
    return ret;
C
Cole Robinson 已提交
3793 3794 3795 3796
}


static int
3797
testStoragePoolRefresh(virStoragePoolPtr pool,
C
Cole Robinson 已提交
3798
                       unsigned int flags ATTRIBUTE_UNUSED) {
3799 3800
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
3801
    int ret = -1;
3802

3803
    testDriverLock(privconn);
3804 3805
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
3806
    testDriverUnlock(privconn);
3807 3808 3809

    if (privpool == NULL) {
        testError(pool->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
3810
        goto cleanup;
3811 3812 3813 3814 3815
    }

    if (!virStoragePoolObjIsActive(privpool)) {
        testError(pool->conn, VIR_ERR_INTERNAL_ERROR,
                  _("storage pool '%s' is not active"), pool->name);
3816
        goto cleanup;
3817
    }
3818
    ret = 0;
C
Cole Robinson 已提交
3819

3820
cleanup:
3821 3822
    if (privpool)
        virStoragePoolObjUnlock(privpool);
3823
    return ret;
C
Cole Robinson 已提交
3824 3825 3826 3827
}


static int
3828
testStoragePoolGetInfo(virStoragePoolPtr pool,
C
Cole Robinson 已提交
3829
                       virStoragePoolInfoPtr info) {
3830 3831
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
3832
    int ret = -1;
3833

3834
    testDriverLock(privconn);
3835 3836
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
3837
    testDriverUnlock(privconn);
3838 3839 3840

    if (privpool == NULL) {
        testError(pool->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
3841
        goto cleanup;
3842
    }
C
Cole Robinson 已提交
3843 3844 3845 3846 3847 3848 3849 3850 3851

    memset(info, 0, sizeof(virStoragePoolInfo));
    if (privpool->active)
        info->state = VIR_STORAGE_POOL_RUNNING;
    else
        info->state = VIR_STORAGE_POOL_INACTIVE;
    info->capacity = privpool->def->capacity;
    info->allocation = privpool->def->allocation;
    info->available = privpool->def->available;
3852
    ret = 0;
C
Cole Robinson 已提交
3853

3854
cleanup:
3855 3856
    if (privpool)
        virStoragePoolObjUnlock(privpool);
3857
    return ret;
C
Cole Robinson 已提交
3858 3859 3860
}

static char *
3861
testStoragePoolDumpXML(virStoragePoolPtr pool,
C
Cole Robinson 已提交
3862
                       unsigned int flags ATTRIBUTE_UNUSED) {
3863 3864
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
3865
    char *ret = NULL;
3866

3867
    testDriverLock(privconn);
3868 3869
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
3870
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
3871

3872 3873
    if (privpool == NULL) {
        testError(pool->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
3874
        goto cleanup;
3875 3876
    }

3877 3878 3879
    ret = virStoragePoolDefFormat(pool->conn, privpool->def);

cleanup:
3880 3881
    if (privpool)
        virStoragePoolObjUnlock(privpool);
3882
    return ret;
C
Cole Robinson 已提交
3883 3884 3885
}

static int
3886
testStoragePoolGetAutostart(virStoragePoolPtr pool,
C
Cole Robinson 已提交
3887
                            int *autostart) {
3888 3889
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
3890
    int ret = -1;
3891

3892
    testDriverLock(privconn);
3893 3894
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
3895
    testDriverUnlock(privconn);
3896 3897 3898

    if (privpool == NULL) {
        testError(pool->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
3899
        goto cleanup;
3900
    }
C
Cole Robinson 已提交
3901 3902 3903 3904 3905 3906

    if (!privpool->configFile) {
        *autostart = 0;
    } else {
        *autostart = privpool->autostart;
    }
3907
    ret = 0;
C
Cole Robinson 已提交
3908

3909
cleanup:
3910 3911
    if (privpool)
        virStoragePoolObjUnlock(privpool);
3912
    return ret;
C
Cole Robinson 已提交
3913 3914 3915
}

static int
3916
testStoragePoolSetAutostart(virStoragePoolPtr pool,
C
Cole Robinson 已提交
3917
                            int autostart) {
3918 3919
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
3920
    int ret = -1;
3921

3922
    testDriverLock(privconn);
3923 3924
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
3925
    testDriverUnlock(privconn);
3926 3927 3928

    if (privpool == NULL) {
        testError(pool->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
3929
        goto cleanup;
3930
    }
C
Cole Robinson 已提交
3931 3932

    if (!privpool->configFile) {
3933
        testError(pool->conn, VIR_ERR_INVALID_ARG,
C
Cole Robinson 已提交
3934
                  "%s", _("pool has no config file"));
3935
        goto cleanup;
C
Cole Robinson 已提交
3936 3937 3938 3939
    }

    autostart = (autostart != 0);
    privpool->autostart = autostart;
3940 3941 3942
    ret = 0;

cleanup:
3943 3944
    if (privpool)
        virStoragePoolObjUnlock(privpool);
3945
    return ret;
C
Cole Robinson 已提交
3946 3947 3948 3949
}


static int
3950 3951 3952
testStoragePoolNumVolumes(virStoragePoolPtr pool) {
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
3953
    int ret = -1;
3954

3955
    testDriverLock(privconn);
3956 3957
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
3958
    testDriverUnlock(privconn);
3959 3960 3961

    if (privpool == NULL) {
        testError(pool->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
3962
        goto cleanup;
3963 3964 3965 3966 3967
    }

    if (!virStoragePoolObjIsActive(privpool)) {
        testError(pool->conn, VIR_ERR_INTERNAL_ERROR,
                  _("storage pool '%s' is not active"), pool->name);
3968
        goto cleanup;
3969
    }
C
Cole Robinson 已提交
3970

3971 3972 3973
    ret = privpool->volumes.count;

cleanup:
3974 3975
    if (privpool)
        virStoragePoolObjUnlock(privpool);
3976
    return ret;
C
Cole Robinson 已提交
3977 3978 3979
}

static int
3980
testStoragePoolListVolumes(virStoragePoolPtr pool,
C
Cole Robinson 已提交
3981 3982
                           char **const names,
                           int maxnames) {
3983 3984
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
C
Cole Robinson 已提交
3985 3986
    int i = 0, n = 0;

3987
    memset(names, 0, maxnames * sizeof(*names));
3988 3989

    testDriverLock(privconn);
3990 3991
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
3992
    testDriverUnlock(privconn);
3993 3994 3995

    if (privpool == NULL) {
        testError(pool->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
3996
        goto cleanup;
3997 3998 3999 4000 4001 4002
    }


    if (!virStoragePoolObjIsActive(privpool)) {
        testError(pool->conn, VIR_ERR_INTERNAL_ERROR,
                  _("storage pool '%s' is not active"), pool->name);
4003
        goto cleanup;
4004 4005
    }

C
Cole Robinson 已提交
4006 4007
    for (i = 0 ; i < privpool->volumes.count && n < maxnames ; i++) {
        if ((names[n++] = strdup(privpool->volumes.objs[i]->name)) == NULL) {
4008
            virReportOOMError(pool->conn);
C
Cole Robinson 已提交
4009 4010 4011 4012
            goto cleanup;
        }
    }

4013
    virStoragePoolObjUnlock(privpool);
C
Cole Robinson 已提交
4014 4015 4016 4017 4018 4019
    return n;

 cleanup:
    for (n = 0 ; n < maxnames ; n++)
        VIR_FREE(names[i]);

4020
    memset(names, 0, maxnames * sizeof(*names));
4021 4022
    if (privpool)
        virStoragePoolObjUnlock(privpool);
C
Cole Robinson 已提交
4023 4024 4025 4026 4027
    return -1;
}


static virStorageVolPtr
4028
testStorageVolumeLookupByName(virStoragePoolPtr pool,
C
Cole Robinson 已提交
4029
                              const char *name ATTRIBUTE_UNUSED) {
4030 4031 4032
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol;
4033
    virStorageVolPtr ret = NULL;
4034

4035
    testDriverLock(privconn);
4036 4037
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4038
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
4039

4040 4041
    if (privpool == NULL) {
        testError(pool->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
4042
        goto cleanup;
4043 4044 4045 4046 4047 4048
    }


    if (!virStoragePoolObjIsActive(privpool)) {
        testError(pool->conn, VIR_ERR_INTERNAL_ERROR,
                  _("storage pool '%s' is not active"), pool->name);
4049
        goto cleanup;
4050 4051 4052 4053 4054 4055
    }

    privvol = virStorageVolDefFindByName(privpool, name);

    if (!privvol) {
        testError(pool->conn, VIR_ERR_INVALID_STORAGE_VOL,
C
Cole Robinson 已提交
4056
                  _("no storage vol with matching name '%s'"), name);
4057
        goto cleanup;
C
Cole Robinson 已提交
4058 4059
    }

4060 4061 4062 4063
    ret = virGetStorageVol(pool->conn, privpool->def->name,
                           privvol->name, privvol->key);

cleanup:
4064 4065
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4066
    return ret;
C
Cole Robinson 已提交
4067 4068 4069 4070 4071 4072
}


static virStorageVolPtr
testStorageVolumeLookupByKey(virConnectPtr conn,
                             const char *key) {
4073
    testConnPtr privconn = conn->privateData;
C
Cole Robinson 已提交
4074
    unsigned int i;
4075
    virStorageVolPtr ret = NULL;
C
Cole Robinson 已提交
4076

4077
    testDriverLock(privconn);
C
Cole Robinson 已提交
4078
    for (i = 0 ; i < privconn->pools.count ; i++) {
4079
        virStoragePoolObjLock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
4080
        if (virStoragePoolObjIsActive(privconn->pools.objs[i])) {
4081
            virStorageVolDefPtr privvol =
C
Cole Robinson 已提交
4082 4083
                virStorageVolDefFindByKey(privconn->pools.objs[i], key);

4084 4085 4086 4087 4088
            if (privvol) {
                ret = virGetStorageVol(conn,
                                       privconn->pools.objs[i]->def->name,
                                       privvol->name,
                                       privvol->key);
4089
                virStoragePoolObjUnlock(privconn->pools.objs[i]);
4090 4091
                break;
            }
C
Cole Robinson 已提交
4092
        }
4093
        virStoragePoolObjUnlock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
4094
    }
4095
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
4096

4097 4098 4099 4100 4101
    if (!ret)
        testError(conn, VIR_ERR_INVALID_STORAGE_VOL,
                  _("no storage vol with matching key '%s'"), key);

    return ret;
C
Cole Robinson 已提交
4102 4103 4104 4105 4106
}

static virStorageVolPtr
testStorageVolumeLookupByPath(virConnectPtr conn,
                              const char *path) {
4107
    testConnPtr privconn = conn->privateData;
C
Cole Robinson 已提交
4108
    unsigned int i;
4109
    virStorageVolPtr ret = NULL;
C
Cole Robinson 已提交
4110

4111
    testDriverLock(privconn);
C
Cole Robinson 已提交
4112
    for (i = 0 ; i < privconn->pools.count ; i++) {
4113
        virStoragePoolObjLock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
4114
        if (virStoragePoolObjIsActive(privconn->pools.objs[i])) {
4115
            virStorageVolDefPtr privvol =
C
Cole Robinson 已提交
4116 4117
                virStorageVolDefFindByPath(privconn->pools.objs[i], path);

4118 4119 4120 4121 4122
            if (privvol) {
                ret = virGetStorageVol(conn,
                                       privconn->pools.objs[i]->def->name,
                                       privvol->name,
                                       privvol->key);
4123
                virStoragePoolObjUnlock(privconn->pools.objs[i]);
4124 4125
                break;
            }
C
Cole Robinson 已提交
4126
        }
4127
        virStoragePoolObjUnlock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
4128
    }
4129
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
4130

4131 4132 4133 4134 4135
    if (!ret)
        testError(conn, VIR_ERR_INVALID_STORAGE_VOL,
                  _("no storage vol with matching path '%s'"), path);

    return ret;
C
Cole Robinson 已提交
4136 4137 4138
}

static virStorageVolPtr
4139
testStorageVolumeCreateXML(virStoragePoolPtr pool,
C
Cole Robinson 已提交
4140 4141
                           const char *xmldesc,
                           unsigned int flags ATTRIBUTE_UNUSED) {
4142 4143
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4144 4145
    virStorageVolDefPtr privvol = NULL;
    virStorageVolPtr ret = NULL;
4146

4147
    testDriverLock(privconn);
4148 4149
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4150
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
4151

4152 4153
    if (privpool == NULL) {
        testError(pool->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
4154
        goto cleanup;
4155 4156 4157 4158 4159
    }

    if (!virStoragePoolObjIsActive(privpool)) {
        testError(pool->conn, VIR_ERR_INTERNAL_ERROR,
                  _("storage pool '%s' is not active"), pool->name);
4160
        goto cleanup;
4161
    }
C
Cole Robinson 已提交
4162

4163
    privvol = virStorageVolDefParseString(pool->conn, privpool->def, xmldesc);
4164
    if (privvol == NULL)
4165
        goto cleanup;
4166 4167

    if (virStorageVolDefFindByName(privpool, privvol->name)) {
4168
        testError(pool->conn, VIR_ERR_INVALID_STORAGE_VOL,
C
Cole Robinson 已提交
4169
                  "%s", _("storage vol already exists"));
4170
        goto cleanup;
C
Cole Robinson 已提交
4171 4172 4173
    }

    /* Make sure enough space */
4174
    if ((privpool->def->allocation + privvol->allocation) >
C
Cole Robinson 已提交
4175
         privpool->def->capacity) {
4176
        testError(pool->conn, VIR_ERR_INTERNAL_ERROR,
C
Cole Robinson 已提交
4177
                  _("Not enough free space in pool for volume '%s'"),
4178
                  privvol->name);
4179
        goto cleanup;
C
Cole Robinson 已提交
4180 4181 4182 4183
    }

    if (VIR_REALLOC_N(privpool->volumes.objs,
                      privpool->volumes.count+1) < 0) {
4184
        virReportOOMError(pool->conn);
4185
        goto cleanup;
C
Cole Robinson 已提交
4186 4187
    }

4188 4189 4190
    if (virAsprintf(&privvol->target.path, "%s/%s",
                    privpool->def->target.path,
                    privvol->name) == -1) {
4191
        virReportOOMError(pool->conn);
4192
        goto cleanup;
C
Cole Robinson 已提交
4193 4194
    }

4195 4196
    privvol->key = strdup(privvol->target.path);
    if (privvol->key == NULL) {
4197
        virReportOOMError(pool->conn);
4198
        goto cleanup;
C
Cole Robinson 已提交
4199 4200
    }

4201
    privpool->def->allocation += privvol->allocation;
C
Cole Robinson 已提交
4202 4203 4204
    privpool->def->available = (privpool->def->capacity -
                                privpool->def->allocation);

4205
    privpool->volumes.objs[privpool->volumes.count++] = privvol;
C
Cole Robinson 已提交
4206

4207 4208
    ret = virGetStorageVol(pool->conn, privpool->def->name,
                           privvol->name, privvol->key);
4209
    privvol = NULL;
4210 4211 4212

cleanup:
    virStorageVolDefFree(privvol);
4213 4214
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4215
    return ret;
C
Cole Robinson 已提交
4216 4217
}

4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243
static virStorageVolPtr
testStorageVolumeCreateXMLFrom(virStoragePoolPtr pool,
                               const char *xmldesc,
                               virStorageVolPtr clonevol,
                               unsigned int flags ATTRIBUTE_UNUSED) {
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol = NULL, origvol = NULL;
    virStorageVolPtr ret = NULL;

    testDriverLock(privconn);
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
    testDriverUnlock(privconn);

    if (privpool == NULL) {
        testError(pool->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
        goto cleanup;
    }

    if (!virStoragePoolObjIsActive(privpool)) {
        testError(pool->conn, VIR_ERR_INTERNAL_ERROR,
                  _("storage pool '%s' is not active"), pool->name);
        goto cleanup;
    }

4244
    privvol = virStorageVolDefParseString(pool->conn, privpool->def, xmldesc);
4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278
    if (privvol == NULL)
        goto cleanup;

    if (virStorageVolDefFindByName(privpool, privvol->name)) {
        testError(pool->conn, VIR_ERR_INVALID_STORAGE_VOL,
                  "%s", _("storage vol already exists"));
        goto cleanup;
    }

    origvol = virStorageVolDefFindByName(privpool, clonevol->name);
    if (!origvol) {
        testError(pool->conn, VIR_ERR_INVALID_STORAGE_VOL,
                  _("no storage vol with matching name '%s'"),
                  clonevol->name);
        goto cleanup;
    }

    /* Make sure enough space */
    if ((privpool->def->allocation + privvol->allocation) >
         privpool->def->capacity) {
        testError(pool->conn, VIR_ERR_INTERNAL_ERROR,
                  _("Not enough free space in pool for volume '%s'"),
                  privvol->name);
        goto cleanup;
    }
    privpool->def->available = (privpool->def->capacity -
                                privpool->def->allocation);

    if (VIR_REALLOC_N(privpool->volumes.objs,
                      privpool->volumes.count+1) < 0) {
        virReportOOMError(pool->conn);
        goto cleanup;
    }

4279 4280 4281
    if (virAsprintf(&privvol->target.path, "%s/%s",
                    privpool->def->target.path,
                    privvol->name) == -1) {
4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308
        virReportOOMError(pool->conn);
        goto cleanup;
    }

    privvol->key = strdup(privvol->target.path);
    if (privvol->key == NULL) {
        virReportOOMError(pool->conn);
        goto cleanup;
    }

    privpool->def->allocation += privvol->allocation;
    privpool->def->available = (privpool->def->capacity -
                                privpool->def->allocation);

    privpool->volumes.objs[privpool->volumes.count++] = privvol;

    ret = virGetStorageVol(pool->conn, privpool->def->name,
                           privvol->name, privvol->key);
    privvol = NULL;

cleanup:
    virStorageVolDefFree(privvol);
    if (privpool)
        virStoragePoolObjUnlock(privpool);
    return ret;
}

C
Cole Robinson 已提交
4309
static int
4310
testStorageVolumeDelete(virStorageVolPtr vol,
C
Cole Robinson 已提交
4311
                        unsigned int flags ATTRIBUTE_UNUSED) {
4312 4313 4314
    testConnPtr privconn = vol->conn->privateData;
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol;
C
Cole Robinson 已提交
4315
    int i;
4316
    int ret = -1;
C
Cole Robinson 已提交
4317

4318
    testDriverLock(privconn);
4319 4320
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           vol->pool);
4321
    testDriverUnlock(privconn);
4322 4323 4324

    if (privpool == NULL) {
        testError(vol->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
4325
        goto cleanup;
4326 4327 4328 4329 4330 4331 4332 4333 4334
    }


    privvol = virStorageVolDefFindByName(privpool, vol->name);

    if (privvol == NULL) {
        testError(vol->conn, VIR_ERR_INVALID_STORAGE_VOL,
                  _("no storage vol with matching name '%s'"),
                  vol->name);
4335
        goto cleanup;
4336 4337 4338 4339 4340
    }

    if (!virStoragePoolObjIsActive(privpool)) {
        testError(vol->conn, VIR_ERR_INTERNAL_ERROR,
                  _("storage pool '%s' is not active"), vol->pool);
4341
        goto cleanup;
4342 4343 4344
    }


C
Cole Robinson 已提交
4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367
    privpool->def->allocation -= privvol->allocation;
    privpool->def->available = (privpool->def->capacity -
                                privpool->def->allocation);

    for (i = 0 ; i < privpool->volumes.count ; i++) {
        if (privpool->volumes.objs[i] == privvol) {
            virStorageVolDefFree(privvol);

            if (i < (privpool->volumes.count - 1))
                memmove(privpool->volumes.objs + i,
                        privpool->volumes.objs + i + 1,
                        sizeof(*(privpool->volumes.objs)) *
                                (privpool->volumes.count - (i + 1)));

            if (VIR_REALLOC_N(privpool->volumes.objs,
                              privpool->volumes.count - 1) < 0) {
                ; /* Failure to reduce memory allocation isn't fatal */
            }
            privpool->volumes.count--;

            break;
        }
    }
4368
    ret = 0;
C
Cole Robinson 已提交
4369

4370
cleanup:
4371 4372
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4373
    return ret;
C
Cole Robinson 已提交
4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389
}


static int testStorageVolumeTypeForPool(int pooltype) {

    switch(pooltype) {
        case VIR_STORAGE_POOL_DIR:
        case VIR_STORAGE_POOL_FS:
        case VIR_STORAGE_POOL_NETFS:
            return VIR_STORAGE_VOL_FILE;
        default:
            return VIR_STORAGE_VOL_BLOCK;
    }
}

static int
4390
testStorageVolumeGetInfo(virStorageVolPtr vol,
C
Cole Robinson 已提交
4391
                         virStorageVolInfoPtr info) {
4392 4393 4394
    testConnPtr privconn = vol->conn->privateData;
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol;
4395
    int ret = -1;
4396

4397
    testDriverLock(privconn);
4398 4399
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           vol->pool);
4400
    testDriverUnlock(privconn);
4401 4402 4403

    if (privpool == NULL) {
        testError(vol->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
4404
        goto cleanup;
4405 4406 4407 4408 4409 4410 4411 4412
    }

    privvol = virStorageVolDefFindByName(privpool, vol->name);

    if (privvol == NULL) {
        testError(vol->conn, VIR_ERR_INVALID_STORAGE_VOL,
                  _("no storage vol with matching name '%s'"),
                  vol->name);
4413
        goto cleanup;
4414 4415 4416 4417 4418
    }

    if (!virStoragePoolObjIsActive(privpool)) {
        testError(vol->conn, VIR_ERR_INTERNAL_ERROR,
                  _("storage pool '%s' is not active"), vol->pool);
4419
        goto cleanup;
4420
    }
C
Cole Robinson 已提交
4421 4422 4423 4424 4425

    memset(info, 0, sizeof(*info));
    info->type = testStorageVolumeTypeForPool(privpool->def->type);
    info->capacity = privvol->capacity;
    info->allocation = privvol->allocation;
4426
    ret = 0;
C
Cole Robinson 已提交
4427

4428
cleanup:
4429 4430
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4431
    return ret;
C
Cole Robinson 已提交
4432 4433 4434
}

static char *
4435
testStorageVolumeGetXMLDesc(virStorageVolPtr vol,
C
Cole Robinson 已提交
4436
                            unsigned int flags ATTRIBUTE_UNUSED) {
4437 4438 4439
    testConnPtr privconn = vol->conn->privateData;
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol;
4440
    char *ret = NULL;
4441

4442
    testDriverLock(privconn);
4443 4444
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           vol->pool);
4445
    testDriverUnlock(privconn);
4446 4447 4448

    if (privpool == NULL) {
        testError(vol->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
4449
        goto cleanup;
4450 4451 4452 4453 4454 4455 4456 4457
    }

    privvol = virStorageVolDefFindByName(privpool, vol->name);

    if (privvol == NULL) {
        testError(vol->conn, VIR_ERR_INVALID_STORAGE_VOL,
                  _("no storage vol with matching name '%s'"),
                  vol->name);
4458
        goto cleanup;
4459
    }
C
Cole Robinson 已提交
4460

4461 4462 4463
    if (!virStoragePoolObjIsActive(privpool)) {
        testError(vol->conn, VIR_ERR_INTERNAL_ERROR,
                  _("storage pool '%s' is not active"), vol->pool);
4464
        goto cleanup;
4465 4466
    }

4467 4468 4469
    ret = virStorageVolDefFormat(vol->conn, privpool->def, privvol);

cleanup:
4470 4471
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4472
    return ret;
C
Cole Robinson 已提交
4473 4474 4475
}

static char *
4476 4477 4478 4479
testStorageVolumeGetPath(virStorageVolPtr vol) {
    testConnPtr privconn = vol->conn->privateData;
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol;
4480
    char *ret = NULL;
4481

4482
    testDriverLock(privconn);
4483 4484
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           vol->pool);
4485
    testDriverUnlock(privconn);
4486 4487 4488

    if (privpool == NULL) {
        testError(vol->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
4489
        goto cleanup;
4490 4491 4492 4493 4494 4495 4496 4497
    }

    privvol = virStorageVolDefFindByName(privpool, vol->name);

    if (privvol == NULL) {
        testError(vol->conn, VIR_ERR_INVALID_STORAGE_VOL,
                  _("no storage vol with matching name '%s'"),
                  vol->name);
4498
        goto cleanup;
4499 4500 4501 4502 4503
    }

    if (!virStoragePoolObjIsActive(privpool)) {
        testError(vol->conn, VIR_ERR_INTERNAL_ERROR,
                  _("storage pool '%s' is not active"), vol->pool);
4504
        goto cleanup;
4505 4506
    }

C
Cole Robinson 已提交
4507
    ret = strdup(privvol->target.path);
4508
    if (ret == NULL)
4509
        virReportOOMError(vol->conn);
4510 4511

cleanup:
4512 4513
    if (privpool)
        virStoragePoolObjUnlock(privpool);
C
Cole Robinson 已提交
4514 4515 4516
    return ret;
}

4517

4518
/* Node device implementations */
4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533
static virDrvOpenStatus testDevMonOpen(virConnectPtr conn,
                                       virConnectAuthPtr auth ATTRIBUTE_UNUSED,
                                       int flags ATTRIBUTE_UNUSED) {
    if (STRNEQ(conn->driver->name, "Test"))
        return VIR_DRV_OPEN_DECLINED;

    conn->devMonPrivateData = conn->privateData;
    return VIR_DRV_OPEN_SUCCESS;
}

static int testDevMonClose(virConnectPtr conn) {
    conn->devMonPrivateData = NULL;
    return 0;
}

4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671
static int
testNodeNumOfDevices(virConnectPtr conn,
                     const char *cap,
                     unsigned int flags ATTRIBUTE_UNUSED)
{
    testConnPtr driver = conn->privateData;
    int ndevs = 0;
    unsigned int i;

    testDriverLock(driver);
    for (i = 0; i < driver->devs.count; i++)
        if ((cap == NULL) ||
            virNodeDeviceHasCap(driver->devs.objs[i], cap))
            ++ndevs;
    testDriverUnlock(driver);

    return ndevs;
}

static int
testNodeListDevices(virConnectPtr conn,
                    const char *cap,
                    char **const names,
                    int maxnames,
                    unsigned int flags ATTRIBUTE_UNUSED)
{
    testConnPtr driver = conn->privateData;
    int ndevs = 0;
    unsigned int i;

    testDriverLock(driver);
    for (i = 0; i < driver->devs.count && ndevs < maxnames; i++) {
        virNodeDeviceObjLock(driver->devs.objs[i]);
        if (cap == NULL ||
            virNodeDeviceHasCap(driver->devs.objs[i], cap)) {
            if ((names[ndevs++] = strdup(driver->devs.objs[i]->def->name)) == NULL) {
                virNodeDeviceObjUnlock(driver->devs.objs[i]);
                goto failure;
            }
        }
        virNodeDeviceObjUnlock(driver->devs.objs[i]);
    }
    testDriverUnlock(driver);

    return ndevs;

 failure:
    testDriverUnlock(driver);
    --ndevs;
    while (--ndevs >= 0)
        VIR_FREE(names[ndevs]);
    return -1;
}

static virNodeDevicePtr
testNodeDeviceLookupByName(virConnectPtr conn, const char *name)
{
    testConnPtr driver = conn->privateData;
    virNodeDeviceObjPtr obj;
    virNodeDevicePtr ret = NULL;

    testDriverLock(driver);
    obj = virNodeDeviceFindByName(&driver->devs, name);
    testDriverUnlock(driver);

    if (!obj) {
        virNodeDeviceReportError(conn, VIR_ERR_NO_NODE_DEVICE, NULL);
        goto cleanup;
    }

    ret = virGetNodeDevice(conn, name);

cleanup:
    if (obj)
        virNodeDeviceObjUnlock(obj);
    return ret;
}

static char *
testNodeDeviceDumpXML(virNodeDevicePtr dev,
                      unsigned int flags ATTRIBUTE_UNUSED)
{
    testConnPtr driver = dev->conn->privateData;
    virNodeDeviceObjPtr obj;
    char *ret = NULL;

    testDriverLock(driver);
    obj = virNodeDeviceFindByName(&driver->devs, dev->name);
    testDriverUnlock(driver);

    if (!obj) {
        virNodeDeviceReportError(dev->conn, VIR_ERR_NO_NODE_DEVICE,
                                _("no node device with matching name '%s'"),
                                 dev->name);
        goto cleanup;
    }

    ret = virNodeDeviceDefFormat(dev->conn, obj->def);

cleanup:
    if (obj)
        virNodeDeviceObjUnlock(obj);
    return ret;
}

static char *
testNodeDeviceGetParent(virNodeDevicePtr dev)
{
    testConnPtr driver = dev->conn->privateData;
    virNodeDeviceObjPtr obj;
    char *ret = NULL;

    testDriverLock(driver);
    obj = virNodeDeviceFindByName(&driver->devs, dev->name);
    testDriverUnlock(driver);

    if (!obj) {
        virNodeDeviceReportError(dev->conn, VIR_ERR_NO_NODE_DEVICE,
                                _("no node device with matching name '%s'"),
                                 dev->name);
        goto cleanup;
    }

    if (obj->def->parent) {
        ret = strdup(obj->def->parent);
        if (!ret)
            virReportOOMError(dev->conn);
    } else {
        virNodeDeviceReportError(dev->conn, VIR_ERR_INTERNAL_ERROR,
                                 "%s", _("no parent for this device"));
    }

cleanup:
    if (obj)
        virNodeDeviceObjUnlock(obj);
    return ret;
}

4672

4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741
static int
testNodeDeviceNumOfCaps(virNodeDevicePtr dev)
{
    testConnPtr driver = dev->conn->privateData;
    virNodeDeviceObjPtr obj;
    virNodeDevCapsDefPtr caps;
    int ncaps = 0;
    int ret = -1;

    testDriverLock(driver);
    obj = virNodeDeviceFindByName(&driver->devs, dev->name);
    testDriverUnlock(driver);

    if (!obj) {
        virNodeDeviceReportError(dev->conn, VIR_ERR_NO_NODE_DEVICE,
                                _("no node device with matching name '%s'"),
                                 dev->name);
        goto cleanup;
    }

    for (caps = obj->def->caps; caps; caps = caps->next)
        ++ncaps;
    ret = ncaps;

cleanup:
    if (obj)
        virNodeDeviceObjUnlock(obj);
    return ret;
}


static int
testNodeDeviceListCaps(virNodeDevicePtr dev, char **const names, int maxnames)
{
    testConnPtr driver = dev->conn->privateData;
    virNodeDeviceObjPtr obj;
    virNodeDevCapsDefPtr caps;
    int ncaps = 0;
    int ret = -1;

    testDriverLock(driver);
    obj = virNodeDeviceFindByName(&driver->devs, dev->name);
    testDriverUnlock(driver);

    if (!obj) {
        virNodeDeviceReportError(dev->conn, VIR_ERR_NO_NODE_DEVICE,
                                _("no node device with matching name '%s'"),
                                 dev->name);
        goto cleanup;
    }

    for (caps = obj->def->caps; caps && ncaps < maxnames; caps = caps->next) {
        names[ncaps] = strdup(virNodeDevCapTypeToString(caps->type));
        if (names[ncaps++] == NULL)
            goto cleanup;
    }
    ret = ncaps;

cleanup:
    if (obj)
        virNodeDeviceObjUnlock(obj);
    if (ret == -1) {
        --ncaps;
        while (--ncaps >= 0)
            VIR_FREE(names[ncaps]);
    }
    return ret;
}

4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866
static virNodeDevicePtr
testNodeDeviceCreateXML(virConnectPtr conn,
                        const char *xmlDesc,
                        unsigned int flags ATTRIBUTE_UNUSED)
{
    testConnPtr driver = conn->privateData;
    virNodeDeviceDefPtr def = NULL;
    virNodeDeviceObjPtr obj = NULL;
    char *wwnn = NULL, *wwpn = NULL;
    int parent_host = -1;
    virNodeDevicePtr dev = NULL;
    virNodeDevCapsDefPtr caps;

    testDriverLock(driver);

    def = virNodeDeviceDefParseString(conn, xmlDesc, CREATE_DEVICE);
    if (def == NULL) {
        goto cleanup;
    }

    /* We run these next two simply for validation */
    if (virNodeDeviceGetWWNs(conn, def, &wwnn, &wwpn) == -1) {
        goto cleanup;
    }

    if (virNodeDeviceGetParentHost(conn,
                                   &driver->devs,
                                   def->name,
                                   def->parent,
                                   &parent_host) == -1) {
        goto cleanup;
    }

    /* 'name' is supposed to be filled in by the node device backend, which
     * we don't have. Use WWPN instead. */
    VIR_FREE(def->name);
    if (!(def->name = strdup(wwpn))) {
        virReportOOMError(conn);
        goto cleanup;
    }

    /* Fill in a random 'host' value, since this would also come from
     * the backend */
    caps = def->caps;
    while (caps) {
        if (caps->type != VIR_NODE_DEV_CAP_SCSI_HOST)
            continue;

        caps->data.scsi_host.host = virRandom(1024);
        caps = caps->next;
    }


    if (!(obj = virNodeDeviceAssignDef(conn, &driver->devs, def))) {
        goto cleanup;
    }
    virNodeDeviceObjUnlock(obj);

    dev = virGetNodeDevice(conn, def->name);
    def = NULL;
cleanup:
    testDriverUnlock(driver);
    if (def)
        virNodeDeviceDefFree(def);
    VIR_FREE(wwnn);
    VIR_FREE(wwpn);
    return dev;
}

static int
testNodeDeviceDestroy(virNodeDevicePtr dev)
{
    int ret = 0;
    testConnPtr driver = dev->conn->privateData;
    virNodeDeviceObjPtr obj = NULL;
    char *parent_name = NULL, *wwnn = NULL, *wwpn = NULL;
    int parent_host = -1;

    testDriverLock(driver);
    obj = virNodeDeviceFindByName(&driver->devs, dev->name);
    testDriverUnlock(driver);

    if (!obj) {
        virNodeDeviceReportError(dev->conn, VIR_ERR_NO_NODE_DEVICE, NULL);
        goto out;
    }

    if (virNodeDeviceGetWWNs(dev->conn, obj->def, &wwnn, &wwpn) == -1) {
        goto out;
    }

    parent_name = strdup(obj->def->parent);
    if (parent_name == NULL) {
        virReportOOMError(dev->conn);
        goto out;
    }

    /* virNodeDeviceGetParentHost will cause the device object's lock to be
     * taken, so we have to dup the parent's name and drop the lock
     * before calling it.  We don't need the reference to the object
     * any more once we have the parent's name.  */
    virNodeDeviceObjUnlock(obj);

    /* We do this just for basic validation */
    if (virNodeDeviceGetParentHost(dev->conn,
                                   &driver->devs,
                                   dev->name,
                                   parent_name,
                                   &parent_host) == -1) {
        obj = NULL;
        goto out;
    }

    virNodeDeviceObjLock(obj);
    virNodeDeviceObjRemove(&driver->devs, obj);

out:
    if (obj)
        virNodeDeviceObjUnlock(obj);
    VIR_FREE(parent_name);
    VIR_FREE(wwnn);
    VIR_FREE(wwpn);
    return ret;
}

4867 4868

/* Domain event implementations */
4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964
static int
testDomainEventRegister (virConnectPtr conn,
                         virConnectDomainEventCallback callback,
                         void *opaque,
                         virFreeCallback freecb)
{
    testConnPtr driver = conn->privateData;
    int ret;

    testDriverLock(driver);
    ret = virDomainEventCallbackListAdd(conn, driver->domainEventCallbacks,
                                        callback, opaque, freecb);
    testDriverUnlock(driver);

    return ret;
}

static int
testDomainEventDeregister (virConnectPtr conn,
                           virConnectDomainEventCallback callback)
{
    testConnPtr driver = conn->privateData;
    int ret;

    testDriverLock(driver);
    if (driver->domainEventDispatching)
        ret = virDomainEventCallbackListMarkDelete(conn, driver->domainEventCallbacks,
                                                   callback);
    else
        ret = virDomainEventCallbackListRemove(conn, driver->domainEventCallbacks,
                                               callback);
    testDriverUnlock(driver);

    return ret;
}

static void testDomainEventDispatchFunc(virConnectPtr conn,
                                        virDomainEventPtr event,
                                        virConnectDomainEventCallback cb,
                                        void *cbopaque,
                                        void *opaque)
{
    testConnPtr driver = opaque;

    /* Drop the lock whle dispatching, for sake of re-entrancy */
    testDriverUnlock(driver);
    virDomainEventDispatchDefaultFunc(conn, event, cb, cbopaque, NULL);
    testDriverLock(driver);
}

static void testDomainEventFlush(int timer ATTRIBUTE_UNUSED, void *opaque)
{
    testConnPtr driver = opaque;
    virDomainEventQueue tempQueue;

    testDriverLock(driver);

    driver->domainEventDispatching = 1;

    /* Copy the queue, so we're reentrant safe */
    tempQueue.count = driver->domainEventQueue->count;
    tempQueue.events = driver->domainEventQueue->events;
    driver->domainEventQueue->count = 0;
    driver->domainEventQueue->events = NULL;

    virEventUpdateTimeout(driver->domainEventTimer, -1);
    virDomainEventQueueDispatch(&tempQueue,
                                driver->domainEventCallbacks,
                                testDomainEventDispatchFunc,
                                driver);

    /* Purge any deleted callbacks */
    virDomainEventCallbackListPurgeMarked(driver->domainEventCallbacks);

    driver->domainEventDispatching = 0;
    testDriverUnlock(driver);
}


/* driver must be locked before calling */
static void testDomainEventQueue(testConnPtr driver,
                                 virDomainEventPtr event)
{
    if (driver->domainEventTimer < 0) {
        virDomainEventFree(event);
        return;
    }

    if (virDomainEventQueuePush(driver->domainEventQueue,
                                event) < 0)
        virDomainEventFree(event);

    if (driver->domainEventQueue->count == 1)
        virEventUpdateTimeout(driver->domainEventTimer, 0);
}

4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978
static virDrvOpenStatus testSecretOpen(virConnectPtr conn,
                                       virConnectAuthPtr auth ATTRIBUTE_UNUSED,
                                       int flags ATTRIBUTE_UNUSED) {
    if (STRNEQ(conn->driver->name, "Test"))
        return VIR_DRV_OPEN_DECLINED;

    conn->secretPrivateData = conn->privateData;
    return VIR_DRV_OPEN_SUCCESS;
}

static int testSecretClose(virConnectPtr conn) {
    conn->secretPrivateData = NULL;
    return 0;
}
4979

4980 4981 4982 4983 4984
static virDriver testDriver = {
    VIR_DRV_TEST,
    "Test",
    testOpen, /* open */
    testClose, /* close */
4985
    NULL, /* supports_feature */
4986 4987
    NULL, /* type */
    testGetVersion, /* version */
4988
    virGetHostname, /* getHostname */
4989 4990 4991 4992 4993
    testGetMaxVCPUs, /* getMaxVcpus */
    testNodeGetInfo, /* nodeGetInfo */
    testGetCapabilities, /* getCapabilities */
    testListDomains, /* listDomains */
    testNumOfDomains, /* numOfDomains */
4994
    testDomainCreateXML, /* domainCreateXML */
4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011
    testLookupDomainByID, /* domainLookupByID */
    testLookupDomainByUUID, /* domainLookupByUUID */
    testLookupDomainByName, /* domainLookupByName */
    testPauseDomain, /* domainSuspend */
    testResumeDomain, /* domainResume */
    testShutdownDomain, /* domainShutdown */
    testRebootDomain, /* domainReboot */
    testDestroyDomain, /* domainDestroy */
    testGetOSType, /* domainGetOSType */
    testGetMaxMemory, /* domainGetMaxMemory */
    testSetMaxMemory, /* domainSetMaxMemory */
    testSetMemory, /* domainSetMemory */
    testGetDomainInfo, /* domainGetInfo */
    testDomainSave, /* domainSave */
    testDomainRestore, /* domainRestore */
    testDomainCoreDump, /* domainCoreDump */
    testSetVcpus, /* domainSetVcpus */
C
Cole Robinson 已提交
5012
    testDomainPinVcpu, /* domainPinVcpu */
C
Cole Robinson 已提交
5013
    testDomainGetVcpus, /* domainGetVcpus */
C
Cole Robinson 已提交
5014
    testDomainGetMaxVcpus, /* domainGetMaxVcpus */
5015 5016
    NULL, /* domainGetSecurityLabel */
    NULL, /* nodeGetSecurityModel */
5017
    testDomainDumpXML, /* domainDumpXML */
5018 5019
    NULL, /* domainXMLFromNative */
    NULL, /* domainXMLToNative */
5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031
    testListDefinedDomains, /* listDefinedDomains */
    testNumOfDefinedDomains, /* numOfDefinedDomains */
    testDomainCreate, /* domainCreate */
    testDomainDefineXML, /* domainDefineXML */
    testDomainUndefine, /* domainUndefine */
    NULL, /* domainAttachDevice */
    NULL, /* domainDetachDevice */
    testDomainGetAutostart, /* domainGetAutostart */
    testDomainSetAutostart, /* domainSetAutostart */
    testDomainGetSchedulerType, /* domainGetSchedulerType */
    testDomainGetSchedulerParams, /* domainGetSchedulerParameters */
    testDomainSetSchedulerParams, /* domainSetSchedulerParameters */
5032 5033 5034
    NULL, /* domainMigratePrepare */
    NULL, /* domainMigratePerform */
    NULL, /* domainMigrateFinish */
5035 5036
    testDomainBlockStats, /* domainBlockStats */
    testDomainInterfaceStats, /* domainInterfaceStats */
R
Richard W.M. Jones 已提交
5037
    NULL, /* domainBlockPeek */
R
Richard W.M. Jones 已提交
5038
    NULL, /* domainMemoryPeek */
5039
    testNodeGetCellsFreeMemory, /* nodeGetCellsFreeMemory */
5040
    NULL, /* getFreeMemory */
5041 5042
    testDomainEventRegister, /* domainEventRegister */
    testDomainEventDeregister, /* domainEventDeregister */
D
Daniel Veillard 已提交
5043 5044
    NULL, /* domainMigratePrepare2 */
    NULL, /* domainMigrateFinish2 */
5045
    NULL, /* nodeDeviceDettach */
5046 5047
    NULL, /* nodeDeviceReAttach */
    NULL, /* nodeDeviceReset */
C
Chris Lalancette 已提交
5048
    NULL, /* domainMigratePrepareTunnel */
5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071
};

static virNetworkDriver testNetworkDriver = {
    "Test",
    testOpenNetwork, /* open */
    testCloseNetwork, /* close */
    testNumNetworks, /* numOfNetworks */
    testListNetworks, /* listNetworks */
    testNumDefinedNetworks, /* numOfDefinedNetworks */
    testListDefinedNetworks, /* listDefinedNetworks */
    testLookupNetworkByUUID, /* networkLookupByUUID */
    testLookupNetworkByName, /* networkLookupByName */
    testNetworkCreate, /* networkCreateXML */
    testNetworkDefine, /* networkDefineXML */
    testNetworkUndefine, /* networkUndefine */
    testNetworkStart, /* networkCreate */
    testNetworkDestroy, /* networkDestroy */
    testNetworkDumpXML, /* networkDumpXML */
    testNetworkGetBridgeName, /* networkGetBridgeName */
    testNetworkGetAutostart, /* networkGetAutostart */
    testNetworkSetAutostart, /* networkSetAutostart */
};

L
Laine Stump 已提交
5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089
static virInterfaceDriver testInterfaceDriver = {
    "Test",                     /* name */
    testOpenInterface,          /* open */
    testCloseInterface,         /* close */
    testNumOfInterfaces,        /* numOfInterfaces */
    testListInterfaces,         /* listInterfaces */
    testNumOfDefinedInterfaces, /* numOfDefinedInterfaces */
    testListDefinedInterfaces,  /* listDefinedInterfaces */
    testLookupInterfaceByName,  /* interfaceLookupByName */
    testLookupInterfaceByMACString, /* interfaceLookupByMACString */
    testInterfaceGetXMLDesc,    /* interfaceGetXMLDesc */
    testInterfaceDefineXML,     /* interfaceDefineXML */
    testInterfaceUndefine,      /* interfaceUndefine */
    testInterfaceCreate,        /* interfaceCreate */
    testInterfaceDestroy,       /* interfaceDestroy */
};


5090 5091 5092 5093
static virStorageDriver testStorageDriver = {
    .name = "Test",
    .open = testStorageOpen,
    .close = testStorageClose,
C
Cole Robinson 已提交
5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121

    .numOfPools = testStorageNumPools,
    .listPools = testStorageListPools,
    .numOfDefinedPools = testStorageNumDefinedPools,
    .listDefinedPools = testStorageListDefinedPools,
    .findPoolSources = testStorageFindPoolSources,
    .poolLookupByName = testStoragePoolLookupByName,
    .poolLookupByUUID = testStoragePoolLookupByUUID,
    .poolLookupByVolume = testStoragePoolLookupByVolume,
    .poolCreateXML = testStoragePoolCreate,
    .poolDefineXML = testStoragePoolDefine,
    .poolBuild = testStoragePoolBuild,
    .poolUndefine = testStoragePoolUndefine,
    .poolCreate = testStoragePoolStart,
    .poolDestroy = testStoragePoolDestroy,
    .poolDelete = testStoragePoolDelete,
    .poolRefresh = testStoragePoolRefresh,
    .poolGetInfo = testStoragePoolGetInfo,
    .poolGetXMLDesc = testStoragePoolDumpXML,
    .poolGetAutostart = testStoragePoolGetAutostart,
    .poolSetAutostart = testStoragePoolSetAutostart,
    .poolNumOfVolumes = testStoragePoolNumVolumes,
    .poolListVolumes = testStoragePoolListVolumes,

    .volLookupByName = testStorageVolumeLookupByName,
    .volLookupByKey = testStorageVolumeLookupByKey,
    .volLookupByPath = testStorageVolumeLookupByPath,
    .volCreateXML = testStorageVolumeCreateXML,
5122
    .volCreateXMLFrom = testStorageVolumeCreateXMLFrom,
C
Cole Robinson 已提交
5123 5124 5125 5126
    .volDelete = testStorageVolumeDelete,
    .volGetInfo = testStorageVolumeGetInfo,
    .volGetXMLDesc = testStorageVolumeGetXMLDesc,
    .volGetPath = testStorageVolumeGetPath,
5127 5128
};

5129 5130 5131 5132
static virDeviceMonitor testDevMonitor = {
    .name = "Test",
    .open = testDevMonOpen,
    .close = testDevMonClose,
5133 5134 5135 5136 5137 5138 5139 5140

    .numOfDevices = testNodeNumOfDevices,
    .listDevices = testNodeListDevices,
    .deviceLookupByName = testNodeDeviceLookupByName,
    .deviceDumpXML = testNodeDeviceDumpXML,
    .deviceGetParent = testNodeDeviceGetParent,
    .deviceNumOfCaps = testNodeDeviceNumOfCaps,
    .deviceListCaps = testNodeDeviceListCaps,
5141 5142
    .deviceCreateXML = testNodeDeviceCreateXML,
    .deviceDestroy = testNodeDeviceDestroy,
5143 5144
};

5145 5146 5147 5148 5149
static virSecretDriver testSecretDriver = {
    .name = "Test",
    .open = testSecretOpen,
    .close = testSecretClose,
};
5150 5151


5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163
/**
 * testRegister:
 *
 * Registers the test driver
 */
int
testRegister(void)
{
    if (virRegisterDriver(&testDriver) < 0)
        return -1;
    if (virRegisterNetworkDriver(&testNetworkDriver) < 0)
        return -1;
L
Laine Stump 已提交
5164 5165
    if (virRegisterInterfaceDriver(&testInterfaceDriver) < 0)
        return -1;
5166 5167
    if (virRegisterStorageDriver(&testStorageDriver) < 0)
        return -1;
5168 5169
    if (virRegisterDeviceMonitor(&testDevMonitor) < 0)
        return -1;
5170 5171
    if (virRegisterSecretDriver(&testSecretDriver) < 0)
        return -1;
5172

5173 5174
    return 0;
}