Add Honeycomb persistence tests
[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         logger.console("\nStarting Honeycomb service ...")
57
58         cmd = "{0}/bin/start".format(Const.REMOTE_HC_DIR)
59
60         for node in nodes:
61             if node['type'] == NodeType.DUT:
62                 ssh = SSH()
63                 ssh.connect(node)
64                 (ret_code, _, _) = ssh.exec_command_sudo(cmd)
65                 if int(ret_code) != 0:
66                     raise HoneycombError('Node {0} failed to start Honeycomb.'.
67                                          format(node['host']))
68                 else:
69                     logger.info("Starting the Honeycomb service on node {0} is "
70                                 "in progress ...".format(node['host']))
71
72     @staticmethod
73     def stop_honeycomb_on_duts(*nodes):
74         """Stop the Honeycomb service on specified DUT nodes.
75
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
79         stopped.
80         :param nodes: List of nodes to stop Honeycomb on.
81         :type nodes: list
82         :raises HoneycombError: If Honeycomb failed to stop.
83         """
84         logger.console("\nShutting down Honeycomb service ...")
85
86         cmd = "{0}/bin/stop".format(Const.REMOTE_HC_DIR)
87         errors = []
88
89         for node in nodes:
90             if node['type'] == NodeType.DUT:
91                 ssh = SSH()
92                 ssh.connect(node)
93                 (ret_code, _, _) = ssh.exec_command_sudo(cmd)
94                 if int(ret_code) != 0:
95                     errors.append(node['host'])
96                 else:
97                     logger.info("Stopping the Honeycomb service on node {0} is "
98                                 "in progress ...".format(node['host']))
99         if errors:
100             raise HoneycombError('Node(s) {0} failed to stop Honeycomb.'.
101                                  format(errors))
102
103     @staticmethod
104     def check_honeycomb_startup_state(*nodes):
105         """Check state of Honeycomb service during startup on specified nodes.
106
107         Reads html path from template file oper_vpp_version.url.
108
109         Honeycomb nodes reply with connection refused or the following status
110         codes depending on startup progress: codes 200, 401, 403, 404, 500, 503
111
112         :param nodes: List of DUT nodes starting Honeycomb.
113         :type nodes: list
114         :return: True if all GETs returned code 200(OK).
115         :rtype bool
116         """
117         path = HcUtil.read_path_from_url_file("oper_vpp_version")
118         expected_status_codes = (HTTPCodes.UNAUTHORIZED,
119                                  HTTPCodes.FORBIDDEN,
120                                  HTTPCodes.NOT_FOUND,
121                                  HTTPCodes.SERVICE_UNAVAILABLE,
122                                  HTTPCodes.INTERNAL_SERVER_ERROR)
123
124         for node in nodes:
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 '
135                                     'password.')
136                     raise HoneycombError('Honeycomb on node {0} running but '
137                                          'not yet ready.'.format(node['host']),
138                                          enable_logging=False)
139                 else:
140                     raise HoneycombError('Unexpected return code: {0}.'.
141                                          format(status_code))
142         return True
143
144     @staticmethod
145     def check_honeycomb_shutdown_state(*nodes):
146         """Check state of Honeycomb service during shutdown on specified nodes.
147
148         Honeycomb nodes reply with connection refused or the following status
149         codes depending on shutdown progress: codes 200, 404.
150
151         :param nodes: List of DUT nodes stopping Honeycomb.
152         :type nodes: list
153         :return: True if all GETs fail to connect.
154         :rtype bool
155         """
156         cmd = "ps -ef | grep -v grep | grep karaf"
157         for node in nodes:
158             if node['type'] == NodeType.DUT:
159                 try:
160                     status_code, _ = HTTPRequest.get(node, '/index.html',
161                                                      timeout=5,
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)
171                     else:
172                         raise HoneycombError('Unexpected return code: {0}.'.
173                                              format(status_code))
174                 except HTTPRequestError:
175                     logger.debug('Connection refused, checking the process '
176                                  'state ...')
177                     ssh = SSH()
178                     ssh.connect(node)
179                     (ret_code, _, _) = ssh.exec_command_sudo(cmd)
180                     if ret_code == 0:
181                         raise HoneycombError('Honeycomb on node {0} is still '
182                                              'running.'.format(node['host']),
183                                              enable_logging=False)
184                     else:
185                         logger.info("Honeycomb on node {0} has stopped".
186                                     format(node['host']))
187         return True

©2016 FD.io a Linux Foundation Collaborative Project. All Rights Reserved.
Linux Foundation is a registered trademark of The Linux Foundation. Linux is a registered trademark of Linus Torvalds.
Please see our privacy policy and terms of use.