1
0
mirror of https://gitlab.com/libvirt/libvirt.git synced 2024-12-25 01:34:11 +03:00
libvirt/tests/vboxsnapshotxmltest.c
Michal Privoznik a156b69c35 tests: Use virTestCompareToString() more
Instead of using:

  if (STRNEQ(a, b)) {
      virTestDifference(stderr, a, b);
      ...
  }

we can use:

  if (virTestCompareToString(a, b) < ) {
      ...
  }

Generated by the following spatch:

  @@
  expression a, b;
  @@

  - if (STRNEQ(a, b)) {
  + if (virTestCompareToString(a, b) < 0) {
      ...
  -   virTestDifference(stderr, a, b);
      ...
      }

and its variations (STRNEQ_NULLABLE() instead of STRNEQ(), then
in some cases variables passed to STRNEQ() are in reversed order
when compared to virTestCompareToString()).

However, coccinelle failed to recognize the pattern in
testNWFilterEBIPTablesAllTeardown() so I had to fix it manually.
Also, I manually fixed testFormat() in tests/sockettest.c as I
didn't bother writing another spatch rule just for that.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Jonathon Jongsma <jjongsma@redhat.com>
2022-12-01 14:22:39 +01:00

143 lines
3.2 KiB
C

#include <config.h>
#include <unistd.h>
#include "testutils.h"
#ifdef WITH_VBOX
# include "vbox/vbox_snapshot_conf.h"
# define VIR_FROM_THIS VIR_FROM_NONE
static const char *testSnapshotXMLVariableLineRegexStr =
"lastStateChange=[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}Z";
GRegex *testSnapshotXMLVariableLineRegex = NULL;
static char *
testFilterXML(char *xml)
{
g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER;
g_auto(GStrv) xmlLines = NULL;
char **xmlLine;
if (!(xmlLines = g_strsplit(xml, "\n", 0))) {
VIR_FREE(xml);
return NULL;
}
VIR_FREE(xml);
for (xmlLine = xmlLines; *xmlLine; xmlLine++) {
if (g_regex_match(testSnapshotXMLVariableLineRegex, *xmlLine, 0, NULL))
continue;
virBufferStrcat(&buf, *xmlLine, "\n", NULL);
}
return virBufferContentAndReset(&buf);
}
static int
testCompareXMLtoXMLFiles(const char *xml)
{
g_autofree char *xmlData = NULL;
g_autofree char *actual = NULL;
g_autofree char *pathResult = NULL;
int ret = -1;
virVBoxSnapshotConfMachine *machine = NULL;
pathResult = g_strdup(abs_builddir "/vboxsnapshotxmldata/testResult.vbox");
if (g_mkdir_with_parents(abs_builddir "/vboxsnapshotxmldata", 0777) < 0)
goto cleanup;
if (virTestLoadFile(xml, &xmlData) < 0)
goto cleanup;
if (!(machine = virVBoxSnapshotConfLoadVboxFile(xml, (char*)"")))
goto cleanup;
if (virVBoxSnapshotConfSaveVboxFile(machine, pathResult) < 0)
goto cleanup;
if (virTestLoadFile(pathResult, &actual) < 0)
goto cleanup;
if (!(actual = testFilterXML(actual)))
goto cleanup;
if (!(xmlData = testFilterXML(xmlData)))
goto cleanup;
if (virTestCompareToString(xmlData, actual) < 0) {
goto cleanup;
}
ret = 0;
cleanup:
unlink(pathResult);
rmdir(abs_builddir "/vboxsnapshotxmldata");
virVBoxSnapshotConfMachineFree(machine);
return ret;
}
static int
testCompareXMLToXMLHelper(const void *data)
{
int result = -1;
g_autofree char *xml = NULL;
xml = g_strdup_printf("%s/vboxsnapshotxmldata/%s.vbox", abs_srcdir,
(const char *)data);
result = testCompareXMLtoXMLFiles(xml);
return result;
}
static int
mymain(void)
{
int ret = 0;
g_autoptr(GError) err = NULL;
testSnapshotXMLVariableLineRegex = g_regex_new(testSnapshotXMLVariableLineRegexStr,
0, 0, &err);
if (!testSnapshotXMLVariableLineRegex) {
ret = -1;
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
"failed to compile test regex");
goto cleanup;
}
# define DO_TEST(name) \
if (virTestRun("VBox Snapshot XML-2-XML " name, \
testCompareXMLToXMLHelper, (name)) < 0) \
ret = -1
DO_TEST("2disks-nosnap");
DO_TEST("2disks-1snap");
DO_TEST("2disks-2snap");
DO_TEST("2disks-3snap");
DO_TEST("2disks-3snap-brother");
cleanup:
if (testSnapshotXMLVariableLineRegex)
g_regex_unref(testSnapshotXMLVariableLineRegex);
return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
}
VIR_TEST_MAIN(mymain)
#else
int main(void)
{
return EXIT_AM_SKIP;
}
#endif /* WITH_VBOX */