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.
56 logger.console("\nStarting Honeycomb service ...")
58 cmd = "{0}/bin/start".format(Const.REMOTE_HC_DIR)
61 if node['type'] == NodeType.DUT:
64 (ret_code, _, _) = ssh.exec_command_sudo(cmd)
65 if int(ret_code) != 0:
66 raise HoneycombError('Node {0} failed to start Honeycomb.'.
69 logger.info("Starting the Honeycomb service on node {0} is "
70 "in progress ...".format(node['host']))
73 def stop_honeycomb_on_duts(*nodes):
74 """Stop the Honeycomb service on specified DUT nodes.
76 This keyword stops the Honeycomb service on specified nodes. It just
77 stops the Honeycomb and does not check its shutdown state. Use the
78 keyword "Check Honeycomb Shutdown State" to check if Honeycomb has
80 :param nodes: List of nodes to stop Honeycomb on.
82 :raises HoneycombError: If Honeycomb failed to stop.
84 logger.console("\nShutting down Honeycomb service ...")
86 cmd = "{0}/bin/stop".format(Const.REMOTE_HC_DIR)
90 if node['type'] == NodeType.DUT:
93 (ret_code, _, _) = ssh.exec_command_sudo(cmd)
94 if int(ret_code) != 0:
95 errors.append(node['host'])
97 logger.info("Stopping the Honeycomb service on node {0} is "
98 "in progress ...".format(node['host']))
100 raise HoneycombError('Node(s) {0} failed to stop Honeycomb.'.
104 def check_honeycomb_startup_state(*nodes):
105 """Check state of Honeycomb service during startup on specified nodes.
107 Reads html path from template file oper_vpp_version.url.
109 Honeycomb nodes reply with connection refused or the following status
110 codes depending on startup progress: codes 200, 401, 403, 404, 500, 503
112 :param nodes: List of DUT nodes starting Honeycomb.
114 :return: True if all GETs returned code 200(OK).
117 path = HcUtil.read_path_from_url_file("oper_vpp_version")
118 expected_status_codes = (HTTPCodes.UNAUTHORIZED,
121 HTTPCodes.SERVICE_UNAVAILABLE,
122 HTTPCodes.INTERNAL_SERVER_ERROR)
125 if node['type'] == NodeType.DUT:
126 status_code, _ = HTTPRequest.get(node, path, timeout=10,
127 enable_logging=False)
128 if status_code == HTTPCodes.OK:
129 logger.info("Honeycomb on node {0} is up and running".
130 format(node['host']))
131 elif status_code in expected_status_codes:
132 if status_code == HTTPCodes.UNAUTHORIZED:
133 logger.info('Unauthorized. If this triggers keyword '
134 'timeout, verify Honeycomb username and '
136 raise HoneycombError('Honeycomb on node {0} running but '
137 'not yet ready.'.format(node['host']),
138 enable_logging=False)
140 raise HoneycombError('Unexpected return code: {0}.'.
145 def check_honeycomb_shutdown_state(*nodes):
146 """Check state of Honeycomb service during shutdown on specified nodes.
148 Honeycomb nodes reply with connection refused or the following status
149 codes depending on shutdown progress: codes 200, 404.
151 :param nodes: List of DUT nodes stopping Honeycomb.
153 :return: True if all GETs fail to connect.
156 cmd = "ps -ef | grep -v grep | grep karaf"
158 if node['type'] == NodeType.DUT:
160 status_code, _ = HTTPRequest.get(node, '/index.html',
162 enable_logging=False)
163 if status_code == HTTPCodes.OK:
164 raise HoneycombError('Honeycomb on node {0} is still '
165 'running.'.format(node['host']),
166 enable_logging=False)
167 elif status_code == HTTPCodes.NOT_FOUND:
168 raise HoneycombError('Honeycomb on node {0} is shutting'
169 ' down.'.format(node['host']),
170 enable_logging=False)
172 raise HoneycombError('Unexpected return code: {0}.'.
174 except HTTPRequestError:
175 logger.debug('Connection refused, checking the process '
179 (ret_code, _, _) = ssh.exec_command_sudo(cmd)
181 raise HoneycombError('Honeycomb on node {0} is still '
182 'running.'.format(node['host']),
183 enable_logging=False)
185 logger.info("Honeycomb on node {0} has stopped".
186 format(node['host']))