[ovs-dev] [PATCH 2/3] ovs-vsctl: Introduce new single-line output format.

Ben Pfaff blp at nicira.com
Thu Oct 15 22:38:50 UTC 2009


This output format should make it possible for tools to unambiguously parse
output from multiple ovs-vsctl commands in a single run (to be introduced
in an upcoming commit).

CC: Ian Campbell <Ian.Campbell at citrix.com>
---
 tests/ovs-vsctl.at       |   43 +++++++++++++++++++++++++++++++++++++++++--
 utilities/ovs-vsctl.8.in |    5 +++++
 utilities/ovs-vsctl.in   |   15 +++++++++++----
 3 files changed, 57 insertions(+), 6 deletions(-)

diff --git a/tests/ovs-vsctl.at b/tests/ovs-vsctl.at
index 15e5b1e..834ecc6 100644
--- a/tests/ovs-vsctl.at
+++ b/tests/ovs-vsctl.at
@@ -15,16 +15,31 @@ dnl specified PARENT and is on the given VLAN.
 m4_define([_CHECK_BRIDGE],
   [AT_CHECK([RUN_OVS_VSCTL([br-to-parent $1])], [0], [$2
 ])
+
+   # Check br-to-vlan, without --oneline.
    AT_CHECK([RUN_OVS_VSCTL([br-to-vlan $1])], [0], [$3
+])
+   # Check br-to-vlan, with --oneline.
+   # (This particular test is interesting with --oneline because it returns
+   # an integer instead of a string and that can cause type mismatches inside
+   # python if not done carefully.)
+   AT_CHECK([RUN_OVS_VSCTL([--oneline br-to-vlan $1])], [0], [$3
 ])])
 m4_define([CHECK_BRIDGES],
-  [dnl Check that the bridges appear on list-br.
+  [dnl Check that the bridges appear on list-br, without --oneline.
    AT_CHECK(
      [RUN_OVS_VSCTL([list-br])],
      [0],
      [m4_foreach([brinfo], [$@], [m4_car(brinfo)
 ])])
 
+   dnl Check that the bridges appear on list-br, with --oneline.
+   AT_CHECK(
+     [RUN_OVS_VSCTL([--oneline list-br])],
+     [0],
+     [m4_join([\n], m4_foreach([brinfo], [$@], [m4_car(brinfo),]))
+])
+
    dnl Check that each bridge exists according to br-exists and that
    dnl a bridge that should not exist does not.
    m4_foreach([brinfo], [$@], 
@@ -41,11 +56,19 @@ dnl list of ports, which must be in alphabetical order.  Also checks
 dnl that "ovs-vsctl port-to-br" reports that each port is
 dnl in BRIDGE.
 m4_define([CHECK_PORTS],
-  [AT_CHECK(
+  [dnl Check ports without --oneline.
+   AT_CHECK(
      [RUN_OVS_VSCTL([list-ports $1])],
      [0],
      [m4_foreach([port], m4_cdr($@), [port
 ])])
+
+   dnl Check ports with --oneline.
+   AT_CHECK(
+     [RUN_OVS_VSCTL([--oneline list-ports $1])],
+     [0],
+     [m4_join([\n], m4_shift($@))
+])
    AT_CHECK([RUN_OVS_VSCTL([port-to-br $1])], [1], [], [ovs-vsctl: no port named $1
 ])
    m4_foreach(
@@ -111,6 +134,22 @@ CHECK_PORTS([b])
 CHECK_IFACES([b])
 AT_CLEANUP
 
+AT_SETUP([add-br a, add-port a a1, add-port a a2])
+AT_KEYWORDS([ovs-vsctl])
+AT_CHECK([RUN_OVS_VSCTL(
+   [add-br a], 
+   [add-port a a1],
+   [add-port a a2])])
+AT_CHECK([cat conf], [0],
+  [bridge.a.port=a
+bridge.a.port=a1
+bridge.a.port=a2
+])
+CHECK_BRIDGES([a, a, 0])
+CHECK_PORTS([a], [a1], [a2])
+CHECK_IFACES([a], [a1], [a2])
+AT_CLEANUP
+
 AT_SETUP([add-br a b, add-port a a1, add-port b b1, del-br a])
 AT_KEYWORDS([ovs-vsctl])
 AT_CHECK([RUN_OVS_VSCTL(
diff --git a/utilities/ovs-vsctl.8.in b/utilities/ovs-vsctl.8.in
index b05a955..bc4cb11 100644
--- a/utilities/ovs-vsctl.8.in
+++ b/utilities/ovs-vsctl.8.in
@@ -98,6 +98,11 @@ its configuration file.
 By default, \fBovs\-vsctl\fR logs its arguments and the details of any
 changes that it makes to the system log.  This option disables this
 logging.
+.IP "\fB\-\-oneline\fR"
+Modifies the output format so that the output for a command is printed
+on a single line.  New-line characters that would otherwise separate
+lines are printed as \fB\\n\fR, and any instances of \fB\\fR that
+would otherwise appear in the output are doubled.
 .
 .SH COMMANDS
 The commands implemented by \fBovs\-vsctl\fR are described in the
diff --git a/utilities/ovs-vsctl.in b/utilities/ovs-vsctl.in
index 23d8ac0..012ce4e 100755
--- a/utilities/ovs-vsctl.in
+++ b/utilities/ovs-vsctl.in
@@ -474,7 +474,7 @@ def cmd_iface_to_br(cfg, iface):
 
 def cmd_br_to_vlan(cfg, bridge):
     parent, vlan = find_bridge(cfg, bridge)
-    return vlan,
+    return ["%s" % vlan]
 
 def cmd_br_to_parent(cfg, bridge):
     parent, vlan = find_bridge(cfg, bridge)
@@ -488,6 +488,7 @@ def main():
                                            "target=",
                                            "no-reload",
                                            "no-syslog",
+                                           "oneline",
                                            "help",
                                            "version"])
     except getopt.GetoptError, msg:
@@ -495,6 +496,7 @@ def main():
         sys.exit(1)
 
     # Handle options.
+    oneline = False
     for opt, optarg in options:
         if opt == "-c" or opt == "--config":
             global VSWITCHD_CONF
@@ -514,6 +516,8 @@ def main():
         elif opt == "--no-syslog":
             global SYSLOG
             SYSLOG = False
+        elif opt == "--oneline":
+            oneline = True
         else:
             raise RuntimeError("unhandled option %s" % opt)
 
@@ -557,9 +561,12 @@ def main():
     else:
         cfg = cfg_read(VSWITCHD_CONF, is_mutator)
         output = function(cfg, *args)
-        if output:
-            for line in output:
-                print line
+        if output != None:
+            if oneline:
+                print '\\n'.join([s.replace('\\', '\\\\') for s in output])
+            else:
+                for line in output:
+                    print line
         if is_mutator:
             cfg_save(cfg, VSWITCHD_CONF)
         sys.exit(0)
-- 
1.6.3.3





More information about the dev mailing list