PageRenderTime 76ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/domainsnapshotxml2xmltest.c

https://gitlab.com/unofficial-mirrors/libvirt
C | 243 lines | 179 code | 54 blank | 10 comment | 23 complexity | 510112a56e5752d22cc3460780d2deee MD5 | raw file
  1. #include <config.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. #include <string.h>
  6. #include <sys/types.h>
  7. #include <fcntl.h>
  8. #include <regex.h>
  9. #include "testutils.h"
  10. #ifdef WITH_QEMU
  11. # include "internal.h"
  12. # include "qemu/qemu_conf.h"
  13. # include "qemu/qemu_domain.h"
  14. # include "testutilsqemu.h"
  15. # include "virstring.h"
  16. # define VIR_FROM_THIS VIR_FROM_NONE
  17. static virQEMUDriver driver;
  18. /* This regex will skip the following XML constructs in test files
  19. * that are dynamically generated and thus problematic to test:
  20. * <name>1234352345</name> if the snapshot has no name,
  21. * <creationTime>23523452345</creationTime>,
  22. * <state>nostate</state> as the backend code doesn't fill this
  23. */
  24. static const char *testSnapshotXMLVariableLineRegexStr =
  25. "(<(name|creationTime)>[0-9]+</(name|creationTime)>|"
  26. "<state>nostate</state>)";
  27. regex_t *testSnapshotXMLVariableLineRegex = NULL;
  28. static char *
  29. testFilterXML(char *xml)
  30. {
  31. virBuffer buf = VIR_BUFFER_INITIALIZER;
  32. char **xmlLines = NULL;
  33. char **xmlLine;
  34. char *ret = NULL;
  35. if (!(xmlLines = virStringSplit(xml, "\n", 0))) {
  36. VIR_FREE(xml);
  37. goto cleanup;
  38. }
  39. VIR_FREE(xml);
  40. for (xmlLine = xmlLines; *xmlLine; xmlLine++) {
  41. if (regexec(testSnapshotXMLVariableLineRegex,
  42. *xmlLine, 0, NULL, 0) == 0)
  43. continue;
  44. virBufferStrcat(&buf, *xmlLine, "\n", NULL);
  45. }
  46. if (virBufferCheckError(&buf) < 0)
  47. goto cleanup;
  48. ret = virBufferContentAndReset(&buf);
  49. cleanup:
  50. virBufferFreeAndReset(&buf);
  51. virStringFreeList(xmlLines);
  52. return ret;
  53. }
  54. static int
  55. testCompareXMLToXMLFiles(const char *inxml,
  56. const char *outxml,
  57. const char *uuid,
  58. bool internal,
  59. bool redefine)
  60. {
  61. char *inXmlData = NULL;
  62. char *outXmlData = NULL;
  63. char *actual = NULL;
  64. int ret = -1;
  65. virDomainSnapshotDefPtr def = NULL;
  66. unsigned int flags = VIR_DOMAIN_SNAPSHOT_PARSE_DISKS;
  67. if (internal)
  68. flags |= VIR_DOMAIN_SNAPSHOT_PARSE_INTERNAL;
  69. if (redefine)
  70. flags |= VIR_DOMAIN_SNAPSHOT_PARSE_REDEFINE;
  71. if (virTestLoadFile(inxml, &inXmlData) < 0)
  72. goto cleanup;
  73. if (virTestLoadFile(outxml, &outXmlData) < 0)
  74. goto cleanup;
  75. if (!(def = virDomainSnapshotDefParseString(inXmlData, driver.caps,
  76. driver.xmlopt,
  77. flags)))
  78. goto cleanup;
  79. if (!(actual = virDomainSnapshotDefFormat(uuid, def, driver.caps,
  80. VIR_DOMAIN_DEF_FORMAT_SECURE,
  81. internal)))
  82. goto cleanup;
  83. if (!redefine) {
  84. if (!(actual = testFilterXML(actual)))
  85. goto cleanup;
  86. if (!(outXmlData = testFilterXML(outXmlData)))
  87. goto cleanup;
  88. }
  89. if (STRNEQ(outXmlData, actual)) {
  90. virTestDifferenceFull(stderr, outXmlData, outxml, actual, inxml);
  91. goto cleanup;
  92. }
  93. ret = 0;
  94. cleanup:
  95. VIR_FREE(inXmlData);
  96. VIR_FREE(outXmlData);
  97. VIR_FREE(actual);
  98. virDomainSnapshotDefFree(def);
  99. return ret;
  100. }
  101. struct testInfo {
  102. const char *inxml;
  103. const char *outxml;
  104. const char *uuid;
  105. bool internal;
  106. bool redefine;
  107. };
  108. static int
  109. testCompareXMLToXMLHelper(const void *data)
  110. {
  111. const struct testInfo *info = data;
  112. return testCompareXMLToXMLFiles(info->inxml, info->outxml, info->uuid,
  113. info->internal, info->redefine);
  114. }
  115. static int
  116. mymain(void)
  117. {
  118. int ret = 0;
  119. if (qemuTestDriverInit(&driver) < 0)
  120. return EXIT_FAILURE;
  121. /* TODO: test with format probing disabled too */
  122. driver.config->allowDiskFormatProbing = true;
  123. if (VIR_ALLOC(testSnapshotXMLVariableLineRegex) < 0)
  124. goto cleanup;
  125. if (regcomp(testSnapshotXMLVariableLineRegex,
  126. testSnapshotXMLVariableLineRegexStr,
  127. REG_EXTENDED | REG_NOSUB) != 0) {
  128. virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
  129. "failed to compile test regex");
  130. goto cleanup;
  131. }
  132. # define DO_TEST(prefix, name, inpath, outpath, uuid, internal, redefine) \
  133. do { \
  134. const struct testInfo info = {abs_srcdir "/" inpath "/" name ".xml", \
  135. abs_srcdir "/" outpath "/" name ".xml", \
  136. uuid, internal, redefine}; \
  137. if (virTestRun("SNAPSHOT XML-2-XML " prefix " " name, \
  138. testCompareXMLToXMLHelper, &info) < 0) \
  139. ret = -1; \
  140. } while (0)
  141. # define DO_TEST_IN(name, uuid) DO_TEST("in->in", name,\
  142. "domainsnapshotxml2xmlin",\
  143. "domainsnapshotxml2xmlin",\
  144. uuid, false, false)
  145. # define DO_TEST_OUT(name, uuid, internal) DO_TEST("out->out", name,\
  146. "domainsnapshotxml2xmlout",\
  147. "domainsnapshotxml2xmlout",\
  148. uuid, internal, true)
  149. # define DO_TEST_INOUT(name, uuid, internal, redefine) \
  150. DO_TEST("in->out", name,\
  151. "domainsnapshotxml2xmlin",\
  152. "domainsnapshotxml2xmlout",\
  153. uuid, internal, redefine)
  154. /* Unset or set all envvars here that are copied in qemudBuildCommandLine
  155. * using ADD_ENV_COPY, otherwise these tests may fail due to unexpected
  156. * values for these envvars */
  157. setenv("PATH", "/bin", 1);
  158. DO_TEST_OUT("all_parameters", "9d37b878-a7cc-9f9a-b78f-49b3abad25a8", true);
  159. DO_TEST_OUT("disk_snapshot_redefine", "c7a5fdbd-edaf-9455-926a-d65c16db1809", true);
  160. DO_TEST_OUT("full_domain", "c7a5fdbd-edaf-9455-926a-d65c16db1809", true);
  161. DO_TEST_OUT("noparent_nodescription_noactive", NULL, false);
  162. DO_TEST_OUT("noparent_nodescription", NULL, true);
  163. DO_TEST_OUT("noparent", "9d37b878-a7cc-9f9a-b78f-49b3abad25a8", false);
  164. DO_TEST_OUT("metadata", "c7a5fdbd-edaf-9455-926a-d65c16db1809", false);
  165. DO_TEST_OUT("external_vm_redefine", "c7a5fdbd-edaf-9455-926a-d65c16db1809", false);
  166. DO_TEST_INOUT("empty", "9d37b878-a7cc-9f9a-b78f-49b3abad25a8", false, false);
  167. DO_TEST_INOUT("noparent", "9d37b878-a7cc-9f9a-b78f-49b3abad25a8", false, false);
  168. DO_TEST_INOUT("external_vm", NULL, false, false);
  169. DO_TEST_INOUT("noparent", "9d37b878-a7cc-9f9a-b78f-49b3abad25a8", false, false);
  170. DO_TEST_INOUT("disk_snapshot", NULL, false, false);
  171. DO_TEST_INOUT("disk_driver_name_null", NULL, false, false);
  172. DO_TEST_IN("name_and_description", NULL);
  173. DO_TEST_IN("description_only", NULL);
  174. DO_TEST_IN("name_only", NULL);
  175. cleanup:
  176. if (testSnapshotXMLVariableLineRegex)
  177. regfree(testSnapshotXMLVariableLineRegex);
  178. VIR_FREE(testSnapshotXMLVariableLineRegex);
  179. qemuTestDriverFree(&driver);
  180. return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
  181. }
  182. VIRT_TEST_MAIN(mymain)
  183. #else
  184. int
  185. main(void)
  186. {
  187. return EXIT_AM_SKIP;
  188. }
  189. #endif /* WITH_QEMU */