test.c 42.1 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 24 25 26
 *
 * Daniel Berrange <berrange@redhat.com>
 */

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

#include "internal.h"
#include "test.h"
36
#include "xml.h"
37

38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
int testOpen(virConnectPtr conn,
             const char *name,
             int flags);
int testClose  (virConnectPtr conn);
int testGetVersion(virConnectPtr conn,
                   unsigned long *hvVer);
int testNodeGetInfo(virConnectPtr conn,
                    virNodeInfoPtr info);
int testNumOfDomains(virConnectPtr conn);
int testListDomains(virConnectPtr conn,
                    int *ids,
                    int maxids);
char *testGetOSType(virDomainPtr dom);
virDomainPtr
testDomainCreateLinux(virConnectPtr conn, const char *xmlDesc,
                      unsigned int flags ATTRIBUTE_UNUSED);
virDomainPtr testLookupDomainByID(virConnectPtr conn,
                                  int id);
virDomainPtr testLookupDomainByUUID(virConnectPtr conn,
                                    const unsigned char *uuid);
virDomainPtr testLookupDomainByName(virConnectPtr conn,
                                    const char *name);
int testDestroyDomain(virDomainPtr domain);
int testResumeDomain(virDomainPtr domain);
int testPauseDomain(virDomainPtr domain);
int testShutdownDomain (virDomainPtr domain);
int testRebootDomain (virDomainPtr domain,
                      virDomainRestart action);
int testGetDomainInfo(virDomainPtr domain,
                      virDomainInfoPtr info);
unsigned long testGetMaxMemory(virDomainPtr domain);
int testSetMaxMemory(virDomainPtr domain,
                     unsigned long memory);
int testSetMemory(virDomainPtr domain,
                  unsigned long memory);
int testSetVcpus(virDomainPtr domain,
                 unsigned int nrCpus);
char * testDomainDumpXML(virDomainPtr domain, int flags);

int testNumOfDefinedDomains(virConnectPtr conn);
int testListDefinedDomains(virConnectPtr conn,
                           const char **names,
                           int maxnames);

virDomainPtr testDomainDefineXML(virConnectPtr conn,
                                 const char *xml);

int testDomainCreate(virDomainPtr dom);

int testDomainUndefine(virDomainPtr dom);

89
static virDriver testDriver = {
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
    VIR_DRV_TEST,
    "Test",
    LIBVIR_VERSION_NUMBER,
    testOpen, /* open */
    testClose, /* close */
    NULL, /* type */
    testGetVersion, /* version */
    testNodeGetInfo, /* nodeGetInfo */
    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 */
    NULL, /* domainSave */
    NULL, /* domainRestore */
    NULL, /* domainCoreDump */
    testSetVcpus, /* domainSetVcpus */
    NULL, /* domainPinVcpu */
    NULL, /* domainGetVcpus */
    testDomainDumpXML, /* domainDumpXML */
    testListDefinedDomains, /* listDefinedDomains */
    testNumOfDefinedDomains, /* numOfDefinedDomains */
    testDomainCreate, /* domainCreate */
    testDomainDefineXML, /* domainDefineXML */
    testDomainUndefine, /* domainUndefine */
    NULL, /* domainAttachDevice */
    NULL, /* domainDetachDevice */
128 129 130
};

typedef struct _testDev {
131 132
    char name[20];
    virDeviceMode mode;
133 134 135 136 137
} testDev;

#define MAX_DEVICES 10

typedef struct _testDom {
138
    int active;
139
    int id;
140
    char name[20];
141
    unsigned char uuid[VIR_UUID_BUFLEN];
142 143 144 145 146 147 148 149 150
    virDomainKernel kernel;
    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];
151 152 153 154 155
} testDom;

#define MAX_DOMAINS 20

typedef struct _testCon {
156 157 158 159
    int active;
    virNodeInfo nodeInfo;
    int numDomains;
    testDom domains[MAX_DOMAINS];
160 161 162 163 164
} testCon;

#define MAX_CONNECTIONS 5

typedef struct _testNode {
165 166
    int numConnections;
    testCon connections[MAX_CONNECTIONS];
167 168 169 170 171 172 173 174
} testNode;

/* XXX, how about we stuff this in a SHM
   segment so multiple apps can run tests
   against the mock hypervisor concurrently.
   Would need a pthread process shared mutex
   too probably */
static testNode *node = NULL;
175
static int nextDomID = 1;
176

177
static const virNodeInfo defaultNodeInfo = {
178 179 180 181 182 183 184 185
    "i686",
    1024*1024*3, /* 3 GB */
    16,
    1400,
    2,
    2,
    2,
    2,
186 187 188 189
};

static void
testError(virConnectPtr con,
190 191 192
          virDomainPtr dom,
          virErrorNumber error,
          const char *info)
193
{
194
    const char *errmsg;
195

196 197
    if (error == VIR_ERR_OK)
        return;
198

199
    errmsg = __virErrorMsg(error, info);
200
    __virRaiseError(con, dom, NULL, VIR_FROM_XEN, error, VIR_ERR_ERROR,
201
                    errmsg, info, NULL, 0, 0, errmsg, info, 0);
202 203
}

204
static int testRestartStringToFlag(const char *str) {
205 206 207 208 209 210 211 212 213 214 215
    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);
    }
216 217 218
}

static const char *testRestartFlagToString(int flag) {
219 220 221 222 223 224 225 226 227 228 229
    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);
230
}
231 232 233 234 235 236 237 238

/**
 * testRegister:
 *
 * Registers the test driver
 */
void testRegister(void)
{
239
    virRegisterDriver(&testDriver);
240 241
}

242
static int testLoadDomain(virConnectPtr conn,
243 244 245 246 247 248
                          int domid,
                          xmlDocPtr xml) {
    xmlNodePtr root = NULL;
    xmlXPathContextPtr ctxt = NULL;
    xmlXPathObjectPtr obj = NULL;
    char *name = NULL;
249
    unsigned char rawuuid[VIR_UUID_BUFLEN];
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
    char *dst_uuid;
    testCon *con;
    struct timeval tv;
    unsigned long memory = 0;
    unsigned long maxMem = 0;
    int nrVirtCpu;
    char *conv;
    int handle = -1, i;
    virDomainRestart onReboot = VIR_DOMAIN_RESTART;
    virDomainRestart onPoweroff = VIR_DOMAIN_DESTROY;
    virDomainRestart onCrash = VIR_DOMAIN_RENAME_RESTART;

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

267 268 269 270
    root = xmlDocGetRootElement(xml);
    if ((root == NULL) || (!xmlStrEqual(root->name, BAD_CAST "domain"))) {
        testError(conn, NULL, VIR_ERR_XML_ERROR, _("domain"));
        goto error;
271
    }
272 273 274 275 276 277 278 279 280 281 282 283 284 285

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

    obj = xmlXPathEval(BAD_CAST "string(/domain/name[1])", ctxt);
    if ((obj == NULL) || (obj->type != XPATH_STRING) ||
        (obj->stringval == NULL) || (obj->stringval[0] == 0)) {
        testError(conn, NULL, VIR_ERR_INTERNAL_ERROR, _("domain name"));
        goto error;
    }
    name = strdup((const char *)obj->stringval);
286 287
    xmlXPathFreeObject(obj);

288 289 290 291 292 293 294 295 296 297
    obj = xmlXPathEval(BAD_CAST "string(/domain/uuid[1])", ctxt);
    if ((obj == NULL) || (obj->type != XPATH_STRING) ||
        (obj->stringval == NULL) || (obj->stringval[0] == 0)) {
        testError(conn, NULL, VIR_ERR_XML_ERROR, _("domain uuid"));
        goto error;
    }
    dst_uuid = (char *) &rawuuid[0];
    if (!(virParseUUID((char **)&dst_uuid, (const char *)obj->stringval))) {
        testError(conn, NULL, VIR_ERR_XML_ERROR, _("domain uuid"));
        goto error;
298 299 300
    }
    xmlXPathFreeObject(obj);

301 302 303 304 305 306 307 308 309 310
    obj = xmlXPathEval(BAD_CAST "string(/domain/memory[1])", ctxt);
    if ((obj == NULL) || (obj->type != XPATH_STRING) ||
        (obj->stringval == NULL) || (obj->stringval[0] == 0)) {
        testError(conn, NULL, VIR_ERR_XML_ERROR, _("domain memory"));
        goto error;
    }
    maxMem = strtoll((const char*)obj->stringval, &conv, 10);
    if (conv == (const char*)obj->stringval) {
        testError(conn, NULL, VIR_ERR_XML_ERROR, _("domain memory"));
        goto error;
311 312 313 314
    }
    xmlXPathFreeObject(obj);


315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
    obj = xmlXPathEval(BAD_CAST "string(/domain/currentMemory[1])", ctxt);
    if ((obj == NULL) || (obj->type != XPATH_STRING) ||
        (obj->stringval == NULL) || (obj->stringval[0] == 0)) {
        memory = maxMem;
    } else {
        memory = strtoll((const char*)obj->stringval, &conv, 10);
        if (conv == (const char*)obj->stringval) {
            testError(conn, NULL, VIR_ERR_XML_ERROR, _("domain current memory"));
            goto error;
        }
    }
    if (obj)
        xmlXPathFreeObject(obj);


330 331


332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
    obj = xmlXPathEval(BAD_CAST "string(/domain/vcpu[1])", ctxt);
    if ((obj == NULL) || (obj->type != XPATH_STRING) ||
        (obj->stringval == NULL) || (obj->stringval[0] == 0)) {
        nrVirtCpu = 1;
    } else {
        nrVirtCpu = strtoll((const char*)obj->stringval, &conv, 10);
        if (conv == (const char*)obj->stringval) {
            testError(conn, NULL, VIR_ERR_XML_ERROR, _("domain vcpus"));
            goto error;
        }
    }
    if (obj)
        xmlXPathFreeObject(obj);

    obj = xmlXPathEval(BAD_CAST "string(/domain/on_reboot[1])", ctxt);
    if ((obj != NULL) && (obj->type == XPATH_STRING) &&
        (obj->stringval != NULL) && (obj->stringval[0] != 0)) {
        if (!(onReboot = testRestartStringToFlag((const char *)obj->stringval))) {
            testError(conn, NULL, VIR_ERR_XML_ERROR, _("domain reboot behaviour"));
            goto error;
        }
    }
    if (obj)
        xmlXPathFreeObject(obj);

    obj = xmlXPathEval(BAD_CAST "string(/domain/on_poweroff[1])", ctxt);
    if ((obj != NULL) && (obj->type == XPATH_STRING) &&
        (obj->stringval != NULL) && (obj->stringval[0] != 0)) {
        if (!(onReboot = testRestartStringToFlag((const char *)obj->stringval))) {
            testError(conn, NULL, VIR_ERR_XML_ERROR, _("domain poweroff behaviour"));
            goto error;
        }
    }
    if (obj)
        xmlXPathFreeObject(obj);

    obj = xmlXPathEval(BAD_CAST "string(/domain/on_crash[1])", ctxt);
    if ((obj != NULL) && (obj->type == XPATH_STRING) &&
        (obj->stringval != NULL) && (obj->stringval[0] != 0)) {
        if (!(onReboot = testRestartStringToFlag((const char *)obj->stringval))) {
            testError(conn, NULL, VIR_ERR_XML_ERROR, _("domain crash behaviour"));
            goto error;
        }
    }
    if (obj)
        xmlXPathFreeObject(obj);
378

379
    con = &node->connections[conn->handle];
380

381 382 383 384 385 386 387 388 389 390
    for (i = 0 ; i < MAX_DOMAINS ; i++) {
        if (!con->domains[i].active) {
            handle = i;
            break;
        }
    }
    if (handle < 0)
        return (-1);

    con->domains[handle].active = 1;
391
    con->domains[handle].id = domid;
392
    strncpy(con->domains[handle].name, name, sizeof(con->domains[handle].name));
393
    free(name);
394 395 396 397 398
    name = NULL;

    if (memory > maxMem)
        memory = maxMem;

399
    memmove(con->domains[handle].uuid, rawuuid, VIR_UUID_BUFLEN);
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
    con->domains[handle].info.maxMem = maxMem;
    con->domains[handle].info.memory = memory;
    con->domains[handle].info.state = domid < 0 ? VIR_DOMAIN_SHUTOFF : VIR_DOMAIN_RUNNING;
    con->domains[handle].info.nrVirtCpu = nrVirtCpu;
    con->domains[handle].info.cpuTime = ((tv.tv_sec * 1000ll * 1000ll  * 1000ll) + (tv.tv_usec * 1000ll));
    con->domains[handle].maxVCPUs = nrVirtCpu;

    con->domains[handle].onReboot = onReboot;
    con->domains[handle].onPoweroff = onPoweroff;
    con->domains[handle].onCrash = onCrash;

    return (0);

 error:
    if (obj)
        xmlXPathFreeObject(obj);
    if (name)
        free(name);
    return (-1);
419 420 421
}

static int testLoadDomainFromDoc(virConnectPtr conn,
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437
                                 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))) {
        testError(NULL, NULL, VIR_ERR_XML_ERROR, _("domain"));
        return (-1);
    }

    ret = testLoadDomain(conn, domid, xml);

    xmlFreeDoc(xml);

    return (ret);
438 439 440
}

static int testLoadDomainFromFile(virConnectPtr conn,
441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
                                  int domid,
                                  const char *file) {
    int ret, fd;
    xmlDocPtr xml;

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

    if (!(xml = xmlReadFd(fd, file, NULL,
                          XML_PARSE_NOENT | XML_PARSE_NONET |
                          XML_PARSE_NOERROR | XML_PARSE_NOWARNING))) {
        testError(NULL, NULL, VIR_ERR_XML_ERROR, _("domain"));
        close(fd);
        return (-1);
    }
458 459
    close(fd);

460
    ret = testLoadDomain(conn, domid, xml);
461

462
    xmlFreeDoc(xml);
463

464
    return (ret);
465 466 467 468
}


static int testOpenDefault(virConnectPtr conn,
469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
                           int connid) {
    int u;
    struct timeval tv;

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

    conn->handle = connid;
    node->connections[connid].active = 1;
    memmove(&node->connections[connid].nodeInfo, &defaultNodeInfo, sizeof(defaultNodeInfo));

    node->connections[connid].numDomains = 1;
    node->connections[connid].domains[0].active = 1;
484
    node->connections[connid].domains[0].id = nextDomID++;
485 486 487 488
    node->connections[connid].domains[0].onReboot = VIR_DOMAIN_RESTART;
    node->connections[connid].domains[0].onCrash = VIR_DOMAIN_RESTART;
    node->connections[connid].domains[0].onPoweroff = VIR_DOMAIN_DESTROY;
    strcpy(node->connections[connid].domains[0].name, "test");
489
    for (u = 0 ; u < VIR_UUID_BUFLEN ; u++) {
490 491 492 493 494 495 496 497
        node->connections[connid].domains[0].uuid[u] = (u * 75)%255;
    }
    node->connections[connid].domains[0].info.maxMem = 8192 * 1024;
    node->connections[connid].domains[0].info.memory = 2048 * 1024;
    node->connections[connid].domains[0].info.state = VIR_DOMAIN_RUNNING;
    node->connections[connid].domains[0].info.nrVirtCpu = 2;
    node->connections[connid].domains[0].info.cpuTime = ((tv.tv_sec * 1000ll * 1000ll  * 1000ll) + (tv.tv_usec * 1000ll));
    return (0);
498 499 500 501
}


static char *testBuildFilename(const char *relativeTo,
502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519
                               const char *filename) {
    char *offset;
    int baseLen;
    if (!filename || filename[0] == '\0')
        return (NULL);
    if (filename[0] == '/')
        return strdup(filename);

    offset = rindex(relativeTo, '/');
    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);
    }
520 521 522
}

static int testOpenFromFile(virConnectPtr conn,
523 524 525 526 527 528 529 530 531 532 533 534
                            int connid,
                            const char *file) {
    int fd, i;
    xmlDocPtr xml;
    xmlNodePtr root = NULL;
    xmlXPathContextPtr ctxt = NULL;
    xmlXPathObjectPtr obj = NULL;
    virNodeInfoPtr nodeInfo;

    if ((fd = open(file, O_RDONLY)) < 0) {
        testError(NULL, NULL, VIR_ERR_INTERNAL_ERROR, _("loading host definition file"));
        return (-1);
535 536
    }

537 538 539 540 541
    if (!(xml = xmlReadFd(fd, file, NULL,
                          XML_PARSE_NOENT | XML_PARSE_NONET |
                          XML_PARSE_NOERROR | XML_PARSE_NOWARNING))) {
        testError(NULL, NULL, VIR_ERR_INTERNAL_ERROR, _("host"));
        goto error;
542
    }
543 544
    close(fd);
    fd = -1;
545

546 547 548 549
    root = xmlDocGetRootElement(xml);
    if ((root == NULL) || (!xmlStrEqual(root->name, BAD_CAST "node"))) {
        testError(NULL, NULL, VIR_ERR_XML_ERROR, _("node"));
        goto error;
550 551
    }

552 553 554 555
    ctxt = xmlXPathNewContext(xml);
    if (ctxt == NULL) {
        testError(NULL, NULL, VIR_ERR_INTERNAL_ERROR, _("creating xpath context"));
        goto error;
556
    }
557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573

    conn->handle = connid;
    node->connections[connid].active = 1;
    node->connections[connid].numDomains = 0;
    memmove(&node->connections[connid].nodeInfo, &defaultNodeInfo, sizeof(defaultNodeInfo));

    nodeInfo = &node->connections[connid].nodeInfo;
    obj = xmlXPathEval(BAD_CAST "string(/node/cpu/nodes[1])", ctxt);
    if ((obj != NULL) && (obj->type == XPATH_STRING) &&
        (obj->stringval != NULL) && (obj->stringval[0] != 0)) {
        char *conv = NULL;
        nodeInfo->nodes = strtol((const char*)obj->stringval, &conv, 10);
        if (conv == (const char*)obj->stringval) {
            testError(conn, NULL, VIR_ERR_XML_ERROR, _("node cpu numa nodes"));
            goto error;
        }
        xmlXPathFreeObject(obj);
574
    }
575 576 577 578 579 580 581 582 583 584 585

    obj = xmlXPathEval(BAD_CAST "string(/node/cpu/sockets[1])", ctxt);
    if ((obj != NULL) && (obj->type == XPATH_STRING) &&
        (obj->stringval != NULL) && (obj->stringval[0] != 0)) {
        char *conv = NULL;
        nodeInfo->sockets = strtol((const char*)obj->stringval, &conv, 10);
        if (conv == (const char*)obj->stringval) {
            testError(conn, NULL, VIR_ERR_XML_ERROR, _("node cpu sockets"));
            goto error;
        }
        xmlXPathFreeObject(obj);
586
    }
587 588 589 590 591 592 593 594 595 596 597

    obj = xmlXPathEval(BAD_CAST "string(/node/cpu/cores[1])", ctxt);
    if ((obj != NULL) && (obj->type == XPATH_STRING) &&
        (obj->stringval != NULL) && (obj->stringval[0] != 0)) {
        char *conv = NULL;
        nodeInfo->cores = strtol((const char*)obj->stringval, &conv, 10);
        if (conv == (const char*)obj->stringval) {
            testError(conn, NULL, VIR_ERR_XML_ERROR, _("node cpu cores"));
            goto error;
        }
        xmlXPathFreeObject(obj);
598 599
    }

600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 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 641 642
    obj = xmlXPathEval(BAD_CAST "string(/node/cpu/threads[1])", ctxt);
    if ((obj != NULL) && (obj->type == XPATH_STRING) &&
        (obj->stringval != NULL) && (obj->stringval[0] != 0)) {
        char *conv = NULL;
        nodeInfo->threads = strtol((const char*)obj->stringval, &conv, 10);
        if (conv == (const char*)obj->stringval) {
            testError(conn, NULL, VIR_ERR_XML_ERROR, _("node cpu threads"));
            goto error;
        }
        xmlXPathFreeObject(obj);
    }
    nodeInfo->cpus = nodeInfo->cores * nodeInfo->threads * nodeInfo->sockets * nodeInfo->nodes;
    obj = xmlXPathEval(BAD_CAST "string(/node/cpu/active[1])", ctxt);
    if ((obj != NULL) && (obj->type == XPATH_STRING) &&
        (obj->stringval != NULL) && (obj->stringval[0] != 0)) {
        char *conv = NULL;
        unsigned int active = strtol((const char*)obj->stringval, &conv, 10);    
        if (conv == (const char*)obj->stringval) {
            testError(conn, NULL, VIR_ERR_XML_ERROR, _("node active cpu"));
            goto error;
        }
        if (active < nodeInfo->cpus) {
            nodeInfo->cpus = active;
        }
        xmlXPathFreeObject(obj);
    }
    obj = xmlXPathEval(BAD_CAST "string(/node/cpu/mhz[1])", ctxt);
    if ((obj != NULL) && (obj->type == XPATH_STRING) &&
        (obj->stringval != NULL) && (obj->stringval[0] != 0)) {
        char *conv = NULL;
        nodeInfo->mhz = strtol((const char*)obj->stringval, &conv, 10);
        if (conv == (const char*)obj->stringval) {
            testError(conn, NULL, VIR_ERR_XML_ERROR, _("node cpu mhz"));
            goto error;
        }
        xmlXPathFreeObject(obj);
    }
    obj = xmlXPathEval(BAD_CAST "string(/node/cpu/model[1])", ctxt);
    if ((obj != NULL) && (obj->type == XPATH_STRING) &&
        (obj->stringval != NULL) && (obj->stringval[0] != 0)) {
        strncpy(nodeInfo->model, (const char *)obj->stringval, sizeof(nodeInfo->model)-1);
        nodeInfo->model[sizeof(nodeInfo->model)-1] = '\0';
        xmlXPathFreeObject(obj);
643 644
    }

645 646 647 648 649 650 651 652 653 654 655
    obj = xmlXPathEval(BAD_CAST "string(/node/memory[1])", ctxt);
    if ((obj != NULL) && (obj->type == XPATH_STRING) &&
        (obj->stringval != NULL) && (obj->stringval[0] != 0)) {
        char *conv = NULL;
        nodeInfo->memory = strtol((const char*)obj->stringval, &conv, 10);
        if (conv == (const char*)obj->stringval) {
            testError(conn, NULL, VIR_ERR_XML_ERROR, _("node memory"));
            goto error;
        }
        xmlXPathFreeObject(obj);
    }
656

657 658 659 660 661
    obj = xmlXPathEval(BAD_CAST "/node/domain", ctxt);
    if ((obj == NULL) || (obj->type != XPATH_NODESET) ||
        (obj->nodesetval == NULL)) {
        testError(NULL, NULL, VIR_ERR_XML_ERROR, _("node domain list"));
        goto error;
662
    }
663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678

    for (i = 0 ; i < obj->nodesetval->nodeNr ; i++) {
        xmlChar *domFile = xmlGetProp(obj->nodesetval->nodeTab[i], BAD_CAST "file");
        char *absFile = testBuildFilename(file, (const char *)domFile);
        int domid = nextDomID++;
        free(domFile);
        if (!absFile) {
            testError(NULL, NULL, VIR_ERR_INTERNAL_ERROR, _("resolving domain filename"));
            goto error;
        }
        if (testLoadDomainFromFile(conn, domid, absFile) != 0) {
            free(absFile);
            goto error;
        }
        free(absFile);
        node->connections[connid].numDomains++;
679 680
    }

681 682
    xmlXPathFreeObject(obj);
    xmlFreeDoc(xml);
683

684
    return (0);
685 686

 error:
687 688 689 690 691 692
    if (node->connections[connid].active) {
        for (i = 0 ; i <node->connections[connid].numDomains ; i++) {
            node->connections[connid].domains[i].active = 0;
        }
        node->connections[connid].numDomains = 0;
        node->connections[connid].active = 0;
693
    }
694 695 696 697 698 699 700
    if (obj)
        xmlXPathFreeObject(obj);
    if (xml)
        xmlFreeDoc(xml);
    if (fd != -1)
        close(fd);
    return (-1);
701 702 703
}

static int getNextConnection(void) {
704 705 706 707 708 709 710 711 712 713 714 715 716 717
    int i;
    if (node == NULL) {
        node = calloc(1, sizeof(testNode));
        nextDomID = 1;
        if (!node) {
            testError(NULL, NULL, VIR_ERR_NO_MEMORY, _("allocating node"));
            return (-1);
        }
    }

    for (i = 0 ; i < MAX_CONNECTIONS ; i++) {
        if (!node->connections[i].active) {
            return (i);
        }
718
    }
719 720
    return (-1);
}
721

722 723 724 725 726
static int getDomainIndex(virDomainPtr domain) {
    int i;
    testCon *con;
    con = &node->connections[domain->conn->handle];
    for (i = 0 ; i < MAX_DOMAINS ; i++) {
727 728
        if (domain->id >= 0) {
            if (domain->id == con->domains[i].id)
729 730 731 732 733
                return (i);
        } else {
            if (!strcmp(domain->name, con->domains[i].name))
                return (i);
        }
734
    }
735
    return (-1);
736
}
737 738 739 740 741

int testOpen(virConnectPtr conn,
             const char *name,
             int flags)
{
742 743
    xmlURIPtr uri;
    int ret, connid;
744

745 746 747
    if (!name) {
        return (-1);
    }
748

749 750 751 752 753 754
    uri = xmlParseURI(name);
    if (uri == NULL) {
        if (!(flags & VIR_DRV_OPEN_QUIET))
            testError(conn, NULL, VIR_ERR_NO_SUPPORT, name);
        return(-1);
    }
755

756 757 758 759 760 761
    if (!uri->scheme ||
        strcmp(uri->scheme, "test") ||
        !uri->path) {
        xmlFreeURI(uri);
        return (-1);
    }
762 763


764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780
    if ((connid = getNextConnection()) < 0) {
        testError(NULL, NULL, VIR_ERR_INTERNAL_ERROR, _("too many connections"));
        return (-1);
    }

    if (!strcmp(uri->path, "/default")) {
        ret = testOpenDefault(conn,
                              connid);
    } else {
        ret = testOpenFromFile(conn,
                               connid,
                               uri->path);
    }

    xmlFreeURI(uri);

    return (ret);
781 782 783 784
}

int testClose(virConnectPtr conn)
{
785 786 787 788 789
    testCon *con = &node->connections[conn->handle];
    con->active = 0;
    conn->handle = -1;
    memset(con, 0, sizeof(testCon));
    return (0);
790 791
}

792
int testGetVersion(virConnectPtr conn ATTRIBUTE_UNUSED,
793 794
                   unsigned long *hvVer)
{
795 796
    *hvVer = 2;
    return (0);
797 798
}

799
int testNodeGetInfo(virConnectPtr conn,
800 801
                    virNodeInfoPtr info)
{
802 803 804
    testCon *con = &node->connections[conn->handle];
    memcpy(info, &con->nodeInfo, sizeof(virNodeInfo));
    return (0);
805 806 807 808
}

int testNumOfDomains(virConnectPtr conn)
{
809 810 811 812 813 814 815 816 817
    int numActive = 0, i;
    testCon *con = &node->connections[conn->handle];
    for (i = 0 ; i < MAX_DOMAINS ; i++) {
        if (!con->domains[i].active ||
            con->domains[i].info.state == VIR_DOMAIN_SHUTOFF)
            continue;
        numActive++;
    }
    return (numActive);
818 819
}

820 821
virDomainPtr
testDomainCreateLinux(virConnectPtr conn, const char *xmlDesc,
822
                      unsigned int flags ATTRIBUTE_UNUSED)
823
{
824 825 826
    testCon *con;
    int domid, handle = -1, i;
    virDomainPtr dom;
827

828 829 830 831 832 833 834 835 836 837 838 839
    if (!VIR_IS_CONNECT(conn)) {
        testError(conn, NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
        return (NULL);
    }
    if (xmlDesc == NULL) {
        testError(conn, NULL, VIR_ERR_INVALID_ARG, __FUNCTION__);
        return (NULL);
    }
    if (conn->flags & VIR_CONNECT_RO) {
        testError(conn, NULL, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
        return (NULL);
    }
840
  
841 842 843 844 845 846 847 848 849 850 851
    con = &node->connections[conn->handle];

    if (con->numDomains == MAX_DOMAINS) {
        testError(NULL, NULL, VIR_ERR_INTERNAL_ERROR, _("too many domains"));
        return (NULL);
    }

    domid = nextDomID++;
    if (testLoadDomainFromDoc(conn, domid, xmlDesc) < 0)
        return (NULL);
    for (i = 0 ; i < MAX_DOMAINS ; i++) {
852
        if (con->domains[i].id == domid) {
853 854 855 856 857 858 859 860 861 862 863
            handle = i;
            break;
        }
    }
    dom = virGetDomain(conn, con->domains[handle].name, con->domains[handle].uuid);
    if (dom == NULL) {
        testError(conn, NULL, VIR_ERR_NO_MEMORY, _("allocating domain"));
        return (NULL);
    }
    con->numDomains++;
    return (dom);
864 865 866
}


867 868 869
virDomainPtr testLookupDomainByID(virConnectPtr conn,
                                  int id)
{
870 871 872 873 874 875
    testCon *con = &node->connections[conn->handle];
    virDomainPtr dom;
    int i, idx = -1;

    for (i = 0 ; i < MAX_DOMAINS ; i++) {
        if (con->domains[i].active &&
876
            con->domains[i].id == id) {
877 878 879 880 881 882 883 884 885 886 887 888 889 890
            idx = i;
            break;
        }
    }

    if (idx < 0) {
        return(NULL);
    }

    dom = virGetDomain(conn, con->domains[idx].name, con->domains[idx].uuid);
    if (dom == NULL) {
        testError(conn, NULL, VIR_ERR_NO_MEMORY, _("allocating domain"));
        return(NULL);
    }
891
    dom->id = id;
892
    return (dom);
893 894 895 896 897
}

virDomainPtr testLookupDomainByUUID(virConnectPtr conn,
                                    const unsigned char *uuid)
{
898 899 900 901 902
    testCon *con = &node->connections[conn->handle];
    virDomainPtr dom = NULL;
    int i, idx = -1;
    for (i = 0 ; i < MAX_DOMAINS ; i++) {
        if (con->domains[i].active &&
903
            memcmp(uuid, con->domains[i].uuid, VIR_UUID_BUFLEN) == 0) {
904 905 906
            idx = i;
            break;
        }
907
    }
908 909 910 911 912 913
    if (idx >= 0) {
        dom = virGetDomain(conn, con->domains[idx].name, con->domains[idx].uuid);
        if (dom == NULL) {
            testError(conn, NULL, VIR_ERR_NO_MEMORY, _("allocating domain"));
            return(NULL);
        }
914
        dom->id = con->domains[idx].id;
915 916
    }
    return (dom);
917 918 919 920 921
}

virDomainPtr testLookupDomainByName(virConnectPtr conn,
                                    const char *name)
{
922 923 924 925 926 927 928 929 930
    testCon *con = &node->connections[conn->handle];
    virDomainPtr dom = NULL;
    int i, idx = -1;
    for (i = 0 ; i < MAX_DOMAINS ; i++) {
        if (con->domains[i].active &&
            strcmp(name, con->domains[i].name) == 0) {
            idx = i;
            break;
        }
931
    }
932 933 934 935 936 937
    if (idx >= 0) {
        dom = virGetDomain(conn, con->domains[idx].name, con->domains[idx].uuid);
        if (dom == NULL) {
            testError(conn, NULL, VIR_ERR_NO_MEMORY, _("allocating domain"));
            return(NULL);
        }
938
        dom->id = con->domains[idx].id;
939 940
    }
    return (dom);
941 942 943 944 945 946
}

int testListDomains (virConnectPtr conn,
                     int *ids,
                     int maxids)
{
947 948 949 950 951 952
    testCon *con = &node->connections[conn->handle];
    int n, i;

    for (i = 0, n = 0 ; i < MAX_DOMAINS && n < maxids ; i++) {
        if (con->domains[i].active &&
            con->domains[i].info.state != VIR_DOMAIN_SHUTOFF) {
953
            ids[n++] = con->domains[i].id;
954
        }
955
    }
956
    return (n);
957 958 959 960
}

int testDestroyDomain (virDomainPtr domain)
{
961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976
    testCon *con;
    int domidx;
    if ((domain == NULL) || (domain->conn == NULL) || (domain->name == NULL) ||
        ((domidx = getDomainIndex(domain)) < 0)) {
        testError((domain ? domain->conn : NULL), domain, VIR_ERR_INVALID_ARG,
                  __FUNCTION__);
        return (-1);
    }
    if (domain->conn->flags & VIR_CONNECT_RO) {
        testError(domain->conn, domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
        return (-1);
    }

    con = &node->connections[domain->conn->handle];
    con->domains[domidx].active = 0;
    return (0);
977 978 979 980
}

int testResumeDomain (virDomainPtr domain)
{
981 982 983 984 985 986 987 988 989 990 991 992
    testCon *con;
    int domidx;
    if ((domain == NULL) || (domain->conn == NULL) || (domain->name == NULL) ||
        ((domidx = getDomainIndex(domain)) < 0)) {
        testError((domain ? domain->conn : NULL), domain, VIR_ERR_INVALID_ARG,
                  __FUNCTION__);
        return (-1);
    }
    if (domain->conn->flags & VIR_CONNECT_RO) {
        testError(domain->conn, domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
        return (-1);
    }
993

994 995 996
    con = &node->connections[domain->conn->handle];
    con->domains[domidx].info.state = VIR_DOMAIN_RUNNING;
    return (0);
997 998 999 1000
}

int testPauseDomain (virDomainPtr domain)
{
1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012
    testCon *con;\
    int domidx;
    if ((domain == NULL) || (domain->conn == NULL) || (domain->name == NULL) ||
        ((domidx = getDomainIndex(domain)) < 0)) {
        testError((domain ? domain->conn : NULL), domain, VIR_ERR_INVALID_ARG,
                  __FUNCTION__);
        return (-1);
    }
    if (domain->conn->flags & VIR_CONNECT_RO) {
        testError(domain->conn, domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
        return (-1);
    }
1013

1014 1015 1016
    con = &node->connections[domain->conn->handle];
    con->domains[domidx].info.state = VIR_DOMAIN_PAUSED;
    return (0);
1017 1018
}

1019 1020 1021 1022 1023 1024
/* We don't do an immediate shutdown. We basically pretend that
   out shutdown sequence takes 'n' seconds to complete. SO, here
   we just set state to shutdown, and subsquent calls to getDomainInfo
   will check to see if shutdown ought to be marked complete. */
int testShutdownDomain (virDomainPtr domain)
{
1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037
    testCon *con;
    int domidx;
    struct timeval tv;
    if ((domain == NULL) || (domain->conn == NULL) || (domain->name == NULL) ||
        ((domidx = getDomainIndex(domain)) < 0)) {
        testError((domain ? domain->conn : NULL), domain, VIR_ERR_INVALID_ARG,
                  __FUNCTION__);
        return (-1);
    }
    if (domain->conn->flags & VIR_CONNECT_RO) {
        testError(domain->conn, domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
        return (-1);
    }
1038

1039
    con = &node->connections[domain->conn->handle];
1040

1041 1042 1043 1044
    if (gettimeofday(&tv, NULL) < 0) {
        testError(NULL, NULL, VIR_ERR_INTERNAL_ERROR, _("getting time of day"));
        return (-1);
    }
1045

1046
    con->domains[domidx].info.state = VIR_DOMAIN_SHUTOFF;
1047 1048
    domain->id = -1;
    con->domains[domidx].id = -1;
1049 1050

    return (0);
1051 1052 1053 1054 1055
}

/* Similar behaviour as shutdown */
int testRebootDomain (virDomainPtr domain, virDomainRestart action)
{
1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068
    testCon *con;
    int domidx;
    struct timeval tv;
    if ((domain == NULL) || (domain->conn == NULL) || (domain->name == NULL) ||
        ((domidx = getDomainIndex(domain)) < 0)) {
        testError((domain ? domain->conn : NULL), domain, VIR_ERR_INVALID_ARG,
                  __FUNCTION__);
        return (-1);
    }
    if (domain->conn->flags & VIR_CONNECT_RO) {
        testError(domain->conn, domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
        return (-1);
    }
1069

1070
    con = &node->connections[domain->conn->handle];
1071

1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096
    if (gettimeofday(&tv, NULL) < 0) {
        testError(NULL, NULL, VIR_ERR_INTERNAL_ERROR, _("getting time of day"));
        return (-1);
    }

    if (!action)
        action = VIR_DOMAIN_RESTART;

    con->domains[domidx].info.state = VIR_DOMAIN_SHUTDOWN;
    switch (action) {
    case VIR_DOMAIN_DESTROY:
        con->domains[domidx].info.state = VIR_DOMAIN_SHUTOFF;
        break;

    case VIR_DOMAIN_RESTART:
        con->domains[domidx].info.state = VIR_DOMAIN_RUNNING;
        break;

    case VIR_DOMAIN_PRESERVE:
        con->domains[domidx].info.state = VIR_DOMAIN_SHUTOFF;
        break;

    case VIR_DOMAIN_RENAME_RESTART:
        con->domains[domidx].info.state = VIR_DOMAIN_RUNNING;
        break;
1097

1098 1099 1100 1101
    default:
        con->domains[domidx].info.state = VIR_DOMAIN_SHUTOFF;
        break;
    }
1102 1103
    domain->id = -1;
    con->domains[domidx].id = -1;
1104

1105
    return (0);
1106 1107
}

1108 1109 1110
int testGetDomainInfo (virDomainPtr domain,
                       virDomainInfoPtr info)
{
1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135
    struct timeval tv;
    testCon *con;
    int domidx;
    if ((domain == NULL) || (domain->conn == NULL) || (domain->name == NULL) ||
        ((domidx = getDomainIndex(domain)) < 0)) {
        testError((domain ? domain->conn : NULL), domain, VIR_ERR_INVALID_ARG,
                  __FUNCTION__);
        return (-1);
    }

    con = &node->connections[domain->conn->handle];

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

    if (con->domains[domidx].info.state == VIR_DOMAIN_SHUTOFF) {
        con->domains[domidx].info.cpuTime = 0;
        con->domains[domidx].info.memory = 0;
    } else {
        con->domains[domidx].info.cpuTime = ((tv.tv_sec * 1000ll * 1000ll  * 1000ll) + (tv.tv_usec * 1000ll));
    }
    memcpy(info, &con->domains[domidx].info, sizeof(virDomainInfo));
    return (0);
1136 1137
}

1138
char *testGetOSType(virDomainPtr dom ATTRIBUTE_UNUSED) {
1139
    return strdup("linux");
1140 1141
}

1142
unsigned long testGetMaxMemory(virDomainPtr domain) {
1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153
    testCon *con;
    int domidx;
    if ((domain == NULL) || (domain->conn == NULL) || (domain->name == NULL) ||
        ((domidx = getDomainIndex(domain)) < 0)) {
        testError((domain ? domain->conn : NULL), domain, VIR_ERR_INVALID_ARG,
                  __FUNCTION__);
        return (-1);
    }

    con = &node->connections[domain->conn->handle];
    return con->domains[domidx].info.maxMem;
1154 1155
}

1156 1157
int testSetMaxMemory(virDomainPtr domain,
                     unsigned long memory)
1158
{
1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170
    testCon *con;
    int domidx;
    if ((domain == NULL) || (domain->conn == NULL) || (domain->name == NULL) ||
        ((domidx = getDomainIndex(domain)) < 0)) {
        testError((domain ? domain->conn : NULL), domain, VIR_ERR_INVALID_ARG,
                  __FUNCTION__);
        return (-1);
    }
    if (domain->conn->flags & VIR_CONNECT_RO) {
        testError(domain->conn, domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
        return (-1);
    }
1171

1172 1173 1174 1175
    con = &node->connections[domain->conn->handle];
    /* XXX validate not over host memory wrt to other domains */
    con->domains[domidx].info.maxMem = memory;
    return (0);
1176 1177
}

1178 1179
int testSetMemory(virDomainPtr domain,
                  unsigned long memory)
1180
{
1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192
    testCon *con;
    int domidx;
    if ((domain == NULL) || (domain->conn == NULL) || (domain->name == NULL) ||
        ((domidx = getDomainIndex(domain)) < 0)) {
        testError((domain ? domain->conn : NULL), domain, VIR_ERR_INVALID_ARG,
                  __FUNCTION__);
        return (-1);
    }
    if (domain->conn->flags & VIR_CONNECT_RO) {
        testError(domain->conn, domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
        return (-1);
    }
1193

1194
    con = &node->connections[domain->conn->handle];
1195

1196 1197 1198 1199
    if (memory > con->domains[domidx].info.maxMem) {
        testError(domain->conn, domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
        return (-1);
    }
1200

1201 1202
    con->domains[domidx].info.memory = memory;
    return (0);
1203 1204 1205
}

int testSetVcpus(virDomainPtr domain,
1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218
                 unsigned int nrCpus) {
    testCon *con;
    int domidx;
    if ((domain == NULL) || (domain->conn == NULL) || (domain->name == NULL) ||
        ((domidx = getDomainIndex(domain)) < 0)) {
        testError((domain ? domain->conn : NULL), domain, VIR_ERR_INVALID_ARG,
                  __FUNCTION__);
        return (-1);
    }
    if (domain->conn->flags & VIR_CONNECT_RO) {
        testError(domain->conn, domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
        return (-1);
    }
1219

1220
    con = &node->connections[domain->conn->handle];
1221

1222 1223 1224 1225 1226
    /* We allow more cpus in guest than host */
    if (nrCpus > 32) {
        testError(domain->conn, domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
        return (-1);
    }
1227

1228 1229
    con->domains[domidx].info.nrVirtCpu = nrCpus;
    return (0);
1230 1231 1232 1233
}

char * testDomainDumpXML(virDomainPtr domain, int flags ATTRIBUTE_UNUSED)
{
1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251
    virBufferPtr buf;
    char *xml;
    unsigned char *uuid;
    testCon *con;
    int domidx;
    if ((domain == NULL) || (domain->conn == NULL) || (domain->name == NULL) ||
        ((domidx = getDomainIndex(domain)) < 0)) {
        testError((domain ? domain->conn : NULL), domain, VIR_ERR_INVALID_ARG,
                  __FUNCTION__);
        return (NULL);
    }

    con = &node->connections[domain->conn->handle];

    if (!(buf = virBufferNew(4000))) {
        return (NULL);
    }

1252
    virBufferVSprintf(buf, "<domain type='test' id='%d'>\n", domain->id);
1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273
    virBufferVSprintf(buf, "  <name>%s</name>\n", domain->name);
    uuid = domain->uuid;
    virBufferVSprintf(buf,
                      "  <uuid>%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x</uuid>\n",
                      uuid[0], uuid[1], uuid[2], uuid[3],
                      uuid[4], uuid[5], uuid[6], uuid[7],
                      uuid[8], uuid[9], uuid[10], uuid[11],
                      uuid[12], uuid[13], uuid[14], uuid[15]);

    virBufferVSprintf(buf, "  <memory>%d</memory>\n", con->domains[domidx].info.maxMem);
    virBufferVSprintf(buf, "  <vcpu>%d</vcpu>\n", con->domains[domidx].info.nrVirtCpu);
    virBufferVSprintf(buf, "  <on_reboot>%s</on_reboot>\n", testRestartFlagToString(con->domains[domidx].onReboot));
    virBufferVSprintf(buf, "  <on_poweroff>%s</on_poweroff>\n", testRestartFlagToString(con->domains[domidx].onPoweroff));
    virBufferVSprintf(buf, "  <on_crash>%s</on_crash>\n", testRestartFlagToString(con->domains[domidx].onCrash));

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

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

    return (xml);
1274
}
1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 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 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345


int testNumOfDefinedDomains(virConnectPtr conn) {
    int numInactive = 0, i;
    testCon *con = &node->connections[conn->handle];
    for (i = 0 ; i < MAX_DOMAINS ; i++) {
        if (!con->domains[i].active ||
            con->domains[i].info.state != VIR_DOMAIN_SHUTOFF)
            continue;
        numInactive++;
    }
    return (numInactive);
}

int testListDefinedDomains(virConnectPtr conn,
                           const char **names,
                           int maxnames) {
    testCon *con = &node->connections[conn->handle];
    int n = 0, i;

    for (i = 0, n = 0 ; i < MAX_DOMAINS && n < maxnames ; i++) {
        if (con->domains[i].active &&
            con->domains[i].info.state == VIR_DOMAIN_SHUTOFF) {
            names[n++] = strdup(con->domains[i].name);
        }
    }
    return (n);
}

virDomainPtr testDomainDefineXML(virConnectPtr conn,
                                 const char *doc) {
    int ret;
    xmlDocPtr xml;
    int domid;

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

    domid = nextDomID++;
    ret = testLoadDomain(conn, domid, xml);

    xmlFreeDoc(xml);

    if (ret < 0)
        return (NULL);

    return testLookupDomainByID(conn, domid);
}

int testDomainCreate(virDomainPtr domain) {
    testCon *con;
    int domidx;
    if ((domain == NULL) || (domain->conn == NULL) || (domain->name == NULL) ||
        ((domidx = getDomainIndex(domain)) < 0)) {
        testError((domain ? domain->conn : NULL), domain, VIR_ERR_INVALID_ARG,
                  __FUNCTION__);
        return (-1);
    }

    con = &node->connections[domain->conn->handle];

    if (con->domains[domidx].info.state != VIR_DOMAIN_SHUTOFF) {
        testError(domain->conn, domain, VIR_ERR_INTERNAL_ERROR,
                  _("Domain is already running"));
        return (-1);
    }

1346
    domain->id = con->domains[domidx].id = nextDomID++;
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 1380 1381 1382
    con->domains[domidx].info.state = VIR_DOMAIN_RUNNING;

    return (0);
}

int testDomainUndefine(virDomainPtr domain) {
    testCon *con;
    int domidx;
    if ((domain == NULL) || (domain->conn == NULL) || (domain->name == NULL) ||
        ((domidx = getDomainIndex(domain)) < 0)) {
        testError((domain ? domain->conn : NULL), domain, VIR_ERR_INVALID_ARG,
                  __FUNCTION__);
        return (-1);
    }

    con = &node->connections[domain->conn->handle];

    if (con->domains[domidx].info.state != VIR_DOMAIN_SHUTOFF) {
        testError(domain->conn, domain, VIR_ERR_INTERNAL_ERROR,
                  _("Domain is still running"));
        return (-1);
    }

    con->domains[domidx].active = 0;

    return (0);
}

/*
 * Local variables:
 *  indent-tabs-mode: nil
 *  c-indent-level: 4
 *  c-basic-offset: 4
 *  tab-width: 4
 * End:
 */