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

24
#include <config.h>
25

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

34 35

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

54 55
#define VIR_FROM_THIS VIR_FROM_TEST

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

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

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

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

#define MAX_CELLS 128
76

77
struct _testConn {
78
    virMutex lock;
79

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


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

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

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

117

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

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


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

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

138 139 140 141 142 143 144 145 146 147 148 149 150 151
static void *testDomainObjPrivateAlloc(void)
{
    testDomainObjPrivatePtr priv;

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

    return priv;
}

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

D
Daniel P. Berrange 已提交
152
    VIR_FREE(priv->vcpu_infos);
153 154 155 156 157
    VIR_FREE(priv->cpumaps);
    VIR_FREE(priv);
}


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

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

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

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

180 181 182 183 184 185 186 187 188 189
    for (i = 0; i < ARRAY_CARDINALITY(guest_types) ; i++) {
        if ((guest = virCapabilitiesAddGuest(caps,
                                             guest_types[i],
                                             TEST_MODEL,
                                             TEST_MODEL_WORDSIZE,
                                             TEST_EMULATOR,
                                             NULL,
                                             0,
                                             NULL)) == NULL)
            goto no_memory;
190

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

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

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

208
    return caps;
209

210
no_memory:
211
    virReportOOMError(conn);
212 213
    virCapabilitiesFree(caps);
    return NULL;
214 215
}

216

217 218 219 220 221 222 223 224 225 226
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>";
227 228


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

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

260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
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";

283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
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>";

301
static const unsigned long long defaultPoolCap = (100 * 1024 * 1024 * 1024ull);
C
Cole Robinson 已提交
302 303
static const unsigned long long defaultPoolAlloc = 0;

304 305 306
static int testStoragePoolObjSetDefaults(virConnectPtr conn,
                                         virStoragePoolObjPtr pool);
static int testNodeGetInfo(virConnectPtr conn, virNodeInfoPtr info);
307

308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
static char *
testDomainGenerateIfname(virConnectPtr conn,
                         virDomainDefPtr domdef) {
    int maxif = 1024;
    int ifctr, i;

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

        if (virAsprintf(&ifname, "testnet%d", ifctr) < 0) {
            virReportOOMError(conn);
            return NULL;
        }

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

        if (!found)
            return ifname;
    }

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

341 342 343
static int
testDomainGenerateIfnames(virConnectPtr conn,
                          virDomainDefPtr domdef)
344 345 346 347 348 349 350 351 352 353
{
    int i = 0;

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

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

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

359
    return 0;
360 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 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
/* 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) {
        virReportOOMError(conn);
        goto cleanup;
    }

    if (VIR_REALLOC_N(privdata->cpumaps, nvcpus * cpumaplen) < 0) {
        virReportOOMError(conn);
        goto cleanup;
    }

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

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

    ret = 0;
cleanup:
    return ret;
}

/* Set up domain runtime state */
454 455 456 457 458
static int
testDomainStartState(virConnectPtr conn,
                     virDomainObjPtr dom)
{
    testConnPtr privconn = conn->privateData;
459
    int ret = -1;
460

461 462 463 464
    if (testDomainUpdateVCPUs(conn, dom, dom->def->vcpus, 1) < 0)
        goto cleanup;

    /* Set typical run state */
465 466 467
    dom->state = VIR_DOMAIN_RUNNING;
    dom->def->id = privconn->nextDomID++;

468 469 470
    ret = 0;
cleanup:
    return ret;
471
}
472

473 474 475 476
static void
testDomainShutdownState(virDomainPtr domain,
                        virDomainObjPtr privdom)
{
477 478 479 480 481 482
    if (privdom->newDef) {
        virDomainDefFree(privdom->def);
        privdom->def = privdom->newDef;
        privdom->newDef = NULL;
    }

483 484 485 486 487
    privdom->state = VIR_DOMAIN_SHUTOFF;
    privdom->def->id = -1;
    domain->id = -1;
}

488
static int testOpenDefault(virConnectPtr conn) {
489 490
    int u;
    struct timeval tv;
491
    testConnPtr privconn;
492 493 494 495
    virDomainDefPtr domdef = NULL;
    virDomainObjPtr domobj = NULL;
    virNetworkDefPtr netdef = NULL;
    virNetworkObjPtr netobj = NULL;
L
Laine Stump 已提交
496 497
    virInterfaceDefPtr interfacedef = NULL;
    virInterfaceObjPtr interfaceobj = NULL;
C
Cole Robinson 已提交
498 499
    virStoragePoolDefPtr pooldef = NULL;
    virStoragePoolObjPtr poolobj = NULL;
500 501
    virNodeDeviceDefPtr nodedef = NULL;
    virNodeDeviceObjPtr nodeobj = NULL;
502

503
    if (VIR_ALLOC(privconn) < 0) {
504
        virReportOOMError(conn);
505 506
        return VIR_DRV_OPEN_ERROR;
    }
507 508 509 510 511 512 513
    if (virMutexInit(&privconn->lock) < 0) {
        testError(conn, VIR_ERR_INTERNAL_ERROR,
                  "%s", _("cannot initialize mutex"));
        VIR_FREE(privconn);
        return VIR_DRV_OPEN_ERROR;
    }

514
    testDriverLock(privconn);
515
    conn->privateData = privconn;
516 517

    if (gettimeofday(&tv, NULL) < 0) {
518 519
        virReportSystemError(conn, errno,
                             "%s", _("getting time of day"));
520
        goto error;
521 522
    }

523 524 525
    if (virDomainObjListInit(&privconn->domains) < 0)
        goto error;

526
    memmove(&privconn->nodeInfo, &defaultNodeInfo, sizeof(defaultNodeInfo));
527

528 529 530 531 532 533 534 535 536 537
    // Numa setup
    privconn->numCells = 2;
    for (u = 0; u < 2; ++u) {
        privconn->cells[u].numCpus = 8;
        privconn->cells[u].mem = (u + 1) * 2048 * 1024;
    }
    for (u = 0 ; u < 16 ; u++) {
        privconn->cells[u % 2].cpus[(u / 2)] = u;
    }

538 539 540 541 542
    if (!(privconn->caps = testBuildCapabilities(conn)))
        goto error;

    privconn->nextDomID = 1;

543 544 545
    if (!(domdef = virDomainDefParseString(conn, privconn->caps,
                                           defaultDomainXML,
                                           VIR_DOMAIN_XML_INACTIVE)))
546
        goto error;
547
    if (testDomainGenerateIfnames(conn, domdef) < 0)
548
        goto error;
549 550
    if (!(domobj = virDomainAssignDef(conn, privconn->caps,
                                      &privconn->domains, domdef)))
551 552
        goto error;
    domdef = NULL;
553 554 555 556 557 558

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

559
    domobj->persistent = 1;
560
    virDomainObjUnlock(domobj);
561 562 563 564 565 566 567 568 569

    if (!(netdef = virNetworkDefParseString(conn, defaultNetworkXML)))
        goto error;
    if (!(netobj = virNetworkAssignDef(conn, &privconn->networks, netdef))) {
        virNetworkDefFree(netdef);
        goto error;
    }
    netobj->active = 1;
    netobj->persistent = 1;
570
    virNetworkObjUnlock(netobj);
571

L
Laine Stump 已提交
572 573 574 575 576 577 578 579 580
    if (!(interfacedef = virInterfaceDefParseString(conn, defaultInterfaceXML)))
        goto error;
    if (!(interfaceobj = virInterfaceAssignDef(conn, &privconn->ifaces, interfacedef))) {
        virInterfaceDefFree(interfacedef);
        goto error;
    }
    interfaceobj->active = 1;
    virInterfaceObjUnlock(interfaceobj);

581
    if (!(pooldef = virStoragePoolDefParseString(conn, defaultPoolXML)))
C
Cole Robinson 已提交
582 583 584 585 586 587 588
        goto error;

    if (!(poolobj = virStoragePoolObjAssignDef(conn, &privconn->pools,
                                               pooldef))) {
        virStoragePoolDefFree(pooldef);
        goto error;
    }
589

590
    if (testStoragePoolObjSetDefaults(conn, poolobj) == -1) {
591
        virStoragePoolObjUnlock(poolobj);
C
Cole Robinson 已提交
592
        goto error;
593
    }
C
Cole Robinson 已提交
594
    poolobj->active = 1;
595
    virStoragePoolObjUnlock(poolobj);
C
Cole Robinson 已提交
596

597 598 599 600 601 602 603 604 605 606
    /* Init default node device */
    if (!(nodedef = virNodeDeviceDefParseString(conn, defaultNodeXML, 0)))
        goto error;
    if (!(nodeobj = virNodeDeviceAssignDef(conn, &privconn->devs,
                                           nodedef))) {
        virNodeDeviceDefFree(nodedef);
        goto error;
    }
    virNodeDeviceObjUnlock(nodeobj);

607
    testDriverUnlock(privconn);
608

609 610 611
    return VIR_DRV_OPEN_SUCCESS;

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


static char *testBuildFilename(const char *relativeTo,
627 628 629 630 631 632 633 634
                               const char *filename) {
    char *offset;
    int baseLen;
    if (!filename || filename[0] == '\0')
        return (NULL);
    if (filename[0] == '/')
        return strdup(filename);

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

652 653 654 655 656 657 658 659 660
static int testOpenVolumesForPool(virConnectPtr conn,
                                  xmlDocPtr xml,
                                  xmlXPathContextPtr ctxt,
                                  const char *file,
                                  virStoragePoolObjPtr pool,
                                  int poolidx) {
    char *vol_xpath;
    int i, ret, func_ret = -1;
    xmlNodePtr *vols = NULL;
661
    virStorageVolDefPtr def = NULL;
662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732

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

    ret = virXPathNodeSet(conn, vol_xpath, ctxt, &vols);
    VIR_FREE(vol_xpath);
    if (ret < 0) {
        testError(NULL, VIR_ERR_XML_ERROR,
                  _("node vol list for pool '%s'"), pool->def->name);
        goto error;
    }

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

            def = virStorageVolDefParseFile(conn, pool->def, absFile);
            VIR_FREE(absFile);
            if (!def)
                goto error;
        } else {
            if ((def = virStorageVolDefParseNode(conn, pool->def, xml,
                                                 vols[i])) == NULL) {
                goto error;
            }
        }

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

        if (virAsprintf(&def->target.path, "%s/%s",
                        pool->def->target.path,
                        def->name) == -1) {
            virReportOOMError(conn);
            goto error;
        }

        def->key = strdup(def->target.path);
        if (def->key == NULL) {
            virReportOOMError(conn);
            goto error;
        }

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

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

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

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

759
    testDriverLock(privconn);
760
    conn->privateData = privconn;
761

762 763 764
    if (virDomainObjListInit(&privconn->domains) < 0)
        goto error;

765 766
    if (!(privconn->caps = testBuildCapabilities(conn)))
        goto error;
767 768

    if ((fd = open(file, O_RDONLY)) < 0) {
769 770 771
        virReportSystemError(NULL, errno,
                             _("loading host definition file '%s'"),
                             file);
772
        goto error;
773 774
    }

775 776 777
    if (!(xml = xmlReadFd(fd, file, NULL,
                          XML_PARSE_NOENT | XML_PARSE_NONET |
                          XML_PARSE_NOERROR | XML_PARSE_NOWARNING))) {
778 779
        testError(NULL, VIR_ERR_INTERNAL_ERROR,
                  _("Invalid XML in file '%s'"), file);
780
        goto error;
781
    }
782 783
    close(fd);
    fd = -1;
784

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

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

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

    nodeInfo = &privconn->nodeInfo;
809
    ret = virXPathLong(conn, "string(/node/cpu/nodes[1])", ctxt, &l);
810 811 812
    if (ret == 0) {
        nodeInfo->nodes = l;
    } else if (ret == -2) {
J
Jim Meyering 已提交
813
        testError(NULL, VIR_ERR_XML_ERROR, "%s", _("node cpu numa nodes"));
814
        goto error;
815
    }
816

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

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

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

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

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

870
    ret = virXPathLong(conn, "string(/node/memory[1])", ctxt, &l);
871 872 873
    if (ret == 0) {
        nodeInfo->memory = l;
    } else if (ret == -2) {
J
Jim Meyering 已提交
874
        testError(NULL, VIR_ERR_XML_ERROR, "%s", _("node memory"));
875
        goto error;
876
    }
877

878
    ret = virXPathNodeSet(conn, "/node/domain", ctxt, &domains);
879
    if (ret < 0) {
J
Jim Meyering 已提交
880
        testError(NULL, VIR_ERR_XML_ERROR, "%s", _("node domain list"));
881
        goto error;
882
    }
883

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

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

912 913 914 915 916
        if (testDomainStartState(conn, dom) < 0) {
            virDomainObjUnlock(dom);
            goto error;
        }

917
        dom->persistent = 1;
918
        virDomainObjUnlock(dom);
919
    }
920
    VIR_FREE(domains);
921

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

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

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

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

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

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

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

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

        if (!(pool = virStoragePoolObjAssignDef(conn, &privconn->pools,
                                                def))) {
            virStoragePoolDefFree(def);
            goto error;
        }

1029
        if (testStoragePoolObjSetDefaults(conn, pool) == -1) {
1030
            virStoragePoolObjUnlock(pool);
C
Cole Robinson 已提交
1031
            goto error;
1032
        }
C
Cole Robinson 已提交
1033
        pool->active = 1;
1034 1035 1036 1037 1038 1039 1040

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

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

1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081
    ret = virXPathNodeSet(conn, "/node/device", ctxt, &devs);
    if (ret < 0) {
        testError(NULL, VIR_ERR_XML_ERROR, "%s", _("node device list"));
        goto error;
    }
    for (i = 0 ; i < ret ; i++) {
        virNodeDeviceDefPtr def;
        virNodeDeviceObjPtr dev;
        char *relFile = virXMLPropString(devs[i], "file");

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

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

            def = virNodeDeviceDefParseFile(conn, absFile, 0);
            VIR_FREE(absFile);
            if (!def)
                goto error;
        } else {
            if ((def = virNodeDeviceDefParseNode(conn, xml, devs[i], 0)) == NULL)
                goto error;
        }
        if (!(dev = virNodeDeviceAssignDef(conn, &privconn->devs, def))) {
            virNodeDeviceDefFree(def);
            goto error;
        }
        virNodeDeviceObjUnlock(dev);
    }
    VIR_FREE(devs);


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

1086
    return (0);
1087 1088

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

1107

1108
static virDrvOpenStatus testOpen(virConnectPtr conn,
1109
                    virConnectAuthPtr auth ATTRIBUTE_UNUSED,
1110
                    int flags ATTRIBUTE_UNUSED)
1111
{
1112
    int ret;
1113

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

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

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

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

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

1139 1140
    if (ret == VIR_DRV_OPEN_SUCCESS) {
        testConnPtr privconn = conn->privateData;
1141
        testDriverLock(privconn);
1142 1143 1144 1145
        /* Init callback list */
        if (VIR_ALLOC(privconn->domainEventCallbacks) < 0 ||
            !(privconn->domainEventQueue = virDomainEventQueueNew())) {
            virReportOOMError(NULL);
1146
            testDriverUnlock(privconn);
1147 1148 1149 1150 1151 1152 1153 1154
            testClose(conn);
            return VIR_DRV_OPEN_ERROR;
        }

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

1158
    return (ret);
1159 1160
}

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

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

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

1178
    testDriverUnlock(privconn);
1179
    virMutexDestroy(&privconn->lock);
1180

1181
    VIR_FREE (privconn);
1182
    conn->privateData = NULL;
1183
    return 0;
1184 1185
}

1186 1187
static int testGetVersion(virConnectPtr conn ATTRIBUTE_UNUSED,
                          unsigned long *hvVer)
1188
{
1189 1190
    *hvVer = 2;
    return (0);
1191 1192
}

1193 1194 1195 1196 1197 1198 1199 1200 1201 1202
static int testIsSecure(virConnectPtr conn ATTRIBUTE_UNUSED)
{
    return 1;
}

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

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

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

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

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

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

1239
    return count;
1240 1241
}

1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283
static int testDomainIsActive(virDomainPtr dom)
{
    testConnPtr privconn = dom->conn->privateData;
    virDomainObjPtr obj;
    int ret = -1;

    testDriverLock(privconn);
    obj = virDomainFindByUUID(&privconn->domains, dom->uuid);
    testDriverUnlock(privconn);
    if (!obj) {
        testError(dom->conn, VIR_ERR_NO_DOMAIN, NULL);
        goto cleanup;
    }
    ret = virDomainObjIsActive(obj);

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

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

    testDriverLock(privconn);
    obj = virDomainFindByUUID(&privconn->domains, dom->uuid);
    testDriverUnlock(privconn);
    if (!obj) {
        testError(dom->conn, VIR_ERR_NO_DOMAIN, NULL);
        goto cleanup;
    }
    ret = obj->persistent;

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

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

1294
    testDriverLock(privconn);
1295 1296
    if ((def = virDomainDefParseString(conn, privconn->caps, xml,
                                       VIR_DOMAIN_XML_INACTIVE)) == NULL)
1297
        goto cleanup;
1298

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

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

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

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

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

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


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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1421
    return n;
1422 1423
}

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

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

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

1440
    testDomainShutdownState(domain, privdom);
1441 1442 1443
    event = virDomainEventNewFromObj(privdom,
                                     VIR_DOMAIN_EVENT_STOPPED,
                                     VIR_DOMAIN_EVENT_STOPPED_DESTROYED);
1444

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

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

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

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

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

1478
    if (privdom->state != VIR_DOMAIN_PAUSED) {
1479 1480 1481
        testError(domain->conn,
                  VIR_ERR_INTERNAL_ERROR, _("domain '%s' not paused"),
                  domain->name);
1482
        goto cleanup;
1483
    }
1484

1485
    privdom->state = VIR_DOMAIN_RUNNING;
1486 1487 1488
    event = virDomainEventNewFromObj(privdom,
                                     VIR_DOMAIN_EVENT_RESUMED,
                                     VIR_DOMAIN_EVENT_RESUMED_UNPAUSED);
1489 1490 1491
    ret = 0;

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

1502
static int testPauseDomain (virDomainPtr domain)
1503
{
1504 1505
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
1506
    virDomainEventPtr event = NULL;
1507
    int ret = -1;
1508

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

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

1519 1520
    if (privdom->state == VIR_DOMAIN_SHUTOFF ||
        privdom->state == VIR_DOMAIN_PAUSED) {
1521 1522 1523
        testError(domain->conn,
                  VIR_ERR_INTERNAL_ERROR, _("domain '%s' not running"),
                  domain->name);
1524
        goto cleanup;
1525
    }
1526

1527
    privdom->state = VIR_DOMAIN_PAUSED;
1528 1529 1530
    event = virDomainEventNewFromObj(privdom,
                                     VIR_DOMAIN_EVENT_SUSPENDED,
                                     VIR_DOMAIN_EVENT_SUSPENDED_PAUSED);
1531 1532 1533
    ret = 0;

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

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

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

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

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

1561
    if (privdom->state == VIR_DOMAIN_SHUTOFF) {
1562 1563
        testError(domain->conn, VIR_ERR_INTERNAL_ERROR,
                  _("domain '%s' not running"), domain->name);
1564
        goto cleanup;
1565
    }
1566

1567
    testDomainShutdownState(domain, privdom);
1568 1569 1570
    event = virDomainEventNewFromObj(privdom,
                                     VIR_DOMAIN_EVENT_STOPPED,
                                     VIR_DOMAIN_EVENT_STOPPED_SHUTDOWN);
1571

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

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

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

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

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

1606 1607 1608 1609
    privdom->state = VIR_DOMAIN_SHUTDOWN;
    switch (privdom->def->onReboot) {
    case VIR_DOMAIN_LIFECYCLE_DESTROY:
        privdom->state = VIR_DOMAIN_SHUTOFF;
1610 1611
        break;

1612 1613
    case VIR_DOMAIN_LIFECYCLE_RESTART:
        privdom->state = VIR_DOMAIN_RUNNING;
1614 1615
        break;

1616 1617
    case VIR_DOMAIN_LIFECYCLE_PRESERVE:
        privdom->state = VIR_DOMAIN_SHUTOFF;
1618 1619
        break;

1620 1621
    case VIR_DOMAIN_LIFECYCLE_RESTART_RENAME:
        privdom->state = VIR_DOMAIN_RUNNING;
1622
        break;
1623

1624
    default:
1625
        privdom->state = VIR_DOMAIN_SHUTOFF;
1626 1627
        break;
    }
1628

1629
    if (privdom->state == VIR_DOMAIN_SHUTOFF) {
1630
        testDomainShutdownState(domain, privdom);
1631 1632 1633
        event = virDomainEventNewFromObj(privdom,
                                         VIR_DOMAIN_EVENT_STOPPED,
                                         VIR_DOMAIN_EVENT_STOPPED_SHUTDOWN);
1634

1635 1636 1637 1638 1639
        if (!privdom->persistent) {
            virDomainRemoveInactive(&privconn->domains,
                                    privdom);
            privdom = NULL;
        }
1640 1641
    }

1642 1643
    ret = 0;
cleanup:
1644 1645
    if (privdom)
        virDomainObjUnlock(privdom);
1646 1647
    if (event)
        testDomainEventQueue(privconn, event);
1648
    testDriverUnlock(privconn);
1649
    return ret;
1650 1651
}

1652 1653
static int testGetDomainInfo (virDomainPtr domain,
                              virDomainInfoPtr info)
1654
{
1655
    testConnPtr privconn = domain->conn->privateData;
1656
    struct timeval tv;
1657
    virDomainObjPtr privdom;
1658
    int ret = -1;
1659

1660
    testDriverLock(privconn);
1661 1662
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
1663
    testDriverUnlock(privconn);
1664 1665 1666

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

    if (gettimeofday(&tv, NULL) < 0) {
1671
        testError(domain->conn, VIR_ERR_INTERNAL_ERROR,
J
Jim Meyering 已提交
1672
                  "%s", _("getting time of day"));
1673
        goto cleanup;
1674 1675
    }

1676 1677 1678 1679 1680
    info->state = privdom->state;
    info->memory = privdom->def->memory;
    info->maxMem = privdom->def->maxmem;
    info->nrVirtCpu = privdom->def->vcpus;
    info->cpuTime = ((tv.tv_sec * 1000ll * 1000ll  * 1000ll) + (tv.tv_usec * 1000ll));
1681 1682 1683
    ret = 0;

cleanup:
1684 1685
    if (privdom)
        virDomainObjUnlock(privdom);
1686
    return ret;
1687 1688
}

1689 1690 1691 1692 1693
#define TEST_SAVE_MAGIC "TestGuestMagic"

static int testDomainSave(virDomainPtr domain,
                          const char *path)
{
1694
    testConnPtr privconn = domain->conn->privateData;
1695 1696 1697
    char *xml = NULL;
    int fd = -1;
    int len;
1698
    virDomainObjPtr privdom;
1699
    virDomainEventPtr event = NULL;
1700
    int ret = -1;
1701

1702
    testDriverLock(privconn);
1703 1704 1705 1706 1707
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);

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

C
Cole Robinson 已提交
1711 1712 1713 1714
    xml = virDomainDefFormat(domain->conn,
                             privdom->def,
                             VIR_DOMAIN_XML_SECURE);

1715
    if (xml == NULL) {
1716 1717 1718
        virReportSystemError(domain->conn, errno,
                             _("saving domain '%s' failed to allocate space for metadata"),
                             domain->name);
1719
        goto cleanup;
1720
    }
1721 1722

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

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

1756
    testDomainShutdownState(domain, privdom);
1757 1758 1759
    event = virDomainEventNewFromObj(privdom,
                                     VIR_DOMAIN_EVENT_STOPPED,
                                     VIR_DOMAIN_EVENT_STOPPED_SAVED);
1760

1761 1762 1763
    if (!privdom->persistent) {
        virDomainRemoveInactive(&privconn->domains,
                                privdom);
1764
        privdom = NULL;
1765
    }
1766

1767
    ret = 0;
1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778
cleanup:
    VIR_FREE(xml);

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

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

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

1839
    testDriverLock(privconn);
1840 1841
    def = virDomainDefParseString(conn, privconn->caps, xml,
                                  VIR_DOMAIN_XML_INACTIVE);
1842
    if (!def)
1843
        goto cleanup;
1844

1845 1846 1847
    if (virDomainObjIsDuplicate(&privconn->domains, def, 1) < 0)
        goto cleanup;

1848
    if (testDomainGenerateIfnames(conn, def) < 0)
1849
        goto cleanup;
1850 1851
    if (!(dom = virDomainAssignDef(conn, privconn->caps,
                                   &privconn->domains, def)))
1852 1853
        goto cleanup;
    def = NULL;
1854

1855 1856 1857
    if (testDomainStartState(conn, dom) < 0)
        goto cleanup;

1858 1859 1860
    event = virDomainEventNewFromObj(dom,
                                     VIR_DOMAIN_EVENT_STARTED,
                                     VIR_DOMAIN_EVENT_STARTED_RESTORED);
1861
    ret = 0;
1862 1863 1864 1865 1866 1867

cleanup:
    virDomainDefFree(def);
    VIR_FREE(xml);
    if (fd != -1)
        close(fd);
1868 1869
    if (dom)
        virDomainObjUnlock(dom);
1870 1871
    if (event)
        testDomainEventQueue(privconn, event);
1872
    testDriverUnlock(privconn);
1873
    return ret;
1874 1875
}

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

1886
    testDriverLock(privconn);
1887 1888 1889 1890 1891
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);

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

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

1914 1915 1916 1917 1918 1919 1920 1921 1922 1923
    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;
        }
1924
    }
1925

1926
    ret = 0;
1927 1928 1929
cleanup:
    if (fd != -1)
        close(fd);
1930 1931
    if (privdom)
        virDomainObjUnlock(privdom);
1932 1933
    if (event)
        testDomainEventQueue(privconn, event);
1934
    testDriverUnlock(privconn);
1935
    return ret;
1936 1937
}

1938 1939 1940
static char *testGetOSType(virDomainPtr dom) {
    char *ret = strdup("linux");
    if (!ret)
1941
        virReportOOMError(dom->conn);
1942
    return ret;
1943 1944 1945
}

static unsigned long testGetMaxMemory(virDomainPtr domain) {
1946 1947
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
1948
    unsigned long ret = 0;
1949

1950
    testDriverLock(privconn);
1951 1952
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
1953
    testDriverUnlock(privconn);
1954 1955 1956

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

1960 1961 1962
    ret = privdom->def->maxmem;

cleanup:
1963 1964
    if (privdom)
        virDomainObjUnlock(privdom);
1965
    return ret;
1966 1967 1968 1969 1970
}

static int testSetMaxMemory(virDomainPtr domain,
                            unsigned long memory)
{
1971 1972
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
1973
    int ret = -1;
1974

1975
    testDriverLock(privconn);
1976 1977
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
1978
    testDriverUnlock(privconn);
1979 1980 1981

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

    /* XXX validate not over host memory wrt to other domains */
1986
    privdom->def->maxmem = memory;
1987 1988 1989
    ret = 0;

cleanup:
1990 1991
    if (privdom)
        virDomainObjUnlock(privdom);
1992
    return ret;
1993 1994
}

1995 1996 1997
static int testSetMemory(virDomainPtr domain,
                         unsigned long memory)
{
1998 1999
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
2000
    int ret = -1;
2001

2002
    testDriverLock(privconn);
2003 2004
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
2005
    testDriverUnlock(privconn);
2006 2007 2008

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

2012
    if (memory > privdom->def->maxmem) {
2013
        testError(domain->conn,
2014
                  VIR_ERR_INVALID_ARG, __FUNCTION__);
2015
        goto cleanup;
2016
    }
2017

2018
    privdom->def->memory = memory;
2019 2020 2021
    ret = 0;

cleanup:
2022 2023
    if (privdom)
        virDomainObjUnlock(privdom);
2024
    return ret;
2025 2026
}

C
Cole Robinson 已提交
2027 2028 2029 2030 2031
static int testDomainGetMaxVcpus(virDomainPtr domain)
{
    return testGetMaxVCPUs(domain->conn, "test");
}

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

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

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

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

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

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

2067 2068 2069 2070
    /* Update VCPU state for the running domain */
    if (testDomainUpdateVCPUs(domain->conn, privdom, nrCpus, 0) < 0)
        goto cleanup;

2071
    privdom->def->vcpus = nrCpus;
2072 2073 2074
    ret = 0;

cleanup:
2075 2076
    if (privdom)
        virDomainObjUnlock(privdom);
2077
    return ret;
2078 2079
}

C
Cole Robinson 已提交
2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167
static int testDomainGetVcpus(virDomainPtr domain,
                              virVcpuInfoPtr info,
                              int maxinfo,
                              unsigned char *cpumaps,
                              int maplen)
{
    testConnPtr privconn = domain->conn->privateData;
    testDomainObjPrivatePtr privdomdata;
    virDomainObjPtr privdom;
    int i, v, maxcpu, hostcpus;
    int ret = -1;
    struct timeval tv;
    unsigned long long statbase;

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

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

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

    privdomdata = privdom->privateData;

    if (gettimeofday(&tv, NULL) < 0) {
        virReportSystemError(domain->conn, errno,
                             "%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 已提交
2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224
static int testDomainPinVcpu(virDomainPtr domain,
                             unsigned int vcpu,
                             unsigned char *cpumap,
                             int maplen)
{
    testConnPtr privconn = domain->conn->privateData;
    testDomainObjPrivatePtr privdomdata;
    virDomainObjPtr privdom;
    unsigned char *privcpumap;
    int i, maxcpu, hostcpus, privmaplen;
    int ret = -1;

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

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

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

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

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

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

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

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

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

2225
static char *testDomainDumpXML(virDomainPtr domain, int flags)
2226
{
2227
    testConnPtr privconn = domain->conn->privateData;
2228
    virDomainDefPtr def;
2229
    virDomainObjPtr privdom;
2230 2231
    char *ret = NULL;

2232 2233 2234 2235 2236 2237
    testDriverLock(privconn);
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
    testDriverUnlock(privconn);

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

2242 2243
    def = (flags & VIR_DOMAIN_XML_INACTIVE) &&
        privdom->newDef ? privdom->newDef : privdom->def;
2244

2245 2246 2247 2248 2249
    ret = virDomainDefFormat(domain->conn,
                             def,
                             flags);

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

2255
static int testNumOfDefinedDomains(virConnectPtr conn) {
2256
    testConnPtr privconn = conn->privateData;
2257
    int count;
2258

2259
    testDriverLock(privconn);
2260
    count = virDomainObjListNumOfDomains(&privconn->domains, 0);
2261
    testDriverUnlock(privconn);
2262

2263
    return count;
2264 2265
}

2266 2267 2268
static int testListDefinedDomains(virConnectPtr conn,
                                  char **const names,
                                  int maxnames) {
2269

2270
    testConnPtr privconn = conn->privateData;
2271
    int n;
2272

2273
    testDriverLock(privconn);
2274
    memset(names, 0, sizeof(*names)*maxnames);
2275
    n = virDomainObjListGetInactiveNames(&privconn->domains, names, maxnames);
2276
    testDriverUnlock(privconn);
2277

2278
    return n;
2279 2280
}

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

2290
    testDriverLock(privconn);
2291 2292
    if ((def = virDomainDefParseString(conn, privconn->caps, xml,
                                       VIR_DOMAIN_XML_INACTIVE)) == NULL)
2293
        goto cleanup;
2294

2295 2296 2297
    if ((dupVM = virDomainObjIsDuplicate(&privconn->domains, def, 0)) < 0)
        goto cleanup;

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

2306 2307
    event = virDomainEventNewFromObj(dom,
                                     VIR_DOMAIN_EVENT_DEFINED,
2308 2309 2310
                                     !dupVM ?
                                     VIR_DOMAIN_EVENT_DEFINED_ADDED :
                                     VIR_DOMAIN_EVENT_DEFINED_UPDATED);
2311

2312
    ret = virGetDomain(conn, dom->def->name, dom->def->uuid);
2313
    if (ret)
2314
        ret->id = dom->def->id;
2315 2316 2317

cleanup:
    virDomainDefFree(def);
2318 2319
    if (dom)
        virDomainObjUnlock(dom);
2320 2321
    if (event)
        testDomainEventQueue(privconn, event);
2322
    testDriverUnlock(privconn);
2323
    return ret;
2324 2325
}

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

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

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

2347
cleanup:
2348
    testDriverUnlock(privconn);
2349
    return ret;
2350 2351 2352
}


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

2359
    testDriverLock(privconn);
2360 2361 2362 2363 2364
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);

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

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

2374 2375 2376 2377
    if (testDomainStartState(domain->conn, privdom) < 0)
        goto cleanup;
    domain->id = privdom->def->id;

2378 2379 2380
    event = virDomainEventNewFromObj(privdom,
                                     VIR_DOMAIN_EVENT_STARTED,
                                     VIR_DOMAIN_EVENT_STARTED_BOOTED);
2381
    ret = 0;
2382

2383
cleanup:
2384 2385
    if (privdom)
        virDomainObjUnlock(privdom);
2386 2387
    if (event)
        testDomainEventQueue(privconn, event);
2388
    testDriverUnlock(privconn);
2389
    return ret;
2390 2391 2392
}

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

2398
    testDriverLock(privconn);
2399 2400 2401 2402 2403
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);

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

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

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

2422
cleanup:
2423 2424
    if (privdom)
        virDomainObjUnlock(privdom);
2425 2426
    if (event)
        testDomainEventQueue(privconn, event);
2427
    testDriverUnlock(privconn);
2428
    return ret;
2429 2430
}

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

2438
    testDriverLock(privconn);
2439 2440
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
2441
    testDriverUnlock(privconn);
2442 2443 2444

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

2448
    *autostart = privdom->autostart;
2449 2450 2451
    ret = 0;

cleanup:
2452 2453
    if (privdom)
        virDomainObjUnlock(privdom);
2454
    return ret;
2455 2456 2457 2458 2459 2460
}


static int testDomainSetAutostart(virDomainPtr domain,
                                  int autostart)
{
2461 2462
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
2463
    int ret = -1;
2464

2465
    testDriverLock(privconn);
2466 2467
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
2468
    testDriverUnlock(privconn);
2469 2470 2471

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

2475
    privdom->autostart = autostart ? 1 : 0;
2476 2477 2478
    ret = 0;

cleanup:
2479 2480
    if (privdom)
        virDomainObjUnlock(privdom);
2481
    return ret;
2482
}
2483

2484 2485 2486
static char *testDomainGetSchedulerType(virDomainPtr domain,
                                        int *nparams)
{
2487 2488
    char *type = NULL;

2489 2490
    *nparams = 1;
    type = strdup("fair");
2491
    if (!type)
2492
        virReportOOMError(domain->conn);
2493

2494 2495 2496 2497 2498 2499 2500
    return type;
}

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

2505
    testDriverLock(privconn);
2506 2507
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
2508
    testDriverUnlock(privconn);
2509 2510 2511

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

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

cleanup:
2527 2528
    if (privdom)
        virDomainObjUnlock(privdom);
2529
    return ret;
2530
}
2531 2532


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

2541
    testDriverLock(privconn);
2542 2543
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
2544
    testDriverUnlock(privconn);
2545 2546 2547

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

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

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

2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684
static int testDomainBlockStats(virDomainPtr domain,
                                const char *path,
                                struct _virDomainBlockStats *stats)
{
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
    struct timeval tv;
    unsigned long long statbase;
    int i, found = 0, ret = -1;

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

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

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

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

    if (gettimeofday(&tv, NULL) < 0) {
        virReportSystemError(domain->conn, errno,
                             "%s", _("getting time of day"));
        goto error;
    }

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

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

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

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

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

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

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

    if (gettimeofday(&tv, NULL) < 0) {
        virReportSystemError(domain->conn, errno,
                             "%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;
}

2685
static virDrvOpenStatus testOpenNetwork(virConnectPtr conn,
2686
                                        virConnectAuthPtr auth ATTRIBUTE_UNUSED,
2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703
                                        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)
{
2704 2705
    testConnPtr privconn = conn->privateData;
    virNetworkObjPtr net;
2706
    virNetworkPtr ret = NULL;
2707

2708 2709 2710 2711 2712
    testDriverLock(privconn);
    net = virNetworkFindByUUID(&privconn->networks, uuid);
    testDriverUnlock(privconn);

    if (net == NULL) {
2713
        testError (conn, VIR_ERR_NO_NETWORK, NULL);
2714
        goto cleanup;
2715 2716
    }

2717 2718 2719
    ret = virGetNetwork(conn, net->def->name, net->def->uuid);

cleanup:
2720 2721
    if (net)
        virNetworkObjUnlock(net);
2722
    return ret;
2723
}
2724

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

2732 2733 2734 2735 2736
    testDriverLock(privconn);
    net = virNetworkFindByName(&privconn->networks, name);
    testDriverUnlock(privconn);

    if (net == NULL) {
2737
        testError (conn, VIR_ERR_NO_NETWORK, NULL);
2738
        goto cleanup;
2739 2740
    }

2741 2742 2743
    ret = virGetNetwork(conn, net->def->name, net->def->uuid);

cleanup:
2744 2745
    if (net)
        virNetworkObjUnlock(net);
2746
    return ret;
2747 2748 2749 2750
}


static int testNumNetworks(virConnectPtr conn) {
2751
    testConnPtr privconn = conn->privateData;
2752
    int numActive = 0, i;
2753

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

2763
    return numActive;
2764 2765 2766
}

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

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

2783 2784 2785
    return n;

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

static int testNumDefinedNetworks(virConnectPtr conn) {
2794
    testConnPtr privconn = conn->privateData;
2795
    int numInactive = 0, i;
2796

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

2806
    return numInactive;
2807 2808 2809
}

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

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

2826 2827 2828
    return n;

no_memory:
2829
    virReportOOMError(conn);
2830 2831
    for (n = 0 ; n < nnames ; n++)
        VIR_FREE(names[n]);
2832
    testDriverUnlock(privconn);
2833
    return -1;
2834 2835
}

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

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

    testDriverLock(privconn);
    obj = virNetworkFindByUUID(&privconn->networks, net->uuid);
    testDriverUnlock(privconn);
    if (!obj) {
        testError(net->conn, VIR_ERR_NO_NETWORK, NULL);
        goto cleanup;
    }
    ret = virNetworkObjIsActive(obj);

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

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

    testDriverLock(privconn);
    obj = virNetworkFindByUUID(&privconn->networks, net->uuid);
    testDriverUnlock(privconn);
    if (!obj) {
        testError(net->conn, VIR_ERR_NO_NETWORK, NULL);
        goto cleanup;
    }
    ret = obj->persistent;

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


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

2886
    testDriverLock(privconn);
2887
    if ((def = virNetworkDefParseString(conn, xml)) == NULL)
2888
        goto cleanup;
2889

2890
    if ((net = virNetworkAssignDef(conn, &privconn->networks, def)) == NULL)
2891 2892
        goto cleanup;
    def = NULL;
2893
    net->active = 1;
2894

2895
    ret = virGetNetwork(conn, net->def->name, net->def->uuid);
2896

2897 2898
cleanup:
    virNetworkDefFree(def);
2899 2900 2901
    if (net)
        virNetworkObjUnlock(net);
    testDriverUnlock(privconn);
2902
    return ret;
2903 2904 2905
}

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

2911
    testDriverLock(privconn);
2912
    if ((def = virNetworkDefParseString(conn, xml)) == NULL)
2913
        goto cleanup;
2914

2915
    if ((net = virNetworkAssignDef(conn, &privconn->networks, def)) == NULL)
2916 2917
        goto cleanup;
    def = NULL;
2918
    net->persistent = 1;
2919

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

cleanup:
    virNetworkDefFree(def);
2924 2925 2926
    if (net)
        virNetworkObjUnlock(net);
    testDriverUnlock(privconn);
2927
    return ret;
2928 2929 2930
}

static int testNetworkUndefine(virNetworkPtr network) {
2931 2932
    testConnPtr privconn = network->conn->privateData;
    virNetworkObjPtr privnet;
2933
    int ret = -1;
2934

2935
    testDriverLock(privconn);
2936 2937 2938 2939 2940
    privnet = virNetworkFindByName(&privconn->networks,
                                   network->name);

    if (privnet == NULL) {
        testError(network->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
2941
        goto cleanup;
2942
    }
2943

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

2950 2951
    virNetworkRemoveInactive(&privconn->networks,
                             privnet);
2952
    privnet = NULL;
2953
    ret = 0;
2954

2955
cleanup:
2956 2957 2958
    if (privnet)
        virNetworkObjUnlock(privnet);
    testDriverUnlock(privconn);
2959
    return ret;
2960 2961 2962
}

static int testNetworkStart(virNetworkPtr network) {
2963 2964
    testConnPtr privconn = network->conn->privateData;
    virNetworkObjPtr privnet;
2965
    int ret = -1;
2966

2967
    testDriverLock(privconn);
2968 2969
    privnet = virNetworkFindByName(&privconn->networks,
                                   network->name);
2970
    testDriverUnlock(privconn);
2971 2972 2973

    if (privnet == NULL) {
        testError(network->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
2974
        goto cleanup;
2975
    }
2976

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

2983
    privnet->active = 1;
2984
    ret = 0;
2985

2986
cleanup:
2987 2988
    if (privnet)
        virNetworkObjUnlock(privnet);
2989
    return ret;
2990 2991 2992
}

static int testNetworkDestroy(virNetworkPtr network) {
2993 2994
    testConnPtr privconn = network->conn->privateData;
    virNetworkObjPtr privnet;
2995
    int ret = -1;
2996

2997
    testDriverLock(privconn);
2998 2999 3000 3001 3002
    privnet = virNetworkFindByName(&privconn->networks,
                                   network->name);

    if (privnet == NULL) {
        testError(network->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
3003
        goto cleanup;
3004
    }
3005

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

cleanup:
3015 3016 3017
    if (privnet)
        virNetworkObjUnlock(privnet);
    testDriverUnlock(privconn);
3018
    return ret;
3019 3020 3021
}

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

3026
    testDriverLock(privconn);
3027 3028
    privnet = virNetworkFindByName(&privconn->networks,
                                   network->name);
3029
    testDriverUnlock(privconn);
3030 3031 3032

    if (privnet == NULL) {
        testError(network->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
3033
        goto cleanup;
3034
    }
3035

3036 3037 3038
    ret = virNetworkDefFormat(network->conn, privnet->def);

cleanup:
3039 3040
    if (privnet)
        virNetworkObjUnlock(privnet);
3041
    return ret;
3042 3043 3044
}

static char *testNetworkGetBridgeName(virNetworkPtr network) {
3045
    testConnPtr privconn = network->conn->privateData;
3046
    char *bridge = NULL;
3047 3048
    virNetworkObjPtr privnet;

3049
    testDriverLock(privconn);
3050 3051
    privnet = virNetworkFindByName(&privconn->networks,
                                   network->name);
3052
    testDriverUnlock(privconn);
3053 3054 3055

    if (privnet == NULL) {
        testError(network->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
3056
        goto cleanup;
3057 3058
    }

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

    if (!(bridge = strdup(privnet->def->bridge))) {
3067
        virReportOOMError(network->conn);
3068
        goto cleanup;
3069
    }
3070 3071

cleanup:
3072 3073
    if (privnet)
        virNetworkObjUnlock(privnet);
3074 3075 3076 3077 3078
    return bridge;
}

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

3083
    testDriverLock(privconn);
3084 3085
    privnet = virNetworkFindByName(&privconn->networks,
                                   network->name);
3086
    testDriverUnlock(privconn);
3087 3088 3089

    if (privnet == NULL) {
        testError(network->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
3090
        goto cleanup;
3091 3092
    }

3093
    *autostart = privnet->autostart;
3094 3095 3096
    ret = 0;

cleanup:
3097 3098
    if (privnet)
        virNetworkObjUnlock(privnet);
3099
    return ret;
3100 3101 3102 3103
}

static int testNetworkSetAutostart(virNetworkPtr network,
                                   int autostart) {
3104 3105
    testConnPtr privconn = network->conn->privateData;
    virNetworkObjPtr privnet;
3106
    int ret = -1;
3107

3108
    testDriverLock(privconn);
3109 3110
    privnet = virNetworkFindByName(&privconn->networks,
                                   network->name);
3111
    testDriverUnlock(privconn);
3112 3113 3114

    if (privnet == NULL) {
        testError(network->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
3115
        goto cleanup;
3116 3117
    }

3118
    privnet->autostart = autostart ? 1 : 0;
3119 3120 3121
    ret = 0;

cleanup:
3122 3123
    if (privnet)
        virNetworkObjUnlock(privnet);
3124
    return ret;
3125
}
3126

C
Cole Robinson 已提交
3127

L
Laine Stump 已提交
3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157
/*
 * 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 已提交
3158
        if (virInterfaceObjIsActive(privconn->ifaces.objs[i])) {
L
Laine Stump 已提交
3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175
            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 已提交
3176
        if (virInterfaceObjIsActive(privconn->ifaces.objs[i])) {
L
Laine Stump 已提交
3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203
            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:
    virReportOOMError(conn);
    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 已提交
3204
        if (!virInterfaceObjIsActive(privconn->ifaces.objs[i])) {
L
Laine Stump 已提交
3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221
            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 已提交
3222
        if (!virInterfaceObjIsActive(privconn->ifaces.objs[i])) {
L
Laine Stump 已提交
3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295
            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:
    virReportOOMError(conn);
    for (n = 0 ; n < nnames ; n++)
        VIR_FREE(names[n]);
    testDriverUnlock(privconn);
    return -1;
}

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

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

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

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

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

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

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

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

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

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

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

3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317
static int testInterfaceIsActive(virInterfacePtr iface)
{
    testConnPtr privconn = iface->conn->privateData;
    virInterfaceObjPtr obj;
    int ret = -1;

    testDriverLock(privconn);
    obj = virInterfaceFindByName(&privconn->ifaces, iface->name);
    testDriverUnlock(privconn);
    if (!obj) {
        testError(iface->conn, VIR_ERR_NO_INTERFACE, NULL);
        goto cleanup;
    }
    ret = virInterfaceObjIsActive(obj);

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


L
Laine Stump 已提交
3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457
static char *testInterfaceGetXMLDesc(virInterfacePtr iface,
                                     unsigned int flags ATTRIBUTE_UNUSED)
{
    testConnPtr privconn = iface->conn->privateData;
    virInterfaceObjPtr privinterface;
    char *ret = NULL;

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

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

    ret = virInterfaceDefFormat(iface->conn, privinterface->def);

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);
    if ((def = virInterfaceDefParseString(conn, xmlStr)) == NULL)
        goto cleanup;

    if ((iface = virInterfaceAssignDef(conn, &privconn->ifaces, def)) == NULL)
        goto cleanup;
    def = NULL;

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

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

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

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

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

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

cleanup:
    testDriverUnlock(privconn);
    return ret;
}

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

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

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

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

    privinterface->active = 1;
    ret = 0;

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

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

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

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

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

    privinterface->active = 0;
    ret = 0;

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



C
Cole Robinson 已提交
3458 3459 3460 3461
/*
 * Storage Driver routines
 */

3462

3463 3464
static int testStoragePoolObjSetDefaults(virConnectPtr conn,
                                         virStoragePoolObjPtr pool) {
C
Cole Robinson 已提交
3465 3466 3467 3468 3469 3470 3471

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

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

    return 0;
}

3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493
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;
}

3494

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

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

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

3511 3512 3513
    ret = virGetStoragePool(conn, pool->def->name, pool->def->uuid);

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

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

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

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

3535 3536 3537
    ret = virGetStoragePool(conn, pool->def->name, pool->def->uuid);

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

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

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

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

    return numActive;
}

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

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

    return n;

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

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

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

    return numInactive;
}

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

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

    return n;

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


3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683
static int testStoragePoolIsActive(virStoragePoolPtr pool)
{
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr obj;
    int ret = -1;

    testDriverLock(privconn);
    obj = virStoragePoolObjFindByUUID(&privconn->pools, pool->uuid);
    testDriverUnlock(privconn);
    if (!obj) {
        testError(pool->conn, VIR_ERR_NO_STORAGE_POOL, NULL);
        goto cleanup;
    }
    ret = virStoragePoolObjIsActive(obj);

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

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

    testDriverLock(privconn);
    obj = virStoragePoolObjFindByUUID(&privconn->pools, pool->uuid);
    testDriverUnlock(privconn);
    if (!obj) {
        testError(pool->conn, VIR_ERR_NO_STORAGE_POOL, NULL);
        goto cleanup;
    }
    ret = obj->configFile ? 1 : 0;

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



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

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

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

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

    privpool->active = 1;
3708
    ret = 0;
C
Cole Robinson 已提交
3709

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

static char *
3717 3718 3719
testStorageFindPoolSources(virConnectPtr conn,
                           const char *type,
                           const char *srcSpec,
C
Cole Robinson 已提交
3720 3721
                           unsigned int flags ATTRIBUTE_UNUSED)
{
3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766
    virStoragePoolSourcePtr source = NULL;
    int pool_type;
    char *ret = NULL;

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

    if (srcSpec) {
        source = virStoragePoolDefParseSourceString(conn, srcSpec, pool_type);
        if (!source)
            goto cleanup;
    }

    switch (pool_type) {

    case VIR_STORAGE_POOL_LOGICAL:
        ret = strdup(defaultPoolSourcesLogicalXML);
        if (!ret)
            virReportOOMError(conn);
        break;

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

        if (virAsprintf(&ret, defaultPoolSourcesNetFSXML,
                        source->host.name) < 0)
            virReportOOMError(conn);
        break;

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

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


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

3779
    testDriverLock(privconn);
3780
    if (!(def = virStoragePoolDefParseString(conn, xml)))
3781
        goto cleanup;
C
Cole Robinson 已提交
3782

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

3792
    if (!(pool = virStoragePoolObjAssignDef(conn, &privconn->pools, def)))
3793
        goto cleanup;
3794
    def = NULL;
C
Cole Robinson 已提交
3795

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

3803 3804 3805 3806
    ret = virGetStoragePool(conn, pool->def->name, pool->def->uuid);

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

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

3822
    testDriverLock(privconn);
3823
    if (!(def = virStoragePoolDefParseString(conn, xml)))
3824
        goto cleanup;
C
Cole Robinson 已提交
3825 3826 3827 3828 3829

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

3830
    if (!(pool = virStoragePoolObjAssignDef(conn, &privconn->pools, def)))
3831 3832
        goto cleanup;
    def = NULL;
C
Cole Robinson 已提交
3833

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

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

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

static int
3851 3852 3853
testStoragePoolUndefine(virStoragePoolPtr pool) {
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
3854
    int ret = -1;
3855

3856
    testDriverLock(privconn);
3857 3858 3859 3860 3861
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);

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

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

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

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

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

3888
    testDriverLock(privconn);
3889 3890
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
3891
    testDriverUnlock(privconn);
3892 3893 3894

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

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

3905
cleanup:
3906 3907
    if (privpool)
        virStoragePoolObjUnlock(privpool);
3908
    return ret;
C
Cole Robinson 已提交
3909 3910 3911 3912
}


static int
3913 3914 3915
testStoragePoolDestroy(virStoragePoolPtr pool) {
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
3916
    int ret = -1;
3917

3918
    testDriverLock(privconn);
3919 3920 3921 3922 3923
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);

    if (privpool == NULL) {
        testError(pool->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
3924
        goto cleanup;
3925 3926 3927 3928 3929
    }

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

    privpool->active = 0;

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

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


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

3956
    testDriverLock(privconn);
3957 3958
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
3959
    testDriverUnlock(privconn);
3960 3961 3962

    if (privpool == NULL) {
        testError(pool->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
3963 3964 3965 3966 3967 3968 3969
        goto cleanup;
    }

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

3972
    ret = 0;
C
Cole Robinson 已提交
3973

3974
cleanup:
3975 3976
    if (privpool)
        virStoragePoolObjUnlock(privpool);
3977
    return ret;
C
Cole Robinson 已提交
3978 3979 3980 3981
}


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

3988
    testDriverLock(privconn);
3989 3990
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
3991
    testDriverUnlock(privconn);
3992 3993 3994

    if (privpool == NULL) {
        testError(pool->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
3995
        goto cleanup;
3996 3997 3998 3999 4000
    }

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

4005
cleanup:
4006 4007
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4008
    return ret;
C
Cole Robinson 已提交
4009 4010 4011 4012
}


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

4019
    testDriverLock(privconn);
4020 4021
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4022
    testDriverUnlock(privconn);
4023 4024 4025

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

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

4039
cleanup:
4040 4041
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4042
    return ret;
C
Cole Robinson 已提交
4043 4044 4045
}

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

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

4057 4058
    if (privpool == NULL) {
        testError(pool->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
4059
        goto cleanup;
4060 4061
    }

4062 4063 4064
    ret = virStoragePoolDefFormat(pool->conn, privpool->def);

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

static int
4071
testStoragePoolGetAutostart(virStoragePoolPtr pool,
C
Cole Robinson 已提交
4072
                            int *autostart) {
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 4083

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

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

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

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

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

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

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

    autostart = (autostart != 0);
    privpool->autostart = autostart;
4125 4126 4127
    ret = 0;

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


static int
4135 4136 4137
testStoragePoolNumVolumes(virStoragePoolPtr pool) {
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4138
    int ret = -1;
4139

4140
    testDriverLock(privconn);
4141 4142
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4143
    testDriverUnlock(privconn);
4144 4145 4146

    if (privpool == NULL) {
        testError(pool->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
4147
        goto cleanup;
4148 4149 4150 4151 4152
    }

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

4156 4157 4158
    ret = privpool->volumes.count;

cleanup:
4159 4160
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4161
    return ret;
C
Cole Robinson 已提交
4162 4163 4164
}

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

4172
    memset(names, 0, maxnames * sizeof(*names));
4173 4174

    testDriverLock(privconn);
4175 4176
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4177
    testDriverUnlock(privconn);
4178 4179 4180

    if (privpool == NULL) {
        testError(pool->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
4181
        goto cleanup;
4182 4183 4184 4185 4186 4187
    }


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

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

4198
    virStoragePoolObjUnlock(privpool);
C
Cole Robinson 已提交
4199 4200 4201 4202 4203 4204
    return n;

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

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


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

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

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


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

    privvol = virStorageVolDefFindByName(privpool, name);

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

4245 4246 4247 4248
    ret = virGetStorageVol(pool->conn, privpool->def->name,
                           privvol->name, privvol->key);

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


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

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

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

4282 4283 4284 4285 4286
    if (!ret)
        testError(conn, VIR_ERR_INVALID_STORAGE_VOL,
                  _("no storage vol with matching key '%s'"), key);

    return ret;
C
Cole Robinson 已提交
4287 4288 4289 4290 4291
}

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

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

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

4316 4317 4318 4319 4320
    if (!ret)
        testError(conn, VIR_ERR_INVALID_STORAGE_VOL,
                  _("no storage vol with matching path '%s'"), path);

    return ret;
C
Cole Robinson 已提交
4321 4322 4323
}

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

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

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

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

4348
    privvol = virStorageVolDefParseString(pool->conn, privpool->def, xmldesc);
4349
    if (privvol == NULL)
4350
        goto cleanup;
4351 4352

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

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

    if (VIR_REALLOC_N(privpool->volumes.objs,
                      privpool->volumes.count+1) < 0) {
4369
        virReportOOMError(pool->conn);
4370
        goto cleanup;
C
Cole Robinson 已提交
4371 4372
    }

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

4380 4381
    privvol->key = strdup(privvol->target.path);
    if (privvol->key == NULL) {
4382
        virReportOOMError(pool->conn);
4383
        goto cleanup;
C
Cole Robinson 已提交
4384 4385
    }

4386
    privpool->def->allocation += privvol->allocation;
C
Cole Robinson 已提交
4387 4388 4389
    privpool->def->available = (privpool->def->capacity -
                                privpool->def->allocation);

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

4392 4393
    ret = virGetStorageVol(pool->conn, privpool->def->name,
                           privvol->name, privvol->key);
4394
    privvol = NULL;
4395 4396 4397

cleanup:
    virStorageVolDefFree(privvol);
4398 4399
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4400
    return ret;
C
Cole Robinson 已提交
4401 4402
}

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

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

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

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

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

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

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

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

    if (VIR_REALLOC_N(privpool->volumes.objs,
                      privpool->volumes.count+1) < 0) {
        virReportOOMError(pool->conn);
        goto cleanup;
    }

4464 4465 4466
    if (virAsprintf(&privvol->target.path, "%s/%s",
                    privpool->def->target.path,
                    privvol->name) == -1) {
4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493
        virReportOOMError(pool->conn);
        goto cleanup;
    }

    privvol->key = strdup(privvol->target.path);
    if (privvol->key == NULL) {
        virReportOOMError(pool->conn);
        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 已提交
4494
static int
4495
testStorageVolumeDelete(virStorageVolPtr vol,
C
Cole Robinson 已提交
4496
                        unsigned int flags ATTRIBUTE_UNUSED) {
4497 4498 4499
    testConnPtr privconn = vol->conn->privateData;
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol;
C
Cole Robinson 已提交
4500
    int i;
4501
    int ret = -1;
C
Cole Robinson 已提交
4502

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

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


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

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

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


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

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


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

4582
    testDriverLock(privconn);
4583 4584
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           vol->pool);
4585
    testDriverUnlock(privconn);
4586 4587 4588

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

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

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

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

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

4613
cleanup:
4614 4615
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4616
    return ret;
C
Cole Robinson 已提交
4617 4618 4619
}

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

4627
    testDriverLock(privconn);
4628 4629
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           vol->pool);
4630
    testDriverUnlock(privconn);
4631 4632 4633

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

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

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

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

4652 4653 4654
    ret = virStorageVolDefFormat(vol->conn, privpool->def, privvol);

cleanup:
4655 4656
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4657
    return ret;
C
Cole Robinson 已提交
4658 4659 4660
}

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

4667
    testDriverLock(privconn);
4668 4669
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           vol->pool);
4670
    testDriverUnlock(privconn);
4671 4672 4673

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

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

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

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

C
Cole Robinson 已提交
4692
    ret = strdup(privvol->target.path);
4693
    if (ret == NULL)
4694
        virReportOOMError(vol->conn);
4695 4696

cleanup:
4697 4698
    if (privpool)
        virStoragePoolObjUnlock(privpool);
C
Cole Robinson 已提交
4699 4700 4701
    return ret;
}

4702

4703
/* Node device implementations */
4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718
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;
}

4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 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
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) {
        virNodeDeviceReportError(conn, VIR_ERR_NO_NODE_DEVICE, NULL);
        goto cleanup;
    }

    ret = virGetNodeDevice(conn, name);

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

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

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

    if (!obj) {
        virNodeDeviceReportError(dev->conn, VIR_ERR_NO_NODE_DEVICE,
                                _("no node device with matching name '%s'"),
                                 dev->name);
        goto cleanup;
    }

    ret = virNodeDeviceDefFormat(dev->conn, obj->def);

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) {
        virNodeDeviceReportError(dev->conn, VIR_ERR_NO_NODE_DEVICE,
                                _("no node device with matching name '%s'"),
                                 dev->name);
        goto cleanup;
    }

    if (obj->def->parent) {
        ret = strdup(obj->def->parent);
        if (!ret)
            virReportOOMError(dev->conn);
    } else {
        virNodeDeviceReportError(dev->conn, VIR_ERR_INTERNAL_ERROR,
                                 "%s", _("no parent for this device"));
    }

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

4857

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

4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 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 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051
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);

    def = virNodeDeviceDefParseString(conn, xmlDesc, CREATE_DEVICE);
    if (def == NULL) {
        goto cleanup;
    }

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

    if (virNodeDeviceGetParentHost(conn,
                                   &driver->devs,
                                   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))) {
        virReportOOMError(conn);
        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;
    }


    if (!(obj = virNodeDeviceAssignDef(conn, &driver->devs, def))) {
        goto cleanup;
    }
    virNodeDeviceObjUnlock(obj);

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

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

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

    if (!obj) {
        virNodeDeviceReportError(dev->conn, VIR_ERR_NO_NODE_DEVICE, NULL);
        goto out;
    }

    if (virNodeDeviceGetWWNs(dev->conn, obj->def, &wwnn, &wwpn) == -1) {
        goto out;
    }

    parent_name = strdup(obj->def->parent);
    if (parent_name == NULL) {
        virReportOOMError(dev->conn);
        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 */
    if (virNodeDeviceGetParentHost(dev->conn,
                                   &driver->devs,
                                   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;
}

5052 5053

/* Domain event implementations */
5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149
static int
testDomainEventRegister (virConnectPtr conn,
                         virConnectDomainEventCallback callback,
                         void *opaque,
                         virFreeCallback freecb)
{
    testConnPtr driver = conn->privateData;
    int ret;

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

    return ret;
}

static int
testDomainEventDeregister (virConnectPtr conn,
                           virConnectDomainEventCallback callback)
{
    testConnPtr driver = conn->privateData;
    int ret;

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

    return ret;
}

static void testDomainEventDispatchFunc(virConnectPtr conn,
                                        virDomainEventPtr event,
                                        virConnectDomainEventCallback cb,
                                        void *cbopaque,
                                        void *opaque)
{
    testConnPtr driver = opaque;

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

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

    testDriverLock(driver);

    driver->domainEventDispatching = 1;

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

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

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

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


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

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

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

5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163
static virDrvOpenStatus testSecretOpen(virConnectPtr conn,
                                       virConnectAuthPtr auth ATTRIBUTE_UNUSED,
                                       int flags ATTRIBUTE_UNUSED) {
    if (STRNEQ(conn->driver->name, "Test"))
        return VIR_DRV_OPEN_DECLINED;

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

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

5165 5166 5167 5168 5169
static virDriver testDriver = {
    VIR_DRV_TEST,
    "Test",
    testOpen, /* open */
    testClose, /* close */
5170
    NULL, /* supports_feature */
5171 5172
    NULL, /* type */
    testGetVersion, /* version */
5173
    NULL, /* libvirtVersion (impl. in libvirt.c) */
5174
    virGetHostname, /* getHostname */
5175 5176 5177 5178 5179
    testGetMaxVCPUs, /* getMaxVcpus */
    testNodeGetInfo, /* nodeGetInfo */
    testGetCapabilities, /* getCapabilities */
    testListDomains, /* listDomains */
    testNumOfDomains, /* numOfDomains */
5180
    testDomainCreateXML, /* domainCreateXML */
5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197
    testLookupDomainByID, /* domainLookupByID */
    testLookupDomainByUUID, /* domainLookupByUUID */
    testLookupDomainByName, /* domainLookupByName */
    testPauseDomain, /* domainSuspend */
    testResumeDomain, /* domainResume */
    testShutdownDomain, /* domainShutdown */
    testRebootDomain, /* domainReboot */
    testDestroyDomain, /* domainDestroy */
    testGetOSType, /* domainGetOSType */
    testGetMaxMemory, /* domainGetMaxMemory */
    testSetMaxMemory, /* domainSetMaxMemory */
    testSetMemory, /* domainSetMemory */
    testGetDomainInfo, /* domainGetInfo */
    testDomainSave, /* domainSave */
    testDomainRestore, /* domainRestore */
    testDomainCoreDump, /* domainCoreDump */
    testSetVcpus, /* domainSetVcpus */
C
Cole Robinson 已提交
5198
    testDomainPinVcpu, /* domainPinVcpu */
C
Cole Robinson 已提交
5199
    testDomainGetVcpus, /* domainGetVcpus */
C
Cole Robinson 已提交
5200
    testDomainGetMaxVcpus, /* domainGetMaxVcpus */
5201 5202
    NULL, /* domainGetSecurityLabel */
    NULL, /* nodeGetSecurityModel */
5203
    testDomainDumpXML, /* domainDumpXML */
5204 5205
    NULL, /* domainXMLFromNative */
    NULL, /* domainXMLToNative */
5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217
    testListDefinedDomains, /* listDefinedDomains */
    testNumOfDefinedDomains, /* numOfDefinedDomains */
    testDomainCreate, /* domainCreate */
    testDomainDefineXML, /* domainDefineXML */
    testDomainUndefine, /* domainUndefine */
    NULL, /* domainAttachDevice */
    NULL, /* domainDetachDevice */
    testDomainGetAutostart, /* domainGetAutostart */
    testDomainSetAutostart, /* domainSetAutostart */
    testDomainGetSchedulerType, /* domainGetSchedulerType */
    testDomainGetSchedulerParams, /* domainGetSchedulerParameters */
    testDomainSetSchedulerParams, /* domainSetSchedulerParameters */
5218 5219 5220
    NULL, /* domainMigratePrepare */
    NULL, /* domainMigratePerform */
    NULL, /* domainMigrateFinish */
5221 5222
    testDomainBlockStats, /* domainBlockStats */
    testDomainInterfaceStats, /* domainInterfaceStats */
R
Richard W.M. Jones 已提交
5223
    NULL, /* domainBlockPeek */
R
Richard W.M. Jones 已提交
5224
    NULL, /* domainMemoryPeek */
5225
    testNodeGetCellsFreeMemory, /* nodeGetCellsFreeMemory */
5226
    NULL, /* getFreeMemory */
5227 5228
    testDomainEventRegister, /* domainEventRegister */
    testDomainEventDeregister, /* domainEventDeregister */
D
Daniel Veillard 已提交
5229 5230
    NULL, /* domainMigratePrepare2 */
    NULL, /* domainMigrateFinish2 */
5231
    NULL, /* nodeDeviceDettach */
5232 5233
    NULL, /* nodeDeviceReAttach */
    NULL, /* nodeDeviceReset */
C
Chris Lalancette 已提交
5234
    NULL, /* domainMigratePrepareTunnel */
5235 5236 5237 5238
    testIsEncrypted, /* isEncrypted */
    testIsSecure, /* isEncrypted */
    testDomainIsActive, /* domainIsActive */
    testDomainIsPersistent, /* domainIsPersistent */
5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259
};

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

L
Laine Stump 已提交
5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278
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 */
5279
    testInterfaceIsActive,      /* interfaceIsActive */
L
Laine Stump 已提交
5280 5281 5282
};


5283 5284 5285 5286
static virStorageDriver testStorageDriver = {
    .name = "Test",
    .open = testStorageOpen,
    .close = testStorageClose,
C
Cole Robinson 已提交
5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314

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

    .volLookupByName = testStorageVolumeLookupByName,
    .volLookupByKey = testStorageVolumeLookupByKey,
    .volLookupByPath = testStorageVolumeLookupByPath,
    .volCreateXML = testStorageVolumeCreateXML,
5315
    .volCreateXMLFrom = testStorageVolumeCreateXMLFrom,
C
Cole Robinson 已提交
5316 5317 5318 5319
    .volDelete = testStorageVolumeDelete,
    .volGetInfo = testStorageVolumeGetInfo,
    .volGetXMLDesc = testStorageVolumeGetXMLDesc,
    .volGetPath = testStorageVolumeGetPath,
5320 5321
    .poolIsActive = testStoragePoolIsActive,
    .poolIsPersistent = testStoragePoolIsPersistent,
5322 5323
};

5324 5325 5326 5327
static virDeviceMonitor testDevMonitor = {
    .name = "Test",
    .open = testDevMonOpen,
    .close = testDevMonClose,
5328 5329 5330 5331 5332 5333 5334 5335

    .numOfDevices = testNodeNumOfDevices,
    .listDevices = testNodeListDevices,
    .deviceLookupByName = testNodeDeviceLookupByName,
    .deviceDumpXML = testNodeDeviceDumpXML,
    .deviceGetParent = testNodeDeviceGetParent,
    .deviceNumOfCaps = testNodeDeviceNumOfCaps,
    .deviceListCaps = testNodeDeviceListCaps,
5336 5337
    .deviceCreateXML = testNodeDeviceCreateXML,
    .deviceDestroy = testNodeDeviceDestroy,
5338 5339
};

5340 5341 5342 5343 5344
static virSecretDriver testSecretDriver = {
    .name = "Test",
    .open = testSecretOpen,
    .close = testSecretClose,
};
5345 5346


5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358
/**
 * testRegister:
 *
 * Registers the test driver
 */
int
testRegister(void)
{
    if (virRegisterDriver(&testDriver) < 0)
        return -1;
    if (virRegisterNetworkDriver(&testNetworkDriver) < 0)
        return -1;
L
Laine Stump 已提交
5359 5360
    if (virRegisterInterfaceDriver(&testInterfaceDriver) < 0)
        return -1;
5361 5362
    if (virRegisterStorageDriver(&testStorageDriver) < 0)
        return -1;
5363 5364
    if (virRegisterDeviceMonitor(&testDevMonitor) < 0)
        return -1;
5365 5366
    if (virRegisterSecretDriver(&testSecretDriver) < 0)
        return -1;
5367

5368 5369
    return 0;
}