Merge pull request #10 from cgaonker/master

Changes to cord-test.py to include new options to run, build and list…
diff --git a/src/test/setup/cord-test.py b/src/test/setup/cord-test.py
index d77a2a5..525f8fe 100755
--- a/src/test/setup/cord-test.py
+++ b/src/test/setup/cord-test.py
@@ -36,8 +36,10 @@
                        ('/var/run/docker.sock', '/var/run/docker.sock')
                        )
     basename = 'cord-tester'
+    IMAGE = 'cord-test/nose'
+    ALL_TESTS = ('tls', 'dhcp', 'igmp', 'subscriber', 'vrouter', 'flows')
 
-    def __init__(self, ctlr_ip = None, image = 'cord-test/nose', tag = 'latest',
+    def __init__(self, ctlr_ip = None, image = IMAGE, tag = 'latest',
                  env = None, rm = False, update = False):
         self.ctlr_ip = ctlr_ip
         self.rm = rm
@@ -207,7 +209,7 @@
 
 ##default onos/radius/test container images and names
 onos_image_default='onosproject/onos:latest'
-nose_image_default='cord-test/nose:latest'
+nose_image_default= '{}:latest'.format(CordTester.IMAGE)
 test_type_default='dhcp'
 onos_app_version = '1.0-SNAPSHOT'
 cord_tester_base = os.path.dirname(os.path.realpath(__file__))
@@ -216,22 +218,24 @@
 def runTest(args):
     #Start the cord test tcp server
     test_server = cord_test_server_start()
-    tests = args.test_type.split('-')
+    if args.test_type.lower() == 'all':
+        tests = CordTester.ALL_TESTS
+        args.radius = True
+        args.quagga = True
+    else:
+        tests = args.test_type.lower().split('-')
+
     onos_cnt = {'tag':'latest'}
-    nose_cnt = {'image': 'cord-test/nose','tag': 'latest'}
+    nose_cnt = {'image': CordTester.IMAGE, 'tag': 'latest'}
+    update_map = { 'quagga' : False, 'test' : False, 'radius' : False }
+    update_map[args.update.lower()] = True
+    
+    if args.update.lower() == 'all':
+       for c in update_map.keys():
+           update_map[c] = True
+    
     radius_ip = None
     quagga_ip = None
-    if args.cleanup:
-        cleanup_container = args.cleanup
-        if cleanup_container.find(':') < 0:
-            cleanup_container += ':latest'
-        print('Cleaning up containers %s' %cleanup_container)
-        Container.cleanup(cleanup_container)
-        sys.exit(0)
-
-    if args.list:
-        CordTester.list_tests(tests)
-        sys.exit(0)
 
     #don't spawn onos if the user has specified external test controller with test interface config
     if args.test_controller:
@@ -251,7 +255,7 @@
 
         ##Start Radius container if specified
         if args.radius == True:
-            radius = Radius()
+            radius = Radius( update = update_map['radius'])
             radius_ip = radius.ip()
             print('Radius server running with IP %s' %radius_ip)
         else:
@@ -263,7 +267,7 @@
     
     if args.quagga == True:
         #Start quagga. Builds container if required
-        quagga = Quagga()
+        quagga = Quagga(update = update_map['quagga'])
         quagga_ip = quagga.ip()
         
     test_cnt_env = { 'ONOS_CONTROLLER_IP' : onos_ip,
@@ -277,28 +281,69 @@
     test_cnt = CordTester(ctlr_ip = onos_ip, image = nose_cnt['image'], tag = nose_cnt['tag'],
                           env = test_cnt_env,
                           rm = False if args.keep else True,
-                          update = args.update)
+                          update = update_map['test'])
     if args.start_switch or not args.olt:
         test_cnt.start_switch()
     test_cnt.setup_intfs()
     test_cnt.run_tests(tests)
     cord_test_server_stop(test_server)
 
+def cleanupTests(args):
+    test_container = '{}:latest'.format(CordTester.IMAGE)
+    print('Cleaning up Test containers ...')
+    Container.cleanup(test_container)
+
+def listTests(args):
+    if args.test == 'all':
+        tests = CordTester.ALL_TESTS
+    else:
+        tests = args.test.lower().split('-')
+    CordTester.list_tests(tests)
+
+def buildImages(args):
+    if args.image == 'all' or args.image == 'quagga':
+        Quagga.build_image(Quagga.IMAGE)
+    
+    if args.image == 'all' or args.image == 'radius':
+        Radius.build_image(Radius.IMAGE)
+
+    if args.image == 'all' or args.image == 'test':
+        CordTester.build_image(CordTester.IMAGE)
+
 if __name__ == '__main__':
     parser = ArgumentParser(description='Cord Tester')
-    parser.add_argument('-t', '--test-type', default=test_type_default, type=str)
-    parser.add_argument('-o', '--onos', default=onos_image_default, type=str, help='ONOS container image')
-    parser.add_argument('-r', '--radius',action='store_true', help='Start Radius service')
-    parser.add_argument('-q', '--quagga',action='store_true',help='Provision quagga container for vrouter')
-    parser.add_argument('-a', '--app', default=onos_app_file, type=str, help='Cord ONOS app filename')
-    parser.add_argument('-p', '--olt', action='store_true', help='Use OLT config')
-    parser.add_argument('-l', '--list', action='store_true', help='List test cases')
-    parser.add_argument('-e', '--test-controller', default='', type=str, help='External test controller ip for Onos and/or radius server.'
+    subparser = parser.add_subparsers()
+    parser_run = subparser.add_parser('run', help='Run cord tester')
+    parser_run.add_argument('-t', '--test-type', default=test_type_default, help='Specify test type or test case to run')
+    parser_run.add_argument('-o', '--onos', default=onos_image_default, type=str, help='ONOS container image')
+    parser_run.add_argument('-r', '--radius',action='store_true', help='Start Radius service')
+    parser_run.add_argument('-q', '--quagga',action='store_true',help='Provision quagga container for vrouter')
+    parser_run.add_argument('-a', '--app', default=onos_app_file, type=str, help='Cord ONOS app filename')
+    parser_run.add_argument('-p', '--olt', action='store_true', help='Use OLT config')
+    parser_run.add_argument('-e', '--test-controller', default='', type=str, help='External test controller ip for Onos and/or radius server. '
                         'Eg: 10.0.0.2/10.0.0.3 to specify ONOS and Radius ip to connect')
-    parser.add_argument('-c', '--cleanup', default='', type=str, help='Cleanup test containers')
-    parser.add_argument('-k', '--keep', action='store_true', help='Keep test container after tests')
-    parser.add_argument('-s', '--start-switch', action='store_true', help='Start OVS when running under OLT config')
-    parser.add_argument('-u', '--update', action='store_true', help='Update test container image')
-    parser.set_defaults(func=runTest)
+    parser_run.add_argument('-k', '--keep', action='store_true', help='Keep test container after tests')
+    parser_run.add_argument('-s', '--start-switch', action='store_true', help='Start OVS when running under OLT config')
+    parser_run.add_argument('-u', '--update', default='none', choices=['test','quagga','radius', 'all'], type=str, help='Update cord tester container images. '
+                        'Eg: --update=quagga to rebuild quagga image.'
+                        '    --update=radius to rebuild radius server image.'
+                        '    --update=test to rebuild cord test image.(Default)'
+                        '    --update=all to rebuild all cord tester images.')
+    parser_run.set_defaults(func=runTest)
+
+    parser_list = subparser.add_parser('list', help='List test cases')
+    parser_list.add_argument('-t', '--test', default='all', help='Specify test type to list test cases. '
+                             'Eg: -t tls to list tls test cases.'
+                             '    -t tls-dhcp-vrouter to list tls,dhcp and vrouter test cases.'
+                             '    -t all to list all test cases.')
+    parser_list.set_defaults(func=listTests)
+
+    parser_build = subparser.add_parser('build', help='Build cord test container images')
+    parser_build.add_argument('image', choices=['quagga', 'radius', 'test', 'all'])
+    parser_build.set_defaults(func=buildImages)
+
+    parser_cleanup = subparser.add_parser('cleanup', help='Cleanup test containers')
+    parser_cleanup.set_defaults(func=cleanupTests)
+
     args = parser.parse_args()
     args.func(args)
diff --git a/src/test/setup/eval.sh b/src/test/setup/eval.sh
index f37b0c4..51513c9 100755
--- a/src/test/setup/eval.sh
+++ b/src/test/setup/eval.sh
@@ -3,16 +3,16 @@
 if [ ! -f $cord_tester ]; then
   cord_tester="$HOME/cord-tester/src/test/setup/cord-test.py"
 fi
-ONOS_IGMP="onosproject/onos:1.5"
+echo "Building all cord-tester images."
+$cord_tester build all
 docker kill cord-onos || true
 docker kill cord-quagga || true
+docker kill cord-radius || true
 echo "Running TLS authentication test"
-$cord_tester -r -t tls
+$cord_tester run -r -t tls
 echo "Running DHCP request test"
-$cord_tester -q -t dhcp
-docker kill cord-onos || true
+$cord_tester run -t dhcp
 echo "Running IGMP join verify test"
-$cord_tester -q -o $ONOS_IGMP -t igmp:igmp_exchange.test_igmp_join_verify_traffic
-docker kill cord-onos || true
+$cord_tester run -t igmp:igmp_exchange.test_igmp_join_verify_traffic
 echo "Running VROUTER test with 5 routes"
-$cord_tester -q -t vrouter:vrouter_exchange.test_vrouter_1
+$cord_tester run -q -t vrouter:vrouter_exchange.test_vrouter_1
diff --git a/src/test/utils/CordContainer.py b/src/test/utils/CordContainer.py
index c04f219..de4ec2f 100644
--- a/src/test/utils/CordContainer.py
+++ b/src/test/utils/CordContainer.py
@@ -199,8 +199,9 @@
     host_config_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', 'setup/onos-config')
     guest_config_dir = '/root/onos/config'
     host_guest_map = ( (host_config_dir, guest_config_dir), )
+    NAME = 'cord-onos'
 
-    def __init__(self, name = 'cord-onos', image = 'onosproject/onos', tag = 'latest', 
+    def __init__(self, name = NAME, image = 'onosproject/onos', tag = 'latest', 
                  boot_delay = 60, restart = False, network_cfg = None):
         if restart is True:
             ##Find the right image to restart
@@ -245,10 +246,13 @@
     host_guest_map = ( (host_db_dir, guest_db_dir),
                        (host_config_dir, guest_config_dir)
                        )
-    def __init__(self, name = 'cord-radius', image = 'cord-test/radius', tag = 'latest',
-                 boot_delay = 10, restart = False):
+    IMAGE = 'cord-test/radius'
+    NAME = 'cord-radius'
+
+    def __init__(self, name = NAME, image = IMAGE, tag = 'latest',
+                 boot_delay = 10, restart = False, update = False):
         super(Radius, self).__init__(name, image, tag = tag, command = self.start_command)
-        if not self.img_exists():
+        if update is True or not self.img_exists():
             self.build_image(image)
         if restart is True and self.exists():
             self.kill()
@@ -289,11 +293,13 @@
     guest_quagga_config = '/root/config'
     quagga_config_file = os.path.join(guest_quagga_config, 'testrib.conf')
     host_guest_map = ( (host_quagga_config, guest_quagga_config), )
-    
-    def __init__(self, name = 'cord-quagga', image = 'cord-test/quagga', tag = 'latest', 
-                 boot_delay = 15, restart = False, config_file = quagga_config_file):
+    IMAGE = 'cord-test/quagga'
+    NAME = 'cord-quagga'
+
+    def __init__(self, name = NAME, image = IMAGE, tag = 'latest', 
+                 boot_delay = 15, restart = False, config_file = quagga_config_file, update = False):
         super(Quagga, self).__init__(name, image, tag = tag, quagga_config = self.quagga_config)
-        if not self.img_exists():
+        if update is True or not self.img_exists():
             self.build_image(image)
         if restart is True and self.exists():
             self.kill()