test_driver.c 158.4 KB
Newer Older
1 2 3
/*
 * test.c: A "mock" hypervisor for use by application unit tests
 *
4
 * Copyright (C) 2006-2012 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
#include "virtypedparam.h"
54
#include "virrandom.h"
55
#include "virdomainlist.h"
56

57 58
#define VIR_FROM_THIS VIR_FROM_TEST

59 60 61 62 63 64 65 66 67
/* Driver specific info to carry with a domain */
struct _testDomainObjPrivate {
    virVcpuInfoPtr vcpu_infos;

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

68 69 70 71 72 73 74 75 76 77 78
#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
79

80
struct _testConn {
81
    virMutex lock;
82

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

97
    virDomainEventStatePtr domainEventState;
98 99 100
};
typedef struct _testConn testConn;
typedef struct _testConn *testConnPtr;
101

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

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

117

118 119 120 121 122
static int testClose(virConnectPtr conn);
static void testDomainEventQueue(testConnPtr driver,
                                 virDomainEventPtr event);


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

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

133 134 135 136 137 138 139 140 141 142 143 144 145 146
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 已提交
147
    VIR_FREE(priv->vcpu_infos);
148 149 150 151 152
    VIR_FREE(priv->cpumaps);
    VIR_FREE(priv);
}


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

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

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

169 170
    caps->defaultConsoleTargetType = testDefaultConsoleType;

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

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

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

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

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

207 208 209
    caps->privateDataAllocFunc = testDomainObjPrivateAlloc;
    caps->privateDataFreeFunc = testDomainObjPrivateFree;

210 211 212 213 214 215 216 217
    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;

218
    return caps;
219

220
no_memory:
221
    virReportOOMError();
222 223
    virCapabilitiesFree(caps);
    return NULL;
224 225
}

226

227 228 229
static const char *defaultDomainXML =
"<domain type='test'>"
"  <name>test</name>"
230
"  <uuid>6695eb01-f6a4-8304-79aa-97f2502e193f</uuid>"
231 232 233 234 235 236 237
"  <memory>8388608</memory>"
"  <currentMemory>2097152</currentMemory>"
"  <vcpu>2</vcpu>"
"  <os>"
"    <type>hvm</type>"
"  </os>"
"</domain>";
238 239


240 241 242
static const char *defaultNetworkXML =
"<network>"
"  <name>default</name>"
243
"  <uuid>dd8fe884-6c02-601e-7551-cca97df1c5df</uuid>"
244 245 246 247 248 249 250 251
"  <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
static const char *defaultPoolXML =
"<pool type='dir'>"
"  <name>default-pool</name>"
267
"  <uuid>dfe224cb-28fb-8dd0-c4b2-64eb3f0f4566</uuid>"
C
Cole Robinson 已提交
268 269 270 271 272
"  <target>"
"    <path>/default-pool</path>"
"  </target>"
"</pool>";

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

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

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

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

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

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

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

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

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

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

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

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

369
    return 0;
370 371
}

372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435
/* 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) {
436
        virReportOOMError();
437 438 439 440
        goto cleanup;
    }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    privconn->nextDomID = 1;

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

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

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

573
    virDomainObjUnlock(domobj);
574

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

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

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

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

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

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

620
    testDriverUnlock(privconn);
621

622 623 624
    return VIR_DRV_OPEN_SUCCESS;

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


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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

918
        virDomainObjUnlock(dom);
919
    }
920
    VIR_FREE(domains);
921

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

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

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

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

983
        if (!(iface = virInterfaceAssignDef(&privconn->ifaces, def))) {
L
Laine Stump 已提交
984 985 986
            virInterfaceDefFree(def);
            goto error;
        }
987 988

        iface->active = 1;
L
Laine Stump 已提交
989 990 991 992
        virInterfaceObjUnlock(iface);
    }
    VIR_FREE(ifaces);

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

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

1022
        if (!(pool = virStoragePoolObjAssignDef(&privconn->pools,
C
Cole Robinson 已提交
1023 1024 1025 1026 1027
                                                def))) {
            virStoragePoolDefFree(def);
            goto error;
        }

1028
        if (testStoragePoolObjSetDefaults(pool) == -1) {
1029
            virStoragePoolObjUnlock(pool);
C
Cole Robinson 已提交
1030
            goto error;
1031
        }
C
Cole Robinson 已提交
1032
        pool->active = 1;
1033 1034

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

1040
        virStoragePoolObjUnlock(pool);
C
Cole Robinson 已提交
1041
    }
1042
    VIR_FREE(pools);
C
Cole Robinson 已提交
1043

1044
    ret = virXPathNodeSet("/node/device", ctxt, &devs);
1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057
    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) {
1058 1059
                virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                               _("resolving device filename"));
1060 1061 1062
                goto error;
            }

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


J
Jim Meyering 已提交
1080
    xmlXPathFreeContext(ctxt);
1081
    xmlFreeDoc(xml);
1082
    testDriverUnlock(privconn);
1083

1084
    return 0;
1085 1086

 error:
J
Jim Meyering 已提交
1087
    xmlXPathFreeContext(ctxt);
1088
    xmlFreeDoc(xml);
1089 1090
    VIR_FREE(domains);
    VIR_FREE(networks);
L
Laine Stump 已提交
1091
    VIR_FREE(ifaces);
C
Cole Robinson 已提交
1092
    VIR_FREE(pools);
1093
    VIR_FREE(devs);
1094
    virDomainObjListDeinit(&privconn->domains);
1095
    virNetworkObjListFree(&privconn->networks);
L
Laine Stump 已提交
1096
    virInterfaceObjListFree(&privconn->ifaces);
C
Cole Robinson 已提交
1097
    virStoragePoolObjListFree(&privconn->pools);
E
Eric Blake 已提交
1098
    VIR_FREE(privconn->path);
1099
    testDriverUnlock(privconn);
1100
    VIR_FREE(privconn);
1101
    conn->privateData = NULL;
1102
    return VIR_DRV_OPEN_ERROR;
1103 1104
}

1105

1106
static virDrvOpenStatus testOpen(virConnectPtr conn,
1107
                                 virConnectAuthPtr auth ATTRIBUTE_UNUSED,
E
Eric Blake 已提交
1108
                                 unsigned int flags)
1109
{
1110
    int ret;
1111
    testConnPtr privconn;
1112

E
Eric Blake 已提交
1113 1114
    virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);

1115
    if (!conn->uri)
1116
        return VIR_DRV_OPEN_DECLINED;
1117

1118
    if (!conn->uri->scheme || STRNEQ(conn->uri->scheme, "test"))
1119
        return VIR_DRV_OPEN_DECLINED;
1120

1121
    /* Remote driver should handle these. */
1122
    if (conn->uri->server)
1123 1124
        return VIR_DRV_OPEN_DECLINED;

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

1134
    if (STREQ(conn->uri->path, "/default"))
1135 1136
        ret = testOpenDefault(conn);
    else
1137
        ret = testOpenFromFile(conn,
1138
                               conn->uri->path);
1139

1140 1141 1142 1143 1144
    if (ret != VIR_DRV_OPEN_SUCCESS)
        return ret;

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

1146
    privconn->domainEventState = virDomainEventStateNew();
1147
    if (!privconn->domainEventState) {
1148
        testDriverUnlock(privconn);
1149 1150
        testClose(conn);
        return VIR_DRV_OPEN_ERROR;
1151 1152
    }

1153 1154 1155
    testDriverUnlock(privconn);

    return VIR_DRV_OPEN_SUCCESS;
1156 1157
}

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

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

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

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

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

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

1196 1197 1198 1199 1200
static int testIsAlive(virConnectPtr conn ATTRIBUTE_UNUSED)
{
    return 1;
}

1201 1202 1203 1204 1205 1206 1207 1208
static int testGetMaxVCPUs(virConnectPtr conn ATTRIBUTE_UNUSED,
                           const char *type ATTRIBUTE_UNUSED)
{
    return 32;
}

static int testNodeGetInfo(virConnectPtr conn,
                           virNodeInfoPtr info)
1209
{
1210
    testConnPtr privconn = conn->privateData;
1211
    testDriverLock(privconn);
1212
    memcpy(info, &privconn->nodeInfo, sizeof(virNodeInfo));
1213
    testDriverUnlock(privconn);
1214
    return 0;
1215 1216
}

1217
static char *testGetCapabilities (virConnectPtr conn)
1218
{
1219
    testConnPtr privconn = conn->privateData;
1220
    char *xml;
1221
    testDriverLock(privconn);
1222
    if ((xml = virCapabilitiesFormatXML(privconn->caps)) == NULL)
1223
        virReportOOMError();
1224
    testDriverUnlock(privconn);
1225
    return xml;
1226 1227
}

1228
static int testNumOfDomains(virConnectPtr conn)
1229
{
1230
    testConnPtr privconn = conn->privateData;
1231
    int count;
1232

1233
    testDriverLock(privconn);
1234
    count = virDomainObjListNumOfDomains(&privconn->domains, 1);
1235
    testDriverUnlock(privconn);
1236

1237
    return count;
1238 1239
}

1240 1241 1242 1243 1244 1245 1246 1247 1248 1249
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) {
1250
        virReportError(VIR_ERR_NO_DOMAIN, NULL);
1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270
        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) {
1271
        virReportError(VIR_ERR_NO_DOMAIN, NULL);
1272 1273 1274 1275 1276 1277 1278 1279 1280 1281
        goto cleanup;
    }
    ret = obj->persistent;

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

1282 1283 1284 1285 1286
static int testDomainIsUpdated(virDomainPtr dom ATTRIBUTE_UNUSED)
{
    return 0;
}

1287
static virDomainPtr
1288
testDomainCreateXML(virConnectPtr conn, const char *xml,
1289
                      unsigned int flags)
1290
{
1291
    testConnPtr privconn = conn->privateData;
1292
    virDomainPtr ret = NULL;
1293
    virDomainDefPtr def;
1294
    virDomainObjPtr dom = NULL;
1295
    virDomainEventPtr event = NULL;
1296

1297 1298
    virCheckFlags(0, NULL);

1299
    testDriverLock(privconn);
1300
    if ((def = virDomainDefParseString(privconn->caps, xml,
M
Matthias Bolte 已提交
1301
                                       1 << VIR_DOMAIN_VIRT_TEST,
1302
                                       VIR_DOMAIN_XML_INACTIVE)) == NULL)
1303
        goto cleanup;
1304

1305 1306 1307
    if (virDomainObjIsDuplicate(&privconn->domains, def, 1) < 0)
        goto cleanup;

1308
    if (testDomainGenerateIfnames(def) < 0)
1309
        goto cleanup;
1310
    if (!(dom = virDomainAssignDef(privconn->caps,
1311
                                   &privconn->domains, def, false)))
1312 1313
        goto cleanup;
    def = NULL;
1314

J
Jiri Denemark 已提交
1315
    if (testDomainStartState(conn, dom, VIR_DOMAIN_RUNNING_BOOTED) < 0)
1316
        goto cleanup;
1317

1318 1319 1320 1321
    event = virDomainEventNewFromObj(dom,
                                     VIR_DOMAIN_EVENT_STARTED,
                                     VIR_DOMAIN_EVENT_STARTED_BOOTED);

1322
    ret = virGetDomain(conn, dom->def->name, dom->def->uuid);
1323
    if (ret)
1324
        ret->id = dom->def->id;
1325 1326

cleanup:
1327 1328
    if (dom)
        virDomainObjUnlock(dom);
1329 1330
    if (event)
        testDomainEventQueue(privconn, event);
1331
    virDomainDefFree(def);
1332
    testDriverUnlock(privconn);
1333
    return ret;
1334 1335 1336
}


1337 1338
static virDomainPtr testLookupDomainByID(virConnectPtr conn,
                                         int id)
1339
{
1340
    testConnPtr privconn = conn->privateData;
1341 1342
    virDomainPtr ret = NULL;
    virDomainObjPtr dom;
1343

1344 1345 1346 1347 1348
    testDriverLock(privconn);
    dom = virDomainFindByID(&privconn->domains, id);
    testDriverUnlock(privconn);

    if (dom == NULL) {
1349
        virReportError(VIR_ERR_NO_DOMAIN, NULL);
1350
        goto cleanup;
1351 1352
    }

1353
    ret = virGetDomain(conn, dom->def->name, dom->def->uuid);
1354 1355 1356 1357
    if (ret)
        ret->id = dom->def->id;

cleanup:
1358 1359
    if (dom)
        virDomainObjUnlock(dom);
1360
    return ret;
1361 1362
}

1363 1364
static virDomainPtr testLookupDomainByUUID(virConnectPtr conn,
                                           const unsigned char *uuid)
1365
{
1366
    testConnPtr privconn = conn->privateData;
1367 1368
    virDomainPtr ret = NULL;
    virDomainObjPtr dom ;
1369

1370 1371 1372 1373 1374
    testDriverLock(privconn);
    dom = virDomainFindByUUID(&privconn->domains, uuid);
    testDriverUnlock(privconn);

    if (dom == NULL) {
1375
        virReportError(VIR_ERR_NO_DOMAIN, NULL);
1376
        goto cleanup;
1377
    }
1378

1379
    ret = virGetDomain(conn, dom->def->name, dom->def->uuid);
1380 1381 1382 1383
    if (ret)
        ret->id = dom->def->id;

cleanup:
1384 1385
    if (dom)
        virDomainObjUnlock(dom);
1386
    return ret;
1387 1388
}

1389 1390
static virDomainPtr testLookupDomainByName(virConnectPtr conn,
                                           const char *name)
1391
{
1392
    testConnPtr privconn = conn->privateData;
1393 1394
    virDomainPtr ret = NULL;
    virDomainObjPtr dom;
1395

1396 1397 1398 1399 1400
    testDriverLock(privconn);
    dom = virDomainFindByName(&privconn->domains, name);
    testDriverUnlock(privconn);

    if (dom == NULL) {
1401
        virReportError(VIR_ERR_NO_DOMAIN, NULL);
1402
        goto cleanup;
1403
    }
1404

1405
    ret = virGetDomain(conn, dom->def->name, dom->def->uuid);
1406 1407 1408 1409
    if (ret)
        ret->id = dom->def->id;

cleanup:
1410 1411
    if (dom)
        virDomainObjUnlock(dom);
1412
    return ret;
1413 1414
}

1415 1416 1417
static int testListDomains (virConnectPtr conn,
                            int *ids,
                            int maxids)
1418
{
1419
    testConnPtr privconn = conn->privateData;
1420
    int n;
1421

1422
    testDriverLock(privconn);
1423
    n = virDomainObjListGetActiveIDs(&privconn->domains, ids, maxids);
1424
    testDriverUnlock(privconn);
1425

1426
    return n;
1427 1428
}

1429
static int testDestroyDomain (virDomainPtr domain)
1430
{
1431 1432
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
1433
    virDomainEventPtr event = NULL;
1434
    int ret = -1;
1435

1436
    testDriverLock(privconn);
1437 1438 1439 1440
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);

    if (privdom == NULL) {
1441
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
1442
        goto cleanup;
1443
    }
1444

J
Jiri Denemark 已提交
1445
    testDomainShutdownState(domain, privdom, VIR_DOMAIN_SHUTOFF_DESTROYED);
1446 1447 1448
    event = virDomainEventNewFromObj(privdom,
                                     VIR_DOMAIN_EVENT_STOPPED,
                                     VIR_DOMAIN_EVENT_STOPPED_DESTROYED);
1449

1450 1451 1452
    if (!privdom->persistent) {
        virDomainRemoveInactive(&privconn->domains,
                                privdom);
1453
        privdom = NULL;
1454
    }
1455 1456 1457

    ret = 0;
cleanup:
1458 1459
    if (privdom)
        virDomainObjUnlock(privdom);
1460 1461
    if (event)
        testDomainEventQueue(privconn, event);
1462
    testDriverUnlock(privconn);
1463
    return ret;
1464 1465
}

1466
static int testResumeDomain (virDomainPtr domain)
1467
{
1468 1469
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
1470
    virDomainEventPtr event = NULL;
1471
    int ret = -1;
1472

1473
    testDriverLock(privconn);
1474 1475
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
1476
    testDriverUnlock(privconn);
1477 1478

    if (privdom == NULL) {
1479
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
1480
        goto cleanup;
1481
    }
1482

J
Jiri Denemark 已提交
1483
    if (virDomainObjGetState(privdom, NULL) != VIR_DOMAIN_PAUSED) {
1484 1485
        virReportError(VIR_ERR_INTERNAL_ERROR, _("domain '%s' not paused"),
                       domain->name);
1486
        goto cleanup;
1487
    }
1488

J
Jiri Denemark 已提交
1489 1490
    virDomainObjSetState(privdom, VIR_DOMAIN_RUNNING,
                         VIR_DOMAIN_RUNNING_UNPAUSED);
1491 1492 1493
    event = virDomainEventNewFromObj(privdom,
                                     VIR_DOMAIN_EVENT_RESUMED,
                                     VIR_DOMAIN_EVENT_RESUMED_UNPAUSED);
1494 1495 1496
    ret = 0;

cleanup:
1497 1498
    if (privdom)
        virDomainObjUnlock(privdom);
1499 1500 1501 1502 1503
    if (event) {
        testDriverLock(privconn);
        testDomainEventQueue(privconn, event);
        testDriverUnlock(privconn);
    }
1504
    return ret;
1505 1506
}

1507
static int testPauseDomain (virDomainPtr domain)
1508
{
1509 1510
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
1511
    virDomainEventPtr event = NULL;
1512
    int ret = -1;
J
Jiri Denemark 已提交
1513
    int state;
1514

1515
    testDriverLock(privconn);
1516 1517
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
1518
    testDriverUnlock(privconn);
1519 1520

    if (privdom == NULL) {
1521
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
1522
        goto cleanup;
1523
    }
1524

J
Jiri Denemark 已提交
1525 1526
    state = virDomainObjGetState(privdom, NULL);
    if (state == VIR_DOMAIN_SHUTOFF || state == VIR_DOMAIN_PAUSED) {
1527 1528
        virReportError(VIR_ERR_INTERNAL_ERROR, _("domain '%s' not running"),
                       domain->name);
1529
        goto cleanup;
1530
    }
1531

J
Jiri Denemark 已提交
1532
    virDomainObjSetState(privdom, VIR_DOMAIN_PAUSED, VIR_DOMAIN_PAUSED_USER);
1533 1534 1535
    event = virDomainEventNewFromObj(privdom,
                                     VIR_DOMAIN_EVENT_SUSPENDED,
                                     VIR_DOMAIN_EVENT_SUSPENDED_PAUSED);
1536 1537 1538
    ret = 0;

cleanup:
1539 1540
    if (privdom)
        virDomainObjUnlock(privdom);
1541 1542 1543 1544 1545 1546

    if (event) {
        testDriverLock(privconn);
        testDomainEventQueue(privconn, event);
        testDriverUnlock(privconn);
    }
1547
    return ret;
1548 1549
}

1550 1551
static int testShutdownDomainFlags(virDomainPtr domain,
                                   unsigned int flags)
1552
{
1553 1554
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
1555
    virDomainEventPtr event = NULL;
1556
    int ret = -1;
1557

1558 1559
    virCheckFlags(0, -1);

1560
    testDriverLock(privconn);
1561 1562 1563 1564
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);

    if (privdom == NULL) {
1565
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
1566
        goto cleanup;
1567
    }
1568

J
Jiri Denemark 已提交
1569
    if (virDomainObjGetState(privdom, NULL) == VIR_DOMAIN_SHUTOFF) {
1570 1571
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("domain '%s' not running"), domain->name);
1572
        goto cleanup;
1573
    }
1574

J
Jiri Denemark 已提交
1575
    testDomainShutdownState(domain, privdom, VIR_DOMAIN_SHUTOFF_SHUTDOWN);
1576 1577 1578
    event = virDomainEventNewFromObj(privdom,
                                     VIR_DOMAIN_EVENT_STOPPED,
                                     VIR_DOMAIN_EVENT_STOPPED_SHUTDOWN);
1579

1580 1581 1582 1583 1584
    if (!privdom->persistent) {
        virDomainRemoveInactive(&privconn->domains,
                                privdom);
        privdom = NULL;
    }
1585

1586
    ret = 0;
1587
cleanup:
1588 1589
    if (privdom)
        virDomainObjUnlock(privdom);
1590 1591
    if (event)
        testDomainEventQueue(privconn, event);
1592
    testDriverUnlock(privconn);
1593
    return ret;
1594 1595
}

1596 1597 1598 1599 1600
static int testShutdownDomain (virDomainPtr domain)
{
    return testShutdownDomainFlags(domain, 0);
}

1601
/* Similar behaviour as shutdown */
1602 1603
static int testRebootDomain (virDomainPtr domain,
                             unsigned int action ATTRIBUTE_UNUSED)
1604
{
1605 1606
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
1607
    virDomainEventPtr event = NULL;
1608
    int ret = -1;
1609

1610
    testDriverLock(privconn);
1611 1612 1613 1614
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);

    if (privdom == NULL) {
1615
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
1616
        goto cleanup;
1617
    }
1618

J
Jiri Denemark 已提交
1619 1620 1621
    virDomainObjSetState(privdom, VIR_DOMAIN_SHUTDOWN,
                         VIR_DOMAIN_SHUTDOWN_USER);

1622 1623
    switch (privdom->def->onReboot) {
    case VIR_DOMAIN_LIFECYCLE_DESTROY:
J
Jiri Denemark 已提交
1624 1625
        virDomainObjSetState(privdom, VIR_DOMAIN_SHUTOFF,
                             VIR_DOMAIN_SHUTOFF_SHUTDOWN);
1626 1627
        break;

1628
    case VIR_DOMAIN_LIFECYCLE_RESTART:
J
Jiri Denemark 已提交
1629 1630
        virDomainObjSetState(privdom, VIR_DOMAIN_RUNNING,
                             VIR_DOMAIN_RUNNING_BOOTED);
1631 1632
        break;

1633
    case VIR_DOMAIN_LIFECYCLE_PRESERVE:
J
Jiri Denemark 已提交
1634 1635
        virDomainObjSetState(privdom, VIR_DOMAIN_SHUTOFF,
                             VIR_DOMAIN_SHUTOFF_SHUTDOWN);
1636 1637
        break;

1638
    case VIR_DOMAIN_LIFECYCLE_RESTART_RENAME:
J
Jiri Denemark 已提交
1639 1640
        virDomainObjSetState(privdom, VIR_DOMAIN_RUNNING,
                             VIR_DOMAIN_RUNNING_BOOTED);
1641
        break;
1642

1643
    default:
J
Jiri Denemark 已提交
1644 1645
        virDomainObjSetState(privdom, VIR_DOMAIN_SHUTOFF,
                             VIR_DOMAIN_SHUTOFF_SHUTDOWN);
1646 1647
        break;
    }
1648

J
Jiri Denemark 已提交
1649 1650
    if (virDomainObjGetState(privdom, NULL) == VIR_DOMAIN_SHUTOFF) {
        testDomainShutdownState(domain, privdom, VIR_DOMAIN_SHUTOFF_SHUTDOWN);
1651 1652 1653
        event = virDomainEventNewFromObj(privdom,
                                         VIR_DOMAIN_EVENT_STOPPED,
                                         VIR_DOMAIN_EVENT_STOPPED_SHUTDOWN);
1654

1655 1656 1657 1658 1659
        if (!privdom->persistent) {
            virDomainRemoveInactive(&privconn->domains,
                                    privdom);
            privdom = NULL;
        }
1660 1661
    }

1662 1663
    ret = 0;
cleanup:
1664 1665
    if (privdom)
        virDomainObjUnlock(privdom);
1666 1667
    if (event)
        testDomainEventQueue(privconn, event);
1668
    testDriverUnlock(privconn);
1669
    return ret;
1670 1671
}

1672 1673
static int testGetDomainInfo (virDomainPtr domain,
                              virDomainInfoPtr info)
1674
{
1675
    testConnPtr privconn = domain->conn->privateData;
1676
    struct timeval tv;
1677
    virDomainObjPtr privdom;
1678
    int ret = -1;
1679

1680
    testDriverLock(privconn);
1681 1682
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
1683
    testDriverUnlock(privconn);
1684 1685

    if (privdom == NULL) {
1686
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
1687
        goto cleanup;
1688
    }
1689 1690

    if (gettimeofday(&tv, NULL) < 0) {
1691 1692
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("getting time of day"));
1693
        goto cleanup;
1694 1695
    }

J
Jiri Denemark 已提交
1696
    info->state = virDomainObjGetState(privdom, NULL);
1697 1698
    info->memory = privdom->def->mem.cur_balloon;
    info->maxMem = privdom->def->mem.max_balloon;
1699 1700
    info->nrVirtCpu = privdom->def->vcpus;
    info->cpuTime = ((tv.tv_sec * 1000ll * 1000ll  * 1000ll) + (tv.tv_usec * 1000ll));
1701 1702 1703
    ret = 0;

cleanup:
1704 1705
    if (privdom)
        virDomainObjUnlock(privdom);
1706
    return ret;
1707 1708
}

1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726
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) {
1727
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
1728 1729 1730
        goto cleanup;
    }

J
Jiri Denemark 已提交
1731
    *state = virDomainObjGetState(privdom, reason);
1732 1733 1734 1735 1736 1737 1738 1739
    ret = 0;

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

1740 1741
#define TEST_SAVE_MAGIC "TestGuestMagic"

1742 1743 1744
static int
testDomainSaveFlags(virDomainPtr domain, const char *path,
                    const char *dxml, unsigned int flags)
1745
{
1746
    testConnPtr privconn = domain->conn->privateData;
1747 1748 1749
    char *xml = NULL;
    int fd = -1;
    int len;
1750
    virDomainObjPtr privdom;
1751
    virDomainEventPtr event = NULL;
1752
    int ret = -1;
1753

1754 1755
    virCheckFlags(0, -1);
    if (dxml) {
1756 1757
        virReportError(VIR_ERR_ARGUMENT_UNSUPPORTED, "%s",
                       _("xml modification unsupported"));
1758 1759 1760
        return -1;
    }

1761
    testDriverLock(privconn);
1762 1763 1764 1765
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);

    if (privdom == NULL) {
1766
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
1767
        goto cleanup;
1768
    }
1769

1770
    xml = virDomainDefFormat(privdom->def,
C
Cole Robinson 已提交
1771 1772
                             VIR_DOMAIN_XML_SECURE);

1773
    if (xml == NULL) {
1774
        virReportSystemError(errno,
1775 1776
                             _("saving domain '%s' failed to allocate space for metadata"),
                             domain->name);
1777
        goto cleanup;
1778
    }
1779 1780

    if ((fd = open(path, O_CREAT|O_TRUNC|O_WRONLY, S_IRUSR|S_IWUSR)) < 0) {
1781
        virReportSystemError(errno,
1782 1783
                             _("saving domain '%s' to '%s': open failed"),
                             domain->name, path);
1784
        goto cleanup;
1785
    }
1786
    len = strlen(xml);
1787
    if (safewrite(fd, TEST_SAVE_MAGIC, sizeof(TEST_SAVE_MAGIC)) < 0) {
1788
        virReportSystemError(errno,
1789 1790
                             _("saving domain '%s' to '%s': write failed"),
                             domain->name, path);
1791
        goto cleanup;
1792
    }
1793
    if (safewrite(fd, (char*)&len, sizeof(len)) < 0) {
1794
        virReportSystemError(errno,
1795 1796
                             _("saving domain '%s' to '%s': write failed"),
                             domain->name, path);
1797
        goto cleanup;
1798
    }
1799
    if (safewrite(fd, xml, len) < 0) {
1800
        virReportSystemError(errno,
1801 1802
                             _("saving domain '%s' to '%s': write failed"),
                             domain->name, path);
1803
        goto cleanup;
1804
    }
1805

1806
    if (VIR_CLOSE(fd) < 0) {
1807
        virReportSystemError(errno,
1808 1809
                             _("saving domain '%s' to '%s': write failed"),
                             domain->name, path);
1810
        goto cleanup;
1811
    }
1812 1813
    fd = -1;

J
Jiri Denemark 已提交
1814
    testDomainShutdownState(domain, privdom, VIR_DOMAIN_SHUTOFF_SAVED);
1815 1816 1817
    event = virDomainEventNewFromObj(privdom,
                                     VIR_DOMAIN_EVENT_STOPPED,
                                     VIR_DOMAIN_EVENT_STOPPED_SAVED);
1818

1819 1820 1821
    if (!privdom->persistent) {
        virDomainRemoveInactive(&privconn->domains,
                                privdom);
1822
        privdom = NULL;
1823
    }
1824

1825
    ret = 0;
1826 1827 1828 1829 1830 1831 1832
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) {
1833
        VIR_FORCE_CLOSE(fd);
1834 1835
        unlink(path);
    }
1836 1837
    if (privdom)
        virDomainObjUnlock(privdom);
1838 1839
    if (event)
        testDomainEventQueue(privconn, event);
1840
    testDriverUnlock(privconn);
1841
    return ret;
1842 1843
}

1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855
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)
1856
{
1857
    testConnPtr privconn = conn->privateData;
1858
    char *xml = NULL;
1859
    char magic[15];
1860 1861 1862
    int fd = -1;
    int len;
    virDomainDefPtr def = NULL;
1863
    virDomainObjPtr dom = NULL;
1864
    virDomainEventPtr event = NULL;
1865
    int ret = -1;
1866

1867 1868
    virCheckFlags(0, -1);
    if (dxml) {
1869 1870
        virReportError(VIR_ERR_ARGUMENT_UNSUPPORTED, "%s",
                       _("xml modification unsupported"));
1871 1872 1873
        return -1;
    }

1874 1875
    testDriverLock(privconn);

1876
    if ((fd = open(path, O_RDONLY)) < 0) {
1877
        virReportSystemError(errno,
1878 1879
                             _("cannot read domain image '%s'"),
                             path);
1880
        goto cleanup;
1881
    }
1882
    if (saferead(fd, magic, sizeof(magic)) != sizeof(magic)) {
1883
        virReportSystemError(errno,
1884 1885
                             _("incomplete save header in '%s'"),
                             path);
1886
        goto cleanup;
1887
    }
1888
    if (memcmp(magic, TEST_SAVE_MAGIC, sizeof(magic))) {
1889 1890
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("mismatched header magic"));
1891
        goto cleanup;
1892
    }
1893
    if (saferead(fd, (char*)&len, sizeof(len)) != sizeof(len)) {
1894
        virReportSystemError(errno,
1895 1896
                             _("failed to read metadata length in '%s'"),
                             path);
1897
        goto cleanup;
1898 1899
    }
    if (len < 1 || len > 8192) {
1900 1901
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("length of metadata out of range"));
1902
        goto cleanup;
1903
    }
1904
    if (VIR_ALLOC_N(xml, len+1) < 0) {
1905
        virReportOOMError();
1906
        goto cleanup;
1907
    }
1908
    if (saferead(fd, xml, len) != len) {
1909
        virReportSystemError(errno,
1910
                             _("incomplete metdata in '%s'"), path);
1911
        goto cleanup;
1912 1913
    }
    xml[len] = '\0';
1914

1915
    def = virDomainDefParseString(privconn->caps, xml,
M
Matthias Bolte 已提交
1916
                                  1 << VIR_DOMAIN_VIRT_TEST,
1917
                                  VIR_DOMAIN_XML_INACTIVE);
1918
    if (!def)
1919
        goto cleanup;
1920

1921 1922 1923
    if (virDomainObjIsDuplicate(&privconn->domains, def, 1) < 0)
        goto cleanup;

1924
    if (testDomainGenerateIfnames(def) < 0)
1925
        goto cleanup;
1926
    if (!(dom = virDomainAssignDef(privconn->caps,
1927
                                   &privconn->domains, def, true)))
1928 1929
        goto cleanup;
    def = NULL;
1930

J
Jiri Denemark 已提交
1931
    if (testDomainStartState(conn, dom, VIR_DOMAIN_RUNNING_RESTORED) < 0)
1932 1933
        goto cleanup;

1934 1935 1936
    event = virDomainEventNewFromObj(dom,
                                     VIR_DOMAIN_EVENT_STARTED,
                                     VIR_DOMAIN_EVENT_STARTED_RESTORED);
1937
    ret = 0;
1938 1939 1940 1941

cleanup:
    virDomainDefFree(def);
    VIR_FREE(xml);
1942
    VIR_FORCE_CLOSE(fd);
1943 1944
    if (dom)
        virDomainObjUnlock(dom);
1945 1946
    if (event)
        testDomainEventQueue(privconn, event);
1947
    testDriverUnlock(privconn);
1948
    return ret;
1949 1950
}

1951 1952 1953 1954 1955 1956 1957
static int
testDomainRestore(virConnectPtr conn,
                  const char *path)
{
    return testDomainRestoreFlags(conn, path, NULL, 0);
}

1958 1959
static int testDomainCoreDump(virDomainPtr domain,
                              const char *to,
E
Eric Blake 已提交
1960
                              unsigned int flags)
1961
{
1962
    testConnPtr privconn = domain->conn->privateData;
1963
    int fd = -1;
1964
    virDomainObjPtr privdom;
1965
    virDomainEventPtr event = NULL;
1966
    int ret = -1;
1967

E
Eric Blake 已提交
1968 1969
    virCheckFlags(VIR_DUMP_CRASH, -1);

1970
    testDriverLock(privconn);
1971 1972 1973 1974
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);

    if (privdom == NULL) {
1975
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
1976
        goto cleanup;
1977
    }
1978 1979

    if ((fd = open(to, O_CREAT|O_TRUNC|O_WRONLY, S_IRUSR|S_IWUSR)) < 0) {
1980
        virReportSystemError(errno,
1981 1982
                             _("domain '%s' coredump: failed to open %s"),
                             domain->name, to);
1983
        goto cleanup;
1984
    }
1985
    if (safewrite(fd, TEST_SAVE_MAGIC, sizeof(TEST_SAVE_MAGIC)) < 0) {
1986
        virReportSystemError(errno,
1987 1988
                             _("domain '%s' coredump: failed to write header to %s"),
                             domain->name, to);
1989
        goto cleanup;
1990
    }
1991
    if (VIR_CLOSE(fd) < 0) {
1992
        virReportSystemError(errno,
1993 1994
                             _("domain '%s' coredump: write failed: %s"),
                             domain->name, to);
1995
        goto cleanup;
1996
    }
1997

1998
    if (flags & VIR_DUMP_CRASH) {
J
Jiri Denemark 已提交
1999
        testDomainShutdownState(domain, privdom, VIR_DOMAIN_SHUTOFF_CRASHED);
2000 2001 2002 2003 2004 2005 2006 2007
        event = virDomainEventNewFromObj(privdom,
                                         VIR_DOMAIN_EVENT_STOPPED,
                                         VIR_DOMAIN_EVENT_STOPPED_CRASHED);
        if (!privdom->persistent) {
            virDomainRemoveInactive(&privconn->domains,
                                    privdom);
            privdom = NULL;
        }
2008
    }
2009

2010
    ret = 0;
2011
cleanup:
2012
    VIR_FORCE_CLOSE(fd);
2013 2014
    if (privdom)
        virDomainObjUnlock(privdom);
2015 2016
    if (event)
        testDomainEventQueue(privconn, event);
2017
    testDriverUnlock(privconn);
2018
    return ret;
2019 2020
}

2021
static char *testGetOSType(virDomainPtr dom ATTRIBUTE_UNUSED) {
2022 2023
    char *ret = strdup("linux");
    if (!ret)
2024
        virReportOOMError();
2025
    return ret;
2026 2027
}

2028
static unsigned long long testGetMaxMemory(virDomainPtr domain) {
2029 2030
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
2031
    unsigned long long ret = 0;
2032

2033
    testDriverLock(privconn);
2034 2035
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
2036
    testDriverUnlock(privconn);
2037 2038

    if (privdom == NULL) {
2039
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2040
        goto cleanup;
2041
    }
2042

2043
    ret = privdom->def->mem.max_balloon;
2044 2045

cleanup:
2046 2047
    if (privdom)
        virDomainObjUnlock(privdom);
2048
    return ret;
2049 2050 2051 2052 2053
}

static int testSetMaxMemory(virDomainPtr domain,
                            unsigned long memory)
{
2054 2055
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
2056
    int ret = -1;
2057

2058
    testDriverLock(privconn);
2059 2060
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
2061
    testDriverUnlock(privconn);
2062 2063

    if (privdom == NULL) {
2064
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2065
        goto cleanup;
2066
    }
2067 2068

    /* XXX validate not over host memory wrt to other domains */
2069
    privdom->def->mem.max_balloon = memory;
2070 2071 2072
    ret = 0;

cleanup:
2073 2074
    if (privdom)
        virDomainObjUnlock(privdom);
2075
    return ret;
2076 2077
}

2078 2079 2080
static int testSetMemory(virDomainPtr domain,
                         unsigned long memory)
{
2081 2082
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
2083
    int ret = -1;
2084

2085
    testDriverLock(privconn);
2086 2087
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
2088
    testDriverUnlock(privconn);
2089 2090

    if (privdom == NULL) {
2091
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2092
        goto cleanup;
2093
    }
2094

2095
    if (memory > privdom->def->mem.max_balloon) {
2096
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2097
        goto cleanup;
2098
    }
2099

2100
    privdom->def->mem.cur_balloon = memory;
2101 2102 2103
    ret = 0;

cleanup:
2104 2105
    if (privdom)
        virDomainObjUnlock(privdom);
2106
    return ret;
2107 2108
}

2109 2110
static int
testDomainGetVcpusFlags(virDomainPtr domain, unsigned int flags)
C
Cole Robinson 已提交
2111
{
2112 2113 2114 2115 2116
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr vm;
    virDomainDefPtr def;
    int ret = -1;

2117 2118
    virCheckFlags(VIR_DOMAIN_AFFECT_LIVE |
                  VIR_DOMAIN_AFFECT_CONFIG |
2119 2120 2121 2122 2123 2124 2125 2126 2127
                  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);
2128 2129
        virReportError(VIR_ERR_NO_DOMAIN,
                       _("no domain with matching uuid '%s'"), uuidstr);
2130 2131 2132
        goto cleanup;
    }

2133 2134
    if (virDomainLiveConfigHelperMethod(privconn->caps, vm, &flags, &def) < 0)
        goto cleanup;
2135

2136
    if (flags & VIR_DOMAIN_AFFECT_LIVE)
2137 2138 2139 2140 2141 2142 2143 2144
        def = vm->def;

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

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

2147 2148 2149
static int
testDomainGetMaxVcpus(virDomainPtr domain)
{
2150
    return testDomainGetVcpusFlags(domain, (VIR_DOMAIN_AFFECT_LIVE |
2151 2152 2153 2154 2155 2156 2157
                                            VIR_DOMAIN_VCPU_MAXIMUM));
}

static int
testDomainSetVcpusFlags(virDomainPtr domain, unsigned int nrCpus,
                        unsigned int flags)
{
2158
    testConnPtr privconn = domain->conn->privateData;
2159
    virDomainObjPtr privdom = NULL;
2160
    virDomainDefPtr persistentDef;
C
Cole Robinson 已提交
2161 2162
    int ret = -1, maxvcpus;

2163 2164
    virCheckFlags(VIR_DOMAIN_AFFECT_LIVE |
                  VIR_DOMAIN_AFFECT_CONFIG |
2165 2166 2167 2168
                  VIR_DOMAIN_VCPU_MAXIMUM, -1);

    /* At least one of LIVE or CONFIG must be set.  MAXIMUM cannot be
     * mixed with LIVE.  */
2169 2170 2171
    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)) {
2172 2173
        virReportError(VIR_ERR_INVALID_ARG,
                       _("invalid flag combination: (0x%x)"), flags);
2174 2175 2176
        return -1;
    }
    if (!nrCpus || (maxvcpus = testGetMaxVCPUs(domain->conn, NULL)) < nrCpus) {
2177 2178
        virReportError(VIR_ERR_INVALID_ARG,
                       _("argument out of range: %d"), nrCpus);
2179 2180
        return -1;
    }
2181

2182
    testDriverLock(privconn);
2183
    privdom = virDomainFindByUUID(&privconn->domains, domain->uuid);
2184 2185 2186
    testDriverUnlock(privconn);

    if (privdom == NULL) {
2187
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2188
        goto cleanup;
2189
    }
2190

2191
    if (!virDomainObjIsActive(privdom) && (flags & VIR_DOMAIN_AFFECT_LIVE)) {
2192 2193
        virReportError(VIR_ERR_OPERATION_INVALID,
                       "%s", _("cannot hotplug vcpus for an inactive domain"));
C
Cole Robinson 已提交
2194 2195 2196
        goto cleanup;
    }

2197 2198
    /* We allow more cpus in guest than host, but not more than the
     * domain's starting limit.  */
C
Cole Robinson 已提交
2199 2200
    if (!(flags & (VIR_DOMAIN_VCPU_MAXIMUM)) &&
        privdom->def->maxvcpus < maxvcpus)
2201
        maxvcpus = privdom->def->maxvcpus;
C
Cole Robinson 已提交
2202

C
Cole Robinson 已提交
2203
    if (nrCpus > maxvcpus) {
2204 2205 2206
        virReportError(VIR_ERR_INVALID_ARG,
                       _("requested cpu amount exceeds maximum (%d > %d)"),
                       nrCpus, maxvcpus);
2207
        goto cleanup;
2208
    }
2209

2210 2211 2212 2213
    if (!(persistentDef = virDomainObjGetPersistentDef(privconn->caps,
                                                       privdom)))
        goto cleanup;

2214
    switch (flags) {
2215
    case VIR_DOMAIN_VCPU_MAXIMUM | VIR_DOMAIN_AFFECT_CONFIG:
2216 2217 2218
        persistentDef->maxvcpus = nrCpus;
        if (nrCpus < persistentDef->vcpus)
            persistentDef->vcpus = nrCpus;
2219 2220
        ret = 0;
        break;
2221

2222
    case VIR_DOMAIN_AFFECT_CONFIG:
2223
        persistentDef->vcpus = nrCpus;
2224 2225 2226
        ret = 0;
        break;

2227
    case VIR_DOMAIN_AFFECT_LIVE:
2228 2229 2230
        ret = testDomainUpdateVCPUs(domain->conn, privdom, nrCpus, 0);
        break;

2231
    case VIR_DOMAIN_AFFECT_LIVE | VIR_DOMAIN_AFFECT_CONFIG:
2232
        ret = testDomainUpdateVCPUs(domain->conn, privdom, nrCpus, 0);
2233 2234 2235
        if (ret == 0) {
            persistentDef->vcpus = nrCpus;
        }
2236 2237
        break;
    }
2238 2239

cleanup:
2240 2241
    if (privdom)
        virDomainObjUnlock(privdom);
2242
    return ret;
2243 2244
}

2245 2246 2247
static int
testSetVcpus(virDomainPtr domain, unsigned int nrCpus)
{
2248
    return testDomainSetVcpusFlags(domain, nrCpus, VIR_DOMAIN_AFFECT_LIVE);
2249 2250
}

C
Cole Robinson 已提交
2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269
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) {
2270
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
C
Cole Robinson 已提交
2271 2272 2273 2274
        goto cleanup;
    }

    if (!virDomainObjIsActive(privdom)) {
2275 2276
        virReportError(VIR_ERR_OPERATION_INVALID,
                       "%s",_("cannot list vcpus for an inactive domain"));
C
Cole Robinson 已提交
2277 2278 2279 2280 2281 2282
        goto cleanup;
    }

    privdomdata = privdom->privateData;

    if (gettimeofday(&tv, NULL) < 0) {
2283
        virReportSystemError(errno,
C
Cole Robinson 已提交
2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 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
                             "%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 已提交
2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355
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) {
2356
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
C
Cole Robinson 已提交
2357 2358 2359 2360
        goto cleanup;
    }

    if (!virDomainObjIsActive(privdom)) {
2361 2362
        virReportError(VIR_ERR_OPERATION_INVALID,
                       "%s",_("cannot pin vcpus on an inactive domain"));
C
Cole Robinson 已提交
2363 2364 2365 2366
        goto cleanup;
    }

    if (vcpu > privdom->def->vcpus) {
2367 2368
        virReportError(VIR_ERR_INVALID_ARG, "%s",
                       _("requested vcpu is higher than allocated vcpus"));
C
Cole Robinson 已提交
2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395
        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;
}

2396
static char *testDomainGetXMLDesc(virDomainPtr domain, unsigned int flags)
2397
{
2398
    testConnPtr privconn = domain->conn->privateData;
2399
    virDomainDefPtr def;
2400
    virDomainObjPtr privdom;
2401 2402
    char *ret = NULL;

2403 2404
    /* Flags checked by virDomainDefFormat */

2405 2406 2407 2408 2409 2410
    testDriverLock(privconn);
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
    testDriverUnlock(privconn);

    if (privdom == NULL) {
2411
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2412
        goto cleanup;
2413
    }
2414

2415 2416
    def = (flags & VIR_DOMAIN_XML_INACTIVE) &&
        privdom->newDef ? privdom->newDef : privdom->def;
2417

2418
    ret = virDomainDefFormat(def,
2419 2420 2421
                             flags);

cleanup:
2422 2423
    if (privdom)
        virDomainObjUnlock(privdom);
2424
    return ret;
2425
}
2426

2427
static int testNumOfDefinedDomains(virConnectPtr conn) {
2428
    testConnPtr privconn = conn->privateData;
2429
    int count;
2430

2431
    testDriverLock(privconn);
2432
    count = virDomainObjListNumOfDomains(&privconn->domains, 0);
2433
    testDriverUnlock(privconn);
2434

2435
    return count;
2436 2437
}

2438 2439 2440
static int testListDefinedDomains(virConnectPtr conn,
                                  char **const names,
                                  int maxnames) {
2441

2442
    testConnPtr privconn = conn->privateData;
2443
    int n;
2444

2445
    testDriverLock(privconn);
2446
    memset(names, 0, sizeof(*names)*maxnames);
2447
    n = virDomainObjListGetInactiveNames(&privconn->domains, names, maxnames);
2448
    testDriverUnlock(privconn);
2449

2450
    return n;
2451 2452
}

2453
static virDomainPtr testDomainDefineXML(virConnectPtr conn,
2454
                                        const char *xml) {
2455
    testConnPtr privconn = conn->privateData;
2456
    virDomainPtr ret = NULL;
2457
    virDomainDefPtr def;
2458
    virDomainObjPtr dom = NULL;
2459
    virDomainEventPtr event = NULL;
2460
    int dupVM;
2461

2462
    testDriverLock(privconn);
2463
    if ((def = virDomainDefParseString(privconn->caps, xml,
M
Matthias Bolte 已提交
2464
                                       1 << VIR_DOMAIN_VIRT_TEST,
2465
                                       VIR_DOMAIN_XML_INACTIVE)) == NULL)
2466
        goto cleanup;
2467

2468 2469 2470
    if ((dupVM = virDomainObjIsDuplicate(&privconn->domains, def, 0)) < 0)
        goto cleanup;

2471
    if (testDomainGenerateIfnames(def) < 0)
2472
        goto cleanup;
2473
    if (!(dom = virDomainAssignDef(privconn->caps,
2474
                                   &privconn->domains, def, false)))
2475
        goto cleanup;
2476
    def = NULL;
2477
    dom->persistent = 1;
2478

2479 2480
    event = virDomainEventNewFromObj(dom,
                                     VIR_DOMAIN_EVENT_DEFINED,
2481 2482 2483
                                     !dupVM ?
                                     VIR_DOMAIN_EVENT_DEFINED_ADDED :
                                     VIR_DOMAIN_EVENT_DEFINED_UPDATED);
2484

2485
    ret = virGetDomain(conn, dom->def->name, dom->def->uuid);
2486
    if (ret)
2487
        ret->id = dom->def->id;
2488 2489 2490

cleanup:
    virDomainDefFree(def);
2491 2492
    if (dom)
        virDomainObjUnlock(dom);
2493 2494
    if (event)
        testDomainEventQueue(privconn, event);
2495
    testDriverUnlock(privconn);
2496
    return ret;
2497 2498
}

2499 2500 2501
static int testNodeGetCellsFreeMemory(virConnectPtr conn,
                                      unsigned long long *freemems,
                                      int startCell, int maxCells) {
2502
    testConnPtr privconn = conn->privateData;
2503
    int i, j;
2504
    int ret = -1;
2505

2506
    testDriverLock(privconn);
2507
    if (startCell > privconn->numCells) {
2508 2509
        virReportError(VIR_ERR_INVALID_ARG,
                       "%s", _("Range exceeds available cells"));
2510
        goto cleanup;
2511 2512 2513 2514 2515 2516 2517
    }

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

2520
cleanup:
2521
    testDriverUnlock(privconn);
2522
    return ret;
2523 2524 2525
}


2526
static int testDomainCreateWithFlags(virDomainPtr domain, unsigned int flags) {
2527 2528
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
2529
    virDomainEventPtr event = NULL;
2530
    int ret = -1;
2531

2532 2533
    virCheckFlags(0, -1);

2534
    testDriverLock(privconn);
2535 2536 2537 2538
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);

    if (privdom == NULL) {
2539
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2540
        goto cleanup;
2541
    }
2542

J
Jiri Denemark 已提交
2543
    if (virDomainObjGetState(privdom, NULL) != VIR_DOMAIN_SHUTOFF) {
2544 2545
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Domain '%s' is already running"), domain->name);
2546
        goto cleanup;
2547 2548
    }

J
Jiri Denemark 已提交
2549 2550
    if (testDomainStartState(domain->conn, privdom,
                             VIR_DOMAIN_RUNNING_BOOTED) < 0)
2551 2552 2553
        goto cleanup;
    domain->id = privdom->def->id;

2554 2555 2556
    event = virDomainEventNewFromObj(privdom,
                                     VIR_DOMAIN_EVENT_STARTED,
                                     VIR_DOMAIN_EVENT_STARTED_BOOTED);
2557
    ret = 0;
2558

2559
cleanup:
2560 2561
    if (privdom)
        virDomainObjUnlock(privdom);
2562 2563
    if (event)
        testDomainEventQueue(privconn, event);
2564
    testDriverUnlock(privconn);
2565
    return ret;
2566 2567
}

2568 2569 2570 2571
static int testDomainCreate(virDomainPtr domain) {
    return testDomainCreateWithFlags(domain, 0);
}

2572 2573 2574
static int testDomainUndefineFlags(virDomainPtr domain,
                                   unsigned int flags)
{
2575 2576
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
2577
    virDomainEventPtr event = NULL;
2578
    int ret = -1;
2579

2580 2581
    virCheckFlags(0, -1);

2582
    testDriverLock(privconn);
2583 2584 2585 2586
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);

    if (privdom == NULL) {
2587
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2588
        goto cleanup;
2589
    }
2590

2591 2592 2593
    event = virDomainEventNewFromObj(privdom,
                                     VIR_DOMAIN_EVENT_UNDEFINED,
                                     VIR_DOMAIN_EVENT_UNDEFINED_REMOVED);
2594 2595
    if (virDomainObjIsActive(privdom)) {
        privdom->persistent = 0;
2596 2597 2598 2599 2600 2601
    } else {
        virDomainRemoveInactive(&privconn->domains,
                                privdom);
        privdom = NULL;
    }

2602
    ret = 0;
2603

2604
cleanup:
2605 2606
    if (privdom)
        virDomainObjUnlock(privdom);
2607 2608
    if (event)
        testDomainEventQueue(privconn, event);
2609
    testDriverUnlock(privconn);
2610
    return ret;
2611 2612
}

2613 2614 2615 2616 2617
static int testDomainUndefine(virDomainPtr domain)
{
    return testDomainUndefineFlags(domain, 0);
}

2618 2619 2620
static int testDomainGetAutostart(virDomainPtr domain,
                                  int *autostart)
{
2621 2622
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
2623
    int ret = -1;
2624

2625
    testDriverLock(privconn);
2626 2627
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
2628
    testDriverUnlock(privconn);
2629 2630

    if (privdom == NULL) {
2631
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2632
        goto cleanup;
2633 2634
    }

2635
    *autostart = privdom->autostart;
2636 2637 2638
    ret = 0;

cleanup:
2639 2640
    if (privdom)
        virDomainObjUnlock(privdom);
2641
    return ret;
2642 2643 2644 2645 2646 2647
}


static int testDomainSetAutostart(virDomainPtr domain,
                                  int autostart)
{
2648 2649
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
2650
    int ret = -1;
2651

2652
    testDriverLock(privconn);
2653 2654
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
2655
    testDriverUnlock(privconn);
2656 2657

    if (privdom == NULL) {
2658
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2659
        goto cleanup;
2660 2661
    }

2662
    privdom->autostart = autostart ? 1 : 0;
2663 2664 2665
    ret = 0;

cleanup:
2666 2667
    if (privdom)
        virDomainObjUnlock(privdom);
2668
    return ret;
2669
}
2670

2671
static char *testDomainGetSchedulerType(virDomainPtr domain ATTRIBUTE_UNUSED,
2672 2673
                                        int *nparams)
{
2674 2675
    char *type = NULL;

2676 2677 2678
    if (nparams)
        *nparams = 1;

2679
    type = strdup("fair");
2680
    if (!type)
2681
        virReportOOMError();
2682

2683 2684 2685
    return type;
}

2686 2687 2688 2689 2690
static int
testDomainGetSchedulerParamsFlags(virDomainPtr domain,
                                  virTypedParameterPtr params,
                                  int *nparams,
                                  unsigned int flags)
2691
{
2692 2693
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
2694
    int ret = -1;
2695

2696 2697
    virCheckFlags(0, -1);

2698
    testDriverLock(privconn);
2699 2700
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
2701
    testDriverUnlock(privconn);
2702 2703

    if (privdom == NULL) {
2704
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2705
        goto cleanup;
2706 2707
    }

2708 2709
    if (virTypedParameterAssign(params, VIR_DOMAIN_SCHEDULER_WEIGHT,
                                VIR_TYPED_PARAM_UINT, 50) < 0)
2710
        goto cleanup;
2711 2712
    /* XXX */
    /*params[0].value.ui = privdom->weight;*/
2713 2714

    *nparams = 1;
2715 2716 2717
    ret = 0;

cleanup:
2718 2719
    if (privdom)
        virDomainObjUnlock(privdom);
2720
    return ret;
2721
}
2722

2723 2724 2725 2726 2727 2728 2729
static int
testDomainGetSchedulerParams(virDomainPtr domain,
                             virTypedParameterPtr params,
                             int *nparams)
{
    return testDomainGetSchedulerParamsFlags(domain, params, nparams, 0);
}
2730

2731 2732 2733 2734 2735
static int
testDomainSetSchedulerParamsFlags(virDomainPtr domain,
                                  virTypedParameterPtr params,
                                  int nparams,
                                  unsigned int flags)
2736
{
2737 2738
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
2739
    int ret = -1, i;
2740

2741
    virCheckFlags(0, -1);
2742 2743 2744 2745 2746
    if (virTypedParameterArrayValidate(params, nparams,
                                       VIR_DOMAIN_SCHEDULER_WEIGHT,
                                       VIR_TYPED_PARAM_UINT,
                                       NULL) < 0)
        return -1;
2747

2748
    testDriverLock(privconn);
2749 2750
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
2751
    testDriverUnlock(privconn);
2752 2753

    if (privdom == NULL) {
2754
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2755
        goto cleanup;
2756 2757
    }

2758
    for (i = 0; i < nparams; i++) {
2759 2760 2761
        if (STREQ(params[i].field, VIR_DOMAIN_SCHEDULER_WEIGHT)) {
            /* XXX */
            /*privdom->weight = params[i].value.ui;*/
2762
        }
2763
    }
2764

2765 2766 2767
    ret = 0;

cleanup:
2768 2769
    if (privdom)
        virDomainObjUnlock(privdom);
2770
    return ret;
2771 2772
}

2773 2774 2775 2776 2777 2778 2779 2780
static int
testDomainSetSchedulerParams(virDomainPtr domain,
                             virTypedParameterPtr params,
                             int nparams)
{
    return testDomainSetSchedulerParamsFlags(domain, params, nparams, 0);
}

2781 2782 2783 2784 2785 2786 2787 2788
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;
2789
    int ret = -1;
2790 2791 2792 2793 2794 2795 2796

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

    if (privdom == NULL) {
2797
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2798 2799 2800
        goto error;
    }

2801
    if (virDomainDiskIndexByName(privdom->def, path, false) < 0) {
2802 2803
        virReportError(VIR_ERR_INVALID_ARG,
                       _("invalid path: %s"), path);
2804 2805 2806 2807
        goto error;
    }

    if (gettimeofday(&tv, NULL) < 0) {
2808
        virReportSystemError(errno,
2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843
                             "%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) {
2844
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856
        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) {
2857 2858
        virReportError(VIR_ERR_INVALID_ARG,
                       _("invalid path, '%s' is not a known interface"), path);
2859 2860 2861 2862
        goto error;
    }

    if (gettimeofday(&tv, NULL) < 0) {
2863
        virReportSystemError(errno,
2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885
                             "%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;
}

2886
static virDrvOpenStatus testOpenNetwork(virConnectPtr conn,
2887
                                        virConnectAuthPtr auth ATTRIBUTE_UNUSED,
E
Eric Blake 已提交
2888 2889 2890 2891
                                        unsigned int flags)
{
    virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);

2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907
    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)
{
2908 2909
    testConnPtr privconn = conn->privateData;
    virNetworkObjPtr net;
2910
    virNetworkPtr ret = NULL;
2911

2912 2913 2914 2915 2916
    testDriverLock(privconn);
    net = virNetworkFindByUUID(&privconn->networks, uuid);
    testDriverUnlock(privconn);

    if (net == NULL) {
2917
        virReportError(VIR_ERR_NO_NETWORK, NULL);
2918
        goto cleanup;
2919 2920
    }

2921 2922 2923
    ret = virGetNetwork(conn, net->def->name, net->def->uuid);

cleanup:
2924 2925
    if (net)
        virNetworkObjUnlock(net);
2926
    return ret;
2927
}
2928

2929
static virNetworkPtr testLookupNetworkByName(virConnectPtr conn,
2930
                                             const char *name)
2931
{
2932
    testConnPtr privconn = conn->privateData;
2933 2934
    virNetworkObjPtr net;
    virNetworkPtr ret = NULL;
2935

2936 2937 2938 2939 2940
    testDriverLock(privconn);
    net = virNetworkFindByName(&privconn->networks, name);
    testDriverUnlock(privconn);

    if (net == NULL) {
2941
        virReportError(VIR_ERR_NO_NETWORK, NULL);
2942
        goto cleanup;
2943 2944
    }

2945 2946 2947
    ret = virGetNetwork(conn, net->def->name, net->def->uuid);

cleanup:
2948 2949
    if (net)
        virNetworkObjUnlock(net);
2950
    return ret;
2951 2952 2953 2954
}


static int testNumNetworks(virConnectPtr conn) {
2955
    testConnPtr privconn = conn->privateData;
2956
    int numActive = 0, i;
2957

2958 2959 2960
    testDriverLock(privconn);
    for (i = 0 ; i < privconn->networks.count ; i++) {
        virNetworkObjLock(privconn->networks.objs[i]);
D
Daniel P. Berrange 已提交
2961
        if (virNetworkObjIsActive(privconn->networks.objs[i]))
2962
            numActive++;
2963 2964 2965
        virNetworkObjUnlock(privconn->networks.objs[i]);
    }
    testDriverUnlock(privconn);
2966

2967
    return numActive;
2968 2969 2970
}

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

2974
    testDriverLock(privconn);
2975
    memset(names, 0, sizeof(*names)*nnames);
2976 2977
    for (i = 0 ; i < privconn->networks.count && n < nnames ; i++) {
        virNetworkObjLock(privconn->networks.objs[i]);
D
Daniel P. Berrange 已提交
2978
        if (virNetworkObjIsActive(privconn->networks.objs[i]) &&
2979 2980
            !(names[n++] = strdup(privconn->networks.objs[i]->def->name))) {
            virNetworkObjUnlock(privconn->networks.objs[i]);
2981
            goto no_memory;
2982 2983 2984 2985
        }
        virNetworkObjUnlock(privconn->networks.objs[i]);
    }
    testDriverUnlock(privconn);
2986

2987 2988 2989
    return n;

no_memory:
2990
    virReportOOMError();
2991 2992
    for (n = 0 ; n < nnames ; n++)
        VIR_FREE(names[n]);
2993
    testDriverUnlock(privconn);
2994
    return -1;
2995 2996 2997
}

static int testNumDefinedNetworks(virConnectPtr conn) {
2998
    testConnPtr privconn = conn->privateData;
2999
    int numInactive = 0, i;
3000

3001 3002 3003
    testDriverLock(privconn);
    for (i = 0 ; i < privconn->networks.count ; i++) {
        virNetworkObjLock(privconn->networks.objs[i]);
D
Daniel P. Berrange 已提交
3004
        if (!virNetworkObjIsActive(privconn->networks.objs[i]))
3005
            numInactive++;
3006 3007 3008
        virNetworkObjUnlock(privconn->networks.objs[i]);
    }
    testDriverUnlock(privconn);
3009

3010
    return numInactive;
3011 3012 3013
}

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

3017
    testDriverLock(privconn);
3018
    memset(names, 0, sizeof(*names)*nnames);
3019 3020
    for (i = 0 ; i < privconn->networks.count && n < nnames ; i++) {
        virNetworkObjLock(privconn->networks.objs[i]);
D
Daniel P. Berrange 已提交
3021
        if (!virNetworkObjIsActive(privconn->networks.objs[i]) &&
3022 3023
            !(names[n++] = strdup(privconn->networks.objs[i]->def->name))) {
            virNetworkObjUnlock(privconn->networks.objs[i]);
3024
            goto no_memory;
3025 3026 3027 3028
        }
        virNetworkObjUnlock(privconn->networks.objs[i]);
    }
    testDriverUnlock(privconn);
3029

3030 3031 3032
    return n;

no_memory:
3033
    virReportOOMError();
3034 3035
    for (n = 0 ; n < nnames ; n++)
        VIR_FREE(names[n]);
3036
    testDriverUnlock(privconn);
3037
    return -1;
3038 3039
}

3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050

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) {
3051
        virReportError(VIR_ERR_NO_NETWORK, NULL);
3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071
        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) {
3072
        virReportError(VIR_ERR_NO_NETWORK, NULL);
3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083
        goto cleanup;
    }
    ret = obj->persistent;

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


3084
static virNetworkPtr testNetworkCreate(virConnectPtr conn, const char *xml) {
3085
    testConnPtr privconn = conn->privateData;
3086
    virNetworkDefPtr def;
3087
    virNetworkObjPtr net = NULL;
3088
    virNetworkPtr ret = NULL;
3089

3090
    testDriverLock(privconn);
3091
    if ((def = virNetworkDefParseString(xml)) == NULL)
3092
        goto cleanup;
3093

3094
    if ((net = virNetworkAssignDef(&privconn->networks, def)) == NULL)
3095 3096
        goto cleanup;
    def = NULL;
3097
    net->active = 1;
3098

3099
    ret = virGetNetwork(conn, net->def->name, net->def->uuid);
3100

3101 3102
cleanup:
    virNetworkDefFree(def);
3103 3104 3105
    if (net)
        virNetworkObjUnlock(net);
    testDriverUnlock(privconn);
3106
    return ret;
3107 3108 3109
}

static virNetworkPtr testNetworkDefine(virConnectPtr conn, const char *xml) {
3110
    testConnPtr privconn = conn->privateData;
3111
    virNetworkDefPtr def;
3112
    virNetworkObjPtr net = NULL;
3113
    virNetworkPtr ret = NULL;
3114

3115
    testDriverLock(privconn);
3116
    if ((def = virNetworkDefParseString(xml)) == NULL)
3117
        goto cleanup;
3118

3119
    if ((net = virNetworkAssignDef(&privconn->networks, def)) == NULL)
3120 3121
        goto cleanup;
    def = NULL;
3122
    net->persistent = 1;
3123

3124
    ret = virGetNetwork(conn, net->def->name, net->def->uuid);
3125 3126 3127

cleanup:
    virNetworkDefFree(def);
3128 3129 3130
    if (net)
        virNetworkObjUnlock(net);
    testDriverUnlock(privconn);
3131
    return ret;
3132 3133 3134
}

static int testNetworkUndefine(virNetworkPtr network) {
3135 3136
    testConnPtr privconn = network->conn->privateData;
    virNetworkObjPtr privnet;
3137
    int ret = -1;
3138

3139
    testDriverLock(privconn);
3140 3141 3142 3143
    privnet = virNetworkFindByName(&privconn->networks,
                                   network->name);

    if (privnet == NULL) {
3144
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3145
        goto cleanup;
3146
    }
3147

D
Daniel P. Berrange 已提交
3148
    if (virNetworkObjIsActive(privnet)) {
3149 3150
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("Network '%s' is still running"), network->name);
3151
        goto cleanup;
3152 3153
    }

3154 3155
    virNetworkRemoveInactive(&privconn->networks,
                             privnet);
3156
    privnet = NULL;
3157
    ret = 0;
3158

3159
cleanup:
3160 3161 3162
    if (privnet)
        virNetworkObjUnlock(privnet);
    testDriverUnlock(privconn);
3163
    return ret;
3164 3165 3166
}

static int testNetworkStart(virNetworkPtr network) {
3167 3168
    testConnPtr privconn = network->conn->privateData;
    virNetworkObjPtr privnet;
3169
    int ret = -1;
3170

3171
    testDriverLock(privconn);
3172 3173
    privnet = virNetworkFindByName(&privconn->networks,
                                   network->name);
3174
    testDriverUnlock(privconn);
3175 3176

    if (privnet == NULL) {
3177
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3178
        goto cleanup;
3179
    }
3180

D
Daniel P. Berrange 已提交
3181
    if (virNetworkObjIsActive(privnet)) {
3182 3183
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("Network '%s' is already running"), network->name);
3184
        goto cleanup;
3185 3186
    }

3187
    privnet->active = 1;
3188
    ret = 0;
3189

3190
cleanup:
3191 3192
    if (privnet)
        virNetworkObjUnlock(privnet);
3193
    return ret;
3194 3195 3196
}

static int testNetworkDestroy(virNetworkPtr network) {
3197 3198
    testConnPtr privconn = network->conn->privateData;
    virNetworkObjPtr privnet;
3199
    int ret = -1;
3200

3201
    testDriverLock(privconn);
3202 3203 3204 3205
    privnet = virNetworkFindByName(&privconn->networks,
                                   network->name);

    if (privnet == NULL) {
3206
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3207
        goto cleanup;
3208
    }
3209

3210 3211 3212 3213
    privnet->active = 0;
    if (!privnet->persistent) {
        virNetworkRemoveInactive(&privconn->networks,
                                 privnet);
3214
        privnet = NULL;
3215
    }
3216 3217 3218
    ret = 0;

cleanup:
3219 3220 3221
    if (privnet)
        virNetworkObjUnlock(privnet);
    testDriverUnlock(privconn);
3222
    return ret;
3223 3224
}

3225
static char *testNetworkGetXMLDesc(virNetworkPtr network,
E
Eric Blake 已提交
3226
                                   unsigned int flags)
3227
{
3228 3229
    testConnPtr privconn = network->conn->privateData;
    virNetworkObjPtr privnet;
3230
    char *ret = NULL;
3231

E
Eric Blake 已提交
3232 3233
    virCheckFlags(0, NULL);

3234
    testDriverLock(privconn);
3235 3236
    privnet = virNetworkFindByName(&privconn->networks,
                                   network->name);
3237
    testDriverUnlock(privconn);
3238 3239

    if (privnet == NULL) {
3240
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3241
        goto cleanup;
3242
    }
3243

3244
    ret = virNetworkDefFormat(privnet->def, flags);
3245 3246

cleanup:
3247 3248
    if (privnet)
        virNetworkObjUnlock(privnet);
3249
    return ret;
3250 3251 3252
}

static char *testNetworkGetBridgeName(virNetworkPtr network) {
3253
    testConnPtr privconn = network->conn->privateData;
3254
    char *bridge = NULL;
3255 3256
    virNetworkObjPtr privnet;

3257
    testDriverLock(privconn);
3258 3259
    privnet = virNetworkFindByName(&privconn->networks,
                                   network->name);
3260
    testDriverUnlock(privconn);
3261 3262

    if (privnet == NULL) {
3263
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3264
        goto cleanup;
3265 3266
    }

3267
    if (!(privnet->def->bridge)) {
3268 3269 3270
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("network '%s' does not have a bridge name."),
                       privnet->def->name);
3271 3272 3273 3274
        goto cleanup;
    }

    if (!(bridge = strdup(privnet->def->bridge))) {
3275
        virReportOOMError();
3276
        goto cleanup;
3277
    }
3278 3279

cleanup:
3280 3281
    if (privnet)
        virNetworkObjUnlock(privnet);
3282 3283 3284 3285 3286
    return bridge;
}

static int testNetworkGetAutostart(virNetworkPtr network,
                                   int *autostart) {
3287 3288
    testConnPtr privconn = network->conn->privateData;
    virNetworkObjPtr privnet;
3289
    int ret = -1;
3290

3291
    testDriverLock(privconn);
3292 3293
    privnet = virNetworkFindByName(&privconn->networks,
                                   network->name);
3294
    testDriverUnlock(privconn);
3295 3296

    if (privnet == NULL) {
3297
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3298
        goto cleanup;
3299 3300
    }

3301
    *autostart = privnet->autostart;
3302 3303 3304
    ret = 0;

cleanup:
3305 3306
    if (privnet)
        virNetworkObjUnlock(privnet);
3307
    return ret;
3308 3309 3310 3311
}

static int testNetworkSetAutostart(virNetworkPtr network,
                                   int autostart) {
3312 3313
    testConnPtr privconn = network->conn->privateData;
    virNetworkObjPtr privnet;
3314
    int ret = -1;
3315

3316
    testDriverLock(privconn);
3317 3318
    privnet = virNetworkFindByName(&privconn->networks,
                                   network->name);
3319
    testDriverUnlock(privconn);
3320 3321

    if (privnet == NULL) {
3322
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3323
        goto cleanup;
3324 3325
    }

3326
    privnet->autostart = autostart ? 1 : 0;
3327 3328 3329
    ret = 0;

cleanup:
3330 3331
    if (privnet)
        virNetworkObjUnlock(privnet);
3332
    return ret;
3333
}
3334

C
Cole Robinson 已提交
3335

L
Laine Stump 已提交
3336 3337 3338 3339 3340 3341
/*
 * Physical host interface routines
 */

static virDrvOpenStatus testOpenInterface(virConnectPtr conn,
                                          virConnectAuthPtr auth ATTRIBUTE_UNUSED,
E
Eric Blake 已提交
3342
                                          unsigned int flags)
L
Laine Stump 已提交
3343
{
E
Eric Blake 已提交
3344 3345
    virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);

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

    if (ifacect > 1) {
3494
        virReportError(VIR_ERR_MULTIPLE_INTERFACES, NULL);
L
Laine Stump 已提交
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;
}

3506 3507 3508 3509 3510 3511 3512 3513 3514 3515
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) {
3516
        virReportError(VIR_ERR_NO_INTERFACE, NULL);
3517 3518 3519 3520 3521 3522 3523 3524 3525 3526
        goto cleanup;
    }
    ret = virInterfaceObjIsActive(obj);

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

3527
static int testInterfaceChangeBegin(virConnectPtr conn,
E
Eric Blake 已提交
3528
                                    unsigned int flags)
3529 3530 3531 3532
{
    testConnPtr privconn = conn->privateData;
    int ret = -1;

E
Eric Blake 已提交
3533 3534
    virCheckFlags(0, -1);

3535 3536
    testDriverLock(privconn);
    if (privconn->transaction_running) {
3537 3538
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("there is another transaction running."));
3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554
        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 已提交
3555
                                     unsigned int flags)
3556 3557 3558 3559
{
    testConnPtr privconn = conn->privateData;
    int ret = -1;

E
Eric Blake 已提交
3560 3561
    virCheckFlags(0, -1);

3562 3563 3564
    testDriverLock(privconn);

    if (!privconn->transaction_running) {
3565 3566 3567
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("no transaction running, "
                         "nothing to be committed."));
3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582
        goto cleanup;
    }

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

    ret = 0;

cleanup:
    testDriverUnlock(privconn);

    return ret;
}

static int testInterfaceChangeRollback(virConnectPtr conn,
E
Eric Blake 已提交
3583
                                       unsigned int flags)
3584 3585 3586 3587
{
    testConnPtr privconn = conn->privateData;
    int ret = -1;

E
Eric Blake 已提交
3588 3589
    virCheckFlags(0, -1);

3590 3591 3592
    testDriverLock(privconn);

    if (!privconn->transaction_running) {
3593 3594 3595
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("no transaction running, "
                         "nothing to rollback."));
3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612
        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;
}
3613

L
Laine Stump 已提交
3614
static char *testInterfaceGetXMLDesc(virInterfacePtr iface,
E
Eric Blake 已提交
3615
                                     unsigned int flags)
L
Laine Stump 已提交
3616 3617 3618 3619 3620
{
    testConnPtr privconn = iface->conn->privateData;
    virInterfaceObjPtr privinterface;
    char *ret = NULL;

E
Eric Blake 已提交
3621 3622
    virCheckFlags(0, NULL);

L
Laine Stump 已提交
3623 3624 3625 3626 3627 3628
    testDriverLock(privconn);
    privinterface = virInterfaceFindByName(&privconn->ifaces,
                                           iface->name);
    testDriverUnlock(privconn);

    if (privinterface == NULL) {
3629
        virReportError(VIR_ERR_NO_INTERFACE, __FUNCTION__);
L
Laine Stump 已提交
3630 3631 3632
        goto cleanup;
    }

3633
    ret = virInterfaceDefFormat(privinterface->def);
L
Laine Stump 已提交
3634 3635 3636 3637 3638 3639 3640 3641 3642

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


static virInterfacePtr testInterfaceDefineXML(virConnectPtr conn, const char *xmlStr,
E
Eric Blake 已提交
3643
                                              unsigned int flags)
L
Laine Stump 已提交
3644 3645 3646 3647 3648 3649
{
    testConnPtr privconn = conn->privateData;
    virInterfaceDefPtr def;
    virInterfaceObjPtr iface = NULL;
    virInterfacePtr ret = NULL;

E
Eric Blake 已提交
3650 3651
    virCheckFlags(0, NULL);

L
Laine Stump 已提交
3652
    testDriverLock(privconn);
3653
    if ((def = virInterfaceDefParseString(xmlStr)) == NULL)
L
Laine Stump 已提交
3654 3655
        goto cleanup;

3656
    if ((iface = virInterfaceAssignDef(&privconn->ifaces, def)) == NULL)
L
Laine Stump 已提交
3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680
        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) {
3681
        virReportError(VIR_ERR_NO_INTERFACE, NULL);
L
Laine Stump 已提交
3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694
        goto cleanup;
    }

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

cleanup:
    testDriverUnlock(privconn);
    return ret;
}

static int testInterfaceCreate(virInterfacePtr iface,
E
Eric Blake 已提交
3695
                               unsigned int flags)
L
Laine Stump 已提交
3696 3697 3698 3699 3700
{
    testConnPtr privconn = iface->conn->privateData;
    virInterfaceObjPtr privinterface;
    int ret = -1;

E
Eric Blake 已提交
3701 3702
    virCheckFlags(0, -1);

L
Laine Stump 已提交
3703 3704 3705 3706 3707
    testDriverLock(privconn);
    privinterface = virInterfaceFindByName(&privconn->ifaces,
                                           iface->name);

    if (privinterface == NULL) {
3708
        virReportError(VIR_ERR_NO_INTERFACE, NULL);
L
Laine Stump 已提交
3709 3710 3711 3712
        goto cleanup;
    }

    if (privinterface->active != 0) {
3713
        virReportError(VIR_ERR_OPERATION_INVALID, NULL);
L
Laine Stump 已提交
3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727
        goto cleanup;
    }

    privinterface->active = 1;
    ret = 0;

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

static int testInterfaceDestroy(virInterfacePtr iface,
E
Eric Blake 已提交
3728
                                unsigned int flags)
L
Laine Stump 已提交
3729 3730 3731 3732 3733
{
    testConnPtr privconn = iface->conn->privateData;
    virInterfaceObjPtr privinterface;
    int ret = -1;

E
Eric Blake 已提交
3734 3735
    virCheckFlags(0, -1);

L
Laine Stump 已提交
3736 3737 3738 3739 3740
    testDriverLock(privconn);
    privinterface = virInterfaceFindByName(&privconn->ifaces,
                                           iface->name);

    if (privinterface == NULL) {
3741
        virReportError(VIR_ERR_NO_INTERFACE, NULL);
L
Laine Stump 已提交
3742 3743 3744 3745
        goto cleanup;
    }

    if (privinterface->active == 0) {
3746
        virReportError(VIR_ERR_OPERATION_INVALID, NULL);
L
Laine Stump 已提交
3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761
        goto cleanup;
    }

    privinterface->active = 0;
    ret = 0;

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



C
Cole Robinson 已提交
3762 3763 3764 3765
/*
 * Storage Driver routines
 */

3766

3767
static int testStoragePoolObjSetDefaults(virStoragePoolObjPtr pool) {
C
Cole Robinson 已提交
3768 3769 3770 3771 3772 3773 3774

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

    pool->configFile = strdup("\0");
    if (!pool->configFile) {
3775
        virReportOOMError();
C
Cole Robinson 已提交
3776 3777 3778 3779 3780 3781
        return -1;
    }

    return 0;
}

3782 3783
static virDrvOpenStatus testStorageOpen(virConnectPtr conn,
                                        virConnectAuthPtr auth ATTRIBUTE_UNUSED,
E
Eric Blake 已提交
3784 3785 3786 3787
                                        unsigned int flags)
{
    virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);

3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799
    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;
}

3800

C
Cole Robinson 已提交
3801 3802 3803
static virStoragePoolPtr
testStoragePoolLookupByUUID(virConnectPtr conn,
                            const unsigned char *uuid) {
3804
    testConnPtr privconn = conn->privateData;
3805 3806
    virStoragePoolObjPtr pool;
    virStoragePoolPtr ret = NULL;
C
Cole Robinson 已提交
3807

3808
    testDriverLock(privconn);
3809
    pool = virStoragePoolObjFindByUUID(&privconn->pools, uuid);
3810
    testDriverUnlock(privconn);
3811 3812

    if (pool == NULL) {
3813
        virReportError(VIR_ERR_NO_STORAGE_POOL, NULL);
3814
        goto cleanup;
C
Cole Robinson 已提交
3815 3816
    }

3817 3818 3819
    ret = virGetStoragePool(conn, pool->def->name, pool->def->uuid);

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

static virStoragePoolPtr
testStoragePoolLookupByName(virConnectPtr conn,
                            const char *name) {
3828
    testConnPtr privconn = conn->privateData;
3829 3830
    virStoragePoolObjPtr pool;
    virStoragePoolPtr ret = NULL;
C
Cole Robinson 已提交
3831

3832
    testDriverLock(privconn);
3833
    pool = virStoragePoolObjFindByName(&privconn->pools, name);
3834
    testDriverUnlock(privconn);
3835 3836

    if (pool == NULL) {
3837
        virReportError(VIR_ERR_NO_STORAGE_POOL, NULL);
3838
        goto cleanup;
C
Cole Robinson 已提交
3839 3840
    }

3841 3842 3843
    ret = virGetStoragePool(conn, pool->def->name, pool->def->uuid);

cleanup:
3844 3845
    if (pool)
        virStoragePoolObjUnlock(pool);
3846
    return ret;
C
Cole Robinson 已提交
3847 3848 3849 3850 3851 3852 3853 3854 3855
}

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

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

3859
    testDriverLock(privconn);
C
Cole Robinson 已提交
3860 3861 3862
    for (i = 0 ; i < privconn->pools.count ; i++)
        if (virStoragePoolObjIsActive(privconn->pools.objs[i]))
            numActive++;
3863
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
3864 3865 3866 3867 3868 3869 3870 3871

    return numActive;
}

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

3875
    testDriverLock(privconn);
C
Cole Robinson 已提交
3876
    memset(names, 0, sizeof(*names)*nnames);
3877 3878
    for (i = 0 ; i < privconn->pools.count && n < nnames ; i++) {
        virStoragePoolObjLock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
3879
        if (virStoragePoolObjIsActive(privconn->pools.objs[i]) &&
3880 3881
            !(names[n++] = strdup(privconn->pools.objs[i]->def->name))) {
            virStoragePoolObjUnlock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
3882
            goto no_memory;
3883 3884 3885 3886
        }
        virStoragePoolObjUnlock(privconn->pools.objs[i]);
    }
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
3887 3888 3889 3890

    return n;

no_memory:
3891
    virReportOOMError();
C
Cole Robinson 已提交
3892 3893
    for (n = 0 ; n < nnames ; n++)
        VIR_FREE(names[n]);
3894
    testDriverUnlock(privconn);
3895
    return -1;
C
Cole Robinson 已提交
3896 3897 3898 3899
}

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

3903 3904 3905
    testDriverLock(privconn);
    for (i = 0 ; i < privconn->pools.count ; i++) {
        virStoragePoolObjLock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
3906 3907
        if (!virStoragePoolObjIsActive(privconn->pools.objs[i]))
            numInactive++;
3908 3909 3910
        virStoragePoolObjUnlock(privconn->pools.objs[i]);
    }
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
3911 3912 3913 3914 3915 3916 3917 3918

    return numInactive;
}

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

3922
    testDriverLock(privconn);
C
Cole Robinson 已提交
3923
    memset(names, 0, sizeof(*names)*nnames);
3924 3925
    for (i = 0 ; i < privconn->pools.count && n < nnames ; i++) {
        virStoragePoolObjLock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
3926
        if (!virStoragePoolObjIsActive(privconn->pools.objs[i]) &&
3927 3928
            !(names[n++] = strdup(privconn->pools.objs[i]->def->name))) {
            virStoragePoolObjUnlock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
3929
            goto no_memory;
3930 3931 3932 3933
        }
        virStoragePoolObjUnlock(privconn->pools.objs[i]);
    }
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
3934 3935 3936 3937

    return n;

no_memory:
3938
    virReportOOMError();
C
Cole Robinson 已提交
3939 3940
    for (n = 0 ; n < nnames ; n++)
        VIR_FREE(names[n]);
3941
    testDriverUnlock(privconn);
3942
    return -1;
C
Cole Robinson 已提交
3943 3944 3945
}


3946 3947 3948 3949 3950 3951 3952 3953 3954 3955
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) {
3956
        virReportError(VIR_ERR_NO_STORAGE_POOL, NULL);
3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976
        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) {
3977
        virReportError(VIR_ERR_NO_STORAGE_POOL, NULL);
3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989
        goto cleanup;
    }
    ret = obj->configFile ? 1 : 0;

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



C
Cole Robinson 已提交
3990
static int
3991
testStoragePoolStart(virStoragePoolPtr pool,
E
Eric Blake 已提交
3992 3993
                     unsigned int flags)
{
3994 3995
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
3996
    int ret = -1;
3997

E
Eric Blake 已提交
3998 3999
    virCheckFlags(0, -1);

4000
    testDriverLock(privconn);
4001 4002
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4003
    testDriverUnlock(privconn);
4004 4005

    if (privpool == NULL) {
4006
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4007
        goto cleanup;
4008 4009
    }

4010
    if (virStoragePoolObjIsActive(privpool)) {
4011 4012
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is already active"), pool->name);
4013 4014
        goto cleanup;
    }
C
Cole Robinson 已提交
4015 4016

    privpool->active = 1;
4017
    ret = 0;
C
Cole Robinson 已提交
4018

4019
cleanup:
4020 4021
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4022
    return ret;
C
Cole Robinson 已提交
4023 4024 4025
}

static char *
4026
testStorageFindPoolSources(virConnectPtr conn ATTRIBUTE_UNUSED,
4027 4028
                           const char *type,
                           const char *srcSpec,
E
Eric Blake 已提交
4029
                           unsigned int flags)
C
Cole Robinson 已提交
4030
{
4031 4032 4033 4034
    virStoragePoolSourcePtr source = NULL;
    int pool_type;
    char *ret = NULL;

E
Eric Blake 已提交
4035 4036
    virCheckFlags(0, NULL);

4037 4038
    pool_type = virStoragePoolTypeFromString(type);
    if (!pool_type) {
4039 4040
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("unknown storage pool type %s"), type);
4041 4042 4043 4044
        goto cleanup;
    }

    if (srcSpec) {
4045
        source = virStoragePoolDefParseSourceString(srcSpec, pool_type);
4046 4047 4048 4049 4050 4051 4052 4053 4054
        if (!source)
            goto cleanup;
    }

    switch (pool_type) {

    case VIR_STORAGE_POOL_LOGICAL:
        ret = strdup(defaultPoolSourcesLogicalXML);
        if (!ret)
4055
            virReportOOMError();
4056 4057 4058
        break;

    case VIR_STORAGE_POOL_NETFS:
4059
        if (!source || !source->hosts[0].name) {
4060 4061
            virReportError(VIR_ERR_INVALID_ARG,
                           "%s", _("hostname must be specified for netfs sources"));
4062 4063 4064 4065
            goto cleanup;
        }

        if (virAsprintf(&ret, defaultPoolSourcesNetFSXML,
4066
                        source->hosts[0].name) < 0)
4067
            virReportOOMError();
4068 4069 4070
        break;

    default:
4071 4072
        virReportError(VIR_ERR_NO_SUPPORT,
                       _("pool type '%s' does not support source discovery"), type);
4073 4074 4075 4076 4077
    }

cleanup:
    virStoragePoolSourceFree(source);
    return ret;
C
Cole Robinson 已提交
4078 4079 4080 4081 4082 4083
}


static virStoragePoolPtr
testStoragePoolCreate(virConnectPtr conn,
                      const char *xml,
E
Eric Blake 已提交
4084 4085
                      unsigned int flags)
{
4086
    testConnPtr privconn = conn->privateData;
C
Cole Robinson 已提交
4087
    virStoragePoolDefPtr def;
4088
    virStoragePoolObjPtr pool = NULL;
4089
    virStoragePoolPtr ret = NULL;
C
Cole Robinson 已提交
4090

E
Eric Blake 已提交
4091 4092
    virCheckFlags(0, NULL);

4093
    testDriverLock(privconn);
4094
    if (!(def = virStoragePoolDefParseString(xml)))
4095
        goto cleanup;
C
Cole Robinson 已提交
4096

4097 4098 4099 4100
    pool = virStoragePoolObjFindByUUID(&privconn->pools, def->uuid);
    if (!pool)
        pool = virStoragePoolObjFindByName(&privconn->pools, def->name);
    if (pool) {
4101 4102
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("storage pool already exists"));
4103
        goto cleanup;
C
Cole Robinson 已提交
4104 4105
    }

4106
    if (!(pool = virStoragePoolObjAssignDef(&privconn->pools, def)))
4107
        goto cleanup;
4108
    def = NULL;
C
Cole Robinson 已提交
4109

4110
    if (testStoragePoolObjSetDefaults(pool) == -1) {
C
Cole Robinson 已提交
4111
        virStoragePoolObjRemove(&privconn->pools, pool);
4112 4113
        pool = NULL;
        goto cleanup;
C
Cole Robinson 已提交
4114 4115 4116
    }
    pool->active = 1;

4117 4118 4119 4120
    ret = virGetStoragePool(conn, pool->def->name, pool->def->uuid);

cleanup:
    virStoragePoolDefFree(def);
4121 4122 4123
    if (pool)
        virStoragePoolObjUnlock(pool);
    testDriverUnlock(privconn);
4124
    return ret;
C
Cole Robinson 已提交
4125 4126 4127 4128 4129
}

static virStoragePoolPtr
testStoragePoolDefine(virConnectPtr conn,
                      const char *xml,
E
Eric Blake 已提交
4130 4131
                      unsigned int flags)
{
4132
    testConnPtr privconn = conn->privateData;
C
Cole Robinson 已提交
4133
    virStoragePoolDefPtr def;
4134
    virStoragePoolObjPtr pool = NULL;
4135
    virStoragePoolPtr ret = NULL;
C
Cole Robinson 已提交
4136

E
Eric Blake 已提交
4137 4138
    virCheckFlags(0, NULL);

4139
    testDriverLock(privconn);
4140
    if (!(def = virStoragePoolDefParseString(xml)))
4141
        goto cleanup;
C
Cole Robinson 已提交
4142 4143 4144 4145 4146

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

4147
    if (!(pool = virStoragePoolObjAssignDef(&privconn->pools, def)))
4148 4149
        goto cleanup;
    def = NULL;
C
Cole Robinson 已提交
4150

4151
    if (testStoragePoolObjSetDefaults(pool) == -1) {
C
Cole Robinson 已提交
4152
        virStoragePoolObjRemove(&privconn->pools, pool);
4153 4154
        pool = NULL;
        goto cleanup;
C
Cole Robinson 已提交
4155 4156
    }

4157 4158 4159 4160
    ret = virGetStoragePool(conn, pool->def->name, pool->def->uuid);

cleanup:
    virStoragePoolDefFree(def);
4161 4162 4163
    if (pool)
        virStoragePoolObjUnlock(pool);
    testDriverUnlock(privconn);
4164
    return ret;
C
Cole Robinson 已提交
4165 4166 4167
}

static int
4168 4169 4170
testStoragePoolUndefine(virStoragePoolPtr pool) {
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4171
    int ret = -1;
4172

4173
    testDriverLock(privconn);
4174 4175 4176 4177
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);

    if (privpool == NULL) {
4178
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4179
        goto cleanup;
4180 4181
    }

4182
    if (virStoragePoolObjIsActive(privpool)) {
4183 4184
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is already active"), pool->name);
4185 4186
        goto cleanup;
    }
C
Cole Robinson 已提交
4187 4188

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

4191
cleanup:
4192 4193 4194
    if (privpool)
        virStoragePoolObjUnlock(privpool);
    testDriverUnlock(privconn);
4195
    return ret;
C
Cole Robinson 已提交
4196 4197 4198
}

static int
4199
testStoragePoolBuild(virStoragePoolPtr pool,
E
Eric Blake 已提交
4200 4201
                     unsigned int flags)
{
4202 4203
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4204
    int ret = -1;
4205

E
Eric Blake 已提交
4206 4207
    virCheckFlags(0, -1);

4208
    testDriverLock(privconn);
4209 4210
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4211
    testDriverUnlock(privconn);
4212 4213

    if (privpool == NULL) {
4214
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4215
        goto cleanup;
4216 4217
    }

4218
    if (virStoragePoolObjIsActive(privpool)) {
4219 4220
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is already active"), pool->name);
4221 4222
        goto cleanup;
    }
4223
    ret = 0;
C
Cole Robinson 已提交
4224

4225
cleanup:
4226 4227
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4228
    return ret;
C
Cole Robinson 已提交
4229 4230 4231 4232
}


static int
4233 4234 4235
testStoragePoolDestroy(virStoragePoolPtr pool) {
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4236
    int ret = -1;
4237

4238
    testDriverLock(privconn);
4239 4240 4241 4242
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);

    if (privpool == NULL) {
4243
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4244
        goto cleanup;
4245 4246 4247
    }

    if (!virStoragePoolObjIsActive(privpool)) {
4248 4249
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), pool->name);
4250
        goto cleanup;
4251
    }
C
Cole Robinson 已提交
4252 4253 4254

    privpool->active = 0;

4255
    if (privpool->configFile == NULL) {
C
Cole Robinson 已提交
4256
        virStoragePoolObjRemove(&privconn->pools, privpool);
4257 4258
        privpool = NULL;
    }
4259
    ret = 0;
C
Cole Robinson 已提交
4260

4261
cleanup:
4262 4263 4264
    if (privpool)
        virStoragePoolObjUnlock(privpool);
    testDriverUnlock(privconn);
4265
    return ret;
C
Cole Robinson 已提交
4266 4267 4268 4269
}


static int
4270
testStoragePoolDelete(virStoragePoolPtr pool,
E
Eric Blake 已提交
4271 4272
                      unsigned int flags)
{
4273 4274
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4275
    int ret = -1;
4276

E
Eric Blake 已提交
4277 4278
    virCheckFlags(0, -1);

4279
    testDriverLock(privconn);
4280 4281
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4282
    testDriverUnlock(privconn);
4283 4284

    if (privpool == NULL) {
4285
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4286 4287 4288 4289
        goto cleanup;
    }

    if (virStoragePoolObjIsActive(privpool)) {
4290 4291
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is already active"), pool->name);
4292
        goto cleanup;
4293 4294
    }

4295
    ret = 0;
C
Cole Robinson 已提交
4296

4297
cleanup:
4298 4299
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4300
    return ret;
C
Cole Robinson 已提交
4301 4302 4303 4304
}


static int
4305
testStoragePoolRefresh(virStoragePoolPtr pool,
E
Eric Blake 已提交
4306 4307
                       unsigned int flags)
{
4308 4309
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4310
    int ret = -1;
4311

E
Eric Blake 已提交
4312 4313
    virCheckFlags(0, -1);

4314
    testDriverLock(privconn);
4315 4316
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4317
    testDriverUnlock(privconn);
4318 4319

    if (privpool == NULL) {
4320
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4321
        goto cleanup;
4322 4323 4324
    }

    if (!virStoragePoolObjIsActive(privpool)) {
4325 4326
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), pool->name);
4327
        goto cleanup;
4328
    }
4329
    ret = 0;
C
Cole Robinson 已提交
4330

4331
cleanup:
4332 4333
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4334
    return ret;
C
Cole Robinson 已提交
4335 4336 4337 4338
}


static int
4339
testStoragePoolGetInfo(virStoragePoolPtr pool,
C
Cole Robinson 已提交
4340
                       virStoragePoolInfoPtr info) {
4341 4342
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4343
    int ret = -1;
4344

4345
    testDriverLock(privconn);
4346 4347
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4348
    testDriverUnlock(privconn);
4349 4350

    if (privpool == NULL) {
4351
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4352
        goto cleanup;
4353
    }
C
Cole Robinson 已提交
4354 4355 4356 4357 4358 4359 4360 4361 4362

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

4365
cleanup:
4366 4367
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4368
    return ret;
C
Cole Robinson 已提交
4369 4370 4371
}

static char *
4372
testStoragePoolGetXMLDesc(virStoragePoolPtr pool,
E
Eric Blake 已提交
4373 4374
                          unsigned int flags)
{
4375 4376
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4377
    char *ret = NULL;
4378

E
Eric Blake 已提交
4379 4380
    virCheckFlags(0, NULL);

4381
    testDriverLock(privconn);
4382 4383
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4384
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
4385

4386
    if (privpool == NULL) {
4387
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4388
        goto cleanup;
4389 4390
    }

4391
    ret = virStoragePoolDefFormat(privpool->def);
4392 4393

cleanup:
4394 4395
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4396
    return ret;
C
Cole Robinson 已提交
4397 4398 4399
}

static int
4400
testStoragePoolGetAutostart(virStoragePoolPtr pool,
C
Cole Robinson 已提交
4401
                            int *autostart) {
4402 4403
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4404
    int ret = -1;
4405

4406
    testDriverLock(privconn);
4407 4408
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4409
    testDriverUnlock(privconn);
4410 4411

    if (privpool == NULL) {
4412
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4413
        goto cleanup;
4414
    }
C
Cole Robinson 已提交
4415 4416 4417 4418 4419 4420

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

4423
cleanup:
4424 4425
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4426
    return ret;
C
Cole Robinson 已提交
4427 4428 4429
}

static int
4430
testStoragePoolSetAutostart(virStoragePoolPtr pool,
C
Cole Robinson 已提交
4431
                            int autostart) {
4432 4433
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4434
    int ret = -1;
4435

4436
    testDriverLock(privconn);
4437 4438
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4439
    testDriverUnlock(privconn);
4440 4441

    if (privpool == NULL) {
4442
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4443
        goto cleanup;
4444
    }
C
Cole Robinson 已提交
4445 4446

    if (!privpool->configFile) {
4447 4448
        virReportError(VIR_ERR_INVALID_ARG,
                       "%s", _("pool has no config file"));
4449
        goto cleanup;
C
Cole Robinson 已提交
4450 4451 4452 4453
    }

    autostart = (autostart != 0);
    privpool->autostart = autostart;
4454 4455 4456
    ret = 0;

cleanup:
4457 4458
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4459
    return ret;
C
Cole Robinson 已提交
4460 4461 4462 4463
}


static int
4464 4465 4466
testStoragePoolNumVolumes(virStoragePoolPtr pool) {
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4467
    int ret = -1;
4468

4469
    testDriverLock(privconn);
4470 4471
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4472
    testDriverUnlock(privconn);
4473 4474

    if (privpool == NULL) {
4475
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4476
        goto cleanup;
4477 4478 4479
    }

    if (!virStoragePoolObjIsActive(privpool)) {
4480 4481
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), pool->name);
4482
        goto cleanup;
4483
    }
C
Cole Robinson 已提交
4484

4485 4486 4487
    ret = privpool->volumes.count;

cleanup:
4488 4489
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4490
    return ret;
C
Cole Robinson 已提交
4491 4492 4493
}

static int
4494
testStoragePoolListVolumes(virStoragePoolPtr pool,
C
Cole Robinson 已提交
4495 4496
                           char **const names,
                           int maxnames) {
4497 4498
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
C
Cole Robinson 已提交
4499 4500
    int i = 0, n = 0;

4501
    memset(names, 0, maxnames * sizeof(*names));
4502 4503

    testDriverLock(privconn);
4504 4505
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4506
    testDriverUnlock(privconn);
4507 4508

    if (privpool == NULL) {
4509
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4510
        goto cleanup;
4511 4512 4513 4514
    }


    if (!virStoragePoolObjIsActive(privpool)) {
4515 4516
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), pool->name);
4517
        goto cleanup;
4518 4519
    }

C
Cole Robinson 已提交
4520 4521
    for (i = 0 ; i < privpool->volumes.count && n < maxnames ; i++) {
        if ((names[n++] = strdup(privpool->volumes.objs[i]->name)) == NULL) {
4522
            virReportOOMError();
C
Cole Robinson 已提交
4523 4524 4525 4526
            goto cleanup;
        }
    }

4527
    virStoragePoolObjUnlock(privpool);
C
Cole Robinson 已提交
4528 4529 4530 4531 4532 4533
    return n;

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

4534
    memset(names, 0, maxnames * sizeof(*names));
4535 4536
    if (privpool)
        virStoragePoolObjUnlock(privpool);
C
Cole Robinson 已提交
4537 4538 4539 4540 4541
    return -1;
}


static virStorageVolPtr
4542
testStorageVolumeLookupByName(virStoragePoolPtr pool,
C
Cole Robinson 已提交
4543
                              const char *name ATTRIBUTE_UNUSED) {
4544 4545 4546
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol;
4547
    virStorageVolPtr ret = NULL;
4548

4549
    testDriverLock(privconn);
4550 4551
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4552
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
4553

4554
    if (privpool == NULL) {
4555
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4556
        goto cleanup;
4557 4558 4559 4560
    }


    if (!virStoragePoolObjIsActive(privpool)) {
4561 4562
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), pool->name);
4563
        goto cleanup;
4564 4565 4566 4567 4568
    }

    privvol = virStorageVolDefFindByName(privpool, name);

    if (!privvol) {
4569 4570
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching name '%s'"), name);
4571
        goto cleanup;
C
Cole Robinson 已提交
4572 4573
    }

4574 4575 4576 4577
    ret = virGetStorageVol(pool->conn, privpool->def->name,
                           privvol->name, privvol->key);

cleanup:
4578 4579
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4580
    return ret;
C
Cole Robinson 已提交
4581 4582 4583 4584 4585 4586
}


static virStorageVolPtr
testStorageVolumeLookupByKey(virConnectPtr conn,
                             const char *key) {
4587
    testConnPtr privconn = conn->privateData;
C
Cole Robinson 已提交
4588
    unsigned int i;
4589
    virStorageVolPtr ret = NULL;
C
Cole Robinson 已提交
4590

4591
    testDriverLock(privconn);
C
Cole Robinson 已提交
4592
    for (i = 0 ; i < privconn->pools.count ; i++) {
4593
        virStoragePoolObjLock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
4594
        if (virStoragePoolObjIsActive(privconn->pools.objs[i])) {
4595
            virStorageVolDefPtr privvol =
C
Cole Robinson 已提交
4596 4597
                virStorageVolDefFindByKey(privconn->pools.objs[i], key);

4598 4599 4600 4601 4602
            if (privvol) {
                ret = virGetStorageVol(conn,
                                       privconn->pools.objs[i]->def->name,
                                       privvol->name,
                                       privvol->key);
4603
                virStoragePoolObjUnlock(privconn->pools.objs[i]);
4604 4605
                break;
            }
C
Cole Robinson 已提交
4606
        }
4607
        virStoragePoolObjUnlock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
4608
    }
4609
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
4610

4611
    if (!ret)
4612 4613
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching key '%s'"), key);
4614 4615

    return ret;
C
Cole Robinson 已提交
4616 4617 4618 4619 4620
}

static virStorageVolPtr
testStorageVolumeLookupByPath(virConnectPtr conn,
                              const char *path) {
4621
    testConnPtr privconn = conn->privateData;
C
Cole Robinson 已提交
4622
    unsigned int i;
4623
    virStorageVolPtr ret = NULL;
C
Cole Robinson 已提交
4624

4625
    testDriverLock(privconn);
C
Cole Robinson 已提交
4626
    for (i = 0 ; i < privconn->pools.count ; i++) {
4627
        virStoragePoolObjLock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
4628
        if (virStoragePoolObjIsActive(privconn->pools.objs[i])) {
4629
            virStorageVolDefPtr privvol =
C
Cole Robinson 已提交
4630 4631
                virStorageVolDefFindByPath(privconn->pools.objs[i], path);

4632 4633 4634 4635 4636
            if (privvol) {
                ret = virGetStorageVol(conn,
                                       privconn->pools.objs[i]->def->name,
                                       privvol->name,
                                       privvol->key);
4637
                virStoragePoolObjUnlock(privconn->pools.objs[i]);
4638 4639
                break;
            }
C
Cole Robinson 已提交
4640
        }
4641
        virStoragePoolObjUnlock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
4642
    }
4643
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
4644

4645
    if (!ret)
4646 4647
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching path '%s'"), path);
4648 4649

    return ret;
C
Cole Robinson 已提交
4650 4651 4652
}

static virStorageVolPtr
4653
testStorageVolumeCreateXML(virStoragePoolPtr pool,
C
Cole Robinson 已提交
4654
                           const char *xmldesc,
E
Eric Blake 已提交
4655 4656
                           unsigned int flags)
{
4657 4658
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4659 4660
    virStorageVolDefPtr privvol = NULL;
    virStorageVolPtr ret = NULL;
4661

E
Eric Blake 已提交
4662 4663
    virCheckFlags(0, NULL);

4664
    testDriverLock(privconn);
4665 4666
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4667
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
4668

4669
    if (privpool == NULL) {
4670
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4671
        goto cleanup;
4672 4673 4674
    }

    if (!virStoragePoolObjIsActive(privpool)) {
4675 4676
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), pool->name);
4677
        goto cleanup;
4678
    }
C
Cole Robinson 已提交
4679

4680
    privvol = virStorageVolDefParseString(privpool->def, xmldesc);
4681
    if (privvol == NULL)
4682
        goto cleanup;
4683 4684

    if (virStorageVolDefFindByName(privpool, privvol->name)) {
4685 4686
        virReportError(VIR_ERR_OPERATION_FAILED,
                       "%s", _("storage vol already exists"));
4687
        goto cleanup;
C
Cole Robinson 已提交
4688 4689 4690
    }

    /* Make sure enough space */
4691
    if ((privpool->def->allocation + privvol->allocation) >
C
Cole Robinson 已提交
4692
         privpool->def->capacity) {
4693 4694 4695
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Not enough free space in pool for volume '%s'"),
                       privvol->name);
4696
        goto cleanup;
C
Cole Robinson 已提交
4697 4698 4699 4700
    }

    if (VIR_REALLOC_N(privpool->volumes.objs,
                      privpool->volumes.count+1) < 0) {
4701
        virReportOOMError();
4702
        goto cleanup;
C
Cole Robinson 已提交
4703 4704
    }

4705 4706 4707
    if (virAsprintf(&privvol->target.path, "%s/%s",
                    privpool->def->target.path,
                    privvol->name) == -1) {
4708
        virReportOOMError();
4709
        goto cleanup;
C
Cole Robinson 已提交
4710 4711
    }

4712 4713
    privvol->key = strdup(privvol->target.path);
    if (privvol->key == NULL) {
4714
        virReportOOMError();
4715
        goto cleanup;
C
Cole Robinson 已提交
4716 4717
    }

4718
    privpool->def->allocation += privvol->allocation;
C
Cole Robinson 已提交
4719 4720 4721
    privpool->def->available = (privpool->def->capacity -
                                privpool->def->allocation);

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

4724 4725
    ret = virGetStorageVol(pool->conn, privpool->def->name,
                           privvol->name, privvol->key);
4726
    privvol = NULL;
4727 4728 4729

cleanup:
    virStorageVolDefFree(privvol);
4730 4731
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4732
    return ret;
C
Cole Robinson 已提交
4733 4734
}

4735 4736 4737 4738
static virStorageVolPtr
testStorageVolumeCreateXMLFrom(virStoragePoolPtr pool,
                               const char *xmldesc,
                               virStorageVolPtr clonevol,
E
Eric Blake 已提交
4739 4740
                               unsigned int flags)
{
4741 4742 4743 4744 4745
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol = NULL, origvol = NULL;
    virStorageVolPtr ret = NULL;

E
Eric Blake 已提交
4746 4747
    virCheckFlags(0, NULL);

4748 4749 4750 4751 4752 4753
    testDriverLock(privconn);
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
    testDriverUnlock(privconn);

    if (privpool == NULL) {
4754
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4755 4756 4757 4758
        goto cleanup;
    }

    if (!virStoragePoolObjIsActive(privpool)) {
4759 4760
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), pool->name);
4761 4762 4763
        goto cleanup;
    }

4764
    privvol = virStorageVolDefParseString(privpool->def, xmldesc);
4765 4766 4767 4768
    if (privvol == NULL)
        goto cleanup;

    if (virStorageVolDefFindByName(privpool, privvol->name)) {
4769 4770
        virReportError(VIR_ERR_OPERATION_FAILED,
                       "%s", _("storage vol already exists"));
4771 4772 4773 4774 4775
        goto cleanup;
    }

    origvol = virStorageVolDefFindByName(privpool, clonevol->name);
    if (!origvol) {
4776 4777 4778
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching name '%s'"),
                       clonevol->name);
4779 4780 4781 4782 4783 4784
        goto cleanup;
    }

    /* Make sure enough space */
    if ((privpool->def->allocation + privvol->allocation) >
         privpool->def->capacity) {
4785 4786 4787
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Not enough free space in pool for volume '%s'"),
                       privvol->name);
4788 4789 4790 4791 4792 4793 4794
        goto cleanup;
    }
    privpool->def->available = (privpool->def->capacity -
                                privpool->def->allocation);

    if (VIR_REALLOC_N(privpool->volumes.objs,
                      privpool->volumes.count+1) < 0) {
4795
        virReportOOMError();
4796 4797 4798
        goto cleanup;
    }

4799 4800 4801
    if (virAsprintf(&privvol->target.path, "%s/%s",
                    privpool->def->target.path,
                    privvol->name) == -1) {
4802
        virReportOOMError();
4803 4804 4805 4806 4807
        goto cleanup;
    }

    privvol->key = strdup(privvol->target.path);
    if (privvol->key == NULL) {
4808
        virReportOOMError();
4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828
        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 已提交
4829
static int
4830
testStorageVolumeDelete(virStorageVolPtr vol,
E
Eric Blake 已提交
4831 4832
                        unsigned int flags)
{
4833 4834 4835
    testConnPtr privconn = vol->conn->privateData;
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol;
C
Cole Robinson 已提交
4836
    int i;
4837
    int ret = -1;
C
Cole Robinson 已提交
4838

E
Eric Blake 已提交
4839 4840
    virCheckFlags(0, -1);

4841
    testDriverLock(privconn);
4842 4843
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           vol->pool);
4844
    testDriverUnlock(privconn);
4845 4846

    if (privpool == NULL) {
4847
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4848
        goto cleanup;
4849 4850 4851 4852 4853 4854
    }


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

    if (privvol == NULL) {
4855 4856 4857
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching name '%s'"),
                       vol->name);
4858
        goto cleanup;
4859 4860 4861
    }

    if (!virStoragePoolObjIsActive(privpool)) {
4862 4863
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), vol->pool);
4864
        goto cleanup;
4865 4866 4867
    }


C
Cole Robinson 已提交
4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890
    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;
        }
    }
4891
    ret = 0;
C
Cole Robinson 已提交
4892

4893
cleanup:
4894 4895
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4896
    return ret;
C
Cole Robinson 已提交
4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912
}


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
4913
testStorageVolumeGetInfo(virStorageVolPtr vol,
C
Cole Robinson 已提交
4914
                         virStorageVolInfoPtr info) {
4915 4916 4917
    testConnPtr privconn = vol->conn->privateData;
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol;
4918
    int ret = -1;
4919

4920
    testDriverLock(privconn);
4921 4922
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           vol->pool);
4923
    testDriverUnlock(privconn);
4924 4925

    if (privpool == NULL) {
4926
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4927
        goto cleanup;
4928 4929 4930 4931 4932
    }

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

    if (privvol == NULL) {
4933 4934 4935
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching name '%s'"),
                       vol->name);
4936
        goto cleanup;
4937 4938 4939
    }

    if (!virStoragePoolObjIsActive(privpool)) {
4940 4941
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), vol->pool);
4942
        goto cleanup;
4943
    }
C
Cole Robinson 已提交
4944 4945 4946 4947 4948

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

4951
cleanup:
4952 4953
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4954
    return ret;
C
Cole Robinson 已提交
4955 4956 4957
}

static char *
4958
testStorageVolumeGetXMLDesc(virStorageVolPtr vol,
E
Eric Blake 已提交
4959 4960
                            unsigned int flags)
{
4961 4962 4963
    testConnPtr privconn = vol->conn->privateData;
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol;
4964
    char *ret = NULL;
4965

E
Eric Blake 已提交
4966 4967
    virCheckFlags(0, NULL);

4968
    testDriverLock(privconn);
4969 4970
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           vol->pool);
4971
    testDriverUnlock(privconn);
4972 4973

    if (privpool == NULL) {
4974
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4975
        goto cleanup;
4976 4977 4978 4979 4980
    }

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

    if (privvol == NULL) {
4981 4982 4983
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching name '%s'"),
                       vol->name);
4984
        goto cleanup;
4985
    }
C
Cole Robinson 已提交
4986

4987
    if (!virStoragePoolObjIsActive(privpool)) {
4988 4989
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), vol->pool);
4990
        goto cleanup;
4991 4992
    }

4993
    ret = virStorageVolDefFormat(privpool->def, privvol);
4994 4995

cleanup:
4996 4997
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4998
    return ret;
C
Cole Robinson 已提交
4999 5000 5001
}

static char *
5002 5003 5004 5005
testStorageVolumeGetPath(virStorageVolPtr vol) {
    testConnPtr privconn = vol->conn->privateData;
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol;
5006
    char *ret = NULL;
5007

5008
    testDriverLock(privconn);
5009 5010
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           vol->pool);
5011
    testDriverUnlock(privconn);
5012 5013

    if (privpool == NULL) {
5014
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
5015
        goto cleanup;
5016 5017 5018 5019 5020
    }

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

    if (privvol == NULL) {
5021 5022 5023
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching name '%s'"),
                       vol->name);
5024
        goto cleanup;
5025 5026 5027
    }

    if (!virStoragePoolObjIsActive(privpool)) {
5028 5029
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), vol->pool);
5030
        goto cleanup;
5031 5032
    }

C
Cole Robinson 已提交
5033
    ret = strdup(privvol->target.path);
5034
    if (ret == NULL)
5035
        virReportOOMError();
5036 5037

cleanup:
5038 5039
    if (privpool)
        virStoragePoolObjUnlock(privpool);
C
Cole Robinson 已提交
5040 5041 5042
    return ret;
}

5043

5044
/* Node device implementations */
5045 5046
static virDrvOpenStatus testDevMonOpen(virConnectPtr conn,
                                       virConnectAuthPtr auth ATTRIBUTE_UNUSED,
E
Eric Blake 已提交
5047 5048 5049 5050
                                       unsigned int flags)
{
    virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);

5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062
    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;
}

5063 5064 5065
static int
testNodeNumOfDevices(virConnectPtr conn,
                     const char *cap,
E
Eric Blake 已提交
5066
                     unsigned int flags)
5067 5068 5069 5070 5071
{
    testConnPtr driver = conn->privateData;
    int ndevs = 0;
    unsigned int i;

E
Eric Blake 已提交
5072 5073
    virCheckFlags(0, -1);

5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088
    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 已提交
5089
                    unsigned int flags)
5090 5091 5092 5093 5094
{
    testConnPtr driver = conn->privateData;
    int ndevs = 0;
    unsigned int i;

E
Eric Blake 已提交
5095 5096
    virCheckFlags(0, -1);

5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132
    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) {
5133
        virReportError(VIR_ERR_NO_NODE_DEVICE, NULL);
5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145
        goto cleanup;
    }

    ret = virGetNodeDevice(conn, name);

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

static char *
5146
testNodeDeviceGetXMLDesc(virNodeDevicePtr dev,
E
Eric Blake 已提交
5147
                         unsigned int flags)
5148 5149 5150 5151 5152
{
    testConnPtr driver = dev->conn->privateData;
    virNodeDeviceObjPtr obj;
    char *ret = NULL;

E
Eric Blake 已提交
5153 5154
    virCheckFlags(0, NULL);

5155 5156 5157 5158 5159
    testDriverLock(driver);
    obj = virNodeDeviceFindByName(&driver->devs, dev->name);
    testDriverUnlock(driver);

    if (!obj) {
5160 5161 5162
        virReportError(VIR_ERR_NO_NODE_DEVICE,
                       _("no node device with matching name '%s'"),
                       dev->name);
5163 5164 5165
        goto cleanup;
    }

5166
    ret = virNodeDeviceDefFormat(obj->def);
5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185

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) {
5186 5187 5188
        virReportError(VIR_ERR_NO_NODE_DEVICE,
                       _("no node device with matching name '%s'"),
                       dev->name);
5189 5190 5191 5192 5193 5194
        goto cleanup;
    }

    if (obj->def->parent) {
        ret = strdup(obj->def->parent);
        if (!ret)
5195
            virReportOOMError();
5196
    } else {
5197 5198
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("no parent for this device"));
5199 5200 5201 5202 5203 5204 5205 5206
    }

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

5207

5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221
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) {
5222 5223 5224
        virReportError(VIR_ERR_NO_NODE_DEVICE,
                       _("no node device with matching name '%s'"),
                       dev->name);
5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252
        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) {
5253 5254 5255
        virReportError(VIR_ERR_NO_NODE_DEVICE,
                       _("no node device with matching name '%s'"),
                       dev->name);
5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276
        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;
}

5277 5278 5279
static virNodeDevicePtr
testNodeDeviceCreateXML(virConnectPtr conn,
                        const char *xmlDesc,
E
Eric Blake 已提交
5280
                        unsigned int flags)
5281 5282 5283 5284 5285 5286 5287 5288 5289
{
    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 已提交
5290 5291
    virCheckFlags(0, NULL);

5292 5293
    testDriverLock(driver);

5294
    def = virNodeDeviceDefParseString(xmlDesc, CREATE_DEVICE, NULL);
5295 5296 5297 5298 5299
    if (def == NULL) {
        goto cleanup;
    }

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

5304
    if (virNodeDeviceGetParentHost(&driver->devs,
5305 5306 5307 5308 5309 5310 5311 5312 5313 5314
                                   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))) {
5315
        virReportOOMError();
5316 5317 5318 5319 5320 5321 5322 5323 5324 5325
        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;

5326
        caps->data.scsi_host.host = virRandomBits(10);
5327 5328 5329 5330
        caps = caps->next;
    }


5331
    if (!(obj = virNodeDeviceAssignDef(&driver->devs, def))) {
5332 5333 5334 5335 5336 5337 5338 5339
        goto cleanup;
    }
    virNodeDeviceObjUnlock(obj);

    dev = virGetNodeDevice(conn, def->name);
    def = NULL;
cleanup:
    testDriverUnlock(driver);
5340
    virNodeDeviceDefFree(def);
5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359
    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) {
5360
        virReportError(VIR_ERR_NO_NODE_DEVICE, NULL);
5361 5362 5363
        goto out;
    }

5364
    if (virNodeDeviceGetWWNs(obj->def, &wwnn, &wwpn) == -1) {
5365 5366 5367 5368 5369
        goto out;
    }

    parent_name = strdup(obj->def->parent);
    if (parent_name == NULL) {
5370
        virReportOOMError();
5371 5372 5373 5374 5375 5376 5377 5378 5379 5380
        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 */
5381
    if (virNodeDeviceGetParentHost(&driver->devs,
5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400
                                   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;
}

5401 5402

/* Domain event implementations */
5403
static int
5404 5405 5406 5407
testDomainEventRegister(virConnectPtr conn,
                        virConnectDomainEventCallback callback,
                        void *opaque,
                        virFreeCallback freecb)
5408 5409 5410 5411 5412
{
    testConnPtr driver = conn->privateData;
    int ret;

    testDriverLock(driver);
5413 5414 5415
    ret = virDomainEventStateRegister(conn,
                                      driver->domainEventState,
                                      callback, opaque, freecb);
5416 5417 5418 5419 5420
    testDriverUnlock(driver);

    return ret;
}

5421

5422
static int
5423 5424
testDomainEventDeregister(virConnectPtr conn,
                          virConnectDomainEventCallback callback)
5425 5426 5427 5428 5429
{
    testConnPtr driver = conn->privateData;
    int ret;

    testDriverLock(driver);
5430 5431 5432
    ret = virDomainEventStateDeregister(conn,
                                        driver->domainEventState,
                                        callback);
5433 5434 5435 5436 5437
    testDriverUnlock(driver);

    return ret;
}

5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450

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

    testDriverLock(driver);
5451 5452 5453 5454
    if (virDomainEventStateRegisterID(conn,
                                      driver->domainEventState,
                                      dom, eventID,
                                      callback, opaque, freecb, &ret) < 0)
5455
        ret = -1;
5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468
    testDriverUnlock(driver);

    return ret;
}

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

    testDriverLock(driver);
5469 5470 5471
    ret = virDomainEventStateDeregisterID(conn,
                                          driver->domainEventState,
                                          callbackID);
5472 5473 5474 5475 5476 5477
    testDriverUnlock(driver);

    return ret;
}


5478 5479 5480 5481
/* driver must be locked before calling */
static void testDomainEventQueue(testConnPtr driver,
                                 virDomainEventPtr event)
{
5482
    virDomainEventStateQueue(driver->domainEventState, event);
5483 5484
}

5485 5486
static virDrvOpenStatus testSecretOpen(virConnectPtr conn,
                                       virConnectAuthPtr auth ATTRIBUTE_UNUSED,
E
Eric Blake 已提交
5487 5488 5489 5490
                                       unsigned int flags)
{
    virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);

5491 5492 5493
    if (STRNEQ(conn->driver->name, "Test"))
        return VIR_DRV_OPEN_DECLINED;

5494
    conn->secretPrivateData = conn->privateData;
5495 5496 5497 5498 5499 5500 5501
    return VIR_DRV_OPEN_SUCCESS;
}

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

5503 5504 5505

static virDrvOpenStatus testNWFilterOpen(virConnectPtr conn,
                                         virConnectAuthPtr auth ATTRIBUTE_UNUSED,
E
Eric Blake 已提交
5506 5507 5508 5509
                                         unsigned int flags)
{
    virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);

5510 5511 5512
    if (STRNEQ(conn->driver->name, "Test"))
        return VIR_DRV_OPEN_DECLINED;

5513
    conn->nwfilterPrivateData = conn->privateData;
5514 5515 5516 5517 5518 5519 5520 5521
    return VIR_DRV_OPEN_SUCCESS;
}

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

5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538
static int testListAllDomains(virConnectPtr conn,
                              virDomainPtr **domains,
                              unsigned int flags)
{
    testConnPtr privconn = conn->privateData;
    int ret;

    virCheckFlags(VIR_CONNECT_LIST_FILTERS_ALL, -1);

    testDriverLock(privconn);
    ret = virDomainList(conn, privconn->domains.objs, domains, flags);
    testDriverUnlock(privconn);

    return ret;
}


5539
static virDriver testDriver = {
5540 5541
    .no = VIR_DRV_TEST,
    .name = "Test",
5542 5543 5544 5545 5546 5547 5548 5549 5550
    .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 */
5551
    .listAllDomains = testListAllDomains, /* 0.9.13 */
5552 5553 5554 5555 5556 5557 5558
    .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 */
5559
    .domainShutdownFlags = testShutdownDomainFlags, /* 0.9.10 */
5560 5561 5562 5563 5564 5565 5566 5567 5568
    .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 */
5569
    .domainSaveFlags = testDomainSaveFlags, /* 0.9.4 */
5570
    .domainRestore = testDomainRestore, /* 0.3.2 */
5571
    .domainRestoreFlags = testDomainRestoreFlags, /* 0.9.4 */
5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585
    .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 */
5586
    .domainUndefineFlags = testDomainUndefineFlags, /* 0.9.4 */
5587 5588 5589 5590
    .domainGetAutostart = testDomainGetAutostart, /* 0.3.2 */
    .domainSetAutostart = testDomainSetAutostart, /* 0.3.2 */
    .domainGetSchedulerType = testDomainGetSchedulerType, /* 0.3.2 */
    .domainGetSchedulerParameters = testDomainGetSchedulerParams, /* 0.3.2 */
5591
    .domainGetSchedulerParametersFlags = testDomainGetSchedulerParamsFlags, /* 0.9.2 */
5592
    .domainSetSchedulerParameters = testDomainSetSchedulerParams, /* 0.3.2 */
5593
    .domainSetSchedulerParametersFlags = testDomainSetSchedulerParamsFlags, /* 0.9.2 */
5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605
    .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 */
5606
    .isAlive = testIsAlive, /* 0.9.8 */
5607 5608 5609 5610
};

static virNetworkDriver testNetworkDriver = {
    "Test",
5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629
    .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 */
5630 5631
};

L
Laine Stump 已提交
5632 5633
static virInterfaceDriver testInterfaceDriver = {
    "Test",                     /* name */
5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647
    .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 */
5648 5649 5650
    .interfaceChangeBegin = testInterfaceChangeBegin,   /* 0.9.2 */
    .interfaceChangeCommit = testInterfaceChangeCommit,  /* 0.9.2 */
    .interfaceChangeRollback = testInterfaceChangeRollback, /* 0.9.2 */
L
Laine Stump 已提交
5651 5652 5653
};


5654 5655
static virStorageDriver testStorageDriver = {
    .name = "Test",
5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692
    .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 */
5693 5694
};

5695 5696
static virDeviceMonitor testDevMonitor = {
    .name = "Test",
5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708
    .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 */
5709 5710
};

5711 5712
static virSecretDriver testSecretDriver = {
    .name = "Test",
5713 5714
    .open = testSecretOpen, /* 0.7.1 */
    .close = testSecretClose, /* 0.7.1 */
5715
};
5716 5717


5718 5719
static virNWFilterDriver testNWFilterDriver = {
    .name = "Test",
5720 5721
    .open = testNWFilterOpen, /* 0.8.0 */
    .close = testNWFilterClose, /* 0.8.0 */
5722 5723
};

5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735
/**
 * testRegister:
 *
 * Registers the test driver
 */
int
testRegister(void)
{
    if (virRegisterDriver(&testDriver) < 0)
        return -1;
    if (virRegisterNetworkDriver(&testNetworkDriver) < 0)
        return -1;
L
Laine Stump 已提交
5736 5737
    if (virRegisterInterfaceDriver(&testInterfaceDriver) < 0)
        return -1;
5738 5739
    if (virRegisterStorageDriver(&testStorageDriver) < 0)
        return -1;
5740 5741
    if (virRegisterDeviceMonitor(&testDevMonitor) < 0)
        return -1;
5742 5743
    if (virRegisterSecretDriver(&testSecretDriver) < 0)
        return -1;
5744 5745
    if (virRegisterNWFilterDriver(&testNWFilterDriver) < 0)
        return -1;
5746

5747 5748
    return 0;
}