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

24
#include <config.h>
25

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

34 35

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

54 55
#define VIR_FROM_THIS VIR_FROM_TEST

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

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

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

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

#define MAX_CELLS 128
76

77
struct _testConn {
78
    virMutex lock;
79

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


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

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

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

117

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

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


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

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

138 139 140 141 142 143 144 145 146 147 148 149 150 151
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 已提交
152
    VIR_FREE(priv->vcpu_infos);
153 154 155 156 157
    VIR_FREE(priv->cpumaps);
    VIR_FREE(priv);
}


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

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

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

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

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

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

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

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

208 209 210 211 212 213 214 215
    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;

216
    return caps;
217

218
no_memory:
219
    virReportOOMError();
220 221
    virCapabilitiesFree(caps);
    return NULL;
222 223
}

224

225 226 227 228 229 230 231 232 233 234
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>";
235 236


237 238 239 240 241 242 243 244 245 246 247
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>";
248

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

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

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

309
static const unsigned long long defaultPoolCap = (100 * 1024 * 1024 * 1024ull);
C
Cole Robinson 已提交
310 311
static const unsigned long long defaultPoolAlloc = 0;

312
static int testStoragePoolObjSetDefaults(virStoragePoolObjPtr pool);
313
static int testNodeGetInfo(virConnectPtr conn, virNodeInfoPtr info);
314

315 316 317 318 319 320 321 322 323 324 325
static char *
testDomainGenerateIfname(virConnectPtr conn,
                         virDomainDefPtr domdef) {
    int maxif = 1024;
    int ifctr, i;

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

        if (virAsprintf(&ifname, "testnet%d", ifctr) < 0) {
326
            virReportOOMError();
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
            return NULL;
        }

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

        if (!found)
            return ifname;
    }

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

348 349 350
static int
testDomainGenerateIfnames(virConnectPtr conn,
                          virDomainDefPtr domdef)
351 352 353 354 355 356 357 358 359 360
{
    int i = 0;

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

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

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

366
    return 0;
367 368
}

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

    if (VIR_REALLOC_N(privdata->cpumaps, nvcpus * cpumaplen) < 0) {
438
        virReportOOMError();
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
        goto cleanup;
    }

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

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

    ret = 0;
cleanup:
    return ret;
}

/* Set up domain runtime state */
461 462 463 464 465
static int
testDomainStartState(virConnectPtr conn,
                     virDomainObjPtr dom)
{
    testConnPtr privconn = conn->privateData;
466
    int ret = -1;
467

468 469 470 471
    if (testDomainUpdateVCPUs(conn, dom, dom->def->vcpus, 1) < 0)
        goto cleanup;

    /* Set typical run state */
472 473 474
    dom->state = VIR_DOMAIN_RUNNING;
    dom->def->id = privconn->nextDomID++;

475 476 477
    ret = 0;
cleanup:
    return ret;
478
}
479

480 481 482 483
static void
testDomainShutdownState(virDomainPtr domain,
                        virDomainObjPtr privdom)
{
484 485 486 487 488 489
    if (privdom->newDef) {
        virDomainDefFree(privdom->def);
        privdom->def = privdom->newDef;
        privdom->newDef = NULL;
    }

490 491 492 493 494
    privdom->state = VIR_DOMAIN_SHUTOFF;
    privdom->def->id = -1;
    domain->id = -1;
}

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

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

521
    testDriverLock(privconn);
522
    conn->privateData = privconn;
523 524

    if (gettimeofday(&tv, NULL) < 0) {
525
        virReportSystemError(errno,
526
                             "%s", _("getting time of day"));
527
        goto error;
528 529
    }

530 531 532
    if (virDomainObjListInit(&privconn->domains) < 0)
        goto error;

533
    memmove(&privconn->nodeInfo, &defaultNodeInfo, sizeof(defaultNodeInfo));
534

535 536 537 538 539 540 541 542 543 544
    // Numa setup
    privconn->numCells = 2;
    for (u = 0; u < 2; ++u) {
        privconn->cells[u].numCpus = 8;
        privconn->cells[u].mem = (u + 1) * 2048 * 1024;
    }
    for (u = 0 ; u < 16 ; u++) {
        privconn->cells[u % 2].cpus[(u / 2)] = u;
    }

545 546 547 548 549
    if (!(privconn->caps = testBuildCapabilities(conn)))
        goto error;

    privconn->nextDomID = 1;

550
    if (!(domdef = virDomainDefParseString(privconn->caps,
551 552
                                           defaultDomainXML,
                                           VIR_DOMAIN_XML_INACTIVE)))
553
        goto error;
554
    if (testDomainGenerateIfnames(conn, domdef) < 0)
555
        goto error;
556
    if (!(domobj = virDomainAssignDef(privconn->caps,
557
                                      &privconn->domains, domdef, false)))
558 559
        goto error;
    domdef = NULL;
560 561 562 563 564 565

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

566
    domobj->persistent = 1;
567
    virDomainObjUnlock(domobj);
568

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

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

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

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

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

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

614
    testDriverUnlock(privconn);
615

616 617 618
    return VIR_DRV_OPEN_SUCCESS;

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


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

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

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

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

675
    ret = virXPathNodeSet(vol_xpath, ctxt, &vols);
676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693
    VIR_FREE(vol_xpath);
    if (ret < 0) {
        testError(NULL, VIR_ERR_XML_ERROR,
                  _("node vol list for pool '%s'"), pool->def->name);
        goto error;
    }

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

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

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

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

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

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

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

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

739
static int testOpenFromFile(virConnectPtr conn,
740
                            const char *file) {
741
    int fd = -1, 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 759 760 761 762 763 764
    if (virMutexInit(&privconn->lock) < 0) {
        testError(conn, VIR_ERR_INTERNAL_ERROR,
                  "%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 ((fd = open(file, O_RDONLY)) < 0) {
775
        virReportSystemError(errno,
776 777
                             _("loading host definition file '%s'"),
                             file);
778
        goto error;
779 780
    }

781 782 783
    if (!(xml = xmlReadFd(fd, file, NULL,
                          XML_PARSE_NOENT | XML_PARSE_NONET |
                          XML_PARSE_NOERROR | XML_PARSE_NOWARNING))) {
784 785
        testError(NULL, VIR_ERR_INTERNAL_ERROR,
                  _("Invalid XML in file '%s'"), file);
786
        goto error;
787
    }
788 789
    close(fd);
    fd = -1;
790

791 792
    root = xmlDocGetRootElement(xml);
    if ((root == NULL) || (!xmlStrEqual(root->name, BAD_CAST "node"))) {
793 794
        testError(NULL, VIR_ERR_XML_ERROR, "%s",
                  _("Root element is not 'node'"));
795
        goto error;
796 797
    }

798 799
    ctxt = xmlXPathNewContext(xml);
    if (ctxt == NULL) {
C
Cole Robinson 已提交
800 801
        testError(NULL, VIR_ERR_INTERNAL_ERROR, "%s",
                  _("creating xpath context"));
802
        goto error;
803
    }
804

805
    privconn->nextDomID = 1;
806
    privconn->numCells = 0;
C
Chris Lalancette 已提交
807 808 809 810 811
    if (virStrcpyStatic(privconn->path, file) == NULL) {
        testError(NULL, VIR_ERR_INTERNAL_ERROR,
                  _("Path %s too big for destination"), file);
        goto error;
    }
812 813 814
    memmove(&privconn->nodeInfo, &defaultNodeInfo, sizeof(defaultNodeInfo));

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

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

831
    ret = virXPathLong("string(/node/cpu/cores[1])", ctxt, &l);
832 833 834
    if (ret == 0) {
        nodeInfo->cores = l;
    } else if (ret == -2) {
J
Jim Meyering 已提交
835
        testError(NULL, VIR_ERR_XML_ERROR, "%s", _("node cpu cores"));
836
        goto error;
837 838
    }

839
    ret = virXPathLong("string(/node/cpu/threads[1])", ctxt, &l);
840 841 842
    if (ret == 0) {
        nodeInfo->threads = l;
    } else if (ret == -2) {
J
Jim Meyering 已提交
843
        testError(NULL, VIR_ERR_XML_ERROR, "%s", _("node cpu threads"));
844
        goto error;
845
    }
846

847
    nodeInfo->cpus = nodeInfo->cores * nodeInfo->threads * nodeInfo->sockets * nodeInfo->nodes;
848
    ret = virXPathLong("string(/node/cpu/active[1])", ctxt, &l);
849 850
    if (ret == 0) {
        if (l < nodeInfo->cpus) {
851 852
            nodeInfo->cpus = l;
        }
853
    } else if (ret == -2) {
J
Jim Meyering 已提交
854
        testError(NULL, VIR_ERR_XML_ERROR, "%s", _("node active cpu"));
855
        goto error;
856
    }
857
    ret = virXPathLong("string(/node/cpu/mhz[1])", ctxt, &l);
858 859 860
    if (ret == 0) {
        nodeInfo->mhz = l;
    } else if (ret == -2) {
J
Jim Meyering 已提交
861
        testError(NULL, VIR_ERR_XML_ERROR, "%s", _("node cpu mhz"));
862
        goto error;
863 864
    }

865
    str = virXPathString("string(/node/cpu/model[1])", ctxt);
866
    if (str != NULL) {
C
Chris Lalancette 已提交
867 868 869 870 871 872
        if (virStrcpyStatic(nodeInfo->model, str) == NULL) {
            testError(NULL, VIR_ERR_INTERNAL_ERROR,
                      _("Model %s too big for destination"), str);
            VIR_FREE(str);
            goto error;
        }
873
        VIR_FREE(str);
874 875
    }

876
    ret = virXPathLong("string(/node/memory[1])", ctxt, &l);
877 878 879
    if (ret == 0) {
        nodeInfo->memory = l;
    } else if (ret == -2) {
J
Jim Meyering 已提交
880
        testError(NULL, VIR_ERR_XML_ERROR, "%s", _("node memory"));
881
        goto error;
882
    }
883

884
    ret = virXPathNodeSet("/node/domain", ctxt, &domains);
885
    if (ret < 0) {
J
Jim Meyering 已提交
886
        testError(NULL, VIR_ERR_XML_ERROR, "%s", _("node domain list"));
887
        goto error;
888
    }
889

890
    for (i = 0 ; i < ret ; i++) {
891 892 893 894 895 896
        virDomainDefPtr def;
        char *relFile = virXMLPropString(domains[i], "file");
        if (relFile != NULL) {
            char *absFile = testBuildFilename(file, relFile);
            VIR_FREE(relFile);
            if (!absFile) {
J
Jim Meyering 已提交
897
                testError(NULL, VIR_ERR_INTERNAL_ERROR, "%s", _("resolving domain filename"));
898 899
                goto error;
            }
900
            def = virDomainDefParseFile(privconn->caps, absFile,
901
                                        VIR_DOMAIN_XML_INACTIVE);
902
            VIR_FREE(absFile);
903 904 905
            if (!def)
                goto error;
        } else {
906 907
            if ((def = virDomainDefParseNode(privconn->caps, xml, domains[i],
                                             VIR_DOMAIN_XML_INACTIVE)) == NULL)
908 909 910
                goto error;
        }

911
        if (testDomainGenerateIfnames(conn, def) < 0 ||
912
            !(dom = virDomainAssignDef(privconn->caps,
913
                                       &privconn->domains, def, false))) {
914
            virDomainDefFree(def);
915 916
            goto error;
        }
917

918 919 920 921 922
        if (testDomainStartState(conn, dom) < 0) {
            virDomainObjUnlock(dom);
            goto error;
        }

923
        dom->persistent = 1;
924
        virDomainObjUnlock(dom);
925
    }
926
    VIR_FREE(domains);
927

928
    ret = virXPathNodeSet("/node/network", ctxt, &networks);
929
    if (ret < 0) {
J
Jim Meyering 已提交
930
        testError(NULL, VIR_ERR_XML_ERROR, "%s", _("node network list"));
931 932 933 934 935 936 937 938
        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);
939
            if (!absFile) {
J
Jim Meyering 已提交
940
                testError(NULL, VIR_ERR_INTERNAL_ERROR, "%s", _("resolving network filename"));
941 942
                goto error;
            }
943

944
            def = virNetworkDefParseFile(absFile);
945
            VIR_FREE(absFile);
946 947 948
            if (!def)
                goto error;
        } else {
949
            if ((def = virNetworkDefParseNode(xml, networks[i])) == NULL)
950
                goto error;
951
        }
952
        if (!(net = virNetworkAssignDef(&privconn->networks,
953 954 955
                                        def))) {
            virNetworkDefFree(def);
            goto error;
956
        }
957
        net->persistent = 1;
958
        net->active = 1;
959
        virNetworkObjUnlock(net);
960
    }
961
    VIR_FREE(networks);
962

L
Laine Stump 已提交
963
    /* Parse interface definitions */
964
    ret = virXPathNodeSet("/node/interface", ctxt, &ifaces);
L
Laine Stump 已提交
965 966 967 968 969 970 971 972 973 974 975 976 977 978 979
    if (ret < 0) {
        testError(NULL, VIR_ERR_XML_ERROR, "%s", _("node interface list"));
        goto error;
    }
    for (i = 0 ; i < ret ; i++) {
        virInterfaceDefPtr def;
        char *relFile = virXMLPropString(ifaces[i], "file");
        if (relFile != NULL) {
            char *absFile = testBuildFilename(file, relFile);
            VIR_FREE(relFile);
            if (!absFile) {
                testError(NULL, VIR_ERR_INTERNAL_ERROR, "%s", _("resolving interface filename"));
                goto error;
            }

980
            def = virInterfaceDefParseFile(absFile);
L
Laine Stump 已提交
981 982 983 984
            VIR_FREE(absFile);
            if (!def)
                goto error;
        } else {
985
            if ((def = virInterfaceDefParseNode(xml, ifaces[i])) == NULL)
L
Laine Stump 已提交
986 987
                goto error;
        }
988

989
        if (!(iface = virInterfaceAssignDef(&privconn->ifaces, def))) {
L
Laine Stump 已提交
990 991 992
            virInterfaceDefFree(def);
            goto error;
        }
993 994

        iface->active = 1;
L
Laine Stump 已提交
995 996 997 998
        virInterfaceObjUnlock(iface);
    }
    VIR_FREE(ifaces);

C
Cole Robinson 已提交
999
    /* Parse Storage Pool list */
1000
    ret = virXPathNodeSet("/node/pool", ctxt, &pools);
C
Cole Robinson 已提交
1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017
    if (ret < 0) {
        testError(NULL, VIR_ERR_XML_ERROR, "%s", _("node pool list"));
        goto error;
    }
    for (i = 0 ; i < ret ; i++) {
        virStoragePoolDefPtr def;
        virStoragePoolObjPtr pool;
        char *relFile = virXMLPropString(pools[i], "file");
        if (relFile != NULL) {
            char *absFile = testBuildFilename(file, relFile);
            VIR_FREE(relFile);
            if (!absFile) {
                testError(NULL, VIR_ERR_INTERNAL_ERROR, "%s",
                          _("resolving pool filename"));
                goto error;
            }

1018
            def = virStoragePoolDefParseFile(absFile);
C
Cole Robinson 已提交
1019 1020 1021 1022
            VIR_FREE(absFile);
            if (!def)
                goto error;
        } else {
1023
            if ((def = virStoragePoolDefParseNode(xml,
1024
                                                  pools[i])) == NULL) {
C
Cole Robinson 已提交
1025 1026 1027 1028
                goto error;
            }
        }

1029
        if (!(pool = virStoragePoolObjAssignDef(&privconn->pools,
C
Cole Robinson 已提交
1030 1031 1032 1033 1034
                                                def))) {
            virStoragePoolDefFree(def);
            goto error;
        }

1035
        if (testStoragePoolObjSetDefaults(pool) == -1) {
1036
            virStoragePoolObjUnlock(pool);
C
Cole Robinson 已提交
1037
            goto error;
1038
        }
C
Cole Robinson 已提交
1039
        pool->active = 1;
1040 1041

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

1047
        virStoragePoolObjUnlock(pool);
C
Cole Robinson 已提交
1048
    }
1049
    VIR_FREE(pools);
C
Cole Robinson 已提交
1050

1051
    ret = virXPathNodeSet("/node/device", ctxt, &devs);
1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070
    if (ret < 0) {
        testError(NULL, VIR_ERR_XML_ERROR, "%s", _("node device list"));
        goto error;
    }
    for (i = 0 ; i < ret ; i++) {
        virNodeDeviceDefPtr def;
        virNodeDeviceObjPtr dev;
        char *relFile = virXMLPropString(devs[i], "file");

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

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

1071
            def = virNodeDeviceDefParseFile(absFile, 0);
1072 1073 1074 1075
            VIR_FREE(absFile);
            if (!def)
                goto error;
        } else {
1076
            if ((def = virNodeDeviceDefParseNode(xml, devs[i], 0)) == NULL)
1077 1078
                goto error;
        }
1079
        if (!(dev = virNodeDeviceAssignDef(&privconn->devs, def))) {
1080 1081 1082 1083 1084 1085 1086 1087
            virNodeDeviceDefFree(def);
            goto error;
        }
        virNodeDeviceObjUnlock(dev);
    }
    VIR_FREE(devs);


J
Jim Meyering 已提交
1088
    xmlXPathFreeContext(ctxt);
1089
    xmlFreeDoc(xml);
1090
    testDriverUnlock(privconn);
1091

1092
    return (0);
1093 1094

 error:
J
Jim Meyering 已提交
1095
    xmlXPathFreeContext(ctxt);
1096
    xmlFreeDoc(xml);
1097 1098
    VIR_FREE(domains);
    VIR_FREE(networks);
L
Laine Stump 已提交
1099
    VIR_FREE(ifaces);
C
Cole Robinson 已提交
1100
    VIR_FREE(pools);
1101 1102
    if (fd != -1)
        close(fd);
1103
    virDomainObjListDeinit(&privconn->domains);
1104
    virNetworkObjListFree(&privconn->networks);
L
Laine Stump 已提交
1105
    virInterfaceObjListFree(&privconn->ifaces);
C
Cole Robinson 已提交
1106
    virStoragePoolObjListFree(&privconn->pools);
1107
    testDriverUnlock(privconn);
1108
    VIR_FREE(privconn);
1109
    conn->privateData = NULL;
1110
    return VIR_DRV_OPEN_ERROR;
1111 1112
}

1113

1114
static virDrvOpenStatus testOpen(virConnectPtr conn,
1115
                    virConnectAuthPtr auth ATTRIBUTE_UNUSED,
1116
                    int flags ATTRIBUTE_UNUSED)
1117
{
1118
    int ret;
1119

1120
    if (!conn->uri)
1121
        return VIR_DRV_OPEN_DECLINED;
1122

1123
    if (!conn->uri->scheme || STRNEQ(conn->uri->scheme, "test"))
1124
        return VIR_DRV_OPEN_DECLINED;
1125

1126
    /* Remote driver should handle these. */
1127
    if (conn->uri->server)
1128 1129
        return VIR_DRV_OPEN_DECLINED;

1130
    /* From this point on, the connection is for us. */
1131 1132 1133
    if (!conn->uri->path
        || conn->uri->path[0] == '\0'
        || (conn->uri->path[0] == '/' && conn->uri->path[1] == '\0')) {
1134
        testError (NULL, VIR_ERR_INVALID_ARG,
J
Jim Meyering 已提交
1135
                   "%s", _("testOpen: supply a path or use test:///default"));
1136 1137
        return VIR_DRV_OPEN_ERROR;
    }
1138

1139
    if (STREQ(conn->uri->path, "/default"))
1140 1141
        ret = testOpenDefault(conn);
    else
1142
        ret = testOpenFromFile(conn,
1143
                               conn->uri->path);
1144

1145 1146
    if (ret == VIR_DRV_OPEN_SUCCESS) {
        testConnPtr privconn = conn->privateData;
1147
        testDriverLock(privconn);
1148 1149 1150
        /* Init callback list */
        if (VIR_ALLOC(privconn->domainEventCallbacks) < 0 ||
            !(privconn->domainEventQueue = virDomainEventQueueNew())) {
1151
            virReportOOMError();
1152
            testDriverUnlock(privconn);
1153 1154 1155 1156 1157 1158 1159 1160
            testClose(conn);
            return VIR_DRV_OPEN_ERROR;
        }

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

1164
    return (ret);
1165 1166
}

1167
static int testClose(virConnectPtr conn)
1168
{
1169
    testConnPtr privconn = conn->privateData;
1170
    testDriverLock(privconn);
1171
    virCapabilitiesFree(privconn->caps);
1172
    virDomainObjListDeinit(&privconn->domains);
D
Daniel P. Berrange 已提交
1173
    virNodeDeviceObjListFree(&privconn->devs);
1174
    virNetworkObjListFree(&privconn->networks);
L
Laine Stump 已提交
1175
    virInterfaceObjListFree(&privconn->ifaces);
C
Cole Robinson 已提交
1176
    virStoragePoolObjListFree(&privconn->pools);
1177 1178 1179 1180 1181 1182 1183

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

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

1184
    testDriverUnlock(privconn);
1185
    virMutexDestroy(&privconn->lock);
1186

1187
    VIR_FREE (privconn);
1188
    conn->privateData = NULL;
1189
    return 0;
1190 1191
}

1192 1193
static int testGetVersion(virConnectPtr conn ATTRIBUTE_UNUSED,
                          unsigned long *hvVer)
1194
{
1195 1196
    *hvVer = 2;
    return (0);
1197 1198
}

1199 1200 1201 1202 1203 1204 1205 1206 1207 1208
static int testIsSecure(virConnectPtr conn ATTRIBUTE_UNUSED)
{
    return 1;
}

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

1209 1210 1211 1212 1213 1214 1215 1216
static int testGetMaxVCPUs(virConnectPtr conn ATTRIBUTE_UNUSED,
                           const char *type ATTRIBUTE_UNUSED)
{
    return 32;
}

static int testNodeGetInfo(virConnectPtr conn,
                           virNodeInfoPtr info)
1217
{
1218
    testConnPtr privconn = conn->privateData;
1219
    testDriverLock(privconn);
1220
    memcpy(info, &privconn->nodeInfo, sizeof(virNodeInfo));
1221
    testDriverUnlock(privconn);
1222
    return (0);
1223 1224
}

1225
static char *testGetCapabilities (virConnectPtr conn)
1226
{
1227
    testConnPtr privconn = conn->privateData;
1228
    char *xml;
1229
    testDriverLock(privconn);
1230
    if ((xml = virCapabilitiesFormatXML(privconn->caps)) == NULL)
1231
        virReportOOMError();
1232
    testDriverUnlock(privconn);
1233
    return xml;
1234 1235
}

1236
static int testNumOfDomains(virConnectPtr conn)
1237
{
1238
    testConnPtr privconn = conn->privateData;
1239
    int count;
1240

1241
    testDriverLock(privconn);
1242
    count = virDomainObjListNumOfDomains(&privconn->domains, 1);
1243
    testDriverUnlock(privconn);
1244

1245
    return count;
1246 1247
}

1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289
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) {
        testError(dom->conn, VIR_ERR_NO_DOMAIN, NULL);
        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) {
        testError(dom->conn, VIR_ERR_NO_DOMAIN, NULL);
        goto cleanup;
    }
    ret = obj->persistent;

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

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

1300
    testDriverLock(privconn);
1301
    if ((def = virDomainDefParseString(privconn->caps, xml,
1302
                                       VIR_DOMAIN_XML_INACTIVE)) == NULL)
1303
        goto cleanup;
1304

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

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

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

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

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

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


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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1426
    return n;
1427 1428
}

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

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

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

1445
    testDomainShutdownState(domain, privdom);
1446 1447 1448
    event = virDomainEventNewFromObj(privdom,
                                     VIR_DOMAIN_EVENT_STOPPED,
                                     VIR_DOMAIN_EVENT_STOPPED_DESTROYED);
1449

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

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

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

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

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

1483
    if (privdom->state != VIR_DOMAIN_PAUSED) {
1484 1485 1486
        testError(domain->conn,
                  VIR_ERR_INTERNAL_ERROR, _("domain '%s' not paused"),
                  domain->name);
1487
        goto cleanup;
1488
    }
1489

1490
    privdom->state = VIR_DOMAIN_RUNNING;
1491 1492 1493
    event = virDomainEventNewFromObj(privdom,
                                     VIR_DOMAIN_EVENT_RESUMED,
                                     VIR_DOMAIN_EVENT_RESUMED_UNPAUSED);
1494 1495 1496
    ret = 0;

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

1507
static int testPauseDomain (virDomainPtr domain)
1508
{
1509 1510
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
1511
    virDomainEventPtr event = NULL;
1512
    int ret = -1;
1513

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

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

1524 1525
    if (privdom->state == VIR_DOMAIN_SHUTOFF ||
        privdom->state == VIR_DOMAIN_PAUSED) {
1526 1527 1528
        testError(domain->conn,
                  VIR_ERR_INTERNAL_ERROR, _("domain '%s' not running"),
                  domain->name);
1529
        goto cleanup;
1530
    }
1531

1532
    privdom->state = VIR_DOMAIN_PAUSED;
1533 1534 1535
    event = virDomainEventNewFromObj(privdom,
                                     VIR_DOMAIN_EVENT_SUSPENDED,
                                     VIR_DOMAIN_EVENT_SUSPENDED_PAUSED);
1536 1537 1538
    ret = 0;

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

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

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

1557
    testDriverLock(privconn);
1558 1559 1560 1561 1562
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);

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

1566
    if (privdom->state == VIR_DOMAIN_SHUTOFF) {
1567 1568
        testError(domain->conn, VIR_ERR_INTERNAL_ERROR,
                  _("domain '%s' not running"), domain->name);
1569
        goto cleanup;
1570
    }
1571

1572
    testDomainShutdownState(domain, privdom);
1573 1574 1575
    event = virDomainEventNewFromObj(privdom,
                                     VIR_DOMAIN_EVENT_STOPPED,
                                     VIR_DOMAIN_EVENT_STOPPED_SHUTDOWN);
1576

1577 1578 1579 1580 1581
    if (!privdom->persistent) {
        virDomainRemoveInactive(&privconn->domains,
                                privdom);
        privdom = NULL;
    }
1582

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

/* Similar behaviour as shutdown */
1594 1595
static int testRebootDomain (virDomainPtr domain,
                             unsigned int action ATTRIBUTE_UNUSED)
1596
{
1597 1598
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
1599
    virDomainEventPtr event = NULL;
1600
    int ret = -1;
1601

1602
    testDriverLock(privconn);
1603 1604 1605 1606 1607
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);

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

1611 1612 1613 1614
    privdom->state = VIR_DOMAIN_SHUTDOWN;
    switch (privdom->def->onReboot) {
    case VIR_DOMAIN_LIFECYCLE_DESTROY:
        privdom->state = VIR_DOMAIN_SHUTOFF;
1615 1616
        break;

1617 1618
    case VIR_DOMAIN_LIFECYCLE_RESTART:
        privdom->state = VIR_DOMAIN_RUNNING;
1619 1620
        break;

1621 1622
    case VIR_DOMAIN_LIFECYCLE_PRESERVE:
        privdom->state = VIR_DOMAIN_SHUTOFF;
1623 1624
        break;

1625 1626
    case VIR_DOMAIN_LIFECYCLE_RESTART_RENAME:
        privdom->state = VIR_DOMAIN_RUNNING;
1627
        break;
1628

1629
    default:
1630
        privdom->state = VIR_DOMAIN_SHUTOFF;
1631 1632
        break;
    }
1633

1634
    if (privdom->state == VIR_DOMAIN_SHUTOFF) {
1635
        testDomainShutdownState(domain, privdom);
1636 1637 1638
        event = virDomainEventNewFromObj(privdom,
                                         VIR_DOMAIN_EVENT_STOPPED,
                                         VIR_DOMAIN_EVENT_STOPPED_SHUTDOWN);
1639

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

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

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

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

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

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

1681 1682 1683 1684 1685
    info->state = privdom->state;
    info->memory = privdom->def->memory;
    info->maxMem = privdom->def->maxmem;
    info->nrVirtCpu = privdom->def->vcpus;
    info->cpuTime = ((tv.tv_sec * 1000ll * 1000ll  * 1000ll) + (tv.tv_usec * 1000ll));
1686 1687 1688
    ret = 0;

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

1694 1695 1696 1697 1698
#define TEST_SAVE_MAGIC "TestGuestMagic"

static int testDomainSave(virDomainPtr domain,
                          const char *path)
{
1699
    testConnPtr privconn = domain->conn->privateData;
1700 1701 1702
    char *xml = NULL;
    int fd = -1;
    int len;
1703
    virDomainObjPtr privdom;
1704
    virDomainEventPtr event = NULL;
1705
    int ret = -1;
1706

1707
    testDriverLock(privconn);
1708 1709 1710 1711 1712
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);

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

1716
    xml = virDomainDefFormat(privdom->def,
C
Cole Robinson 已提交
1717 1718
                             VIR_DOMAIN_XML_SECURE);

1719
    if (xml == NULL) {
1720
        virReportSystemError(errno,
1721 1722
                             _("saving domain '%s' failed to allocate space for metadata"),
                             domain->name);
1723
        goto cleanup;
1724
    }
1725 1726

    if ((fd = open(path, O_CREAT|O_TRUNC|O_WRONLY, S_IRUSR|S_IWUSR)) < 0) {
1727
        virReportSystemError(errno,
1728 1729
                             _("saving domain '%s' to '%s': open failed"),
                             domain->name, path);
1730
        goto cleanup;
1731
    }
1732
    len = strlen(xml);
1733
    if (safewrite(fd, TEST_SAVE_MAGIC, sizeof(TEST_SAVE_MAGIC)) < 0) {
1734
        virReportSystemError(errno,
1735 1736
                             _("saving domain '%s' to '%s': write failed"),
                             domain->name, path);
1737
        goto cleanup;
1738
    }
1739
    if (safewrite(fd, (char*)&len, sizeof(len)) < 0) {
1740
        virReportSystemError(errno,
1741 1742
                             _("saving domain '%s' to '%s': write failed"),
                             domain->name, path);
1743
        goto cleanup;
1744
    }
1745
    if (safewrite(fd, xml, len) < 0) {
1746
        virReportSystemError(errno,
1747 1748
                             _("saving domain '%s' to '%s': write failed"),
                             domain->name, path);
1749
        goto cleanup;
1750
    }
1751

1752
    if (close(fd) < 0) {
1753
        virReportSystemError(errno,
1754 1755
                             _("saving domain '%s' to '%s': write failed"),
                             domain->name, path);
1756
        goto cleanup;
1757
    }
1758 1759
    fd = -1;

1760
    testDomainShutdownState(domain, privdom);
1761 1762 1763
    event = virDomainEventNewFromObj(privdom,
                                     VIR_DOMAIN_EVENT_STOPPED,
                                     VIR_DOMAIN_EVENT_STOPPED_SAVED);
1764

1765 1766 1767
    if (!privdom->persistent) {
        virDomainRemoveInactive(&privconn->domains,
                                privdom);
1768
        privdom = NULL;
1769
    }
1770

1771
    ret = 0;
1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782
cleanup:
    VIR_FREE(xml);

    /* Don't report failure in close or unlink, because
     * in either case we're already in a failure scenario
     * and have reported a earlier error */
    if (ret != 0) {
        if (fd != -1)
            close(fd);
        unlink(path);
    }
1783 1784
    if (privdom)
        virDomainObjUnlock(privdom);
1785 1786
    if (event)
        testDomainEventQueue(privconn, event);
1787
    testDriverUnlock(privconn);
1788
    return ret;
1789 1790
}

1791 1792
static int testDomainRestore(virConnectPtr conn,
                             const char *path)
1793
{
1794
    testConnPtr privconn = conn->privateData;
1795
    char *xml = NULL;
1796
    char magic[15];
1797 1798 1799
    int fd = -1;
    int len;
    virDomainDefPtr def = NULL;
1800
    virDomainObjPtr dom = NULL;
1801
    virDomainEventPtr event = NULL;
1802
    int ret = -1;
1803 1804

    if ((fd = open(path, O_RDONLY)) < 0) {
1805
        virReportSystemError(errno,
1806 1807
                             _("cannot read domain image '%s'"),
                             path);
1808
        goto cleanup;
1809
    }
1810
    if (saferead(fd, magic, sizeof(magic)) != sizeof(magic)) {
1811
        virReportSystemError(errno,
1812 1813
                             _("incomplete save header in '%s'"),
                             path);
1814
        goto cleanup;
1815
    }
1816
    if (memcmp(magic, TEST_SAVE_MAGIC, sizeof(magic))) {
1817
        testError(conn, VIR_ERR_INTERNAL_ERROR,
J
Jim Meyering 已提交
1818
                  "%s", _("mismatched header magic"));
1819
        goto cleanup;
1820
    }
1821
    if (saferead(fd, (char*)&len, sizeof(len)) != sizeof(len)) {
1822
        virReportSystemError(errno,
1823 1824
                             _("failed to read metadata length in '%s'"),
                             path);
1825
        goto cleanup;
1826 1827
    }
    if (len < 1 || len > 8192) {
1828
        testError(conn, VIR_ERR_INTERNAL_ERROR,
J
Jim Meyering 已提交
1829
                  "%s", _("length of metadata out of range"));
1830
        goto cleanup;
1831
    }
1832
    if (VIR_ALLOC_N(xml, len+1) < 0) {
1833
        virReportOOMError();
1834
        goto cleanup;
1835
    }
1836
    if (saferead(fd, xml, len) != len) {
1837
        virReportSystemError(errno,
1838
                             _("incomplete metdata in '%s'"), path);
1839
        goto cleanup;
1840 1841
    }
    xml[len] = '\0';
1842

1843
    testDriverLock(privconn);
1844
    def = virDomainDefParseString(privconn->caps, xml,
1845
                                  VIR_DOMAIN_XML_INACTIVE);
1846
    if (!def)
1847
        goto cleanup;
1848

1849 1850 1851
    if (virDomainObjIsDuplicate(&privconn->domains, def, 1) < 0)
        goto cleanup;

1852
    if (testDomainGenerateIfnames(conn, def) < 0)
1853
        goto cleanup;
1854
    if (!(dom = virDomainAssignDef(privconn->caps,
1855
                                   &privconn->domains, def, true)))
1856 1857
        goto cleanup;
    def = NULL;
1858

1859 1860 1861
    if (testDomainStartState(conn, dom) < 0)
        goto cleanup;

1862 1863 1864
    event = virDomainEventNewFromObj(dom,
                                     VIR_DOMAIN_EVENT_STARTED,
                                     VIR_DOMAIN_EVENT_STARTED_RESTORED);
1865
    ret = 0;
1866 1867 1868 1869 1870 1871

cleanup:
    virDomainDefFree(def);
    VIR_FREE(xml);
    if (fd != -1)
        close(fd);
1872 1873
    if (dom)
        virDomainObjUnlock(dom);
1874 1875
    if (event)
        testDomainEventQueue(privconn, event);
1876
    testDriverUnlock(privconn);
1877
    return ret;
1878 1879
}

1880 1881 1882
static int testDomainCoreDump(virDomainPtr domain,
                              const char *to,
                              int flags ATTRIBUTE_UNUSED)
1883
{
1884
    testConnPtr privconn = domain->conn->privateData;
1885
    int fd = -1;
1886
    virDomainObjPtr privdom;
1887
    virDomainEventPtr event = NULL;
1888
    int ret = -1;
1889

1890
    testDriverLock(privconn);
1891 1892 1893 1894 1895
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);

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

    if ((fd = open(to, O_CREAT|O_TRUNC|O_WRONLY, S_IRUSR|S_IWUSR)) < 0) {
1900
        virReportSystemError(errno,
1901 1902
                             _("domain '%s' coredump: failed to open %s"),
                             domain->name, to);
1903
        goto cleanup;
1904
    }
1905
    if (safewrite(fd, TEST_SAVE_MAGIC, sizeof(TEST_SAVE_MAGIC)) < 0) {
1906
        virReportSystemError(errno,
1907 1908
                             _("domain '%s' coredump: failed to write header to %s"),
                             domain->name, to);
1909
        goto cleanup;
1910
    }
1911
    if (close(fd) < 0) {
1912
        virReportSystemError(errno,
1913 1914
                             _("domain '%s' coredump: write failed: %s"),
                             domain->name, to);
1915
        goto cleanup;
1916
    }
1917

1918 1919 1920 1921 1922 1923 1924 1925 1926 1927
    if (flags & VIR_DUMP_CRASH) {
        testDomainShutdownState(domain, privdom);
        event = virDomainEventNewFromObj(privdom,
                                         VIR_DOMAIN_EVENT_STOPPED,
                                         VIR_DOMAIN_EVENT_STOPPED_CRASHED);
        if (!privdom->persistent) {
            virDomainRemoveInactive(&privconn->domains,
                                    privdom);
            privdom = NULL;
        }
1928
    }
1929

1930
    ret = 0;
1931 1932 1933
cleanup:
    if (fd != -1)
        close(fd);
1934 1935
    if (privdom)
        virDomainObjUnlock(privdom);
1936 1937
    if (event)
        testDomainEventQueue(privconn, event);
1938
    testDriverUnlock(privconn);
1939
    return ret;
1940 1941
}

1942
static char *testGetOSType(virDomainPtr dom ATTRIBUTE_UNUSED) {
1943 1944
    char *ret = strdup("linux");
    if (!ret)
1945
        virReportOOMError();
1946
    return ret;
1947 1948 1949
}

static unsigned long testGetMaxMemory(virDomainPtr domain) {
1950 1951
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
1952
    unsigned long ret = 0;
1953

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

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

1964 1965 1966
    ret = privdom->def->maxmem;

cleanup:
1967 1968
    if (privdom)
        virDomainObjUnlock(privdom);
1969
    return ret;
1970 1971 1972 1973 1974
}

static int testSetMaxMemory(virDomainPtr domain,
                            unsigned long memory)
{
1975 1976
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
1977
    int ret = -1;
1978

1979
    testDriverLock(privconn);
1980 1981
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
1982
    testDriverUnlock(privconn);
1983 1984 1985

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

    /* XXX validate not over host memory wrt to other domains */
1990
    privdom->def->maxmem = memory;
1991 1992 1993
    ret = 0;

cleanup:
1994 1995
    if (privdom)
        virDomainObjUnlock(privdom);
1996
    return ret;
1997 1998
}

1999 2000 2001
static int testSetMemory(virDomainPtr domain,
                         unsigned long memory)
{
2002 2003
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
2004
    int ret = -1;
2005

2006
    testDriverLock(privconn);
2007 2008
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
2009
    testDriverUnlock(privconn);
2010 2011 2012

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

2016
    if (memory > privdom->def->maxmem) {
2017
        testError(domain->conn,
2018
                  VIR_ERR_INVALID_ARG, __FUNCTION__);
2019
        goto cleanup;
2020
    }
2021

2022
    privdom->def->memory = memory;
2023 2024 2025
    ret = 0;

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

C
Cole Robinson 已提交
2031 2032 2033 2034 2035
static int testDomainGetMaxVcpus(virDomainPtr domain)
{
    return testGetMaxVCPUs(domain->conn, "test");
}

2036 2037
static int testSetVcpus(virDomainPtr domain,
                        unsigned int nrCpus) {
2038
    testConnPtr privconn = domain->conn->privateData;
2039
    virDomainObjPtr privdom = NULL;
C
Cole Robinson 已提交
2040 2041 2042 2043 2044 2045
    int ret = -1, maxvcpus;

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

2047 2048 2049 2050 2051 2052
    testDriverLock(privconn);
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
    testDriverUnlock(privconn);

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

C
Cole Robinson 已提交
2057 2058 2059 2060 2061 2062
    if (!virDomainObjIsActive(privdom)) {
        testError(domain->conn, VIR_ERR_OPERATION_INVALID,
                  "%s", _("cannot hotplug vcpus for an inactive domain"));
        goto cleanup;
    }

2063
    /* We allow more cpus in guest than host */
C
Cole Robinson 已提交
2064 2065 2066 2067
    if (nrCpus > maxvcpus) {
        testError(domain->conn, VIR_ERR_INVALID_ARG,
                  "requested cpu amount exceeds maximum (%d > %d)",
                  nrCpus, maxvcpus);
2068
        goto cleanup;
2069
    }
2070

2071 2072 2073 2074
    /* Update VCPU state for the running domain */
    if (testDomainUpdateVCPUs(domain->conn, privdom, nrCpus, 0) < 0)
        goto cleanup;

2075
    privdom->def->vcpus = nrCpus;
2076 2077 2078
    ret = 0;

cleanup:
2079 2080
    if (privdom)
        virDomainObjUnlock(privdom);
2081
    return ret;
2082 2083
}

C
Cole Robinson 已提交
2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115
static int testDomainGetVcpus(virDomainPtr domain,
                              virVcpuInfoPtr info,
                              int maxinfo,
                              unsigned char *cpumaps,
                              int maplen)
{
    testConnPtr privconn = domain->conn->privateData;
    testDomainObjPrivatePtr privdomdata;
    virDomainObjPtr privdom;
    int i, v, maxcpu, hostcpus;
    int ret = -1;
    struct timeval tv;
    unsigned long long statbase;

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

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

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

    privdomdata = privdom->privateData;

    if (gettimeofday(&tv, NULL) < 0) {
2116
        virReportSystemError(errno,
C
Cole Robinson 已提交
2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171
                             "%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 已提交
2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228
static int testDomainPinVcpu(virDomainPtr domain,
                             unsigned int vcpu,
                             unsigned char *cpumap,
                             int maplen)
{
    testConnPtr privconn = domain->conn->privateData;
    testDomainObjPrivatePtr privdomdata;
    virDomainObjPtr privdom;
    unsigned char *privcpumap;
    int i, maxcpu, hostcpus, privmaplen;
    int ret = -1;

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

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

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

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

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

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

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

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

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

2229
static char *testDomainDumpXML(virDomainPtr domain, int flags)
2230
{
2231
    testConnPtr privconn = domain->conn->privateData;
2232
    virDomainDefPtr def;
2233
    virDomainObjPtr privdom;
2234 2235
    char *ret = NULL;

2236 2237 2238 2239 2240 2241
    testDriverLock(privconn);
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
    testDriverUnlock(privconn);

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

2246 2247
    def = (flags & VIR_DOMAIN_XML_INACTIVE) &&
        privdom->newDef ? privdom->newDef : privdom->def;
2248

2249
    ret = virDomainDefFormat(def,
2250 2251 2252
                             flags);

cleanup:
2253 2254
    if (privdom)
        virDomainObjUnlock(privdom);
2255
    return ret;
2256
}
2257

2258
static int testNumOfDefinedDomains(virConnectPtr conn) {
2259
    testConnPtr privconn = conn->privateData;
2260
    int count;
2261

2262
    testDriverLock(privconn);
2263
    count = virDomainObjListNumOfDomains(&privconn->domains, 0);
2264
    testDriverUnlock(privconn);
2265

2266
    return count;
2267 2268
}

2269 2270 2271
static int testListDefinedDomains(virConnectPtr conn,
                                  char **const names,
                                  int maxnames) {
2272

2273
    testConnPtr privconn = conn->privateData;
2274
    int n;
2275

2276
    testDriverLock(privconn);
2277
    memset(names, 0, sizeof(*names)*maxnames);
2278
    n = virDomainObjListGetInactiveNames(&privconn->domains, names, maxnames);
2279
    testDriverUnlock(privconn);
2280

2281
    return n;
2282 2283
}

2284
static virDomainPtr testDomainDefineXML(virConnectPtr conn,
2285
                                        const char *xml) {
2286
    testConnPtr privconn = conn->privateData;
2287
    virDomainPtr ret = NULL;
2288
    virDomainDefPtr def;
2289
    virDomainObjPtr dom = NULL;
2290
    virDomainEventPtr event = NULL;
2291
    int dupVM;
2292

2293
    testDriverLock(privconn);
2294
    if ((def = virDomainDefParseString(privconn->caps, xml,
2295
                                       VIR_DOMAIN_XML_INACTIVE)) == NULL)
2296
        goto cleanup;
2297

2298 2299 2300
    if ((dupVM = virDomainObjIsDuplicate(&privconn->domains, def, 0)) < 0)
        goto cleanup;

2301 2302
    if (testDomainGenerateIfnames(conn, def) < 0)
        goto cleanup;
2303
    if (!(dom = virDomainAssignDef(privconn->caps,
2304
                                   &privconn->domains, def, false)))
2305
        goto cleanup;
2306
    def = NULL;
2307
    dom->persistent = 1;
2308

2309 2310
    event = virDomainEventNewFromObj(dom,
                                     VIR_DOMAIN_EVENT_DEFINED,
2311 2312 2313
                                     !dupVM ?
                                     VIR_DOMAIN_EVENT_DEFINED_ADDED :
                                     VIR_DOMAIN_EVENT_DEFINED_UPDATED);
2314

2315
    ret = virGetDomain(conn, dom->def->name, dom->def->uuid);
2316
    if (ret)
2317
        ret->id = dom->def->id;
2318 2319 2320

cleanup:
    virDomainDefFree(def);
2321 2322
    if (dom)
        virDomainObjUnlock(dom);
2323 2324
    if (event)
        testDomainEventQueue(privconn, event);
2325
    testDriverUnlock(privconn);
2326
    return ret;
2327 2328
}

2329 2330 2331
static int testNodeGetCellsFreeMemory(virConnectPtr conn,
                                      unsigned long long *freemems,
                                      int startCell, int maxCells) {
2332
    testConnPtr privconn = conn->privateData;
2333
    int i, j;
2334
    int ret = -1;
2335

2336
    testDriverLock(privconn);
2337
    if (startCell > privconn->numCells) {
2338
        testError(conn, VIR_ERR_INVALID_ARG,
J
Jim Meyering 已提交
2339
                  "%s", _("Range exceeds available cells"));
2340
        goto cleanup;
2341 2342 2343 2344 2345 2346 2347
    }

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

2350
cleanup:
2351
    testDriverUnlock(privconn);
2352
    return ret;
2353 2354 2355
}


2356
static int testDomainCreate(virDomainPtr domain) {
2357 2358
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
2359
    virDomainEventPtr event = NULL;
2360
    int ret = -1;
2361

2362
    testDriverLock(privconn);
2363 2364 2365 2366 2367
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);

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

2371
    if (privdom->state != VIR_DOMAIN_SHUTOFF) {
2372 2373
        testError(domain->conn, VIR_ERR_INTERNAL_ERROR,
                  _("Domain '%s' is already running"), domain->name);
2374
        goto cleanup;
2375 2376
    }

2377 2378 2379 2380
    if (testDomainStartState(domain->conn, privdom) < 0)
        goto cleanup;
    domain->id = privdom->def->id;

2381 2382 2383
    event = virDomainEventNewFromObj(privdom,
                                     VIR_DOMAIN_EVENT_STARTED,
                                     VIR_DOMAIN_EVENT_STARTED_BOOTED);
2384
    ret = 0;
2385

2386
cleanup:
2387 2388
    if (privdom)
        virDomainObjUnlock(privdom);
2389 2390
    if (event)
        testDomainEventQueue(privconn, event);
2391
    testDriverUnlock(privconn);
2392
    return ret;
2393 2394 2395
}

static int testDomainUndefine(virDomainPtr domain) {
2396 2397
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
2398
    virDomainEventPtr event = NULL;
2399
    int ret = -1;
2400

2401
    testDriverLock(privconn);
2402 2403 2404 2405 2406
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);

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

2410
    if (privdom->state != VIR_DOMAIN_SHUTOFF) {
2411 2412
        testError(domain->conn, VIR_ERR_INTERNAL_ERROR,
                  _("Domain '%s' is still running"), domain->name);
2413
        goto cleanup;
2414 2415
    }

2416
    privdom->state = VIR_DOMAIN_SHUTOFF;
2417 2418 2419
    event = virDomainEventNewFromObj(privdom,
                                     VIR_DOMAIN_EVENT_UNDEFINED,
                                     VIR_DOMAIN_EVENT_UNDEFINED_REMOVED);
2420 2421
    virDomainRemoveInactive(&privconn->domains,
                            privdom);
2422
    privdom = NULL;
2423
    ret = 0;
2424

2425
cleanup:
2426 2427
    if (privdom)
        virDomainObjUnlock(privdom);
2428 2429
    if (event)
        testDomainEventQueue(privconn, event);
2430
    testDriverUnlock(privconn);
2431
    return ret;
2432 2433
}

2434 2435 2436
static int testDomainGetAutostart(virDomainPtr domain,
                                  int *autostart)
{
2437 2438
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
2439
    int ret = -1;
2440

2441
    testDriverLock(privconn);
2442 2443
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
2444
    testDriverUnlock(privconn);
2445 2446 2447

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

2451
    *autostart = privdom->autostart;
2452 2453 2454
    ret = 0;

cleanup:
2455 2456
    if (privdom)
        virDomainObjUnlock(privdom);
2457
    return ret;
2458 2459 2460 2461 2462 2463
}


static int testDomainSetAutostart(virDomainPtr domain,
                                  int autostart)
{
2464 2465
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
2466
    int ret = -1;
2467

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

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

2478
    privdom->autostart = autostart ? 1 : 0;
2479 2480 2481
    ret = 0;

cleanup:
2482 2483
    if (privdom)
        virDomainObjUnlock(privdom);
2484
    return ret;
2485
}
2486

2487
static char *testDomainGetSchedulerType(virDomainPtr domain ATTRIBUTE_UNUSED,
2488 2489
                                        int *nparams)
{
2490 2491
    char *type = NULL;

2492 2493
    *nparams = 1;
    type = strdup("fair");
2494
    if (!type)
2495
        virReportOOMError();
2496

2497 2498 2499 2500 2501 2502 2503
    return type;
}

static int testDomainGetSchedulerParams(virDomainPtr domain,
                                        virSchedParameterPtr params,
                                        int *nparams)
{
2504 2505
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
2506
    int ret = -1;
2507

2508
    testDriverLock(privconn);
2509 2510
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
2511
    testDriverUnlock(privconn);
2512 2513 2514

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

2518
    if (*nparams != 1) {
2519
        testError(domain->conn, VIR_ERR_INVALID_ARG, "nparams");
2520
        goto cleanup;
2521
    }
2522 2523
    strcpy(params[0].field, "weight");
    params[0].type = VIR_DOMAIN_SCHED_FIELD_UINT;
2524 2525 2526
    /* XXX */
    /*params[0].value.ui = privdom->weight;*/
    params[0].value.ui = 50;
2527 2528 2529
    ret = 0;

cleanup:
2530 2531
    if (privdom)
        virDomainObjUnlock(privdom);
2532
    return ret;
2533
}
2534 2535


2536 2537 2538 2539
static int testDomainSetSchedulerParams(virDomainPtr domain,
                                        virSchedParameterPtr params,
                                        int nparams)
{
2540 2541
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
2542
    int ret = -1;
2543

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

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

2554
    if (nparams != 1) {
2555
        testError(domain->conn, VIR_ERR_INVALID_ARG, "nparams");
2556
        goto cleanup;
2557
    }
2558
    if (STRNEQ(params[0].field, "weight")) {
2559
        testError(domain->conn, VIR_ERR_INVALID_ARG, "field");
2560
        goto cleanup;
2561 2562
    }
    if (params[0].type != VIR_DOMAIN_SCHED_FIELD_UINT) {
2563
        testError(domain->conn, VIR_ERR_INVALID_ARG, "type");
2564
        goto cleanup;
2565
    }
2566 2567
    /* XXX */
    /*privdom->weight = params[0].value.ui;*/
2568 2569 2570
    ret = 0;

cleanup:
2571 2572
    if (privdom)
        virDomainObjUnlock(privdom);
2573
    return ret;
2574 2575
}

2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609
static int testDomainBlockStats(virDomainPtr domain,
                                const char *path,
                                struct _virDomainBlockStats *stats)
{
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
    struct timeval tv;
    unsigned long long statbase;
    int i, found = 0, ret = -1;

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

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

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

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

    if (gettimeofday(&tv, NULL) < 0) {
2610
        virReportSystemError(errno,
2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664
                             "%s", _("getting time of day"));
        goto error;
    }

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

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

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

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

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

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

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

    if (gettimeofday(&tv, NULL) < 0) {
2665
        virReportSystemError(errno,
2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687
                             "%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;
}

2688
static virDrvOpenStatus testOpenNetwork(virConnectPtr conn,
2689
                                        virConnectAuthPtr auth ATTRIBUTE_UNUSED,
2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706
                                        int flags ATTRIBUTE_UNUSED) {
    if (STRNEQ(conn->driver->name, "Test"))
        return VIR_DRV_OPEN_DECLINED;

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

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


static virNetworkPtr testLookupNetworkByUUID(virConnectPtr conn,
                                           const unsigned char *uuid)
{
2707 2708
    testConnPtr privconn = conn->privateData;
    virNetworkObjPtr net;
2709
    virNetworkPtr ret = NULL;
2710

2711 2712 2713 2714 2715
    testDriverLock(privconn);
    net = virNetworkFindByUUID(&privconn->networks, uuid);
    testDriverUnlock(privconn);

    if (net == NULL) {
2716
        testError (conn, VIR_ERR_NO_NETWORK, NULL);
2717
        goto cleanup;
2718 2719
    }

2720 2721 2722
    ret = virGetNetwork(conn, net->def->name, net->def->uuid);

cleanup:
2723 2724
    if (net)
        virNetworkObjUnlock(net);
2725
    return ret;
2726
}
2727

2728
static virNetworkPtr testLookupNetworkByName(virConnectPtr conn,
2729
                                             const char *name)
2730
{
2731
    testConnPtr privconn = conn->privateData;
2732 2733
    virNetworkObjPtr net;
    virNetworkPtr ret = NULL;
2734

2735 2736 2737 2738 2739
    testDriverLock(privconn);
    net = virNetworkFindByName(&privconn->networks, name);
    testDriverUnlock(privconn);

    if (net == NULL) {
2740
        testError (conn, VIR_ERR_NO_NETWORK, NULL);
2741
        goto cleanup;
2742 2743
    }

2744 2745 2746
    ret = virGetNetwork(conn, net->def->name, net->def->uuid);

cleanup:
2747 2748
    if (net)
        virNetworkObjUnlock(net);
2749
    return ret;
2750 2751 2752 2753
}


static int testNumNetworks(virConnectPtr conn) {
2754
    testConnPtr privconn = conn->privateData;
2755
    int numActive = 0, i;
2756

2757 2758 2759
    testDriverLock(privconn);
    for (i = 0 ; i < privconn->networks.count ; i++) {
        virNetworkObjLock(privconn->networks.objs[i]);
D
Daniel P. Berrange 已提交
2760
        if (virNetworkObjIsActive(privconn->networks.objs[i]))
2761
            numActive++;
2762 2763 2764
        virNetworkObjUnlock(privconn->networks.objs[i]);
    }
    testDriverUnlock(privconn);
2765

2766
    return numActive;
2767 2768 2769
}

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

2773
    testDriverLock(privconn);
2774
    memset(names, 0, sizeof(*names)*nnames);
2775 2776
    for (i = 0 ; i < privconn->networks.count && n < nnames ; i++) {
        virNetworkObjLock(privconn->networks.objs[i]);
D
Daniel P. Berrange 已提交
2777
        if (virNetworkObjIsActive(privconn->networks.objs[i]) &&
2778 2779
            !(names[n++] = strdup(privconn->networks.objs[i]->def->name))) {
            virNetworkObjUnlock(privconn->networks.objs[i]);
2780
            goto no_memory;
2781 2782 2783 2784
        }
        virNetworkObjUnlock(privconn->networks.objs[i]);
    }
    testDriverUnlock(privconn);
2785

2786 2787 2788
    return n;

no_memory:
2789
    virReportOOMError();
2790 2791
    for (n = 0 ; n < nnames ; n++)
        VIR_FREE(names[n]);
2792
    testDriverUnlock(privconn);
2793
    return -1;
2794 2795 2796
}

static int testNumDefinedNetworks(virConnectPtr conn) {
2797
    testConnPtr privconn = conn->privateData;
2798
    int numInactive = 0, i;
2799

2800 2801 2802
    testDriverLock(privconn);
    for (i = 0 ; i < privconn->networks.count ; i++) {
        virNetworkObjLock(privconn->networks.objs[i]);
D
Daniel P. Berrange 已提交
2803
        if (!virNetworkObjIsActive(privconn->networks.objs[i]))
2804
            numInactive++;
2805 2806 2807
        virNetworkObjUnlock(privconn->networks.objs[i]);
    }
    testDriverUnlock(privconn);
2808

2809
    return numInactive;
2810 2811 2812
}

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

2816
    testDriverLock(privconn);
2817
    memset(names, 0, sizeof(*names)*nnames);
2818 2819
    for (i = 0 ; i < privconn->networks.count && n < nnames ; i++) {
        virNetworkObjLock(privconn->networks.objs[i]);
D
Daniel P. Berrange 已提交
2820
        if (!virNetworkObjIsActive(privconn->networks.objs[i]) &&
2821 2822
            !(names[n++] = strdup(privconn->networks.objs[i]->def->name))) {
            virNetworkObjUnlock(privconn->networks.objs[i]);
2823
            goto no_memory;
2824 2825 2826 2827
        }
        virNetworkObjUnlock(privconn->networks.objs[i]);
    }
    testDriverUnlock(privconn);
2828

2829 2830 2831
    return n;

no_memory:
2832
    virReportOOMError();
2833 2834
    for (n = 0 ; n < nnames ; n++)
        VIR_FREE(names[n]);
2835
    testDriverUnlock(privconn);
2836
    return -1;
2837 2838
}

2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882

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) {
        testError(net->conn, VIR_ERR_NO_NETWORK, NULL);
        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) {
        testError(net->conn, VIR_ERR_NO_NETWORK, NULL);
        goto cleanup;
    }
    ret = obj->persistent;

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


2883
static virNetworkPtr testNetworkCreate(virConnectPtr conn, const char *xml) {
2884
    testConnPtr privconn = conn->privateData;
2885
    virNetworkDefPtr def;
2886
    virNetworkObjPtr net = NULL;
2887
    virNetworkPtr ret = NULL;
2888

2889
    testDriverLock(privconn);
2890
    if ((def = virNetworkDefParseString(xml)) == NULL)
2891
        goto cleanup;
2892

2893
    if ((net = virNetworkAssignDef(&privconn->networks, def)) == NULL)
2894 2895
        goto cleanup;
    def = NULL;
2896
    net->active = 1;
2897

2898
    ret = virGetNetwork(conn, net->def->name, net->def->uuid);
2899

2900 2901
cleanup:
    virNetworkDefFree(def);
2902 2903 2904
    if (net)
        virNetworkObjUnlock(net);
    testDriverUnlock(privconn);
2905
    return ret;
2906 2907 2908
}

static virNetworkPtr testNetworkDefine(virConnectPtr conn, const char *xml) {
2909
    testConnPtr privconn = conn->privateData;
2910
    virNetworkDefPtr def;
2911
    virNetworkObjPtr net = NULL;
2912
    virNetworkPtr ret = NULL;
2913

2914
    testDriverLock(privconn);
2915
    if ((def = virNetworkDefParseString(xml)) == NULL)
2916
        goto cleanup;
2917

2918
    if ((net = virNetworkAssignDef(&privconn->networks, def)) == NULL)
2919 2920
        goto cleanup;
    def = NULL;
2921
    net->persistent = 1;
2922

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

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

static int testNetworkUndefine(virNetworkPtr network) {
2934 2935
    testConnPtr privconn = network->conn->privateData;
    virNetworkObjPtr privnet;
2936
    int ret = -1;
2937

2938
    testDriverLock(privconn);
2939 2940 2941 2942 2943
    privnet = virNetworkFindByName(&privconn->networks,
                                   network->name);

    if (privnet == NULL) {
        testError(network->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
2944
        goto cleanup;
2945
    }
2946

D
Daniel P. Berrange 已提交
2947
    if (virNetworkObjIsActive(privnet)) {
2948 2949
        testError(network->conn, VIR_ERR_INTERNAL_ERROR,
                  _("Network '%s' is still running"), network->name);
2950
        goto cleanup;
2951 2952
    }

2953 2954
    virNetworkRemoveInactive(&privconn->networks,
                             privnet);
2955
    privnet = NULL;
2956
    ret = 0;
2957

2958
cleanup:
2959 2960 2961
    if (privnet)
        virNetworkObjUnlock(privnet);
    testDriverUnlock(privconn);
2962
    return ret;
2963 2964 2965
}

static int testNetworkStart(virNetworkPtr network) {
2966 2967
    testConnPtr privconn = network->conn->privateData;
    virNetworkObjPtr privnet;
2968
    int ret = -1;
2969

2970
    testDriverLock(privconn);
2971 2972
    privnet = virNetworkFindByName(&privconn->networks,
                                   network->name);
2973
    testDriverUnlock(privconn);
2974 2975 2976

    if (privnet == NULL) {
        testError(network->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
2977
        goto cleanup;
2978
    }
2979

D
Daniel P. Berrange 已提交
2980
    if (virNetworkObjIsActive(privnet)) {
2981 2982
        testError(network->conn, VIR_ERR_INTERNAL_ERROR,
                  _("Network '%s' is already running"), network->name);
2983
        goto cleanup;
2984 2985
    }

2986
    privnet->active = 1;
2987
    ret = 0;
2988

2989
cleanup:
2990 2991
    if (privnet)
        virNetworkObjUnlock(privnet);
2992
    return ret;
2993 2994 2995
}

static int testNetworkDestroy(virNetworkPtr network) {
2996 2997
    testConnPtr privconn = network->conn->privateData;
    virNetworkObjPtr privnet;
2998
    int ret = -1;
2999

3000
    testDriverLock(privconn);
3001 3002 3003 3004 3005
    privnet = virNetworkFindByName(&privconn->networks,
                                   network->name);

    if (privnet == NULL) {
        testError(network->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
3006
        goto cleanup;
3007
    }
3008

3009 3010 3011 3012
    privnet->active = 0;
    if (!privnet->persistent) {
        virNetworkRemoveInactive(&privconn->networks,
                                 privnet);
3013
        privnet = NULL;
3014
    }
3015 3016 3017
    ret = 0;

cleanup:
3018 3019 3020
    if (privnet)
        virNetworkObjUnlock(privnet);
    testDriverUnlock(privconn);
3021
    return ret;
3022 3023 3024
}

static char *testNetworkDumpXML(virNetworkPtr network, int flags ATTRIBUTE_UNUSED) {
3025 3026
    testConnPtr privconn = network->conn->privateData;
    virNetworkObjPtr privnet;
3027
    char *ret = NULL;
3028

3029
    testDriverLock(privconn);
3030 3031
    privnet = virNetworkFindByName(&privconn->networks,
                                   network->name);
3032
    testDriverUnlock(privconn);
3033 3034 3035

    if (privnet == NULL) {
        testError(network->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
3036
        goto cleanup;
3037
    }
3038

3039
    ret = virNetworkDefFormat(privnet->def);
3040 3041

cleanup:
3042 3043
    if (privnet)
        virNetworkObjUnlock(privnet);
3044
    return ret;
3045 3046 3047
}

static char *testNetworkGetBridgeName(virNetworkPtr network) {
3048
    testConnPtr privconn = network->conn->privateData;
3049
    char *bridge = NULL;
3050 3051
    virNetworkObjPtr privnet;

3052
    testDriverLock(privconn);
3053 3054
    privnet = virNetworkFindByName(&privconn->networks,
                                   network->name);
3055
    testDriverUnlock(privconn);
3056 3057 3058

    if (privnet == NULL) {
        testError(network->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
3059
        goto cleanup;
3060 3061
    }

3062 3063 3064 3065 3066 3067 3068 3069
    if (!(privnet->def->bridge)) {
        testError(network->conn, VIR_ERR_INTERNAL_ERROR,
                  _("network '%s' does not have a bridge name."),
                  privnet->def->name);
        goto cleanup;
    }

    if (!(bridge = strdup(privnet->def->bridge))) {
3070
        virReportOOMError();
3071
        goto cleanup;
3072
    }
3073 3074

cleanup:
3075 3076
    if (privnet)
        virNetworkObjUnlock(privnet);
3077 3078 3079 3080 3081
    return bridge;
}

static int testNetworkGetAutostart(virNetworkPtr network,
                                   int *autostart) {
3082 3083
    testConnPtr privconn = network->conn->privateData;
    virNetworkObjPtr privnet;
3084
    int ret = -1;
3085

3086
    testDriverLock(privconn);
3087 3088
    privnet = virNetworkFindByName(&privconn->networks,
                                   network->name);
3089
    testDriverUnlock(privconn);
3090 3091 3092

    if (privnet == NULL) {
        testError(network->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
3093
        goto cleanup;
3094 3095
    }

3096
    *autostart = privnet->autostart;
3097 3098 3099
    ret = 0;

cleanup:
3100 3101
    if (privnet)
        virNetworkObjUnlock(privnet);
3102
    return ret;
3103 3104 3105 3106
}

static int testNetworkSetAutostart(virNetworkPtr network,
                                   int autostart) {
3107 3108
    testConnPtr privconn = network->conn->privateData;
    virNetworkObjPtr privnet;
3109
    int ret = -1;
3110

3111
    testDriverLock(privconn);
3112 3113
    privnet = virNetworkFindByName(&privconn->networks,
                                   network->name);
3114
    testDriverUnlock(privconn);
3115 3116 3117

    if (privnet == NULL) {
        testError(network->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
3118
        goto cleanup;
3119 3120
    }

3121
    privnet->autostart = autostart ? 1 : 0;
3122 3123 3124
    ret = 0;

cleanup:
3125 3126
    if (privnet)
        virNetworkObjUnlock(privnet);
3127
    return ret;
3128
}
3129

C
Cole Robinson 已提交
3130

L
Laine Stump 已提交
3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160
/*
 * Physical host interface routines
 */

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

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

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


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

    testDriverLock(privconn);
    for (i = 0 ; (i < privconn->ifaces.count); i++) {
        virInterfaceObjLock(privconn->ifaces.objs[i]);
D
Daniel P. Berrange 已提交
3161
        if (virInterfaceObjIsActive(privconn->ifaces.objs[i])) {
L
Laine Stump 已提交
3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178
            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 已提交
3179
        if (virInterfaceObjIsActive(privconn->ifaces.objs[i])) {
L
Laine Stump 已提交
3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191
            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:
3192
    virReportOOMError();
L
Laine Stump 已提交
3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206
    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 已提交
3207
        if (!virInterfaceObjIsActive(privconn->ifaces.objs[i])) {
L
Laine Stump 已提交
3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224
            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 已提交
3225
        if (!virInterfaceObjIsActive(privconn->ifaces.objs[i])) {
L
Laine Stump 已提交
3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237
            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:
3238
    virReportOOMError();
L
Laine Stump 已提交
3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298
    for (n = 0 ; n < nnames ; n++)
        VIR_FREE(names[n]);
    testDriverUnlock(privconn);
    return -1;
}

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

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

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

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

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

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

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

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

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

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

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

3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320
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) {
        testError(iface->conn, VIR_ERR_NO_INTERFACE, NULL);
        goto cleanup;
    }
    ret = virInterfaceObjIsActive(obj);

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


L
Laine Stump 已提交
3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337
static char *testInterfaceGetXMLDesc(virInterfacePtr iface,
                                     unsigned int flags ATTRIBUTE_UNUSED)
{
    testConnPtr privconn = iface->conn->privateData;
    virInterfaceObjPtr privinterface;
    char *ret = NULL;

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

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

3338
    ret = virInterfaceDefFormat(privinterface->def);
L
Laine Stump 已提交
3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355

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


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

    testDriverLock(privconn);
3356
    if ((def = virInterfaceDefParseString(xmlStr)) == NULL)
L
Laine Stump 已提交
3357 3358
        goto cleanup;

3359
    if ((iface = virInterfaceAssignDef(&privconn->ifaces, def)) == NULL)
L
Laine Stump 已提交
3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460
        goto cleanup;
    def = NULL;

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

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

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

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

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

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

cleanup:
    testDriverUnlock(privconn);
    return ret;
}

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

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

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

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

    privinterface->active = 1;
    ret = 0;

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

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

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

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

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

    privinterface->active = 0;
    ret = 0;

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



C
Cole Robinson 已提交
3461 3462 3463 3464
/*
 * Storage Driver routines
 */

3465

3466
static int testStoragePoolObjSetDefaults(virStoragePoolObjPtr pool) {
C
Cole Robinson 已提交
3467 3468 3469 3470 3471 3472 3473

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

    pool->configFile = strdup("\0");
    if (!pool->configFile) {
3474
        virReportOOMError();
C
Cole Robinson 已提交
3475 3476 3477 3478 3479 3480
        return -1;
    }

    return 0;
}

3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495
static virDrvOpenStatus testStorageOpen(virConnectPtr conn,
                                        virConnectAuthPtr auth ATTRIBUTE_UNUSED,
                                        int flags ATTRIBUTE_UNUSED) {
    if (STRNEQ(conn->driver->name, "Test"))
        return VIR_DRV_OPEN_DECLINED;

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

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

3496

C
Cole Robinson 已提交
3497 3498 3499
static virStoragePoolPtr
testStoragePoolLookupByUUID(virConnectPtr conn,
                            const unsigned char *uuid) {
3500
    testConnPtr privconn = conn->privateData;
3501 3502
    virStoragePoolObjPtr pool;
    virStoragePoolPtr ret = NULL;
C
Cole Robinson 已提交
3503

3504
    testDriverLock(privconn);
3505
    pool = virStoragePoolObjFindByUUID(&privconn->pools, uuid);
3506
    testDriverUnlock(privconn);
3507 3508

    if (pool == NULL) {
C
Cole Robinson 已提交
3509
        testError (conn, VIR_ERR_NO_STORAGE_POOL, NULL);
3510
        goto cleanup;
C
Cole Robinson 已提交
3511 3512
    }

3513 3514 3515
    ret = virGetStoragePool(conn, pool->def->name, pool->def->uuid);

cleanup:
3516 3517
    if (pool)
        virStoragePoolObjUnlock(pool);
3518
    return ret;
C
Cole Robinson 已提交
3519 3520 3521 3522 3523
}

static virStoragePoolPtr
testStoragePoolLookupByName(virConnectPtr conn,
                            const char *name) {
3524
    testConnPtr privconn = conn->privateData;
3525 3526
    virStoragePoolObjPtr pool;
    virStoragePoolPtr ret = NULL;
C
Cole Robinson 已提交
3527

3528
    testDriverLock(privconn);
3529
    pool = virStoragePoolObjFindByName(&privconn->pools, name);
3530
    testDriverUnlock(privconn);
3531 3532

    if (pool == NULL) {
C
Cole Robinson 已提交
3533
        testError (conn, VIR_ERR_NO_STORAGE_POOL, NULL);
3534
        goto cleanup;
C
Cole Robinson 已提交
3535 3536
    }

3537 3538 3539
    ret = virGetStoragePool(conn, pool->def->name, pool->def->uuid);

cleanup:
3540 3541
    if (pool)
        virStoragePoolObjUnlock(pool);
3542
    return ret;
C
Cole Robinson 已提交
3543 3544 3545 3546 3547 3548 3549 3550 3551
}

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

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

3555
    testDriverLock(privconn);
C
Cole Robinson 已提交
3556 3557 3558
    for (i = 0 ; i < privconn->pools.count ; i++)
        if (virStoragePoolObjIsActive(privconn->pools.objs[i]))
            numActive++;
3559
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
3560 3561 3562 3563 3564 3565 3566 3567

    return numActive;
}

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

3571
    testDriverLock(privconn);
C
Cole Robinson 已提交
3572
    memset(names, 0, sizeof(*names)*nnames);
3573 3574
    for (i = 0 ; i < privconn->pools.count && n < nnames ; i++) {
        virStoragePoolObjLock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
3575
        if (virStoragePoolObjIsActive(privconn->pools.objs[i]) &&
3576 3577
            !(names[n++] = strdup(privconn->pools.objs[i]->def->name))) {
            virStoragePoolObjUnlock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
3578
            goto no_memory;
3579 3580 3581 3582
        }
        virStoragePoolObjUnlock(privconn->pools.objs[i]);
    }
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
3583 3584 3585 3586

    return n;

no_memory:
3587
    virReportOOMError();
C
Cole Robinson 已提交
3588 3589
    for (n = 0 ; n < nnames ; n++)
        VIR_FREE(names[n]);
3590
    testDriverUnlock(privconn);
3591
    return -1;
C
Cole Robinson 已提交
3592 3593 3594 3595
}

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

3599 3600 3601
    testDriverLock(privconn);
    for (i = 0 ; i < privconn->pools.count ; i++) {
        virStoragePoolObjLock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
3602 3603
        if (!virStoragePoolObjIsActive(privconn->pools.objs[i]))
            numInactive++;
3604 3605 3606
        virStoragePoolObjUnlock(privconn->pools.objs[i]);
    }
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
3607 3608 3609 3610 3611 3612 3613 3614

    return numInactive;
}

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

3618
    testDriverLock(privconn);
C
Cole Robinson 已提交
3619
    memset(names, 0, sizeof(*names)*nnames);
3620 3621
    for (i = 0 ; i < privconn->pools.count && n < nnames ; i++) {
        virStoragePoolObjLock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
3622
        if (!virStoragePoolObjIsActive(privconn->pools.objs[i]) &&
3623 3624
            !(names[n++] = strdup(privconn->pools.objs[i]->def->name))) {
            virStoragePoolObjUnlock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
3625
            goto no_memory;
3626 3627 3628 3629
        }
        virStoragePoolObjUnlock(privconn->pools.objs[i]);
    }
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
3630 3631 3632 3633

    return n;

no_memory:
3634
    virReportOOMError();
C
Cole Robinson 已提交
3635 3636
    for (n = 0 ; n < nnames ; n++)
        VIR_FREE(names[n]);
3637
    testDriverUnlock(privconn);
3638
    return -1;
C
Cole Robinson 已提交
3639 3640 3641
}


3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685
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) {
        testError(pool->conn, VIR_ERR_NO_STORAGE_POOL, NULL);
        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) {
        testError(pool->conn, VIR_ERR_NO_STORAGE_POOL, NULL);
        goto cleanup;
    }
    ret = obj->configFile ? 1 : 0;

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



C
Cole Robinson 已提交
3686
static int
3687
testStoragePoolStart(virStoragePoolPtr pool,
C
Cole Robinson 已提交
3688
                     unsigned int flags ATTRIBUTE_UNUSED) {
3689 3690
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
3691
    int ret = -1;
3692

3693
    testDriverLock(privconn);
3694 3695
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
3696
    testDriverUnlock(privconn);
3697 3698 3699

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

3703 3704 3705 3706 3707
    if (virStoragePoolObjIsActive(privpool)) {
        testError(pool->conn, VIR_ERR_INTERNAL_ERROR,
                  _("storage pool '%s' is already active"), pool->name);
        goto cleanup;
    }
C
Cole Robinson 已提交
3708 3709

    privpool->active = 1;
3710
    ret = 0;
C
Cole Robinson 已提交
3711

3712
cleanup:
3713 3714
    if (privpool)
        virStoragePoolObjUnlock(privpool);
3715
    return ret;
C
Cole Robinson 已提交
3716 3717 3718
}

static char *
3719 3720 3721
testStorageFindPoolSources(virConnectPtr conn,
                           const char *type,
                           const char *srcSpec,
C
Cole Robinson 已提交
3722 3723
                           unsigned int flags ATTRIBUTE_UNUSED)
{
3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735
    virStoragePoolSourcePtr source = NULL;
    int pool_type;
    char *ret = NULL;

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

    if (srcSpec) {
3736
        source = virStoragePoolDefParseSourceString(srcSpec, pool_type);
3737 3738 3739 3740 3741 3742 3743 3744 3745
        if (!source)
            goto cleanup;
    }

    switch (pool_type) {

    case VIR_STORAGE_POOL_LOGICAL:
        ret = strdup(defaultPoolSourcesLogicalXML);
        if (!ret)
3746
            virReportOOMError();
3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757
        break;

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

        if (virAsprintf(&ret, defaultPoolSourcesNetFSXML,
                        source->host.name) < 0)
3758
            virReportOOMError();
3759 3760 3761 3762 3763 3764 3765 3766 3767
        break;

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

cleanup:
    virStoragePoolSourceFree(source);
3768
    VIR_FREE(source);
3769
    return ret;
C
Cole Robinson 已提交
3770 3771 3772 3773 3774 3775 3776
}


static virStoragePoolPtr
testStoragePoolCreate(virConnectPtr conn,
                      const char *xml,
                      unsigned int flags ATTRIBUTE_UNUSED) {
3777
    testConnPtr privconn = conn->privateData;
C
Cole Robinson 已提交
3778
    virStoragePoolDefPtr def;
3779
    virStoragePoolObjPtr pool = NULL;
3780
    virStoragePoolPtr ret = NULL;
C
Cole Robinson 已提交
3781

3782
    testDriverLock(privconn);
3783
    if (!(def = virStoragePoolDefParseString(xml)))
3784
        goto cleanup;
C
Cole Robinson 已提交
3785

3786 3787 3788 3789
    pool = virStoragePoolObjFindByUUID(&privconn->pools, def->uuid);
    if (!pool)
        pool = virStoragePoolObjFindByName(&privconn->pools, def->name);
    if (pool) {
C
Cole Robinson 已提交
3790 3791
        testError(conn, VIR_ERR_INTERNAL_ERROR,
                  "%s", _("storage pool already exists"));
3792
        goto cleanup;
C
Cole Robinson 已提交
3793 3794
    }

3795
    if (!(pool = virStoragePoolObjAssignDef(&privconn->pools, def)))
3796
        goto cleanup;
3797
    def = NULL;
C
Cole Robinson 已提交
3798

3799
    if (testStoragePoolObjSetDefaults(pool) == -1) {
C
Cole Robinson 已提交
3800
        virStoragePoolObjRemove(&privconn->pools, pool);
3801 3802
        pool = NULL;
        goto cleanup;
C
Cole Robinson 已提交
3803 3804 3805
    }
    pool->active = 1;

3806 3807 3808 3809
    ret = virGetStoragePool(conn, pool->def->name, pool->def->uuid);

cleanup:
    virStoragePoolDefFree(def);
3810 3811 3812
    if (pool)
        virStoragePoolObjUnlock(pool);
    testDriverUnlock(privconn);
3813
    return ret;
C
Cole Robinson 已提交
3814 3815 3816 3817 3818 3819
}

static virStoragePoolPtr
testStoragePoolDefine(virConnectPtr conn,
                      const char *xml,
                      unsigned int flags ATTRIBUTE_UNUSED) {
3820
    testConnPtr privconn = conn->privateData;
C
Cole Robinson 已提交
3821
    virStoragePoolDefPtr def;
3822
    virStoragePoolObjPtr pool = NULL;
3823
    virStoragePoolPtr ret = NULL;
C
Cole Robinson 已提交
3824

3825
    testDriverLock(privconn);
3826
    if (!(def = virStoragePoolDefParseString(xml)))
3827
        goto cleanup;
C
Cole Robinson 已提交
3828 3829 3830 3831 3832

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

3833
    if (!(pool = virStoragePoolObjAssignDef(&privconn->pools, def)))
3834 3835
        goto cleanup;
    def = NULL;
C
Cole Robinson 已提交
3836

3837
    if (testStoragePoolObjSetDefaults(pool) == -1) {
C
Cole Robinson 已提交
3838
        virStoragePoolObjRemove(&privconn->pools, pool);
3839 3840
        pool = NULL;
        goto cleanup;
C
Cole Robinson 已提交
3841 3842
    }

3843 3844 3845 3846
    ret = virGetStoragePool(conn, pool->def->name, pool->def->uuid);

cleanup:
    virStoragePoolDefFree(def);
3847 3848 3849
    if (pool)
        virStoragePoolObjUnlock(pool);
    testDriverUnlock(privconn);
3850
    return ret;
C
Cole Robinson 已提交
3851 3852 3853
}

static int
3854 3855 3856
testStoragePoolUndefine(virStoragePoolPtr pool) {
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
3857
    int ret = -1;
3858

3859
    testDriverLock(privconn);
3860 3861 3862 3863 3864
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);

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

3868 3869 3870 3871 3872
    if (virStoragePoolObjIsActive(privpool)) {
        testError(pool->conn, VIR_ERR_INTERNAL_ERROR,
                  _("storage pool '%s' is already active"), pool->name);
        goto cleanup;
    }
C
Cole Robinson 已提交
3873 3874

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

3877
cleanup:
3878 3879 3880
    if (privpool)
        virStoragePoolObjUnlock(privpool);
    testDriverUnlock(privconn);
3881
    return ret;
C
Cole Robinson 已提交
3882 3883 3884
}

static int
3885
testStoragePoolBuild(virStoragePoolPtr pool,
C
Cole Robinson 已提交
3886
                     unsigned int flags ATTRIBUTE_UNUSED) {
3887 3888
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
3889
    int ret = -1;
3890

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

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

3901 3902 3903 3904 3905
    if (virStoragePoolObjIsActive(privpool)) {
        testError(pool->conn, VIR_ERR_INTERNAL_ERROR,
                  _("storage pool '%s' is already active"), pool->name);
        goto cleanup;
    }
3906
    ret = 0;
C
Cole Robinson 已提交
3907

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


static int
3916 3917 3918
testStoragePoolDestroy(virStoragePoolPtr pool) {
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
3919
    int ret = -1;
3920

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

    if (privpool == NULL) {
        testError(pool->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
3927
        goto cleanup;
3928 3929 3930 3931 3932
    }

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

    privpool->active = 0;

3938
    if (privpool->configFile == NULL) {
C
Cole Robinson 已提交
3939
        virStoragePoolObjRemove(&privconn->pools, privpool);
3940 3941
        privpool = NULL;
    }
3942
    ret = 0;
C
Cole Robinson 已提交
3943

3944
cleanup:
3945 3946 3947
    if (privpool)
        virStoragePoolObjUnlock(privpool);
    testDriverUnlock(privconn);
3948
    return ret;
C
Cole Robinson 已提交
3949 3950 3951 3952
}


static int
3953
testStoragePoolDelete(virStoragePoolPtr pool,
C
Cole Robinson 已提交
3954
                      unsigned int flags ATTRIBUTE_UNUSED) {
3955 3956
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
3957
    int ret = -1;
3958

3959
    testDriverLock(privconn);
3960 3961
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
3962
    testDriverUnlock(privconn);
3963 3964 3965

    if (privpool == NULL) {
        testError(pool->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
3966 3967 3968 3969 3970 3971 3972
        goto cleanup;
    }

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

3975
    ret = 0;
C
Cole Robinson 已提交
3976

3977
cleanup:
3978 3979
    if (privpool)
        virStoragePoolObjUnlock(privpool);
3980
    return ret;
C
Cole Robinson 已提交
3981 3982 3983 3984
}


static int
3985
testStoragePoolRefresh(virStoragePoolPtr pool,
C
Cole Robinson 已提交
3986
                       unsigned int flags ATTRIBUTE_UNUSED) {
3987 3988
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
3989
    int ret = -1;
3990

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

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

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

4008
cleanup:
4009 4010
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4011
    return ret;
C
Cole Robinson 已提交
4012 4013 4014 4015
}


static int
4016
testStoragePoolGetInfo(virStoragePoolPtr pool,
C
Cole Robinson 已提交
4017
                       virStoragePoolInfoPtr info) {
4018 4019
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4020
    int ret = -1;
4021

4022
    testDriverLock(privconn);
4023 4024
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4025
    testDriverUnlock(privconn);
4026 4027 4028

    if (privpool == NULL) {
        testError(pool->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
4029
        goto cleanup;
4030
    }
C
Cole Robinson 已提交
4031 4032 4033 4034 4035 4036 4037 4038 4039

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

4042
cleanup:
4043 4044
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4045
    return ret;
C
Cole Robinson 已提交
4046 4047 4048
}

static char *
4049
testStoragePoolDumpXML(virStoragePoolPtr pool,
C
Cole Robinson 已提交
4050
                       unsigned int flags ATTRIBUTE_UNUSED) {
4051 4052
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4053
    char *ret = NULL;
4054

4055
    testDriverLock(privconn);
4056 4057
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4058
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
4059

4060 4061
    if (privpool == NULL) {
        testError(pool->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
4062
        goto cleanup;
4063 4064
    }

4065
    ret = virStoragePoolDefFormat(privpool->def);
4066 4067

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

static int
4074
testStoragePoolGetAutostart(virStoragePoolPtr pool,
C
Cole Robinson 已提交
4075
                            int *autostart) {
4076 4077
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4078
    int ret = -1;
4079

4080
    testDriverLock(privconn);
4081 4082
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4083
    testDriverUnlock(privconn);
4084 4085 4086

    if (privpool == NULL) {
        testError(pool->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
4087
        goto cleanup;
4088
    }
C
Cole Robinson 已提交
4089 4090 4091 4092 4093 4094

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

4097
cleanup:
4098 4099
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4100
    return ret;
C
Cole Robinson 已提交
4101 4102 4103
}

static int
4104
testStoragePoolSetAutostart(virStoragePoolPtr pool,
C
Cole Robinson 已提交
4105
                            int autostart) {
4106 4107
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4108
    int ret = -1;
4109

4110
    testDriverLock(privconn);
4111 4112
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4113
    testDriverUnlock(privconn);
4114 4115 4116

    if (privpool == NULL) {
        testError(pool->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
4117
        goto cleanup;
4118
    }
C
Cole Robinson 已提交
4119 4120

    if (!privpool->configFile) {
4121
        testError(pool->conn, VIR_ERR_INVALID_ARG,
C
Cole Robinson 已提交
4122
                  "%s", _("pool has no config file"));
4123
        goto cleanup;
C
Cole Robinson 已提交
4124 4125 4126 4127
    }

    autostart = (autostart != 0);
    privpool->autostart = autostart;
4128 4129 4130
    ret = 0;

cleanup:
4131 4132
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4133
    return ret;
C
Cole Robinson 已提交
4134 4135 4136 4137
}


static int
4138 4139 4140
testStoragePoolNumVolumes(virStoragePoolPtr pool) {
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4141
    int ret = -1;
4142

4143
    testDriverLock(privconn);
4144 4145
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4146
    testDriverUnlock(privconn);
4147 4148 4149

    if (privpool == NULL) {
        testError(pool->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
4150
        goto cleanup;
4151 4152 4153 4154 4155
    }

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

4159 4160 4161
    ret = privpool->volumes.count;

cleanup:
4162 4163
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4164
    return ret;
C
Cole Robinson 已提交
4165 4166 4167
}

static int
4168
testStoragePoolListVolumes(virStoragePoolPtr pool,
C
Cole Robinson 已提交
4169 4170
                           char **const names,
                           int maxnames) {
4171 4172
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
C
Cole Robinson 已提交
4173 4174
    int i = 0, n = 0;

4175
    memset(names, 0, maxnames * sizeof(*names));
4176 4177

    testDriverLock(privconn);
4178 4179
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4180
    testDriverUnlock(privconn);
4181 4182 4183

    if (privpool == NULL) {
        testError(pool->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
4184
        goto cleanup;
4185 4186 4187 4188 4189 4190
    }


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

C
Cole Robinson 已提交
4194 4195
    for (i = 0 ; i < privpool->volumes.count && n < maxnames ; i++) {
        if ((names[n++] = strdup(privpool->volumes.objs[i]->name)) == NULL) {
4196
            virReportOOMError();
C
Cole Robinson 已提交
4197 4198 4199 4200
            goto cleanup;
        }
    }

4201
    virStoragePoolObjUnlock(privpool);
C
Cole Robinson 已提交
4202 4203 4204 4205 4206 4207
    return n;

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

4208
    memset(names, 0, maxnames * sizeof(*names));
4209 4210
    if (privpool)
        virStoragePoolObjUnlock(privpool);
C
Cole Robinson 已提交
4211 4212 4213 4214 4215
    return -1;
}


static virStorageVolPtr
4216
testStorageVolumeLookupByName(virStoragePoolPtr pool,
C
Cole Robinson 已提交
4217
                              const char *name ATTRIBUTE_UNUSED) {
4218 4219 4220
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol;
4221
    virStorageVolPtr ret = NULL;
4222

4223
    testDriverLock(privconn);
4224 4225
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4226
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
4227

4228 4229
    if (privpool == NULL) {
        testError(pool->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
4230
        goto cleanup;
4231 4232 4233 4234 4235 4236
    }


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

    privvol = virStorageVolDefFindByName(privpool, name);

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

4248 4249 4250 4251
    ret = virGetStorageVol(pool->conn, privpool->def->name,
                           privvol->name, privvol->key);

cleanup:
4252 4253
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4254
    return ret;
C
Cole Robinson 已提交
4255 4256 4257 4258 4259 4260
}


static virStorageVolPtr
testStorageVolumeLookupByKey(virConnectPtr conn,
                             const char *key) {
4261
    testConnPtr privconn = conn->privateData;
C
Cole Robinson 已提交
4262
    unsigned int i;
4263
    virStorageVolPtr ret = NULL;
C
Cole Robinson 已提交
4264

4265
    testDriverLock(privconn);
C
Cole Robinson 已提交
4266
    for (i = 0 ; i < privconn->pools.count ; i++) {
4267
        virStoragePoolObjLock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
4268
        if (virStoragePoolObjIsActive(privconn->pools.objs[i])) {
4269
            virStorageVolDefPtr privvol =
C
Cole Robinson 已提交
4270 4271
                virStorageVolDefFindByKey(privconn->pools.objs[i], key);

4272 4273 4274 4275 4276
            if (privvol) {
                ret = virGetStorageVol(conn,
                                       privconn->pools.objs[i]->def->name,
                                       privvol->name,
                                       privvol->key);
4277
                virStoragePoolObjUnlock(privconn->pools.objs[i]);
4278 4279
                break;
            }
C
Cole Robinson 已提交
4280
        }
4281
        virStoragePoolObjUnlock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
4282
    }
4283
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
4284

4285 4286 4287 4288 4289
    if (!ret)
        testError(conn, VIR_ERR_INVALID_STORAGE_VOL,
                  _("no storage vol with matching key '%s'"), key);

    return ret;
C
Cole Robinson 已提交
4290 4291 4292 4293 4294
}

static virStorageVolPtr
testStorageVolumeLookupByPath(virConnectPtr conn,
                              const char *path) {
4295
    testConnPtr privconn = conn->privateData;
C
Cole Robinson 已提交
4296
    unsigned int i;
4297
    virStorageVolPtr ret = NULL;
C
Cole Robinson 已提交
4298

4299
    testDriverLock(privconn);
C
Cole Robinson 已提交
4300
    for (i = 0 ; i < privconn->pools.count ; i++) {
4301
        virStoragePoolObjLock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
4302
        if (virStoragePoolObjIsActive(privconn->pools.objs[i])) {
4303
            virStorageVolDefPtr privvol =
C
Cole Robinson 已提交
4304 4305
                virStorageVolDefFindByPath(privconn->pools.objs[i], path);

4306 4307 4308 4309 4310
            if (privvol) {
                ret = virGetStorageVol(conn,
                                       privconn->pools.objs[i]->def->name,
                                       privvol->name,
                                       privvol->key);
4311
                virStoragePoolObjUnlock(privconn->pools.objs[i]);
4312 4313
                break;
            }
C
Cole Robinson 已提交
4314
        }
4315
        virStoragePoolObjUnlock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
4316
    }
4317
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
4318

4319 4320 4321 4322 4323
    if (!ret)
        testError(conn, VIR_ERR_INVALID_STORAGE_VOL,
                  _("no storage vol with matching path '%s'"), path);

    return ret;
C
Cole Robinson 已提交
4324 4325 4326
}

static virStorageVolPtr
4327
testStorageVolumeCreateXML(virStoragePoolPtr pool,
C
Cole Robinson 已提交
4328 4329
                           const char *xmldesc,
                           unsigned int flags ATTRIBUTE_UNUSED) {
4330 4331
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4332 4333
    virStorageVolDefPtr privvol = NULL;
    virStorageVolPtr ret = NULL;
4334

4335
    testDriverLock(privconn);
4336 4337
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4338
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
4339

4340 4341
    if (privpool == NULL) {
        testError(pool->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
4342
        goto cleanup;
4343 4344 4345 4346 4347
    }

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

4351
    privvol = virStorageVolDefParseString(privpool->def, xmldesc);
4352
    if (privvol == NULL)
4353
        goto cleanup;
4354 4355

    if (virStorageVolDefFindByName(privpool, privvol->name)) {
4356
        testError(pool->conn, VIR_ERR_INVALID_STORAGE_VOL,
C
Cole Robinson 已提交
4357
                  "%s", _("storage vol already exists"));
4358
        goto cleanup;
C
Cole Robinson 已提交
4359 4360 4361
    }

    /* Make sure enough space */
4362
    if ((privpool->def->allocation + privvol->allocation) >
C
Cole Robinson 已提交
4363
         privpool->def->capacity) {
4364
        testError(pool->conn, VIR_ERR_INTERNAL_ERROR,
C
Cole Robinson 已提交
4365
                  _("Not enough free space in pool for volume '%s'"),
4366
                  privvol->name);
4367
        goto cleanup;
C
Cole Robinson 已提交
4368 4369 4370 4371
    }

    if (VIR_REALLOC_N(privpool->volumes.objs,
                      privpool->volumes.count+1) < 0) {
4372
        virReportOOMError();
4373
        goto cleanup;
C
Cole Robinson 已提交
4374 4375
    }

4376 4377 4378
    if (virAsprintf(&privvol->target.path, "%s/%s",
                    privpool->def->target.path,
                    privvol->name) == -1) {
4379
        virReportOOMError();
4380
        goto cleanup;
C
Cole Robinson 已提交
4381 4382
    }

4383 4384
    privvol->key = strdup(privvol->target.path);
    if (privvol->key == NULL) {
4385
        virReportOOMError();
4386
        goto cleanup;
C
Cole Robinson 已提交
4387 4388
    }

4389
    privpool->def->allocation += privvol->allocation;
C
Cole Robinson 已提交
4390 4391 4392
    privpool->def->available = (privpool->def->capacity -
                                privpool->def->allocation);

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

4395 4396
    ret = virGetStorageVol(pool->conn, privpool->def->name,
                           privvol->name, privvol->key);
4397
    privvol = NULL;
4398 4399 4400

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

4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431
static virStorageVolPtr
testStorageVolumeCreateXMLFrom(virStoragePoolPtr pool,
                               const char *xmldesc,
                               virStorageVolPtr clonevol,
                               unsigned int flags ATTRIBUTE_UNUSED) {
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol = NULL, origvol = NULL;
    virStorageVolPtr ret = NULL;

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

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

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

4432
    privvol = virStorageVolDefParseString(privpool->def, xmldesc);
4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462
    if (privvol == NULL)
        goto cleanup;

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

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

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

    if (VIR_REALLOC_N(privpool->volumes.objs,
                      privpool->volumes.count+1) < 0) {
4463
        virReportOOMError();
4464 4465 4466
        goto cleanup;
    }

4467 4468 4469
    if (virAsprintf(&privvol->target.path, "%s/%s",
                    privpool->def->target.path,
                    privvol->name) == -1) {
4470
        virReportOOMError();
4471 4472 4473 4474 4475
        goto cleanup;
    }

    privvol->key = strdup(privvol->target.path);
    if (privvol->key == NULL) {
4476
        virReportOOMError();
4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496
        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 已提交
4497
static int
4498
testStorageVolumeDelete(virStorageVolPtr vol,
C
Cole Robinson 已提交
4499
                        unsigned int flags ATTRIBUTE_UNUSED) {
4500 4501 4502
    testConnPtr privconn = vol->conn->privateData;
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol;
C
Cole Robinson 已提交
4503
    int i;
4504
    int ret = -1;
C
Cole Robinson 已提交
4505

4506
    testDriverLock(privconn);
4507 4508
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           vol->pool);
4509
    testDriverUnlock(privconn);
4510 4511 4512

    if (privpool == NULL) {
        testError(vol->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
4513
        goto cleanup;
4514 4515 4516 4517 4518 4519 4520 4521 4522
    }


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

    if (privvol == NULL) {
        testError(vol->conn, VIR_ERR_INVALID_STORAGE_VOL,
                  _("no storage vol with matching name '%s'"),
                  vol->name);
4523
        goto cleanup;
4524 4525 4526 4527 4528
    }

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


C
Cole Robinson 已提交
4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555
    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;
        }
    }
4556
    ret = 0;
C
Cole Robinson 已提交
4557

4558
cleanup:
4559 4560
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4561
    return ret;
C
Cole Robinson 已提交
4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577
}


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
4578
testStorageVolumeGetInfo(virStorageVolPtr vol,
C
Cole Robinson 已提交
4579
                         virStorageVolInfoPtr info) {
4580 4581 4582
    testConnPtr privconn = vol->conn->privateData;
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol;
4583
    int ret = -1;
4584

4585
    testDriverLock(privconn);
4586 4587
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           vol->pool);
4588
    testDriverUnlock(privconn);
4589 4590 4591

    if (privpool == NULL) {
        testError(vol->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
4592
        goto cleanup;
4593 4594 4595 4596 4597 4598 4599 4600
    }

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

    if (privvol == NULL) {
        testError(vol->conn, VIR_ERR_INVALID_STORAGE_VOL,
                  _("no storage vol with matching name '%s'"),
                  vol->name);
4601
        goto cleanup;
4602 4603 4604 4605 4606
    }

    if (!virStoragePoolObjIsActive(privpool)) {
        testError(vol->conn, VIR_ERR_INTERNAL_ERROR,
                  _("storage pool '%s' is not active"), vol->pool);
4607
        goto cleanup;
4608
    }
C
Cole Robinson 已提交
4609 4610 4611 4612 4613

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

4616
cleanup:
4617 4618
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4619
    return ret;
C
Cole Robinson 已提交
4620 4621 4622
}

static char *
4623
testStorageVolumeGetXMLDesc(virStorageVolPtr vol,
C
Cole Robinson 已提交
4624
                            unsigned int flags ATTRIBUTE_UNUSED) {
4625 4626 4627
    testConnPtr privconn = vol->conn->privateData;
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol;
4628
    char *ret = NULL;
4629

4630
    testDriverLock(privconn);
4631 4632
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           vol->pool);
4633
    testDriverUnlock(privconn);
4634 4635 4636

    if (privpool == NULL) {
        testError(vol->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
4637
        goto cleanup;
4638 4639 4640 4641 4642 4643 4644 4645
    }

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

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

4649 4650 4651
    if (!virStoragePoolObjIsActive(privpool)) {
        testError(vol->conn, VIR_ERR_INTERNAL_ERROR,
                  _("storage pool '%s' is not active"), vol->pool);
4652
        goto cleanup;
4653 4654
    }

4655
    ret = virStorageVolDefFormat(privpool->def, privvol);
4656 4657

cleanup:
4658 4659
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4660
    return ret;
C
Cole Robinson 已提交
4661 4662 4663
}

static char *
4664 4665 4666 4667
testStorageVolumeGetPath(virStorageVolPtr vol) {
    testConnPtr privconn = vol->conn->privateData;
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol;
4668
    char *ret = NULL;
4669

4670
    testDriverLock(privconn);
4671 4672
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           vol->pool);
4673
    testDriverUnlock(privconn);
4674 4675 4676

    if (privpool == NULL) {
        testError(vol->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
4677
        goto cleanup;
4678 4679 4680 4681 4682 4683 4684 4685
    }

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

    if (privvol == NULL) {
        testError(vol->conn, VIR_ERR_INVALID_STORAGE_VOL,
                  _("no storage vol with matching name '%s'"),
                  vol->name);
4686
        goto cleanup;
4687 4688 4689 4690 4691
    }

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

C
Cole Robinson 已提交
4695
    ret = strdup(privvol->target.path);
4696
    if (ret == NULL)
4697
        virReportOOMError();
4698 4699

cleanup:
4700 4701
    if (privpool)
        virStoragePoolObjUnlock(privpool);
C
Cole Robinson 已提交
4702 4703 4704
    return ret;
}

4705

4706
/* Node device implementations */
4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721
static virDrvOpenStatus testDevMonOpen(virConnectPtr conn,
                                       virConnectAuthPtr auth ATTRIBUTE_UNUSED,
                                       int flags ATTRIBUTE_UNUSED) {
    if (STRNEQ(conn->driver->name, "Test"))
        return VIR_DRV_OPEN_DECLINED;

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

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

4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787
static int
testNodeNumOfDevices(virConnectPtr conn,
                     const char *cap,
                     unsigned int flags ATTRIBUTE_UNUSED)
{
    testConnPtr driver = conn->privateData;
    int ndevs = 0;
    unsigned int i;

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

    return ndevs;
}

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

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

    return ndevs;

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

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

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

    if (!obj) {
4788
        virNodeDeviceReportError(VIR_ERR_NO_NODE_DEVICE, NULL);
4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812
        goto cleanup;
    }

    ret = virGetNodeDevice(conn, name);

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

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

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

    if (!obj) {
4813 4814
        virNodeDeviceReportError(VIR_ERR_NO_NODE_DEVICE,
                                 _("no node device with matching name '%s'"),
4815 4816 4817 4818
                                 dev->name);
        goto cleanup;
    }

4819
    ret = virNodeDeviceDefFormat(obj->def);
4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838

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) {
4839
        virNodeDeviceReportError(VIR_ERR_NO_NODE_DEVICE,
4840 4841 4842 4843 4844 4845 4846 4847
                                _("no node device with matching name '%s'"),
                                 dev->name);
        goto cleanup;
    }

    if (obj->def->parent) {
        ret = strdup(obj->def->parent);
        if (!ret)
4848
            virReportOOMError();
4849
    } else {
4850
        virNodeDeviceReportError(VIR_ERR_INTERNAL_ERROR,
4851 4852 4853 4854 4855 4856 4857 4858 4859
                                 "%s", _("no parent for this device"));
    }

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

4860

4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874
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) {
4875 4876
        virNodeDeviceReportError(VIR_ERR_NO_NODE_DEVICE,
                                 _("no node device with matching name '%s'"),
4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905
                                 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) {
4906
        virNodeDeviceReportError(VIR_ERR_NO_NODE_DEVICE,
4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929
                                _("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;
}

4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944
static virNodeDevicePtr
testNodeDeviceCreateXML(virConnectPtr conn,
                        const char *xmlDesc,
                        unsigned int flags ATTRIBUTE_UNUSED)
{
    testConnPtr driver = conn->privateData;
    virNodeDeviceDefPtr def = NULL;
    virNodeDeviceObjPtr obj = NULL;
    char *wwnn = NULL, *wwpn = NULL;
    int parent_host = -1;
    virNodeDevicePtr dev = NULL;
    virNodeDevCapsDefPtr caps;

    testDriverLock(driver);

4945
    def = virNodeDeviceDefParseString(xmlDesc, CREATE_DEVICE);
4946 4947 4948 4949 4950
    if (def == NULL) {
        goto cleanup;
    }

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

4955
    if (virNodeDeviceGetParentHost(&driver->devs,
4956 4957 4958 4959 4960 4961 4962 4963 4964 4965
                                   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))) {
4966
        virReportOOMError();
4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981
        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;
    }


4982
    if (!(obj = virNodeDeviceAssignDef(&driver->devs, def))) {
4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011
        goto cleanup;
    }
    virNodeDeviceObjUnlock(obj);

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

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

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

    if (!obj) {
5012
        virNodeDeviceReportError(VIR_ERR_NO_NODE_DEVICE, NULL);
5013 5014 5015
        goto out;
    }

5016
    if (virNodeDeviceGetWWNs(obj->def, &wwnn, &wwpn) == -1) {
5017 5018 5019 5020 5021
        goto out;
    }

    parent_name = strdup(obj->def->parent);
    if (parent_name == NULL) {
5022
        virReportOOMError();
5023 5024 5025 5026 5027 5028 5029 5030 5031 5032
        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 */
5033
    if (virNodeDeviceGetParentHost(&driver->devs,
5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052
                                   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;
}

5053 5054

/* Domain event implementations */
5055
static int
5056 5057 5058 5059
testDomainEventRegister(virConnectPtr conn,
                        virConnectDomainEventCallback callback,
                        void *opaque,
                        virFreeCallback freecb)
5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071
{
    testConnPtr driver = conn->privateData;
    int ret;

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

    return ret;
}

5072

5073
static int
5074 5075
testDomainEventDeregister(virConnectPtr conn,
                          virConnectDomainEventCallback callback)
5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091
{
    testConnPtr driver = conn->privateData;
    int ret;

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

    return ret;
}

5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132

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

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

    return ret;
}

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

    testDriverLock(driver);
    if (driver->domainEventDispatching)
        ret = virDomainEventCallbackListMarkDeleteID(conn, driver->domainEventCallbacks,
                                                     callbackID);
    else
        ret = virDomainEventCallbackListRemoveID(conn, driver->domainEventCallbacks,
                                                 callbackID);
    testDriverUnlock(driver);

    return ret;
}


5133 5134
static void testDomainEventDispatchFunc(virConnectPtr conn,
                                        virDomainEventPtr event,
5135
                                        virConnectDomainEventGenericCallback cb,
5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192
                                        void *cbopaque,
                                        void *opaque)
{
    testConnPtr driver = opaque;

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

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

    testDriverLock(driver);

    driver->domainEventDispatching = 1;

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

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

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

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


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

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

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

5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206
static virDrvOpenStatus testSecretOpen(virConnectPtr conn,
                                       virConnectAuthPtr auth ATTRIBUTE_UNUSED,
                                       int flags ATTRIBUTE_UNUSED) {
    if (STRNEQ(conn->driver->name, "Test"))
        return VIR_DRV_OPEN_DECLINED;

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

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

5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223

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

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

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

5224 5225 5226 5227 5228
static virDriver testDriver = {
    VIR_DRV_TEST,
    "Test",
    testOpen, /* open */
    testClose, /* close */
5229
    NULL, /* supports_feature */
5230 5231
    NULL, /* type */
    testGetVersion, /* version */
5232
    NULL, /* libvirtVersion (impl. in libvirt.c) */
5233
    virGetHostname, /* getHostname */
5234 5235 5236 5237 5238
    testGetMaxVCPUs, /* getMaxVcpus */
    testNodeGetInfo, /* nodeGetInfo */
    testGetCapabilities, /* getCapabilities */
    testListDomains, /* listDomains */
    testNumOfDomains, /* numOfDomains */
5239
    testDomainCreateXML, /* domainCreateXML */
5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256
    testLookupDomainByID, /* domainLookupByID */
    testLookupDomainByUUID, /* domainLookupByUUID */
    testLookupDomainByName, /* domainLookupByName */
    testPauseDomain, /* domainSuspend */
    testResumeDomain, /* domainResume */
    testShutdownDomain, /* domainShutdown */
    testRebootDomain, /* domainReboot */
    testDestroyDomain, /* domainDestroy */
    testGetOSType, /* domainGetOSType */
    testGetMaxMemory, /* domainGetMaxMemory */
    testSetMaxMemory, /* domainSetMaxMemory */
    testSetMemory, /* domainSetMemory */
    testGetDomainInfo, /* domainGetInfo */
    testDomainSave, /* domainSave */
    testDomainRestore, /* domainRestore */
    testDomainCoreDump, /* domainCoreDump */
    testSetVcpus, /* domainSetVcpus */
C
Cole Robinson 已提交
5257
    testDomainPinVcpu, /* domainPinVcpu */
C
Cole Robinson 已提交
5258
    testDomainGetVcpus, /* domainGetVcpus */
C
Cole Robinson 已提交
5259
    testDomainGetMaxVcpus, /* domainGetMaxVcpus */
5260 5261
    NULL, /* domainGetSecurityLabel */
    NULL, /* nodeGetSecurityModel */
5262
    testDomainDumpXML, /* domainDumpXML */
5263 5264
    NULL, /* domainXMLFromNative */
    NULL, /* domainXMLToNative */
5265 5266 5267 5268 5269 5270
    testListDefinedDomains, /* listDefinedDomains */
    testNumOfDefinedDomains, /* numOfDefinedDomains */
    testDomainCreate, /* domainCreate */
    testDomainDefineXML, /* domainDefineXML */
    testDomainUndefine, /* domainUndefine */
    NULL, /* domainAttachDevice */
5271
    NULL, /* domainAttachDeviceFlags */
5272
    NULL, /* domainDetachDevice */
5273
    NULL, /* domainDetachDeviceFlags */
5274
    NULL, /* domainUpdateDeviceFlags */
5275 5276 5277 5278 5279
    testDomainGetAutostart, /* domainGetAutostart */
    testDomainSetAutostart, /* domainSetAutostart */
    testDomainGetSchedulerType, /* domainGetSchedulerType */
    testDomainGetSchedulerParams, /* domainGetSchedulerParameters */
    testDomainSetSchedulerParams, /* domainSetSchedulerParameters */
5280 5281 5282
    NULL, /* domainMigratePrepare */
    NULL, /* domainMigratePerform */
    NULL, /* domainMigrateFinish */
5283 5284
    testDomainBlockStats, /* domainBlockStats */
    testDomainInterfaceStats, /* domainInterfaceStats */
5285
    NULL, /* domainMemoryStats */
R
Richard W.M. Jones 已提交
5286
    NULL, /* domainBlockPeek */
R
Richard W.M. Jones 已提交
5287
    NULL, /* domainMemoryPeek */
5288
    testNodeGetCellsFreeMemory, /* nodeGetCellsFreeMemory */
5289
    NULL, /* getFreeMemory */
5290 5291
    testDomainEventRegister, /* domainEventRegister */
    testDomainEventDeregister, /* domainEventDeregister */
D
Daniel Veillard 已提交
5292 5293
    NULL, /* domainMigratePrepare2 */
    NULL, /* domainMigrateFinish2 */
5294
    NULL, /* nodeDeviceDettach */
5295 5296
    NULL, /* nodeDeviceReAttach */
    NULL, /* nodeDeviceReset */
C
Chris Lalancette 已提交
5297
    NULL, /* domainMigratePrepareTunnel */
5298 5299 5300 5301
    testIsEncrypted, /* isEncrypted */
    testIsSecure, /* isEncrypted */
    testDomainIsActive, /* domainIsActive */
    testDomainIsPersistent, /* domainIsPersistent */
J
Jiri Denemark 已提交
5302
    NULL, /* cpuCompare */
5303
    NULL, /* cpuBaseline */
5304
    NULL, /* domainGetJobInfo */
5305
    NULL, /* domainAbortJob */
5306
    NULL, /* domainMigrateSetMaxDowntime */
5307 5308
    testDomainEventRegisterAny, /* domainEventRegisterAny */
    testDomainEventDeregisterAny, /* domainEventDeregisterAny */
5309 5310 5311
    NULL, /* domainManagedSave */
    NULL, /* domainHasManagedSaveImage */
    NULL, /* domainManagedSaveRemove */
5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332
};

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

L
Laine Stump 已提交
5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351
static virInterfaceDriver testInterfaceDriver = {
    "Test",                     /* name */
    testOpenInterface,          /* open */
    testCloseInterface,         /* close */
    testNumOfInterfaces,        /* numOfInterfaces */
    testListInterfaces,         /* listInterfaces */
    testNumOfDefinedInterfaces, /* numOfDefinedInterfaces */
    testListDefinedInterfaces,  /* listDefinedInterfaces */
    testLookupInterfaceByName,  /* interfaceLookupByName */
    testLookupInterfaceByMACString, /* interfaceLookupByMACString */
    testInterfaceGetXMLDesc,    /* interfaceGetXMLDesc */
    testInterfaceDefineXML,     /* interfaceDefineXML */
    testInterfaceUndefine,      /* interfaceUndefine */
    testInterfaceCreate,        /* interfaceCreate */
    testInterfaceDestroy,       /* interfaceDestroy */
5352
    testInterfaceIsActive,      /* interfaceIsActive */
L
Laine Stump 已提交
5353 5354 5355
};


5356 5357 5358 5359
static virStorageDriver testStorageDriver = {
    .name = "Test",
    .open = testStorageOpen,
    .close = testStorageClose,
C
Cole Robinson 已提交
5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387

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

    .volLookupByName = testStorageVolumeLookupByName,
    .volLookupByKey = testStorageVolumeLookupByKey,
    .volLookupByPath = testStorageVolumeLookupByPath,
    .volCreateXML = testStorageVolumeCreateXML,
5388
    .volCreateXMLFrom = testStorageVolumeCreateXMLFrom,
C
Cole Robinson 已提交
5389 5390 5391 5392
    .volDelete = testStorageVolumeDelete,
    .volGetInfo = testStorageVolumeGetInfo,
    .volGetXMLDesc = testStorageVolumeGetXMLDesc,
    .volGetPath = testStorageVolumeGetPath,
5393 5394
    .poolIsActive = testStoragePoolIsActive,
    .poolIsPersistent = testStoragePoolIsPersistent,
5395 5396
};

5397 5398 5399 5400
static virDeviceMonitor testDevMonitor = {
    .name = "Test",
    .open = testDevMonOpen,
    .close = testDevMonClose,
5401 5402 5403 5404 5405 5406 5407 5408

    .numOfDevices = testNodeNumOfDevices,
    .listDevices = testNodeListDevices,
    .deviceLookupByName = testNodeDeviceLookupByName,
    .deviceDumpXML = testNodeDeviceDumpXML,
    .deviceGetParent = testNodeDeviceGetParent,
    .deviceNumOfCaps = testNodeDeviceNumOfCaps,
    .deviceListCaps = testNodeDeviceListCaps,
5409 5410
    .deviceCreateXML = testNodeDeviceCreateXML,
    .deviceDestroy = testNodeDeviceDestroy,
5411 5412
};

5413 5414 5415 5416 5417
static virSecretDriver testSecretDriver = {
    .name = "Test",
    .open = testSecretOpen,
    .close = testSecretClose,
};
5418 5419


5420 5421 5422 5423 5424 5425
static virNWFilterDriver testNWFilterDriver = {
    .name = "Test",
    .open = testNWFilterOpen,
    .close = testNWFilterClose,
};

5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437
/**
 * testRegister:
 *
 * Registers the test driver
 */
int
testRegister(void)
{
    if (virRegisterDriver(&testDriver) < 0)
        return -1;
    if (virRegisterNetworkDriver(&testNetworkDriver) < 0)
        return -1;
L
Laine Stump 已提交
5438 5439
    if (virRegisterInterfaceDriver(&testInterfaceDriver) < 0)
        return -1;
5440 5441
    if (virRegisterStorageDriver(&testStorageDriver) < 0)
        return -1;
5442 5443
    if (virRegisterDeviceMonitor(&testDevMonitor) < 0)
        return -1;
5444 5445
    if (virRegisterSecretDriver(&testSecretDriver) < 0)
        return -1;
5446 5447
    if (virRegisterNWFilterDriver(&testNWFilterDriver) < 0)
        return -1;
5448

5449 5450
    return 0;
}