Jon Hall | ca31989 | 2017-06-15 15:25:22 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | """ |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 3 | Copyright 2017 Open Networking Foundation (ONF) |
Jon Hall | ca31989 | 2017-06-15 15:25:22 -0700 | [diff] [blame] | 4 | |
| 5 | Please refer questions to either the onos test mailing list at <onos-test@onosproject.org>, |
| 6 | the System Testing Plans and Results wiki page at <https://wiki.onosproject.org/x/voMg>, |
| 7 | or the System Testing Guide page at <https://wiki.onosproject.org/x/WYQg> |
| 8 | |
| 9 | TestON is free software: you can redistribute it and/or modify |
| 10 | it under the terms of the GNU General Public License as published by |
| 11 | the Free Software Foundation, either version 2 of the License, or |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 12 | (at your option) any later version. |
Jon Hall | ca31989 | 2017-06-15 15:25:22 -0700 | [diff] [blame] | 13 | |
| 14 | TestON is distributed in the hope that it will be useful, |
| 15 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | GNU General Public License for more details. |
| 18 | |
| 19 | You should have received a copy of the GNU General Public License |
| 20 | along with TestON. If not, see <http://www.gnu.org/licenses/>. |
| 21 | |
| 22 | |
| 23 | This driver is used to interact with an ONOS cluster. It should |
| 24 | handle creating the necessary components to interact with each specific ONOS nodes. |
| 25 | |
| 26 | Please refer questions to either the onos test mailing list at <onos-test@onosproject.org>, |
| 27 | the System Testing Plans and Results wiki page at <https://wiki.onosproject.org/x/voMg>, |
| 28 | or the System Testing Guide page at <https://wiki.onosproject.org/x/WYQg> |
| 29 | |
| 30 | """ |
| 31 | import pexpect |
| 32 | import os |
| 33 | from drivers.common.clidriver import CLI |
| 34 | |
| 35 | # FIXME: Move this to it's own file? |
| 36 | class Controller(): |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 37 | |
Jon Hall | ca31989 | 2017-06-15 15:25:22 -0700 | [diff] [blame] | 38 | def __str__( self ): |
| 39 | return self.name |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 40 | |
Jon Hall | ca31989 | 2017-06-15 15:25:22 -0700 | [diff] [blame] | 41 | def __repr__( self ): |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 42 | # TODO use repr() for components? |
Jon Hall | ca31989 | 2017-06-15 15:25:22 -0700 | [diff] [blame] | 43 | return "%s<IP=%s, CLI=%s, REST=%s, Bench=%s >" % ( self.name, |
Jon Hall | 4173b24 | 2017-09-12 17:04:38 -0700 | [diff] [blame] | 44 | self.ipAddress, |
| 45 | self.CLI, |
| 46 | self.REST, |
| 47 | self.Bench ) |
Jon Hall | ca31989 | 2017-06-15 15:25:22 -0700 | [diff] [blame] | 48 | |
| 49 | def __getattr__( self, name ): |
| 50 | """ |
| 51 | Called when an attribute lookup has not found the attribute |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 52 | in the usual places (i.e. it is not an instance attribute nor |
| 53 | is it found in the class tree for self). name is the attribute |
| 54 | name. This method should return the (computed) attribute value |
Jon Hall | ca31989 | 2017-06-15 15:25:22 -0700 | [diff] [blame] | 55 | or raise an AttributeError exception. |
| 56 | |
| 57 | We will look into each of the node's component handles to try to find the attreibute, looking at REST first |
| 58 | """ |
| 59 | if hasattr( self.REST, name ): |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 60 | main.log.debug( "%s: Using Rest driver's attribute for '%s'" % ( self.name, name ) ) |
| 61 | return getattr( self.REST, name ) |
Jon Hall | ca31989 | 2017-06-15 15:25:22 -0700 | [diff] [blame] | 62 | if hasattr( self.CLI, name ): |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 63 | main.log.debug( "%s: Using CLI driver's attribute for '%s'" % ( self.name, name ) ) |
| 64 | return getattr( self.CLI, name ) |
Jon Hall | ca31989 | 2017-06-15 15:25:22 -0700 | [diff] [blame] | 65 | if hasattr( self.Bench, name ): |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 66 | main.log.debug( "%s: Using Bench driver's attribute for '%s'" % ( self.name, name ) ) |
| 67 | return getattr( self.Bench, name ) |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 68 | raise AttributeError( "Could not find the attribute %s in %r or it's component handles" % ( name, self ) ) |
Jon Hall | ca31989 | 2017-06-15 15:25:22 -0700 | [diff] [blame] | 69 | |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 70 | def __init__( self, name, ipAddress, CLI=None, REST=None, Bench=None, pos=None, |
Jon Hall | 06fd0df | 2021-01-25 15:50:06 -0800 | [diff] [blame] | 71 | userName=None, server=None, k8s=None, dockerPrompt=None ): |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 72 | # TODO: validate these arguments |
Jon Hall | ca31989 | 2017-06-15 15:25:22 -0700 | [diff] [blame] | 73 | self.name = str( name ) |
| 74 | self.ipAddress = ipAddress |
| 75 | self.CLI = CLI |
| 76 | self.REST = REST |
| 77 | self.Bench = Bench |
| 78 | self.active = False |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 79 | self.pos = pos |
| 80 | self.ip_address = ipAddress |
| 81 | self.user_name = userName |
Jon Hall | 4173b24 | 2017-09-12 17:04:38 -0700 | [diff] [blame] | 82 | self.server = server |
Jon Hall | 06fd0df | 2021-01-25 15:50:06 -0800 | [diff] [blame] | 83 | self.k8s = k8s |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 84 | self.dockerPrompt = dockerPrompt |
Jon Hall | ca31989 | 2017-06-15 15:25:22 -0700 | [diff] [blame] | 85 | |
| 86 | class OnosClusterDriver( CLI ): |
| 87 | |
| 88 | def __init__( self ): |
| 89 | """ |
| 90 | Initialize client |
| 91 | """ |
| 92 | self.name = None |
| 93 | self.home = None |
| 94 | self.handle = None |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 95 | self.useDocker = False |
| 96 | self.dockerPrompt = None |
Jon Hall | e37bd1f | 2020-09-10 12:16:41 -0700 | [diff] [blame] | 97 | self.maxNodes = None |
Siddesh | 5275062 | 2021-03-05 19:52:03 +0000 | [diff] [blame] | 98 | self.karafPromptPass = None |
Jon Hall | 06fd0df | 2021-01-25 15:50:06 -0800 | [diff] [blame] | 99 | self.kubeConfig = None |
Siddesh | 5275062 | 2021-03-05 19:52:03 +0000 | [diff] [blame] | 100 | self.karafPromptUser = None |
Jon Hall | 3a03cad | 2021-04-07 11:21:55 -0700 | [diff] [blame] | 101 | self.nodeUser = None |
| 102 | self.nodePass = None |
Jon Hall | ca31989 | 2017-06-15 15:25:22 -0700 | [diff] [blame] | 103 | self.nodes = [] |
| 104 | super( OnosClusterDriver, self ).__init__() |
| 105 | |
Jon Hall | ca31989 | 2017-06-15 15:25:22 -0700 | [diff] [blame] | 106 | def connect( self, **connectargs ): |
| 107 | """ |
| 108 | Creates ssh handle for ONOS "bench". |
| 109 | NOTE: |
| 110 | The ip_address would come from the topo file using the host tag, the |
| 111 | value can be an environment variable as well as a "localhost" to get |
| 112 | the ip address needed to ssh to the "bench" |
| 113 | """ |
| 114 | try: |
| 115 | for key in connectargs: |
| 116 | vars( self )[ key ] = connectargs[ key ] |
| 117 | self.home = "~/onos" |
| 118 | for key in self.options: |
| 119 | if key == "home": |
Jon Hall | e37bd1f | 2020-09-10 12:16:41 -0700 | [diff] [blame] | 120 | self.home = self.options[ key ] |
Jon Hall | ca31989 | 2017-06-15 15:25:22 -0700 | [diff] [blame] | 121 | elif key == "karaf_username": |
| 122 | self.karafUser = self.options[ key ] |
| 123 | elif key == "karaf_password": |
| 124 | self.karafPass = self.options[ key ] |
Siddesh | 5275062 | 2021-03-05 19:52:03 +0000 | [diff] [blame] | 125 | elif key == "node_username": |
| 126 | self.nodeUser = self.options[ key ] |
| 127 | elif key == "node_password": |
| 128 | self.nodePass = self.options[ key ] |
| 129 | elif key == "karafPrompt_username": |
| 130 | self.karafPromptUser = self.options[ key ] |
| 131 | elif key == "karafPrompt_password": |
| 132 | self.karafPromptPass = self.options[ key ] |
Jon Hall | ca31989 | 2017-06-15 15:25:22 -0700 | [diff] [blame] | 133 | elif key == "cluster_name": |
| 134 | prefix = self.options[ key ] |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 135 | elif key == "useDocker": |
| 136 | self.useDocker = "True" == self.options[ key ] |
| 137 | elif key == "docker_prompt": |
| 138 | self.dockerPrompt = self.options[ key ] |
Jon Hall | 9b0de1f | 2020-08-24 15:38:04 -0700 | [diff] [blame] | 139 | elif key == "web_user": |
| 140 | self.webUser = self.options[ key ] |
| 141 | elif key == "web_pass": |
| 142 | self.webPass = self.options[ key ] |
Jon Hall | e37bd1f | 2020-09-10 12:16:41 -0700 | [diff] [blame] | 143 | elif key == "nodes": |
| 144 | # Maximum number of ONOS nodes to run, if there is any |
| 145 | self.maxNodes = self.options[ key ] |
Jon Hall | 06fd0df | 2021-01-25 15:50:06 -0800 | [diff] [blame] | 146 | elif key == "kubeConfig": |
| 147 | self.kubeConfig = self.options[ key ] |
Jon Hall | ca31989 | 2017-06-15 15:25:22 -0700 | [diff] [blame] | 148 | |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 149 | self.home = self.checkOptions( self.home, "~/onos" ) |
| 150 | self.karafUser = self.checkOptions( self.karafUser, self.user_name ) |
| 151 | self.karafPass = self.checkOptions( self.karafPass, self.pwd ) |
Siddesh | 5275062 | 2021-03-05 19:52:03 +0000 | [diff] [blame] | 152 | self.nodeUser = self.checkOptions( self.nodeUser, self.user_name ) |
| 153 | self.nodePass = self.checkOptions( self.nodePass, self.pwd ) |
| 154 | self.karafPromptUser = self.checkOptions( self.karafPromptUser, self.user_name ) |
| 155 | self.karafPromptPass = self.checkOptions( self.karafPromptPass, self.pwd ) |
Jon Hall | 9b0de1f | 2020-08-24 15:38:04 -0700 | [diff] [blame] | 156 | self.webUser = self.checkOptions( self.webUser, "onos" ) |
| 157 | self.webPass = self.checkOptions( self.webPass, "rocks" ) |
Jon Hall | ca31989 | 2017-06-15 15:25:22 -0700 | [diff] [blame] | 158 | prefix = self.checkOptions( prefix, "ONOS" ) |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 159 | self.useDocker = self.checkOptions( self.useDocker, False ) |
| 160 | self.dockerPrompt = self.checkOptions( self.dockerPrompt, "~/onos#" ) |
Jon Hall | e37bd1f | 2020-09-10 12:16:41 -0700 | [diff] [blame] | 161 | self.maxNodes = int( self.checkOptions( self.maxNodes, 100 ) ) |
Jon Hall | 06fd0df | 2021-01-25 15:50:06 -0800 | [diff] [blame] | 162 | self.kubeConfig = self.checkOptions( self.kubeConfig, None ) |
Jon Hall | ca31989 | 2017-06-15 15:25:22 -0700 | [diff] [blame] | 163 | |
| 164 | self.name = self.options[ 'name' ] |
| 165 | |
Jon Hall | ca31989 | 2017-06-15 15:25:22 -0700 | [diff] [blame] | 166 | |
Jon Hall | 06fd0df | 2021-01-25 15:50:06 -0800 | [diff] [blame] | 167 | if not self.kubeConfig: |
| 168 | # Grabs all OC environment variables based on max number of nodes |
| 169 | # TODO: Also support giving an ip range as a compononet option |
| 170 | self.onosIps = {} # Dictionary of all possible ONOS ip |
| 171 | |
| 172 | try: |
| 173 | if self.maxNodes: |
| 174 | for i in range( self.maxNodes ): |
| 175 | envString = "OC" + str( i + 1 ) |
| 176 | # If there is no more OC# then break the loop |
| 177 | if os.getenv( envString ): |
| 178 | self.onosIps[ envString ] = os.getenv( envString ) |
| 179 | else: |
| 180 | self.maxNodes = len( self.onosIps ) |
| 181 | main.log.info( self.name + |
| 182 | ": Created cluster data with " + |
| 183 | str( self.maxNodes ) + |
| 184 | " maximum number" + |
| 185 | " of nodes" ) |
| 186 | break |
| 187 | |
| 188 | if not self.onosIps: |
| 189 | main.log.info( "Could not read any environment variable" |
| 190 | + " please load a cell file with all" + |
| 191 | " onos IP" ) |
| 192 | self.maxNodes = None |
Jon Hall | ca31989 | 2017-06-15 15:25:22 -0700 | [diff] [blame] | 193 | else: |
Jon Hall | 06fd0df | 2021-01-25 15:50:06 -0800 | [diff] [blame] | 194 | main.log.info( self.name + ": Found " + |
| 195 | str( self.onosIps.values() ) + |
| 196 | " ONOS IPs" ) |
| 197 | except KeyError: |
| 198 | main.log.info( "Invalid environment variable" ) |
| 199 | except Exception as inst: |
| 200 | main.log.error( "Uncaught exception: " + str( inst ) ) |
Jon Hall | ca31989 | 2017-06-15 15:25:22 -0700 | [diff] [blame] | 201 | try: |
| 202 | if os.getenv( str( self.ip_address ) ) is not None: |
| 203 | self.ip_address = os.getenv( str( self.ip_address ) ) |
| 204 | else: |
| 205 | main.log.info( self.name + |
| 206 | ": Trying to connect to " + |
| 207 | self.ip_address ) |
| 208 | except KeyError: |
| 209 | main.log.info( "Invalid host name," + |
| 210 | " connecting to local host instead" ) |
| 211 | self.ip_address = 'localhost' |
| 212 | except Exception as inst: |
| 213 | main.log.error( "Uncaught exception: " + str( inst ) ) |
| 214 | |
| 215 | self.handle = super( OnosClusterDriver, self ).connect( |
| 216 | user_name=self.user_name, |
| 217 | ip_address=self.ip_address, |
| 218 | port=self.port, |
| 219 | pwd=self.pwd, |
| 220 | home=self.home ) |
| 221 | |
| 222 | if self.handle: |
| 223 | self.handle.sendline( "cd " + self.home ) |
| 224 | self.handle.expect( "\$" ) |
Jon Hall | 06fd0df | 2021-01-25 15:50:06 -0800 | [diff] [blame] | 225 | if self.kubeConfig: |
| 226 | # Try to get # of onos nodes using given kubernetes configuration |
| 227 | names = self.kubectlGetPodNames( self.kubeConfig, |
| 228 | main.params[ 'kubernetes' ][ 'namespace' ], |
| 229 | main.params[ 'kubernetes' ][ 'appName' ] ) |
| 230 | self.podNames = names |
| 231 | self.onosIps = {} # Dictionary of all possible ONOS ip |
| 232 | for i in range( 1, len( names ) + 1 ): |
| 233 | self.onosIps[ 'OC%i' % i ] = self.ip_address |
| 234 | self.maxNodes = len( names ) |
Jon Hall | ca31989 | 2017-06-15 15:25:22 -0700 | [diff] [blame] | 235 | self.createComponents( prefix=prefix ) |
Jon Hall | 06fd0df | 2021-01-25 15:50:06 -0800 | [diff] [blame] | 236 | if self.kubeConfig: |
| 237 | # Create Port Forwarding sessions for each controller |
| 238 | for node in self.nodes: |
| 239 | kubectl = node.k8s |
| 240 | index = self.nodes.index( node ) |
| 241 | # Store each pod name in the k8s component |
| 242 | kubectl.podName = self.podNames[ index ] |
| 243 | # Setup port-forwarding and save the local port |
| 244 | guiPort = 8181 |
| 245 | cliPort = 8101 |
| 246 | portsList = "" |
| 247 | for port in [ guiPort, cliPort ]: |
| 248 | localPort = port + index + 1 |
| 249 | portsList += "%s:%s " % ( localPort, port ) |
| 250 | if port == cliPort: |
| 251 | node.CLI.karafPort = localPort |
Jon Hall | 50a0001 | 2021-03-08 11:06:11 -0800 | [diff] [blame] | 252 | elif port == guiPort: |
| 253 | node.REST.port = localPort |
Jon Hall | 06fd0df | 2021-01-25 15:50:06 -0800 | [diff] [blame] | 254 | main.log.info( "Setting up port forward for pod %s: [ %s ]" % ( self.podNames[ index ], portsList ) ) |
| 255 | pf = kubectl.kubectlPortForward( self.podNames[ index ], |
| 256 | portsList, |
| 257 | kubectl.kubeConfig, |
| 258 | main.params[ 'kubernetes' ][ 'namespace' ] ) |
| 259 | if not pf: |
| 260 | main.log.error( "Failed to create port forwarding" ) |
| 261 | return main.FALSE |
Jon Hall | ca31989 | 2017-06-15 15:25:22 -0700 | [diff] [blame] | 262 | return self.handle |
| 263 | else: |
| 264 | main.log.info( "Failed to create ONOS handle" ) |
| 265 | return main.FALSE |
| 266 | except pexpect.EOF: |
| 267 | main.log.error( self.name + ": EOF exception found" ) |
| 268 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 269 | main.cleanAndExit() |
Jon Hall | ca31989 | 2017-06-15 15:25:22 -0700 | [diff] [blame] | 270 | except Exception: |
| 271 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 272 | main.cleanAndExit() |
Jon Hall | ca31989 | 2017-06-15 15:25:22 -0700 | [diff] [blame] | 273 | |
| 274 | def disconnect( self ): |
| 275 | """ |
| 276 | Called when Test is complete to disconnect the ONOS handle. |
| 277 | """ |
| 278 | response = main.TRUE |
| 279 | try: |
| 280 | if self.handle: |
| 281 | self.handle.sendline( "" ) |
| 282 | self.handle.expect( "\$" ) |
| 283 | self.handle.sendline( "exit" ) |
| 284 | self.handle.expect( "closed" ) |
| 285 | except pexpect.EOF: |
| 286 | main.log.error( self.name + ": EOF exception found" ) |
| 287 | main.log.error( self.name + ": " + self.handle.before ) |
| 288 | except ValueError: |
| 289 | main.log.exception( "Exception in disconnect of " + self.name ) |
| 290 | response = main.TRUE |
| 291 | except Exception: |
| 292 | main.log.exception( self.name + ": Connection failed to the host" ) |
| 293 | response = main.FALSE |
| 294 | return response |
| 295 | |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 296 | def setCliOptions( self, name, host ): |
Jon Hall | ca31989 | 2017-06-15 15:25:22 -0700 | [diff] [blame] | 297 | """ |
| 298 | Parse the cluster options to create an ONOS cli component with the given name |
| 299 | """ |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 300 | main.componentDictionary[name] = main.componentDictionary[self.name].copy() |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 301 | clihost = main.componentDictionary[ name ][ 'COMPONENTS' ].get( "diff_clihost", "" ) |
| 302 | if clihost == "True": |
| 303 | main.componentDictionary[ name ][ 'host' ] = host |
Jon Hall | 06fd0df | 2021-01-25 15:50:06 -0800 | [diff] [blame] | 304 | home = main.componentDictionary[name]['COMPONENTS'].get( "onos_home", None ) |
| 305 | main.componentDictionary[name]['home'] = self.checkOptions( home, None ) |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 306 | main.componentDictionary[name]['type'] = "OnosCliDriver" |
Siddesh | 5275062 | 2021-03-05 19:52:03 +0000 | [diff] [blame] | 307 | main.componentDictionary[name]['COMPONENTS']['karafPromptUser'] = self.karafPromptUser |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 308 | main.componentDictionary[name]['connect_order'] = str( int( main.componentDictionary[name]['connect_order'] ) + 1 ) |
Jon Hall | ca31989 | 2017-06-15 15:25:22 -0700 | [diff] [blame] | 309 | |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 310 | def createCliComponent( self, name, host ): |
Jon Hall | ca31989 | 2017-06-15 15:25:22 -0700 | [diff] [blame] | 311 | """ |
| 312 | Creates a new onos cli component. |
| 313 | |
| 314 | Arguments: |
| 315 | name - The string of the name of this component. The new component |
| 316 | will be assigned to main.<name> . |
| 317 | In addition, main.<name>.name = str( name ) |
| 318 | """ |
| 319 | try: |
| 320 | # look to see if this component already exists |
| 321 | getattr( main, name ) |
| 322 | except AttributeError: |
| 323 | # namespace is clear, creating component |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 324 | self.setCliOptions( name, host ) |
Jon Hall | ca31989 | 2017-06-15 15:25:22 -0700 | [diff] [blame] | 325 | return main.componentInit( name ) |
| 326 | except pexpect.EOF: |
| 327 | main.log.error( self.name + ": EOF exception found" ) |
| 328 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 329 | main.cleanAndExit() |
Jon Hall | ca31989 | 2017-06-15 15:25:22 -0700 | [diff] [blame] | 330 | except Exception: |
| 331 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 332 | main.cleanAndExit() |
Jon Hall | ca31989 | 2017-06-15 15:25:22 -0700 | [diff] [blame] | 333 | else: |
| 334 | # namespace is not clear! |
| 335 | main.log.error( name + " component already exists!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 336 | main.cleanAndExit() |
Jon Hall | ca31989 | 2017-06-15 15:25:22 -0700 | [diff] [blame] | 337 | |
| 338 | def setRestOptions( self, name, host ): |
| 339 | """ |
| 340 | Parse the cluster options to create an ONOS cli component with the given name |
| 341 | """ |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 342 | main.componentDictionary[name] = main.componentDictionary[self.name].copy() |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 343 | user = main.componentDictionary[name]['COMPONENTS'].get( "web_user", "onos" ) |
| 344 | main.componentDictionary[name]['user'] = self.checkOptions( user, "onos" ) |
| 345 | password = main.componentDictionary[name]['COMPONENTS'].get( "web_pass", "rocks" ) |
You Wang | bef7ea1 | 2018-09-21 13:15:12 -0700 | [diff] [blame] | 346 | main.componentDictionary[name]['password'] = self.checkOptions( password, "rocks" ) |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 347 | main.componentDictionary[name]['host'] = host |
| 348 | port = main.componentDictionary[name]['COMPONENTS'].get( "rest_port", "8181" ) |
| 349 | main.componentDictionary[name]['port'] = self.checkOptions( port, "8181" ) |
| 350 | main.componentDictionary[name]['type'] = "OnosRestDriver" |
| 351 | main.componentDictionary[name]['connect_order'] = str( int( main.componentDictionary[name]['connect_order'] ) + 1 ) |
Jon Hall | ca31989 | 2017-06-15 15:25:22 -0700 | [diff] [blame] | 352 | |
| 353 | def createRestComponent( self, name, ipAddress ): |
| 354 | """ |
| 355 | Creates a new onos rest component. |
| 356 | |
| 357 | Arguments: |
| 358 | name - The string of the name of this component. The new component |
| 359 | will be assigned to main.<name> . |
| 360 | In addition, main.<name>.name = str( name ) |
| 361 | """ |
| 362 | try: |
| 363 | # look to see if this component already exists |
| 364 | getattr( main, name ) |
| 365 | except AttributeError: |
| 366 | # namespace is clear, creating component |
| 367 | self.setRestOptions( name, ipAddress ) |
| 368 | return main.componentInit( name ) |
| 369 | except pexpect.EOF: |
| 370 | main.log.error( self.name + ": EOF exception found" ) |
| 371 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 372 | main.cleanAndExit() |
Jon Hall | ca31989 | 2017-06-15 15:25:22 -0700 | [diff] [blame] | 373 | except Exception: |
| 374 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 375 | main.cleanAndExit() |
Jon Hall | ca31989 | 2017-06-15 15:25:22 -0700 | [diff] [blame] | 376 | else: |
| 377 | # namespace is not clear! |
| 378 | main.log.error( name + " component already exists!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 379 | main.cleanAndExit() |
Jon Hall | ca31989 | 2017-06-15 15:25:22 -0700 | [diff] [blame] | 380 | |
| 381 | def setBenchOptions( self, name ): |
| 382 | """ |
| 383 | Parse the cluster options to create an ONOS "bench" component with the given name |
| 384 | """ |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 385 | main.componentDictionary[name] = main.componentDictionary[self.name].copy() |
| 386 | main.componentDictionary[name]['type'] = "OnosDriver" |
| 387 | home = main.componentDictionary[name]['COMPONENTS'].get( "onos_home", None ) |
| 388 | main.componentDictionary[name]['home'] = self.checkOptions( home, None ) |
| 389 | main.componentDictionary[name]['connect_order'] = str( int( main.componentDictionary[name]['connect_order'] ) + 1 ) |
Jon Hall | ca31989 | 2017-06-15 15:25:22 -0700 | [diff] [blame] | 390 | |
| 391 | def createBenchComponent( self, name ): |
| 392 | """ |
| 393 | Creates a new onos "bench" component. |
| 394 | |
| 395 | Arguments: |
| 396 | name - The string of the name of this component. The new component |
| 397 | will be assigned to main.<name> . |
| 398 | In addition, main.<name>.name = str( name ) |
| 399 | """ |
| 400 | try: |
| 401 | # look to see if this component already exists |
| 402 | getattr( main, name ) |
| 403 | except AttributeError: |
| 404 | # namespace is clear, creating component |
| 405 | self.setBenchOptions( name ) |
| 406 | return main.componentInit( name ) |
| 407 | except pexpect.EOF: |
| 408 | main.log.error( self.name + ": EOF exception found" ) |
| 409 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 410 | main.cleanAndExit() |
Jon Hall | ca31989 | 2017-06-15 15:25:22 -0700 | [diff] [blame] | 411 | except Exception: |
| 412 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 413 | main.cleanAndExit() |
Jon Hall | ca31989 | 2017-06-15 15:25:22 -0700 | [diff] [blame] | 414 | else: |
| 415 | # namespace is not clear! |
| 416 | main.log.error( name + " component already exists!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 417 | main.cleanAndExit() |
Jon Hall | ca31989 | 2017-06-15 15:25:22 -0700 | [diff] [blame] | 418 | |
Jon Hall | 4173b24 | 2017-09-12 17:04:38 -0700 | [diff] [blame] | 419 | def setServerOptions( self, name, ipAddress ): |
| 420 | """ |
| 421 | Parse the cluster options to create an ONOS "server" component with the given name |
| 422 | |
| 423 | Arguments: |
| 424 | name - The name of the server componet |
| 425 | ipAddress - The ip address of the server |
| 426 | """ |
| 427 | main.componentDictionary[name] = main.componentDictionary[self.name].copy() |
| 428 | main.componentDictionary[name]['type'] = "OnosDriver" |
| 429 | main.componentDictionary[name]['host'] = ipAddress |
| 430 | home = main.componentDictionary[name]['COMPONENTS'].get( "onos_home", None ) |
| 431 | main.componentDictionary[name]['home'] = self.checkOptions( home, None ) |
You Wang | 4cc6191 | 2018-08-28 10:10:58 -0700 | [diff] [blame] | 432 | # TODO: for now we use karaf user name and password also for logging to the onos nodes |
Jon Hall | 06fd0df | 2021-01-25 15:50:06 -0800 | [diff] [blame] | 433 | # FIXME: We shouldn't use karaf* for this, what we want is another set of variables to |
| 434 | # login to a shell on the server ONOS is running on |
Siddesh | 5275062 | 2021-03-05 19:52:03 +0000 | [diff] [blame] | 435 | main.componentDictionary[name]['user'] = self.nodeUser |
| 436 | main.componentDictionary[name]['password'] = self.nodePass |
Jon Hall | 4173b24 | 2017-09-12 17:04:38 -0700 | [diff] [blame] | 437 | main.componentDictionary[name]['connect_order'] = str( int( main.componentDictionary[name]['connect_order'] ) + 1 ) |
Jon Hall | 4173b24 | 2017-09-12 17:04:38 -0700 | [diff] [blame] | 438 | |
Jon Hall | 4173b24 | 2017-09-12 17:04:38 -0700 | [diff] [blame] | 439 | def createServerComponent( self, name, ipAddress ): |
| 440 | """ |
| 441 | Creates a new onos "server" component. This will be connected to the |
| 442 | node ONOS is running on. |
| 443 | |
| 444 | Arguments: |
| 445 | name - The string of the name of this component. The new component |
| 446 | will be assigned to main.<name> . |
| 447 | In addition, main.<name>.name = str( name ) |
| 448 | ipAddress - The ip address of the server |
| 449 | """ |
| 450 | try: |
| 451 | # look to see if this component already exists |
| 452 | getattr( main, name ) |
| 453 | except AttributeError: |
| 454 | # namespace is clear, creating component |
| 455 | self.setServerOptions( name, ipAddress ) |
| 456 | return main.componentInit( name ) |
| 457 | except pexpect.EOF: |
| 458 | main.log.error( self.name + ": EOF exception found" ) |
| 459 | main.log.error( self.name + ": " + self.handle.before ) |
| 460 | main.cleanAndExit() |
| 461 | except Exception: |
| 462 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 463 | main.cleanAndExit() |
| 464 | else: |
| 465 | # namespace is not clear! |
| 466 | main.log.error( name + " component already exists!" ) |
| 467 | main.cleanAndExit() |
| 468 | |
Jon Hall | 4173b24 | 2017-09-12 17:04:38 -0700 | [diff] [blame] | 469 | def createComponents( self, prefix='', createServer=True ): |
Jon Hall | ca31989 | 2017-06-15 15:25:22 -0700 | [diff] [blame] | 470 | """ |
| 471 | Creates a CLI and REST component for each nodes in the cluster |
| 472 | """ |
| 473 | # TODO: This needs work to support starting two seperate clusters in one test |
| 474 | cliPrefix = prefix + "cli" |
| 475 | restPrefix = prefix + "rest" |
| 476 | benchPrefix = prefix + "bench" |
Jon Hall | 4173b24 | 2017-09-12 17:04:38 -0700 | [diff] [blame] | 477 | serverPrefix = prefix + "server" |
Jon Hall | 06fd0df | 2021-01-25 15:50:06 -0800 | [diff] [blame] | 478 | k8sPrefix = prefix + "k8s" |
Jon Hall | ca31989 | 2017-06-15 15:25:22 -0700 | [diff] [blame] | 479 | for i in xrange( 1, self.maxNodes + 1 ): |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 480 | cliName = cliPrefix + str( i ) |
Jon Hall | ca31989 | 2017-06-15 15:25:22 -0700 | [diff] [blame] | 481 | restName = restPrefix + str( i ) |
| 482 | benchName = benchPrefix + str( i ) |
Jon Hall | 4173b24 | 2017-09-12 17:04:38 -0700 | [diff] [blame] | 483 | serverName = serverPrefix + str( i ) |
Jon Hall | 06fd0df | 2021-01-25 15:50:06 -0800 | [diff] [blame] | 484 | if self.kubeConfig: |
| 485 | k8sName = k8sPrefix + str( i ) |
Jon Hall | ca31989 | 2017-06-15 15:25:22 -0700 | [diff] [blame] | 486 | |
| 487 | # Unfortunately this means we need to have a cell set beofre running TestON, |
| 488 | # Even if it is just the entire possible cluster size |
| 489 | ip = self.onosIps[ 'OC' + str( i ) ] |
| 490 | |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 491 | cli = self.createCliComponent( cliName, ip ) |
Jon Hall | ca31989 | 2017-06-15 15:25:22 -0700 | [diff] [blame] | 492 | rest = self.createRestComponent( restName, ip ) |
| 493 | bench = self.createBenchComponent( benchName ) |
Jon Hall | 4173b24 | 2017-09-12 17:04:38 -0700 | [diff] [blame] | 494 | server = self.createServerComponent( serverName, ip ) if createServer else None |
Jon Hall | 06fd0df | 2021-01-25 15:50:06 -0800 | [diff] [blame] | 495 | k8s = self.createServerComponent( k8sName, ip ) if self.kubeConfig else None |
| 496 | if self.kubeConfig: |
| 497 | k8s.kubeConfig = self.kubeConfig |
| 498 | k8s.podName = None |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 499 | self.nodes.append( Controller( prefix + str( i ), ip, cli, rest, bench, i - 1, |
Jon Hall | 06fd0df | 2021-01-25 15:50:06 -0800 | [diff] [blame] | 500 | self.user_name, server=server, k8s=k8s, |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 501 | dockerPrompt=self.dockerPrompt ) ) |