Basic Platform test suite using onos docker images; test  activate and deactivate apps

Change-Id: I731f5245883c2ffe6de87b41a613d7159d403b4d
diff --git a/TestON/drivers/common/api/dockerapidriver.py b/TestON/drivers/common/api/dockerapidriver.py
index 2630f6b..fc96682 100644
--- a/TestON/drivers/common/api/dockerapidriver.py
+++ b/TestON/drivers/common/api/dockerapidriver.py
@@ -5,6 +5,7 @@
 import re
 import subprocess
 from docker import Client
+from docker import errors
 from drivers.common.apidriver import API
 
 class DockerApiDriver( API ):
@@ -39,20 +40,23 @@
         except Exception as e:
             main.log.exception( e )
 
-    def dockerPull( self, image="onosproject/onos", tag="latest" ):
+    def dockerPull( self, onosRepo ="onosproject/onos", onosTag="latest" ):
         """
         Pulls Docker image from repository
         """
         try:
             main.log.info( self.name +
-                           ": Pulling Docker image " + image + ":"+tag )
-            for line in self.dockerClient.pull( repository=image, \
-                    tag=tag, stream=True ):
-                response = json.dumps( json.loads( line ), indent=4 )
-            if( re.search( "Downloaded", response ) ):
+                           ": Pulling Docker image " + onosRepo + ":"+ onosTag )
+            for line in self.dockerClient.pull( repository = onosRepo, \
+                    tag = onosTag, stream = True ):
+                print(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 )
                 return main.TRUE
             else:
-                main.log.error( "Failed to download image: " + image +":"+ tag )
+                main.log.error( "Failed to download image from: " + onosRepo +":"+ onosTag )
                 main.log.error( "Error respone: " )
                 main.log.error( response )
                 return main.FALSE
@@ -61,36 +65,39 @@
             main.cleanup()
             main.exit()
 
-    def dockerRun( self, image="onosproject/onos", node="onos1" ):
+    def dockerCreateCT( self, onosImage="onosproject/onos:latest", onosNode="onos1" ):
         """
-            Run specified Docker container
+            Create a Docker container with a specific image
         """
         try:
             main.log.info( self.name +
-                           ": Creating Docker conatiner for node " + node )
-            reponse = self.dockerClient.create_container( image=image, \
-                    tty=True, hostname=node, detach=True )
-            if( reponse['Warnings'] == 'None' ):
-                main.log.info( "Created container for node: " + node )
-                return main.TRUE
+                           ": Creating Docker container for node: " + onosNode )
+            response = self.dockerClient.create_container( image=onosImage, \
+                    tty=True, name=onosNode, detach=True )
+            #print response
+            #print response.get("Id")
+            #print response.get("Warnings")
+            if( str( response.get("Warnings") ) == 'None' ):
+                main.log.info( "Created container for node: " + onosNode + "; container id is: " + response.get("Id") )
+                return ( main.TRUE, response.get("Id") )
             else:
                 main.log.info( "Noticed warnings during create" )
-                return main.FALSE
+                return ( main.FALSE, null)
         except Exception:
             main.log.exception( self.name + ": Uncaught exception!" )
             main.cleanup()
             main.exit()
 
-    def dockerStart( self, node="onos1" ):
+    def dockerStartCT( self, ctID ):
         """
             Start Docker container
         """
         try:
             main.log.info( self.name +
-                           ": Starting Docker conatiner for node " + node )
-            reponse = self.dockerClient.start( node )
-            if( reponse == 'None' ):
-                main.log.info( "Started container for node: " + node )
+                           ": Starting Docker conatiner Id " + ctID )
+            response = self.dockerClient.start( container = ctID )
+            if response is None:
+                main.log.info( "Started container for Id: " + ctID )
                 return main.TRUE
             else:
                 main.log.info( "Noticed warnings during start" )
@@ -100,35 +107,38 @@
             main.cleanup()
             main.exit()
 
-    def dockerStop( self, node="onos1" ):
+    def dockerStopCT( self, ctName ):
         """
             Stop docker container
         """
         try:
             main.log.info( self.name +
-                           ": Stopping Docker conatiner for node " + node )
-            reponse = self.dockerClient.stop( node )
-            if( reponse == 'None' ):
-                main.log.info( "Stopped container for node: " + node )
+                           ": Stopping Docker conatiner for node " + ctName )
+            response = self.dockerClient.stop( ctName )
+            if response is None:
+                main.log.info( "Stopped container for node: " + ctName )
                 return main.TRUE
             else:
                 main.log.info( "Noticed warnings during stop" )
                 return main.FALSE
+        except errors.NotFound:
+            main.log.info( ctName + " not found! Continue on tests...")
+            return main.TRUE
         except Exception:
             main.log.exception( self.name + ": Uncaught exception!" )
-            main.cleanup()
-            main.exit()
+            #main.cleanup()
+            #main.exit()
 
-    def dockerRestart( self, node="onos1" ):
+    def dockerRestartCT( self, ctName ):
         """
             Restart Docker container
         """
         try:
             main.log.info( self.name +
-                           ": Restarting Docker conatiner for node " + node )
-            reponse = self.dockerClient.restart( node )
-            if( reponse == 'None' ):
-                main.log.info( "Restarted container for node: " + node )
+                           ": Restarting Docker conatiner for node " + ctName )
+            response = self.dockerClient.restart( ctName )
+            if response is None:
+                main.log.info( "Restarted container for node: " + ctName )
                 return main.TRUE
             else:
                 main.log.info( "Noticed warnings during Restart" )
@@ -138,62 +148,77 @@
             main.cleanup()
             main.exit()
 
-    def dockerStatus( self, node="onos1" ):
+    def dockerCheckCTName( self, ctName):
         """
             Check Docker conatiner status
         """
         try:
             main.log.info( self.name +
-                           ": Checking Docker Status for node " + node )
-            reponse = self.dockerClient.inspect_container( node )
-            if( reponse == 'True' ):
-                main.log.info( "Container " + node + " is running" )
+                           ": Checkeng Docker Status for CT with 'Names'  " + ctName )
+            namelist = [response["Names"] for response in self.dockerClient.containers(all=True) if not []]
+            main.log.info("Name list is: " + str(namelist) )
+            if( [ctName] in namelist):
+                main.log.info( "Container " + ctName + " exists" )
                 return main.TRUE
             else:
-                main.log.info( "Container " + node + " is not running" )
+                main.log.info( "Container " + ctName + " does not exist" )
                 return main.FALSE
+        except errors.NotFound:
+            main.log.warn( ctName + "not found! Continue with the tests...")
+            return main.FALSE
         except Exception:
-            main.log.exception( self.name + ": Uncaught exception!" )
-            main.cleanup()
-            main.exit()
+            main.log.exception( self.name + ": Uncaught exception! Continue tests..." )
+            #main.cleanup()
+            #main.exit()
 
-    def dockerRemove( self, node="onos1" ):
+    def dockerRemoveCT( self, ctName ):
         """
             Remove Docker conatiner
         """
         try:
             main.log.info( self.name +
-                           ": Removing Docker container for node " + node )
-            reponse = self.dockerClient.remove_container( node )
-            if( reponse == 'None' ):
-                main.log.info( "Removed container for node: " + node )
+                           ": Removing Docker container for node " + ctName )
+            response = self.dockerClient.remove_container( ctName, force=True )
+            if response is None:
+                main.log.info( "Removed container for node: " + ctName )
                 return main.TRUE
             else:
-                main.log.info( "Noticed warnings during Remove " + node )
+                main.log.info( "Noticed warnings during Remove " + ctName)
                 return main.FALSE
+            main.log.exception(self.name + ": not found, continuing...")
+        except errors.NotFound:
+            main.log.warn( ctName + "not found! Continue with the tests...")
+            return main.TRUE
         except Exception:
-            main.log.exception( self.name + ": Uncaught exception!" )
-            main.cleanup()
-            main.exit()
+            main.log.exception( self.name + ": Uncaught exception! Continuing..." )
+            #main.cleanup()
+            #main.exit()
 
-    def dockerImageRemove( self, image="onosproject/onos" ):
+    def dockerRemoveImage( self, image="onosproject/onos:latest" ):
         """
             Remove Docker image
         """
-        try:
-            main.log.info( self.name +
-                           ": Removing Docker container for node " + node )
-            reponse = self.dockerClient.remove_image( image )
-            if( reponse == 'None' ):
-                main.log.info( "Removed Docker image: " + image )
+        if self.dockerClient.images() is []: 
+            main.log.info( "no image found with RepoTags: " + image )
+            return main.TRUE
+        else:
+            try:
+                main.log.info( self.name +
+                           ": Removing Docker image " + image )
+                response = self.dockerClient.remove_image( image, force = True)
+                if response is None:
+                    main.log.info( "Removed Docker image: " + image )
+                    return main.TRUE
+                else:
+                    main.log.info( "Noticed warnings during Remove " + image )
+                    return main.FALSE
+            except errors.NotFound:
+                main.log.warn( image + "not found! Continue with the tests...")
                 return main.TRUE
-            else:
-                main.log.info( "Noticed warnings during Remove " + image )
-                return main.FALSE
-        except Exception:
-            main.log.exception( self.name + ": Uncaught exception!" )
-            main.cleanup()
-            main.exit()
+            except Exception:
+                main.log.exception( self.name + ": Uncaught exception! Continuing..." )
+                #main.cleanup()
+                #main.exit()
 
     def fetchLatestClusterFile( self, branch="master" ):
         """
@@ -211,32 +236,37 @@
             main.cleanup()
             main.exit()
 
-    def onosFormCluster( self, onosIPs, user="onos", passwd="rocks" ):
+    def onosFormCluster( self, onosIPs, cmdPath="~/OnosSystemTest/TestON/tests/PLATdockertest/Dependency", user="karaf", passwd="karaf" ):
         """
             From ONOS cluster for IP addresses in onosIPs list
         """
         try:
             onosIPs = " ".join(onosIPs)
-            command = "./onos-form-cluster -u " + user + " -p " + passwd + \
-                    " `" + onosIPs + "`"
-            subprocess.call( command ) # output checks are missing for now
-            return main.TRUE # FALSE condition will be verified from output
+            command = cmdPath + "/onos-form-cluster -u " + user + " -p " + passwd + \
+                    " " + onosIPs
+            result = subprocess.call( command, shell=True )
+            if result == 0:
+                return main.TRUE
+            else:
+                main.log.info("Something is not right in forming cluster>")
+                return main.FALSE
         except Exception:
             main.log.exception( self.name + ": Uncaught exception!" )
             main.cleanup()
             main.exit()
 
-    def dockerIP( self, node='onos1' ):
+    def dockerIP( self, ctName ):
         """
             Fetch IP address assigned to specified node/container
         """
         try:
-            output = self.dockerClient.inspect_container(node)
+            output = self.dockerClient.inspect_container( ctName )
             nodeIP = output['NetworkSettings']['IPAddress']
-            #main.log.info( " Docker IP " + str(nodeIP) )
+            main.log.info( " Docker IP " + str(nodeIP) )
             return str(nodeIP)
 
         except Exception:
             main.log.exception( self.name + ": Uncaught exception!" )
             main.cleanup()
             main.exit()
+