test_driver.c 132.4 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 65 66
#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
67

68
struct _testConn {
69
    virMutex lock;
70

71 72
    char path[PATH_MAX];
    int nextDomID;
73
    virCapsPtr caps;
74
    virNodeInfo nodeInfo;
75
    virDomainObjList domains;
76
    virNetworkObjList networks;
L
Laine Stump 已提交
77
    virInterfaceObjList ifaces;
C
Cole Robinson 已提交
78
    virStoragePoolObjList pools;
79
    virNodeDeviceObjList devs;
80 81
    int numCells;
    testCell cells[MAX_CELLS];
82 83 84 85 86 87 88


    /* An array of callbacks */
    virDomainEventCallbackListPtr domainEventCallbacks;
    virDomainEventQueuePtr domainEventQueue;
    int domainEventTimer;
    int domainEventDispatching;
89 90 91
};
typedef struct _testConn testConn;
typedef struct _testConn *testConnPtr;
92

93
#define TEST_MODEL "i686"
94
#define TEST_MODEL_WORDSIZE 32
95
#define TEST_EMULATOR "/usr/bin/test-hv"
96

97
static const virNodeInfo defaultNodeInfo = {
98
    TEST_MODEL,
99 100 101 102 103 104 105
    1024*1024*3, /* 3 GB */
    16,
    1400,
    2,
    2,
    2,
    2,
106 107
};

108

109
#define testError(conn, code, fmt...)                               \
110
        virReportErrorHelper(conn, VIR_FROM_TEST, code, __FILE__, \
111
                               __FUNCTION__, __LINE__, fmt)
112

113 114 115 116 117 118
static int testClose(virConnectPtr conn);
static void testDomainEventFlush(int timer, void *opaque);
static void testDomainEventQueue(testConnPtr driver,
                                 virDomainEventPtr event);


119 120
static void testDriverLock(testConnPtr driver)
{
121
    virMutexLock(&driver->lock);
122 123 124 125
}

static void testDriverUnlock(testConnPtr driver)
{
126
    virMutexUnlock(&driver->lock);
127 128
}

129 130
static virCapsPtr
testBuildCapabilities(virConnectPtr conn) {
131
    testConnPtr privconn = conn->privateData;
132 133 134 135
    virCapsPtr caps;
    virCapsGuestPtr guest;
    const char *const guest_types[] = { "hvm", "xen" };
    int i;
136

137 138
    if ((caps = virCapabilitiesNew(TEST_MODEL, 0, 0)) == NULL)
        goto no_memory;
139

140 141 142 143
    if (virCapabilitiesAddHostFeature(caps, "pae") < 0)
        goto no_memory;
    if (virCapabilitiesAddHostFeature(caps ,"nonpae") < 0)
        goto no_memory;
144

145 146 147 148
    for (i = 0; i < privconn->numCells; i++) {
        if (virCapabilitiesAddHostNUMACell(caps, i, privconn->cells[i].numCpus,
                                           privconn->cells[i].cpus) < 0)
            goto no_memory;
149 150
    }

151 152 153 154 155 156 157 158 159 160
    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;
161

162 163 164 165 166 167 168
        if (virCapabilitiesAddGuestDomain(guest,
                                          "test",
                                          NULL,
                                          NULL,
                                          0,
                                          NULL) == NULL)
            goto no_memory;
169

170 171 172 173
        if (virCapabilitiesAddGuestFeature(guest, "pae", 1, 1) == NULL)
            goto no_memory;
        if (virCapabilitiesAddGuestFeature(guest ,"nonpae", 1, 1) == NULL)
            goto no_memory;
174 175
    }

176
    return caps;
177

178
no_memory:
179
    virReportOOMError(conn);
180 181
    virCapabilitiesFree(caps);
    return NULL;
182 183
}

184

185 186 187 188 189 190 191 192 193 194
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>";
195 196


197 198 199 200 201 202 203 204 205 206 207
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>";
208

L
Laine Stump 已提交
209 210 211 212 213 214 215 216 217 218 219
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 已提交
220 221 222 223 224 225 226 227
static const char *defaultPoolXML =
"<pool type='dir'>"
"  <name>default-pool</name>"
"  <target>"
"    <path>/default-pool</path>"
"  </target>"
"</pool>";

228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
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>";

246
static const unsigned long long defaultPoolCap = (100 * 1024 * 1024 * 1024ull);
C
Cole Robinson 已提交
247 248
static const unsigned long long defaultPoolAlloc = 0;

249
static int testStoragePoolObjSetDefaults(virConnectPtr conn, virStoragePoolObjPtr pool);
250

251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
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;
}

284 285 286
static int
testDomainGenerateIfnames(virConnectPtr conn,
                          virDomainDefPtr domdef)
287 288 289 290 291 292 293 294 295 296
{
    int i = 0;

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

        ifname = testDomainGenerateIfname(conn, domdef);
        if (!ifname)
297
            return -1;
298 299 300 301

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

302
    return 0;
303 304
}

305

306
static int testOpenDefault(virConnectPtr conn) {
307 308
    int u;
    struct timeval tv;
309
    testConnPtr privconn;
310 311 312 313
    virDomainDefPtr domdef = NULL;
    virDomainObjPtr domobj = NULL;
    virNetworkDefPtr netdef = NULL;
    virNetworkObjPtr netobj = NULL;
L
Laine Stump 已提交
314 315
    virInterfaceDefPtr interfacedef = NULL;
    virInterfaceObjPtr interfaceobj = NULL;
C
Cole Robinson 已提交
316 317
    virStoragePoolDefPtr pooldef = NULL;
    virStoragePoolObjPtr poolobj = NULL;
318 319
    virNodeDeviceDefPtr nodedef = NULL;
    virNodeDeviceObjPtr nodeobj = NULL;
320

321
    if (VIR_ALLOC(privconn) < 0) {
322
        virReportOOMError(conn);
323 324
        return VIR_DRV_OPEN_ERROR;
    }
325 326 327 328 329 330 331
    if (virMutexInit(&privconn->lock) < 0) {
        testError(conn, VIR_ERR_INTERNAL_ERROR,
                  "%s", _("cannot initialize mutex"));
        VIR_FREE(privconn);
        return VIR_DRV_OPEN_ERROR;
    }

332
    testDriverLock(privconn);
333
    conn->privateData = privconn;
334 335

    if (gettimeofday(&tv, NULL) < 0) {
336 337
        virReportSystemError(conn, errno,
                             "%s", _("getting time of day"));
338
        goto error;
339 340
    }

341
    memmove(&privconn->nodeInfo, &defaultNodeInfo, sizeof(defaultNodeInfo));
342

343 344 345 346 347 348 349 350 351 352
    // 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;
    }

353 354 355 356 357
    if (!(privconn->caps = testBuildCapabilities(conn)))
        goto error;

    privconn->nextDomID = 1;

358 359 360
    if (!(domdef = virDomainDefParseString(conn, privconn->caps,
                                           defaultDomainXML,
                                           VIR_DOMAIN_XML_INACTIVE)))
361
        goto error;
362
    if (testDomainGenerateIfnames(conn, domdef) < 0)
363
        goto error;
364 365 366
    if (!(domobj = virDomainAssignDef(conn, &privconn->domains, domdef)))
        goto error;
    domdef = NULL;
367
    domobj->def->id = privconn->nextDomID++;
368 369
    domobj->state = VIR_DOMAIN_RUNNING;
    domobj->persistent = 1;
370
    virDomainObjUnlock(domobj);
371 372 373 374 375 376 377 378 379

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

L
Laine Stump 已提交
382 383 384 385 386 387 388 389 390
    if (!(interfacedef = virInterfaceDefParseString(conn, defaultInterfaceXML)))
        goto error;
    if (!(interfaceobj = virInterfaceAssignDef(conn, &privconn->ifaces, interfacedef))) {
        virInterfaceDefFree(interfacedef);
        goto error;
    }
    interfaceobj->active = 1;
    virInterfaceObjUnlock(interfaceobj);

391
    if (!(pooldef = virStoragePoolDefParseString(conn, defaultPoolXML)))
C
Cole Robinson 已提交
392 393 394 395 396 397 398
        goto error;

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

400
    if (testStoragePoolObjSetDefaults(conn, poolobj) == -1) {
401
        virStoragePoolObjUnlock(poolobj);
C
Cole Robinson 已提交
402
        goto error;
403
    }
C
Cole Robinson 已提交
404
    poolobj->active = 1;
405
    virStoragePoolObjUnlock(poolobj);
C
Cole Robinson 已提交
406

407 408 409 410 411 412 413 414 415 416
    /* 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);

417
    testDriverUnlock(privconn);
418

419 420 421
    return VIR_DRV_OPEN_SUCCESS;

error:
422
    virDomainObjListFree(&privconn->domains);
423
    virNetworkObjListFree(&privconn->networks);
L
Laine Stump 已提交
424
    virInterfaceObjListFree(&privconn->ifaces);
C
Cole Robinson 已提交
425
    virStoragePoolObjListFree(&privconn->pools);
426
    virNodeDeviceObjListFree(&privconn->devs);
427
    virCapabilitiesFree(privconn->caps);
428
    testDriverUnlock(privconn);
429
    conn->privateData = NULL;
430
    VIR_FREE(privconn);
431
    virDomainDefFree(domdef);
432
    return VIR_DRV_OPEN_ERROR;
433 434 435 436
}


static char *testBuildFilename(const char *relativeTo,
437 438 439 440 441 442 443 444
                               const char *filename) {
    char *offset;
    int baseLen;
    if (!filename || filename[0] == '\0')
        return (NULL);
    if (filename[0] == '/')
        return strdup(filename);

445
    offset = strrchr(relativeTo, '/');
446
    if ((baseLen = (offset-relativeTo+1))) {
447
        char *absFile;
C
Chris Lalancette 已提交
448 449
        int totalLen = baseLen + strlen(filename) + 1;
        if (VIR_ALLOC_N(absFile, totalLen) < 0)
450
            return NULL;
C
Chris Lalancette 已提交
451 452 453 454
        if (virStrncpy(absFile, relativeTo, baseLen, totalLen) == NULL) {
            VIR_FREE(absFile);
            return NULL;
        }
455 456 457 458 459
        strcat(absFile, filename);
        return absFile;
    } else {
        return strdup(filename);
    }
460 461
}

462 463 464 465 466 467 468 469 470
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;
471
    virStorageVolDefPtr def = NULL;
472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542

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

543
static int testOpenFromFile(virConnectPtr conn,
544
                            const char *file) {
545
    int fd = -1, i, ret;
546 547
    long l;
    char *str;
548
    xmlDocPtr xml = NULL;
549
    xmlNodePtr root = NULL;
550 551
    xmlNodePtr *domains = NULL, *networks = NULL, *ifaces = NULL,
               *pools = NULL, *devs = NULL;
552 553
    xmlXPathContextPtr ctxt = NULL;
    virNodeInfoPtr nodeInfo;
554
    virNetworkObjPtr net;
L
Laine Stump 已提交
555
    virInterfaceObjPtr iface;
556
    virDomainObjPtr dom;
557 558
    testConnPtr privconn;
    if (VIR_ALLOC(privconn) < 0) {
559
        virReportOOMError(conn);
560 561
        return VIR_DRV_OPEN_ERROR;
    }
562 563 564 565 566 567 568
    if (virMutexInit(&privconn->lock) < 0) {
        testError(conn, VIR_ERR_INTERNAL_ERROR,
                  "%s", _("cannot initialize mutex"));
        VIR_FREE(privconn);
        return VIR_DRV_OPEN_ERROR;
    }

569
    testDriverLock(privconn);
570
    conn->privateData = privconn;
571 572 573

    if (!(privconn->caps = testBuildCapabilities(conn)))
        goto error;
574 575

    if ((fd = open(file, O_RDONLY)) < 0) {
576 577 578
        virReportSystemError(NULL, errno,
                             _("loading host definition file '%s'"),
                             file);
579
        goto error;
580 581
    }

582 583 584
    if (!(xml = xmlReadFd(fd, file, NULL,
                          XML_PARSE_NOENT | XML_PARSE_NONET |
                          XML_PARSE_NOERROR | XML_PARSE_NOWARNING))) {
585 586
        testError(NULL, VIR_ERR_INTERNAL_ERROR,
                  _("Invalid XML in file '%s'"), file);
587
        goto error;
588
    }
589 590
    close(fd);
    fd = -1;
591

592 593
    root = xmlDocGetRootElement(xml);
    if ((root == NULL) || (!xmlStrEqual(root->name, BAD_CAST "node"))) {
594 595
        testError(NULL, VIR_ERR_XML_ERROR, "%s",
                  _("Root element is not 'node'"));
596
        goto error;
597 598
    }

599 600
    ctxt = xmlXPathNewContext(xml);
    if (ctxt == NULL) {
C
Cole Robinson 已提交
601 602
        testError(NULL, VIR_ERR_INTERNAL_ERROR, "%s",
                  _("creating xpath context"));
603
        goto error;
604
    }
605

606
    privconn->nextDomID = 1;
607
    privconn->numCells = 0;
C
Chris Lalancette 已提交
608 609 610 611 612
    if (virStrcpyStatic(privconn->path, file) == NULL) {
        testError(NULL, VIR_ERR_INTERNAL_ERROR,
                  _("Path %s too big for destination"), file);
        goto error;
    }
613 614 615
    memmove(&privconn->nodeInfo, &defaultNodeInfo, sizeof(defaultNodeInfo));

    nodeInfo = &privconn->nodeInfo;
616
    ret = virXPathLong(conn, "string(/node/cpu/nodes[1])", ctxt, &l);
617 618 619
    if (ret == 0) {
        nodeInfo->nodes = l;
    } else if (ret == -2) {
J
Jim Meyering 已提交
620
        testError(NULL, VIR_ERR_XML_ERROR, "%s", _("node cpu numa nodes"));
621
        goto error;
622
    }
623

624
    ret = virXPathLong(conn, "string(/node/cpu/sockets[1])", ctxt, &l);
625 626 627
    if (ret == 0) {
        nodeInfo->sockets = l;
    } else if (ret == -2) {
J
Jim Meyering 已提交
628
        testError(NULL, VIR_ERR_XML_ERROR, "%s", _("node cpu sockets"));
629
        goto error;
630
    }
631

632
    ret = virXPathLong(conn, "string(/node/cpu/cores[1])", ctxt, &l);
633 634 635
    if (ret == 0) {
        nodeInfo->cores = l;
    } else if (ret == -2) {
J
Jim Meyering 已提交
636
        testError(NULL, VIR_ERR_XML_ERROR, "%s", _("node cpu cores"));
637
        goto error;
638 639
    }

640
    ret = virXPathLong(conn, "string(/node/cpu/threads[1])", ctxt, &l);
641 642 643
    if (ret == 0) {
        nodeInfo->threads = l;
    } else if (ret == -2) {
J
Jim Meyering 已提交
644
        testError(NULL, VIR_ERR_XML_ERROR, "%s", _("node cpu threads"));
645
        goto error;
646
    }
647

648
    nodeInfo->cpus = nodeInfo->cores * nodeInfo->threads * nodeInfo->sockets * nodeInfo->nodes;
649
    ret = virXPathLong(conn, "string(/node/cpu/active[1])", ctxt, &l);
650 651
    if (ret == 0) {
        if (l < nodeInfo->cpus) {
652 653
            nodeInfo->cpus = l;
        }
654
    } else if (ret == -2) {
J
Jim Meyering 已提交
655
        testError(NULL, VIR_ERR_XML_ERROR, "%s", _("node active cpu"));
656
        goto error;
657
    }
658
    ret = virXPathLong(conn, "string(/node/cpu/mhz[1])", ctxt, &l);
659 660 661
    if (ret == 0) {
        nodeInfo->mhz = l;
    } else if (ret == -2) {
J
Jim Meyering 已提交
662
        testError(NULL, VIR_ERR_XML_ERROR, "%s", _("node cpu mhz"));
663
        goto error;
664 665
    }

666
    str = virXPathString(conn, "string(/node/cpu/model[1])", ctxt);
667
    if (str != NULL) {
C
Chris Lalancette 已提交
668 669 670 671 672 673
        if (virStrcpyStatic(nodeInfo->model, str) == NULL) {
            testError(NULL, VIR_ERR_INTERNAL_ERROR,
                      _("Model %s too big for destination"), str);
            VIR_FREE(str);
            goto error;
        }
674
        VIR_FREE(str);
675 676
    }

677
    ret = virXPathLong(conn, "string(/node/memory[1])", ctxt, &l);
678 679 680
    if (ret == 0) {
        nodeInfo->memory = l;
    } else if (ret == -2) {
J
Jim Meyering 已提交
681
        testError(NULL, VIR_ERR_XML_ERROR, "%s", _("node memory"));
682
        goto error;
683
    }
684

685
    ret = virXPathNodeSet(conn, "/node/domain", ctxt, &domains);
686
    if (ret < 0) {
J
Jim Meyering 已提交
687
        testError(NULL, VIR_ERR_XML_ERROR, "%s", _("node domain list"));
688
        goto error;
689
    }
690

691
    for (i = 0 ; i < ret ; i++) {
692 693 694 695 696 697
        virDomainDefPtr def;
        char *relFile = virXMLPropString(domains[i], "file");
        if (relFile != NULL) {
            char *absFile = testBuildFilename(file, relFile);
            VIR_FREE(relFile);
            if (!absFile) {
J
Jim Meyering 已提交
698
                testError(NULL, VIR_ERR_INTERNAL_ERROR, "%s", _("resolving domain filename"));
699 700
                goto error;
            }
701 702
            def = virDomainDefParseFile(conn, privconn->caps, absFile,
                                        VIR_DOMAIN_XML_INACTIVE);
703
            VIR_FREE(absFile);
704 705 706
            if (!def)
                goto error;
        } else {
707 708
            if ((def = virDomainDefParseNode(conn, privconn->caps, xml, domains[i],
                                   VIR_DOMAIN_XML_INACTIVE)) == NULL)
709 710 711
                goto error;
        }

712 713
        if (testDomainGenerateIfnames(conn, def) < 0 ||
            !(dom = virDomainAssignDef(conn, &privconn->domains, def))) {
714
            virDomainDefFree(def);
715 716
            goto error;
        }
717 718 719 720

        dom->state = VIR_DOMAIN_RUNNING;
        dom->def->id = privconn->nextDomID++;
        dom->persistent = 1;
721
        virDomainObjUnlock(dom);
722
    }
723
    VIR_FREE(domains);
724

725
    ret = virXPathNodeSet(conn, "/node/network", ctxt, &networks);
726
    if (ret < 0) {
J
Jim Meyering 已提交
727
        testError(NULL, VIR_ERR_XML_ERROR, "%s", _("node network list"));
728 729 730 731 732 733 734 735
        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);
736
            if (!absFile) {
J
Jim Meyering 已提交
737
                testError(NULL, VIR_ERR_INTERNAL_ERROR, "%s", _("resolving network filename"));
738 739
                goto error;
            }
740 741

            def = virNetworkDefParseFile(conn, absFile);
742
            VIR_FREE(absFile);
743 744 745 746 747
            if (!def)
                goto error;
        } else {
            if ((def = virNetworkDefParseNode(conn, xml, networks[i])) == NULL)
                goto error;
748
        }
749 750 751 752
        if (!(net = virNetworkAssignDef(conn, &privconn->networks,
                                        def))) {
            virNetworkDefFree(def);
            goto error;
753
        }
754
        net->persistent = 1;
755
        net->active = 1;
756
        virNetworkObjUnlock(net);
757
    }
758
    VIR_FREE(networks);
759

L
Laine Stump 已提交
760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792
    /* 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 {
        if ((def = virInterfaceDefParseNode(conn, xml, ifaces[i])) == NULL)
                goto error;
        }
        if (!(iface = virInterfaceAssignDef(conn, &privconn->ifaces, def))) {
            virInterfaceDefFree(def);
            goto error;
        }
        virInterfaceObjUnlock(iface);
    }
    VIR_FREE(ifaces);

C
Cole Robinson 已提交
793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811
    /* 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;
            }

812
            def = virStoragePoolDefParseFile(conn, absFile);
C
Cole Robinson 已提交
813 814 815 816
            VIR_FREE(absFile);
            if (!def)
                goto error;
        } else {
817 818
            if ((def = virStoragePoolDefParseNode(conn, xml,
                                                  pools[i])) == NULL) {
C
Cole Robinson 已提交
819 820 821 822 823 824 825 826 827 828
                goto error;
            }
        }

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

829
        if (testStoragePoolObjSetDefaults(conn, pool) == -1) {
830
            virStoragePoolObjUnlock(pool);
C
Cole Robinson 已提交
831
            goto error;
832
        }
C
Cole Robinson 已提交
833
        pool->active = 1;
834 835 836 837 838 839 840

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

841
        virStoragePoolObjUnlock(pool);
C
Cole Robinson 已提交
842
    }
843
    VIR_FREE(pools);
C
Cole Robinson 已提交
844

845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881
    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 已提交
882
    xmlXPathFreeContext(ctxt);
883
    xmlFreeDoc(xml);
884
    testDriverUnlock(privconn);
885

886
    return (0);
887 888

 error:
J
Jim Meyering 已提交
889
    xmlXPathFreeContext(ctxt);
890
    xmlFreeDoc(xml);
891 892
    VIR_FREE(domains);
    VIR_FREE(networks);
L
Laine Stump 已提交
893
    VIR_FREE(ifaces);
C
Cole Robinson 已提交
894
    VIR_FREE(pools);
895 896
    if (fd != -1)
        close(fd);
897
    virDomainObjListFree(&privconn->domains);
898
    virNetworkObjListFree(&privconn->networks);
L
Laine Stump 已提交
899
    virInterfaceObjListFree(&privconn->ifaces);
C
Cole Robinson 已提交
900
    virStoragePoolObjListFree(&privconn->pools);
901
    testDriverUnlock(privconn);
902
    VIR_FREE(privconn);
903
    conn->privateData = NULL;
904
    return VIR_DRV_OPEN_ERROR;
905 906
}

907

908
static virDrvOpenStatus testOpen(virConnectPtr conn,
909
                    virConnectAuthPtr auth ATTRIBUTE_UNUSED,
910
                    int flags ATTRIBUTE_UNUSED)
911
{
912
    int ret;
913

914
    if (!conn->uri)
915
        return VIR_DRV_OPEN_DECLINED;
916

917
    if (!conn->uri->scheme || STRNEQ(conn->uri->scheme, "test"))
918
        return VIR_DRV_OPEN_DECLINED;
919

920
    /* Remote driver should handle these. */
921
    if (conn->uri->server)
922 923
        return VIR_DRV_OPEN_DECLINED;

924
    /* From this point on, the connection is for us. */
925 926 927
    if (!conn->uri->path
        || conn->uri->path[0] == '\0'
        || (conn->uri->path[0] == '/' && conn->uri->path[1] == '\0')) {
928
        testError (NULL, VIR_ERR_INVALID_ARG,
J
Jim Meyering 已提交
929
                   "%s", _("testOpen: supply a path or use test:///default"));
930 931
        return VIR_DRV_OPEN_ERROR;
    }
932

933
    if (STREQ(conn->uri->path, "/default"))
934 935
        ret = testOpenDefault(conn);
    else
936
        ret = testOpenFromFile(conn,
937
                               conn->uri->path);
938

939 940
    if (ret == VIR_DRV_OPEN_SUCCESS) {
        testConnPtr privconn = conn->privateData;
941
        testDriverLock(privconn);
942 943 944 945
        /* Init callback list */
        if (VIR_ALLOC(privconn->domainEventCallbacks) < 0 ||
            !(privconn->domainEventQueue = virDomainEventQueueNew())) {
            virReportOOMError(NULL);
946
            testDriverUnlock(privconn);
947 948 949 950 951 952 953 954
            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.");
955
        testDriverUnlock(privconn);
956 957
    }

958
    return (ret);
959 960
}

961
static int testClose(virConnectPtr conn)
962
{
963
    testConnPtr privconn = conn->privateData;
964
    testDriverLock(privconn);
965
    virCapabilitiesFree(privconn->caps);
966
    virDomainObjListFree(&privconn->domains);
967
    virNetworkObjListFree(&privconn->networks);
L
Laine Stump 已提交
968
    virInterfaceObjListFree(&privconn->ifaces);
C
Cole Robinson 已提交
969
    virStoragePoolObjListFree(&privconn->pools);
970 971 972 973 974 975 976

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

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

977
    testDriverUnlock(privconn);
978
    virMutexDestroy(&privconn->lock);
979

980
    VIR_FREE (privconn);
981
    conn->privateData = NULL;
982
    return 0;
983 984
}

985 986
static int testGetVersion(virConnectPtr conn ATTRIBUTE_UNUSED,
                          unsigned long *hvVer)
987
{
988 989
    *hvVer = 2;
    return (0);
990 991
}

992
static char *testGetHostname (virConnectPtr conn)
993
{
994
    char *result;
995

996 997
    result = virGetHostname();
    if (result == NULL) {
998 999
        virReportSystemError(conn, errno,
                             "%s", _("cannot lookup hostname"));
1000 1001
        return NULL;
    }
1002 1003
    /* Caller frees this string. */
    return result;
1004 1005
}

1006 1007 1008 1009 1010 1011 1012 1013
static int testGetMaxVCPUs(virConnectPtr conn ATTRIBUTE_UNUSED,
                           const char *type ATTRIBUTE_UNUSED)
{
    return 32;
}

static int testNodeGetInfo(virConnectPtr conn,
                           virNodeInfoPtr info)
1014
{
1015
    testConnPtr privconn = conn->privateData;
1016
    testDriverLock(privconn);
1017
    memcpy(info, &privconn->nodeInfo, sizeof(virNodeInfo));
1018
    testDriverUnlock(privconn);
1019
    return (0);
1020 1021
}

1022
static char *testGetCapabilities (virConnectPtr conn)
1023
{
1024
    testConnPtr privconn = conn->privateData;
1025
    char *xml;
1026
    testDriverLock(privconn);
1027
    if ((xml = virCapabilitiesFormatXML(privconn->caps)) == NULL)
1028
        virReportOOMError(conn);
1029
    testDriverUnlock(privconn);
1030
    return xml;
1031 1032
}

1033
static int testNumOfDomains(virConnectPtr conn)
1034
{
1035
    testConnPtr privconn = conn->privateData;
1036
    unsigned int numActive = 0, i;
1037

1038
    testDriverLock(privconn);
1039 1040
    for (i = 0 ; i < privconn->domains.count ; i++)
        if (virDomainIsActive(privconn->domains.objs[i]))
1041
            numActive++;
1042
    testDriverUnlock(privconn);
1043

1044
    return numActive;
1045 1046
}

1047
static virDomainPtr
1048
testDomainCreateXML(virConnectPtr conn, const char *xml,
1049
                      unsigned int flags ATTRIBUTE_UNUSED)
1050
{
1051
    testConnPtr privconn = conn->privateData;
1052
    virDomainPtr ret = NULL;
1053
    virDomainDefPtr def;
1054
    virDomainObjPtr dom = NULL;
1055
    virDomainEventPtr event = NULL;
1056

1057
    testDriverLock(privconn);
1058 1059
    if ((def = virDomainDefParseString(conn, privconn->caps, xml,
                                       VIR_DOMAIN_XML_INACTIVE)) == NULL)
1060
        goto cleanup;
1061

1062
    if (testDomainGenerateIfnames(conn, def) < 0)
1063
        goto cleanup;
1064 1065 1066
    if (!(dom = virDomainAssignDef(conn, &privconn->domains, def)))
        goto cleanup;
    def = NULL;
1067 1068
    dom->state = VIR_DOMAIN_RUNNING;
    dom->def->id = privconn->nextDomID++;
1069

1070 1071 1072 1073
    event = virDomainEventNewFromObj(dom,
                                     VIR_DOMAIN_EVENT_STARTED,
                                     VIR_DOMAIN_EVENT_STARTED_BOOTED);

1074
    ret = virGetDomain(conn, dom->def->name, dom->def->uuid);
1075
    if (ret)
1076
        ret->id = dom->def->id;
1077 1078

cleanup:
1079 1080
    if (dom)
        virDomainObjUnlock(dom);
1081 1082
    if (event)
        testDomainEventQueue(privconn, event);
1083 1084
    if (def)
        virDomainDefFree(def);
1085
    testDriverUnlock(privconn);
1086
    return ret;
1087 1088 1089
}


1090 1091
static virDomainPtr testLookupDomainByID(virConnectPtr conn,
                                         int id)
1092
{
1093
    testConnPtr privconn = conn->privateData;
1094 1095
    virDomainPtr ret = NULL;
    virDomainObjPtr dom;
1096

1097 1098 1099 1100 1101
    testDriverLock(privconn);
    dom = virDomainFindByID(&privconn->domains, id);
    testDriverUnlock(privconn);

    if (dom == NULL) {
1102
        testError (conn, VIR_ERR_NO_DOMAIN, NULL);
1103
        goto cleanup;
1104 1105
    }

1106
    ret = virGetDomain(conn, dom->def->name, dom->def->uuid);
1107 1108 1109 1110
    if (ret)
        ret->id = dom->def->id;

cleanup:
1111 1112
    if (dom)
        virDomainObjUnlock(dom);
1113
    return ret;
1114 1115
}

1116 1117
static virDomainPtr testLookupDomainByUUID(virConnectPtr conn,
                                           const unsigned char *uuid)
1118
{
1119
    testConnPtr privconn = conn->privateData;
1120 1121
    virDomainPtr ret = NULL;
    virDomainObjPtr dom ;
1122

1123 1124 1125 1126 1127
    testDriverLock(privconn);
    dom = virDomainFindByUUID(&privconn->domains, uuid);
    testDriverUnlock(privconn);

    if (dom == NULL) {
1128
        testError (conn, VIR_ERR_NO_DOMAIN, NULL);
1129
        goto cleanup;
1130
    }
1131

1132
    ret = virGetDomain(conn, dom->def->name, dom->def->uuid);
1133 1134 1135 1136
    if (ret)
        ret->id = dom->def->id;

cleanup:
1137 1138
    if (dom)
        virDomainObjUnlock(dom);
1139
    return ret;
1140 1141
}

1142 1143
static virDomainPtr testLookupDomainByName(virConnectPtr conn,
                                           const char *name)
1144
{
1145
    testConnPtr privconn = conn->privateData;
1146 1147
    virDomainPtr ret = NULL;
    virDomainObjPtr dom;
1148

1149 1150 1151 1152 1153
    testDriverLock(privconn);
    dom = virDomainFindByName(&privconn->domains, name);
    testDriverUnlock(privconn);

    if (dom == NULL) {
1154
        testError (conn, VIR_ERR_NO_DOMAIN, NULL);
1155
        goto cleanup;
1156
    }
1157

1158
    ret = virGetDomain(conn, dom->def->name, dom->def->uuid);
1159 1160 1161 1162
    if (ret)
        ret->id = dom->def->id;

cleanup:
1163 1164
    if (dom)
        virDomainObjUnlock(dom);
1165
    return ret;
1166 1167
}

1168 1169 1170
static int testListDomains (virConnectPtr conn,
                            int *ids,
                            int maxids)
1171
{
1172
    testConnPtr privconn = conn->privateData;
1173
    unsigned int n = 0, i;
1174

1175 1176 1177
    testDriverLock(privconn);
    for (i = 0 ; i < privconn->domains.count && n < maxids ; i++) {
        virDomainObjLock(privconn->domains.objs[i]);
1178 1179
        if (virDomainIsActive(privconn->domains.objs[i]))
            ids[n++] = privconn->domains.objs[i]->def->id;
1180 1181 1182
        virDomainObjUnlock(privconn->domains.objs[i]);
    }
    testDriverUnlock(privconn);
1183

1184
    return n;
1185 1186
}

1187
static int testDestroyDomain (virDomainPtr domain)
1188
{
1189 1190
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
1191
    virDomainEventPtr event = NULL;
1192
    int ret = -1;
1193

1194
    testDriverLock(privconn);
1195 1196 1197 1198 1199
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);

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

1203
    privdom->state = VIR_DOMAIN_SHUTOFF;
1204 1205
    privdom->def->id = -1;
    domain->id = -1;
1206 1207 1208
    event = virDomainEventNewFromObj(privdom,
                                     VIR_DOMAIN_EVENT_STOPPED,
                                     VIR_DOMAIN_EVENT_STOPPED_DESTROYED);
1209 1210 1211
    if (!privdom->persistent) {
        virDomainRemoveInactive(&privconn->domains,
                                privdom);
1212
        privdom = NULL;
1213
    }
1214

1215

1216 1217
    ret = 0;
cleanup:
1218 1219
    if (privdom)
        virDomainObjUnlock(privdom);
1220 1221
    if (event)
        testDomainEventQueue(privconn, event);
1222
    testDriverUnlock(privconn);
1223
    return ret;
1224 1225
}

1226
static int testResumeDomain (virDomainPtr domain)
1227
{
1228 1229
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
1230
    virDomainEventPtr event = NULL;
1231
    int ret = -1;
1232

1233
    testDriverLock(privconn);
1234 1235
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
1236
    testDriverUnlock(privconn);
1237 1238 1239

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

1243
    if (privdom->state != VIR_DOMAIN_PAUSED) {
1244 1245 1246
        testError(domain->conn,
                  VIR_ERR_INTERNAL_ERROR, _("domain '%s' not paused"),
                  domain->name);
1247
        goto cleanup;
1248
    }
1249

1250
    privdom->state = VIR_DOMAIN_RUNNING;
1251 1252 1253
    event = virDomainEventNewFromObj(privdom,
                                     VIR_DOMAIN_EVENT_RESUMED,
                                     VIR_DOMAIN_EVENT_RESUMED_UNPAUSED);
1254 1255 1256
    ret = 0;

cleanup:
1257 1258
    if (privdom)
        virDomainObjUnlock(privdom);
1259 1260 1261 1262 1263
    if (event) {
        testDriverLock(privconn);
        testDomainEventQueue(privconn, event);
        testDriverUnlock(privconn);
    }
1264
    return ret;
1265 1266
}

1267
static int testPauseDomain (virDomainPtr domain)
1268
{
1269 1270
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
1271
    virDomainEventPtr event = NULL;
1272
    int ret = -1;
1273

1274
    testDriverLock(privconn);
1275 1276
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
1277
    testDriverUnlock(privconn);
1278 1279 1280

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

1284 1285
    if (privdom->state == VIR_DOMAIN_SHUTOFF ||
        privdom->state == VIR_DOMAIN_PAUSED) {
1286 1287 1288
        testError(domain->conn,
                  VIR_ERR_INTERNAL_ERROR, _("domain '%s' not running"),
                  domain->name);
1289
        goto cleanup;
1290
    }
1291

1292
    privdom->state = VIR_DOMAIN_PAUSED;
1293 1294 1295
    event = virDomainEventNewFromObj(privdom,
                                     VIR_DOMAIN_EVENT_SUSPENDED,
                                     VIR_DOMAIN_EVENT_SUSPENDED_PAUSED);
1296 1297 1298
    ret = 0;

cleanup:
1299 1300
    if (privdom)
        virDomainObjUnlock(privdom);
1301 1302 1303 1304 1305 1306

    if (event) {
        testDriverLock(privconn);
        testDomainEventQueue(privconn, event);
        testDriverUnlock(privconn);
    }
1307
    return ret;
1308 1309
}

1310
static int testShutdownDomain (virDomainPtr domain)
1311
{
1312 1313
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
1314
    virDomainEventPtr event = NULL;
1315
    int ret = -1;
1316

1317
    testDriverLock(privconn);
1318 1319 1320 1321 1322
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);

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

1326
    if (privdom->state == VIR_DOMAIN_SHUTOFF) {
1327 1328
        testError(domain->conn, VIR_ERR_INTERNAL_ERROR,
                  _("domain '%s' not running"), domain->name);
1329
        goto cleanup;
1330
    }
1331

1332
    privdom->state = VIR_DOMAIN_SHUTOFF;
1333
    domain->id = -1;
1334
    privdom->def->id = -1;
1335 1336 1337
    event = virDomainEventNewFromObj(privdom,
                                     VIR_DOMAIN_EVENT_STOPPED,
                                     VIR_DOMAIN_EVENT_STOPPED_SHUTDOWN);
1338 1339 1340 1341 1342
    if (!privdom->persistent) {
        virDomainRemoveInactive(&privconn->domains,
                                privdom);
        privdom = NULL;
    }
1343
    ret = 0;
1344

1345
cleanup:
1346 1347
    if (privdom)
        virDomainObjUnlock(privdom);
1348 1349
    if (event)
        testDomainEventQueue(privconn, event);
1350
    testDriverUnlock(privconn);
1351
    return ret;
1352 1353 1354
}

/* Similar behaviour as shutdown */
1355 1356
static int testRebootDomain (virDomainPtr domain,
                             unsigned int action ATTRIBUTE_UNUSED)
1357
{
1358 1359
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
1360
    virDomainEventPtr event = NULL;
1361
    int ret = -1;
1362

1363
    testDriverLock(privconn);
1364 1365 1366 1367 1368
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);

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

1372 1373 1374 1375
    privdom->state = VIR_DOMAIN_SHUTDOWN;
    switch (privdom->def->onReboot) {
    case VIR_DOMAIN_LIFECYCLE_DESTROY:
        privdom->state = VIR_DOMAIN_SHUTOFF;
1376
        domain->id = -1;
1377
        privdom->def->id = -1;
1378 1379
        break;

1380 1381
    case VIR_DOMAIN_LIFECYCLE_RESTART:
        privdom->state = VIR_DOMAIN_RUNNING;
1382 1383
        break;

1384 1385
    case VIR_DOMAIN_LIFECYCLE_PRESERVE:
        privdom->state = VIR_DOMAIN_SHUTOFF;
1386
        domain->id = -1;
1387
        privdom->def->id = -1;
1388 1389
        break;

1390 1391
    case VIR_DOMAIN_LIFECYCLE_RESTART_RENAME:
        privdom->state = VIR_DOMAIN_RUNNING;
1392
        break;
1393

1394
    default:
1395
        privdom->state = VIR_DOMAIN_SHUTOFF;
1396
        domain->id = -1;
1397
        privdom->def->id = -1;
1398 1399
        break;
    }
1400

1401 1402 1403 1404 1405 1406 1407 1408 1409
    if (privdom->state == VIR_DOMAIN_SHUTOFF) {
        event = virDomainEventNewFromObj(privdom,
                                         VIR_DOMAIN_EVENT_STOPPED,
                                         VIR_DOMAIN_EVENT_STOPPED_SHUTDOWN);
        if (!privdom->persistent) {
            virDomainRemoveInactive(&privconn->domains,
                                    privdom);
            privdom = NULL;
        }
1410 1411
    }

1412 1413 1414
    ret = 0;

cleanup:
1415 1416
    if (privdom)
        virDomainObjUnlock(privdom);
1417 1418
    if (event)
        testDomainEventQueue(privconn, event);
1419
    testDriverUnlock(privconn);
1420
    return ret;
1421 1422
}

1423 1424
static int testGetDomainInfo (virDomainPtr domain,
                              virDomainInfoPtr info)
1425
{
1426
    testConnPtr privconn = domain->conn->privateData;
1427
    struct timeval tv;
1428
    virDomainObjPtr privdom;
1429
    int ret = -1;
1430

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

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

    if (gettimeofday(&tv, NULL) < 0) {
1442
        testError(domain->conn, VIR_ERR_INTERNAL_ERROR,
J
Jim Meyering 已提交
1443
                  "%s", _("getting time of day"));
1444
        goto cleanup;
1445 1446
    }

1447 1448 1449 1450 1451
    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));
1452 1453 1454
    ret = 0;

cleanup:
1455 1456
    if (privdom)
        virDomainObjUnlock(privdom);
1457
    return ret;
1458 1459
}

1460 1461 1462 1463 1464
#define TEST_SAVE_MAGIC "TestGuestMagic"

static int testDomainSave(virDomainPtr domain,
                          const char *path)
{
1465
    testConnPtr privconn = domain->conn->privateData;
1466 1467 1468
    char *xml = NULL;
    int fd = -1;
    int len;
1469
    virDomainObjPtr privdom;
1470
    virDomainEventPtr event = NULL;
1471
    int ret = -1;
1472

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

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

C
Cole Robinson 已提交
1482 1483 1484 1485
    xml = virDomainDefFormat(domain->conn,
                             privdom->def,
                             VIR_DOMAIN_XML_SECURE);

1486
    if (xml == NULL) {
1487 1488 1489
        virReportSystemError(domain->conn, errno,
                             _("saving domain '%s' failed to allocate space for metadata"),
                             domain->name);
1490
        goto cleanup;
1491
    }
1492 1493

    if ((fd = open(path, O_CREAT|O_TRUNC|O_WRONLY, S_IRUSR|S_IWUSR)) < 0) {
1494 1495 1496
        virReportSystemError(domain->conn, errno,
                             _("saving domain '%s' to '%s': open failed"),
                             domain->name, path);
1497
        goto cleanup;
1498
    }
1499
    len = strlen(xml);
1500
    if (safewrite(fd, TEST_SAVE_MAGIC, sizeof(TEST_SAVE_MAGIC)) < 0) {
1501 1502 1503
        virReportSystemError(domain->conn, errno,
                             _("saving domain '%s' to '%s': write failed"),
                             domain->name, path);
1504
        goto cleanup;
1505
    }
1506
    if (safewrite(fd, (char*)&len, sizeof(len)) < 0) {
1507 1508 1509
        virReportSystemError(domain->conn, errno,
                             _("saving domain '%s' to '%s': write failed"),
                             domain->name, path);
1510
        goto cleanup;
1511
    }
1512
    if (safewrite(fd, xml, len) < 0) {
1513 1514 1515
        virReportSystemError(domain->conn, errno,
                             _("saving domain '%s' to '%s': write failed"),
                             domain->name, path);
1516
        goto cleanup;
1517
    }
1518

1519
    if (close(fd) < 0) {
1520 1521 1522
        virReportSystemError(domain->conn, errno,
                             _("saving domain '%s' to '%s': write failed"),
                             domain->name, path);
1523
        goto cleanup;
1524
    }
1525 1526
    fd = -1;

1527
    privdom->state = VIR_DOMAIN_SHUTOFF;
1528 1529 1530
    event = virDomainEventNewFromObj(privdom,
                                     VIR_DOMAIN_EVENT_STOPPED,
                                     VIR_DOMAIN_EVENT_STOPPED_SAVED);
1531 1532 1533
    if (!privdom->persistent) {
        virDomainRemoveInactive(&privconn->domains,
                                privdom);
1534
        privdom = NULL;
1535
    }
1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548
    ret = 0;

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);
    }
1549 1550
    if (privdom)
        virDomainObjUnlock(privdom);
1551 1552
    if (event)
        testDomainEventQueue(privconn, event);
1553
    testDriverUnlock(privconn);
1554
    return ret;
1555 1556
}

1557 1558
static int testDomainRestore(virConnectPtr conn,
                             const char *path)
1559
{
1560
    testConnPtr privconn = conn->privateData;
1561
    char *xml = NULL;
1562
    char magic[15];
1563 1564 1565
    int fd = -1;
    int len;
    virDomainDefPtr def = NULL;
1566
    virDomainObjPtr dom = NULL;
1567
    virDomainEventPtr event = NULL;
1568
    int ret = -1;
1569 1570

    if ((fd = open(path, O_RDONLY)) < 0) {
1571 1572 1573
        virReportSystemError(conn, errno,
                             _("cannot read domain image '%s'"),
                             path);
1574
        goto cleanup;
1575
    }
1576 1577 1578 1579
    if (saferead(fd, magic, sizeof(magic)) != sizeof(magic)) {
        virReportSystemError(conn, errno,
                             _("incomplete save header in '%s'"),
                             path);
1580
        goto cleanup;
1581
    }
1582
    if (memcmp(magic, TEST_SAVE_MAGIC, sizeof(magic))) {
1583
        testError(conn, VIR_ERR_INTERNAL_ERROR,
J
Jim Meyering 已提交
1584
                  "%s", _("mismatched header magic"));
1585
        goto cleanup;
1586
    }
1587 1588 1589 1590
    if (saferead(fd, (char*)&len, sizeof(len)) != sizeof(len)) {
        virReportSystemError(conn, errno,
                             _("failed to read metadata length in '%s'"),
                             path);
1591
        goto cleanup;
1592 1593
    }
    if (len < 1 || len > 8192) {
1594
        testError(conn, VIR_ERR_INTERNAL_ERROR,
J
Jim Meyering 已提交
1595
                  "%s", _("length of metadata out of range"));
1596
        goto cleanup;
1597
    }
1598
    if (VIR_ALLOC_N(xml, len+1) < 0) {
1599
        virReportOOMError(conn);
1600
        goto cleanup;
1601
    }
1602 1603 1604
    if (saferead(fd, xml, len) != len) {
        virReportSystemError(conn, errno,
                             _("incomplete metdata in '%s'"), path);
1605
        goto cleanup;
1606 1607
    }
    xml[len] = '\0';
1608

1609
    testDriverLock(privconn);
1610 1611
    def = virDomainDefParseString(conn, privconn->caps, xml,
                                  VIR_DOMAIN_XML_INACTIVE);
1612
    if (!def)
1613
        goto cleanup;
1614

1615
    if (testDomainGenerateIfnames(conn, def) < 0)
1616
        goto cleanup;
1617 1618 1619
    if (!(dom = virDomainAssignDef(conn, &privconn->domains, def)))
        goto cleanup;
    def = NULL;
1620

1621 1622
    dom->state = VIR_DOMAIN_RUNNING;
    dom->def->id = privconn->nextDomID++;
1623 1624 1625
    event = virDomainEventNewFromObj(dom,
                                     VIR_DOMAIN_EVENT_STARTED,
                                     VIR_DOMAIN_EVENT_STARTED_RESTORED);
1626
    ret = 0;
1627 1628 1629 1630 1631 1632

cleanup:
    virDomainDefFree(def);
    VIR_FREE(xml);
    if (fd != -1)
        close(fd);
1633 1634
    if (dom)
        virDomainObjUnlock(dom);
1635 1636
    if (event)
        testDomainEventQueue(privconn, event);
1637
    testDriverUnlock(privconn);
1638
    return ret;
1639 1640
}

1641 1642 1643
static int testDomainCoreDump(virDomainPtr domain,
                              const char *to,
                              int flags ATTRIBUTE_UNUSED)
1644
{
1645
    testConnPtr privconn = domain->conn->privateData;
1646
    int fd = -1;
1647
    virDomainObjPtr privdom;
1648
    virDomainEventPtr event = NULL;
1649
    int ret = -1;
1650

1651
    testDriverLock(privconn);
1652 1653 1654 1655 1656
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);

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

    if ((fd = open(to, O_CREAT|O_TRUNC|O_WRONLY, S_IRUSR|S_IWUSR)) < 0) {
1661 1662 1663
        virReportSystemError(domain->conn, errno,
                             _("domain '%s' coredump: failed to open %s"),
                             domain->name, to);
1664
        goto cleanup;
1665
    }
1666
    if (safewrite(fd, TEST_SAVE_MAGIC, sizeof(TEST_SAVE_MAGIC)) < 0) {
1667 1668 1669
        virReportSystemError(domain->conn, errno,
                             _("domain '%s' coredump: failed to write header to %s"),
                             domain->name, to);
1670
        goto cleanup;
1671
    }
1672
    if (close(fd) < 0) {
1673 1674 1675
        virReportSystemError(domain->conn, errno,
                             _("domain '%s' coredump: write failed: %s"),
                             domain->name, to);
1676
        goto cleanup;
1677
    }
1678
    privdom->state = VIR_DOMAIN_SHUTOFF;
1679 1680 1681
    event = virDomainEventNewFromObj(privdom,
                                     VIR_DOMAIN_EVENT_STOPPED,
                                     VIR_DOMAIN_EVENT_STOPPED_CRASHED);
1682 1683 1684
    if (!privdom->persistent) {
        virDomainRemoveInactive(&privconn->domains,
                                privdom);
1685
        privdom = NULL;
1686
    }
1687 1688 1689 1690 1691
    ret = 0;

cleanup:
    if (fd != -1)
        close(fd);
1692 1693
    if (privdom)
        virDomainObjUnlock(privdom);
1694 1695
    if (event)
        testDomainEventQueue(privconn, event);
1696
    testDriverUnlock(privconn);
1697
    return ret;
1698 1699
}

1700 1701 1702
static char *testGetOSType(virDomainPtr dom) {
    char *ret = strdup("linux");
    if (!ret)
1703
        virReportOOMError(dom->conn);
1704
    return ret;
1705 1706 1707
}

static unsigned long testGetMaxMemory(virDomainPtr domain) {
1708 1709
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
1710
    unsigned long ret = 0;
1711

1712
    testDriverLock(privconn);
1713 1714
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
1715
    testDriverUnlock(privconn);
1716 1717 1718

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

1722 1723 1724
    ret = privdom->def->maxmem;

cleanup:
1725 1726
    if (privdom)
        virDomainObjUnlock(privdom);
1727
    return ret;
1728 1729 1730 1731 1732
}

static int testSetMaxMemory(virDomainPtr domain,
                            unsigned long memory)
{
1733 1734
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
1735
    int ret = -1;
1736

1737
    testDriverLock(privconn);
1738 1739
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
1740
    testDriverUnlock(privconn);
1741 1742 1743

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

    /* XXX validate not over host memory wrt to other domains */
1748
    privdom->def->maxmem = memory;
1749 1750 1751
    ret = 0;

cleanup:
1752 1753
    if (privdom)
        virDomainObjUnlock(privdom);
1754
    return ret;
1755 1756
}

1757 1758 1759
static int testSetMemory(virDomainPtr domain,
                         unsigned long memory)
{
1760 1761
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
1762
    int ret = -1;
1763

1764
    testDriverLock(privconn);
1765 1766
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
1767
    testDriverUnlock(privconn);
1768 1769 1770

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

1774
    if (memory > privdom->def->maxmem) {
1775
        testError(domain->conn,
1776
                  VIR_ERR_INVALID_ARG, __FUNCTION__);
1777
        goto cleanup;
1778
    }
1779

1780
    privdom->def->memory = memory;
1781 1782 1783
    ret = 0;

cleanup:
1784 1785
    if (privdom)
        virDomainObjUnlock(privdom);
1786
    return ret;
1787 1788 1789 1790
}

static int testSetVcpus(virDomainPtr domain,
                        unsigned int nrCpus) {
1791 1792
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
1793 1794
    int ret = -1;

1795 1796 1797 1798 1799 1800
    testDriverLock(privconn);
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
    testDriverUnlock(privconn);

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

1805 1806
    /* We allow more cpus in guest than host */
    if (nrCpus > 32) {
1807
        testError(domain->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
1808
        goto cleanup;
1809
    }
1810

1811
    privdom->def->vcpus = nrCpus;
1812 1813 1814
    ret = 0;

cleanup:
1815 1816
    if (privdom)
        virDomainObjUnlock(privdom);
1817
    return ret;
1818 1819
}

1820
static char *testDomainDumpXML(virDomainPtr domain, int flags)
1821
{
1822
    testConnPtr privconn = domain->conn->privateData;
1823
    virDomainDefPtr def;
1824
    virDomainObjPtr privdom;
1825 1826
    char *ret = NULL;

1827 1828 1829 1830 1831 1832
    testDriverLock(privconn);
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
    testDriverUnlock(privconn);

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

1837 1838
    def = (flags & VIR_DOMAIN_XML_INACTIVE) &&
        privdom->newDef ? privdom->newDef : privdom->def;
1839

1840 1841 1842 1843 1844
    ret = virDomainDefFormat(domain->conn,
                             def,
                             flags);

cleanup:
1845 1846
    if (privdom)
        virDomainObjUnlock(privdom);
1847
    return ret;
1848
}
1849

1850
static int testNumOfDefinedDomains(virConnectPtr conn) {
1851
    testConnPtr privconn = conn->privateData;
1852
    unsigned int numInactive = 0, i;
1853

1854 1855 1856
    testDriverLock(privconn);
    for (i = 0 ; i < privconn->domains.count ; i++) {
        virDomainObjLock(privconn->domains.objs[i]);
1857
        if (!virDomainIsActive(privconn->domains.objs[i]))
1858
            numInactive++;
1859 1860 1861
        virDomainObjUnlock(privconn->domains.objs[i]);
    }
    testDriverUnlock(privconn);
1862

1863
    return numInactive;
1864 1865
}

1866 1867 1868
static int testListDefinedDomains(virConnectPtr conn,
                                  char **const names,
                                  int maxnames) {
1869
    testConnPtr privconn = conn->privateData;
1870
    unsigned int n = 0, i;
1871

1872
    testDriverLock(privconn);
1873
    memset(names, 0, sizeof(*names)*maxnames);
1874 1875
    for (i = 0 ; i < privconn->domains.count && n < maxnames ; i++) {
        virDomainObjLock(privconn->domains.objs[i]);
1876
        if (!virDomainIsActive(privconn->domains.objs[i]) &&
1877 1878
            !(names[n++] = strdup(privconn->domains.objs[i]->def->name))) {
            virDomainObjUnlock(privconn->domains.objs[i]);
1879
            goto no_memory;
1880 1881 1882 1883
        }
        virDomainObjUnlock(privconn->domains.objs[i]);
    }
    testDriverUnlock(privconn);
1884

1885 1886 1887
    return n;

no_memory:
1888
    virReportOOMError(conn);
1889 1890
    for (n = 0 ; n < maxnames ; n++)
        VIR_FREE(names[n]);
1891
    testDriverUnlock(privconn);
1892
    return -1;
1893 1894
}

1895
static virDomainPtr testDomainDefineXML(virConnectPtr conn,
1896
                                        const char *xml) {
1897
    testConnPtr privconn = conn->privateData;
1898
    virDomainPtr ret = NULL;
1899
    virDomainDefPtr def;
1900
    virDomainObjPtr dom = NULL;
1901
    virDomainEventPtr event = NULL;
1902

1903
    testDriverLock(privconn);
1904 1905
    if ((def = virDomainDefParseString(conn, privconn->caps, xml,
                                       VIR_DOMAIN_XML_INACTIVE)) == NULL)
1906
        goto cleanup;
1907

1908 1909 1910
    if (testDomainGenerateIfnames(conn, def) < 0)
        goto cleanup;
    if (!(dom = virDomainAssignDef(conn, &privconn->domains, def)))
1911
        goto cleanup;
1912
    def = NULL;
1913
    dom->persistent = 1;
1914

1915 1916 1917
    event = virDomainEventNewFromObj(dom,
                                     VIR_DOMAIN_EVENT_DEFINED,
                                     VIR_DOMAIN_EVENT_DEFINED_ADDED);
1918

1919
    ret = virGetDomain(conn, dom->def->name, dom->def->uuid);
1920
    if (ret)
1921
        ret->id = dom->def->id;
1922 1923 1924

cleanup:
    virDomainDefFree(def);
1925 1926
    if (dom)
        virDomainObjUnlock(dom);
1927 1928
    if (event)
        testDomainEventQueue(privconn, event);
1929
    testDriverUnlock(privconn);
1930
    return ret;
1931 1932
}

1933 1934 1935
static int testNodeGetCellsFreeMemory(virConnectPtr conn,
                                      unsigned long long *freemems,
                                      int startCell, int maxCells) {
1936
    testConnPtr privconn = conn->privateData;
1937
    int i, j;
1938
    int ret = -1;
1939

1940
    testDriverLock(privconn);
1941
    if (startCell > privconn->numCells) {
1942
        testError(conn, VIR_ERR_INVALID_ARG,
J
Jim Meyering 已提交
1943
                  "%s", _("Range exceeds available cells"));
1944
        goto cleanup;
1945 1946 1947 1948 1949 1950 1951
    }

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

1954
cleanup:
1955
    testDriverUnlock(privconn);
1956
    return ret;
1957 1958 1959
}


1960
static int testDomainCreate(virDomainPtr domain) {
1961 1962
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
1963
    virDomainEventPtr event = NULL;
1964
    int ret = -1;
1965

1966
    testDriverLock(privconn);
1967 1968 1969 1970 1971
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);

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

1975
    if (privdom->state != VIR_DOMAIN_SHUTOFF) {
1976 1977
        testError(domain->conn, VIR_ERR_INTERNAL_ERROR,
                  _("Domain '%s' is already running"), domain->name);
1978
        goto cleanup;
1979 1980
    }

1981 1982
    domain->id = privdom->def->id = privconn->nextDomID++;
    privdom->state = VIR_DOMAIN_RUNNING;
1983 1984 1985
    event = virDomainEventNewFromObj(privdom,
                                     VIR_DOMAIN_EVENT_STARTED,
                                     VIR_DOMAIN_EVENT_STARTED_BOOTED);
1986
    ret = 0;
1987

1988
cleanup:
1989 1990
    if (privdom)
        virDomainObjUnlock(privdom);
1991 1992
    if (event)
        testDomainEventQueue(privconn, event);
1993
    testDriverUnlock(privconn);
1994
    return ret;
1995 1996 1997
}

static int testDomainUndefine(virDomainPtr domain) {
1998 1999
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
2000
    virDomainEventPtr event = NULL;
2001
    int ret = -1;
2002

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

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

2012
    if (privdom->state != VIR_DOMAIN_SHUTOFF) {
2013 2014
        testError(domain->conn, VIR_ERR_INTERNAL_ERROR,
                  _("Domain '%s' is still running"), domain->name);
2015
        goto cleanup;
2016 2017
    }

2018
    privdom->state = VIR_DOMAIN_SHUTOFF;
2019 2020 2021
    event = virDomainEventNewFromObj(privdom,
                                     VIR_DOMAIN_EVENT_UNDEFINED,
                                     VIR_DOMAIN_EVENT_UNDEFINED_REMOVED);
2022 2023
    virDomainRemoveInactive(&privconn->domains,
                            privdom);
2024
    privdom = NULL;
2025
    ret = 0;
2026

2027
cleanup:
2028 2029
    if (privdom)
        virDomainObjUnlock(privdom);
2030 2031
    if (event)
        testDomainEventQueue(privconn, event);
2032
    testDriverUnlock(privconn);
2033
    return ret;
2034 2035
}

2036 2037 2038
static int testDomainGetAutostart(virDomainPtr domain,
                                  int *autostart)
{
2039 2040
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
2041
    int ret = -1;
2042

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

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

2053
    *autostart = privdom->autostart;
2054 2055 2056
    ret = 0;

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


static int testDomainSetAutostart(virDomainPtr domain,
                                  int autostart)
{
2066 2067
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
2068
    int ret = -1;
2069

2070
    testDriverLock(privconn);
2071 2072
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
2073
    testDriverUnlock(privconn);
2074 2075 2076

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

2080
    privdom->autostart = autostart ? 1 : 0;
2081 2082 2083
    ret = 0;

cleanup:
2084 2085
    if (privdom)
        virDomainObjUnlock(privdom);
2086
    return ret;
2087
}
2088

2089 2090 2091
static char *testDomainGetSchedulerType(virDomainPtr domain,
                                        int *nparams)
{
2092 2093
    char *type = NULL;

2094 2095
    *nparams = 1;
    type = strdup("fair");
2096
    if (!type)
2097
        virReportOOMError(domain->conn);
2098

2099 2100 2101 2102 2103 2104 2105
    return type;
}

static int testDomainGetSchedulerParams(virDomainPtr domain,
                                        virSchedParameterPtr params,
                                        int *nparams)
{
2106 2107
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
2108
    int ret = -1;
2109

2110
    testDriverLock(privconn);
2111 2112
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
2113
    testDriverUnlock(privconn);
2114 2115 2116

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

2120
    if (*nparams != 1) {
2121
        testError(domain->conn, VIR_ERR_INVALID_ARG, "nparams");
2122
        goto cleanup;
2123
    }
2124 2125
    strcpy(params[0].field, "weight");
    params[0].type = VIR_DOMAIN_SCHED_FIELD_UINT;
2126 2127 2128
    /* XXX */
    /*params[0].value.ui = privdom->weight;*/
    params[0].value.ui = 50;
2129 2130 2131
    ret = 0;

cleanup:
2132 2133
    if (privdom)
        virDomainObjUnlock(privdom);
2134
    return ret;
2135
}
2136 2137


2138 2139 2140 2141
static int testDomainSetSchedulerParams(virDomainPtr domain,
                                        virSchedParameterPtr params,
                                        int nparams)
{
2142 2143
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
2144
    int ret = -1;
2145

2146
    testDriverLock(privconn);
2147 2148
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);
2149
    testDriverUnlock(privconn);
2150 2151 2152

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

2156
    if (nparams != 1) {
2157
        testError(domain->conn, VIR_ERR_INVALID_ARG, "nparams");
2158
        goto cleanup;
2159
    }
2160
    if (STRNEQ(params[0].field, "weight")) {
2161
        testError(domain->conn, VIR_ERR_INVALID_ARG, "field");
2162
        goto cleanup;
2163 2164
    }
    if (params[0].type != VIR_DOMAIN_SCHED_FIELD_UINT) {
2165
        testError(domain->conn, VIR_ERR_INVALID_ARG, "type");
2166
        goto cleanup;
2167
    }
2168 2169
    /* XXX */
    /*privdom->weight = params[0].value.ui;*/
2170 2171 2172
    ret = 0;

cleanup:
2173 2174
    if (privdom)
        virDomainObjUnlock(privdom);
2175
    return ret;
2176 2177
}

2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289
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;
}

2290
static virDrvOpenStatus testOpenNetwork(virConnectPtr conn,
2291
                                        virConnectAuthPtr auth ATTRIBUTE_UNUSED,
2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308
                                        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)
{
2309 2310
    testConnPtr privconn = conn->privateData;
    virNetworkObjPtr net;
2311
    virNetworkPtr ret = NULL;
2312

2313 2314 2315 2316 2317
    testDriverLock(privconn);
    net = virNetworkFindByUUID(&privconn->networks, uuid);
    testDriverUnlock(privconn);

    if (net == NULL) {
2318
        testError (conn, VIR_ERR_NO_NETWORK, NULL);
2319
        goto cleanup;
2320 2321
    }

2322 2323 2324
    ret = virGetNetwork(conn, net->def->name, net->def->uuid);

cleanup:
2325 2326
    if (net)
        virNetworkObjUnlock(net);
2327
    return ret;
2328
}
2329

2330
static virNetworkPtr testLookupNetworkByName(virConnectPtr conn,
2331
                                             const char *name)
2332
{
2333
    testConnPtr privconn = conn->privateData;
2334 2335
    virNetworkObjPtr net;
    virNetworkPtr ret = NULL;
2336

2337 2338 2339 2340 2341
    testDriverLock(privconn);
    net = virNetworkFindByName(&privconn->networks, name);
    testDriverUnlock(privconn);

    if (net == NULL) {
2342
        testError (conn, VIR_ERR_NO_NETWORK, NULL);
2343
        goto cleanup;
2344 2345
    }

2346 2347 2348
    ret = virGetNetwork(conn, net->def->name, net->def->uuid);

cleanup:
2349 2350
    if (net)
        virNetworkObjUnlock(net);
2351
    return ret;
2352 2353 2354 2355
}


static int testNumNetworks(virConnectPtr conn) {
2356
    testConnPtr privconn = conn->privateData;
2357
    int numActive = 0, i;
2358

2359 2360 2361
    testDriverLock(privconn);
    for (i = 0 ; i < privconn->networks.count ; i++) {
        virNetworkObjLock(privconn->networks.objs[i]);
2362
        if (virNetworkIsActive(privconn->networks.objs[i]))
2363
            numActive++;
2364 2365 2366
        virNetworkObjUnlock(privconn->networks.objs[i]);
    }
    testDriverUnlock(privconn);
2367

2368
    return numActive;
2369 2370 2371
}

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

2375
    testDriverLock(privconn);
2376
    memset(names, 0, sizeof(*names)*nnames);
2377 2378
    for (i = 0 ; i < privconn->networks.count && n < nnames ; i++) {
        virNetworkObjLock(privconn->networks.objs[i]);
2379
        if (virNetworkIsActive(privconn->networks.objs[i]) &&
2380 2381
            !(names[n++] = strdup(privconn->networks.objs[i]->def->name))) {
            virNetworkObjUnlock(privconn->networks.objs[i]);
2382
            goto no_memory;
2383 2384 2385 2386
        }
        virNetworkObjUnlock(privconn->networks.objs[i]);
    }
    testDriverUnlock(privconn);
2387

2388 2389 2390
    return n;

no_memory:
2391
    virReportOOMError(conn);
2392 2393
    for (n = 0 ; n < nnames ; n++)
        VIR_FREE(names[n]);
2394
    testDriverUnlock(privconn);
2395
    return -1;
2396 2397 2398
}

static int testNumDefinedNetworks(virConnectPtr conn) {
2399
    testConnPtr privconn = conn->privateData;
2400
    int numInactive = 0, i;
2401

2402 2403 2404
    testDriverLock(privconn);
    for (i = 0 ; i < privconn->networks.count ; i++) {
        virNetworkObjLock(privconn->networks.objs[i]);
2405
        if (!virNetworkIsActive(privconn->networks.objs[i]))
2406
            numInactive++;
2407 2408 2409
        virNetworkObjUnlock(privconn->networks.objs[i]);
    }
    testDriverUnlock(privconn);
2410

2411
    return numInactive;
2412 2413 2414
}

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

2418
    testDriverLock(privconn);
2419
    memset(names, 0, sizeof(*names)*nnames);
2420 2421
    for (i = 0 ; i < privconn->networks.count && n < nnames ; i++) {
        virNetworkObjLock(privconn->networks.objs[i]);
2422
        if (!virNetworkIsActive(privconn->networks.objs[i]) &&
2423 2424
            !(names[n++] = strdup(privconn->networks.objs[i]->def->name))) {
            virNetworkObjUnlock(privconn->networks.objs[i]);
2425
            goto no_memory;
2426 2427 2428 2429
        }
        virNetworkObjUnlock(privconn->networks.objs[i]);
    }
    testDriverUnlock(privconn);
2430

2431 2432 2433
    return n;

no_memory:
2434
    virReportOOMError(conn);
2435 2436
    for (n = 0 ; n < nnames ; n++)
        VIR_FREE(names[n]);
2437
    testDriverUnlock(privconn);
2438
    return -1;
2439 2440 2441
}

static virNetworkPtr testNetworkCreate(virConnectPtr conn, const char *xml) {
2442
    testConnPtr privconn = conn->privateData;
2443
    virNetworkDefPtr def;
2444
    virNetworkObjPtr net = NULL;
2445
    virNetworkPtr ret = NULL;
2446

2447
    testDriverLock(privconn);
2448
    if ((def = virNetworkDefParseString(conn, xml)) == NULL)
2449
        goto cleanup;
2450

2451
    if ((net = virNetworkAssignDef(conn, &privconn->networks, def)) == NULL)
2452 2453
        goto cleanup;
    def = NULL;
2454
    net->active = 1;
2455

2456
    ret = virGetNetwork(conn, net->def->name, net->def->uuid);
2457

2458 2459
cleanup:
    virNetworkDefFree(def);
2460 2461 2462
    if (net)
        virNetworkObjUnlock(net);
    testDriverUnlock(privconn);
2463
    return ret;
2464 2465 2466
}

static virNetworkPtr testNetworkDefine(virConnectPtr conn, const char *xml) {
2467
    testConnPtr privconn = conn->privateData;
2468
    virNetworkDefPtr def;
2469
    virNetworkObjPtr net = NULL;
2470
    virNetworkPtr ret = NULL;
2471

2472
    testDriverLock(privconn);
2473
    if ((def = virNetworkDefParseString(conn, xml)) == NULL)
2474
        goto cleanup;
2475

2476
    if ((net = virNetworkAssignDef(conn, &privconn->networks, def)) == NULL)
2477 2478
        goto cleanup;
    def = NULL;
2479
    net->persistent = 1;
2480

2481
    ret = virGetNetwork(conn, net->def->name, net->def->uuid);
2482 2483 2484

cleanup:
    virNetworkDefFree(def);
2485 2486 2487
    if (net)
        virNetworkObjUnlock(net);
    testDriverUnlock(privconn);
2488
    return ret;
2489 2490 2491
}

static int testNetworkUndefine(virNetworkPtr network) {
2492 2493
    testConnPtr privconn = network->conn->privateData;
    virNetworkObjPtr privnet;
2494
    int ret = -1;
2495

2496
    testDriverLock(privconn);
2497 2498 2499 2500 2501
    privnet = virNetworkFindByName(&privconn->networks,
                                   network->name);

    if (privnet == NULL) {
        testError(network->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
2502
        goto cleanup;
2503
    }
2504

2505
    if (virNetworkIsActive(privnet)) {
2506 2507
        testError(network->conn, VIR_ERR_INTERNAL_ERROR,
                  _("Network '%s' is still running"), network->name);
2508
        goto cleanup;
2509 2510
    }

2511 2512
    virNetworkRemoveInactive(&privconn->networks,
                             privnet);
2513
    privnet = NULL;
2514
    ret = 0;
2515

2516
cleanup:
2517 2518 2519
    if (privnet)
        virNetworkObjUnlock(privnet);
    testDriverUnlock(privconn);
2520
    return ret;
2521 2522 2523
}

static int testNetworkStart(virNetworkPtr network) {
2524 2525
    testConnPtr privconn = network->conn->privateData;
    virNetworkObjPtr privnet;
2526
    int ret = -1;
2527

2528
    testDriverLock(privconn);
2529 2530
    privnet = virNetworkFindByName(&privconn->networks,
                                   network->name);
2531
    testDriverUnlock(privconn);
2532 2533 2534

    if (privnet == NULL) {
        testError(network->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
2535
        goto cleanup;
2536
    }
2537

2538
    if (virNetworkIsActive(privnet)) {
2539 2540
        testError(network->conn, VIR_ERR_INTERNAL_ERROR,
                  _("Network '%s' is already running"), network->name);
2541
        goto cleanup;
2542 2543
    }

2544
    privnet->active = 1;
2545
    ret = 0;
2546

2547
cleanup:
2548 2549
    if (privnet)
        virNetworkObjUnlock(privnet);
2550
    return ret;
2551 2552 2553
}

static int testNetworkDestroy(virNetworkPtr network) {
2554 2555
    testConnPtr privconn = network->conn->privateData;
    virNetworkObjPtr privnet;
2556
    int ret = -1;
2557

2558
    testDriverLock(privconn);
2559 2560 2561 2562 2563
    privnet = virNetworkFindByName(&privconn->networks,
                                   network->name);

    if (privnet == NULL) {
        testError(network->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
2564
        goto cleanup;
2565
    }
2566

2567 2568 2569 2570
    privnet->active = 0;
    if (!privnet->persistent) {
        virNetworkRemoveInactive(&privconn->networks,
                                 privnet);
2571
        privnet = NULL;
2572
    }
2573 2574 2575
    ret = 0;

cleanup:
2576 2577 2578
    if (privnet)
        virNetworkObjUnlock(privnet);
    testDriverUnlock(privconn);
2579
    return ret;
2580 2581 2582
}

static char *testNetworkDumpXML(virNetworkPtr network, int flags ATTRIBUTE_UNUSED) {
2583 2584
    testConnPtr privconn = network->conn->privateData;
    virNetworkObjPtr privnet;
2585
    char *ret = NULL;
2586

2587
    testDriverLock(privconn);
2588 2589
    privnet = virNetworkFindByName(&privconn->networks,
                                   network->name);
2590
    testDriverUnlock(privconn);
2591 2592 2593

    if (privnet == NULL) {
        testError(network->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
2594
        goto cleanup;
2595
    }
2596

2597 2598 2599
    ret = virNetworkDefFormat(network->conn, privnet->def);

cleanup:
2600 2601
    if (privnet)
        virNetworkObjUnlock(privnet);
2602
    return ret;
2603 2604 2605
}

static char *testNetworkGetBridgeName(virNetworkPtr network) {
2606
    testConnPtr privconn = network->conn->privateData;
2607
    char *bridge = NULL;
2608 2609
    virNetworkObjPtr privnet;

2610
    testDriverLock(privconn);
2611 2612
    privnet = virNetworkFindByName(&privconn->networks,
                                   network->name);
2613
    testDriverUnlock(privconn);
2614 2615 2616

    if (privnet == NULL) {
        testError(network->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
2617
        goto cleanup;
2618 2619
    }

2620 2621 2622 2623 2624 2625 2626 2627
    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))) {
2628
        virReportOOMError(network->conn);
2629
        goto cleanup;
2630
    }
2631 2632

cleanup:
2633 2634
    if (privnet)
        virNetworkObjUnlock(privnet);
2635 2636 2637 2638 2639
    return bridge;
}

static int testNetworkGetAutostart(virNetworkPtr network,
                                   int *autostart) {
2640 2641
    testConnPtr privconn = network->conn->privateData;
    virNetworkObjPtr privnet;
2642
    int ret = -1;
2643

2644
    testDriverLock(privconn);
2645 2646
    privnet = virNetworkFindByName(&privconn->networks,
                                   network->name);
2647
    testDriverUnlock(privconn);
2648 2649 2650

    if (privnet == NULL) {
        testError(network->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
2651
        goto cleanup;
2652 2653
    }

2654
    *autostart = privnet->autostart;
2655 2656 2657
    ret = 0;

cleanup:
2658 2659
    if (privnet)
        virNetworkObjUnlock(privnet);
2660
    return ret;
2661 2662 2663 2664
}

static int testNetworkSetAutostart(virNetworkPtr network,
                                   int autostart) {
2665 2666
    testConnPtr privconn = network->conn->privateData;
    virNetworkObjPtr privnet;
2667
    int ret = -1;
2668

2669
    testDriverLock(privconn);
2670 2671
    privnet = virNetworkFindByName(&privconn->networks,
                                   network->name);
2672
    testDriverUnlock(privconn);
2673 2674 2675

    if (privnet == NULL) {
        testError(network->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
2676
        goto cleanup;
2677 2678
    }

2679
    privnet->autostart = autostart ? 1 : 0;
2680 2681 2682
    ret = 0;

cleanup:
2683 2684
    if (privnet)
        virNetworkObjUnlock(privnet);
2685
    return ret;
2686
}
2687

C
Cole Robinson 已提交
2688

L
Laine Stump 已提交
2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 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 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996
/*
 * 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]);
        if (virInterfaceIsActive(privconn->ifaces.objs[i])) {
            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]);
        if (virInterfaceIsActive(privconn->ifaces.objs[i])) {
            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]);
        if (!virInterfaceIsActive(privconn->ifaces.objs[i])) {
            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]);
        if (!virInterfaceIsActive(privconn->ifaces.objs[i])) {
            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;
}

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 已提交
2997 2998 2999 3000
/*
 * Storage Driver routines
 */

3001 3002
static int testStoragePoolObjSetDefaults(virConnectPtr conn,
                                         virStoragePoolObjPtr pool) {
C
Cole Robinson 已提交
3003 3004 3005 3006 3007 3008 3009

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

    pool->configFile = strdup("\0");
    if (!pool->configFile) {
3010
        virReportOOMError(conn);
C
Cole Robinson 已提交
3011 3012 3013 3014 3015 3016
        return -1;
    }

    return 0;
}

3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031
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;
}

C
Cole Robinson 已提交
3032 3033 3034
static virStoragePoolPtr
testStoragePoolLookupByUUID(virConnectPtr conn,
                            const unsigned char *uuid) {
3035
    testConnPtr privconn = conn->privateData;
3036 3037
    virStoragePoolObjPtr pool;
    virStoragePoolPtr ret = NULL;
C
Cole Robinson 已提交
3038

3039
    testDriverLock(privconn);
3040
    pool = virStoragePoolObjFindByUUID(&privconn->pools, uuid);
3041
    testDriverUnlock(privconn);
3042 3043

    if (pool == NULL) {
C
Cole Robinson 已提交
3044
        testError (conn, VIR_ERR_NO_STORAGE_POOL, NULL);
3045
        goto cleanup;
C
Cole Robinson 已提交
3046 3047
    }

3048 3049 3050
    ret = virGetStoragePool(conn, pool->def->name, pool->def->uuid);

cleanup:
3051 3052
    if (pool)
        virStoragePoolObjUnlock(pool);
3053
    return ret;
C
Cole Robinson 已提交
3054 3055 3056 3057 3058
}

static virStoragePoolPtr
testStoragePoolLookupByName(virConnectPtr conn,
                            const char *name) {
3059
    testConnPtr privconn = conn->privateData;
3060 3061
    virStoragePoolObjPtr pool;
    virStoragePoolPtr ret = NULL;
C
Cole Robinson 已提交
3062

3063
    testDriverLock(privconn);
3064
    pool = virStoragePoolObjFindByName(&privconn->pools, name);
3065
    testDriverUnlock(privconn);
3066 3067

    if (pool == NULL) {
C
Cole Robinson 已提交
3068
        testError (conn, VIR_ERR_NO_STORAGE_POOL, NULL);
3069
        goto cleanup;
C
Cole Robinson 已提交
3070 3071
    }

3072 3073 3074
    ret = virGetStoragePool(conn, pool->def->name, pool->def->uuid);

cleanup:
3075 3076
    if (pool)
        virStoragePoolObjUnlock(pool);
3077
    return ret;
C
Cole Robinson 已提交
3078 3079 3080 3081 3082 3083 3084 3085 3086
}

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

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

3090
    testDriverLock(privconn);
C
Cole Robinson 已提交
3091 3092 3093
    for (i = 0 ; i < privconn->pools.count ; i++)
        if (virStoragePoolObjIsActive(privconn->pools.objs[i]))
            numActive++;
3094
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
3095 3096 3097 3098 3099 3100 3101 3102

    return numActive;
}

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

3106
    testDriverLock(privconn);
C
Cole Robinson 已提交
3107
    memset(names, 0, sizeof(*names)*nnames);
3108 3109
    for (i = 0 ; i < privconn->pools.count && n < nnames ; i++) {
        virStoragePoolObjLock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
3110
        if (virStoragePoolObjIsActive(privconn->pools.objs[i]) &&
3111 3112
            !(names[n++] = strdup(privconn->pools.objs[i]->def->name))) {
            virStoragePoolObjUnlock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
3113
            goto no_memory;
3114 3115 3116 3117
        }
        virStoragePoolObjUnlock(privconn->pools.objs[i]);
    }
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
3118 3119 3120 3121

    return n;

no_memory:
3122
    virReportOOMError(conn);
C
Cole Robinson 已提交
3123 3124
    for (n = 0 ; n < nnames ; n++)
        VIR_FREE(names[n]);
3125
    testDriverUnlock(privconn);
3126
    return -1;
C
Cole Robinson 已提交
3127 3128 3129 3130
}

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

3134 3135 3136
    testDriverLock(privconn);
    for (i = 0 ; i < privconn->pools.count ; i++) {
        virStoragePoolObjLock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
3137 3138
        if (!virStoragePoolObjIsActive(privconn->pools.objs[i]))
            numInactive++;
3139 3140 3141
        virStoragePoolObjUnlock(privconn->pools.objs[i]);
    }
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
3142 3143 3144 3145 3146 3147 3148 3149

    return numInactive;
}

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

3153
    testDriverLock(privconn);
C
Cole Robinson 已提交
3154
    memset(names, 0, sizeof(*names)*nnames);
3155 3156
    for (i = 0 ; i < privconn->pools.count && n < nnames ; i++) {
        virStoragePoolObjLock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
3157
        if (!virStoragePoolObjIsActive(privconn->pools.objs[i]) &&
3158 3159
            !(names[n++] = strdup(privconn->pools.objs[i]->def->name))) {
            virStoragePoolObjUnlock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
3160
            goto no_memory;
3161 3162 3163 3164
        }
        virStoragePoolObjUnlock(privconn->pools.objs[i]);
    }
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
3165 3166 3167 3168

    return n;

no_memory:
3169
    virReportOOMError(conn);
C
Cole Robinson 已提交
3170 3171
    for (n = 0 ; n < nnames ; n++)
        VIR_FREE(names[n]);
3172
    testDriverUnlock(privconn);
3173
    return -1;
C
Cole Robinson 已提交
3174 3175 3176 3177
}


static int
3178
testStoragePoolStart(virStoragePoolPtr pool,
C
Cole Robinson 已提交
3179
                     unsigned int flags ATTRIBUTE_UNUSED) {
3180 3181
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
3182
    int ret = -1;
3183

3184
    testDriverLock(privconn);
3185 3186
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
3187
    testDriverUnlock(privconn);
3188 3189 3190

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

3194 3195 3196 3197 3198
    if (virStoragePoolObjIsActive(privpool)) {
        testError(pool->conn, VIR_ERR_INTERNAL_ERROR,
                  _("storage pool '%s' is already active"), pool->name);
        goto cleanup;
    }
C
Cole Robinson 已提交
3199 3200

    privpool->active = 1;
3201
    ret = 0;
C
Cole Robinson 已提交
3202

3203
cleanup:
3204 3205
    if (privpool)
        virStoragePoolObjUnlock(privpool);
3206
    return ret;
C
Cole Robinson 已提交
3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222
}

static char *
testStorageFindPoolSources(virConnectPtr conn ATTRIBUTE_UNUSED,
                           const char *type ATTRIBUTE_UNUSED,
                           const char *srcSpec ATTRIBUTE_UNUSED,
                           unsigned int flags ATTRIBUTE_UNUSED)
{
    return NULL;
}


static virStoragePoolPtr
testStoragePoolCreate(virConnectPtr conn,
                      const char *xml,
                      unsigned int flags ATTRIBUTE_UNUSED) {
3223
    testConnPtr privconn = conn->privateData;
C
Cole Robinson 已提交
3224
    virStoragePoolDefPtr def;
3225
    virStoragePoolObjPtr pool = NULL;
3226
    virStoragePoolPtr ret = NULL;
C
Cole Robinson 已提交
3227

3228
    testDriverLock(privconn);
3229
    if (!(def = virStoragePoolDefParseString(conn, xml)))
3230
        goto cleanup;
C
Cole Robinson 已提交
3231

3232 3233 3234 3235
    pool = virStoragePoolObjFindByUUID(&privconn->pools, def->uuid);
    if (!pool)
        pool = virStoragePoolObjFindByName(&privconn->pools, def->name);
    if (pool) {
C
Cole Robinson 已提交
3236 3237
        testError(conn, VIR_ERR_INTERNAL_ERROR,
                  "%s", _("storage pool already exists"));
3238
        goto cleanup;
C
Cole Robinson 已提交
3239 3240
    }

3241
    if (!(pool = virStoragePoolObjAssignDef(conn, &privconn->pools, def)))
3242
        goto cleanup;
3243
    def = NULL;
C
Cole Robinson 已提交
3244

3245
    if (testStoragePoolObjSetDefaults(conn, pool) == -1) {
C
Cole Robinson 已提交
3246
        virStoragePoolObjRemove(&privconn->pools, pool);
3247 3248
        pool = NULL;
        goto cleanup;
C
Cole Robinson 已提交
3249 3250 3251
    }
    pool->active = 1;

3252 3253 3254 3255
    ret = virGetStoragePool(conn, pool->def->name, pool->def->uuid);

cleanup:
    virStoragePoolDefFree(def);
3256 3257 3258
    if (pool)
        virStoragePoolObjUnlock(pool);
    testDriverUnlock(privconn);
3259
    return ret;
C
Cole Robinson 已提交
3260 3261 3262 3263 3264 3265
}

static virStoragePoolPtr
testStoragePoolDefine(virConnectPtr conn,
                      const char *xml,
                      unsigned int flags ATTRIBUTE_UNUSED) {
3266
    testConnPtr privconn = conn->privateData;
C
Cole Robinson 已提交
3267
    virStoragePoolDefPtr def;
3268
    virStoragePoolObjPtr pool = NULL;
3269
    virStoragePoolPtr ret = NULL;
C
Cole Robinson 已提交
3270

3271
    testDriverLock(privconn);
3272
    if (!(def = virStoragePoolDefParseString(conn, xml)))
3273
        goto cleanup;
C
Cole Robinson 已提交
3274 3275 3276 3277 3278

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

3279
    if (!(pool = virStoragePoolObjAssignDef(conn, &privconn->pools, def)))
3280 3281
        goto cleanup;
    def = NULL;
C
Cole Robinson 已提交
3282

3283
    if (testStoragePoolObjSetDefaults(conn, pool) == -1) {
C
Cole Robinson 已提交
3284
        virStoragePoolObjRemove(&privconn->pools, pool);
3285 3286
        pool = NULL;
        goto cleanup;
C
Cole Robinson 已提交
3287 3288
    }

3289 3290 3291 3292
    ret = virGetStoragePool(conn, pool->def->name, pool->def->uuid);

cleanup:
    virStoragePoolDefFree(def);
3293 3294 3295
    if (pool)
        virStoragePoolObjUnlock(pool);
    testDriverUnlock(privconn);
3296
    return ret;
C
Cole Robinson 已提交
3297 3298 3299
}

static int
3300 3301 3302
testStoragePoolUndefine(virStoragePoolPtr pool) {
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
3303
    int ret = -1;
3304

3305
    testDriverLock(privconn);
3306 3307 3308 3309 3310
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);

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

3314 3315 3316 3317 3318
    if (virStoragePoolObjIsActive(privpool)) {
        testError(pool->conn, VIR_ERR_INTERNAL_ERROR,
                  _("storage pool '%s' is already active"), pool->name);
        goto cleanup;
    }
C
Cole Robinson 已提交
3319 3320

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

3323
cleanup:
3324 3325 3326
    if (privpool)
        virStoragePoolObjUnlock(privpool);
    testDriverUnlock(privconn);
3327
    return ret;
C
Cole Robinson 已提交
3328 3329 3330
}

static int
3331
testStoragePoolBuild(virStoragePoolPtr pool,
C
Cole Robinson 已提交
3332
                     unsigned int flags ATTRIBUTE_UNUSED) {
3333 3334
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
3335
    int ret = -1;
3336

3337
    testDriverLock(privconn);
3338 3339
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
3340
    testDriverUnlock(privconn);
3341 3342 3343

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

3347 3348 3349 3350 3351
    if (virStoragePoolObjIsActive(privpool)) {
        testError(pool->conn, VIR_ERR_INTERNAL_ERROR,
                  _("storage pool '%s' is already active"), pool->name);
        goto cleanup;
    }
3352
    ret = 0;
C
Cole Robinson 已提交
3353

3354
cleanup:
3355 3356
    if (privpool)
        virStoragePoolObjUnlock(privpool);
3357
    return ret;
C
Cole Robinson 已提交
3358 3359 3360 3361
}


static int
3362 3363 3364
testStoragePoolDestroy(virStoragePoolPtr pool) {
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
3365
    int ret = -1;
3366

3367
    testDriverLock(privconn);
3368 3369 3370 3371 3372
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);

    if (privpool == NULL) {
        testError(pool->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
3373
        goto cleanup;
3374 3375 3376 3377 3378
    }

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

    privpool->active = 0;

3384
    if (privpool->configFile == NULL) {
C
Cole Robinson 已提交
3385
        virStoragePoolObjRemove(&privconn->pools, privpool);
3386 3387
        privpool = NULL;
    }
3388
    ret = 0;
C
Cole Robinson 已提交
3389

3390
cleanup:
3391 3392 3393
    if (privpool)
        virStoragePoolObjUnlock(privpool);
    testDriverUnlock(privconn);
3394
    return ret;
C
Cole Robinson 已提交
3395 3396 3397 3398
}


static int
3399
testStoragePoolDelete(virStoragePoolPtr pool,
C
Cole Robinson 已提交
3400
                      unsigned int flags ATTRIBUTE_UNUSED) {
3401 3402
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
3403
    int ret = -1;
3404

3405
    testDriverLock(privconn);
3406 3407
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
3408
    testDriverUnlock(privconn);
3409 3410 3411

    if (privpool == NULL) {
        testError(pool->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
3412 3413 3414 3415 3416 3417 3418
        goto cleanup;
    }

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

3421
    ret = 0;
C
Cole Robinson 已提交
3422

3423
cleanup:
3424 3425
    if (privpool)
        virStoragePoolObjUnlock(privpool);
3426
    return ret;
C
Cole Robinson 已提交
3427 3428 3429 3430
}


static int
3431
testStoragePoolRefresh(virStoragePoolPtr pool,
C
Cole Robinson 已提交
3432
                       unsigned int flags ATTRIBUTE_UNUSED) {
3433 3434
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
3435
    int ret = -1;
3436

3437
    testDriverLock(privconn);
3438 3439
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
3440
    testDriverUnlock(privconn);
3441 3442 3443

    if (privpool == NULL) {
        testError(pool->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
3444
        goto cleanup;
3445 3446 3447 3448 3449
    }

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

3454
cleanup:
3455 3456
    if (privpool)
        virStoragePoolObjUnlock(privpool);
3457
    return ret;
C
Cole Robinson 已提交
3458 3459 3460 3461
}


static int
3462
testStoragePoolGetInfo(virStoragePoolPtr pool,
C
Cole Robinson 已提交
3463
                       virStoragePoolInfoPtr info) {
3464 3465
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
3466
    int ret = -1;
3467

3468
    testDriverLock(privconn);
3469 3470
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
3471
    testDriverUnlock(privconn);
3472 3473 3474

    if (privpool == NULL) {
        testError(pool->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
3475
        goto cleanup;
3476
    }
C
Cole Robinson 已提交
3477 3478 3479 3480 3481 3482 3483 3484 3485

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

3488
cleanup:
3489 3490
    if (privpool)
        virStoragePoolObjUnlock(privpool);
3491
    return ret;
C
Cole Robinson 已提交
3492 3493 3494
}

static char *
3495
testStoragePoolDumpXML(virStoragePoolPtr pool,
C
Cole Robinson 已提交
3496
                       unsigned int flags ATTRIBUTE_UNUSED) {
3497 3498
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
3499
    char *ret = NULL;
3500

3501
    testDriverLock(privconn);
3502 3503
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
3504
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
3505

3506 3507
    if (privpool == NULL) {
        testError(pool->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
3508
        goto cleanup;
3509 3510
    }

3511 3512 3513
    ret = virStoragePoolDefFormat(pool->conn, privpool->def);

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

static int
3520
testStoragePoolGetAutostart(virStoragePoolPtr pool,
C
Cole Robinson 已提交
3521
                            int *autostart) {
3522 3523
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
3524
    int ret = -1;
3525

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

    if (privpool == NULL) {
        testError(pool->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
3533
        goto cleanup;
3534
    }
C
Cole Robinson 已提交
3535 3536 3537 3538 3539 3540

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

3543
cleanup:
3544 3545
    if (privpool)
        virStoragePoolObjUnlock(privpool);
3546
    return ret;
C
Cole Robinson 已提交
3547 3548 3549
}

static int
3550
testStoragePoolSetAutostart(virStoragePoolPtr pool,
C
Cole Robinson 已提交
3551
                            int autostart) {
3552 3553
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
3554
    int ret = -1;
3555

3556
    testDriverLock(privconn);
3557 3558
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
3559
    testDriverUnlock(privconn);
3560 3561 3562

    if (privpool == NULL) {
        testError(pool->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
3563
        goto cleanup;
3564
    }
C
Cole Robinson 已提交
3565 3566

    if (!privpool->configFile) {
3567
        testError(pool->conn, VIR_ERR_INVALID_ARG,
C
Cole Robinson 已提交
3568
                  "%s", _("pool has no config file"));
3569
        goto cleanup;
C
Cole Robinson 已提交
3570 3571 3572 3573
    }

    autostart = (autostart != 0);
    privpool->autostart = autostart;
3574 3575 3576
    ret = 0;

cleanup:
3577 3578
    if (privpool)
        virStoragePoolObjUnlock(privpool);
3579
    return ret;
C
Cole Robinson 已提交
3580 3581 3582 3583
}


static int
3584 3585 3586
testStoragePoolNumVolumes(virStoragePoolPtr pool) {
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
3587
    int ret = -1;
3588

3589
    testDriverLock(privconn);
3590 3591
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
3592
    testDriverUnlock(privconn);
3593 3594 3595

    if (privpool == NULL) {
        testError(pool->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
3596
        goto cleanup;
3597 3598 3599 3600 3601
    }

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

3605 3606 3607
    ret = privpool->volumes.count;

cleanup:
3608 3609
    if (privpool)
        virStoragePoolObjUnlock(privpool);
3610
    return ret;
C
Cole Robinson 已提交
3611 3612 3613
}

static int
3614
testStoragePoolListVolumes(virStoragePoolPtr pool,
C
Cole Robinson 已提交
3615 3616
                           char **const names,
                           int maxnames) {
3617 3618
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
C
Cole Robinson 已提交
3619 3620
    int i = 0, n = 0;

3621
    memset(names, 0, maxnames * sizeof(*names));
3622 3623

    testDriverLock(privconn);
3624 3625
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
3626
    testDriverUnlock(privconn);
3627 3628 3629

    if (privpool == NULL) {
        testError(pool->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
3630
        goto cleanup;
3631 3632 3633 3634 3635 3636
    }


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

C
Cole Robinson 已提交
3640 3641
    for (i = 0 ; i < privpool->volumes.count && n < maxnames ; i++) {
        if ((names[n++] = strdup(privpool->volumes.objs[i]->name)) == NULL) {
3642
            virReportOOMError(pool->conn);
C
Cole Robinson 已提交
3643 3644 3645 3646
            goto cleanup;
        }
    }

3647
    virStoragePoolObjUnlock(privpool);
C
Cole Robinson 已提交
3648 3649 3650 3651 3652 3653
    return n;

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

3654
    memset(names, 0, maxnames * sizeof(*names));
3655 3656
    if (privpool)
        virStoragePoolObjUnlock(privpool);
C
Cole Robinson 已提交
3657 3658 3659 3660 3661
    return -1;
}


static virStorageVolPtr
3662
testStorageVolumeLookupByName(virStoragePoolPtr pool,
C
Cole Robinson 已提交
3663
                              const char *name ATTRIBUTE_UNUSED) {
3664 3665 3666
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol;
3667
    virStorageVolPtr ret = NULL;
3668

3669
    testDriverLock(privconn);
3670 3671
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
3672
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
3673

3674 3675
    if (privpool == NULL) {
        testError(pool->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
3676
        goto cleanup;
3677 3678 3679 3680 3681 3682
    }


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

    privvol = virStorageVolDefFindByName(privpool, name);

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

3694 3695 3696 3697
    ret = virGetStorageVol(pool->conn, privpool->def->name,
                           privvol->name, privvol->key);

cleanup:
3698 3699
    if (privpool)
        virStoragePoolObjUnlock(privpool);
3700
    return ret;
C
Cole Robinson 已提交
3701 3702 3703 3704 3705 3706
}


static virStorageVolPtr
testStorageVolumeLookupByKey(virConnectPtr conn,
                             const char *key) {
3707
    testConnPtr privconn = conn->privateData;
C
Cole Robinson 已提交
3708
    unsigned int i;
3709
    virStorageVolPtr ret = NULL;
C
Cole Robinson 已提交
3710

3711
    testDriverLock(privconn);
C
Cole Robinson 已提交
3712
    for (i = 0 ; i < privconn->pools.count ; i++) {
3713
        virStoragePoolObjLock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
3714
        if (virStoragePoolObjIsActive(privconn->pools.objs[i])) {
3715
            virStorageVolDefPtr privvol =
C
Cole Robinson 已提交
3716 3717
                virStorageVolDefFindByKey(privconn->pools.objs[i], key);

3718 3719 3720 3721 3722
            if (privvol) {
                ret = virGetStorageVol(conn,
                                       privconn->pools.objs[i]->def->name,
                                       privvol->name,
                                       privvol->key);
3723
                virStoragePoolObjUnlock(privconn->pools.objs[i]);
3724 3725
                break;
            }
C
Cole Robinson 已提交
3726
        }
3727
        virStoragePoolObjUnlock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
3728
    }
3729
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
3730

3731 3732 3733 3734 3735
    if (!ret)
        testError(conn, VIR_ERR_INVALID_STORAGE_VOL,
                  _("no storage vol with matching key '%s'"), key);

    return ret;
C
Cole Robinson 已提交
3736 3737 3738 3739 3740
}

static virStorageVolPtr
testStorageVolumeLookupByPath(virConnectPtr conn,
                              const char *path) {
3741
    testConnPtr privconn = conn->privateData;
C
Cole Robinson 已提交
3742
    unsigned int i;
3743
    virStorageVolPtr ret = NULL;
C
Cole Robinson 已提交
3744

3745
    testDriverLock(privconn);
C
Cole Robinson 已提交
3746
    for (i = 0 ; i < privconn->pools.count ; i++) {
3747
        virStoragePoolObjLock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
3748
        if (virStoragePoolObjIsActive(privconn->pools.objs[i])) {
3749
            virStorageVolDefPtr privvol =
C
Cole Robinson 已提交
3750 3751
                virStorageVolDefFindByPath(privconn->pools.objs[i], path);

3752 3753 3754 3755 3756
            if (privvol) {
                ret = virGetStorageVol(conn,
                                       privconn->pools.objs[i]->def->name,
                                       privvol->name,
                                       privvol->key);
3757
                virStoragePoolObjUnlock(privconn->pools.objs[i]);
3758 3759
                break;
            }
C
Cole Robinson 已提交
3760
        }
3761
        virStoragePoolObjUnlock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
3762
    }
3763
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
3764

3765 3766 3767 3768 3769
    if (!ret)
        testError(conn, VIR_ERR_INVALID_STORAGE_VOL,
                  _("no storage vol with matching path '%s'"), path);

    return ret;
C
Cole Robinson 已提交
3770 3771 3772
}

static virStorageVolPtr
3773
testStorageVolumeCreateXML(virStoragePoolPtr pool,
C
Cole Robinson 已提交
3774 3775
                           const char *xmldesc,
                           unsigned int flags ATTRIBUTE_UNUSED) {
3776 3777
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
3778 3779
    virStorageVolDefPtr privvol = NULL;
    virStorageVolPtr ret = NULL;
3780

3781
    testDriverLock(privconn);
3782 3783
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
3784
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
3785

3786 3787
    if (privpool == NULL) {
        testError(pool->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
3788
        goto cleanup;
3789 3790 3791 3792 3793
    }

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

3797
    privvol = virStorageVolDefParseString(pool->conn, privpool->def, xmldesc);
3798
    if (privvol == NULL)
3799
        goto cleanup;
3800 3801

    if (virStorageVolDefFindByName(privpool, privvol->name)) {
3802
        testError(pool->conn, VIR_ERR_INVALID_STORAGE_VOL,
C
Cole Robinson 已提交
3803
                  "%s", _("storage vol already exists"));
3804
        goto cleanup;
C
Cole Robinson 已提交
3805 3806 3807
    }

    /* Make sure enough space */
3808
    if ((privpool->def->allocation + privvol->allocation) >
C
Cole Robinson 已提交
3809
         privpool->def->capacity) {
3810
        testError(pool->conn, VIR_ERR_INTERNAL_ERROR,
C
Cole Robinson 已提交
3811
                  _("Not enough free space in pool for volume '%s'"),
3812
                  privvol->name);
3813
        goto cleanup;
C
Cole Robinson 已提交
3814 3815 3816 3817
    }

    if (VIR_REALLOC_N(privpool->volumes.objs,
                      privpool->volumes.count+1) < 0) {
3818
        virReportOOMError(pool->conn);
3819
        goto cleanup;
C
Cole Robinson 已提交
3820 3821
    }

3822 3823 3824
    if (virAsprintf(&privvol->target.path, "%s/%s",
                    privpool->def->target.path,
                    privvol->name) == -1) {
3825
        virReportOOMError(pool->conn);
3826
        goto cleanup;
C
Cole Robinson 已提交
3827 3828
    }

3829 3830
    privvol->key = strdup(privvol->target.path);
    if (privvol->key == NULL) {
3831
        virReportOOMError(pool->conn);
3832
        goto cleanup;
C
Cole Robinson 已提交
3833 3834
    }

3835
    privpool->def->allocation += privvol->allocation;
C
Cole Robinson 已提交
3836 3837 3838
    privpool->def->available = (privpool->def->capacity -
                                privpool->def->allocation);

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

3841 3842
    ret = virGetStorageVol(pool->conn, privpool->def->name,
                           privvol->name, privvol->key);
3843
    privvol = NULL;
3844 3845 3846

cleanup:
    virStorageVolDefFree(privvol);
3847 3848
    if (privpool)
        virStoragePoolObjUnlock(privpool);
3849
    return ret;
C
Cole Robinson 已提交
3850 3851
}

3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877
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;
    }

3878
    privvol = virStorageVolDefParseString(pool->conn, privpool->def, xmldesc);
3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912
    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;
    }

3913 3914 3915
    if (virAsprintf(&privvol->target.path, "%s/%s",
                    privpool->def->target.path,
                    privvol->name) == -1) {
3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942
        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 已提交
3943
static int
3944
testStorageVolumeDelete(virStorageVolPtr vol,
C
Cole Robinson 已提交
3945
                        unsigned int flags ATTRIBUTE_UNUSED) {
3946 3947 3948
    testConnPtr privconn = vol->conn->privateData;
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol;
C
Cole Robinson 已提交
3949
    int i;
3950
    int ret = -1;
C
Cole Robinson 已提交
3951

3952
    testDriverLock(privconn);
3953 3954
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           vol->pool);
3955
    testDriverUnlock(privconn);
3956 3957 3958

    if (privpool == NULL) {
        testError(vol->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
3959
        goto cleanup;
3960 3961 3962 3963 3964 3965 3966 3967 3968
    }


    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);
3969
        goto cleanup;
3970 3971 3972 3973 3974
    }

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


C
Cole Robinson 已提交
3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001
    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;
        }
    }
4002
    ret = 0;
C
Cole Robinson 已提交
4003

4004
cleanup:
4005 4006
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4007
    return ret;
C
Cole Robinson 已提交
4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023
}


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
4024
testStorageVolumeGetInfo(virStorageVolPtr vol,
C
Cole Robinson 已提交
4025
                         virStorageVolInfoPtr info) {
4026 4027 4028
    testConnPtr privconn = vol->conn->privateData;
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol;
4029
    int ret = -1;
4030

4031
    testDriverLock(privconn);
4032 4033
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           vol->pool);
4034
    testDriverUnlock(privconn);
4035 4036 4037

    if (privpool == NULL) {
        testError(vol->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
4038
        goto cleanup;
4039 4040 4041 4042 4043 4044 4045 4046
    }

    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);
4047
        goto cleanup;
4048 4049 4050 4051 4052
    }

    if (!virStoragePoolObjIsActive(privpool)) {
        testError(vol->conn, VIR_ERR_INTERNAL_ERROR,
                  _("storage pool '%s' is not active"), vol->pool);
4053
        goto cleanup;
4054
    }
C
Cole Robinson 已提交
4055 4056 4057 4058 4059

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

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

static char *
4069
testStorageVolumeGetXMLDesc(virStorageVolPtr vol,
C
Cole Robinson 已提交
4070
                            unsigned int flags ATTRIBUTE_UNUSED) {
4071 4072 4073
    testConnPtr privconn = vol->conn->privateData;
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol;
4074
    char *ret = NULL;
4075

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

    if (privpool == NULL) {
        testError(vol->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
4083
        goto cleanup;
4084 4085 4086 4087 4088 4089 4090 4091
    }

    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);
4092
        goto cleanup;
4093
    }
C
Cole Robinson 已提交
4094

4095 4096 4097
    if (!virStoragePoolObjIsActive(privpool)) {
        testError(vol->conn, VIR_ERR_INTERNAL_ERROR,
                  _("storage pool '%s' is not active"), vol->pool);
4098
        goto cleanup;
4099 4100
    }

4101 4102 4103
    ret = virStorageVolDefFormat(vol->conn, privpool->def, privvol);

cleanup:
4104 4105
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4106
    return ret;
C
Cole Robinson 已提交
4107 4108 4109
}

static char *
4110 4111 4112 4113
testStorageVolumeGetPath(virStorageVolPtr vol) {
    testConnPtr privconn = vol->conn->privateData;
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol;
4114
    char *ret = NULL;
4115

4116
    testDriverLock(privconn);
4117 4118
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           vol->pool);
4119
    testDriverUnlock(privconn);
4120 4121 4122

    if (privpool == NULL) {
        testError(vol->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
4123
        goto cleanup;
4124 4125 4126 4127 4128 4129 4130 4131
    }

    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);
4132
        goto cleanup;
4133 4134 4135 4136 4137
    }

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

C
Cole Robinson 已提交
4141
    ret = strdup(privvol->target.path);
4142
    if (ret == NULL)
4143
        virReportOOMError(vol->conn);
4144 4145

cleanup:
4146 4147
    if (privpool)
        virStoragePoolObjUnlock(privpool);
C
Cole Robinson 已提交
4148 4149 4150
    return ret;
}

4151

4152
/* Node device implementations */
4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167
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;
}

4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305
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;
}

4306

4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377
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;
}


/* Domain event implementations */
4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 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 4429 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 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473
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);
}

4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487
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;
}
4488

4489 4490 4491 4492 4493
static virDriver testDriver = {
    VIR_DRV_TEST,
    "Test",
    testOpen, /* open */
    testClose, /* close */
4494
    NULL, /* supports_feature */
4495 4496
    NULL, /* type */
    testGetVersion, /* version */
4497
    testGetHostname, /* getHostname */
4498 4499 4500 4501 4502
    testGetMaxVCPUs, /* getMaxVcpus */
    testNodeGetInfo, /* nodeGetInfo */
    testGetCapabilities, /* getCapabilities */
    testListDomains, /* listDomains */
    testNumOfDomains, /* numOfDomains */
4503
    testDomainCreateXML, /* domainCreateXML */
4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523
    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 */
    NULL, /* domainPinVcpu */
    NULL, /* domainGetVcpus */
    NULL, /* domainGetMaxVcpus */
4524 4525
    NULL, /* domainGetSecurityLabel */
    NULL, /* nodeGetSecurityModel */
4526
    testDomainDumpXML, /* domainDumpXML */
4527 4528
    NULL, /* domainXMLFromNative */
    NULL, /* domainXMLToNative */
4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540
    testListDefinedDomains, /* listDefinedDomains */
    testNumOfDefinedDomains, /* numOfDefinedDomains */
    testDomainCreate, /* domainCreate */
    testDomainDefineXML, /* domainDefineXML */
    testDomainUndefine, /* domainUndefine */
    NULL, /* domainAttachDevice */
    NULL, /* domainDetachDevice */
    testDomainGetAutostart, /* domainGetAutostart */
    testDomainSetAutostart, /* domainSetAutostart */
    testDomainGetSchedulerType, /* domainGetSchedulerType */
    testDomainGetSchedulerParams, /* domainGetSchedulerParameters */
    testDomainSetSchedulerParams, /* domainSetSchedulerParameters */
4541 4542 4543
    NULL, /* domainMigratePrepare */
    NULL, /* domainMigratePerform */
    NULL, /* domainMigrateFinish */
4544 4545
    testDomainBlockStats, /* domainBlockStats */
    testDomainInterfaceStats, /* domainInterfaceStats */
R
Richard W.M. Jones 已提交
4546
    NULL, /* domainBlockPeek */
R
Richard W.M. Jones 已提交
4547
    NULL, /* domainMemoryPeek */
4548
    testNodeGetCellsFreeMemory, /* nodeGetCellsFreeMemory */
4549
    NULL, /* getFreeMemory */
4550 4551
    testDomainEventRegister, /* domainEventRegister */
    testDomainEventDeregister, /* domainEventDeregister */
D
Daniel Veillard 已提交
4552 4553
    NULL, /* domainMigratePrepare2 */
    NULL, /* domainMigrateFinish2 */
4554
    NULL, /* nodeDeviceDettach */
4555 4556
    NULL, /* nodeDeviceReAttach */
    NULL, /* nodeDeviceReset */
C
Chris Lalancette 已提交
4557
    NULL, /* domainMigratePrepareTunnel */
4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580
};

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

L
Laine Stump 已提交
4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598
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 */
};


4599 4600 4601 4602
static virStorageDriver testStorageDriver = {
    .name = "Test",
    .open = testStorageOpen,
    .close = testStorageClose,
C
Cole Robinson 已提交
4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630

    .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,
4631
    .volCreateXMLFrom = testStorageVolumeCreateXMLFrom,
C
Cole Robinson 已提交
4632 4633 4634 4635
    .volDelete = testStorageVolumeDelete,
    .volGetInfo = testStorageVolumeGetInfo,
    .volGetXMLDesc = testStorageVolumeGetXMLDesc,
    .volGetPath = testStorageVolumeGetPath,
4636 4637
};

4638 4639 4640 4641
static virDeviceMonitor testDevMonitor = {
    .name = "Test",
    .open = testDevMonOpen,
    .close = testDevMonClose,
4642 4643 4644 4645 4646 4647 4648 4649 4650 4651

    .numOfDevices = testNodeNumOfDevices,
    .listDevices = testNodeListDevices,
    .deviceLookupByName = testNodeDeviceLookupByName,
    .deviceDumpXML = testNodeDeviceDumpXML,
    .deviceGetParent = testNodeDeviceGetParent,
    .deviceNumOfCaps = testNodeDeviceNumOfCaps,
    .deviceListCaps = testNodeDeviceListCaps,
    //.deviceCreateXML = nodeDeviceCreateXML;
    //.deviceDestroy = nodeDeviceDestroy;
4652 4653
};

4654 4655 4656 4657 4658
static virSecretDriver testSecretDriver = {
    .name = "Test",
    .open = testSecretOpen,
    .close = testSecretClose,
};
4659 4660


4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672
/**
 * testRegister:
 *
 * Registers the test driver
 */
int
testRegister(void)
{
    if (virRegisterDriver(&testDriver) < 0)
        return -1;
    if (virRegisterNetworkDriver(&testNetworkDriver) < 0)
        return -1;
L
Laine Stump 已提交
4673 4674
    if (virRegisterInterfaceDriver(&testInterfaceDriver) < 0)
        return -1;
4675 4676
    if (virRegisterStorageDriver(&testStorageDriver) < 0)
        return -1;
4677 4678
    if (virRegisterDeviceMonitor(&testDevMonitor) < 0)
        return -1;
4679 4680
    if (virRegisterSecretDriver(&testSecretDriver) < 0)
        return -1;
4681

4682 4683
    return 0;
}