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.topology import NodeType
19 from resources.libraries.python.ssh import SSH
20 from resources.libraries.python.HTTPRequest import HTTPRequest, HTTPCodes, \
22 from resources.libraries.python.HoneycombUtil import HoneycombUtil as HcUtil
23 from resources.libraries.python.HoneycombUtil import HoneycombError
24 from resources.libraries.python.constants import Constants as Const
27 class HoneycombSetup(object):
28 """Implements keywords for Honeycomb setup.
30 The keywords implemented in this class make possible to:
33 - check the Honeycomb start-up state,
34 - check the Honeycomb shutdown state,
35 - add VPP to the topology.
42 def start_honeycomb_on_duts(*nodes):
43 """Start Honeycomb on specified DUT nodes.
45 This keyword starts the Honeycomb service on specified DUTs.
46 The keyword just starts the Honeycomb and does not check its startup
47 state. Use the keyword "Check Honeycomb Startup State" to check if the
48 Honeycomb is up and running.
49 Honeycomb must be installed in "/opt" directory, otherwise the start
51 :param nodes: List of nodes to start Honeycomb on.
53 :raises HoneycombError: If Honeycomb fails to start.
55 logger.console("Starting Honeycomb service ...")
57 cmd = "{0}/start".format(Const.REMOTE_HC_DIR)
60 if node['type'] == NodeType.DUT:
63 (ret_code, _, _) = ssh.exec_command_sudo(cmd)
64 if int(ret_code) != 0:
65 raise HoneycombError('Node {0} failed to start Honeycomb.'.
68 logger.info("Starting the Honeycomb service on node {0} is "
69 "in progress ...".format(node['host']))
72 def stop_honeycomb_on_duts(*nodes):
73 """Stop the Honeycomb service on specified DUT nodes.
75 This keyword stops the Honeycomb service on specified nodes. It just
76 stops the Honeycomb and does not check its shutdown state. Use the
77 keyword "Check Honeycomb Shutdown State" to check if Honeycomb has
79 :param nodes: List of nodes to stop Honeycomb on.
81 :raises HoneycombError: If Honeycomb failed to stop.
83 logger.console("Shutting down Honeycomb service ...")
85 cmd = "{0}/stop".format(Const.REMOTE_HC_DIR)
89 if node['type'] == NodeType.DUT:
92 (ret_code, _, _) = ssh.exec_command_sudo(cmd)
93 if int(ret_code) != 0:
94 errors.append(node['host'])
96 logger.info("Stopping the Honeycomb service on node {0} is "
97 "in progress ...".format(node['host']))
99 raise HoneycombError('Node(s) {0} failed to stop Honeycomb.'.
103 def check_honeycomb_startup_state(*nodes):
104 """Check state of Honeycomb service during startup on specified nodes.
106 Reads html path from template file oper_vpp_version.url.
108 Honeycomb nodes reply with connection refused or the following status
109 codes depending on startup progress: codes 200, 401, 403, 404, 500, 503
111 :param nodes: List of DUT nodes starting Honeycomb.
113 :return: True if all GETs returned code 200(OK).
116 path = HcUtil.read_path_from_url_file("oper_vpp_version")
117 expected_status_codes = (HTTPCodes.UNAUTHORIZED,
120 HTTPCodes.SERVICE_UNAVAILABLE,
121 HTTPCodes.INTERNAL_SERVER_ERROR)
124 if node['type'] == NodeType.DUT:
125 status_code, _ = HTTPRequest.get(node, path, timeout=10,
126 enable_logging=False)
127 if status_code == HTTPCodes.OK:
128 logger.info("Honeycomb on node {0} is up and running".
129 format(node['host']))
130 elif status_code in expected_status_codes:
131 if status_code == HTTPCodes.UNAUTHORIZED:
132 logger.info('Unauthorized. If this triggers keyword '
133 'timeout, verify Honeycomb username and '
135 raise HoneycombError('Honeycomb on node {0} running but '
136 'not yet ready.'.format(node['host']),
137 enable_logging=False)
139 raise HoneycombError('Unexpected return code: {0}.'.
144 def check_honeycomb_shutdown_state(*nodes):
145 """Check state of Honeycomb service during shutdown on specified nodes.
147 Honeycomb nodes reply with connection refused or the following status
148 codes depending on shutdown progress: codes 200, 404.
150 :param nodes: List of DUT nodes stopping Honeycomb.
152 :return: True if all GETs fail to connect.
155 cmd = "ps -ef | grep -v grep | grep karaf"
157 if node['type'] == NodeType.DUT:
159 status_code, _ = HTTPRequest.get(node, '/index.html',
161 enable_logging=False)
162 if status_code == HTTPCodes.OK:
163 raise HoneycombError('Honeycomb on node {0} is still '
164 'running.'.format(node['host']),
165 enable_logging=False)
166 elif status_code == HTTPCodes.NOT_FOUND:
167 raise HoneycombError('Honeycomb on node {0} is shutting'
168 ' down.'.format(node['host']),
169 enable_logging=False)
171 raise HoneycombError('Unexpected return code: {0}.'.
173 except HTTPRequestError:
174 logger.debug('Connection refused, checking the process '
178 (ret_code, _, _) = ssh.exec_command_sudo(cmd)
180 raise HoneycombError('Honeycomb on node {0} is still '
181 'running.'.format(node['host']),
182 enable_logging=False)
184 logger.info("Honeycomb on node {0} has stopped".
185 format(node['host']))