]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
tests: Use virTestCompareToString() more
authorMichal Privoznik <mprivozn@redhat.com>
Wed, 30 Nov 2022 08:57:49 +0000 (09:57 +0100)
committerMichal Privoznik <mprivozn@redhat.com>
Thu, 1 Dec 2022 13:22:39 +0000 (14:22 +0100)
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>
15 files changed:
tests/commandtest.c
tests/esxutilstest.c
tests/nwfilterebiptablestest.c
tests/openvzutilstest.c
tests/sockettest.c
tests/utiltest.c
tests/vboxsnapshotxmltest.c
tests/virbuftest.c
tests/virfirewalltest.c
tests/virjsontest.c
tests/virkmodtest.c
tests/virnetdevbandwidthtest.c
tests/virnetdevopenvswitchtest.c
tests/virnetsockettest.c
tests/virshtest.c

index ffc4b24ef422bfee54f966921d1f4266a10b513e..62275ba96d16d0857d418d8e1c5aa2493bd29f7d 100644 (file)
@@ -436,8 +436,7 @@ static int test13(const void *unused G_GNUC_UNUSED)
 
     g_clear_pointer(&cmd, virCommandFree);
 
-    if (STRNEQ(outactual, outexpect)) {
-        virTestDifference(stderr, outexpect, outactual);
+    if (virTestCompareToString(outexpect, outactual) < 0) {
         goto cleanup;
     }
 
@@ -497,16 +496,13 @@ static int test14(const void *unused G_GNUC_UNUSED)
     if (!jointactual)
         goto cleanup;
 
-    if (STRNEQ(outactual, outexpect)) {
-        virTestDifference(stderr, outexpect, outactual);
+    if (virTestCompareToString(outexpect, outactual) < 0) {
         goto cleanup;
     }
-    if (STRNEQ(erractual, errexpect)) {
-        virTestDifference(stderr, errexpect, erractual);
+    if (virTestCompareToString(errexpect, erractual) < 0) {
         goto cleanup;
     }
-    if (STRNEQ(jointactual, jointexpect)) {
-        virTestDifference(stderr, jointexpect, jointactual);
+    if (virTestCompareToString(jointexpect, jointactual) < 0) {
         goto cleanup;
     }
 
@@ -569,8 +565,7 @@ static int test16(const void *unused G_GNUC_UNUSED)
         return -1;
     }
 
-    if (STRNEQ(outactual, outexpect)) {
-        virTestDifference(stderr, outexpect, outactual);
+    if (virTestCompareToString(outexpect, outactual) < 0) {
         return -1;
     }
 
@@ -774,13 +769,11 @@ static int test21(const void *unused G_GNUC_UNUSED)
     if (virTestGetVerbose())
         printf("STDOUT:%s\nSTDERR:%s\n", NULLSTR(outbuf), NULLSTR(errbuf));
 
-    if (STRNEQ_NULLABLE(outbuf, outbufExpected)) {
-        virTestDifference(stderr, outbufExpected, outbuf);
+    if (virTestCompareToString(outbufExpected, outbuf) < 0) {
         return -1;
     }
 
-    if (STRNEQ_NULLABLE(errbuf, errbufExpected)) {
-        virTestDifference(stderr, errbufExpected, errbuf);
+    if (virTestCompareToString(errbufExpected, errbuf) < 0) {
         return -1;
     }
 
@@ -1016,8 +1009,7 @@ static int test26(const void *unused G_GNUC_UNUSED)
         return -1;
     }
 
-    if (STRNEQ(outactual, outexpect)) {
-        virTestDifference(stderr, outexpect, outactual);
+    if (virTestCompareToString(outexpect, outactual) < 0) {
         return -1;
     }
 
@@ -1086,12 +1078,10 @@ static int test27(const void *unused G_GNUC_UNUSED)
     if (!outactual || !erractual)
         return -1;
 
-    if (STRNEQ(outactual, outexpect)) {
-        virTestDifference(stderr, outexpect, outactual);
+    if (virTestCompareToString(outexpect, outactual) < 0) {
         return -1;
     }
-    if (STRNEQ(erractual, errexpect)) {
-        virTestDifference(stderr, errexpect, erractual);
+    if (virTestCompareToString(errexpect, erractual) < 0) {
         return -1;
     }
 
@@ -1225,8 +1215,7 @@ test29(const void *unused G_GNUC_UNUSED)
         }
     }
 
-    if (STRNEQ_NULLABLE(outactual, outexpect)) {
-        virTestDifference(stderr, outexpect, outactual);
+    if (virTestCompareToString(outexpect, outactual) < 0) {
         goto cleanup;
     }
 
index 4591cf49dbc4b224f19aaf264157fe3a14b38056..5cebc10e441b08f8bef47458eb532d99ca4cf45f 100644 (file)
@@ -50,19 +50,15 @@ testParseDatastorePath(const void *data G_GNUC_UNUSED)
         if (paths[i].result < 0)
             continue;
 
-        if (STRNEQ(paths[i].datastoreName, datastoreName)) {
-            virTestDifference(stderr, paths[i].datastoreName, datastoreName);
+        if (virTestCompareToString(paths[i].datastoreName, datastoreName) < 0) {
             return -1;
         }
 
-        if (STRNEQ(paths[i].directoryName, directoryName)) {
-            virTestDifference(stderr, paths[i].directoryName, directoryName);
+        if (virTestCompareToString(paths[i].directoryName, directoryName) < 0) {
             return -1;
         }
 
-        if (STRNEQ(paths[i].directoryAndFileName, directoryAndFileName)) {
-            virTestDifference(stderr, paths[i].directoryAndFileName,
-                              directoryAndFileName);
+        if (virTestCompareToString(paths[i].directoryAndFileName, directoryAndFileName) < 0) {
             return -1;
         }
     }
index f76f13fd25e0f69181a59baa3253acbedae49b7f..fe0d8e869cf42ddfbe9a9ff76f9664799872873a 100644 (file)
@@ -107,8 +107,7 @@ testNWFilterEBIPTablesAllTeardown(const void *opaque G_GNUC_UNUSED)
 
     actual = virBufferContentAndReset(&buf);
 
-    if (STRNEQ_NULLABLE(actual, expected)) {
-        virTestDifference(stderr, expected, actual);
+    if (virTestCompareToString(actual, expected) < 0) {
         return -1;
     }
 
@@ -169,8 +168,7 @@ testNWFilterEBIPTablesTearOldRules(const void *opaque G_GNUC_UNUSED)
 
     actual = virBufferContentAndReset(&buf);
 
-    if (STRNEQ_NULLABLE(actual, expected)) {
-        virTestDifference(stderr, expected, actual);
+    if (virTestCompareToString(expected, actual) < 0) {
         return -1;
     }
 
@@ -209,8 +207,7 @@ testNWFilterEBIPTablesRemoveBasicRules(const void *opaque G_GNUC_UNUSED)
 
     actual = virBufferContentAndReset(&buf);
 
-    if (STRNEQ_NULLABLE(actual, expected)) {
-        virTestDifference(stderr, expected, actual);
+    if (virTestCompareToString(expected, actual) < 0) {
         return -1;
     }
 
@@ -234,8 +231,7 @@ testNWFilterEBIPTablesTearNewRules(const void *opaque G_GNUC_UNUSED)
 
     actual = virBufferContentAndReset(&buf);
 
-    if (STRNEQ_NULLABLE(actual, expected)) {
-        virTestDifference(stderr, expected, actual);
+    if (virTestCompareToString(expected, actual) < 0) {
         return -1;
     }
 
@@ -297,8 +293,7 @@ testNWFilterEBIPTablesApplyBasicRules(const void *opaque G_GNUC_UNUSED)
 
     actual = virBufferContentAndReset(&buf);
 
-    if (STRNEQ_NULLABLE(actual, expected)) {
-        virTestDifference(stderr, expected, actual);
+    if (virTestCompareToString(expected, actual) < 0) {
         return -1;
     }
 
@@ -378,8 +373,7 @@ testNWFilterEBIPTablesApplyDHCPOnlyRules(const void *opaque G_GNUC_UNUSED)
 
     actual = virBufferContentAndReset(&buf);
 
-    if (STRNEQ_NULLABLE(actual, expected)) {
-        virTestDifference(stderr, expected, actual);
+    if (virTestCompareToString(expected, actual) < 0) {
         return -1;
     }
 
@@ -442,8 +436,7 @@ testNWFilterEBIPTablesApplyDropAllRules(const void *opaque G_GNUC_UNUSED)
 
     actual = virBufferContentAndReset(&buf);
 
-    if (STRNEQ_NULLABLE(actual, expected)) {
-        virTestDifference(stderr, expected, actual);
+    if (virTestCompareToString(expected, actual) < 0) {
         return -1;
     }
 
index 77e086e3902cb74364a5c9018129f9cf9f13ef78..6ebfb0449fa04a2f5cb5477237e3903841f58766 100644 (file)
@@ -49,8 +49,7 @@ testReadConfigParam(const void *data G_GNUC_UNUSED)
         if (configParams[i].ret != 1)
             continue;
 
-        if (STRNEQ(configParams[i].value, value)) {
-            virTestDifference(stderr, configParams[i].value, value);
+        if (virTestCompareToString(configParams[i].value, value) < 0) {
             return -1;
         }
     }
@@ -114,8 +113,7 @@ testReadNetworkConf(const void *data G_GNUC_UNUSED)
         goto cleanup;
     }
 
-    if (STRNEQ(expected, actual)) {
-        virTestDifference(stderr, expected, actual);
+    if (virTestCompareToString(expected, actual) < 0) {
         goto cleanup;
     }
 
index f2b75a2e5334164766e7b2e1e2361b1239449eb2..6b9063e11d969014a428cd3de3f81b39c5a40dec 100644 (file)
@@ -48,8 +48,7 @@ static int testFormat(virSocketAddr *addr, const char *addrstr, bool pass)
     if (!newaddrstr)
         return pass ? -1 : 0;
 
-    if (STRNEQ(newaddrstr, addrstr)) {
-        virTestDifference(stderr, addrstr, newaddrstr);
+    if (virTestCompareToString(newaddrstr, addrstr) < 0) {
         return pass ? -1 : 0;
     } else {
         return pass ? 0 : -1;
index e7a79f808f197df10c01bbb3e7e0a1990b39567a..e90baca65fd6c96683dcc476b42f8c184684b4d0 100644 (file)
@@ -47,8 +47,7 @@ testIndexToDiskName(const void *data G_GNUC_UNUSED)
 
         diskName = virIndexToDiskName(i, "sd");
 
-        if (STRNEQ(diskNames[i], diskName)) {
-            virTestDifference(stderr, diskNames[i], diskName);
+        if (virTestCompareToString(diskNames[i], diskName) < 0) {
             return -1;
         }
     }
index 3ad829889571d34dca27f41e8eab4d45723596b2..ab65999df16d003e2706deeeb27853cdf2672f90 100644 (file)
@@ -69,8 +69,7 @@ testCompareXMLtoXMLFiles(const char *xml)
     if (!(xmlData = testFilterXML(xmlData)))
         goto cleanup;
 
-    if (STRNEQ(actual, xmlData)) {
-        virTestDifference(stderr, xmlData, actual);
+    if (virTestCompareToString(xmlData, actual) < 0) {
         goto cleanup;
     }
 
index 144df6e66bda2d9c78deb2a88f8f85f05b2c3ec8..6b810381fb747a19d33d35369501dc24aa1f3502 100644 (file)
@@ -92,8 +92,7 @@ static int testBufAutoIndent(const void *data G_GNUC_UNUSED)
     virBufferAddChar(buf, '\n');
 
     result = virBufferContentAndReset(buf);
-    if (STRNEQ_NULLABLE(result, expected)) {
-        virTestDifference(stderr, expected, result);
+    if (virTestCompareToString(expected, result) < 0) {
         ret = -1;
     }
     return ret;
@@ -122,8 +121,7 @@ static int testBufTrim(const void *data G_GNUC_UNUSED)
     virBufferTrim(buf, ",,");
 
     result = virBufferContentAndReset(buf);
-    if (STRNEQ_NULLABLE(result, expected)) {
-        virTestDifference(stderr, expected, result);
+    if (virTestCompareToString(expected, result) < 0) {
         return -1;
     }
 
@@ -145,8 +143,7 @@ testBufTrimChars(const void *opaque)
         return -1;
     }
 
-    if (STRNEQ_NULLABLE(actual, data->expect)) {
-        virTestDifference(stderr, data->expect, actual);
+    if (virTestCompareToString(data->expect, actual) < 0) {
         return -1;
     }
 
@@ -250,8 +247,7 @@ static int testBufAddBuffer(const void *data G_GNUC_UNUSED)
     }
 
     result = virBufferContentAndReset(&buf1);
-    if (STRNEQ_NULLABLE(result, expected)) {
-        virTestDifference(stderr, expected, result);
+    if (virTestCompareToString(expected, result) < 0) {
         return -1;
     }
 
@@ -276,8 +272,7 @@ testBufAddStr(const void *opaque)
         return -1;
     }
 
-    if (STRNEQ_NULLABLE(actual, data->expect)) {
-        virTestDifference(stderr, data->expect, actual);
+    if (virTestCompareToString(data->expect, actual) < 0) {
         return -1;
     }
 
@@ -303,8 +298,7 @@ testBufEscapeStr(const void *opaque)
         return -1;
     }
 
-    if (STRNEQ_NULLABLE(actual, data->expect)) {
-        virTestDifference(stderr, data->expect, actual);
+    if (virTestCompareToString(data->expect, actual) < 0) {
         return -1;
     }
 
@@ -326,8 +320,7 @@ testBufEscapeRegex(const void *opaque)
         return -1;
     }
 
-    if (STRNEQ_NULLABLE(actual, data->expect)) {
-        virTestDifference(stderr, data->expect, actual);
+    if (virTestCompareToString(data->expect, actual) < 0) {
         return -1;
     }
 
index e3d15fb67bc1f4a57c7464a9d47bd5434f050b39..51c800633145585724ac4764b87deb1a6433681b 100644 (file)
@@ -89,9 +89,8 @@ testFirewallSingleGroup(const void *opaque G_GNUC_UNUSED)
 
     actual = virBufferCurrentContent(&cmdbuf);
 
-    if (STRNEQ_NULLABLE(expected, actual)) {
+    if (virTestCompareToString(expected, actual) < 0) {
         fprintf(stderr, "Unexpected command execution\n");
-        virTestDifference(stderr, expected, actual);
         return -1;
     }
 
@@ -136,9 +135,8 @@ testFirewallRemoveRule(const void *opaque G_GNUC_UNUSED)
 
     actual = virBufferCurrentContent(&cmdbuf);
 
-    if (STRNEQ_NULLABLE(expected, actual)) {
+    if (virTestCompareToString(expected, actual) < 0) {
         fprintf(stderr, "Unexpected command execution\n");
-        virTestDifference(stderr, expected, actual);
         return -1;
     }
 
@@ -190,9 +188,8 @@ testFirewallManyGroups(const void *opaque G_GNUC_UNUSED)
 
     actual = virBufferCurrentContent(&cmdbuf);
 
-    if (STRNEQ_NULLABLE(expected, actual)) {
+    if (virTestCompareToString(expected, actual) < 0) {
         fprintf(stderr, "Unexpected command execution\n");
-        virTestDifference(stderr, expected, actual);
         return -1;
     }
 
@@ -265,9 +262,8 @@ testFirewallIgnoreFailGroup(const void *opaque G_GNUC_UNUSED)
 
     actual = virBufferCurrentContent(&cmdbuf);
 
-    if (STRNEQ_NULLABLE(expected, actual)) {
+    if (virTestCompareToString(expected, actual) < 0) {
         fprintf(stderr, "Unexpected command execution\n");
-        virTestDifference(stderr, expected, actual);
         return -1;
     }
 
@@ -318,9 +314,8 @@ testFirewallIgnoreFailRule(const void *opaque G_GNUC_UNUSED)
 
     actual = virBufferCurrentContent(&cmdbuf);
 
-    if (STRNEQ_NULLABLE(expected, actual)) {
+    if (virTestCompareToString(expected, actual) < 0) {
         fprintf(stderr, "Unexpected command execution\n");
-        virTestDifference(stderr, expected, actual);
         return -1;
     }
 
@@ -365,9 +360,8 @@ testFirewallNoRollback(const void *opaque G_GNUC_UNUSED)
 
     actual = virBufferCurrentContent(&cmdbuf);
 
-    if (STRNEQ_NULLABLE(expected, actual)) {
+    if (virTestCompareToString(expected, actual) < 0) {
         fprintf(stderr, "Unexpected command execution\n");
-        virTestDifference(stderr, expected, actual);
         return -1;
     }
 
@@ -431,9 +425,8 @@ testFirewallSingleRollback(const void *opaque G_GNUC_UNUSED)
 
     actual = virBufferCurrentContent(&cmdbuf);
 
-    if (STRNEQ_NULLABLE(expected, actual)) {
+    if (virTestCompareToString(expected, actual) < 0) {
         fprintf(stderr, "Unexpected command execution\n");
-        virTestDifference(stderr, expected, actual);
         return -1;
     }
 
@@ -500,9 +493,8 @@ testFirewallManyRollback(const void *opaque G_GNUC_UNUSED)
 
     actual = virBufferCurrentContent(&cmdbuf);
 
-    if (STRNEQ_NULLABLE(expected, actual)) {
+    if (virTestCompareToString(expected, actual) < 0) {
         fprintf(stderr, "Unexpected command execution\n");
-        virTestDifference(stderr, expected, actual);
         return -1;
     }
 
@@ -599,9 +591,8 @@ testFirewallChainedRollback(const void *opaque G_GNUC_UNUSED)
 
     actual = virBufferCurrentContent(&cmdbuf);
 
-    if (STRNEQ_NULLABLE(expected, actual)) {
+    if (virTestCompareToString(expected, actual) < 0) {
         fprintf(stderr, "Unexpected command execution\n");
-        virTestDifference(stderr, expected, actual);
         return -1;
     }
 
@@ -763,9 +754,8 @@ testFirewallQuery(const void *opaque G_GNUC_UNUSED)
         return -1;
     }
 
-    if (STRNEQ_NULLABLE(expected, actual)) {
+    if (virTestCompareToString(expected, actual) < 0) {
         fprintf(stderr, "Unexpected command execution\n");
-        virTestDifference(stderr, expected, actual);
         return -1;
     }
 
index 78283b632abea1727d319ed9eb9946a9e3b610cc..294889a795640a528b58e9c35f5f8ec728fd6529 100644 (file)
@@ -93,8 +93,7 @@ testJSONFromString(const void *data)
         return -1;
     }
 
-    if (STRNEQ(expectstr, formatted)) {
-        virTestDifference(stderr, expectstr, formatted);
+    if (virTestCompareToString(expectstr, formatted) < 0) {
         return -1;
     }
 
@@ -424,8 +423,7 @@ testJSONEscapeObj(const void *data G_GNUC_UNUSED)
         return -1;
     }
 
-    if (STRNEQ(parsednestedstr, neststr)) {
-        virTestDifference(stderr, neststr, parsednestedstr);
+    if (virTestCompareToString(neststr, parsednestedstr) < 0) {
         return -1;
     }
 
index 0e662878b2f2143439ce2b5a9f40bcb6b34a3422..ec28ef12826be505964278870d6a5fee35fcfc71 100644 (file)
@@ -40,8 +40,7 @@ checkOutput(virBuffer *buf, const char *exp_cmd)
         return -1;
     }
 
-    if (STRNEQ(exp_cmd, actual_cmd)) {
-        virTestDifference(stderr, exp_cmd, actual_cmd);
+    if (virTestCompareToString(exp_cmd, actual_cmd) < 0) {
         return -1;
     }
 
index fced6578116c3cbe9a750c806d006f99ce5f4b4c..f7c38faa2e0968d75737471ef97fff4f9f916878 100644 (file)
@@ -92,8 +92,7 @@ testVirNetDevBandwidthSet(const void *data)
          * Maybe that's expected, actually. */
     }
 
-    if (STRNEQ_NULLABLE(exp_cmd, actual_cmd)) {
-        virTestDifference(stderr, exp_cmd, actual_cmd);
+    if (virTestCompareToString(exp_cmd, actual_cmd) < 0) {
         return -1;
     }
 
index e5883eb07608d9ddd9e717df4bdfcae1e1c83126..6e93f5e65af94c528c1a2627795f09bcb85b3508 100644 (file)
@@ -175,8 +175,7 @@ testVirNetDevOpenvswitchInterfaceSetQos(const void *data)
          * Maybe that's expected, actually. */
     }
 
-    if (STRNEQ_NULLABLE(info->exp_cmd, actual_cmd)) {
-        virTestDifference(stderr, info->exp_cmd, actual_cmd);
+    if (virTestCompareToString(info->exp_cmd, actual_cmd) < 0) {
         return -1;
     }
 
@@ -204,8 +203,7 @@ testVirNetDevOpenvswitchInterfaceClearQos(const void *data)
          * Maybe that's expected, actually. */
     }
 
-    if (STRNEQ_NULLABLE(info->exp_cmd, actual_cmd)) {
-        virTestDifference(stderr, info->exp_cmd, actual_cmd);
+    if (virTestCompareToString(info->exp_cmd, actual_cmd) < 0) {
         return -1;
     }
 
index 396005899a29c7c1eaf22665d1eb862f76667f0a..2295e2977776a382d595bc4a40d7791245f0cfc6 100644 (file)
@@ -488,8 +488,7 @@ static int testSocketSSH(const void *opaque)
         }
         buf[rv] = '\0';
 
-        if (STRNEQ(buf, data->expectOut)) {
-            virTestDifference(stderr, data->expectOut, buf);
+        if (virTestCompareToString(data->expectOut, buf) < 0) {
             goto cleanup;
         }
 
index 3d297a1db2818ecbe07351216f328b9ccdd54fda..cf834bb847245c304c33cdc8a4b63c3c04312bf4 100644 (file)
@@ -134,8 +134,7 @@ testCompareOutputLit(const char *expectData,
     if (filter && testFilterLine(actualData, filter) < 0)
         return -1;
 
-    if (STRNEQ(expectData, actualData)) {
-        virTestDifference(stderr, expectData, actualData);
+    if (virTestCompareToString(expectData, actualData) < 0) {
         return -1;
     }