Devin Lim | 58046fa | 2017-07-05 16:55:00 -0700 | [diff] [blame^] | 1 | |
| 2 | import time |
| 3 | import re |
| 4 | import imp |
| 5 | |
| 6 | class ONOSSetup: |
| 7 | main = None |
| 8 | def __init__( self ): |
| 9 | self.default = '' |
| 10 | def envSetupDescription ( self ): |
| 11 | main.case( "Constructing test variables and building ONOS package" ) |
| 12 | main.step( "Constructing test variables" ) |
| 13 | main.caseExplanation = "For loading from params file, and pull" + \ |
| 14 | " and build the latest ONOS package" |
| 15 | main.testOnDirectory = re.sub( "(/tests)$", "", main.testDir ) |
| 16 | |
| 17 | def gitPulling( self ): |
| 18 | main.case( "Pull onos branch and build onos on Teststation." ) |
| 19 | gitPull = main.params[ 'GIT' ][ 'pull' ] |
| 20 | gitBranch = main.params[ 'GIT' ][ 'branch' ] |
| 21 | if gitPull == 'True': |
| 22 | main.step( "Git Checkout ONOS branch: " + gitBranch ) |
| 23 | stepResult = main.ONOSbench.gitCheckout( branch=gitBranch ) |
| 24 | utilities.assert_equals( expect=main.TRUE, |
| 25 | actual=stepResult, |
| 26 | onpass="Successfully checkout onos branch.", |
| 27 | onfail="Failed to checkout onos branch. Exiting test..." ) |
| 28 | if not stepResult: main.exit() |
| 29 | |
| 30 | main.step( "Git Pull on ONOS branch:" + gitBranch ) |
| 31 | stepResult = main.ONOSbench.gitPull() |
| 32 | utilities.assert_equals( expect=main.TRUE, |
| 33 | actual=stepResult, |
| 34 | onpass="Successfully pull onos. ", |
| 35 | onfail="Failed to pull onos. Exiting test ..." ) |
| 36 | if not stepResult: main.exit() |
| 37 | |
| 38 | else: |
| 39 | main.log.info( "Skipped git checkout and pull as they are disabled in params file" ) |
| 40 | |
| 41 | return main.TRUE |
| 42 | |
| 43 | def setRest( self, hasRest, i ): |
| 44 | if hasRest: |
| 45 | main.RESTs.append( getattr( main, "ONOSrest" + str( i ) ) ) |
| 46 | |
| 47 | def setNode( self, hasNode, i ): |
| 48 | if hasNode: |
| 49 | main.nodes.append( getattr( main, 'ONOS' + str(i)) ) |
| 50 | |
| 51 | def setCli( self, hasCli, i ): |
| 52 | if hasCli: |
| 53 | main.CLIs.append( getattr ( main, "ONOScli" + str( i ) ) ) |
| 54 | |
| 55 | def getNumNode( self, hasCli, hasNode, hasRest ): |
| 56 | if hasCli: |
| 57 | return len( main.CLIs ) |
| 58 | if hasNode: |
| 59 | return len( main.nodes ) |
| 60 | if hasRest: |
| 61 | return len( main.RESTs ) |
| 62 | |
| 63 | def envSetup ( self, hasMultiNodeRounds=False, hasRest=False, hasNode=False, |
| 64 | hasCli=True, specificIp="", includeGitPull=True, makeMaxNodes=True ): |
| 65 | if includeGitPull : |
| 66 | self.gitPulling() |
| 67 | if main.ONOSbench.maxNodes: |
| 68 | main.maxNodes = int( main.ONOSbench.maxNodes ) |
| 69 | else: |
| 70 | main.maxNodes = 0 |
| 71 | main.cellData = {} # For creating cell file |
| 72 | if hasCli: |
| 73 | main.CLIs = [] |
| 74 | if hasRest: |
| 75 | main.RESTs = [] |
| 76 | if hasNode: |
| 77 | main.nodes = [] |
| 78 | main.ONOSip = [] # List of IPs of active ONOS nodes. CASE 2 |
| 79 | |
| 80 | if specificIp == "": |
| 81 | if makeMaxNodes: |
| 82 | main.ONOSip = main.ONOSbench.getOnosIps() |
| 83 | else: |
| 84 | main.ONOSip.append( specificIp ) |
| 85 | |
| 86 | # Assigning ONOS cli handles to a list |
| 87 | try: |
| 88 | for i in range( 1, ( main.maxNodes if makeMaxNodes else main.numCtrls ) + 1 ): |
| 89 | self.setCli( hasCli, i ) |
| 90 | self.setRest( hasRest, i ) |
| 91 | self.setNode( hasNode, i ) |
| 92 | if not makeMaxNodes: |
| 93 | main.ONOSip.append( main.ONOSbench.getOnosIps()[ i - 1 ] ) |
| 94 | except AttributeError: |
| 95 | numNode = self.getNumNode( hasCli, hasNode, hasRest ) |
| 96 | main.log.warn( "A " + str( main.maxNodes ) + " node cluster " + |
| 97 | "was defined in env variables, but only " + |
| 98 | str( numNode ) + |
| 99 | " nodes were defined in the .topo file. " + |
| 100 | "Using " + str( numNode ) + |
| 101 | " nodes for the test." ) |
| 102 | main.maxNodes = numNode |
| 103 | |
| 104 | main.log.debug( "Found ONOS ips: {}".format( main.ONOSip ) ) |
| 105 | if ( not hasCli or main.CLIs ) and ( not hasRest or main.RESTs )\ |
| 106 | and ( not hasNode or main.nodes ): |
| 107 | return main.TRUE |
| 108 | else: |
| 109 | main.log.error( "Did not properly created list of ONOS CLI handle" ) |
| 110 | return main.FALSE |
| 111 | |
| 112 | def envSetupException ( self, e ): |
| 113 | main.log.exception( e ) |
| 114 | main.cleanup() |
| 115 | main.exit() |
| 116 | |
| 117 | def evnSetupConclusion ( self, stepResult ): |
| 118 | utilities.assert_equals( expect=main.TRUE, |
| 119 | actual=stepResult, |
| 120 | onpass="Successfully construct " + |
| 121 | "test variables ", |
| 122 | onfail="Failed to construct test variables" ) |
| 123 | |
| 124 | main.commit = main.ONOSbench.getVersion( report=True ) |
| 125 | |
| 126 | def getNumCtrls( self, hasMultiNodeRounds ): |
| 127 | if hasMultiNodeRounds: |
| 128 | try: |
| 129 | main.cycle |
| 130 | except Exception: |
| 131 | main.cycle = 0 |
| 132 | main.cycle += 1 |
| 133 | # main.scale[ 0 ] determines the current number of ONOS controller |
| 134 | main.numCtrls = int( main.scale.pop( 0 ) ) |
| 135 | else: |
| 136 | main.numCtrls = main.maxNodes |
| 137 | |
| 138 | def killingAllOnos( self, killRemoveMax, stopOnos ): |
| 139 | # kill off all onos processes |
| 140 | main.log.info( "Safety check, killing all ONOS processes" + |
| 141 | " before initiating environment setup" ) |
| 142 | |
| 143 | for i in range( main.maxNodes if killRemoveMax else main.numCtrls ): |
| 144 | if stopOnos: |
| 145 | main.ONOSbench.onosStop( main.ONOSip[ i ] ) |
| 146 | main.ONOSbench.onosKill( main.ONOSip[ i ] ) |
| 147 | |
| 148 | def createApplyCell( self, newCell, cellName, Mininet, useSSH ): |
| 149 | if newCell: |
| 150 | tempOnosIp = [] |
| 151 | for i in range( main.numCtrls ): |
| 152 | tempOnosIp.append( main.ONOSip[i] ) |
| 153 | |
| 154 | main.ONOSbench.createCellFile( main.ONOSbench.ip_address, |
| 155 | cellName, |
| 156 | Mininet if isinstance( Mininet, str ) else |
| 157 | Mininet.ip_address, |
| 158 | main.apps, |
| 159 | tempOnosIp, |
| 160 | main.ONOScli1.karafUser, |
| 161 | useSSH=useSSH ) |
| 162 | main.step( "Apply cell to environment" ) |
| 163 | cellResult = main.ONOSbench.setCell( cellName ) |
| 164 | verifyResult = main.ONOSbench.verifyCell() |
| 165 | stepResult = cellResult and verifyResult |
| 166 | utilities.assert_equals( expect=main.TRUE, |
| 167 | actual=stepResult, |
| 168 | onpass="Successfully applied cell to " + |
| 169 | "environment", |
| 170 | onfail="Failed to apply cell to environment " ) |
| 171 | return stepResult |
| 172 | |
| 173 | def uninstallOnos( self, killRemoveMax ): |
| 174 | main.step( "Uninstalling ONOS package" ) |
| 175 | onosUninstallResult = main.TRUE |
| 176 | for i in range( main.maxNodes if killRemoveMax else main.numCtrls ): |
| 177 | onosUninstallResult = onosUninstallResult and \ |
| 178 | main.ONOSbench.onosUninstall( nodeIp=main.ONOSip[ i ] ) |
| 179 | utilities.assert_equals( expect=main.TRUE, |
| 180 | actual=onosUninstallResult, |
| 181 | onpass="Successfully uninstalled ONOS package", |
| 182 | onfail="Failed to uninstall ONOS package" ) |
| 183 | return onosUninstallResult |
| 184 | |
| 185 | def buildOnos( self ): |
| 186 | main.step( "Creating ONOS package" ) |
| 187 | packageResult = main.ONOSbench.buckBuild() |
| 188 | utilities.assert_equals( expect=main.TRUE, |
| 189 | actual=packageResult, |
| 190 | onpass="Successfully created ONOS package", |
| 191 | onfail="Failed to create ONOS package" ) |
| 192 | return packageResult |
| 193 | |
| 194 | def installOnos( self, installMax ): |
| 195 | main.step( "Installing ONOS package" ) |
| 196 | onosInstallResult = main.TRUE |
| 197 | |
| 198 | for i in range( main.ONOSbench.maxNodes if installMax else main.numCtrls ): |
| 199 | options = "-f" |
| 200 | if installMax and i >= main.numCtrls: |
| 201 | options = "-nf" |
| 202 | onosInstallResult = onosInstallResult and \ |
| 203 | main.ONOSbench.onosInstall( node=main.ONOSip[ i ], options=options ) |
| 204 | utilities.assert_equals( expect=main.TRUE, |
| 205 | actual=onosInstallResult, |
| 206 | onpass="Successfully installed ONOS package", |
| 207 | onfail="Failed to install ONOS package" ) |
| 208 | if not onosInstallResult: |
| 209 | main.cleanup() |
| 210 | main.exit() |
| 211 | return onosInstallResult |
| 212 | |
| 213 | def setupSsh( self ): |
| 214 | main.step( "Set up ONOS secure SSH" ) |
| 215 | secureSshResult = main.TRUE |
| 216 | for i in range( main.numCtrls ): |
| 217 | secureSshResult = secureSshResult and main.ONOSbench.onosSecureSSH( node=main.ONOSip[ i ] ) |
| 218 | utilities.assert_equals( expect=main.TRUE, |
| 219 | actual=secureSshResult, |
| 220 | onpass="Test step PASS", |
| 221 | onfail="Test step FAIL" ) |
| 222 | return secureSshResult |
| 223 | |
| 224 | def checkOnosService( self ): |
| 225 | stopResult = main.TRUE |
| 226 | startResult = main.TRUE |
| 227 | onosIsUp = main.TRUE |
| 228 | main.step( "Starting ONOS service" ) |
| 229 | for i in range( main.numCtrls ): |
| 230 | onosIsUp = onosIsUp and main.ONOSbench.isup( main.ONOSip[ i ] ) |
| 231 | if onosIsUp == main.TRUE: |
| 232 | main.log.report( "ONOS instance {0} is up and ready".format( i + 1 ) ) |
| 233 | else: |
| 234 | main.log.report( "ONOS instance {0} may not be up, stop and ".format( i + 1 ) + |
| 235 | "start ONOS again " ) |
| 236 | stopResult = stopResult and main.ONOSbench.onosStop( main.ONOSip[ i ] ) |
| 237 | startResult = startResult and main.ONOSbench.onosStart( main.ONOSip[ i ] ) |
| 238 | if not startResult or stopResult: |
| 239 | main.log.report( "ONOS instance {0} did not start correctly.".format( i + 1 ) ) |
| 240 | stepResult = onosIsUp and stopResult and startResult |
| 241 | utilities.assert_equals( expect=main.TRUE, |
| 242 | actual=stepResult, |
| 243 | onpass="ONOS service is ready on all nodes", |
| 244 | onfail="ONOS service did not start properly on all nodes" ) |
| 245 | return stepResult |
| 246 | |
| 247 | def startOnosClis( self ): |
| 248 | startCliResult = main.TRUE |
| 249 | main.step( "Starting ONOS CLI sessions" ) |
| 250 | pool = [] |
| 251 | main.threadID = 0 |
| 252 | for i in range( main.numCtrls ): |
| 253 | t = main.Thread( target=main.CLIs[ i ].startOnosCli, |
| 254 | threadID=main.threadID, |
| 255 | name="startOnosCli-" + str( i ), |
| 256 | args=[ main.ONOSip[ i ] ] ) |
| 257 | pool.append( t ) |
| 258 | t.start() |
| 259 | main.threadID = main.threadID + 1 |
| 260 | for t in pool: |
| 261 | t.join() |
| 262 | startCliResult = startCliResult and t.result |
| 263 | if not startCliResult: |
| 264 | main.log.info( "ONOS CLI did not start up properly" ) |
| 265 | main.cleanup() |
| 266 | main.exit() |
| 267 | else: |
| 268 | main.log.info( "Successful CLI startup" ) |
| 269 | utilities.assert_equals( expect=main.TRUE, |
| 270 | actual=startCliResult, |
| 271 | onpass="Successfully start ONOS cli", |
| 272 | onfail="Failed to start ONOS cli" ) |
| 273 | return startCliResult |
| 274 | |
| 275 | def ONOSSetUp( self, Mininet, hasMultiNodeRounds=False, hasCli=True, newCell=True, |
| 276 | cellName="temp", removeLog=False, extraApply=None, arg=None, extraClean=None, |
| 277 | skipPack=False, installMax=False, useSSH=True, killRemoveMax=True, |
| 278 | CtrlsSet=True, stopOnos=False ): |
| 279 | if CtrlsSet: |
| 280 | self.getNumCtrls( hasMultiNodeRounds ) |
| 281 | |
| 282 | main.case( "Starting up " + str( main.numCtrls ) + |
| 283 | " node(s) ONOS cluster" ) |
| 284 | main.caseExplanation = "Set up ONOS with " + str( main.numCtrls ) + \ |
| 285 | " node(s) ONOS cluster" |
| 286 | self.killingAllOnos( killRemoveMax, stopOnos ) |
| 287 | |
| 288 | main.log.info( "NODE COUNT = " + str( main.numCtrls ) ) |
| 289 | cellResult = True |
| 290 | packageResult = True |
| 291 | onosUninstallResult = True |
| 292 | onosCliResult = True |
| 293 | if not skipPack: |
| 294 | cellResult = self.createApplyCell( newCell, cellName, Mininet, useSSH ) |
| 295 | |
| 296 | if removeLog: |
| 297 | main.log.info("Removing raft logs") |
| 298 | main.ONOSbench.onosRemoveRaftLogs() |
| 299 | |
| 300 | onosUninstallResult = self.uninstallOnos( killRemoveMax ) |
| 301 | |
| 302 | if extraApply is not None: |
| 303 | extraApply( metadataMethod=arg ) if arg is not None else extraApply() |
| 304 | |
| 305 | packageResult = self.buildOnos() |
| 306 | |
| 307 | onosInstallResult = self.installOnos( installMax ) |
| 308 | |
| 309 | if extraClean is not None: |
| 310 | extraClean() |
| 311 | secureSshResult = True |
| 312 | if useSSH: |
| 313 | secureSshResult = self.setupSsh() |
| 314 | |
| 315 | onosServiceResult = self.checkOnosService() |
| 316 | |
| 317 | if hasCli: |
| 318 | onosCliResult = self.startOnosClis() |
| 319 | |
| 320 | return cellResult and packageResult and onosUninstallResult and \ |
| 321 | onosInstallResult and secureSshResult and onosServiceResult and onosCliResult |