d6ac9efde02209ad4d86bd8f4518f4e694e37a44
[anna.git] / example / diameter / launcher / resources / rest_api / ct / conftest.py
1 # Keep sorted
2 import base64
3 from collections import defaultdict
4 import glob
5 from hyper import HTTP20Connection
6 #import inspect
7 import json
8 import logging
9 import os
10 import pytest
11 import re
12
13 #############
14 # CONSTANTS #
15 #############
16
17 # Endpoint
18 ADML_HOST = 'localhost'
19 ADML_PORT = '8074'
20 ADML_ENDPOINT = ADML_HOST + ':' + ADML_PORT
21 ADML_URI_PREFIX = ''
22
23 # Headers
24 CONTENT_LENGTH = 'content-length'
25
26 # Flow calculation throw admlf (ADML Flow) fixture:
27 #   flow = admlf.getId()
28 #
29 # For sequenced tests, the id is just a monotonically increased number from 1.
30 # For parallel tests, this id is sequenced from 1 for every worker, then, globally,
31 #  this is a handicap to manage flow ids (tests ids) for ADML FSM (finite state machine).
32 # We consider a base multiplier of 10000, so collisions would take place when workers
33 #  reserves more than 10000 flows. With a 'worst-case' assumption of `5 flows per test case`,
34 #  you should cover up to 5000 per worker. Anyway, feel free to increase this value,
35 #  specially if you are thinking in use pytest and ADML Agent for system tests.
36 FLOW_BASE_MULTIPLIER = 10000
37
38 ######################
39 # CLASSES & FIXTURES #
40 ######################
41
42 class Sequencer(object):
43     def __init__(self, request):
44         self.sequence = 0
45         self.request = request
46
47     def __wid(self):
48         """
49         Returns the worker id, or 'master' if not parallel execution is done
50         """
51         wid = 'master'
52         if hasattr(self.request.config, 'slaveinput'):
53           wid = self.request.config.slaveinput['slaveid']
54
55         return wid
56
57     def getId(self):
58         """
59         Returns the next identifier value (monotonically increased in every call)
60         """
61         self.sequence += 1
62
63         wid = self.__wid()
64         if wid == "master":
65           return self.sequence
66
67         # Workers are named: wd0, wd1, wd2, etc.
68         wid_number = int(re.findall(r'\d+', wid)[0])
69
70         return FLOW_BASE_MULTIPLIER * wid_number + self.sequence
71
72
73
74 @pytest.fixture(scope='session')
75 def admlf(request):
76   """
77   ADML Flow
78   """
79   return Sequencer(request)
80
81
82
83 # Logging
84 class MyLogger:
85
86   # CRITICAL ERROR WARNING INFO DEBUG NOSET
87   def setLevelInfo(): logging.getLogger().setLevel(logging.INFO)
88   def setLevelDebug(): logging.getLogger().setLevel(logging.DEBUG)
89
90   def error(message): logging.getLogger().error(message)
91   def warning(message): logging.getLogger().warning(message)
92   def info(message): logging.getLogger().info(message)
93   def debug(message): logging.getLogger().debug(message)
94
95 @pytest.fixture(scope='session')
96 def mylogger():
97   return MyLogger
98
99 MyLogger.logger = logging.getLogger('CT')
100
101 # Base64 encoding:
102 @pytest.fixture(scope='session')
103 def b64_encode():
104   def encode(message):
105     message_bytes = message.encode('ascii')
106     base64_bytes = base64.b64encode(message_bytes)
107     return base64_bytes.decode('ascii')
108   return encode
109
110 # Base64 decoding:
111 @pytest.fixture(scope='session')
112 def b64_decode():
113   def decode(base64_message):
114     base64_bytes = base64_message.encode('ascii')
115     message_bytes = base64.b64decode(base64_bytes)
116     return message_bytes.decode('ascii')
117   return decode
118
119 # HTTP communication:
120 class RestClient(object):
121     """A client helper to perform rest operations: GET, POST.
122
123     Attributes:
124         endpoint: server endpoint to make the HTTP2.0 connection
125     """
126
127     def __init__(self, endpoint):
128         """Return a RestClient object for ADML endpoint."""
129         self._endpoint = endpoint
130         self._ip = self._endpoint.split(':')[0]
131         self._connection = HTTP20Connection(host=self._endpoint)
132
133     def _log_http(self, kind, method, url, body, headers):
134         length = len(body) if body else 0
135         MyLogger.info(
136                 '{} {}{} {} headers: {!s} data: {}:{!a}'.format(
137                 method, self._endpoint, url, kind, headers, length, body))
138
139     def _log_request(self, method, url, body, headers):
140         self._log_http('REQUEST', method, url, body, headers)
141
142     def _log_response(self, method, url, response):
143         self._log_http(
144                 'RESPONSE:{}'.format(response["status"]), method, url,
145                 response["body"], response["headers"])
146
147     #def log_event(self, level, log_msg):
148     #    # Log caller function name and formated message
149     #    MyLogger.logger.log(level, inspect.getouterframes(inspect.currentframe())[1].function + ': {!a}'.format(log_msg))
150
151     def parse(self, response):
152         response_body = response.read(decode_content=True).decode('utf-8')
153         if len(response_body) != 0:
154           response_body_dict = json.loads(response_body)
155         else:
156           response_body_dict = ''
157         response_data = { "status":response.status, "body":response_body_dict, "headers":response.headers }
158         return response_data
159
160     def request(self, requestMethod, requestUrl, requestBody=None, requestHeaders=None):
161       """
162       Returns response data dictionary with 'status', 'body' and 'headers'
163       """
164       requestBody = RestClient._pad_body_and_length(requestBody, requestHeaders)
165       self._log_request(requestMethod, requestUrl, requestBody, requestHeaders)
166       self._connection.request(method=requestMethod, url=requestUrl, body=requestBody, headers=requestHeaders)
167       response = self.parse(self._connection.get_response())
168       self._log_response(requestMethod, requestUrl, response)
169       return response
170
171     def _pad_body_and_length(requestBody, requestHeaders):
172         """Pad the body and adjust content-length if needed.
173         When the length of the body is multiple of 1024 this function appends
174         one space to the body and increases by one the content-length.
175
176         This is a workaround for hyper issue 355 [0].
177         The issue has been fixed but it has not been released yet.
178
179         [0]: https://github.com/Lukasa/hyper/issues/355
180
181         EXAMPLE
182         >>> body, headers = ' '*1024, { 'content-length':'41' }
183         >>> body = RestClient._pad_body_and_length(body, headers)
184         >>> ( len(body), headers['content-length'] )
185         (1025, '42')
186         """
187         if requestBody and 0 == (len(requestBody) % 1024):
188             logging.warning( "RestClient.request:" +
189                              " padding body because" +
190                              " its length ({})".format(len(requestBody)) +
191                              " is multiple of 1024")
192             requestBody += " "
193             content_length = CONTENT_LENGTH
194             if requestHeaders and content_length in requestHeaders:
195                 length = int(requestHeaders[content_length])
196                 requestHeaders[content_length] = str(length+1)
197         return requestBody
198
199     def get(self, requestUrl):
200         return self.request('GET', requestUrl)
201
202     def post(self, requestUrl, requestBody = None, requestHeaders={'content-type': 'application/json'}):
203         return self.request('POST', requestUrl, requestBody, requestHeaders)
204
205     def postDict(self, requestUrl, requestBody = None, requestHeaders={'content-type': 'application/json'}):
206         """
207            Accepts request body as python dictionary
208         """
209         requestBodyJson = None
210         if requestBody: requestBodyJson = json.dumps(requestBody, indent=None, separators=(',', ':'))
211         return self.request('POST', requestUrl, requestBodyJson, requestHeaders)
212
213
214     #def delete(self, requestUrl):
215     #  return self.request('DELETE', requestUrl)
216
217     def __assert_received_expected(self, received, expected, what):
218         match = (received == expected)
219         log = "Received {what}: {received} | Expected {what}: {expected}".format(received=received, expected=expected, what=what)
220         if match: MyLogger.info(log)
221         else: MyLogger.error(log)
222
223         assert match
224
225     def check_response_status(self, received, expected, **kwargs):
226         """
227         received: status code received (got from response data parsed, field 'status')
228         expected: status code expected
229         """
230         self.__assert_received_expected(received, expected, "status code")
231
232     #def check_expected_cause(self, response, **kwargs):
233     #    """
234     #    received: response data parsed where field 'body'->'cause' is analyzed
235     #    kwargs: aditional regexp to match expected cause
236     #    """
237     #    if "expected_cause" in kwargs:
238     #        received_content = response["body"]
239     #        received_cause = received_content.get("cause", "")
240     #        regular_expr_cause = kwargs["expected_cause"]
241     #        regular_expr_flag = kwargs.get("regular_expression_flag", 0)
242     #        matchObj = re.match(regular_expr_cause, received_cause, regular_expr_flag)
243     #        log = 'Test error cause: "{}"~=/{}/.'.format(received_cause, regular_expr_cause)
244     #        if matchObj: MyLogger.info(log)
245     #        else: MyLogger.error(log)
246     #
247     #        assert matchObj is not None
248
249     def check_response_body(self, received, expected, inputJsonString = False):
250         """
251         received: body content received (got from response data parsed, field 'body')
252         expected: body content expected
253         inputJsonString: input parameters as json string (default are python dictionaries)
254         """
255         if inputJsonString:
256           # Decode json:
257           received = json.loads(received)
258           expected = json.loads(expected)
259
260         self.__assert_received_expected(received, expected, "body")
261
262     def check_response_headers(self, received, expected):
263         """
264         received: headers received (got from response data parsed, field 'headers')
265         expected: headers expected
266         """
267         self.__assert_received_expected(received, expected, "headers")
268
269     def assert_response__status_body_headers(self, response, status, bodyDict, headersDict = None):
270         """
271         response: Response parsed data
272         status: numeric status code
273         body: body dictionary to match with
274         headers: headers dictionary to match with (by default, not checked: internally length and content-type application/json is verified)
275         """
276         self.check_response_status(response["status"], status)
277         self.check_response_body(response["body"], bodyDict)
278         if headersDict: self.check_response_headers(response["headers"], headersDict)
279
280
281     def close(self):
282       self._connection.close()
283
284
285 # ADML Client simple fixture
286 @pytest.fixture(scope='session')
287 def admlc():
288   admlc = RestClient(ADML_ENDPOINT)
289   yield admlc
290   admlc.close()
291   print("ADMLC Teardown")
292
293 @pytest.fixture(scope='session')
294 def resources():
295   resourcesDict={}
296   MyLogger.info("Gathering test suite resources ...")
297   for resource in glob.glob('resources/*'):
298     f = open(resource, "r")
299     name = os.path.basename(resource)
300     resourcesDict[name] = f.read()
301     f.close()
302
303   def get_resources(key, **kwargs):
304     # Be careful with templates containing curly braces:
305     # https://stackoverflow.com/questions/5466451/how-can-i-print-literal-curly-brace-characters-in-python-string-and-also-use-fo
306     resource = resourcesDict[key]
307
308     if kwargs:
309       args = defaultdict (str, kwargs)
310       resource = resource.format_map(args)
311
312     return resource
313
314   yield get_resources
315
316 ################
317 # Experimental #
318 ################
319
320 REQUEST_BODY_DIAMETER_HEX = '''
321 {{
322    "diameterHex":"{diameterHex}"
323 }}'''
324
325 REQUEST_BODY_NODE = {
326     "name":"{name}"
327 }
328
329
330 PARAMS = [
331   (ADML_URI_PREFIX, '/decode', REQUEST_BODY_DIAMETER_HEX, ADML_ENDPOINT),
332 ]
333
334 # Share RestClient connection for all the tests: session-scoped fixture
335 @pytest.fixture(scope="session", params=PARAMS)
336 def request_data(request):
337   admlc = RestClient(request.param[3])
338   def get_request_data(**kwargs):
339     args = defaultdict (str, kwargs)
340     uri_prefix = request.param[0]
341     request_uri_suffix=request.param[1]
342     formatted_uri=uri_prefix + request_uri_suffix.format_map(args)
343     request_body=request.param[2]
344     formatted_request_body=request_body.format_map(args)
345     return formatted_uri,formatted_request_body,admlc
346
347   yield get_request_data
348   admlc.close()
349   print("RestClient Teardown")
350
351 # Fixture usage example: requestUrl,requestBody,admlc = request_data(diameterHex="<hex content>")
352