test_driver.c 166.8 KB
Newer Older
1 2 3
/*
 * test.c: A "mock" hypervisor for use by application unit tests
 *
4
 * Copyright (C) 2006-2012 Red Hat, Inc.
5
 * Copyright (C) 2006 Daniel P. Berrange
6
 *
7 8 9 10 11 12 13 14 15 16 17
 * 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
18
 * License along with this library.  If not, see
O
Osier Yang 已提交
19
 * <http://www.gnu.org/licenses/>.
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 "virerror.h"
36
#include "datatypes.h"
37
#include "test_driver.h"
38
#include "virbuffer.h"
39
#include "viruuid.h"
40
#include "capabilities.h"
41
#include "configmake.h"
42
#include "viralloc.h"
43
#include "network_conf.h"
L
Laine Stump 已提交
44
#include "interface_conf.h"
45
#include "domain_conf.h"
46
#include "domain_event.h"
47
#include "fdstream.h"
C
Cole Robinson 已提交
48
#include "storage_conf.h"
49
#include "node_device_conf.h"
50
#include "virxml.h"
51
#include "virthread.h"
52
#include "virlog.h"
E
Eric Blake 已提交
53
#include "virfile.h"
54
#include "virtypedparam.h"
55
#include "virrandom.h"
56
#include "virstring.h"
57

58 59
#define VIR_FROM_THIS VIR_FROM_TEST

60 61 62 63 64 65 66 67 68
/* Driver specific info to carry with a domain */
struct _testDomainObjPrivate {
    virVcpuInfoPtr vcpu_infos;

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

69 70 71 72 73
#define MAX_CPUS 128

struct _testCell {
    unsigned long mem;
    int numCpus;
74
    virCapsHostNUMACellCPU cpus[MAX_CPUS];
75 76 77 78 79
};
typedef struct _testCell testCell;
typedef struct _testCell *testCellPtr;

#define MAX_CELLS 128
80

81
struct _testConn {
82
    virMutex lock;
83

E
Eric Blake 已提交
84
    char *path;
85
    int nextDomID;
86
    virCapsPtr caps;
87
    virDomainXMLOptionPtr xmlopt;
88
    virNodeInfo nodeInfo;
89
    virDomainObjListPtr domains;
90
    virNetworkObjList networks;
L
Laine Stump 已提交
91
    virInterfaceObjList ifaces;
92 93
    bool transaction_running;
    virInterfaceObjList backupIfaces;
C
Cole Robinson 已提交
94
    virStoragePoolObjList pools;
95
    virNodeDeviceObjList devs;
96 97
    int numCells;
    testCell cells[MAX_CELLS];
98

99
    virDomainEventStatePtr domainEventState;
100 101 102
};
typedef struct _testConn testConn;
typedef struct _testConn *testConnPtr;
103

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

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

119

120
static int testConnectClose(virConnectPtr conn);
121 122 123 124
static void testDomainEventQueue(testConnPtr driver,
                                 virDomainEventPtr event);


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

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

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


155
static virDomainXMLOptionPtr
156 157 158 159
testBuildXMLConfig(void)
{
    virDomainXMLPrivateDataCallbacks priv = { .alloc = testDomainObjPrivateAlloc,
                                              .free = testDomainObjPrivateFree };
160
    return virDomainXMLOptionNew(NULL, &priv, NULL);
161 162 163
}


164 165
static virCapsPtr
testBuildCapabilities(virConnectPtr conn) {
166
    testConnPtr privconn = conn->privateData;
167 168 169 170
    virCapsPtr caps;
    virCapsGuestPtr guest;
    const char *const guest_types[] = { "hvm", "xen" };
    int i;
171

172
    if ((caps = virCapabilitiesNew(VIR_ARCH_I686, 0, 0)) == NULL)
173
        goto no_memory;
174

175 176 177 178
    if (virCapabilitiesAddHostFeature(caps, "pae") < 0)
        goto no_memory;
    if (virCapabilitiesAddHostFeature(caps ,"nonpae") < 0)
        goto no_memory;
179

180
    for (i = 0; i < privconn->numCells; i++) {
181 182 183 184 185 186 187 188 189
        virCapsHostNUMACellCPUPtr cpu_cells;

        if (VIR_ALLOC_N(cpu_cells, privconn->cells[i].numCpus) < 0)
            goto no_memory;

        memcpy(cpu_cells, privconn->cells[i].cpus,
               sizeof(*cpu_cells) * privconn->cells[i].numCpus);


190
        if (virCapabilitiesAddHostNUMACell(caps, i, privconn->cells[i].numCpus,
191
                                           0, cpu_cells) < 0)
192
            goto no_memory;
193 194
    }

195
    for (i = 0; i < ARRAY_CARDINALITY(guest_types); i++) {
196 197
        if ((guest = virCapabilitiesAddGuest(caps,
                                             guest_types[i],
198
                                             VIR_ARCH_I686,
199 200 201 202 203
                                             TEST_EMULATOR,
                                             NULL,
                                             0,
                                             NULL)) == NULL)
            goto no_memory;
204

205 206 207 208 209 210 211
        if (virCapabilitiesAddGuestDomain(guest,
                                          "test",
                                          NULL,
                                          NULL,
                                          0,
                                          NULL) == NULL)
            goto no_memory;
212

213 214 215 216
        if (virCapabilitiesAddGuestFeature(guest, "pae", 1, 1) == NULL)
            goto no_memory;
        if (virCapabilitiesAddGuestFeature(guest ,"nonpae", 1, 1) == NULL)
            goto no_memory;
217 218
    }

219 220 221
    caps->host.nsecModels = 1;
    if (VIR_ALLOC_N(caps->host.secModels, caps->host.nsecModels) < 0)
        goto no_memory;
222 223
    if (VIR_STRDUP(caps->host.secModels[0].model, "testSecurity") < 0)
        goto error;
224

225 226
    if (VIR_STRDUP(caps->host.secModels[0].doi, "") < 0)
        goto error;
227

228
    return caps;
229

230
no_memory:
231
    virReportOOMError();
232
error:
233
    virObjectUnref(caps);
234
    return NULL;
235 236
}

237

238 239 240
static const char *defaultDomainXML =
"<domain type='test'>"
"  <name>test</name>"
241
"  <uuid>6695eb01-f6a4-8304-79aa-97f2502e193f</uuid>"
242 243 244 245 246 247 248
"  <memory>8388608</memory>"
"  <currentMemory>2097152</currentMemory>"
"  <vcpu>2</vcpu>"
"  <os>"
"    <type>hvm</type>"
"  </os>"
"</domain>";
249 250


251 252 253
static const char *defaultNetworkXML =
"<network>"
"  <name>default</name>"
254
"  <uuid>dd8fe884-6c02-601e-7551-cca97df1c5df</uuid>"
255 256 257 258 259 260 261 262
"  <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>";
263

L
Laine Stump 已提交
264 265 266 267 268 269 270 271 272 273 274
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 已提交
275 276 277
static const char *defaultPoolXML =
"<pool type='dir'>"
"  <name>default-pool</name>"
278
"  <uuid>dfe224cb-28fb-8dd0-c4b2-64eb3f0f4566</uuid>"
C
Cole Robinson 已提交
279 280 281 282 283
"  <target>"
"    <path>/default-pool</path>"
"  </target>"
"</pool>";

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

307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
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>";

325
static const unsigned long long defaultPoolCap = (100 * 1024 * 1024 * 1024ull);
C
Cole Robinson 已提交
326 327
static const unsigned long long defaultPoolAlloc = 0;

328
static int testStoragePoolObjSetDefaults(virStoragePoolObjPtr pool);
329
static int testNodeGetInfo(virConnectPtr conn, virNodeInfoPtr info);
330

331
static char *
332
testDomainGenerateIfname(virDomainDefPtr domdef) {
333 334 335 336 337 338 339 340
    int maxif = 1024;
    int ifctr, i;

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

        if (virAsprintf(&ifname, "testnet%d", ifctr) < 0) {
341
            virReportOOMError();
342 343 344 345
            return NULL;
        }

        /* Generate network interface names */
346
        for (i = 0; i < domdef->nnets; i++) {
347
            if (domdef->nets[i]->ifname &&
348
                STREQ(domdef->nets[i]->ifname, ifname)) {
349 350 351 352 353 354 355 356 357
                found = 1;
                break;
            }
        }

        if (!found)
            return ifname;
    }

358 359
    virReportError(VIR_ERR_INTERNAL_ERROR,
                   _("Exceeded max iface limit %d"), maxif);
360 361 362
    return NULL;
}

363
static int
364
testDomainGenerateIfnames(virDomainDefPtr domdef)
365 366 367 368 369 370 371 372
{
    int i = 0;

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

373
        ifname = testDomainGenerateIfname(domdef);
374
        if (!ifname)
375
            return -1;
376 377 378 379

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

380
    return 0;
381 382
}

383 384 385 386 387 388 389 390 391 392 393 394
/* 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;
H
Hu Tao 已提交
395
    bool cpu;
396 397 398 399 400 401 402 403 404 405 406

    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) {
H
Hu Tao 已提交
407 408 409
            if (virBitmapGetBit(dom->def->cpumask, j, &cpu) < 0)
                return -1;
            if (cpu) {
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
                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) {
450
        virReportOOMError();
451 452 453 454
        goto cleanup;
    }

    if (VIR_REALLOC_N(privdata->cpumaps, nvcpus * cpumaplen) < 0) {
455
        virReportOOMError();
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471
        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;
    }

472
    dom->def->vcpus = nvcpus;
473 474 475 476 477
    ret = 0;
cleanup:
    return ret;
}

478 479
static void
testDomainShutdownState(virDomainPtr domain,
J
Jiri Denemark 已提交
480 481
                        virDomainObjPtr privdom,
                        virDomainShutoffReason reason)
482 483 484 485 486 487 488
{
    if (privdom->newDef) {
        virDomainDefFree(privdom->def);
        privdom->def = privdom->newDef;
        privdom->newDef = NULL;
    }

J
Jiri Denemark 已提交
489
    virDomainObjSetState(privdom, VIR_DOMAIN_SHUTOFF, reason);
490 491 492 493 494
    privdom->def->id = -1;
    if (domain)
        domain->id = -1;
}

495
/* Set up domain runtime state */
496 497
static int
testDomainStartState(virConnectPtr conn,
J
Jiri Denemark 已提交
498 499
                     virDomainObjPtr dom,
                     virDomainRunningReason reason)
500 501
{
    testConnPtr privconn = conn->privateData;
502
    int ret = -1;
503

504 505 506
    if (testDomainUpdateVCPUs(conn, dom, dom->def->vcpus, 1) < 0)
        goto cleanup;

J
Jiri Denemark 已提交
507
    virDomainObjSetState(dom, VIR_DOMAIN_RUNNING, reason);
508 509
    dom->def->id = privconn->nextDomID++;

510
    if (virDomainObjSetDefTransient(privconn->caps,
511
                                    privconn->xmlopt,
512
                                    dom, false) < 0) {
513 514 515
        goto cleanup;
    }

516 517
    ret = 0;
cleanup:
518
    if (ret < 0)
J
Jiri Denemark 已提交
519
        testDomainShutdownState(NULL, dom, VIR_DOMAIN_SHUTOFF_FAILED);
520
    return ret;
521
}
522

523
static int testOpenDefault(virConnectPtr conn) {
524
    int u;
525
    testConnPtr privconn;
526 527 528 529
    virDomainDefPtr domdef = NULL;
    virDomainObjPtr domobj = NULL;
    virNetworkDefPtr netdef = NULL;
    virNetworkObjPtr netobj = NULL;
L
Laine Stump 已提交
530 531
    virInterfaceDefPtr interfacedef = NULL;
    virInterfaceObjPtr interfaceobj = NULL;
C
Cole Robinson 已提交
532 533
    virStoragePoolDefPtr pooldef = NULL;
    virStoragePoolObjPtr poolobj = NULL;
534 535
    virNodeDeviceDefPtr nodedef = NULL;
    virNodeDeviceObjPtr nodeobj = NULL;
536

537
    if (VIR_ALLOC(privconn) < 0) {
538
        virReportOOMError();
539 540
        return VIR_DRV_OPEN_ERROR;
    }
541
    if (virMutexInit(&privconn->lock) < 0) {
542 543
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("cannot initialize mutex"));
544 545 546 547
        VIR_FREE(privconn);
        return VIR_DRV_OPEN_ERROR;
    }

548
    testDriverLock(privconn);
549
    conn->privateData = privconn;
550

551
    if (!(privconn->domains = virDomainObjListNew()))
552 553
        goto error;

554
    memmove(&privconn->nodeInfo, &defaultNodeInfo, sizeof(defaultNodeInfo));
555

556
    /* Numa setup */
557 558 559 560 561
    privconn->numCells = 2;
    for (u = 0; u < 2; ++u) {
        privconn->cells[u].numCpus = 8;
        privconn->cells[u].mem = (u + 1) * 2048 * 1024;
    }
562
    for (u = 0; u < 16; u++) {
563 564 565 566 567 568 569 570 571 572
        virBitmapPtr siblings = virBitmapNew(16);
        if (!siblings) {
            virReportOOMError();
            goto error;
        }
        ignore_value(virBitmapSetBit(siblings, u));
        privconn->cells[u / 8].cpus[(u % 8)].id = u;
        privconn->cells[u / 8].cpus[(u % 8)].socket_id = u / 8;
        privconn->cells[u / 8].cpus[(u % 8)].core_id = u % 8;
        privconn->cells[u / 8].cpus[(u % 8)].siblings = siblings;
573 574
    }

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

578
    if (!(privconn->xmlopt = testBuildXMLConfig()))
579 580
        goto error;

581 582
    privconn->nextDomID = 1;

583 584
    if (!(domdef = virDomainDefParseString(defaultDomainXML,
                                           privconn->caps,
585
                                           privconn->xmlopt,
M
Matthias Bolte 已提交
586
                                           1 << VIR_DOMAIN_VIRT_TEST,
587
                                           VIR_DOMAIN_XML_INACTIVE)))
588
        goto error;
M
Matthias Bolte 已提交
589

590
    if (testDomainGenerateIfnames(domdef) < 0)
591
        goto error;
592
    if (!(domobj = virDomainObjListAdd(privconn->domains,
593
                                       domdef,
594
                                       privconn->xmlopt,
595
                                       0, NULL)))
596 597
        goto error;
    domdef = NULL;
598

599
    domobj->persistent = 1;
J
Jiri Denemark 已提交
600
    if (testDomainStartState(conn, domobj, VIR_DOMAIN_RUNNING_BOOTED) < 0) {
601
        virObjectUnlock(domobj);
602 603 604
        goto error;
    }

605
    virObjectUnlock(domobj);
606

607
    if (!(netdef = virNetworkDefParseString(defaultNetworkXML)))
608
        goto error;
609
    if (!(netobj = virNetworkAssignDef(&privconn->networks, netdef, false))) {
610 611 612 613 614
        virNetworkDefFree(netdef);
        goto error;
    }
    netobj->active = 1;
    netobj->persistent = 1;
615
    virNetworkObjUnlock(netobj);
616

617
    if (!(interfacedef = virInterfaceDefParseString(defaultInterfaceXML)))
L
Laine Stump 已提交
618
        goto error;
619
    if (!(interfaceobj = virInterfaceAssignDef(&privconn->ifaces, interfacedef))) {
L
Laine Stump 已提交
620 621 622 623 624 625
        virInterfaceDefFree(interfacedef);
        goto error;
    }
    interfaceobj->active = 1;
    virInterfaceObjUnlock(interfaceobj);

626
    if (!(pooldef = virStoragePoolDefParseString(defaultPoolXML)))
C
Cole Robinson 已提交
627 628
        goto error;

629
    if (!(poolobj = virStoragePoolObjAssignDef(&privconn->pools,
C
Cole Robinson 已提交
630 631 632 633
                                               pooldef))) {
        virStoragePoolDefFree(pooldef);
        goto error;
    }
634

635
    if (testStoragePoolObjSetDefaults(poolobj) == -1) {
636
        virStoragePoolObjUnlock(poolobj);
C
Cole Robinson 已提交
637
        goto error;
638
    }
C
Cole Robinson 已提交
639
    poolobj->active = 1;
640
    virStoragePoolObjUnlock(poolobj);
C
Cole Robinson 已提交
641

642
    /* Init default node device */
643
    if (!(nodedef = virNodeDeviceDefParseString(defaultNodeXML, 0, NULL)))
644
        goto error;
645
    if (!(nodeobj = virNodeDeviceAssignDef(&privconn->devs,
646 647 648 649 650 651
                                           nodedef))) {
        virNodeDeviceDefFree(nodedef);
        goto error;
    }
    virNodeDeviceObjUnlock(nodeobj);

652
    testDriverUnlock(privconn);
653

654 655 656
    return VIR_DRV_OPEN_SUCCESS;

error:
657
    virObjectUnref(privconn->domains);
658
    virNetworkObjListFree(&privconn->networks);
L
Laine Stump 已提交
659
    virInterfaceObjListFree(&privconn->ifaces);
C
Cole Robinson 已提交
660
    virStoragePoolObjListFree(&privconn->pools);
661
    virNodeDeviceObjListFree(&privconn->devs);
662
    virObjectUnref(privconn->caps);
663
    testDriverUnlock(privconn);
664
    conn->privateData = NULL;
665
    VIR_FREE(privconn);
666
    virDomainDefFree(domdef);
667
    return VIR_DRV_OPEN_ERROR;
668 669 670 671
}


static char *testBuildFilename(const char *relativeTo,
672 673 674
                               const char *filename) {
    char *offset;
    int baseLen;
675 676
    char *ret;

677
    if (!filename || filename[0] == '\0')
678
        return NULL;
679 680 681 682
    if (filename[0] == '/') {
        ignore_value(VIR_STRDUP(ret, filename));
        return ret;
    }
683

684
    offset = strrchr(relativeTo, '/');
685
    if ((baseLen = (offset-relativeTo+1))) {
686
        char *absFile;
C
Chris Lalancette 已提交
687 688
        int totalLen = baseLen + strlen(filename) + 1;
        if (VIR_ALLOC_N(absFile, totalLen) < 0)
689
            return NULL;
C
Chris Lalancette 已提交
690 691 692 693
        if (virStrncpy(absFile, relativeTo, baseLen, totalLen) == NULL) {
            VIR_FREE(absFile);
            return NULL;
        }
694 695 696
        strcat(absFile, filename);
        return absFile;
    } else {
697 698
        ignore_value(VIR_STRDUP(ret, filename));
        return ret;
699
    }
700 701
}

702
static int testOpenVolumesForPool(xmlDocPtr xml,
703 704 705 706 707 708 709
                                  xmlXPathContextPtr ctxt,
                                  const char *file,
                                  virStoragePoolObjPtr pool,
                                  int poolidx) {
    char *vol_xpath;
    int i, ret, func_ret = -1;
    xmlNodePtr *vols = NULL;
710
    virStorageVolDefPtr def = NULL;
711 712 713

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

718
    ret = virXPathNodeSet(vol_xpath, ctxt, &vols);
719 720 721 722 723
    VIR_FREE(vol_xpath);
    if (ret < 0) {
        goto error;
    }

724
    for (i = 0; i < ret; i++) {
725 726 727 728 729
        char *relFile = virXMLPropString(vols[i], "file");
        if (relFile != NULL) {
            char *absFile = testBuildFilename(file, relFile);
            VIR_FREE(relFile);
            if (!absFile) {
730 731
                virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                               _("resolving volume filename"));
732 733 734
                goto error;
            }

735
            def = virStorageVolDefParseFile(pool->def, absFile);
736 737 738 739
            VIR_FREE(absFile);
            if (!def)
                goto error;
        } else {
740
            if ((def = virStorageVolDefParseNode(pool->def, xml,
741 742 743 744 745 746 747
                                                 vols[i])) == NULL) {
                goto error;
            }
        }

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

752 753 754 755 756 757 758
        if (def->target.path == NULL) {
            if (virAsprintf(&def->target.path, "%s/%s",
                            pool->def->target.path,
                            def->name) == -1) {
                virReportOOMError();
                goto error;
            }
759 760
        }

761 762
        if (!def->key && VIR_STRDUP(def->key, def->target.path) < 0)
            goto error;
763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778

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

779
static int testOpenFromFile(virConnectPtr conn,
780
                            const char *file) {
781
    int i, ret;
782 783
    long l;
    char *str;
784
    xmlDocPtr xml = NULL;
785 786
    xmlNodePtr *domains = NULL, *networks = NULL, *ifaces = NULL,
               *pools = NULL, *devs = NULL;
787 788
    xmlXPathContextPtr ctxt = NULL;
    virNodeInfoPtr nodeInfo;
789
    virNetworkObjPtr net;
L
Laine Stump 已提交
790
    virInterfaceObjPtr iface;
791
    virDomainObjPtr dom;
792 793
    testConnPtr privconn;
    if (VIR_ALLOC(privconn) < 0) {
794
        virReportOOMError();
795 796
        return VIR_DRV_OPEN_ERROR;
    }
797
    if (virMutexInit(&privconn->lock) < 0) {
798 799
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("cannot initialize mutex"));
800 801 802 803
        VIR_FREE(privconn);
        return VIR_DRV_OPEN_ERROR;
    }

804
    testDriverLock(privconn);
805
    conn->privateData = privconn;
806

807
    if (!(privconn->domains = virDomainObjListNew()))
808 809
        goto error;

810 811
    if (!(privconn->caps = testBuildCapabilities(conn)))
        goto error;
812

813
    if (!(privconn->xmlopt = testBuildXMLConfig()))
814 815
        goto error;

816
    if (!(xml = virXMLParseFileCtxt(file, &ctxt))) {
817
        goto error;
818 819
    }

820
    if (!xmlStrEqual(ctxt->node->name, BAD_CAST "node")) {
821 822
        virReportError(VIR_ERR_XML_ERROR, "%s",
                       _("Root element is not 'node'"));
823
        goto error;
824 825
    }

826
    privconn->nextDomID = 1;
827
    privconn->numCells = 0;
828
    if (VIR_STRDUP(privconn->path, file) < 0)
C
Chris Lalancette 已提交
829
        goto error;
830 831 832
    memmove(&privconn->nodeInfo, &defaultNodeInfo, sizeof(defaultNodeInfo));

    nodeInfo = &privconn->nodeInfo;
833
    ret = virXPathLong("string(/node/cpu/nodes[1])", ctxt, &l);
834 835 836
    if (ret == 0) {
        nodeInfo->nodes = l;
    } else if (ret == -2) {
837 838
        virReportError(VIR_ERR_XML_ERROR, "%s",
                       _("invalid node cpu nodes value"));
839
        goto error;
840
    }
841

842
    ret = virXPathLong("string(/node/cpu/sockets[1])", ctxt, &l);
843 844 845
    if (ret == 0) {
        nodeInfo->sockets = l;
    } else if (ret == -2) {
846 847
        virReportError(VIR_ERR_XML_ERROR, "%s",
                       _("invalid node cpu sockets value"));
848
        goto error;
849
    }
850

851
    ret = virXPathLong("string(/node/cpu/cores[1])", ctxt, &l);
852 853 854
    if (ret == 0) {
        nodeInfo->cores = l;
    } else if (ret == -2) {
855 856
        virReportError(VIR_ERR_XML_ERROR, "%s",
                       _("invalid node cpu cores value"));
857
        goto error;
858 859
    }

860
    ret = virXPathLong("string(/node/cpu/threads[1])", ctxt, &l);
861 862 863
    if (ret == 0) {
        nodeInfo->threads = l;
    } else if (ret == -2) {
864 865
        virReportError(VIR_ERR_XML_ERROR, "%s",
                       _("invalid node cpu threads value"));
866
        goto error;
867
    }
868

869
    nodeInfo->cpus = nodeInfo->cores * nodeInfo->threads * nodeInfo->sockets * nodeInfo->nodes;
870
    ret = virXPathLong("string(/node/cpu/active[1])", ctxt, &l);
871 872
    if (ret == 0) {
        if (l < nodeInfo->cpus) {
873 874
            nodeInfo->cpus = l;
        }
875
    } else if (ret == -2) {
876 877
        virReportError(VIR_ERR_XML_ERROR, "%s",
                       _("invalid node cpu active value"));
878
        goto error;
879
    }
880
    ret = virXPathLong("string(/node/cpu/mhz[1])", ctxt, &l);
881 882 883
    if (ret == 0) {
        nodeInfo->mhz = l;
    } else if (ret == -2) {
884 885
        virReportError(VIR_ERR_XML_ERROR, "%s",
                       _("invalid node cpu mhz value"));
886
        goto error;
887 888
    }

889
    str = virXPathString("string(/node/cpu/model[1])", ctxt);
890
    if (str != NULL) {
C
Chris Lalancette 已提交
891
        if (virStrcpyStatic(nodeInfo->model, str) == NULL) {
892 893
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Model %s too big for destination"), str);
C
Chris Lalancette 已提交
894 895 896
            VIR_FREE(str);
            goto error;
        }
897
        VIR_FREE(str);
898 899
    }

900
    ret = virXPathLong("string(/node/memory[1])", ctxt, &l);
901 902 903
    if (ret == 0) {
        nodeInfo->memory = l;
    } else if (ret == -2) {
904 905
        virReportError(VIR_ERR_XML_ERROR, "%s",
                       _("invalid node memory value"));
906
        goto error;
907
    }
908

909
    ret = virXPathNodeSet("/node/domain", ctxt, &domains);
910
    if (ret < 0) {
911
        goto error;
912
    }
913

914
    for (i = 0; i < ret; i++) {
915 916 917 918 919 920
        virDomainDefPtr def;
        char *relFile = virXMLPropString(domains[i], "file");
        if (relFile != NULL) {
            char *absFile = testBuildFilename(file, relFile);
            VIR_FREE(relFile);
            if (!absFile) {
921 922
                virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                               _("resolving domain filename"));
923 924
                goto error;
            }
925 926
            def = virDomainDefParseFile(absFile, privconn->caps,
                                        privconn->xmlopt,
M
Matthias Bolte 已提交
927
                                        1 << VIR_DOMAIN_VIRT_TEST,
928
                                        VIR_DOMAIN_XML_INACTIVE);
929
            VIR_FREE(absFile);
930 931 932
            if (!def)
                goto error;
        } else {
933 934
            if ((def = virDomainDefParseNode(xml, domains[i],
                                             privconn->caps, privconn->xmlopt,
M
Matthias Bolte 已提交
935
                                             1 << VIR_DOMAIN_VIRT_TEST,
936
                                             VIR_DOMAIN_XML_INACTIVE)) == NULL)
937 938 939
                goto error;
        }

940
        if (testDomainGenerateIfnames(def) < 0 ||
941
            !(dom = virDomainObjListAdd(privconn->domains,
942
                                        def,
943
                                        privconn->xmlopt,
944
                                        0, NULL))) {
945
            virDomainDefFree(def);
946 947
            goto error;
        }
948

949
        dom->persistent = 1;
J
Jiri Denemark 已提交
950
        if (testDomainStartState(conn, dom, VIR_DOMAIN_RUNNING_BOOTED) < 0) {
951
            virObjectUnlock(dom);
952 953 954
            goto error;
        }

955
        virObjectUnlock(dom);
956
    }
957
    VIR_FREE(domains);
958

959
    ret = virXPathNodeSet("/node/network", ctxt, &networks);
960 961 962
    if (ret < 0) {
        goto error;
    }
963
    for (i = 0; i < ret; i++) {
964 965 966 967 968
        virNetworkDefPtr def;
        char *relFile = virXMLPropString(networks[i], "file");
        if (relFile != NULL) {
            char *absFile = testBuildFilename(file, relFile);
            VIR_FREE(relFile);
969
            if (!absFile) {
970 971
                virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                               _("resolving network filename"));
972 973
                goto error;
            }
974

975
            def = virNetworkDefParseFile(absFile);
976
            VIR_FREE(absFile);
977 978 979
            if (!def)
                goto error;
        } else {
980
            if ((def = virNetworkDefParseNode(xml, networks[i])) == NULL)
981
                goto error;
982
        }
983
        if (!(net = virNetworkAssignDef(&privconn->networks, def, false))) {
984 985
            virNetworkDefFree(def);
            goto error;
986
        }
987
        net->persistent = 1;
988
        net->active = 1;
989
        virNetworkObjUnlock(net);
990
    }
991
    VIR_FREE(networks);
992

L
Laine Stump 已提交
993
    /* Parse interface definitions */
994
    ret = virXPathNodeSet("/node/interface", ctxt, &ifaces);
L
Laine Stump 已提交
995 996 997
    if (ret < 0) {
        goto error;
    }
998
    for (i = 0; i < ret; i++) {
L
Laine Stump 已提交
999 1000 1001 1002 1003 1004
        virInterfaceDefPtr def;
        char *relFile = virXMLPropString(ifaces[i], "file");
        if (relFile != NULL) {
            char *absFile = testBuildFilename(file, relFile);
            VIR_FREE(relFile);
            if (!absFile) {
1005 1006
                virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                               _("resolving interface filename"));
L
Laine Stump 已提交
1007 1008 1009
                goto error;
            }

1010
            def = virInterfaceDefParseFile(absFile);
L
Laine Stump 已提交
1011 1012 1013 1014
            VIR_FREE(absFile);
            if (!def)
                goto error;
        } else {
1015
            if ((def = virInterfaceDefParseNode(xml, ifaces[i])) == NULL)
L
Laine Stump 已提交
1016 1017
                goto error;
        }
1018

1019
        if (!(iface = virInterfaceAssignDef(&privconn->ifaces, def))) {
L
Laine Stump 已提交
1020 1021 1022
            virInterfaceDefFree(def);
            goto error;
        }
1023 1024

        iface->active = 1;
L
Laine Stump 已提交
1025 1026 1027 1028
        virInterfaceObjUnlock(iface);
    }
    VIR_FREE(ifaces);

C
Cole Robinson 已提交
1029
    /* Parse Storage Pool list */
1030
    ret = virXPathNodeSet("/node/pool", ctxt, &pools);
C
Cole Robinson 已提交
1031 1032 1033
    if (ret < 0) {
        goto error;
    }
1034
    for (i = 0; i < ret; i++) {
C
Cole Robinson 已提交
1035 1036 1037 1038 1039 1040 1041
        virStoragePoolDefPtr def;
        virStoragePoolObjPtr pool;
        char *relFile = virXMLPropString(pools[i], "file");
        if (relFile != NULL) {
            char *absFile = testBuildFilename(file, relFile);
            VIR_FREE(relFile);
            if (!absFile) {
1042 1043
                virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                               _("resolving pool filename"));
C
Cole Robinson 已提交
1044 1045 1046
                goto error;
            }

1047
            def = virStoragePoolDefParseFile(absFile);
C
Cole Robinson 已提交
1048 1049 1050 1051
            VIR_FREE(absFile);
            if (!def)
                goto error;
        } else {
1052
            if ((def = virStoragePoolDefParseNode(xml,
1053
                                                  pools[i])) == NULL) {
C
Cole Robinson 已提交
1054 1055 1056 1057
                goto error;
            }
        }

1058
        if (!(pool = virStoragePoolObjAssignDef(&privconn->pools,
C
Cole Robinson 已提交
1059 1060 1061 1062 1063
                                                def))) {
            virStoragePoolDefFree(def);
            goto error;
        }

1064
        if (testStoragePoolObjSetDefaults(pool) == -1) {
1065
            virStoragePoolObjUnlock(pool);
C
Cole Robinson 已提交
1066
            goto error;
1067
        }
C
Cole Robinson 已提交
1068
        pool->active = 1;
1069 1070

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

1076
        virStoragePoolObjUnlock(pool);
C
Cole Robinson 已提交
1077
    }
1078
    VIR_FREE(pools);
C
Cole Robinson 已提交
1079

1080
    ret = virXPathNodeSet("/node/device", ctxt, &devs);
1081 1082 1083
    if (ret < 0) {
        goto error;
    }
1084
    for (i = 0; i < ret; i++) {
1085 1086 1087 1088 1089 1090 1091 1092 1093
        virNodeDeviceDefPtr def;
        virNodeDeviceObjPtr dev;
        char *relFile = virXMLPropString(devs[i], "file");

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

            if (!absFile) {
1094 1095
                virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                               _("resolving device filename"));
1096 1097 1098
                goto error;
            }

1099
            def = virNodeDeviceDefParseFile(absFile, 0, NULL);
1100 1101 1102 1103
            VIR_FREE(absFile);
            if (!def)
                goto error;
        } else {
1104
            if ((def = virNodeDeviceDefParseNode(xml, devs[i], 0, NULL)) == NULL)
1105 1106
                goto error;
        }
1107
        if (!(dev = virNodeDeviceAssignDef(&privconn->devs, def))) {
1108 1109 1110 1111 1112 1113 1114 1115
            virNodeDeviceDefFree(def);
            goto error;
        }
        virNodeDeviceObjUnlock(dev);
    }
    VIR_FREE(devs);


J
Jim Meyering 已提交
1116
    xmlXPathFreeContext(ctxt);
1117
    xmlFreeDoc(xml);
1118
    testDriverUnlock(privconn);
1119

1120
    return 0;
1121 1122

 error:
J
Jim Meyering 已提交
1123
    xmlXPathFreeContext(ctxt);
1124
    xmlFreeDoc(xml);
1125 1126
    VIR_FREE(domains);
    VIR_FREE(networks);
L
Laine Stump 已提交
1127
    VIR_FREE(ifaces);
C
Cole Robinson 已提交
1128
    VIR_FREE(pools);
1129
    VIR_FREE(devs);
1130
    virObjectUnref(privconn->domains);
1131
    virNetworkObjListFree(&privconn->networks);
L
Laine Stump 已提交
1132
    virInterfaceObjListFree(&privconn->ifaces);
C
Cole Robinson 已提交
1133
    virStoragePoolObjListFree(&privconn->pools);
E
Eric Blake 已提交
1134
    VIR_FREE(privconn->path);
1135
    testDriverUnlock(privconn);
1136
    VIR_FREE(privconn);
1137
    conn->privateData = NULL;
1138
    return VIR_DRV_OPEN_ERROR;
1139 1140
}

1141

1142 1143 1144
static virDrvOpenStatus testConnectOpen(virConnectPtr conn,
                                        virConnectAuthPtr auth ATTRIBUTE_UNUSED,
                                        unsigned int flags)
1145
{
1146
    int ret;
1147
    testConnPtr privconn;
1148

E
Eric Blake 已提交
1149 1150
    virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);

1151
    if (!conn->uri)
1152
        return VIR_DRV_OPEN_DECLINED;
1153

1154
    if (!conn->uri->scheme || STRNEQ(conn->uri->scheme, "test"))
1155
        return VIR_DRV_OPEN_DECLINED;
1156

1157
    /* Remote driver should handle these. */
1158
    if (conn->uri->server)
1159 1160
        return VIR_DRV_OPEN_DECLINED;

1161
    /* From this point on, the connection is for us. */
1162 1163 1164
    if (!conn->uri->path
        || conn->uri->path[0] == '\0'
        || (conn->uri->path[0] == '/' && conn->uri->path[1] == '\0')) {
1165 1166
        virReportError(VIR_ERR_INVALID_ARG,
                       "%s", _("testOpen: supply a path or use test:///default"));
1167 1168
        return VIR_DRV_OPEN_ERROR;
    }
1169

1170
    if (STREQ(conn->uri->path, "/default"))
1171 1172
        ret = testOpenDefault(conn);
    else
1173
        ret = testOpenFromFile(conn,
1174
                               conn->uri->path);
1175

1176 1177 1178 1179 1180
    if (ret != VIR_DRV_OPEN_SUCCESS)
        return ret;

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

1182
    privconn->domainEventState = virDomainEventStateNew();
1183
    if (!privconn->domainEventState) {
1184
        testDriverUnlock(privconn);
1185
        testConnectClose(conn);
1186
        return VIR_DRV_OPEN_ERROR;
1187 1188
    }

1189 1190 1191
    testDriverUnlock(privconn);

    return VIR_DRV_OPEN_SUCCESS;
1192 1193
}

1194
static int testConnectClose(virConnectPtr conn)
1195
{
1196
    testConnPtr privconn = conn->privateData;
1197
    testDriverLock(privconn);
1198
    virObjectUnref(privconn->caps);
1199
    virObjectUnref(privconn->xmlopt);
1200
    virObjectUnref(privconn->domains);
D
Daniel P. Berrange 已提交
1201
    virNodeDeviceObjListFree(&privconn->devs);
1202
    virNetworkObjListFree(&privconn->networks);
L
Laine Stump 已提交
1203
    virInterfaceObjListFree(&privconn->ifaces);
C
Cole Robinson 已提交
1204
    virStoragePoolObjListFree(&privconn->pools);
1205
    virDomainEventStateFree(privconn->domainEventState);
E
Eric Blake 已提交
1206
    VIR_FREE(privconn->path);
1207

1208
    testDriverUnlock(privconn);
1209
    virMutexDestroy(&privconn->lock);
1210

1211
    VIR_FREE(privconn);
1212
    conn->privateData = NULL;
1213
    return 0;
1214 1215
}

1216 1217
static int testConnectGetVersion(virConnectPtr conn ATTRIBUTE_UNUSED,
                                 unsigned long *hvVer)
1218
{
1219
    *hvVer = 2;
1220
    return 0;
1221 1222
}

1223 1224 1225 1226 1227 1228
static char *testConnectGetHostname(virConnectPtr conn ATTRIBUTE_UNUSED)
{
    return virGetHostname();
}


1229
static int testConnectIsSecure(virConnectPtr conn ATTRIBUTE_UNUSED)
1230 1231 1232 1233
{
    return 1;
}

1234
static int testConnectIsEncrypted(virConnectPtr conn ATTRIBUTE_UNUSED)
1235 1236 1237 1238
{
    return 0;
}

1239
static int testConnectIsAlive(virConnectPtr conn ATTRIBUTE_UNUSED)
1240 1241 1242 1243
{
    return 1;
}

1244 1245
static int testConnectGetMaxVcpus(virConnectPtr conn ATTRIBUTE_UNUSED,
                                  const char *type ATTRIBUTE_UNUSED)
1246 1247 1248 1249 1250 1251
{
    return 32;
}

static int testNodeGetInfo(virConnectPtr conn,
                           virNodeInfoPtr info)
1252
{
1253
    testConnPtr privconn = conn->privateData;
1254
    testDriverLock(privconn);
1255
    memcpy(info, &privconn->nodeInfo, sizeof(virNodeInfo));
1256
    testDriverUnlock(privconn);
1257
    return 0;
1258 1259
}

1260
static char *testConnectGetCapabilities(virConnectPtr conn)
1261
{
1262
    testConnPtr privconn = conn->privateData;
1263
    char *xml;
1264
    testDriverLock(privconn);
1265
    if ((xml = virCapabilitiesFormatXML(privconn->caps)) == NULL)
1266
        virReportOOMError();
1267
    testDriverUnlock(privconn);
1268
    return xml;
1269 1270
}

1271
static int testConnectNumOfDomains(virConnectPtr conn)
1272
{
1273
    testConnPtr privconn = conn->privateData;
1274
    int count;
1275

1276
    testDriverLock(privconn);
1277
    count = virDomainObjListNumOfDomains(privconn->domains, 1);
1278
    testDriverUnlock(privconn);
1279

1280
    return count;
1281 1282
}

1283 1284 1285 1286 1287 1288 1289
static int testDomainIsActive(virDomainPtr dom)
{
    testConnPtr privconn = dom->conn->privateData;
    virDomainObjPtr obj;
    int ret = -1;

    testDriverLock(privconn);
1290
    obj = virDomainObjListFindByUUID(privconn->domains, dom->uuid);
1291 1292
    testDriverUnlock(privconn);
    if (!obj) {
1293
        virReportError(VIR_ERR_NO_DOMAIN, NULL);
1294 1295 1296 1297 1298 1299
        goto cleanup;
    }
    ret = virDomainObjIsActive(obj);

cleanup:
    if (obj)
1300
        virObjectUnlock(obj);
1301 1302 1303 1304 1305 1306 1307 1308 1309 1310
    return ret;
}

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

    testDriverLock(privconn);
1311
    obj = virDomainObjListFindByUUID(privconn->domains, dom->uuid);
1312 1313
    testDriverUnlock(privconn);
    if (!obj) {
1314
        virReportError(VIR_ERR_NO_DOMAIN, NULL);
1315 1316 1317 1318 1319 1320
        goto cleanup;
    }
    ret = obj->persistent;

cleanup:
    if (obj)
1321
        virObjectUnlock(obj);
1322 1323 1324
    return ret;
}

1325 1326 1327 1328 1329
static int testDomainIsUpdated(virDomainPtr dom ATTRIBUTE_UNUSED)
{
    return 0;
}

1330
static virDomainPtr
1331
testDomainCreateXML(virConnectPtr conn, const char *xml,
1332
                      unsigned int flags)
1333
{
1334
    testConnPtr privconn = conn->privateData;
1335
    virDomainPtr ret = NULL;
1336
    virDomainDefPtr def;
1337
    virDomainObjPtr dom = NULL;
1338
    virDomainEventPtr event = NULL;
1339

1340 1341
    virCheckFlags(0, NULL);

1342
    testDriverLock(privconn);
1343 1344
    if ((def = virDomainDefParseString(xml,privconn->caps, privconn->xmlopt,
                                       1 << VIR_DOMAIN_VIRT_TEST,
1345
                                       VIR_DOMAIN_XML_INACTIVE)) == NULL)
1346
        goto cleanup;
1347

1348
    if (testDomainGenerateIfnames(def) < 0)
1349
        goto cleanup;
1350
    if (!(dom = virDomainObjListAdd(privconn->domains,
1351
                                    def,
1352
                                    privconn->xmlopt,
1353 1354
                                    VIR_DOMAIN_OBJ_LIST_ADD_CHECK_LIVE,
                                    NULL)))
1355 1356
        goto cleanup;
    def = NULL;
1357

J
Jiri Denemark 已提交
1358
    if (testDomainStartState(conn, dom, VIR_DOMAIN_RUNNING_BOOTED) < 0)
1359
        goto cleanup;
1360

1361 1362 1363 1364
    event = virDomainEventNewFromObj(dom,
                                     VIR_DOMAIN_EVENT_STARTED,
                                     VIR_DOMAIN_EVENT_STARTED_BOOTED);

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

cleanup:
1370
    if (dom)
1371
        virObjectUnlock(dom);
1372 1373
    if (event)
        testDomainEventQueue(privconn, event);
1374
    virDomainDefFree(def);
1375
    testDriverUnlock(privconn);
1376
    return ret;
1377 1378 1379
}


1380
static virDomainPtr testDomainLookupByID(virConnectPtr conn,
1381
                                         int id)
1382
{
1383
    testConnPtr privconn = conn->privateData;
1384 1385
    virDomainPtr ret = NULL;
    virDomainObjPtr dom;
1386

1387
    testDriverLock(privconn);
1388
    dom = virDomainObjListFindByID(privconn->domains, id);
1389 1390 1391
    testDriverUnlock(privconn);

    if (dom == NULL) {
1392
        virReportError(VIR_ERR_NO_DOMAIN, NULL);
1393
        goto cleanup;
1394 1395
    }

1396
    ret = virGetDomain(conn, dom->def->name, dom->def->uuid);
1397 1398 1399 1400
    if (ret)
        ret->id = dom->def->id;

cleanup:
1401
    if (dom)
1402
        virObjectUnlock(dom);
1403
    return ret;
1404 1405
}

1406
static virDomainPtr testDomainLookupByUUID(virConnectPtr conn,
1407
                                           const unsigned char *uuid)
1408
{
1409
    testConnPtr privconn = conn->privateData;
1410
    virDomainPtr ret = NULL;
1411
    virDomainObjPtr dom;
1412

1413
    testDriverLock(privconn);
1414
    dom = virDomainObjListFindByUUID(privconn->domains, uuid);
1415 1416 1417
    testDriverUnlock(privconn);

    if (dom == NULL) {
1418
        virReportError(VIR_ERR_NO_DOMAIN, NULL);
1419
        goto cleanup;
1420
    }
1421

1422
    ret = virGetDomain(conn, dom->def->name, dom->def->uuid);
1423 1424 1425 1426
    if (ret)
        ret->id = dom->def->id;

cleanup:
1427
    if (dom)
1428
        virObjectUnlock(dom);
1429
    return ret;
1430 1431
}

1432
static virDomainPtr testDomainLookupByName(virConnectPtr conn,
1433
                                           const char *name)
1434
{
1435
    testConnPtr privconn = conn->privateData;
1436 1437
    virDomainPtr ret = NULL;
    virDomainObjPtr dom;
1438

1439
    testDriverLock(privconn);
1440
    dom = virDomainObjListFindByName(privconn->domains, name);
1441 1442 1443
    testDriverUnlock(privconn);

    if (dom == NULL) {
1444
        virReportError(VIR_ERR_NO_DOMAIN, NULL);
1445
        goto cleanup;
1446
    }
1447

1448
    ret = virGetDomain(conn, dom->def->name, dom->def->uuid);
1449 1450 1451 1452
    if (ret)
        ret->id = dom->def->id;

cleanup:
1453
    if (dom)
1454
        virObjectUnlock(dom);
1455
    return ret;
1456 1457
}

1458 1459 1460
static int testConnectListDomains(virConnectPtr conn,
                                  int *ids,
                                  int maxids)
1461
{
1462
    testConnPtr privconn = conn->privateData;
1463
    int n;
1464

1465
    testDriverLock(privconn);
1466
    n = virDomainObjListGetActiveIDs(privconn->domains, ids, maxids);
1467
    testDriverUnlock(privconn);
1468

1469
    return n;
1470 1471
}

1472
static int testDomainDestroy(virDomainPtr domain)
1473
{
1474 1475
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
1476
    virDomainEventPtr event = NULL;
1477
    int ret = -1;
1478

1479
    testDriverLock(privconn);
1480 1481
    privdom = virDomainObjListFindByName(privconn->domains,
                                         domain->name);
1482 1483

    if (privdom == NULL) {
1484
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
1485
        goto cleanup;
1486
    }
1487

J
Jiri Denemark 已提交
1488
    testDomainShutdownState(domain, privdom, VIR_DOMAIN_SHUTOFF_DESTROYED);
1489 1490 1491
    event = virDomainEventNewFromObj(privdom,
                                     VIR_DOMAIN_EVENT_STOPPED,
                                     VIR_DOMAIN_EVENT_STOPPED_DESTROYED);
1492

1493
    if (!privdom->persistent) {
1494 1495
        virDomainObjListRemove(privconn->domains,
                               privdom);
1496
        privdom = NULL;
1497
    }
1498 1499 1500

    ret = 0;
cleanup:
1501
    if (privdom)
1502
        virObjectUnlock(privdom);
1503 1504
    if (event)
        testDomainEventQueue(privconn, event);
1505
    testDriverUnlock(privconn);
1506
    return ret;
1507 1508
}

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

1516
    testDriverLock(privconn);
1517 1518
    privdom = virDomainObjListFindByName(privconn->domains,
                                         domain->name);
1519
    testDriverUnlock(privconn);
1520 1521

    if (privdom == NULL) {
1522
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
1523
        goto cleanup;
1524
    }
1525

J
Jiri Denemark 已提交
1526
    if (virDomainObjGetState(privdom, NULL) != VIR_DOMAIN_PAUSED) {
1527 1528
        virReportError(VIR_ERR_INTERNAL_ERROR, _("domain '%s' not paused"),
                       domain->name);
1529
        goto cleanup;
1530
    }
1531

J
Jiri Denemark 已提交
1532 1533
    virDomainObjSetState(privdom, VIR_DOMAIN_RUNNING,
                         VIR_DOMAIN_RUNNING_UNPAUSED);
1534 1535 1536
    event = virDomainEventNewFromObj(privdom,
                                     VIR_DOMAIN_EVENT_RESUMED,
                                     VIR_DOMAIN_EVENT_RESUMED_UNPAUSED);
1537 1538 1539
    ret = 0;

cleanup:
1540
    if (privdom)
1541
        virObjectUnlock(privdom);
1542 1543 1544 1545 1546
    if (event) {
        testDriverLock(privconn);
        testDomainEventQueue(privconn, event);
        testDriverUnlock(privconn);
    }
1547
    return ret;
1548 1549
}

1550
static int testDomainSuspend(virDomainPtr domain)
1551
{
1552 1553
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
1554
    virDomainEventPtr event = NULL;
1555
    int ret = -1;
J
Jiri Denemark 已提交
1556
    int state;
1557

1558
    testDriverLock(privconn);
1559 1560
    privdom = virDomainObjListFindByName(privconn->domains,
                                         domain->name);
1561
    testDriverUnlock(privconn);
1562 1563

    if (privdom == NULL) {
1564
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
1565
        goto cleanup;
1566
    }
1567

J
Jiri Denemark 已提交
1568 1569
    state = virDomainObjGetState(privdom, NULL);
    if (state == VIR_DOMAIN_SHUTOFF || state == VIR_DOMAIN_PAUSED) {
1570 1571
        virReportError(VIR_ERR_INTERNAL_ERROR, _("domain '%s' not running"),
                       domain->name);
1572
        goto cleanup;
1573
    }
1574

J
Jiri Denemark 已提交
1575
    virDomainObjSetState(privdom, VIR_DOMAIN_PAUSED, VIR_DOMAIN_PAUSED_USER);
1576 1577 1578
    event = virDomainEventNewFromObj(privdom,
                                     VIR_DOMAIN_EVENT_SUSPENDED,
                                     VIR_DOMAIN_EVENT_SUSPENDED_PAUSED);
1579 1580 1581
    ret = 0;

cleanup:
1582
    if (privdom)
1583
        virObjectUnlock(privdom);
1584 1585 1586 1587 1588 1589

    if (event) {
        testDriverLock(privconn);
        testDomainEventQueue(privconn, event);
        testDriverUnlock(privconn);
    }
1590
    return ret;
1591 1592
}

1593
static int testDomainShutdownFlags(virDomainPtr domain,
1594
                                   unsigned int flags)
1595
{
1596 1597
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
1598
    virDomainEventPtr event = NULL;
1599
    int ret = -1;
1600

1601 1602
    virCheckFlags(0, -1);

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

    if (privdom == NULL) {
1608
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
1609
        goto cleanup;
1610
    }
1611

J
Jiri Denemark 已提交
1612
    if (virDomainObjGetState(privdom, NULL) == VIR_DOMAIN_SHUTOFF) {
1613 1614
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("domain '%s' not running"), domain->name);
1615
        goto cleanup;
1616
    }
1617

J
Jiri Denemark 已提交
1618
    testDomainShutdownState(domain, privdom, VIR_DOMAIN_SHUTOFF_SHUTDOWN);
1619 1620 1621
    event = virDomainEventNewFromObj(privdom,
                                     VIR_DOMAIN_EVENT_STOPPED,
                                     VIR_DOMAIN_EVENT_STOPPED_SHUTDOWN);
1622

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

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

1639
static int testDomainShutdown(virDomainPtr domain)
1640
{
1641
    return testDomainShutdownFlags(domain, 0);
1642 1643
}

1644
/* Similar behaviour as shutdown */
1645
static int testDomainReboot(virDomainPtr domain,
1646
                            unsigned int action ATTRIBUTE_UNUSED)
1647
{
1648 1649
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
1650
    virDomainEventPtr event = NULL;
1651
    int ret = -1;
1652

1653
    testDriverLock(privconn);
1654 1655
    privdom = virDomainObjListFindByName(privconn->domains,
                                         domain->name);
1656 1657

    if (privdom == NULL) {
1658
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
1659
        goto cleanup;
1660
    }
1661

J
Jiri Denemark 已提交
1662 1663 1664
    virDomainObjSetState(privdom, VIR_DOMAIN_SHUTDOWN,
                         VIR_DOMAIN_SHUTDOWN_USER);

1665 1666
    switch (privdom->def->onReboot) {
    case VIR_DOMAIN_LIFECYCLE_DESTROY:
J
Jiri Denemark 已提交
1667 1668
        virDomainObjSetState(privdom, VIR_DOMAIN_SHUTOFF,
                             VIR_DOMAIN_SHUTOFF_SHUTDOWN);
1669 1670
        break;

1671
    case VIR_DOMAIN_LIFECYCLE_RESTART:
J
Jiri Denemark 已提交
1672 1673
        virDomainObjSetState(privdom, VIR_DOMAIN_RUNNING,
                             VIR_DOMAIN_RUNNING_BOOTED);
1674 1675
        break;

1676
    case VIR_DOMAIN_LIFECYCLE_PRESERVE:
J
Jiri Denemark 已提交
1677 1678
        virDomainObjSetState(privdom, VIR_DOMAIN_SHUTOFF,
                             VIR_DOMAIN_SHUTOFF_SHUTDOWN);
1679 1680
        break;

1681
    case VIR_DOMAIN_LIFECYCLE_RESTART_RENAME:
J
Jiri Denemark 已提交
1682 1683
        virDomainObjSetState(privdom, VIR_DOMAIN_RUNNING,
                             VIR_DOMAIN_RUNNING_BOOTED);
1684
        break;
1685

1686
    default:
J
Jiri Denemark 已提交
1687 1688
        virDomainObjSetState(privdom, VIR_DOMAIN_SHUTOFF,
                             VIR_DOMAIN_SHUTOFF_SHUTDOWN);
1689 1690
        break;
    }
1691

J
Jiri Denemark 已提交
1692 1693
    if (virDomainObjGetState(privdom, NULL) == VIR_DOMAIN_SHUTOFF) {
        testDomainShutdownState(domain, privdom, VIR_DOMAIN_SHUTOFF_SHUTDOWN);
1694 1695 1696
        event = virDomainEventNewFromObj(privdom,
                                         VIR_DOMAIN_EVENT_STOPPED,
                                         VIR_DOMAIN_EVENT_STOPPED_SHUTDOWN);
1697

1698
        if (!privdom->persistent) {
1699 1700
            virDomainObjListRemove(privconn->domains,
                                   privdom);
1701 1702
            privdom = NULL;
        }
1703 1704
    }

1705 1706
    ret = 0;
cleanup:
1707
    if (privdom)
1708
        virObjectUnlock(privdom);
1709 1710
    if (event)
        testDomainEventQueue(privconn, event);
1711
    testDriverUnlock(privconn);
1712
    return ret;
1713 1714
}

1715
static int testDomainGetInfo(virDomainPtr domain,
1716
                             virDomainInfoPtr info)
1717
{
1718
    testConnPtr privconn = domain->conn->privateData;
1719
    struct timeval tv;
1720
    virDomainObjPtr privdom;
1721
    int ret = -1;
1722

1723
    testDriverLock(privconn);
1724 1725
    privdom = virDomainObjListFindByName(privconn->domains,
                                         domain->name);
1726
    testDriverUnlock(privconn);
1727 1728

    if (privdom == NULL) {
1729
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
1730
        goto cleanup;
1731
    }
1732 1733

    if (gettimeofday(&tv, NULL) < 0) {
1734 1735
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("getting time of day"));
1736
        goto cleanup;
1737 1738
    }

J
Jiri Denemark 已提交
1739
    info->state = virDomainObjGetState(privdom, NULL);
1740 1741
    info->memory = privdom->def->mem.cur_balloon;
    info->maxMem = privdom->def->mem.max_balloon;
1742 1743
    info->nrVirtCpu = privdom->def->vcpus;
    info->cpuTime = ((tv.tv_sec * 1000ll * 1000ll  * 1000ll) + (tv.tv_usec * 1000ll));
1744 1745 1746
    ret = 0;

cleanup:
1747
    if (privdom)
1748
        virObjectUnlock(privdom);
1749
    return ret;
1750 1751
}

1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764
static int
testDomainGetState(virDomainPtr domain,
                   int *state,
                   int *reason,
                   unsigned int flags)
{
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
    int ret = -1;

    virCheckFlags(0, -1);

    testDriverLock(privconn);
1765 1766
    privdom = virDomainObjListFindByName(privconn->domains,
                                         domain->name);
1767 1768 1769
    testDriverUnlock(privconn);

    if (privdom == NULL) {
1770
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
1771 1772 1773
        goto cleanup;
    }

J
Jiri Denemark 已提交
1774
    *state = virDomainObjGetState(privdom, reason);
1775 1776 1777 1778
    ret = 0;

cleanup:
    if (privdom)
1779
        virObjectUnlock(privdom);
1780 1781 1782
    return ret;
}

1783 1784
#define TEST_SAVE_MAGIC "TestGuestMagic"

1785 1786 1787
static int
testDomainSaveFlags(virDomainPtr domain, const char *path,
                    const char *dxml, unsigned int flags)
1788
{
1789
    testConnPtr privconn = domain->conn->privateData;
1790 1791 1792
    char *xml = NULL;
    int fd = -1;
    int len;
1793
    virDomainObjPtr privdom;
1794
    virDomainEventPtr event = NULL;
1795
    int ret = -1;
1796

1797 1798
    virCheckFlags(0, -1);
    if (dxml) {
1799 1800
        virReportError(VIR_ERR_ARGUMENT_UNSUPPORTED, "%s",
                       _("xml modification unsupported"));
1801 1802 1803
        return -1;
    }

1804
    testDriverLock(privconn);
1805 1806
    privdom = virDomainObjListFindByName(privconn->domains,
                                         domain->name);
1807 1808

    if (privdom == NULL) {
1809
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
1810
        goto cleanup;
1811
    }
1812

1813
    xml = virDomainDefFormat(privdom->def,
C
Cole Robinson 已提交
1814 1815
                             VIR_DOMAIN_XML_SECURE);

1816
    if (xml == NULL) {
1817
        virReportSystemError(errno,
1818 1819
                             _("saving domain '%s' failed to allocate space for metadata"),
                             domain->name);
1820
        goto cleanup;
1821
    }
1822 1823

    if ((fd = open(path, O_CREAT|O_TRUNC|O_WRONLY, S_IRUSR|S_IWUSR)) < 0) {
1824
        virReportSystemError(errno,
1825 1826
                             _("saving domain '%s' to '%s': open failed"),
                             domain->name, path);
1827
        goto cleanup;
1828
    }
1829
    len = strlen(xml);
1830
    if (safewrite(fd, TEST_SAVE_MAGIC, sizeof(TEST_SAVE_MAGIC)) < 0) {
1831
        virReportSystemError(errno,
1832 1833
                             _("saving domain '%s' to '%s': write failed"),
                             domain->name, path);
1834
        goto cleanup;
1835
    }
1836
    if (safewrite(fd, (char*)&len, sizeof(len)) < 0) {
1837
        virReportSystemError(errno,
1838 1839
                             _("saving domain '%s' to '%s': write failed"),
                             domain->name, path);
1840
        goto cleanup;
1841
    }
1842
    if (safewrite(fd, xml, len) < 0) {
1843
        virReportSystemError(errno,
1844 1845
                             _("saving domain '%s' to '%s': write failed"),
                             domain->name, path);
1846
        goto cleanup;
1847
    }
1848

1849
    if (VIR_CLOSE(fd) < 0) {
1850
        virReportSystemError(errno,
1851 1852
                             _("saving domain '%s' to '%s': write failed"),
                             domain->name, path);
1853
        goto cleanup;
1854
    }
1855 1856
    fd = -1;

J
Jiri Denemark 已提交
1857
    testDomainShutdownState(domain, privdom, VIR_DOMAIN_SHUTOFF_SAVED);
1858 1859 1860
    event = virDomainEventNewFromObj(privdom,
                                     VIR_DOMAIN_EVENT_STOPPED,
                                     VIR_DOMAIN_EVENT_STOPPED_SAVED);
1861

1862
    if (!privdom->persistent) {
1863 1864
        virDomainObjListRemove(privconn->domains,
                               privdom);
1865
        privdom = NULL;
1866
    }
1867

1868
    ret = 0;
1869 1870 1871 1872 1873 1874 1875
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) {
1876
        VIR_FORCE_CLOSE(fd);
1877 1878
        unlink(path);
    }
1879
    if (privdom)
1880
        virObjectUnlock(privdom);
1881 1882
    if (event)
        testDomainEventQueue(privconn, event);
1883
    testDriverUnlock(privconn);
1884
    return ret;
1885 1886
}

1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898
static int
testDomainSave(virDomainPtr domain,
               const char *path)
{
    return testDomainSaveFlags(domain, path, NULL, 0);
}

static int
testDomainRestoreFlags(virConnectPtr conn,
                       const char *path,
                       const char *dxml,
                       unsigned int flags)
1899
{
1900
    testConnPtr privconn = conn->privateData;
1901
    char *xml = NULL;
1902
    char magic[15];
1903 1904 1905
    int fd = -1;
    int len;
    virDomainDefPtr def = NULL;
1906
    virDomainObjPtr dom = NULL;
1907
    virDomainEventPtr event = NULL;
1908
    int ret = -1;
1909

1910 1911
    virCheckFlags(0, -1);
    if (dxml) {
1912 1913
        virReportError(VIR_ERR_ARGUMENT_UNSUPPORTED, "%s",
                       _("xml modification unsupported"));
1914 1915 1916
        return -1;
    }

1917 1918
    testDriverLock(privconn);

1919
    if ((fd = open(path, O_RDONLY)) < 0) {
1920
        virReportSystemError(errno,
1921 1922
                             _("cannot read domain image '%s'"),
                             path);
1923
        goto cleanup;
1924
    }
1925
    if (saferead(fd, magic, sizeof(magic)) != sizeof(magic)) {
1926
        virReportSystemError(errno,
1927 1928
                             _("incomplete save header in '%s'"),
                             path);
1929
        goto cleanup;
1930
    }
1931
    if (memcmp(magic, TEST_SAVE_MAGIC, sizeof(magic))) {
1932 1933
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("mismatched header magic"));
1934
        goto cleanup;
1935
    }
1936
    if (saferead(fd, (char*)&len, sizeof(len)) != sizeof(len)) {
1937
        virReportSystemError(errno,
1938 1939
                             _("failed to read metadata length in '%s'"),
                             path);
1940
        goto cleanup;
1941 1942
    }
    if (len < 1 || len > 8192) {
1943 1944
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("length of metadata out of range"));
1945
        goto cleanup;
1946
    }
1947
    if (VIR_ALLOC_N(xml, len+1) < 0) {
1948
        virReportOOMError();
1949
        goto cleanup;
1950
    }
1951
    if (saferead(fd, xml, len) != len) {
1952
        virReportSystemError(errno,
1953
                             _("incomplete metdata in '%s'"), path);
1954
        goto cleanup;
1955 1956
    }
    xml[len] = '\0';
1957

1958 1959
    def = virDomainDefParseString(xml, privconn->caps, privconn->xmlopt,
                                  1 << VIR_DOMAIN_VIRT_TEST,
1960
                                  VIR_DOMAIN_XML_INACTIVE);
1961
    if (!def)
1962
        goto cleanup;
1963

1964
    if (testDomainGenerateIfnames(def) < 0)
1965
        goto cleanup;
1966
    if (!(dom = virDomainObjListAdd(privconn->domains,
1967
                                    def,
1968
                                    privconn->xmlopt,
1969 1970 1971
                                    VIR_DOMAIN_OBJ_LIST_ADD_LIVE |
                                    VIR_DOMAIN_OBJ_LIST_ADD_CHECK_LIVE,
                                    NULL)))
1972 1973
        goto cleanup;
    def = NULL;
1974

J
Jiri Denemark 已提交
1975
    if (testDomainStartState(conn, dom, VIR_DOMAIN_RUNNING_RESTORED) < 0)
1976 1977
        goto cleanup;

1978 1979 1980
    event = virDomainEventNewFromObj(dom,
                                     VIR_DOMAIN_EVENT_STARTED,
                                     VIR_DOMAIN_EVENT_STARTED_RESTORED);
1981
    ret = 0;
1982 1983 1984 1985

cleanup:
    virDomainDefFree(def);
    VIR_FREE(xml);
1986
    VIR_FORCE_CLOSE(fd);
1987
    if (dom)
1988
        virObjectUnlock(dom);
1989 1990
    if (event)
        testDomainEventQueue(privconn, event);
1991
    testDriverUnlock(privconn);
1992
    return ret;
1993 1994
}

1995 1996 1997 1998 1999 2000 2001
static int
testDomainRestore(virConnectPtr conn,
                  const char *path)
{
    return testDomainRestoreFlags(conn, path, NULL, 0);
}

2002 2003
static int testDomainCoreDump(virDomainPtr domain,
                              const char *to,
E
Eric Blake 已提交
2004
                              unsigned int flags)
2005
{
2006
    testConnPtr privconn = domain->conn->privateData;
2007
    int fd = -1;
2008
    virDomainObjPtr privdom;
2009
    virDomainEventPtr event = NULL;
2010
    int ret = -1;
2011

E
Eric Blake 已提交
2012 2013
    virCheckFlags(VIR_DUMP_CRASH, -1);

2014
    testDriverLock(privconn);
2015 2016
    privdom = virDomainObjListFindByName(privconn->domains,
                                         domain->name);
2017 2018

    if (privdom == NULL) {
2019
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2020
        goto cleanup;
2021
    }
2022 2023

    if ((fd = open(to, O_CREAT|O_TRUNC|O_WRONLY, S_IRUSR|S_IWUSR)) < 0) {
2024
        virReportSystemError(errno,
2025 2026
                             _("domain '%s' coredump: failed to open %s"),
                             domain->name, to);
2027
        goto cleanup;
2028
    }
2029
    if (safewrite(fd, TEST_SAVE_MAGIC, sizeof(TEST_SAVE_MAGIC)) < 0) {
2030
        virReportSystemError(errno,
2031 2032
                             _("domain '%s' coredump: failed to write header to %s"),
                             domain->name, to);
2033
        goto cleanup;
2034
    }
2035
    if (VIR_CLOSE(fd) < 0) {
2036
        virReportSystemError(errno,
2037 2038
                             _("domain '%s' coredump: write failed: %s"),
                             domain->name, to);
2039
        goto cleanup;
2040
    }
2041

2042
    if (flags & VIR_DUMP_CRASH) {
J
Jiri Denemark 已提交
2043
        testDomainShutdownState(domain, privdom, VIR_DOMAIN_SHUTOFF_CRASHED);
2044 2045 2046 2047
        event = virDomainEventNewFromObj(privdom,
                                         VIR_DOMAIN_EVENT_STOPPED,
                                         VIR_DOMAIN_EVENT_STOPPED_CRASHED);
        if (!privdom->persistent) {
2048 2049
            virDomainObjListRemove(privconn->domains,
                                   privdom);
2050 2051
            privdom = NULL;
        }
2052
    }
2053

2054
    ret = 0;
2055
cleanup:
2056
    VIR_FORCE_CLOSE(fd);
2057
    if (privdom)
2058
        virObjectUnlock(privdom);
2059 2060
    if (event)
        testDomainEventQueue(privconn, event);
2061
    testDriverUnlock(privconn);
2062
    return ret;
2063 2064
}

2065
static char *testDomainGetOSType(virDomainPtr dom ATTRIBUTE_UNUSED) {
2066 2067 2068
    char *ret;

    ignore_value(VIR_STRDUP(ret, "linux"));
2069
    return ret;
2070 2071
}

2072
static unsigned long long testDomainGetMaxMemory(virDomainPtr domain) {
2073 2074
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
2075
    unsigned long long ret = 0;
2076

2077
    testDriverLock(privconn);
2078 2079
    privdom = virDomainObjListFindByName(privconn->domains,
                                         domain->name);
2080
    testDriverUnlock(privconn);
2081 2082

    if (privdom == NULL) {
2083
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2084
        goto cleanup;
2085
    }
2086

2087
    ret = privdom->def->mem.max_balloon;
2088 2089

cleanup:
2090
    if (privdom)
2091
        virObjectUnlock(privdom);
2092
    return ret;
2093 2094
}

2095 2096
static int testDomainSetMaxMemory(virDomainPtr domain,
                                  unsigned long memory)
2097
{
2098 2099
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
2100
    int ret = -1;
2101

2102
    testDriverLock(privconn);
2103 2104
    privdom = virDomainObjListFindByName(privconn->domains,
                                         domain->name);
2105
    testDriverUnlock(privconn);
2106 2107

    if (privdom == NULL) {
2108
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2109
        goto cleanup;
2110
    }
2111 2112

    /* XXX validate not over host memory wrt to other domains */
2113
    privdom->def->mem.max_balloon = memory;
2114 2115 2116
    ret = 0;

cleanup:
2117
    if (privdom)
2118
        virObjectUnlock(privdom);
2119
    return ret;
2120 2121
}

2122 2123
static int testDomainSetMemory(virDomainPtr domain,
                               unsigned long memory)
2124
{
2125 2126
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
2127
    int ret = -1;
2128

2129
    testDriverLock(privconn);
2130 2131
    privdom = virDomainObjListFindByName(privconn->domains,
                                         domain->name);
2132
    testDriverUnlock(privconn);
2133 2134

    if (privdom == NULL) {
2135
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2136
        goto cleanup;
2137
    }
2138

2139
    if (memory > privdom->def->mem.max_balloon) {
2140
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2141
        goto cleanup;
2142
    }
2143

2144
    privdom->def->mem.cur_balloon = memory;
2145 2146 2147
    ret = 0;

cleanup:
2148
    if (privdom)
2149
        virObjectUnlock(privdom);
2150
    return ret;
2151 2152
}

2153 2154
static int
testDomainGetVcpusFlags(virDomainPtr domain, unsigned int flags)
C
Cole Robinson 已提交
2155
{
2156 2157 2158 2159 2160
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr vm;
    virDomainDefPtr def;
    int ret = -1;

2161 2162
    virCheckFlags(VIR_DOMAIN_AFFECT_LIVE |
                  VIR_DOMAIN_AFFECT_CONFIG |
2163 2164 2165
                  VIR_DOMAIN_VCPU_MAXIMUM, -1);

    testDriverLock(privconn);
2166
    vm = virDomainObjListFindByUUID(privconn->domains, domain->uuid);
2167 2168 2169 2170 2171
    testDriverUnlock(privconn);

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

2177
    if (virDomainLiveConfigHelperMethod(privconn->caps, privconn->xmlopt,
2178
                                        vm, &flags, &def) < 0)
2179
        goto cleanup;
2180

2181
    if (flags & VIR_DOMAIN_AFFECT_LIVE)
2182 2183 2184 2185 2186 2187
        def = vm->def;

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

cleanup:
    if (vm)
2188
        virObjectUnlock(vm);
2189
    return ret;
C
Cole Robinson 已提交
2190 2191
}

2192 2193 2194
static int
testDomainGetMaxVcpus(virDomainPtr domain)
{
2195
    return testDomainGetVcpusFlags(domain, (VIR_DOMAIN_AFFECT_LIVE |
2196 2197 2198 2199 2200 2201 2202
                                            VIR_DOMAIN_VCPU_MAXIMUM));
}

static int
testDomainSetVcpusFlags(virDomainPtr domain, unsigned int nrCpus,
                        unsigned int flags)
{
2203
    testConnPtr privconn = domain->conn->privateData;
2204
    virDomainObjPtr privdom = NULL;
2205
    virDomainDefPtr persistentDef;
C
Cole Robinson 已提交
2206 2207
    int ret = -1, maxvcpus;

2208 2209
    virCheckFlags(VIR_DOMAIN_AFFECT_LIVE |
                  VIR_DOMAIN_AFFECT_CONFIG |
2210 2211 2212 2213
                  VIR_DOMAIN_VCPU_MAXIMUM, -1);

    /* At least one of LIVE or CONFIG must be set.  MAXIMUM cannot be
     * mixed with LIVE.  */
2214 2215 2216
    if ((flags & (VIR_DOMAIN_AFFECT_LIVE | VIR_DOMAIN_AFFECT_CONFIG)) == 0 ||
        (flags & (VIR_DOMAIN_VCPU_MAXIMUM | VIR_DOMAIN_AFFECT_LIVE)) ==
         (VIR_DOMAIN_VCPU_MAXIMUM | VIR_DOMAIN_AFFECT_LIVE)) {
2217 2218
        virReportError(VIR_ERR_INVALID_ARG,
                       _("invalid flag combination: (0x%x)"), flags);
2219 2220
        return -1;
    }
2221
    if (!nrCpus || (maxvcpus = testConnectGetMaxVcpus(domain->conn, NULL)) < nrCpus) {
2222 2223
        virReportError(VIR_ERR_INVALID_ARG,
                       _("argument out of range: %d"), nrCpus);
2224 2225
        return -1;
    }
2226

2227
    testDriverLock(privconn);
2228
    privdom = virDomainObjListFindByUUID(privconn->domains, domain->uuid);
2229 2230 2231
    testDriverUnlock(privconn);

    if (privdom == NULL) {
2232
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2233
        goto cleanup;
2234
    }
2235

2236
    if (!virDomainObjIsActive(privdom) && (flags & VIR_DOMAIN_AFFECT_LIVE)) {
2237 2238
        virReportError(VIR_ERR_OPERATION_INVALID,
                       "%s", _("cannot hotplug vcpus for an inactive domain"));
C
Cole Robinson 已提交
2239 2240 2241
        goto cleanup;
    }

2242 2243
    /* We allow more cpus in guest than host, but not more than the
     * domain's starting limit.  */
C
Cole Robinson 已提交
2244 2245
    if (!(flags & (VIR_DOMAIN_VCPU_MAXIMUM)) &&
        privdom->def->maxvcpus < maxvcpus)
2246
        maxvcpus = privdom->def->maxvcpus;
C
Cole Robinson 已提交
2247

C
Cole Robinson 已提交
2248
    if (nrCpus > maxvcpus) {
2249 2250 2251
        virReportError(VIR_ERR_INVALID_ARG,
                       _("requested cpu amount exceeds maximum (%d > %d)"),
                       nrCpus, maxvcpus);
2252
        goto cleanup;
2253
    }
2254

2255
    if (!(persistentDef = virDomainObjGetPersistentDef(privconn->caps,
2256
                                                       privconn->xmlopt,
2257 2258 2259
                                                       privdom)))
        goto cleanup;

2260
    switch (flags) {
2261
    case VIR_DOMAIN_VCPU_MAXIMUM | VIR_DOMAIN_AFFECT_CONFIG:
2262 2263 2264
        persistentDef->maxvcpus = nrCpus;
        if (nrCpus < persistentDef->vcpus)
            persistentDef->vcpus = nrCpus;
2265 2266
        ret = 0;
        break;
2267

2268
    case VIR_DOMAIN_AFFECT_CONFIG:
2269
        persistentDef->vcpus = nrCpus;
2270 2271 2272
        ret = 0;
        break;

2273
    case VIR_DOMAIN_AFFECT_LIVE:
2274 2275 2276
        ret = testDomainUpdateVCPUs(domain->conn, privdom, nrCpus, 0);
        break;

2277
    case VIR_DOMAIN_AFFECT_LIVE | VIR_DOMAIN_AFFECT_CONFIG:
2278
        ret = testDomainUpdateVCPUs(domain->conn, privdom, nrCpus, 0);
2279 2280 2281
        if (ret == 0) {
            persistentDef->vcpus = nrCpus;
        }
2282 2283
        break;
    }
2284 2285

cleanup:
2286
    if (privdom)
2287
        virObjectUnlock(privdom);
2288
    return ret;
2289 2290
}

2291
static int
2292
testDomainSetVcpus(virDomainPtr domain, unsigned int nrCpus)
2293
{
2294
    return testDomainSetVcpusFlags(domain, nrCpus, VIR_DOMAIN_AFFECT_LIVE);
2295 2296
}

C
Cole Robinson 已提交
2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311
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);
2312
    privdom = virDomainObjListFindByName(privconn->domains, domain->name);
C
Cole Robinson 已提交
2313 2314 2315
    testDriverUnlock(privconn);

    if (privdom == NULL) {
2316
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
C
Cole Robinson 已提交
2317 2318 2319 2320
        goto cleanup;
    }

    if (!virDomainObjIsActive(privdom)) {
2321 2322
        virReportError(VIR_ERR_OPERATION_INVALID,
                       "%s",_("cannot list vcpus for an inactive domain"));
C
Cole Robinson 已提交
2323 2324 2325 2326 2327 2328
        goto cleanup;
    }

    privdomdata = privdom->privateData;

    if (gettimeofday(&tv, NULL) < 0) {
2329
        virReportSystemError(errno,
C
Cole Robinson 已提交
2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349
                             "%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);

2350
        for (i = 0; i < maxinfo; i++) {
C
Cole Robinson 已提交
2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366
            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);

2367
        for (v = 0; v < maxinfo; v++) {
C
Cole Robinson 已提交
2368 2369
            unsigned char *cpumap = VIR_GET_CPUMAP(cpumaps, maplen, v);

2370
            for (i = 0; i < maxcpu; i++) {
C
Cole Robinson 已提交
2371 2372 2373 2374 2375 2376 2377 2378 2379 2380
                if (VIR_CPU_USABLE(privdomdata->cpumaps, privmaplen, v, i)) {
                    VIR_USE_CPU(cpumap, i);
                }
            }
        }
    }

    ret = maxinfo;
cleanup:
    if (privdom)
2381
        virObjectUnlock(privdom);
C
Cole Robinson 已提交
2382 2383 2384
    return ret;
}

C
Cole Robinson 已提交
2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397
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);
2398
    privdom = virDomainObjListFindByName(privconn->domains, domain->name);
C
Cole Robinson 已提交
2399 2400 2401
    testDriverUnlock(privconn);

    if (privdom == NULL) {
2402
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
C
Cole Robinson 已提交
2403 2404 2405 2406
        goto cleanup;
    }

    if (!virDomainObjIsActive(privdom)) {
2407 2408
        virReportError(VIR_ERR_OPERATION_INVALID,
                       "%s",_("cannot pin vcpus on an inactive domain"));
C
Cole Robinson 已提交
2409 2410 2411 2412
        goto cleanup;
    }

    if (vcpu > privdom->def->vcpus) {
2413 2414
        virReportError(VIR_ERR_INVALID_ARG, "%s",
                       _("requested vcpu is higher than allocated vcpus"));
C
Cole Robinson 已提交
2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428
        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);

2429
    for (i = 0; i < maxcpu; i++) {
C
Cole Robinson 已提交
2430 2431 2432 2433 2434 2435 2436 2437
        if (VIR_CPU_USABLE(cpumap, maplen, 0, i)) {
            VIR_USE_CPU(privcpumap, i);
        }
    }

    ret = 0;
cleanup:
    if (privdom)
2438
        virObjectUnlock(privdom);
C
Cole Robinson 已提交
2439 2440 2441
    return ret;
}

2442
static char *testDomainGetXMLDesc(virDomainPtr domain, unsigned int flags)
2443
{
2444
    testConnPtr privconn = domain->conn->privateData;
2445
    virDomainDefPtr def;
2446
    virDomainObjPtr privdom;
2447 2448
    char *ret = NULL;

2449 2450
    /* Flags checked by virDomainDefFormat */

2451
    testDriverLock(privconn);
2452 2453
    privdom = virDomainObjListFindByName(privconn->domains,
                                         domain->name);
2454 2455 2456
    testDriverUnlock(privconn);

    if (privdom == NULL) {
2457
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2458
        goto cleanup;
2459
    }
2460

2461 2462
    def = (flags & VIR_DOMAIN_XML_INACTIVE) &&
        privdom->newDef ? privdom->newDef : privdom->def;
2463

2464
    ret = virDomainDefFormat(def,
2465 2466 2467
                             flags);

cleanup:
2468
    if (privdom)
2469
        virObjectUnlock(privdom);
2470
    return ret;
2471
}
2472

2473
static int testConnectNumOfDefinedDomains(virConnectPtr conn) {
2474
    testConnPtr privconn = conn->privateData;
2475
    int count;
2476

2477
    testDriverLock(privconn);
2478
    count = virDomainObjListNumOfDomains(privconn->domains, 0);
2479
    testDriverUnlock(privconn);
2480

2481
    return count;
2482 2483
}

2484 2485 2486
static int testConnectListDefinedDomains(virConnectPtr conn,
                                         char **const names,
                                         int maxnames) {
2487

2488
    testConnPtr privconn = conn->privateData;
2489
    int n;
2490

2491
    testDriverLock(privconn);
2492
    memset(names, 0, sizeof(*names)*maxnames);
2493
    n = virDomainObjListGetInactiveNames(privconn->domains, names, maxnames);
2494
    testDriverUnlock(privconn);
2495

2496
    return n;
2497 2498
}

2499
static virDomainPtr testDomainDefineXML(virConnectPtr conn,
2500
                                        const char *xml) {
2501
    testConnPtr privconn = conn->privateData;
2502
    virDomainPtr ret = NULL;
2503
    virDomainDefPtr def;
2504
    virDomainObjPtr dom = NULL;
2505
    virDomainEventPtr event = NULL;
2506
    virDomainDefPtr oldDef = NULL;
2507

2508
    testDriverLock(privconn);
2509 2510
    if ((def = virDomainDefParseString(xml, privconn->caps, privconn->xmlopt,
                                       1 << VIR_DOMAIN_VIRT_TEST,
2511
                                       VIR_DOMAIN_XML_INACTIVE)) == NULL)
2512
        goto cleanup;
2513

2514
    if (testDomainGenerateIfnames(def) < 0)
2515
        goto cleanup;
2516
    if (!(dom = virDomainObjListAdd(privconn->domains,
2517
                                    def,
2518
                                    privconn->xmlopt,
2519 2520
                                    0,
                                    &oldDef)))
2521
        goto cleanup;
2522
    def = NULL;
2523
    dom->persistent = 1;
2524

2525 2526
    event = virDomainEventNewFromObj(dom,
                                     VIR_DOMAIN_EVENT_DEFINED,
2527
                                     !oldDef ?
2528 2529
                                     VIR_DOMAIN_EVENT_DEFINED_ADDED :
                                     VIR_DOMAIN_EVENT_DEFINED_UPDATED);
2530

2531
    ret = virGetDomain(conn, dom->def->name, dom->def->uuid);
2532
    if (ret)
2533
        ret->id = dom->def->id;
2534 2535 2536

cleanup:
    virDomainDefFree(def);
2537
    virDomainDefFree(oldDef);
2538
    if (dom)
2539
        virObjectUnlock(dom);
2540 2541
    if (event)
        testDomainEventQueue(privconn, event);
2542
    testDriverUnlock(privconn);
2543
    return ret;
2544 2545
}

2546 2547 2548
static int testNodeGetCellsFreeMemory(virConnectPtr conn,
                                      unsigned long long *freemems,
                                      int startCell, int maxCells) {
2549
    testConnPtr privconn = conn->privateData;
2550
    int i, j;
2551
    int ret = -1;
2552

2553
    testDriverLock(privconn);
2554
    if (startCell > privconn->numCells) {
2555 2556
        virReportError(VIR_ERR_INVALID_ARG,
                       "%s", _("Range exceeds available cells"));
2557
        goto cleanup;
2558 2559 2560
    }

    for (i = startCell, j = 0;
2561
         (i < privconn->numCells && j < maxCells);
2562 2563 2564
         ++i, ++j) {
        freemems[j] = privconn->cells[i].mem;
    }
2565
    ret = j;
2566

2567
cleanup:
2568
    testDriverUnlock(privconn);
2569
    return ret;
2570 2571 2572
}


2573
static int testDomainCreateWithFlags(virDomainPtr domain, unsigned int flags) {
2574 2575
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
2576
    virDomainEventPtr event = NULL;
2577
    int ret = -1;
2578

2579 2580
    virCheckFlags(0, -1);

2581
    testDriverLock(privconn);
2582 2583
    privdom = virDomainObjListFindByName(privconn->domains,
                                         domain->name);
2584 2585

    if (privdom == NULL) {
2586
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2587
        goto cleanup;
2588
    }
2589

J
Jiri Denemark 已提交
2590
    if (virDomainObjGetState(privdom, NULL) != VIR_DOMAIN_SHUTOFF) {
2591 2592
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Domain '%s' is already running"), domain->name);
2593
        goto cleanup;
2594 2595
    }

J
Jiri Denemark 已提交
2596 2597
    if (testDomainStartState(domain->conn, privdom,
                             VIR_DOMAIN_RUNNING_BOOTED) < 0)
2598 2599 2600
        goto cleanup;
    domain->id = privdom->def->id;

2601 2602 2603
    event = virDomainEventNewFromObj(privdom,
                                     VIR_DOMAIN_EVENT_STARTED,
                                     VIR_DOMAIN_EVENT_STARTED_BOOTED);
2604
    ret = 0;
2605

2606
cleanup:
2607
    if (privdom)
2608
        virObjectUnlock(privdom);
2609 2610
    if (event)
        testDomainEventQueue(privconn, event);
2611
    testDriverUnlock(privconn);
2612
    return ret;
2613 2614
}

2615 2616 2617 2618
static int testDomainCreate(virDomainPtr domain) {
    return testDomainCreateWithFlags(domain, 0);
}

2619 2620 2621
static int testDomainUndefineFlags(virDomainPtr domain,
                                   unsigned int flags)
{
2622 2623
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
2624
    virDomainEventPtr event = NULL;
2625
    int ret = -1;
2626

2627 2628
    virCheckFlags(0, -1);

2629
    testDriverLock(privconn);
2630 2631
    privdom = virDomainObjListFindByName(privconn->domains,
                                         domain->name);
2632 2633

    if (privdom == NULL) {
2634
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2635
        goto cleanup;
2636
    }
2637

2638 2639 2640
    event = virDomainEventNewFromObj(privdom,
                                     VIR_DOMAIN_EVENT_UNDEFINED,
                                     VIR_DOMAIN_EVENT_UNDEFINED_REMOVED);
2641 2642
    if (virDomainObjIsActive(privdom)) {
        privdom->persistent = 0;
2643
    } else {
2644 2645
        virDomainObjListRemove(privconn->domains,
                               privdom);
2646 2647 2648
        privdom = NULL;
    }

2649
    ret = 0;
2650

2651
cleanup:
2652
    if (privdom)
2653
        virObjectUnlock(privdom);
2654 2655
    if (event)
        testDomainEventQueue(privconn, event);
2656
    testDriverUnlock(privconn);
2657
    return ret;
2658 2659
}

2660 2661 2662 2663 2664
static int testDomainUndefine(virDomainPtr domain)
{
    return testDomainUndefineFlags(domain, 0);
}

2665 2666 2667
static int testDomainGetAutostart(virDomainPtr domain,
                                  int *autostart)
{
2668 2669
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
2670
    int ret = -1;
2671

2672
    testDriverLock(privconn);
2673 2674
    privdom = virDomainObjListFindByName(privconn->domains,
                                         domain->name);
2675
    testDriverUnlock(privconn);
2676 2677

    if (privdom == NULL) {
2678
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2679
        goto cleanup;
2680 2681
    }

2682
    *autostart = privdom->autostart;
2683 2684 2685
    ret = 0;

cleanup:
2686
    if (privdom)
2687
        virObjectUnlock(privdom);
2688
    return ret;
2689 2690 2691 2692 2693 2694
}


static int testDomainSetAutostart(virDomainPtr domain,
                                  int autostart)
{
2695 2696
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
2697
    int ret = -1;
2698

2699
    testDriverLock(privconn);
2700 2701
    privdom = virDomainObjListFindByName(privconn->domains,
                                         domain->name);
2702
    testDriverUnlock(privconn);
2703 2704

    if (privdom == NULL) {
2705
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2706
        goto cleanup;
2707 2708
    }

2709
    privdom->autostart = autostart ? 1 : 0;
2710 2711 2712
    ret = 0;

cleanup:
2713
    if (privdom)
2714
        virObjectUnlock(privdom);
2715
    return ret;
2716
}
2717

2718
static char *testDomainGetSchedulerType(virDomainPtr domain ATTRIBUTE_UNUSED,
2719 2720
                                        int *nparams)
{
2721 2722
    char *type = NULL;

2723 2724 2725
    if (nparams)
        *nparams = 1;

2726
    ignore_value(VIR_STRDUP(type, "fair"));
2727

2728 2729 2730
    return type;
}

2731
static int
2732 2733 2734 2735
testDomainGetSchedulerParametersFlags(virDomainPtr domain,
                                      virTypedParameterPtr params,
                                      int *nparams,
                                      unsigned int flags)
2736
{
2737 2738
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
2739
    int ret = -1;
2740

2741 2742
    virCheckFlags(0, -1);

2743
    testDriverLock(privconn);
2744 2745
    privdom = virDomainObjListFindByName(privconn->domains,
                                         domain->name);
2746
    testDriverUnlock(privconn);
2747 2748

    if (privdom == NULL) {
2749
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2750
        goto cleanup;
2751 2752
    }

2753 2754
    if (virTypedParameterAssign(params, VIR_DOMAIN_SCHEDULER_WEIGHT,
                                VIR_TYPED_PARAM_UINT, 50) < 0)
2755
        goto cleanup;
2756 2757
    /* XXX */
    /*params[0].value.ui = privdom->weight;*/
2758 2759

    *nparams = 1;
2760 2761 2762
    ret = 0;

cleanup:
2763
    if (privdom)
2764
        virObjectUnlock(privdom);
2765
    return ret;
2766
}
2767

2768
static int
2769 2770 2771
testDomainGetSchedulerParameters(virDomainPtr domain,
                                 virTypedParameterPtr params,
                                 int *nparams)
2772
{
2773
    return testDomainGetSchedulerParametersFlags(domain, params, nparams, 0);
2774
}
2775

2776
static int
2777 2778 2779 2780
testDomainSetSchedulerParametersFlags(virDomainPtr domain,
                                      virTypedParameterPtr params,
                                      int nparams,
                                      unsigned int flags)
2781
{
2782 2783
    testConnPtr privconn = domain->conn->privateData;
    virDomainObjPtr privdom;
2784
    int ret = -1, i;
2785

2786
    virCheckFlags(0, -1);
2787 2788 2789 2790 2791
    if (virTypedParameterArrayValidate(params, nparams,
                                       VIR_DOMAIN_SCHEDULER_WEIGHT,
                                       VIR_TYPED_PARAM_UINT,
                                       NULL) < 0)
        return -1;
2792

2793
    testDriverLock(privconn);
2794 2795
    privdom = virDomainObjListFindByName(privconn->domains,
                                         domain->name);
2796
    testDriverUnlock(privconn);
2797 2798

    if (privdom == NULL) {
2799
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2800
        goto cleanup;
2801 2802
    }

2803
    for (i = 0; i < nparams; i++) {
2804 2805 2806
        if (STREQ(params[i].field, VIR_DOMAIN_SCHEDULER_WEIGHT)) {
            /* XXX */
            /*privdom->weight = params[i].value.ui;*/
2807
        }
2808
    }
2809

2810 2811 2812
    ret = 0;

cleanup:
2813
    if (privdom)
2814
        virObjectUnlock(privdom);
2815
    return ret;
2816 2817
}

2818
static int
2819 2820 2821
testDomainSetSchedulerParameters(virDomainPtr domain,
                                 virTypedParameterPtr params,
                                 int nparams)
2822
{
2823
    return testDomainSetSchedulerParametersFlags(domain, params, nparams, 0);
2824 2825
}

2826 2827 2828 2829 2830 2831 2832 2833
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;
2834
    int ret = -1;
2835 2836

    testDriverLock(privconn);
2837 2838
    privdom = virDomainObjListFindByName(privconn->domains,
                                         domain->name);
2839 2840 2841
    testDriverUnlock(privconn);

    if (privdom == NULL) {
2842
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2843 2844 2845
        goto error;
    }

2846
    if (virDomainDiskIndexByName(privdom->def, path, false) < 0) {
2847 2848
        virReportError(VIR_ERR_INVALID_ARG,
                       _("invalid path: %s"), path);
2849 2850 2851 2852
        goto error;
    }

    if (gettimeofday(&tv, NULL) < 0) {
2853
        virReportSystemError(errno,
2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868
                             "%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)
2869
        virObjectUnlock(privdom);
2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883
    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);
2884 2885
    privdom = virDomainObjListFindByName(privconn->domains,
                                         domain->name);
2886 2887 2888
    testDriverUnlock(privconn);

    if (privdom == NULL) {
2889
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
2890 2891 2892
        goto error;
    }

2893
    for (i = 0; i < privdom->def->nnets; i++) {
2894
        if (privdom->def->nets[i]->ifname &&
2895
            STREQ(privdom->def->nets[i]->ifname, path)) {
2896 2897 2898 2899 2900 2901
            found = 1;
            break;
        }
    }

    if (!found) {
2902 2903
        virReportError(VIR_ERR_INVALID_ARG,
                       _("invalid path, '%s' is not a known interface"), path);
2904 2905 2906 2907
        goto error;
    }

    if (gettimeofday(&tv, NULL) < 0) {
2908
        virReportSystemError(errno,
2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926
                             "%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)
2927
        virObjectUnlock(privdom);
2928 2929 2930
    return ret;
}

2931
static virDrvOpenStatus testNetworkOpen(virConnectPtr conn,
2932
                                        virConnectAuthPtr auth ATTRIBUTE_UNUSED,
E
Eric Blake 已提交
2933 2934 2935 2936
                                        unsigned int flags)
{
    virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);

2937 2938 2939 2940 2941 2942 2943
    if (STRNEQ(conn->driver->name, "Test"))
        return VIR_DRV_OPEN_DECLINED;

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

2944
static int testNetworkClose(virConnectPtr conn) {
2945 2946 2947 2948 2949
    conn->networkPrivateData = NULL;
    return 0;
}


2950 2951
static virNetworkPtr testNetworkLookupByUUID(virConnectPtr conn,
                                             const unsigned char *uuid)
2952
{
2953 2954
    testConnPtr privconn = conn->privateData;
    virNetworkObjPtr net;
2955
    virNetworkPtr ret = NULL;
2956

2957 2958 2959 2960 2961
    testDriverLock(privconn);
    net = virNetworkFindByUUID(&privconn->networks, uuid);
    testDriverUnlock(privconn);

    if (net == NULL) {
2962
        virReportError(VIR_ERR_NO_NETWORK, NULL);
2963
        goto cleanup;
2964 2965
    }

2966 2967 2968
    ret = virGetNetwork(conn, net->def->name, net->def->uuid);

cleanup:
2969 2970
    if (net)
        virNetworkObjUnlock(net);
2971
    return ret;
2972
}
2973

2974
static virNetworkPtr testNetworkLookupByName(virConnectPtr conn,
2975
                                             const char *name)
2976
{
2977
    testConnPtr privconn = conn->privateData;
2978 2979
    virNetworkObjPtr net;
    virNetworkPtr ret = NULL;
2980

2981 2982 2983 2984 2985
    testDriverLock(privconn);
    net = virNetworkFindByName(&privconn->networks, name);
    testDriverUnlock(privconn);

    if (net == NULL) {
2986
        virReportError(VIR_ERR_NO_NETWORK, NULL);
2987
        goto cleanup;
2988 2989
    }

2990 2991 2992
    ret = virGetNetwork(conn, net->def->name, net->def->uuid);

cleanup:
2993 2994
    if (net)
        virNetworkObjUnlock(net);
2995
    return ret;
2996 2997 2998
}


2999
static int testConnectNumOfNetworks(virConnectPtr conn) {
3000
    testConnPtr privconn = conn->privateData;
3001
    int numActive = 0, i;
3002

3003
    testDriverLock(privconn);
3004
    for (i = 0; i < privconn->networks.count; i++) {
3005
        virNetworkObjLock(privconn->networks.objs[i]);
D
Daniel P. Berrange 已提交
3006
        if (virNetworkObjIsActive(privconn->networks.objs[i]))
3007
            numActive++;
3008 3009 3010
        virNetworkObjUnlock(privconn->networks.objs[i]);
    }
    testDriverUnlock(privconn);
3011

3012
    return numActive;
3013 3014
}

3015
static int testConnectListNetworks(virConnectPtr conn, char **const names, int nnames) {
3016
    testConnPtr privconn = conn->privateData;
3017
    int n = 0, i;
3018

3019
    testDriverLock(privconn);
3020
    memset(names, 0, sizeof(*names)*nnames);
3021
    for (i = 0; i < privconn->networks.count && n < nnames; i++) {
3022
        virNetworkObjLock(privconn->networks.objs[i]);
D
Daniel P. Berrange 已提交
3023
        if (virNetworkObjIsActive(privconn->networks.objs[i]) &&
3024
            VIR_STRDUP(names[n++], privconn->networks.objs[i]->def->name) < 0) {
3025
            virNetworkObjUnlock(privconn->networks.objs[i]);
3026
            goto error;
3027 3028 3029 3030
        }
        virNetworkObjUnlock(privconn->networks.objs[i]);
    }
    testDriverUnlock(privconn);
3031

3032 3033
    return n;

3034
error:
3035
    for (n = 0; n < nnames; n++)
3036
        VIR_FREE(names[n]);
3037
    testDriverUnlock(privconn);
3038
    return -1;
3039 3040
}

3041
static int testConnectNumOfDefinedNetworks(virConnectPtr conn) {
3042
    testConnPtr privconn = conn->privateData;
3043
    int numInactive = 0, i;
3044

3045
    testDriverLock(privconn);
3046
    for (i = 0; i < privconn->networks.count; i++) {
3047
        virNetworkObjLock(privconn->networks.objs[i]);
D
Daniel P. Berrange 已提交
3048
        if (!virNetworkObjIsActive(privconn->networks.objs[i]))
3049
            numInactive++;
3050 3051 3052
        virNetworkObjUnlock(privconn->networks.objs[i]);
    }
    testDriverUnlock(privconn);
3053

3054
    return numInactive;
3055 3056
}

3057
static int testConnectListDefinedNetworks(virConnectPtr conn, char **const names, int nnames) {
3058
    testConnPtr privconn = conn->privateData;
3059
    int n = 0, i;
3060

3061
    testDriverLock(privconn);
3062
    memset(names, 0, sizeof(*names)*nnames);
3063
    for (i = 0; i < privconn->networks.count && n < nnames; i++) {
3064
        virNetworkObjLock(privconn->networks.objs[i]);
D
Daniel P. Berrange 已提交
3065
        if (!virNetworkObjIsActive(privconn->networks.objs[i]) &&
3066
            VIR_STRDUP(names[n++], privconn->networks.objs[i]->def->name) < 0) {
3067
            virNetworkObjUnlock(privconn->networks.objs[i]);
3068
            goto error;
3069 3070 3071 3072
        }
        virNetworkObjUnlock(privconn->networks.objs[i]);
    }
    testDriverUnlock(privconn);
3073

3074 3075
    return n;

3076
error:
3077
    for (n = 0; n < nnames; n++)
3078
        VIR_FREE(names[n]);
3079
    testDriverUnlock(privconn);
3080
    return -1;
3081 3082
}

3083
static int
3084
testConnectListAllNetworks(virConnectPtr conn,
3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098
                           virNetworkPtr **nets,
                           unsigned int flags)
{
    testConnPtr privconn = conn->privateData;
    int ret = -1;

    virCheckFlags(VIR_CONNECT_LIST_NETWORKS_FILTERS_ALL, -1);

    testDriverLock(privconn);
    ret = virNetworkList(conn, privconn->networks, nets, flags);
    testDriverUnlock(privconn);

    return ret;
}
3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109

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) {
3110
        virReportError(VIR_ERR_NO_NETWORK, NULL);
3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130
        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) {
3131
        virReportError(VIR_ERR_NO_NETWORK, NULL);
3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142
        goto cleanup;
    }
    ret = obj->persistent;

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


3143
static virNetworkPtr testNetworkCreateXML(virConnectPtr conn, const char *xml) {
3144
    testConnPtr privconn = conn->privateData;
3145
    virNetworkDefPtr def;
3146
    virNetworkObjPtr net = NULL;
3147
    virNetworkPtr ret = NULL;
3148

3149
    testDriverLock(privconn);
3150
    if ((def = virNetworkDefParseString(xml)) == NULL)
3151
        goto cleanup;
3152

3153
    if (!(net = virNetworkAssignDef(&privconn->networks, def, false)))
3154 3155
        goto cleanup;
    def = NULL;
3156
    net->active = 1;
3157

3158
    ret = virGetNetwork(conn, net->def->name, net->def->uuid);
3159

3160 3161
cleanup:
    virNetworkDefFree(def);
3162 3163 3164
    if (net)
        virNetworkObjUnlock(net);
    testDriverUnlock(privconn);
3165
    return ret;
3166 3167
}

3168
static
3169
virNetworkPtr testNetworkDefineXML(virConnectPtr conn, const char *xml)
3170
{
3171
    testConnPtr privconn = conn->privateData;
3172
    virNetworkDefPtr def;
3173
    virNetworkObjPtr net = NULL;
3174
    virNetworkPtr ret = NULL;
3175

3176
    testDriverLock(privconn);
3177
    if ((def = virNetworkDefParseString(xml)) == NULL)
3178
        goto cleanup;
3179

3180
    if (!(net = virNetworkAssignDef(&privconn->networks, def, false)))
3181 3182
        goto cleanup;
    def = NULL;
3183
    net->persistent = 1;
3184

3185
    ret = virGetNetwork(conn, net->def->name, net->def->uuid);
3186 3187 3188

cleanup:
    virNetworkDefFree(def);
3189 3190 3191
    if (net)
        virNetworkObjUnlock(net);
    testDriverUnlock(privconn);
3192
    return ret;
3193 3194 3195
}

static int testNetworkUndefine(virNetworkPtr network) {
3196 3197
    testConnPtr privconn = network->conn->privateData;
    virNetworkObjPtr privnet;
3198
    int ret = -1;
3199

3200
    testDriverLock(privconn);
3201 3202 3203 3204
    privnet = virNetworkFindByName(&privconn->networks,
                                   network->name);

    if (privnet == NULL) {
3205
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3206
        goto cleanup;
3207
    }
3208

D
Daniel P. Berrange 已提交
3209
    if (virNetworkObjIsActive(privnet)) {
3210 3211
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("Network '%s' is still running"), network->name);
3212
        goto cleanup;
3213 3214
    }

3215 3216
    virNetworkRemoveInactive(&privconn->networks,
                             privnet);
3217
    privnet = NULL;
3218
    ret = 0;
3219

3220
cleanup:
3221 3222 3223
    if (privnet)
        virNetworkObjUnlock(privnet);
    testDriverUnlock(privconn);
3224
    return ret;
3225 3226
}

3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274
static int
testNetworkUpdate(virNetworkPtr net,
                  unsigned int command,
                  unsigned int section,
                  int parentIndex,
                  const char *xml,
                  unsigned int flags)
{
    testConnPtr privconn = net->conn->privateData;
    virNetworkObjPtr network = NULL;
    int isActive, ret = -1;

    virCheckFlags(VIR_NETWORK_UPDATE_AFFECT_LIVE |
                  VIR_NETWORK_UPDATE_AFFECT_CONFIG,
                  -1);

    testDriverLock(privconn);

    network = virNetworkFindByUUID(&privconn->networks, net->uuid);
    if (!network) {
        virReportError(VIR_ERR_NO_NETWORK,
                       "%s", _("no network with matching uuid"));
        goto cleanup;
    }

    /* VIR_NETWORK_UPDATE_AFFECT_CURRENT means "change LIVE if network
     * is active, else change CONFIG
    */
    isActive = virNetworkObjIsActive(network);
    if ((flags & (VIR_NETWORK_UPDATE_AFFECT_LIVE
                   | VIR_NETWORK_UPDATE_AFFECT_CONFIG)) ==
        VIR_NETWORK_UPDATE_AFFECT_CURRENT) {
        if (isActive)
            flags |= VIR_NETWORK_UPDATE_AFFECT_LIVE;
        else
            flags |= VIR_NETWORK_UPDATE_AFFECT_CONFIG;
    }

    /* update the network config in memory/on disk */
    if (virNetworkObjUpdate(network, command, section, parentIndex, xml, flags) < 0)
       goto cleanup;

    ret = 0;
cleanup:
    testDriverUnlock(privconn);
    return ret;
}

3275
static int testNetworkCreate(virNetworkPtr network) {
3276 3277
    testConnPtr privconn = network->conn->privateData;
    virNetworkObjPtr privnet;
3278
    int ret = -1;
3279

3280
    testDriverLock(privconn);
3281 3282
    privnet = virNetworkFindByName(&privconn->networks,
                                   network->name);
3283
    testDriverUnlock(privconn);
3284 3285

    if (privnet == NULL) {
3286
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3287
        goto cleanup;
3288
    }
3289

D
Daniel P. Berrange 已提交
3290
    if (virNetworkObjIsActive(privnet)) {
3291 3292
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("Network '%s' is already running"), network->name);
3293
        goto cleanup;
3294 3295
    }

3296
    privnet->active = 1;
3297
    ret = 0;
3298

3299
cleanup:
3300 3301
    if (privnet)
        virNetworkObjUnlock(privnet);
3302
    return ret;
3303 3304 3305
}

static int testNetworkDestroy(virNetworkPtr network) {
3306 3307
    testConnPtr privconn = network->conn->privateData;
    virNetworkObjPtr privnet;
3308
    int ret = -1;
3309

3310
    testDriverLock(privconn);
3311 3312 3313 3314
    privnet = virNetworkFindByName(&privconn->networks,
                                   network->name);

    if (privnet == NULL) {
3315
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3316
        goto cleanup;
3317
    }
3318

3319 3320 3321 3322
    privnet->active = 0;
    if (!privnet->persistent) {
        virNetworkRemoveInactive(&privconn->networks,
                                 privnet);
3323
        privnet = NULL;
3324
    }
3325 3326 3327
    ret = 0;

cleanup:
3328 3329 3330
    if (privnet)
        virNetworkObjUnlock(privnet);
    testDriverUnlock(privconn);
3331
    return ret;
3332 3333
}

3334
static char *testNetworkGetXMLDesc(virNetworkPtr network,
E
Eric Blake 已提交
3335
                                   unsigned int flags)
3336
{
3337 3338
    testConnPtr privconn = network->conn->privateData;
    virNetworkObjPtr privnet;
3339
    char *ret = NULL;
3340

E
Eric Blake 已提交
3341 3342
    virCheckFlags(0, NULL);

3343
    testDriverLock(privconn);
3344 3345
    privnet = virNetworkFindByName(&privconn->networks,
                                   network->name);
3346
    testDriverUnlock(privconn);
3347 3348

    if (privnet == NULL) {
3349
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3350
        goto cleanup;
3351
    }
3352

3353
    ret = virNetworkDefFormat(privnet->def, flags);
3354 3355

cleanup:
3356 3357
    if (privnet)
        virNetworkObjUnlock(privnet);
3358
    return ret;
3359 3360 3361
}

static char *testNetworkGetBridgeName(virNetworkPtr network) {
3362
    testConnPtr privconn = network->conn->privateData;
3363
    char *bridge = NULL;
3364 3365
    virNetworkObjPtr privnet;

3366
    testDriverLock(privconn);
3367 3368
    privnet = virNetworkFindByName(&privconn->networks,
                                   network->name);
3369
    testDriverUnlock(privconn);
3370 3371

    if (privnet == NULL) {
3372
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3373
        goto cleanup;
3374 3375
    }

3376
    if (!(privnet->def->bridge)) {
3377 3378 3379
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("network '%s' does not have a bridge name."),
                       privnet->def->name);
3380 3381 3382
        goto cleanup;
    }

3383
    ignore_value(VIR_STRDUP(bridge, privnet->def->bridge));
3384 3385

cleanup:
3386 3387
    if (privnet)
        virNetworkObjUnlock(privnet);
3388 3389 3390 3391 3392
    return bridge;
}

static int testNetworkGetAutostart(virNetworkPtr network,
                                   int *autostart) {
3393 3394
    testConnPtr privconn = network->conn->privateData;
    virNetworkObjPtr privnet;
3395
    int ret = -1;
3396

3397
    testDriverLock(privconn);
3398 3399
    privnet = virNetworkFindByName(&privconn->networks,
                                   network->name);
3400
    testDriverUnlock(privconn);
3401 3402

    if (privnet == NULL) {
3403
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3404
        goto cleanup;
3405 3406
    }

3407
    *autostart = privnet->autostart;
3408 3409 3410
    ret = 0;

cleanup:
3411 3412
    if (privnet)
        virNetworkObjUnlock(privnet);
3413
    return ret;
3414 3415 3416 3417
}

static int testNetworkSetAutostart(virNetworkPtr network,
                                   int autostart) {
3418 3419
    testConnPtr privconn = network->conn->privateData;
    virNetworkObjPtr privnet;
3420
    int ret = -1;
3421

3422
    testDriverLock(privconn);
3423 3424
    privnet = virNetworkFindByName(&privconn->networks,
                                   network->name);
3425
    testDriverUnlock(privconn);
3426 3427

    if (privnet == NULL) {
3428
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
3429
        goto cleanup;
3430 3431
    }

3432
    privnet->autostart = autostart ? 1 : 0;
3433 3434 3435
    ret = 0;

cleanup:
3436 3437
    if (privnet)
        virNetworkObjUnlock(privnet);
3438
    return ret;
3439
}
3440

C
Cole Robinson 已提交
3441

L
Laine Stump 已提交
3442 3443 3444 3445
/*
 * Physical host interface routines
 */

3446
static virDrvOpenStatus testInterfaceOpen(virConnectPtr conn,
L
Laine Stump 已提交
3447
                                          virConnectAuthPtr auth ATTRIBUTE_UNUSED,
E
Eric Blake 已提交
3448
                                          unsigned int flags)
L
Laine Stump 已提交
3449
{
E
Eric Blake 已提交
3450 3451
    virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);

L
Laine Stump 已提交
3452 3453 3454 3455 3456 3457 3458
    if (STRNEQ(conn->driver->name, "Test"))
        return VIR_DRV_OPEN_DECLINED;

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

3459
static int testInterfaceClose(virConnectPtr conn)
L
Laine Stump 已提交
3460 3461 3462 3463 3464 3465
{
    conn->interfacePrivateData = NULL;
    return 0;
}


3466
static int testConnectNumOfInterfaces(virConnectPtr conn)
L
Laine Stump 已提交
3467 3468 3469 3470 3471
{
    testConnPtr privconn = conn->privateData;
    int i, count = 0;

    testDriverLock(privconn);
3472
    for (i = 0; (i < privconn->ifaces.count); i++) {
L
Laine Stump 已提交
3473
        virInterfaceObjLock(privconn->ifaces.objs[i]);
D
Daniel P. Berrange 已提交
3474
        if (virInterfaceObjIsActive(privconn->ifaces.objs[i])) {
L
Laine Stump 已提交
3475 3476 3477 3478 3479 3480 3481 3482
            count++;
        }
        virInterfaceObjUnlock(privconn->ifaces.objs[i]);
    }
    testDriverUnlock(privconn);
    return count;
}

3483
static int testConnectListInterfaces(virConnectPtr conn, char **const names, int nnames)
L
Laine Stump 已提交
3484 3485 3486 3487 3488 3489
{
    testConnPtr privconn = conn->privateData;
    int n = 0, i;

    testDriverLock(privconn);
    memset(names, 0, sizeof(*names)*nnames);
3490
    for (i = 0; (i < privconn->ifaces.count) && (n < nnames); i++) {
L
Laine Stump 已提交
3491
        virInterfaceObjLock(privconn->ifaces.objs[i]);
D
Daniel P. Berrange 已提交
3492
        if (virInterfaceObjIsActive(privconn->ifaces.objs[i])) {
3493
            if (VIR_STRDUP(names[n++], privconn->ifaces.objs[i]->def->name) < 0) {
L
Laine Stump 已提交
3494
                virInterfaceObjUnlock(privconn->ifaces.objs[i]);
3495
                goto error;
L
Laine Stump 已提交
3496 3497 3498 3499 3500 3501 3502 3503
            }
        }
        virInterfaceObjUnlock(privconn->ifaces.objs[i]);
    }
    testDriverUnlock(privconn);

    return n;

3504
error:
3505
    for (n = 0; n < nnames; n++)
L
Laine Stump 已提交
3506 3507 3508 3509 3510
        VIR_FREE(names[n]);
    testDriverUnlock(privconn);
    return -1;
}

3511
static int testConnectNumOfDefinedInterfaces(virConnectPtr conn)
L
Laine Stump 已提交
3512 3513 3514 3515 3516
{
    testConnPtr privconn = conn->privateData;
    int i, count = 0;

    testDriverLock(privconn);
3517
    for (i = 0; i < privconn->ifaces.count; i++) {
L
Laine Stump 已提交
3518
        virInterfaceObjLock(privconn->ifaces.objs[i]);
D
Daniel P. Berrange 已提交
3519
        if (!virInterfaceObjIsActive(privconn->ifaces.objs[i])) {
L
Laine Stump 已提交
3520 3521 3522 3523 3524 3525 3526 3527
            count++;
        }
        virInterfaceObjUnlock(privconn->ifaces.objs[i]);
    }
    testDriverUnlock(privconn);
    return count;
}

3528
static int testConnectListDefinedInterfaces(virConnectPtr conn, char **const names, int nnames)
L
Laine Stump 已提交
3529 3530 3531 3532 3533 3534
{
    testConnPtr privconn = conn->privateData;
    int n = 0, i;

    testDriverLock(privconn);
    memset(names, 0, sizeof(*names)*nnames);
3535
    for (i = 0; (i < privconn->ifaces.count) && (n < nnames); i++) {
L
Laine Stump 已提交
3536
        virInterfaceObjLock(privconn->ifaces.objs[i]);
D
Daniel P. Berrange 已提交
3537
        if (!virInterfaceObjIsActive(privconn->ifaces.objs[i])) {
3538
            if (VIR_STRDUP(names[n++], privconn->ifaces.objs[i]->def->name) < 0) {
L
Laine Stump 已提交
3539
                virInterfaceObjUnlock(privconn->ifaces.objs[i]);
3540
                goto error;
L
Laine Stump 已提交
3541 3542 3543 3544 3545 3546 3547 3548
            }
        }
        virInterfaceObjUnlock(privconn->ifaces.objs[i]);
    }
    testDriverUnlock(privconn);

    return n;

3549
error:
3550
    for (n = 0; n < nnames; n++)
L
Laine Stump 已提交
3551 3552 3553 3554 3555
        VIR_FREE(names[n]);
    testDriverUnlock(privconn);
    return -1;
}

3556
static virInterfacePtr testInterfaceLookupByName(virConnectPtr conn,
L
Laine Stump 已提交
3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567
                                                 const char *name)
{
    testConnPtr privconn = conn->privateData;
    virInterfaceObjPtr iface;
    virInterfacePtr ret = NULL;

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

    if (iface == NULL) {
3568
        virReportError(VIR_ERR_NO_INTERFACE, NULL);
L
Laine Stump 已提交
3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579
        goto cleanup;
    }

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

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

3580
static virInterfacePtr testInterfaceLookupByMACString(virConnectPtr conn,
L
Laine Stump 已提交
3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592
                                                      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) {
3593
        virReportError(VIR_ERR_NO_INTERFACE, NULL);
L
Laine Stump 已提交
3594 3595 3596 3597
        goto cleanup;
    }

    if (ifacect > 1) {
3598
        virReportError(VIR_ERR_MULTIPLE_INTERFACES, NULL);
L
Laine Stump 已提交
3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609
        goto cleanup;
    }

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

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

3610 3611 3612 3613 3614 3615 3616 3617 3618 3619
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) {
3620
        virReportError(VIR_ERR_NO_INTERFACE, NULL);
3621 3622 3623 3624 3625 3626 3627 3628 3629 3630
        goto cleanup;
    }
    ret = virInterfaceObjIsActive(obj);

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

3631
static int testInterfaceChangeBegin(virConnectPtr conn,
E
Eric Blake 已提交
3632
                                    unsigned int flags)
3633 3634 3635 3636
{
    testConnPtr privconn = conn->privateData;
    int ret = -1;

E
Eric Blake 已提交
3637 3638
    virCheckFlags(0, -1);

3639 3640
    testDriverLock(privconn);
    if (privconn->transaction_running) {
3641
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
3642
                       _("there is another transaction running."));
3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658
        goto cleanup;
    }

    privconn->transaction_running = true;

    if (virInterfaceObjListClone(&privconn->ifaces,
                                 &privconn->backupIfaces) < 0)
        goto cleanup;

    ret = 0;
cleanup:
    testDriverUnlock(privconn);
    return ret;
}

static int testInterfaceChangeCommit(virConnectPtr conn,
E
Eric Blake 已提交
3659
                                     unsigned int flags)
3660 3661 3662 3663
{
    testConnPtr privconn = conn->privateData;
    int ret = -1;

E
Eric Blake 已提交
3664 3665
    virCheckFlags(0, -1);

3666 3667 3668
    testDriverLock(privconn);

    if (!privconn->transaction_running) {
3669
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
3670 3671
                       _("no transaction running, "
                         "nothing to be committed."));
3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686
        goto cleanup;
    }

    virInterfaceObjListFree(&privconn->backupIfaces);
    privconn->transaction_running = false;

    ret = 0;

cleanup:
    testDriverUnlock(privconn);

    return ret;
}

static int testInterfaceChangeRollback(virConnectPtr conn,
E
Eric Blake 已提交
3687
                                       unsigned int flags)
3688 3689 3690 3691
{
    testConnPtr privconn = conn->privateData;
    int ret = -1;

E
Eric Blake 已提交
3692 3693
    virCheckFlags(0, -1);

3694 3695 3696
    testDriverLock(privconn);

    if (!privconn->transaction_running) {
3697
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
3698 3699
                       _("no transaction running, "
                         "nothing to rollback."));
3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716
        goto cleanup;
    }

    virInterfaceObjListFree(&privconn->ifaces);
    privconn->ifaces.count = privconn->backupIfaces.count;
    privconn->ifaces.objs = privconn->backupIfaces.objs;
    privconn->backupIfaces.count = 0;
    privconn->backupIfaces.objs = NULL;

    privconn->transaction_running = false;

    ret = 0;

cleanup:
    testDriverUnlock(privconn);
    return ret;
}
3717

L
Laine Stump 已提交
3718
static char *testInterfaceGetXMLDesc(virInterfacePtr iface,
E
Eric Blake 已提交
3719
                                     unsigned int flags)
L
Laine Stump 已提交
3720 3721 3722 3723 3724
{
    testConnPtr privconn = iface->conn->privateData;
    virInterfaceObjPtr privinterface;
    char *ret = NULL;

E
Eric Blake 已提交
3725 3726
    virCheckFlags(0, NULL);

L
Laine Stump 已提交
3727 3728 3729 3730 3731 3732
    testDriverLock(privconn);
    privinterface = virInterfaceFindByName(&privconn->ifaces,
                                           iface->name);
    testDriverUnlock(privconn);

    if (privinterface == NULL) {
3733
        virReportError(VIR_ERR_NO_INTERFACE, __FUNCTION__);
L
Laine Stump 已提交
3734 3735 3736
        goto cleanup;
    }

3737
    ret = virInterfaceDefFormat(privinterface->def);
L
Laine Stump 已提交
3738 3739 3740 3741 3742 3743 3744 3745 3746

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


static virInterfacePtr testInterfaceDefineXML(virConnectPtr conn, const char *xmlStr,
E
Eric Blake 已提交
3747
                                              unsigned int flags)
L
Laine Stump 已提交
3748 3749 3750 3751 3752 3753
{
    testConnPtr privconn = conn->privateData;
    virInterfaceDefPtr def;
    virInterfaceObjPtr iface = NULL;
    virInterfacePtr ret = NULL;

E
Eric Blake 已提交
3754 3755
    virCheckFlags(0, NULL);

L
Laine Stump 已提交
3756
    testDriverLock(privconn);
3757
    if ((def = virInterfaceDefParseString(xmlStr)) == NULL)
L
Laine Stump 已提交
3758 3759
        goto cleanup;

3760
    if ((iface = virInterfaceAssignDef(&privconn->ifaces, def)) == NULL)
L
Laine Stump 已提交
3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784
        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) {
3785
        virReportError(VIR_ERR_NO_INTERFACE, NULL);
L
Laine Stump 已提交
3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798
        goto cleanup;
    }

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

cleanup:
    testDriverUnlock(privconn);
    return ret;
}

static int testInterfaceCreate(virInterfacePtr iface,
E
Eric Blake 已提交
3799
                               unsigned int flags)
L
Laine Stump 已提交
3800 3801 3802 3803 3804
{
    testConnPtr privconn = iface->conn->privateData;
    virInterfaceObjPtr privinterface;
    int ret = -1;

E
Eric Blake 已提交
3805 3806
    virCheckFlags(0, -1);

L
Laine Stump 已提交
3807 3808 3809 3810 3811
    testDriverLock(privconn);
    privinterface = virInterfaceFindByName(&privconn->ifaces,
                                           iface->name);

    if (privinterface == NULL) {
3812
        virReportError(VIR_ERR_NO_INTERFACE, NULL);
L
Laine Stump 已提交
3813 3814 3815 3816
        goto cleanup;
    }

    if (privinterface->active != 0) {
3817
        virReportError(VIR_ERR_OPERATION_INVALID, NULL);
L
Laine Stump 已提交
3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831
        goto cleanup;
    }

    privinterface->active = 1;
    ret = 0;

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

static int testInterfaceDestroy(virInterfacePtr iface,
E
Eric Blake 已提交
3832
                                unsigned int flags)
L
Laine Stump 已提交
3833 3834 3835 3836 3837
{
    testConnPtr privconn = iface->conn->privateData;
    virInterfaceObjPtr privinterface;
    int ret = -1;

E
Eric Blake 已提交
3838 3839
    virCheckFlags(0, -1);

L
Laine Stump 已提交
3840 3841 3842 3843 3844
    testDriverLock(privconn);
    privinterface = virInterfaceFindByName(&privconn->ifaces,
                                           iface->name);

    if (privinterface == NULL) {
3845
        virReportError(VIR_ERR_NO_INTERFACE, NULL);
L
Laine Stump 已提交
3846 3847 3848 3849
        goto cleanup;
    }

    if (privinterface->active == 0) {
3850
        virReportError(VIR_ERR_OPERATION_INVALID, NULL);
L
Laine Stump 已提交
3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865
        goto cleanup;
    }

    privinterface->active = 0;
    ret = 0;

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



C
Cole Robinson 已提交
3866 3867 3868 3869
/*
 * Storage Driver routines
 */

3870

3871
static int testStoragePoolObjSetDefaults(virStoragePoolObjPtr pool) {
C
Cole Robinson 已提交
3872 3873 3874 3875 3876

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

3877
    return VIR_STRDUP(pool->configFile, "");
C
Cole Robinson 已提交
3878 3879
}

3880 3881
static virDrvOpenStatus testStorageOpen(virConnectPtr conn,
                                        virConnectAuthPtr auth ATTRIBUTE_UNUSED,
E
Eric Blake 已提交
3882 3883 3884 3885
                                        unsigned int flags)
{
    virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);

3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897
    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;
}

3898

C
Cole Robinson 已提交
3899 3900 3901
static virStoragePoolPtr
testStoragePoolLookupByUUID(virConnectPtr conn,
                            const unsigned char *uuid) {
3902
    testConnPtr privconn = conn->privateData;
3903 3904
    virStoragePoolObjPtr pool;
    virStoragePoolPtr ret = NULL;
C
Cole Robinson 已提交
3905

3906
    testDriverLock(privconn);
3907
    pool = virStoragePoolObjFindByUUID(&privconn->pools, uuid);
3908
    testDriverUnlock(privconn);
3909 3910

    if (pool == NULL) {
3911
        virReportError(VIR_ERR_NO_STORAGE_POOL, NULL);
3912
        goto cleanup;
C
Cole Robinson 已提交
3913 3914
    }

3915 3916
    ret = virGetStoragePool(conn, pool->def->name, pool->def->uuid,
                            NULL, NULL);
3917 3918

cleanup:
3919 3920
    if (pool)
        virStoragePoolObjUnlock(pool);
3921
    return ret;
C
Cole Robinson 已提交
3922 3923 3924 3925 3926
}

static virStoragePoolPtr
testStoragePoolLookupByName(virConnectPtr conn,
                            const char *name) {
3927
    testConnPtr privconn = conn->privateData;
3928 3929
    virStoragePoolObjPtr pool;
    virStoragePoolPtr ret = NULL;
C
Cole Robinson 已提交
3930

3931
    testDriverLock(privconn);
3932
    pool = virStoragePoolObjFindByName(&privconn->pools, name);
3933
    testDriverUnlock(privconn);
3934 3935

    if (pool == NULL) {
3936
        virReportError(VIR_ERR_NO_STORAGE_POOL, NULL);
3937
        goto cleanup;
C
Cole Robinson 已提交
3938 3939
    }

3940 3941
    ret = virGetStoragePool(conn, pool->def->name, pool->def->uuid,
                            NULL, NULL);
3942 3943

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

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

static int
3955
testConnectNumOfStoragePools(virConnectPtr conn) {
3956
    testConnPtr privconn = conn->privateData;
C
Cole Robinson 已提交
3957 3958
    int numActive = 0, i;

3959
    testDriverLock(privconn);
3960
    for (i = 0; i < privconn->pools.count; i++)
C
Cole Robinson 已提交
3961 3962
        if (virStoragePoolObjIsActive(privconn->pools.objs[i]))
            numActive++;
3963
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
3964 3965 3966 3967 3968

    return numActive;
}

static int
3969 3970 3971
testConnectListStoragePools(virConnectPtr conn,
                            char **const names,
                            int nnames) {
3972
    testConnPtr privconn = conn->privateData;
C
Cole Robinson 已提交
3973 3974
    int n = 0, i;

3975
    testDriverLock(privconn);
C
Cole Robinson 已提交
3976
    memset(names, 0, sizeof(*names)*nnames);
3977
    for (i = 0; i < privconn->pools.count && n < nnames; i++) {
3978
        virStoragePoolObjLock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
3979
        if (virStoragePoolObjIsActive(privconn->pools.objs[i]) &&
3980
            VIR_STRDUP(names[n++], privconn->pools.objs[i]->def->name) < 0) {
3981
            virStoragePoolObjUnlock(privconn->pools.objs[i]);
3982
            goto error;
3983 3984 3985 3986
        }
        virStoragePoolObjUnlock(privconn->pools.objs[i]);
    }
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
3987 3988 3989

    return n;

3990
error:
3991
    for (n = 0; n < nnames; n++)
C
Cole Robinson 已提交
3992
        VIR_FREE(names[n]);
3993
    testDriverUnlock(privconn);
3994
    return -1;
C
Cole Robinson 已提交
3995 3996 3997
}

static int
3998
testConnectNumOfDefinedStoragePools(virConnectPtr conn) {
3999
    testConnPtr privconn = conn->privateData;
C
Cole Robinson 已提交
4000 4001
    int numInactive = 0, i;

4002
    testDriverLock(privconn);
4003
    for (i = 0; i < privconn->pools.count; i++) {
4004
        virStoragePoolObjLock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
4005 4006
        if (!virStoragePoolObjIsActive(privconn->pools.objs[i]))
            numInactive++;
4007 4008 4009
        virStoragePoolObjUnlock(privconn->pools.objs[i]);
    }
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
4010 4011 4012 4013 4014

    return numInactive;
}

static int
4015 4016 4017
testConnectListDefinedStoragePools(virConnectPtr conn,
                                   char **const names,
                                   int nnames) {
4018
    testConnPtr privconn = conn->privateData;
C
Cole Robinson 已提交
4019 4020
    int n = 0, i;

4021
    testDriverLock(privconn);
C
Cole Robinson 已提交
4022
    memset(names, 0, sizeof(*names)*nnames);
4023
    for (i = 0; i < privconn->pools.count && n < nnames; i++) {
4024
        virStoragePoolObjLock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
4025
        if (!virStoragePoolObjIsActive(privconn->pools.objs[i]) &&
4026
            VIR_STRDUP(names[n++], privconn->pools.objs[i]->def->name) < 0) {
4027
            virStoragePoolObjUnlock(privconn->pools.objs[i]);
4028
            goto error;
4029 4030 4031 4032
        }
        virStoragePoolObjUnlock(privconn->pools.objs[i]);
    }
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
4033 4034 4035

    return n;

4036
error:
4037
    for (n = 0; n < nnames; n++)
C
Cole Robinson 已提交
4038
        VIR_FREE(names[n]);
4039
    testDriverUnlock(privconn);
4040
    return -1;
C
Cole Robinson 已提交
4041 4042
}

4043
static int
4044 4045 4046
testConnectListAllStoragePools(virConnectPtr conn,
                               virStoragePoolPtr **pools,
                               unsigned int flags)
4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058
{
    testConnPtr privconn = conn->privateData;
    int ret = -1;

    virCheckFlags(VIR_CONNECT_LIST_STORAGE_POOLS_FILTERS_ALL, -1);

    testDriverLock(privconn);
    ret = virStoragePoolList(conn, privconn->pools, pools, flags);
    testDriverUnlock(privconn);

    return ret;
}
C
Cole Robinson 已提交
4059

4060 4061 4062 4063 4064 4065 4066 4067 4068 4069
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) {
4070
        virReportError(VIR_ERR_NO_STORAGE_POOL, NULL);
4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090
        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) {
4091
        virReportError(VIR_ERR_NO_STORAGE_POOL, NULL);
4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103
        goto cleanup;
    }
    ret = obj->configFile ? 1 : 0;

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



C
Cole Robinson 已提交
4104
static int
4105 4106
testStoragePoolCreate(virStoragePoolPtr pool,
                      unsigned int flags)
E
Eric Blake 已提交
4107
{
4108 4109
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4110
    int ret = -1;
4111

E
Eric Blake 已提交
4112 4113
    virCheckFlags(0, -1);

4114
    testDriverLock(privconn);
4115 4116
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4117
    testDriverUnlock(privconn);
4118 4119

    if (privpool == NULL) {
4120
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4121
        goto cleanup;
4122 4123
    }

4124
    if (virStoragePoolObjIsActive(privpool)) {
4125 4126
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is already active"), pool->name);
4127 4128
        goto cleanup;
    }
C
Cole Robinson 已提交
4129 4130

    privpool->active = 1;
4131
    ret = 0;
C
Cole Robinson 已提交
4132

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

static char *
4140 4141 4142 4143
testConnectFindStoragePoolSources(virConnectPtr conn ATTRIBUTE_UNUSED,
                                  const char *type,
                                  const char *srcSpec,
                                  unsigned int flags)
C
Cole Robinson 已提交
4144
{
4145 4146 4147 4148
    virStoragePoolSourcePtr source = NULL;
    int pool_type;
    char *ret = NULL;

E
Eric Blake 已提交
4149 4150
    virCheckFlags(0, NULL);

4151 4152
    pool_type = virStoragePoolTypeFromString(type);
    if (!pool_type) {
4153 4154
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("unknown storage pool type %s"), type);
4155 4156 4157 4158
        goto cleanup;
    }

    if (srcSpec) {
4159
        source = virStoragePoolDefParseSourceString(srcSpec, pool_type);
4160 4161 4162 4163 4164 4165 4166
        if (!source)
            goto cleanup;
    }

    switch (pool_type) {

    case VIR_STORAGE_POOL_LOGICAL:
4167
        ignore_value(VIR_STRDUP(ret, defaultPoolSourcesLogicalXML));
4168 4169 4170
        break;

    case VIR_STORAGE_POOL_NETFS:
4171
        if (!source || !source->hosts[0].name) {
4172 4173
            virReportError(VIR_ERR_INVALID_ARG,
                           "%s", _("hostname must be specified for netfs sources"));
4174 4175 4176 4177
            goto cleanup;
        }

        if (virAsprintf(&ret, defaultPoolSourcesNetFSXML,
4178
                        source->hosts[0].name) < 0)
4179
            virReportOOMError();
4180 4181 4182
        break;

    default:
4183 4184
        virReportError(VIR_ERR_NO_SUPPORT,
                       _("pool type '%s' does not support source discovery"), type);
4185 4186 4187 4188 4189
    }

cleanup:
    virStoragePoolSourceFree(source);
    return ret;
C
Cole Robinson 已提交
4190 4191 4192 4193
}


static virStoragePoolPtr
4194 4195 4196
testStoragePoolCreateXML(virConnectPtr conn,
                         const char *xml,
                         unsigned int flags)
E
Eric Blake 已提交
4197
{
4198
    testConnPtr privconn = conn->privateData;
C
Cole Robinson 已提交
4199
    virStoragePoolDefPtr def;
4200
    virStoragePoolObjPtr pool = NULL;
4201
    virStoragePoolPtr ret = NULL;
C
Cole Robinson 已提交
4202

E
Eric Blake 已提交
4203 4204
    virCheckFlags(0, NULL);

4205
    testDriverLock(privconn);
4206
    if (!(def = virStoragePoolDefParseString(xml)))
4207
        goto cleanup;
C
Cole Robinson 已提交
4208

4209 4210 4211 4212
    pool = virStoragePoolObjFindByUUID(&privconn->pools, def->uuid);
    if (!pool)
        pool = virStoragePoolObjFindByName(&privconn->pools, def->name);
    if (pool) {
4213 4214
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("storage pool already exists"));
4215
        goto cleanup;
C
Cole Robinson 已提交
4216 4217
    }

4218
    if (!(pool = virStoragePoolObjAssignDef(&privconn->pools, def)))
4219
        goto cleanup;
4220
    def = NULL;
C
Cole Robinson 已提交
4221

4222
    if (testStoragePoolObjSetDefaults(pool) == -1) {
C
Cole Robinson 已提交
4223
        virStoragePoolObjRemove(&privconn->pools, pool);
4224 4225
        pool = NULL;
        goto cleanup;
C
Cole Robinson 已提交
4226 4227 4228
    }
    pool->active = 1;

4229 4230
    ret = virGetStoragePool(conn, pool->def->name, pool->def->uuid,
                            NULL, NULL);
4231 4232 4233

cleanup:
    virStoragePoolDefFree(def);
4234 4235 4236
    if (pool)
        virStoragePoolObjUnlock(pool);
    testDriverUnlock(privconn);
4237
    return ret;
C
Cole Robinson 已提交
4238 4239 4240
}

static virStoragePoolPtr
4241 4242 4243
testStoragePoolDefineXML(virConnectPtr conn,
                         const char *xml,
                         unsigned int flags)
E
Eric Blake 已提交
4244
{
4245
    testConnPtr privconn = conn->privateData;
C
Cole Robinson 已提交
4246
    virStoragePoolDefPtr def;
4247
    virStoragePoolObjPtr pool = NULL;
4248
    virStoragePoolPtr ret = NULL;
C
Cole Robinson 已提交
4249

E
Eric Blake 已提交
4250 4251
    virCheckFlags(0, NULL);

4252
    testDriverLock(privconn);
4253
    if (!(def = virStoragePoolDefParseString(xml)))
4254
        goto cleanup;
C
Cole Robinson 已提交
4255 4256 4257 4258 4259

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

4260
    if (!(pool = virStoragePoolObjAssignDef(&privconn->pools, def)))
4261 4262
        goto cleanup;
    def = NULL;
C
Cole Robinson 已提交
4263

4264
    if (testStoragePoolObjSetDefaults(pool) == -1) {
C
Cole Robinson 已提交
4265
        virStoragePoolObjRemove(&privconn->pools, pool);
4266 4267
        pool = NULL;
        goto cleanup;
C
Cole Robinson 已提交
4268 4269
    }

4270 4271
    ret = virGetStoragePool(conn, pool->def->name, pool->def->uuid,
                            NULL, NULL);
4272 4273 4274

cleanup:
    virStoragePoolDefFree(def);
4275 4276 4277
    if (pool)
        virStoragePoolObjUnlock(pool);
    testDriverUnlock(privconn);
4278
    return ret;
C
Cole Robinson 已提交
4279 4280 4281
}

static int
4282 4283 4284
testStoragePoolUndefine(virStoragePoolPtr pool) {
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4285
    int ret = -1;
4286

4287
    testDriverLock(privconn);
4288 4289 4290 4291
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);

    if (privpool == NULL) {
4292
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4293
        goto cleanup;
4294 4295
    }

4296
    if (virStoragePoolObjIsActive(privpool)) {
4297 4298
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is already active"), pool->name);
4299 4300
        goto cleanup;
    }
C
Cole Robinson 已提交
4301 4302

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

4305
cleanup:
4306 4307 4308
    if (privpool)
        virStoragePoolObjUnlock(privpool);
    testDriverUnlock(privconn);
4309
    return ret;
C
Cole Robinson 已提交
4310 4311 4312
}

static int
4313
testStoragePoolBuild(virStoragePoolPtr pool,
E
Eric Blake 已提交
4314 4315
                     unsigned int flags)
{
4316 4317
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4318
    int ret = -1;
4319

E
Eric Blake 已提交
4320 4321
    virCheckFlags(0, -1);

4322
    testDriverLock(privconn);
4323 4324
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4325
    testDriverUnlock(privconn);
4326 4327

    if (privpool == NULL) {
4328
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4329
        goto cleanup;
4330 4331
    }

4332
    if (virStoragePoolObjIsActive(privpool)) {
4333 4334
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is already active"), pool->name);
4335 4336
        goto cleanup;
    }
4337
    ret = 0;
C
Cole Robinson 已提交
4338

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


static int
4347 4348 4349
testStoragePoolDestroy(virStoragePoolPtr pool) {
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4350
    int ret = -1;
4351

4352
    testDriverLock(privconn);
4353 4354 4355 4356
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);

    if (privpool == NULL) {
4357
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4358
        goto cleanup;
4359 4360 4361
    }

    if (!virStoragePoolObjIsActive(privpool)) {
4362 4363
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), pool->name);
4364
        goto cleanup;
4365
    }
C
Cole Robinson 已提交
4366 4367 4368

    privpool->active = 0;

4369
    if (privpool->configFile == NULL) {
C
Cole Robinson 已提交
4370
        virStoragePoolObjRemove(&privconn->pools, privpool);
4371 4372
        privpool = NULL;
    }
4373
    ret = 0;
C
Cole Robinson 已提交
4374

4375
cleanup:
4376 4377 4378
    if (privpool)
        virStoragePoolObjUnlock(privpool);
    testDriverUnlock(privconn);
4379
    return ret;
C
Cole Robinson 已提交
4380 4381 4382 4383
}


static int
4384
testStoragePoolDelete(virStoragePoolPtr pool,
E
Eric Blake 已提交
4385 4386
                      unsigned int flags)
{
4387 4388
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4389
    int ret = -1;
4390

E
Eric Blake 已提交
4391 4392
    virCheckFlags(0, -1);

4393
    testDriverLock(privconn);
4394 4395
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4396
    testDriverUnlock(privconn);
4397 4398

    if (privpool == NULL) {
4399
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4400 4401 4402 4403
        goto cleanup;
    }

    if (virStoragePoolObjIsActive(privpool)) {
4404 4405
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is already active"), pool->name);
4406
        goto cleanup;
4407 4408
    }

4409
    ret = 0;
C
Cole Robinson 已提交
4410

4411
cleanup:
4412 4413
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4414
    return ret;
C
Cole Robinson 已提交
4415 4416 4417 4418
}


static int
4419
testStoragePoolRefresh(virStoragePoolPtr pool,
E
Eric Blake 已提交
4420 4421
                       unsigned int flags)
{
4422 4423
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4424
    int ret = -1;
4425

E
Eric Blake 已提交
4426 4427
    virCheckFlags(0, -1);

4428
    testDriverLock(privconn);
4429 4430
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4431
    testDriverUnlock(privconn);
4432 4433

    if (privpool == NULL) {
4434
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4435
        goto cleanup;
4436 4437 4438
    }

    if (!virStoragePoolObjIsActive(privpool)) {
4439 4440
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), pool->name);
4441
        goto cleanup;
4442
    }
4443
    ret = 0;
C
Cole Robinson 已提交
4444

4445
cleanup:
4446 4447
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4448
    return ret;
C
Cole Robinson 已提交
4449 4450 4451 4452
}


static int
4453
testStoragePoolGetInfo(virStoragePoolPtr pool,
C
Cole Robinson 已提交
4454
                       virStoragePoolInfoPtr info) {
4455 4456
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4457
    int ret = -1;
4458

4459
    testDriverLock(privconn);
4460 4461
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4462
    testDriverUnlock(privconn);
4463 4464

    if (privpool == NULL) {
4465
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4466
        goto cleanup;
4467
    }
C
Cole Robinson 已提交
4468 4469 4470 4471 4472 4473 4474 4475 4476

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

4479
cleanup:
4480 4481
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4482
    return ret;
C
Cole Robinson 已提交
4483 4484 4485
}

static char *
4486
testStoragePoolGetXMLDesc(virStoragePoolPtr pool,
E
Eric Blake 已提交
4487 4488
                          unsigned int flags)
{
4489 4490
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4491
    char *ret = NULL;
4492

E
Eric Blake 已提交
4493 4494
    virCheckFlags(0, NULL);

4495
    testDriverLock(privconn);
4496 4497
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4498
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
4499

4500
    if (privpool == NULL) {
4501
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4502
        goto cleanup;
4503 4504
    }

4505
    ret = virStoragePoolDefFormat(privpool->def);
4506 4507

cleanup:
4508 4509
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4510
    return ret;
C
Cole Robinson 已提交
4511 4512 4513
}

static int
4514
testStoragePoolGetAutostart(virStoragePoolPtr pool,
C
Cole Robinson 已提交
4515
                            int *autostart) {
4516 4517
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4518
    int ret = -1;
4519

4520
    testDriverLock(privconn);
4521 4522
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4523
    testDriverUnlock(privconn);
4524 4525

    if (privpool == NULL) {
4526
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4527
        goto cleanup;
4528
    }
C
Cole Robinson 已提交
4529 4530 4531 4532 4533 4534

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

4537
cleanup:
4538 4539
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4540
    return ret;
C
Cole Robinson 已提交
4541 4542 4543
}

static int
4544
testStoragePoolSetAutostart(virStoragePoolPtr pool,
C
Cole Robinson 已提交
4545
                            int autostart) {
4546 4547
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4548
    int ret = -1;
4549

4550
    testDriverLock(privconn);
4551 4552
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4553
    testDriverUnlock(privconn);
4554 4555

    if (privpool == NULL) {
4556
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4557
        goto cleanup;
4558
    }
C
Cole Robinson 已提交
4559 4560

    if (!privpool->configFile) {
4561 4562
        virReportError(VIR_ERR_INVALID_ARG,
                       "%s", _("pool has no config file"));
4563
        goto cleanup;
C
Cole Robinson 已提交
4564 4565 4566 4567
    }

    autostart = (autostart != 0);
    privpool->autostart = autostart;
4568 4569 4570
    ret = 0;

cleanup:
4571 4572
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4573
    return ret;
C
Cole Robinson 已提交
4574 4575 4576 4577
}


static int
4578
testStoragePoolNumOfVolumes(virStoragePoolPtr pool) {
4579 4580
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4581
    int ret = -1;
4582

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

    if (privpool == NULL) {
4589
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4590
        goto cleanup;
4591 4592 4593
    }

    if (!virStoragePoolObjIsActive(privpool)) {
4594 4595
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), pool->name);
4596
        goto cleanup;
4597
    }
C
Cole Robinson 已提交
4598

4599 4600 4601
    ret = privpool->volumes.count;

cleanup:
4602 4603
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4604
    return ret;
C
Cole Robinson 已提交
4605 4606 4607
}

static int
4608
testStoragePoolListVolumes(virStoragePoolPtr pool,
C
Cole Robinson 已提交
4609 4610
                           char **const names,
                           int maxnames) {
4611 4612
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
C
Cole Robinson 已提交
4613 4614
    int i = 0, n = 0;

4615
    memset(names, 0, maxnames * sizeof(*names));
4616 4617

    testDriverLock(privconn);
4618 4619
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4620
    testDriverUnlock(privconn);
4621 4622

    if (privpool == NULL) {
4623
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4624
        goto cleanup;
4625 4626 4627 4628
    }


    if (!virStoragePoolObjIsActive(privpool)) {
4629 4630
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), pool->name);
4631
        goto cleanup;
4632 4633
    }

4634
    for (i = 0; i < privpool->volumes.count && n < maxnames; i++) {
4635
        if (VIR_STRDUP(names[n++], privpool->volumes.objs[i]->name) < 0)
C
Cole Robinson 已提交
4636 4637 4638
            goto cleanup;
    }

4639
    virStoragePoolObjUnlock(privpool);
C
Cole Robinson 已提交
4640 4641 4642
    return n;

 cleanup:
4643
    for (n = 0; n < maxnames; n++)
C
Cole Robinson 已提交
4644 4645
        VIR_FREE(names[i]);

4646
    memset(names, 0, maxnames * sizeof(*names));
4647 4648
    if (privpool)
        virStoragePoolObjUnlock(privpool);
C
Cole Robinson 已提交
4649 4650 4651
    return -1;
}

4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692
static int
testStoragePoolListAllVolumes(virStoragePoolPtr obj,
                              virStorageVolPtr **vols,
                              unsigned int flags) {
    testConnPtr privconn = obj->conn->privateData;
    virStoragePoolObjPtr pool;
    int i;
    virStorageVolPtr *tmp_vols = NULL;
    virStorageVolPtr vol = NULL;
    int nvols = 0;
    int ret = -1;

    virCheckFlags(0, -1);

    testDriverLock(privconn);
    pool = virStoragePoolObjFindByUUID(&privconn->pools, obj->uuid);
    testDriverUnlock(privconn);

    if (!pool) {
        virReportError(VIR_ERR_NO_STORAGE_POOL, "%s",
                       _("no storage pool with matching uuid"));
        goto cleanup;
    }

    if (!virStoragePoolObjIsActive(pool)) {
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
                       _("storage pool is not active"));
        goto cleanup;
    }

     /* Just returns the volumes count */
    if (!vols) {
        ret = pool->volumes.count;
        goto cleanup;
    }

    if (VIR_ALLOC_N(tmp_vols, pool->volumes.count + 1) < 0) {
         virReportOOMError();
         goto cleanup;
    }

4693
    for (i = 0; i < pool->volumes.count; i++) {
4694 4695
        if (!(vol = virGetStorageVol(obj->conn, pool->def->name,
                                     pool->volumes.objs[i]->name,
4696 4697
                                     pool->volumes.objs[i]->key,
                                     NULL, NULL)))
4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711
            goto cleanup;
        tmp_vols[nvols++] = vol;
    }

    *vols = tmp_vols;
    tmp_vols = NULL;
    ret = nvols;

 cleanup:
    if (tmp_vols) {
        for (i = 0; i < nvols; i++) {
            if (tmp_vols[i])
                virStorageVolFree(tmp_vols[i]);
        }
4712
        VIR_FREE(tmp_vols);
4713 4714 4715 4716 4717 4718 4719
    }

    if (pool)
        virStoragePoolObjUnlock(pool);

    return ret;
}
C
Cole Robinson 已提交
4720 4721

static virStorageVolPtr
4722 4723
testStorageVolLookupByName(virStoragePoolPtr pool,
                           const char *name ATTRIBUTE_UNUSED) {
4724 4725 4726
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol;
4727
    virStorageVolPtr ret = NULL;
4728

4729
    testDriverLock(privconn);
4730 4731
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4732
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
4733

4734
    if (privpool == NULL) {
4735
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4736
        goto cleanup;
4737 4738 4739 4740
    }


    if (!virStoragePoolObjIsActive(privpool)) {
4741 4742
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), pool->name);
4743
        goto cleanup;
4744 4745 4746 4747 4748
    }

    privvol = virStorageVolDefFindByName(privpool, name);

    if (!privvol) {
4749 4750
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching name '%s'"), name);
4751
        goto cleanup;
C
Cole Robinson 已提交
4752 4753
    }

4754
    ret = virGetStorageVol(pool->conn, privpool->def->name,
4755 4756
                           privvol->name, privvol->key,
                           NULL, NULL);
4757 4758

cleanup:
4759 4760
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4761
    return ret;
C
Cole Robinson 已提交
4762 4763 4764 4765
}


static virStorageVolPtr
4766 4767
testStorageVolLookupByKey(virConnectPtr conn,
                          const char *key) {
4768
    testConnPtr privconn = conn->privateData;
C
Cole Robinson 已提交
4769
    unsigned int i;
4770
    virStorageVolPtr ret = NULL;
C
Cole Robinson 已提交
4771

4772
    testDriverLock(privconn);
4773
    for (i = 0; i < privconn->pools.count; i++) {
4774
        virStoragePoolObjLock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
4775
        if (virStoragePoolObjIsActive(privconn->pools.objs[i])) {
4776
            virStorageVolDefPtr privvol =
C
Cole Robinson 已提交
4777 4778
                virStorageVolDefFindByKey(privconn->pools.objs[i], key);

4779 4780 4781 4782
            if (privvol) {
                ret = virGetStorageVol(conn,
                                       privconn->pools.objs[i]->def->name,
                                       privvol->name,
4783 4784
                                       privvol->key,
                                       NULL, NULL);
4785
                virStoragePoolObjUnlock(privconn->pools.objs[i]);
4786 4787
                break;
            }
C
Cole Robinson 已提交
4788
        }
4789
        virStoragePoolObjUnlock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
4790
    }
4791
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
4792

4793
    if (!ret)
4794 4795
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching key '%s'"), key);
4796 4797

    return ret;
C
Cole Robinson 已提交
4798 4799 4800
}

static virStorageVolPtr
4801 4802
testStorageVolLookupByPath(virConnectPtr conn,
                           const char *path) {
4803
    testConnPtr privconn = conn->privateData;
C
Cole Robinson 已提交
4804
    unsigned int i;
4805
    virStorageVolPtr ret = NULL;
C
Cole Robinson 已提交
4806

4807
    testDriverLock(privconn);
4808
    for (i = 0; i < privconn->pools.count; i++) {
4809
        virStoragePoolObjLock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
4810
        if (virStoragePoolObjIsActive(privconn->pools.objs[i])) {
4811
            virStorageVolDefPtr privvol =
C
Cole Robinson 已提交
4812 4813
                virStorageVolDefFindByPath(privconn->pools.objs[i], path);

4814 4815 4816 4817
            if (privvol) {
                ret = virGetStorageVol(conn,
                                       privconn->pools.objs[i]->def->name,
                                       privvol->name,
4818 4819
                                       privvol->key,
                                       NULL, NULL);
4820
                virStoragePoolObjUnlock(privconn->pools.objs[i]);
4821 4822
                break;
            }
C
Cole Robinson 已提交
4823
        }
4824
        virStoragePoolObjUnlock(privconn->pools.objs[i]);
C
Cole Robinson 已提交
4825
    }
4826
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
4827

4828
    if (!ret)
4829 4830
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching path '%s'"), path);
4831 4832

    return ret;
C
Cole Robinson 已提交
4833 4834 4835
}

static virStorageVolPtr
4836 4837 4838
testStorageVolCreateXML(virStoragePoolPtr pool,
                        const char *xmldesc,
                        unsigned int flags)
E
Eric Blake 已提交
4839
{
4840 4841
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
4842 4843
    virStorageVolDefPtr privvol = NULL;
    virStorageVolPtr ret = NULL;
4844

E
Eric Blake 已提交
4845 4846
    virCheckFlags(0, NULL);

4847
    testDriverLock(privconn);
4848 4849
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
4850
    testDriverUnlock(privconn);
C
Cole Robinson 已提交
4851

4852
    if (privpool == NULL) {
4853
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4854
        goto cleanup;
4855 4856 4857
    }

    if (!virStoragePoolObjIsActive(privpool)) {
4858 4859
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), pool->name);
4860
        goto cleanup;
4861
    }
C
Cole Robinson 已提交
4862

4863
    privvol = virStorageVolDefParseString(privpool->def, xmldesc);
4864
    if (privvol == NULL)
4865
        goto cleanup;
4866 4867

    if (virStorageVolDefFindByName(privpool, privvol->name)) {
4868 4869
        virReportError(VIR_ERR_OPERATION_FAILED,
                       "%s", _("storage vol already exists"));
4870
        goto cleanup;
C
Cole Robinson 已提交
4871 4872 4873
    }

    /* Make sure enough space */
4874
    if ((privpool->def->allocation + privvol->allocation) >
C
Cole Robinson 已提交
4875
         privpool->def->capacity) {
4876 4877 4878
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Not enough free space in pool for volume '%s'"),
                       privvol->name);
4879
        goto cleanup;
C
Cole Robinson 已提交
4880 4881 4882 4883
    }

    if (VIR_REALLOC_N(privpool->volumes.objs,
                      privpool->volumes.count+1) < 0) {
4884
        virReportOOMError();
4885
        goto cleanup;
C
Cole Robinson 已提交
4886 4887
    }

4888 4889 4890
    if (virAsprintf(&privvol->target.path, "%s/%s",
                    privpool->def->target.path,
                    privvol->name) == -1) {
4891
        virReportOOMError();
4892
        goto cleanup;
C
Cole Robinson 已提交
4893 4894
    }

4895
    if (VIR_STRDUP(privvol->key, privvol->target.path) < 0)
4896
        goto cleanup;
C
Cole Robinson 已提交
4897

4898
    privpool->def->allocation += privvol->allocation;
C
Cole Robinson 已提交
4899 4900 4901
    privpool->def->available = (privpool->def->capacity -
                                privpool->def->allocation);

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

4904
    ret = virGetStorageVol(pool->conn, privpool->def->name,
4905 4906
                           privvol->name, privvol->key,
                           NULL, NULL);
4907
    privvol = NULL;
4908 4909 4910

cleanup:
    virStorageVolDefFree(privvol);
4911 4912
    if (privpool)
        virStoragePoolObjUnlock(privpool);
4913
    return ret;
C
Cole Robinson 已提交
4914 4915
}

4916
static virStorageVolPtr
4917 4918 4919 4920
testStorageVolCreateXMLFrom(virStoragePoolPtr pool,
                            const char *xmldesc,
                            virStorageVolPtr clonevol,
                            unsigned int flags)
E
Eric Blake 已提交
4921
{
4922 4923 4924 4925 4926
    testConnPtr privconn = pool->conn->privateData;
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol = NULL, origvol = NULL;
    virStorageVolPtr ret = NULL;

E
Eric Blake 已提交
4927 4928
    virCheckFlags(0, NULL);

4929 4930 4931 4932 4933 4934
    testDriverLock(privconn);
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           pool->name);
    testDriverUnlock(privconn);

    if (privpool == NULL) {
4935
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
4936 4937 4938 4939
        goto cleanup;
    }

    if (!virStoragePoolObjIsActive(privpool)) {
4940 4941
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), pool->name);
4942 4943 4944
        goto cleanup;
    }

4945
    privvol = virStorageVolDefParseString(privpool->def, xmldesc);
4946 4947 4948 4949
    if (privvol == NULL)
        goto cleanup;

    if (virStorageVolDefFindByName(privpool, privvol->name)) {
4950 4951
        virReportError(VIR_ERR_OPERATION_FAILED,
                       "%s", _("storage vol already exists"));
4952 4953 4954 4955 4956
        goto cleanup;
    }

    origvol = virStorageVolDefFindByName(privpool, clonevol->name);
    if (!origvol) {
4957 4958 4959
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching name '%s'"),
                       clonevol->name);
4960 4961 4962 4963 4964 4965
        goto cleanup;
    }

    /* Make sure enough space */
    if ((privpool->def->allocation + privvol->allocation) >
         privpool->def->capacity) {
4966 4967 4968
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Not enough free space in pool for volume '%s'"),
                       privvol->name);
4969 4970 4971 4972 4973 4974 4975
        goto cleanup;
    }
    privpool->def->available = (privpool->def->capacity -
                                privpool->def->allocation);

    if (VIR_REALLOC_N(privpool->volumes.objs,
                      privpool->volumes.count+1) < 0) {
4976
        virReportOOMError();
4977 4978 4979
        goto cleanup;
    }

4980 4981 4982
    if (virAsprintf(&privvol->target.path, "%s/%s",
                    privpool->def->target.path,
                    privvol->name) == -1) {
4983
        virReportOOMError();
4984 4985 4986
        goto cleanup;
    }

4987
    if (VIR_STRDUP(privvol->key, privvol->target.path) < 0)
4988 4989 4990 4991 4992 4993 4994 4995 4996
        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,
4997 4998
                           privvol->name, privvol->key,
                           NULL, NULL);
4999 5000 5001 5002 5003 5004 5005 5006 5007
    privvol = NULL;

cleanup:
    virStorageVolDefFree(privvol);
    if (privpool)
        virStoragePoolObjUnlock(privpool);
    return ret;
}

C
Cole Robinson 已提交
5008
static int
5009 5010
testStorageVolDelete(virStorageVolPtr vol,
                     unsigned int flags)
E
Eric Blake 已提交
5011
{
5012 5013 5014
    testConnPtr privconn = vol->conn->privateData;
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol;
C
Cole Robinson 已提交
5015
    int i;
5016
    int ret = -1;
C
Cole Robinson 已提交
5017

E
Eric Blake 已提交
5018 5019
    virCheckFlags(0, -1);

5020
    testDriverLock(privconn);
5021 5022
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           vol->pool);
5023
    testDriverUnlock(privconn);
5024 5025

    if (privpool == NULL) {
5026
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
5027
        goto cleanup;
5028 5029 5030 5031 5032 5033
    }


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

    if (privvol == NULL) {
5034 5035 5036
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching name '%s'"),
                       vol->name);
5037
        goto cleanup;
5038 5039 5040
    }

    if (!virStoragePoolObjIsActive(privpool)) {
5041 5042
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), vol->pool);
5043
        goto cleanup;
5044 5045 5046
    }


C
Cole Robinson 已提交
5047 5048 5049 5050
    privpool->def->allocation -= privvol->allocation;
    privpool->def->available = (privpool->def->capacity -
                                privpool->def->allocation);

5051
    for (i = 0; i < privpool->volumes.count; i++) {
C
Cole Robinson 已提交
5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069
        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;
        }
    }
5070
    ret = 0;
C
Cole Robinson 已提交
5071

5072
cleanup:
5073 5074
    if (privpool)
        virStoragePoolObjUnlock(privpool);
5075
    return ret;
C
Cole Robinson 已提交
5076 5077 5078 5079 5080
}


static int testStorageVolumeTypeForPool(int pooltype) {

5081
    switch (pooltype) {
C
Cole Robinson 已提交
5082 5083 5084 5085 5086 5087 5088 5089 5090 5091
        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
5092 5093
testStorageVolGetInfo(virStorageVolPtr vol,
                      virStorageVolInfoPtr info) {
5094 5095 5096
    testConnPtr privconn = vol->conn->privateData;
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol;
5097
    int ret = -1;
5098

5099
    testDriverLock(privconn);
5100 5101
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           vol->pool);
5102
    testDriverUnlock(privconn);
5103 5104

    if (privpool == NULL) {
5105
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
5106
        goto cleanup;
5107 5108 5109 5110 5111
    }

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

    if (privvol == NULL) {
5112 5113 5114
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching name '%s'"),
                       vol->name);
5115
        goto cleanup;
5116 5117 5118
    }

    if (!virStoragePoolObjIsActive(privpool)) {
5119 5120
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), vol->pool);
5121
        goto cleanup;
5122
    }
C
Cole Robinson 已提交
5123 5124 5125 5126 5127

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

5130
cleanup:
5131 5132
    if (privpool)
        virStoragePoolObjUnlock(privpool);
5133
    return ret;
C
Cole Robinson 已提交
5134 5135 5136
}

static char *
5137 5138
testStorageVolGetXMLDesc(virStorageVolPtr vol,
                         unsigned int flags)
E
Eric Blake 已提交
5139
{
5140 5141 5142
    testConnPtr privconn = vol->conn->privateData;
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol;
5143
    char *ret = NULL;
5144

E
Eric Blake 已提交
5145 5146
    virCheckFlags(0, NULL);

5147
    testDriverLock(privconn);
5148 5149
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           vol->pool);
5150
    testDriverUnlock(privconn);
5151 5152

    if (privpool == NULL) {
5153
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
5154
        goto cleanup;
5155 5156 5157 5158 5159
    }

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

    if (privvol == NULL) {
5160 5161 5162
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching name '%s'"),
                       vol->name);
5163
        goto cleanup;
5164
    }
C
Cole Robinson 已提交
5165

5166
    if (!virStoragePoolObjIsActive(privpool)) {
5167 5168
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), vol->pool);
5169
        goto cleanup;
5170 5171
    }

5172
    ret = virStorageVolDefFormat(privpool->def, privvol);
5173 5174

cleanup:
5175 5176
    if (privpool)
        virStoragePoolObjUnlock(privpool);
5177
    return ret;
C
Cole Robinson 已提交
5178 5179 5180
}

static char *
5181
testStorageVolGetPath(virStorageVolPtr vol) {
5182 5183 5184
    testConnPtr privconn = vol->conn->privateData;
    virStoragePoolObjPtr privpool;
    virStorageVolDefPtr privvol;
5185
    char *ret = NULL;
5186

5187
    testDriverLock(privconn);
5188 5189
    privpool = virStoragePoolObjFindByName(&privconn->pools,
                                           vol->pool);
5190
    testDriverUnlock(privconn);
5191 5192

    if (privpool == NULL) {
5193
        virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
5194
        goto cleanup;
5195 5196 5197 5198 5199
    }

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

    if (privvol == NULL) {
5200 5201 5202
        virReportError(VIR_ERR_NO_STORAGE_VOL,
                       _("no storage vol with matching name '%s'"),
                       vol->name);
5203
        goto cleanup;
5204 5205 5206
    }

    if (!virStoragePoolObjIsActive(privpool)) {
5207 5208
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("storage pool '%s' is not active"), vol->pool);
5209
        goto cleanup;
5210 5211
    }

5212
    ignore_value(VIR_STRDUP(ret, privvol->target.path));
5213 5214

cleanup:
5215 5216
    if (privpool)
        virStoragePoolObjUnlock(privpool);
C
Cole Robinson 已提交
5217 5218 5219
    return ret;
}

5220

5221
/* Node device implementations */
5222 5223 5224
static virDrvOpenStatus testNodeDeviceOpen(virConnectPtr conn,
                                           virConnectAuthPtr auth ATTRIBUTE_UNUSED,
                                           unsigned int flags)
E
Eric Blake 已提交
5225 5226 5227
{
    virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);

5228 5229 5230
    if (STRNEQ(conn->driver->name, "Test"))
        return VIR_DRV_OPEN_DECLINED;

5231
    conn->nodeDevicePrivateData = conn->privateData;
5232 5233 5234
    return VIR_DRV_OPEN_SUCCESS;
}

5235 5236
static int testNodeDeviceClose(virConnectPtr conn) {
    conn->nodeDevicePrivateData = NULL;
5237 5238 5239
    return 0;
}

5240 5241 5242
static int
testNodeNumOfDevices(virConnectPtr conn,
                     const char *cap,
E
Eric Blake 已提交
5243
                     unsigned int flags)
5244 5245 5246 5247 5248
{
    testConnPtr driver = conn->privateData;
    int ndevs = 0;
    unsigned int i;

E
Eric Blake 已提交
5249 5250
    virCheckFlags(0, -1);

5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265
    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,
E
Eric Blake 已提交
5266
                    unsigned int flags)
5267 5268 5269 5270 5271
{
    testConnPtr driver = conn->privateData;
    int ndevs = 0;
    unsigned int i;

E
Eric Blake 已提交
5272 5273
    virCheckFlags(0, -1);

5274 5275 5276 5277 5278
    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)) {
5279
            if (VIR_STRDUP(names[ndevs++], driver->devs.objs[i]->def->name) < 0) {
5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309
                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) {
5310
        virReportError(VIR_ERR_NO_NODE_DEVICE, NULL);
5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322
        goto cleanup;
    }

    ret = virGetNodeDevice(conn, name);

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

static char *
5323
testNodeDeviceGetXMLDesc(virNodeDevicePtr dev,
E
Eric Blake 已提交
5324
                         unsigned int flags)
5325 5326 5327 5328 5329
{
    testConnPtr driver = dev->conn->privateData;
    virNodeDeviceObjPtr obj;
    char *ret = NULL;

E
Eric Blake 已提交
5330 5331
    virCheckFlags(0, NULL);

5332 5333 5334 5335 5336
    testDriverLock(driver);
    obj = virNodeDeviceFindByName(&driver->devs, dev->name);
    testDriverUnlock(driver);

    if (!obj) {
5337 5338 5339
        virReportError(VIR_ERR_NO_NODE_DEVICE,
                       _("no node device with matching name '%s'"),
                       dev->name);
5340 5341 5342
        goto cleanup;
    }

5343
    ret = virNodeDeviceDefFormat(obj->def);
5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362

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) {
5363 5364 5365
        virReportError(VIR_ERR_NO_NODE_DEVICE,
                       _("no node device with matching name '%s'"),
                       dev->name);
5366 5367 5368 5369
        goto cleanup;
    }

    if (obj->def->parent) {
5370
        ignore_value(VIR_STRDUP(ret, obj->def->parent));
5371
    } else {
5372 5373
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("no parent for this device"));
5374 5375 5376 5377 5378 5379 5380 5381
    }

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

5382

5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396
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) {
5397 5398 5399
        virReportError(VIR_ERR_NO_NODE_DEVICE,
                       _("no node device with matching name '%s'"),
                       dev->name);
5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427
        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) {
5428 5429 5430
        virReportError(VIR_ERR_NO_NODE_DEVICE,
                       _("no node device with matching name '%s'"),
                       dev->name);
5431 5432 5433 5434
        goto cleanup;
    }

    for (caps = obj->def->caps; caps && ncaps < maxnames; caps = caps->next) {
5435
        if (VIR_STRDUP(names[ncaps++], virNodeDevCapTypeToString(caps->type)) < 0)
5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450
            goto cleanup;
    }
    ret = ncaps;

cleanup:
    if (obj)
        virNodeDeviceObjUnlock(obj);
    if (ret == -1) {
        --ncaps;
        while (--ncaps >= 0)
            VIR_FREE(names[ncaps]);
    }
    return ret;
}

5451 5452 5453
static virNodeDevicePtr
testNodeDeviceCreateXML(virConnectPtr conn,
                        const char *xmlDesc,
E
Eric Blake 已提交
5454
                        unsigned int flags)
5455 5456 5457 5458 5459 5460 5461 5462 5463
{
    testConnPtr driver = conn->privateData;
    virNodeDeviceDefPtr def = NULL;
    virNodeDeviceObjPtr obj = NULL;
    char *wwnn = NULL, *wwpn = NULL;
    int parent_host = -1;
    virNodeDevicePtr dev = NULL;
    virNodeDevCapsDefPtr caps;

E
Eric Blake 已提交
5464 5465
    virCheckFlags(0, NULL);

5466 5467
    testDriverLock(driver);

5468
    def = virNodeDeviceDefParseString(xmlDesc, CREATE_DEVICE, NULL);
5469 5470 5471 5472 5473
    if (def == NULL) {
        goto cleanup;
    }

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

5478
    if (virNodeDeviceGetParentHost(&driver->devs,
5479 5480 5481 5482 5483 5484 5485 5486 5487
                                   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);
5488
    if (VIR_STRDUP(def->name, wwpn) < 0)
5489 5490 5491 5492 5493 5494 5495 5496 5497
        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;

5498
        caps->data.scsi_host.host = virRandomBits(10);
5499 5500 5501 5502
        caps = caps->next;
    }


5503
    if (!(obj = virNodeDeviceAssignDef(&driver->devs, def))) {
5504 5505 5506 5507 5508 5509 5510 5511
        goto cleanup;
    }
    virNodeDeviceObjUnlock(obj);

    dev = virGetNodeDevice(conn, def->name);
    def = NULL;
cleanup:
    testDriverUnlock(driver);
5512
    virNodeDeviceDefFree(def);
5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531
    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) {
5532
        virReportError(VIR_ERR_NO_NODE_DEVICE, NULL);
5533 5534 5535
        goto out;
    }

5536
    if (virNodeDeviceGetWWNs(obj->def, &wwnn, &wwpn) == -1) {
5537 5538 5539
        goto out;
    }

5540
    if (VIR_STRDUP(parent_name, obj->def->parent) < 0)
5541 5542 5543 5544 5545 5546 5547 5548 5549
        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 */
5550
    if (virNodeDeviceGetParentHost(&driver->devs,
5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569
                                   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;
}

5570 5571

/* Domain event implementations */
5572
static int
5573 5574 5575 5576
testConnectDomainEventRegister(virConnectPtr conn,
                               virConnectDomainEventCallback callback,
                               void *opaque,
                               virFreeCallback freecb)
5577 5578 5579 5580 5581
{
    testConnPtr driver = conn->privateData;
    int ret;

    testDriverLock(driver);
5582 5583 5584
    ret = virDomainEventStateRegister(conn,
                                      driver->domainEventState,
                                      callback, opaque, freecb);
5585 5586 5587 5588 5589
    testDriverUnlock(driver);

    return ret;
}

5590

5591
static int
5592 5593
testConnectDomainEventDeregister(virConnectPtr conn,
                                 virConnectDomainEventCallback callback)
5594 5595 5596 5597 5598
{
    testConnPtr driver = conn->privateData;
    int ret;

    testDriverLock(driver);
5599 5600 5601
    ret = virDomainEventStateDeregister(conn,
                                        driver->domainEventState,
                                        callback);
5602 5603 5604 5605 5606
    testDriverUnlock(driver);

    return ret;
}

5607 5608

static int
5609 5610 5611 5612 5613 5614
testConnectDomainEventRegisterAny(virConnectPtr conn,
                                  virDomainPtr dom,
                                  int eventID,
                                  virConnectDomainEventGenericCallback callback,
                                  void *opaque,
                                  virFreeCallback freecb)
5615 5616 5617 5618 5619
{
    testConnPtr driver = conn->privateData;
    int ret;

    testDriverLock(driver);
5620 5621 5622 5623
    if (virDomainEventStateRegisterID(conn,
                                      driver->domainEventState,
                                      dom, eventID,
                                      callback, opaque, freecb, &ret) < 0)
5624
        ret = -1;
5625 5626 5627 5628 5629 5630
    testDriverUnlock(driver);

    return ret;
}

static int
5631 5632
testConnectDomainEventDeregisterAny(virConnectPtr conn,
                                    int callbackID)
5633 5634 5635 5636 5637
{
    testConnPtr driver = conn->privateData;
    int ret;

    testDriverLock(driver);
5638 5639 5640
    ret = virDomainEventStateDeregisterID(conn,
                                          driver->domainEventState,
                                          callbackID);
5641 5642 5643 5644 5645 5646
    testDriverUnlock(driver);

    return ret;
}


5647 5648 5649 5650
/* driver must be locked before calling */
static void testDomainEventQueue(testConnPtr driver,
                                 virDomainEventPtr event)
{
5651
    virDomainEventStateQueue(driver->domainEventState, event);
5652 5653
}

5654 5655
static virDrvOpenStatus testSecretOpen(virConnectPtr conn,
                                       virConnectAuthPtr auth ATTRIBUTE_UNUSED,
E
Eric Blake 已提交
5656 5657 5658 5659
                                       unsigned int flags)
{
    virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);

5660 5661 5662
    if (STRNEQ(conn->driver->name, "Test"))
        return VIR_DRV_OPEN_DECLINED;

5663
    conn->secretPrivateData = conn->privateData;
5664 5665 5666 5667 5668 5669 5670
    return VIR_DRV_OPEN_SUCCESS;
}

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

5672 5673 5674

static virDrvOpenStatus testNWFilterOpen(virConnectPtr conn,
                                         virConnectAuthPtr auth ATTRIBUTE_UNUSED,
E
Eric Blake 已提交
5675 5676 5677 5678
                                         unsigned int flags)
{
    virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);

5679 5680 5681
    if (STRNEQ(conn->driver->name, "Test"))
        return VIR_DRV_OPEN_DECLINED;

5682
    conn->nwfilterPrivateData = conn->privateData;
5683 5684 5685 5686 5687 5688 5689 5690
    return VIR_DRV_OPEN_SUCCESS;
}

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

5691 5692 5693
static int testConnectListAllDomains(virConnectPtr conn,
                                     virDomainPtr **domains,
                                     unsigned int flags)
5694 5695 5696 5697
{
    testConnPtr privconn = conn->privateData;
    int ret;

O
Osier Yang 已提交
5698
    virCheckFlags(VIR_CONNECT_LIST_DOMAINS_FILTERS_ALL, -1);
5699 5700

    testDriverLock(privconn);
5701
    ret = virDomainObjListExport(privconn->domains, conn, domains, flags);
5702 5703 5704 5705 5706
    testDriverUnlock(privconn);

    return ret;
}

5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736
static int
testNodeGetCPUMap(virConnectPtr conn,
                  unsigned char **cpumap,
                  unsigned int *online,
                  unsigned int flags)
{
    testConnPtr privconn = conn->privateData;
    int ret = -1;

    virCheckFlags(0, -1);

    testDriverLock(privconn);
    if (cpumap) {
        if (VIR_ALLOC_N(*cpumap, 1) < 0) {
            virReportOOMError();
            goto cleanup;
        }
        *cpumap[0] = 0x15;
    }

    if (online)
        *online = 3;

    ret = 8;

cleanup:
    testDriverUnlock(privconn);
    return ret;
}

5737 5738 5739 5740 5741 5742 5743 5744 5745 5746
static char *
testDomainScreenshot(virDomainPtr dom ATTRIBUTE_UNUSED,
                     virStreamPtr st,
                     unsigned int screen ATTRIBUTE_UNUSED,
                     unsigned int flags)
{
    char *ret = NULL;

    virCheckFlags(0, NULL);

5747
    if (VIR_STRDUP(ret, "image/png") < 0)
5748 5749 5750 5751 5752 5753 5754 5755
        return NULL;

    if (virFDStreamOpenFile(st, PKGDATADIR "/libvirtLogo.png", 0, 0, O_RDONLY < 0))
        VIR_FREE(ret);

    return ret;
}

5756

5757
static virDriver testDriver = {
5758 5759
    .no = VIR_DRV_TEST,
    .name = "Test",
5760 5761 5762
    .connectOpen = testConnectOpen, /* 0.1.1 */
    .connectClose = testConnectClose, /* 0.1.1 */
    .connectGetVersion = testConnectGetVersion, /* 0.1.1 */
5763
    .connectGetHostname = testConnectGetHostname, /* 0.6.3 */
5764
    .connectGetMaxVcpus = testConnectGetMaxVcpus, /* 0.3.2 */
5765
    .nodeGetInfo = testNodeGetInfo, /* 0.1.1 */
5766 5767 5768 5769
    .connectGetCapabilities = testConnectGetCapabilities, /* 0.2.1 */
    .connectListDomains = testConnectListDomains, /* 0.1.1 */
    .connectNumOfDomains = testConnectNumOfDomains, /* 0.1.1 */
    .connectListAllDomains = testConnectListAllDomains, /* 0.9.13 */
5770
    .domainCreateXML = testDomainCreateXML, /* 0.1.4 */
5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784
    .domainLookupByID = testDomainLookupByID, /* 0.1.1 */
    .domainLookupByUUID = testDomainLookupByUUID, /* 0.1.1 */
    .domainLookupByName = testDomainLookupByName, /* 0.1.1 */
    .domainSuspend = testDomainSuspend, /* 0.1.1 */
    .domainResume = testDomainResume, /* 0.1.1 */
    .domainShutdown = testDomainShutdown, /* 0.1.1 */
    .domainShutdownFlags = testDomainShutdownFlags, /* 0.9.10 */
    .domainReboot = testDomainReboot, /* 0.1.1 */
    .domainDestroy = testDomainDestroy, /* 0.1.1 */
    .domainGetOSType = testDomainGetOSType, /* 0.1.9 */
    .domainGetMaxMemory = testDomainGetMaxMemory, /* 0.1.4 */
    .domainSetMaxMemory = testDomainSetMaxMemory, /* 0.1.1 */
    .domainSetMemory = testDomainSetMemory, /* 0.1.4 */
    .domainGetInfo = testDomainGetInfo, /* 0.1.1 */
5785 5786
    .domainGetState = testDomainGetState, /* 0.9.2 */
    .domainSave = testDomainSave, /* 0.3.2 */
5787
    .domainSaveFlags = testDomainSaveFlags, /* 0.9.4 */
5788
    .domainRestore = testDomainRestore, /* 0.3.2 */
5789
    .domainRestoreFlags = testDomainRestoreFlags, /* 0.9.4 */
5790
    .domainCoreDump = testDomainCoreDump, /* 0.3.2 */
5791
    .domainSetVcpus = testDomainSetVcpus, /* 0.1.4 */
5792 5793 5794 5795 5796 5797
    .domainSetVcpusFlags = testDomainSetVcpusFlags, /* 0.8.5 */
    .domainGetVcpusFlags = testDomainGetVcpusFlags, /* 0.8.5 */
    .domainPinVcpu = testDomainPinVcpu, /* 0.7.3 */
    .domainGetVcpus = testDomainGetVcpus, /* 0.7.3 */
    .domainGetMaxVcpus = testDomainGetMaxVcpus, /* 0.7.3 */
    .domainGetXMLDesc = testDomainGetXMLDesc, /* 0.1.4 */
5798 5799
    .connectListDefinedDomains = testConnectListDefinedDomains, /* 0.1.11 */
    .connectNumOfDefinedDomains = testConnectNumOfDefinedDomains, /* 0.1.11 */
5800 5801 5802 5803
    .domainCreate = testDomainCreate, /* 0.1.11 */
    .domainCreateWithFlags = testDomainCreateWithFlags, /* 0.8.2 */
    .domainDefineXML = testDomainDefineXML, /* 0.1.11 */
    .domainUndefine = testDomainUndefine, /* 0.1.11 */
5804
    .domainUndefineFlags = testDomainUndefineFlags, /* 0.9.4 */
5805 5806 5807
    .domainGetAutostart = testDomainGetAutostart, /* 0.3.2 */
    .domainSetAutostart = testDomainSetAutostart, /* 0.3.2 */
    .domainGetSchedulerType = testDomainGetSchedulerType, /* 0.3.2 */
5808 5809 5810 5811
    .domainGetSchedulerParameters = testDomainGetSchedulerParameters, /* 0.3.2 */
    .domainGetSchedulerParametersFlags = testDomainGetSchedulerParametersFlags, /* 0.9.2 */
    .domainSetSchedulerParameters = testDomainSetSchedulerParameters, /* 0.3.2 */
    .domainSetSchedulerParametersFlags = testDomainSetSchedulerParametersFlags, /* 0.9.2 */
5812 5813 5814
    .domainBlockStats = testDomainBlockStats, /* 0.7.0 */
    .domainInterfaceStats = testDomainInterfaceStats, /* 0.7.0 */
    .nodeGetCellsFreeMemory = testNodeGetCellsFreeMemory, /* 0.4.2 */
5815 5816 5817 5818
    .connectDomainEventRegister = testConnectDomainEventRegister, /* 0.6.0 */
    .connectDomainEventDeregister = testConnectDomainEventDeregister, /* 0.6.0 */
    .connectIsEncrypted = testConnectIsEncrypted, /* 0.7.3 */
    .connectIsSecure = testConnectIsSecure, /* 0.7.3 */
5819 5820 5821
    .domainIsActive = testDomainIsActive, /* 0.7.3 */
    .domainIsPersistent = testDomainIsPersistent, /* 0.7.3 */
    .domainIsUpdated = testDomainIsUpdated, /* 0.8.6 */
5822 5823 5824
    .connectDomainEventRegisterAny = testConnectDomainEventRegisterAny, /* 0.8.0 */
    .connectDomainEventDeregisterAny = testConnectDomainEventDeregisterAny, /* 0.8.0 */
    .connectIsAlive = testConnectIsAlive, /* 0.9.8 */
5825
    .nodeGetCPUMap = testNodeGetCPUMap, /* 1.0.0 */
5826
    .domainScreenshot = testDomainScreenshot, /* 1.0.5 */
5827 5828 5829 5830
};

static virNetworkDriver testNetworkDriver = {
    "Test",
5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841
    .networkOpen = testNetworkOpen, /* 0.3.2 */
    .networkClose = testNetworkClose, /* 0.3.2 */
    .connectNumOfNetworks = testConnectNumOfNetworks, /* 0.3.2 */
    .connectListNetworks = testConnectListNetworks, /* 0.3.2 */
    .connectNumOfDefinedNetworks = testConnectNumOfDefinedNetworks, /* 0.3.2 */
    .connectListDefinedNetworks = testConnectListDefinedNetworks, /* 0.3.2 */
    .connectListAllNetworks = testConnectListAllNetworks, /* 0.10.2 */
    .networkLookupByUUID = testNetworkLookupByUUID, /* 0.3.2 */
    .networkLookupByName = testNetworkLookupByName, /* 0.3.2 */
    .networkCreateXML = testNetworkCreateXML, /* 0.3.2 */
    .networkDefineXML = testNetworkDefineXML, /* 0.3.2 */
5842
    .networkUndefine = testNetworkUndefine, /* 0.3.2 */
5843
    .networkUpdate = testNetworkUpdate, /* 0.10.2 */
5844
    .networkCreate = testNetworkCreate, /* 0.3.2 */
5845 5846 5847 5848 5849 5850 5851
    .networkDestroy = testNetworkDestroy, /* 0.3.2 */
    .networkGetXMLDesc = testNetworkGetXMLDesc, /* 0.3.2 */
    .networkGetBridgeName = testNetworkGetBridgeName, /* 0.3.2 */
    .networkGetAutostart = testNetworkGetAutostart, /* 0.3.2 */
    .networkSetAutostart = testNetworkSetAutostart, /* 0.3.2 */
    .networkIsActive = testNetworkIsActive, /* 0.7.3 */
    .networkIsPersistent = testNetworkIsPersistent, /* 0.7.3 */
5852 5853
};

L
Laine Stump 已提交
5854 5855
static virInterfaceDriver testInterfaceDriver = {
    "Test",                     /* name */
5856 5857 5858 5859 5860 5861 5862 5863
    .interfaceOpen = testInterfaceOpen, /* 0.7.0 */
    .interfaceClose = testInterfaceClose, /* 0.7.0 */
    .connectNumOfInterfaces = testConnectNumOfInterfaces, /* 0.7.0 */
    .connectListInterfaces = testConnectListInterfaces, /* 0.7.0 */
    .connectNumOfDefinedInterfaces = testConnectNumOfDefinedInterfaces, /* 0.7.0 */
    .connectListDefinedInterfaces = testConnectListDefinedInterfaces, /* 0.7.0 */
    .interfaceLookupByName = testInterfaceLookupByName, /* 0.7.0 */
    .interfaceLookupByMACString = testInterfaceLookupByMACString, /* 0.7.0 */
5864 5865 5866 5867 5868 5869
    .interfaceGetXMLDesc = testInterfaceGetXMLDesc, /* 0.7.0 */
    .interfaceDefineXML = testInterfaceDefineXML, /* 0.7.0 */
    .interfaceUndefine = testInterfaceUndefine, /* 0.7.0 */
    .interfaceCreate = testInterfaceCreate, /* 0.7.0 */
    .interfaceDestroy = testInterfaceDestroy, /* 0.7.0 */
    .interfaceIsActive = testInterfaceIsActive, /* 0.7.3 */
5870 5871 5872
    .interfaceChangeBegin = testInterfaceChangeBegin,   /* 0.9.2 */
    .interfaceChangeCommit = testInterfaceChangeCommit,  /* 0.9.2 */
    .interfaceChangeRollback = testInterfaceChangeRollback, /* 0.9.2 */
L
Laine Stump 已提交
5873 5874 5875
};


5876 5877
static virStorageDriver testStorageDriver = {
    .name = "Test",
5878 5879
    .storageOpen = testStorageOpen, /* 0.4.1 */
    .storageClose = testStorageClose, /* 0.4.1 */
5880

5881 5882 5883 5884 5885 5886
    .connectNumOfStoragePools = testConnectNumOfStoragePools, /* 0.5.0 */
    .connectListStoragePools = testConnectListStoragePools, /* 0.5.0 */
    .connectNumOfDefinedStoragePools = testConnectNumOfDefinedStoragePools, /* 0.5.0 */
    .connectListDefinedStoragePools = testConnectListDefinedStoragePools, /* 0.5.0 */
    .connectListAllStoragePools = testConnectListAllStoragePools, /* 0.10.2 */
    .connectFindStoragePoolSources = testConnectFindStoragePoolSources, /* 0.5.0 */
5887 5888 5889
    .storagePoolLookupByName = testStoragePoolLookupByName, /* 0.5.0 */
    .storagePoolLookupByUUID = testStoragePoolLookupByUUID, /* 0.5.0 */
    .storagePoolLookupByVolume = testStoragePoolLookupByVolume, /* 0.5.0 */
5890 5891
    .storagePoolCreateXML = testStoragePoolCreateXML, /* 0.5.0 */
    .storagePoolDefineXML = testStoragePoolDefineXML, /* 0.5.0 */
5892 5893
    .storagePoolBuild = testStoragePoolBuild, /* 0.5.0 */
    .storagePoolUndefine = testStoragePoolUndefine, /* 0.5.0 */
5894
    .storagePoolCreate = testStoragePoolCreate, /* 0.5.0 */
5895 5896 5897 5898 5899 5900 5901
    .storagePoolDestroy = testStoragePoolDestroy, /* 0.5.0 */
    .storagePoolDelete = testStoragePoolDelete, /* 0.5.0 */
    .storagePoolRefresh = testStoragePoolRefresh, /* 0.5.0 */
    .storagePoolGetInfo = testStoragePoolGetInfo, /* 0.5.0 */
    .storagePoolGetXMLDesc = testStoragePoolGetXMLDesc, /* 0.5.0 */
    .storagePoolGetAutostart = testStoragePoolGetAutostart, /* 0.5.0 */
    .storagePoolSetAutostart = testStoragePoolSetAutostart, /* 0.5.0 */
5902
    .storagePoolNumOfVolumes = testStoragePoolNumOfVolumes, /* 0.5.0 */
5903 5904 5905
    .storagePoolListVolumes = testStoragePoolListVolumes, /* 0.5.0 */
    .storagePoolListAllVolumes = testStoragePoolListAllVolumes, /* 0.10.2 */

5906 5907 5908 5909 5910 5911 5912 5913 5914
    .storageVolLookupByName = testStorageVolLookupByName, /* 0.5.0 */
    .storageVolLookupByKey = testStorageVolLookupByKey, /* 0.5.0 */
    .storageVolLookupByPath = testStorageVolLookupByPath, /* 0.5.0 */
    .storageVolCreateXML = testStorageVolCreateXML, /* 0.5.0 */
    .storageVolCreateXMLFrom = testStorageVolCreateXMLFrom, /* 0.6.4 */
    .storageVolDelete = testStorageVolDelete, /* 0.5.0 */
    .storageVolGetInfo = testStorageVolGetInfo, /* 0.5.0 */
    .storageVolGetXMLDesc = testStorageVolGetXMLDesc, /* 0.5.0 */
    .storageVolGetPath = testStorageVolGetPath, /* 0.5.0 */
5915 5916
    .storagePoolIsActive = testStoragePoolIsActive, /* 0.7.3 */
    .storagePoolIsPersistent = testStoragePoolIsPersistent, /* 0.7.3 */
5917 5918
};

5919
static virNodeDeviceDriver testNodeDeviceDriver = {
5920
    .name = "Test",
5921 5922
    .nodeDeviceOpen = testNodeDeviceOpen, /* 0.6.0 */
    .nodeDeviceClose = testNodeDeviceClose, /* 0.6.0 */
5923 5924 5925 5926 5927 5928 5929 5930 5931 5932

    .nodeNumOfDevices = testNodeNumOfDevices, /* 0.7.2 */
    .nodeListDevices = testNodeListDevices, /* 0.7.2 */
    .nodeDeviceLookupByName = testNodeDeviceLookupByName, /* 0.7.2 */
    .nodeDeviceGetXMLDesc = testNodeDeviceGetXMLDesc, /* 0.7.2 */
    .nodeDeviceGetParent = testNodeDeviceGetParent, /* 0.7.2 */
    .nodeDeviceNumOfCaps = testNodeDeviceNumOfCaps, /* 0.7.2 */
    .nodeDeviceListCaps = testNodeDeviceListCaps, /* 0.7.2 */
    .nodeDeviceCreateXML = testNodeDeviceCreateXML, /* 0.7.3 */
    .nodeDeviceDestroy = testNodeDeviceDestroy, /* 0.7.3 */
5933 5934
};

5935 5936
static virSecretDriver testSecretDriver = {
    .name = "Test",
5937 5938
    .secretOpen = testSecretOpen, /* 0.7.1 */
    .secretClose = testSecretClose, /* 0.7.1 */
5939
};
5940 5941


5942 5943
static virNWFilterDriver testNWFilterDriver = {
    .name = "Test",
5944 5945
    .nwfilterOpen = testNWFilterOpen, /* 0.8.0 */
    .nwfilterClose = testNWFilterClose, /* 0.8.0 */
5946 5947
};

5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959
/**
 * testRegister:
 *
 * Registers the test driver
 */
int
testRegister(void)
{
    if (virRegisterDriver(&testDriver) < 0)
        return -1;
    if (virRegisterNetworkDriver(&testNetworkDriver) < 0)
        return -1;
L
Laine Stump 已提交
5960 5961
    if (virRegisterInterfaceDriver(&testInterfaceDriver) < 0)
        return -1;
5962 5963
    if (virRegisterStorageDriver(&testStorageDriver) < 0)
        return -1;
5964
    if (virRegisterNodeDeviceDriver(&testNodeDeviceDriver) < 0)
5965
        return -1;
5966 5967
    if (virRegisterSecretDriver(&testSecretDriver) < 0)
        return -1;
5968 5969
    if (virRegisterNWFilterDriver(&testNWFilterDriver) < 0)
        return -1;
5970

5971 5972
    return 0;
}