blob: bad8c4e660d08d299a7aa7b0f4961878f883da5d [file] [log] [blame]
adminbae64d82013-08-01 10:50:15 -07001#!/usr/bin/env python
2'''
3Created 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
22CassandraCliDriver is the basic driver which will handle the Cassandra functions
23'''
24
25import pexpect
26import struct
27import fcntl
28import os
29import signal
30import re
31import sys
32import core.teston
33import time
34
35sys.path.append("../")
36from drivers.common.clidriver import CLI
37
38class CassandraCliDriver(CLI):
39 '''
adminaeedddd2013-08-02 15:14:15 -070040 CassandraCliDriver is the basic driver which will handle the Cassandra's functions
adminbae64d82013-08-01 10:50:15 -070041 '''
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 :
admine0ae8202013-08-28 11:51:43 -070058 #self.start()
adminbae64d82013-08-01 10:50:15 -070059 return main.TRUE
60 else :
Jon Hallf89c8552014-04-02 13:14:06 -070061 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")
adminbae64d82013-08-01 10:50:15 -070063 return main.FALSE
64
65
66 def start(self):
adminaeedddd2013-08-02 15:14:15 -070067 '''
68 This Function will start the Cassandra
69 '''
Jon Hallf89c8552014-04-02 13:14:06 -070070 main.log.info(self.name + ": Starting Cassandra" )
adminbae64d82013-08-01 10:50:15 -070071 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 Hallf89c8552014-04-02 13:14:06 -070079 main.log.info(self.name + ": Cassandra Started ")
adminbae64d82013-08-01 10:50:15 -070080 return main.TRUE
81 else:
Jon Hallf89c8552014-04-02 13:14:06 -070082 main.log.error(self.name + ": Failed to start Cassandra"+ response)
adminbae64d82013-08-01 10:50:15 -070083 return main.FALSE
84
85 def status(self):
adminaeedddd2013-08-02 15:14:15 -070086 '''
87 This Function will return the Status of the Cassandra
88 '''
adminbae64d82013-08-01 10:50:15 -070089 time.sleep(5)
Jon Hallf89c8552014-04-02 13:14:06 -070090 self.execute(cmd="\n",prompt="\$",timeout=10)
adminbae64d82013-08-01 10:50:15 -070091 response = self.execute(cmd="~/ONOS/start-cassandra.sh status ",prompt="\d+\sinstance\sof\scassandra\srunning(.*)",timeout=10)
92
93
Jon Hallf89c8552014-04-02 13:14:06 -070094 #self.execute(cmd="\n",prompt="\$",timeout=10)
95 #return response
adminbae64d82013-08-01 10:50:15 -070096
Jon Hallf89c8552014-04-02 13:14:06 -070097 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")
adminbae64d82013-08-01 10:50:15 -0700102 return main.TRUE
Jon Hallf89c8552014-04-02 13:14:06 -0700103 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
adminbae64d82013-08-01 10:50:15 -0700106 return main.TRUE
Jon Hallf89c8552014-04-02 13:14:06 -0700107 else:
108 main.log.warn(self.name + ": Cannot determine cassandra status")
109 return main.False
adminbae64d82013-08-01 10:50:15 -0700110
111 def stop(self):
adminaeedddd2013-08-02 15:14:15 -0700112 '''
113 This Function will stop the Cassandra if it is Running
114 '''
Jon Hallf89c8552014-04-02 13:14:06 -0700115 self.execute(cmd="\n",prompt="\$",timeout=10)
adminbae64d82013-08-01 10:50:15 -0700116 time.sleep(5)
117 response = self.execute(cmd="~/ONOS/start-cassandra.sh stop ",prompt="Killed\sexisting\sprosess(.*)",timeout=10)
Jon Hallf89c8552014-04-02 13:14:06 -0700118 self.execute(cmd="\n",prompt="\$",timeout=10)
adminbae64d82013-08-01 10:50:15 -0700119 if re.search("Killed\sexisting\sprosess(.*)",response):
Jon Hallf89c8552014-04-02 13:14:06 -0700120 main.log.info(self.name + ": Cassandra Stopped")
adminbae64d82013-08-01 10:50:15 -0700121 return main.TRUE
122 else:
Jon Hallf89c8552014-04-02 13:14:06 -0700123 main.log.warn(self.name + ": Cassndra is not Running")
adminbae64d82013-08-01 10:50:15 -0700124 return main.FALSE
125
126 def disconnect(self):
adminaeedddd2013-08-02 15:14:15 -0700127 '''
128 Called at the end of the test to disconnect the ssh handle.
129 '''
adminbae64d82013-08-01 10:50:15 -0700130 response = ''
131 if self.handle:
132 self.handle.sendline("exit")
133 self.handle.expect("closed")
134 else :
Jon Hallf89c8552014-04-02 13:14:06 -0700135 main.log.error(self.name + ": Connection failed to the host")
adminbae64d82013-08-01 10:50:15 -0700136 response = main.FALSE
137 return response
138
139 def isup(self):
adminaeedddd2013-08-02 15:14:15 -0700140 '''
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 '''
adminbae64d82013-08-01 10:50:15 -0700145 tries = 5
Jon Hallf89c8552014-04-02 13:14:06 -0700146 main.log.info(self.name + ": trying %i times" % tries )
adminbae64d82013-08-01 10:50:15 -0700147 for i in range(tries):
Jon Hallf89c8552014-04-02 13:14:06 -0700148 self.execute(cmd="\n",prompt="\$",timeout=10)
adminbae64d82013-08-01 10:50:15 -0700149 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 Hallf89c8552014-04-02 13:14:06 -0700155 #pattern = '(.*)Up(.*)Normal(.*)\n(.*)Up(.*)Normal(.*)\n(.*)Up(.*)Normal(.*)\n(.*)Up(.*)Normal(.*)'
156 pattern = '(' + self.ip_address.replace('.', '\\.') + '.*)Up(.*)Normal(.*)'
adminbae64d82013-08-01 10:50:15 -0700157 if re.search(pattern, result):
158 return main.TRUE
159 return main.FALSE