test.c 31.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
/*
 * test.c: A "mock" hypervisor for use by application unit tests
 *
 * Copyright (C) 2006 Red Hat, Inc.
 *
 * See COPYING.LIB for the License of this software
 *
 * Daniel Berrange <berrange@redhat.com>
 */

#include <stdio.h>
#include <string.h>
#include <sys/time.h>
14 15 16
#include <libxml/parser.h>
#include <libxml/tree.h>
#include <libxml/xpath.h>
17
#include <libxml/uri.h>
18 19
#include <fcntl.h>
#include <unistd.h>
20 21 22

#include "internal.h"
#include "test.h"
23
#include "xml.h"
24 25

static virDriver testDriver = {
26
  VIR_DRV_TEST,
27
  "Test",
28
  LIBVIR_VERSION_NUMBER,
29 30 31 32 33 34 35 36
  NULL, /* init */
  testOpen, /* open */
  testClose, /* close */
  NULL, /* type */
  testGetVersion, /* version */
  testNodeGetInfo, /* nodeGetInfo */
  testListDomains, /* listDomains */
  testNumOfDomains, /* numOfDomains */
37
  testDomainCreateLinux, /* domainCreateLinux */
38 39 40 41 42
  testLookupDomainByID, /* domainLookupByID */
  testLookupDomainByUUID, /* domainLookupByUUID */
  testLookupDomainByName, /* domainLookupByName */
  testPauseDomain, /* domainSuspend */
  testResumeDomain, /* domainResume */
43 44
  testShutdownDomain, /* domainShutdown */
  testRebootDomain, /* domainReboot */
45 46 47 48 49 50
  testDestroyDomain, /* domainDestroy */
  NULL, /* domainFree */
  NULL, /* domainGetName */
  NULL, /* domainGetID */
  NULL, /* domainGetUUID */
  NULL, /* domainGetOSType */
51
  testGetMaxMemory, /* domainGetMaxMemory */
52
  testSetMaxMemory, /* domainSetMaxMemory */
53
  testSetMemory, /* domainSetMemory */
54 55
  testGetDomainInfo, /* domainGetInfo */
  NULL, /* domainSave */
56
  NULL, /* domainRestore */
57
  testSetVcpus, /* domainSetVcpus */
58
  NULL, /* domainPinVcpu */
59
  NULL, /* domainGetVcpus */
60
  testDomainDumpXML, /* domainDumpXML */
61 62 63 64 65
  NULL, /* listDefinedDomains */
  NULL, /* numOfDefinedDomains */
  NULL, /* domainCreate */
  NULL, /* domainDefineXML */
  NULL, /* domainUndefine */
66 67
};

68 69 70
/* Amount of time it takes to shutdown */
#define SHUTDOWN_DURATION 15

71 72 73 74 75 76 77 78 79 80 81 82 83
typedef struct _testDev {
  char name[20];
  virDeviceMode mode;
} testDev;

#define MAX_DEVICES 10

typedef struct _testDom {
  int active;
  char name[20];
  unsigned char uuid[16];
  virDomainKernel kernel;
  virDomainInfo info;
84
  time_t shutdownStartedAt;
85 86 87 88
  virDomainRestart onRestart; /* What to do at end of current shutdown procedure */
  virDomainRestart onReboot;
  virDomainRestart onPoweroff;
  virDomainRestart onCrash;
89 90 91 92 93 94 95 96
  int numDevices;
  testDev devices[MAX_DEVICES];
} testDom;

#define MAX_DOMAINS 20

typedef struct _testCon {
  int active;
97
  virNodeInfo nodeInfo;
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
  int numDomains;
  testDom domains[MAX_DOMAINS];
} testCon;

#define MAX_CONNECTIONS 5

typedef struct _testNode {
  int numConnections;
  testCon connections[MAX_CONNECTIONS];
} 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;

116 117
static const virNodeInfo defaultNodeInfo = {
  "i686",
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
  1024*1024*3, /* 3 GB */
  16,
  1400,
  2,
  2,
  2,
  2,
};

static void
testError(virConnectPtr con,
	  virDomainPtr dom,
	  virErrorNumber error,
	  const char *info)
{
  const char *errmsg;

  if (error == VIR_ERR_OK)
    return;

  errmsg = __virErrorMsg(error, info);
  __virRaiseError(con, dom, VIR_FROM_XEN, error, VIR_ERR_ERROR,
		  errmsg, info, NULL, 0, 0, errmsg, info, 0);
}

143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
static int testRestartStringToFlag(const char *str) {
  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;
  }
}

static const char *testRestartFlagToString(int flag) {
  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;
}
170 171 172 173 174 175 176 177 178 179 180

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

181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
static int testLoadDomain(virConnectPtr conn,
			  int domid,
			  xmlDocPtr xml) {
  xmlNodePtr root = NULL;
  xmlXPathContextPtr ctxt = NULL;
  xmlXPathObjectPtr obj = NULL;
  char *name = NULL;
  unsigned char rawuuid[16];
  char *dst_uuid;
  testCon *con;
  struct timeval tv;
  unsigned long memory;
  int nrVirtCpu;
  char *conv;
  virDomainRestart onReboot = VIR_DOMAIN_RESTART;
  virDomainRestart onPoweroff = VIR_DOMAIN_DESTROY;
  virDomainRestart onCrash = VIR_DOMAIN_RENAME_RESTART;

  if (gettimeofday(&tv, NULL) < 0) {
200
    testError(conn, NULL, VIR_ERR_INTERNAL_ERROR, _("getting time of day"));
201 202 203 204 205
    return -1;
  }

  root = xmlDocGetRootElement(xml);
  if ((root == NULL) || (!xmlStrEqual(root->name, BAD_CAST "domain"))) {
206
    testError(conn, NULL, VIR_ERR_XML_ERROR, _("domain"));
207 208 209 210 211
    goto error;
  }

  ctxt = xmlXPathNewContext(xml);
  if (ctxt == NULL) {
212
    testError(conn, NULL, VIR_ERR_INTERNAL_ERROR, _("creating xpath context"));
213 214 215 216 217 218
    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)) {
219
    testError(conn, NULL, VIR_ERR_INTERNAL_ERROR, _("domain name"));
220 221 222 223 224 225 226 227
    goto error;
  }
  name = strdup((const char *)obj->stringval);
  xmlXPathFreeObject(obj);

  obj = xmlXPathEval(BAD_CAST "string(/domain/uuid[1])", ctxt);
  if ((obj == NULL) || (obj->type != XPATH_STRING) ||
      (obj->stringval == NULL) || (obj->stringval[0] == 0)) {
228
    testError(conn, NULL, VIR_ERR_XML_ERROR, _("domain uuid"));
229 230 231 232
    goto error;
  }
  dst_uuid = (char *) &rawuuid[0];
  if (!(virParseUUID((char **)&dst_uuid, (const char *)obj->stringval))) {
233
    testError(conn, NULL, VIR_ERR_XML_ERROR, _("domain uuid"));
234 235 236 237 238 239 240
    goto error;
  }
  xmlXPathFreeObject(obj);

  obj = xmlXPathEval(BAD_CAST "string(/domain/memory[1])", ctxt);
  if ((obj == NULL) || (obj->type != XPATH_STRING) ||
      (obj->stringval == NULL) || (obj->stringval[0] == 0)) {
241
    testError(conn, NULL, VIR_ERR_XML_ERROR, _("domain memory"));
242 243 244 245
    goto error;
  }
  memory = strtoll((const char*)obj->stringval, &conv, 10);
  if (conv == (const char*)obj->stringval) {
246
    testError(conn, NULL, VIR_ERR_XML_ERROR, _("domain memory"));
247 248 249 250 251 252 253 254 255 256 257
    goto error;
  }
  xmlXPathFreeObject(obj);

  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) {
258
      testError(conn, NULL, VIR_ERR_XML_ERROR, _("domain vcpus"));
259 260 261 262 263 264 265 266 267 268
      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))) {
269
      testError(conn, NULL, VIR_ERR_XML_ERROR, _("domain reboot behaviour"));
270 271 272 273 274 275 276 277 278 279
      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))) {
280
      testError(conn, NULL, VIR_ERR_XML_ERROR, _("domain poweroff behaviour"));
281 282 283 284 285 286 287 288 289 290
      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))) {
291
      testError(conn, NULL, VIR_ERR_XML_ERROR, _("domain crash behaviour"));
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
      goto error;
    }
  }
  if (obj)
    xmlXPathFreeObject(obj);

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

  con->domains[domid].active = 1;
  strncpy(con->domains[domid].name, name, sizeof(con->domains[domid].name));
  free(name);
  name = NULL;

  memmove(con->domains[domid].uuid, rawuuid, 16);
  con->domains[domid].info.maxMem = memory;
  con->domains[domid].info.memory = memory;
  con->domains[domid].info.state = VIR_DOMAIN_RUNNING;
  con->domains[domid].info.nrVirtCpu = nrVirtCpu;
  con->domains[domid].info.cpuTime = ((tv.tv_sec * 1000ll * 1000ll  * 1000ll) + (tv.tv_usec * 1000ll));

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

  return 0;

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

static int testLoadDomainFromDoc(virConnectPtr conn,
				 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))) {
334
    testError(NULL, NULL, VIR_ERR_XML_ERROR, _("domain"));
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
    return -1;
  }

  ret = testLoadDomain(conn, domid, xml);

  xmlFreeDoc(xml);

  return ret;
}

static int testLoadDomainFromFile(virConnectPtr conn,
				  int domid,
				  const char *file) {
  int ret, fd;
  xmlDocPtr xml;

  if ((fd = open(file, O_RDONLY)) < 0) {
352
    testError(NULL, NULL, VIR_ERR_INTERNAL_ERROR, _("load domain definition file"));
353 354 355 356 357 358
    return -1;
  }

  if (!(xml = xmlReadFd(fd, file, NULL,
			XML_PARSE_NOENT | XML_PARSE_NONET |
			XML_PARSE_NOERROR | XML_PARSE_NOWARNING))) {
359
    testError(NULL, NULL, VIR_ERR_XML_ERROR, _("domain"));
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
    close(fd);
    return -1;
  }
  close(fd);

  ret = testLoadDomain(conn, domid, xml);

  xmlFreeDoc(xml);

  return ret;
}


static int testOpenDefault(virConnectPtr conn,
			   int connid) {
  int u;
  struct timeval tv;

  if (gettimeofday(&tv, NULL) < 0) {
379
    testError(NULL, NULL, VIR_ERR_INTERNAL_ERROR, _("getting time of day"));
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
    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;
  strcpy(node->connections[connid].domains[0].name, "Domain-0");
  for (u = 0 ; u < 16 ; u++) {
    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;
}


static char *testBuildFilename(const char *relativeTo,
			       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);
  }
}

static int testOpenFromFile(virConnectPtr conn,
			    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) {
434
    testError(NULL, NULL, VIR_ERR_INTERNAL_ERROR, _("loading host definition file"));
435 436 437 438 439 440
    return -1;
  }

  if (!(xml = xmlReadFd(fd, file, NULL,
			XML_PARSE_NOENT | XML_PARSE_NONET |
			XML_PARSE_NOERROR | XML_PARSE_NOWARNING))) {
441
    testError(NULL, NULL, VIR_ERR_INTERNAL_ERROR, _("host"));
442 443 444 445 446 447 448
    goto error;
  }
  close(fd);
  fd = -1;

  root = xmlDocGetRootElement(xml);
  if ((root == NULL) || (!xmlStrEqual(root->name, BAD_CAST "node"))) {
449
    testError(NULL, NULL, VIR_ERR_XML_ERROR, _("node"));
450 451 452 453 454
    goto error;
  }

  ctxt = xmlXPathNewContext(xml);
  if (ctxt == NULL) {
455
    testError(NULL, NULL, VIR_ERR_INTERNAL_ERROR, _("creating xpath context"));
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470
    goto error;
  }

  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) {
471
      testError(conn, NULL, VIR_ERR_XML_ERROR, _("node cpu numa nodes"));
472 473 474 475 476 477 478 479 480 481 482
      goto error;
    }
    xmlXPathFreeObject(obj);
  }

  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) {
483
      testError(conn, NULL, VIR_ERR_XML_ERROR, _("node cpu sockets"));
484 485 486 487 488 489 490 491 492 493 494
      goto error;
    }
    xmlXPathFreeObject(obj);
  }

  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) {
495
      testError(conn, NULL, VIR_ERR_XML_ERROR, _("node cpu cores"));
496 497 498 499 500 501 502 503 504 505 506
      goto error;
    }
    xmlXPathFreeObject(obj);
  }

  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) {
507
      testError(conn, NULL, VIR_ERR_XML_ERROR, _("node cpu threads"));
508 509 510 511 512 513 514 515 516 517 518
      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) {
519
      testError(conn, NULL, VIR_ERR_XML_ERROR, _("node active cpu"));
520 521 522 523 524 525 526 527 528 529 530 531 532
      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) {
533
      testError(conn, NULL, VIR_ERR_XML_ERROR, _("node cpu mhz"));
534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551
      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);
  }

  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) {
552
      testError(conn, NULL, VIR_ERR_XML_ERROR, _("node memory"));
553 554 555 556 557 558 559 560
      goto error;
    }
    xmlXPathFreeObject(obj);
  }

  obj = xmlXPathEval(BAD_CAST "/node/domain", ctxt);
  if ((obj == NULL) || (obj->type != XPATH_NODESET) ||
      (obj->nodesetval == NULL)) {
561
    testError(NULL, NULL, VIR_ERR_XML_ERROR, _("node domain list"));
562 563 564 565 566 567 568 569
    goto error;
  }

  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);
    free(domFile);
    if (!absFile) {
570
      testError(NULL, NULL, VIR_ERR_INTERNAL_ERROR, _("resolving domain filename"));
571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607
      goto error;
    }
    if (testLoadDomainFromFile(conn, i, absFile) != 0) {
      free(absFile);
      goto error;
    }
    free(absFile);
    node->connections[connid].numDomains++;
  }

  xmlXPathFreeObject(obj);
  xmlFreeDoc(xml);

  return 0;

 error:
  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;
  }
  if (obj)
    xmlXPathFreeObject(obj);
  if (xml)
    xmlFreeDoc(xml);
  if (fd != -1)
    close(fd);
  return -1;
}

static int getNextConnection(void) {
  int i;
  if (node == NULL) {
    node = calloc(1, sizeof(testNode));
    if (!node) {
608
      testError(NULL, NULL, VIR_ERR_NO_MEMORY, _("allocating node"));
609 610 611 612 613 614 615 616 617 618 619
      return -1;
    }
  }

  for (i = 0 ; i < MAX_CONNECTIONS ; i++) {
    if (!node->connections[i].active) {
      return i;
    }
  }
  return -1;
}
620 621 622 623 624 625

int testOpen(virConnectPtr conn,
             const char *name,
             int flags)
{
  xmlURIPtr uri;
626
  int ret, connid;
627 628 629 630 631 632 633 634 635 636 637 638 639 640

  if (!name) {
    return -1;
  }

  uri = xmlParseURI(name);
  if (uri == NULL) {
    if (!(flags & VIR_DRV_OPEN_QUIET))
      testError(conn, NULL, VIR_ERR_NO_SUPPORT, name);
    return(-1);
  }

  if (!uri->scheme ||
      strcmp(uri->scheme, "test") ||
641
      !uri->path) {
642 643 644 645 646
    xmlFreeURI(uri);
    return -1;
  }


647
  if ((connid = getNextConnection()) < 0) {
648
    testError(NULL, NULL, VIR_ERR_INTERNAL_ERROR, _("too many connections"));
649
    return -1;
650 651
  }

652 653 654 655 656 657 658
  if (!strcmp(uri->path, "/default")) {
    ret = testOpenDefault(conn,
			  connid);
  } else {
    ret = testOpenFromFile(conn,
			   connid,
			   uri->path);
659 660
  }

661
  xmlFreeURI(uri);
662

663
  return (ret);
664 665 666 667 668 669 670 671 672 673 674
}

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

675
int testGetVersion(virConnectPtr conn ATTRIBUTE_UNUSED,
676 677
                   unsigned long *hvVer)
{
678
  *hvVer = 2;
679 680 681
  return 0;
}

682
int testNodeGetInfo(virConnectPtr conn,
683 684
                    virNodeInfoPtr info)
{
685 686
  testCon *con = &node->connections[conn->handle];
  memcpy(info, &con->nodeInfo, sizeof(virNodeInfo));
687 688 689 690 691 692 693 694 695
  return 0;
}

int testNumOfDomains(virConnectPtr conn)
{
  testCon *con = &node->connections[conn->handle];
  return con->numDomains;
}

696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712
virDomainPtr
testDomainCreateLinux(virConnectPtr conn, const char *xmlDesc,
		      unsigned int flags ATTRIBUTE_UNUSED)
{
  testCon *con;
  int i;
  virDomainPtr dom;

  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) {
713
    testError(conn, NULL, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
714 715 716 717 718 719 720 721 722 723 724
    return (NULL);
  }
  
  con = &node->connections[conn->handle];

  for (i = 0 ; i < MAX_DOMAINS ; i++) {
    if (!con->domains[i].active) {
      if (testLoadDomainFromDoc(conn, i, xmlDesc) < 0)
	return NULL;
      dom = virGetDomain(conn, con->domains[i].name, con->domains[i].uuid);
      if (dom == NULL) {
725
	testError(conn, NULL, VIR_ERR_NO_MEMORY, _("allocating domain"));
726 727 728 729 730 731 732
	return NULL;
      }
      con->numDomains++;
      return dom;
    }
  }
  
733
  testError(NULL, NULL, VIR_ERR_INTERNAL_ERROR, _("too many domains"));
734 735 736 737
  return (NULL);
}


738 739 740 741 742
virDomainPtr testLookupDomainByID(virConnectPtr conn,
                                  int id)
{
  testCon *con = &node->connections[conn->handle];
  virDomainPtr dom;
743

744 745 746
  if (!con->domains[id].active) {
    return NULL;
  }
747

748 749
  dom = virGetDomain(conn, con->domains[id].name, con->domains[id].uuid);
  if (dom == NULL) {
750
    testError(conn, NULL, VIR_ERR_NO_MEMORY, _("allocating domain"));
751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772
    return(NULL);
  }
  dom->handle = id;
  return dom;
}

virDomainPtr testLookupDomainByUUID(virConnectPtr conn,
                                    const unsigned char *uuid)
{
  testCon *con = &node->connections[conn->handle];
  virDomainPtr dom = NULL;
  int i, id = -1;
  for (i = 0 ; i < MAX_DOMAINS ; i++) {
    if (con->domains[i].active &&
	memcmp(uuid, con->domains[i].uuid, 16) == 0) {
      id = i;
      break;
    }
  }
  if (id >= 0) {
    dom = virGetDomain(conn, con->domains[id].name, con->domains[id].uuid);
    if (dom == NULL) {
773
      testError(conn, NULL, VIR_ERR_NO_MEMORY, _("allocating domain"));
774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796
      return(NULL);
    }
    dom->handle = id;
  }
  return dom;
}

virDomainPtr testLookupDomainByName(virConnectPtr conn,
                                    const char *name)
{
  testCon *con = &node->connections[conn->handle];
  virDomainPtr dom = NULL;
  int i, id = -1;
  for (i = 0 ; i < MAX_DOMAINS ; i++) {
    if (con->domains[i].active &&
	strcmp(name, con->domains[i].name) == 0) {
      id = i;
      break;
    }
  }
  if (id >= 0) {
    dom = virGetDomain(conn, con->domains[id].name, con->domains[id].uuid);
    if (dom == NULL) {
797
      testError(conn, NULL, VIR_ERR_NO_MEMORY, _("allocating domain"));
798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821
      return(NULL);
    }
    dom->handle = id;
  }
  return dom;
}

int testListDomains (virConnectPtr conn,
                     int *ids,
                     int maxids)
{
  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) {
      ids[n++] = i;
    }
  }
  return n;
}

int testDestroyDomain (virDomainPtr domain)
{
822 823 824 825 826 827 828
  testCon *con;
  if ((domain == NULL) || (domain->conn == NULL) || (domain->name == NULL)) {
    testError((domain ? domain->conn : NULL), domain, VIR_ERR_INVALID_ARG,
	      __FUNCTION__);
    return(-1);
  }
  if (domain->conn->flags & VIR_CONNECT_RO) {
829
    testError(domain->conn, domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
830 831 832 833
    return (-1);
  }
  
  con = &node->connections[domain->conn->handle];
834
  con->domains[domain->handle].active = 0;
835
  return (0);
836 837 838 839
}

int testResumeDomain (virDomainPtr domain)
{
840 841 842 843 844 845 846
  testCon *con;
  if ((domain == NULL) || (domain->conn == NULL) || (domain->name == NULL)) {
    testError((domain ? domain->conn : NULL), domain, VIR_ERR_INVALID_ARG,
	      __FUNCTION__);
    return(-1);
  }
  if (domain->conn->flags & VIR_CONNECT_RO) {
847
    testError(domain->conn, domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
848 849 850 851
    return (-1);
  }

  con = &node->connections[domain->conn->handle];
852 853 854 855 856 857
  con->domains[domain->handle].info.state = VIR_DOMAIN_RUNNING;
  return 0;
}

int testPauseDomain (virDomainPtr domain)
{
858 859 860 861 862 863 864
  testCon *con;
  if ((domain == NULL) || (domain->conn == NULL) || (domain->name == NULL)) {
    testError((domain ? domain->conn : NULL), domain, VIR_ERR_INVALID_ARG,
	      __FUNCTION__);
    return(-1);
  }
  if (domain->conn->flags & VIR_CONNECT_RO) {
865
    testError(domain->conn, domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
866 867 868 869
    return (-1);
  }

  con = &node->connections[domain->conn->handle];
870
  con->domains[domain->handle].info.state = VIR_DOMAIN_PAUSED;
871
  return (0);
872 873
}

874 875 876 877 878 879
/* 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)
{
880
  testCon *con;
881
  struct timeval tv;
882 883 884 885 886 887
  if ((domain == NULL) || (domain->conn == NULL) || (domain->name == NULL)) {
    testError((domain ? domain->conn : NULL), domain, VIR_ERR_INVALID_ARG,
	      __FUNCTION__);
    return (-1);
  }
  if (domain->conn->flags & VIR_CONNECT_RO) {
888
    testError(domain->conn, domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
889 890 891 892 893
    return (-1);
  }

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

894
  if (gettimeofday(&tv, NULL) < 0) {
895
    testError(NULL, NULL, VIR_ERR_INTERNAL_ERROR, _("getting time of day"));
896
    return (-1);
897 898 899 900 901
  }

  con->domains[domain->handle].info.state = VIR_DOMAIN_SHUTDOWN;
  con->domains[domain->handle].onRestart = VIR_DOMAIN_DESTROY;
  con->domains[domain->handle].shutdownStartedAt = tv.tv_sec;
902
  return (0);
903 904 905 906 907
}

/* Similar behaviour as shutdown */
int testRebootDomain (virDomainPtr domain, virDomainRestart action)
{
908
  testCon *con;
909
  struct timeval tv;
910 911 912 913 914 915
  if ((domain == NULL) || (domain->conn == NULL) || (domain->name == NULL)) {
    testError((domain ? domain->conn : NULL), domain, VIR_ERR_INVALID_ARG,
	      __FUNCTION__);
    return(-1);
  }
  if (domain->conn->flags & VIR_CONNECT_RO) {
916
    testError(domain->conn, domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
917 918 919 920 921
    return (-1);
  }

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

922
  if (gettimeofday(&tv, NULL) < 0) {
923
    testError(NULL, NULL, VIR_ERR_INTERNAL_ERROR, _("getting time of day"));
924
    return (-1);
925 926 927 928 929 930 931 932
  }

  if (!action)
    action = VIR_DOMAIN_RESTART;

  con->domains[domain->handle].info.state = VIR_DOMAIN_SHUTDOWN;
  con->domains[domain->handle].onRestart = action;
  con->domains[domain->handle].shutdownStartedAt = tv.tv_sec;
933
  return (0);
934 935
}

936 937 938 939
int testGetDomainInfo (virDomainPtr domain,
                       virDomainInfoPtr info)
{
  struct timeval tv;
940 941 942 943 944 945 946 947 948
  testCon *con;
  if ((domain == NULL) || (domain->conn == NULL) || (domain->name == NULL)) {
    testError((domain ? domain->conn : NULL), domain, VIR_ERR_INVALID_ARG,
	      __FUNCTION__);
    return(-1);
  }

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

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

954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987
  /* Check to see if there is an in-progresss shutdown/reboot that
     needs to be marked completed now */
  if (con->domains[domain->handle].info.state == VIR_DOMAIN_SHUTDOWN &&
      (tv.tv_sec - con->domains[domain->handle].shutdownStartedAt) > SHUTDOWN_DURATION) {

      switch (con->domains[domain->handle].onRestart) {
      case VIR_DOMAIN_DESTROY:
	con->domains[domain->handle].info.state = VIR_DOMAIN_SHUTOFF;
	break;

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

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

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

      default:
	con->domains[domain->handle].info.state = VIR_DOMAIN_SHUTOFF;
	break;
    }
  }

  if (con->domains[domain->handle].info.state == VIR_DOMAIN_SHUTOFF) {
    con->domains[domain->handle].info.cpuTime = 0;
    con->domains[domain->handle].info.memory = 0;
  } else {
    con->domains[domain->handle].info.cpuTime = ((tv.tv_sec * 1000ll * 1000ll  * 1000ll) + (tv.tv_usec * 1000ll));
  }
988
  memcpy(info, &con->domains[domain->handle].info, sizeof(virDomainInfo));
989 990 991 992 993 994 995 996 997 998 999 1000 1001
  return (0);
}

unsigned long testGetMaxMemory(virDomainPtr domain) {
  testCon *con;
  if ((domain == NULL) || (domain->conn == NULL) || (domain->name == NULL)) {
    testError((domain ? domain->conn : NULL), domain, VIR_ERR_INVALID_ARG,
	      __FUNCTION__);
    return(-1);
  }
  
  con = &node->connections[domain->conn->handle];
  return con->domains[domain->handle].info.maxMem;
1002 1003 1004 1005 1006
}

int testSetMaxMemory (virDomainPtr domain,
                      unsigned long memory)
{
1007 1008 1009 1010 1011 1012 1013
  testCon *con;
  if ((domain == NULL) || (domain->conn == NULL) || (domain->name == NULL)) {
    testError((domain ? domain->conn : NULL), domain, VIR_ERR_INVALID_ARG,
	      __FUNCTION__);
    return(-1);
  }
  if (domain->conn->flags & VIR_CONNECT_RO) {
1014
    testError(domain->conn, domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
1015 1016 1017 1018 1019
    return (-1);
  }

  con = &node->connections[domain->conn->handle];
  /* XXX validate not over host memory wrt to other domains */
1020
  con->domains[domain->handle].info.maxMem = memory;
1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033
  return (0);
}

int testSetMemory (virDomainPtr domain,
		   unsigned long memory)
{
  testCon *con;
  if ((domain == NULL) || (domain->conn == NULL) || (domain->name == NULL)) {
    testError((domain ? domain->conn : NULL), domain, VIR_ERR_INVALID_ARG,
	      __FUNCTION__);
    return(-1);
  }
  if (domain->conn->flags & VIR_CONNECT_RO) {
1034
    testError(domain->conn, domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
1035 1036 1037 1038 1039 1040
    return (-1);
  }

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

  if (memory > con->domains[domain->handle].info.maxMem) {
1041
    testError(domain->conn, domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058
    return (-1);
  }

  con->domains[domain->handle].info.memory = memory;
  return (0);
}

int testSetVcpus(virDomainPtr domain,
		 unsigned int nrCpus) {
  testCon *con;

  if ((domain == NULL) || (domain->conn == NULL) || (domain->name == NULL)) {
    testError((domain ? domain->conn : NULL), domain, VIR_ERR_INVALID_ARG,
	      __FUNCTION__);
    return(-1);
  }
  if (domain->conn->flags & VIR_CONNECT_RO) {
1059
    testError(domain->conn, domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
1060 1061 1062 1063 1064 1065 1066
    return (-1);
  }

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

  /* We allow more cpus in guest than host */
  if (nrCpus > 32) {
1067
    testError(domain->conn, domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
1068 1069 1070 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 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113
    return (-1);
  }

  con->domains[domain->handle].info.nrVirtCpu = nrCpus;
  return (0);
}

char * testDomainDumpXML(virDomainPtr domain, int flags ATTRIBUTE_UNUSED)
{
  virBufferPtr buf;
  char *xml;
  unsigned char *uuid;
  testCon *con;
  if ((domain == NULL) || (domain->conn == NULL) || (domain->name == NULL)) {
    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);
  }
  
  virBufferVSprintf(buf, "<domain type='test' id='%d'>\n", domain->handle);
  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[domain->handle].info.maxMem);
  virBufferVSprintf(buf, "  <vcpu>%d</vcpu>\n", con->domains[domain->handle].info.nrVirtCpu);
  virBufferVSprintf(buf, "  <on_reboot>%s</on_reboot>\n", testRestartFlagToString(con->domains[domain->handle].onReboot));
  virBufferVSprintf(buf, "  <on_poweroff>%s</on_poweroff>\n", testRestartFlagToString(con->domains[domain->handle].onPoweroff));
  virBufferVSprintf(buf, "  <on_crash>%s</on_crash>\n", testRestartFlagToString(con->domains[domain->handle].onCrash));
  
  virBufferAdd(buf, "</domain>\n", -1);
  
  xml = buf->content;
  free(buf);
  return xml;
1114
}