From d869d820272d83a7bcc1fb2479f2f75707fcea0c Mon Sep 17 00:00:00 2001
From: Bob Lantz <rlantz@cs.stanford.edu>
Date: Mon, 16 Aug 2010 20:21:08 -0700
Subject: [PATCH] Added support for pre- and post-test CLI scripts.

---
 bin/mn         | 38 ++++++++++++++++++++++++++++----------
 mininet/cli.py |  7 +++++--
 2 files changed, 33 insertions(+), 12 deletions(-)

diff --git a/bin/mn b/bin/mn
index 42b3e256..7bb4256b 100755
--- a/bin/mn
+++ b/bin/mn
@@ -17,6 +17,7 @@ import sys
 import time
 
 from mininet.clean import cleanup
+from mininet.cli import CLI
 from mininet.log import lg, LEVELS, info
 from mininet.net import Mininet, init
 from mininet.node import KernelSwitch, Host, Controller, ControllerParams, NOX
@@ -50,9 +51,11 @@ CONTROLLERS = { 'ref': Controller,
                'none': lambda name: None }
 
 # optional tests to run
-TESTS = [ 'cli', 'build', 'pingAll', 'pingPair', 'iperf', 'all', 'iperfUdp',
+TESTS = [ 'cli', 'build', 'pingall', 'pingpair', 'iperf', 'all', 'iperfudp',
          'none' ]
 
+ALTSPELLING = { 'pingall': 'pingAll', 'pingpair': 'pingPair',
+    'iperfudp': 'iperfUdp', 'iperfUDP': 'iperfUdp' }
 
 def buildTopo( topo ):
     "Create topology from string with format (object, arg1, arg2,...)."
@@ -99,6 +102,7 @@ class MininetRunner( object ):
     def __init__( self ):
         "Init."
         self.options = None
+        self.args = None  # May be used someday for more CLI scripts
         self.validate = None
 
         self.parseArgs()
@@ -170,9 +174,14 @@ class MininetRunner( object ):
         opts.add_option( '--port', type='int', default=6633,
                         help='[port integer for a listening remote'
                         ' controller]' )
-        opts.add_option( '--inNamespace', action='store_true',
+        opts.add_option( '--innamespace', action='store_true',
                         default=False, help='sw and ctrl in namespace?' )
-        self.options = opts.parse_args()[ 0 ]
+        opts.add_option( '--pre', type='string', default=None,
+                        help='[CLI script to run before tests]' )
+        opts.add_option( '--post', type='string', default=None,
+                        help='[CLI script to run after tests]' )
+
+        self.options, self.args = opts.parse_args()
 
     def setup( self ):
         "Setup and validate environment."
@@ -209,7 +218,7 @@ class MininetRunner( object ):
             self.validate( self.options )
 
         controllerParams = ControllerParams( '10.0.0.0', 8 )
-        inNamespace = self.options.inNamespace
+        inNamespace = self.options.innamespace
         xterms = self.options.xterms
         mac = self.options.mac
         arp = self.options.arp
@@ -217,20 +226,29 @@ class MininetRunner( object ):
                      inNamespace=inNamespace,
                      xterms=xterms, autoSetMacs=mac,
                      autoStaticArp=arp )
+        if self.options.pre:
+            CLI( mn, script=self.options.pre )
 
         test = self.options.test
+        test = ALTSPELLING.get( test, test )
+        
+        mn.start()
+        
         if test == 'none':
-            mn.start()
-            mn.stop()
-        elif test == 'cli':
-            mn.interact()
+            pass
         elif test == 'all':
             mn.start()
             mn.ping()
             mn.iperf()
-            mn.stop()
+        elif test == 'cli':
+            CLI( mn )
         elif test != 'build':
-            mn.run( getattr( mn, test ) )
+            getattr( mn, test )()
+
+        if self.options.post:
+            CLI( mn, script=self.options.post )
+
+        mn.stop()
 
         elapsed = float( time.time() - start )
         info( 'completed in %0.3f seconds\n' % elapsed )
diff --git a/mininet/cli.py b/mininet/cli.py
index b9dbc3f8..a8843163 100644
--- a/mininet/cli.py
+++ b/mininet/cli.py
@@ -40,7 +40,7 @@ class CLI( Cmd ):
 
     prompt = 'mininet> '
 
-    def __init__( self, mininet, stdin=sys.stdin ):
+    def __init__( self, mininet, stdin=sys.stdin, script=None ):
         self.mn = mininet
         self.nodelist = self.mn.controllers + self.mn.switches + self.mn.hosts
         self.nodemap = {}  # map names to Node objects
@@ -50,9 +50,12 @@ def __init__( self, mininet, stdin=sys.stdin ):
         self.stdin = stdin
         self.inPoller = poll()
         self.inPoller.register( stdin )
-        self.inputFile = None
+        self.inputFile = script
         Cmd.__init__( self )
         info( '*** Starting CLI:\n' )
+        if self.inputFile:
+            self.do_source( self.inputFile )
+            return
         while True:
             try:
                 # Make sure no nodes are still waiting
-- 
GitLab