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

24
#include <config.h>
25

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

34 35

#include "virterror_internal.h"
36
#include "datatypes.h"
37
#include "test_driver.h"
38
#include "buf.h"
39
#include "util.h"
40
#include "uuid.h"
41
#include "capabilities.h"
42
#include "memory.h"
43
#include "network_conf.h"
L
Laine Stump 已提交
44
#include "interface_conf.h"
45
#include "domain_conf.h"
46 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
#include "files.h"
54

55 56
#define VIR_FROM_THIS VIR_FROM_TEST

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

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

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

78
struct _testConn {
79
    virMutex lock;
80

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

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

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

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

113

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

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


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

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

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


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

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

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

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

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

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

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

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

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

212
    return caps;
213

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

220

221 222 223 224 225 226 227 228 229 230
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>";
231 232


233 234 235 236 237 238 239 240 241 242 243
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>";
244

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

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

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

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

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

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

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

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

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

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

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

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

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

360
    return 0;
361 362
}

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

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

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

455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470
static void
testDomainShutdownState(virDomainPtr domain,
                        virDomainObjPtr privdom)
{
    if (privdom->newDef) {
        virDomainDefFree(privdom->def);
        privdom->def = privdom->newDef;
        privdom->newDef = NULL;
    }

    privdom->state = VIR_DOMAIN_SHUTOFF;
    privdom->def->id = -1;
    if (domain)
        domain->id = -1;
}

471
/* Set up domain runtime state */
472 473 474 475 476
static int
testDomainStartState(virConnectPtr conn,
                     virDomainObjPtr dom)
{
    testConnPtr privconn = conn->privateData;
477
    int ret = -1;
478

479 480 481
    if (testDomainUpdateVCPUs(conn, dom, dom->def->vcpus, 1) < 0)
        goto cleanup;

482 483 484
    dom->state = VIR_DOMAIN_RUNNING;
    dom->def->id = privconn->nextDomID++;

485
    if (virDomainObjSetDefTransient(privconn->caps, dom, false) < 0) {
486 487 488
        goto cleanup;
    }

489 490
    ret = 0;
cleanup:
491 492
    if (ret < 0)
        testDomainShutdownState(NULL, dom);
493
    return ret;
494
}
495

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

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

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

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

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

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

536
    /* Numa setup */
537 538 539 540 541 542 543 544 545
    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;
    }

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

    privconn->nextDomID = 1;

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

562
    domobj->persistent = 1;
563 564 565 566 567
    if (testDomainStartState(conn, domobj) < 0) {
        virDomainObjUnlock(domobj);
        goto error;
    }

568
    virDomainObjUnlock(domobj);
569

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

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

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

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

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

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

615
    testDriverUnlock(privconn);
616

617 618 619
    return VIR_DRV_OPEN_SUCCESS;

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


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

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

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

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

676
    ret = virXPathNodeSet(vol_xpath, ctxt, &vols);
677 678 679 680 681 682 683 684 685 686 687
    VIR_FREE(vol_xpath);
    if (ret < 0) {
        goto error;
    }

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

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

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

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

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

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

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

768
    testDriverLock(privconn);
769
    conn->privateData = privconn;
770

771 772 773
    if (virDomainObjListInit(&privconn->domains) < 0)
        goto error;

774 775
    if (!(privconn->caps = testBuildCapabilities(conn)))
        goto error;
776

777
    if (!(xml = virXMLParseFile(file))) {
778
        goto error;
779 780
    }

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

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

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

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

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

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

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

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

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

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

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

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

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

907
        dom->persistent = 1;
908 909 910 911 912
        if (testDomainStartState(conn, dom) < 0) {
            virDomainObjUnlock(dom);
            goto error;
        }

913
        virDomainObjUnlock(dom);
914
    }
915
    VIR_FREE(domains);
916

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

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

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

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

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

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

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

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

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

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

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

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

1037
    ret = virXPathNodeSet("/node/device", ctxt, &devs);
1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
    if (ret < 0) {
        goto error;
    }
    for (i = 0 ; i < ret ; i++) {
        virNodeDeviceDefPtr def;
        virNodeDeviceObjPtr dev;
        char *relFile = virXMLPropString(devs[i], "file");

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

            if (!absFile) {
1051
                testError(VIR_ERR_INTERNAL_ERROR, "%s",
1052 1053 1054 1055
                          _("resolving device filename"));
                goto error;
            }

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


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

1077
    return (0);
1078 1079

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

1096

1097
static virDrvOpenStatus testOpen(virConnectPtr conn,
1098
                    virConnectAuthPtr auth ATTRIBUTE_UNUSED,
1099
                    int flags ATTRIBUTE_UNUSED)
1100
{
1101
    int ret;
1102
    testConnPtr privconn;
1103

1104
    if (!conn->uri)
1105
        return VIR_DRV_OPEN_DECLINED;
1106

1107
    if (!conn->uri->scheme || STRNEQ(conn->uri->scheme, "test"))
1108
        return VIR_DRV_OPEN_DECLINED;
1109

1110
    /* Remote driver should handle these. */
1111
    if (conn->uri->server)
1112 1113
        return VIR_DRV_OPEN_DECLINED;

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

1123
    if (STREQ(conn->uri->path, "/default"))
1124 1125
        ret = testOpenDefault(conn);
    else
1126
        ret = testOpenFromFile(conn,
1127
                               conn->uri->path);
1128

1129 1130 1131 1132 1133
    if (ret != VIR_DRV_OPEN_SUCCESS)
        return ret;

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

1135 1136 1137 1138 1139
    privconn->domainEventState = virDomainEventStateNew(testDomainEventFlush,
                                                        privconn,
                                                        NULL,
                                                        false);
    if (!privconn->domainEventState) {
1140
        testDriverUnlock(privconn);
1141 1142
        testClose(conn);
        return VIR_DRV_OPEN_ERROR;
1143 1144
    }

1145 1146 1147
    testDriverUnlock(privconn);

    return VIR_DRV_OPEN_SUCCESS;
1148 1149
}

1150
static int testClose(virConnectPtr conn)
1151
{
1152
    testConnPtr privconn = conn->privateData;
1153
    testDriverLock(privconn);
1154
    virCapabilitiesFree(privconn->caps);
1155
    virDomainObjListDeinit(&privconn->domains);
D
Daniel P. Berrange 已提交
1156
    virNodeDeviceObjListFree(&privconn->devs);
1157
    virNetworkObjListFree(&privconn->networks);
L
Laine Stump 已提交
1158
    virInterfaceObjListFree(&privconn->ifaces);
C
Cole Robinson 已提交
1159
    virStoragePoolObjListFree(&privconn->pools);
1160
    virDomainEventStateFree(privconn->domainEventState);
1161

1162
    testDriverUnlock(privconn);
1163
    virMutexDestroy(&privconn->lock);
1164

1165
    VIR_FREE (privconn);
1166
    conn->privateData = NULL;
1167
    return 0;
1168 1169
}

1170 1171
static int testGetVersion(virConnectPtr conn ATTRIBUTE_UNUSED,
                          unsigned long *hvVer)
1172
{
1173 1174
    *hvVer = 2;
    return (0);
1175 1176
}

1177 1178 1179 1180 1181 1182 1183 1184 1185 1186
static int testIsSecure(virConnectPtr conn ATTRIBUTE_UNUSED)
{
    return 1;
}

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

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

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

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

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

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

1223
    return count;
1224 1225
}

1226 1227 1228 1229 1230 1231 1232 1233 1234 1235
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) {
1236
        testError(VIR_ERR_NO_DOMAIN, NULL);
1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256
        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) {
1257
        testError(VIR_ERR_NO_DOMAIN, NULL);
1258 1259 1260 1261 1262 1263 1264 1265 1266 1267
        goto cleanup;
    }
    ret = obj->persistent;

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

1268 1269 1270 1271 1272
static int testDomainIsUpdated(virDomainPtr dom ATTRIBUTE_UNUSED)
{
    return 0;
}

1273
static virDomainPtr
1274
testDomainCreateXML(virConnectPtr conn, const char *xml,
1275
                      unsigned int flags)
1276
{
1277
    testConnPtr privconn = conn->privateData;
1278
    virDomainPtr ret = NULL;
1279
    virDomainDefPtr def;
1280
    virDomainObjPtr dom = NULL;
1281
    virDomainEventPtr event = NULL;
1282

1283 1284
    virCheckFlags(0, NULL);

1285
    testDriverLock(privconn);
1286
    if ((def = virDomainDefParseString(privconn->caps, xml,
1287
                                       VIR_DOMAIN_XML_INACTIVE)) == NULL)
1288
        goto cleanup;
1289

1290 1291 1292
    if (virDomainObjIsDuplicate(&privconn->domains, def, 1) < 0)
        goto cleanup;

1293
    if (testDomainGenerateIfnames(def) < 0)
1294
        goto cleanup;
1295
    if (!(dom = virDomainAssignDef(privconn->caps,
1296
                                   &privconn->domains, def, false)))
1297 1298
        goto cleanup;
    def = NULL;
1299 1300 1301

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

1303 1304 1305 1306
    event = virDomainEventNewFromObj(dom,
                                     VIR_DOMAIN_EVENT_STARTED,
                                     VIR_DOMAIN_EVENT_STARTED_BOOTED);

1307
    ret = virGetDomain(conn, dom->def->name, dom->def->uuid);
1308
    if (ret)
1309
        ret->id = dom->def->id;
1310 1311

cleanup:
1312 1313
    if (dom)
        virDomainObjUnlock(dom);
1314 1315
    if (event)
        testDomainEventQueue(privconn, event);
1316
    virDomainDefFree(def);
1317
    testDriverUnlock(privconn);
1318
    return ret;
1319 1320 1321
}


1322 1323
static virDomainPtr testLookupDomainByID(virConnectPtr conn,
                                         int id)
1324
{
1325
    testConnPtr privconn = conn->privateData;
1326 1327
    virDomainPtr ret = NULL;
    virDomainObjPtr dom;
1328

1329 1330 1331 1332 1333
    testDriverLock(privconn);
    dom = virDomainFindByID(&privconn->domains, id);
    testDriverUnlock(privconn);

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

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

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

1348 1349
static virDomainPtr testLookupDomainByUUID(virConnectPtr conn,
                                           const unsigned char *uuid)
1350
{
1351
    testConnPtr privconn = conn->privateData;
1352 1353
    virDomainPtr ret = NULL;
    virDomainObjPtr dom ;
1354

1355 1356 1357 1358 1359
    testDriverLock(privconn);
    dom = virDomainFindByUUID(&privconn->domains, uuid);
    testDriverUnlock(privconn);

    if (dom == NULL) {
1360
        testError(VIR_ERR_NO_DOMAIN, NULL);
1361
        goto cleanup;
1362
    }
1363

1364
    ret = virGetDomain(conn, dom->def->name, dom->def->uuid);
1365 1366 1367 1368
    if (ret)
        ret->id = dom->def->id;

cleanup:
1369 1370
    if (dom)
        virDomainObjUnlock(dom);
1371
    return ret;
1372 1373
}

1374 1375
static virDomainPtr testLookupDomainByName(virConnectPtr conn,
                                           const char *name)
1376
{
1377
    testConnPtr privconn = conn->privateData;
1378 1379
    virDomainPtr ret = NULL;
    virDomainObjPtr dom;
1380

1381 1382 1383 1384 1385
    testDriverLock(privconn);
    dom = virDomainFindByName(&privconn->domains, name);
    testDriverUnlock(privconn);

    if (dom == NULL) {
1386
        testError(VIR_ERR_NO_DOMAIN, NULL);
1387
        goto cleanup;
1388
    }
1389

1390
    ret = virGetDomain(conn, dom->def->name, dom->def->uuid);
1391 1392 1393 1394
    if (ret)
        ret->id = dom->def->id;

cleanup:
1395 1396
    if (dom)
        virDomainObjUnlock(dom);
1397
    return ret;
1398 1399
}

1400 1401 1402
static int testListDomains (virConnectPtr conn,
                            int *ids,
                            int maxids)
1403
{
1404
    testConnPtr privconn = conn->privateData;
1405
    int n;
1406

1407
    testDriverLock(privconn);
1408
    n = virDomainObjListGetActiveIDs(&privconn->domains, ids, maxids);
1409
    testDriverUnlock(privconn);
1410

1411
    return n;
1412 1413
}

1414
static int testDestroyDomain (virDomainPtr domain)
1415
{
1416 1417
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
1418
    virDomainEventPtr event = NULL;
1419
    int ret = -1;
1420

1421
    testDriverLock(privconn);
1422 1423 1424 1425
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);

    if (privdom == NULL) {
1426
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
1427
        goto cleanup;
1428
    }
1429

1430
    testDomainShutdownState(domain, privdom);
1431 1432 1433
    event = virDomainEventNewFromObj(privdom,
                                     VIR_DOMAIN_EVENT_STOPPED,
                                     VIR_DOMAIN_EVENT_STOPPED_DESTROYED);
1434

1435 1436 1437
    if (!privdom->persistent) {
        virDomainRemoveInactive(&privconn->domains,
                                privdom);
1438
        privdom = NULL;
1439
    }
1440 1441 1442

    ret = 0;
cleanup:
1443 1444
    if (privdom)
        virDomainObjUnlock(privdom);
1445 1446
    if (event)
        testDomainEventQueue(privconn, event);
1447
    testDriverUnlock(privconn);
1448
    return ret;
1449 1450
}

1451
static int testResumeDomain (virDomainPtr domain)
1452
{
1453 1454
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
1455
    virDomainEventPtr event = NULL;
1456
    int ret = -1;
1457

1458
    testDriverLock(privconn);
1459 1460
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
1461
    testDriverUnlock(privconn);
1462 1463

    if (privdom == NULL) {
1464
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
1465
        goto cleanup;
1466
    }
1467

1468
    if (privdom->state != VIR_DOMAIN_PAUSED) {
1469
        testError(VIR_ERR_INTERNAL_ERROR, _("domain '%s' not paused"),
1470
                  domain->name);
1471
        goto cleanup;
1472
    }
1473

1474
    privdom->state = VIR_DOMAIN_RUNNING;
1475 1476 1477
    event = virDomainEventNewFromObj(privdom,
                                     VIR_DOMAIN_EVENT_RESUMED,
                                     VIR_DOMAIN_EVENT_RESUMED_UNPAUSED);
1478 1479 1480
    ret = 0;

cleanup:
1481 1482
    if (privdom)
        virDomainObjUnlock(privdom);
1483 1484 1485 1486 1487
    if (event) {
        testDriverLock(privconn);
        testDomainEventQueue(privconn, event);
        testDriverUnlock(privconn);
    }
1488
    return ret;
1489 1490
}

1491
static int testPauseDomain (virDomainPtr domain)
1492
{
1493 1494
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
1495
    virDomainEventPtr event = NULL;
1496
    int ret = -1;
1497

1498
    testDriverLock(privconn);
1499 1500
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
1501
    testDriverUnlock(privconn);
1502 1503

    if (privdom == NULL) {
1504
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
1505
        goto cleanup;
1506
    }
1507

1508 1509
    if (privdom->state == VIR_DOMAIN_SHUTOFF ||
        privdom->state == VIR_DOMAIN_PAUSED) {
1510
        testError(VIR_ERR_INTERNAL_ERROR, _("domain '%s' not running"),
1511
                  domain->name);
1512
        goto cleanup;
1513
    }
1514

1515
    privdom->state = VIR_DOMAIN_PAUSED;
1516 1517 1518
    event = virDomainEventNewFromObj(privdom,
                                     VIR_DOMAIN_EVENT_SUSPENDED,
                                     VIR_DOMAIN_EVENT_SUSPENDED_PAUSED);
1519 1520 1521
    ret = 0;

cleanup:
1522 1523
    if (privdom)
        virDomainObjUnlock(privdom);
1524 1525 1526 1527 1528 1529

    if (event) {
        testDriverLock(privconn);
        testDomainEventQueue(privconn, event);
        testDriverUnlock(privconn);
    }
1530
    return ret;
1531 1532
}

1533
static int testShutdownDomain (virDomainPtr domain)
1534
{
1535 1536
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
1537
    virDomainEventPtr event = NULL;
1538
    int ret = -1;
1539

1540
    testDriverLock(privconn);
1541 1542 1543 1544
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);

    if (privdom == NULL) {
1545
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
1546
        goto cleanup;
1547
    }
1548

1549
    if (privdom->state == VIR_DOMAIN_SHUTOFF) {
1550
        testError(VIR_ERR_INTERNAL_ERROR,
1551
                  _("domain '%s' not running"), domain->name);
1552
        goto cleanup;
1553
    }
1554

1555
    testDomainShutdownState(domain, privdom);
1556 1557 1558
    event = virDomainEventNewFromObj(privdom,
                                     VIR_DOMAIN_EVENT_STOPPED,
                                     VIR_DOMAIN_EVENT_STOPPED_SHUTDOWN);
1559

1560 1561 1562 1563 1564
    if (!privdom->persistent) {
        virDomainRemoveInactive(&privconn->domains,
                                privdom);
        privdom = NULL;
    }
1565

1566
    ret = 0;
1567
cleanup:
1568 1569
    if (privdom)
        virDomainObjUnlock(privdom);
1570 1571
    if (event)
        testDomainEventQueue(privconn, event);
1572
    testDriverUnlock(privconn);
1573
    return ret;
1574 1575 1576
}

/* Similar behaviour as shutdown */
1577 1578
static int testRebootDomain (virDomainPtr domain,
                             unsigned int action ATTRIBUTE_UNUSED)
1579
{
1580 1581
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
1582
    virDomainEventPtr event = NULL;
1583
    int ret = -1;
1584

1585
    testDriverLock(privconn);
1586 1587 1588 1589
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);

    if (privdom == NULL) {
1590
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
1591
        goto cleanup;
1592
    }
1593

1594 1595 1596 1597
    privdom->state = VIR_DOMAIN_SHUTDOWN;
    switch (privdom->def->onReboot) {
    case VIR_DOMAIN_LIFECYCLE_DESTROY:
        privdom->state = VIR_DOMAIN_SHUTOFF;
1598 1599
        break;

1600 1601
    case VIR_DOMAIN_LIFECYCLE_RESTART:
        privdom->state = VIR_DOMAIN_RUNNING;
1602 1603
        break;

1604 1605
    case VIR_DOMAIN_LIFECYCLE_PRESERVE:
        privdom->state = VIR_DOMAIN_SHUTOFF;
1606 1607
        break;

1608 1609
    case VIR_DOMAIN_LIFECYCLE_RESTART_RENAME:
        privdom->state = VIR_DOMAIN_RUNNING;
1610
        break;
1611

1612
    default:
1613
        privdom->state = VIR_DOMAIN_SHUTOFF;
1614 1615
        break;
    }
1616

1617
    if (privdom->state == VIR_DOMAIN_SHUTOFF) {
1618
        testDomainShutdownState(domain, privdom);
1619 1620 1621
        event = virDomainEventNewFromObj(privdom,
                                         VIR_DOMAIN_EVENT_STOPPED,
                                         VIR_DOMAIN_EVENT_STOPPED_SHUTDOWN);
1622

1623 1624 1625 1626 1627
        if (!privdom->persistent) {
            virDomainRemoveInactive(&privconn->domains,
                                    privdom);
            privdom = NULL;
        }
1628 1629
    }

1630 1631
    ret = 0;
cleanup:
1632 1633
    if (privdom)
        virDomainObjUnlock(privdom);
1634 1635
    if (event)
        testDomainEventQueue(privconn, event);
1636
    testDriverUnlock(privconn);
1637
    return ret;
1638 1639
}

1640 1641
static int testGetDomainInfo (virDomainPtr domain,
                              virDomainInfoPtr info)
1642
{
1643
    testConnPtr privconn = domain->conn->privateData;
1644
    struct timeval tv;
1645
    virDomainObjPtr privdom;
1646
    int ret = -1;
1647

1648
    testDriverLock(privconn);
1649 1650
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
1651
    testDriverUnlock(privconn);
1652 1653

    if (privdom == NULL) {
1654
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
1655
        goto cleanup;
1656
    }
1657 1658

    if (gettimeofday(&tv, NULL) < 0) {
1659
        testError(VIR_ERR_INTERNAL_ERROR,
J
Jim Meyering 已提交
1660
                  "%s", _("getting time of day"));
1661
        goto cleanup;
1662 1663
    }

1664
    info->state = privdom->state;
1665 1666
    info->memory = privdom->def->mem.cur_balloon;
    info->maxMem = privdom->def->mem.max_balloon;
1667 1668
    info->nrVirtCpu = privdom->def->vcpus;
    info->cpuTime = ((tv.tv_sec * 1000ll * 1000ll  * 1000ll) + (tv.tv_usec * 1000ll));
1669 1670 1671
    ret = 0;

cleanup:
1672 1673
    if (privdom)
        virDomainObjUnlock(privdom);
1674
    return ret;
1675 1676
}

1677 1678 1679 1680 1681
#define TEST_SAVE_MAGIC "TestGuestMagic"

static int testDomainSave(virDomainPtr domain,
                          const char *path)
{
1682
    testConnPtr privconn = domain->conn->privateData;
1683 1684 1685
    char *xml = NULL;
    int fd = -1;
    int len;
1686
    virDomainObjPtr privdom;
1687
    virDomainEventPtr event = NULL;
1688
    int ret = -1;
1689

1690
    testDriverLock(privconn);
1691 1692 1693 1694
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);

    if (privdom == NULL) {
1695
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
1696
        goto cleanup;
1697
    }
1698

1699
    xml = virDomainDefFormat(privdom->def,
C
Cole Robinson 已提交
1700 1701
                             VIR_DOMAIN_XML_SECURE);

1702
    if (xml == NULL) {
1703
        virReportSystemError(errno,
1704 1705
                             _("saving domain '%s' failed to allocate space for metadata"),
                             domain->name);
1706
        goto cleanup;
1707
    }
1708 1709

    if ((fd = open(path, O_CREAT|O_TRUNC|O_WRONLY, S_IRUSR|S_IWUSR)) < 0) {
1710
        virReportSystemError(errno,
1711 1712
                             _("saving domain '%s' to '%s': open failed"),
                             domain->name, path);
1713
        goto cleanup;
1714
    }
1715
    len = strlen(xml);
1716
    if (safewrite(fd, TEST_SAVE_MAGIC, sizeof(TEST_SAVE_MAGIC)) < 0) {
1717
        virReportSystemError(errno,
1718 1719
                             _("saving domain '%s' to '%s': write failed"),
                             domain->name, path);
1720
        goto cleanup;
1721
    }
1722
    if (safewrite(fd, (char*)&len, sizeof(len)) < 0) {
1723
        virReportSystemError(errno,
1724 1725
                             _("saving domain '%s' to '%s': write failed"),
                             domain->name, path);
1726
        goto cleanup;
1727
    }
1728
    if (safewrite(fd, xml, len) < 0) {
1729
        virReportSystemError(errno,
1730 1731
                             _("saving domain '%s' to '%s': write failed"),
                             domain->name, path);
1732
        goto cleanup;
1733
    }
1734

1735
    if (VIR_CLOSE(fd) < 0) {
1736
        virReportSystemError(errno,
1737 1738
                             _("saving domain '%s' to '%s': write failed"),
                             domain->name, path);
1739
        goto cleanup;
1740
    }
1741 1742
    fd = -1;

1743
    testDomainShutdownState(domain, privdom);
1744 1745 1746
    event = virDomainEventNewFromObj(privdom,
                                     VIR_DOMAIN_EVENT_STOPPED,
                                     VIR_DOMAIN_EVENT_STOPPED_SAVED);
1747

1748 1749 1750
    if (!privdom->persistent) {
        virDomainRemoveInactive(&privconn->domains,
                                privdom);
1751
        privdom = NULL;
1752
    }
1753

1754
    ret = 0;
1755 1756 1757 1758 1759 1760 1761
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) {
1762
        VIR_FORCE_CLOSE(fd);
1763 1764
        unlink(path);
    }
1765 1766
    if (privdom)
        virDomainObjUnlock(privdom);
1767 1768
    if (event)
        testDomainEventQueue(privconn, event);
1769
    testDriverUnlock(privconn);
1770
    return ret;
1771 1772
}

1773 1774
static int testDomainRestore(virConnectPtr conn,
                             const char *path)
1775
{
1776
    testConnPtr privconn = conn->privateData;
1777
    char *xml = NULL;
1778
    char magic[15];
1779 1780 1781
    int fd = -1;
    int len;
    virDomainDefPtr def = NULL;
1782
    virDomainObjPtr dom = NULL;
1783
    virDomainEventPtr event = NULL;
1784
    int ret = -1;
1785 1786

    if ((fd = open(path, O_RDONLY)) < 0) {
1787
        virReportSystemError(errno,
1788 1789
                             _("cannot read domain image '%s'"),
                             path);
1790
        goto cleanup;
1791
    }
1792
    if (saferead(fd, magic, sizeof(magic)) != sizeof(magic)) {
1793
        virReportSystemError(errno,
1794 1795
                             _("incomplete save header in '%s'"),
                             path);
1796
        goto cleanup;
1797
    }
1798
    if (memcmp(magic, TEST_SAVE_MAGIC, sizeof(magic))) {
1799
        testError(VIR_ERR_INTERNAL_ERROR,
J
Jim Meyering 已提交
1800
                  "%s", _("mismatched header magic"));
1801
        goto cleanup;
1802
    }
1803
    if (saferead(fd, (char*)&len, sizeof(len)) != sizeof(len)) {
1804
        virReportSystemError(errno,
1805 1806
                             _("failed to read metadata length in '%s'"),
                             path);
1807
        goto cleanup;
1808 1809
    }
    if (len < 1 || len > 8192) {
1810
        testError(VIR_ERR_INTERNAL_ERROR,
J
Jim Meyering 已提交
1811
                  "%s", _("length of metadata out of range"));
1812
        goto cleanup;
1813
    }
1814
    if (VIR_ALLOC_N(xml, len+1) < 0) {
1815
        virReportOOMError();
1816
        goto cleanup;
1817
    }
1818
    if (saferead(fd, xml, len) != len) {
1819
        virReportSystemError(errno,
1820
                             _("incomplete metdata in '%s'"), path);
1821
        goto cleanup;
1822 1823
    }
    xml[len] = '\0';
1824

1825
    testDriverLock(privconn);
1826
    def = virDomainDefParseString(privconn->caps, xml,
1827
                                  VIR_DOMAIN_XML_INACTIVE);
1828
    if (!def)
1829
        goto cleanup;
1830

1831 1832 1833
    if (virDomainObjIsDuplicate(&privconn->domains, def, 1) < 0)
        goto cleanup;

1834
    if (testDomainGenerateIfnames(def) < 0)
1835
        goto cleanup;
1836
    if (!(dom = virDomainAssignDef(privconn->caps,
1837
                                   &privconn->domains, def, true)))
1838 1839
        goto cleanup;
    def = NULL;
1840

1841 1842 1843
    if (testDomainStartState(conn, dom) < 0)
        goto cleanup;

1844 1845 1846
    event = virDomainEventNewFromObj(dom,
                                     VIR_DOMAIN_EVENT_STARTED,
                                     VIR_DOMAIN_EVENT_STARTED_RESTORED);
1847
    ret = 0;
1848 1849 1850 1851

cleanup:
    virDomainDefFree(def);
    VIR_FREE(xml);
1852
    VIR_FORCE_CLOSE(fd);
1853 1854
    if (dom)
        virDomainObjUnlock(dom);
1855 1856
    if (event)
        testDomainEventQueue(privconn, event);
1857
    testDriverUnlock(privconn);
1858
    return ret;
1859 1860
}

1861 1862 1863
static int testDomainCoreDump(virDomainPtr domain,
                              const char *to,
                              int flags ATTRIBUTE_UNUSED)
1864
{
1865
    testConnPtr privconn = domain->conn->privateData;
1866
    int fd = -1;
1867
    virDomainObjPtr privdom;
1868
    virDomainEventPtr event = NULL;
1869
    int ret = -1;
1870

1871
    testDriverLock(privconn);
1872 1873 1874 1875
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);

    if (privdom == NULL) {
1876
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
1877
        goto cleanup;
1878
    }
1879 1880

    if ((fd = open(to, O_CREAT|O_TRUNC|O_WRONLY, S_IRUSR|S_IWUSR)) < 0) {
1881
        virReportSystemError(errno,
1882 1883
                             _("domain '%s' coredump: failed to open %s"),
                             domain->name, to);
1884
        goto cleanup;
1885
    }
1886
    if (safewrite(fd, TEST_SAVE_MAGIC, sizeof(TEST_SAVE_MAGIC)) < 0) {
1887
        virReportSystemError(errno,
1888 1889
                             _("domain '%s' coredump: failed to write header to %s"),
                             domain->name, to);
1890
        goto cleanup;
1891
    }
1892
    if (VIR_CLOSE(fd) < 0) {
1893
        virReportSystemError(errno,
1894 1895
                             _("domain '%s' coredump: write failed: %s"),
                             domain->name, to);
1896
        goto cleanup;
1897
    }
1898

1899 1900 1901 1902 1903 1904 1905 1906 1907 1908
    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;
        }
1909
    }
1910

1911
    ret = 0;
1912
cleanup:
1913
    VIR_FORCE_CLOSE(fd);
1914 1915
    if (privdom)
        virDomainObjUnlock(privdom);
1916 1917
    if (event)
        testDomainEventQueue(privconn, event);
1918
    testDriverUnlock(privconn);
1919
    return ret;
1920 1921
}

1922
static char *testGetOSType(virDomainPtr dom ATTRIBUTE_UNUSED) {
1923 1924
    char *ret = strdup("linux");
    if (!ret)
1925
        virReportOOMError();
1926
    return ret;
1927 1928 1929
}

static unsigned long testGetMaxMemory(virDomainPtr domain) {
1930 1931
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
1932
    unsigned long ret = 0;
1933

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

    if (privdom == NULL) {
1940
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
1941
        goto cleanup;
1942
    }
1943

1944
    ret = privdom->def->mem.max_balloon;
1945 1946

cleanup:
1947 1948
    if (privdom)
        virDomainObjUnlock(privdom);
1949
    return ret;
1950 1951 1952 1953 1954
}

static int testSetMaxMemory(virDomainPtr domain,
                            unsigned long memory)
{
1955 1956
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
1957
    int ret = -1;
1958

1959
    testDriverLock(privconn);
1960 1961
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
1962
    testDriverUnlock(privconn);
1963 1964

    if (privdom == NULL) {
1965
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
1966
        goto cleanup;
1967
    }
1968 1969

    /* XXX validate not over host memory wrt to other domains */
1970
    privdom->def->mem.max_balloon = memory;
1971 1972 1973
    ret = 0;

cleanup:
1974 1975
    if (privdom)
        virDomainObjUnlock(privdom);
1976
    return ret;
1977 1978
}

1979 1980 1981
static int testSetMemory(virDomainPtr domain,
                         unsigned long memory)
{
1982 1983
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
1984
    int ret = -1;
1985

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

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

1996
    if (memory > privdom->def->mem.max_balloon) {
1997
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
1998
        goto cleanup;
1999
    }
2000

2001
    privdom->def->mem.cur_balloon = memory;
2002 2003 2004
    ret = 0;

cleanup:
2005 2006
    if (privdom)
        virDomainObjUnlock(privdom);
2007
    return ret;
2008 2009
}

2010 2011
static int
testDomainGetVcpusFlags(virDomainPtr domain, unsigned int flags)
C
Cole Robinson 已提交
2012
{
2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr vm;
    virDomainDefPtr def;
    int ret = -1;

    virCheckFlags(VIR_DOMAIN_VCPU_LIVE |
                  VIR_DOMAIN_VCPU_CONFIG |
                  VIR_DOMAIN_VCPU_MAXIMUM, -1);

    /* Exactly one of LIVE or CONFIG must be set.  */
    if (!(flags & VIR_DOMAIN_VCPU_LIVE) == !(flags & VIR_DOMAIN_VCPU_CONFIG)) {
        testError(VIR_ERR_INVALID_ARG,
                  _("invalid flag combination: (0x%x)"), flags);
2026 2027 2028
        return -1;
    }

2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057
    testDriverLock(privconn);
    vm = virDomainFindByUUID(&privconn->domains, domain->uuid);
    testDriverUnlock(privconn);

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

    if (flags & VIR_DOMAIN_VCPU_LIVE) {
        if (!virDomainObjIsActive(vm)) {
            testError(VIR_ERR_OPERATION_INVALID, "%s",
                      _("domain not active"));
            goto cleanup;
        }
        def = vm->def;
    } else {
        def = vm->newDef ? vm->newDef : vm->def;
    }

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

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

2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070
static int
testDomainGetMaxVcpus(virDomainPtr domain)
{
    return testDomainGetVcpusFlags(domain, (VIR_DOMAIN_VCPU_LIVE |
                                            VIR_DOMAIN_VCPU_MAXIMUM));
}

static int
testDomainSetVcpusFlags(virDomainPtr domain, unsigned int nrCpus,
                        unsigned int flags)
{
2071
    testConnPtr privconn = domain->conn->privateData;
2072
    virDomainObjPtr privdom = NULL;
2073
    virDomainDefPtr persistentDef;
C
Cole Robinson 已提交
2074 2075
    int ret = -1, maxvcpus;

2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091
    virCheckFlags(VIR_DOMAIN_VCPU_LIVE |
                  VIR_DOMAIN_VCPU_CONFIG |
                  VIR_DOMAIN_VCPU_MAXIMUM, -1);

    /* At least one of LIVE or CONFIG must be set.  MAXIMUM cannot be
     * mixed with LIVE.  */
    if ((flags & (VIR_DOMAIN_VCPU_LIVE | VIR_DOMAIN_VCPU_CONFIG)) == 0 ||
        (flags & (VIR_DOMAIN_VCPU_MAXIMUM | VIR_DOMAIN_VCPU_LIVE)) ==
         (VIR_DOMAIN_VCPU_MAXIMUM | VIR_DOMAIN_VCPU_LIVE)) {
        testError(VIR_ERR_INVALID_ARG,
                  _("invalid flag combination: (0x%x)"), flags);
        return -1;
    }
    if (!nrCpus || (maxvcpus = testGetMaxVCPUs(domain->conn, NULL)) < nrCpus) {
        testError(VIR_ERR_INVALID_ARG,
                  _("argument out of range: %d"), nrCpus);
2092 2093
        return -1;
    }
2094

2095
    testDriverLock(privconn);
2096
    privdom = virDomainFindByUUID(&privconn->domains, domain->uuid);
2097 2098 2099
    testDriverUnlock(privconn);

    if (privdom == NULL) {
2100
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2101
        goto cleanup;
2102
    }
2103

2104
    if (!virDomainObjIsActive(privdom) && (flags & VIR_DOMAIN_VCPU_LIVE)) {
2105
        testError(VIR_ERR_OPERATION_INVALID,
C
Cole Robinson 已提交
2106 2107 2108 2109
                  "%s", _("cannot hotplug vcpus for an inactive domain"));
        goto cleanup;
    }

2110 2111
    /* We allow more cpus in guest than host, but not more than the
     * domain's starting limit.  */
C
Cole Robinson 已提交
2112 2113
    if (!(flags & (VIR_DOMAIN_VCPU_MAXIMUM)) &&
        privdom->def->maxvcpus < maxvcpus)
2114
        maxvcpus = privdom->def->maxvcpus;
C
Cole Robinson 已提交
2115

C
Cole Robinson 已提交
2116
    if (nrCpus > maxvcpus) {
2117
        testError(VIR_ERR_INVALID_ARG,
C
Cole Robinson 已提交
2118 2119
                  "requested cpu amount exceeds maximum (%d > %d)",
                  nrCpus, maxvcpus);
2120
        goto cleanup;
2121
    }
2122

2123 2124 2125 2126
    if (!(persistentDef = virDomainObjGetPersistentDef(privconn->caps,
                                                       privdom)))
        goto cleanup;

2127 2128
    switch (flags) {
    case VIR_DOMAIN_VCPU_MAXIMUM | VIR_DOMAIN_VCPU_CONFIG:
2129 2130 2131
        persistentDef->maxvcpus = nrCpus;
        if (nrCpus < persistentDef->vcpus)
            persistentDef->vcpus = nrCpus;
2132 2133
        ret = 0;
        break;
2134

2135
    case VIR_DOMAIN_VCPU_CONFIG:
2136
        persistentDef->vcpus = nrCpus;
2137 2138 2139 2140 2141 2142 2143 2144 2145
        ret = 0;
        break;

    case VIR_DOMAIN_VCPU_LIVE:
        ret = testDomainUpdateVCPUs(domain->conn, privdom, nrCpus, 0);
        break;

    case VIR_DOMAIN_VCPU_LIVE | VIR_DOMAIN_VCPU_CONFIG:
        ret = testDomainUpdateVCPUs(domain->conn, privdom, nrCpus, 0);
2146 2147 2148
        if (ret == 0) {
            persistentDef->vcpus = nrCpus;
        }
2149 2150
        break;
    }
2151 2152

cleanup:
2153 2154
    if (privdom)
        virDomainObjUnlock(privdom);
2155
    return ret;
2156 2157
}

2158 2159 2160 2161 2162 2163
static int
testSetVcpus(virDomainPtr domain, unsigned int nrCpus)
{
    return testDomainSetVcpusFlags(domain, nrCpus, VIR_DOMAIN_VCPU_LIVE);
}

C
Cole Robinson 已提交
2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182
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) {
2183
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
C
Cole Robinson 已提交
2184 2185 2186 2187
        goto cleanup;
    }

    if (!virDomainObjIsActive(privdom)) {
2188
        testError(VIR_ERR_OPERATION_INVALID,
C
Cole Robinson 已提交
2189 2190 2191 2192 2193 2194 2195
                  "%s",_("cannot list vcpus for an inactive domain"));
        goto cleanup;
    }

    privdomdata = privdom->privateData;

    if (gettimeofday(&tv, NULL) < 0) {
2196
        virReportSystemError(errno,
C
Cole Robinson 已提交
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 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251
                             "%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 已提交
2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268
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) {
2269
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
C
Cole Robinson 已提交
2270 2271 2272 2273
        goto cleanup;
    }

    if (!virDomainObjIsActive(privdom)) {
2274
        testError(VIR_ERR_OPERATION_INVALID,
C
Cole Robinson 已提交
2275 2276 2277 2278 2279
                  "%s",_("cannot pin vcpus on an inactive domain"));
        goto cleanup;
    }

    if (vcpu > privdom->def->vcpus) {
2280
        testError(VIR_ERR_INVALID_ARG, "%s",
C
Cole Robinson 已提交
2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308
                  _("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;
}

2309
static char *testDomainGetXMLDesc(virDomainPtr domain, int flags)
2310
{
2311
    testConnPtr privconn = domain->conn->privateData;
2312
    virDomainDefPtr def;
2313
    virDomainObjPtr privdom;
2314 2315
    char *ret = NULL;

2316 2317 2318 2319 2320 2321
    testDriverLock(privconn);
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
    testDriverUnlock(privconn);

    if (privdom == NULL) {
2322
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2323
        goto cleanup;
2324
    }
2325

2326 2327
    def = (flags & VIR_DOMAIN_XML_INACTIVE) &&
        privdom->newDef ? privdom->newDef : privdom->def;
2328

2329
    ret = virDomainDefFormat(def,
2330 2331 2332
                             flags);

cleanup:
2333 2334
    if (privdom)
        virDomainObjUnlock(privdom);
2335
    return ret;
2336
}
2337

2338
static int testNumOfDefinedDomains(virConnectPtr conn) {
2339
    testConnPtr privconn = conn->privateData;
2340
    int count;
2341

2342
    testDriverLock(privconn);
2343
    count = virDomainObjListNumOfDomains(&privconn->domains, 0);
2344
    testDriverUnlock(privconn);
2345

2346
    return count;
2347 2348
}

2349 2350 2351
static int testListDefinedDomains(virConnectPtr conn,
                                  char **const names,
                                  int maxnames) {
2352

2353
    testConnPtr privconn = conn->privateData;
2354
    int n;
2355

2356
    testDriverLock(privconn);
2357
    memset(names, 0, sizeof(*names)*maxnames);
2358
    n = virDomainObjListGetInactiveNames(&privconn->domains, names, maxnames);
2359
    testDriverUnlock(privconn);
2360

2361
    return n;
2362 2363
}

2364
static virDomainPtr testDomainDefineXML(virConnectPtr conn,
2365
                                        const char *xml) {
2366
    testConnPtr privconn = conn->privateData;
2367
    virDomainPtr ret = NULL;
2368
    virDomainDefPtr def;
2369
    virDomainObjPtr dom = NULL;
2370
    virDomainEventPtr event = NULL;
2371
    int dupVM;
2372

2373
    testDriverLock(privconn);
2374
    if ((def = virDomainDefParseString(privconn->caps, xml,
2375
                                       VIR_DOMAIN_XML_INACTIVE)) == NULL)
2376
        goto cleanup;
2377

2378 2379 2380
    if ((dupVM = virDomainObjIsDuplicate(&privconn->domains, def, 0)) < 0)
        goto cleanup;

2381
    if (testDomainGenerateIfnames(def) < 0)
2382
        goto cleanup;
2383
    if (!(dom = virDomainAssignDef(privconn->caps,
2384
                                   &privconn->domains, def, false)))
2385
        goto cleanup;
2386
    def = NULL;
2387
    dom->persistent = 1;
2388

2389 2390
    event = virDomainEventNewFromObj(dom,
                                     VIR_DOMAIN_EVENT_DEFINED,
2391 2392 2393
                                     !dupVM ?
                                     VIR_DOMAIN_EVENT_DEFINED_ADDED :
                                     VIR_DOMAIN_EVENT_DEFINED_UPDATED);
2394

2395
    ret = virGetDomain(conn, dom->def->name, dom->def->uuid);
2396
    if (ret)
2397
        ret->id = dom->def->id;
2398 2399 2400

cleanup:
    virDomainDefFree(def);
2401 2402
    if (dom)
        virDomainObjUnlock(dom);
2403 2404
    if (event)
        testDomainEventQueue(privconn, event);
2405
    testDriverUnlock(privconn);
2406
    return ret;
2407 2408
}

2409 2410 2411
static int testNodeGetCellsFreeMemory(virConnectPtr conn,
                                      unsigned long long *freemems,
                                      int startCell, int maxCells) {
2412
    testConnPtr privconn = conn->privateData;
2413
    int i, j;
2414
    int ret = -1;
2415

2416
    testDriverLock(privconn);
2417
    if (startCell > privconn->numCells) {
2418
        testError(VIR_ERR_INVALID_ARG,
J
Jim Meyering 已提交
2419
                  "%s", _("Range exceeds available cells"));
2420
        goto cleanup;
2421 2422 2423 2424 2425 2426 2427
    }

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

2430
cleanup:
2431
    testDriverUnlock(privconn);
2432
    return ret;
2433 2434 2435
}


2436
static int testDomainCreateWithFlags(virDomainPtr domain, unsigned int flags) {
2437 2438
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
2439
    virDomainEventPtr event = NULL;
2440
    int ret = -1;
2441

2442 2443
    virCheckFlags(0, -1);

2444
    testDriverLock(privconn);
2445 2446 2447 2448
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);

    if (privdom == NULL) {
2449
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2450
        goto cleanup;
2451
    }
2452

2453
    if (privdom->state != VIR_DOMAIN_SHUTOFF) {
2454
        testError(VIR_ERR_INTERNAL_ERROR,
2455
                  _("Domain '%s' is already running"), domain->name);
2456
        goto cleanup;
2457 2458
    }

2459 2460 2461 2462
    if (testDomainStartState(domain->conn, privdom) < 0)
        goto cleanup;
    domain->id = privdom->def->id;

2463 2464 2465
    event = virDomainEventNewFromObj(privdom,
                                     VIR_DOMAIN_EVENT_STARTED,
                                     VIR_DOMAIN_EVENT_STARTED_BOOTED);
2466
    ret = 0;
2467

2468
cleanup:
2469 2470
    if (privdom)
        virDomainObjUnlock(privdom);
2471 2472
    if (event)
        testDomainEventQueue(privconn, event);
2473
    testDriverUnlock(privconn);
2474
    return ret;
2475 2476
}

2477 2478 2479 2480
static int testDomainCreate(virDomainPtr domain) {
    return testDomainCreateWithFlags(domain, 0);
}

2481
static int testDomainUndefine(virDomainPtr domain) {
2482 2483
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
2484
    virDomainEventPtr event = NULL;
2485
    int ret = -1;
2486

2487
    testDriverLock(privconn);
2488 2489 2490 2491
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);

    if (privdom == NULL) {
2492
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2493
        goto cleanup;
2494
    }
2495

2496
    if (privdom->state != VIR_DOMAIN_SHUTOFF) {
2497
        testError(VIR_ERR_INTERNAL_ERROR,
2498
                  _("Domain '%s' is still running"), domain->name);
2499
        goto cleanup;
2500 2501
    }

2502
    privdom->state = VIR_DOMAIN_SHUTOFF;
2503 2504 2505
    event = virDomainEventNewFromObj(privdom,
                                     VIR_DOMAIN_EVENT_UNDEFINED,
                                     VIR_DOMAIN_EVENT_UNDEFINED_REMOVED);
2506 2507
    virDomainRemoveInactive(&privconn->domains,
                            privdom);
2508
    privdom = NULL;
2509
    ret = 0;
2510

2511
cleanup:
2512 2513
    if (privdom)
        virDomainObjUnlock(privdom);
2514 2515
    if (event)
        testDomainEventQueue(privconn, event);
2516
    testDriverUnlock(privconn);
2517
    return ret;
2518 2519
}

2520 2521 2522
static int testDomainGetAutostart(virDomainPtr domain,
                                  int *autostart)
{
2523 2524
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
2525
    int ret = -1;
2526

2527
    testDriverLock(privconn);
2528 2529
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
2530
    testDriverUnlock(privconn);
2531 2532

    if (privdom == NULL) {
2533
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2534
        goto cleanup;
2535 2536
    }

2537
    *autostart = privdom->autostart;
2538 2539 2540
    ret = 0;

cleanup:
2541 2542
    if (privdom)
        virDomainObjUnlock(privdom);
2543
    return ret;
2544 2545 2546 2547 2548 2549
}


static int testDomainSetAutostart(virDomainPtr domain,
                                  int autostart)
{
2550 2551
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
2552
    int ret = -1;
2553

2554
    testDriverLock(privconn);
2555 2556
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
2557
    testDriverUnlock(privconn);
2558 2559

    if (privdom == NULL) {
2560
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2561
        goto cleanup;
2562 2563
    }

2564
    privdom->autostart = autostart ? 1 : 0;
2565 2566 2567
    ret = 0;

cleanup:
2568 2569
    if (privdom)
        virDomainObjUnlock(privdom);
2570
    return ret;
2571
}
2572

2573
static char *testDomainGetSchedulerType(virDomainPtr domain ATTRIBUTE_UNUSED,
2574 2575
                                        int *nparams)
{
2576 2577
    char *type = NULL;

2578 2579
    *nparams = 1;
    type = strdup("fair");
2580
    if (!type)
2581
        virReportOOMError();
2582

2583 2584 2585 2586 2587 2588 2589
    return type;
}

static int testDomainGetSchedulerParams(virDomainPtr domain,
                                        virSchedParameterPtr params,
                                        int *nparams)
{
2590 2591
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
2592
    int ret = -1;
2593

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

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

2604
    if (*nparams != 1) {
2605
        testError(VIR_ERR_INVALID_ARG, "nparams");
2606
        goto cleanup;
2607
    }
2608 2609
    strcpy(params[0].field, "weight");
    params[0].type = VIR_DOMAIN_SCHED_FIELD_UINT;
2610 2611 2612
    /* XXX */
    /*params[0].value.ui = privdom->weight;*/
    params[0].value.ui = 50;
2613 2614 2615
    ret = 0;

cleanup:
2616 2617
    if (privdom)
        virDomainObjUnlock(privdom);
2618
    return ret;
2619
}
2620 2621


2622 2623 2624 2625
static int testDomainSetSchedulerParams(virDomainPtr domain,
                                        virSchedParameterPtr params,
                                        int nparams)
{
2626 2627
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
2628
    int ret = -1;
2629

2630
    testDriverLock(privconn);
2631 2632
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
2633
    testDriverUnlock(privconn);
2634 2635

    if (privdom == NULL) {
2636
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2637
        goto cleanup;
2638 2639
    }

2640
    if (nparams != 1) {
2641
        testError(VIR_ERR_INVALID_ARG, "nparams");
2642
        goto cleanup;
2643
    }
2644
    if (STRNEQ(params[0].field, "weight")) {
2645
        testError(VIR_ERR_INVALID_ARG, "field");
2646
        goto cleanup;
2647 2648
    }
    if (params[0].type != VIR_DOMAIN_SCHED_FIELD_UINT) {
2649
        testError(VIR_ERR_INVALID_ARG, "type");
2650
        goto cleanup;
2651
    }
2652 2653
    /* XXX */
    /*privdom->weight = params[0].value.ui;*/
2654 2655 2656
    ret = 0;

cleanup:
2657 2658
    if (privdom)
        virDomainObjUnlock(privdom);
2659
    return ret;
2660 2661
}

2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677
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) {
2678
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689
        goto error;
    }

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

    if (!found) {
2690
        testError(VIR_ERR_INVALID_ARG,
2691 2692 2693 2694 2695
                  _("invalid path: %s"), path);
        goto error;
    }

    if (gettimeofday(&tv, NULL) < 0) {
2696
        virReportSystemError(errno,
2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731
                             "%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) {
2732
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744
        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) {
2745
        testError(VIR_ERR_INVALID_ARG,
2746 2747 2748 2749 2750
                  _("invalid path, '%s' is not a known interface"), path);
        goto error;
    }

    if (gettimeofday(&tv, NULL) < 0) {
2751
        virReportSystemError(errno,
2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773
                             "%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;
}

2774
static virDrvOpenStatus testOpenNetwork(virConnectPtr conn,
2775
                                        virConnectAuthPtr auth ATTRIBUTE_UNUSED,
2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792
                                        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)
{
2793 2794
    testConnPtr privconn = conn->privateData;
    virNetworkObjPtr net;
2795
    virNetworkPtr ret = NULL;
2796

2797 2798 2799 2800 2801
    testDriverLock(privconn);
    net = virNetworkFindByUUID(&privconn->networks, uuid);
    testDriverUnlock(privconn);

    if (net == NULL) {
2802
        testError(VIR_ERR_NO_NETWORK, NULL);
2803
        goto cleanup;
2804 2805
    }

2806 2807 2808
    ret = virGetNetwork(conn, net->def->name, net->def->uuid);

cleanup:
2809 2810
    if (net)
        virNetworkObjUnlock(net);
2811
    return ret;
2812
}
2813

2814
static virNetworkPtr testLookupNetworkByName(virConnectPtr conn,
2815
                                             const char *name)
2816
{
2817
    testConnPtr privconn = conn->privateData;
2818 2819
    virNetworkObjPtr net;
    virNetworkPtr ret = NULL;
2820

2821 2822 2823 2824 2825
    testDriverLock(privconn);
    net = virNetworkFindByName(&privconn->networks, name);
    testDriverUnlock(privconn);

    if (net == NULL) {
2826
        testError(VIR_ERR_NO_NETWORK, NULL);
2827
        goto cleanup;
2828 2829
    }

2830 2831 2832
    ret = virGetNetwork(conn, net->def->name, net->def->uuid);

cleanup:
2833 2834
    if (net)
        virNetworkObjUnlock(net);
2835
    return ret;
2836 2837 2838 2839
}


static int testNumNetworks(virConnectPtr conn) {
2840
    testConnPtr privconn = conn->privateData;
2841
    int numActive = 0, i;
2842

2843 2844 2845
    testDriverLock(privconn);
    for (i = 0 ; i < privconn->networks.count ; i++) {
        virNetworkObjLock(privconn->networks.objs[i]);
D
Daniel P. Berrange 已提交
2846
        if (virNetworkObjIsActive(privconn->networks.objs[i]))
2847
            numActive++;
2848 2849 2850
        virNetworkObjUnlock(privconn->networks.objs[i]);
    }
    testDriverUnlock(privconn);
2851

2852
    return numActive;
2853 2854 2855
}

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

2859
    testDriverLock(privconn);
2860
    memset(names, 0, sizeof(*names)*nnames);
2861 2862
    for (i = 0 ; i < privconn->networks.count && n < nnames ; i++) {
        virNetworkObjLock(privconn->networks.objs[i]);
D
Daniel P. Berrange 已提交
2863
        if (virNetworkObjIsActive(privconn->networks.objs[i]) &&
2864 2865
            !(names[n++] = strdup(privconn->networks.objs[i]->def->name))) {
            virNetworkObjUnlock(privconn->networks.objs[i]);
2866
            goto no_memory;
2867 2868 2869 2870
        }
        virNetworkObjUnlock(privconn->networks.objs[i]);
    }
    testDriverUnlock(privconn);
2871

2872 2873 2874
    return n;

no_memory:
2875
    virReportOOMError();
2876 2877
    for (n = 0 ; n < nnames ; n++)
        VIR_FREE(names[n]);
2878
    testDriverUnlock(privconn);
2879
    return -1;
2880 2881 2882
}

static int testNumDefinedNetworks(virConnectPtr conn) {
2883
    testConnPtr privconn = conn->privateData;
2884
    int numInactive = 0, i;
2885

2886 2887 2888
    testDriverLock(privconn);
    for (i = 0 ; i < privconn->networks.count ; i++) {
        virNetworkObjLock(privconn->networks.objs[i]);
D
Daniel P. Berrange 已提交
2889
        if (!virNetworkObjIsActive(privconn->networks.objs[i]))
2890
            numInactive++;
2891 2892 2893
        virNetworkObjUnlock(privconn->networks.objs[i]);
    }
    testDriverUnlock(privconn);
2894

2895
    return numInactive;
2896 2897 2898
}

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

2902
    testDriverLock(privconn);
2903
    memset(names, 0, sizeof(*names)*nnames);
2904 2905
    for (i = 0 ; i < privconn->networks.count && n < nnames ; i++) {
        virNetworkObjLock(privconn->networks.objs[i]);
D
Daniel P. Berrange 已提交
2906
        if (!virNetworkObjIsActive(privconn->networks.objs[i]) &&
2907 2908
            !(names[n++] = strdup(privconn->networks.objs[i]->def->name))) {
            virNetworkObjUnlock(privconn->networks.objs[i]);
2909
            goto no_memory;
2910 2911 2912 2913
        }
        virNetworkObjUnlock(privconn->networks.objs[i]);
    }
    testDriverUnlock(privconn);
2914

2915 2916 2917
    return n;

no_memory:
2918
    virReportOOMError();
2919 2920
    for (n = 0 ; n < nnames ; n++)
        VIR_FREE(names[n]);
2921
    testDriverUnlock(privconn);
2922
    return -1;
2923 2924
}

2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935

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) {
2936
        testError(VIR_ERR_NO_NETWORK, NULL);
2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956
        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) {
2957
        testError(VIR_ERR_NO_NETWORK, NULL);
2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968
        goto cleanup;
    }
    ret = obj->persistent;

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


2969
static virNetworkPtr testNetworkCreate(virConnectPtr conn, const char *xml) {
2970
    testConnPtr privconn = conn->privateData;
2971
    virNetworkDefPtr def;
2972
    virNetworkObjPtr net = NULL;
2973
    virNetworkPtr ret = NULL;
2974

2975
    testDriverLock(privconn);
2976
    if ((def = virNetworkDefParseString(xml)) == NULL)
2977
        goto cleanup;
2978

2979
    if ((net = virNetworkAssignDef(&privconn->networks, def)) == NULL)
2980 2981
        goto cleanup;
    def = NULL;
2982
    net->active = 1;
2983

2984
    ret = virGetNetwork(conn, net->def->name, net->def->uuid);
2985

2986 2987
cleanup:
    virNetworkDefFree(def);
2988 2989 2990
    if (net)
        virNetworkObjUnlock(net);
    testDriverUnlock(privconn);
2991
    return ret;
2992 2993 2994
}

static virNetworkPtr testNetworkDefine(virConnectPtr conn, const char *xml) {
2995
    testConnPtr privconn = conn->privateData;
2996
    virNetworkDefPtr def;
2997
    virNetworkObjPtr net = NULL;
2998
    virNetworkPtr ret = NULL;
2999

3000
    testDriverLock(privconn);
3001
    if ((def = virNetworkDefParseString(xml)) == NULL)
3002
        goto cleanup;
3003

3004
    if ((net = virNetworkAssignDef(&privconn->networks, def)) == NULL)
3005 3006
        goto cleanup;
    def = NULL;
3007
    net->persistent = 1;
3008

3009
    ret = virGetNetwork(conn, net->def->name, net->def->uuid);
3010 3011 3012

cleanup:
    virNetworkDefFree(def);
3013 3014 3015
    if (net)
        virNetworkObjUnlock(net);
    testDriverUnlock(privconn);
3016
    return ret;
3017 3018 3019
}

static int testNetworkUndefine(virNetworkPtr network) {
3020 3021
    testConnPtr privconn = network->conn->privateData;
    virNetworkObjPtr privnet;
3022
    int ret = -1;
3023

3024
    testDriverLock(privconn);
3025 3026 3027 3028
    privnet = virNetworkFindByName(&privconn->networks,
                                   network->name);

    if (privnet == NULL) {
3029
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3030
        goto cleanup;
3031
    }
3032

D
Daniel P. Berrange 已提交
3033
    if (virNetworkObjIsActive(privnet)) {
3034
        testError(VIR_ERR_OPERATION_INVALID,
3035
                  _("Network '%s' is still running"), network->name);
3036
        goto cleanup;
3037 3038
    }

3039 3040
    virNetworkRemoveInactive(&privconn->networks,
                             privnet);
3041
    privnet = NULL;
3042
    ret = 0;
3043

3044
cleanup:
3045 3046 3047
    if (privnet)
        virNetworkObjUnlock(privnet);
    testDriverUnlock(privconn);
3048
    return ret;
3049 3050 3051
}

static int testNetworkStart(virNetworkPtr network) {
3052 3053
    testConnPtr privconn = network->conn->privateData;
    virNetworkObjPtr privnet;
3054
    int ret = -1;
3055

3056
    testDriverLock(privconn);
3057 3058
    privnet = virNetworkFindByName(&privconn->networks,
                                   network->name);
3059
    testDriverUnlock(privconn);
3060 3061

    if (privnet == NULL) {
3062
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3063
        goto cleanup;
3064
    }
3065

D
Daniel P. Berrange 已提交
3066
    if (virNetworkObjIsActive(privnet)) {
3067
        testError(VIR_ERR_OPERATION_INVALID,
3068
                  _("Network '%s' is already running"), network->name);
3069
        goto cleanup;
3070 3071
    }

3072
    privnet->active = 1;
3073
    ret = 0;
3074

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

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

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

    if (privnet == NULL) {
3091
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3092
        goto cleanup;
3093
    }
3094

3095 3096 3097 3098
    privnet->active = 0;
    if (!privnet->persistent) {
        virNetworkRemoveInactive(&privconn->networks,
                                 privnet);
3099
        privnet = NULL;
3100
    }
3101 3102 3103
    ret = 0;

cleanup:
3104 3105 3106
    if (privnet)
        virNetworkObjUnlock(privnet);
    testDriverUnlock(privconn);
3107
    return ret;
3108 3109
}

3110
static char *testNetworkGetXMLDesc(virNetworkPtr network, int flags ATTRIBUTE_UNUSED) {
3111 3112
    testConnPtr privconn = network->conn->privateData;
    virNetworkObjPtr privnet;
3113
    char *ret = NULL;
3114

3115
    testDriverLock(privconn);
3116 3117
    privnet = virNetworkFindByName(&privconn->networks,
                                   network->name);
3118
    testDriverUnlock(privconn);
3119 3120

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

3125
    ret = virNetworkDefFormat(privnet->def);
3126 3127

cleanup:
3128 3129
    if (privnet)
        virNetworkObjUnlock(privnet);
3130
    return ret;
3131 3132 3133
}

static char *testNetworkGetBridgeName(virNetworkPtr network) {
3134
    testConnPtr privconn = network->conn->privateData;
3135
    char *bridge = NULL;
3136 3137
    virNetworkObjPtr privnet;

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

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

3148
    if (!(privnet->def->bridge)) {
3149
        testError(VIR_ERR_INTERNAL_ERROR,
3150 3151 3152 3153 3154 3155
                  _("network '%s' does not have a bridge name."),
                  privnet->def->name);
        goto cleanup;
    }

    if (!(bridge = strdup(privnet->def->bridge))) {
3156
        virReportOOMError();
3157
        goto cleanup;
3158
    }
3159 3160

cleanup:
3161 3162
    if (privnet)
        virNetworkObjUnlock(privnet);
3163 3164 3165 3166 3167
    return bridge;
}

static int testNetworkGetAutostart(virNetworkPtr network,
                                   int *autostart) {
3168 3169
    testConnPtr privconn = network->conn->privateData;
    virNetworkObjPtr privnet;
3170
    int ret = -1;
3171

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

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

3182
    *autostart = privnet->autostart;
3183 3184 3185
    ret = 0;

cleanup:
3186 3187
    if (privnet)
        virNetworkObjUnlock(privnet);
3188
    return ret;
3189 3190 3191 3192
}

static int testNetworkSetAutostart(virNetworkPtr network,
                                   int autostart) {
3193 3194
    testConnPtr privconn = network->conn->privateData;
    virNetworkObjPtr privnet;
3195
    int ret = -1;
3196

3197
    testDriverLock(privconn);
3198 3199
    privnet = virNetworkFindByName(&privconn->networks,
                                   network->name);
3200
    testDriverUnlock(privconn);
3201 3202

    if (privnet == NULL) {
3203
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3204
        goto cleanup;
3205 3206
    }

3207
    privnet->autostart = autostart ? 1 : 0;
3208 3209 3210
    ret = 0;

cleanup:
3211 3212
    if (privnet)
        virNetworkObjUnlock(privnet);
3213
    return ret;
3214
}
3215

C
Cole Robinson 已提交
3216

L
Laine Stump 已提交
3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246
/*
 * 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 已提交
3247
        if (virInterfaceObjIsActive(privconn->ifaces.objs[i])) {
L
Laine Stump 已提交
3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264
            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 已提交
3265
        if (virInterfaceObjIsActive(privconn->ifaces.objs[i])) {
L
Laine Stump 已提交
3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277
            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:
3278
    virReportOOMError();
L
Laine Stump 已提交
3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292
    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 已提交
3293
        if (!virInterfaceObjIsActive(privconn->ifaces.objs[i])) {
L
Laine Stump 已提交
3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310
            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 已提交
3311
        if (!virInterfaceObjIsActive(privconn->ifaces.objs[i])) {
L
Laine Stump 已提交
3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323
            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:
3324
    virReportOOMError();
L
Laine Stump 已提交
3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342
    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) {
3343
        testError(VIR_ERR_NO_INTERFACE, NULL);
L
Laine Stump 已提交
3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367
        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) {
3368
        testError(VIR_ERR_NO_INTERFACE, NULL);
L
Laine Stump 已提交
3369 3370 3371 3372
        goto cleanup;
    }

    if (ifacect > 1) {
3373
        testError(VIR_ERR_MULTIPLE_INTERFACES, NULL);
L
Laine Stump 已提交
3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384
        goto cleanup;
    }

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

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

3385 3386 3387 3388 3389 3390 3391 3392 3393 3394
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) {
3395
        testError(VIR_ERR_NO_INTERFACE, NULL);
3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406
        goto cleanup;
    }
    ret = virInterfaceObjIsActive(obj);

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


L
Laine Stump 已提交
3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419
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) {
3420
        testError(VIR_ERR_NO_INTERFACE, __FUNCTION__);
L
Laine Stump 已提交
3421 3422 3423
        goto cleanup;
    }

3424
    ret = virInterfaceDefFormat(privinterface->def);
L
Laine Stump 已提交
3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441

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);
3442
    if ((def = virInterfaceDefParseString(xmlStr)) == NULL)
L
Laine Stump 已提交
3443 3444
        goto cleanup;

3445
    if ((iface = virInterfaceAssignDef(&privconn->ifaces, def)) == NULL)
L
Laine Stump 已提交
3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469
        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) {
3470
        testError(VIR_ERR_NO_INTERFACE, NULL);
L
Laine Stump 已提交
3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494
        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) {
3495
        testError(VIR_ERR_NO_INTERFACE, NULL);
L
Laine Stump 已提交
3496 3497 3498 3499
        goto cleanup;
    }

    if (privinterface->active != 0) {
3500
        testError(VIR_ERR_OPERATION_INVALID, NULL);
L
Laine Stump 已提交
3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525
        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) {
3526
        testError(VIR_ERR_NO_INTERFACE, NULL);
L
Laine Stump 已提交
3527 3528 3529 3530
        goto cleanup;
    }

    if (privinterface->active == 0) {
3531
        testError(VIR_ERR_OPERATION_INVALID, NULL);
L
Laine Stump 已提交
3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546
        goto cleanup;
    }

    privinterface->active = 0;
    ret = 0;

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



C
Cole Robinson 已提交
3547 3548 3549 3550
/*
 * Storage Driver routines
 */

3551

3552
static int testStoragePoolObjSetDefaults(virStoragePoolObjPtr pool) {
C
Cole Robinson 已提交
3553 3554 3555 3556 3557 3558 3559

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

    pool->configFile = strdup("\0");
    if (!pool->configFile) {
3560
        virReportOOMError();
C
Cole Robinson 已提交
3561 3562 3563 3564 3565 3566
        return -1;
    }

    return 0;
}

3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581
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;
}

3582

C
Cole Robinson 已提交
3583 3584 3585
static virStoragePoolPtr
testStoragePoolLookupByUUID(virConnectPtr conn,
                            const unsigned char *uuid) {
3586
    testConnPtr privconn = conn->privateData;
3587 3588
    virStoragePoolObjPtr pool;
    virStoragePoolPtr ret = NULL;
C
Cole Robinson 已提交
3589

3590
    testDriverLock(privconn);
3591
    pool = virStoragePoolObjFindByUUID(&privconn->pools, uuid);
3592
    testDriverUnlock(privconn);
3593 3594

    if (pool == NULL) {
3595
        testError(VIR_ERR_NO_STORAGE_POOL, NULL);
3596
        goto cleanup;
C
Cole Robinson 已提交
3597 3598
    }

3599 3600 3601
    ret = virGetStoragePool(conn, pool->def->name, pool->def->uuid);

cleanup:
3602 3603
    if (pool)
        virStoragePoolObjUnlock(pool);
3604
    return ret;
C
Cole Robinson 已提交
3605 3606 3607 3608 3609
}

static virStoragePoolPtr
testStoragePoolLookupByName(virConnectPtr conn,
                            const char *name) {
3610
    testConnPtr privconn = conn->privateData;
3611 3612
    virStoragePoolObjPtr pool;
    virStoragePoolPtr ret = NULL;
C
Cole Robinson 已提交
3613

3614
    testDriverLock(privconn);
3615
    pool = virStoragePoolObjFindByName(&privconn->pools, name);
3616
    testDriverUnlock(privconn);
3617 3618

    if (pool == NULL) {
3619
        testError(VIR_ERR_NO_STORAGE_POOL, NULL);
3620
        goto cleanup;
C
Cole Robinson 已提交
3621 3622
    }

3623 3624 3625
    ret = virGetStoragePool(conn, pool->def->name, pool->def->uuid);

cleanup:
3626 3627
    if (pool)
        virStoragePoolObjUnlock(pool);
3628
    return ret;
C
Cole Robinson 已提交
3629 3630 3631 3632 3633 3634 3635 3636 3637
}

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

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

3641
    testDriverLock(privconn);
C
Cole Robinson 已提交
3642 3643 3644
    for (i = 0 ; i < privconn->pools.count ; i++)
        if (virStoragePoolObjIsActive(privconn->pools.objs[i]))
            numActive++;
3645
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
3646 3647 3648 3649 3650 3651 3652 3653

    return numActive;
}

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

3657
    testDriverLock(privconn);
C
Cole Robinson 已提交
3658
    memset(names, 0, sizeof(*names)*nnames);
3659 3660
    for (i = 0 ; i < privconn->pools.count && n < nnames ; i++) {
        virStoragePoolObjLock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
3661
        if (virStoragePoolObjIsActive(privconn->pools.objs[i]) &&
3662 3663
            !(names[n++] = strdup(privconn->pools.objs[i]->def->name))) {
            virStoragePoolObjUnlock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
3664
            goto no_memory;
3665 3666 3667 3668
        }
        virStoragePoolObjUnlock(privconn->pools.objs[i]);
    }
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
3669 3670 3671 3672

    return n;

no_memory:
3673
    virReportOOMError();
C
Cole Robinson 已提交
3674 3675
    for (n = 0 ; n < nnames ; n++)
        VIR_FREE(names[n]);
3676
    testDriverUnlock(privconn);
3677
    return -1;
C
Cole Robinson 已提交
3678 3679 3680 3681
}

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

3685 3686 3687
    testDriverLock(privconn);
    for (i = 0 ; i < privconn->pools.count ; i++) {
        virStoragePoolObjLock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
3688 3689
        if (!virStoragePoolObjIsActive(privconn->pools.objs[i]))
            numInactive++;
3690 3691 3692
        virStoragePoolObjUnlock(privconn->pools.objs[i]);
    }
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
3693 3694 3695 3696 3697 3698 3699 3700

    return numInactive;
}

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

3704
    testDriverLock(privconn);
C
Cole Robinson 已提交
3705
    memset(names, 0, sizeof(*names)*nnames);
3706 3707
    for (i = 0 ; i < privconn->pools.count && n < nnames ; i++) {
        virStoragePoolObjLock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
3708
        if (!virStoragePoolObjIsActive(privconn->pools.objs[i]) &&
3709 3710
            !(names[n++] = strdup(privconn->pools.objs[i]->def->name))) {
            virStoragePoolObjUnlock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
3711
            goto no_memory;
3712 3713 3714 3715
        }
        virStoragePoolObjUnlock(privconn->pools.objs[i]);
    }
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
3716 3717 3718 3719

    return n;

no_memory:
3720
    virReportOOMError();
C
Cole Robinson 已提交
3721 3722
    for (n = 0 ; n < nnames ; n++)
        VIR_FREE(names[n]);
3723
    testDriverUnlock(privconn);
3724
    return -1;
C
Cole Robinson 已提交
3725 3726 3727
}


3728 3729 3730 3731 3732 3733 3734 3735 3736 3737
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) {
3738
        testError(VIR_ERR_NO_STORAGE_POOL, NULL);
3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758
        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) {
3759
        testError(VIR_ERR_NO_STORAGE_POOL, NULL);
3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771
        goto cleanup;
    }
    ret = obj->configFile ? 1 : 0;

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



C
Cole Robinson 已提交
3772
static int
3773
testStoragePoolStart(virStoragePoolPtr pool,
C
Cole Robinson 已提交
3774
                     unsigned int flags ATTRIBUTE_UNUSED) {
3775 3776
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
3777
    int ret = -1;
3778

3779
    testDriverLock(privconn);
3780 3781
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
3782
    testDriverUnlock(privconn);
3783 3784

    if (privpool == NULL) {
3785
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3786
        goto cleanup;
3787 3788
    }

3789
    if (virStoragePoolObjIsActive(privpool)) {
3790
        testError(VIR_ERR_OPERATION_INVALID,
3791 3792 3793
                  _("storage pool '%s' is already active"), pool->name);
        goto cleanup;
    }
C
Cole Robinson 已提交
3794 3795

    privpool->active = 1;
3796
    ret = 0;
C
Cole Robinson 已提交
3797

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

static char *
3805
testStorageFindPoolSources(virConnectPtr conn ATTRIBUTE_UNUSED,
3806 3807
                           const char *type,
                           const char *srcSpec,
C
Cole Robinson 已提交
3808 3809
                           unsigned int flags ATTRIBUTE_UNUSED)
{
3810 3811 3812 3813 3814 3815
    virStoragePoolSourcePtr source = NULL;
    int pool_type;
    char *ret = NULL;

    pool_type = virStoragePoolTypeFromString(type);
    if (!pool_type) {
3816
        testError(VIR_ERR_INTERNAL_ERROR,
3817 3818 3819 3820 3821
                  _("unknown storage pool type %s"), type);
        goto cleanup;
    }

    if (srcSpec) {
3822
        source = virStoragePoolDefParseSourceString(srcSpec, pool_type);
3823 3824 3825 3826 3827 3828 3829 3830 3831
        if (!source)
            goto cleanup;
    }

    switch (pool_type) {

    case VIR_STORAGE_POOL_LOGICAL:
        ret = strdup(defaultPoolSourcesLogicalXML);
        if (!ret)
3832
            virReportOOMError();
3833 3834 3835 3836
        break;

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

        if (virAsprintf(&ret, defaultPoolSourcesNetFSXML,
                        source->host.name) < 0)
3844
            virReportOOMError();
3845 3846 3847
        break;

    default:
3848
        testError(VIR_ERR_NO_SUPPORT,
3849 3850 3851 3852 3853
                  _("pool type '%s' does not support source discovery"), type);
    }

cleanup:
    virStoragePoolSourceFree(source);
3854
    VIR_FREE(source);
3855
    return ret;
C
Cole Robinson 已提交
3856 3857 3858 3859 3860 3861 3862
}


static virStoragePoolPtr
testStoragePoolCreate(virConnectPtr conn,
                      const char *xml,
                      unsigned int flags ATTRIBUTE_UNUSED) {
3863
    testConnPtr privconn = conn->privateData;
C
Cole Robinson 已提交
3864
    virStoragePoolDefPtr def;
3865
    virStoragePoolObjPtr pool = NULL;
3866
    virStoragePoolPtr ret = NULL;
C
Cole Robinson 已提交
3867

3868
    testDriverLock(privconn);
3869
    if (!(def = virStoragePoolDefParseString(xml)))
3870
        goto cleanup;
C
Cole Robinson 已提交
3871

3872 3873 3874 3875
    pool = virStoragePoolObjFindByUUID(&privconn->pools, def->uuid);
    if (!pool)
        pool = virStoragePoolObjFindByName(&privconn->pools, def->name);
    if (pool) {
3876
        testError(VIR_ERR_INTERNAL_ERROR,
C
Cole Robinson 已提交
3877
                  "%s", _("storage pool already exists"));
3878
        goto cleanup;
C
Cole Robinson 已提交
3879 3880
    }

3881
    if (!(pool = virStoragePoolObjAssignDef(&privconn->pools, def)))
3882
        goto cleanup;
3883
    def = NULL;
C
Cole Robinson 已提交
3884

3885
    if (testStoragePoolObjSetDefaults(pool) == -1) {
C
Cole Robinson 已提交
3886
        virStoragePoolObjRemove(&privconn->pools, pool);
3887 3888
        pool = NULL;
        goto cleanup;
C
Cole Robinson 已提交
3889 3890 3891
    }
    pool->active = 1;

3892 3893 3894 3895
    ret = virGetStoragePool(conn, pool->def->name, pool->def->uuid);

cleanup:
    virStoragePoolDefFree(def);
3896 3897 3898
    if (pool)
        virStoragePoolObjUnlock(pool);
    testDriverUnlock(privconn);
3899
    return ret;
C
Cole Robinson 已提交
3900 3901 3902 3903 3904 3905
}

static virStoragePoolPtr
testStoragePoolDefine(virConnectPtr conn,
                      const char *xml,
                      unsigned int flags ATTRIBUTE_UNUSED) {
3906
    testConnPtr privconn = conn->privateData;
C
Cole Robinson 已提交
3907
    virStoragePoolDefPtr def;
3908
    virStoragePoolObjPtr pool = NULL;
3909
    virStoragePoolPtr ret = NULL;
C
Cole Robinson 已提交
3910

3911
    testDriverLock(privconn);
3912
    if (!(def = virStoragePoolDefParseString(xml)))
3913
        goto cleanup;
C
Cole Robinson 已提交
3914 3915 3916 3917 3918

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

3919
    if (!(pool = virStoragePoolObjAssignDef(&privconn->pools, def)))
3920 3921
        goto cleanup;
    def = NULL;
C
Cole Robinson 已提交
3922

3923
    if (testStoragePoolObjSetDefaults(pool) == -1) {
C
Cole Robinson 已提交
3924
        virStoragePoolObjRemove(&privconn->pools, pool);
3925 3926
        pool = NULL;
        goto cleanup;
C
Cole Robinson 已提交
3927 3928
    }

3929 3930 3931 3932
    ret = virGetStoragePool(conn, pool->def->name, pool->def->uuid);

cleanup:
    virStoragePoolDefFree(def);
3933 3934 3935
    if (pool)
        virStoragePoolObjUnlock(pool);
    testDriverUnlock(privconn);
3936
    return ret;
C
Cole Robinson 已提交
3937 3938 3939
}

static int
3940 3941 3942
testStoragePoolUndefine(virStoragePoolPtr pool) {
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
3943
    int ret = -1;
3944

3945
    testDriverLock(privconn);
3946 3947 3948 3949
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);

    if (privpool == NULL) {
3950
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3951
        goto cleanup;
3952 3953
    }

3954
    if (virStoragePoolObjIsActive(privpool)) {
3955
        testError(VIR_ERR_OPERATION_INVALID,
3956 3957 3958
                  _("storage pool '%s' is already active"), pool->name);
        goto cleanup;
    }
C
Cole Robinson 已提交
3959 3960

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

3963
cleanup:
3964 3965 3966
    if (privpool)
        virStoragePoolObjUnlock(privpool);
    testDriverUnlock(privconn);
3967
    return ret;
C
Cole Robinson 已提交
3968 3969 3970
}

static int
3971
testStoragePoolBuild(virStoragePoolPtr pool,
C
Cole Robinson 已提交
3972
                     unsigned int flags ATTRIBUTE_UNUSED) {
3973 3974
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
3975
    int ret = -1;
3976

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

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

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

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


static int
4002 4003 4004
testStoragePoolDestroy(virStoragePoolPtr pool) {
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4005
    int ret = -1;
4006

4007
    testDriverLock(privconn);
4008 4009 4010 4011
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);

    if (privpool == NULL) {
4012
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4013
        goto cleanup;
4014 4015 4016
    }

    if (!virStoragePoolObjIsActive(privpool)) {
4017
        testError(VIR_ERR_OPERATION_INVALID,
4018
                  _("storage pool '%s' is not active"), pool->name);
4019
        goto cleanup;
4020
    }
C
Cole Robinson 已提交
4021 4022 4023

    privpool->active = 0;

4024
    if (privpool->configFile == NULL) {
C
Cole Robinson 已提交
4025
        virStoragePoolObjRemove(&privconn->pools, privpool);
4026 4027
        privpool = NULL;
    }
4028
    ret = 0;
C
Cole Robinson 已提交
4029

4030
cleanup:
4031 4032 4033
    if (privpool)
        virStoragePoolObjUnlock(privpool);
    testDriverUnlock(privconn);
4034
    return ret;
C
Cole Robinson 已提交
4035 4036 4037 4038
}


static int
4039
testStoragePoolDelete(virStoragePoolPtr pool,
C
Cole Robinson 已提交
4040
                      unsigned int flags ATTRIBUTE_UNUSED) {
4041 4042
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4043
    int ret = -1;
4044

4045
    testDriverLock(privconn);
4046 4047
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4048
    testDriverUnlock(privconn);
4049 4050

    if (privpool == NULL) {
4051
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4052 4053 4054 4055
        goto cleanup;
    }

    if (virStoragePoolObjIsActive(privpool)) {
4056
        testError(VIR_ERR_OPERATION_INVALID,
4057 4058
                  _("storage pool '%s' is already active"), pool->name);
        goto cleanup;
4059 4060
    }

4061
    ret = 0;
C
Cole Robinson 已提交
4062

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


static int
4071
testStoragePoolRefresh(virStoragePoolPtr pool,
C
Cole Robinson 已提交
4072
                       unsigned int flags ATTRIBUTE_UNUSED) {
4073 4074
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4075
    int ret = -1;
4076

4077
    testDriverLock(privconn);
4078 4079
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4080
    testDriverUnlock(privconn);
4081 4082

    if (privpool == NULL) {
4083
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4084
        goto cleanup;
4085 4086 4087
    }

    if (!virStoragePoolObjIsActive(privpool)) {
4088
        testError(VIR_ERR_OPERATION_INVALID,
4089
                  _("storage pool '%s' is not active"), pool->name);
4090
        goto cleanup;
4091
    }
4092
    ret = 0;
C
Cole Robinson 已提交
4093

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


static int
4102
testStoragePoolGetInfo(virStoragePoolPtr pool,
C
Cole Robinson 已提交
4103
                       virStoragePoolInfoPtr info) {
4104 4105
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4106
    int ret = -1;
4107

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

    if (privpool == NULL) {
4114
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4115
        goto cleanup;
4116
    }
C
Cole Robinson 已提交
4117 4118 4119 4120 4121 4122 4123 4124 4125

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

4128
cleanup:
4129 4130
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4131
    return ret;
C
Cole Robinson 已提交
4132 4133 4134
}

static char *
4135 4136
testStoragePoolGetXMLDesc(virStoragePoolPtr pool,
                          unsigned int flags ATTRIBUTE_UNUSED) {
4137 4138
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4139
    char *ret = NULL;
4140

4141
    testDriverLock(privconn);
4142 4143
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4144
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
4145

4146
    if (privpool == NULL) {
4147
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4148
        goto cleanup;
4149 4150
    }

4151
    ret = virStoragePoolDefFormat(privpool->def);
4152 4153

cleanup:
4154 4155
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4156
    return ret;
C
Cole Robinson 已提交
4157 4158 4159
}

static int
4160
testStoragePoolGetAutostart(virStoragePoolPtr pool,
C
Cole Robinson 已提交
4161
                            int *autostart) {
4162 4163
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4164
    int ret = -1;
4165

4166
    testDriverLock(privconn);
4167 4168
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4169
    testDriverUnlock(privconn);
4170 4171

    if (privpool == NULL) {
4172
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4173
        goto cleanup;
4174
    }
C
Cole Robinson 已提交
4175 4176 4177 4178 4179 4180

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

4183
cleanup:
4184 4185
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4186
    return ret;
C
Cole Robinson 已提交
4187 4188 4189
}

static int
4190
testStoragePoolSetAutostart(virStoragePoolPtr pool,
C
Cole Robinson 已提交
4191
                            int autostart) {
4192 4193
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4194
    int ret = -1;
4195

4196
    testDriverLock(privconn);
4197 4198
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4199
    testDriverUnlock(privconn);
4200 4201

    if (privpool == NULL) {
4202
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4203
        goto cleanup;
4204
    }
C
Cole Robinson 已提交
4205 4206

    if (!privpool->configFile) {
4207
        testError(VIR_ERR_INVALID_ARG,
C
Cole Robinson 已提交
4208
                  "%s", _("pool has no config file"));
4209
        goto cleanup;
C
Cole Robinson 已提交
4210 4211 4212 4213
    }

    autostart = (autostart != 0);
    privpool->autostart = autostart;
4214 4215 4216
    ret = 0;

cleanup:
4217 4218
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4219
    return ret;
C
Cole Robinson 已提交
4220 4221 4222 4223
}


static int
4224 4225 4226
testStoragePoolNumVolumes(virStoragePoolPtr pool) {
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4227
    int ret = -1;
4228

4229
    testDriverLock(privconn);
4230 4231
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4232
    testDriverUnlock(privconn);
4233 4234

    if (privpool == NULL) {
4235
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4236
        goto cleanup;
4237 4238 4239
    }

    if (!virStoragePoolObjIsActive(privpool)) {
4240
        testError(VIR_ERR_OPERATION_INVALID,
4241
                  _("storage pool '%s' is not active"), pool->name);
4242
        goto cleanup;
4243
    }
C
Cole Robinson 已提交
4244

4245 4246 4247
    ret = privpool->volumes.count;

cleanup:
4248 4249
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4250
    return ret;
C
Cole Robinson 已提交
4251 4252 4253
}

static int
4254
testStoragePoolListVolumes(virStoragePoolPtr pool,
C
Cole Robinson 已提交
4255 4256
                           char **const names,
                           int maxnames) {
4257 4258
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
C
Cole Robinson 已提交
4259 4260
    int i = 0, n = 0;

4261
    memset(names, 0, maxnames * sizeof(*names));
4262 4263

    testDriverLock(privconn);
4264 4265
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4266
    testDriverUnlock(privconn);
4267 4268

    if (privpool == NULL) {
4269
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4270
        goto cleanup;
4271 4272 4273 4274
    }


    if (!virStoragePoolObjIsActive(privpool)) {
4275
        testError(VIR_ERR_OPERATION_INVALID,
4276
                  _("storage pool '%s' is not active"), pool->name);
4277
        goto cleanup;
4278 4279
    }

C
Cole Robinson 已提交
4280 4281
    for (i = 0 ; i < privpool->volumes.count && n < maxnames ; i++) {
        if ((names[n++] = strdup(privpool->volumes.objs[i]->name)) == NULL) {
4282
            virReportOOMError();
C
Cole Robinson 已提交
4283 4284 4285 4286
            goto cleanup;
        }
    }

4287
    virStoragePoolObjUnlock(privpool);
C
Cole Robinson 已提交
4288 4289 4290 4291 4292 4293
    return n;

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

4294
    memset(names, 0, maxnames * sizeof(*names));
4295 4296
    if (privpool)
        virStoragePoolObjUnlock(privpool);
C
Cole Robinson 已提交
4297 4298 4299 4300 4301
    return -1;
}


static virStorageVolPtr
4302
testStorageVolumeLookupByName(virStoragePoolPtr pool,
C
Cole Robinson 已提交
4303
                              const char *name ATTRIBUTE_UNUSED) {
4304 4305 4306
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol;
4307
    virStorageVolPtr ret = NULL;
4308

4309
    testDriverLock(privconn);
4310 4311
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4312
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
4313

4314
    if (privpool == NULL) {
4315
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4316
        goto cleanup;
4317 4318 4319 4320
    }


    if (!virStoragePoolObjIsActive(privpool)) {
4321
        testError(VIR_ERR_OPERATION_INVALID,
4322
                  _("storage pool '%s' is not active"), pool->name);
4323
        goto cleanup;
4324 4325 4326 4327 4328
    }

    privvol = virStorageVolDefFindByName(privpool, name);

    if (!privvol) {
4329
        testError(VIR_ERR_NO_STORAGE_VOL,
C
Cole Robinson 已提交
4330
                  _("no storage vol with matching name '%s'"), name);
4331
        goto cleanup;
C
Cole Robinson 已提交
4332 4333
    }

4334 4335 4336 4337
    ret = virGetStorageVol(pool->conn, privpool->def->name,
                           privvol->name, privvol->key);

cleanup:
4338 4339
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4340
    return ret;
C
Cole Robinson 已提交
4341 4342 4343 4344 4345 4346
}


static virStorageVolPtr
testStorageVolumeLookupByKey(virConnectPtr conn,
                             const char *key) {
4347
    testConnPtr privconn = conn->privateData;
C
Cole Robinson 已提交
4348
    unsigned int i;
4349
    virStorageVolPtr ret = NULL;
C
Cole Robinson 已提交
4350

4351
    testDriverLock(privconn);
C
Cole Robinson 已提交
4352
    for (i = 0 ; i < privconn->pools.count ; i++) {
4353
        virStoragePoolObjLock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
4354
        if (virStoragePoolObjIsActive(privconn->pools.objs[i])) {
4355
            virStorageVolDefPtr privvol =
C
Cole Robinson 已提交
4356 4357
                virStorageVolDefFindByKey(privconn->pools.objs[i], key);

4358 4359 4360 4361 4362
            if (privvol) {
                ret = virGetStorageVol(conn,
                                       privconn->pools.objs[i]->def->name,
                                       privvol->name,
                                       privvol->key);
4363
                virStoragePoolObjUnlock(privconn->pools.objs[i]);
4364 4365
                break;
            }
C
Cole Robinson 已提交
4366
        }
4367
        virStoragePoolObjUnlock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
4368
    }
4369
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
4370

4371
    if (!ret)
4372
        testError(VIR_ERR_NO_STORAGE_VOL,
4373 4374 4375
                  _("no storage vol with matching key '%s'"), key);

    return ret;
C
Cole Robinson 已提交
4376 4377 4378 4379 4380
}

static virStorageVolPtr
testStorageVolumeLookupByPath(virConnectPtr conn,
                              const char *path) {
4381
    testConnPtr privconn = conn->privateData;
C
Cole Robinson 已提交
4382
    unsigned int i;
4383
    virStorageVolPtr ret = NULL;
C
Cole Robinson 已提交
4384

4385
    testDriverLock(privconn);
C
Cole Robinson 已提交
4386
    for (i = 0 ; i < privconn->pools.count ; i++) {
4387
        virStoragePoolObjLock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
4388
        if (virStoragePoolObjIsActive(privconn->pools.objs[i])) {
4389
            virStorageVolDefPtr privvol =
C
Cole Robinson 已提交
4390 4391
                virStorageVolDefFindByPath(privconn->pools.objs[i], path);

4392 4393 4394 4395 4396
            if (privvol) {
                ret = virGetStorageVol(conn,
                                       privconn->pools.objs[i]->def->name,
                                       privvol->name,
                                       privvol->key);
4397
                virStoragePoolObjUnlock(privconn->pools.objs[i]);
4398 4399
                break;
            }
C
Cole Robinson 已提交
4400
        }
4401
        virStoragePoolObjUnlock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
4402
    }
4403
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
4404

4405
    if (!ret)
4406
        testError(VIR_ERR_NO_STORAGE_VOL,
4407 4408 4409
                  _("no storage vol with matching path '%s'"), path);

    return ret;
C
Cole Robinson 已提交
4410 4411 4412
}

static virStorageVolPtr
4413
testStorageVolumeCreateXML(virStoragePoolPtr pool,
C
Cole Robinson 已提交
4414 4415
                           const char *xmldesc,
                           unsigned int flags ATTRIBUTE_UNUSED) {
4416 4417
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4418 4419
    virStorageVolDefPtr privvol = NULL;
    virStorageVolPtr ret = NULL;
4420

4421
    testDriverLock(privconn);
4422 4423
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4424
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
4425

4426
    if (privpool == NULL) {
4427
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4428
        goto cleanup;
4429 4430 4431
    }

    if (!virStoragePoolObjIsActive(privpool)) {
4432
        testError(VIR_ERR_OPERATION_INVALID,
4433
                  _("storage pool '%s' is not active"), pool->name);
4434
        goto cleanup;
4435
    }
C
Cole Robinson 已提交
4436

4437
    privvol = virStorageVolDefParseString(privpool->def, xmldesc);
4438
    if (privvol == NULL)
4439
        goto cleanup;
4440 4441

    if (virStorageVolDefFindByName(privpool, privvol->name)) {
4442
        testError(VIR_ERR_OPERATION_FAILED,
C
Cole Robinson 已提交
4443
                  "%s", _("storage vol already exists"));
4444
        goto cleanup;
C
Cole Robinson 已提交
4445 4446 4447
    }

    /* Make sure enough space */
4448
    if ((privpool->def->allocation + privvol->allocation) >
C
Cole Robinson 已提交
4449
         privpool->def->capacity) {
4450
        testError(VIR_ERR_INTERNAL_ERROR,
C
Cole Robinson 已提交
4451
                  _("Not enough free space in pool for volume '%s'"),
4452
                  privvol->name);
4453
        goto cleanup;
C
Cole Robinson 已提交
4454 4455 4456 4457
    }

    if (VIR_REALLOC_N(privpool->volumes.objs,
                      privpool->volumes.count+1) < 0) {
4458
        virReportOOMError();
4459
        goto cleanup;
C
Cole Robinson 已提交
4460 4461
    }

4462 4463 4464
    if (virAsprintf(&privvol->target.path, "%s/%s",
                    privpool->def->target.path,
                    privvol->name) == -1) {
4465
        virReportOOMError();
4466
        goto cleanup;
C
Cole Robinson 已提交
4467 4468
    }

4469 4470
    privvol->key = strdup(privvol->target.path);
    if (privvol->key == NULL) {
4471
        virReportOOMError();
4472
        goto cleanup;
C
Cole Robinson 已提交
4473 4474
    }

4475
    privpool->def->allocation += privvol->allocation;
C
Cole Robinson 已提交
4476 4477 4478
    privpool->def->available = (privpool->def->capacity -
                                privpool->def->allocation);

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

4481 4482
    ret = virGetStorageVol(pool->conn, privpool->def->name,
                           privvol->name, privvol->key);
4483
    privvol = NULL;
4484 4485 4486

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

4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507
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) {
4508
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4509 4510 4511 4512
        goto cleanup;
    }

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

4518
    privvol = virStorageVolDefParseString(privpool->def, xmldesc);
4519 4520 4521 4522
    if (privvol == NULL)
        goto cleanup;

    if (virStorageVolDefFindByName(privpool, privvol->name)) {
4523
        testError(VIR_ERR_OPERATION_FAILED,
4524 4525 4526 4527 4528 4529
                  "%s", _("storage vol already exists"));
        goto cleanup;
    }

    origvol = virStorageVolDefFindByName(privpool, clonevol->name);
    if (!origvol) {
4530
        testError(VIR_ERR_NO_STORAGE_VOL,
4531 4532 4533 4534 4535 4536 4537 4538
                  _("no storage vol with matching name '%s'"),
                  clonevol->name);
        goto cleanup;
    }

    /* Make sure enough space */
    if ((privpool->def->allocation + privvol->allocation) >
         privpool->def->capacity) {
4539
        testError(VIR_ERR_INTERNAL_ERROR,
4540 4541 4542 4543 4544 4545 4546 4547 4548
                  _("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) {
4549
        virReportOOMError();
4550 4551 4552
        goto cleanup;
    }

4553 4554 4555
    if (virAsprintf(&privvol->target.path, "%s/%s",
                    privpool->def->target.path,
                    privvol->name) == -1) {
4556
        virReportOOMError();
4557 4558 4559 4560 4561
        goto cleanup;
    }

    privvol->key = strdup(privvol->target.path);
    if (privvol->key == NULL) {
4562
        virReportOOMError();
4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582
        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 已提交
4583
static int
4584
testStorageVolumeDelete(virStorageVolPtr vol,
C
Cole Robinson 已提交
4585
                        unsigned int flags ATTRIBUTE_UNUSED) {
4586 4587 4588
    testConnPtr privconn = vol->conn->privateData;
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol;
C
Cole Robinson 已提交
4589
    int i;
4590
    int ret = -1;
C
Cole Robinson 已提交
4591

4592
    testDriverLock(privconn);
4593 4594
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           vol->pool);
4595
    testDriverUnlock(privconn);
4596 4597

    if (privpool == NULL) {
4598
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4599
        goto cleanup;
4600 4601 4602 4603 4604 4605
    }


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

    if (privvol == NULL) {
4606
        testError(VIR_ERR_NO_STORAGE_VOL,
4607 4608
                  _("no storage vol with matching name '%s'"),
                  vol->name);
4609
        goto cleanup;
4610 4611 4612
    }

    if (!virStoragePoolObjIsActive(privpool)) {
4613
        testError(VIR_ERR_OPERATION_INVALID,
4614
                  _("storage pool '%s' is not active"), vol->pool);
4615
        goto cleanup;
4616 4617 4618
    }


C
Cole Robinson 已提交
4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641
    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;
        }
    }
4642
    ret = 0;
C
Cole Robinson 已提交
4643

4644
cleanup:
4645 4646
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4647
    return ret;
C
Cole Robinson 已提交
4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663
}


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
4664
testStorageVolumeGetInfo(virStorageVolPtr vol,
C
Cole Robinson 已提交
4665
                         virStorageVolInfoPtr info) {
4666 4667 4668
    testConnPtr privconn = vol->conn->privateData;
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol;
4669
    int ret = -1;
4670

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

    if (privpool == NULL) {
4677
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4678
        goto cleanup;
4679 4680 4681 4682 4683
    }

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

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

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

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

4702
cleanup:
4703 4704
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4705
    return ret;
C
Cole Robinson 已提交
4706 4707 4708
}

static char *
4709
testStorageVolumeGetXMLDesc(virStorageVolPtr vol,
C
Cole Robinson 已提交
4710
                            unsigned int flags ATTRIBUTE_UNUSED) {
4711 4712 4713
    testConnPtr privconn = vol->conn->privateData;
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol;
4714
    char *ret = NULL;
4715

4716
    testDriverLock(privconn);
4717 4718
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           vol->pool);
4719
    testDriverUnlock(privconn);
4720 4721

    if (privpool == NULL) {
4722
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4723
        goto cleanup;
4724 4725 4726 4727 4728
    }

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

    if (privvol == NULL) {
4729
        testError(VIR_ERR_NO_STORAGE_VOL,
4730 4731
                  _("no storage vol with matching name '%s'"),
                  vol->name);
4732
        goto cleanup;
4733
    }
C
Cole Robinson 已提交
4734

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

4741
    ret = virStorageVolDefFormat(privpool->def, privvol);
4742 4743

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

static char *
4750 4751 4752 4753
testStorageVolumeGetPath(virStorageVolPtr vol) {
    testConnPtr privconn = vol->conn->privateData;
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol;
4754
    char *ret = NULL;
4755

4756
    testDriverLock(privconn);
4757 4758
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           vol->pool);
4759
    testDriverUnlock(privconn);
4760 4761

    if (privpool == NULL) {
4762
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4763
        goto cleanup;
4764 4765 4766 4767 4768
    }

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

    if (privvol == NULL) {
4769
        testError(VIR_ERR_NO_STORAGE_VOL,
4770 4771
                  _("no storage vol with matching name '%s'"),
                  vol->name);
4772
        goto cleanup;
4773 4774 4775
    }

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

C
Cole Robinson 已提交
4781
    ret = strdup(privvol->target.path);
4782
    if (ret == NULL)
4783
        virReportOOMError();
4784 4785

cleanup:
4786 4787
    if (privpool)
        virStoragePoolObjUnlock(privpool);
C
Cole Robinson 已提交
4788 4789 4790
    return ret;
}

4791

4792
/* Node device implementations */
4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807
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;
}

4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873
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) {
4874
        virNodeDeviceReportError(VIR_ERR_NO_NODE_DEVICE, NULL);
4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886
        goto cleanup;
    }

    ret = virGetNodeDevice(conn, name);

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

static char *
4887 4888
testNodeDeviceGetXMLDesc(virNodeDevicePtr dev,
                         unsigned int flags ATTRIBUTE_UNUSED)
4889 4890 4891 4892 4893 4894 4895 4896 4897 4898
{
    testConnPtr driver = dev->conn->privateData;
    virNodeDeviceObjPtr obj;
    char *ret = NULL;

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

    if (!obj) {
4899 4900
        virNodeDeviceReportError(VIR_ERR_NO_NODE_DEVICE,
                                 _("no node device with matching name '%s'"),
4901 4902 4903 4904
                                 dev->name);
        goto cleanup;
    }

4905
    ret = virNodeDeviceDefFormat(obj->def);
4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924

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) {
4925
        virNodeDeviceReportError(VIR_ERR_NO_NODE_DEVICE,
4926 4927 4928 4929 4930 4931 4932 4933
                                _("no node device with matching name '%s'"),
                                 dev->name);
        goto cleanup;
    }

    if (obj->def->parent) {
        ret = strdup(obj->def->parent);
        if (!ret)
4934
            virReportOOMError();
4935
    } else {
4936
        virNodeDeviceReportError(VIR_ERR_INTERNAL_ERROR,
4937 4938 4939 4940 4941 4942 4943 4944 4945
                                 "%s", _("no parent for this device"));
    }

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

4946

4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960
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) {
4961 4962
        virNodeDeviceReportError(VIR_ERR_NO_NODE_DEVICE,
                                 _("no node device with matching name '%s'"),
4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991
                                 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) {
4992
        virNodeDeviceReportError(VIR_ERR_NO_NODE_DEVICE,
4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015
                                _("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;
}

5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030
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);

5031
    def = virNodeDeviceDefParseString(xmlDesc, CREATE_DEVICE);
5032 5033 5034 5035 5036
    if (def == NULL) {
        goto cleanup;
    }

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

5041
    if (virNodeDeviceGetParentHost(&driver->devs,
5042 5043 5044 5045 5046 5047 5048 5049 5050 5051
                                   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))) {
5052
        virReportOOMError();
5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067
        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;
    }


5068
    if (!(obj = virNodeDeviceAssignDef(&driver->devs, def))) {
5069 5070 5071 5072 5073 5074 5075 5076
        goto cleanup;
    }
    virNodeDeviceObjUnlock(obj);

    dev = virGetNodeDevice(conn, def->name);
    def = NULL;
cleanup:
    testDriverUnlock(driver);
5077
    virNodeDeviceDefFree(def);
5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096
    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) {
5097
        virNodeDeviceReportError(VIR_ERR_NO_NODE_DEVICE, NULL);
5098 5099 5100
        goto out;
    }

5101
    if (virNodeDeviceGetWWNs(obj->def, &wwnn, &wwpn) == -1) {
5102 5103 5104 5105 5106
        goto out;
    }

    parent_name = strdup(obj->def->parent);
    if (parent_name == NULL) {
5107
        virReportOOMError();
5108 5109 5110 5111 5112 5113 5114 5115 5116 5117
        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 */
5118
    if (virNodeDeviceGetParentHost(&driver->devs,
5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137
                                   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;
}

5138 5139

/* Domain event implementations */
5140
static int
5141 5142 5143 5144
testDomainEventRegister(virConnectPtr conn,
                        virConnectDomainEventCallback callback,
                        void *opaque,
                        virFreeCallback freecb)
5145 5146 5147 5148 5149
{
    testConnPtr driver = conn->privateData;
    int ret;

    testDriverLock(driver);
5150 5151
    ret = virDomainEventCallbackListAdd(conn,
                                        driver->domainEventState->callbacks,
5152 5153 5154 5155 5156 5157
                                        callback, opaque, freecb);
    testDriverUnlock(driver);

    return ret;
}

5158

5159
static int
5160 5161
testDomainEventDeregister(virConnectPtr conn,
                          virConnectDomainEventCallback callback)
5162 5163 5164 5165 5166
{
    testConnPtr driver = conn->privateData;
    int ret;

    testDriverLock(driver);
5167 5168 5169
    ret = virDomainEventStateDeregister(conn,
                                        driver->domainEventState,
                                        callback);
5170 5171 5172 5173 5174
    testDriverUnlock(driver);

    return ret;
}

5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187

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

    testDriverLock(driver);
5188 5189
    ret = virDomainEventCallbackListAddID(conn,
                                          driver->domainEventState->callbacks,
5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204
                                          dom, eventID,
                                          callback, opaque, freecb);
    testDriverUnlock(driver);

    return ret;
}

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

    testDriverLock(driver);
5205 5206 5207
    ret = virDomainEventStateDeregisterAny(conn,
                                           driver->domainEventState,
                                           callbackID);
5208 5209 5210 5211 5212 5213
    testDriverUnlock(driver);

    return ret;
}


5214 5215
static void testDomainEventDispatchFunc(virConnectPtr conn,
                                        virDomainEventPtr event,
5216
                                        virConnectDomainEventGenericCallback cb,
5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232
                                        void *cbopaque,
                                        void *opaque)
{
    testConnPtr driver = opaque;

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

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

    testDriverLock(driver);
5233 5234 5235
    virDomainEventStateFlush(driver->domainEventState,
                             testDomainEventDispatchFunc,
                             driver);
5236 5237 5238 5239 5240 5241 5242 5243
    testDriverUnlock(driver);
}


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

5247 5248 5249 5250 5251 5252
static virDrvOpenStatus testSecretOpen(virConnectPtr conn,
                                       virConnectAuthPtr auth ATTRIBUTE_UNUSED,
                                       int flags ATTRIBUTE_UNUSED) {
    if (STRNEQ(conn->driver->name, "Test"))
        return VIR_DRV_OPEN_DECLINED;

5253
    conn->secretPrivateData = conn->privateData;
5254 5255 5256 5257 5258 5259 5260
    return VIR_DRV_OPEN_SUCCESS;
}

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

5262 5263 5264 5265 5266 5267 5268

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

5269
    conn->nwfilterPrivateData = conn->privateData;
5270 5271 5272 5273 5274 5275 5276 5277
    return VIR_DRV_OPEN_SUCCESS;
}

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

5278 5279 5280 5281 5282
static virDriver testDriver = {
    VIR_DRV_TEST,
    "Test",
    testOpen, /* open */
    testClose, /* close */
5283
    NULL, /* supports_feature */
5284 5285
    NULL, /* type */
    testGetVersion, /* version */
5286
    NULL, /* libvirtVersion (impl. in libvirt.c) */
5287
    virGetHostname, /* getHostname */
E
Eric Blake 已提交
5288
    NULL, /* getSysinfo */
5289 5290 5291 5292 5293
    testGetMaxVCPUs, /* getMaxVcpus */
    testNodeGetInfo, /* nodeGetInfo */
    testGetCapabilities, /* getCapabilities */
    testListDomains, /* listDomains */
    testNumOfDomains, /* numOfDomains */
5294
    testDomainCreateXML, /* domainCreateXML */
5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306
    testLookupDomainByID, /* domainLookupByID */
    testLookupDomainByUUID, /* domainLookupByUUID */
    testLookupDomainByName, /* domainLookupByName */
    testPauseDomain, /* domainSuspend */
    testResumeDomain, /* domainResume */
    testShutdownDomain, /* domainShutdown */
    testRebootDomain, /* domainReboot */
    testDestroyDomain, /* domainDestroy */
    testGetOSType, /* domainGetOSType */
    testGetMaxMemory, /* domainGetMaxMemory */
    testSetMaxMemory, /* domainSetMaxMemory */
    testSetMemory, /* domainSetMemory */
5307
    NULL, /* domainSetMemoryFlags */
5308 5309 5310 5311
    NULL, /* domainSetMemoryParameters */
    NULL, /* domainGetMemoryParameters */
    NULL, /* domainSetBlkioParameters */
    NULL, /* domainGetBlkioParameters */
5312
    testGetDomainInfo, /* domainGetInfo */
5313
    NULL, /* domainGetState */
5314 5315 5316
    testDomainSave, /* domainSave */
    testDomainRestore, /* domainRestore */
    testDomainCoreDump, /* domainCoreDump */
5317
    NULL, /* domainScreenshot */
5318
    testSetVcpus, /* domainSetVcpus */
5319 5320
    testDomainSetVcpusFlags, /* domainSetVcpusFlags */
    testDomainGetVcpusFlags, /* domainGetVcpusFlags */
C
Cole Robinson 已提交
5321
    testDomainPinVcpu, /* domainPinVcpu */
C
Cole Robinson 已提交
5322
    testDomainGetVcpus, /* domainGetVcpus */
C
Cole Robinson 已提交
5323
    testDomainGetMaxVcpus, /* domainGetMaxVcpus */
5324 5325
    NULL, /* domainGetSecurityLabel */
    NULL, /* nodeGetSecurityModel */
5326
    testDomainGetXMLDesc, /* domainGetXMLDesc */
5327 5328
    NULL, /* domainXMLFromNative */
    NULL, /* domainXMLToNative */
5329 5330 5331
    testListDefinedDomains, /* listDefinedDomains */
    testNumOfDefinedDomains, /* numOfDefinedDomains */
    testDomainCreate, /* domainCreate */
5332
    testDomainCreateWithFlags, /* domainCreateWithFlags */
5333 5334 5335
    testDomainDefineXML, /* domainDefineXML */
    testDomainUndefine, /* domainUndefine */
    NULL, /* domainAttachDevice */
5336
    NULL, /* domainAttachDeviceFlags */
5337
    NULL, /* domainDetachDevice */
5338
    NULL, /* domainDetachDeviceFlags */
5339
    NULL, /* domainUpdateDeviceFlags */
5340 5341 5342 5343 5344
    testDomainGetAutostart, /* domainGetAutostart */
    testDomainSetAutostart, /* domainSetAutostart */
    testDomainGetSchedulerType, /* domainGetSchedulerType */
    testDomainGetSchedulerParams, /* domainGetSchedulerParameters */
    testDomainSetSchedulerParams, /* domainSetSchedulerParameters */
5345 5346 5347
    NULL, /* domainMigratePrepare */
    NULL, /* domainMigratePerform */
    NULL, /* domainMigrateFinish */
5348 5349
    testDomainBlockStats, /* domainBlockStats */
    testDomainInterfaceStats, /* domainInterfaceStats */
5350
    NULL, /* domainMemoryStats */
R
Richard W.M. Jones 已提交
5351
    NULL, /* domainBlockPeek */
R
Richard W.M. Jones 已提交
5352
    NULL, /* domainMemoryPeek */
5353
    NULL, /* domainGetBlockInfo */
5354
    testNodeGetCellsFreeMemory, /* nodeGetCellsFreeMemory */
5355
    NULL, /* getFreeMemory */
5356 5357
    testDomainEventRegister, /* domainEventRegister */
    testDomainEventDeregister, /* domainEventDeregister */
D
Daniel Veillard 已提交
5358 5359
    NULL, /* domainMigratePrepare2 */
    NULL, /* domainMigrateFinish2 */
5360
    NULL, /* nodeDeviceDettach */
5361 5362
    NULL, /* nodeDeviceReAttach */
    NULL, /* nodeDeviceReset */
C
Chris Lalancette 已提交
5363
    NULL, /* domainMigratePrepareTunnel */
5364 5365 5366 5367
    testIsEncrypted, /* isEncrypted */
    testIsSecure, /* isEncrypted */
    testDomainIsActive, /* domainIsActive */
    testDomainIsPersistent, /* domainIsPersistent */
5368
    testDomainIsUpdated, /* domainIsUpdated */
J
Jiri Denemark 已提交
5369
    NULL, /* cpuCompare */
5370
    NULL, /* cpuBaseline */
5371
    NULL, /* domainGetJobInfo */
5372
    NULL, /* domainAbortJob */
5373
    NULL, /* domainMigrateSetMaxDowntime */
5374
    NULL, /* domainMigrateSetMaxSpeed */
5375 5376
    testDomainEventRegisterAny, /* domainEventRegisterAny */
    testDomainEventDeregisterAny, /* domainEventDeregisterAny */
5377 5378 5379
    NULL, /* domainManagedSave */
    NULL, /* domainHasManagedSaveImage */
    NULL, /* domainManagedSaveRemove */
C
Chris Lalancette 已提交
5380
    NULL, /* domainSnapshotCreateXML */
5381
    NULL, /* domainSnapshotGetXMLDesc */
C
Chris Lalancette 已提交
5382 5383 5384 5385 5386 5387 5388
    NULL, /* domainSnapshotNum */
    NULL, /* domainSnapshotListNames */
    NULL, /* domainSnapshotLookupByName */
    NULL, /* domainHasCurrentSnapshot */
    NULL, /* domainSnapshotCurrent */
    NULL, /* domainRevertToSnapshot */
    NULL, /* domainSnapshotDelete */
C
Chris Lalancette 已提交
5389
    NULL, /* qemuDomainMonitorCommand */
5390
    NULL, /* domainOpenConsole */
5391
    NULL, /* domainInjectNMI */
5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408
};

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 */
5409
    testNetworkGetXMLDesc, /* networkGetXMLDesc */
5410 5411 5412
    testNetworkGetBridgeName, /* networkGetBridgeName */
    testNetworkGetAutostart, /* networkGetAutostart */
    testNetworkSetAutostart, /* networkSetAutostart */
5413 5414
    testNetworkIsActive, /* networkIsActive */
    testNetworkIsPersistent, /* networkIsPersistent */
5415 5416
};

L
Laine Stump 已提交
5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431
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 */
5432
    testInterfaceIsActive,      /* interfaceIsActive */
L
Laine Stump 已提交
5433 5434 5435
};


5436 5437 5438 5439
static virStorageDriver testStorageDriver = {
    .name = "Test",
    .open = testStorageOpen,
    .close = testStorageClose,
C
Cole Robinson 已提交
5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457

    .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,
5458
    .poolGetXMLDesc = testStoragePoolGetXMLDesc,
C
Cole Robinson 已提交
5459 5460 5461 5462 5463 5464 5465 5466 5467
    .poolGetAutostart = testStoragePoolGetAutostart,
    .poolSetAutostart = testStoragePoolSetAutostart,
    .poolNumOfVolumes = testStoragePoolNumVolumes,
    .poolListVolumes = testStoragePoolListVolumes,

    .volLookupByName = testStorageVolumeLookupByName,
    .volLookupByKey = testStorageVolumeLookupByKey,
    .volLookupByPath = testStorageVolumeLookupByPath,
    .volCreateXML = testStorageVolumeCreateXML,
5468
    .volCreateXMLFrom = testStorageVolumeCreateXMLFrom,
C
Cole Robinson 已提交
5469 5470 5471 5472
    .volDelete = testStorageVolumeDelete,
    .volGetInfo = testStorageVolumeGetInfo,
    .volGetXMLDesc = testStorageVolumeGetXMLDesc,
    .volGetPath = testStorageVolumeGetPath,
5473 5474
    .poolIsActive = testStoragePoolIsActive,
    .poolIsPersistent = testStoragePoolIsPersistent,
5475 5476
};

5477 5478 5479 5480
static virDeviceMonitor testDevMonitor = {
    .name = "Test",
    .open = testDevMonOpen,
    .close = testDevMonClose,
5481 5482 5483 5484

    .numOfDevices = testNodeNumOfDevices,
    .listDevices = testNodeListDevices,
    .deviceLookupByName = testNodeDeviceLookupByName,
5485
    .deviceGetXMLDesc = testNodeDeviceGetXMLDesc,
5486 5487 5488
    .deviceGetParent = testNodeDeviceGetParent,
    .deviceNumOfCaps = testNodeDeviceNumOfCaps,
    .deviceListCaps = testNodeDeviceListCaps,
5489 5490
    .deviceCreateXML = testNodeDeviceCreateXML,
    .deviceDestroy = testNodeDeviceDestroy,
5491 5492
};

5493 5494 5495 5496 5497
static virSecretDriver testSecretDriver = {
    .name = "Test",
    .open = testSecretOpen,
    .close = testSecretClose,
};
5498 5499


5500 5501 5502 5503 5504 5505
static virNWFilterDriver testNWFilterDriver = {
    .name = "Test",
    .open = testNWFilterOpen,
    .close = testNWFilterClose,
};

5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517
/**
 * testRegister:
 *
 * Registers the test driver
 */
int
testRegister(void)
{
    if (virRegisterDriver(&testDriver) < 0)
        return -1;
    if (virRegisterNetworkDriver(&testNetworkDriver) < 0)
        return -1;
L
Laine Stump 已提交
5518 5519
    if (virRegisterInterfaceDriver(&testInterfaceDriver) < 0)
        return -1;
5520 5521
    if (virRegisterStorageDriver(&testStorageDriver) < 0)
        return -1;
5522 5523
    if (virRegisterDeviceMonitor(&testDevMonitor) < 0)
        return -1;
5524 5525
    if (virRegisterSecretDriver(&testSecretDriver) < 0)
        return -1;
5526 5527
    if (virRegisterNWFilterDriver(&testNWFilterDriver) < 0)
        return -1;
5528

5529 5530
    return 0;
}