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 = "sudo service honeycomb start"
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 = "sudo service honeycomb stop"
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}.'.
147 status_code, _ = HcUtil.get_honeycomb_data(
148 node, "config_vpp_interfaces")
149 if status_code != HTTPCodes.OK:
150 raise HoneycombError('Honeycomb on node {0} running but '
151 'not yet ready.'.format(node['host']),
152 enable_logging=False)
156 def check_honeycomb_shutdown_state(*nodes):
157 """Check state of Honeycomb service during shutdown on specified nodes.
159 Honeycomb nodes reply with connection refused or the following status
160 codes depending on shutdown progress: codes 200, 404.
162 :param nodes: List of DUT nodes stopping Honeycomb.
164 :return: True if all GETs fail to connect.
167 cmd = "ps -ef | grep -v grep | grep honeycomb"
169 if node['type'] == NodeType.DUT:
171 status_code, _ = HTTPRequest.get(node, '/index.html',
173 enable_logging=False)
174 if status_code == HTTPCodes.OK:
175 raise HoneycombError('Honeycomb on node {0} is still '
176 'running.'.format(node['host']),
177 enable_logging=False)
178 elif status_code == HTTPCodes.NOT_FOUND:
179 raise HoneycombError('Honeycomb on node {0} is shutting'
180 ' down.'.format(node['host']),
181 enable_logging=False)
183 raise HoneycombError('Unexpected return code: {0}.'.
185 except HTTPRequestError:
186 logger.debug('Connection refused, checking the process '
190 (ret_code, _, _) = ssh.exec_command_sudo(cmd)
192 raise HoneycombError('Honeycomb on node {0} is still '
193 'running.'.format(node['host']),
194 enable_logging=False)
196 logger.info("Honeycomb on node {0} has stopped".
197 format(node['host']))
201 def configure_unsecured_access(*nodes):
202 """Configure Honeycomb to allow restconf requests through insecure HTTP
203 used by tests. By default this is only allowed for localhost.
205 :param nodes: All nodes in test topology.
207 :raises HoneycombError: If the configuration could not be changed.
209 # TODO: Modify tests to use HTTPS instead.
211 find = "restconf-binding-address"
212 replace = '\\"restconf-binding-address\\": \\"0.0.0.0\\",'
214 argument = '"/{0}/c\\ {1}"'.format(find, replace)
215 path = "{0}/config/honeycomb.json".format(Const.REMOTE_HC_DIR)
216 command = "sed -i {0} {1}".format(argument, path)
220 if node['type'] == NodeType.DUT:
222 (ret_code, _, stderr) = ssh.exec_command_sudo(command)
224 raise HoneycombError("Failed to modify configuration on "
225 "node {0}, {1}".format(node, stderr))
228 def print_environment(nodes):
229 """Print information about the nodes to log. The information is defined
230 by commands in cmds tuple at the beginning of this method.
232 :param nodes: List of DUT nodes to get information about.
236 # TODO: When everything is set and running in VIRL env, transform this
237 # method to a keyword checking the environment.
245 "dpkg --list | grep openjdk",
246 "ls -la /opt/honeycomb",
247 "ls -la /opt/honeycomb/v3po-karaf-1.0.0-SNAPSHOT")
250 if node['type'] == NodeType.DUT:
251 logger.info("Checking node {} ...".format(node['host']))
253 logger.info("Command: {}".format(cmd))
256 ssh.exec_command_sudo(cmd)
259 def print_ports(node):
260 """Uses "sudo netstat -anp | grep java" to print port where a java
263 :param node: Honeycomb node where we want to print the ports.
267 cmds = ("netstat -anp | grep java",
268 "ps -ef | grep karaf")
270 logger.info("Checking node {} ...".format(node['host']))
272 logger.info("Command: {}".format(cmd))
275 ssh.exec_command_sudo(cmd)