admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | ''' |
| 3 | Created on 31-May-2013 |
| 4 | |
| 5 | @author: Anil Kumar (anilkumar.s@paxterrasolutions.com) |
| 6 | |
| 7 | |
| 8 | TestON is free software: you can redistribute it and/or modify |
| 9 | it under the terms of the GNU General Public License as published by |
| 10 | the Free Software Foundation, either version 2 of the License, or |
| 11 | (at your option) any later version. |
| 12 | |
| 13 | TestON is distributed in the hope that it will be useful, |
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | GNU General Public License for more details. |
| 17 | |
| 18 | You should have received a copy of the GNU General Public License |
| 19 | along with TestON. If not, see <http://www.gnu.org/licenses/>. |
| 20 | |
| 21 | |
| 22 | CassandraCliDriver is the basic driver which will handle the Cassandra functions |
| 23 | ''' |
| 24 | |
| 25 | import pexpect |
| 26 | import struct |
| 27 | import fcntl |
| 28 | import os |
| 29 | import signal |
| 30 | import re |
| 31 | import sys |
| 32 | import core.teston |
| 33 | import time |
| 34 | |
| 35 | sys.path.append("../") |
| 36 | from drivers.common.clidriver import CLI |
| 37 | |
| 38 | class CassandraCliDriver(CLI): |
| 39 | ''' |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 40 | CassandraCliDriver is the basic driver which will handle the Cassandra's functions |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 41 | ''' |
| 42 | def __init__(self): |
| 43 | super(CLI, self).__init__() |
| 44 | self.handle = self |
| 45 | self.wrapped = sys.modules[__name__] |
| 46 | |
| 47 | def connect(self, **connectargs): |
| 48 | # Here the main is the TestON instance after creating all the log handles. |
| 49 | self.port = None |
| 50 | for key in connectargs: |
| 51 | vars(self)[key] = connectargs[key] |
| 52 | |
| 53 | self.name = self.options['name'] |
| 54 | self.handle = super(CassandraCliDriver, self).connect(user_name = self.user_name, ip_address = self.ip_address,port = self.port, pwd = self.pwd) |
| 55 | |
| 56 | self.ssh_handle = self.handle |
| 57 | if self.handle : |
admin | e0ae820 | 2013-08-28 11:51:43 -0700 | [diff] [blame] | 58 | #self.start() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 59 | return main.TRUE |
| 60 | else : |
Jon Hall | f89c855 | 2014-04-02 13:14:06 -0700 | [diff] [blame] | 61 | main.log.error(self.name + ": Connection failed to the host "+self.user_name+"@"+self.ip_address) |
| 62 | main.log.error(self.name + ": Failed to connect to the Onos system") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 63 | return main.FALSE |
| 64 | |
| 65 | |
| 66 | def start(self): |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 67 | ''' |
| 68 | This Function will start the Cassandra |
| 69 | ''' |
Jon Hall | f89c855 | 2014-04-02 13:14:06 -0700 | [diff] [blame] | 70 | main.log.info(self.name + ": Starting Cassandra" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 71 | self.handle.sendline("") |
| 72 | self.handle.expect("\$") |
| 73 | self.handle.sendline("~/ONOS/start-cassandra.sh start") |
| 74 | self.handle.expect("start-cassandra.sh start") |
| 75 | self.handle.expect("\$") |
| 76 | response = self.handle.before + self.handle.after |
| 77 | time.sleep(5) |
| 78 | if re.search("Starting\scassandra(.*)", response): |
Jon Hall | f89c855 | 2014-04-02 13:14:06 -0700 | [diff] [blame] | 79 | main.log.info(self.name + ": Cassandra Started ") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 80 | return main.TRUE |
| 81 | else: |
Jon Hall | f89c855 | 2014-04-02 13:14:06 -0700 | [diff] [blame] | 82 | main.log.error(self.name + ": Failed to start Cassandra"+ response) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 83 | return main.FALSE |
| 84 | |
| 85 | def status(self): |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 86 | ''' |
| 87 | This Function will return the Status of the Cassandra |
| 88 | ''' |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 89 | time.sleep(5) |
Jon Hall | f89c855 | 2014-04-02 13:14:06 -0700 | [diff] [blame] | 90 | self.execute(cmd="\n",prompt="\$",timeout=10) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 91 | response = self.execute(cmd="~/ONOS/start-cassandra.sh status ",prompt="\d+\sinstance\sof\scassandra\srunning(.*)",timeout=10) |
| 92 | |
| 93 | |
Jon Hall | f89c855 | 2014-04-02 13:14:06 -0700 | [diff] [blame] | 94 | #self.execute(cmd="\n",prompt="\$",timeout=10) |
| 95 | #return response |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 96 | |
Jon Hall | f89c855 | 2014-04-02 13:14:06 -0700 | [diff] [blame] | 97 | if re.search("0\sinstance\sof\scassandra\srunning(.*)",response) : |
| 98 | main.log.info(self.name + ": Cassandra not running") |
| 99 | return main.FALSE |
| 100 | elif re.search("1\sinstance\sof\scassandra\srunning(.*)",response): |
| 101 | main.log.warn(self.name + ": Cassandra Running") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 102 | return main.TRUE |
Jon Hall | f89c855 | 2014-04-02 13:14:06 -0700 | [diff] [blame] | 103 | elif re.search("\sinstance\sof\scassandra\srunning(.*)",response): |
| 104 | main.log.warn(self.name + ": Multiple instances of Cassandra Running on the same machine!") |
| 105 | #Known bug: Once ONOS starts the script shows 2 instances |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 106 | return main.TRUE |
Jon Hall | f89c855 | 2014-04-02 13:14:06 -0700 | [diff] [blame] | 107 | else: |
| 108 | main.log.warn(self.name + ": Cannot determine cassandra status") |
| 109 | return main.False |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 110 | |
| 111 | def stop(self): |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 112 | ''' |
| 113 | This Function will stop the Cassandra if it is Running |
| 114 | ''' |
Jon Hall | f89c855 | 2014-04-02 13:14:06 -0700 | [diff] [blame] | 115 | self.execute(cmd="\n",prompt="\$",timeout=10) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 116 | time.sleep(5) |
| 117 | response = self.execute(cmd="~/ONOS/start-cassandra.sh stop ",prompt="Killed\sexisting\sprosess(.*)",timeout=10) |
Jon Hall | f89c855 | 2014-04-02 13:14:06 -0700 | [diff] [blame] | 118 | self.execute(cmd="\n",prompt="\$",timeout=10) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 119 | if re.search("Killed\sexisting\sprosess(.*)",response): |
Jon Hall | f89c855 | 2014-04-02 13:14:06 -0700 | [diff] [blame] | 120 | main.log.info(self.name + ": Cassandra Stopped") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 121 | return main.TRUE |
| 122 | else: |
Jon Hall | f89c855 | 2014-04-02 13:14:06 -0700 | [diff] [blame] | 123 | main.log.warn(self.name + ": Cassndra is not Running") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 124 | return main.FALSE |
| 125 | |
| 126 | def disconnect(self): |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 127 | ''' |
| 128 | Called at the end of the test to disconnect the ssh handle. |
| 129 | ''' |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 130 | response = '' |
| 131 | if self.handle: |
| 132 | self.handle.sendline("exit") |
| 133 | self.handle.expect("closed") |
| 134 | else : |
Jon Hall | f89c855 | 2014-04-02 13:14:06 -0700 | [diff] [blame] | 135 | main.log.error(self.name + ": Connection failed to the host") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 136 | response = main.FALSE |
| 137 | return response |
| 138 | |
| 139 | def isup(self): |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 140 | ''' |
| 141 | A more complete status check of cassandra. |
| 142 | Tries 5 times to call start-cassandra.sh status |
| 143 | returns TRUE if it sees four occurances of both Up, and Normal |
| 144 | ''' |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 145 | tries = 5 |
Jon Hall | f89c855 | 2014-04-02 13:14:06 -0700 | [diff] [blame] | 146 | main.log.info(self.name + ": trying %i times" % tries ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 147 | for i in range(tries): |
Jon Hall | f89c855 | 2014-04-02 13:14:06 -0700 | [diff] [blame] | 148 | self.execute(cmd="\n",prompt="\$",timeout=10) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 149 | self.handle.sendline("") |
| 150 | self.handle.expect("\$") |
| 151 | self.handle.sendline("~/ONOS/start-cassandra.sh status") |
| 152 | self.handle.expect("sh status") |
| 153 | self.handle.expect("\$") |
| 154 | result = self.handle.before + self.handle.after |
Jon Hall | f89c855 | 2014-04-02 13:14:06 -0700 | [diff] [blame] | 155 | #pattern = '(.*)Up(.*)Normal(.*)\n(.*)Up(.*)Normal(.*)\n(.*)Up(.*)Normal(.*)\n(.*)Up(.*)Normal(.*)' |
| 156 | pattern = '(' + self.ip_address.replace('.', '\\.') + '.*)Up(.*)Normal(.*)' |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 157 | if re.search(pattern, result): |
| 158 | return main.TRUE |
| 159 | return main.FALSE |