Add iACL IPv4/IPv6 tests.
[csit.git] / resources / libraries / python / 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.topology import NodeType
19 from resources.libraries.python.ssh import SSH
20 from resources.libraries.python.HTTPRequest import HTTPRequest, HTTPCodes, \
21     HTTPRequestError
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
25
26
27 class HoneycombSetup(object):
28     """Implements keywords for Honeycomb setup.
29
30     The keywords implemented in this class make possible to:
31     - start Honeycomb,
32     - stop Honeycomb,
33     - check the Honeycomb start-up state,
34     - check the Honeycomb shutdown state,
35     - add VPP to the topology.
36     """
37
38     def __init__(self):
39         pass
40
41     @staticmethod
42     def start_honeycomb_on_duts(*nodes):
43         """Start Honeycomb on specified DUT nodes.
44
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
50         will fail.
51         :param nodes: List of nodes to start Honeycomb on.
52         :type nodes: list
53         :raises HoneycombError: If Honeycomb fails to start.
54         """
55         logger.console("Starting Honeycomb service ...")
56
57         cmd = "{0}/start".format(Const.REMOTE_HC_DIR)
58
59         for node in nodes:
60             if node['type'] == NodeType.DUT:
61                 ssh = SSH()
62                 ssh.connect(node)
63                 (ret_code, _, _) = ssh.exec_command_sudo(cmd)
64                 if int(ret_code) != 0:
65                     raise HoneycombError('Node {0} failed to start Honeycomb.'.
66                                          format(node['host']))
67                 else:
68                     logger.info("Starting the Honeycomb service on node {0} is "
69                                 "in progress ...".format(node['host']))
70
71     @staticmethod
72     def stop_honeycomb_on_duts(*nodes):
73         """Stop the Honeycomb service on specified DUT nodes.
74
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
78         stopped.
79         :param nodes: List of nodes to stop Honeycomb on.
80         :type nodes: list
81         :raises HoneycombError: If Honeycomb failed to stop.
82         """
83         logger.console("Shutting down Honeycomb service ...")
84
85         cmd = "{0}/stop".format(Const.REMOTE_HC_DIR)
86         errors = []
87
88         for node in nodes:
89             if node['type'] == NodeType.DUT:
90                 ssh = SSH()
91                 ssh.connect(node)
92                 (ret_code, _, _) = ssh.exec_command_sudo(cmd)
93                 if int(ret_code) != 0:
94                     errors.append(node['host'])
95                 else:
96                     logger.info("Stopping the Honeycomb service on node {0} is "
97                                 "in progress ...".format(node['host']))
98         if errors:
99             raise HoneycombError('Node(s) {0} failed to stop Honeycomb.'.
100                                  format(errors))
101
102     @staticmethod
103     def check_honeycomb_startup_state(*nodes):
104         """Check state of Honeycomb service during startup on specified nodes.
105
106         Reads html path from template file oper_vpp_version.url.
107
108         Honeycomb nodes reply with connection refused or the following status
109         codes depending on startup progress: codes 200, 401, 403, 404, 500, 503
110
111         :param nodes: List of DUT nodes starting Honeycomb.
112         :type nodes: list
113         :return: True if all GETs returned code 200(OK).
114         :rtype bool
115         """
116         path = HcUtil.read_path_from_url_file("oper_vpp_version")
117         expected_status_codes = (HTTPCodes.UNAUTHORIZED,
118                                  HTTPCodes.FORBIDDEN,
119                                  HTTPCodes.NOT_FOUND,
120                                  HTTPCodes.SERVICE_UNAVAILABLE,
121                                  HTTPCodes.INTERNAL_SERVER_ERROR)
122
123         for node in nodes:
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 '
134                                     'password.')
135                     raise HoneycombError('Honeycomb on node {0} running but '
136                                          'not yet ready.'.format(node['host']),
137                                          enable_logging=False)
138                 else:
139                     raise HoneycombError('Unexpected return code: {0}.'.
140                                          format(status_code))
141         return True
142
143     @staticmethod
144     def check_honeycomb_shutdown_state(*nodes):
145         """Check state of Honeycomb service during shutdown on specified nodes.
146
147         Honeycomb nodes reply with connection refused or the following status
148         codes depending on shutdown progress: codes 200, 404.
149
150         :param nodes: List of DUT nodes stopping Honeycomb.
151         :type nodes: list
152         :return: True if all GETs fail to connect.
153         :rtype bool
154         """
155         cmd = "ps -ef | grep -v grep | grep karaf"
156         for node in nodes:
157             if node['type'] == NodeType.DUT:
158                 try:
159                     status_code, _ = HTTPRequest.get(node, '/index.html',
160                                                      timeout=5,
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)
170                     else:
171                         raise HoneycombError('Unexpected return code: {0}.'.
172                                              format(status_code))
173                 except HTTPRequestError:
174                     logger.debug('Connection refused, checking the process '
175                                  'state ...')
176                     ssh = SSH()
177                     ssh.connect(node)
178                     (ret_code, _, _) = ssh.exec_command_sudo(cmd)
179                     if ret_code == 0:
180                         raise HoneycombError('Honeycomb on node {0} is still '
181                                              'running.'.format(node['host']),
182                                              enable_logging=False)
183                     else:
184                         logger.info("Honeycomb on node {0} has stopped".
185                                     format(node['host']))
186         return True