]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
tests: dynamically replace dnsmasq path
authorPhilipp Hahn <hahn@univention.de>
Mon, 30 Jan 2012 17:13:08 +0000 (18:13 +0100)
committerEric Blake <eblake@redhat.com>
Thu, 2 Feb 2012 00:02:45 +0000 (17:02 -0700)
The path to the dnsmasq binary can be configured while in the test data
the path is hard-coded to /usr/bin/. This break the test suite if a the
binary is located in a different location, like /usr/local/sbin/.

Replace the hard coded path in the test data by a token, which is
dynamically replaced in networkxml2argvtest with the configured path
after the test data has been loaded.

(Another option would have been to modify configure.ac to generate the
 test data during configure, but I do not know of an easy way do trick
 configure into mass-generate those test files without listing every
 single one, which I consider less flexible.)

- unit-test the unit-test:
  #include <assert.h>
  #define TEST(in,token,rep,out) { char *buf = strdup(in); assert(!replaceTokens(&buf, token, rep) && !strcmp(buf, out)); free(buf); }
  TEST("", "AA", "B", "");
  TEST("A", "AA", "B", "A");
  TEST("AA", "AA", "B", "B");
  TEST("AAA", "AA", "B", "BA");
  TEST("AA", "AA", "BB", "BB");
  TEST("AA", "AA", "BBB", "BBB");
  TEST("<AA", "AA", "B", "<B");
  TEST("<AA", "AA", "BB", "<BB");
  TEST("<AA", "AA", "BBB", "<BBB");
  TEST("AA>", "AA", "B", "B>");
  TEST("AA>", "AA", "BB", "BB>");
  TEST("AA>", "AA", "BBB", "BBB>");
  TEST("<AA>", "AA", "B", "<B>");
  TEST("<AA>", "AA", "BB", "<BB>");
  TEST("<AA>", "AA", "BBB", "<BBB>");
  TEST("<AA|AA>", "AA", "B", "<B|B>");
  TEST("<AA|AA>", "AA", "BB", "<BB|BB>");
  TEST("<AA|AA>", "AA", "BBB", "<BBB|BBB>");
  TEST("<AAAA>", "AA", "B", "<BB>");
  TEST("<AAAA>", "AA", "BB", "<BBBB>");
  TEST("<AAAA>", "AA", "BBB", "<BBBBBB>");
  TEST("AAAA>", "AA", "B", "BB>");
  TEST("AAAA>", "AA", "BB", "BBBB>");
  TEST("AAAA>", "AA", "BBB", "BBBBBB>");
  TEST("<AAAA", "AA", "B", "<BB");
  TEST("<AAAA", "AA", "BB", "<BBBB");
  TEST("<AAAA", "AA", "BBB", "<BBBBBB");
  alarm(1); /* no infinite loop */
  TEST("A", "A", "A", "A");
  TEST("AA", "A", "A", "AA");
  alarm(0);

Signed-off-by: Philipp Hahn <hahn@univention.de>
tests/networkxml2argvdata/isolated-network.argv
tests/networkxml2argvdata/nat-network-dns-hosts.argv
tests/networkxml2argvdata/nat-network-dns-srv-record-minimal.argv
tests/networkxml2argvdata/nat-network-dns-srv-record.argv
tests/networkxml2argvdata/nat-network-dns-txt-record.argv
tests/networkxml2argvdata/nat-network.argv
tests/networkxml2argvdata/netboot-network.argv
tests/networkxml2argvdata/netboot-proxy-network.argv
tests/networkxml2argvdata/routed-network.argv
tests/networkxml2argvtest.c

index 7ea2e9440409938dc4ab2ea9c861a8c1218dd017..a9beb05911fd5b3bccb24bc2aeec30166a87c33c 100644 (file)
@@ -1,4 +1,4 @@
-/usr/sbin/dnsmasq --strict-order --bind-interfaces --conf-file= \
+@DNSMASQ@ --strict-order --bind-interfaces --conf-file= \
 --except-interface lo --dhcp-option=3 --no-resolv \
 --listen-address 192.168.152.1 \
 --dhcp-range 192.168.152.2,192.168.152.254 \
index 2158df8702413112ea9ff51c488e0f7588479d1d..c7e496797df6fbc2185792096986ad4d0dffd6cb 100644 (file)
@@ -1,3 +1,3 @@
-/usr/sbin/dnsmasq --strict-order --bind-interfaces --domain example.com \
+@DNSMASQ@ --strict-order --bind-interfaces --domain example.com \
 --conf-file= --except-interface lo --listen-address 192.168.122.1 \
 --expand-hosts --addn-hosts=/var/lib/libvirt/dnsmasq/default.addnhosts\
index 021e8f07a5ad6c3144ea74687029bdcd19e3335e..ea1da6d5f8984a717c3d2f302bb6e1ae716a4d10 100644 (file)
@@ -1,4 +1,4 @@
-/usr/sbin/dnsmasq \
+@DNSMASQ@ \
 --strict-order \
 --bind-interfaces \
 --conf-file= \
index 85afbba700143f02ade3990176c9a89727c77a87..84c2d2f60d50fef2bd836519a60dcb02b640eb3f 100644 (file)
@@ -1,4 +1,4 @@
-/usr/sbin/dnsmasq \
+@DNSMASQ@ \
 --strict-order \
 --bind-interfaces \
 --conf-file= \
index be6ba4bff02f6921f51a85ca0638c105669411ab..bed309ffe6c6dcc45698f261be83e9e8da68241e 100644 (file)
@@ -1,4 +1,4 @@
-/usr/sbin/dnsmasq --strict-order --bind-interfaces --conf-file= \
+@DNSMASQ@ --strict-order --bind-interfaces --conf-file= \
 --except-interface lo --txt-record=example,example value \
 --listen-address 192.168.122.1 --listen-address 192.168.123.1 \
 --listen-address 2001:db8:ac10:fe01::1 \
index d7faee201a1d037818ce8bf59c09808fff487148..80878a8eb0052fd30d2325d45a0c0eafccc6a4d0 100644 (file)
@@ -1,4 +1,4 @@
-/usr/sbin/dnsmasq --strict-order --bind-interfaces --conf-file= \
+@DNSMASQ@ --strict-order --bind-interfaces --conf-file= \
 --except-interface lo --listen-address 192.168.122.1 \
 --listen-address 192.168.123.1 --listen-address 2001:db8:ac10:fe01::1 \
 --listen-address 2001:db8:ac10:fd01::1 --listen-address 10.24.10.1 \
index 78e873c951e8b1c2d1c8ad05fd728d80048189d5..7ae5b75bc60a34daccaa9aa0f7ffa039065a2991 100644 (file)
@@ -1,4 +1,4 @@
-/usr/sbin/dnsmasq --strict-order --bind-interfaces --domain example.com \
+@DNSMASQ@ --strict-order --bind-interfaces --domain example.com \
 --conf-file= --except-interface lo --listen-address 192.168.122.1 \
 --dhcp-range 192.168.122.2,192.168.122.254 \
 --dhcp-leasefile=/var/lib/libvirt/dnsmasq/netboot.leases \
index 5fe1b8ef1bae3ff4e660d3e08aab33110aebd77d..2c5a0d53397efbb1163bcb179a72d91879819b5f 100644 (file)
@@ -1,4 +1,4 @@
-/usr/sbin/dnsmasq --strict-order --bind-interfaces --domain example.com \
+@DNSMASQ@ --strict-order --bind-interfaces --domain example.com \
 --conf-file= --except-interface lo --listen-address 192.168.122.1 \
 --dhcp-range 192.168.122.2,192.168.122.254 \
 --dhcp-leasefile=/var/lib/libvirt/dnsmasq/netboot.leases \
index d059630d803b28d9adf22adc488e4be2a274a2c5..75303e6ee491e285321084ffadc702b8c2ce7fa4 100644 (file)
@@ -1,2 +1,2 @@
-/usr/sbin/dnsmasq --strict-order --bind-interfaces --conf-file= \
+@DNSMASQ@ --strict-order --bind-interfaces --conf-file= \
 --except-interface lo --listen-address 192.168.122.1\
index 2dd9b7f14a857ae67faca70aaec6a318f96f816a..5c6e29d2e06291f38a0a7f4e50fd054537bbaa5c 100644 (file)
 #include "memory.h"
 #include "network/bridge_driver.h"
 
+/* Replace all occurrences of @token in @buf by @replacement and adjust size of
+ * @buf accordingly. Returns 0 on success and -1 on out-of-memory errors. */
+static int replaceTokens(char **buf, const char *token, const char *replacement) {
+    char *token_start, *token_end;
+    size_t buf_len, rest_len;
+    const size_t token_len = strlen(token);
+    const size_t replacement_len = strlen(replacement);
+    const int diff = replacement_len - token_len;
+
+    buf_len = rest_len = strlen(*buf) + 1;
+    token_end = *buf;
+    for (;;) {
+        token_start = strstr(token_end, token);
+        if (token_start == NULL)
+            break;
+        rest_len -= token_start + token_len - token_end;
+        token_end = token_start + token_len;
+        buf_len += diff;
+        if (diff > 0)
+            if (VIR_REALLOC_N(*buf, buf_len) < 0)
+                return -1;
+        if (diff != 0)
+            memmove(token_end + diff, token_end, rest_len);
+        memcpy(token_start, replacement, replacement_len);
+        token_end += diff;
+    }
+    /* if diff < 0, we could shrink the buffer here... */
+    return 0;
+}
+
 static int testCompareXMLToArgvFiles(const char *inxml, const char *outargv) {
     char *inXmlData = NULL;
     char *outArgvData = NULL;
@@ -32,6 +62,9 @@ static int testCompareXMLToArgvFiles(const char *inxml, const char *outargv) {
     if (virtTestLoadFile(outargv, &outArgvData) < 0)
         goto fail;
 
+    if (replaceTokens(&outArgvData, "@DNSMASQ@", DNSMASQ))
+        goto fail;
+
     if (!(dev = virNetworkDefParseString(inXmlData)))
         goto fail;