1
- # Copyright (c) 2018 http://reportportal.io
2
- #
3
- # Licensed under the Apache License, Version 2.0 (the "License");
4
- # you may not use this file except in compliance with the License.
5
- # You may obtain a copy of the License at
6
- #
7
- # http://www.apache.org/licenses/LICENSE-2.0
8
- #
9
- # Unless required by applicable law or agreed to in writing, software
10
- # distributed under the License is distributed on an "AS IS" BASIS,
11
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- # See the License for the specific language governing permissions and
13
- # limitations under the License.
1
+ """
2
+ Copyright (c) 2018 http://reportportal.io .
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ """
14
16
15
17
16
18
import collections
28
30
logger .addHandler (logging .NullHandler ())
29
31
30
32
31
- def _dict_to_payload (dictionary ):
32
- def _str (value ):
33
- if isinstance (value , six .text_type ):
34
- # Don't try to encode 'unicode' in Python 2.
35
- return value
36
- return str (value )
33
+ def _convert_string (value ):
34
+ """Support and convert strings in py2 and py3.
35
+
36
+ :param value: input string
37
+ :return value: convert string
38
+ """
39
+ if isinstance (value , six .text_type ):
40
+ # Don't try to encode 'unicode' in Python 2.
41
+ return value
42
+ return str (value )
43
+
44
+
45
+ def _list_to_payload (dictionary ):
46
+ """Convert dict to list of dicts.
37
47
48
+ :param dictionary: initial dict
49
+ :return list: list of dicts
50
+ """
38
51
return [
39
- {"key" : key , "value" : _str (value )}
40
- for key , value in dictionary .items ()
52
+ {"key" : key , "value" : _convert_string (value )}
53
+ for key , value in sorted ( dictionary .items () )
41
54
]
42
55
43
56
44
57
def _get_id (response ):
58
+ """Get id from Response.
59
+
60
+ :param response: Response object
61
+ :return id: int value of id
62
+ """
45
63
try :
46
64
return _get_data (response )["id" ]
47
65
except KeyError :
@@ -50,6 +68,12 @@ def _get_id(response):
50
68
51
69
52
70
def _get_msg (response ):
71
+ """
72
+ Get message from Response.
73
+
74
+ :param response: Response object
75
+ :return: data: json data
76
+ """
53
77
try :
54
78
return _get_data (response )
55
79
except KeyError :
@@ -58,6 +82,12 @@ def _get_msg(response):
58
82
59
83
60
84
def _get_data (response ):
85
+ """
86
+ Get data from Response.
87
+
88
+ :param response: Response object
89
+ :return: json data
90
+ """
61
91
data = _get_json (response )
62
92
error_messages = _get_messages (data )
63
93
error_count = len (error_messages )
@@ -76,6 +106,12 @@ def _get_data(response):
76
106
77
107
78
108
def _get_json (response ):
109
+ """
110
+ Get json from Response.
111
+
112
+ :param response: Response object
113
+ :return: data: json object
114
+ """
79
115
try :
80
116
if response .text :
81
117
return response .json ()
@@ -87,6 +123,12 @@ def _get_json(response):
87
123
88
124
89
125
def _get_messages (data ):
126
+ """
127
+ Get messages (ErrorCode) from Response.
128
+
129
+ :param data: dict of datas
130
+ :return list: Empty list or list of errors
131
+ """
90
132
error_messages = []
91
133
for ret in data .get ("responses" , [data ]):
92
134
if "errorCode" in ret :
@@ -109,6 +151,7 @@ def uri_join(*uri_parts):
109
151
110
152
Returns:
111
153
An uri string.
154
+
112
155
"""
113
156
return '/' .join (str (s ).strip ('/' ).strip ('\\ ' ) for s in uri_parts )
114
157
@@ -151,6 +194,7 @@ def __init__(self,
151
194
self .verify_ssl = verify_ssl
152
195
153
196
def terminate (self , * args , ** kwargs ):
197
+ """Call this to terminate the service."""
154
198
pass
155
199
156
200
def start_launch (self ,
@@ -160,8 +204,9 @@ def start_launch(self,
160
204
attributes = None ,
161
205
mode = None ,
162
206
** kwargs ):
207
+ """Start a new launch with the given parameters."""
163
208
if attributes is not None :
164
- attributes = _dict_to_payload (attributes )
209
+ attributes = _list_to_payload (attributes )
165
210
data = {
166
211
"name" : name ,
167
212
"description" : description ,
@@ -176,8 +221,10 @@ def start_launch(self,
176
221
return self .launch_id
177
222
178
223
def finish_launch (self , end_time , status = None , ** kwargs ):
179
- """
180
- status can be (PASSED, FAILED, STOPPED, SKIPPED, RESETED, CANCELLED)
224
+ """Finish a launch with the given parameters.
225
+
226
+ Status can be one of the followings:
227
+ (PASSED, FAILED, STOPPED, SKIPPED, RESETED, CANCELLED)
181
228
"""
182
229
data = {
183
230
"endTime" : end_time ,
@@ -199,9 +246,11 @@ def start_test_item(self,
199
246
has_stats = True ,
200
247
** kwargs ):
201
248
"""
202
- item_type can be (SUITE, STORY, TEST, SCENARIO, STEP, BEFORE_CLASS,
249
+ Item_type can be.
250
+
251
+ (SUITE, STORY, TEST, SCENARIO, STEP, BEFORE_CLASS,
203
252
BEFORE_GROUPS, BEFORE_METHOD, BEFORE_SUITE, BEFORE_TEST, AFTER_CLASS,
204
- AFTER_GROUPS, AFTER_METHOD, AFTER_SUITE, AFTER_TEST)
253
+ AFTER_GROUPS, AFTER_METHOD, AFTER_SUITE, AFTER_TEST).
205
254
206
255
attributes and parameters should be a dictionary
207
256
with the following format:
@@ -211,10 +260,10 @@ def start_test_item(self,
211
260
...
212
261
}
213
262
"""
214
- if attributes is not None :
215
- attributes = _dict_to_payload (attributes )
216
- if parameters is not None :
217
- parameters = _dict_to_payload (parameters )
263
+ if attributes :
264
+ attributes = _list_to_payload (attributes )
265
+ if parameters :
266
+ parameters = _list_to_payload (parameters )
218
267
219
268
data = {
220
269
"name" : name ,
@@ -226,7 +275,7 @@ def start_test_item(self,
226
275
"parameters" : parameters ,
227
276
"hasStats" : has_stats
228
277
}
229
- if parent_item_id is not None :
278
+ if parent_item_id :
230
279
url = uri_join (self .base_url_v2 , "item" , parent_item_id )
231
280
else :
232
281
url = uri_join (self .base_url_v2 , "item" )
@@ -243,13 +292,24 @@ def finish_test_item(self,
243
292
issue = None ,
244
293
attributes = None ,
245
294
** kwargs ):
295
+ """Finish the test item and return HTTP response.
296
+
297
+ :param item_id: id of the test item
298
+ :param end_time: time in UTC format
299
+ :param status: status of the test
300
+ :param issue: description of an issue
301
+ :param attributes: list of attributes
302
+ :param kwargs: other parameters
303
+ :return: json message
304
+
305
+ """
246
306
# check if skipped test should not be marked as "TO INVESTIGATE"
247
307
if issue is None and status == "SKIPPED" \
248
308
and not self .is_skipped_an_issue :
249
309
issue = {"issue_type" : "NOT_ISSUE" }
250
310
251
- if attributes is not None :
252
- attributes = _dict_to_payload (attributes )
311
+ if attributes :
312
+ attributes = _list_to_payload (attributes )
253
313
254
314
data = {
255
315
"endTime" : end_time ,
@@ -264,12 +324,27 @@ def finish_test_item(self,
264
324
return _get_msg (r )
265
325
266
326
def get_project_settings (self ):
327
+ """
328
+ Get settings from project.
329
+
330
+ :return: json body
331
+ """
267
332
url = uri_join (self .base_url_v1 , "settings" )
268
333
r = self .session .get (url = url , json = {}, verify = self .verify_ssl )
269
334
logger .debug ("settings" )
270
335
return _get_json (r )
271
336
272
337
def log (self , time , message , level = None , attachment = None , item_id = None ):
338
+ """
339
+ Create log for test.
340
+
341
+ :param time: time in UTC
342
+ :param message: description
343
+ :param level:
344
+ :param attachment: files
345
+ :param item_id: id of item
346
+ :return: id of item from response
347
+ """
273
348
data = {
274
349
"launchUuid" : self .launch_id ,
275
350
"time" : time ,
@@ -288,19 +363,18 @@ def log(self, time, message, level=None, attachment=None, item_id=None):
288
363
return _get_id (r )
289
364
290
365
def log_batch (self , log_data , item_id = None ):
291
- """Logs batch of messages with attachment.
366
+ """
367
+ Log batch of messages with attachment.
292
368
293
369
Args:
294
- log_data: list of log records.
370
+ log_data: list of log records.
295
371
log record is a dict of;
296
372
time, message, level, attachment
297
373
attachment is a dict of:
298
374
name: name of attachment
299
375
data: fileobj or content
300
376
mime: content type for attachment
301
-
302
377
"""
303
-
304
378
url = uri_join (self .base_url_v2 , "log" )
305
379
306
380
attachments = []
0 commit comments