Merge "Changes made so that PLATdockertest pulls all images from onos dockerhub"
diff --git a/TestON/drivers/common/api/dockerapidriver.py b/TestON/drivers/common/api/dockerapidriver.py
index 10743da..1d19488 100644
--- a/TestON/drivers/common/api/dockerapidriver.py
+++ b/TestON/drivers/common/api/dockerapidriver.py
@@ -47,6 +47,20 @@
         except Exception as e:
             main.log.exception( e )
 
+    def getListOfImages( self, repo="onosproject/onos" ):
+        """
+        Get the list of image tags
+        """
+        try:
+            imageList = list( self.dockerClient.images( name=repo ) )
+            imageListToSend = []
+            for imageDict in imageList:
+                if imageDict[ 'RepoTags' ] is not None:
+                    imageListToSend.append( imageDict['RepoTags'][0].encode('UTF8').split(':')[1] )
+            return imageListToSend
+        except Exception as e:
+            main.log.exception( e )
+
     def dockerPull( self, onosRepo ="onosproject/onos", onosTag="latest" ):
         """
         Pulls Docker image from repository
@@ -60,8 +74,8 @@
             main.log.info(json.dumps(json.loads(line), indent =4))
 
             #response = json.dumps( json.load( pullResult ), indent=4 )
-            if re.search( "for onosproject/onos:latest", line ):
-                main.log.info( "latest onos docker image pulled is: " + line )
+            if re.search( "for onosproject/onos:" + onosTag, line ):
+                main.log.info( "onos docker image pulled is: " + line )
                 return main.TRUE
             else:
                 main.log.error( "Failed to download image from: " + onosRepo +":"+ onosTag )
@@ -202,18 +216,19 @@
             #main.cleanup()
             #main.exit()
 
-    def dockerRemoveImage( self, image="onosproject/onos:latest" ):
+    def dockerRemoveImage( self, imageRepoTag=None ):
         """
             Remove Docker image
         """
         rmResult = main.TRUE
-
         if self.dockerClient.images() is []:
-            main.log.info( "No docker image found with RepoTags: " + image )
+            main.log.info( "No docker image found" )
             return rmResult
         else:
-            list = [ image["Id"] for image in self.dockerClient.images() if image["RepoTags"][0] == '<none>:<none>' ]
-            for id in list:
+            imageList = [ image["Id"] for image in self.dockerClient.images()
+                                        if image["RepoTags"] is None
+                                           or imageRepoTag in image["RepoTags"] ]
+            for id in imageList:
                 try:
                     main.log.info( self.name + ": Removing Docker image " + id )
                     response = self.dockerClient.remove_image(id, force = True)
diff --git a/TestON/install.sh b/TestON/install.sh
index fc434bc..ee9bc18 100755
--- a/TestON/install.sh
+++ b/TestON/install.sh
@@ -83,9 +83,9 @@
     echo "Installing TestON dependencies"
     if [ "$DIST" = "Fedora" ]; then
         # Fedora may have vlan enabled by default. Still need to confirm and update later
-        $cmd python-pip build-essential python-dev pep8 python3-requests
+        $cmd python-pip build-essential python-dev pep8 python3-requests docker docker-engine
     else
-        $cmd python-pip build-essential python-dev pep8 vlan python3-requests
+        $cmd python-pip build-essential python-dev pep8 vlan python3-requests docker docker-engine
     fi
 
     # Some Distos have this already from another package
diff --git a/TestON/requirements.txt b/TestON/requirements.txt
index 28f7503..08e9840 100644
--- a/TestON/requirements.txt
+++ b/TestON/requirements.txt
@@ -12,3 +12,7 @@
 requests==2.2.1
 scapy==2.3.1
 ipaddress==1.0.16
+docker==2.1.0
+docker-py==1.10.6
+docker-pycreds==0.2.1
+urllib3==1.20
diff --git a/TestON/tests/PLAT/PLATdockertest/PLATdockertest.params b/TestON/tests/PLAT/PLATdockertest/PLATdockertest.params
index 53c4b8c..b45ed9c 100755
--- a/TestON/tests/PLAT/PLATdockertest/PLATdockertest.params
+++ b/TestON/tests/PLAT/PLATdockertest/PLATdockertest.params
@@ -1,6 +1,6 @@
 <PARAMS>
 
-    <testcases>1,1000,5,10,110,900,1000</testcases>
+    <testcases>0,[1,1000,10,110,900,1000]*8</testcases>
 
     <DOCKER>
         <repo>onosproject/onos</repo>
diff --git a/TestON/tests/PLAT/PLATdockertest/PLATdockertest.py b/TestON/tests/PLAT/PLATdockertest/PLATdockertest.py
index 7133931..9abc871 100755
--- a/TestON/tests/PLAT/PLATdockertest/PLATdockertest.py
+++ b/TestON/tests/PLAT/PLATdockertest/PLATdockertest.py
@@ -24,6 +24,27 @@
         DOCKERREPO = "onosproject/onos"
         DOCKERTAG = "latest"
 
+    def CASE0( self, main ):
+        """
+        Pull all docker images and get a list of image tags
+        """
+        main.case( "Pull all docker images and get a list of image tags" )
+        main.step( "Pull all ONOS docker images" )
+        import os
+        DOCKERREPO = main.params[ 'DOCKER' ][ 'repo' ]
+        os.system( "docker pull -a " + DOCKERREPO )
+        imageTagList = list()
+        imageTagCounter = 0
+        main.step( "Get a list of image tags" )
+        stepResult = main.FALSE
+        imageTagList = main.ONOSbenchDocker.getListOfImages( DOCKERREPO )
+        if imageTagList is not []:
+            main.log.info( "The Image tag list is: " + str(imageTagList) )
+            stepResult = main.TRUE
+        utilities.assert_equals( expect = main.TRUE, actual = stepResult,
+                                    onpass = "image tag list pulled successfully",
+                                    onfail = "image tag list not pulled" )
+
     def CASE1( self, main ):
         """
         1) set up test params;
@@ -32,14 +53,16 @@
         import time
         import subprocess
 
-        main.case("Set case test params")
+        if imageTagCounter < len( imageTagList ):
+            DOCKERTAG = imageTagList[imageTagCounter]
+        imageTagCounter += 1
+
+        main.case("Set case test params for onos image {}".format( DOCKERTAG ))
         main.step("Initialize test params")
         NODElist = main.params["SCALE"]["nodelist"].split(',')
         main.log.info("onos container names are: " + ",".join(NODElist) )
         IPlist = list()
         main.testOnDirectory = re.sub( "(/tests)$", "", main.testDir )
-        DOCKERREPO = main.params[ 'DOCKER' ][ 'repo' ]
-        DOCKERTAG = main.params[ 'DOCKER' ][ 'tag' ]
         CTIDlist = list()
 
         main.log.info("Check docker status, it not running, try restart it")
@@ -58,16 +81,20 @@
             main.log.warn("docker is not running - exiting test")
             main.exit()
             main.cleanup()
+        if imageTagCounter > len( imageTagList ):
+            main.log.warn("All images have been tested")
+            main.exit()
+            main.cleanup()
 
     def CASE5(self, main):
         """
-        Pull (default) "onosproject/onos:latest" image
+        Pull the specified image
         """
 
-        main.case( "Pull latest onos docker image from onosproject/onos - \
-                    it may take sometime if this is a first time pulling." )
+        main.case( "Pull onos docker image {} from {} - \
+                    it may take sometime if this is a first time pulling.".format( DOCKERTAG, DOCKERREPO ) )
         stepResult = main.FALSE
-        main.step( "pull latest image from onosproject/onos")
+        main.step( "pull image {} from {}".format( DOCKERTAG, DOCKERREPO ) )
         stepResult = main.ONOSbenchDocker.dockerPull( onosRepo = DOCKERREPO, onosTag = DOCKERTAG )
         utilities.assert_equals( expect = main.TRUE, actual = stepResult,
                                     onpass = "Succeeded in pulling " + DOCKERREPO + ":" + DOCKERTAG,
@@ -81,10 +108,10 @@
         import re
         createResult = main.TRUE
         startResult = main.TRUE
-        main.case( "Start onos container(s)")
+        main.case( "Start onos container(s) for onos image {}".format( DOCKERTAG ))
         image = DOCKERREPO + ":" + DOCKERTAG
 
-        main.step( "Create and (re)start docker container(s) if not already exist")
+        main.step( "Create and (re)start docker container(s) for onos image {} if not already exist".format( DOCKERTAG ))
         #stepResult = main.FALSE
 
         for ct in xrange(0, len(NODElist)):
@@ -94,7 +121,7 @@
                 CTIDlist.append(ctid)
                 startResult = main.ONOSbenchDocker.dockerStartCT( ctID = ctid )
             else:
-                main.log.info("Container exists for node onos" + str(ct + 1) + "; restart container with latest image" )
+                main.log.info("Container exists for node onos" + str(ct + 1) + "; restart container with {} image".format( DOCKERTAG ) )
                 startResult = main.ONOSbenchDocker.dockerRestartCT( ctName = NODElist[ct ] )
 
         utilities.assert_equals( expect = main.TRUE, actual = createResult and startResult,
@@ -126,6 +153,8 @@
         import time
         import json
 
+        main.case( "Form onos cluster and check status of onos apps for onos image {}".format( DOCKERTAG ) )
+
         startupSleep = int(main.params["SLEEP"]["startup"])
 
         appToAct = main.params["CASE110"]["apps"]
@@ -134,7 +163,7 @@
         main.log.info( "Wait for startup, sleep (sec): " + str(startupSleep))
         time.sleep(startupSleep)
 
-        main.step( "Check initial app states from onos1")
+        main.step( "Check initial app states from onos1 for onos image {}".format( DOCKERTAG ))
         stepResult = main.TRUE
         response = main.ONOSbenchRest.apps( ip=IPlist[0], port = 8181 )
         main.log.debug("Rest call response is: " + response)
@@ -167,9 +196,12 @@
         main.log.debug("Rest call response: " + str(status) + " - " + response)
         if status == 200:
             jrsp = json.loads(response)
-            clusterIP = [item["ip"]for item in jrsp["nodes"] if item["status"]== "READY"]
+            if DOCKERTAG == "1.2" or DOCKERTAG == "1.3" or DOCKERTAG == "1.4" or DOCKERTAG == "1.5":
+                clusterIP = [item["ip"]for item in jrsp["nodes"] if item["status"]== "ACTIVE"]
+            else:
+                clusterIP = [item["ip"]for item in jrsp["nodes"] if item["status"]== "READY"]
             main.log.debug(" IPlist is:" + ",".join(IPlist))
-            main.log.debug("cluster IP is" + ",".join(clusterIP) )
+            main.log.debug(" cluster IP is" + ",".join(clusterIP))
             if set(IPlist) == set(clusterIP): stepResult = main.TRUE
 
         utilities.assert_equals( expect = main.TRUE, actual = stepResult,
@@ -206,7 +238,7 @@
             time.sleep(5)
             appResults.append(appRslt)
             stepResult = stepResult and appRslt
-        main.log.debug("Apps activation result for " + ",".join(applist) + ": " + str(appResults) ) 
+        main.log.debug("Apps activation result for " + ",".join(applist) + ": " + str(appResults) )
         utilities.assert_equals( expect = main.TRUE, actual = stepResult,
                                     onpass = "Successfully activated apps",
                                     onfail = "Failed to activated apps correctly" )
@@ -241,7 +273,7 @@
         user = main.params["DOCKER"]["user"]
         pwd = main.params["DOCKER"]["password"]
 
-        main.case("onos Exceptions check")
+        main.case("onos Exceptions check with onos image {}".format( DOCKERTAG ))
         main.step("check onos for any exceptions")
 
         for ip in IPlist:
@@ -278,7 +310,7 @@
     def CASE1000( self, main ):
 
         """
-        Cleanup after tests - stop and delete the containers created; delete "onosproject/onos:latest image
+        Cleanup after tests - stop and delete the containers created; delete the image
         """
         import time
 
@@ -286,11 +318,11 @@
         main.step("Stop onos containers")
         stepResult = main.TRUE
         for ctname in NODElist:
-            if main.ONOSbenchDocker.dockerCheckCTName(ctName = "/" + ctname):
-                main.log.info( "stopping docker container: /" + ctname)
-                stopResult = main.ONOSbenchDocker.dockerStopCT( ctName = "/" + ctname )
+            if main.ONOSbenchDocker.dockerCheckCTName( ctName="/" + ctname ):
+                main.log.info( "stopping docker container: /" + ctname )
+                stopResult = main.ONOSbenchDocker.dockerStopCT( ctName="/" + ctname )
                 time.sleep(10)
-                rmResult = main.ONOSbenchDocker.dockerRemoveCT( ctName = "/" + ctname)
+                rmResult = main.ONOSbenchDocker.dockerRemoveCT( ctName="/" + ctname )
                 stepResult = stepResult and stopResult and rmResult
         utilities.assert_equals( expect = main.TRUE, actual = stepResult,
                                     onpass = "Container successfully stopped",
@@ -299,7 +331,7 @@
         #main.step( "remove exiting onosproject/onos images")
         #stepResult = main.ONOSbenchDocker.dockerRemoveImage( image = DOCKERREPO + ":" + DOCKERTAG )
         main.step( "remove dangling 'none:none' images")
-        stepResult = main.ONOSbenchDocker.dockerRemoveImage( image = "<none>:<none>" )
+        stepResult = main.ONOSbenchDocker.dockerRemoveImage()
         utilities.assert_equals( expect = main.TRUE, actual = stepResult,
                                     onpass = "Succeeded in cleaning up images",
                                     onfail = "Failed in cleaning up images" )