04af9a5a8b66535530f11d00ce420a1c1f2ce85a
[csit.git] / resources / libraries / python / honeycomb / HoneycombSetup.py
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:
5 #
6 #     http://www.apache.org/licenses/LICENSE-2.0
7 #
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.
13
14 """Implementation of keywords for Honeycomb setup."""
15
16 from robot.api import logger
17
18 from resources.libraries.python.HTTPRequest import HTTPRequest, HTTPCodes, \
19     HTTPRequestError
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
26
27
28 class HoneycombSetup(object):
29     """Implements keywords for Honeycomb setup.
30
31     The keywords implemented in this class make possible to:
32     - start Honeycomb,
33     - stop Honeycomb,
34     - check the Honeycomb start-up state,
35     - check the Honeycomb shutdown state,
36     - add VPP to the topology.
37     """
38
39     def __init__(self):
40         pass
41
42     @staticmethod
43     def start_honeycomb_on_duts(*nodes):
44         """Start Honeycomb on specified DUT nodes.
45
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
51         will fail.
52         :param nodes: List of nodes to start Honeycomb on.
53         :type nodes: list
54         :raises HoneycombError: If Honeycomb fails to start.
55         """
56
57         HoneycombSetup.print_environment(nodes)
58
59         logger.console("\nStarting Honeycomb service ...")
60
61         cmd = "{0}/bin/start".format(Const.REMOTE_HC_DIR)
62
63         for node in nodes:
64             if node['type'] == NodeType.DUT:
65                 ssh = SSH()
66                 ssh.connect(node)
67                 (ret_code, _, _) = ssh.exec_command_sudo(cmd)
68                 if int(ret_code) != 0:
69                     raise HoneycombError('Node {0} failed to start Honeycomb.'.
70                                          format(node['host']))
71                 else:
72                     logger.info("Starting the Honeycomb service on node {0} is "
73                                 "in progress ...".format(node['host']))
74
75     @staticmethod
76     def stop_honeycomb_on_duts(*nodes):
77         """Stop the Honeycomb service on specified DUT nodes.
78
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
82         stopped.
83         :param nodes: List of nodes to stop Honeycomb on.
84         :type nodes: list
85         :raises HoneycombError: If Honeycomb failed to stop.
86         """
87         logger.console("\nShutting down Honeycomb service ...")
88
89         cmd = "{0}/bin/stop".format(Const.REMOTE_HC_DIR)
90         errors = []
91
92         for node in nodes:
93             if node['type'] == NodeType.DUT:
94                 ssh = SSH()
95                 ssh.connect(node)
96                 (ret_code, _, _) = ssh.exec_command_sudo(cmd)
97                 if int(ret_code) != 0:
98                     errors.append(node['host'])
99                 else:
100                     logger.info("Stopping the Honeycomb service on node {0} is "
101                                 "in progress ...".format(node['host']))
102         if errors:
103             raise HoneycombError('Node(s) {0} failed to stop Honeycomb.'.
104                                  format(errors))
105
106     @staticmethod
107     def check_honeycomb_startup_state(*nodes):
108         """Check state of Honeycomb service during startup on specified nodes.
109
110         Reads html path from template file oper_vpp_version.url.
111
112         Honeycomb nodes reply with connection refused or the following status
113         codes depending on startup progress: codes 200, 401, 403, 404, 500, 503
114
115         :param nodes: List of DUT nodes starting Honeycomb.
116         :type nodes: list
117         :return: True if all GETs returned code 200(OK).
118         :rtype bool
119         """
120         path = HcUtil.read_path_from_url_file("oper_vpp_version")
121         expected_status_codes = (HTTPCodes.UNAUTHORIZED,
122                                  HTTPCodes.FORBIDDEN,
123                                  HTTPCodes.NOT_FOUND,
124                                  HTTPCodes.SERVICE_UNAVAILABLE,
125                                  HTTPCodes.INTERNAL_SERVER_ERROR)
126
127         for node in nodes:
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 '
139                                     'password.')
140                     raise HoneycombError('Honeycomb on node {0} running but '
141                                          'not yet ready.'.format(node['host']),
142                                          enable_logging=False)
143                 else:
144                     raise HoneycombError('Unexpected return code: {0}.'.
145                                          format(status_code))
146         return True
147
148     @staticmethod
149     def check_honeycomb_shutdown_state(*nodes):
150         """Check state of Honeycomb service during shutdown on specified nodes.
151
152         Honeycomb nodes reply with connection refused or the following status
153         codes depending on shutdown progress: codes 200, 404.
154
155         :param nodes: List of DUT nodes stopping Honeycomb.
156         :type nodes: list
157         :return: True if all GETs fail to connect.
158         :rtype bool
159         """
160         cmd = "ps -ef | grep -v grep | grep karaf"
161         for node in nodes:
162             if node['type'] == NodeType.DUT:
163                 try:
164                     status_code, _ = HTTPRequest.get(node, '/index.html',
165                                                      timeout=5,
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)
175                     else:
176                         raise HoneycombError('Unexpected return code: {0}.'.
177                                              format(status_code))
178                 except HTTPRequestError:
179                     logger.debug('Connection refused, checking the process '
180                                  'state ...')
181                     ssh = SSH()
182                     ssh.connect(node)
183                     (ret_code, _, _) = ssh.exec_command_sudo(cmd)
184                     if ret_code == 0:
185                         raise HoneycombError('Honeycomb on node {0} is still '
186                                              'running.'.format(node['host']),
187                                              enable_logging=False)
188                     else:
189                         logger.info("Honeycomb on node {0} has stopped".
190                                     format(node['host']))
191         return True
192
193     @staticmethod
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.
197
198         :param nodes: List of DUT nodes to get information about.
199         :type nodes: list
200         """
201
202         # TODO: When everything is set and running in VIRL env, transform this
203         # method to a keyword checking the environment.
204
205         cmds = ("uname -a",
206                 "df -lh",
207                 "echo $JAVA_HOME",
208                 "echo $PATH",
209                 "which java",
210                 "java -version",
211                 "dpkg --list | grep openjdk",
212                 "ls -la /opt/honeycomb",
213                 "ls -la /opt/honeycomb/v3po-karaf-1.0.0-SNAPSHOT")
214
215         for node in nodes:
216             if node['type'] == NodeType.DUT:
217                 logger.info("Checking node {} ...".format(node['host']))
218                 for cmd in cmds:
219                     logger.info("Command: {}".format(cmd))
220                     ssh = SSH()
221                     ssh.connect(node)
222                     ssh.exec_command_sudo(cmd)
223
224     @staticmethod
225     def print_ports(node):
226         """Uses "sudo netstat -anp | grep java" to print port where a java
227         application listens.
228
229         :param node: Honeycomb node where we want to print the ports.
230         :type node: dict
231         """
232
233         cmds = ("netstat -anp | grep java",
234                 "ps -ef | grep karaf")
235
236         logger.info("Checking node {} ...".format(node['host']))
237         for cmd in cmds:
238             logger.info("Command: {}".format(cmd))
239             ssh = SSH()
240             ssh.connect(node)
241             ssh.exec_command_sudo(cmd)