admin | f73f051 | 2013-12-17 16:33:37 -0800 | [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 | ''' |
| 40 | CassandraCliDriver is the basic driver which will handle the Cassandra's functions |
| 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 : |
| 58 | #self.start() |
| 59 | return main.TRUE |
| 60 | else : |
| 61 | main.log.error("Connection failed to the host "+self.user_name+"@"+self.ip_address) |
| 62 | main.log.error("Failed to connect to the Onos system") |
| 63 | return main.FALSE |
| 64 | |
| 65 | |
| 66 | def start(self): |
| 67 | ''' |
| 68 | This Function will start the Cassandra |
| 69 | ''' |
| 70 | main.log.info( "Starting Cassandra" ) |
| 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): |
| 79 | main.log.info("Cassandra Started ") |
| 80 | return main.TRUE |
| 81 | else: |
| 82 | main.log.error("Failed to start Cassandra"+ response) |
| 83 | return main.FALSE |
| 84 | |
| 85 | def status(self): |
| 86 | ''' |
| 87 | This Function will return the Status of the Cassandra |
| 88 | ''' |
| 89 | time.sleep(5) |
| 90 | self.execute(cmd="\r",prompt="\$",timeout=10) |
| 91 | response = self.execute(cmd="~/ONOS/start-cassandra.sh status ",prompt="\d+\sinstance\sof\scassandra\srunning(.*)",timeout=10) |
| 92 | |
| 93 | |
| 94 | self.execute(cmd="\r",prompt="\$",timeout=10) |
| 95 | return response |
| 96 | |
| 97 | if re.search("0\sinstance\sof\scassandra\srunning(.*)") : |
| 98 | main.log.info("Cassandra not running") |
| 99 | return main.TRUE |
| 100 | elif re.search("1\sinstance\sof\scassandra\srunning(.*)"): |
| 101 | main.log.warn("Cassandra Running") |
| 102 | return main.TRUE |
| 103 | |
| 104 | def stop(self): |
| 105 | ''' |
| 106 | This Function will stop the Cassandra if it is Running |
| 107 | ''' |
| 108 | self.execute(cmd="\r",prompt="\$",timeout=10) |
| 109 | time.sleep(5) |
| 110 | response = self.execute(cmd="~/ONOS/start-cassandra.sh stop ",prompt="Killed\sexisting\sprosess(.*)",timeout=10) |
| 111 | self.execute(cmd="\r",prompt="\$",timeout=10) |
| 112 | if re.search("Killed\sexisting\sprosess(.*)",response): |
| 113 | main.log.info("Cassandra Stopped") |
| 114 | return main.TRUE |
| 115 | else: |
| 116 | main.log.warn("Cassndra is not Running") |
| 117 | return main.FALSE |
| 118 | |
| 119 | def disconnect(self): |
| 120 | ''' |
| 121 | Called at the end of the test to disconnect the ssh handle. |
| 122 | ''' |
| 123 | response = '' |
| 124 | if self.handle: |
| 125 | self.handle.sendline("exit") |
| 126 | self.handle.expect("closed") |
| 127 | else : |
| 128 | main.log.error("Connection failed to the host") |
| 129 | response = main.FALSE |
| 130 | return response |
| 131 | |
| 132 | def isup(self): |
| 133 | ''' |
| 134 | A more complete status check of cassandra. |
| 135 | Tries 5 times to call start-cassandra.sh status |
| 136 | returns TRUE if it sees four occurances of both Up, and Normal |
| 137 | ''' |
| 138 | tries = 5 |
| 139 | main.log.info("trying %i times" % tries ) |
| 140 | for i in range(tries): |
| 141 | self.execute(cmd="\r",prompt="\$",timeout=10) |
| 142 | self.handle.sendline("") |
| 143 | self.handle.expect("\$") |
| 144 | self.handle.sendline("~/ONOS/start-cassandra.sh status") |
| 145 | self.handle.expect("sh status") |
| 146 | self.handle.expect("\$") |
| 147 | result = self.handle.before + self.handle.after |
| 148 | pattern = '(.*)Up(.*)Normal(.*)\n(.*)Up(.*)Normal(.*)\n(.*)Up(.*)Normal(.*)\n(.*)Up(.*)Normal(.*)' |
| 149 | if re.search(pattern, result): |
| 150 | return main.TRUE |
| 151 | return main.FALSE |