[libvirt] [PATCH] tests: redo test argv file line wrapping

Daniel P. Berrange berrange at redhat.com
Fri Nov 6 13:30:35 UTC 2015


Back in

  commit bd6c46fa0cfe275c24debc1152cfc5206c04b59b
  Author: Juerg Haefliger <juerg.haefliger at hp.com>
  Date:   Mon Jan 31 06:42:57 2011 -0500

    tests: handle backspace-newline pairs in test input files

all the test argv files were line wrapped so that the args
were less than 80 characters.

The way the line wrapping was done turns out to be quite
undesirable, because it often leaves multiple parameters
on the same line. If we later need to add or remove
individual parameters, then it leaves us having to redo
line wrapping.

This commit changes the line wrapping so that every
single "-param value" is one its own new line. If the
"value" is still too long, then we break on ',' or ':'
or ' ' as needed.

This means that when we come to add / remove parameters
from the test files line, the patch diffs will only
ever show a single line added/removed which will greatly
simplify review work.

Signed-off-by: Daniel P. Berrange <berrange at redhat.com>
---
 .../bhyvexml2argvdata/bhyvexml2argv-acpiapic.args  |  10 +-
 .../networkxml2firewalldata/nat-default-linux.args | 137 ++++++++++++++++-----
 .../qemuxml2argv-aarch64-aavmf-virtio-mmio.args    |  33 +++--
 tests/test-wrap-argv.pl                            | 106 ++++++++++++++++
 4 files changed, 247 insertions(+), 39 deletions(-)
 create mode 100644 tests/test-wrap-argv.pl

To avoid sending a 10 MB email, I'v only include these 3 example file
changes. To view *all* the changes see my staging branch

  https://gitlab.com/berrange/libvirt/commit/4fe45c39dae176ae4dbc7e126d2a2ff29b49eceb

'make check' and 'make syntax-check' of course still pass after
applying the big change.

diff --git a/tests/bhyvexml2argvdata/bhyvexml2argv-acpiapic.args b/tests/bhyvexml2argvdata/bhyvexml2argv-acpiapic.args
index 6b26964..e1afefd 100644
--- a/tests/bhyvexml2argvdata/bhyvexml2argv-acpiapic.args
+++ b/tests/bhyvexml2argvdata/bhyvexml2argv-acpiapic.args
@@ -1,3 +1,11 @@
-/usr/sbin/bhyve -c 1 -m 214 -A -I -u -H -P -s 0:0,hostbridge \
+/usr/sbin/bhyve \
+-c 1 \
+-m 214 \
+-A \
+-I \
+-u \
+-H \
+-P \
+-s 0:0,hostbridge \
 -s 3:0,virtio-net,faketapdev,mac=52:54:00:00:00:00 \
 -s 2:0,ahci-hd,/tmp/freebsd.img bhyve
diff --git a/tests/networkxml2firewalldata/nat-default-linux.args b/tests/networkxml2firewalldata/nat-default-linux.args
index b92a845..ffdafdf 100644
--- a/tests/networkxml2firewalldata/nat-default-linux.args
+++ b/tests/networkxml2firewalldata/nat-default-linux.args
@@ -1,30 +1,107 @@
-iptables --table filter --insert INPUT --in-interface virbr0 --protocol tcp \
---destination-port 67 --jump ACCEPT
-iptables --table filter --insert INPUT --in-interface virbr0 --protocol udp \
---destination-port 67 --jump ACCEPT
-iptables --table filter --insert OUTPUT --out-interface virbr0 --protocol udp \
---destination-port 68 --jump ACCEPT
-iptables --table filter --insert INPUT --in-interface virbr0 --protocol tcp \
---destination-port 53 --jump ACCEPT
-iptables --table filter --insert INPUT --in-interface virbr0 --protocol udp \
---destination-port 53 --jump ACCEPT
-iptables --table filter --insert FORWARD --in-interface virbr0 --jump REJECT
-iptables --table filter --insert FORWARD --out-interface virbr0 --jump REJECT
-iptables --table filter --insert FORWARD --in-interface virbr0 \
---out-interface virbr0 --jump ACCEPT
-iptables --table filter --insert FORWARD --source 192.168.122.0/24 \
---in-interface virbr0 --jump ACCEPT
-iptables --table filter --insert FORWARD --destination 192.168.122.0/24 \
---out-interface virbr0 --match conntrack --ctstate ESTABLISHED,RELATED --jump ACCEPT
-iptables --table nat --insert POSTROUTING --source 192.168.122.0/24 '!' \
---destination 192.168.122.0/24 --jump MASQUERADE
-iptables --table nat --insert POSTROUTING --source 192.168.122.0/24 \
--p udp '!' --destination 192.168.122.0/24 --jump MASQUERADE --to-ports 1024-65535
-iptables --table nat --insert POSTROUTING --source 192.168.122.0/24 \
--p tcp '!' --destination 192.168.122.0/24 --jump MASQUERADE --to-ports 1024-65535
-iptables --table nat --insert POSTROUTING --source 192.168.122.0/24 \
---destination 255.255.255.255/32 --jump RETURN
-iptables --table nat --insert POSTROUTING --source 192.168.122.0/24 \
---destination 224.0.0.0/24 --jump RETURN
-iptables --table mangle --insert POSTROUTING --out-interface virbr0 \
---protocol udp --destination-port 68 --jump CHECKSUM --checksum-fill
+iptables \
+--table filter \
+--insert INPUT \
+--in-interface virbr0 \
+--protocol tcp \
+--destination-port 67 \
+--jump ACCEPT
+iptables \
+--table filter \
+--insert INPUT \
+--in-interface virbr0 \
+--protocol udp \
+--destination-port 67 \
+--jump ACCEPT
+iptables \
+--table filter \
+--insert OUTPUT \
+--out-interface virbr0 \
+--protocol udp \
+--destination-port 68 \
+--jump ACCEPT
+iptables \
+--table filter \
+--insert INPUT \
+--in-interface virbr0 \
+--protocol tcp \
+--destination-port 53 \
+--jump ACCEPT
+iptables \
+--table filter \
+--insert INPUT \
+--in-interface virbr0 \
+--protocol udp \
+--destination-port 53 \
+--jump ACCEPT
+iptables \
+--table filter \
+--insert FORWARD \
+--in-interface virbr0 \
+--jump REJECT
+iptables \
+--table filter \
+--insert FORWARD \
+--out-interface virbr0 \
+--jump REJECT
+iptables \
+--table filter \
+--insert FORWARD \
+--in-interface virbr0 \
+--out-interface virbr0 \
+--jump ACCEPT
+iptables \
+--table filter \
+--insert FORWARD \
+--source 192.168.122.0/24 \
+--in-interface virbr0 \
+--jump ACCEPT
+iptables \
+--table filter \
+--insert FORWARD \
+--destination 192.168.122.0/24 \
+--out-interface virbr0 \
+--match conntrack \
+--ctstate ESTABLISHED,RELATED \
+--jump ACCEPT
+iptables \
+--table nat \
+--insert POSTROUTING \
+--source 192.168.122.0/24 '!' \
+--destination 192.168.122.0/24 \
+--jump MASQUERADE
+iptables \
+--table nat \
+--insert POSTROUTING \
+--source 192.168.122.0/24 \
+-p udp '!' \
+--destination 192.168.122.0/24 \
+--jump MASQUERADE \
+--to-ports 1024-65535
+iptables \
+--table nat \
+--insert POSTROUTING \
+--source 192.168.122.0/24 \
+-p tcp '!' \
+--destination 192.168.122.0/24 \
+--jump MASQUERADE \
+--to-ports 1024-65535
+iptables \
+--table nat \
+--insert POSTROUTING \
+--source 192.168.122.0/24 \
+--destination 255.255.255.255/32 \
+--jump RETURN
+iptables \
+--table nat \
+--insert POSTROUTING \
+--source 192.168.122.0/24 \
+--destination 224.0.0.0/24 \
+--jump RETURN
+iptables \
+--table mangle \
+--insert POSTROUTING \
+--out-interface virbr0 \
+--protocol udp \
+--destination-port 68 \
+--jump CHECKSUM \
+--checksum-fill
diff --git a/tests/qemuxml2argvdata/qemuxml2argv-aarch64-aavmf-virtio-mmio.args b/tests/qemuxml2argvdata/qemuxml2argv-aarch64-aavmf-virtio-mmio.args
index d6d8aed..4cc4cd2 100644
--- a/tests/qemuxml2argvdata/qemuxml2argv-aarch64-aavmf-virtio-mmio.args
+++ b/tests/qemuxml2argvdata/qemuxml2argv-aarch64-aavmf-virtio-mmio.args
@@ -1,14 +1,31 @@
-LC_ALL=C PATH=/bin HOME=/home/test USER=test LOGNAME=test QEMU_AUDIO_DRV=none \
-/usr/bin/qemu-system-aarch64 -S -M virt -cpu cortex-a53 \
--m 1024 -smp 1 -nographic \
--nodefconfig -nodefaults -monitor unix:/tmp/test-monitor,server,nowait \
--boot c -kernel /aarch64.kernel -initrd /aarch64.initrd -append \
-'earlyprintk console=ttyAMA0,115200n8 rw root=/dev/vda rootwait' \
--dtb /aarch64.dtb -device virtio-serial-device,id=virtio-serial0 -usb \
+LC_ALL=C \
+PATH=/bin \
+HOME=/home/test \
+USER=test \
+LOGNAME=test \
+QEMU_AUDIO_DRV=none \
+/usr/bin/qemu-system-aarch64 \
+-S \
+-M virt \
+-cpu cortex-a53 \
+-m 1024 \
+-smp 1 \
+-nographic \
+-nodefconfig \
+-nodefaults \
+-monitor unix:/tmp/test-monitor,server,nowait \
+-boot c \
+-kernel /aarch64.kernel \
+-initrd /aarch64.initrd \
+-append 'earlyprintk console=ttyAMA0,115200n8 rw root=/dev/vda rootwait' \
+-dtb /aarch64.dtb \
+-device virtio-serial-device,id=virtio-serial0 \
+-usb \
 -drive file=/aarch64.raw,if=none,id=drive-virtio-disk0 \
 -device virtio-blk-device,drive=drive-virtio-disk0,id=virtio-disk0 \
 -device virtio-net-device,vlan=0,id=net0,mac=52:54:00:09:a4:37 \
--net user,vlan=0,name=hostnet0 -chardev pty,id=charconsole0 \
+-net user,vlan=0,name=hostnet0 \
+-chardev pty,id=charconsole0 \
 -device virtconsole,chardev=charconsole0,id=console0 \
 -device virtio-balloon-device,id=balloon0 \
 -object rng-random,id=objrng0,filename=/dev/random \
diff --git a/tests/test-wrap-argv.pl b/tests/test-wrap-argv.pl
new file mode 100644
index 0000000..e8c782b
--- /dev/null
+++ b/tests/test-wrap-argv.pl
@@ -0,0 +1,106 @@
+#!/usr/bin/perl
+
+
+
+foreach my $file (@ARGV) {
+    &rewrap($file);
+}
+
+sub rewrap {
+    my $file = shift;
+
+    # Read the original file
+    open FILE, "<", $file or die "cannot read $file: $!";
+    my @lines;
+    while (<FILE>) {
+        # If there is a trailing '\' then kill the new line
+        if (/\\$/) {
+            chomp;
+            $_ =~ s/\\$//;
+        }
+
+        push @lines, $_;
+    }
+
+    # Skip empty files
+    return unless @lines;
+
+    # Kill the last new line in the file
+    chomp @lines[$#lines];
+    close FILE;
+
+    # Reconstruct the master data by joining all lines
+    # and then split again based on the real desired
+    # newlines
+    @lines = split /\n/, join('', @lines);
+
+    open FILE, ">", $file or die "cannot create $file: $!";
+
+    print "Process $file\n";
+    # Now each @lines represents a single command, we
+    # can process them
+    foreach my $line (@lines) {
+        my @bits = split / /, join('', $line);
+
+        # @bits contains env vars, then the command line
+        # and then the arguments
+        my @env;
+        my $cmd;
+        my @args;
+
+        foreach my $bit (@bits) {
+            # If no command is defined yet, we must still
+            # have env vars
+            if (!defined $cmd) {
+                # Look for leading / or = to indicate command name
+                if ($bit =~ m,^/,) {
+                    $cmd = $bit;
+                } else {
+                    push @env, $bit;
+                }
+            } else {
+                # If there's a leading '-' then this is a new
+                # parameter, otherwise its a value for the prev
+                # parameter.
+                if ($bit =~ m,^-,) {
+                    push @args, $bit;
+                } else {
+                    $args[$#args] .= " " . $bit;
+                }
+            }
+        }
+
+        # Print env + command first
+        print FILE join(" \\\n", @env, $cmd), " \\\n";
+        # We might have to split line argument values...
+        for (my $i = 0; $i <= $#args; $i++) {
+            my $arg = $args[$i];
+            while (length($arg) > 80) {
+                my $split = rindex $arg, ",", 80;
+                if ($split == -1) {
+                    $split = rindex $arg, ":", 80;
+                }
+                if ($split == -1) {
+                    $split = rindex $arg, " ", 80;
+                }
+                if ($split == -1) {
+                    warn "$file: cannot find nice place to split '$arg' below 80 chars\n";
+                    $split = 79;
+                }
+                $split++;
+
+                my $head = substr $arg, 0, $split;
+                $arg = substr $arg, $split;
+
+                print FILE $head, "\\\n";
+            }
+            print FILE $arg;
+            if ($i != $#args) {
+                print FILE " \\\n";
+            } else {
+                print FILE "\n";
+            }
+        }
+    }
+    close FILE;
+}
-- 
2.5.0




More information about the libvir-list mailing list