test_driver.c 158.4 KB
Newer Older
1 2 3
/*
 * test.c: A "mock" hypervisor for use by application unit tests
 *
E
Eric Blake 已提交
4
 * Copyright (C) 2006-2011 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
#include "domain_event.h"
C
Cole Robinson 已提交
47
#include "storage_conf.h"
48
#include "node_device_conf.h"
49
#include "xml.h"
50
#include "threads.h"
51
#include "logging.h"
E
Eric Blake 已提交
52
#include "virfile.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

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

94
    virDomainEventStatePtr domainEventState;
95 96 97
};
typedef struct _testConn testConn;
typedef struct _testConn *testConnPtr;
98

99
#define TEST_MODEL "i686"
100
#define TEST_MODEL_WORDSIZE 32
101
#define TEST_EMULATOR "/usr/bin/test-hv"
102

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

114

115
#define testError(code, ...)                                      \
116
        virReportErrorHelper(VIR_FROM_TEST, code, __FILE__,       \
117
                             __FUNCTION__, __LINE__, __VA_ARGS__)
118

119 120 121 122 123 124
static int testClose(virConnectPtr conn);
static void testDomainEventFlush(int timer, void *opaque);
static void testDomainEventQueue(testConnPtr driver,
                                 virDomainEventPtr event);


125 126
static void testDriverLock(testConnPtr driver)
{
127
    virMutexLock(&driver->lock);
128 129 130 131
}

static void testDriverUnlock(testConnPtr driver)
{
132
    virMutexUnlock(&driver->lock);
133 134
}

135 136 137 138 139 140 141 142 143 144 145 146 147 148
static void *testDomainObjPrivateAlloc(void)
{
    testDomainObjPrivatePtr priv;

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

    return priv;
}

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

D
Daniel P. Berrange 已提交
149
    VIR_FREE(priv->vcpu_infos);
150 151 152 153 154
    VIR_FREE(priv->cpumaps);
    VIR_FREE(priv);
}


155 156 157 158 159
static int testDefaultConsoleType(const char *ostype ATTRIBUTE_UNUSED)
{
    return VIR_DOMAIN_CHR_CONSOLE_TARGET_TYPE_SERIAL;
}

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

168 169
    if ((caps = virCapabilitiesNew(TEST_MODEL, 0, 0)) == NULL)
        goto no_memory;
170

171 172
    caps->defaultConsoleTargetType = testDefaultConsoleType;

173 174 175 176
    if (virCapabilitiesAddHostFeature(caps, "pae") < 0)
        goto no_memory;
    if (virCapabilitiesAddHostFeature(caps ,"nonpae") < 0)
        goto no_memory;
177

178 179 180 181
    for (i = 0; i < privconn->numCells; i++) {
        if (virCapabilitiesAddHostNUMACell(caps, i, privconn->cells[i].numCpus,
                                           privconn->cells[i].cpus) < 0)
            goto no_memory;
182 183
    }

184 185 186 187 188 189 190 191 192 193
    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;
194

195 196 197 198 199 200 201
        if (virCapabilitiesAddGuestDomain(guest,
                                          "test",
                                          NULL,
                                          NULL,
                                          0,
                                          NULL) == NULL)
            goto no_memory;
202

203 204 205 206
        if (virCapabilitiesAddGuestFeature(guest, "pae", 1, 1) == NULL)
            goto no_memory;
        if (virCapabilitiesAddGuestFeature(guest ,"nonpae", 1, 1) == NULL)
            goto no_memory;
207 208
    }

209 210 211
    caps->privateDataAllocFunc = testDomainObjPrivateAlloc;
    caps->privateDataFreeFunc = testDomainObjPrivateFree;

212 213 214 215 216 217 218 219
    caps->host.secModel.model = strdup("testSecurity");
    if (!caps->host.secModel.model)
        goto no_memory;

    caps->host.secModel.doi = strdup("");
    if (!caps->host.secModel.doi)
        goto no_memory;

220
    return caps;
221

222
no_memory:
223
    virReportOOMError();
224 225
    virCapabilitiesFree(caps);
    return NULL;
226 227
}

228

229 230 231 232 233 234 235 236 237 238
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>";
239 240


241 242 243 244 245 246 247 248 249 250 251
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>";
252

L
Laine Stump 已提交
253 254 255 256 257 258 259 260 261 262 263
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 已提交
264 265 266 267 268 269 270 271
static const char *defaultPoolXML =
"<pool type='dir'>"
"  <name>default-pool</name>"
"  <target>"
"    <path>/default-pool</path>"
"  </target>"
"</pool>";

272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
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";

295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
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>";

313
static const unsigned long long defaultPoolCap = (100 * 1024 * 1024 * 1024ull);
C
Cole Robinson 已提交
314 315
static const unsigned long long defaultPoolAlloc = 0;

316
static int testStoragePoolObjSetDefaults(virStoragePoolObjPtr pool);
317
static int testNodeGetInfo(virConnectPtr conn, virNodeInfoPtr info);
318

319
static char *
320
testDomainGenerateIfname(virDomainDefPtr domdef) {
321 322 323 324 325 326 327 328
    int maxif = 1024;
    int ifctr, i;

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

        if (virAsprintf(&ifname, "testnet%d", ifctr) < 0) {
329
            virReportOOMError();
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
            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;
    }

346
    testError(VIR_ERR_INTERNAL_ERROR,
347 348 349 350
              _("Exceeded max iface limit %d"), maxif);
    return NULL;
}

351
static int
352
testDomainGenerateIfnames(virDomainDefPtr domdef)
353 354 355 356 357 358 359 360
{
    int i = 0;

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

361
        ifname = testDomainGenerateIfname(domdef);
362
        if (!ifname)
363
            return -1;
364 365 366 367

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

368
    return 0;
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
/* 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) {
435
        virReportOOMError();
436 437 438 439
        goto cleanup;
    }

    if (VIR_REALLOC_N(privdata->cpumaps, nvcpus * cpumaplen) < 0) {
440
        virReportOOMError();
441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
        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;
    }

457
    dom->def->vcpus = nvcpus;
458 459 460 461 462
    ret = 0;
cleanup:
    return ret;
}

463 464
static void
testDomainShutdownState(virDomainPtr domain,
J
Jiri Denemark 已提交
465 466
                        virDomainObjPtr privdom,
                        virDomainShutoffReason reason)
467 468 469 470 471 472 473
{
    if (privdom->newDef) {
        virDomainDefFree(privdom->def);
        privdom->def = privdom->newDef;
        privdom->newDef = NULL;
    }

J
Jiri Denemark 已提交
474
    virDomainObjSetState(privdom, VIR_DOMAIN_SHUTOFF, reason);
475 476 477 478 479
    privdom->def->id = -1;
    if (domain)
        domain->id = -1;
}

480
/* Set up domain runtime state */
481 482
static int
testDomainStartState(virConnectPtr conn,
J
Jiri Denemark 已提交
483 484
                     virDomainObjPtr dom,
                     virDomainRunningReason reason)
485 486
{
    testConnPtr privconn = conn->privateData;
487
    int ret = -1;
488

489 490 491
    if (testDomainUpdateVCPUs(conn, dom, dom->def->vcpus, 1) < 0)
        goto cleanup;

J
Jiri Denemark 已提交
492
    virDomainObjSetState(dom, VIR_DOMAIN_RUNNING, reason);
493 494
    dom->def->id = privconn->nextDomID++;

495
    if (virDomainObjSetDefTransient(privconn->caps, dom, false) < 0) {
496 497 498
        goto cleanup;
    }

499 500
    ret = 0;
cleanup:
501
    if (ret < 0)
J
Jiri Denemark 已提交
502
        testDomainShutdownState(NULL, dom, VIR_DOMAIN_SHUTOFF_FAILED);
503
    return ret;
504
}
505

506
static int testOpenDefault(virConnectPtr conn) {
507
    int u;
508
    testConnPtr privconn;
509 510 511 512
    virDomainDefPtr domdef = NULL;
    virDomainObjPtr domobj = NULL;
    virNetworkDefPtr netdef = NULL;
    virNetworkObjPtr netobj = NULL;
L
Laine Stump 已提交
513 514
    virInterfaceDefPtr interfacedef = NULL;
    virInterfaceObjPtr interfaceobj = NULL;
C
Cole Robinson 已提交
515 516
    virStoragePoolDefPtr pooldef = NULL;
    virStoragePoolObjPtr poolobj = NULL;
517 518
    virNodeDeviceDefPtr nodedef = NULL;
    virNodeDeviceObjPtr nodeobj = NULL;
519

520
    if (VIR_ALLOC(privconn) < 0) {
521
        virReportOOMError();
522 523
        return VIR_DRV_OPEN_ERROR;
    }
524
    if (virMutexInit(&privconn->lock) < 0) {
525
        testError(VIR_ERR_INTERNAL_ERROR,
526 527 528 529 530
                  "%s", _("cannot initialize mutex"));
        VIR_FREE(privconn);
        return VIR_DRV_OPEN_ERROR;
    }

531
    testDriverLock(privconn);
532
    conn->privateData = privconn;
533

534 535 536
    if (virDomainObjListInit(&privconn->domains) < 0)
        goto error;

537
    memmove(&privconn->nodeInfo, &defaultNodeInfo, sizeof(defaultNodeInfo));
538

539
    /* Numa setup */
540 541 542 543 544 545 546 547 548
    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;
    }

549 550 551 552 553
    if (!(privconn->caps = testBuildCapabilities(conn)))
        goto error;

    privconn->nextDomID = 1;

M
Matthias Bolte 已提交
554 555
    if (!(domdef = virDomainDefParseString(privconn->caps, defaultDomainXML,
                                           1 << VIR_DOMAIN_VIRT_TEST,
556
                                           VIR_DOMAIN_XML_INACTIVE)))
557
        goto error;
M
Matthias Bolte 已提交
558

559
    if (testDomainGenerateIfnames(domdef) < 0)
560
        goto error;
561
    if (!(domobj = virDomainAssignDef(privconn->caps,
562
                                      &privconn->domains, domdef, false)))
563 564
        goto error;
    domdef = NULL;
565

566
    domobj->persistent = 1;
J
Jiri Denemark 已提交
567
    if (testDomainStartState(conn, domobj, VIR_DOMAIN_RUNNING_BOOTED) < 0) {
568 569 570 571
        virDomainObjUnlock(domobj);
        goto error;
    }

572
    virDomainObjUnlock(domobj);
573

574
    if (!(netdef = virNetworkDefParseString(defaultNetworkXML)))
575
        goto error;
576
    if (!(netobj = virNetworkAssignDef(&privconn->networks, netdef))) {
577 578 579 580 581
        virNetworkDefFree(netdef);
        goto error;
    }
    netobj->active = 1;
    netobj->persistent = 1;
582
    virNetworkObjUnlock(netobj);
583

584
    if (!(interfacedef = virInterfaceDefParseString(defaultInterfaceXML)))
L
Laine Stump 已提交
585
        goto error;
586
    if (!(interfaceobj = virInterfaceAssignDef(&privconn->ifaces, interfacedef))) {
L
Laine Stump 已提交
587 588 589 590 591 592
        virInterfaceDefFree(interfacedef);
        goto error;
    }
    interfaceobj->active = 1;
    virInterfaceObjUnlock(interfaceobj);

593
    if (!(pooldef = virStoragePoolDefParseString(defaultPoolXML)))
C
Cole Robinson 已提交
594 595
        goto error;

596
    if (!(poolobj = virStoragePoolObjAssignDef(&privconn->pools,
C
Cole Robinson 已提交
597 598 599 600
                                               pooldef))) {
        virStoragePoolDefFree(pooldef);
        goto error;
    }
601

602
    if (testStoragePoolObjSetDefaults(poolobj) == -1) {
603
        virStoragePoolObjUnlock(poolobj);
C
Cole Robinson 已提交
604
        goto error;
605
    }
C
Cole Robinson 已提交
606
    poolobj->active = 1;
607
    virStoragePoolObjUnlock(poolobj);
C
Cole Robinson 已提交
608

609
    /* Init default node device */
610
    if (!(nodedef = virNodeDeviceDefParseString(defaultNodeXML, 0)))
611
        goto error;
612
    if (!(nodeobj = virNodeDeviceAssignDef(&privconn->devs,
613 614 615 616 617 618
                                           nodedef))) {
        virNodeDeviceDefFree(nodedef);
        goto error;
    }
    virNodeDeviceObjUnlock(nodeobj);

619
    testDriverUnlock(privconn);
620

621 622 623
    return VIR_DRV_OPEN_SUCCESS;

error:
624
    virDomainObjListDeinit(&privconn->domains);
625
    virNetworkObjListFree(&privconn->networks);
L
Laine Stump 已提交
626
    virInterfaceObjListFree(&privconn->ifaces);
C
Cole Robinson 已提交
627
    virStoragePoolObjListFree(&privconn->pools);
628
    virNodeDeviceObjListFree(&privconn->devs);
629
    virCapabilitiesFree(privconn->caps);
630
    testDriverUnlock(privconn);
631
    conn->privateData = NULL;
632
    VIR_FREE(privconn);
633
    virDomainDefFree(domdef);
634
    return VIR_DRV_OPEN_ERROR;
635 636 637 638
}


static char *testBuildFilename(const char *relativeTo,
639 640 641 642 643 644 645 646
                               const char *filename) {
    char *offset;
    int baseLen;
    if (!filename || filename[0] == '\0')
        return (NULL);
    if (filename[0] == '/')
        return strdup(filename);

647
    offset = strrchr(relativeTo, '/');
648
    if ((baseLen = (offset-relativeTo+1))) {
649
        char *absFile;
C
Chris Lalancette 已提交
650 651
        int totalLen = baseLen + strlen(filename) + 1;
        if (VIR_ALLOC_N(absFile, totalLen) < 0)
652
            return NULL;
C
Chris Lalancette 已提交
653 654 655 656
        if (virStrncpy(absFile, relativeTo, baseLen, totalLen) == NULL) {
            VIR_FREE(absFile);
            return NULL;
        }
657 658 659 660 661
        strcat(absFile, filename);
        return absFile;
    } else {
        return strdup(filename);
    }
662 663
}

664
static int testOpenVolumesForPool(xmlDocPtr xml,
665 666 667 668 669 670 671
                                  xmlXPathContextPtr ctxt,
                                  const char *file,
                                  virStoragePoolObjPtr pool,
                                  int poolidx) {
    char *vol_xpath;
    int i, ret, func_ret = -1;
    xmlNodePtr *vols = NULL;
672
    virStorageVolDefPtr def = NULL;
673 674 675

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

680
    ret = virXPathNodeSet(vol_xpath, ctxt, &vols);
681 682 683 684 685 686 687 688 689 690 691
    VIR_FREE(vol_xpath);
    if (ret < 0) {
        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) {
692
                testError(VIR_ERR_INTERNAL_ERROR, "%s",
693 694 695 696
                          _("resolving volume filename"));
                goto error;
            }

697
            def = virStorageVolDefParseFile(pool->def, absFile);
698 699 700 701
            VIR_FREE(absFile);
            if (!def)
                goto error;
        } else {
702
            if ((def = virStorageVolDefParseNode(pool->def, xml,
703 704 705 706 707 708 709
                                                 vols[i])) == NULL) {
                goto error;
            }
        }

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

714 715 716 717 718 719 720
        if (def->target.path == NULL) {
            if (virAsprintf(&def->target.path, "%s/%s",
                            pool->def->target.path,
                            def->name) == -1) {
                virReportOOMError();
                goto error;
            }
721 722 723
        }

        if (def->key == NULL) {
724 725 726 727 728
            def->key = strdup(def->target.path);
            if (def->key == NULL) {
                virReportOOMError();
                goto error;
            }
729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745
        }

        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;
}

746
static int testOpenFromFile(virConnectPtr conn,
747
                            const char *file) {
748
    int i, ret;
749 750
    long l;
    char *str;
751
    xmlDocPtr xml = NULL;
752 753
    xmlNodePtr *domains = NULL, *networks = NULL, *ifaces = NULL,
               *pools = NULL, *devs = NULL;
754 755
    xmlXPathContextPtr ctxt = NULL;
    virNodeInfoPtr nodeInfo;
756
    virNetworkObjPtr net;
L
Laine Stump 已提交
757
    virInterfaceObjPtr iface;
758
    virDomainObjPtr dom;
759 760
    testConnPtr privconn;
    if (VIR_ALLOC(privconn) < 0) {
761
        virReportOOMError();
762 763
        return VIR_DRV_OPEN_ERROR;
    }
764
    if (virMutexInit(&privconn->lock) < 0) {
765
        testError(VIR_ERR_INTERNAL_ERROR,
766 767 768 769 770
                  "%s", _("cannot initialize mutex"));
        VIR_FREE(privconn);
        return VIR_DRV_OPEN_ERROR;
    }

771
    testDriverLock(privconn);
772
    conn->privateData = privconn;
773

774 775 776
    if (virDomainObjListInit(&privconn->domains) < 0)
        goto error;

777 778
    if (!(privconn->caps = testBuildCapabilities(conn)))
        goto error;
779

780
    if (!(xml = virXMLParseFileCtxt(file, &ctxt))) {
781
        goto error;
782 783
    }

784
    if (!xmlStrEqual(ctxt->node->name, BAD_CAST "node")) {
785
        testError(VIR_ERR_XML_ERROR, "%s",
786
                  _("Root element is not 'node'"));
787
        goto error;
788 789
    }

790
    privconn->nextDomID = 1;
791
    privconn->numCells = 0;
E
Eric Blake 已提交
792 793
    if ((privconn->path = strdup(file)) == NULL) {
        virReportOOMError();
C
Chris Lalancette 已提交
794 795
        goto error;
    }
796 797 798
    memmove(&privconn->nodeInfo, &defaultNodeInfo, sizeof(defaultNodeInfo));

    nodeInfo = &privconn->nodeInfo;
799
    ret = virXPathLong("string(/node/cpu/nodes[1])", ctxt, &l);
800 801 802
    if (ret == 0) {
        nodeInfo->nodes = l;
    } else if (ret == -2) {
803 804
        testError(VIR_ERR_XML_ERROR, "%s",
                  _("invalid node cpu nodes value"));
805
        goto error;
806
    }
807

808
    ret = virXPathLong("string(/node/cpu/sockets[1])", ctxt, &l);
809 810 811
    if (ret == 0) {
        nodeInfo->sockets = l;
    } else if (ret == -2) {
812 813
        testError(VIR_ERR_XML_ERROR, "%s",
                  _("invalid node cpu sockets value"));
814
        goto error;
815
    }
816

817
    ret = virXPathLong("string(/node/cpu/cores[1])", ctxt, &l);
818 819 820
    if (ret == 0) {
        nodeInfo->cores = l;
    } else if (ret == -2) {
821 822
        testError(VIR_ERR_XML_ERROR, "%s",
                  _("invalid node cpu cores value"));
823
        goto error;
824 825
    }

826
    ret = virXPathLong("string(/node/cpu/threads[1])", ctxt, &l);
827 828 829
    if (ret == 0) {
        nodeInfo->threads = l;
    } else if (ret == -2) {
830 831
        testError(VIR_ERR_XML_ERROR, "%s",
                  _("invalid node cpu threads value"));
832
        goto error;
833
    }
834

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

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

866
    ret = virXPathLong("string(/node/memory[1])", ctxt, &l);
867 868 869
    if (ret == 0) {
        nodeInfo->memory = l;
    } else if (ret == -2) {
870 871
        testError(VIR_ERR_XML_ERROR, "%s",
                  _("invalid node memory value"));
872
        goto error;
873
    }
874

875
    ret = virXPathNodeSet("/node/domain", ctxt, &domains);
876
    if (ret < 0) {
877
        goto error;
878
    }
879

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

903
        if (testDomainGenerateIfnames(def) < 0 ||
904
            !(dom = virDomainAssignDef(privconn->caps,
905
                                       &privconn->domains, def, false))) {
906
            virDomainDefFree(def);
907 908
            goto error;
        }
909

910
        dom->persistent = 1;
J
Jiri Denemark 已提交
911
        if (testDomainStartState(conn, dom, VIR_DOMAIN_RUNNING_BOOTED) < 0) {
912 913 914 915
            virDomainObjUnlock(dom);
            goto error;
        }

916
        virDomainObjUnlock(dom);
917
    }
918
    VIR_FREE(domains);
919

920
    ret = virXPathNodeSet("/node/network", ctxt, &networks);
921 922 923 924 925 926 927 928 929
    if (ret < 0) {
        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);
930
            if (!absFile) {
931
                testError(VIR_ERR_INTERNAL_ERROR, "%s", _("resolving network filename"));
932 933
                goto error;
            }
934

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

L
Laine Stump 已提交
954
    /* Parse interface definitions */
955
    ret = virXPathNodeSet("/node/interface", ctxt, &ifaces);
L
Laine Stump 已提交
956 957 958 959 960 961 962 963 964 965
    if (ret < 0) {
        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) {
966
                testError(VIR_ERR_INTERNAL_ERROR, "%s", _("resolving interface filename"));
L
Laine Stump 已提交
967 968 969
                goto error;
            }

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

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

        iface->active = 1;
L
Laine Stump 已提交
985 986 987 988
        virInterfaceObjUnlock(iface);
    }
    VIR_FREE(ifaces);

C
Cole Robinson 已提交
989
    /* Parse Storage Pool list */
990
    ret = virXPathNodeSet("/node/pool", ctxt, &pools);
C
Cole Robinson 已提交
991 992 993 994 995 996 997 998 999 1000 1001
    if (ret < 0) {
        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) {
1002
                testError(VIR_ERR_INTERNAL_ERROR, "%s",
C
Cole Robinson 已提交
1003 1004 1005 1006
                          _("resolving pool filename"));
                goto error;
            }

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

1018
        if (!(pool = virStoragePoolObjAssignDef(&privconn->pools,
C
Cole Robinson 已提交
1019 1020 1021 1022 1023
                                                def))) {
            virStoragePoolDefFree(def);
            goto error;
        }

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

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

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

1040
    ret = virXPathNodeSet("/node/device", ctxt, &devs);
1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053
    if (ret < 0) {
        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) {
1054
                testError(VIR_ERR_INTERNAL_ERROR, "%s",
1055 1056 1057 1058
                          _("resolving device filename"));
                goto error;
            }

1059
            def = virNodeDeviceDefParseFile(absFile, 0);
1060 1061 1062 1063
            VIR_FREE(absFile);
            if (!def)
                goto error;
        } else {
1064
            if ((def = virNodeDeviceDefParseNode(xml, devs[i], 0)) == NULL)
1065 1066
                goto error;
        }
1067
        if (!(dev = virNodeDeviceAssignDef(&privconn->devs, def))) {
1068 1069 1070 1071 1072 1073 1074 1075
            virNodeDeviceDefFree(def);
            goto error;
        }
        virNodeDeviceObjUnlock(dev);
    }
    VIR_FREE(devs);


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

1080
    return (0);
1081 1082

 error:
J
Jim Meyering 已提交
1083
    xmlXPathFreeContext(ctxt);
1084
    xmlFreeDoc(xml);
1085 1086
    VIR_FREE(domains);
    VIR_FREE(networks);
L
Laine Stump 已提交
1087
    VIR_FREE(ifaces);
C
Cole Robinson 已提交
1088
    VIR_FREE(pools);
1089
    virDomainObjListDeinit(&privconn->domains);
1090
    virNetworkObjListFree(&privconn->networks);
L
Laine Stump 已提交
1091
    virInterfaceObjListFree(&privconn->ifaces);
C
Cole Robinson 已提交
1092
    virStoragePoolObjListFree(&privconn->pools);
E
Eric Blake 已提交
1093
    VIR_FREE(privconn->path);
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,
E
Eric Blake 已提交
1103
                                 unsigned int flags)
1104
{
1105
    int ret;
1106
    testConnPtr privconn;
1107

E
Eric Blake 已提交
1108 1109
    virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);

1110
    if (!conn->uri)
1111
        return VIR_DRV_OPEN_DECLINED;
1112

1113
    if (!conn->uri->scheme || STRNEQ(conn->uri->scheme, "test"))
1114
        return VIR_DRV_OPEN_DECLINED;
1115

1116
    /* Remote driver should handle these. */
1117
    if (conn->uri->server)
1118 1119
        return VIR_DRV_OPEN_DECLINED;

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

1129
    if (STREQ(conn->uri->path, "/default"))
1130 1131
        ret = testOpenDefault(conn);
    else
1132
        ret = testOpenFromFile(conn,
1133
                               conn->uri->path);
1134

1135 1136 1137 1138 1139
    if (ret != VIR_DRV_OPEN_SUCCESS)
        return ret;

    privconn = conn->privateData;
    testDriverLock(privconn);
1140

1141 1142 1143 1144 1145
    privconn->domainEventState = virDomainEventStateNew(testDomainEventFlush,
                                                        privconn,
                                                        NULL,
                                                        false);
    if (!privconn->domainEventState) {
1146
        testDriverUnlock(privconn);
1147 1148
        testClose(conn);
        return VIR_DRV_OPEN_ERROR;
1149 1150
    }

1151 1152 1153
    testDriverUnlock(privconn);

    return VIR_DRV_OPEN_SUCCESS;
1154 1155
}

1156
static int testClose(virConnectPtr conn)
1157
{
1158
    testConnPtr privconn = conn->privateData;
1159
    testDriverLock(privconn);
1160
    virCapabilitiesFree(privconn->caps);
1161
    virDomainObjListDeinit(&privconn->domains);
D
Daniel P. Berrange 已提交
1162
    virNodeDeviceObjListFree(&privconn->devs);
1163
    virNetworkObjListFree(&privconn->networks);
L
Laine Stump 已提交
1164
    virInterfaceObjListFree(&privconn->ifaces);
C
Cole Robinson 已提交
1165
    virStoragePoolObjListFree(&privconn->pools);
1166
    virDomainEventStateFree(privconn->domainEventState);
E
Eric Blake 已提交
1167
    VIR_FREE(privconn->path);
1168

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

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

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

1184 1185 1186 1187 1188 1189 1190 1191 1192 1193
static int testIsSecure(virConnectPtr conn ATTRIBUTE_UNUSED)
{
    return 1;
}

static int testIsEncrypted(virConnectPtr conn ATTRIBUTE_UNUSED)
{
    return 0;
}

1194 1195 1196 1197 1198 1199 1200 1201
static int testGetMaxVCPUs(virConnectPtr conn ATTRIBUTE_UNUSED,
                           const char *type ATTRIBUTE_UNUSED)
{
    return 32;
}

static int testNodeGetInfo(virConnectPtr conn,
                           virNodeInfoPtr info)
1202
{
1203
    testConnPtr privconn = conn->privateData;
1204
    testDriverLock(privconn);
1205
    memcpy(info, &privconn->nodeInfo, sizeof(virNodeInfo));
1206
    testDriverUnlock(privconn);
1207
    return (0);
1208 1209
}

1210
static char *testGetCapabilities (virConnectPtr conn)
1211
{
1212
    testConnPtr privconn = conn->privateData;
1213
    char *xml;
1214
    testDriverLock(privconn);
1215
    if ((xml = virCapabilitiesFormatXML(privconn->caps)) == NULL)
1216
        virReportOOMError();
1217
    testDriverUnlock(privconn);
1218
    return xml;
1219 1220
}

1221
static int testNumOfDomains(virConnectPtr conn)
1222
{
1223
    testConnPtr privconn = conn->privateData;
1224
    int count;
1225

1226
    testDriverLock(privconn);
1227
    count = virDomainObjListNumOfDomains(&privconn->domains, 1);
1228
    testDriverUnlock(privconn);
1229

1230
    return count;
1231 1232
}

1233 1234 1235 1236 1237 1238 1239 1240 1241 1242
static int testDomainIsActive(virDomainPtr dom)
{
    testConnPtr privconn = dom->conn->privateData;
    virDomainObjPtr obj;
    int ret = -1;

    testDriverLock(privconn);
    obj = virDomainFindByUUID(&privconn->domains, dom->uuid);
    testDriverUnlock(privconn);
    if (!obj) {
1243
        testError(VIR_ERR_NO_DOMAIN, NULL);
1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263
        goto cleanup;
    }
    ret = virDomainObjIsActive(obj);

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

static int testDomainIsPersistent(virDomainPtr dom)
{
    testConnPtr privconn = dom->conn->privateData;
    virDomainObjPtr obj;
    int ret = -1;

    testDriverLock(privconn);
    obj = virDomainFindByUUID(&privconn->domains, dom->uuid);
    testDriverUnlock(privconn);
    if (!obj) {
1264
        testError(VIR_ERR_NO_DOMAIN, NULL);
1265 1266 1267 1268 1269 1270 1271 1272 1273 1274
        goto cleanup;
    }
    ret = obj->persistent;

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

1275 1276 1277 1278 1279
static int testDomainIsUpdated(virDomainPtr dom ATTRIBUTE_UNUSED)
{
    return 0;
}

1280
static virDomainPtr
1281
testDomainCreateXML(virConnectPtr conn, const char *xml,
1282
                      unsigned int flags)
1283
{
1284
    testConnPtr privconn = conn->privateData;
1285
    virDomainPtr ret = NULL;
1286
    virDomainDefPtr def;
1287
    virDomainObjPtr dom = NULL;
1288
    virDomainEventPtr event = NULL;
1289

1290 1291
    virCheckFlags(0, NULL);

1292
    testDriverLock(privconn);
1293
    if ((def = virDomainDefParseString(privconn->caps, xml,
M
Matthias Bolte 已提交
1294
                                       1 << VIR_DOMAIN_VIRT_TEST,
1295
                                       VIR_DOMAIN_XML_INACTIVE)) == NULL)
1296
        goto cleanup;
1297

1298 1299 1300
    if (virDomainObjIsDuplicate(&privconn->domains, def, 1) < 0)
        goto cleanup;

1301
    if (testDomainGenerateIfnames(def) < 0)
1302
        goto cleanup;
1303
    if (!(dom = virDomainAssignDef(privconn->caps,
1304
                                   &privconn->domains, def, false)))
1305 1306
        goto cleanup;
    def = NULL;
1307

J
Jiri Denemark 已提交
1308
    if (testDomainStartState(conn, dom, VIR_DOMAIN_RUNNING_BOOTED) < 0)
1309
        goto cleanup;
1310

1311 1312 1313 1314
    event = virDomainEventNewFromObj(dom,
                                     VIR_DOMAIN_EVENT_STARTED,
                                     VIR_DOMAIN_EVENT_STARTED_BOOTED);

1315
    ret = virGetDomain(conn, dom->def->name, dom->def->uuid);
1316
    if (ret)
1317
        ret->id = dom->def->id;
1318 1319

cleanup:
1320 1321
    if (dom)
        virDomainObjUnlock(dom);
1322 1323
    if (event)
        testDomainEventQueue(privconn, event);
1324
    virDomainDefFree(def);
1325
    testDriverUnlock(privconn);
1326
    return ret;
1327 1328 1329
}


1330 1331
static virDomainPtr testLookupDomainByID(virConnectPtr conn,
                                         int id)
1332
{
1333
    testConnPtr privconn = conn->privateData;
1334 1335
    virDomainPtr ret = NULL;
    virDomainObjPtr dom;
1336

1337 1338 1339 1340 1341
    testDriverLock(privconn);
    dom = virDomainFindByID(&privconn->domains, id);
    testDriverUnlock(privconn);

    if (dom == NULL) {
1342
        testError(VIR_ERR_NO_DOMAIN, NULL);
1343
        goto cleanup;
1344 1345
    }

1346
    ret = virGetDomain(conn, dom->def->name, dom->def->uuid);
1347 1348 1349 1350
    if (ret)
        ret->id = dom->def->id;

cleanup:
1351 1352
    if (dom)
        virDomainObjUnlock(dom);
1353
    return ret;
1354 1355
}

1356 1357
static virDomainPtr testLookupDomainByUUID(virConnectPtr conn,
                                           const unsigned char *uuid)
1358
{
1359
    testConnPtr privconn = conn->privateData;
1360 1361
    virDomainPtr ret = NULL;
    virDomainObjPtr dom ;
1362

1363 1364 1365 1366 1367
    testDriverLock(privconn);
    dom = virDomainFindByUUID(&privconn->domains, uuid);
    testDriverUnlock(privconn);

    if (dom == NULL) {
1368
        testError(VIR_ERR_NO_DOMAIN, NULL);
1369
        goto cleanup;
1370
    }
1371

1372
    ret = virGetDomain(conn, dom->def->name, dom->def->uuid);
1373 1374 1375 1376
    if (ret)
        ret->id = dom->def->id;

cleanup:
1377 1378
    if (dom)
        virDomainObjUnlock(dom);
1379
    return ret;
1380 1381
}

1382 1383
static virDomainPtr testLookupDomainByName(virConnectPtr conn,
                                           const char *name)
1384
{
1385
    testConnPtr privconn = conn->privateData;
1386 1387
    virDomainPtr ret = NULL;
    virDomainObjPtr dom;
1388

1389 1390 1391 1392 1393
    testDriverLock(privconn);
    dom = virDomainFindByName(&privconn->domains, name);
    testDriverUnlock(privconn);

    if (dom == NULL) {
1394
        testError(VIR_ERR_NO_DOMAIN, NULL);
1395
        goto cleanup;
1396
    }
1397

1398
    ret = virGetDomain(conn, dom->def->name, dom->def->uuid);
1399 1400 1401 1402
    if (ret)
        ret->id = dom->def->id;

cleanup:
1403 1404
    if (dom)
        virDomainObjUnlock(dom);
1405
    return ret;
1406 1407
}

1408 1409 1410
static int testListDomains (virConnectPtr conn,
                            int *ids,
                            int maxids)
1411
{
1412
    testConnPtr privconn = conn->privateData;
1413
    int n;
1414

1415
    testDriverLock(privconn);
1416
    n = virDomainObjListGetActiveIDs(&privconn->domains, ids, maxids);
1417
    testDriverUnlock(privconn);
1418

1419
    return n;
1420 1421
}

1422
static int testDestroyDomain (virDomainPtr domain)
1423
{
1424 1425
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
1426
    virDomainEventPtr event = NULL;
1427
    int ret = -1;
1428

1429
    testDriverLock(privconn);
1430 1431 1432 1433
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);

    if (privdom == NULL) {
1434
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
1435
        goto cleanup;
1436
    }
1437

J
Jiri Denemark 已提交
1438
    testDomainShutdownState(domain, privdom, VIR_DOMAIN_SHUTOFF_DESTROYED);
1439 1440 1441
    event = virDomainEventNewFromObj(privdom,
                                     VIR_DOMAIN_EVENT_STOPPED,
                                     VIR_DOMAIN_EVENT_STOPPED_DESTROYED);
1442

1443 1444 1445
    if (!privdom->persistent) {
        virDomainRemoveInactive(&privconn->domains,
                                privdom);
1446
        privdom = NULL;
1447
    }
1448 1449 1450

    ret = 0;
cleanup:
1451 1452
    if (privdom)
        virDomainObjUnlock(privdom);
1453 1454
    if (event)
        testDomainEventQueue(privconn, event);
1455
    testDriverUnlock(privconn);
1456
    return ret;
1457 1458
}

1459
static int testResumeDomain (virDomainPtr domain)
1460
{
1461 1462
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
1463
    virDomainEventPtr event = NULL;
1464
    int ret = -1;
1465

1466
    testDriverLock(privconn);
1467 1468
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
1469
    testDriverUnlock(privconn);
1470 1471

    if (privdom == NULL) {
1472
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
1473
        goto cleanup;
1474
    }
1475

J
Jiri Denemark 已提交
1476
    if (virDomainObjGetState(privdom, NULL) != VIR_DOMAIN_PAUSED) {
1477
        testError(VIR_ERR_INTERNAL_ERROR, _("domain '%s' not paused"),
1478
                  domain->name);
1479
        goto cleanup;
1480
    }
1481

J
Jiri Denemark 已提交
1482 1483
    virDomainObjSetState(privdom, VIR_DOMAIN_RUNNING,
                         VIR_DOMAIN_RUNNING_UNPAUSED);
1484 1485 1486
    event = virDomainEventNewFromObj(privdom,
                                     VIR_DOMAIN_EVENT_RESUMED,
                                     VIR_DOMAIN_EVENT_RESUMED_UNPAUSED);
1487 1488 1489
    ret = 0;

cleanup:
1490 1491
    if (privdom)
        virDomainObjUnlock(privdom);
1492 1493 1494 1495 1496
    if (event) {
        testDriverLock(privconn);
        testDomainEventQueue(privconn, event);
        testDriverUnlock(privconn);
    }
1497
    return ret;
1498 1499
}

1500
static int testPauseDomain (virDomainPtr domain)
1501
{
1502 1503
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
1504
    virDomainEventPtr event = NULL;
1505
    int ret = -1;
J
Jiri Denemark 已提交
1506
    int state;
1507

1508
    testDriverLock(privconn);
1509 1510
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
1511
    testDriverUnlock(privconn);
1512 1513

    if (privdom == NULL) {
1514
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
1515
        goto cleanup;
1516
    }
1517

J
Jiri Denemark 已提交
1518 1519
    state = virDomainObjGetState(privdom, NULL);
    if (state == VIR_DOMAIN_SHUTOFF || state == VIR_DOMAIN_PAUSED) {
1520
        testError(VIR_ERR_INTERNAL_ERROR, _("domain '%s' not running"),
1521
                  domain->name);
1522
        goto cleanup;
1523
    }
1524

J
Jiri Denemark 已提交
1525
    virDomainObjSetState(privdom, VIR_DOMAIN_PAUSED, VIR_DOMAIN_PAUSED_USER);
1526 1527 1528
    event = virDomainEventNewFromObj(privdom,
                                     VIR_DOMAIN_EVENT_SUSPENDED,
                                     VIR_DOMAIN_EVENT_SUSPENDED_PAUSED);
1529 1530 1531
    ret = 0;

cleanup:
1532 1533
    if (privdom)
        virDomainObjUnlock(privdom);
1534 1535 1536 1537 1538 1539

    if (event) {
        testDriverLock(privconn);
        testDomainEventQueue(privconn, event);
        testDriverUnlock(privconn);
    }
1540
    return ret;
1541 1542
}

1543
static int testShutdownDomain (virDomainPtr domain)
1544
{
1545 1546
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
1547
    virDomainEventPtr event = NULL;
1548
    int ret = -1;
1549

1550
    testDriverLock(privconn);
1551 1552 1553 1554
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);

    if (privdom == NULL) {
1555
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
1556
        goto cleanup;
1557
    }
1558

J
Jiri Denemark 已提交
1559
    if (virDomainObjGetState(privdom, NULL) == VIR_DOMAIN_SHUTOFF) {
1560
        testError(VIR_ERR_INTERNAL_ERROR,
1561
                  _("domain '%s' not running"), domain->name);
1562
        goto cleanup;
1563
    }
1564

J
Jiri Denemark 已提交
1565
    testDomainShutdownState(domain, privdom, VIR_DOMAIN_SHUTOFF_SHUTDOWN);
1566 1567 1568
    event = virDomainEventNewFromObj(privdom,
                                     VIR_DOMAIN_EVENT_STOPPED,
                                     VIR_DOMAIN_EVENT_STOPPED_SHUTDOWN);
1569

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

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

/* Similar behaviour as shutdown */
1587 1588
static int testRebootDomain (virDomainPtr domain,
                             unsigned int action ATTRIBUTE_UNUSED)
1589
{
1590 1591
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
1592
    virDomainEventPtr event = NULL;
1593
    int ret = -1;
1594

1595
    testDriverLock(privconn);
1596 1597 1598 1599
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);

    if (privdom == NULL) {
1600
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
1601
        goto cleanup;
1602
    }
1603

J
Jiri Denemark 已提交
1604 1605 1606
    virDomainObjSetState(privdom, VIR_DOMAIN_SHUTDOWN,
                         VIR_DOMAIN_SHUTDOWN_USER);

1607 1608
    switch (privdom->def->onReboot) {
    case VIR_DOMAIN_LIFECYCLE_DESTROY:
J
Jiri Denemark 已提交
1609 1610
        virDomainObjSetState(privdom, VIR_DOMAIN_SHUTOFF,
                             VIR_DOMAIN_SHUTOFF_SHUTDOWN);
1611 1612
        break;

1613
    case VIR_DOMAIN_LIFECYCLE_RESTART:
J
Jiri Denemark 已提交
1614 1615
        virDomainObjSetState(privdom, VIR_DOMAIN_RUNNING,
                             VIR_DOMAIN_RUNNING_BOOTED);
1616 1617
        break;

1618
    case VIR_DOMAIN_LIFECYCLE_PRESERVE:
J
Jiri Denemark 已提交
1619 1620
        virDomainObjSetState(privdom, VIR_DOMAIN_SHUTOFF,
                             VIR_DOMAIN_SHUTOFF_SHUTDOWN);
1621 1622
        break;

1623
    case VIR_DOMAIN_LIFECYCLE_RESTART_RENAME:
J
Jiri Denemark 已提交
1624 1625
        virDomainObjSetState(privdom, VIR_DOMAIN_RUNNING,
                             VIR_DOMAIN_RUNNING_BOOTED);
1626
        break;
1627

1628
    default:
J
Jiri Denemark 已提交
1629 1630
        virDomainObjSetState(privdom, VIR_DOMAIN_SHUTOFF,
                             VIR_DOMAIN_SHUTOFF_SHUTDOWN);
1631 1632
        break;
    }
1633

J
Jiri Denemark 已提交
1634 1635
    if (virDomainObjGetState(privdom, NULL) == VIR_DOMAIN_SHUTOFF) {
        testDomainShutdownState(domain, privdom, VIR_DOMAIN_SHUTOFF_SHUTDOWN);
1636 1637 1638
        event = virDomainEventNewFromObj(privdom,
                                         VIR_DOMAIN_EVENT_STOPPED,
                                         VIR_DOMAIN_EVENT_STOPPED_SHUTDOWN);
1639

1640 1641 1642 1643 1644
        if (!privdom->persistent) {
            virDomainRemoveInactive(&privconn->domains,
                                    privdom);
            privdom = NULL;
        }
1645 1646
    }

1647 1648
    ret = 0;
cleanup:
1649 1650
    if (privdom)
        virDomainObjUnlock(privdom);
1651 1652
    if (event)
        testDomainEventQueue(privconn, event);
1653
    testDriverUnlock(privconn);
1654
    return ret;
1655 1656
}

1657 1658
static int testGetDomainInfo (virDomainPtr domain,
                              virDomainInfoPtr info)
1659
{
1660
    testConnPtr privconn = domain->conn->privateData;
1661
    struct timeval tv;
1662
    virDomainObjPtr privdom;
1663
    int ret = -1;
1664

1665
    testDriverLock(privconn);
1666 1667
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
1668
    testDriverUnlock(privconn);
1669 1670

    if (privdom == NULL) {
1671
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
1672
        goto cleanup;
1673
    }
1674 1675

    if (gettimeofday(&tv, NULL) < 0) {
1676
        testError(VIR_ERR_INTERNAL_ERROR,
J
Jim Meyering 已提交
1677
                  "%s", _("getting time of day"));
1678
        goto cleanup;
1679 1680
    }

J
Jiri Denemark 已提交
1681
    info->state = virDomainObjGetState(privdom, NULL);
1682 1683
    info->memory = privdom->def->mem.cur_balloon;
    info->maxMem = privdom->def->mem.max_balloon;
1684 1685
    info->nrVirtCpu = privdom->def->vcpus;
    info->cpuTime = ((tv.tv_sec * 1000ll * 1000ll  * 1000ll) + (tv.tv_usec * 1000ll));
1686 1687 1688
    ret = 0;

cleanup:
1689 1690
    if (privdom)
        virDomainObjUnlock(privdom);
1691
    return ret;
1692 1693
}

1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715
static int
testDomainGetState(virDomainPtr domain,
                   int *state,
                   int *reason,
                   unsigned int flags)
{
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
    int ret = -1;

    virCheckFlags(0, -1);

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

    if (privdom == NULL) {
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
        goto cleanup;
    }

J
Jiri Denemark 已提交
1716
    *state = virDomainObjGetState(privdom, reason);
1717 1718 1719 1720 1721 1722 1723 1724
    ret = 0;

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

1725 1726
#define TEST_SAVE_MAGIC "TestGuestMagic"

1727 1728 1729
static int
testDomainSaveFlags(virDomainPtr domain, const char *path,
                    const char *dxml, unsigned int flags)
1730
{
1731
    testConnPtr privconn = domain->conn->privateData;
1732 1733 1734
    char *xml = NULL;
    int fd = -1;
    int len;
1735
    virDomainObjPtr privdom;
1736
    virDomainEventPtr event = NULL;
1737
    int ret = -1;
1738

1739 1740 1741 1742 1743 1744 1745
    virCheckFlags(0, -1);
    if (dxml) {
        testError(VIR_ERR_ARGUMENT_UNSUPPORTED, "%s",
                  _("xml modification unsupported"));
        return -1;
    }

1746
    testDriverLock(privconn);
1747 1748 1749 1750
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);

    if (privdom == NULL) {
1751
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
1752
        goto cleanup;
1753
    }
1754

1755
    xml = virDomainDefFormat(privdom->def,
C
Cole Robinson 已提交
1756 1757
                             VIR_DOMAIN_XML_SECURE);

1758
    if (xml == NULL) {
1759
        virReportSystemError(errno,
1760 1761
                             _("saving domain '%s' failed to allocate space for metadata"),
                             domain->name);
1762
        goto cleanup;
1763
    }
1764 1765

    if ((fd = open(path, O_CREAT|O_TRUNC|O_WRONLY, S_IRUSR|S_IWUSR)) < 0) {
1766
        virReportSystemError(errno,
1767 1768
                             _("saving domain '%s' to '%s': open failed"),
                             domain->name, path);
1769
        goto cleanup;
1770
    }
1771
    len = strlen(xml);
1772
    if (safewrite(fd, TEST_SAVE_MAGIC, sizeof(TEST_SAVE_MAGIC)) < 0) {
1773
        virReportSystemError(errno,
1774 1775
                             _("saving domain '%s' to '%s': write failed"),
                             domain->name, path);
1776
        goto cleanup;
1777
    }
1778
    if (safewrite(fd, (char*)&len, sizeof(len)) < 0) {
1779
        virReportSystemError(errno,
1780 1781
                             _("saving domain '%s' to '%s': write failed"),
                             domain->name, path);
1782
        goto cleanup;
1783
    }
1784
    if (safewrite(fd, xml, len) < 0) {
1785
        virReportSystemError(errno,
1786 1787
                             _("saving domain '%s' to '%s': write failed"),
                             domain->name, path);
1788
        goto cleanup;
1789
    }
1790

1791
    if (VIR_CLOSE(fd) < 0) {
1792
        virReportSystemError(errno,
1793 1794
                             _("saving domain '%s' to '%s': write failed"),
                             domain->name, path);
1795
        goto cleanup;
1796
    }
1797 1798
    fd = -1;

J
Jiri Denemark 已提交
1799
    testDomainShutdownState(domain, privdom, VIR_DOMAIN_SHUTOFF_SAVED);
1800 1801 1802
    event = virDomainEventNewFromObj(privdom,
                                     VIR_DOMAIN_EVENT_STOPPED,
                                     VIR_DOMAIN_EVENT_STOPPED_SAVED);
1803

1804 1805 1806
    if (!privdom->persistent) {
        virDomainRemoveInactive(&privconn->domains,
                                privdom);
1807
        privdom = NULL;
1808
    }
1809

1810
    ret = 0;
1811 1812 1813 1814 1815 1816 1817
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) {
1818
        VIR_FORCE_CLOSE(fd);
1819 1820
        unlink(path);
    }
1821 1822
    if (privdom)
        virDomainObjUnlock(privdom);
1823 1824
    if (event)
        testDomainEventQueue(privconn, event);
1825
    testDriverUnlock(privconn);
1826
    return ret;
1827 1828
}

1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840
static int
testDomainSave(virDomainPtr domain,
               const char *path)
{
    return testDomainSaveFlags(domain, path, NULL, 0);
}

static int
testDomainRestoreFlags(virConnectPtr conn,
                       const char *path,
                       const char *dxml,
                       unsigned int flags)
1841
{
1842
    testConnPtr privconn = conn->privateData;
1843
    char *xml = NULL;
1844
    char magic[15];
1845 1846 1847
    int fd = -1;
    int len;
    virDomainDefPtr def = NULL;
1848
    virDomainObjPtr dom = NULL;
1849
    virDomainEventPtr event = NULL;
1850
    int ret = -1;
1851

1852 1853 1854 1855 1856 1857 1858
    virCheckFlags(0, -1);
    if (dxml) {
        testError(VIR_ERR_ARGUMENT_UNSUPPORTED, "%s",
                  _("xml modification unsupported"));
        return -1;
    }

1859
    if ((fd = open(path, O_RDONLY)) < 0) {
1860
        virReportSystemError(errno,
1861 1862
                             _("cannot read domain image '%s'"),
                             path);
1863
        goto cleanup;
1864
    }
1865
    if (saferead(fd, magic, sizeof(magic)) != sizeof(magic)) {
1866
        virReportSystemError(errno,
1867 1868
                             _("incomplete save header in '%s'"),
                             path);
1869
        goto cleanup;
1870
    }
1871
    if (memcmp(magic, TEST_SAVE_MAGIC, sizeof(magic))) {
1872
        testError(VIR_ERR_INTERNAL_ERROR,
J
Jim Meyering 已提交
1873
                  "%s", _("mismatched header magic"));
1874
        goto cleanup;
1875
    }
1876
    if (saferead(fd, (char*)&len, sizeof(len)) != sizeof(len)) {
1877
        virReportSystemError(errno,
1878 1879
                             _("failed to read metadata length in '%s'"),
                             path);
1880
        goto cleanup;
1881 1882
    }
    if (len < 1 || len > 8192) {
1883
        testError(VIR_ERR_INTERNAL_ERROR,
J
Jim Meyering 已提交
1884
                  "%s", _("length of metadata out of range"));
1885
        goto cleanup;
1886
    }
1887
    if (VIR_ALLOC_N(xml, len+1) < 0) {
1888
        virReportOOMError();
1889
        goto cleanup;
1890
    }
1891
    if (saferead(fd, xml, len) != len) {
1892
        virReportSystemError(errno,
1893
                             _("incomplete metdata in '%s'"), path);
1894
        goto cleanup;
1895 1896
    }
    xml[len] = '\0';
1897

1898
    testDriverLock(privconn);
1899
    def = virDomainDefParseString(privconn->caps, xml,
M
Matthias Bolte 已提交
1900
                                  1 << VIR_DOMAIN_VIRT_TEST,
1901
                                  VIR_DOMAIN_XML_INACTIVE);
1902
    if (!def)
1903
        goto cleanup;
1904

1905 1906 1907
    if (virDomainObjIsDuplicate(&privconn->domains, def, 1) < 0)
        goto cleanup;

1908
    if (testDomainGenerateIfnames(def) < 0)
1909
        goto cleanup;
1910
    if (!(dom = virDomainAssignDef(privconn->caps,
1911
                                   &privconn->domains, def, true)))
1912 1913
        goto cleanup;
    def = NULL;
1914

J
Jiri Denemark 已提交
1915
    if (testDomainStartState(conn, dom, VIR_DOMAIN_RUNNING_RESTORED) < 0)
1916 1917
        goto cleanup;

1918 1919 1920
    event = virDomainEventNewFromObj(dom,
                                     VIR_DOMAIN_EVENT_STARTED,
                                     VIR_DOMAIN_EVENT_STARTED_RESTORED);
1921
    ret = 0;
1922 1923 1924 1925

cleanup:
    virDomainDefFree(def);
    VIR_FREE(xml);
1926
    VIR_FORCE_CLOSE(fd);
1927 1928
    if (dom)
        virDomainObjUnlock(dom);
1929 1930
    if (event)
        testDomainEventQueue(privconn, event);
1931
    testDriverUnlock(privconn);
1932
    return ret;
1933 1934
}

1935 1936 1937 1938 1939 1940 1941
static int
testDomainRestore(virConnectPtr conn,
                  const char *path)
{
    return testDomainRestoreFlags(conn, path, NULL, 0);
}

1942 1943
static int testDomainCoreDump(virDomainPtr domain,
                              const char *to,
E
Eric Blake 已提交
1944
                              unsigned int flags)
1945
{
1946
    testConnPtr privconn = domain->conn->privateData;
1947
    int fd = -1;
1948
    virDomainObjPtr privdom;
1949
    virDomainEventPtr event = NULL;
1950
    int ret = -1;
1951

E
Eric Blake 已提交
1952 1953
    virCheckFlags(VIR_DUMP_CRASH, -1);

1954
    testDriverLock(privconn);
1955 1956 1957 1958
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);

    if (privdom == NULL) {
1959
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
1960
        goto cleanup;
1961
    }
1962 1963

    if ((fd = open(to, O_CREAT|O_TRUNC|O_WRONLY, S_IRUSR|S_IWUSR)) < 0) {
1964
        virReportSystemError(errno,
1965 1966
                             _("domain '%s' coredump: failed to open %s"),
                             domain->name, to);
1967
        goto cleanup;
1968
    }
1969
    if (safewrite(fd, TEST_SAVE_MAGIC, sizeof(TEST_SAVE_MAGIC)) < 0) {
1970
        virReportSystemError(errno,
1971 1972
                             _("domain '%s' coredump: failed to write header to %s"),
                             domain->name, to);
1973
        goto cleanup;
1974
    }
1975
    if (VIR_CLOSE(fd) < 0) {
1976
        virReportSystemError(errno,
1977 1978
                             _("domain '%s' coredump: write failed: %s"),
                             domain->name, to);
1979
        goto cleanup;
1980
    }
1981

1982
    if (flags & VIR_DUMP_CRASH) {
J
Jiri Denemark 已提交
1983
        testDomainShutdownState(domain, privdom, VIR_DOMAIN_SHUTOFF_CRASHED);
1984 1985 1986 1987 1988 1989 1990 1991
        event = virDomainEventNewFromObj(privdom,
                                         VIR_DOMAIN_EVENT_STOPPED,
                                         VIR_DOMAIN_EVENT_STOPPED_CRASHED);
        if (!privdom->persistent) {
            virDomainRemoveInactive(&privconn->domains,
                                    privdom);
            privdom = NULL;
        }
1992
    }
1993

1994
    ret = 0;
1995
cleanup:
1996
    VIR_FORCE_CLOSE(fd);
1997 1998
    if (privdom)
        virDomainObjUnlock(privdom);
1999 2000
    if (event)
        testDomainEventQueue(privconn, event);
2001
    testDriverUnlock(privconn);
2002
    return ret;
2003 2004
}

2005
static char *testGetOSType(virDomainPtr dom ATTRIBUTE_UNUSED) {
2006 2007
    char *ret = strdup("linux");
    if (!ret)
2008
        virReportOOMError();
2009
    return ret;
2010 2011 2012
}

static unsigned long testGetMaxMemory(virDomainPtr domain) {
2013 2014
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
2015
    unsigned long ret = 0;
2016

2017
    testDriverLock(privconn);
2018 2019
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
2020
    testDriverUnlock(privconn);
2021 2022

    if (privdom == NULL) {
2023
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2024
        goto cleanup;
2025
    }
2026

2027
    ret = privdom->def->mem.max_balloon;
2028 2029

cleanup:
2030 2031
    if (privdom)
        virDomainObjUnlock(privdom);
2032
    return ret;
2033 2034 2035 2036 2037
}

static int testSetMaxMemory(virDomainPtr domain,
                            unsigned long memory)
{
2038 2039
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
2040
    int ret = -1;
2041

2042
    testDriverLock(privconn);
2043 2044
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
2045
    testDriverUnlock(privconn);
2046 2047

    if (privdom == NULL) {
2048
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2049
        goto cleanup;
2050
    }
2051 2052

    /* XXX validate not over host memory wrt to other domains */
2053
    privdom->def->mem.max_balloon = memory;
2054 2055 2056
    ret = 0;

cleanup:
2057 2058
    if (privdom)
        virDomainObjUnlock(privdom);
2059
    return ret;
2060 2061
}

2062 2063 2064
static int testSetMemory(virDomainPtr domain,
                         unsigned long memory)
{
2065 2066
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
2067
    int ret = -1;
2068

2069
    testDriverLock(privconn);
2070 2071
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
2072
    testDriverUnlock(privconn);
2073 2074

    if (privdom == NULL) {
2075
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2076
        goto cleanup;
2077
    }
2078

2079
    if (memory > privdom->def->mem.max_balloon) {
2080
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2081
        goto cleanup;
2082
    }
2083

2084
    privdom->def->mem.cur_balloon = memory;
2085 2086 2087
    ret = 0;

cleanup:
2088 2089
    if (privdom)
        virDomainObjUnlock(privdom);
2090
    return ret;
2091 2092
}

2093 2094
static int
testDomainGetVcpusFlags(virDomainPtr domain, unsigned int flags)
C
Cole Robinson 已提交
2095
{
2096 2097 2098 2099
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr vm;
    virDomainDefPtr def;
    int ret = -1;
2100
    bool active;
2101

2102 2103
    virCheckFlags(VIR_DOMAIN_AFFECT_LIVE |
                  VIR_DOMAIN_AFFECT_CONFIG |
2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117
                  VIR_DOMAIN_VCPU_MAXIMUM, -1);

    testDriverLock(privconn);
    vm = virDomainFindByUUID(&privconn->domains, domain->uuid);
    testDriverUnlock(privconn);

    if (!vm) {
        char uuidstr[VIR_UUID_STRING_BUFLEN];
        virUUIDFormat(domain->uuid, uuidstr);
        testError(VIR_ERR_NO_DOMAIN,
                  _("no domain with matching uuid '%s'"), uuidstr);
        goto cleanup;
    }

2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133
    active = virDomainObjIsActive(vm);

    if ((flags & (VIR_DOMAIN_VCPU_LIVE | VIR_DOMAIN_VCPU_CONFIG)) == 0) {
        if (active)
            flags |= VIR_DOMAIN_VCPU_LIVE;
        else
            flags |= VIR_DOMAIN_VCPU_CONFIG;
    }
    if ((flags & VIR_DOMAIN_AFFECT_LIVE) && (flags & VIR_DOMAIN_AFFECT_CONFIG)) {
        testError(VIR_ERR_INVALID_ARG,
                  _("invalid flag combination: (0x%x)"), flags);
        return -1;
    }



2134
    if (flags & VIR_DOMAIN_AFFECT_LIVE) {
2135
        if (!active) {
2136 2137 2138 2139 2140 2141
            testError(VIR_ERR_OPERATION_INVALID, "%s",
                      _("domain not active"));
            goto cleanup;
        }
        def = vm->def;
    } else {
2142 2143 2144 2145 2146
        if (!vm->persistent) {
            testError(VIR_ERR_OPERATION_INVALID, "%s",
                      _("domain is transient"));
            goto cleanup;
        }
2147 2148 2149 2150 2151 2152 2153 2154 2155
        def = vm->newDef ? vm->newDef : vm->def;
    }

    ret = (flags & VIR_DOMAIN_VCPU_MAXIMUM) ? def->maxvcpus : def->vcpus;

cleanup:
    if (vm)
        virDomainObjUnlock(vm);
    return ret;
C
Cole Robinson 已提交
2156 2157
}

2158 2159 2160
static int
testDomainGetMaxVcpus(virDomainPtr domain)
{
2161
    return testDomainGetVcpusFlags(domain, (VIR_DOMAIN_AFFECT_LIVE |
2162 2163 2164 2165 2166 2167 2168
                                            VIR_DOMAIN_VCPU_MAXIMUM));
}

static int
testDomainSetVcpusFlags(virDomainPtr domain, unsigned int nrCpus,
                        unsigned int flags)
{
2169
    testConnPtr privconn = domain->conn->privateData;
2170
    virDomainObjPtr privdom = NULL;
2171
    virDomainDefPtr persistentDef;
C
Cole Robinson 已提交
2172 2173
    int ret = -1, maxvcpus;

2174 2175
    virCheckFlags(VIR_DOMAIN_AFFECT_LIVE |
                  VIR_DOMAIN_AFFECT_CONFIG |
2176 2177 2178 2179
                  VIR_DOMAIN_VCPU_MAXIMUM, -1);

    /* At least one of LIVE or CONFIG must be set.  MAXIMUM cannot be
     * mixed with LIVE.  */
2180 2181 2182
    if ((flags & (VIR_DOMAIN_AFFECT_LIVE | VIR_DOMAIN_AFFECT_CONFIG)) == 0 ||
        (flags & (VIR_DOMAIN_VCPU_MAXIMUM | VIR_DOMAIN_AFFECT_LIVE)) ==
         (VIR_DOMAIN_VCPU_MAXIMUM | VIR_DOMAIN_AFFECT_LIVE)) {
2183 2184 2185 2186 2187 2188 2189
        testError(VIR_ERR_INVALID_ARG,
                  _("invalid flag combination: (0x%x)"), flags);
        return -1;
    }
    if (!nrCpus || (maxvcpus = testGetMaxVCPUs(domain->conn, NULL)) < nrCpus) {
        testError(VIR_ERR_INVALID_ARG,
                  _("argument out of range: %d"), nrCpus);
2190 2191
        return -1;
    }
2192

2193
    testDriverLock(privconn);
2194
    privdom = virDomainFindByUUID(&privconn->domains, domain->uuid);
2195 2196 2197
    testDriverUnlock(privconn);

    if (privdom == NULL) {
2198
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2199
        goto cleanup;
2200
    }
2201

2202
    if (!virDomainObjIsActive(privdom) && (flags & VIR_DOMAIN_AFFECT_LIVE)) {
2203
        testError(VIR_ERR_OPERATION_INVALID,
C
Cole Robinson 已提交
2204 2205 2206 2207
                  "%s", _("cannot hotplug vcpus for an inactive domain"));
        goto cleanup;
    }

2208 2209
    /* We allow more cpus in guest than host, but not more than the
     * domain's starting limit.  */
C
Cole Robinson 已提交
2210 2211
    if (!(flags & (VIR_DOMAIN_VCPU_MAXIMUM)) &&
        privdom->def->maxvcpus < maxvcpus)
2212
        maxvcpus = privdom->def->maxvcpus;
C
Cole Robinson 已提交
2213

C
Cole Robinson 已提交
2214
    if (nrCpus > maxvcpus) {
2215
        testError(VIR_ERR_INVALID_ARG,
C
Cole Robinson 已提交
2216 2217
                  "requested cpu amount exceeds maximum (%d > %d)",
                  nrCpus, maxvcpus);
2218
        goto cleanup;
2219
    }
2220

2221 2222 2223 2224
    if (!(persistentDef = virDomainObjGetPersistentDef(privconn->caps,
                                                       privdom)))
        goto cleanup;

2225
    switch (flags) {
2226
    case VIR_DOMAIN_VCPU_MAXIMUM | VIR_DOMAIN_AFFECT_CONFIG:
2227 2228 2229
        persistentDef->maxvcpus = nrCpus;
        if (nrCpus < persistentDef->vcpus)
            persistentDef->vcpus = nrCpus;
2230 2231
        ret = 0;
        break;
2232

2233
    case VIR_DOMAIN_AFFECT_CONFIG:
2234
        persistentDef->vcpus = nrCpus;
2235 2236 2237
        ret = 0;
        break;

2238
    case VIR_DOMAIN_AFFECT_LIVE:
2239 2240 2241
        ret = testDomainUpdateVCPUs(domain->conn, privdom, nrCpus, 0);
        break;

2242
    case VIR_DOMAIN_AFFECT_LIVE | VIR_DOMAIN_AFFECT_CONFIG:
2243
        ret = testDomainUpdateVCPUs(domain->conn, privdom, nrCpus, 0);
2244 2245 2246
        if (ret == 0) {
            persistentDef->vcpus = nrCpus;
        }
2247 2248
        break;
    }
2249 2250

cleanup:
2251 2252
    if (privdom)
        virDomainObjUnlock(privdom);
2253
    return ret;
2254 2255
}

2256 2257 2258
static int
testSetVcpus(virDomainPtr domain, unsigned int nrCpus)
{
2259
    return testDomainSetVcpusFlags(domain, nrCpus, VIR_DOMAIN_AFFECT_LIVE);
2260 2261
}

C
Cole Robinson 已提交
2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280
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) {
2281
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
C
Cole Robinson 已提交
2282 2283 2284 2285
        goto cleanup;
    }

    if (!virDomainObjIsActive(privdom)) {
2286
        testError(VIR_ERR_OPERATION_INVALID,
C
Cole Robinson 已提交
2287 2288 2289 2290 2291 2292 2293
                  "%s",_("cannot list vcpus for an inactive domain"));
        goto cleanup;
    }

    privdomdata = privdom->privateData;

    if (gettimeofday(&tv, NULL) < 0) {
2294
        virReportSystemError(errno,
C
Cole Robinson 已提交
2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349
                             "%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 已提交
2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366
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) {
2367
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
C
Cole Robinson 已提交
2368 2369 2370 2371
        goto cleanup;
    }

    if (!virDomainObjIsActive(privdom)) {
2372
        testError(VIR_ERR_OPERATION_INVALID,
C
Cole Robinson 已提交
2373 2374 2375 2376 2377
                  "%s",_("cannot pin vcpus on an inactive domain"));
        goto cleanup;
    }

    if (vcpu > privdom->def->vcpus) {
2378
        testError(VIR_ERR_INVALID_ARG, "%s",
C
Cole Robinson 已提交
2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406
                  _("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;
}

2407
static char *testDomainGetXMLDesc(virDomainPtr domain, unsigned int flags)
2408
{
2409
    testConnPtr privconn = domain->conn->privateData;
2410
    virDomainDefPtr def;
2411
    virDomainObjPtr privdom;
2412 2413
    char *ret = NULL;

2414 2415
    /* Flags checked by virDomainDefFormat */

2416 2417 2418 2419 2420 2421
    testDriverLock(privconn);
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
    testDriverUnlock(privconn);

    if (privdom == NULL) {
2422
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2423
        goto cleanup;
2424
    }
2425

2426 2427
    def = (flags & VIR_DOMAIN_XML_INACTIVE) &&
        privdom->newDef ? privdom->newDef : privdom->def;
2428

2429
    ret = virDomainDefFormat(def,
2430 2431 2432
                             flags);

cleanup:
2433 2434
    if (privdom)
        virDomainObjUnlock(privdom);
2435
    return ret;
2436
}
2437

2438
static int testNumOfDefinedDomains(virConnectPtr conn) {
2439
    testConnPtr privconn = conn->privateData;
2440
    int count;
2441

2442
    testDriverLock(privconn);
2443
    count = virDomainObjListNumOfDomains(&privconn->domains, 0);
2444
    testDriverUnlock(privconn);
2445

2446
    return count;
2447 2448
}

2449 2450 2451
static int testListDefinedDomains(virConnectPtr conn,
                                  char **const names,
                                  int maxnames) {
2452

2453
    testConnPtr privconn = conn->privateData;
2454
    int n;
2455

2456
    testDriverLock(privconn);
2457
    memset(names, 0, sizeof(*names)*maxnames);
2458
    n = virDomainObjListGetInactiveNames(&privconn->domains, names, maxnames);
2459
    testDriverUnlock(privconn);
2460

2461
    return n;
2462 2463
}

2464
static virDomainPtr testDomainDefineXML(virConnectPtr conn,
2465
                                        const char *xml) {
2466
    testConnPtr privconn = conn->privateData;
2467
    virDomainPtr ret = NULL;
2468
    virDomainDefPtr def;
2469
    virDomainObjPtr dom = NULL;
2470
    virDomainEventPtr event = NULL;
2471
    int dupVM;
2472

2473
    testDriverLock(privconn);
2474
    if ((def = virDomainDefParseString(privconn->caps, xml,
M
Matthias Bolte 已提交
2475
                                       1 << VIR_DOMAIN_VIRT_TEST,
2476
                                       VIR_DOMAIN_XML_INACTIVE)) == NULL)
2477
        goto cleanup;
2478

2479 2480 2481
    if ((dupVM = virDomainObjIsDuplicate(&privconn->domains, def, 0)) < 0)
        goto cleanup;

2482
    if (testDomainGenerateIfnames(def) < 0)
2483
        goto cleanup;
2484
    if (!(dom = virDomainAssignDef(privconn->caps,
2485
                                   &privconn->domains, def, false)))
2486
        goto cleanup;
2487
    def = NULL;
2488
    dom->persistent = 1;
2489

2490 2491
    event = virDomainEventNewFromObj(dom,
                                     VIR_DOMAIN_EVENT_DEFINED,
2492 2493 2494
                                     !dupVM ?
                                     VIR_DOMAIN_EVENT_DEFINED_ADDED :
                                     VIR_DOMAIN_EVENT_DEFINED_UPDATED);
2495

2496
    ret = virGetDomain(conn, dom->def->name, dom->def->uuid);
2497
    if (ret)
2498
        ret->id = dom->def->id;
2499 2500 2501

cleanup:
    virDomainDefFree(def);
2502 2503
    if (dom)
        virDomainObjUnlock(dom);
2504 2505
    if (event)
        testDomainEventQueue(privconn, event);
2506
    testDriverUnlock(privconn);
2507
    return ret;
2508 2509
}

2510 2511 2512
static int testNodeGetCellsFreeMemory(virConnectPtr conn,
                                      unsigned long long *freemems,
                                      int startCell, int maxCells) {
2513
    testConnPtr privconn = conn->privateData;
2514
    int i, j;
2515
    int ret = -1;
2516

2517
    testDriverLock(privconn);
2518
    if (startCell > privconn->numCells) {
2519
        testError(VIR_ERR_INVALID_ARG,
J
Jim Meyering 已提交
2520
                  "%s", _("Range exceeds available cells"));
2521
        goto cleanup;
2522 2523 2524 2525 2526 2527 2528
    }

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

2531
cleanup:
2532
    testDriverUnlock(privconn);
2533
    return ret;
2534 2535 2536
}


2537
static int testDomainCreateWithFlags(virDomainPtr domain, unsigned int flags) {
2538 2539
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
2540
    virDomainEventPtr event = NULL;
2541
    int ret = -1;
2542

2543 2544
    virCheckFlags(0, -1);

2545
    testDriverLock(privconn);
2546 2547 2548 2549
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);

    if (privdom == NULL) {
2550
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2551
        goto cleanup;
2552
    }
2553

J
Jiri Denemark 已提交
2554
    if (virDomainObjGetState(privdom, NULL) != VIR_DOMAIN_SHUTOFF) {
2555
        testError(VIR_ERR_INTERNAL_ERROR,
2556
                  _("Domain '%s' is already running"), domain->name);
2557
        goto cleanup;
2558 2559
    }

J
Jiri Denemark 已提交
2560 2561
    if (testDomainStartState(domain->conn, privdom,
                             VIR_DOMAIN_RUNNING_BOOTED) < 0)
2562 2563 2564
        goto cleanup;
    domain->id = privdom->def->id;

2565 2566 2567
    event = virDomainEventNewFromObj(privdom,
                                     VIR_DOMAIN_EVENT_STARTED,
                                     VIR_DOMAIN_EVENT_STARTED_BOOTED);
2568
    ret = 0;
2569

2570
cleanup:
2571 2572
    if (privdom)
        virDomainObjUnlock(privdom);
2573 2574
    if (event)
        testDomainEventQueue(privconn, event);
2575
    testDriverUnlock(privconn);
2576
    return ret;
2577 2578
}

2579 2580 2581 2582
static int testDomainCreate(virDomainPtr domain) {
    return testDomainCreateWithFlags(domain, 0);
}

2583 2584 2585
static int testDomainUndefineFlags(virDomainPtr domain,
                                   unsigned int flags)
{
2586 2587
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
2588
    virDomainEventPtr event = NULL;
2589
    int ret = -1;
2590

2591 2592
    virCheckFlags(0, -1);

2593
    testDriverLock(privconn);
2594 2595 2596 2597
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);

    if (privdom == NULL) {
2598
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2599
        goto cleanup;
2600
    }
2601

2602 2603 2604
    event = virDomainEventNewFromObj(privdom,
                                     VIR_DOMAIN_EVENT_UNDEFINED,
                                     VIR_DOMAIN_EVENT_UNDEFINED_REMOVED);
2605 2606
    if (virDomainObjIsActive(privdom)) {
        privdom->persistent = 0;
2607 2608 2609 2610 2611 2612
    } else {
        virDomainRemoveInactive(&privconn->domains,
                                privdom);
        privdom = NULL;
    }

2613
    ret = 0;
2614

2615
cleanup:
2616 2617
    if (privdom)
        virDomainObjUnlock(privdom);
2618 2619
    if (event)
        testDomainEventQueue(privconn, event);
2620
    testDriverUnlock(privconn);
2621
    return ret;
2622 2623
}

2624 2625 2626 2627 2628
static int testDomainUndefine(virDomainPtr domain)
{
    return testDomainUndefineFlags(domain, 0);
}

2629 2630 2631
static int testDomainGetAutostart(virDomainPtr domain,
                                  int *autostart)
{
2632 2633
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
2634
    int ret = -1;
2635

2636
    testDriverLock(privconn);
2637 2638
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
2639
    testDriverUnlock(privconn);
2640 2641

    if (privdom == NULL) {
2642
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2643
        goto cleanup;
2644 2645
    }

2646
    *autostart = privdom->autostart;
2647 2648 2649
    ret = 0;

cleanup:
2650 2651
    if (privdom)
        virDomainObjUnlock(privdom);
2652
    return ret;
2653 2654 2655 2656 2657 2658
}


static int testDomainSetAutostart(virDomainPtr domain,
                                  int autostart)
{
2659 2660
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
2661
    int ret = -1;
2662

2663
    testDriverLock(privconn);
2664 2665
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
2666
    testDriverUnlock(privconn);
2667 2668

    if (privdom == NULL) {
2669
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2670
        goto cleanup;
2671 2672
    }

2673
    privdom->autostart = autostart ? 1 : 0;
2674 2675 2676
    ret = 0;

cleanup:
2677 2678
    if (privdom)
        virDomainObjUnlock(privdom);
2679
    return ret;
2680
}
2681

2682
static char *testDomainGetSchedulerType(virDomainPtr domain ATTRIBUTE_UNUSED,
2683 2684
                                        int *nparams)
{
2685 2686
    char *type = NULL;

2687 2688 2689
    if (nparams)
        *nparams = 1;

2690
    type = strdup("fair");
2691
    if (!type)
2692
        virReportOOMError();
2693

2694 2695 2696
    return type;
}

2697 2698 2699 2700 2701
static int
testDomainGetSchedulerParamsFlags(virDomainPtr domain,
                                  virTypedParameterPtr params,
                                  int *nparams,
                                  unsigned int flags)
2702
{
2703 2704
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
2705
    int ret = -1;
2706

2707 2708
    virCheckFlags(0, -1);

2709
    testDriverLock(privconn);
2710 2711
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
2712
    testDriverUnlock(privconn);
2713 2714

    if (privdom == NULL) {
2715
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2716
        goto cleanup;
2717 2718
    }

E
Eric Blake 已提交
2719 2720 2721 2722
    if (virStrcpyStatic(params[0].field,
                        VIR_DOMAIN_SCHEDULER_WEIGHT) == NULL) {
        testError(VIR_ERR_INTERNAL_ERROR, _("Field name '%s' too long"),
                  VIR_DOMAIN_SCHEDULER_WEIGHT);
2723
        goto cleanup;
2724
    }
2725
    params[0].type = VIR_TYPED_PARAM_UINT;
2726 2727 2728
    /* XXX */
    /*params[0].value.ui = privdom->weight;*/
    params[0].value.ui = 50;
2729 2730

    *nparams = 1;
2731 2732 2733
    ret = 0;

cleanup:
2734 2735
    if (privdom)
        virDomainObjUnlock(privdom);
2736
    return ret;
2737
}
2738

2739 2740 2741 2742 2743 2744 2745
static int
testDomainGetSchedulerParams(virDomainPtr domain,
                             virTypedParameterPtr params,
                             int *nparams)
{
    return testDomainGetSchedulerParamsFlags(domain, params, nparams, 0);
}
2746

2747 2748 2749 2750 2751
static int
testDomainSetSchedulerParamsFlags(virDomainPtr domain,
                                  virTypedParameterPtr params,
                                  int nparams,
                                  unsigned int flags)
2752
{
2753 2754
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
2755
    int ret = -1, i;
2756

2757 2758
    virCheckFlags(0, -1);

2759
    testDriverLock(privconn);
2760 2761
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
2762
    testDriverUnlock(privconn);
2763 2764

    if (privdom == NULL) {
2765
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2766
        goto cleanup;
2767 2768
    }

2769
    for (i = 0; i < nparams; i++) {
2770
        if (STRNEQ(params[i].field, VIR_DOMAIN_SCHEDULER_WEIGHT)) {
2771 2772 2773
            testError(VIR_ERR_INVALID_ARG, "field");
            goto cleanup;
        }
2774
        if (params[i].type != VIR_TYPED_PARAM_UINT) {
2775 2776 2777 2778 2779
            testError(VIR_ERR_INVALID_ARG, "type");
            goto cleanup;
        }
        /* XXX */
        /*privdom->weight = params[i].value.ui;*/
2780
    }
2781

2782 2783 2784
    ret = 0;

cleanup:
2785 2786
    if (privdom)
        virDomainObjUnlock(privdom);
2787
    return ret;
2788 2789
}

2790 2791 2792 2793 2794 2795 2796 2797
static int
testDomainSetSchedulerParams(virDomainPtr domain,
                             virTypedParameterPtr params,
                             int nparams)
{
    return testDomainSetSchedulerParamsFlags(domain, params, nparams, 0);
}

2798 2799 2800 2801 2802 2803 2804 2805
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;
2806
    int ret = -1;
2807 2808 2809 2810 2811 2812 2813

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

    if (privdom == NULL) {
2814
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2815 2816 2817
        goto error;
    }

2818
    if (virDomainDiskIndexByName(privdom->def, path, false) < 0) {
2819
        testError(VIR_ERR_INVALID_ARG,
2820 2821 2822 2823 2824
                  _("invalid path: %s"), path);
        goto error;
    }

    if (gettimeofday(&tv, NULL) < 0) {
2825
        virReportSystemError(errno,
2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860
                             "%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) {
2861
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873
        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) {
2874
        testError(VIR_ERR_INVALID_ARG,
2875 2876 2877 2878 2879
                  _("invalid path, '%s' is not a known interface"), path);
        goto error;
    }

    if (gettimeofday(&tv, NULL) < 0) {
2880
        virReportSystemError(errno,
2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902
                             "%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;
}

2903
static virDrvOpenStatus testOpenNetwork(virConnectPtr conn,
2904
                                        virConnectAuthPtr auth ATTRIBUTE_UNUSED,
E
Eric Blake 已提交
2905 2906 2907 2908
                                        unsigned int flags)
{
    virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);

2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924
    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)
{
2925 2926
    testConnPtr privconn = conn->privateData;
    virNetworkObjPtr net;
2927
    virNetworkPtr ret = NULL;
2928

2929 2930 2931 2932 2933
    testDriverLock(privconn);
    net = virNetworkFindByUUID(&privconn->networks, uuid);
    testDriverUnlock(privconn);

    if (net == NULL) {
2934
        testError(VIR_ERR_NO_NETWORK, NULL);
2935
        goto cleanup;
2936 2937
    }

2938 2939 2940
    ret = virGetNetwork(conn, net->def->name, net->def->uuid);

cleanup:
2941 2942
    if (net)
        virNetworkObjUnlock(net);
2943
    return ret;
2944
}
2945

2946
static virNetworkPtr testLookupNetworkByName(virConnectPtr conn,
2947
                                             const char *name)
2948
{
2949
    testConnPtr privconn = conn->privateData;
2950 2951
    virNetworkObjPtr net;
    virNetworkPtr ret = NULL;
2952

2953 2954 2955 2956 2957
    testDriverLock(privconn);
    net = virNetworkFindByName(&privconn->networks, name);
    testDriverUnlock(privconn);

    if (net == NULL) {
2958
        testError(VIR_ERR_NO_NETWORK, NULL);
2959
        goto cleanup;
2960 2961
    }

2962 2963 2964
    ret = virGetNetwork(conn, net->def->name, net->def->uuid);

cleanup:
2965 2966
    if (net)
        virNetworkObjUnlock(net);
2967
    return ret;
2968 2969 2970 2971
}


static int testNumNetworks(virConnectPtr conn) {
2972
    testConnPtr privconn = conn->privateData;
2973
    int numActive = 0, i;
2974

2975 2976 2977
    testDriverLock(privconn);
    for (i = 0 ; i < privconn->networks.count ; i++) {
        virNetworkObjLock(privconn->networks.objs[i]);
D
Daniel P. Berrange 已提交
2978
        if (virNetworkObjIsActive(privconn->networks.objs[i]))
2979
            numActive++;
2980 2981 2982
        virNetworkObjUnlock(privconn->networks.objs[i]);
    }
    testDriverUnlock(privconn);
2983

2984
    return numActive;
2985 2986 2987
}

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

2991
    testDriverLock(privconn);
2992
    memset(names, 0, sizeof(*names)*nnames);
2993 2994
    for (i = 0 ; i < privconn->networks.count && n < nnames ; i++) {
        virNetworkObjLock(privconn->networks.objs[i]);
D
Daniel P. Berrange 已提交
2995
        if (virNetworkObjIsActive(privconn->networks.objs[i]) &&
2996 2997
            !(names[n++] = strdup(privconn->networks.objs[i]->def->name))) {
            virNetworkObjUnlock(privconn->networks.objs[i]);
2998
            goto no_memory;
2999 3000 3001 3002
        }
        virNetworkObjUnlock(privconn->networks.objs[i]);
    }
    testDriverUnlock(privconn);
3003

3004 3005 3006
    return n;

no_memory:
3007
    virReportOOMError();
3008 3009
    for (n = 0 ; n < nnames ; n++)
        VIR_FREE(names[n]);
3010
    testDriverUnlock(privconn);
3011
    return -1;
3012 3013 3014
}

static int testNumDefinedNetworks(virConnectPtr conn) {
3015
    testConnPtr privconn = conn->privateData;
3016
    int numInactive = 0, i;
3017

3018 3019 3020
    testDriverLock(privconn);
    for (i = 0 ; i < privconn->networks.count ; i++) {
        virNetworkObjLock(privconn->networks.objs[i]);
D
Daniel P. Berrange 已提交
3021
        if (!virNetworkObjIsActive(privconn->networks.objs[i]))
3022
            numInactive++;
3023 3024 3025
        virNetworkObjUnlock(privconn->networks.objs[i]);
    }
    testDriverUnlock(privconn);
3026

3027
    return numInactive;
3028 3029 3030
}

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

3034
    testDriverLock(privconn);
3035
    memset(names, 0, sizeof(*names)*nnames);
3036 3037
    for (i = 0 ; i < privconn->networks.count && n < nnames ; i++) {
        virNetworkObjLock(privconn->networks.objs[i]);
D
Daniel P. Berrange 已提交
3038
        if (!virNetworkObjIsActive(privconn->networks.objs[i]) &&
3039 3040
            !(names[n++] = strdup(privconn->networks.objs[i]->def->name))) {
            virNetworkObjUnlock(privconn->networks.objs[i]);
3041
            goto no_memory;
3042 3043 3044 3045
        }
        virNetworkObjUnlock(privconn->networks.objs[i]);
    }
    testDriverUnlock(privconn);
3046

3047 3048 3049
    return n;

no_memory:
3050
    virReportOOMError();
3051 3052
    for (n = 0 ; n < nnames ; n++)
        VIR_FREE(names[n]);
3053
    testDriverUnlock(privconn);
3054
    return -1;
3055 3056
}

3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067

static int testNetworkIsActive(virNetworkPtr net)
{
    testConnPtr privconn = net->conn->privateData;
    virNetworkObjPtr obj;
    int ret = -1;

    testDriverLock(privconn);
    obj = virNetworkFindByUUID(&privconn->networks, net->uuid);
    testDriverUnlock(privconn);
    if (!obj) {
3068
        testError(VIR_ERR_NO_NETWORK, NULL);
3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088
        goto cleanup;
    }
    ret = virNetworkObjIsActive(obj);

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

static int testNetworkIsPersistent(virNetworkPtr net)
{
    testConnPtr privconn = net->conn->privateData;
    virNetworkObjPtr obj;
    int ret = -1;

    testDriverLock(privconn);
    obj = virNetworkFindByUUID(&privconn->networks, net->uuid);
    testDriverUnlock(privconn);
    if (!obj) {
3089
        testError(VIR_ERR_NO_NETWORK, NULL);
3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100
        goto cleanup;
    }
    ret = obj->persistent;

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


3101
static virNetworkPtr testNetworkCreate(virConnectPtr conn, const char *xml) {
3102
    testConnPtr privconn = conn->privateData;
3103
    virNetworkDefPtr def;
3104
    virNetworkObjPtr net = NULL;
3105
    virNetworkPtr ret = NULL;
3106

3107
    testDriverLock(privconn);
3108
    if ((def = virNetworkDefParseString(xml)) == NULL)
3109
        goto cleanup;
3110

3111
    if ((net = virNetworkAssignDef(&privconn->networks, def)) == NULL)
3112 3113
        goto cleanup;
    def = NULL;
3114
    net->active = 1;
3115

3116
    ret = virGetNetwork(conn, net->def->name, net->def->uuid);
3117

3118 3119
cleanup:
    virNetworkDefFree(def);
3120 3121 3122
    if (net)
        virNetworkObjUnlock(net);
    testDriverUnlock(privconn);
3123
    return ret;
3124 3125 3126
}

static virNetworkPtr testNetworkDefine(virConnectPtr conn, const char *xml) {
3127
    testConnPtr privconn = conn->privateData;
3128
    virNetworkDefPtr def;
3129
    virNetworkObjPtr net = NULL;
3130
    virNetworkPtr ret = NULL;
3131

3132
    testDriverLock(privconn);
3133
    if ((def = virNetworkDefParseString(xml)) == NULL)
3134
        goto cleanup;
3135

3136
    if ((net = virNetworkAssignDef(&privconn->networks, def)) == NULL)
3137 3138
        goto cleanup;
    def = NULL;
3139
    net->persistent = 1;
3140

3141
    ret = virGetNetwork(conn, net->def->name, net->def->uuid);
3142 3143 3144

cleanup:
    virNetworkDefFree(def);
3145 3146 3147
    if (net)
        virNetworkObjUnlock(net);
    testDriverUnlock(privconn);
3148
    return ret;
3149 3150 3151
}

static int testNetworkUndefine(virNetworkPtr network) {
3152 3153
    testConnPtr privconn = network->conn->privateData;
    virNetworkObjPtr privnet;
3154
    int ret = -1;
3155

3156
    testDriverLock(privconn);
3157 3158 3159 3160
    privnet = virNetworkFindByName(&privconn->networks,
                                   network->name);

    if (privnet == NULL) {
3161
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3162
        goto cleanup;
3163
    }
3164

D
Daniel P. Berrange 已提交
3165
    if (virNetworkObjIsActive(privnet)) {
3166
        testError(VIR_ERR_OPERATION_INVALID,
3167
                  _("Network '%s' is still running"), network->name);
3168
        goto cleanup;
3169 3170
    }

3171 3172
    virNetworkRemoveInactive(&privconn->networks,
                             privnet);
3173
    privnet = NULL;
3174
    ret = 0;
3175

3176
cleanup:
3177 3178 3179
    if (privnet)
        virNetworkObjUnlock(privnet);
    testDriverUnlock(privconn);
3180
    return ret;
3181 3182 3183
}

static int testNetworkStart(virNetworkPtr network) {
3184 3185
    testConnPtr privconn = network->conn->privateData;
    virNetworkObjPtr privnet;
3186
    int ret = -1;
3187

3188
    testDriverLock(privconn);
3189 3190
    privnet = virNetworkFindByName(&privconn->networks,
                                   network->name);
3191
    testDriverUnlock(privconn);
3192 3193

    if (privnet == NULL) {
3194
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3195
        goto cleanup;
3196
    }
3197

D
Daniel P. Berrange 已提交
3198
    if (virNetworkObjIsActive(privnet)) {
3199
        testError(VIR_ERR_OPERATION_INVALID,
3200
                  _("Network '%s' is already running"), network->name);
3201
        goto cleanup;
3202 3203
    }

3204
    privnet->active = 1;
3205
    ret = 0;
3206

3207
cleanup:
3208 3209
    if (privnet)
        virNetworkObjUnlock(privnet);
3210
    return ret;
3211 3212 3213
}

static int testNetworkDestroy(virNetworkPtr network) {
3214 3215
    testConnPtr privconn = network->conn->privateData;
    virNetworkObjPtr privnet;
3216
    int ret = -1;
3217

3218
    testDriverLock(privconn);
3219 3220 3221 3222
    privnet = virNetworkFindByName(&privconn->networks,
                                   network->name);

    if (privnet == NULL) {
3223
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3224
        goto cleanup;
3225
    }
3226

3227 3228 3229 3230
    privnet->active = 0;
    if (!privnet->persistent) {
        virNetworkRemoveInactive(&privconn->networks,
                                 privnet);
3231
        privnet = NULL;
3232
    }
3233 3234 3235
    ret = 0;

cleanup:
3236 3237 3238
    if (privnet)
        virNetworkObjUnlock(privnet);
    testDriverUnlock(privconn);
3239
    return ret;
3240 3241
}

3242
static char *testNetworkGetXMLDesc(virNetworkPtr network,
E
Eric Blake 已提交
3243
                                   unsigned int flags)
3244
{
3245 3246
    testConnPtr privconn = network->conn->privateData;
    virNetworkObjPtr privnet;
3247
    char *ret = NULL;
3248

E
Eric Blake 已提交
3249 3250
    virCheckFlags(0, NULL);

3251
    testDriverLock(privconn);
3252 3253
    privnet = virNetworkFindByName(&privconn->networks,
                                   network->name);
3254
    testDriverUnlock(privconn);
3255 3256

    if (privnet == NULL) {
3257
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3258
        goto cleanup;
3259
    }
3260

3261
    ret = virNetworkDefFormat(privnet->def);
3262 3263

cleanup:
3264 3265
    if (privnet)
        virNetworkObjUnlock(privnet);
3266
    return ret;
3267 3268 3269
}

static char *testNetworkGetBridgeName(virNetworkPtr network) {
3270
    testConnPtr privconn = network->conn->privateData;
3271
    char *bridge = NULL;
3272 3273
    virNetworkObjPtr privnet;

3274
    testDriverLock(privconn);
3275 3276
    privnet = virNetworkFindByName(&privconn->networks,
                                   network->name);
3277
    testDriverUnlock(privconn);
3278 3279

    if (privnet == NULL) {
3280
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3281
        goto cleanup;
3282 3283
    }

3284
    if (!(privnet->def->bridge)) {
3285
        testError(VIR_ERR_INTERNAL_ERROR,
3286 3287 3288 3289 3290 3291
                  _("network '%s' does not have a bridge name."),
                  privnet->def->name);
        goto cleanup;
    }

    if (!(bridge = strdup(privnet->def->bridge))) {
3292
        virReportOOMError();
3293
        goto cleanup;
3294
    }
3295 3296

cleanup:
3297 3298
    if (privnet)
        virNetworkObjUnlock(privnet);
3299 3300 3301 3302 3303
    return bridge;
}

static int testNetworkGetAutostart(virNetworkPtr network,
                                   int *autostart) {
3304 3305
    testConnPtr privconn = network->conn->privateData;
    virNetworkObjPtr privnet;
3306
    int ret = -1;
3307

3308
    testDriverLock(privconn);
3309 3310
    privnet = virNetworkFindByName(&privconn->networks,
                                   network->name);
3311
    testDriverUnlock(privconn);
3312 3313

    if (privnet == NULL) {
3314
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3315
        goto cleanup;
3316 3317
    }

3318
    *autostart = privnet->autostart;
3319 3320 3321
    ret = 0;

cleanup:
3322 3323
    if (privnet)
        virNetworkObjUnlock(privnet);
3324
    return ret;
3325 3326 3327 3328
}

static int testNetworkSetAutostart(virNetworkPtr network,
                                   int autostart) {
3329 3330
    testConnPtr privconn = network->conn->privateData;
    virNetworkObjPtr privnet;
3331
    int ret = -1;
3332

3333
    testDriverLock(privconn);
3334 3335
    privnet = virNetworkFindByName(&privconn->networks,
                                   network->name);
3336
    testDriverUnlock(privconn);
3337 3338

    if (privnet == NULL) {
3339
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3340
        goto cleanup;
3341 3342
    }

3343
    privnet->autostart = autostart ? 1 : 0;
3344 3345 3346
    ret = 0;

cleanup:
3347 3348
    if (privnet)
        virNetworkObjUnlock(privnet);
3349
    return ret;
3350
}
3351

C
Cole Robinson 已提交
3352

L
Laine Stump 已提交
3353 3354 3355 3356 3357 3358
/*
 * Physical host interface routines
 */

static virDrvOpenStatus testOpenInterface(virConnectPtr conn,
                                          virConnectAuthPtr auth ATTRIBUTE_UNUSED,
E
Eric Blake 已提交
3359
                                          unsigned int flags)
L
Laine Stump 已提交
3360
{
E
Eric Blake 已提交
3361 3362
    virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);

L
Laine Stump 已提交
3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384
    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 已提交
3385
        if (virInterfaceObjIsActive(privconn->ifaces.objs[i])) {
L
Laine Stump 已提交
3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402
            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 已提交
3403
        if (virInterfaceObjIsActive(privconn->ifaces.objs[i])) {
L
Laine Stump 已提交
3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415
            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:
3416
    virReportOOMError();
L
Laine Stump 已提交
3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430
    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 已提交
3431
        if (!virInterfaceObjIsActive(privconn->ifaces.objs[i])) {
L
Laine Stump 已提交
3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448
            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 已提交
3449
        if (!virInterfaceObjIsActive(privconn->ifaces.objs[i])) {
L
Laine Stump 已提交
3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461
            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:
3462
    virReportOOMError();
L
Laine Stump 已提交
3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480
    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) {
3481
        testError(VIR_ERR_NO_INTERFACE, NULL);
L
Laine Stump 已提交
3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505
        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) {
3506
        testError(VIR_ERR_NO_INTERFACE, NULL);
L
Laine Stump 已提交
3507 3508 3509 3510
        goto cleanup;
    }

    if (ifacect > 1) {
3511
        testError(VIR_ERR_MULTIPLE_INTERFACES, NULL);
L
Laine Stump 已提交
3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522
        goto cleanup;
    }

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

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

3523 3524 3525 3526 3527 3528 3529 3530 3531 3532
static int testInterfaceIsActive(virInterfacePtr iface)
{
    testConnPtr privconn = iface->conn->privateData;
    virInterfaceObjPtr obj;
    int ret = -1;

    testDriverLock(privconn);
    obj = virInterfaceFindByName(&privconn->ifaces, iface->name);
    testDriverUnlock(privconn);
    if (!obj) {
3533
        testError(VIR_ERR_NO_INTERFACE, NULL);
3534 3535 3536 3537 3538 3539 3540 3541 3542 3543
        goto cleanup;
    }
    ret = virInterfaceObjIsActive(obj);

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

3544
static int testInterfaceChangeBegin(virConnectPtr conn,
E
Eric Blake 已提交
3545
                                    unsigned int flags)
3546 3547 3548 3549
{
    testConnPtr privconn = conn->privateData;
    int ret = -1;

E
Eric Blake 已提交
3550 3551
    virCheckFlags(0, -1);

3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571
    testDriverLock(privconn);
    if (privconn->transaction_running) {
        testError(VIR_ERR_OPERATION_INVALID,
                  _("there is another transaction running."));
        goto cleanup;
    }

    privconn->transaction_running = true;

    if (virInterfaceObjListClone(&privconn->ifaces,
                                 &privconn->backupIfaces) < 0)
        goto cleanup;

    ret = 0;
cleanup:
    testDriverUnlock(privconn);
    return ret;
}

static int testInterfaceChangeCommit(virConnectPtr conn,
E
Eric Blake 已提交
3572
                                     unsigned int flags)
3573 3574 3575 3576
{
    testConnPtr privconn = conn->privateData;
    int ret = -1;

E
Eric Blake 已提交
3577 3578
    virCheckFlags(0, -1);

3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598
    testDriverLock(privconn);

    if (!privconn->transaction_running) {
        testError(VIR_ERR_OPERATION_INVALID, _("no transaction running, "
                  "nothing to be commited."));
        goto cleanup;
    }

    virInterfaceObjListFree(&privconn->backupIfaces);
    privconn->transaction_running = false;

    ret = 0;

cleanup:
    testDriverUnlock(privconn);

    return ret;
}

static int testInterfaceChangeRollback(virConnectPtr conn,
E
Eric Blake 已提交
3599
                                       unsigned int flags)
3600 3601 3602 3603
{
    testConnPtr privconn = conn->privateData;
    int ret = -1;

E
Eric Blake 已提交
3604 3605
    virCheckFlags(0, -1);

3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627
    testDriverLock(privconn);

    if (!privconn->transaction_running) {
        testError(VIR_ERR_OPERATION_INVALID, _("no transaction running, "
                  "nothing to rollback."));
        goto cleanup;
    }

    virInterfaceObjListFree(&privconn->ifaces);
    privconn->ifaces.count = privconn->backupIfaces.count;
    privconn->ifaces.objs = privconn->backupIfaces.objs;
    privconn->backupIfaces.count = 0;
    privconn->backupIfaces.objs = NULL;

    privconn->transaction_running = false;

    ret = 0;

cleanup:
    testDriverUnlock(privconn);
    return ret;
}
3628

L
Laine Stump 已提交
3629
static char *testInterfaceGetXMLDesc(virInterfacePtr iface,
E
Eric Blake 已提交
3630
                                     unsigned int flags)
L
Laine Stump 已提交
3631 3632 3633 3634 3635
{
    testConnPtr privconn = iface->conn->privateData;
    virInterfaceObjPtr privinterface;
    char *ret = NULL;

E
Eric Blake 已提交
3636 3637
    virCheckFlags(0, NULL);

L
Laine Stump 已提交
3638 3639 3640 3641 3642 3643
    testDriverLock(privconn);
    privinterface = virInterfaceFindByName(&privconn->ifaces,
                                           iface->name);
    testDriverUnlock(privconn);

    if (privinterface == NULL) {
3644
        testError(VIR_ERR_NO_INTERFACE, __FUNCTION__);
L
Laine Stump 已提交
3645 3646 3647
        goto cleanup;
    }

3648
    ret = virInterfaceDefFormat(privinterface->def);
L
Laine Stump 已提交
3649 3650 3651 3652 3653 3654 3655 3656 3657

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


static virInterfacePtr testInterfaceDefineXML(virConnectPtr conn, const char *xmlStr,
E
Eric Blake 已提交
3658
                                              unsigned int flags)
L
Laine Stump 已提交
3659 3660 3661 3662 3663 3664
{
    testConnPtr privconn = conn->privateData;
    virInterfaceDefPtr def;
    virInterfaceObjPtr iface = NULL;
    virInterfacePtr ret = NULL;

E
Eric Blake 已提交
3665 3666
    virCheckFlags(0, NULL);

L
Laine Stump 已提交
3667
    testDriverLock(privconn);
3668
    if ((def = virInterfaceDefParseString(xmlStr)) == NULL)
L
Laine Stump 已提交
3669 3670
        goto cleanup;

3671
    if ((iface = virInterfaceAssignDef(&privconn->ifaces, def)) == NULL)
L
Laine Stump 已提交
3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695
        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) {
3696
        testError(VIR_ERR_NO_INTERFACE, NULL);
L
Laine Stump 已提交
3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709
        goto cleanup;
    }

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

cleanup:
    testDriverUnlock(privconn);
    return ret;
}

static int testInterfaceCreate(virInterfacePtr iface,
E
Eric Blake 已提交
3710
                               unsigned int flags)
L
Laine Stump 已提交
3711 3712 3713 3714 3715
{
    testConnPtr privconn = iface->conn->privateData;
    virInterfaceObjPtr privinterface;
    int ret = -1;

E
Eric Blake 已提交
3716 3717
    virCheckFlags(0, -1);

L
Laine Stump 已提交
3718 3719 3720 3721 3722
    testDriverLock(privconn);
    privinterface = virInterfaceFindByName(&privconn->ifaces,
                                           iface->name);

    if (privinterface == NULL) {
3723
        testError(VIR_ERR_NO_INTERFACE, NULL);
L
Laine Stump 已提交
3724 3725 3726 3727
        goto cleanup;
    }

    if (privinterface->active != 0) {
3728
        testError(VIR_ERR_OPERATION_INVALID, NULL);
L
Laine Stump 已提交
3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742
        goto cleanup;
    }

    privinterface->active = 1;
    ret = 0;

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

static int testInterfaceDestroy(virInterfacePtr iface,
E
Eric Blake 已提交
3743
                                unsigned int flags)
L
Laine Stump 已提交
3744 3745 3746 3747 3748
{
    testConnPtr privconn = iface->conn->privateData;
    virInterfaceObjPtr privinterface;
    int ret = -1;

E
Eric Blake 已提交
3749 3750
    virCheckFlags(0, -1);

L
Laine Stump 已提交
3751 3752 3753 3754 3755
    testDriverLock(privconn);
    privinterface = virInterfaceFindByName(&privconn->ifaces,
                                           iface->name);

    if (privinterface == NULL) {
3756
        testError(VIR_ERR_NO_INTERFACE, NULL);
L
Laine Stump 已提交
3757 3758 3759 3760
        goto cleanup;
    }

    if (privinterface->active == 0) {
3761
        testError(VIR_ERR_OPERATION_INVALID, NULL);
L
Laine Stump 已提交
3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776
        goto cleanup;
    }

    privinterface->active = 0;
    ret = 0;

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



C
Cole Robinson 已提交
3777 3778 3779 3780
/*
 * Storage Driver routines
 */

3781

3782
static int testStoragePoolObjSetDefaults(virStoragePoolObjPtr pool) {
C
Cole Robinson 已提交
3783 3784 3785 3786 3787 3788 3789

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

    pool->configFile = strdup("\0");
    if (!pool->configFile) {
3790
        virReportOOMError();
C
Cole Robinson 已提交
3791 3792 3793 3794 3795 3796
        return -1;
    }

    return 0;
}

3797 3798
static virDrvOpenStatus testStorageOpen(virConnectPtr conn,
                                        virConnectAuthPtr auth ATTRIBUTE_UNUSED,
E
Eric Blake 已提交
3799 3800 3801 3802
                                        unsigned int flags)
{
    virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);

3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814
    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;
}

3815

C
Cole Robinson 已提交
3816 3817 3818
static virStoragePoolPtr
testStoragePoolLookupByUUID(virConnectPtr conn,
                            const unsigned char *uuid) {
3819
    testConnPtr privconn = conn->privateData;
3820 3821
    virStoragePoolObjPtr pool;
    virStoragePoolPtr ret = NULL;
C
Cole Robinson 已提交
3822

3823
    testDriverLock(privconn);
3824
    pool = virStoragePoolObjFindByUUID(&privconn->pools, uuid);
3825
    testDriverUnlock(privconn);
3826 3827

    if (pool == NULL) {
3828
        testError(VIR_ERR_NO_STORAGE_POOL, NULL);
3829
        goto cleanup;
C
Cole Robinson 已提交
3830 3831
    }

3832 3833 3834
    ret = virGetStoragePool(conn, pool->def->name, pool->def->uuid);

cleanup:
3835 3836
    if (pool)
        virStoragePoolObjUnlock(pool);
3837
    return ret;
C
Cole Robinson 已提交
3838 3839 3840 3841 3842
}

static virStoragePoolPtr
testStoragePoolLookupByName(virConnectPtr conn,
                            const char *name) {
3843
    testConnPtr privconn = conn->privateData;
3844 3845
    virStoragePoolObjPtr pool;
    virStoragePoolPtr ret = NULL;
C
Cole Robinson 已提交
3846

3847
    testDriverLock(privconn);
3848
    pool = virStoragePoolObjFindByName(&privconn->pools, name);
3849
    testDriverUnlock(privconn);
3850 3851

    if (pool == NULL) {
3852
        testError(VIR_ERR_NO_STORAGE_POOL, NULL);
3853
        goto cleanup;
C
Cole Robinson 已提交
3854 3855
    }

3856 3857 3858
    ret = virGetStoragePool(conn, pool->def->name, pool->def->uuid);

cleanup:
3859 3860
    if (pool)
        virStoragePoolObjUnlock(pool);
3861
    return ret;
C
Cole Robinson 已提交
3862 3863 3864 3865 3866 3867 3868 3869 3870
}

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

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

3874
    testDriverLock(privconn);
C
Cole Robinson 已提交
3875 3876 3877
    for (i = 0 ; i < privconn->pools.count ; i++)
        if (virStoragePoolObjIsActive(privconn->pools.objs[i]))
            numActive++;
3878
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
3879 3880 3881 3882 3883 3884 3885 3886

    return numActive;
}

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

3890
    testDriverLock(privconn);
C
Cole Robinson 已提交
3891
    memset(names, 0, sizeof(*names)*nnames);
3892 3893
    for (i = 0 ; i < privconn->pools.count && n < nnames ; i++) {
        virStoragePoolObjLock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
3894
        if (virStoragePoolObjIsActive(privconn->pools.objs[i]) &&
3895 3896
            !(names[n++] = strdup(privconn->pools.objs[i]->def->name))) {
            virStoragePoolObjUnlock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
3897
            goto no_memory;
3898 3899 3900 3901
        }
        virStoragePoolObjUnlock(privconn->pools.objs[i]);
    }
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
3902 3903 3904 3905

    return n;

no_memory:
3906
    virReportOOMError();
C
Cole Robinson 已提交
3907 3908
    for (n = 0 ; n < nnames ; n++)
        VIR_FREE(names[n]);
3909
    testDriverUnlock(privconn);
3910
    return -1;
C
Cole Robinson 已提交
3911 3912 3913 3914
}

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

3918 3919 3920
    testDriverLock(privconn);
    for (i = 0 ; i < privconn->pools.count ; i++) {
        virStoragePoolObjLock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
3921 3922
        if (!virStoragePoolObjIsActive(privconn->pools.objs[i]))
            numInactive++;
3923 3924 3925
        virStoragePoolObjUnlock(privconn->pools.objs[i]);
    }
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
3926 3927 3928 3929 3930 3931 3932 3933

    return numInactive;
}

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

3937
    testDriverLock(privconn);
C
Cole Robinson 已提交
3938
    memset(names, 0, sizeof(*names)*nnames);
3939 3940
    for (i = 0 ; i < privconn->pools.count && n < nnames ; i++) {
        virStoragePoolObjLock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
3941
        if (!virStoragePoolObjIsActive(privconn->pools.objs[i]) &&
3942 3943
            !(names[n++] = strdup(privconn->pools.objs[i]->def->name))) {
            virStoragePoolObjUnlock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
3944
            goto no_memory;
3945 3946 3947 3948
        }
        virStoragePoolObjUnlock(privconn->pools.objs[i]);
    }
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
3949 3950 3951 3952

    return n;

no_memory:
3953
    virReportOOMError();
C
Cole Robinson 已提交
3954 3955
    for (n = 0 ; n < nnames ; n++)
        VIR_FREE(names[n]);
3956
    testDriverUnlock(privconn);
3957
    return -1;
C
Cole Robinson 已提交
3958 3959 3960
}


3961 3962 3963 3964 3965 3966 3967 3968 3969 3970
static int testStoragePoolIsActive(virStoragePoolPtr pool)
{
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr obj;
    int ret = -1;

    testDriverLock(privconn);
    obj = virStoragePoolObjFindByUUID(&privconn->pools, pool->uuid);
    testDriverUnlock(privconn);
    if (!obj) {
3971
        testError(VIR_ERR_NO_STORAGE_POOL, NULL);
3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991
        goto cleanup;
    }
    ret = virStoragePoolObjIsActive(obj);

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

static int testStoragePoolIsPersistent(virStoragePoolPtr pool)
{
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr obj;
    int ret = -1;

    testDriverLock(privconn);
    obj = virStoragePoolObjFindByUUID(&privconn->pools, pool->uuid);
    testDriverUnlock(privconn);
    if (!obj) {
3992
        testError(VIR_ERR_NO_STORAGE_POOL, NULL);
3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004
        goto cleanup;
    }
    ret = obj->configFile ? 1 : 0;

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



C
Cole Robinson 已提交
4005
static int
4006
testStoragePoolStart(virStoragePoolPtr pool,
E
Eric Blake 已提交
4007 4008
                     unsigned int flags)
{
4009 4010
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4011
    int ret = -1;
4012

E
Eric Blake 已提交
4013 4014
    virCheckFlags(0, -1);

4015
    testDriverLock(privconn);
4016 4017
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4018
    testDriverUnlock(privconn);
4019 4020

    if (privpool == NULL) {
4021
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4022
        goto cleanup;
4023 4024
    }

4025
    if (virStoragePoolObjIsActive(privpool)) {
4026
        testError(VIR_ERR_OPERATION_INVALID,
4027 4028 4029
                  _("storage pool '%s' is already active"), pool->name);
        goto cleanup;
    }
C
Cole Robinson 已提交
4030 4031

    privpool->active = 1;
4032
    ret = 0;
C
Cole Robinson 已提交
4033

4034
cleanup:
4035 4036
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4037
    return ret;
C
Cole Robinson 已提交
4038 4039 4040
}

static char *
4041
testStorageFindPoolSources(virConnectPtr conn ATTRIBUTE_UNUSED,
4042 4043
                           const char *type,
                           const char *srcSpec,
E
Eric Blake 已提交
4044
                           unsigned int flags)
C
Cole Robinson 已提交
4045
{
4046 4047 4048 4049
    virStoragePoolSourcePtr source = NULL;
    int pool_type;
    char *ret = NULL;

E
Eric Blake 已提交
4050 4051
    virCheckFlags(0, NULL);

4052 4053
    pool_type = virStoragePoolTypeFromString(type);
    if (!pool_type) {
4054
        testError(VIR_ERR_INTERNAL_ERROR,
4055 4056 4057 4058 4059
                  _("unknown storage pool type %s"), type);
        goto cleanup;
    }

    if (srcSpec) {
4060
        source = virStoragePoolDefParseSourceString(srcSpec, pool_type);
4061 4062 4063 4064 4065 4066 4067 4068 4069
        if (!source)
            goto cleanup;
    }

    switch (pool_type) {

    case VIR_STORAGE_POOL_LOGICAL:
        ret = strdup(defaultPoolSourcesLogicalXML);
        if (!ret)
4070
            virReportOOMError();
4071 4072 4073 4074
        break;

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

        if (virAsprintf(&ret, defaultPoolSourcesNetFSXML,
                        source->host.name) < 0)
4082
            virReportOOMError();
4083 4084 4085
        break;

    default:
4086
        testError(VIR_ERR_NO_SUPPORT,
4087 4088 4089 4090 4091 4092
                  _("pool type '%s' does not support source discovery"), type);
    }

cleanup:
    virStoragePoolSourceFree(source);
    return ret;
C
Cole Robinson 已提交
4093 4094 4095 4096 4097 4098
}


static virStoragePoolPtr
testStoragePoolCreate(virConnectPtr conn,
                      const char *xml,
E
Eric Blake 已提交
4099 4100
                      unsigned int flags)
{
4101
    testConnPtr privconn = conn->privateData;
C
Cole Robinson 已提交
4102
    virStoragePoolDefPtr def;
4103
    virStoragePoolObjPtr pool = NULL;
4104
    virStoragePoolPtr ret = NULL;
C
Cole Robinson 已提交
4105

E
Eric Blake 已提交
4106 4107
    virCheckFlags(0, NULL);

4108
    testDriverLock(privconn);
4109
    if (!(def = virStoragePoolDefParseString(xml)))
4110
        goto cleanup;
C
Cole Robinson 已提交
4111

4112 4113 4114 4115
    pool = virStoragePoolObjFindByUUID(&privconn->pools, def->uuid);
    if (!pool)
        pool = virStoragePoolObjFindByName(&privconn->pools, def->name);
    if (pool) {
4116
        testError(VIR_ERR_INTERNAL_ERROR,
C
Cole Robinson 已提交
4117
                  "%s", _("storage pool already exists"));
4118
        goto cleanup;
C
Cole Robinson 已提交
4119 4120
    }

4121
    if (!(pool = virStoragePoolObjAssignDef(&privconn->pools, def)))
4122
        goto cleanup;
4123
    def = NULL;
C
Cole Robinson 已提交
4124

4125
    if (testStoragePoolObjSetDefaults(pool) == -1) {
C
Cole Robinson 已提交
4126
        virStoragePoolObjRemove(&privconn->pools, pool);
4127 4128
        pool = NULL;
        goto cleanup;
C
Cole Robinson 已提交
4129 4130 4131
    }
    pool->active = 1;

4132 4133 4134 4135
    ret = virGetStoragePool(conn, pool->def->name, pool->def->uuid);

cleanup:
    virStoragePoolDefFree(def);
4136 4137 4138
    if (pool)
        virStoragePoolObjUnlock(pool);
    testDriverUnlock(privconn);
4139
    return ret;
C
Cole Robinson 已提交
4140 4141 4142 4143 4144
}

static virStoragePoolPtr
testStoragePoolDefine(virConnectPtr conn,
                      const char *xml,
E
Eric Blake 已提交
4145 4146
                      unsigned int flags)
{
4147
    testConnPtr privconn = conn->privateData;
C
Cole Robinson 已提交
4148
    virStoragePoolDefPtr def;
4149
    virStoragePoolObjPtr pool = NULL;
4150
    virStoragePoolPtr ret = NULL;
C
Cole Robinson 已提交
4151

E
Eric Blake 已提交
4152 4153
    virCheckFlags(0, NULL);

4154
    testDriverLock(privconn);
4155
    if (!(def = virStoragePoolDefParseString(xml)))
4156
        goto cleanup;
C
Cole Robinson 已提交
4157 4158 4159 4160 4161

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

4162
    if (!(pool = virStoragePoolObjAssignDef(&privconn->pools, def)))
4163 4164
        goto cleanup;
    def = NULL;
C
Cole Robinson 已提交
4165

4166
    if (testStoragePoolObjSetDefaults(pool) == -1) {
C
Cole Robinson 已提交
4167
        virStoragePoolObjRemove(&privconn->pools, pool);
4168 4169
        pool = NULL;
        goto cleanup;
C
Cole Robinson 已提交
4170 4171
    }

4172 4173 4174 4175
    ret = virGetStoragePool(conn, pool->def->name, pool->def->uuid);

cleanup:
    virStoragePoolDefFree(def);
4176 4177 4178
    if (pool)
        virStoragePoolObjUnlock(pool);
    testDriverUnlock(privconn);
4179
    return ret;
C
Cole Robinson 已提交
4180 4181 4182
}

static int
4183 4184 4185
testStoragePoolUndefine(virStoragePoolPtr pool) {
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4186
    int ret = -1;
4187

4188
    testDriverLock(privconn);
4189 4190 4191 4192
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);

    if (privpool == NULL) {
4193
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4194
        goto cleanup;
4195 4196
    }

4197
    if (virStoragePoolObjIsActive(privpool)) {
4198
        testError(VIR_ERR_OPERATION_INVALID,
4199 4200 4201
                  _("storage pool '%s' is already active"), pool->name);
        goto cleanup;
    }
C
Cole Robinson 已提交
4202 4203

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

4206
cleanup:
4207 4208 4209
    if (privpool)
        virStoragePoolObjUnlock(privpool);
    testDriverUnlock(privconn);
4210
    return ret;
C
Cole Robinson 已提交
4211 4212 4213
}

static int
4214
testStoragePoolBuild(virStoragePoolPtr pool,
E
Eric Blake 已提交
4215 4216
                     unsigned int flags)
{
4217 4218
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4219
    int ret = -1;
4220

E
Eric Blake 已提交
4221 4222
    virCheckFlags(0, -1);

4223
    testDriverLock(privconn);
4224 4225
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4226
    testDriverUnlock(privconn);
4227 4228

    if (privpool == NULL) {
4229
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4230
        goto cleanup;
4231 4232
    }

4233
    if (virStoragePoolObjIsActive(privpool)) {
4234
        testError(VIR_ERR_OPERATION_INVALID,
4235 4236 4237
                  _("storage pool '%s' is already active"), pool->name);
        goto cleanup;
    }
4238
    ret = 0;
C
Cole Robinson 已提交
4239

4240
cleanup:
4241 4242
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4243
    return ret;
C
Cole Robinson 已提交
4244 4245 4246 4247
}


static int
4248 4249 4250
testStoragePoolDestroy(virStoragePoolPtr pool) {
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4251
    int ret = -1;
4252

4253
    testDriverLock(privconn);
4254 4255 4256 4257
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);

    if (privpool == NULL) {
4258
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4259
        goto cleanup;
4260 4261 4262
    }

    if (!virStoragePoolObjIsActive(privpool)) {
4263
        testError(VIR_ERR_OPERATION_INVALID,
4264
                  _("storage pool '%s' is not active"), pool->name);
4265
        goto cleanup;
4266
    }
C
Cole Robinson 已提交
4267 4268 4269

    privpool->active = 0;

4270
    if (privpool->configFile == NULL) {
C
Cole Robinson 已提交
4271
        virStoragePoolObjRemove(&privconn->pools, privpool);
4272 4273
        privpool = NULL;
    }
4274
    ret = 0;
C
Cole Robinson 已提交
4275

4276
cleanup:
4277 4278 4279
    if (privpool)
        virStoragePoolObjUnlock(privpool);
    testDriverUnlock(privconn);
4280
    return ret;
C
Cole Robinson 已提交
4281 4282 4283 4284
}


static int
4285
testStoragePoolDelete(virStoragePoolPtr pool,
E
Eric Blake 已提交
4286 4287
                      unsigned int flags)
{
4288 4289
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4290
    int ret = -1;
4291

E
Eric Blake 已提交
4292 4293
    virCheckFlags(0, -1);

4294
    testDriverLock(privconn);
4295 4296
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4297
    testDriverUnlock(privconn);
4298 4299

    if (privpool == NULL) {
4300
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4301 4302 4303 4304
        goto cleanup;
    }

    if (virStoragePoolObjIsActive(privpool)) {
4305
        testError(VIR_ERR_OPERATION_INVALID,
4306 4307
                  _("storage pool '%s' is already active"), pool->name);
        goto cleanup;
4308 4309
    }

4310
    ret = 0;
C
Cole Robinson 已提交
4311

4312
cleanup:
4313 4314
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4315
    return ret;
C
Cole Robinson 已提交
4316 4317 4318 4319
}


static int
4320
testStoragePoolRefresh(virStoragePoolPtr pool,
E
Eric Blake 已提交
4321 4322
                       unsigned int flags)
{
4323 4324
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4325
    int ret = -1;
4326

E
Eric Blake 已提交
4327 4328
    virCheckFlags(0, -1);

4329
    testDriverLock(privconn);
4330 4331
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4332
    testDriverUnlock(privconn);
4333 4334

    if (privpool == NULL) {
4335
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4336
        goto cleanup;
4337 4338 4339
    }

    if (!virStoragePoolObjIsActive(privpool)) {
4340
        testError(VIR_ERR_OPERATION_INVALID,
4341
                  _("storage pool '%s' is not active"), pool->name);
4342
        goto cleanup;
4343
    }
4344
    ret = 0;
C
Cole Robinson 已提交
4345

4346
cleanup:
4347 4348
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4349
    return ret;
C
Cole Robinson 已提交
4350 4351 4352 4353
}


static int
4354
testStoragePoolGetInfo(virStoragePoolPtr pool,
C
Cole Robinson 已提交
4355
                       virStoragePoolInfoPtr info) {
4356 4357
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4358
    int ret = -1;
4359

4360
    testDriverLock(privconn);
4361 4362
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4363
    testDriverUnlock(privconn);
4364 4365

    if (privpool == NULL) {
4366
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4367
        goto cleanup;
4368
    }
C
Cole Robinson 已提交
4369 4370 4371 4372 4373 4374 4375 4376 4377

    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;
4378
    ret = 0;
C
Cole Robinson 已提交
4379

4380
cleanup:
4381 4382
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4383
    return ret;
C
Cole Robinson 已提交
4384 4385 4386
}

static char *
4387
testStoragePoolGetXMLDesc(virStoragePoolPtr pool,
E
Eric Blake 已提交
4388 4389
                          unsigned int flags)
{
4390 4391
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4392
    char *ret = NULL;
4393

E
Eric Blake 已提交
4394 4395
    virCheckFlags(0, NULL);

4396
    testDriverLock(privconn);
4397 4398
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4399
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
4400

4401
    if (privpool == NULL) {
4402
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4403
        goto cleanup;
4404 4405
    }

4406
    ret = virStoragePoolDefFormat(privpool->def);
4407 4408

cleanup:
4409 4410
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4411
    return ret;
C
Cole Robinson 已提交
4412 4413 4414
}

static int
4415
testStoragePoolGetAutostart(virStoragePoolPtr pool,
C
Cole Robinson 已提交
4416
                            int *autostart) {
4417 4418
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4419
    int ret = -1;
4420

4421
    testDriverLock(privconn);
4422 4423
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4424
    testDriverUnlock(privconn);
4425 4426

    if (privpool == NULL) {
4427
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4428
        goto cleanup;
4429
    }
C
Cole Robinson 已提交
4430 4431 4432 4433 4434 4435

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

4438
cleanup:
4439 4440
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4441
    return ret;
C
Cole Robinson 已提交
4442 4443 4444
}

static int
4445
testStoragePoolSetAutostart(virStoragePoolPtr pool,
C
Cole Robinson 已提交
4446
                            int autostart) {
4447 4448
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4449
    int ret = -1;
4450

4451
    testDriverLock(privconn);
4452 4453
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4454
    testDriverUnlock(privconn);
4455 4456

    if (privpool == NULL) {
4457
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4458
        goto cleanup;
4459
    }
C
Cole Robinson 已提交
4460 4461

    if (!privpool->configFile) {
4462
        testError(VIR_ERR_INVALID_ARG,
C
Cole Robinson 已提交
4463
                  "%s", _("pool has no config file"));
4464
        goto cleanup;
C
Cole Robinson 已提交
4465 4466 4467 4468
    }

    autostart = (autostart != 0);
    privpool->autostart = autostart;
4469 4470 4471
    ret = 0;

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


static int
4479 4480 4481
testStoragePoolNumVolumes(virStoragePoolPtr pool) {
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4482
    int ret = -1;
4483

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

    if (privpool == NULL) {
4490
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4491
        goto cleanup;
4492 4493 4494
    }

    if (!virStoragePoolObjIsActive(privpool)) {
4495
        testError(VIR_ERR_OPERATION_INVALID,
4496
                  _("storage pool '%s' is not active"), pool->name);
4497
        goto cleanup;
4498
    }
C
Cole Robinson 已提交
4499

4500 4501 4502
    ret = privpool->volumes.count;

cleanup:
4503 4504
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4505
    return ret;
C
Cole Robinson 已提交
4506 4507 4508
}

static int
4509
testStoragePoolListVolumes(virStoragePoolPtr pool,
C
Cole Robinson 已提交
4510 4511
                           char **const names,
                           int maxnames) {
4512 4513
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
C
Cole Robinson 已提交
4514 4515
    int i = 0, n = 0;

4516
    memset(names, 0, maxnames * sizeof(*names));
4517 4518

    testDriverLock(privconn);
4519 4520
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4521
    testDriverUnlock(privconn);
4522 4523

    if (privpool == NULL) {
4524
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4525
        goto cleanup;
4526 4527 4528 4529
    }


    if (!virStoragePoolObjIsActive(privpool)) {
4530
        testError(VIR_ERR_OPERATION_INVALID,
4531
                  _("storage pool '%s' is not active"), pool->name);
4532
        goto cleanup;
4533 4534
    }

C
Cole Robinson 已提交
4535 4536
    for (i = 0 ; i < privpool->volumes.count && n < maxnames ; i++) {
        if ((names[n++] = strdup(privpool->volumes.objs[i]->name)) == NULL) {
4537
            virReportOOMError();
C
Cole Robinson 已提交
4538 4539 4540 4541
            goto cleanup;
        }
    }

4542
    virStoragePoolObjUnlock(privpool);
C
Cole Robinson 已提交
4543 4544 4545 4546 4547 4548
    return n;

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

4549
    memset(names, 0, maxnames * sizeof(*names));
4550 4551
    if (privpool)
        virStoragePoolObjUnlock(privpool);
C
Cole Robinson 已提交
4552 4553 4554 4555 4556
    return -1;
}


static virStorageVolPtr
4557
testStorageVolumeLookupByName(virStoragePoolPtr pool,
C
Cole Robinson 已提交
4558
                              const char *name ATTRIBUTE_UNUSED) {
4559 4560 4561
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol;
4562
    virStorageVolPtr ret = NULL;
4563

4564
    testDriverLock(privconn);
4565 4566
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4567
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
4568

4569
    if (privpool == NULL) {
4570
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4571
        goto cleanup;
4572 4573 4574 4575
    }


    if (!virStoragePoolObjIsActive(privpool)) {
4576
        testError(VIR_ERR_OPERATION_INVALID,
4577
                  _("storage pool '%s' is not active"), pool->name);
4578
        goto cleanup;
4579 4580 4581 4582 4583
    }

    privvol = virStorageVolDefFindByName(privpool, name);

    if (!privvol) {
4584
        testError(VIR_ERR_NO_STORAGE_VOL,
C
Cole Robinson 已提交
4585
                  _("no storage vol with matching name '%s'"), name);
4586
        goto cleanup;
C
Cole Robinson 已提交
4587 4588
    }

4589 4590 4591 4592
    ret = virGetStorageVol(pool->conn, privpool->def->name,
                           privvol->name, privvol->key);

cleanup:
4593 4594
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4595
    return ret;
C
Cole Robinson 已提交
4596 4597 4598 4599 4600 4601
}


static virStorageVolPtr
testStorageVolumeLookupByKey(virConnectPtr conn,
                             const char *key) {
4602
    testConnPtr privconn = conn->privateData;
C
Cole Robinson 已提交
4603
    unsigned int i;
4604
    virStorageVolPtr ret = NULL;
C
Cole Robinson 已提交
4605

4606
    testDriverLock(privconn);
C
Cole Robinson 已提交
4607
    for (i = 0 ; i < privconn->pools.count ; i++) {
4608
        virStoragePoolObjLock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
4609
        if (virStoragePoolObjIsActive(privconn->pools.objs[i])) {
4610
            virStorageVolDefPtr privvol =
C
Cole Robinson 已提交
4611 4612
                virStorageVolDefFindByKey(privconn->pools.objs[i], key);

4613 4614 4615 4616 4617
            if (privvol) {
                ret = virGetStorageVol(conn,
                                       privconn->pools.objs[i]->def->name,
                                       privvol->name,
                                       privvol->key);
4618
                virStoragePoolObjUnlock(privconn->pools.objs[i]);
4619 4620
                break;
            }
C
Cole Robinson 已提交
4621
        }
4622
        virStoragePoolObjUnlock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
4623
    }
4624
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
4625

4626
    if (!ret)
4627
        testError(VIR_ERR_NO_STORAGE_VOL,
4628 4629 4630
                  _("no storage vol with matching key '%s'"), key);

    return ret;
C
Cole Robinson 已提交
4631 4632 4633 4634 4635
}

static virStorageVolPtr
testStorageVolumeLookupByPath(virConnectPtr conn,
                              const char *path) {
4636
    testConnPtr privconn = conn->privateData;
C
Cole Robinson 已提交
4637
    unsigned int i;
4638
    virStorageVolPtr ret = NULL;
C
Cole Robinson 已提交
4639

4640
    testDriverLock(privconn);
C
Cole Robinson 已提交
4641
    for (i = 0 ; i < privconn->pools.count ; i++) {
4642
        virStoragePoolObjLock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
4643
        if (virStoragePoolObjIsActive(privconn->pools.objs[i])) {
4644
            virStorageVolDefPtr privvol =
C
Cole Robinson 已提交
4645 4646
                virStorageVolDefFindByPath(privconn->pools.objs[i], path);

4647 4648 4649 4650 4651
            if (privvol) {
                ret = virGetStorageVol(conn,
                                       privconn->pools.objs[i]->def->name,
                                       privvol->name,
                                       privvol->key);
4652
                virStoragePoolObjUnlock(privconn->pools.objs[i]);
4653 4654
                break;
            }
C
Cole Robinson 已提交
4655
        }
4656
        virStoragePoolObjUnlock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
4657
    }
4658
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
4659

4660
    if (!ret)
4661
        testError(VIR_ERR_NO_STORAGE_VOL,
4662 4663 4664
                  _("no storage vol with matching path '%s'"), path);

    return ret;
C
Cole Robinson 已提交
4665 4666 4667
}

static virStorageVolPtr
4668
testStorageVolumeCreateXML(virStoragePoolPtr pool,
C
Cole Robinson 已提交
4669
                           const char *xmldesc,
E
Eric Blake 已提交
4670 4671
                           unsigned int flags)
{
4672 4673
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4674 4675
    virStorageVolDefPtr privvol = NULL;
    virStorageVolPtr ret = NULL;
4676

E
Eric Blake 已提交
4677 4678
    virCheckFlags(0, NULL);

4679
    testDriverLock(privconn);
4680 4681
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4682
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
4683

4684
    if (privpool == NULL) {
4685
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4686
        goto cleanup;
4687 4688 4689
    }

    if (!virStoragePoolObjIsActive(privpool)) {
4690
        testError(VIR_ERR_OPERATION_INVALID,
4691
                  _("storage pool '%s' is not active"), pool->name);
4692
        goto cleanup;
4693
    }
C
Cole Robinson 已提交
4694

4695
    privvol = virStorageVolDefParseString(privpool->def, xmldesc);
4696
    if (privvol == NULL)
4697
        goto cleanup;
4698 4699

    if (virStorageVolDefFindByName(privpool, privvol->name)) {
4700
        testError(VIR_ERR_OPERATION_FAILED,
C
Cole Robinson 已提交
4701
                  "%s", _("storage vol already exists"));
4702
        goto cleanup;
C
Cole Robinson 已提交
4703 4704 4705
    }

    /* Make sure enough space */
4706
    if ((privpool->def->allocation + privvol->allocation) >
C
Cole Robinson 已提交
4707
         privpool->def->capacity) {
4708
        testError(VIR_ERR_INTERNAL_ERROR,
C
Cole Robinson 已提交
4709
                  _("Not enough free space in pool for volume '%s'"),
4710
                  privvol->name);
4711
        goto cleanup;
C
Cole Robinson 已提交
4712 4713 4714 4715
    }

    if (VIR_REALLOC_N(privpool->volumes.objs,
                      privpool->volumes.count+1) < 0) {
4716
        virReportOOMError();
4717
        goto cleanup;
C
Cole Robinson 已提交
4718 4719
    }

4720 4721 4722
    if (virAsprintf(&privvol->target.path, "%s/%s",
                    privpool->def->target.path,
                    privvol->name) == -1) {
4723
        virReportOOMError();
4724
        goto cleanup;
C
Cole Robinson 已提交
4725 4726
    }

4727 4728
    privvol->key = strdup(privvol->target.path);
    if (privvol->key == NULL) {
4729
        virReportOOMError();
4730
        goto cleanup;
C
Cole Robinson 已提交
4731 4732
    }

4733
    privpool->def->allocation += privvol->allocation;
C
Cole Robinson 已提交
4734 4735 4736
    privpool->def->available = (privpool->def->capacity -
                                privpool->def->allocation);

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

4739 4740
    ret = virGetStorageVol(pool->conn, privpool->def->name,
                           privvol->name, privvol->key);
4741
    privvol = NULL;
4742 4743 4744

cleanup:
    virStorageVolDefFree(privvol);
4745 4746
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4747
    return ret;
C
Cole Robinson 已提交
4748 4749
}

4750 4751 4752 4753
static virStorageVolPtr
testStorageVolumeCreateXMLFrom(virStoragePoolPtr pool,
                               const char *xmldesc,
                               virStorageVolPtr clonevol,
E
Eric Blake 已提交
4754 4755
                               unsigned int flags)
{
4756 4757 4758 4759 4760
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol = NULL, origvol = NULL;
    virStorageVolPtr ret = NULL;

E
Eric Blake 已提交
4761 4762
    virCheckFlags(0, NULL);

4763 4764 4765 4766 4767 4768
    testDriverLock(privconn);
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
    testDriverUnlock(privconn);

    if (privpool == NULL) {
4769
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4770 4771 4772 4773
        goto cleanup;
    }

    if (!virStoragePoolObjIsActive(privpool)) {
4774
        testError(VIR_ERR_OPERATION_INVALID,
4775 4776 4777 4778
                  _("storage pool '%s' is not active"), pool->name);
        goto cleanup;
    }

4779
    privvol = virStorageVolDefParseString(privpool->def, xmldesc);
4780 4781 4782 4783
    if (privvol == NULL)
        goto cleanup;

    if (virStorageVolDefFindByName(privpool, privvol->name)) {
4784
        testError(VIR_ERR_OPERATION_FAILED,
4785 4786 4787 4788 4789 4790
                  "%s", _("storage vol already exists"));
        goto cleanup;
    }

    origvol = virStorageVolDefFindByName(privpool, clonevol->name);
    if (!origvol) {
4791
        testError(VIR_ERR_NO_STORAGE_VOL,
4792 4793 4794 4795 4796 4797 4798 4799
                  _("no storage vol with matching name '%s'"),
                  clonevol->name);
        goto cleanup;
    }

    /* Make sure enough space */
    if ((privpool->def->allocation + privvol->allocation) >
         privpool->def->capacity) {
4800
        testError(VIR_ERR_INTERNAL_ERROR,
4801 4802 4803 4804 4805 4806 4807 4808 4809
                  _("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) {
4810
        virReportOOMError();
4811 4812 4813
        goto cleanup;
    }

4814 4815 4816
    if (virAsprintf(&privvol->target.path, "%s/%s",
                    privpool->def->target.path,
                    privvol->name) == -1) {
4817
        virReportOOMError();
4818 4819 4820 4821 4822
        goto cleanup;
    }

    privvol->key = strdup(privvol->target.path);
    if (privvol->key == NULL) {
4823
        virReportOOMError();
4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843
        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 已提交
4844
static int
4845
testStorageVolumeDelete(virStorageVolPtr vol,
E
Eric Blake 已提交
4846 4847
                        unsigned int flags)
{
4848 4849 4850
    testConnPtr privconn = vol->conn->privateData;
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol;
C
Cole Robinson 已提交
4851
    int i;
4852
    int ret = -1;
C
Cole Robinson 已提交
4853

E
Eric Blake 已提交
4854 4855
    virCheckFlags(0, -1);

4856
    testDriverLock(privconn);
4857 4858
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           vol->pool);
4859
    testDriverUnlock(privconn);
4860 4861

    if (privpool == NULL) {
4862
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4863
        goto cleanup;
4864 4865 4866 4867 4868 4869
    }


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

    if (privvol == NULL) {
4870
        testError(VIR_ERR_NO_STORAGE_VOL,
4871 4872
                  _("no storage vol with matching name '%s'"),
                  vol->name);
4873
        goto cleanup;
4874 4875 4876
    }

    if (!virStoragePoolObjIsActive(privpool)) {
4877
        testError(VIR_ERR_OPERATION_INVALID,
4878
                  _("storage pool '%s' is not active"), vol->pool);
4879
        goto cleanup;
4880 4881 4882
    }


C
Cole Robinson 已提交
4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905
    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;
        }
    }
4906
    ret = 0;
C
Cole Robinson 已提交
4907

4908
cleanup:
4909 4910
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4911
    return ret;
C
Cole Robinson 已提交
4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927
}


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
4928
testStorageVolumeGetInfo(virStorageVolPtr vol,
C
Cole Robinson 已提交
4929
                         virStorageVolInfoPtr info) {
4930 4931 4932
    testConnPtr privconn = vol->conn->privateData;
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol;
4933
    int ret = -1;
4934

4935
    testDriverLock(privconn);
4936 4937
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           vol->pool);
4938
    testDriverUnlock(privconn);
4939 4940

    if (privpool == NULL) {
4941
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4942
        goto cleanup;
4943 4944 4945 4946 4947
    }

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

    if (privvol == NULL) {
4948
        testError(VIR_ERR_NO_STORAGE_VOL,
4949 4950
                  _("no storage vol with matching name '%s'"),
                  vol->name);
4951
        goto cleanup;
4952 4953 4954
    }

    if (!virStoragePoolObjIsActive(privpool)) {
4955
        testError(VIR_ERR_OPERATION_INVALID,
4956
                  _("storage pool '%s' is not active"), vol->pool);
4957
        goto cleanup;
4958
    }
C
Cole Robinson 已提交
4959 4960 4961 4962 4963

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

4966
cleanup:
4967 4968
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4969
    return ret;
C
Cole Robinson 已提交
4970 4971 4972
}

static char *
4973
testStorageVolumeGetXMLDesc(virStorageVolPtr vol,
E
Eric Blake 已提交
4974 4975
                            unsigned int flags)
{
4976 4977 4978
    testConnPtr privconn = vol->conn->privateData;
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol;
4979
    char *ret = NULL;
4980

E
Eric Blake 已提交
4981 4982
    virCheckFlags(0, NULL);

4983
    testDriverLock(privconn);
4984 4985
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           vol->pool);
4986
    testDriverUnlock(privconn);
4987 4988

    if (privpool == NULL) {
4989
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4990
        goto cleanup;
4991 4992 4993 4994 4995
    }

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

    if (privvol == NULL) {
4996
        testError(VIR_ERR_NO_STORAGE_VOL,
4997 4998
                  _("no storage vol with matching name '%s'"),
                  vol->name);
4999
        goto cleanup;
5000
    }
C
Cole Robinson 已提交
5001

5002
    if (!virStoragePoolObjIsActive(privpool)) {
5003
        testError(VIR_ERR_OPERATION_INVALID,
5004
                  _("storage pool '%s' is not active"), vol->pool);
5005
        goto cleanup;
5006 5007
    }

5008
    ret = virStorageVolDefFormat(privpool->def, privvol);
5009 5010

cleanup:
5011 5012
    if (privpool)
        virStoragePoolObjUnlock(privpool);
5013
    return ret;
C
Cole Robinson 已提交
5014 5015 5016
}

static char *
5017 5018 5019 5020
testStorageVolumeGetPath(virStorageVolPtr vol) {
    testConnPtr privconn = vol->conn->privateData;
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol;
5021
    char *ret = NULL;
5022

5023
    testDriverLock(privconn);
5024 5025
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           vol->pool);
5026
    testDriverUnlock(privconn);
5027 5028

    if (privpool == NULL) {
5029
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
5030
        goto cleanup;
5031 5032 5033 5034 5035
    }

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

    if (privvol == NULL) {
5036
        testError(VIR_ERR_NO_STORAGE_VOL,
5037 5038
                  _("no storage vol with matching name '%s'"),
                  vol->name);
5039
        goto cleanup;
5040 5041 5042
    }

    if (!virStoragePoolObjIsActive(privpool)) {
5043
        testError(VIR_ERR_OPERATION_INVALID,
5044
                  _("storage pool '%s' is not active"), vol->pool);
5045
        goto cleanup;
5046 5047
    }

C
Cole Robinson 已提交
5048
    ret = strdup(privvol->target.path);
5049
    if (ret == NULL)
5050
        virReportOOMError();
5051 5052

cleanup:
5053 5054
    if (privpool)
        virStoragePoolObjUnlock(privpool);
C
Cole Robinson 已提交
5055 5056 5057
    return ret;
}

5058

5059
/* Node device implementations */
5060 5061
static virDrvOpenStatus testDevMonOpen(virConnectPtr conn,
                                       virConnectAuthPtr auth ATTRIBUTE_UNUSED,
E
Eric Blake 已提交
5062 5063 5064 5065
                                       unsigned int flags)
{
    virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);

5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077
    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;
}

5078 5079 5080
static int
testNodeNumOfDevices(virConnectPtr conn,
                     const char *cap,
E
Eric Blake 已提交
5081
                     unsigned int flags)
5082 5083 5084 5085 5086
{
    testConnPtr driver = conn->privateData;
    int ndevs = 0;
    unsigned int i;

E
Eric Blake 已提交
5087 5088
    virCheckFlags(0, -1);

5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103
    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,
E
Eric Blake 已提交
5104
                    unsigned int flags)
5105 5106 5107 5108 5109
{
    testConnPtr driver = conn->privateData;
    int ndevs = 0;
    unsigned int i;

E
Eric Blake 已提交
5110 5111
    virCheckFlags(0, -1);

5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147
    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) {
5148
        virNodeDeviceReportError(VIR_ERR_NO_NODE_DEVICE, NULL);
5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160
        goto cleanup;
    }

    ret = virGetNodeDevice(conn, name);

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

static char *
5161
testNodeDeviceGetXMLDesc(virNodeDevicePtr dev,
E
Eric Blake 已提交
5162
                         unsigned int flags)
5163 5164 5165 5166 5167
{
    testConnPtr driver = dev->conn->privateData;
    virNodeDeviceObjPtr obj;
    char *ret = NULL;

E
Eric Blake 已提交
5168 5169
    virCheckFlags(0, NULL);

5170 5171 5172 5173 5174
    testDriverLock(driver);
    obj = virNodeDeviceFindByName(&driver->devs, dev->name);
    testDriverUnlock(driver);

    if (!obj) {
5175 5176
        virNodeDeviceReportError(VIR_ERR_NO_NODE_DEVICE,
                                 _("no node device with matching name '%s'"),
5177 5178 5179 5180
                                 dev->name);
        goto cleanup;
    }

5181
    ret = virNodeDeviceDefFormat(obj->def);
5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200

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) {
5201
        virNodeDeviceReportError(VIR_ERR_NO_NODE_DEVICE,
5202 5203 5204 5205 5206 5207 5208 5209
                                _("no node device with matching name '%s'"),
                                 dev->name);
        goto cleanup;
    }

    if (obj->def->parent) {
        ret = strdup(obj->def->parent);
        if (!ret)
5210
            virReportOOMError();
5211
    } else {
5212
        virNodeDeviceReportError(VIR_ERR_INTERNAL_ERROR,
5213 5214 5215 5216 5217 5218 5219 5220 5221
                                 "%s", _("no parent for this device"));
    }

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

5222

5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236
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) {
5237 5238
        virNodeDeviceReportError(VIR_ERR_NO_NODE_DEVICE,
                                 _("no node device with matching name '%s'"),
5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267
                                 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) {
5268
        virNodeDeviceReportError(VIR_ERR_NO_NODE_DEVICE,
5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291
                                _("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;
}

5292 5293 5294
static virNodeDevicePtr
testNodeDeviceCreateXML(virConnectPtr conn,
                        const char *xmlDesc,
E
Eric Blake 已提交
5295
                        unsigned int flags)
5296 5297 5298 5299 5300 5301 5302 5303 5304
{
    testConnPtr driver = conn->privateData;
    virNodeDeviceDefPtr def = NULL;
    virNodeDeviceObjPtr obj = NULL;
    char *wwnn = NULL, *wwpn = NULL;
    int parent_host = -1;
    virNodeDevicePtr dev = NULL;
    virNodeDevCapsDefPtr caps;

E
Eric Blake 已提交
5305 5306
    virCheckFlags(0, NULL);

5307 5308
    testDriverLock(driver);

5309
    def = virNodeDeviceDefParseString(xmlDesc, CREATE_DEVICE);
5310 5311 5312 5313 5314
    if (def == NULL) {
        goto cleanup;
    }

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

5319
    if (virNodeDeviceGetParentHost(&driver->devs,
5320 5321 5322 5323 5324 5325 5326 5327 5328 5329
                                   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))) {
5330
        virReportOOMError();
5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345
        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;
    }


5346
    if (!(obj = virNodeDeviceAssignDef(&driver->devs, def))) {
5347 5348 5349 5350 5351 5352 5353 5354
        goto cleanup;
    }
    virNodeDeviceObjUnlock(obj);

    dev = virGetNodeDevice(conn, def->name);
    def = NULL;
cleanup:
    testDriverUnlock(driver);
5355
    virNodeDeviceDefFree(def);
5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374
    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) {
5375
        virNodeDeviceReportError(VIR_ERR_NO_NODE_DEVICE, NULL);
5376 5377 5378
        goto out;
    }

5379
    if (virNodeDeviceGetWWNs(obj->def, &wwnn, &wwpn) == -1) {
5380 5381 5382 5383 5384
        goto out;
    }

    parent_name = strdup(obj->def->parent);
    if (parent_name == NULL) {
5385
        virReportOOMError();
5386 5387 5388 5389 5390 5391 5392 5393 5394 5395
        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 */
5396
    if (virNodeDeviceGetParentHost(&driver->devs,
5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415
                                   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;
}

5416 5417

/* Domain event implementations */
5418
static int
5419 5420 5421 5422
testDomainEventRegister(virConnectPtr conn,
                        virConnectDomainEventCallback callback,
                        void *opaque,
                        virFreeCallback freecb)
5423 5424 5425 5426 5427
{
    testConnPtr driver = conn->privateData;
    int ret;

    testDriverLock(driver);
5428 5429
    ret = virDomainEventCallbackListAdd(conn,
                                        driver->domainEventState->callbacks,
5430 5431 5432 5433 5434 5435
                                        callback, opaque, freecb);
    testDriverUnlock(driver);

    return ret;
}

5436

5437
static int
5438 5439
testDomainEventDeregister(virConnectPtr conn,
                          virConnectDomainEventCallback callback)
5440 5441 5442 5443 5444
{
    testConnPtr driver = conn->privateData;
    int ret;

    testDriverLock(driver);
5445 5446 5447
    ret = virDomainEventStateDeregister(conn,
                                        driver->domainEventState,
                                        callback);
5448 5449 5450 5451 5452
    testDriverUnlock(driver);

    return ret;
}

5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465

static int
testDomainEventRegisterAny(virConnectPtr conn,
                           virDomainPtr dom,
                           int eventID,
                           virConnectDomainEventGenericCallback callback,
                           void *opaque,
                           virFreeCallback freecb)
{
    testConnPtr driver = conn->privateData;
    int ret;

    testDriverLock(driver);
5466 5467
    ret = virDomainEventCallbackListAddID(conn,
                                          driver->domainEventState->callbacks,
5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482
                                          dom, eventID,
                                          callback, opaque, freecb);
    testDriverUnlock(driver);

    return ret;
}

static int
testDomainEventDeregisterAny(virConnectPtr conn,
                             int callbackID)
{
    testConnPtr driver = conn->privateData;
    int ret;

    testDriverLock(driver);
5483 5484 5485
    ret = virDomainEventStateDeregisterAny(conn,
                                           driver->domainEventState,
                                           callbackID);
5486 5487 5488 5489 5490 5491
    testDriverUnlock(driver);

    return ret;
}


5492 5493
static void testDomainEventDispatchFunc(virConnectPtr conn,
                                        virDomainEventPtr event,
5494
                                        virConnectDomainEventGenericCallback cb,
5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510
                                        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;

    testDriverLock(driver);
5511 5512 5513
    virDomainEventStateFlush(driver->domainEventState,
                             testDomainEventDispatchFunc,
                             driver);
5514 5515 5516 5517 5518 5519 5520 5521
    testDriverUnlock(driver);
}


/* driver must be locked before calling */
static void testDomainEventQueue(testConnPtr driver,
                                 virDomainEventPtr event)
{
5522
    virDomainEventStateQueue(driver->domainEventState, event);
5523 5524
}

5525 5526
static virDrvOpenStatus testSecretOpen(virConnectPtr conn,
                                       virConnectAuthPtr auth ATTRIBUTE_UNUSED,
E
Eric Blake 已提交
5527 5528 5529 5530
                                       unsigned int flags)
{
    virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);

5531 5532 5533
    if (STRNEQ(conn->driver->name, "Test"))
        return VIR_DRV_OPEN_DECLINED;

5534
    conn->secretPrivateData = conn->privateData;
5535 5536 5537 5538 5539 5540 5541
    return VIR_DRV_OPEN_SUCCESS;
}

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

5543 5544 5545

static virDrvOpenStatus testNWFilterOpen(virConnectPtr conn,
                                         virConnectAuthPtr auth ATTRIBUTE_UNUSED,
E
Eric Blake 已提交
5546 5547 5548 5549
                                         unsigned int flags)
{
    virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);

5550 5551 5552
    if (STRNEQ(conn->driver->name, "Test"))
        return VIR_DRV_OPEN_DECLINED;

5553
    conn->nwfilterPrivateData = conn->privateData;
5554 5555 5556 5557 5558 5559 5560 5561
    return VIR_DRV_OPEN_SUCCESS;
}

static int testNWFilterClose(virConnectPtr conn) {
    conn->nwfilterPrivateData = NULL;
    return 0;
}

5562
static virDriver testDriver = {
5563 5564
    .no = VIR_DRV_TEST,
    .name = "Test",
5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589
    .open = testOpen, /* 0.1.1 */
    .close = testClose, /* 0.1.1 */
    .version = testGetVersion, /* 0.1.1 */
    .getHostname = virGetHostname, /* 0.6.3 */
    .getMaxVcpus = testGetMaxVCPUs, /* 0.3.2 */
    .nodeGetInfo = testNodeGetInfo, /* 0.1.1 */
    .getCapabilities = testGetCapabilities, /* 0.2.1 */
    .listDomains = testListDomains, /* 0.1.1 */
    .numOfDomains = testNumOfDomains, /* 0.1.1 */
    .domainCreateXML = testDomainCreateXML, /* 0.1.4 */
    .domainLookupByID = testLookupDomainByID, /* 0.1.1 */
    .domainLookupByUUID = testLookupDomainByUUID, /* 0.1.1 */
    .domainLookupByName = testLookupDomainByName, /* 0.1.1 */
    .domainSuspend = testPauseDomain, /* 0.1.1 */
    .domainResume = testResumeDomain, /* 0.1.1 */
    .domainShutdown = testShutdownDomain, /* 0.1.1 */
    .domainReboot = testRebootDomain, /* 0.1.1 */
    .domainDestroy = testDestroyDomain, /* 0.1.1 */
    .domainGetOSType = testGetOSType, /* 0.1.9 */
    .domainGetMaxMemory = testGetMaxMemory, /* 0.1.4 */
    .domainSetMaxMemory = testSetMaxMemory, /* 0.1.1 */
    .domainSetMemory = testSetMemory, /* 0.1.4 */
    .domainGetInfo = testGetDomainInfo, /* 0.1.1 */
    .domainGetState = testDomainGetState, /* 0.9.2 */
    .domainSave = testDomainSave, /* 0.3.2 */
5590
    .domainSaveFlags = testDomainSaveFlags, /* 0.9.4 */
5591
    .domainRestore = testDomainRestore, /* 0.3.2 */
5592
    .domainRestoreFlags = testDomainRestoreFlags, /* 0.9.4 */
5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606
    .domainCoreDump = testDomainCoreDump, /* 0.3.2 */
    .domainSetVcpus = testSetVcpus, /* 0.1.4 */
    .domainSetVcpusFlags = testDomainSetVcpusFlags, /* 0.8.5 */
    .domainGetVcpusFlags = testDomainGetVcpusFlags, /* 0.8.5 */
    .domainPinVcpu = testDomainPinVcpu, /* 0.7.3 */
    .domainGetVcpus = testDomainGetVcpus, /* 0.7.3 */
    .domainGetMaxVcpus = testDomainGetMaxVcpus, /* 0.7.3 */
    .domainGetXMLDesc = testDomainGetXMLDesc, /* 0.1.4 */
    .listDefinedDomains = testListDefinedDomains, /* 0.1.11 */
    .numOfDefinedDomains = testNumOfDefinedDomains, /* 0.1.11 */
    .domainCreate = testDomainCreate, /* 0.1.11 */
    .domainCreateWithFlags = testDomainCreateWithFlags, /* 0.8.2 */
    .domainDefineXML = testDomainDefineXML, /* 0.1.11 */
    .domainUndefine = testDomainUndefine, /* 0.1.11 */
5607
    .domainUndefineFlags = testDomainUndefineFlags, /* 0.9.4 */
5608 5609 5610 5611
    .domainGetAutostart = testDomainGetAutostart, /* 0.3.2 */
    .domainSetAutostart = testDomainSetAutostart, /* 0.3.2 */
    .domainGetSchedulerType = testDomainGetSchedulerType, /* 0.3.2 */
    .domainGetSchedulerParameters = testDomainGetSchedulerParams, /* 0.3.2 */
5612
    .domainGetSchedulerParametersFlags = testDomainGetSchedulerParamsFlags, /* 0.9.2 */
5613
    .domainSetSchedulerParameters = testDomainSetSchedulerParams, /* 0.3.2 */
5614
    .domainSetSchedulerParametersFlags = testDomainSetSchedulerParamsFlags, /* 0.9.2 */
5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626
    .domainBlockStats = testDomainBlockStats, /* 0.7.0 */
    .domainInterfaceStats = testDomainInterfaceStats, /* 0.7.0 */
    .nodeGetCellsFreeMemory = testNodeGetCellsFreeMemory, /* 0.4.2 */
    .domainEventRegister = testDomainEventRegister, /* 0.6.0 */
    .domainEventDeregister = testDomainEventDeregister, /* 0.6.0 */
    .isEncrypted = testIsEncrypted, /* 0.7.3 */
    .isSecure = testIsSecure, /* 0.7.3 */
    .domainIsActive = testDomainIsActive, /* 0.7.3 */
    .domainIsPersistent = testDomainIsPersistent, /* 0.7.3 */
    .domainIsUpdated = testDomainIsUpdated, /* 0.8.6 */
    .domainEventRegisterAny = testDomainEventRegisterAny, /* 0.8.0 */
    .domainEventDeregisterAny = testDomainEventDeregisterAny, /* 0.8.0 */
5627 5628 5629 5630
};

static virNetworkDriver testNetworkDriver = {
    "Test",
5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649
    .open = testOpenNetwork, /* 0.3.2 */
    .close = testCloseNetwork, /* 0.3.2 */
    .numOfNetworks = testNumNetworks, /* 0.3.2 */
    .listNetworks = testListNetworks, /* 0.3.2 */
    .numOfDefinedNetworks = testNumDefinedNetworks, /* 0.3.2 */
    .listDefinedNetworks = testListDefinedNetworks, /* 0.3.2 */
    .networkLookupByUUID = testLookupNetworkByUUID, /* 0.3.2 */
    .networkLookupByName = testLookupNetworkByName, /* 0.3.2 */
    .networkCreateXML = testNetworkCreate, /* 0.3.2 */
    .networkDefineXML = testNetworkDefine, /* 0.3.2 */
    .networkUndefine = testNetworkUndefine, /* 0.3.2 */
    .networkCreate = testNetworkStart, /* 0.3.2 */
    .networkDestroy = testNetworkDestroy, /* 0.3.2 */
    .networkGetXMLDesc = testNetworkGetXMLDesc, /* 0.3.2 */
    .networkGetBridgeName = testNetworkGetBridgeName, /* 0.3.2 */
    .networkGetAutostart = testNetworkGetAutostart, /* 0.3.2 */
    .networkSetAutostart = testNetworkSetAutostart, /* 0.3.2 */
    .networkIsActive = testNetworkIsActive, /* 0.7.3 */
    .networkIsPersistent = testNetworkIsPersistent, /* 0.7.3 */
5650 5651
};

L
Laine Stump 已提交
5652 5653
static virInterfaceDriver testInterfaceDriver = {
    "Test",                     /* name */
5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667
    .open = testOpenInterface, /* 0.7.0 */
    .close = testCloseInterface, /* 0.7.0 */
    .numOfInterfaces = testNumOfInterfaces, /* 0.7.0 */
    .listInterfaces = testListInterfaces, /* 0.7.0 */
    .numOfDefinedInterfaces = testNumOfDefinedInterfaces, /* 0.7.0 */
    .listDefinedInterfaces = testListDefinedInterfaces, /* 0.7.0 */
    .interfaceLookupByName = testLookupInterfaceByName, /* 0.7.0 */
    .interfaceLookupByMACString = testLookupInterfaceByMACString, /* 0.7.0 */
    .interfaceGetXMLDesc = testInterfaceGetXMLDesc, /* 0.7.0 */
    .interfaceDefineXML = testInterfaceDefineXML, /* 0.7.0 */
    .interfaceUndefine = testInterfaceUndefine, /* 0.7.0 */
    .interfaceCreate = testInterfaceCreate, /* 0.7.0 */
    .interfaceDestroy = testInterfaceDestroy, /* 0.7.0 */
    .interfaceIsActive = testInterfaceIsActive, /* 0.7.3 */
5668 5669 5670
    .interfaceChangeBegin = testInterfaceChangeBegin,   /* 0.9.2 */
    .interfaceChangeCommit = testInterfaceChangeCommit,  /* 0.9.2 */
    .interfaceChangeRollback = testInterfaceChangeRollback, /* 0.9.2 */
L
Laine Stump 已提交
5671 5672 5673
};


5674 5675
static virStorageDriver testStorageDriver = {
    .name = "Test",
5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712
    .open = testStorageOpen, /* 0.4.1 */
    .close = testStorageClose, /* 0.4.1 */

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

    .volLookupByName = testStorageVolumeLookupByName, /* 0.5.0 */
    .volLookupByKey = testStorageVolumeLookupByKey, /* 0.5.0 */
    .volLookupByPath = testStorageVolumeLookupByPath, /* 0.5.0 */
    .volCreateXML = testStorageVolumeCreateXML, /* 0.5.0 */
    .volCreateXMLFrom = testStorageVolumeCreateXMLFrom, /* 0.6.4 */
    .volDelete = testStorageVolumeDelete, /* 0.5.0 */
    .volGetInfo = testStorageVolumeGetInfo, /* 0.5.0 */
    .volGetXMLDesc = testStorageVolumeGetXMLDesc, /* 0.5.0 */
    .volGetPath = testStorageVolumeGetPath, /* 0.5.0 */
    .poolIsActive = testStoragePoolIsActive, /* 0.7.3 */
    .poolIsPersistent = testStoragePoolIsPersistent, /* 0.7.3 */
5713 5714
};

5715 5716
static virDeviceMonitor testDevMonitor = {
    .name = "Test",
5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728
    .open = testDevMonOpen, /* 0.6.0 */
    .close = testDevMonClose, /* 0.6.0 */

    .numOfDevices = testNodeNumOfDevices, /* 0.7.2 */
    .listDevices = testNodeListDevices, /* 0.7.2 */
    .deviceLookupByName = testNodeDeviceLookupByName, /* 0.7.2 */
    .deviceGetXMLDesc = testNodeDeviceGetXMLDesc, /* 0.7.2 */
    .deviceGetParent = testNodeDeviceGetParent, /* 0.7.2 */
    .deviceNumOfCaps = testNodeDeviceNumOfCaps, /* 0.7.2 */
    .deviceListCaps = testNodeDeviceListCaps, /* 0.7.2 */
    .deviceCreateXML = testNodeDeviceCreateXML, /* 0.7.3 */
    .deviceDestroy = testNodeDeviceDestroy, /* 0.7.3 */
5729 5730
};

5731 5732
static virSecretDriver testSecretDriver = {
    .name = "Test",
5733 5734
    .open = testSecretOpen, /* 0.7.1 */
    .close = testSecretClose, /* 0.7.1 */
5735
};
5736 5737


5738 5739
static virNWFilterDriver testNWFilterDriver = {
    .name = "Test",
5740 5741
    .open = testNWFilterOpen, /* 0.8.0 */
    .close = testNWFilterClose, /* 0.8.0 */
5742 5743
};

5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755
/**
 * testRegister:
 *
 * Registers the test driver
 */
int
testRegister(void)
{
    if (virRegisterDriver(&testDriver) < 0)
        return -1;
    if (virRegisterNetworkDriver(&testNetworkDriver) < 0)
        return -1;
L
Laine Stump 已提交
5756 5757
    if (virRegisterInterfaceDriver(&testInterfaceDriver) < 0)
        return -1;
5758 5759
    if (virRegisterStorageDriver(&testStorageDriver) < 0)
        return -1;
5760 5761
    if (virRegisterDeviceMonitor(&testDevMonitor) < 0)
        return -1;
5762 5763
    if (virRegisterSecretDriver(&testSecretDriver) < 0)
        return -1;
5764 5765
    if (virRegisterNWFilterDriver(&testNWFilterDriver) < 0)
        return -1;
5766

5767 5768
    return 0;
}