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 | ZookeeperCliDriver is the basic driver which will handle the Zookeeper 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 ZookeeperCliDriver(CLI): |
| 39 | ''' |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 40 | ZookeeperCliDriver is the basic driver which will handle the Zookeeper'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] |
Jon Hall | 4a2b048 | 2014-04-18 16:29:26 -0700 | [diff] [blame] | 52 | self.home = "~/ONOS" |
| 53 | #self.home = "~/zookeeper-3.4.5" |
Jon Hall | f89c855 | 2014-04-02 13:14:06 -0700 | [diff] [blame] | 54 | for key in self.options: |
| 55 | if key == "home": |
| 56 | self.home = self.options['home'] |
| 57 | break |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 58 | |
| 59 | self.name = self.options['name'] |
Jon Hall | f89c855 | 2014-04-02 13:14:06 -0700 | [diff] [blame] | 60 | self.handle = super(ZookeeperCliDriver, self).connect(user_name = self.user_name, ip_address = self.ip_address,port = self.port, pwd = self.pwd, home = self.home) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 61 | |
| 62 | self.ssh_handle = self.handle |
| 63 | if self.handle : |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 64 | return main.TRUE |
| 65 | else : |
| 66 | main.log.error("Connection failed to the host "+self.user_name+"@"+self.ip_address) |
Jon Hall | f89c855 | 2014-04-02 13:14:06 -0700 | [diff] [blame] | 67 | main.log.error(self.name + ": Failed to connect to Zookeeper") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 68 | return main.FALSE |
| 69 | |
| 70 | |
| 71 | def start(self): |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 72 | ''' |
| 73 | This Function will start the Zookeeper |
| 74 | ''' |
Jon Hall | f89c855 | 2014-04-02 13:14:06 -0700 | [diff] [blame] | 75 | main.log.info(self.name + ": Starting Zookeeper" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 76 | self.handle.sendline("") |
| 77 | self.handle.expect("\$") |
Jon Hall | 4a2b048 | 2014-04-18 16:29:26 -0700 | [diff] [blame] | 78 | self.handle.sendline("cd "+self.home) |
| 79 | self.handle.sendline("./onos.sh zk start") |
| 80 | self.handle.expect("zk start") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 81 | self.handle.expect("\$") |
| 82 | response = self.handle.before + self.handle.after |
| 83 | if re.search("STARTED", response): |
Jon Hall | f89c855 | 2014-04-02 13:14:06 -0700 | [diff] [blame] | 84 | main.log.info(self.name + ": Zookeeper Started ") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 85 | return main.TRUE |
| 86 | elif re.search("running", response): |
Jon Hall | f89c855 | 2014-04-02 13:14:06 -0700 | [diff] [blame] | 87 | main.log.warn(self.name +": zookeeper ... already running") |
| 88 | return main.TRUE |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 89 | else: |
Jon Hall | f89c855 | 2014-04-02 13:14:06 -0700 | [diff] [blame] | 90 | main.log.error(self.name + ": Failed to start Zookeeper"+ response) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 91 | return main.FALSE |
| 92 | |
| 93 | def status(self): |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 94 | ''' |
| 95 | This Function will return the Status of the Zookeeper |
| 96 | ''' |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 97 | time.sleep(5) |
Jon Hall | f89c855 | 2014-04-02 13:14:06 -0700 | [diff] [blame] | 98 | self.execute(cmd="\n",prompt="\$",timeout=10) |
Jon Hall | 4a2b048 | 2014-04-18 16:29:26 -0700 | [diff] [blame] | 99 | self.handle.sendline("cd "+self.home) |
| 100 | response = self.execute(cmd="./onos.sh zk status ",prompt="JMX",timeout=10) |
Jon Hall | f89c855 | 2014-04-02 13:14:06 -0700 | [diff] [blame] | 101 | |
| 102 | self.execute(cmd="\n",prompt="\$",timeout=10) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 103 | return response |
| 104 | |
| 105 | def stop(self): |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 106 | ''' |
| 107 | This Function will stop the Zookeeper if it is Running |
| 108 | ''' |
Jon Hall | f89c855 | 2014-04-02 13:14:06 -0700 | [diff] [blame] | 109 | self.execute(cmd="\n",prompt="\$",timeout=10) |
Jon Hall | bd795bf | 2014-06-18 09:46:32 -0700 | [diff] [blame^] | 110 | time.sleep(1) |
Jon Hall | 4a2b048 | 2014-04-18 16:29:26 -0700 | [diff] [blame] | 111 | self.handle.sendline("cd "+self.home) |
| 112 | response = self.execute(cmd="./onos.sh zk stop ",prompt="$",timeout=10) |
| 113 | if re.search("stopping",response): |
Jon Hall | f89c855 | 2014-04-02 13:14:06 -0700 | [diff] [blame] | 114 | main.log.info(self.name + ": Zookeeper Stopped") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 115 | return main.TRUE |
| 116 | else: |
Jon Hall | f89c855 | 2014-04-02 13:14:06 -0700 | [diff] [blame] | 117 | main.log.warn(self.name + ": No zookeeper to stop") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 118 | return main.FALSE |
| 119 | |
| 120 | def disconnect(self): |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 121 | ''' |
| 122 | Called at the end of the test to disconnect the ZK handle |
| 123 | ''' |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 124 | response = '' |
| 125 | if self.handle: |
| 126 | self.handle.sendline("exit") |
| 127 | self.handle.expect("closed") |
| 128 | else : |
Jon Hall | f89c855 | 2014-04-02 13:14:06 -0700 | [diff] [blame] | 129 | main.log.error(self.name + ": Connection failed to the host") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 130 | response = main.FALSE |
| 131 | return response |
| 132 | |
| 133 | def isup(self): |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 134 | ''' |
| 135 | Calls the zookeeper status and returns TRUE if it has an assigned Mode to it. |
| 136 | ''' |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 137 | self.execute(cmd="\n",prompt="\$",timeout=10) |
Jon Hall | e80ef8c | 2014-04-29 15:29:13 -0700 | [diff] [blame] | 138 | response = self.execute(cmd=self.home + "/onos.sh zk status ",prompt="Mode",timeout=10) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 139 | pattern = '(.*)Mode(.*)' |
Jon Hall | f89c855 | 2014-04-02 13:14:06 -0700 | [diff] [blame] | 140 | if re.search(pattern, response): |
| 141 | main.log.info(self.name + ": Zookeeper is up.") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 142 | return main.TRUE |
| 143 | else: |
Jon Hall | f89c855 | 2014-04-02 13:14:06 -0700 | [diff] [blame] | 144 | main.log.info(self.name + ": Zookeeper is down.") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 145 | return main.FALSE |
| 146 | |
| 147 | |