1 # Copyright (c) 2016 Cisco and/or its affiliates.
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at:
6 # http://www.apache.org/licenses/LICENSE-2.0
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
14 """Implementation of keywords for Honeycomb setup."""
16 from robot.api import logger
18 from resources.libraries.python.HTTPRequest import HTTPRequest, HTTPCodes, \
20 from resources.libraries.python.constants import Constants as Const
21 from resources.libraries.python.honeycomb.HoneycombUtil import HoneycombError
22 from resources.libraries.python.honeycomb.HoneycombUtil \
23 import HoneycombUtil as HcUtil
24 from resources.libraries.python.ssh import SSH
25 from resources.libraries.python.topology import NodeType
28 class HoneycombSetup(object):
29 """Implements keywords for Honeycomb setup.
31 The keywords implemented in this class make possible to:
34 - check the Honeycomb start-up state,
35 - check the Honeycomb shutdown state,
36 - add VPP to the topology.
43 def start_honeycomb_on_duts(*nodes):
44 """Start Honeycomb on specified DUT nodes.
46 This keyword starts the Honeycomb service on specified DUTs.
47 The keyword just starts the Honeycomb and does not check its startup
48 state. Use the keyword "Check Honeycomb Startup State" to check if the
49 Honeycomb is up and running.
50 Honeycomb must be installed in "/opt" directory, otherwise the start
52 :param nodes: List of nodes to start Honeycomb on.
54 :raises HoneycombError: If Honeycomb fails to start.
57 HoneycombSetup.print_environment(nodes)
59 logger.console("\nStarting Honeycomb service ...")
61 cmd = "{0}/bin/start".format(Const.REMOTE_HC_DIR)
64 if node['type'] == NodeType.DUT:
67 (ret_code, _, _) = ssh.exec_command_sudo(cmd)
68 if int(ret_code) != 0:
69 raise HoneycombError('Node {0} failed to start Honeycomb.'.
72 logger.info("Starting the Honeycomb service on node {0} is "
73 "in progress ...".format(node['host']))
76 def stop_honeycomb_on_duts(*nodes):
77 """Stop the Honeycomb service on specified DUT nodes.
79 This keyword stops the Honeycomb service on specified nodes. It just
80 stops the Honeycomb and does not check its shutdown state. Use the
81 keyword "Check Honeycomb Shutdown State" to check if Honeycomb has
83 :param nodes: List of nodes to stop Honeycomb on.
85 :raises HoneycombError: If Honeycomb failed to stop.
87 logger.console("\nShutting down Honeycomb service ...")
89 cmd = "{0}/bin/stop".format(Const.REMOTE_HC_DIR)
93 if node['type'] == NodeType.DUT:
96 (ret_code, _, _) = ssh.exec_command_sudo(cmd)
97 if int(ret_code) != 0:
98 errors.append(node['host'])
100 logger.info("Stopping the Honeycomb service on node {0} is "
101 "in progress ...".format(node['host']))
103 raise HoneycombError('Node(s) {0} failed to stop Honeycomb.'.
107 def check_honeycomb_startup_state(*nodes):
108 """Check state of Honeycomb service during startup on specified nodes.
110 Reads html path from template file oper_vpp_version.url.
112 Honeycomb nodes reply with connection refused or the following status
113 codes depending on startup progress: codes 200, 401, 403, 404, 500, 503
115 :param nodes: List of DUT nodes starting Honeycomb.
117 :return: True if all GETs returned code 200(OK).
120 path = HcUtil.read_path_from_url_file("oper_vpp_version")
121 expected_status_codes = (HTTPCodes.UNAUTHORIZED,
124 HTTPCodes.SERVICE_UNAVAILABLE,
125 HTTPCodes.INTERNAL_SERVER_ERROR)
128 if node['type'] == NodeType.DUT:
129 HoneycombSetup.print_ports(node)
130 status_code, _ = HTTPRequest.get(node, path, timeout=10,
131 enable_logging=False)
132 if status_code == HTTPCodes.OK:
133 logger.info("Honeycomb on node {0} is up and running".
134 format(node['host']))
135 elif status_code in expected_status_codes:
136 if status_code == HTTPCodes.UNAUTHORIZED:
137 logger.info('Unauthorized. If this triggers keyword '
138 'timeout, verify Honeycomb username and '
140 raise HoneycombError('Honeycomb on node {0} running but '
141 'not yet ready.'.format(node['host']),
142 enable_logging=False)
144 raise HoneycombError('Unexpected return code: {0}.'.
149 def check_honeycomb_shutdown_state(*nodes):
150 """Check state of Honeycomb service during shutdown on specified nodes.
152 Honeycomb nodes reply with connection refused or the following status
153 codes depending on shutdown progress: codes 200, 404.
155 :param nodes: List of DUT nodes stopping Honeycomb.
157 :return: True if all GETs fail to connect.
160 cmd = "ps -ef | grep -v grep | grep karaf"
162 if node['type'] == NodeType.DUT:
164 status_code, _ = HTTPRequest.get(node, '/index.html',
166 enable_logging=False)
167 if status_code == HTTPCodes.OK:
168 raise HoneycombError('Honeycomb on node {0} is still '
169 'running.'.format(node['host']),
170 enable_logging=False)
171 elif status_code == HTTPCodes.NOT_FOUND:
172 raise HoneycombError('Honeycomb on node {0} is shutting'
173 ' down.'.format(node['host']),
174 enable_logging=False)
176 raise HoneycombError('Unexpected return code: {0}.'.
178 except HTTPRequestError:
179 logger.debug('Connection refused, checking the process '
183 (ret_code, _, _) = ssh.exec_command_sudo(cmd)
185 raise HoneycombError('Honeycomb on node {0} is still '
186 'running.'.format(node['host']),
187 enable_logging=False)
189 logger.info("Honeycomb on node {0} has stopped".
190 format(node['host']))
194 def print_environment(nodes):
195 """Print information about the nodes to log. The information is defined
196 by commands in cmds tuple at the beginning of this method.
198 :param nodes: List of DUT nodes to get information about.
202 # TODO: When everything is set and running in VIRL env, transform this
203 # method to a keyword checking the environment.
211 "dpkg --list | grep openjdk",
212 "ls -la /opt/honeycomb",
213 "ls -la /opt/honeycomb/v3po-karaf-1.0.0-SNAPSHOT")
216 if node['type'] == NodeType.DUT:
217 logger.info("Checking node {} ...".format(node['host']))
219 logger.info("Command: {}".format(cmd))
222 ssh.exec_command_sudo(cmd)
225 def print_ports(node):
226 """Uses "sudo netstat -anp | grep java" to print port where a java
229 :param node: Honeycomb node where we want to print the ports.
233 cmds = ("netstat -anp | grep java",
234 "ps -ef | grep karaf")
236 logger.info("Checking node {} ...".format(node['host']))
238 logger.info("Command: {}".format(cmd))
241 ssh.exec_command_sudo(cmd)