test_driver.c 157.4 KB
Newer Older
1 2 3
/*
 * test.c: A "mock" hypervisor for use by application unit tests
 *
E
Eric Blake 已提交
4
 * Copyright (C) 2006-2011 Red Hat, Inc.
5
 * Copyright (C) 2006 Daniel P. Berrange
6
 *
7 8 9 10 11 12 13 14 15 16 17 18 19
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
20 21 22 23
 *
 * Daniel Berrange <berrange@redhat.com>
 */

24
#include <config.h>
25

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

34 35

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

54 55
#define VIR_FROM_THIS VIR_FROM_TEST

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

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

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

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

#define MAX_CELLS 128
76

77
struct _testConn {
78
    virMutex lock;
79

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

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

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

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

114

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

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


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

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

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

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

    return priv;
}

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

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


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

163 164
    if ((caps = virCapabilitiesNew(TEST_MODEL, 0, 0)) == NULL)
        goto no_memory;
165

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

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

177 178 179 180 181 182 183 184 185 186
    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;
187

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

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

202 203 204
    caps->privateDataAllocFunc = testDomainObjPrivateAlloc;
    caps->privateDataFreeFunc = testDomainObjPrivateFree;

205 206 207 208 209 210 211 212
    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;

213
    return caps;
214

215
no_memory:
216
    virReportOOMError();
217 218
    virCapabilitiesFree(caps);
    return NULL;
219 220
}

221

222 223 224 225 226 227 228 229 230 231
static const char *defaultDomainXML =
"<domain type='test'>"
"  <name>test</name>"
"  <memory>8388608</memory>"
"  <currentMemory>2097152</currentMemory>"
"  <vcpu>2</vcpu>"
"  <os>"
"    <type>hvm</type>"
"  </os>"
"</domain>";
232 233


234 235 236 237 238 239 240 241 242 243 244
static const char *defaultNetworkXML =
"<network>"
"  <name>default</name>"
"  <bridge name='virbr0' />"
"  <forward/>"
"  <ip address='192.168.122.1' netmask='255.255.255.0'>"
"    <dhcp>"
"      <range start='192.168.122.2' end='192.168.122.254' />"
"    </dhcp>"
"  </ip>"
"</network>";
245

L
Laine Stump 已提交
246 247 248 249 250 251 252 253 254 255 256
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 已提交
257 258 259 260 261 262 263 264
static const char *defaultPoolXML =
"<pool type='dir'>"
"  <name>default-pool</name>"
"  <target>"
"    <path>/default-pool</path>"
"  </target>"
"</pool>";

265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
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";

288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
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>";

306
static const unsigned long long defaultPoolCap = (100 * 1024 * 1024 * 1024ull);
C
Cole Robinson 已提交
307 308
static const unsigned long long defaultPoolAlloc = 0;

309
static int testStoragePoolObjSetDefaults(virStoragePoolObjPtr pool);
310
static int testNodeGetInfo(virConnectPtr conn, virNodeInfoPtr info);
311

312
static char *
313
testDomainGenerateIfname(virDomainDefPtr domdef) {
314 315 316 317 318 319 320 321
    int maxif = 1024;
    int ifctr, i;

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

        if (virAsprintf(&ifname, "testnet%d", ifctr) < 0) {
322
            virReportOOMError();
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
            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;
    }

339
    testError(VIR_ERR_INTERNAL_ERROR,
340 341 342 343
              _("Exceeded max iface limit %d"), maxif);
    return NULL;
}

344
static int
345
testDomainGenerateIfnames(virDomainDefPtr domdef)
346 347 348 349 350 351 352 353
{
    int i = 0;

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

354
        ifname = testDomainGenerateIfname(domdef);
355
        if (!ifname)
356
            return -1;
357 358 359 360

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

361
    return 0;
362 363
}

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

    if (VIR_REALLOC_N(privdata->cpumaps, nvcpus * cpumaplen) < 0) {
433
        virReportOOMError();
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
        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;
    }

450
    dom->def->vcpus = nvcpus;
451 452 453 454 455
    ret = 0;
cleanup:
    return ret;
}

456 457
static void
testDomainShutdownState(virDomainPtr domain,
J
Jiri Denemark 已提交
458 459
                        virDomainObjPtr privdom,
                        virDomainShutoffReason reason)
460 461 462 463 464 465 466
{
    if (privdom->newDef) {
        virDomainDefFree(privdom->def);
        privdom->def = privdom->newDef;
        privdom->newDef = NULL;
    }

J
Jiri Denemark 已提交
467
    virDomainObjSetState(privdom, VIR_DOMAIN_SHUTOFF, reason);
468 469 470 471 472
    privdom->def->id = -1;
    if (domain)
        domain->id = -1;
}

473
/* Set up domain runtime state */
474 475
static int
testDomainStartState(virConnectPtr conn,
J
Jiri Denemark 已提交
476 477
                     virDomainObjPtr dom,
                     virDomainRunningReason reason)
478 479
{
    testConnPtr privconn = conn->privateData;
480
    int ret = -1;
481

482 483 484
    if (testDomainUpdateVCPUs(conn, dom, dom->def->vcpus, 1) < 0)
        goto cleanup;

J
Jiri Denemark 已提交
485
    virDomainObjSetState(dom, VIR_DOMAIN_RUNNING, reason);
486 487
    dom->def->id = privconn->nextDomID++;

488
    if (virDomainObjSetDefTransient(privconn->caps, dom, false) < 0) {
489 490 491
        goto cleanup;
    }

492 493
    ret = 0;
cleanup:
494
    if (ret < 0)
J
Jiri Denemark 已提交
495
        testDomainShutdownState(NULL, dom, VIR_DOMAIN_SHUTOFF_FAILED);
496
    return ret;
497
}
498

499
static int testOpenDefault(virConnectPtr conn) {
500
    int u;
501
    testConnPtr privconn;
502 503 504 505
    virDomainDefPtr domdef = NULL;
    virDomainObjPtr domobj = NULL;
    virNetworkDefPtr netdef = NULL;
    virNetworkObjPtr netobj = NULL;
L
Laine Stump 已提交
506 507
    virInterfaceDefPtr interfacedef = NULL;
    virInterfaceObjPtr interfaceobj = NULL;
C
Cole Robinson 已提交
508 509
    virStoragePoolDefPtr pooldef = NULL;
    virStoragePoolObjPtr poolobj = NULL;
510 511
    virNodeDeviceDefPtr nodedef = NULL;
    virNodeDeviceObjPtr nodeobj = NULL;
512

513
    if (VIR_ALLOC(privconn) < 0) {
514
        virReportOOMError();
515 516
        return VIR_DRV_OPEN_ERROR;
    }
517
    if (virMutexInit(&privconn->lock) < 0) {
518
        testError(VIR_ERR_INTERNAL_ERROR,
519 520 521 522 523
                  "%s", _("cannot initialize mutex"));
        VIR_FREE(privconn);
        return VIR_DRV_OPEN_ERROR;
    }

524
    testDriverLock(privconn);
525
    conn->privateData = privconn;
526

527 528 529
    if (virDomainObjListInit(&privconn->domains) < 0)
        goto error;

530
    memmove(&privconn->nodeInfo, &defaultNodeInfo, sizeof(defaultNodeInfo));
531

532
    /* Numa setup */
533 534 535 536 537 538 539 540 541
    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;
    }

542 543 544 545 546
    if (!(privconn->caps = testBuildCapabilities(conn)))
        goto error;

    privconn->nextDomID = 1;

M
Matthias Bolte 已提交
547 548
    if (!(domdef = virDomainDefParseString(privconn->caps, defaultDomainXML,
                                           1 << VIR_DOMAIN_VIRT_TEST,
549
                                           VIR_DOMAIN_XML_INACTIVE)))
550
        goto error;
M
Matthias Bolte 已提交
551

552
    if (testDomainGenerateIfnames(domdef) < 0)
553
        goto error;
554
    if (!(domobj = virDomainAssignDef(privconn->caps,
555
                                      &privconn->domains, domdef, false)))
556 557
        goto error;
    domdef = NULL;
558

559
    domobj->persistent = 1;
J
Jiri Denemark 已提交
560
    if (testDomainStartState(conn, domobj, VIR_DOMAIN_RUNNING_BOOTED) < 0) {
561 562 563 564
        virDomainObjUnlock(domobj);
        goto error;
    }

565
    virDomainObjUnlock(domobj);
566

567
    if (!(netdef = virNetworkDefParseString(defaultNetworkXML)))
568
        goto error;
569
    if (!(netobj = virNetworkAssignDef(&privconn->networks, netdef))) {
570 571 572 573 574
        virNetworkDefFree(netdef);
        goto error;
    }
    netobj->active = 1;
    netobj->persistent = 1;
575
    virNetworkObjUnlock(netobj);
576

577
    if (!(interfacedef = virInterfaceDefParseString(defaultInterfaceXML)))
L
Laine Stump 已提交
578
        goto error;
579
    if (!(interfaceobj = virInterfaceAssignDef(&privconn->ifaces, interfacedef))) {
L
Laine Stump 已提交
580 581 582 583 584 585
        virInterfaceDefFree(interfacedef);
        goto error;
    }
    interfaceobj->active = 1;
    virInterfaceObjUnlock(interfaceobj);

586
    if (!(pooldef = virStoragePoolDefParseString(defaultPoolXML)))
C
Cole Robinson 已提交
587 588
        goto error;

589
    if (!(poolobj = virStoragePoolObjAssignDef(&privconn->pools,
C
Cole Robinson 已提交
590 591 592 593
                                               pooldef))) {
        virStoragePoolDefFree(pooldef);
        goto error;
    }
594

595
    if (testStoragePoolObjSetDefaults(poolobj) == -1) {
596
        virStoragePoolObjUnlock(poolobj);
C
Cole Robinson 已提交
597
        goto error;
598
    }
C
Cole Robinson 已提交
599
    poolobj->active = 1;
600
    virStoragePoolObjUnlock(poolobj);
C
Cole Robinson 已提交
601

602
    /* Init default node device */
603
    if (!(nodedef = virNodeDeviceDefParseString(defaultNodeXML, 0)))
604
        goto error;
605
    if (!(nodeobj = virNodeDeviceAssignDef(&privconn->devs,
606 607 608 609 610 611
                                           nodedef))) {
        virNodeDeviceDefFree(nodedef);
        goto error;
    }
    virNodeDeviceObjUnlock(nodeobj);

612
    testDriverUnlock(privconn);
613

614 615 616
    return VIR_DRV_OPEN_SUCCESS;

error:
617
    virDomainObjListDeinit(&privconn->domains);
618
    virNetworkObjListFree(&privconn->networks);
L
Laine Stump 已提交
619
    virInterfaceObjListFree(&privconn->ifaces);
C
Cole Robinson 已提交
620
    virStoragePoolObjListFree(&privconn->pools);
621
    virNodeDeviceObjListFree(&privconn->devs);
622
    virCapabilitiesFree(privconn->caps);
623
    testDriverUnlock(privconn);
624
    conn->privateData = NULL;
625
    VIR_FREE(privconn);
626
    virDomainDefFree(domdef);
627
    return VIR_DRV_OPEN_ERROR;
628 629 630 631
}


static char *testBuildFilename(const char *relativeTo,
632 633 634 635 636 637 638 639
                               const char *filename) {
    char *offset;
    int baseLen;
    if (!filename || filename[0] == '\0')
        return (NULL);
    if (filename[0] == '/')
        return strdup(filename);

640
    offset = strrchr(relativeTo, '/');
641
    if ((baseLen = (offset-relativeTo+1))) {
642
        char *absFile;
C
Chris Lalancette 已提交
643 644
        int totalLen = baseLen + strlen(filename) + 1;
        if (VIR_ALLOC_N(absFile, totalLen) < 0)
645
            return NULL;
C
Chris Lalancette 已提交
646 647 648 649
        if (virStrncpy(absFile, relativeTo, baseLen, totalLen) == NULL) {
            VIR_FREE(absFile);
            return NULL;
        }
650 651 652 653 654
        strcat(absFile, filename);
        return absFile;
    } else {
        return strdup(filename);
    }
655 656
}

657
static int testOpenVolumesForPool(xmlDocPtr xml,
658 659 660 661 662 663 664
                                  xmlXPathContextPtr ctxt,
                                  const char *file,
                                  virStoragePoolObjPtr pool,
                                  int poolidx) {
    char *vol_xpath;
    int i, ret, func_ret = -1;
    xmlNodePtr *vols = NULL;
665
    virStorageVolDefPtr def = NULL;
666 667 668

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

673
    ret = virXPathNodeSet(vol_xpath, ctxt, &vols);
674 675 676 677 678 679 680 681 682 683 684
    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) {
685
                testError(VIR_ERR_INTERNAL_ERROR, "%s",
686 687 688 689
                          _("resolving volume filename"));
                goto error;
            }

690
            def = virStorageVolDefParseFile(pool->def, absFile);
691 692 693 694
            VIR_FREE(absFile);
            if (!def)
                goto error;
        } else {
695
            if ((def = virStorageVolDefParseNode(pool->def, xml,
696 697 698 699 700 701 702
                                                 vols[i])) == NULL) {
                goto error;
            }
        }

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

707 708 709 710 711 712 713
        if (def->target.path == NULL) {
            if (virAsprintf(&def->target.path, "%s/%s",
                            pool->def->target.path,
                            def->name) == -1) {
                virReportOOMError();
                goto error;
            }
714 715 716
        }

        if (def->key == NULL) {
717 718 719 720 721
            def->key = strdup(def->target.path);
            if (def->key == NULL) {
                virReportOOMError();
                goto error;
            }
722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738
        }

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

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

765
    testDriverLock(privconn);
766
    conn->privateData = privconn;
767

768 769 770
    if (virDomainObjListInit(&privconn->domains) < 0)
        goto error;

771 772
    if (!(privconn->caps = testBuildCapabilities(conn)))
        goto error;
773

774
    if (!(xml = virXMLParseFile(file))) {
775
        goto error;
776 777
    }

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

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

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

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

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

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

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

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

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

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

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

882
    for (i = 0 ; i < ret ; i++) {
883 884 885 886 887 888
        virDomainDefPtr def;
        char *relFile = virXMLPropString(domains[i], "file");
        if (relFile != NULL) {
            char *absFile = testBuildFilename(file, relFile);
            VIR_FREE(relFile);
            if (!absFile) {
889
                testError(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
                testError(VIR_ERR_INTERNAL_ERROR, "%s", _("resolving network filename"));
934 935
                goto error;
            }
936

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

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

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

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

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

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

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

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

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

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

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

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

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


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

1082
    return (0);
1083 1084

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

1102

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

E
Eric Blake 已提交
1110 1111
    virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);

1112
    if (!conn->uri)
1113
        return VIR_DRV_OPEN_DECLINED;
1114

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

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

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

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

1137 1138 1139 1140 1141
    if (ret != VIR_DRV_OPEN_SUCCESS)
        return ret;

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

1143 1144 1145 1146 1147
    privconn->domainEventState = virDomainEventStateNew(testDomainEventFlush,
                                                        privconn,
                                                        NULL,
                                                        false);
    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 1183
    *hvVer = 2;
    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 1201 1202 1203
static int testGetMaxVCPUs(virConnectPtr conn ATTRIBUTE_UNUSED,
                           const char *type ATTRIBUTE_UNUSED)
{
    return 32;
}

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

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

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

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

1232
    return count;
1233 1234
}

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

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

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

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

1292 1293
    virCheckFlags(0, NULL);

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

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

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

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

1313 1314 1315 1316
    event = virDomainEventNewFromObj(dom,
                                     VIR_DOMAIN_EVENT_STARTED,
                                     VIR_DOMAIN_EVENT_STARTED_BOOTED);

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

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


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

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

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

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

cleanup:
1353 1354
    if (dom)
        virDomainObjUnlock(dom);
1355
    return ret;
1356 1357
}

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

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

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

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

cleanup:
1379 1380
    if (dom)
        virDomainObjUnlock(dom);
1381
    return ret;
1382 1383
}

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

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

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

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

cleanup:
1405 1406
    if (dom)
        virDomainObjUnlock(dom);
1407
    return ret;
1408 1409
}

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

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

1421
    return n;
1422 1423
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

cleanup:
1534 1535
    if (privdom)
        virDomainObjUnlock(privdom);
1536 1537 1538 1539 1540 1541

    if (event) {
        testDriverLock(privconn);
        testDomainEventQueue(privconn, event);
        testDriverUnlock(privconn);
    }
1542
    return ret;
1543 1544
}

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

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

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

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

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

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

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

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

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

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

J
Jiri Denemark 已提交
1606 1607 1608
    virDomainObjSetState(privdom, VIR_DOMAIN_SHUTDOWN,
                         VIR_DOMAIN_SHUTDOWN_USER);

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

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

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

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

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

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

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

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

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

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

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

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

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

cleanup:
1691 1692
    if (privdom)
        virDomainObjUnlock(privdom);
1693
    return ret;
1694 1695
}

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

    virCheckFlags(0, -1);

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

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

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

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

1727 1728 1729 1730 1731
#define TEST_SAVE_MAGIC "TestGuestMagic"

static int testDomainSave(virDomainPtr domain,
                          const char *path)
{
1732
    testConnPtr privconn = domain->conn->privateData;
1733 1734 1735
    char *xml = NULL;
    int fd = -1;
    int len;
1736
    virDomainObjPtr privdom;
1737
    virDomainEventPtr event = NULL;
1738
    int ret = -1;
1739

1740
    testDriverLock(privconn);
1741 1742 1743 1744
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);

    if (privdom == NULL) {
1745
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
1746
        goto cleanup;
1747
    }
1748

1749
    xml = virDomainDefFormat(privdom->def,
C
Cole Robinson 已提交
1750 1751
                             VIR_DOMAIN_XML_SECURE);

1752
    if (xml == NULL) {
1753
        virReportSystemError(errno,
1754 1755
                             _("saving domain '%s' failed to allocate space for metadata"),
                             domain->name);
1756
        goto cleanup;
1757
    }
1758 1759

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

1785
    if (VIR_CLOSE(fd) < 0) {
1786
        virReportSystemError(errno,
1787 1788
                             _("saving domain '%s' to '%s': write failed"),
                             domain->name, path);
1789
        goto cleanup;
1790
    }
1791 1792
    fd = -1;

J
Jiri Denemark 已提交
1793
    testDomainShutdownState(domain, privdom, VIR_DOMAIN_SHUTOFF_SAVED);
1794 1795 1796
    event = virDomainEventNewFromObj(privdom,
                                     VIR_DOMAIN_EVENT_STOPPED,
                                     VIR_DOMAIN_EVENT_STOPPED_SAVED);
1797

1798 1799 1800
    if (!privdom->persistent) {
        virDomainRemoveInactive(&privconn->domains,
                                privdom);
1801
        privdom = NULL;
1802
    }
1803

1804
    ret = 0;
1805 1806 1807 1808 1809 1810 1811
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) {
1812
        VIR_FORCE_CLOSE(fd);
1813 1814
        unlink(path);
    }
1815 1816
    if (privdom)
        virDomainObjUnlock(privdom);
1817 1818
    if (event)
        testDomainEventQueue(privconn, event);
1819
    testDriverUnlock(privconn);
1820
    return ret;
1821 1822
}

1823 1824
static int testDomainRestore(virConnectPtr conn,
                             const char *path)
1825
{
1826
    testConnPtr privconn = conn->privateData;
1827
    char *xml = NULL;
1828
    char magic[15];
1829 1830 1831
    int fd = -1;
    int len;
    virDomainDefPtr def = NULL;
1832
    virDomainObjPtr dom = NULL;
1833
    virDomainEventPtr event = NULL;
1834
    int ret = -1;
1835 1836

    if ((fd = open(path, O_RDONLY)) < 0) {
1837
        virReportSystemError(errno,
1838 1839
                             _("cannot read domain image '%s'"),
                             path);
1840
        goto cleanup;
1841
    }
1842
    if (saferead(fd, magic, sizeof(magic)) != sizeof(magic)) {
1843
        virReportSystemError(errno,
1844 1845
                             _("incomplete save header in '%s'"),
                             path);
1846
        goto cleanup;
1847
    }
1848
    if (memcmp(magic, TEST_SAVE_MAGIC, sizeof(magic))) {
1849
        testError(VIR_ERR_INTERNAL_ERROR,
J
Jim Meyering 已提交
1850
                  "%s", _("mismatched header magic"));
1851
        goto cleanup;
1852
    }
1853
    if (saferead(fd, (char*)&len, sizeof(len)) != sizeof(len)) {
1854
        virReportSystemError(errno,
1855 1856
                             _("failed to read metadata length in '%s'"),
                             path);
1857
        goto cleanup;
1858 1859
    }
    if (len < 1 || len > 8192) {
1860
        testError(VIR_ERR_INTERNAL_ERROR,
J
Jim Meyering 已提交
1861
                  "%s", _("length of metadata out of range"));
1862
        goto cleanup;
1863
    }
1864
    if (VIR_ALLOC_N(xml, len+1) < 0) {
1865
        virReportOOMError();
1866
        goto cleanup;
1867
    }
1868
    if (saferead(fd, xml, len) != len) {
1869
        virReportSystemError(errno,
1870
                             _("incomplete metdata in '%s'"), path);
1871
        goto cleanup;
1872 1873
    }
    xml[len] = '\0';
1874

1875
    testDriverLock(privconn);
1876
    def = virDomainDefParseString(privconn->caps, xml,
M
Matthias Bolte 已提交
1877
                                  1 << VIR_DOMAIN_VIRT_TEST,
1878
                                  VIR_DOMAIN_XML_INACTIVE);
1879
    if (!def)
1880
        goto cleanup;
1881

1882 1883 1884
    if (virDomainObjIsDuplicate(&privconn->domains, def, 1) < 0)
        goto cleanup;

1885
    if (testDomainGenerateIfnames(def) < 0)
1886
        goto cleanup;
1887
    if (!(dom = virDomainAssignDef(privconn->caps,
1888
                                   &privconn->domains, def, true)))
1889 1890
        goto cleanup;
    def = NULL;
1891

J
Jiri Denemark 已提交
1892
    if (testDomainStartState(conn, dom, VIR_DOMAIN_RUNNING_RESTORED) < 0)
1893 1894
        goto cleanup;

1895 1896 1897
    event = virDomainEventNewFromObj(dom,
                                     VIR_DOMAIN_EVENT_STARTED,
                                     VIR_DOMAIN_EVENT_STARTED_RESTORED);
1898
    ret = 0;
1899 1900 1901 1902

cleanup:
    virDomainDefFree(def);
    VIR_FREE(xml);
1903
    VIR_FORCE_CLOSE(fd);
1904 1905
    if (dom)
        virDomainObjUnlock(dom);
1906 1907
    if (event)
        testDomainEventQueue(privconn, event);
1908
    testDriverUnlock(privconn);
1909
    return ret;
1910 1911
}

1912 1913
static int testDomainCoreDump(virDomainPtr domain,
                              const char *to,
E
Eric Blake 已提交
1914
                              unsigned int flags)
1915
{
1916
    testConnPtr privconn = domain->conn->privateData;
1917
    int fd = -1;
1918
    virDomainObjPtr privdom;
1919
    virDomainEventPtr event = NULL;
1920
    int ret = -1;
1921

E
Eric Blake 已提交
1922 1923
    virCheckFlags(VIR_DUMP_CRASH, -1);

1924
    testDriverLock(privconn);
1925 1926 1927 1928
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);

    if (privdom == NULL) {
1929
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
1930
        goto cleanup;
1931
    }
1932 1933

    if ((fd = open(to, O_CREAT|O_TRUNC|O_WRONLY, S_IRUSR|S_IWUSR)) < 0) {
1934
        virReportSystemError(errno,
1935 1936
                             _("domain '%s' coredump: failed to open %s"),
                             domain->name, to);
1937
        goto cleanup;
1938
    }
1939
    if (safewrite(fd, TEST_SAVE_MAGIC, sizeof(TEST_SAVE_MAGIC)) < 0) {
1940
        virReportSystemError(errno,
1941 1942
                             _("domain '%s' coredump: failed to write header to %s"),
                             domain->name, to);
1943
        goto cleanup;
1944
    }
1945
    if (VIR_CLOSE(fd) < 0) {
1946
        virReportSystemError(errno,
1947 1948
                             _("domain '%s' coredump: write failed: %s"),
                             domain->name, to);
1949
        goto cleanup;
1950
    }
1951

1952
    if (flags & VIR_DUMP_CRASH) {
J
Jiri Denemark 已提交
1953
        testDomainShutdownState(domain, privdom, VIR_DOMAIN_SHUTOFF_CRASHED);
1954 1955 1956 1957 1958 1959 1960 1961
        event = virDomainEventNewFromObj(privdom,
                                         VIR_DOMAIN_EVENT_STOPPED,
                                         VIR_DOMAIN_EVENT_STOPPED_CRASHED);
        if (!privdom->persistent) {
            virDomainRemoveInactive(&privconn->domains,
                                    privdom);
            privdom = NULL;
        }
1962
    }
1963

1964
    ret = 0;
1965
cleanup:
1966
    VIR_FORCE_CLOSE(fd);
1967 1968
    if (privdom)
        virDomainObjUnlock(privdom);
1969 1970
    if (event)
        testDomainEventQueue(privconn, event);
1971
    testDriverUnlock(privconn);
1972
    return ret;
1973 1974
}

1975
static char *testGetOSType(virDomainPtr dom ATTRIBUTE_UNUSED) {
1976 1977
    char *ret = strdup("linux");
    if (!ret)
1978
        virReportOOMError();
1979
    return ret;
1980 1981 1982
}

static unsigned long testGetMaxMemory(virDomainPtr domain) {
1983 1984
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
1985
    unsigned long ret = 0;
1986

1987
    testDriverLock(privconn);
1988 1989
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
1990
    testDriverUnlock(privconn);
1991 1992

    if (privdom == NULL) {
1993
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
1994
        goto cleanup;
1995
    }
1996

1997
    ret = privdom->def->mem.max_balloon;
1998 1999

cleanup:
2000 2001
    if (privdom)
        virDomainObjUnlock(privdom);
2002
    return ret;
2003 2004 2005 2006 2007
}

static int testSetMaxMemory(virDomainPtr domain,
                            unsigned long memory)
{
2008 2009
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
2010
    int ret = -1;
2011

2012
    testDriverLock(privconn);
2013 2014
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
2015
    testDriverUnlock(privconn);
2016 2017

    if (privdom == NULL) {
2018
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2019
        goto cleanup;
2020
    }
2021 2022

    /* XXX validate not over host memory wrt to other domains */
2023
    privdom->def->mem.max_balloon = memory;
2024 2025 2026
    ret = 0;

cleanup:
2027 2028
    if (privdom)
        virDomainObjUnlock(privdom);
2029
    return ret;
2030 2031
}

2032 2033 2034
static int testSetMemory(virDomainPtr domain,
                         unsigned long memory)
{
2035 2036
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
2037
    int ret = -1;
2038

2039
    testDriverLock(privconn);
2040 2041
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
2042
    testDriverUnlock(privconn);
2043 2044

    if (privdom == NULL) {
2045
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2046
        goto cleanup;
2047
    }
2048

2049
    if (memory > privdom->def->mem.max_balloon) {
2050
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2051
        goto cleanup;
2052
    }
2053

2054
    privdom->def->mem.cur_balloon = memory;
2055 2056 2057
    ret = 0;

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

2063 2064
static int
testDomainGetVcpusFlags(virDomainPtr domain, unsigned int flags)
C
Cole Robinson 已提交
2065
{
2066 2067 2068 2069 2070
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr vm;
    virDomainDefPtr def;
    int ret = -1;

2071 2072
    virCheckFlags(VIR_DOMAIN_AFFECT_LIVE |
                  VIR_DOMAIN_AFFECT_CONFIG |
2073 2074 2075
                  VIR_DOMAIN_VCPU_MAXIMUM, -1);

    /* Exactly one of LIVE or CONFIG must be set.  */
2076
    if (!(flags & VIR_DOMAIN_AFFECT_LIVE) == !(flags & VIR_DOMAIN_AFFECT_CONFIG)) {
2077 2078
        testError(VIR_ERR_INVALID_ARG,
                  _("invalid flag combination: (0x%x)"), flags);
2079 2080 2081
        return -1;
    }

2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093
    testDriverLock(privconn);
    vm = virDomainFindByUUID(&privconn->domains, domain->uuid);
    testDriverUnlock(privconn);

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

2094
    if (flags & VIR_DOMAIN_AFFECT_LIVE) {
2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110
        if (!virDomainObjIsActive(vm)) {
            testError(VIR_ERR_OPERATION_INVALID, "%s",
                      _("domain not active"));
            goto cleanup;
        }
        def = vm->def;
    } else {
        def = vm->newDef ? vm->newDef : vm->def;
    }

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

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

2113 2114 2115
static int
testDomainGetMaxVcpus(virDomainPtr domain)
{
2116
    return testDomainGetVcpusFlags(domain, (VIR_DOMAIN_AFFECT_LIVE |
2117 2118 2119 2120 2121 2122 2123
                                            VIR_DOMAIN_VCPU_MAXIMUM));
}

static int
testDomainSetVcpusFlags(virDomainPtr domain, unsigned int nrCpus,
                        unsigned int flags)
{
2124
    testConnPtr privconn = domain->conn->privateData;
2125
    virDomainObjPtr privdom = NULL;
2126
    virDomainDefPtr persistentDef;
C
Cole Robinson 已提交
2127 2128
    int ret = -1, maxvcpus;

2129 2130
    virCheckFlags(VIR_DOMAIN_AFFECT_LIVE |
                  VIR_DOMAIN_AFFECT_CONFIG |
2131 2132 2133 2134
                  VIR_DOMAIN_VCPU_MAXIMUM, -1);

    /* At least one of LIVE or CONFIG must be set.  MAXIMUM cannot be
     * mixed with LIVE.  */
2135 2136 2137
    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)) {
2138 2139 2140 2141 2142 2143 2144
        testError(VIR_ERR_INVALID_ARG,
                  _("invalid flag combination: (0x%x)"), flags);
        return -1;
    }
    if (!nrCpus || (maxvcpus = testGetMaxVCPUs(domain->conn, NULL)) < nrCpus) {
        testError(VIR_ERR_INVALID_ARG,
                  _("argument out of range: %d"), nrCpus);
2145 2146
        return -1;
    }
2147

2148
    testDriverLock(privconn);
2149
    privdom = virDomainFindByUUID(&privconn->domains, domain->uuid);
2150 2151 2152
    testDriverUnlock(privconn);

    if (privdom == NULL) {
2153
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2154
        goto cleanup;
2155
    }
2156

2157
    if (!virDomainObjIsActive(privdom) && (flags & VIR_DOMAIN_AFFECT_LIVE)) {
2158
        testError(VIR_ERR_OPERATION_INVALID,
C
Cole Robinson 已提交
2159 2160 2161 2162
                  "%s", _("cannot hotplug vcpus for an inactive domain"));
        goto cleanup;
    }

2163 2164
    /* We allow more cpus in guest than host, but not more than the
     * domain's starting limit.  */
C
Cole Robinson 已提交
2165 2166
    if (!(flags & (VIR_DOMAIN_VCPU_MAXIMUM)) &&
        privdom->def->maxvcpus < maxvcpus)
2167
        maxvcpus = privdom->def->maxvcpus;
C
Cole Robinson 已提交
2168

C
Cole Robinson 已提交
2169
    if (nrCpus > maxvcpus) {
2170
        testError(VIR_ERR_INVALID_ARG,
C
Cole Robinson 已提交
2171 2172
                  "requested cpu amount exceeds maximum (%d > %d)",
                  nrCpus, maxvcpus);
2173
        goto cleanup;
2174
    }
2175

2176 2177 2178 2179
    if (!(persistentDef = virDomainObjGetPersistentDef(privconn->caps,
                                                       privdom)))
        goto cleanup;

2180
    switch (flags) {
2181
    case VIR_DOMAIN_VCPU_MAXIMUM | VIR_DOMAIN_AFFECT_CONFIG:
2182 2183 2184
        persistentDef->maxvcpus = nrCpus;
        if (nrCpus < persistentDef->vcpus)
            persistentDef->vcpus = nrCpus;
2185 2186
        ret = 0;
        break;
2187

2188
    case VIR_DOMAIN_AFFECT_CONFIG:
2189
        persistentDef->vcpus = nrCpus;
2190 2191 2192
        ret = 0;
        break;

2193
    case VIR_DOMAIN_AFFECT_LIVE:
2194 2195 2196
        ret = testDomainUpdateVCPUs(domain->conn, privdom, nrCpus, 0);
        break;

2197
    case VIR_DOMAIN_AFFECT_LIVE | VIR_DOMAIN_AFFECT_CONFIG:
2198
        ret = testDomainUpdateVCPUs(domain->conn, privdom, nrCpus, 0);
2199 2200 2201
        if (ret == 0) {
            persistentDef->vcpus = nrCpus;
        }
2202 2203
        break;
    }
2204 2205

cleanup:
2206 2207
    if (privdom)
        virDomainObjUnlock(privdom);
2208
    return ret;
2209 2210
}

2211 2212 2213
static int
testSetVcpus(virDomainPtr domain, unsigned int nrCpus)
{
2214
    return testDomainSetVcpusFlags(domain, nrCpus, VIR_DOMAIN_AFFECT_LIVE);
2215 2216
}

C
Cole Robinson 已提交
2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235
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) {
2236
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
C
Cole Robinson 已提交
2237 2238 2239 2240
        goto cleanup;
    }

    if (!virDomainObjIsActive(privdom)) {
2241
        testError(VIR_ERR_OPERATION_INVALID,
C
Cole Robinson 已提交
2242 2243 2244 2245 2246 2247 2248
                  "%s",_("cannot list vcpus for an inactive domain"));
        goto cleanup;
    }

    privdomdata = privdom->privateData;

    if (gettimeofday(&tv, NULL) < 0) {
2249
        virReportSystemError(errno,
C
Cole Robinson 已提交
2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304
                             "%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 已提交
2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321
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) {
2322
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
C
Cole Robinson 已提交
2323 2324 2325 2326
        goto cleanup;
    }

    if (!virDomainObjIsActive(privdom)) {
2327
        testError(VIR_ERR_OPERATION_INVALID,
C
Cole Robinson 已提交
2328 2329 2330 2331 2332
                  "%s",_("cannot pin vcpus on an inactive domain"));
        goto cleanup;
    }

    if (vcpu > privdom->def->vcpus) {
2333
        testError(VIR_ERR_INVALID_ARG, "%s",
C
Cole Robinson 已提交
2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361
                  _("requested vcpu is higher than allocated vcpus"));
        goto cleanup;
    }

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

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

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

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

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

2362
static char *testDomainGetXMLDesc(virDomainPtr domain, unsigned int flags)
2363
{
2364
    testConnPtr privconn = domain->conn->privateData;
2365
    virDomainDefPtr def;
2366
    virDomainObjPtr privdom;
2367 2368
    char *ret = NULL;

2369 2370
    /* Flags checked by virDomainDefFormat */

2371 2372 2373 2374 2375 2376
    testDriverLock(privconn);
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
    testDriverUnlock(privconn);

    if (privdom == NULL) {
2377
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2378
        goto cleanup;
2379
    }
2380

2381 2382
    def = (flags & VIR_DOMAIN_XML_INACTIVE) &&
        privdom->newDef ? privdom->newDef : privdom->def;
2383

2384
    ret = virDomainDefFormat(def,
2385 2386 2387
                             flags);

cleanup:
2388 2389
    if (privdom)
        virDomainObjUnlock(privdom);
2390
    return ret;
2391
}
2392

2393
static int testNumOfDefinedDomains(virConnectPtr conn) {
2394
    testConnPtr privconn = conn->privateData;
2395
    int count;
2396

2397
    testDriverLock(privconn);
2398
    count = virDomainObjListNumOfDomains(&privconn->domains, 0);
2399
    testDriverUnlock(privconn);
2400

2401
    return count;
2402 2403
}

2404 2405 2406
static int testListDefinedDomains(virConnectPtr conn,
                                  char **const names,
                                  int maxnames) {
2407

2408
    testConnPtr privconn = conn->privateData;
2409
    int n;
2410

2411
    testDriverLock(privconn);
2412
    memset(names, 0, sizeof(*names)*maxnames);
2413
    n = virDomainObjListGetInactiveNames(&privconn->domains, names, maxnames);
2414
    testDriverUnlock(privconn);
2415

2416
    return n;
2417 2418
}

2419
static virDomainPtr testDomainDefineXML(virConnectPtr conn,
2420
                                        const char *xml) {
2421
    testConnPtr privconn = conn->privateData;
2422
    virDomainPtr ret = NULL;
2423
    virDomainDefPtr def;
2424
    virDomainObjPtr dom = NULL;
2425
    virDomainEventPtr event = NULL;
2426
    int dupVM;
2427

2428
    testDriverLock(privconn);
2429
    if ((def = virDomainDefParseString(privconn->caps, xml,
M
Matthias Bolte 已提交
2430
                                       1 << VIR_DOMAIN_VIRT_TEST,
2431
                                       VIR_DOMAIN_XML_INACTIVE)) == NULL)
2432
        goto cleanup;
2433

2434 2435 2436
    if ((dupVM = virDomainObjIsDuplicate(&privconn->domains, def, 0)) < 0)
        goto cleanup;

2437
    if (testDomainGenerateIfnames(def) < 0)
2438
        goto cleanup;
2439
    if (!(dom = virDomainAssignDef(privconn->caps,
2440
                                   &privconn->domains, def, false)))
2441
        goto cleanup;
2442
    def = NULL;
2443
    dom->persistent = 1;
2444

2445 2446
    event = virDomainEventNewFromObj(dom,
                                     VIR_DOMAIN_EVENT_DEFINED,
2447 2448 2449
                                     !dupVM ?
                                     VIR_DOMAIN_EVENT_DEFINED_ADDED :
                                     VIR_DOMAIN_EVENT_DEFINED_UPDATED);
2450

2451
    ret = virGetDomain(conn, dom->def->name, dom->def->uuid);
2452
    if (ret)
2453
        ret->id = dom->def->id;
2454 2455 2456

cleanup:
    virDomainDefFree(def);
2457 2458
    if (dom)
        virDomainObjUnlock(dom);
2459 2460
    if (event)
        testDomainEventQueue(privconn, event);
2461
    testDriverUnlock(privconn);
2462
    return ret;
2463 2464
}

2465 2466 2467
static int testNodeGetCellsFreeMemory(virConnectPtr conn,
                                      unsigned long long *freemems,
                                      int startCell, int maxCells) {
2468
    testConnPtr privconn = conn->privateData;
2469
    int i, j;
2470
    int ret = -1;
2471

2472
    testDriverLock(privconn);
2473
    if (startCell > privconn->numCells) {
2474
        testError(VIR_ERR_INVALID_ARG,
J
Jim Meyering 已提交
2475
                  "%s", _("Range exceeds available cells"));
2476
        goto cleanup;
2477 2478 2479 2480 2481 2482 2483
    }

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

2486
cleanup:
2487
    testDriverUnlock(privconn);
2488
    return ret;
2489 2490 2491
}


2492
static int testDomainCreateWithFlags(virDomainPtr domain, unsigned int flags) {
2493 2494
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
2495
    virDomainEventPtr event = NULL;
2496
    int ret = -1;
2497

2498 2499
    virCheckFlags(0, -1);

2500
    testDriverLock(privconn);
2501 2502 2503 2504
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);

    if (privdom == NULL) {
2505
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2506
        goto cleanup;
2507
    }
2508

J
Jiri Denemark 已提交
2509
    if (virDomainObjGetState(privdom, NULL) != VIR_DOMAIN_SHUTOFF) {
2510
        testError(VIR_ERR_INTERNAL_ERROR,
2511
                  _("Domain '%s' is already running"), domain->name);
2512
        goto cleanup;
2513 2514
    }

J
Jiri Denemark 已提交
2515 2516
    if (testDomainStartState(domain->conn, privdom,
                             VIR_DOMAIN_RUNNING_BOOTED) < 0)
2517 2518 2519
        goto cleanup;
    domain->id = privdom->def->id;

2520 2521 2522
    event = virDomainEventNewFromObj(privdom,
                                     VIR_DOMAIN_EVENT_STARTED,
                                     VIR_DOMAIN_EVENT_STARTED_BOOTED);
2523
    ret = 0;
2524

2525
cleanup:
2526 2527
    if (privdom)
        virDomainObjUnlock(privdom);
2528 2529
    if (event)
        testDomainEventQueue(privconn, event);
2530
    testDriverUnlock(privconn);
2531
    return ret;
2532 2533
}

2534 2535 2536 2537
static int testDomainCreate(virDomainPtr domain) {
    return testDomainCreateWithFlags(domain, 0);
}

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

2546 2547
    virCheckFlags(0, -1);

2548
    testDriverLock(privconn);
2549 2550 2551 2552
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);

    if (privdom == NULL) {
2553
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2554
        goto cleanup;
2555
    }
2556

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

2563 2564 2565
    event = virDomainEventNewFromObj(privdom,
                                     VIR_DOMAIN_EVENT_UNDEFINED,
                                     VIR_DOMAIN_EVENT_UNDEFINED_REMOVED);
2566 2567
    virDomainRemoveInactive(&privconn->domains,
                            privdom);
2568
    privdom = NULL;
2569
    ret = 0;
2570

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

2580 2581 2582 2583 2584
static int testDomainUndefine(virDomainPtr domain)
{
    return testDomainUndefineFlags(domain, 0);
}

2585 2586 2587
static int testDomainGetAutostart(virDomainPtr domain,
                                  int *autostart)
{
2588 2589
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
2590
    int ret = -1;
2591

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

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

2602
    *autostart = privdom->autostart;
2603 2604 2605
    ret = 0;

cleanup:
2606 2607
    if (privdom)
        virDomainObjUnlock(privdom);
2608
    return ret;
2609 2610 2611 2612 2613 2614
}


static int testDomainSetAutostart(virDomainPtr domain,
                                  int autostart)
{
2615 2616
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
2617
    int ret = -1;
2618

2619
    testDriverLock(privconn);
2620 2621
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
2622
    testDriverUnlock(privconn);
2623 2624

    if (privdom == NULL) {
2625
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2626
        goto cleanup;
2627 2628
    }

2629
    privdom->autostart = autostart ? 1 : 0;
2630 2631 2632
    ret = 0;

cleanup:
2633 2634
    if (privdom)
        virDomainObjUnlock(privdom);
2635
    return ret;
2636
}
2637

2638
static char *testDomainGetSchedulerType(virDomainPtr domain ATTRIBUTE_UNUSED,
2639 2640
                                        int *nparams)
{
2641 2642
    char *type = NULL;

2643 2644 2645
    if (nparams)
        *nparams = 1;

2646
    type = strdup("fair");
2647
    if (!type)
2648
        virReportOOMError();
2649

2650 2651 2652
    return type;
}

2653 2654 2655 2656 2657
static int
testDomainGetSchedulerParamsFlags(virDomainPtr domain,
                                  virTypedParameterPtr params,
                                  int *nparams,
                                  unsigned int flags)
2658
{
2659 2660
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
2661
    int ret = -1;
2662

2663 2664
    virCheckFlags(0, -1);

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

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

2675 2676
    if (*nparams < 1) {
        testError(VIR_ERR_INVALID_ARG, "%s", _("Invalid parameter count"));
2677
        goto cleanup;
2678
    }
2679
    strcpy(params[0].field, "weight");
2680
    params[0].type = VIR_TYPED_PARAM_UINT;
2681 2682 2683
    /* XXX */
    /*params[0].value.ui = privdom->weight;*/
    params[0].value.ui = 50;
2684 2685

    *nparams = 1;
2686 2687 2688
    ret = 0;

cleanup:
2689 2690
    if (privdom)
        virDomainObjUnlock(privdom);
2691
    return ret;
2692
}
2693

2694 2695 2696 2697 2698 2699 2700
static int
testDomainGetSchedulerParams(virDomainPtr domain,
                             virTypedParameterPtr params,
                             int *nparams)
{
    return testDomainGetSchedulerParamsFlags(domain, params, nparams, 0);
}
2701

2702 2703 2704 2705 2706
static int
testDomainSetSchedulerParamsFlags(virDomainPtr domain,
                                  virTypedParameterPtr params,
                                  int nparams,
                                  unsigned int flags)
2707
{
2708 2709
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
2710
    int ret = -1, i;
2711

2712 2713
    virCheckFlags(0, -1);

2714
    testDriverLock(privconn);
2715 2716
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
2717
    testDriverUnlock(privconn);
2718 2719

    if (privdom == NULL) {
2720
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2721
        goto cleanup;
2722 2723
    }

2724 2725 2726 2727 2728
    for (i = 0; i < nparams; i++) {
        if (STRNEQ(params[i].field, "weight")) {
            testError(VIR_ERR_INVALID_ARG, "field");
            goto cleanup;
        }
2729
        if (params[i].type != VIR_TYPED_PARAM_UINT) {
2730 2731 2732 2733 2734
            testError(VIR_ERR_INVALID_ARG, "type");
            goto cleanup;
        }
        /* XXX */
        /*privdom->weight = params[i].value.ui;*/
2735
    }
2736

2737 2738 2739
    ret = 0;

cleanup:
2740 2741
    if (privdom)
        virDomainObjUnlock(privdom);
2742
    return ret;
2743 2744
}

2745 2746 2747 2748 2749 2750 2751 2752
static int
testDomainSetSchedulerParams(virDomainPtr domain,
                             virTypedParameterPtr params,
                             int nparams)
{
    return testDomainSetSchedulerParamsFlags(domain, params, nparams, 0);
}

2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768
static int testDomainBlockStats(virDomainPtr domain,
                                const char *path,
                                struct _virDomainBlockStats *stats)
{
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
    struct timeval tv;
    unsigned long long statbase;
    int i, found = 0, ret = -1;

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

    if (privdom == NULL) {
2769
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780
        goto error;
    }

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

    if (!found) {
2781
        testError(VIR_ERR_INVALID_ARG,
2782 2783 2784 2785 2786
                  _("invalid path: %s"), path);
        goto error;
    }

    if (gettimeofday(&tv, NULL) < 0) {
2787
        virReportSystemError(errno,
2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822
                             "%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) {
2823
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835
        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) {
2836
        testError(VIR_ERR_INVALID_ARG,
2837 2838 2839 2840 2841
                  _("invalid path, '%s' is not a known interface"), path);
        goto error;
    }

    if (gettimeofday(&tv, NULL) < 0) {
2842
        virReportSystemError(errno,
2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864
                             "%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;
}

2865
static virDrvOpenStatus testOpenNetwork(virConnectPtr conn,
2866
                                        virConnectAuthPtr auth ATTRIBUTE_UNUSED,
E
Eric Blake 已提交
2867 2868 2869 2870
                                        unsigned int flags)
{
    virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);

2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886
    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)
{
2887 2888
    testConnPtr privconn = conn->privateData;
    virNetworkObjPtr net;
2889
    virNetworkPtr ret = NULL;
2890

2891 2892 2893 2894 2895
    testDriverLock(privconn);
    net = virNetworkFindByUUID(&privconn->networks, uuid);
    testDriverUnlock(privconn);

    if (net == NULL) {
2896
        testError(VIR_ERR_NO_NETWORK, NULL);
2897
        goto cleanup;
2898 2899
    }

2900 2901 2902
    ret = virGetNetwork(conn, net->def->name, net->def->uuid);

cleanup:
2903 2904
    if (net)
        virNetworkObjUnlock(net);
2905
    return ret;
2906
}
2907

2908
static virNetworkPtr testLookupNetworkByName(virConnectPtr conn,
2909
                                             const char *name)
2910
{
2911
    testConnPtr privconn = conn->privateData;
2912 2913
    virNetworkObjPtr net;
    virNetworkPtr ret = NULL;
2914

2915 2916 2917 2918 2919
    testDriverLock(privconn);
    net = virNetworkFindByName(&privconn->networks, name);
    testDriverUnlock(privconn);

    if (net == NULL) {
2920
        testError(VIR_ERR_NO_NETWORK, NULL);
2921
        goto cleanup;
2922 2923
    }

2924 2925 2926
    ret = virGetNetwork(conn, net->def->name, net->def->uuid);

cleanup:
2927 2928
    if (net)
        virNetworkObjUnlock(net);
2929
    return ret;
2930 2931 2932 2933
}


static int testNumNetworks(virConnectPtr conn) {
2934
    testConnPtr privconn = conn->privateData;
2935
    int numActive = 0, i;
2936

2937 2938 2939
    testDriverLock(privconn);
    for (i = 0 ; i < privconn->networks.count ; i++) {
        virNetworkObjLock(privconn->networks.objs[i]);
D
Daniel P. Berrange 已提交
2940
        if (virNetworkObjIsActive(privconn->networks.objs[i]))
2941
            numActive++;
2942 2943 2944
        virNetworkObjUnlock(privconn->networks.objs[i]);
    }
    testDriverUnlock(privconn);
2945

2946
    return numActive;
2947 2948 2949
}

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

2953
    testDriverLock(privconn);
2954
    memset(names, 0, sizeof(*names)*nnames);
2955 2956
    for (i = 0 ; i < privconn->networks.count && n < nnames ; i++) {
        virNetworkObjLock(privconn->networks.objs[i]);
D
Daniel P. Berrange 已提交
2957
        if (virNetworkObjIsActive(privconn->networks.objs[i]) &&
2958 2959
            !(names[n++] = strdup(privconn->networks.objs[i]->def->name))) {
            virNetworkObjUnlock(privconn->networks.objs[i]);
2960
            goto no_memory;
2961 2962 2963 2964
        }
        virNetworkObjUnlock(privconn->networks.objs[i]);
    }
    testDriverUnlock(privconn);
2965

2966 2967 2968
    return n;

no_memory:
2969
    virReportOOMError();
2970 2971
    for (n = 0 ; n < nnames ; n++)
        VIR_FREE(names[n]);
2972
    testDriverUnlock(privconn);
2973
    return -1;
2974 2975 2976
}

static int testNumDefinedNetworks(virConnectPtr conn) {
2977
    testConnPtr privconn = conn->privateData;
2978
    int numInactive = 0, i;
2979

2980 2981 2982
    testDriverLock(privconn);
    for (i = 0 ; i < privconn->networks.count ; i++) {
        virNetworkObjLock(privconn->networks.objs[i]);
D
Daniel P. Berrange 已提交
2983
        if (!virNetworkObjIsActive(privconn->networks.objs[i]))
2984
            numInactive++;
2985 2986 2987
        virNetworkObjUnlock(privconn->networks.objs[i]);
    }
    testDriverUnlock(privconn);
2988

2989
    return numInactive;
2990 2991 2992
}

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

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

3009 3010 3011
    return n;

no_memory:
3012
    virReportOOMError();
3013 3014
    for (n = 0 ; n < nnames ; n++)
        VIR_FREE(names[n]);
3015
    testDriverUnlock(privconn);
3016
    return -1;
3017 3018
}

3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029

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) {
3030
        testError(VIR_ERR_NO_NETWORK, NULL);
3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050
        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) {
3051
        testError(VIR_ERR_NO_NETWORK, NULL);
3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062
        goto cleanup;
    }
    ret = obj->persistent;

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


3063
static virNetworkPtr testNetworkCreate(virConnectPtr conn, const char *xml) {
3064
    testConnPtr privconn = conn->privateData;
3065
    virNetworkDefPtr def;
3066
    virNetworkObjPtr net = NULL;
3067
    virNetworkPtr ret = NULL;
3068

3069
    testDriverLock(privconn);
3070
    if ((def = virNetworkDefParseString(xml)) == NULL)
3071
        goto cleanup;
3072

3073
    if ((net = virNetworkAssignDef(&privconn->networks, def)) == NULL)
3074 3075
        goto cleanup;
    def = NULL;
3076
    net->active = 1;
3077

3078
    ret = virGetNetwork(conn, net->def->name, net->def->uuid);
3079

3080 3081
cleanup:
    virNetworkDefFree(def);
3082 3083 3084
    if (net)
        virNetworkObjUnlock(net);
    testDriverUnlock(privconn);
3085
    return ret;
3086 3087 3088
}

static virNetworkPtr testNetworkDefine(virConnectPtr conn, const char *xml) {
3089
    testConnPtr privconn = conn->privateData;
3090
    virNetworkDefPtr def;
3091
    virNetworkObjPtr net = NULL;
3092
    virNetworkPtr ret = NULL;
3093

3094
    testDriverLock(privconn);
3095
    if ((def = virNetworkDefParseString(xml)) == NULL)
3096
        goto cleanup;
3097

3098
    if ((net = virNetworkAssignDef(&privconn->networks, def)) == NULL)
3099 3100
        goto cleanup;
    def = NULL;
3101
    net->persistent = 1;
3102

3103
    ret = virGetNetwork(conn, net->def->name, net->def->uuid);
3104 3105 3106

cleanup:
    virNetworkDefFree(def);
3107 3108 3109
    if (net)
        virNetworkObjUnlock(net);
    testDriverUnlock(privconn);
3110
    return ret;
3111 3112 3113
}

static int testNetworkUndefine(virNetworkPtr network) {
3114 3115
    testConnPtr privconn = network->conn->privateData;
    virNetworkObjPtr privnet;
3116
    int ret = -1;
3117

3118
    testDriverLock(privconn);
3119 3120 3121 3122
    privnet = virNetworkFindByName(&privconn->networks,
                                   network->name);

    if (privnet == NULL) {
3123
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3124
        goto cleanup;
3125
    }
3126

D
Daniel P. Berrange 已提交
3127
    if (virNetworkObjIsActive(privnet)) {
3128
        testError(VIR_ERR_OPERATION_INVALID,
3129
                  _("Network '%s' is still running"), network->name);
3130
        goto cleanup;
3131 3132
    }

3133 3134
    virNetworkRemoveInactive(&privconn->networks,
                             privnet);
3135
    privnet = NULL;
3136
    ret = 0;
3137

3138
cleanup:
3139 3140 3141
    if (privnet)
        virNetworkObjUnlock(privnet);
    testDriverUnlock(privconn);
3142
    return ret;
3143 3144 3145
}

static int testNetworkStart(virNetworkPtr network) {
3146 3147
    testConnPtr privconn = network->conn->privateData;
    virNetworkObjPtr privnet;
3148
    int ret = -1;
3149

3150
    testDriverLock(privconn);
3151 3152
    privnet = virNetworkFindByName(&privconn->networks,
                                   network->name);
3153
    testDriverUnlock(privconn);
3154 3155

    if (privnet == NULL) {
3156
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3157
        goto cleanup;
3158
    }
3159

D
Daniel P. Berrange 已提交
3160
    if (virNetworkObjIsActive(privnet)) {
3161
        testError(VIR_ERR_OPERATION_INVALID,
3162
                  _("Network '%s' is already running"), network->name);
3163
        goto cleanup;
3164 3165
    }

3166
    privnet->active = 1;
3167
    ret = 0;
3168

3169
cleanup:
3170 3171
    if (privnet)
        virNetworkObjUnlock(privnet);
3172
    return ret;
3173 3174 3175
}

static int testNetworkDestroy(virNetworkPtr network) {
3176 3177
    testConnPtr privconn = network->conn->privateData;
    virNetworkObjPtr privnet;
3178
    int ret = -1;
3179

3180
    testDriverLock(privconn);
3181 3182 3183 3184
    privnet = virNetworkFindByName(&privconn->networks,
                                   network->name);

    if (privnet == NULL) {
3185
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3186
        goto cleanup;
3187
    }
3188

3189 3190 3191 3192
    privnet->active = 0;
    if (!privnet->persistent) {
        virNetworkRemoveInactive(&privconn->networks,
                                 privnet);
3193
        privnet = NULL;
3194
    }
3195 3196 3197
    ret = 0;

cleanup:
3198 3199 3200
    if (privnet)
        virNetworkObjUnlock(privnet);
    testDriverUnlock(privconn);
3201
    return ret;
3202 3203
}

3204
static char *testNetworkGetXMLDesc(virNetworkPtr network,
E
Eric Blake 已提交
3205
                                   unsigned int flags)
3206
{
3207 3208
    testConnPtr privconn = network->conn->privateData;
    virNetworkObjPtr privnet;
3209
    char *ret = NULL;
3210

E
Eric Blake 已提交
3211 3212
    virCheckFlags(0, NULL);

3213
    testDriverLock(privconn);
3214 3215
    privnet = virNetworkFindByName(&privconn->networks,
                                   network->name);
3216
    testDriverUnlock(privconn);
3217 3218

    if (privnet == NULL) {
3219
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3220
        goto cleanup;
3221
    }
3222

3223
    ret = virNetworkDefFormat(privnet->def);
3224 3225

cleanup:
3226 3227
    if (privnet)
        virNetworkObjUnlock(privnet);
3228
    return ret;
3229 3230 3231
}

static char *testNetworkGetBridgeName(virNetworkPtr network) {
3232
    testConnPtr privconn = network->conn->privateData;
3233
    char *bridge = NULL;
3234 3235
    virNetworkObjPtr privnet;

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

    if (privnet == NULL) {
3242
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3243
        goto cleanup;
3244 3245
    }

3246
    if (!(privnet->def->bridge)) {
3247
        testError(VIR_ERR_INTERNAL_ERROR,
3248 3249 3250 3251 3252 3253
                  _("network '%s' does not have a bridge name."),
                  privnet->def->name);
        goto cleanup;
    }

    if (!(bridge = strdup(privnet->def->bridge))) {
3254
        virReportOOMError();
3255
        goto cleanup;
3256
    }
3257 3258

cleanup:
3259 3260
    if (privnet)
        virNetworkObjUnlock(privnet);
3261 3262 3263 3264 3265
    return bridge;
}

static int testNetworkGetAutostart(virNetworkPtr network,
                                   int *autostart) {
3266 3267
    testConnPtr privconn = network->conn->privateData;
    virNetworkObjPtr privnet;
3268
    int ret = -1;
3269

3270
    testDriverLock(privconn);
3271 3272
    privnet = virNetworkFindByName(&privconn->networks,
                                   network->name);
3273
    testDriverUnlock(privconn);
3274 3275

    if (privnet == NULL) {
3276
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3277
        goto cleanup;
3278 3279
    }

3280
    *autostart = privnet->autostart;
3281 3282 3283
    ret = 0;

cleanup:
3284 3285
    if (privnet)
        virNetworkObjUnlock(privnet);
3286
    return ret;
3287 3288 3289 3290
}

static int testNetworkSetAutostart(virNetworkPtr network,
                                   int autostart) {
3291 3292
    testConnPtr privconn = network->conn->privateData;
    virNetworkObjPtr privnet;
3293
    int ret = -1;
3294

3295
    testDriverLock(privconn);
3296 3297
    privnet = virNetworkFindByName(&privconn->networks,
                                   network->name);
3298
    testDriverUnlock(privconn);
3299 3300

    if (privnet == NULL) {
3301
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3302
        goto cleanup;
3303 3304
    }

3305
    privnet->autostart = autostart ? 1 : 0;
3306 3307 3308
    ret = 0;

cleanup:
3309 3310
    if (privnet)
        virNetworkObjUnlock(privnet);
3311
    return ret;
3312
}
3313

C
Cole Robinson 已提交
3314

L
Laine Stump 已提交
3315 3316 3317 3318 3319 3320
/*
 * Physical host interface routines
 */

static virDrvOpenStatus testOpenInterface(virConnectPtr conn,
                                          virConnectAuthPtr auth ATTRIBUTE_UNUSED,
E
Eric Blake 已提交
3321
                                          unsigned int flags)
L
Laine Stump 已提交
3322
{
E
Eric Blake 已提交
3323 3324
    virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);

L
Laine Stump 已提交
3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346
    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 已提交
3347
        if (virInterfaceObjIsActive(privconn->ifaces.objs[i])) {
L
Laine Stump 已提交
3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364
            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 已提交
3365
        if (virInterfaceObjIsActive(privconn->ifaces.objs[i])) {
L
Laine Stump 已提交
3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377
            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:
3378
    virReportOOMError();
L
Laine Stump 已提交
3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392
    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 已提交
3393
        if (!virInterfaceObjIsActive(privconn->ifaces.objs[i])) {
L
Laine Stump 已提交
3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410
            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 已提交
3411
        if (!virInterfaceObjIsActive(privconn->ifaces.objs[i])) {
L
Laine Stump 已提交
3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423
            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:
3424
    virReportOOMError();
L
Laine Stump 已提交
3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442
    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) {
3443
        testError(VIR_ERR_NO_INTERFACE, NULL);
L
Laine Stump 已提交
3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467
        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) {
3468
        testError(VIR_ERR_NO_INTERFACE, NULL);
L
Laine Stump 已提交
3469 3470 3471 3472
        goto cleanup;
    }

    if (ifacect > 1) {
3473
        testError(VIR_ERR_MULTIPLE_INTERFACES, NULL);
L
Laine Stump 已提交
3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484
        goto cleanup;
    }

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

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

3485 3486 3487 3488 3489 3490 3491 3492 3493 3494
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) {
3495
        testError(VIR_ERR_NO_INTERFACE, NULL);
3496 3497 3498 3499 3500 3501 3502 3503 3504 3505
        goto cleanup;
    }
    ret = virInterfaceObjIsActive(obj);

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

3506
static int testInterfaceChangeBegin(virConnectPtr conn,
E
Eric Blake 已提交
3507
                                    unsigned int flags)
3508 3509 3510 3511
{
    testConnPtr privconn = conn->privateData;
    int ret = -1;

E
Eric Blake 已提交
3512 3513
    virCheckFlags(0, -1);

3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533
    testDriverLock(privconn);
    if (privconn->transaction_running) {
        testError(VIR_ERR_OPERATION_INVALID,
                  _("there is another transaction running."));
        goto cleanup;
    }

    privconn->transaction_running = true;

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

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

static int testInterfaceChangeCommit(virConnectPtr conn,
E
Eric Blake 已提交
3534
                                     unsigned int flags)
3535 3536 3537 3538
{
    testConnPtr privconn = conn->privateData;
    int ret = -1;

E
Eric Blake 已提交
3539 3540
    virCheckFlags(0, -1);

3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560
    testDriverLock(privconn);

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

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

    ret = 0;

cleanup:
    testDriverUnlock(privconn);

    return ret;
}

static int testInterfaceChangeRollback(virConnectPtr conn,
E
Eric Blake 已提交
3561
                                       unsigned int flags)
3562 3563 3564 3565
{
    testConnPtr privconn = conn->privateData;
    int ret = -1;

E
Eric Blake 已提交
3566 3567
    virCheckFlags(0, -1);

3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589
    testDriverLock(privconn);

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

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

    privconn->transaction_running = false;

    ret = 0;

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

L
Laine Stump 已提交
3591
static char *testInterfaceGetXMLDesc(virInterfacePtr iface,
E
Eric Blake 已提交
3592
                                     unsigned int flags)
L
Laine Stump 已提交
3593 3594 3595 3596 3597
{
    testConnPtr privconn = iface->conn->privateData;
    virInterfaceObjPtr privinterface;
    char *ret = NULL;

E
Eric Blake 已提交
3598 3599
    virCheckFlags(0, NULL);

L
Laine Stump 已提交
3600 3601 3602 3603 3604 3605
    testDriverLock(privconn);
    privinterface = virInterfaceFindByName(&privconn->ifaces,
                                           iface->name);
    testDriverUnlock(privconn);

    if (privinterface == NULL) {
3606
        testError(VIR_ERR_NO_INTERFACE, __FUNCTION__);
L
Laine Stump 已提交
3607 3608 3609
        goto cleanup;
    }

3610
    ret = virInterfaceDefFormat(privinterface->def);
L
Laine Stump 已提交
3611 3612 3613 3614 3615 3616 3617 3618 3619

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


static virInterfacePtr testInterfaceDefineXML(virConnectPtr conn, const char *xmlStr,
E
Eric Blake 已提交
3620
                                              unsigned int flags)
L
Laine Stump 已提交
3621 3622 3623 3624 3625 3626
{
    testConnPtr privconn = conn->privateData;
    virInterfaceDefPtr def;
    virInterfaceObjPtr iface = NULL;
    virInterfacePtr ret = NULL;

E
Eric Blake 已提交
3627 3628
    virCheckFlags(0, NULL);

L
Laine Stump 已提交
3629
    testDriverLock(privconn);
3630
    if ((def = virInterfaceDefParseString(xmlStr)) == NULL)
L
Laine Stump 已提交
3631 3632
        goto cleanup;

3633
    if ((iface = virInterfaceAssignDef(&privconn->ifaces, def)) == NULL)
L
Laine Stump 已提交
3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657
        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) {
3658
        testError(VIR_ERR_NO_INTERFACE, NULL);
L
Laine Stump 已提交
3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671
        goto cleanup;
    }

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

cleanup:
    testDriverUnlock(privconn);
    return ret;
}

static int testInterfaceCreate(virInterfacePtr iface,
E
Eric Blake 已提交
3672
                               unsigned int flags)
L
Laine Stump 已提交
3673 3674 3675 3676 3677
{
    testConnPtr privconn = iface->conn->privateData;
    virInterfaceObjPtr privinterface;
    int ret = -1;

E
Eric Blake 已提交
3678 3679
    virCheckFlags(0, -1);

L
Laine Stump 已提交
3680 3681 3682 3683 3684
    testDriverLock(privconn);
    privinterface = virInterfaceFindByName(&privconn->ifaces,
                                           iface->name);

    if (privinterface == NULL) {
3685
        testError(VIR_ERR_NO_INTERFACE, NULL);
L
Laine Stump 已提交
3686 3687 3688 3689
        goto cleanup;
    }

    if (privinterface->active != 0) {
3690
        testError(VIR_ERR_OPERATION_INVALID, NULL);
L
Laine Stump 已提交
3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704
        goto cleanup;
    }

    privinterface->active = 1;
    ret = 0;

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

static int testInterfaceDestroy(virInterfacePtr iface,
E
Eric Blake 已提交
3705
                                unsigned int flags)
L
Laine Stump 已提交
3706 3707 3708 3709 3710
{
    testConnPtr privconn = iface->conn->privateData;
    virInterfaceObjPtr privinterface;
    int ret = -1;

E
Eric Blake 已提交
3711 3712
    virCheckFlags(0, -1);

L
Laine Stump 已提交
3713 3714 3715 3716 3717
    testDriverLock(privconn);
    privinterface = virInterfaceFindByName(&privconn->ifaces,
                                           iface->name);

    if (privinterface == NULL) {
3718
        testError(VIR_ERR_NO_INTERFACE, NULL);
L
Laine Stump 已提交
3719 3720 3721 3722
        goto cleanup;
    }

    if (privinterface->active == 0) {
3723
        testError(VIR_ERR_OPERATION_INVALID, NULL);
L
Laine Stump 已提交
3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738
        goto cleanup;
    }

    privinterface->active = 0;
    ret = 0;

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



C
Cole Robinson 已提交
3739 3740 3741 3742
/*
 * Storage Driver routines
 */

3743

3744
static int testStoragePoolObjSetDefaults(virStoragePoolObjPtr pool) {
C
Cole Robinson 已提交
3745 3746 3747 3748 3749 3750 3751

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

    pool->configFile = strdup("\0");
    if (!pool->configFile) {
3752
        virReportOOMError();
C
Cole Robinson 已提交
3753 3754 3755 3756 3757 3758
        return -1;
    }

    return 0;
}

3759 3760
static virDrvOpenStatus testStorageOpen(virConnectPtr conn,
                                        virConnectAuthPtr auth ATTRIBUTE_UNUSED,
E
Eric Blake 已提交
3761 3762 3763 3764
                                        unsigned int flags)
{
    virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);

3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776
    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;
}

3777

C
Cole Robinson 已提交
3778 3779 3780
static virStoragePoolPtr
testStoragePoolLookupByUUID(virConnectPtr conn,
                            const unsigned char *uuid) {
3781
    testConnPtr privconn = conn->privateData;
3782 3783
    virStoragePoolObjPtr pool;
    virStoragePoolPtr ret = NULL;
C
Cole Robinson 已提交
3784

3785
    testDriverLock(privconn);
3786
    pool = virStoragePoolObjFindByUUID(&privconn->pools, uuid);
3787
    testDriverUnlock(privconn);
3788 3789

    if (pool == NULL) {
3790
        testError(VIR_ERR_NO_STORAGE_POOL, NULL);
3791
        goto cleanup;
C
Cole Robinson 已提交
3792 3793
    }

3794 3795 3796
    ret = virGetStoragePool(conn, pool->def->name, pool->def->uuid);

cleanup:
3797 3798
    if (pool)
        virStoragePoolObjUnlock(pool);
3799
    return ret;
C
Cole Robinson 已提交
3800 3801 3802 3803 3804
}

static virStoragePoolPtr
testStoragePoolLookupByName(virConnectPtr conn,
                            const char *name) {
3805
    testConnPtr privconn = conn->privateData;
3806 3807
    virStoragePoolObjPtr pool;
    virStoragePoolPtr ret = NULL;
C
Cole Robinson 已提交
3808

3809
    testDriverLock(privconn);
3810
    pool = virStoragePoolObjFindByName(&privconn->pools, name);
3811
    testDriverUnlock(privconn);
3812 3813

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

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

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

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

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

3836
    testDriverLock(privconn);
C
Cole Robinson 已提交
3837 3838 3839
    for (i = 0 ; i < privconn->pools.count ; i++)
        if (virStoragePoolObjIsActive(privconn->pools.objs[i]))
            numActive++;
3840
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
3841 3842 3843 3844 3845 3846 3847 3848

    return numActive;
}

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

3852
    testDriverLock(privconn);
C
Cole Robinson 已提交
3853
    memset(names, 0, sizeof(*names)*nnames);
3854 3855
    for (i = 0 ; i < privconn->pools.count && n < nnames ; i++) {
        virStoragePoolObjLock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
3856
        if (virStoragePoolObjIsActive(privconn->pools.objs[i]) &&
3857 3858
            !(names[n++] = strdup(privconn->pools.objs[i]->def->name))) {
            virStoragePoolObjUnlock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
3859
            goto no_memory;
3860 3861 3862 3863
        }
        virStoragePoolObjUnlock(privconn->pools.objs[i]);
    }
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
3864 3865 3866 3867

    return n;

no_memory:
3868
    virReportOOMError();
C
Cole Robinson 已提交
3869 3870
    for (n = 0 ; n < nnames ; n++)
        VIR_FREE(names[n]);
3871
    testDriverUnlock(privconn);
3872
    return -1;
C
Cole Robinson 已提交
3873 3874 3875 3876
}

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

3880 3881 3882
    testDriverLock(privconn);
    for (i = 0 ; i < privconn->pools.count ; i++) {
        virStoragePoolObjLock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
3883 3884
        if (!virStoragePoolObjIsActive(privconn->pools.objs[i]))
            numInactive++;
3885 3886 3887
        virStoragePoolObjUnlock(privconn->pools.objs[i]);
    }
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
3888 3889 3890 3891 3892 3893 3894 3895

    return numInactive;
}

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

3899
    testDriverLock(privconn);
C
Cole Robinson 已提交
3900
    memset(names, 0, sizeof(*names)*nnames);
3901 3902
    for (i = 0 ; i < privconn->pools.count && n < nnames ; i++) {
        virStoragePoolObjLock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
3903
        if (!virStoragePoolObjIsActive(privconn->pools.objs[i]) &&
3904 3905
            !(names[n++] = strdup(privconn->pools.objs[i]->def->name))) {
            virStoragePoolObjUnlock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
3906
            goto no_memory;
3907 3908 3909 3910
        }
        virStoragePoolObjUnlock(privconn->pools.objs[i]);
    }
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
3911 3912 3913 3914

    return n;

no_memory:
3915
    virReportOOMError();
C
Cole Robinson 已提交
3916 3917
    for (n = 0 ; n < nnames ; n++)
        VIR_FREE(names[n]);
3918
    testDriverUnlock(privconn);
3919
    return -1;
C
Cole Robinson 已提交
3920 3921 3922
}


3923 3924 3925 3926 3927 3928 3929 3930 3931 3932
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) {
3933
        testError(VIR_ERR_NO_STORAGE_POOL, NULL);
3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953
        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) {
3954
        testError(VIR_ERR_NO_STORAGE_POOL, NULL);
3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966
        goto cleanup;
    }
    ret = obj->configFile ? 1 : 0;

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



C
Cole Robinson 已提交
3967
static int
3968
testStoragePoolStart(virStoragePoolPtr pool,
E
Eric Blake 已提交
3969 3970
                     unsigned int flags)
{
3971 3972
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
3973
    int ret = -1;
3974

E
Eric Blake 已提交
3975 3976
    virCheckFlags(0, -1);

3977
    testDriverLock(privconn);
3978 3979
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
3980
    testDriverUnlock(privconn);
3981 3982

    if (privpool == NULL) {
3983
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3984
        goto cleanup;
3985 3986
    }

3987
    if (virStoragePoolObjIsActive(privpool)) {
3988
        testError(VIR_ERR_OPERATION_INVALID,
3989 3990 3991
                  _("storage pool '%s' is already active"), pool->name);
        goto cleanup;
    }
C
Cole Robinson 已提交
3992 3993

    privpool->active = 1;
3994
    ret = 0;
C
Cole Robinson 已提交
3995

3996
cleanup:
3997 3998
    if (privpool)
        virStoragePoolObjUnlock(privpool);
3999
    return ret;
C
Cole Robinson 已提交
4000 4001 4002
}

static char *
4003
testStorageFindPoolSources(virConnectPtr conn ATTRIBUTE_UNUSED,
4004 4005
                           const char *type,
                           const char *srcSpec,
E
Eric Blake 已提交
4006
                           unsigned int flags)
C
Cole Robinson 已提交
4007
{
4008 4009 4010 4011
    virStoragePoolSourcePtr source = NULL;
    int pool_type;
    char *ret = NULL;

E
Eric Blake 已提交
4012 4013
    virCheckFlags(0, NULL);

4014 4015
    pool_type = virStoragePoolTypeFromString(type);
    if (!pool_type) {
4016
        testError(VIR_ERR_INTERNAL_ERROR,
4017 4018 4019 4020 4021
                  _("unknown storage pool type %s"), type);
        goto cleanup;
    }

    if (srcSpec) {
4022
        source = virStoragePoolDefParseSourceString(srcSpec, pool_type);
4023 4024 4025 4026 4027 4028 4029 4030 4031
        if (!source)
            goto cleanup;
    }

    switch (pool_type) {

    case VIR_STORAGE_POOL_LOGICAL:
        ret = strdup(defaultPoolSourcesLogicalXML);
        if (!ret)
4032
            virReportOOMError();
4033 4034 4035 4036
        break;

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

        if (virAsprintf(&ret, defaultPoolSourcesNetFSXML,
                        source->host.name) < 0)
4044
            virReportOOMError();
4045 4046 4047
        break;

    default:
4048
        testError(VIR_ERR_NO_SUPPORT,
4049 4050 4051 4052 4053
                  _("pool type '%s' does not support source discovery"), type);
    }

cleanup:
    virStoragePoolSourceFree(source);
4054
    VIR_FREE(source);
4055
    return ret;
C
Cole Robinson 已提交
4056 4057 4058 4059 4060 4061
}


static virStoragePoolPtr
testStoragePoolCreate(virConnectPtr conn,
                      const char *xml,
E
Eric Blake 已提交
4062 4063
                      unsigned int flags)
{
4064
    testConnPtr privconn = conn->privateData;
C
Cole Robinson 已提交
4065
    virStoragePoolDefPtr def;
4066
    virStoragePoolObjPtr pool = NULL;
4067
    virStoragePoolPtr ret = NULL;
C
Cole Robinson 已提交
4068

E
Eric Blake 已提交
4069 4070
    virCheckFlags(0, NULL);

4071
    testDriverLock(privconn);
4072
    if (!(def = virStoragePoolDefParseString(xml)))
4073
        goto cleanup;
C
Cole Robinson 已提交
4074

4075 4076 4077 4078
    pool = virStoragePoolObjFindByUUID(&privconn->pools, def->uuid);
    if (!pool)
        pool = virStoragePoolObjFindByName(&privconn->pools, def->name);
    if (pool) {
4079
        testError(VIR_ERR_INTERNAL_ERROR,
C
Cole Robinson 已提交
4080
                  "%s", _("storage pool already exists"));
4081
        goto cleanup;
C
Cole Robinson 已提交
4082 4083
    }

4084
    if (!(pool = virStoragePoolObjAssignDef(&privconn->pools, def)))
4085
        goto cleanup;
4086
    def = NULL;
C
Cole Robinson 已提交
4087

4088
    if (testStoragePoolObjSetDefaults(pool) == -1) {
C
Cole Robinson 已提交
4089
        virStoragePoolObjRemove(&privconn->pools, pool);
4090 4091
        pool = NULL;
        goto cleanup;
C
Cole Robinson 已提交
4092 4093 4094
    }
    pool->active = 1;

4095 4096 4097 4098
    ret = virGetStoragePool(conn, pool->def->name, pool->def->uuid);

cleanup:
    virStoragePoolDefFree(def);
4099 4100 4101
    if (pool)
        virStoragePoolObjUnlock(pool);
    testDriverUnlock(privconn);
4102
    return ret;
C
Cole Robinson 已提交
4103 4104 4105 4106 4107
}

static virStoragePoolPtr
testStoragePoolDefine(virConnectPtr conn,
                      const char *xml,
E
Eric Blake 已提交
4108 4109
                      unsigned int flags)
{
4110
    testConnPtr privconn = conn->privateData;
C
Cole Robinson 已提交
4111
    virStoragePoolDefPtr def;
4112
    virStoragePoolObjPtr pool = NULL;
4113
    virStoragePoolPtr ret = NULL;
C
Cole Robinson 已提交
4114

E
Eric Blake 已提交
4115 4116
    virCheckFlags(0, NULL);

4117
    testDriverLock(privconn);
4118
    if (!(def = virStoragePoolDefParseString(xml)))
4119
        goto cleanup;
C
Cole Robinson 已提交
4120 4121 4122 4123 4124

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

4125
    if (!(pool = virStoragePoolObjAssignDef(&privconn->pools, def)))
4126 4127
        goto cleanup;
    def = NULL;
C
Cole Robinson 已提交
4128

4129
    if (testStoragePoolObjSetDefaults(pool) == -1) {
C
Cole Robinson 已提交
4130
        virStoragePoolObjRemove(&privconn->pools, pool);
4131 4132
        pool = NULL;
        goto cleanup;
C
Cole Robinson 已提交
4133 4134
    }

4135 4136 4137 4138
    ret = virGetStoragePool(conn, pool->def->name, pool->def->uuid);

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

static int
4146 4147 4148
testStoragePoolUndefine(virStoragePoolPtr pool) {
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4149
    int ret = -1;
4150

4151
    testDriverLock(privconn);
4152 4153 4154 4155
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);

    if (privpool == NULL) {
4156
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4157
        goto cleanup;
4158 4159
    }

4160
    if (virStoragePoolObjIsActive(privpool)) {
4161
        testError(VIR_ERR_OPERATION_INVALID,
4162 4163 4164
                  _("storage pool '%s' is already active"), pool->name);
        goto cleanup;
    }
C
Cole Robinson 已提交
4165 4166

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

4169
cleanup:
4170 4171 4172
    if (privpool)
        virStoragePoolObjUnlock(privpool);
    testDriverUnlock(privconn);
4173
    return ret;
C
Cole Robinson 已提交
4174 4175 4176
}

static int
4177
testStoragePoolBuild(virStoragePoolPtr pool,
E
Eric Blake 已提交
4178 4179
                     unsigned int flags)
{
4180 4181
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4182
    int ret = -1;
4183

E
Eric Blake 已提交
4184 4185
    virCheckFlags(0, -1);

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

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

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

4203
cleanup:
4204 4205
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4206
    return ret;
C
Cole Robinson 已提交
4207 4208 4209 4210
}


static int
4211 4212 4213
testStoragePoolDestroy(virStoragePoolPtr pool) {
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4214
    int ret = -1;
4215

4216
    testDriverLock(privconn);
4217 4218 4219 4220
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);

    if (privpool == NULL) {
4221
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4222
        goto cleanup;
4223 4224 4225
    }

    if (!virStoragePoolObjIsActive(privpool)) {
4226
        testError(VIR_ERR_OPERATION_INVALID,
4227
                  _("storage pool '%s' is not active"), pool->name);
4228
        goto cleanup;
4229
    }
C
Cole Robinson 已提交
4230 4231 4232

    privpool->active = 0;

4233
    if (privpool->configFile == NULL) {
C
Cole Robinson 已提交
4234
        virStoragePoolObjRemove(&privconn->pools, privpool);
4235 4236
        privpool = NULL;
    }
4237
    ret = 0;
C
Cole Robinson 已提交
4238

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


static int
4248
testStoragePoolDelete(virStoragePoolPtr pool,
E
Eric Blake 已提交
4249 4250
                      unsigned int flags)
{
4251 4252
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4253
    int ret = -1;
4254

E
Eric Blake 已提交
4255 4256
    virCheckFlags(0, -1);

4257
    testDriverLock(privconn);
4258 4259
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4260
    testDriverUnlock(privconn);
4261 4262

    if (privpool == NULL) {
4263
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4264 4265 4266 4267
        goto cleanup;
    }

    if (virStoragePoolObjIsActive(privpool)) {
4268
        testError(VIR_ERR_OPERATION_INVALID,
4269 4270
                  _("storage pool '%s' is already active"), pool->name);
        goto cleanup;
4271 4272
    }

4273
    ret = 0;
C
Cole Robinson 已提交
4274

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


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

E
Eric Blake 已提交
4290 4291
    virCheckFlags(0, -1);

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

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

    if (!virStoragePoolObjIsActive(privpool)) {
4303
        testError(VIR_ERR_OPERATION_INVALID,
4304
                  _("storage pool '%s' is not active"), pool->name);
4305
        goto cleanup;
4306
    }
4307
    ret = 0;
C
Cole Robinson 已提交
4308

4309
cleanup:
4310 4311
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4312
    return ret;
C
Cole Robinson 已提交
4313 4314 4315 4316
}


static int
4317
testStoragePoolGetInfo(virStoragePoolPtr pool,
C
Cole Robinson 已提交
4318
                       virStoragePoolInfoPtr info) {
4319 4320
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4321
    int ret = -1;
4322

4323
    testDriverLock(privconn);
4324 4325
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4326
    testDriverUnlock(privconn);
4327 4328

    if (privpool == NULL) {
4329
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4330
        goto cleanup;
4331
    }
C
Cole Robinson 已提交
4332 4333 4334 4335 4336 4337 4338 4339 4340

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

4343
cleanup:
4344 4345
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4346
    return ret;
C
Cole Robinson 已提交
4347 4348 4349
}

static char *
4350
testStoragePoolGetXMLDesc(virStoragePoolPtr pool,
E
Eric Blake 已提交
4351 4352
                          unsigned int flags)
{
4353 4354
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4355
    char *ret = NULL;
4356

E
Eric Blake 已提交
4357 4358
    virCheckFlags(0, NULL);

4359
    testDriverLock(privconn);
4360 4361
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4362
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
4363

4364
    if (privpool == NULL) {
4365
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4366
        goto cleanup;
4367 4368
    }

4369
    ret = virStoragePoolDefFormat(privpool->def);
4370 4371

cleanup:
4372 4373
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4374
    return ret;
C
Cole Robinson 已提交
4375 4376 4377
}

static int
4378
testStoragePoolGetAutostart(virStoragePoolPtr pool,
C
Cole Robinson 已提交
4379
                            int *autostart) {
4380 4381
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4382
    int ret = -1;
4383

4384
    testDriverLock(privconn);
4385 4386
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4387
    testDriverUnlock(privconn);
4388 4389

    if (privpool == NULL) {
4390
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4391
        goto cleanup;
4392
    }
C
Cole Robinson 已提交
4393 4394 4395 4396 4397 4398

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

4401
cleanup:
4402 4403
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4404
    return ret;
C
Cole Robinson 已提交
4405 4406 4407
}

static int
4408
testStoragePoolSetAutostart(virStoragePoolPtr pool,
C
Cole Robinson 已提交
4409
                            int autostart) {
4410 4411
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4412
    int ret = -1;
4413

4414
    testDriverLock(privconn);
4415 4416
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4417
    testDriverUnlock(privconn);
4418 4419

    if (privpool == NULL) {
4420
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4421
        goto cleanup;
4422
    }
C
Cole Robinson 已提交
4423 4424

    if (!privpool->configFile) {
4425
        testError(VIR_ERR_INVALID_ARG,
C
Cole Robinson 已提交
4426
                  "%s", _("pool has no config file"));
4427
        goto cleanup;
C
Cole Robinson 已提交
4428 4429 4430 4431
    }

    autostart = (autostart != 0);
    privpool->autostart = autostart;
4432 4433 4434
    ret = 0;

cleanup:
4435 4436
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4437
    return ret;
C
Cole Robinson 已提交
4438 4439 4440 4441
}


static int
4442 4443 4444
testStoragePoolNumVolumes(virStoragePoolPtr pool) {
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4445
    int ret = -1;
4446

4447
    testDriverLock(privconn);
4448 4449
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4450
    testDriverUnlock(privconn);
4451 4452

    if (privpool == NULL) {
4453
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4454
        goto cleanup;
4455 4456 4457
    }

    if (!virStoragePoolObjIsActive(privpool)) {
4458
        testError(VIR_ERR_OPERATION_INVALID,
4459
                  _("storage pool '%s' is not active"), pool->name);
4460
        goto cleanup;
4461
    }
C
Cole Robinson 已提交
4462

4463 4464 4465
    ret = privpool->volumes.count;

cleanup:
4466 4467
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4468
    return ret;
C
Cole Robinson 已提交
4469 4470 4471
}

static int
4472
testStoragePoolListVolumes(virStoragePoolPtr pool,
C
Cole Robinson 已提交
4473 4474
                           char **const names,
                           int maxnames) {
4475 4476
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
C
Cole Robinson 已提交
4477 4478
    int i = 0, n = 0;

4479
    memset(names, 0, maxnames * sizeof(*names));
4480 4481

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

    if (privpool == NULL) {
4487
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4488
        goto cleanup;
4489 4490 4491 4492
    }


    if (!virStoragePoolObjIsActive(privpool)) {
4493
        testError(VIR_ERR_OPERATION_INVALID,
4494
                  _("storage pool '%s' is not active"), pool->name);
4495
        goto cleanup;
4496 4497
    }

C
Cole Robinson 已提交
4498 4499
    for (i = 0 ; i < privpool->volumes.count && n < maxnames ; i++) {
        if ((names[n++] = strdup(privpool->volumes.objs[i]->name)) == NULL) {
4500
            virReportOOMError();
C
Cole Robinson 已提交
4501 4502 4503 4504
            goto cleanup;
        }
    }

4505
    virStoragePoolObjUnlock(privpool);
C
Cole Robinson 已提交
4506 4507 4508 4509 4510 4511
    return n;

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

4512
    memset(names, 0, maxnames * sizeof(*names));
4513 4514
    if (privpool)
        virStoragePoolObjUnlock(privpool);
C
Cole Robinson 已提交
4515 4516 4517 4518 4519
    return -1;
}


static virStorageVolPtr
4520
testStorageVolumeLookupByName(virStoragePoolPtr pool,
C
Cole Robinson 已提交
4521
                              const char *name ATTRIBUTE_UNUSED) {
4522 4523 4524
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol;
4525
    virStorageVolPtr ret = NULL;
4526

4527
    testDriverLock(privconn);
4528 4529
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4530
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
4531

4532
    if (privpool == NULL) {
4533
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4534
        goto cleanup;
4535 4536 4537 4538
    }


    if (!virStoragePoolObjIsActive(privpool)) {
4539
        testError(VIR_ERR_OPERATION_INVALID,
4540
                  _("storage pool '%s' is not active"), pool->name);
4541
        goto cleanup;
4542 4543 4544 4545 4546
    }

    privvol = virStorageVolDefFindByName(privpool, name);

    if (!privvol) {
4547
        testError(VIR_ERR_NO_STORAGE_VOL,
C
Cole Robinson 已提交
4548
                  _("no storage vol with matching name '%s'"), name);
4549
        goto cleanup;
C
Cole Robinson 已提交
4550 4551
    }

4552 4553 4554 4555
    ret = virGetStorageVol(pool->conn, privpool->def->name,
                           privvol->name, privvol->key);

cleanup:
4556 4557
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4558
    return ret;
C
Cole Robinson 已提交
4559 4560 4561 4562 4563 4564
}


static virStorageVolPtr
testStorageVolumeLookupByKey(virConnectPtr conn,
                             const char *key) {
4565
    testConnPtr privconn = conn->privateData;
C
Cole Robinson 已提交
4566
    unsigned int i;
4567
    virStorageVolPtr ret = NULL;
C
Cole Robinson 已提交
4568

4569
    testDriverLock(privconn);
C
Cole Robinson 已提交
4570
    for (i = 0 ; i < privconn->pools.count ; i++) {
4571
        virStoragePoolObjLock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
4572
        if (virStoragePoolObjIsActive(privconn->pools.objs[i])) {
4573
            virStorageVolDefPtr privvol =
C
Cole Robinson 已提交
4574 4575
                virStorageVolDefFindByKey(privconn->pools.objs[i], key);

4576 4577 4578 4579 4580
            if (privvol) {
                ret = virGetStorageVol(conn,
                                       privconn->pools.objs[i]->def->name,
                                       privvol->name,
                                       privvol->key);
4581
                virStoragePoolObjUnlock(privconn->pools.objs[i]);
4582 4583
                break;
            }
C
Cole Robinson 已提交
4584
        }
4585
        virStoragePoolObjUnlock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
4586
    }
4587
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
4588

4589
    if (!ret)
4590
        testError(VIR_ERR_NO_STORAGE_VOL,
4591 4592 4593
                  _("no storage vol with matching key '%s'"), key);

    return ret;
C
Cole Robinson 已提交
4594 4595 4596 4597 4598
}

static virStorageVolPtr
testStorageVolumeLookupByPath(virConnectPtr conn,
                              const char *path) {
4599
    testConnPtr privconn = conn->privateData;
C
Cole Robinson 已提交
4600
    unsigned int i;
4601
    virStorageVolPtr ret = NULL;
C
Cole Robinson 已提交
4602

4603
    testDriverLock(privconn);
C
Cole Robinson 已提交
4604
    for (i = 0 ; i < privconn->pools.count ; i++) {
4605
        virStoragePoolObjLock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
4606
        if (virStoragePoolObjIsActive(privconn->pools.objs[i])) {
4607
            virStorageVolDefPtr privvol =
C
Cole Robinson 已提交
4608 4609
                virStorageVolDefFindByPath(privconn->pools.objs[i], path);

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

4623
    if (!ret)
4624
        testError(VIR_ERR_NO_STORAGE_VOL,
4625 4626 4627
                  _("no storage vol with matching path '%s'"), path);

    return ret;
C
Cole Robinson 已提交
4628 4629 4630
}

static virStorageVolPtr
4631
testStorageVolumeCreateXML(virStoragePoolPtr pool,
C
Cole Robinson 已提交
4632
                           const char *xmldesc,
E
Eric Blake 已提交
4633 4634
                           unsigned int flags)
{
4635 4636
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4637 4638
    virStorageVolDefPtr privvol = NULL;
    virStorageVolPtr ret = NULL;
4639

E
Eric Blake 已提交
4640 4641
    virCheckFlags(0, NULL);

4642
    testDriverLock(privconn);
4643 4644
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4645
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
4646

4647
    if (privpool == NULL) {
4648
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4649
        goto cleanup;
4650 4651 4652
    }

    if (!virStoragePoolObjIsActive(privpool)) {
4653
        testError(VIR_ERR_OPERATION_INVALID,
4654
                  _("storage pool '%s' is not active"), pool->name);
4655
        goto cleanup;
4656
    }
C
Cole Robinson 已提交
4657

4658
    privvol = virStorageVolDefParseString(privpool->def, xmldesc);
4659
    if (privvol == NULL)
4660
        goto cleanup;
4661 4662

    if (virStorageVolDefFindByName(privpool, privvol->name)) {
4663
        testError(VIR_ERR_OPERATION_FAILED,
C
Cole Robinson 已提交
4664
                  "%s", _("storage vol already exists"));
4665
        goto cleanup;
C
Cole Robinson 已提交
4666 4667 4668
    }

    /* Make sure enough space */
4669
    if ((privpool->def->allocation + privvol->allocation) >
C
Cole Robinson 已提交
4670
         privpool->def->capacity) {
4671
        testError(VIR_ERR_INTERNAL_ERROR,
C
Cole Robinson 已提交
4672
                  _("Not enough free space in pool for volume '%s'"),
4673
                  privvol->name);
4674
        goto cleanup;
C
Cole Robinson 已提交
4675 4676 4677 4678
    }

    if (VIR_REALLOC_N(privpool->volumes.objs,
                      privpool->volumes.count+1) < 0) {
4679
        virReportOOMError();
4680
        goto cleanup;
C
Cole Robinson 已提交
4681 4682
    }

4683 4684 4685
    if (virAsprintf(&privvol->target.path, "%s/%s",
                    privpool->def->target.path,
                    privvol->name) == -1) {
4686
        virReportOOMError();
4687
        goto cleanup;
C
Cole Robinson 已提交
4688 4689
    }

4690 4691
    privvol->key = strdup(privvol->target.path);
    if (privvol->key == NULL) {
4692
        virReportOOMError();
4693
        goto cleanup;
C
Cole Robinson 已提交
4694 4695
    }

4696
    privpool->def->allocation += privvol->allocation;
C
Cole Robinson 已提交
4697 4698 4699
    privpool->def->available = (privpool->def->capacity -
                                privpool->def->allocation);

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

4702 4703
    ret = virGetStorageVol(pool->conn, privpool->def->name,
                           privvol->name, privvol->key);
4704
    privvol = NULL;
4705 4706 4707

cleanup:
    virStorageVolDefFree(privvol);
4708 4709
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4710
    return ret;
C
Cole Robinson 已提交
4711 4712
}

4713 4714 4715 4716
static virStorageVolPtr
testStorageVolumeCreateXMLFrom(virStoragePoolPtr pool,
                               const char *xmldesc,
                               virStorageVolPtr clonevol,
E
Eric Blake 已提交
4717 4718
                               unsigned int flags)
{
4719 4720 4721 4722 4723
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol = NULL, origvol = NULL;
    virStorageVolPtr ret = NULL;

E
Eric Blake 已提交
4724 4725
    virCheckFlags(0, NULL);

4726 4727 4728 4729 4730 4731
    testDriverLock(privconn);
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
    testDriverUnlock(privconn);

    if (privpool == NULL) {
4732
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4733 4734 4735 4736
        goto cleanup;
    }

    if (!virStoragePoolObjIsActive(privpool)) {
4737
        testError(VIR_ERR_OPERATION_INVALID,
4738 4739 4740 4741
                  _("storage pool '%s' is not active"), pool->name);
        goto cleanup;
    }

4742
    privvol = virStorageVolDefParseString(privpool->def, xmldesc);
4743 4744 4745 4746
    if (privvol == NULL)
        goto cleanup;

    if (virStorageVolDefFindByName(privpool, privvol->name)) {
4747
        testError(VIR_ERR_OPERATION_FAILED,
4748 4749 4750 4751 4752 4753
                  "%s", _("storage vol already exists"));
        goto cleanup;
    }

    origvol = virStorageVolDefFindByName(privpool, clonevol->name);
    if (!origvol) {
4754
        testError(VIR_ERR_NO_STORAGE_VOL,
4755 4756 4757 4758 4759 4760 4761 4762
                  _("no storage vol with matching name '%s'"),
                  clonevol->name);
        goto cleanup;
    }

    /* Make sure enough space */
    if ((privpool->def->allocation + privvol->allocation) >
         privpool->def->capacity) {
4763
        testError(VIR_ERR_INTERNAL_ERROR,
4764 4765 4766 4767 4768 4769 4770 4771 4772
                  _("Not enough free space in pool for volume '%s'"),
                  privvol->name);
        goto cleanup;
    }
    privpool->def->available = (privpool->def->capacity -
                                privpool->def->allocation);

    if (VIR_REALLOC_N(privpool->volumes.objs,
                      privpool->volumes.count+1) < 0) {
4773
        virReportOOMError();
4774 4775 4776
        goto cleanup;
    }

4777 4778 4779
    if (virAsprintf(&privvol->target.path, "%s/%s",
                    privpool->def->target.path,
                    privvol->name) == -1) {
4780
        virReportOOMError();
4781 4782 4783 4784 4785
        goto cleanup;
    }

    privvol->key = strdup(privvol->target.path);
    if (privvol->key == NULL) {
4786
        virReportOOMError();
4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806
        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 已提交
4807
static int
4808
testStorageVolumeDelete(virStorageVolPtr vol,
E
Eric Blake 已提交
4809 4810
                        unsigned int flags)
{
4811 4812 4813
    testConnPtr privconn = vol->conn->privateData;
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol;
C
Cole Robinson 已提交
4814
    int i;
4815
    int ret = -1;
C
Cole Robinson 已提交
4816

E
Eric Blake 已提交
4817 4818
    virCheckFlags(0, -1);

4819
    testDriverLock(privconn);
4820 4821
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           vol->pool);
4822
    testDriverUnlock(privconn);
4823 4824

    if (privpool == NULL) {
4825
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4826
        goto cleanup;
4827 4828 4829 4830 4831 4832
    }


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

    if (privvol == NULL) {
4833
        testError(VIR_ERR_NO_STORAGE_VOL,
4834 4835
                  _("no storage vol with matching name '%s'"),
                  vol->name);
4836
        goto cleanup;
4837 4838 4839
    }

    if (!virStoragePoolObjIsActive(privpool)) {
4840
        testError(VIR_ERR_OPERATION_INVALID,
4841
                  _("storage pool '%s' is not active"), vol->pool);
4842
        goto cleanup;
4843 4844 4845
    }


C
Cole Robinson 已提交
4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868
    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;
        }
    }
4869
    ret = 0;
C
Cole Robinson 已提交
4870

4871
cleanup:
4872 4873
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4874
    return ret;
C
Cole Robinson 已提交
4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890
}


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
4891
testStorageVolumeGetInfo(virStorageVolPtr vol,
C
Cole Robinson 已提交
4892
                         virStorageVolInfoPtr info) {
4893 4894 4895
    testConnPtr privconn = vol->conn->privateData;
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol;
4896
    int ret = -1;
4897

4898
    testDriverLock(privconn);
4899 4900
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           vol->pool);
4901
    testDriverUnlock(privconn);
4902 4903

    if (privpool == NULL) {
4904
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4905
        goto cleanup;
4906 4907 4908 4909 4910
    }

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

    if (privvol == NULL) {
4911
        testError(VIR_ERR_NO_STORAGE_VOL,
4912 4913
                  _("no storage vol with matching name '%s'"),
                  vol->name);
4914
        goto cleanup;
4915 4916 4917
    }

    if (!virStoragePoolObjIsActive(privpool)) {
4918
        testError(VIR_ERR_OPERATION_INVALID,
4919
                  _("storage pool '%s' is not active"), vol->pool);
4920
        goto cleanup;
4921
    }
C
Cole Robinson 已提交
4922 4923 4924 4925 4926

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

4929
cleanup:
4930 4931
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4932
    return ret;
C
Cole Robinson 已提交
4933 4934 4935
}

static char *
4936
testStorageVolumeGetXMLDesc(virStorageVolPtr vol,
E
Eric Blake 已提交
4937 4938
                            unsigned int flags)
{
4939 4940 4941
    testConnPtr privconn = vol->conn->privateData;
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol;
4942
    char *ret = NULL;
4943

E
Eric Blake 已提交
4944 4945
    virCheckFlags(0, NULL);

4946
    testDriverLock(privconn);
4947 4948
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           vol->pool);
4949
    testDriverUnlock(privconn);
4950 4951

    if (privpool == NULL) {
4952
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4953
        goto cleanup;
4954 4955 4956 4957 4958
    }

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

    if (privvol == NULL) {
4959
        testError(VIR_ERR_NO_STORAGE_VOL,
4960 4961
                  _("no storage vol with matching name '%s'"),
                  vol->name);
4962
        goto cleanup;
4963
    }
C
Cole Robinson 已提交
4964

4965
    if (!virStoragePoolObjIsActive(privpool)) {
4966
        testError(VIR_ERR_OPERATION_INVALID,
4967
                  _("storage pool '%s' is not active"), vol->pool);
4968
        goto cleanup;
4969 4970
    }

4971
    ret = virStorageVolDefFormat(privpool->def, privvol);
4972 4973

cleanup:
4974 4975
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4976
    return ret;
C
Cole Robinson 已提交
4977 4978 4979
}

static char *
4980 4981 4982 4983
testStorageVolumeGetPath(virStorageVolPtr vol) {
    testConnPtr privconn = vol->conn->privateData;
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol;
4984
    char *ret = NULL;
4985

4986
    testDriverLock(privconn);
4987 4988
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           vol->pool);
4989
    testDriverUnlock(privconn);
4990 4991

    if (privpool == NULL) {
4992
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4993
        goto cleanup;
4994 4995 4996 4997 4998
    }

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

    if (privvol == NULL) {
4999
        testError(VIR_ERR_NO_STORAGE_VOL,
5000 5001
                  _("no storage vol with matching name '%s'"),
                  vol->name);
5002
        goto cleanup;
5003 5004 5005
    }

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

C
Cole Robinson 已提交
5011
    ret = strdup(privvol->target.path);
5012
    if (ret == NULL)
5013
        virReportOOMError();
5014 5015

cleanup:
5016 5017
    if (privpool)
        virStoragePoolObjUnlock(privpool);
C
Cole Robinson 已提交
5018 5019 5020
    return ret;
}

5021

5022
/* Node device implementations */
5023 5024
static virDrvOpenStatus testDevMonOpen(virConnectPtr conn,
                                       virConnectAuthPtr auth ATTRIBUTE_UNUSED,
E
Eric Blake 已提交
5025 5026 5027 5028
                                       unsigned int flags)
{
    virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);

5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040
    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;
}

5041 5042 5043
static int
testNodeNumOfDevices(virConnectPtr conn,
                     const char *cap,
E
Eric Blake 已提交
5044
                     unsigned int flags)
5045 5046 5047 5048 5049
{
    testConnPtr driver = conn->privateData;
    int ndevs = 0;
    unsigned int i;

E
Eric Blake 已提交
5050 5051
    virCheckFlags(0, -1);

5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066
    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 已提交
5067
                    unsigned int flags)
5068 5069 5070 5071 5072
{
    testConnPtr driver = conn->privateData;
    int ndevs = 0;
    unsigned int i;

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

5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110
    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) {
5111
        virNodeDeviceReportError(VIR_ERR_NO_NODE_DEVICE, NULL);
5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123
        goto cleanup;
    }

    ret = virGetNodeDevice(conn, name);

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

static char *
5124
testNodeDeviceGetXMLDesc(virNodeDevicePtr dev,
E
Eric Blake 已提交
5125
                         unsigned int flags)
5126 5127 5128 5129 5130
{
    testConnPtr driver = dev->conn->privateData;
    virNodeDeviceObjPtr obj;
    char *ret = NULL;

E
Eric Blake 已提交
5131 5132
    virCheckFlags(0, NULL);

5133 5134 5135 5136 5137
    testDriverLock(driver);
    obj = virNodeDeviceFindByName(&driver->devs, dev->name);
    testDriverUnlock(driver);

    if (!obj) {
5138 5139
        virNodeDeviceReportError(VIR_ERR_NO_NODE_DEVICE,
                                 _("no node device with matching name '%s'"),
5140 5141 5142 5143
                                 dev->name);
        goto cleanup;
    }

5144
    ret = virNodeDeviceDefFormat(obj->def);
5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163

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) {
5164
        virNodeDeviceReportError(VIR_ERR_NO_NODE_DEVICE,
5165 5166 5167 5168 5169 5170 5171 5172
                                _("no node device with matching name '%s'"),
                                 dev->name);
        goto cleanup;
    }

    if (obj->def->parent) {
        ret = strdup(obj->def->parent);
        if (!ret)
5173
            virReportOOMError();
5174
    } else {
5175
        virNodeDeviceReportError(VIR_ERR_INTERNAL_ERROR,
5176 5177 5178 5179 5180 5181 5182 5183 5184
                                 "%s", _("no parent for this device"));
    }

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

5185

5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199
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) {
5200 5201
        virNodeDeviceReportError(VIR_ERR_NO_NODE_DEVICE,
                                 _("no node device with matching name '%s'"),
5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230
                                 dev->name);
        goto cleanup;
    }

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

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


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

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

    if (!obj) {
5231
        virNodeDeviceReportError(VIR_ERR_NO_NODE_DEVICE,
5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254
                                _("no node device with matching name '%s'"),
                                 dev->name);
        goto cleanup;
    }

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

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

5255 5256 5257
static virNodeDevicePtr
testNodeDeviceCreateXML(virConnectPtr conn,
                        const char *xmlDesc,
E
Eric Blake 已提交
5258
                        unsigned int flags)
5259 5260 5261 5262 5263 5264 5265 5266 5267
{
    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 已提交
5268 5269
    virCheckFlags(0, NULL);

5270 5271
    testDriverLock(driver);

5272
    def = virNodeDeviceDefParseString(xmlDesc, CREATE_DEVICE);
5273 5274 5275 5276 5277
    if (def == NULL) {
        goto cleanup;
    }

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

5282
    if (virNodeDeviceGetParentHost(&driver->devs,
5283 5284 5285 5286 5287 5288 5289 5290 5291 5292
                                   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))) {
5293
        virReportOOMError();
5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308
        goto cleanup;
    }

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

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


5309
    if (!(obj = virNodeDeviceAssignDef(&driver->devs, def))) {
5310 5311 5312 5313 5314 5315 5316 5317
        goto cleanup;
    }
    virNodeDeviceObjUnlock(obj);

    dev = virGetNodeDevice(conn, def->name);
    def = NULL;
cleanup:
    testDriverUnlock(driver);
5318
    virNodeDeviceDefFree(def);
5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337
    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) {
5338
        virNodeDeviceReportError(VIR_ERR_NO_NODE_DEVICE, NULL);
5339 5340 5341
        goto out;
    }

5342
    if (virNodeDeviceGetWWNs(obj->def, &wwnn, &wwpn) == -1) {
5343 5344 5345 5346 5347
        goto out;
    }

    parent_name = strdup(obj->def->parent);
    if (parent_name == NULL) {
5348
        virReportOOMError();
5349 5350 5351 5352 5353 5354 5355 5356 5357 5358
        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 */
5359
    if (virNodeDeviceGetParentHost(&driver->devs,
5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378
                                   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;
}

5379 5380

/* Domain event implementations */
5381
static int
5382 5383 5384 5385
testDomainEventRegister(virConnectPtr conn,
                        virConnectDomainEventCallback callback,
                        void *opaque,
                        virFreeCallback freecb)
5386 5387 5388 5389 5390
{
    testConnPtr driver = conn->privateData;
    int ret;

    testDriverLock(driver);
5391 5392
    ret = virDomainEventCallbackListAdd(conn,
                                        driver->domainEventState->callbacks,
5393 5394 5395 5396 5397 5398
                                        callback, opaque, freecb);
    testDriverUnlock(driver);

    return ret;
}

5399

5400
static int
5401 5402
testDomainEventDeregister(virConnectPtr conn,
                          virConnectDomainEventCallback callback)
5403 5404 5405 5406 5407
{
    testConnPtr driver = conn->privateData;
    int ret;

    testDriverLock(driver);
5408 5409 5410
    ret = virDomainEventStateDeregister(conn,
                                        driver->domainEventState,
                                        callback);
5411 5412 5413 5414 5415
    testDriverUnlock(driver);

    return ret;
}

5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428

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

    testDriverLock(driver);
5429 5430
    ret = virDomainEventCallbackListAddID(conn,
                                          driver->domainEventState->callbacks,
5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445
                                          dom, eventID,
                                          callback, opaque, freecb);
    testDriverUnlock(driver);

    return ret;
}

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

    testDriverLock(driver);
5446 5447 5448
    ret = virDomainEventStateDeregisterAny(conn,
                                           driver->domainEventState,
                                           callbackID);
5449 5450 5451 5452 5453 5454
    testDriverUnlock(driver);

    return ret;
}


5455 5456
static void testDomainEventDispatchFunc(virConnectPtr conn,
                                        virDomainEventPtr event,
5457
                                        virConnectDomainEventGenericCallback cb,
5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473
                                        void *cbopaque,
                                        void *opaque)
{
    testConnPtr driver = opaque;

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

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

    testDriverLock(driver);
5474 5475 5476
    virDomainEventStateFlush(driver->domainEventState,
                             testDomainEventDispatchFunc,
                             driver);
5477 5478 5479 5480 5481 5482 5483 5484
    testDriverUnlock(driver);
}


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

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

5494 5495 5496
    if (STRNEQ(conn->driver->name, "Test"))
        return VIR_DRV_OPEN_DECLINED;

5497
    conn->secretPrivateData = conn->privateData;
5498 5499 5500 5501 5502 5503 5504
    return VIR_DRV_OPEN_SUCCESS;
}

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

5506 5507 5508

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

5513 5514 5515
    if (STRNEQ(conn->driver->name, "Test"))
        return VIR_DRV_OPEN_DECLINED;

5516
    conn->nwfilterPrivateData = conn->privateData;
5517 5518 5519 5520 5521 5522 5523 5524
    return VIR_DRV_OPEN_SUCCESS;
}

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

5525
static virDriver testDriver = {
5526 5527
    .no = VIR_DRV_TEST,
    .name = "Test",
5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567
    .open = testOpen, /* 0.1.1 */
    .close = testClose, /* 0.1.1 */
    .version = testGetVersion, /* 0.1.1 */
    .getHostname = virGetHostname, /* 0.6.3 */
    .getMaxVcpus = testGetMaxVCPUs, /* 0.3.2 */
    .nodeGetInfo = testNodeGetInfo, /* 0.1.1 */
    .getCapabilities = testGetCapabilities, /* 0.2.1 */
    .listDomains = testListDomains, /* 0.1.1 */
    .numOfDomains = testNumOfDomains, /* 0.1.1 */
    .domainCreateXML = testDomainCreateXML, /* 0.1.4 */
    .domainLookupByID = testLookupDomainByID, /* 0.1.1 */
    .domainLookupByUUID = testLookupDomainByUUID, /* 0.1.1 */
    .domainLookupByName = testLookupDomainByName, /* 0.1.1 */
    .domainSuspend = testPauseDomain, /* 0.1.1 */
    .domainResume = testResumeDomain, /* 0.1.1 */
    .domainShutdown = testShutdownDomain, /* 0.1.1 */
    .domainReboot = testRebootDomain, /* 0.1.1 */
    .domainDestroy = testDestroyDomain, /* 0.1.1 */
    .domainGetOSType = testGetOSType, /* 0.1.9 */
    .domainGetMaxMemory = testGetMaxMemory, /* 0.1.4 */
    .domainSetMaxMemory = testSetMaxMemory, /* 0.1.1 */
    .domainSetMemory = testSetMemory, /* 0.1.4 */
    .domainGetInfo = testGetDomainInfo, /* 0.1.1 */
    .domainGetState = testDomainGetState, /* 0.9.2 */
    .domainSave = testDomainSave, /* 0.3.2 */
    .domainRestore = testDomainRestore, /* 0.3.2 */
    .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 */
5568
    .domainUndefineFlags = testDomainUndefineFlags, /* 0.9.4 */
5569 5570 5571 5572
    .domainGetAutostart = testDomainGetAutostart, /* 0.3.2 */
    .domainSetAutostart = testDomainSetAutostart, /* 0.3.2 */
    .domainGetSchedulerType = testDomainGetSchedulerType, /* 0.3.2 */
    .domainGetSchedulerParameters = testDomainGetSchedulerParams, /* 0.3.2 */
5573
    .domainGetSchedulerParametersFlags = testDomainGetSchedulerParamsFlags, /* 0.9.2 */
5574
    .domainSetSchedulerParameters = testDomainSetSchedulerParams, /* 0.3.2 */
5575
    .domainSetSchedulerParametersFlags = testDomainSetSchedulerParamsFlags, /* 0.9.2 */
5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587
    .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 */
5588 5589 5590 5591
};

static virNetworkDriver testNetworkDriver = {
    "Test",
5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610
    .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 */
5611 5612
};

L
Laine Stump 已提交
5613 5614
static virInterfaceDriver testInterfaceDriver = {
    "Test",                     /* name */
5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628
    .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 */
5629 5630 5631
    .interfaceChangeBegin = testInterfaceChangeBegin,   /* 0.9.2 */
    .interfaceChangeCommit = testInterfaceChangeCommit,  /* 0.9.2 */
    .interfaceChangeRollback = testInterfaceChangeRollback, /* 0.9.2 */
L
Laine Stump 已提交
5632 5633 5634
};


5635 5636
static virStorageDriver testStorageDriver = {
    .name = "Test",
5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673
    .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 */
5674 5675
};

5676 5677
static virDeviceMonitor testDevMonitor = {
    .name = "Test",
5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689
    .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 */
5690 5691
};

5692 5693
static virSecretDriver testSecretDriver = {
    .name = "Test",
5694 5695
    .open = testSecretOpen, /* 0.7.1 */
    .close = testSecretClose, /* 0.7.1 */
5696
};
5697 5698


5699 5700
static virNWFilterDriver testNWFilterDriver = {
    .name = "Test",
5701 5702
    .open = testNWFilterOpen, /* 0.8.0 */
    .close = testNWFilterClose, /* 0.8.0 */
5703 5704
};

5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716
/**
 * testRegister:
 *
 * Registers the test driver
 */
int
testRegister(void)
{
    if (virRegisterDriver(&testDriver) < 0)
        return -1;
    if (virRegisterNetworkDriver(&testNetworkDriver) < 0)
        return -1;
L
Laine Stump 已提交
5717 5718
    if (virRegisterInterfaceDriver(&testInterfaceDriver) < 0)
        return -1;
5719 5720
    if (virRegisterStorageDriver(&testStorageDriver) < 0)
        return -1;
5721 5722
    if (virRegisterDeviceMonitor(&testDevMonitor) < 0)
        return -1;
5723 5724
    if (virRegisterSecretDriver(&testSecretDriver) < 0)
        return -1;
5725 5726
    if (virRegisterNWFilterDriver(&testNWFilterDriver) < 0)
        return -1;
5727

5728 5729
    return 0;
}