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

24
#include <config.h>
25

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

34 35

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

54 55
#define VIR_FROM_THIS VIR_FROM_TEST

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

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

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

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

#define MAX_CELLS 128
76

77
struct _testConn {
78
    virMutex lock;
79

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


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

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

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

117

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

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


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

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

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

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

    return priv;
}

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

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


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

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

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

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

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

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

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

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

208 209 210 211 212 213 214 215
    caps->host.secModel.model = strdup("testSecurity");
    if (!caps->host.secModel.model)
        goto no_memory;

    caps->host.secModel.doi = strdup("");
    if (!caps->host.secModel.doi)
        goto no_memory;

216
    return caps;
217

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

224

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


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

L
Laine Stump 已提交
249 250 251 252 253 254 255 256 257 258 259
static const char *defaultInterfaceXML =
"<interface type=\"ethernet\" name=\"eth1\">"
"  <start mode=\"onboot\"/>"
"  <mac address=\"aa:bb:cc:dd:ee:ff\"/>"
"  <mtu size=\"1492\"/>"
"  <protocol family=\"ipv4\">"
"    <ip address=\"192.168.0.5\" prefix=\"24\"/>"
"    <route gateway=\"192.168.0.1\"/>"
"  </protocol>"
"</interface>";

C
Cole Robinson 已提交
260 261 262 263 264 265 266 267
static const char *defaultPoolXML =
"<pool type='dir'>"
"  <name>default-pool</name>"
"  <target>"
"    <path>/default-pool</path>"
"  </target>"
"</pool>";

268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
static const char *defaultPoolSourcesLogicalXML =
"<sources>\n"
"  <source>\n"
"    <device path='/dev/sda20'/>\n"
"    <name>testvg1</name>\n"
"    <format type='lvm2'/>\n"
"  </source>\n"
"  <source>\n"
"    <device path='/dev/sda21'/>\n"
"    <name>testvg2</name>\n"
"    <format type='lvm2'/>\n"
"  </source>\n"
"</sources>\n";

static const char *defaultPoolSourcesNetFSXML =
"<sources>\n"
"  <source>\n"
"    <host name='%s'/>\n"
"    <dir path='/testshare'/>\n"
"    <format type='nfs'/>\n"
"  </source>\n"
"</sources>\n";

291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
static const char *defaultNodeXML =
"<device>"
"  <name>computer</name>"
"  <capability type='system'>"
"    <hardware>"
"      <vendor>Libvirt</vendor>"
"      <version>Test driver</version>"
"      <serial>123456</serial>"
"      <uuid>11111111-2222-3333-4444-555555555555</uuid>"
"    </hardware>"
"    <firmware>"
"      <vendor>Libvirt</vendor>"
"      <version>Test Driver</version>"
"      <release_date>01/22/2007</release_date>"
"    </firmware>"
"  </capability>"
"</device>";

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

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

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

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

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

342
    testError(VIR_ERR_INTERNAL_ERROR,
343 344 345 346
              _("Exceeded max iface limit %d"), maxif);
    return NULL;
}

347
static int
348
testDomainGenerateIfnames(virDomainDefPtr domdef)
349 350 351 352 353 354 355 356
{
    int i = 0;

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

357
        ifname = testDomainGenerateIfname(domdef);
358
        if (!ifname)
359
            return -1;
360 361 362 363

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

364
    return 0;
365 366
}

367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
/* Helper to update info for a single VCPU */
static int
testDomainUpdateVCPU(virConnectPtr conn ATTRIBUTE_UNUSED,
                     virDomainObjPtr dom,
                     int vcpu,
                     int maplen,
                     int maxcpu)
{
    testDomainObjPrivatePtr privdata = dom->privateData;
    virVcpuInfoPtr info = &privdata->vcpu_infos[vcpu];
    unsigned char *cpumap = VIR_GET_CPUMAP(privdata->cpumaps, maplen, vcpu);
    int j;

    memset(info, 0, sizeof(virVcpuInfo));
    memset(cpumap, 0, maplen);

    info->number    = vcpu;
    info->state     = VIR_VCPU_RUNNING;
    info->cpuTime   = 5000000;
    info->cpu       = 0;

    if (dom->def->cpumask) {
        for (j = 0; j < maxcpu && j < VIR_DOMAIN_CPUMASK_LEN; ++j) {
            if (dom->def->cpumask[j]) {
                VIR_USE_CPU(cpumap, j);
                info->cpu = j;
            }
        }
    } else {
        for (j = 0; j < maxcpu; ++j) {
            if ((j % 3) == 0) {
                /* Mark of every third CPU as usable */
                VIR_USE_CPU(cpumap, j);
                info->cpu = j;
            }
        }
    }

    return 0;
}

/*
 * Update domain VCPU amount and info
 *
 * @conn: virConnectPtr
 * @dom : domain needing updates
 * @nvcpus: New amount of vcpus for the domain
 * @clear_all: If true, rebuild info for ALL vcpus, not just newly added vcpus
 */
static int
testDomainUpdateVCPUs(virConnectPtr conn,
                      virDomainObjPtr dom,
                      int nvcpus,
                      unsigned int clear_all)
{
    testConnPtr privconn = conn->privateData;
    testDomainObjPrivatePtr privdata = dom->privateData;
    int i, ret = -1;
    int cpumaplen, maxcpu;

    maxcpu  = VIR_NODEINFO_MAXCPUS(privconn->nodeInfo);
    cpumaplen = VIR_CPU_MAPLEN(maxcpu);

    if (VIR_REALLOC_N(privdata->vcpu_infos, nvcpus) < 0) {
431
        virReportOOMError();
432 433 434 435
        goto cleanup;
    }

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

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

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

    ret = 0;
cleanup:
    return ret;
}

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

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

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

473 474 475
    ret = 0;
cleanup:
    return ret;
476
}
477

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

488 489 490 491 492
    privdom->state = VIR_DOMAIN_SHUTOFF;
    privdom->def->id = -1;
    domain->id = -1;
}

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

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

519
    testDriverLock(privconn);
520
    conn->privateData = privconn;
521 522

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

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

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

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

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

    privconn->nextDomID = 1;

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

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

564
    domobj->persistent = 1;
565
    virDomainObjUnlock(domobj);
566

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

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

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

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

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

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

612
    testDriverUnlock(privconn);
613

614 615 616
    return VIR_DRV_OPEN_SUCCESS;

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


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

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

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

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

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

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

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

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

        def->key = strdup(def->target.path);
        if (def->key == NULL) {
718
            virReportOOMError();
719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736
            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;
}

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

763
    testDriverLock(privconn);
764
    conn->privateData = privconn;
765

766 767 768
    if (virDomainObjListInit(&privconn->domains) < 0)
        goto error;

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

    if ((fd = open(file, O_RDONLY)) < 0) {
773
        virReportSystemError(errno,
774 775
                             _("loading host definition file '%s'"),
                             file);
776
        goto error;
777 778
    }

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

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

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

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

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

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

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

837
    ret = virXPathLong("string(/node/cpu/threads[1])", ctxt, &l);
838 839 840
    if (ret == 0) {
        nodeInfo->threads = l;
    } else if (ret == -2) {
841
        testError(VIR_ERR_XML_ERROR, "%s", _("node cpu threads"));
842
        goto error;
843
    }
844

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

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

874
    ret = virXPathLong("string(/node/memory[1])", ctxt, &l);
875 876 877
    if (ret == 0) {
        nodeInfo->memory = l;
    } else if (ret == -2) {
878
        testError(VIR_ERR_XML_ERROR, "%s", _("node memory"));
879
        goto error;
880
    }
881

882
    ret = virXPathNodeSet("/node/domain", ctxt, &domains);
883
    if (ret < 0) {
884
        testError(VIR_ERR_XML_ERROR, "%s", _("node domain list"));
885
        goto error;
886
    }
887

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

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

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

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

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

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

L
Laine Stump 已提交
961
    /* Parse interface definitions */
962
    ret = virXPathNodeSet("/node/interface", ctxt, &ifaces);
L
Laine Stump 已提交
963
    if (ret < 0) {
964
        testError(VIR_ERR_XML_ERROR, "%s", _("node interface list"));
L
Laine Stump 已提交
965 966 967 968 969 970 971 972 973
        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) {
974
                testError(VIR_ERR_INTERNAL_ERROR, "%s", _("resolving interface filename"));
L
Laine Stump 已提交
975 976 977
                goto error;
            }

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

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

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

C
Cole Robinson 已提交
997
    /* Parse Storage Pool list */
998
    ret = virXPathNodeSet("/node/pool", ctxt, &pools);
C
Cole Robinson 已提交
999
    if (ret < 0) {
1000
        testError(VIR_ERR_XML_ERROR, "%s", _("node pool list"));
C
Cole Robinson 已提交
1001 1002 1003 1004 1005 1006 1007 1008 1009 1010
        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) {
1011
                testError(VIR_ERR_INTERNAL_ERROR, "%s",
C
Cole Robinson 已提交
1012 1013 1014 1015
                          _("resolving pool filename"));
                goto error;
            }

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

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

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

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

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

1049
    ret = virXPathNodeSet("/node/device", ctxt, &devs);
1050
    if (ret < 0) {
1051
        testError(VIR_ERR_XML_ERROR, "%s", _("node device list"));
1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063
        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) {
1064
                testError(VIR_ERR_INTERNAL_ERROR, "%s",
1065 1066 1067 1068
                          _("resolving device filename"));
                goto error;
            }

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


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

1090
    return (0);
1091 1092

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

1111

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

1118
    if (!conn->uri)
1119
        return VIR_DRV_OPEN_DECLINED;
1120

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

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

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

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

1143 1144
    if (ret == VIR_DRV_OPEN_SUCCESS) {
        testConnPtr privconn = conn->privateData;
1145
        testDriverLock(privconn);
1146 1147 1148
        /* Init callback list */
        if (VIR_ALLOC(privconn->domainEventCallbacks) < 0 ||
            !(privconn->domainEventQueue = virDomainEventQueueNew())) {
1149
            virReportOOMError();
1150
            testDriverUnlock(privconn);
1151 1152 1153 1154 1155 1156 1157 1158
            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.");
1159
        testDriverUnlock(privconn);
1160 1161
    }

1162
    return (ret);
1163 1164
}

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

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

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

1182
    testDriverUnlock(privconn);
1183
    virMutexDestroy(&privconn->lock);
1184

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

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

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

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

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

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

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

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

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

1243
    return count;
1244 1245
}

1246 1247 1248 1249 1250 1251 1252 1253 1254 1255
static int testDomainIsActive(virDomainPtr dom)
{
    testConnPtr privconn = dom->conn->privateData;
    virDomainObjPtr obj;
    int ret = -1;

    testDriverLock(privconn);
    obj = virDomainFindByUUID(&privconn->domains, dom->uuid);
    testDriverUnlock(privconn);
    if (!obj) {
1256
        testError(VIR_ERR_NO_DOMAIN, NULL);
1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276
        goto cleanup;
    }
    ret = virDomainObjIsActive(obj);

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

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

    testDriverLock(privconn);
    obj = virDomainFindByUUID(&privconn->domains, dom->uuid);
    testDriverUnlock(privconn);
    if (!obj) {
1277
        testError(VIR_ERR_NO_DOMAIN, NULL);
1278 1279 1280 1281 1282 1283 1284 1285 1286 1287
        goto cleanup;
    }
    ret = obj->persistent;

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

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

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

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

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

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

1316 1317 1318 1319
    event = virDomainEventNewFromObj(dom,
                                     VIR_DOMAIN_EVENT_STARTED,
                                     VIR_DOMAIN_EVENT_STARTED_BOOTED);

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

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


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

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

    if (dom == NULL) {
1347
        testError(VIR_ERR_NO_DOMAIN, NULL);
1348
        goto cleanup;
1349 1350
    }

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

cleanup:
1356 1357
    if (dom)
        virDomainObjUnlock(dom);
1358
    return ret;
1359 1360
}

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

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

    if (dom == NULL) {
1373
        testError(VIR_ERR_NO_DOMAIN, NULL);
1374
        goto cleanup;
1375
    }
1376

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

cleanup:
1382 1383
    if (dom)
        virDomainObjUnlock(dom);
1384
    return ret;
1385 1386
}

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

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

    if (dom == NULL) {
1399
        testError(VIR_ERR_NO_DOMAIN, NULL);
1400
        goto cleanup;
1401
    }
1402

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

cleanup:
1408 1409
    if (dom)
        virDomainObjUnlock(dom);
1410
    return ret;
1411 1412
}

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

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

1424
    return n;
1425 1426
}

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

1434
    testDriverLock(privconn);
1435 1436 1437 1438
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);

    if (privdom == NULL) {
1439
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
1440
        goto cleanup;
1441
    }
1442

1443
    testDomainShutdownState(domain, privdom);
1444 1445 1446
    event = virDomainEventNewFromObj(privdom,
                                     VIR_DOMAIN_EVENT_STOPPED,
                                     VIR_DOMAIN_EVENT_STOPPED_DESTROYED);
1447

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

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

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

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

    if (privdom == NULL) {
1477
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
1478
        goto cleanup;
1479
    }
1480

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    if (privdom == NULL) {
1667
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
1668
        goto cleanup;
1669
    }
1670 1671

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

1677 1678 1679 1680 1681
    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));
1682 1683 1684
    ret = 0;

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

1690 1691 1692 1693 1694
#define TEST_SAVE_MAGIC "TestGuestMagic"

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

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

    if (privdom == NULL) {
1708
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
1709
        goto cleanup;
1710
    }
1711

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    if (privdom == NULL) {
1891
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
1892
        goto cleanup;
1893
    }
1894 1895

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

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

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

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

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

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

    if (privdom == NULL) {
1956
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
1957
        goto cleanup;
1958
    }
1959

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

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

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

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

    if (privdom == NULL) {
1981
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
1982
        goto cleanup;
1983
    }
1984 1985

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

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

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

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

    if (privdom == NULL) {
2008
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2009
        goto cleanup;
2010
    }
2011

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

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

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

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

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

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

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

    if (privdom == NULL) {
2048
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2049
        goto cleanup;
2050
    }
2051

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

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

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

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

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

C
Cole Robinson 已提交
2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097
static int testDomainGetVcpus(virDomainPtr domain,
                              virVcpuInfoPtr info,
                              int maxinfo,
                              unsigned char *cpumaps,
                              int maplen)
{
    testConnPtr privconn = domain->conn->privateData;
    testDomainObjPrivatePtr privdomdata;
    virDomainObjPtr privdom;
    int i, v, maxcpu, hostcpus;
    int ret = -1;
    struct timeval tv;
    unsigned long long statbase;

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

    if (privdom == NULL) {
2098
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
C
Cole Robinson 已提交
2099 2100 2101 2102
        goto cleanup;
    }

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

    privdomdata = privdom->privateData;

    if (gettimeofday(&tv, NULL) < 0) {
2111
        virReportSystemError(errno,
C
Cole Robinson 已提交
2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166
                             "%s", _("getting time of day"));
        goto cleanup;
    }

    statbase = (tv.tv_sec * 1000UL * 1000UL) + tv.tv_usec;


    hostcpus = VIR_NODEINFO_MAXCPUS(privconn->nodeInfo);
    maxcpu = maplen * 8;
    if (maxcpu > hostcpus)
        maxcpu = hostcpus;

    /* Clamp to actual number of vcpus */
    if (maxinfo > privdom->def->vcpus)
        maxinfo = privdom->def->vcpus;

    /* Populate virVcpuInfo structures */
    if (info != NULL) {
        memset(info, 0, sizeof(*info) * maxinfo);

        for (i = 0 ; i < maxinfo ; i++) {
            virVcpuInfo privinfo = privdomdata->vcpu_infos[i];

            info[i].number = privinfo.number;
            info[i].state = privinfo.state;
            info[i].cpu = privinfo.cpu;

            /* Fake an increasing cpu time value */
            info[i].cpuTime = statbase / 10;
        }
    }

    /* Populate cpumaps */
    if (cpumaps != NULL) {
        int privmaplen = VIR_CPU_MAPLEN(hostcpus);
        memset(cpumaps, 0, maplen * maxinfo);

        for (v = 0 ; v < maxinfo ; v++) {
            unsigned char *cpumap = VIR_GET_CPUMAP(cpumaps, maplen, v);

            for (i = 0 ; i < maxcpu ; i++) {
                if (VIR_CPU_USABLE(privdomdata->cpumaps, privmaplen, v, i)) {
                    VIR_USE_CPU(cpumap, i);
                }
            }
        }
    }

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

C
Cole Robinson 已提交
2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183
static int testDomainPinVcpu(virDomainPtr domain,
                             unsigned int vcpu,
                             unsigned char *cpumap,
                             int maplen)
{
    testConnPtr privconn = domain->conn->privateData;
    testDomainObjPrivatePtr privdomdata;
    virDomainObjPtr privdom;
    unsigned char *privcpumap;
    int i, maxcpu, hostcpus, privmaplen;
    int ret = -1;

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

    if (privdom == NULL) {
2184
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
C
Cole Robinson 已提交
2185 2186 2187 2188
        goto cleanup;
    }

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

    if (vcpu > privdom->def->vcpus) {
2195
        testError(VIR_ERR_INVALID_ARG, "%s",
C
Cole Robinson 已提交
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
                  _("requested vcpu is higher than allocated vcpus"));
        goto cleanup;
    }

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

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

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

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

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

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

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

    if (privdom == NULL) {
2237
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2238
        goto cleanup;
2239
    }
2240

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

2244
    ret = virDomainDefFormat(def,
2245 2246 2247
                             flags);

cleanup:
2248 2249
    if (privdom)
        virDomainObjUnlock(privdom);
2250
    return ret;
2251
}
2252

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

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

2261
    return count;
2262 2263
}

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

2268
    testConnPtr privconn = conn->privateData;
2269
    int n;
2270

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

2276
    return n;
2277 2278
}

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

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

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

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

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

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

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

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

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

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

2345
cleanup:
2346
    testDriverUnlock(privconn);
2347
    return ret;
2348 2349 2350
}


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

2357
    testDriverLock(privconn);
2358 2359 2360 2361
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);

    if (privdom == NULL) {
2362
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2363
        goto cleanup;
2364
    }
2365

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

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

2376 2377 2378
    event = virDomainEventNewFromObj(privdom,
                                     VIR_DOMAIN_EVENT_STARTED,
                                     VIR_DOMAIN_EVENT_STARTED_BOOTED);
2379
    ret = 0;
2380

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

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

2396
    testDriverLock(privconn);
2397 2398 2399 2400
    privdom = virDomainFindByName(&privconn->domains,
                                  domain->name);

    if (privdom == NULL) {
2401
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2402
        goto cleanup;
2403
    }
2404

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

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

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

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

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

    if (privdom == NULL) {
2442
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2443
        goto cleanup;
2444 2445
    }

2446
    *autostart = privdom->autostart;
2447 2448 2449
    ret = 0;

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


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

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

    if (privdom == NULL) {
2469
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2470
        goto cleanup;
2471 2472
    }

2473
    privdom->autostart = autostart ? 1 : 0;
2474 2475 2476
    ret = 0;

cleanup:
2477 2478
    if (privdom)
        virDomainObjUnlock(privdom);
2479
    return ret;
2480
}
2481

2482
static char *testDomainGetSchedulerType(virDomainPtr domain ATTRIBUTE_UNUSED,
2483 2484
                                        int *nparams)
{
2485 2486
    char *type = NULL;

2487 2488
    *nparams = 1;
    type = strdup("fair");
2489
    if (!type)
2490
        virReportOOMError();
2491

2492 2493 2494 2495 2496 2497 2498
    return type;
}

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

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

    if (privdom == NULL) {
2509
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2510
        goto cleanup;
2511 2512
    }

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

cleanup:
2525 2526
    if (privdom)
        virDomainObjUnlock(privdom);
2527
    return ret;
2528
}
2529 2530


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

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

    if (privdom == NULL) {
2545
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2546
        goto cleanup;
2547 2548
    }

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

cleanup:
2566 2567
    if (privdom)
        virDomainObjUnlock(privdom);
2568
    return ret;
2569 2570
}

2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586
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) {
2587
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598
        goto error;
    }

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

    if (!found) {
2599
        testError(VIR_ERR_INVALID_ARG,
2600 2601 2602 2603 2604
                  _("invalid path: %s"), path);
        goto error;
    }

    if (gettimeofday(&tv, NULL) < 0) {
2605
        virReportSystemError(errno,
2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640
                             "%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) {
2641
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653
        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) {
2654
        testError(VIR_ERR_INVALID_ARG,
2655 2656 2657 2658 2659
                  _("invalid path, '%s' is not a known interface"), path);
        goto error;
    }

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

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

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

    if (net == NULL) {
2711
        testError(VIR_ERR_NO_NETWORK, NULL);
2712
        goto cleanup;
2713 2714
    }

2715 2716 2717
    ret = virGetNetwork(conn, net->def->name, net->def->uuid);

cleanup:
2718 2719
    if (net)
        virNetworkObjUnlock(net);
2720
    return ret;
2721
}
2722

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

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

    if (net == NULL) {
2735
        testError(VIR_ERR_NO_NETWORK, NULL);
2736
        goto cleanup;
2737 2738
    }

2739 2740 2741
    ret = virGetNetwork(conn, net->def->name, net->def->uuid);

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


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

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

2761
    return numActive;
2762 2763 2764
}

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

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

2781 2782 2783
    return n;

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

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

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

2804
    return numInactive;
2805 2806 2807
}

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

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

2824 2825 2826
    return n;

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

2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844

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

    testDriverLock(privconn);
    obj = virNetworkFindByUUID(&privconn->networks, net->uuid);
    testDriverUnlock(privconn);
    if (!obj) {
2845
        testError(VIR_ERR_NO_NETWORK, NULL);
2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865
        goto cleanup;
    }
    ret = virNetworkObjIsActive(obj);

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

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

    testDriverLock(privconn);
    obj = virNetworkFindByUUID(&privconn->networks, net->uuid);
    testDriverUnlock(privconn);
    if (!obj) {
2866
        testError(VIR_ERR_NO_NETWORK, NULL);
2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877
        goto cleanup;
    }
    ret = obj->persistent;

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


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

2884
    testDriverLock(privconn);
2885
    if ((def = virNetworkDefParseString(xml)) == NULL)
2886
        goto cleanup;
2887

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

2893
    ret = virGetNetwork(conn, net->def->name, net->def->uuid);
2894

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

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

2909
    testDriverLock(privconn);
2910
    if ((def = virNetworkDefParseString(xml)) == NULL)
2911
        goto cleanup;
2912

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

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

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

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

2933
    testDriverLock(privconn);
2934 2935 2936 2937
    privnet = virNetworkFindByName(&privconn->networks,
                                   network->name);

    if (privnet == NULL) {
2938
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2939
        goto cleanup;
2940
    }
2941

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

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

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

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

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

    if (privnet == NULL) {
2971
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2972
        goto cleanup;
2973
    }
2974

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

2981
    privnet->active = 1;
2982
    ret = 0;
2983

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

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

2995
    testDriverLock(privconn);
2996 2997 2998 2999
    privnet = virNetworkFindByName(&privconn->networks,
                                   network->name);

    if (privnet == NULL) {
3000
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3001
        goto cleanup;
3002
    }
3003

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

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

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

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

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

3034
    ret = virNetworkDefFormat(privnet->def);
3035 3036

cleanup:
3037 3038
    if (privnet)
        virNetworkObjUnlock(privnet);
3039
    return ret;
3040 3041 3042
}

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

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

    if (privnet == NULL) {
3053
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3054
        goto cleanup;
3055 3056
    }

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

    if (!(bridge = strdup(privnet->def->bridge))) {
3065
        virReportOOMError();
3066
        goto cleanup;
3067
    }
3068 3069

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

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

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

    if (privnet == NULL) {
3087
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3088
        goto cleanup;
3089 3090
    }

3091
    *autostart = privnet->autostart;
3092 3093 3094
    ret = 0;

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

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

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

    if (privnet == NULL) {
3112
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3113
        goto cleanup;
3114 3115
    }

3116
    privnet->autostart = autostart ? 1 : 0;
3117 3118 3119
    ret = 0;

cleanup:
3120 3121
    if (privnet)
        virNetworkObjUnlock(privnet);
3122
    return ret;
3123
}
3124

C
Cole Robinson 已提交
3125

L
Laine Stump 已提交
3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155
/*
 * Physical host interface routines
 */

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

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

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


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

    testDriverLock(privconn);
    for (i = 0 ; (i < privconn->ifaces.count); i++) {
        virInterfaceObjLock(privconn->ifaces.objs[i]);
D
Daniel P. Berrange 已提交
3156
        if (virInterfaceObjIsActive(privconn->ifaces.objs[i])) {
L
Laine Stump 已提交
3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173
            count++;
        }
        virInterfaceObjUnlock(privconn->ifaces.objs[i]);
    }
    testDriverUnlock(privconn);
    return count;
}

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

    testDriverLock(privconn);
    memset(names, 0, sizeof(*names)*nnames);
    for (i = 0 ; (i < privconn->ifaces.count) && (n < nnames); i++) {
        virInterfaceObjLock(privconn->ifaces.objs[i]);
D
Daniel P. Berrange 已提交
3174
        if (virInterfaceObjIsActive(privconn->ifaces.objs[i])) {
L
Laine Stump 已提交
3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186
            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:
3187
    virReportOOMError();
L
Laine Stump 已提交
3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201
    for (n = 0 ; n < nnames ; n++)
        VIR_FREE(names[n]);
    testDriverUnlock(privconn);
    return -1;
}

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

    testDriverLock(privconn);
    for (i = 0 ; i < privconn->ifaces.count; i++) {
        virInterfaceObjLock(privconn->ifaces.objs[i]);
D
Daniel P. Berrange 已提交
3202
        if (!virInterfaceObjIsActive(privconn->ifaces.objs[i])) {
L
Laine Stump 已提交
3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219
            count++;
        }
        virInterfaceObjUnlock(privconn->ifaces.objs[i]);
    }
    testDriverUnlock(privconn);
    return count;
}

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

    testDriverLock(privconn);
    memset(names, 0, sizeof(*names)*nnames);
    for (i = 0 ; (i < privconn->ifaces.count) && (n < nnames); i++) {
        virInterfaceObjLock(privconn->ifaces.objs[i]);
D
Daniel P. Berrange 已提交
3220
        if (!virInterfaceObjIsActive(privconn->ifaces.objs[i])) {
L
Laine Stump 已提交
3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232
            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:
3233
    virReportOOMError();
L
Laine Stump 已提交
3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251
    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) {
3252
        testError(VIR_ERR_NO_INTERFACE, NULL);
L
Laine Stump 已提交
3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276
        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) {
3277
        testError(VIR_ERR_NO_INTERFACE, NULL);
L
Laine Stump 已提交
3278 3279 3280 3281
        goto cleanup;
    }

    if (ifacect > 1) {
3282
        testError(VIR_ERR_MULTIPLE_INTERFACES, NULL);
L
Laine Stump 已提交
3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293
        goto cleanup;
    }

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

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

3294 3295 3296 3297 3298 3299 3300 3301 3302 3303
static int testInterfaceIsActive(virInterfacePtr iface)
{
    testConnPtr privconn = iface->conn->privateData;
    virInterfaceObjPtr obj;
    int ret = -1;

    testDriverLock(privconn);
    obj = virInterfaceFindByName(&privconn->ifaces, iface->name);
    testDriverUnlock(privconn);
    if (!obj) {
3304
        testError(VIR_ERR_NO_INTERFACE, NULL);
3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315
        goto cleanup;
    }
    ret = virInterfaceObjIsActive(obj);

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


L
Laine Stump 已提交
3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328
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) {
3329
        testError(VIR_ERR_NO_INTERFACE, __FUNCTION__);
L
Laine Stump 已提交
3330 3331 3332
        goto cleanup;
    }

3333
    ret = virInterfaceDefFormat(privinterface->def);
L
Laine Stump 已提交
3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350

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

3354
    if ((iface = virInterfaceAssignDef(&privconn->ifaces, def)) == NULL)
L
Laine Stump 已提交
3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378
        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) {
3379
        testError(VIR_ERR_NO_INTERFACE, NULL);
L
Laine Stump 已提交
3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403
        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) {
3404
        testError(VIR_ERR_NO_INTERFACE, NULL);
L
Laine Stump 已提交
3405 3406 3407 3408
        goto cleanup;
    }

    if (privinterface->active != 0) {
3409
        testError(VIR_ERR_OPERATION_INVALID, NULL);
L
Laine Stump 已提交
3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434
        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) {
3435
        testError(VIR_ERR_NO_INTERFACE, NULL);
L
Laine Stump 已提交
3436 3437 3438 3439
        goto cleanup;
    }

    if (privinterface->active == 0) {
3440
        testError(VIR_ERR_OPERATION_INVALID, NULL);
L
Laine Stump 已提交
3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455
        goto cleanup;
    }

    privinterface->active = 0;
    ret = 0;

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



C
Cole Robinson 已提交
3456 3457 3458 3459
/*
 * Storage Driver routines
 */

3460

3461
static int testStoragePoolObjSetDefaults(virStoragePoolObjPtr pool) {
C
Cole Robinson 已提交
3462 3463 3464 3465 3466 3467 3468

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

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

    return 0;
}

3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490
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;
}

3491

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

3499
    testDriverLock(privconn);
3500
    pool = virStoragePoolObjFindByUUID(&privconn->pools, uuid);
3501
    testDriverUnlock(privconn);
3502 3503

    if (pool == NULL) {
3504
        testError(VIR_ERR_NO_STORAGE_POOL, NULL);
3505
        goto cleanup;
C
Cole Robinson 已提交
3506 3507
    }

3508 3509 3510
    ret = virGetStoragePool(conn, pool->def->name, pool->def->uuid);

cleanup:
3511 3512
    if (pool)
        virStoragePoolObjUnlock(pool);
3513
    return ret;
C
Cole Robinson 已提交
3514 3515 3516 3517 3518
}

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

3523
    testDriverLock(privconn);
3524
    pool = virStoragePoolObjFindByName(&privconn->pools, name);
3525
    testDriverUnlock(privconn);
3526 3527

    if (pool == NULL) {
3528
        testError(VIR_ERR_NO_STORAGE_POOL, NULL);
3529
        goto cleanup;
C
Cole Robinson 已提交
3530 3531
    }

3532 3533 3534
    ret = virGetStoragePool(conn, pool->def->name, pool->def->uuid);

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

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

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

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

    return numActive;
}

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

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

    return n;

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

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

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

    return numInactive;
}

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

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

    return n;

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


3637 3638 3639 3640 3641 3642 3643 3644 3645 3646
static int testStoragePoolIsActive(virStoragePoolPtr pool)
{
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr obj;
    int ret = -1;

    testDriverLock(privconn);
    obj = virStoragePoolObjFindByUUID(&privconn->pools, pool->uuid);
    testDriverUnlock(privconn);
    if (!obj) {
3647
        testError(VIR_ERR_NO_STORAGE_POOL, NULL);
3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667
        goto cleanup;
    }
    ret = virStoragePoolObjIsActive(obj);

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

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

    testDriverLock(privconn);
    obj = virStoragePoolObjFindByUUID(&privconn->pools, pool->uuid);
    testDriverUnlock(privconn);
    if (!obj) {
3668
        testError(VIR_ERR_NO_STORAGE_POOL, NULL);
3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680
        goto cleanup;
    }
    ret = obj->configFile ? 1 : 0;

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



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

3688
    testDriverLock(privconn);
3689 3690
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
3691
    testDriverUnlock(privconn);
3692 3693

    if (privpool == NULL) {
3694
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3695
        goto cleanup;
3696 3697
    }

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

    privpool->active = 1;
3705
    ret = 0;
C
Cole Robinson 已提交
3706

3707
cleanup:
3708 3709
    if (privpool)
        virStoragePoolObjUnlock(privpool);
3710
    return ret;
C
Cole Robinson 已提交
3711 3712 3713
}

static char *
3714
testStorageFindPoolSources(virConnectPtr conn ATTRIBUTE_UNUSED,
3715 3716
                           const char *type,
                           const char *srcSpec,
C
Cole Robinson 已提交
3717 3718
                           unsigned int flags ATTRIBUTE_UNUSED)
{
3719 3720 3721 3722 3723 3724
    virStoragePoolSourcePtr source = NULL;
    int pool_type;
    char *ret = NULL;

    pool_type = virStoragePoolTypeFromString(type);
    if (!pool_type) {
3725
        testError(VIR_ERR_INTERNAL_ERROR,
3726 3727 3728 3729 3730
                  _("unknown storage pool type %s"), type);
        goto cleanup;
    }

    if (srcSpec) {
3731
        source = virStoragePoolDefParseSourceString(srcSpec, pool_type);
3732 3733 3734 3735 3736 3737 3738 3739 3740
        if (!source)
            goto cleanup;
    }

    switch (pool_type) {

    case VIR_STORAGE_POOL_LOGICAL:
        ret = strdup(defaultPoolSourcesLogicalXML);
        if (!ret)
3741
            virReportOOMError();
3742 3743 3744 3745
        break;

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

        if (virAsprintf(&ret, defaultPoolSourcesNetFSXML,
                        source->host.name) < 0)
3753
            virReportOOMError();
3754 3755 3756
        break;

    default:
3757
        testError(VIR_ERR_NO_SUPPORT,
3758 3759 3760 3761 3762
                  _("pool type '%s' does not support source discovery"), type);
    }

cleanup:
    virStoragePoolSourceFree(source);
3763
    VIR_FREE(source);
3764
    return ret;
C
Cole Robinson 已提交
3765 3766 3767 3768 3769 3770 3771
}


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

3777
    testDriverLock(privconn);
3778
    if (!(def = virStoragePoolDefParseString(xml)))
3779
        goto cleanup;
C
Cole Robinson 已提交
3780

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

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

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

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

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

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

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

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

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

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

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

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

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

3854
    testDriverLock(privconn);
3855 3856 3857 3858
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);

    if (privpool == NULL) {
3859
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3860
        goto cleanup;
3861 3862
    }

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

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

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

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

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

    if (privpool == NULL) {
3892
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3893
        goto cleanup;
3894 3895
    }

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

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


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

3916
    testDriverLock(privconn);
3917 3918 3919 3920
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);

    if (privpool == NULL) {
3921
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3922
        goto cleanup;
3923 3924 3925
    }

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

    privpool->active = 0;

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

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


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

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

    if (privpool == NULL) {
3960
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3961 3962 3963 3964
        goto cleanup;
    }

    if (virStoragePoolObjIsActive(privpool)) {
3965
        testError(VIR_ERR_INTERNAL_ERROR,
3966 3967
                  _("storage pool '%s' is already active"), pool->name);
        goto cleanup;
3968 3969
    }

3970
    ret = 0;
C
Cole Robinson 已提交
3971

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


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

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

    if (privpool == NULL) {
3992
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3993
        goto cleanup;
3994 3995 3996
    }

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

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


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

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

    if (privpool == NULL) {
4023
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4024
        goto cleanup;
4025
    }
C
Cole Robinson 已提交
4026 4027 4028 4029 4030 4031 4032 4033 4034

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

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

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

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

4055
    if (privpool == NULL) {
4056
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4057
        goto cleanup;
4058 4059
    }

4060
    ret = virStoragePoolDefFormat(privpool->def);
4061 4062

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

static int
4069
testStoragePoolGetAutostart(virStoragePoolPtr pool,
C
Cole Robinson 已提交
4070
                            int *autostart) {
4071 4072
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4073
    int ret = -1;
4074

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

    if (privpool == NULL) {
4081
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4082
        goto cleanup;
4083
    }
C
Cole Robinson 已提交
4084 4085 4086 4087 4088 4089

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

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

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

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

    if (privpool == NULL) {
4111
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4112
        goto cleanup;
4113
    }
C
Cole Robinson 已提交
4114 4115

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

    autostart = (autostart != 0);
    privpool->autostart = autostart;
4123 4124 4125
    ret = 0;

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


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

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

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

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

4154 4155 4156
    ret = privpool->volumes.count;

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

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

4170
    memset(names, 0, maxnames * sizeof(*names));
4171 4172

    testDriverLock(privconn);
4173 4174
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4175
    testDriverUnlock(privconn);
4176 4177

    if (privpool == NULL) {
4178
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4179
        goto cleanup;
4180 4181 4182 4183
    }


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

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

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

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

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


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

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

4223
    if (privpool == NULL) {
4224
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4225
        goto cleanup;
4226 4227 4228 4229
    }


    if (!virStoragePoolObjIsActive(privpool)) {
4230
        testError(VIR_ERR_INTERNAL_ERROR,
4231
                  _("storage pool '%s' is not active"), pool->name);
4232
        goto cleanup;
4233 4234 4235 4236 4237
    }

    privvol = virStorageVolDefFindByName(privpool, name);

    if (!privvol) {
4238
        testError(VIR_ERR_INVALID_STORAGE_VOL,
C
Cole Robinson 已提交
4239
                  _("no storage vol with matching name '%s'"), name);
4240
        goto cleanup;
C
Cole Robinson 已提交
4241 4242
    }

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

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


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

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

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

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

    return ret;
C
Cole Robinson 已提交
4285 4286 4287 4288 4289
}

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

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

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

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

    return ret;
C
Cole Robinson 已提交
4319 4320 4321
}

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

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

4335
    if (privpool == NULL) {
4336
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4337
        goto cleanup;
4338 4339 4340
    }

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

4346
    privvol = virStorageVolDefParseString(privpool->def, xmldesc);
4347
    if (privvol == NULL)
4348
        goto cleanup;
4349 4350

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

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

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

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

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

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

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

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

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

4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416
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) {
4417
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4418 4419 4420 4421
        goto cleanup;
    }

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

4427
    privvol = virStorageVolDefParseString(privpool->def, xmldesc);
4428 4429 4430 4431
    if (privvol == NULL)
        goto cleanup;

    if (virStorageVolDefFindByName(privpool, privvol->name)) {
4432
        testError(VIR_ERR_INVALID_STORAGE_VOL,
4433 4434 4435 4436 4437 4438
                  "%s", _("storage vol already exists"));
        goto cleanup;
    }

    origvol = virStorageVolDefFindByName(privpool, clonevol->name);
    if (!origvol) {
4439
        testError(VIR_ERR_INVALID_STORAGE_VOL,
4440 4441 4442 4443 4444 4445 4446 4447
                  _("no storage vol with matching name '%s'"),
                  clonevol->name);
        goto cleanup;
    }

    /* Make sure enough space */
    if ((privpool->def->allocation + privvol->allocation) >
         privpool->def->capacity) {
4448
        testError(VIR_ERR_INTERNAL_ERROR,
4449 4450 4451 4452 4453 4454 4455 4456 4457
                  _("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) {
4458
        virReportOOMError();
4459 4460 4461
        goto cleanup;
    }

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

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

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

    if (privpool == NULL) {
4507
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4508
        goto cleanup;
4509 4510 4511 4512 4513 4514
    }


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

    if (privvol == NULL) {
4515
        testError(VIR_ERR_INVALID_STORAGE_VOL,
4516 4517
                  _("no storage vol with matching name '%s'"),
                  vol->name);
4518
        goto cleanup;
4519 4520 4521
    }

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


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

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


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

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

    if (privpool == NULL) {
4586
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4587
        goto cleanup;
4588 4589 4590 4591 4592
    }

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

    if (privvol == NULL) {
4593
        testError(VIR_ERR_INVALID_STORAGE_VOL,
4594 4595
                  _("no storage vol with matching name '%s'"),
                  vol->name);
4596
        goto cleanup;
4597 4598 4599
    }

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

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

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

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

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

    if (privpool == NULL) {
4631
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4632
        goto cleanup;
4633 4634 4635 4636 4637
    }

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

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

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

4650
    ret = virStorageVolDefFormat(privpool->def, privvol);
4651 4652

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

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

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

    if (privpool == NULL) {
4671
        testError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4672
        goto cleanup;
4673 4674 4675 4676 4677
    }

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

    if (privvol == NULL) {
4678
        testError(VIR_ERR_INVALID_STORAGE_VOL,
4679 4680
                  _("no storage vol with matching name '%s'"),
                  vol->name);
4681
        goto cleanup;
4682 4683 4684
    }

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

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

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

4700

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

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

4814
    ret = virNodeDeviceDefFormat(obj->def);
4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833

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) {
4834
        virNodeDeviceReportError(VIR_ERR_NO_NODE_DEVICE,
4835 4836 4837 4838 4839 4840 4841 4842
                                _("no node device with matching name '%s'"),
                                 dev->name);
        goto cleanup;
    }

    if (obj->def->parent) {
        ret = strdup(obj->def->parent);
        if (!ret)
4843
            virReportOOMError();
4844
    } else {
4845
        virNodeDeviceReportError(VIR_ERR_INTERNAL_ERROR,
4846 4847 4848 4849 4850 4851 4852 4853 4854
                                 "%s", _("no parent for this device"));
    }

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

4855

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

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

    testDriverLock(driver);

4940
    def = virNodeDeviceDefParseString(xmlDesc, CREATE_DEVICE);
4941 4942 4943 4944 4945
    if (def == NULL) {
        goto cleanup;
    }

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

4950
    if (virNodeDeviceGetParentHost(&driver->devs,
4951 4952 4953 4954 4955 4956 4957 4958 4959 4960
                                   def->name,
                                   def->parent,
                                   &parent_host) == -1) {
        goto cleanup;
    }

    /* 'name' is supposed to be filled in by the node device backend, which
     * we don't have. Use WWPN instead. */
    VIR_FREE(def->name);
    if (!(def->name = strdup(wwpn))) {
4961
        virReportOOMError();
4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976
        goto cleanup;
    }

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

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


4977
    if (!(obj = virNodeDeviceAssignDef(&driver->devs, def))) {
4978 4979 4980 4981 4982 4983 4984 4985
        goto cleanup;
    }
    virNodeDeviceObjUnlock(obj);

    dev = virGetNodeDevice(conn, def->name);
    def = NULL;
cleanup:
    testDriverUnlock(driver);
4986
    virNodeDeviceDefFree(def);
4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005
    VIR_FREE(wwnn);
    VIR_FREE(wwpn);
    return dev;
}

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

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

    if (!obj) {
5006
        virNodeDeviceReportError(VIR_ERR_NO_NODE_DEVICE, NULL);
5007 5008 5009
        goto out;
    }

5010
    if (virNodeDeviceGetWWNs(obj->def, &wwnn, &wwpn) == -1) {
5011 5012 5013 5014 5015
        goto out;
    }

    parent_name = strdup(obj->def->parent);
    if (parent_name == NULL) {
5016
        virReportOOMError();
5017 5018 5019 5020 5021 5022 5023 5024 5025 5026
        goto out;
    }

    /* virNodeDeviceGetParentHost will cause the device object's lock to be
     * taken, so we have to dup the parent's name and drop the lock
     * before calling it.  We don't need the reference to the object
     * any more once we have the parent's name.  */
    virNodeDeviceObjUnlock(obj);

    /* We do this just for basic validation */
5027
    if (virNodeDeviceGetParentHost(&driver->devs,
5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046
                                   dev->name,
                                   parent_name,
                                   &parent_host) == -1) {
        obj = NULL;
        goto out;
    }

    virNodeDeviceObjLock(obj);
    virNodeDeviceObjRemove(&driver->devs, obj);

out:
    if (obj)
        virNodeDeviceObjUnlock(obj);
    VIR_FREE(parent_name);
    VIR_FREE(wwnn);
    VIR_FREE(wwpn);
    return ret;
}

5047 5048

/* Domain event implementations */
5049
static int
5050 5051 5052 5053
testDomainEventRegister(virConnectPtr conn,
                        virConnectDomainEventCallback callback,
                        void *opaque,
                        virFreeCallback freecb)
5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065
{
    testConnPtr driver = conn->privateData;
    int ret;

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

    return ret;
}

5066

5067
static int
5068 5069
testDomainEventDeregister(virConnectPtr conn,
                          virConnectDomainEventCallback callback)
5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085
{
    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;
}

5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126

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

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

    return ret;
}

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

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

    return ret;
}


5127 5128
static void testDomainEventDispatchFunc(virConnectPtr conn,
                                        virDomainEventPtr event,
5129
                                        virConnectDomainEventGenericCallback cb,
5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186
                                        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);
}

5187 5188 5189 5190 5191 5192
static virDrvOpenStatus testSecretOpen(virConnectPtr conn,
                                       virConnectAuthPtr auth ATTRIBUTE_UNUSED,
                                       int flags ATTRIBUTE_UNUSED) {
    if (STRNEQ(conn->driver->name, "Test"))
        return VIR_DRV_OPEN_DECLINED;

5193
    conn->secretPrivateData = conn->privateData;
5194 5195 5196 5197 5198 5199 5200
    return VIR_DRV_OPEN_SUCCESS;
}

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

5202 5203 5204 5205 5206 5207 5208

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

5209
    conn->nwfilterPrivateData = conn->privateData;
5210 5211 5212 5213 5214 5215 5216 5217
    return VIR_DRV_OPEN_SUCCESS;
}

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

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

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 */
5337 5338
    testNetworkIsActive, /* networkIsActive */
    testNetworkIsPersistent, /* networkIsPersistent */
5339 5340
};

L
Laine Stump 已提交
5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355
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 */
5356
    testInterfaceIsActive,      /* interfaceIsActive */
L
Laine Stump 已提交
5357 5358 5359
};


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

    .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,
5392
    .volCreateXMLFrom = testStorageVolumeCreateXMLFrom,
C
Cole Robinson 已提交
5393 5394 5395 5396
    .volDelete = testStorageVolumeDelete,
    .volGetInfo = testStorageVolumeGetInfo,
    .volGetXMLDesc = testStorageVolumeGetXMLDesc,
    .volGetPath = testStorageVolumeGetPath,
5397 5398
    .poolIsActive = testStoragePoolIsActive,
    .poolIsPersistent = testStoragePoolIsPersistent,
5399 5400
};

5401 5402 5403 5404
static virDeviceMonitor testDevMonitor = {
    .name = "Test",
    .open = testDevMonOpen,
    .close = testDevMonClose,
5405 5406 5407 5408 5409 5410 5411 5412

    .numOfDevices = testNodeNumOfDevices,
    .listDevices = testNodeListDevices,
    .deviceLookupByName = testNodeDeviceLookupByName,
    .deviceDumpXML = testNodeDeviceDumpXML,
    .deviceGetParent = testNodeDeviceGetParent,
    .deviceNumOfCaps = testNodeDeviceNumOfCaps,
    .deviceListCaps = testNodeDeviceListCaps,
5413 5414
    .deviceCreateXML = testNodeDeviceCreateXML,
    .deviceDestroy = testNodeDeviceDestroy,
5415 5416
};

5417 5418 5419 5420 5421
static virSecretDriver testSecretDriver = {
    .name = "Test",
    .open = testSecretOpen,
    .close = testSecretClose,
};
5422 5423


5424 5425 5426 5427 5428 5429
static virNWFilterDriver testNWFilterDriver = {
    .name = "Test",
    .open = testNWFilterOpen,
    .close = testNWFilterClose,
};

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

5453 5454
    return 0;
}