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

24 25
#include "config.h"

26
#ifdef WITH_TEST
27

28 29 30
#include <stdio.h>
#include <string.h>
#include <sys/time.h>
31 32 33
#include <libxml/parser.h>
#include <libxml/tree.h>
#include <libxml/xpath.h>
34
#include <libxml/uri.h>
35 36
#include <fcntl.h>
#include <unistd.h>
37 38
#include <sys/stat.h>

39
#include "socketcompat.h"
40 41 42

#include "internal.h"
#include "test.h"
43
#include "xml.h"
44
#include "buf.h"
45
#include "uuid.h"
46

47 48 49 50 51 52 53 54 55
/* Flags that determine the action to take on a shutdown or crash of a domain
 */
typedef enum {
     VIR_DOMAIN_DESTROY	= 1, /* destroy the domain */
     VIR_DOMAIN_RESTART	= 2, /* restart the domain */
     VIR_DOMAIN_PRESERVE= 3, /* keep as is, need manual destroy, for debug */
     VIR_DOMAIN_RENAME_RESTART= 4/* restart under an new unique name */
} virDomainRestart;

56
struct _testDev {
57
    char name[20];
58
    int mode;
59 60 61
};
typedef struct _testDev testDev;
typedef struct _testDev *testDevPtr;
62 63 64

#define MAX_DEVICES 10

65
struct _testDom {
66
    int active;
67
    int config;
68
    int id;
69
    char name[20];
70
    unsigned char uuid[VIR_UUID_BUFLEN];
71 72 73 74 75 76 77 78
    virDomainInfo info;
    unsigned int maxVCPUs;
    virDomainRestart onRestart; /* What to do at end of current shutdown procedure */
    virDomainRestart onReboot;
    virDomainRestart onPoweroff;
    virDomainRestart onCrash;
    int numDevices;
    testDev devices[MAX_DEVICES];
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
    int autostart;
    unsigned int weight;
};
typedef struct _testDom testDom;
typedef struct _testDom *testDomPtr;

struct _testNet {
    int active;
    int config;
    int running;
    char name[20];
    char bridge[20];
    unsigned char uuid[VIR_UUID_BUFLEN];
    int forward;
    char forwardDev[IF_NAMESIZE];
    char ipAddress[INET_ADDRSTRLEN];
    char ipNetmask[INET_ADDRSTRLEN];

    char dhcpStart[INET_ADDRSTRLEN];
    char dhcpEnd[INET_ADDRSTRLEN];

    int autostart;
};
typedef struct _testNet testNet;
typedef struct _testNet *testNetPtr;
104 105

#define MAX_DOMAINS 20
106
#define MAX_NETWORKS 20
107

108 109 110
struct _testConn {
    char path[PATH_MAX];
    int nextDomID;
111 112 113
    virNodeInfo nodeInfo;
    int numDomains;
    testDom domains[MAX_DOMAINS];
114 115 116 117 118
    int numNetworks;
    testNet networks[MAX_NETWORKS];
};
typedef struct _testConn testConn;
typedef struct _testConn *testConnPtr;
119

120 121 122
#define TEST_MODEL "i686"
#define TEST_MODEL_WORDSIZE "32"

123
static const virNodeInfo defaultNodeInfo = {
124
    TEST_MODEL,
125 126 127 128 129 130 131
    1024*1024*3, /* 3 GB */
    16,
    1400,
    2,
    2,
    2,
    2,
132 133
};

134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
#define GET_DOMAIN(dom, ret)                                            \
    int domidx;                                                         \
    testConnPtr privconn;                                               \
    testDomPtr privdom;                                                 \
                                                                        \
    privconn = (testConnPtr)dom->conn->privateData;                     \
    if ((domidx = getDomainIndex(dom)) < 0) {                           \
        testError((dom)->conn, (dom), NULL, VIR_ERR_INVALID_ARG,        \
                  __FUNCTION__);                                        \
        return (ret);                                                   \
    }                                                                   \
    privdom = &privconn->domains[domidx];

#define GET_NETWORK(net, ret)                                           \
    int netidx;                                                         \
    testConnPtr privconn;                                               \
    testNetPtr privnet;                                                 \
                                                                        \
    privconn = (testConnPtr)net->conn->privateData;                     \
    if ((netidx = getNetworkIndex(net)) < 0) {                          \
        testError((net)->conn, NULL, (net), VIR_ERR_INVALID_ARG,        \
                  __FUNCTION__);                                        \
        return (ret);                                                   \
    }                                                                   \
    privnet = &privconn->networks[netidx];

#define GET_CONNECTION(conn, ret)                                       \
    testConnPtr privconn;                                               \
                                                                        \
    privconn = (testConnPtr)conn->privateData;


166 167
static void
testError(virConnectPtr con,
168
          virDomainPtr dom,
169
          virNetworkPtr net,
170 171
          virErrorNumber error,
          const char *info)
172
{
173
    const char *errmsg;
174

175 176
    if (error == VIR_ERR_OK)
        return;
177

178
    errmsg = __virErrorMsg(error, info);
179
    __virRaiseError(con, dom, net, VIR_FROM_TEST, error, VIR_ERR_ERROR,
180
                    errmsg, info, NULL, 0, 0, errmsg, info, 0);
181 182
}

183
static int testRestartStringToFlag(const char *str) {
184 185 186 187 188 189 190 191 192 193 194
    if (!strcmp(str, "restart")) {
        return VIR_DOMAIN_RESTART;
    } else if (!strcmp(str, "destroy")) {
        return VIR_DOMAIN_DESTROY;
    } else if (!strcmp(str, "preserve")) {
        return VIR_DOMAIN_PRESERVE;
    } else if (!strcmp(str, "rename-restart")) {
        return VIR_DOMAIN_RENAME_RESTART;
    } else {
        return (0);
    }
195 196 197
}

static const char *testRestartFlagToString(int flag) {
198 199 200 201 202 203 204 205 206 207 208
    switch (flag) {
    case VIR_DOMAIN_RESTART:
        return "restart";
    case VIR_DOMAIN_DESTROY:
        return "destroy";
    case VIR_DOMAIN_PRESERVE:
        return "preserve";
    case VIR_DOMAIN_RENAME_RESTART:
        return "rename-restart";
    }
    return (NULL);
209
}
210 211


212
static int testLoadDomain(virConnectPtr conn,
213 214 215 216 217
                          int domid,
                          xmlDocPtr xml) {
    xmlNodePtr root = NULL;
    xmlXPathContextPtr ctxt = NULL;
    char *name = NULL;
218
    unsigned char uuid[VIR_UUID_BUFLEN];
219 220 221 222
    struct timeval tv;
    unsigned long memory = 0;
    unsigned long maxMem = 0;
    int nrVirtCpu;
223 224 225
    char *str;
    int handle = -1, i, ret;
    long l;
226 227 228
    virDomainRestart onReboot = VIR_DOMAIN_RESTART;
    virDomainRestart onPoweroff = VIR_DOMAIN_DESTROY;
    virDomainRestart onCrash = VIR_DOMAIN_RENAME_RESTART;
229
    GET_CONNECTION(conn, -1);
230 231

    if (gettimeofday(&tv, NULL) < 0) {
232
        testError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR, _("getting time of day"));
233
        return (-1);
234 235
    }

236 237
    root = xmlDocGetRootElement(xml);
    if ((root == NULL) || (!xmlStrEqual(root->name, BAD_CAST "domain"))) {
238
        testError(conn, NULL, NULL, VIR_ERR_XML_ERROR, _("domain"));
239
        goto error;
240
    }
241 242 243

    ctxt = xmlXPathNewContext(xml);
    if (ctxt == NULL) {
244
        testError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR, _("creating xpath context"));
245 246 247
        goto error;
    }

248 249
    name = virXPathString("string(/domain/name[1])", ctxt);
    if (name == NULL) {
250
        testError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR, _("domain name"));
251 252
        goto error;
    }
253

254 255
    str = virXPathString("string(/domain/uuid[1])", ctxt);
    if (str == NULL) {
256
        testError(conn, NULL, NULL, VIR_ERR_XML_ERROR, _("domain uuid"));
257 258
        goto error;
    }
259
    if (virUUIDParse(str, uuid) < 0) {
260
        testError(conn, NULL, NULL, VIR_ERR_XML_ERROR, _("domain uuid"));
261
        goto error;
262
    }
263
    free(str);
264

265 266 267

    ret = virXPathLong("string(/domain/memory[1])", ctxt, &l);
    if (ret != 0) {
268
        testError(conn, NULL, NULL, VIR_ERR_XML_ERROR, _("domain memory"));
269
        goto error;
270
    }
271
    maxMem = l;
272

273 274
    ret = virXPathLong("string(/domain/currentMemory[1])", ctxt, &l);
    if (ret == -1) {
275
        memory = maxMem;
276
    } else if (ret == -2) {
277 278
        testError(conn, NULL, NULL, VIR_ERR_XML_ERROR, _("domain current memory"));
        goto error;
279
    } else {
280
        memory = l;
281 282
    }

283 284
    ret = virXPathLong("string(/domain/vcpu[1])", ctxt, &l);
    if (ret == -1) {
285
        nrVirtCpu = 1;
286
    } else if (ret == -2) {
287 288
        testError(conn, NULL, NULL, VIR_ERR_XML_ERROR, _("domain vcpus"));
        goto error;
289
    } else {
290
        nrVirtCpu = l;
291 292
    }

293 294 295
    str = virXPathString("string(/domain/on_reboot[1])", ctxt);
    if (str != NULL) {
        if (!(onReboot = testRestartStringToFlag(str))) {
296 297
            testError(conn, NULL, NULL, VIR_ERR_XML_ERROR, _("domain reboot behaviour"));
            free(str);
298 299
            goto error;
        }
300
        free(str);
301 302
    }

303 304
    str = virXPathString("string(/domain/on_poweroff[1])", ctxt);
    if (str != NULL) {
305 306 307
        if (!(onPoweroff = testRestartStringToFlag(str))) {
            testError(conn, NULL, NULL, VIR_ERR_XML_ERROR, _("domain poweroff behaviour"));
            free(str);
308 309
            goto error;
        }
310
        free(str);
311 312
    }

313 314
    str = virXPathString("string(/domain/on_crash[1])", ctxt);
    if (str != NULL) {
315 316 317
        if (!(onCrash = testRestartStringToFlag(str))) {
            testError(conn, NULL, NULL, VIR_ERR_XML_ERROR, _("domain crash behaviour"));
            free(str);
318 319
            goto error;
        }
320
        free(str);
321
    }
322

323
    for (i = 0 ; i < MAX_DOMAINS ; i++) {
324
        if (!privconn->domains[i].active) {
325 326 327 328 329 330 331
            handle = i;
            break;
        }
    }
    if (handle < 0)
        return (-1);

332 333 334 335
    privconn->domains[handle].active = 1;
    privconn->domains[handle].id = domid;
    strncpy(privconn->domains[handle].name, name, sizeof(privconn->domains[handle].name)-1);
    privconn->domains[handle].name[sizeof(privconn->domains[handle].name)-1] = '\0';
336
    free(name);
337 338 339 340 341
    name = NULL;

    if (memory > maxMem)
        memory = maxMem;

342
    memmove(privconn->domains[handle].uuid, uuid, VIR_UUID_BUFLEN);
343 344 345 346 347 348
    privconn->domains[handle].info.maxMem = maxMem;
    privconn->domains[handle].info.memory = memory;
    privconn->domains[handle].info.state = domid < 0 ? VIR_DOMAIN_SHUTOFF : VIR_DOMAIN_RUNNING;
    privconn->domains[handle].info.nrVirtCpu = nrVirtCpu;
    privconn->domains[handle].info.cpuTime = ((tv.tv_sec * 1000ll * 1000ll  * 1000ll) + (tv.tv_usec * 1000ll));
    privconn->domains[handle].maxVCPUs = nrVirtCpu;
349

350 351 352
    privconn->domains[handle].onReboot = onReboot;
    privconn->domains[handle].onPoweroff = onPoweroff;
    privconn->domains[handle].onCrash = onCrash;
353

354
    return (handle);
355 356 357 358 359

 error:
    if (name)
        free(name);
    return (-1);
360 361 362
}

static int testLoadDomainFromDoc(virConnectPtr conn,
363 364 365 366 367 368 369
                                 int domid,
                                 const char *doc) {
    int ret;
    xmlDocPtr xml;
    if (!(xml = xmlReadDoc(BAD_CAST doc, "domain.xml", NULL,
                           XML_PARSE_NOENT | XML_PARSE_NONET |
                           XML_PARSE_NOERROR | XML_PARSE_NOWARNING))) {
370
        testError(conn, NULL, NULL, VIR_ERR_XML_ERROR, _("domain"));
371 372 373 374 375 376 377 378
        return (-1);
    }

    ret = testLoadDomain(conn, domid, xml);

    xmlFreeDoc(xml);

    return (ret);
379 380
}

381

382
static int testLoadDomainFromFile(virConnectPtr conn,
383
                                  int domid,
384
                                  const char *filename) {
385 386 387
    int ret, fd;
    xmlDocPtr xml;

388 389
    if ((fd = open(filename, O_RDONLY)) < 0) {
        testError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR, _("load domain definition file"));
390 391 392
        return (-1);
    }

393
    if (!(xml = xmlReadFd(fd, filename, NULL,
394 395
                          XML_PARSE_NOENT | XML_PARSE_NONET |
                          XML_PARSE_NOERROR | XML_PARSE_NOWARNING))) {
396
        testError(conn, NULL, NULL, VIR_ERR_XML_ERROR, _("domain"));
397 398
        return (-1);
    }
399

400
    ret = testLoadDomain(conn, domid, xml);
401

402
    xmlFreeDoc(xml);
403 404 405 406 407 408 409 410 411 412 413
    close(fd);

    return (ret);
}


static int testLoadNetwork(virConnectPtr conn,
                           xmlDocPtr xml) {
    xmlNodePtr root = NULL;
    xmlXPathContextPtr ctxt = NULL;
    char *name = NULL, *bridge = NULL;
414
    unsigned char uuid[VIR_UUID_BUFLEN];
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
    char *str;
    char *ipaddress = NULL, *ipnetmask = NULL, *dhcpstart = NULL, *dhcpend = NULL;
    int forward;
    char *forwardDev = NULL;
    int handle = -1, i;
    GET_CONNECTION(conn, -1);

    root = xmlDocGetRootElement(xml);
    if ((root == NULL) || (!xmlStrEqual(root->name, BAD_CAST "network"))) {
        testError(conn, NULL, NULL, VIR_ERR_XML_ERROR, _("network"));
        goto error;
    }

    ctxt = xmlXPathNewContext(xml);
    if (ctxt == NULL) {
        testError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR, _("creating xpath context"));
        goto error;
    }

    name = virXPathString("string(/network/name[1])", ctxt);
    if (name == NULL) {
        testError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR, _("network name"));
        goto error;
    }

    bridge = virXPathString("string(/network/bridge[1]/@name)", ctxt);

    str = virXPathString("string(/network/uuid[1])", ctxt);
    if (str == NULL) {
        testError(conn, NULL, NULL, VIR_ERR_XML_ERROR, _("network uuid"));
        goto error;
    }
447
    if (virUUIDParse(str, uuid) < 0) {
448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
        testError(conn, NULL, NULL, VIR_ERR_XML_ERROR, _("network uuid"));
        goto error;
    }
    free(str);


    forward = virXPathBoolean("count(/network/forward) != 0", ctxt);
    if (forward < 0) {
        testError(conn, NULL, NULL, VIR_ERR_XML_ERROR, _("network forward"));
        goto error;
    }

    forwardDev = virXPathString("string(/network/forward/@dev)", ctxt);


    ipaddress = virXPathString("string(/network/ip/@address)", ctxt);
    if (ipaddress == NULL) {
        testError(conn, NULL, NULL, VIR_ERR_XML_ERROR, _("ip address"));
        goto error;
    }
    ipnetmask = virXPathString("string(/network/ip/@netmask)", ctxt);
    if (ipnetmask == NULL) {
        testError(conn, NULL, NULL, VIR_ERR_XML_ERROR, _("ip netmask"));
        goto error;
    }
    dhcpstart = virXPathString("string(/network/ip/dhcp/range[1]/@start)", ctxt);
    if (dhcpstart == NULL) {
        testError(conn, NULL, NULL, VIR_ERR_XML_ERROR, _("ip address"));
        goto error;
    }
    dhcpend = virXPathString("string(/network/ip/dhcp/range[1]/@end)", ctxt);
    if (dhcpend == NULL) {
        testError(conn, NULL, NULL, VIR_ERR_XML_ERROR, _("ip address"));
        goto error;
    }

    for (i = 0 ; i < MAX_NETWORKS ; i++) {
        if (!privconn->networks[i].active) {
            handle = i;
            break;
        }
    }
    if (handle < 0)
        return (-1);

    privconn->networks[handle].active = 1;
    privconn->networks[handle].running = 1;
    strncpy(privconn->networks[handle].name, name, sizeof(privconn->networks[handle].name)-1);
    privconn->networks[handle].name[sizeof(privconn->networks[handle].name)-1] = '\0';
    strncpy(privconn->networks[handle].bridge, bridge ? bridge : name, sizeof(privconn->networks[handle].bridge)-1);
    privconn->networks[handle].bridge[sizeof(privconn->networks[handle].bridge)-1] = '\0';
    free(name);
    name = NULL;
    if (bridge) {
        free(bridge);
        bridge = NULL;
    }

506
    memmove(privconn->networks[handle].uuid, uuid, VIR_UUID_BUFLEN);
507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580
    privconn->networks[handle].forward = forward;
    if (forwardDev) {
        strncpy(privconn->networks[handle].forwardDev, forwardDev, sizeof(privconn->networks[handle].forwardDev)-1);
        privconn->networks[handle].forwardDev[sizeof(privconn->networks[handle].forwardDev)-1] = '\0';
    }

    strncpy(privconn->networks[handle].ipAddress, ipaddress, sizeof(privconn->networks[handle].ipAddress)-1);
    privconn->networks[handle].ipAddress[sizeof(privconn->networks[handle].ipAddress)-1] = '\0';
    free(ipaddress);
    strncpy(privconn->networks[handle].ipNetmask, ipnetmask, sizeof(privconn->networks[handle].ipNetmask)-1);
    privconn->networks[handle].ipNetmask[sizeof(privconn->networks[handle].ipNetmask)-1] = '\0';
    free(ipnetmask);
    strncpy(privconn->networks[handle].dhcpStart, dhcpstart, sizeof(privconn->networks[handle].dhcpStart)-1);
    privconn->networks[handle].dhcpStart[sizeof(privconn->networks[handle].dhcpStart)-1] = '\0';
    free(dhcpstart);
    strncpy(privconn->networks[handle].dhcpEnd, dhcpend, sizeof(privconn->networks[handle].dhcpEnd)-1);
    privconn->networks[handle].dhcpEnd[sizeof(privconn->networks[handle].dhcpEnd)-1] = '\0';
    free(dhcpend);
    return (handle);

 error:
    if (ipaddress)
        free(ipaddress);
    if (ipnetmask)
        free(ipnetmask);
    if (dhcpstart)
        free(dhcpstart);
    if (dhcpend)
        free(dhcpend);
    if (name)
        free(name);
    return (-1);
}

static int testLoadNetworkFromDoc(virConnectPtr conn,
                                 const char *doc) {
    int ret;
    xmlDocPtr xml;
    if (!(xml = xmlReadDoc(BAD_CAST doc, "network.xml", NULL,
                           XML_PARSE_NOENT | XML_PARSE_NONET |
                           XML_PARSE_NOERROR | XML_PARSE_NOWARNING))) {
        testError(conn, NULL, NULL, VIR_ERR_XML_ERROR, _("network"));
        return (-1);
    }

    ret = testLoadNetwork(conn, xml);

    xmlFreeDoc(xml);

    return (ret);
}


static int testLoadNetworkFromFile(virConnectPtr conn,
                                  const char *filename) {
    int ret, fd;
    xmlDocPtr xml;

    if ((fd = open(filename, O_RDONLY)) < 0) {
        testError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR, _("load network definition file"));
        return (-1);
    }

    if (!(xml = xmlReadFd(fd, filename, NULL,
                          XML_PARSE_NOENT | XML_PARSE_NONET |
                          XML_PARSE_NOERROR | XML_PARSE_NOWARNING))) {
        testError(conn, NULL, NULL, VIR_ERR_XML_ERROR, _("network"));
        return (-1);
    }

    ret = testLoadNetwork(conn, xml);

    xmlFreeDoc(xml);
    close(fd);
581

582
    return (ret);
583 584 585
}


586
static int testOpenDefault(virConnectPtr conn) {
587 588
    int u;
    struct timeval tv;
589
    testConnPtr privconn = malloc(sizeof(*privconn));
590 591 592 593 594
    if (!privconn) {
        testError(conn, NULL, NULL, VIR_ERR_NO_MEMORY, "testConn");
        return VIR_DRV_OPEN_ERROR;
    }
    memset(privconn, 0, sizeof(testConn));
595 596

    if (gettimeofday(&tv, NULL) < 0) {
597
        testError(NULL, NULL, NULL, VIR_ERR_INTERNAL_ERROR, _("getting time of day"));
598
        return VIR_DRV_OPEN_ERROR;
599 600
    }

601
    memmove(&privconn->nodeInfo, &defaultNodeInfo, sizeof(defaultNodeInfo));
602

603 604 605 606 607 608 609 610 611 612
    strcpy(privconn->path, "/default");
    privconn->nextDomID = 1;
    privconn->numDomains = 1;
    privconn->domains[0].active = 1;
    privconn->domains[0].config = 1;
    privconn->domains[0].id = privconn->nextDomID++;
    privconn->domains[0].onReboot = VIR_DOMAIN_RESTART;
    privconn->domains[0].onCrash = VIR_DOMAIN_RESTART;
    privconn->domains[0].onPoweroff = VIR_DOMAIN_DESTROY;
    strcpy(privconn->domains[0].name, "test");
613
    for (u = 0 ; u < VIR_UUID_BUFLEN ; u++) {
614
        privconn->domains[0].uuid[u] = (u * 75)%255;
615
    }
616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640
    privconn->domains[0].info.maxMem = 8192 * 1024;
    privconn->domains[0].info.memory = 2048 * 1024;
    privconn->domains[0].info.state = VIR_DOMAIN_RUNNING;
    privconn->domains[0].info.nrVirtCpu = 2;
    privconn->domains[0].info.cpuTime = ((tv.tv_sec * 1000ll * 1000ll  * 1000ll) + (tv.tv_usec * 1000ll));


    privconn->numNetworks = 1;
    privconn->networks[0].active = 1;
    privconn->networks[0].config = 1;
    privconn->networks[0].running = 1;
    strcpy(privconn->networks[0].name, "default");
    strcpy(privconn->networks[0].bridge, "default");
    for (u = 0 ; u < VIR_UUID_BUFLEN ; u++) {
        privconn->networks[0].uuid[u] = (u * 75)%255;
    }
    privconn->networks[0].forward = 1;
    strcpy(privconn->networks[0].forwardDev, "eth0");
    strcpy(privconn->networks[0].ipAddress, "192.168.122.1");
    strcpy(privconn->networks[0].ipNetmask, "255.255.255.0");
    strcpy(privconn->networks[0].dhcpStart, "192.168.122.128");
    strcpy(privconn->networks[0].dhcpEnd, "192.168.122.253");

    conn->privateData = privconn;
    return (VIR_DRV_OPEN_SUCCESS);
641 642 643 644
}


static char *testBuildFilename(const char *relativeTo,
645 646 647 648 649 650 651 652
                               const char *filename) {
    char *offset;
    int baseLen;
    if (!filename || filename[0] == '\0')
        return (NULL);
    if (filename[0] == '/')
        return strdup(filename);

653
    offset = strrchr(relativeTo, '/');
654 655 656 657 658 659 660 661 662
    if ((baseLen = (offset-relativeTo+1))) {
        char *absFile = malloc(baseLen + strlen(filename) + 1);
        strncpy(absFile, relativeTo, baseLen);
        absFile[baseLen] = '\0';
        strcat(absFile, filename);
        return absFile;
    } else {
        return strdup(filename);
    }
663 664 665
}

static int testOpenFromFile(virConnectPtr conn,
666
                            const char *file) {
667
    int fd = -1, i, ret;
668 669
    long l;
    char *str;
670
    xmlDocPtr xml = NULL;
671
    xmlNodePtr root = NULL;
672
    xmlNodePtr *domains, *networks = NULL;
673 674
    xmlXPathContextPtr ctxt = NULL;
    virNodeInfoPtr nodeInfo;
675
    testConnPtr privconn = malloc(sizeof(*privconn));
676 677 678 679
    if (!privconn) {
        testError(NULL, NULL, NULL, VIR_ERR_NO_MEMORY, "testConn");
        return VIR_DRV_OPEN_ERROR;
    }
680 681

    if ((fd = open(file, O_RDONLY)) < 0) {
682 683
        testError(NULL, NULL, NULL, VIR_ERR_INTERNAL_ERROR, _("loading host definition file"));
        goto error;
684 685
    }

686 687 688
    if (!(xml = xmlReadFd(fd, file, NULL,
                          XML_PARSE_NOENT | XML_PARSE_NONET |
                          XML_PARSE_NOERROR | XML_PARSE_NOWARNING))) {
689
        testError(NULL, NULL, NULL, VIR_ERR_INTERNAL_ERROR, _("host"));
690
        goto error;
691
    }
692 693
    close(fd);
    fd = -1;
694

695 696
    root = xmlDocGetRootElement(xml);
    if ((root == NULL) || (!xmlStrEqual(root->name, BAD_CAST "node"))) {
697
        testError(NULL, NULL, NULL, VIR_ERR_XML_ERROR, _("node"));
698
        goto error;
699 700
    }

701 702
    ctxt = xmlXPathNewContext(xml);
    if (ctxt == NULL) {
703
        testError(NULL, NULL, NULL, VIR_ERR_INTERNAL_ERROR, _("creating xpath context"));
704
        goto error;
705
    }
706 707


708 709 710 711 712 713 714 715 716
    conn->privateData = privconn;
    privconn->nextDomID = 1;
    privconn->numDomains = 0;
    privconn->numNetworks = 0;
    strncpy(privconn->path, file, PATH_MAX-1);
    privconn->path[PATH_MAX-1] = '\0';
    memmove(&privconn->nodeInfo, &defaultNodeInfo, sizeof(defaultNodeInfo));

    nodeInfo = &privconn->nodeInfo;
717 718 719 720
    ret = virXPathLong("string(/node/cpu/nodes[1])", ctxt, &l);
    if (ret == 0) {
        nodeInfo->nodes = l;
    } else if (ret == -2) {
721 722
        testError(NULL, NULL, NULL, VIR_ERR_XML_ERROR, _("node cpu numa nodes"));
        goto error;
723
    }
724

725 726 727 728
    ret = virXPathLong("string(/node/cpu/sockets[1])", ctxt, &l);
    if (ret == 0) {
        nodeInfo->sockets = l;
    } else if (ret == -2) {
729 730
        testError(NULL, NULL, NULL, VIR_ERR_XML_ERROR, _("node cpu sockets"));
        goto error;
731
    }
732

733 734 735 736
    ret = virXPathLong("string(/node/cpu/cores[1])", ctxt, &l);
    if (ret == 0) {
        nodeInfo->cores = l;
    } else if (ret == -2) {
737 738
        testError(NULL, NULL, NULL, VIR_ERR_XML_ERROR, _("node cpu cores"));
        goto error;
739 740
    }

741 742 743 744
    ret = virXPathLong("string(/node/cpu/threads[1])", ctxt, &l);
    if (ret == 0) {
        nodeInfo->threads = l;
    } else if (ret == -2) {
745 746
        testError(NULL, NULL, NULL, VIR_ERR_XML_ERROR, _("node cpu threads"));
        goto error;
747
    }
748

749
    nodeInfo->cpus = nodeInfo->cores * nodeInfo->threads * nodeInfo->sockets * nodeInfo->nodes;
750 751 752
    ret = virXPathLong("string(/node/cpu/active[1])", ctxt, &l);
    if (ret == 0) {
        if (l < nodeInfo->cpus) {
753 754
            nodeInfo->cpus = l;
        }
755
    } else if (ret == -2) {
756 757
        testError(NULL, NULL, NULL, VIR_ERR_XML_ERROR, _("node active cpu"));
        goto error;
758 759 760 761 762
    }
    ret = virXPathLong("string(/node/cpu/mhz[1])", ctxt, &l);
    if (ret == 0) {
        nodeInfo->mhz = l;
    } else if (ret == -2) {
763 764
        testError(NULL, NULL, NULL, VIR_ERR_XML_ERROR, _("node cpu mhz"));
        goto error;
765 766 767 768 769
    }

    str = virXPathString("string(/node/cpu/model[1])", ctxt);
    if (str != NULL) {
        strncpy(nodeInfo->model, str, sizeof(nodeInfo->model)-1);
770
        nodeInfo->model[sizeof(nodeInfo->model)-1] = '\0';
771
        free(str);
772 773
    }

774 775 776 777
    ret = virXPathLong("string(/node/memory[1])", ctxt, &l);
    if (ret == 0) {
        nodeInfo->memory = l;
    } else if (ret == -2) {
778 779
        testError(NULL, NULL, NULL, VIR_ERR_XML_ERROR, _("node memory"));
        goto error;
780
    }
781

782 783
    ret = virXPathNodeSet("/node/domain", ctxt, &domains);
    if (ret < 0) {
784
        testError(NULL, NULL, NULL, VIR_ERR_XML_ERROR, _("node domain list"));
785
        goto error;
786
    }
787

788 789
    for (i = 0 ; i < ret ; i++) {
        xmlChar *domFile = xmlGetProp(domains[i], BAD_CAST "file");
790
        char *absFile = testBuildFilename(file, (const char *)domFile);
791
        int domid = privconn->nextDomID++, handle;
792 793
        free(domFile);
        if (!absFile) {
794
            testError(NULL, NULL, NULL, VIR_ERR_INTERNAL_ERROR, _("resolving domain filename"));
795 796
            goto error;
        }
797
        if ((handle = testLoadDomainFromFile(conn, domid, absFile)) < 0) {
798 799 800
            free(absFile);
            goto error;
        }
801
        privconn->domains[handle].config = 1;
802
        free(absFile);
803
        privconn->numDomains++;
804
    }
805
    if (domains != NULL) {
806
        free(domains);
807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834
        domains = NULL;
    }


    ret = virXPathNodeSet("/node/network", ctxt, &networks);
    if (ret > 0) {
        for (i = 0 ; i < ret ; i++) {
            xmlChar *netFile = xmlGetProp(networks[i], BAD_CAST "file");
            char *absFile = testBuildFilename(file, (const char *)netFile);
            int handle;
            free(netFile);
            if (!absFile) {
                testError(NULL, NULL, NULL, VIR_ERR_INTERNAL_ERROR, _("resolving network filename"));
                goto error;
            }
            if ((handle = testLoadNetworkFromFile(conn, absFile)) < 0) {
                free(absFile);
                goto error;
            }
            privconn->networks[handle].config = 1;
            free(absFile);
            privconn->numNetworks++;
        }
        if (networks != NULL) {
            free(networks);
            networks = NULL;
        }
    }
835

836
    xmlFreeDoc(xml);
837

838
    return (0);
839 840

 error:
841 842 843 844
    if (domains != NULL)
        free(domains);
    if (networks != NULL)
        free(networks);
845 846 847 848
    if (xml)
        xmlFreeDoc(xml);
    if (fd != -1)
        close(fd);
849 850
    free(privconn);
    conn->privateData = NULL;
851
    return VIR_DRV_OPEN_ERROR;
852 853
}

854 855
static int getDomainIndex(virDomainPtr domain) {
    int i;
856
    GET_CONNECTION(domain->conn, -1);
857

858
    for (i = 0 ; i < MAX_DOMAINS ; i++) {
859
        if (domain->id >= 0) {
860
            if (domain->id == privconn->domains[i].id)
861 862
                return (i);
        } else {
863
            if (STREQ(domain->name, privconn->domains[i].name))
864 865
                return (i);
        }
866
    }
867
    return (-1);
868
}
869

870 871 872 873 874 875 876 877 878 879 880 881
static int getNetworkIndex(virNetworkPtr network) {
    int i;
    GET_CONNECTION(network->conn, -1);

    for (i = 0 ; i < MAX_NETWORKS ; i++) {
        if (STREQ(network->name, privconn->networks[i].name))
            return (i);
    }
    return (-1);
}

static int testOpen(virConnectPtr conn,
882
                    xmlURIPtr uri,
883
                    virConnectAuthPtr auth ATTRIBUTE_UNUSED,
884
                    int flags ATTRIBUTE_UNUSED)
885
{
886
    int ret;
887

888
    if (!uri)
889
        return VIR_DRV_OPEN_DECLINED;
890

891
    if (!uri->scheme || strcmp(uri->scheme, "test") != 0)
892
        return VIR_DRV_OPEN_DECLINED;
893

894
    /* Remote driver should handle these. */
895
    if (uri->server)
896 897
        return VIR_DRV_OPEN_DECLINED;

898
    if (uri->server)
899 900
        return VIR_DRV_OPEN_DECLINED;

901 902 903 904
    /* From this point on, the connection is for us. */
    if (!uri->path
        || uri->path[0] == '\0'
        || (uri->path[0] == '/' && uri->path[1] == '\0')) {
905
        testError (NULL, NULL, NULL, VIR_ERR_INVALID_ARG,
906 907 908
                   _("testOpen: supply a path or use test:///default"));
        return VIR_DRV_OPEN_ERROR;
    }
909

910 911 912
    if (STREQ(uri->path, "/default"))
        ret = testOpenDefault(conn);
    else
913 914 915 916
        ret = testOpenFromFile(conn,
                               uri->path);

    return (ret);
917 918
}

919
static int testClose(virConnectPtr conn)
920
{
921 922 923
    GET_CONNECTION(conn, -1);
    free (privconn);
    conn->privateData = conn;
924
    return 0;
925 926
}

927 928
static int testGetVersion(virConnectPtr conn ATTRIBUTE_UNUSED,
                          unsigned long *hvVer)
929
{
930 931
    *hvVer = 2;
    return (0);
932 933
}

934
static char *testGetHostname (virConnectPtr conn)
935 936 937 938 939 940
{
    int r;
    char hostname [HOST_NAME_MAX+1], *str;

    r = gethostname (hostname, HOST_NAME_MAX+1);
    if (r == -1) {
941
        testError (conn, NULL, NULL, VIR_ERR_SYSTEM_ERROR, strerror (errno));
942 943 944 945
        return NULL;
    }
    str = strdup (hostname);
    if (str == NULL) {
946
        testError (conn, NULL, NULL, VIR_ERR_SYSTEM_ERROR, strerror (errno));
947 948 949 950 951
        return NULL;
    }
    return str;
}

952
static char * testGetURI (virConnectPtr conn)
953 954
{
    char *uri;
955
    GET_CONNECTION(conn, NULL);
956

957 958
    if (asprintf (&uri, "test://%s", privconn->path) == -1) {
        testError (conn, NULL, NULL, VIR_ERR_SYSTEM_ERROR, strerror (errno));
959 960 961 962 963
        return NULL;
    }
    return uri;
}

964 965 966 967 968 969 970 971
static int testGetMaxVCPUs(virConnectPtr conn ATTRIBUTE_UNUSED,
                           const char *type ATTRIBUTE_UNUSED)
{
    return 32;
}

static int testNodeGetInfo(virConnectPtr conn,
                           virNodeInfoPtr info)
972
{
973 974
    GET_CONNECTION(conn, -1);
    memcpy(info, &privconn->nodeInfo, sizeof(virNodeInfo));
975
    return (0);
976 977
}

978
static char *testGetCapabilities (virConnectPtr conn)
979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007
{
    static char caps[] = "\
<capabilities>\n\
  <host>\n\
    <cpu>\n\
      <arch>" TEST_MODEL "</arch>\n\
      <features>\n\
        <pae/>\n\
        <nonpae/>\n\
      </features>\n\
    </cpu>\n\
  </host>\n\
\n\
  <guest>\n\
    <os_type>linux</os_type>\n\
    <arch name=\"" TEST_MODEL "\">\n\
      <wordsize>" TEST_MODEL_WORDSIZE "</wordsize>\n\
      <domain type=\"test\"/>\n\
    </arch>\n\
    <features>\n\
      <pae/>\n\
      <nonpae/>\n\
    </features>\n\
  </guest>\n\
</capabilities>\n\
";

    char *caps_copy = strdup (caps);
    if (!caps_copy) {
1008
        testError(conn, NULL, NULL, VIR_ERR_NO_MEMORY, __FUNCTION__);
1009 1010 1011 1012 1013
        return NULL;
    }
    return caps_copy;
}

1014
static int testNumOfDomains(virConnectPtr conn)
1015
{
1016
    int numActive = 0, i;
1017 1018
    GET_CONNECTION(conn, -1);

1019
    for (i = 0 ; i < MAX_DOMAINS ; i++) {
1020 1021
        if (!privconn->domains[i].active ||
            privconn->domains[i].info.state == VIR_DOMAIN_SHUTOFF)
1022 1023 1024 1025
            continue;
        numActive++;
    }
    return (numActive);
1026 1027
}

1028
static virDomainPtr
1029
testDomainCreateLinux(virConnectPtr conn, const char *xmlDesc,
1030
                      unsigned int flags ATTRIBUTE_UNUSED)
1031
{
1032
    int domid, handle = -1;
1033
    virDomainPtr dom;
1034
    GET_CONNECTION(conn, NULL);
1035

1036
    if (xmlDesc == NULL) {
1037
        testError(conn, NULL, NULL, VIR_ERR_INVALID_ARG, __FUNCTION__);
1038 1039
        return (NULL);
    }
1040 1041 1042

    if (privconn->numDomains == MAX_DOMAINS) {
        testError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR, _("too many domains"));
1043 1044
        return (NULL);
    }
1045

1046 1047
    domid = privconn->nextDomID++;
    if ((handle = testLoadDomainFromDoc(conn, domid, xmlDesc)) < 0)
1048
        return (NULL);
1049
    privconn->domains[handle].config = 0;
1050

1051
    dom = virGetDomain(conn, privconn->domains[handle].name, privconn->domains[handle].uuid);
1052
    if (dom == NULL) return NULL;
1053
    privconn->numDomains++;
1054
    return (dom);
1055 1056 1057
}


1058 1059
static virDomainPtr testLookupDomainByID(virConnectPtr conn,
                                         int id)
1060
{
1061 1062
    virDomainPtr dom;
    int i, idx = -1;
1063
    GET_CONNECTION(conn, NULL);
1064 1065

    for (i = 0 ; i < MAX_DOMAINS ; i++) {
1066 1067
        if (privconn->domains[i].active &&
            privconn->domains[i].id == id) {
1068 1069 1070 1071 1072 1073
            idx = i;
            break;
        }
    }

    if (idx < 0) {
1074
        testError (conn, NULL, NULL, VIR_ERR_NO_DOMAIN, NULL);
1075 1076 1077
        return(NULL);
    }

1078
    dom = virGetDomain(conn, privconn->domains[idx].name, privconn->domains[idx].uuid);
1079
    if (dom == NULL) return NULL;
1080
    dom->id = id;
1081
    return (dom);
1082 1083
}

1084 1085
static virDomainPtr testLookupDomainByUUID(virConnectPtr conn,
                                           const unsigned char *uuid)
1086
{
1087
    virDomainPtr dom;
1088
    int i, idx = -1;
1089
    GET_CONNECTION(conn, NULL);
1090

1091
    for (i = 0 ; i < MAX_DOMAINS ; i++) {
1092 1093
        if (privconn->domains[i].active &&
            memcmp(uuid, privconn->domains[i].uuid, VIR_UUID_BUFLEN) == 0) {
1094 1095 1096
            idx = i;
            break;
        }
1097
    }
1098 1099

    if (idx < 0) {
1100
        testError (conn, NULL, NULL, VIR_ERR_NO_DOMAIN, NULL);
1101
        return NULL;
1102
    }
1103

1104
    dom = virGetDomain(conn, privconn->domains[idx].name, privconn->domains[idx].uuid);
1105
    if (dom == NULL) return NULL;
1106
    dom->id = privconn->domains[idx].id;
1107 1108

    return dom;
1109 1110
}

1111 1112
static virDomainPtr testLookupDomainByName(virConnectPtr conn,
                                           const char *name)
1113
{
1114
    virDomainPtr dom;
1115
    int i, idx = -1;
1116
    GET_CONNECTION(conn, NULL);
1117

1118
    for (i = 0 ; i < MAX_DOMAINS ; i++) {
1119 1120
        if (privconn->domains[i].active &&
            strcmp(name, privconn->domains[i].name) == 0) {
1121 1122 1123
            idx = i;
            break;
        }
1124
    }
1125 1126

    if (idx < 0) {
1127
        testError (conn, NULL, NULL, VIR_ERR_NO_DOMAIN, NULL);
1128
        return NULL;
1129
    }
1130

1131
    dom = virGetDomain(conn, privconn->domains[idx].name, privconn->domains[idx].uuid);
1132
    if (dom == NULL) return NULL;
1133
    dom->id = privconn->domains[idx].id;
1134 1135

    return dom;
1136 1137
}

1138 1139 1140
static int testListDomains (virConnectPtr conn,
                            int *ids,
                            int maxids)
1141
{
1142
    int n, i;
1143
    GET_CONNECTION(conn, -1);
1144 1145

    for (i = 0, n = 0 ; i < MAX_DOMAINS && n < maxids ; i++) {
1146 1147 1148
        if (privconn->domains[i].active &&
            privconn->domains[i].info.state != VIR_DOMAIN_SHUTOFF) {
            ids[n++] = privconn->domains[i].id;
1149
        }
1150
    }
1151
    return (n);
1152 1153
}

1154
static int testDestroyDomain (virDomainPtr domain)
1155
{
1156
    GET_DOMAIN(domain, -1);
1157

1158 1159 1160 1161 1162 1163 1164
    if (privdom->config) {
        privdom->info.state = VIR_DOMAIN_SHUTOFF;
        privdom->id = -1;
        domain->id = -1;
    } else {
        privdom->active = 0;
    }
1165
    return (0);
1166 1167
}

1168
static int testResumeDomain (virDomainPtr domain)
1169
{
1170
    GET_DOMAIN(domain, -1);
1171

1172 1173 1174 1175
    if (privdom->info.state != VIR_DOMAIN_PAUSED) {
        testError(domain->conn, domain, NULL, VIR_ERR_INTERNAL_ERROR, "domain not paused");
        return -1;
    }
1176

1177
    privdom->info.state = VIR_DOMAIN_RUNNING;
1178
    return (0);
1179 1180
}

1181
static int testPauseDomain (virDomainPtr domain)
1182
{
1183
    GET_DOMAIN(domain, -1);
1184

1185 1186 1187 1188 1189
    if (privdom->info.state == VIR_DOMAIN_SHUTOFF ||
        privdom->info.state == VIR_DOMAIN_PAUSED) {
        testError(domain->conn, domain, NULL, VIR_ERR_INTERNAL_ERROR, "domain not running");
        return -1;
    }
1190

1191
    privdom->info.state = VIR_DOMAIN_PAUSED;
1192
    return (0);
1193 1194
}

1195
static int testShutdownDomain (virDomainPtr domain)
1196
{
1197
    GET_DOMAIN(domain, -1);
1198

1199 1200 1201
    if (privdom->info.state == VIR_DOMAIN_SHUTOFF) {
        testError(domain->conn, domain, NULL, VIR_ERR_INTERNAL_ERROR, "domain not running");
        return -1;
1202
    }
1203

1204
    privdom->info.state = VIR_DOMAIN_SHUTOFF;
1205
    domain->id = -1;
1206
    privdom->id = -1;
1207 1208

    return (0);
1209 1210 1211
}

/* Similar behaviour as shutdown */
1212
static int testRebootDomain (virDomainPtr domain, virDomainRestart action)
1213
{
1214
    GET_DOMAIN(domain, -1);
1215 1216 1217 1218

    if (!action)
        action = VIR_DOMAIN_RESTART;

1219
    privdom->info.state = VIR_DOMAIN_SHUTDOWN;
1220 1221
    switch (action) {
    case VIR_DOMAIN_DESTROY:
1222 1223 1224
        privdom->info.state = VIR_DOMAIN_SHUTOFF;
        domain->id = -1;
        privdom->id = -1;
1225 1226 1227
        break;

    case VIR_DOMAIN_RESTART:
1228
        privdom->info.state = VIR_DOMAIN_RUNNING;
1229 1230 1231
        break;

    case VIR_DOMAIN_PRESERVE:
1232 1233 1234
        privdom->info.state = VIR_DOMAIN_SHUTOFF;
        domain->id = -1;
        privdom->id = -1;
1235 1236 1237
        break;

    case VIR_DOMAIN_RENAME_RESTART:
1238
        privdom->info.state = VIR_DOMAIN_RUNNING;
1239
        break;
1240

1241
    default:
1242 1243 1244
        privdom->info.state = VIR_DOMAIN_SHUTOFF;
        domain->id = -1;
        privdom->id = -1;
1245 1246
        break;
    }
1247

1248
    return (0);
1249 1250
}

1251 1252
static int testGetDomainInfo (virDomainPtr domain,
                              virDomainInfoPtr info)
1253
{
1254
    struct timeval tv;
1255
    GET_DOMAIN(domain, -1);
1256 1257

    if (gettimeofday(&tv, NULL) < 0) {
1258
        testError(domain->conn, domain, NULL, VIR_ERR_INTERNAL_ERROR, _("getting time of day"));
1259 1260 1261
        return (-1);
    }

1262 1263 1264
    if (privdom->info.state == VIR_DOMAIN_SHUTOFF) {
        privdom->info.cpuTime = 0;
        privdom->info.memory = 0;
1265
    } else {
1266
        privdom->info.cpuTime = ((tv.tv_sec * 1000ll * 1000ll  * 1000ll) + (tv.tv_usec * 1000ll));
1267
    }
1268
    memcpy(info, &privdom->info, sizeof(virDomainInfo));
1269
    return (0);
1270 1271
}

1272
static char *testDomainDumpXML(virDomainPtr domain, int flags);
1273

1274 1275 1276 1277 1278 1279 1280 1281
#define TEST_SAVE_MAGIC "TestGuestMagic"

static int testDomainSave(virDomainPtr domain,
                          const char *path)
{
    char *xml;
    int fd, len;
    GET_DOMAIN(domain, -1);
1282

1283 1284 1285 1286 1287
    xml = testDomainDumpXML(domain, 0);

    if ((fd = open(path, O_CREAT|O_TRUNC|O_WRONLY, S_IRUSR|S_IWUSR)) < 0) {
        testError(domain->conn, domain, NULL, VIR_ERR_INTERNAL_ERROR,
                  "cannot save domain");
1288 1289
        return (-1);
    }
1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322
    len = strlen(xml);
    if (write(fd, TEST_SAVE_MAGIC, sizeof(TEST_SAVE_MAGIC)) != sizeof(TEST_SAVE_MAGIC)) {
        testError(domain->conn, domain, NULL, VIR_ERR_INTERNAL_ERROR,
                  "cannot write header");
        close(fd);
        return (-1);
    }
    if (write(fd, (char*)&len, sizeof(len)) != sizeof(len)) {
        testError(domain->conn, domain, NULL, VIR_ERR_INTERNAL_ERROR,
                  "cannot write metadata length");
        close(fd);
        return (-1);
    }
    if (write(fd, xml, len) != len) {
        testError(domain->conn, domain, NULL, VIR_ERR_INTERNAL_ERROR,
                  "cannot write metadata");
        close(fd);
        return (-1);
    }
    if (close(fd) < 0) {
        testError(domain->conn, domain, NULL, VIR_ERR_INTERNAL_ERROR,
                  "cannot save domain data");
        close(fd);
        return (-1);
    }
    if (privdom->config) {
        privdom->info.state = VIR_DOMAIN_SHUTOFF;
        privdom->id = -1;
        domain->id = -1;
    } else {
        privdom->active = 0;
    }
    return 0;
1323 1324
}

1325 1326
static int testDomainRestore(virConnectPtr conn,
                             const char *path)
1327
{
1328 1329 1330 1331 1332 1333 1334 1335
    char *xml;
    char magic[15];
    int fd, len, ret, domid;
    GET_CONNECTION(conn, -1);

    if ((fd = open(path, O_RDONLY)) < 0) {
        testError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
                  "cannot read domain image");
1336 1337
        return (-1);
    }
1338 1339 1340 1341
    if (read(fd, magic, sizeof(magic)) != sizeof(magic)) {
        testError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
                  "incomplete save header");
        close(fd);
1342 1343
        return (-1);
    }
1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379
    if (memcmp(magic, TEST_SAVE_MAGIC, sizeof(magic))) {
        testError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
                  "mismatched header magic");
        close(fd);
        return (-1);
    }
    if (read(fd, (char*)&len, sizeof(len)) != sizeof(len)) {
        testError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
                  "failed to read metadata length");
        close(fd);
        return (-1);
    }
    if (len < 1 || len > 8192) {
        testError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
                  "length of metadata out of range");
        close(fd);
        return (-1);
    }
    xml = malloc(len+1);
    if (!xml) {
        testError(conn, NULL, NULL, VIR_ERR_NO_MEMORY, "xml");
        close(fd);
        return (-1);
    }
    if (read(fd, xml, len) != len) {
        testError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
                  "incomplete metdata");
        close(fd);
        return (-1);
    }
    xml[len] = '\0';
    close(fd);
    domid = privconn->nextDomID++;
    ret = testLoadDomainFromDoc(conn, domid, xml);
    free(xml);
    return ret < 0 ? -1 : 0;
1380 1381
}

1382 1383 1384
static int testDomainCoreDump(virDomainPtr domain,
                              const char *to,
                              int flags ATTRIBUTE_UNUSED)
1385
{
1386 1387 1388 1389 1390 1391
    int fd;
    GET_DOMAIN(domain, -1);

    if ((fd = open(to, O_CREAT|O_TRUNC|O_WRONLY, S_IRUSR|S_IWUSR)) < 0) {
        testError(domain->conn, domain, NULL, VIR_ERR_INTERNAL_ERROR,
                  "cannot save domain core");
1392 1393
        return (-1);
    }
1394 1395 1396 1397
    if (write(fd, TEST_SAVE_MAGIC, sizeof(TEST_SAVE_MAGIC)) != sizeof(TEST_SAVE_MAGIC)) {
        testError(domain->conn, domain, NULL, VIR_ERR_INTERNAL_ERROR,
                  "cannot write header");
        close(fd);
1398 1399
        return (-1);
    }
1400 1401 1402 1403
    if (close(fd) < 0) {
        testError(domain->conn, domain, NULL, VIR_ERR_INTERNAL_ERROR,
                  "cannot save domain data");
        close(fd);
1404 1405
        return (-1);
    }
1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421
    if (privdom->config) {
        privdom->info.state = VIR_DOMAIN_SHUTOFF;
        privdom->id = -1;
        domain->id = -1;
    } else {
        privdom->active = 0;
    }
    return 0;
}

static char *testGetOSType(virDomainPtr dom ATTRIBUTE_UNUSED) {
    return strdup("linux");
}

static unsigned long testGetMaxMemory(virDomainPtr domain) {
    GET_DOMAIN(domain, -1);
1422

1423 1424 1425 1426 1427 1428 1429 1430 1431 1432
    return privdom->info.maxMem;
}

static int testSetMaxMemory(virDomainPtr domain,
                            unsigned long memory)
{
    GET_DOMAIN(domain, -1);

    /* XXX validate not over host memory wrt to other domains */
    privdom->info.maxMem = memory;
1433
    return (0);
1434 1435
}

1436 1437 1438 1439
static int testSetMemory(virDomainPtr domain,
                         unsigned long memory)
{
    GET_DOMAIN(domain, -1);
1440

1441 1442
    if (memory > privdom->info.maxMem) {
        testError(domain->conn, domain, NULL, VIR_ERR_INVALID_ARG, __FUNCTION__);
1443 1444
        return (-1);
    }
1445

1446 1447 1448 1449 1450 1451 1452
    privdom->info.memory = memory;
    return (0);
}

static int testSetVcpus(virDomainPtr domain,
                        unsigned int nrCpus) {
    GET_DOMAIN(domain, -1);
1453

1454 1455
    /* We allow more cpus in guest than host */
    if (nrCpus > 32) {
1456
        testError(domain->conn, domain, NULL, VIR_ERR_INVALID_ARG, __FUNCTION__);
1457 1458
        return (-1);
    }
1459

1460
    privdom->info.nrVirtCpu = nrCpus;
1461
    return (0);
1462 1463
}

1464
static char *testDomainDumpXML(virDomainPtr domain, int flags ATTRIBUTE_UNUSED)
1465
{
1466 1467 1468
    virBufferPtr buf;
    char *xml;
    unsigned char *uuid;
1469
    char uuidstr[VIR_UUID_STRING_BUFLEN];
1470
    GET_DOMAIN(domain, NULL);
1471 1472

    if (!(buf = virBufferNew(4000))) {
1473
        testError(domain->conn, domain, NULL, VIR_ERR_NO_MEMORY, __FUNCTION__);
1474 1475 1476
        return (NULL);
    }

1477
    virBufferVSprintf(buf, "<domain type='test' id='%d'>\n", domain->id);
1478 1479
    virBufferVSprintf(buf, "  <name>%s</name>\n", domain->name);
    uuid = domain->uuid;
1480 1481
    virUUIDFormat(uuid, uuidstr);
    virBufferVSprintf(buf, "  <uuid>%s</uuid>\n", uuidstr);
1482 1483 1484 1485 1486
    virBufferVSprintf(buf, "  <memory>%lu</memory>\n", privdom->info.maxMem);
    virBufferVSprintf(buf, "  <vcpu>%d</vcpu>\n", privdom->info.nrVirtCpu);
    virBufferVSprintf(buf, "  <on_reboot>%s</on_reboot>\n", testRestartFlagToString(privdom->onReboot));
    virBufferVSprintf(buf, "  <on_poweroff>%s</on_poweroff>\n", testRestartFlagToString(privdom->onPoweroff));
    virBufferVSprintf(buf, "  <on_crash>%s</on_crash>\n", testRestartFlagToString(privdom->onCrash));
1487 1488 1489 1490 1491 1492 1493

    virBufferAdd(buf, "</domain>\n", -1);

    xml = buf->content;
    free(buf);

    return (xml);
1494
}
1495

1496
static int testNumOfDefinedDomains(virConnectPtr conn) {
1497
    int numInactive = 0, i;
1498 1499
    GET_CONNECTION(conn, -1);

1500
    for (i = 0 ; i < MAX_DOMAINS ; i++) {
1501 1502
        if (!privconn->domains[i].active ||
            privconn->domains[i].info.state != VIR_DOMAIN_SHUTOFF)
1503 1504 1505 1506 1507 1508
            continue;
        numInactive++;
    }
    return (numInactive);
}

1509 1510 1511
static int testListDefinedDomains(virConnectPtr conn,
                                  char **const names,
                                  int maxnames) {
1512
    int n = 0, i;
1513
    GET_CONNECTION(conn, -1);
1514 1515

    for (i = 0, n = 0 ; i < MAX_DOMAINS && n < maxnames ; i++) {
1516 1517 1518
        if (privconn->domains[i].active &&
            privconn->domains[i].info.state == VIR_DOMAIN_SHUTOFF) {
            names[n++] = strdup(privconn->domains[i].name);
1519 1520 1521 1522 1523
        }
    }
    return (n);
}

1524 1525 1526
static virDomainPtr testDomainDefineXML(virConnectPtr conn,
                                        const char *doc) {
    int handle;
1527
    xmlDocPtr xml;
1528
    GET_CONNECTION(conn, NULL);
1529 1530 1531 1532

    if (!(xml = xmlReadDoc(BAD_CAST doc, "domain.xml", NULL,
                           XML_PARSE_NOENT | XML_PARSE_NONET |
                           XML_PARSE_NOERROR | XML_PARSE_NOWARNING))) {
1533
        testError(conn, NULL, NULL, VIR_ERR_XML_ERROR, _("domain"));
1534 1535 1536
        return (NULL);
    }

1537 1538
    handle = testLoadDomain(conn, -1, xml);
    privconn->domains[handle].config = 1;
1539 1540 1541

    xmlFreeDoc(xml);

1542
    if (handle < 0)
1543 1544
        return (NULL);

1545
    return virGetDomain(conn, privconn->domains[handle].name, privconn->domains[handle].uuid);
1546 1547
}

1548 1549
static int testDomainCreate(virDomainPtr domain) {
    GET_DOMAIN(domain, -1);
1550

1551 1552 1553
    if (privdom->info.state != VIR_DOMAIN_SHUTOFF) {
        testError(domain->conn, domain, NULL, VIR_ERR_INTERNAL_ERROR,
                  _("Domain is already running"));
1554 1555 1556
        return (-1);
    }

1557 1558
    domain->id = privdom->id = privconn->nextDomID++;
    privdom->info.state = VIR_DOMAIN_RUNNING;
1559

1560 1561 1562 1563 1564 1565 1566 1567 1568
    return (0);
}

static int testDomainUndefine(virDomainPtr domain) {
    GET_DOMAIN(domain, -1);

    if (privdom->info.state != VIR_DOMAIN_SHUTOFF) {
        testError(domain->conn, domain, NULL, VIR_ERR_INTERNAL_ERROR,
                  _("Domain is still running"));
1569 1570 1571
        return (-1);
    }

1572
    privdom->active = 0;
1573 1574 1575 1576

    return (0);
}

1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592
static int testDomainGetAutostart(virDomainPtr domain,
                                  int *autostart)
{
    GET_DOMAIN(domain, -1);
    *autostart = privdom->autostart;
    return (0);
}


static int testDomainSetAutostart(virDomainPtr domain,
                                  int autostart)
{
    GET_DOMAIN(domain, -1);
    privdom->autostart = autostart ? 1 : 0;
    return (0);
}
1593

1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613
static char *testDomainGetSchedulerType(virDomainPtr domain,
                                        int *nparams)
{
    char *type;
    *nparams = 1;
    type = strdup("fair");
    if (!type) {
        testError(domain->conn, domain, NULL, VIR_ERR_NO_MEMORY, "schedular");
        return (NULL);
    }
    return type;
}

static int testDomainGetSchedulerParams(virDomainPtr domain,
                                        virSchedParameterPtr params,
                                        int *nparams)
{
    GET_DOMAIN(domain, -1);
    if (*nparams != 1) {
        testError(domain->conn, domain, NULL, VIR_ERR_INVALID_ARG, "nparams");
1614 1615
        return (-1);
    }
1616 1617 1618 1619 1620
    strcpy(params[0].field, "weight");
    params[0].type = VIR_DOMAIN_SCHED_FIELD_UINT;
    params[0].value.ui = privdom->weight;
    return 0;
}
1621 1622


1623 1624 1625 1626 1627 1628 1629
static int testDomainSetSchedulerParams(virDomainPtr domain,
                                        virSchedParameterPtr params,
                                        int nparams)
{
    GET_DOMAIN(domain, -1);
    if (nparams != 1) {
        testError(domain->conn, domain, NULL, VIR_ERR_INVALID_ARG, "nparams");
1630 1631
        return (-1);
    }
1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644
    if (STRNEQ(params[0].field, "weight")) {
        testError(domain->conn, domain, NULL, VIR_ERR_INVALID_ARG, "field");
        return (-1);
    }
    if (params[0].type != VIR_DOMAIN_SCHED_FIELD_UINT) {
        testError(domain->conn, domain, NULL, VIR_ERR_INVALID_ARG, "type");
        return (-1);
    }
    privdom->weight = params[0].value.ui;
    return 0;
}

static virDrvOpenStatus testOpenNetwork(virConnectPtr conn,
1645
                                        xmlURIPtr uri ATTRIBUTE_UNUSED,
1646
                                        virConnectAuthPtr auth ATTRIBUTE_UNUSED,
1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673
                                        int flags ATTRIBUTE_UNUSED) {
    if (STRNEQ(conn->driver->name, "Test"))
        return VIR_DRV_OPEN_DECLINED;

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

static int testCloseNetwork(virConnectPtr conn) {
    conn->networkPrivateData = NULL;
    return 0;
}


static virNetworkPtr testLookupNetworkByUUID(virConnectPtr conn,
                                           const unsigned char *uuid)
{
    int i, idx = -1;
    GET_CONNECTION(conn, NULL);

    for (i = 0 ; i < MAX_NETWORKS ; i++) {
        if (privconn->networks[i].active &&
            memcmp(uuid, privconn->networks[i].uuid, VIR_UUID_BUFLEN) == 0) {
            idx = i;
            break;
        }
    }
1674

1675 1676 1677 1678 1679 1680 1681
    if (idx < 0) {
        testError (conn, NULL, NULL, VIR_ERR_NO_NETWORK, NULL);
        return NULL;
    }

    return virGetNetwork(conn, privconn->networks[idx].name, privconn->networks[idx].uuid);
}
1682

1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850
static virNetworkPtr testLookupNetworkByName(virConnectPtr conn,
                                           const char *name)
{
    int i, idx = -1;
    GET_CONNECTION(conn, NULL);

    for (i = 0 ; i < MAX_NETWORKS ; i++) {
        if (privconn->networks[i].active &&
            strcmp(name, privconn->networks[i].name) == 0) {
            idx = i;
            break;
        }
    }

    if (idx < 0) {
        testError (conn, NULL, NULL, VIR_ERR_NO_NETWORK, NULL);
        return NULL;
    }

    return virGetNetwork(conn, privconn->networks[idx].name, privconn->networks[idx].uuid);
}


static int testNumNetworks(virConnectPtr conn) {
    int numInactive = 0, i;
    GET_CONNECTION(conn, -1);

    for (i = 0 ; i < MAX_NETWORKS ; i++) {
        if (!privconn->networks[i].active ||
            !privconn->networks[i].running)
            continue;
        numInactive++;
    }
    return (numInactive);
}

static int testListNetworks(virConnectPtr conn, char **const names, int nnames) {
    int n = 0, i;
    GET_CONNECTION(conn, -1);

    for (i = 0, n = 0 ; i < MAX_NETWORKS && n < nnames ; i++) {
        if (privconn->networks[i].active &&
            privconn->networks[i].running) {
            names[n++] = strdup(privconn->networks[i].name);
        }
    }
    return (n);
}

static int testNumDefinedNetworks(virConnectPtr conn) {
    int numInactive = 0, i;
    GET_CONNECTION(conn, -1);

    for (i = 0 ; i < MAX_NETWORKS ; i++) {
        if (!privconn->networks[i].active ||
            privconn->networks[i].running)
            continue;
        numInactive++;
    }
    return (numInactive);
}

static int testListDefinedNetworks(virConnectPtr conn, char **const names, int nnames) {
    int n = 0, i;
    GET_CONNECTION(conn, -1);

    for (i = 0, n = 0 ; i < MAX_NETWORKS && n < nnames ; i++) {
        if (privconn->networks[i].active &&
            !privconn->networks[i].running) {
            names[n++] = strdup(privconn->networks[i].name);
        }
    }
    return (n);
}

static virNetworkPtr testNetworkCreate(virConnectPtr conn, const char *xml) {
    int handle = -1;
    virNetworkPtr net;
    GET_CONNECTION(conn, NULL);

    if (xml == NULL) {
        testError(conn, NULL, NULL, VIR_ERR_INVALID_ARG, __FUNCTION__);
        return (NULL);
    }

    if (privconn->numNetworks == MAX_NETWORKS) {
        testError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR, _("too many networks"));
        return (NULL);
    }

    if ((handle = testLoadNetworkFromDoc(conn, xml)) < 0)
        return (NULL);
    privconn->networks[handle].config = 0;

    net = virGetNetwork(conn, privconn->networks[handle].name, privconn->networks[handle].uuid);
    if (net == NULL) return NULL;
    privconn->numNetworks++;
    return (net);
}

static virNetworkPtr testNetworkDefine(virConnectPtr conn, const char *xml) {
    int handle = -1;
    virNetworkPtr net;
    GET_CONNECTION(conn, NULL);

    if (xml == NULL) {
        testError(conn, NULL, NULL, VIR_ERR_INVALID_ARG, __FUNCTION__);
        return (NULL);
    }

    if (privconn->numNetworks == MAX_NETWORKS) {
        testError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR, _("too many networks"));
        return (NULL);
    }

    if ((handle = testLoadNetworkFromDoc(conn, xml)) < 0)
        return (NULL);

    net = virGetNetwork(conn, privconn->networks[handle].name, privconn->networks[handle].uuid);
    privconn->networks[handle].config = 1;
    if (net == NULL) return NULL;
    privconn->numNetworks++;
    return (net);
}

static int testNetworkUndefine(virNetworkPtr network) {
    GET_NETWORK(network, -1);

    if (privnet->running) {
        testError(network->conn, NULL, network, VIR_ERR_INTERNAL_ERROR,
                  _("Network is still running"));
        return (-1);
    }

    privnet->active = 0;

    return (0);
}

static int testNetworkStart(virNetworkPtr network) {
    GET_NETWORK(network, -1);

    if (privnet->running) {
        testError(network->conn, NULL, network, VIR_ERR_INTERNAL_ERROR,
                  _("Network is already running"));
        return (-1);
    }

    privnet->running = 1;

    return (0);
}

static int testNetworkDestroy(virNetworkPtr network) {
    GET_NETWORK(network, -1);

    if (privnet->config) {
        privnet->running = 0;
    } else {
        privnet->active = 0;
    }
    return (0);
}

static char *testNetworkDumpXML(virNetworkPtr network, int flags ATTRIBUTE_UNUSED) {
    virBufferPtr buf;
    char *xml;
    unsigned char *uuid;
1851
    char uuidstr[VIR_UUID_STRING_BUFLEN];
1852 1853 1854 1855 1856 1857 1858 1859 1860 1861
    GET_NETWORK(network, NULL);

    if (!(buf = virBufferNew(4000))) {
        testError(network->conn, NULL, network, VIR_ERR_NO_MEMORY, __FUNCTION__);
        return (NULL);
    }

    virBufferAdd(buf, "<network>\n", -1);
    virBufferVSprintf(buf, "  <name>%s</name>\n", network->name);
    uuid = network->uuid;
1862 1863
    virUUIDFormat(uuid, uuidstr);
    virBufferVSprintf(buf, "  <uuid>%s</uuid>\n", uuidstr);
1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909
    virBufferVSprintf(buf, "  <bridge name='%s'/>\n", privnet->bridge);
    if (privnet->forward) {
        if (privnet->forwardDev[0])
            virBufferVSprintf(buf, "  <forward dev='%s'/>\n", privnet->forwardDev);
        else
            virBufferAdd(buf, "  <forward/>\n", -1);
    }

    virBufferVSprintf(buf, "  <ip address='%s' netmask='%s'>\n",
                      privnet->ipAddress, privnet->ipNetmask);
    virBufferAdd(buf, "    <dhcp>\n", -1);
    virBufferVSprintf(buf, "      <range start='%s' end='%s'/>\n",
                      privnet->dhcpStart, privnet->dhcpEnd);
    virBufferAdd(buf, "    </dhcp>\n", -1);
    virBufferAdd(buf, "  </ip>\n", -1);

    virBufferAdd(buf, "</network>\n", -1);

    xml = buf->content;
    free(buf);

    return (xml);
}

static char *testNetworkGetBridgeName(virNetworkPtr network) {
    char *bridge;
    GET_NETWORK(network, NULL);
    bridge = strdup(privnet->bridge);
    if (!bridge) {
        testError(network->conn, NULL, network, VIR_ERR_NO_MEMORY, "network");
        return NULL;
    }
    return bridge;
}

static int testNetworkGetAutostart(virNetworkPtr network,
                                   int *autostart) {
    GET_NETWORK(network, -1);
    *autostart = privnet->autostart;
    return (0);
}

static int testNetworkSetAutostart(virNetworkPtr network,
                                   int autostart) {
    GET_NETWORK(network, -1);
    privnet->autostart = autostart ? 1 : 0;
1910 1911
    return (0);
}
1912 1913 1914 1915 1916 1917 1918 1919


static virDriver testDriver = {
    VIR_DRV_TEST,
    "Test",
    LIBVIR_VERSION_NUMBER,
    testOpen, /* open */
    testClose, /* close */
1920
    NULL, /* supports_feature */
1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963
    NULL, /* type */
    testGetVersion, /* version */
    testGetHostname, /* hostname */
    testGetURI, /* URI */
    testGetMaxVCPUs, /* getMaxVcpus */
    testNodeGetInfo, /* nodeGetInfo */
    testGetCapabilities, /* getCapabilities */
    testListDomains, /* listDomains */
    testNumOfDomains, /* numOfDomains */
    testDomainCreateLinux, /* domainCreateLinux */
    testLookupDomainByID, /* domainLookupByID */
    testLookupDomainByUUID, /* domainLookupByUUID */
    testLookupDomainByName, /* domainLookupByName */
    testPauseDomain, /* domainSuspend */
    testResumeDomain, /* domainResume */
    testShutdownDomain, /* domainShutdown */
    testRebootDomain, /* domainReboot */
    testDestroyDomain, /* domainDestroy */
    testGetOSType, /* domainGetOSType */
    testGetMaxMemory, /* domainGetMaxMemory */
    testSetMaxMemory, /* domainSetMaxMemory */
    testSetMemory, /* domainSetMemory */
    testGetDomainInfo, /* domainGetInfo */
    testDomainSave, /* domainSave */
    testDomainRestore, /* domainRestore */
    testDomainCoreDump, /* domainCoreDump */
    testSetVcpus, /* domainSetVcpus */
    NULL, /* domainPinVcpu */
    NULL, /* domainGetVcpus */
    NULL, /* domainGetMaxVcpus */
    testDomainDumpXML, /* domainDumpXML */
    testListDefinedDomains, /* listDefinedDomains */
    testNumOfDefinedDomains, /* numOfDefinedDomains */
    testDomainCreate, /* domainCreate */
    testDomainDefineXML, /* domainDefineXML */
    testDomainUndefine, /* domainUndefine */
    NULL, /* domainAttachDevice */
    NULL, /* domainDetachDevice */
    testDomainGetAutostart, /* domainGetAutostart */
    testDomainSetAutostart, /* domainSetAutostart */
    testDomainGetSchedulerType, /* domainGetSchedulerType */
    testDomainGetSchedulerParams, /* domainGetSchedulerParameters */
    testDomainSetSchedulerParams, /* domainSetSchedulerParameters */
1964 1965 1966
    NULL, /* domainMigratePrepare */
    NULL, /* domainMigratePerform */
    NULL, /* domainMigrateFinish */
1967 1968
    NULL, /* domainBlockStats */
    NULL, /* domainInterfaceStats */
1969
    NULL, /* nodeGetCellsFreeMemory */
1970
    NULL, /* getFreeMemory */
1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009
};

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


/**
 * testRegister:
 *
 * Registers the test driver
 */
int
testRegister(void)
{
    if (virRegisterDriver(&testDriver) < 0)
        return -1;
    if (virRegisterNetworkDriver(&testNetworkDriver) < 0)
        return -1;
    return 0;
}

2010
#endif /* WITH_TEST */
2011

2012 2013 2014 2015 2016
/*
 * vim: set tabstop=4:
 * vim: set shiftwidth=4:
 * vim: set expandtab:
 */
2017 2018 2019 2020 2021 2022 2023 2024
/*
 * Local variables:
 *  indent-tabs-mode: nil
 *  c-indent-level: 4
 *  c-basic-offset: 4
 *  tab-width: 4
 * End:
 */