test_driver.c 166.9 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, true, NULL, NULL);
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, NULL, NULL);
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, false, NULL, NULL);
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 2494
    n = virDomainObjListGetInactiveNames(privconn->domains, names, maxnames,
                                         NULL, NULL);
2495
    testDriverUnlock(privconn);
2496

2497
    return n;
2498 2499
}

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

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

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

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

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

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

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

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

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

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


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

2580 2581
    virCheckFlags(0, -1);

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

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

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

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

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

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

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

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

2628 2629
    virCheckFlags(0, -1);

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

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

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

2650
    ret = 0;
2651

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

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

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

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

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

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

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


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

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

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

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

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

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

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

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

2729 2730 2731
    return type;
}

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

2742 2743
    virCheckFlags(0, -1);

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

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

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

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

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

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

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

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

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

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

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

2811 2812 2813
    ret = 0;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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


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

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

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

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

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

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

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

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

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

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


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

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

3013
    return numActive;
3014 3015
}

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

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

3033 3034
    return n;

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

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

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

3055
    return numInactive;
3056 3057
}

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

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

3075 3076
    return n;

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

3084
static int
3085
testConnectListAllNetworks(virConnectPtr conn,
3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099
                           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;
}
3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110

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

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


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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275
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;
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

C
Cole Robinson 已提交
3442

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

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

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

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

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


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

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

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

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

    return n;

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

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

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

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

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

    return n;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

3640 3641
    testDriverLock(privconn);
    if (privconn->transaction_running) {
3642
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
3643
                       _("there is another transaction running."));
3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659
        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 已提交
3660
                                     unsigned int flags)
3661 3662 3663 3664
{
    testConnPtr privconn = conn->privateData;
    int ret = -1;

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

3667 3668 3669
    testDriverLock(privconn);

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

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

    ret = 0;

cleanup:
    testDriverUnlock(privconn);

    return ret;
}

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

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

3695 3696 3697
    testDriverLock(privconn);

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

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

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

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

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

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

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


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

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

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

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

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

cleanup:
    testDriverUnlock(privconn);
    return ret;
}

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

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

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

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

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

    privinterface->active = 1;
    ret = 0;

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

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

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

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

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

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

    privinterface->active = 0;
    ret = 0;

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



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

3871

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

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

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

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

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

3899

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

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

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

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

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

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

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

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

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

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

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

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

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

    return numActive;
}

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

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

    return n;

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

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

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

    return numInactive;
}

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

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

    return n;

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

4044
static int
4045 4046 4047
testConnectListAllStoragePools(virConnectPtr conn,
                               virStoragePoolPtr **pools,
                               unsigned int flags)
4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059
{
    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 已提交
4060

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

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



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

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

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

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

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

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

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

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

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

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

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

    switch (pool_type) {

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

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

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

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

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


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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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


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

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

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

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

    privpool->active = 0;

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

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


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

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

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

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

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

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

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


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

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

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

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

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

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


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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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


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

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

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

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

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

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

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

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

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

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


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

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

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

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

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

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

4694
    for (i = 0; i < pool->volumes.count; i++) {
4695 4696
        if (!(vol = virGetStorageVol(obj->conn, pool->def->name,
                                     pool->volumes.objs[i]->name,
4697 4698
                                     pool->volumes.objs[i]->key,
                                     NULL, NULL)))
4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712
            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]);
        }
4713
        VIR_FREE(tmp_vols);
4714 4715 4716 4717 4718 4719 4720
    }

    if (pool)
        virStoragePoolObjUnlock(pool);

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

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

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

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


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

    privvol = virStorageVolDefFindByName(privpool, name);

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

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

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


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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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


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

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

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


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

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

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


static int testStorageVolumeTypeForPool(int pooltype) {

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

5221

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

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

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

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

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

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

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

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

5275 5276 5277 5278 5279
    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)) {
5280
            if (VIR_STRDUP(names[ndevs++], driver->devs.objs[i]->def->name) < 0) {
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 5310
                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) {
5311
        virReportError(VIR_ERR_NO_NODE_DEVICE, NULL);
5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323
        goto cleanup;
    }

    ret = virGetNodeDevice(conn, name);

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

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

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

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

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

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

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

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

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

5383

5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397
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) {
5398 5399 5400
        virReportError(VIR_ERR_NO_NODE_DEVICE,
                       _("no node device with matching name '%s'"),
                       dev->name);
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 5428
        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) {
5429 5430 5431
        virReportError(VIR_ERR_NO_NODE_DEVICE,
                       _("no node device with matching name '%s'"),
                       dev->name);
5432 5433 5434 5435
        goto cleanup;
    }

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

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

5452 5453 5454
static virNodeDevicePtr
testNodeDeviceCreateXML(virConnectPtr conn,
                        const char *xmlDesc,
E
Eric Blake 已提交
5455
                        unsigned int flags)
5456 5457 5458 5459 5460 5461 5462 5463 5464
{
    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 已提交
5465 5466
    virCheckFlags(0, NULL);

5467 5468
    testDriverLock(driver);

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

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

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

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


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

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

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

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

5571 5572

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

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

    return ret;
}

5591

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

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

    return ret;
}

5608 5609

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

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

    return ret;
}

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

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

    return ret;
}


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

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

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

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

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

5673 5674 5675

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

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

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

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

5692

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

O
Osier Yang 已提交
5700
    virCheckFlags(VIR_CONNECT_LIST_DOMAINS_FILTERS_ALL, -1);
5701 5702

    testDriverLock(privconn);
5703 5704
    ret = virDomainObjListExport(privconn->domains, conn, domains,
                                 NULL, flags);
5705 5706 5707 5708 5709
    testDriverUnlock(privconn);

    return ret;
}

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 5737 5738 5739
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;
}

5740 5741 5742 5743 5744 5745 5746 5747 5748 5749
static char *
testDomainScreenshot(virDomainPtr dom ATTRIBUTE_UNUSED,
                     virStreamPtr st,
                     unsigned int screen ATTRIBUTE_UNUSED,
                     unsigned int flags)
{
    char *ret = NULL;

    virCheckFlags(0, NULL);

5750
    if (VIR_STRDUP(ret, "image/png") < 0)
5751 5752 5753 5754 5755 5756 5757 5758
        return NULL;

    if (virFDStreamOpenFile(st, PKGDATADIR "/libvirtLogo.png", 0, 0, O_RDONLY < 0))
        VIR_FREE(ret);

    return ret;
}

5759

5760
static virDriver testDriver = {
5761 5762
    .no = VIR_DRV_TEST,
    .name = "Test",
5763 5764 5765
    .connectOpen = testConnectOpen, /* 0.1.1 */
    .connectClose = testConnectClose, /* 0.1.1 */
    .connectGetVersion = testConnectGetVersion, /* 0.1.1 */
5766
    .connectGetHostname = testConnectGetHostname, /* 0.6.3 */
5767
    .connectGetMaxVcpus = testConnectGetMaxVcpus, /* 0.3.2 */
5768
    .nodeGetInfo = testNodeGetInfo, /* 0.1.1 */
5769 5770 5771 5772
    .connectGetCapabilities = testConnectGetCapabilities, /* 0.2.1 */
    .connectListDomains = testConnectListDomains, /* 0.1.1 */
    .connectNumOfDomains = testConnectNumOfDomains, /* 0.1.1 */
    .connectListAllDomains = testConnectListAllDomains, /* 0.9.13 */
5773
    .domainCreateXML = testDomainCreateXML, /* 0.1.4 */
5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787
    .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 */
5788 5789
    .domainGetState = testDomainGetState, /* 0.9.2 */
    .domainSave = testDomainSave, /* 0.3.2 */
5790
    .domainSaveFlags = testDomainSaveFlags, /* 0.9.4 */
5791
    .domainRestore = testDomainRestore, /* 0.3.2 */
5792
    .domainRestoreFlags = testDomainRestoreFlags, /* 0.9.4 */
5793
    .domainCoreDump = testDomainCoreDump, /* 0.3.2 */
5794
    .domainSetVcpus = testDomainSetVcpus, /* 0.1.4 */
5795 5796 5797 5798 5799 5800
    .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 */
5801 5802
    .connectListDefinedDomains = testConnectListDefinedDomains, /* 0.1.11 */
    .connectNumOfDefinedDomains = testConnectNumOfDefinedDomains, /* 0.1.11 */
5803 5804 5805 5806
    .domainCreate = testDomainCreate, /* 0.1.11 */
    .domainCreateWithFlags = testDomainCreateWithFlags, /* 0.8.2 */
    .domainDefineXML = testDomainDefineXML, /* 0.1.11 */
    .domainUndefine = testDomainUndefine, /* 0.1.11 */
5807
    .domainUndefineFlags = testDomainUndefineFlags, /* 0.9.4 */
5808 5809 5810
    .domainGetAutostart = testDomainGetAutostart, /* 0.3.2 */
    .domainSetAutostart = testDomainSetAutostart, /* 0.3.2 */
    .domainGetSchedulerType = testDomainGetSchedulerType, /* 0.3.2 */
5811 5812 5813 5814
    .domainGetSchedulerParameters = testDomainGetSchedulerParameters, /* 0.3.2 */
    .domainGetSchedulerParametersFlags = testDomainGetSchedulerParametersFlags, /* 0.9.2 */
    .domainSetSchedulerParameters = testDomainSetSchedulerParameters, /* 0.3.2 */
    .domainSetSchedulerParametersFlags = testDomainSetSchedulerParametersFlags, /* 0.9.2 */
5815 5816 5817
    .domainBlockStats = testDomainBlockStats, /* 0.7.0 */
    .domainInterfaceStats = testDomainInterfaceStats, /* 0.7.0 */
    .nodeGetCellsFreeMemory = testNodeGetCellsFreeMemory, /* 0.4.2 */
5818 5819 5820 5821
    .connectDomainEventRegister = testConnectDomainEventRegister, /* 0.6.0 */
    .connectDomainEventDeregister = testConnectDomainEventDeregister, /* 0.6.0 */
    .connectIsEncrypted = testConnectIsEncrypted, /* 0.7.3 */
    .connectIsSecure = testConnectIsSecure, /* 0.7.3 */
5822 5823 5824
    .domainIsActive = testDomainIsActive, /* 0.7.3 */
    .domainIsPersistent = testDomainIsPersistent, /* 0.7.3 */
    .domainIsUpdated = testDomainIsUpdated, /* 0.8.6 */
5825 5826 5827
    .connectDomainEventRegisterAny = testConnectDomainEventRegisterAny, /* 0.8.0 */
    .connectDomainEventDeregisterAny = testConnectDomainEventDeregisterAny, /* 0.8.0 */
    .connectIsAlive = testConnectIsAlive, /* 0.9.8 */
5828
    .nodeGetCPUMap = testNodeGetCPUMap, /* 1.0.0 */
5829
    .domainScreenshot = testDomainScreenshot, /* 1.0.5 */
5830 5831 5832 5833
};

static virNetworkDriver testNetworkDriver = {
    "Test",
5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844
    .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 */
5845
    .networkUndefine = testNetworkUndefine, /* 0.3.2 */
5846
    .networkUpdate = testNetworkUpdate, /* 0.10.2 */
5847
    .networkCreate = testNetworkCreate, /* 0.3.2 */
5848 5849 5850 5851 5852 5853 5854
    .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 */
5855 5856
};

L
Laine Stump 已提交
5857 5858
static virInterfaceDriver testInterfaceDriver = {
    "Test",                     /* name */
5859 5860 5861 5862 5863 5864 5865 5866
    .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 */
5867 5868 5869 5870 5871 5872
    .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 */
5873 5874 5875
    .interfaceChangeBegin = testInterfaceChangeBegin,   /* 0.9.2 */
    .interfaceChangeCommit = testInterfaceChangeCommit,  /* 0.9.2 */
    .interfaceChangeRollback = testInterfaceChangeRollback, /* 0.9.2 */
L
Laine Stump 已提交
5876 5877 5878
};


5879 5880
static virStorageDriver testStorageDriver = {
    .name = "Test",
5881 5882
    .storageOpen = testStorageOpen, /* 0.4.1 */
    .storageClose = testStorageClose, /* 0.4.1 */
5883

5884 5885 5886 5887 5888 5889
    .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 */
5890 5891 5892
    .storagePoolLookupByName = testStoragePoolLookupByName, /* 0.5.0 */
    .storagePoolLookupByUUID = testStoragePoolLookupByUUID, /* 0.5.0 */
    .storagePoolLookupByVolume = testStoragePoolLookupByVolume, /* 0.5.0 */
5893 5894
    .storagePoolCreateXML = testStoragePoolCreateXML, /* 0.5.0 */
    .storagePoolDefineXML = testStoragePoolDefineXML, /* 0.5.0 */
5895 5896
    .storagePoolBuild = testStoragePoolBuild, /* 0.5.0 */
    .storagePoolUndefine = testStoragePoolUndefine, /* 0.5.0 */
5897
    .storagePoolCreate = testStoragePoolCreate, /* 0.5.0 */
5898 5899 5900 5901 5902 5903 5904
    .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 */
5905
    .storagePoolNumOfVolumes = testStoragePoolNumOfVolumes, /* 0.5.0 */
5906 5907 5908
    .storagePoolListVolumes = testStoragePoolListVolumes, /* 0.5.0 */
    .storagePoolListAllVolumes = testStoragePoolListAllVolumes, /* 0.10.2 */

5909 5910 5911 5912 5913 5914 5915 5916 5917
    .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 */
5918 5919
    .storagePoolIsActive = testStoragePoolIsActive, /* 0.7.3 */
    .storagePoolIsPersistent = testStoragePoolIsPersistent, /* 0.7.3 */
5920 5921
};

5922
static virNodeDeviceDriver testNodeDeviceDriver = {
5923
    .name = "Test",
5924 5925
    .nodeDeviceOpen = testNodeDeviceOpen, /* 0.6.0 */
    .nodeDeviceClose = testNodeDeviceClose, /* 0.6.0 */
5926 5927 5928 5929 5930 5931 5932 5933 5934 5935

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

5938 5939
static virSecretDriver testSecretDriver = {
    .name = "Test",
5940 5941
    .secretOpen = testSecretOpen, /* 0.7.1 */
    .secretClose = testSecretClose, /* 0.7.1 */
5942
};
5943 5944


5945 5946
static virNWFilterDriver testNWFilterDriver = {
    .name = "Test",
5947 5948
    .nwfilterOpen = testNWFilterOpen, /* 0.8.0 */
    .nwfilterClose = testNWFilterClose, /* 0.8.0 */
5949 5950
};

5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962
/**
 * testRegister:
 *
 * Registers the test driver
 */
int
testRegister(void)
{
    if (virRegisterDriver(&testDriver) < 0)
        return -1;
    if (virRegisterNetworkDriver(&testNetworkDriver) < 0)
        return -1;
L
Laine Stump 已提交
5963 5964
    if (virRegisterInterfaceDriver(&testInterfaceDriver) < 0)
        return -1;
5965 5966
    if (virRegisterStorageDriver(&testStorageDriver) < 0)
        return -1;
5967
    if (virRegisterNodeDeviceDriver(&testNodeDeviceDriver) < 0)
5968
        return -1;
5969 5970
    if (virRegisterSecretDriver(&testSecretDriver) < 0)
        return -1;
5971 5972
    if (virRegisterNWFilterDriver(&testNWFilterDriver) < 0)
        return -1;
5973

5974 5975
    return 0;
}