Skip to content

Commit e9eaceb

Browse files
authored
Log manager for the log items
1 parent bc27243 commit e9eaceb

File tree

12 files changed

+418
-175
lines changed

12 files changed

+418
-175
lines changed

reportportal_client/client.py

Lines changed: 123 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,13 @@
1919
import requests
2020
from requests.adapters import HTTPAdapter
2121

22+
from reportportal_client.core.log_manager import LogManager
23+
from reportportal_client.core.test_manager import TestManager
2224
from reportportal_client.core.rp_requests import (
2325
HttpRequest,
2426
LaunchStartRequest,
2527
LaunchFinishRequest
2628
)
27-
from reportportal_client.core.test_manager import TestManager
2829
from reportportal_client.helpers import uri_join
2930

3031
logger = logging.getLogger(__name__)
@@ -44,51 +45,120 @@ def __init__(self,
4445
retries=None,
4546
max_pool_size=50,
4647
launch_id=None,
47-
):
48+
**_):
4849
"""Initialize required attributes.
4950
50-
:param endpoint: Endpoint of report portal service
51-
:param project: Project name to use for launch names
52-
:param token: authorization token.
53-
:param log_batch_size: option to set the maximum number of
54-
logs
55-
that can be processed in one batch
56-
:param is_skipped_an_issue: option to mark skipped tests as not
57-
'To Investigate' items on Server side.
58-
:param verify_ssl: option to not verify ssl certificates
59-
:param max_pool_size: option to set the maximum number of
60-
connections to save in the pool.
51+
:param endpoint: Endpoint of the report portal service
52+
:param project: Project name to report to
53+
:param token: Authorization token
54+
:param log_batch_size: Option to set the maximum number of
55+
logs that can be processed in one batch
56+
:param is_skipped_an_issue: Option to mark skipped tests as not
57+
'To Investigate' items on the server side
58+
:param verify_ssl: Option to skip ssl verification
59+
:param max_pool_size: Option to set the maximum number of
60+
connections to save the pool.
6161
"""
6262
self._batch_logs = []
63+
self.api_v1, self.api_v2 = 'v1', 'v2'
6364
self.endpoint = endpoint
64-
self.log_batch_size = log_batch_size
6565
self.project = project
66-
self.token = token
66+
self.base_url_v1 = uri_join(
67+
self.endpoint, 'api/{}'.format(self.api_v1), self.project)
68+
self.base_url_v2 = uri_join(
69+
self.endpoint, 'api/{}'.format(self.api_v2), self.project)
70+
self.is_skipped_an_issue = is_skipped_an_issue
6771
self.launch_id = launch_id
72+
self.log_batch_size = log_batch_size
73+
self.token = token
6874
self.verify_ssl = verify_ssl
69-
self.is_skipped_an_issue = is_skipped_an_issue
70-
71-
self.api_v1 = 'v1'
72-
self.api_v2 = 'v2'
73-
self.base_url_v1 = uri_join(self.endpoint,
74-
"api/{}".format(self.api_v1),
75-
self.project)
76-
self.base_url_v2 = uri_join(self.endpoint,
77-
"api/{}".format(self.api_v2),
78-
self.project)
79-
8075
self.session = requests.Session()
8176
if retries:
8277
self.session.mount('https://', HTTPAdapter(
8378
max_retries=retries, pool_maxsize=max_pool_size))
8479
self.session.mount('http://', HTTPAdapter(
8580
max_retries=retries, pool_maxsize=max_pool_size))
86-
self.session.headers["Authorization"] = "bearer {0}".format(self.token)
81+
self.session.headers['Authorization'] = 'bearer {0}'.format(self.token)
82+
83+
self._log_manager = LogManager(
84+
self.endpoint, self.session, self.api_v2, self.launch_id,
85+
self.project, log_batch_size=log_batch_size)
86+
self._test_manager = TestManager(
87+
self.session, self.endpoint, project, self.launch_id)
88+
89+
def finish_launch(self,
90+
end_time,
91+
status=None,
92+
attributes=None,
93+
**kwargs):
94+
"""Finish launch.
95+
96+
:param end_time: Launch end time
97+
:param status: Launch status. Can be one of the followings:
98+
PASSED, FAILED, STOPPED, SKIPPED, RESETED,
99+
CANCELLED
100+
:param attributes: Launch attributes
101+
"""
102+
url = uri_join(self.base_url_v2, 'launch', self.launch_id, 'finish')
103+
request_payload = LaunchFinishRequest(
104+
end_time=end_time,
105+
status=status,
106+
attributes=attributes,
107+
**kwargs
108+
).payload
109+
response = HttpRequest(self.session.put, url=url, json=request_payload,
110+
verify_ssl=self.verify_ssl).make()
111+
logger.debug('finish_launch - ID: %s', self.launch_id)
112+
return response.message
113+
114+
def finish_test_item(self,
115+
item_id,
116+
end_time,
117+
status,
118+
issue=None,
119+
attributes=None,
120+
**kwargs):
121+
"""Finish suite/case/step/nested step item.
122+
123+
:param item_id: id of the test item
124+
:param end_time: time in UTC format
125+
:param status: status of the test
126+
:param issue: description of an issue
127+
:param attributes: list of attributes
128+
:param kwargs: other parameters
129+
:return: json message
130+
"""
131+
self._test_manager.finish_test_item(self.api_v2,
132+
item_id,
133+
end_time,
134+
status,
135+
issue=issue,
136+
attributes=attributes,
137+
**kwargs)
87138

88-
self._test_manager = TestManager(self.session,
89-
self.endpoint,
90-
project,
91-
self.launch_id)
139+
def get_project_settings(self):
140+
"""Get settings from project.
141+
142+
:return: json body
143+
"""
144+
url = uri_join(self.base_url_v1, 'settings')
145+
r = self.session.get(url=url, json={}, verify=self.verify_ssl)
146+
return r.json()
147+
148+
def log(self, time, message, level=None, attachment=None, item_id=None):
149+
"""Send log message to the Report Portal.
150+
151+
:param time: Time in UTC
152+
:param message: Log message
153+
:param level: Message's log level
154+
:param attachment: Message attachments
155+
:param item_id: ID of the RP item the message belongs to
156+
"""
157+
self._log_manager.log(time, message, level, attachment, item_id)
158+
159+
def start(self):
160+
"""Start the client."""
161+
self._log_manager.start()
92162

93163
def start_launch(self,
94164
name,
@@ -102,16 +172,16 @@ def start_launch(self,
102172
):
103173
"""Start a new launch with the given parameters.
104174
105-
:param name: Name of launch
175+
:param name: Launch name
106176
:param start_time: Launch start time
107177
:param description: Launch description
108178
:param attributes: Launch attributes
109179
:param mode: Launch mode
110-
:param rerun: Launch rerun
111-
:param rerun_of: Items to rerun in launch
180+
:param rerun: Enables launch rerun mode
181+
:param rerun_of: Rerun mode. Specifies launch to be re-runned.
182+
Should be used with the 'rerun' option.
112183
"""
113-
url = uri_join(self.base_url_v2, "launch")
114-
184+
url = uri_join(self.base_url_v2, 'launch')
115185
request_payload = LaunchStartRequest(
116186
name=name,
117187
start_time=start_time,
@@ -122,55 +192,25 @@ def start_launch(self,
122192
rerun_of=rerun_of,
123193
**kwargs
124194
).payload
125-
126195
response = HttpRequest(self.session.post,
127196
url=url,
128197
json=request_payload,
129-
verify=self.verify_ssl).make()
198+
verify_ssl=self.verify_ssl).make()
130199
self._test_manager.launch_id = self.launch_id = response.id
131-
logger.debug("start_launch - ID: %s", self.launch_id)
200+
logger.debug('start_launch - ID: %s', self.launch_id)
132201
return self.launch_id
133202

134-
def finish_launch(self,
135-
end_time,
136-
status=None,
137-
attributes=None,
138-
**kwargs):
139-
"""Finish launch.
140-
141-
:param end_time: Launch end time
142-
:param status: Launch status. Can be one of the followings:
143-
PASSED, FAILED, STOPPED, SKIPPED, RESETED,
144-
CANCELLED
145-
:param attributes: Launch attributes
146-
"""
147-
url = uri_join(self.base_url_v2, "launch", self.launch_id, "finish")
148-
149-
request_payload = LaunchFinishRequest(
150-
end_time=end_time,
151-
status=status,
152-
attributes=attributes,
153-
**kwargs
154-
).payload
155-
156-
response = HttpRequest(self.session.put, url=url, json=request_payload,
157-
verify=self.verify_ssl).make()
158-
159-
logger.debug("finish_launch - ID: %s", self.launch_id)
160-
return response.message
161-
162-
def start_item(self,
163-
name,
164-
start_time,
165-
item_type,
166-
description=None,
167-
attributes=None,
168-
parameters=None,
169-
parent_item_id=None,
170-
has_stats=True,
171-
code_ref=None,
172-
**kwargs
173-
):
203+
def start_test_item(self,
204+
name,
205+
start_time,
206+
item_type,
207+
description=None,
208+
attributes=None,
209+
parameters=None,
210+
parent_item_id=None,
211+
has_stats=True,
212+
code_ref=None,
213+
**kwargs):
174214
"""Start case/step/nested step item.
175215
176216
:param name: Name of test item
@@ -188,42 +228,13 @@ def start_item(self,
188228
start_time,
189229
item_type,
190230
description=description,
191-
attributes=attributes[0],
231+
attributes=attributes,
192232
parameters=parameters,
193233
parent_uuid=parent_item_id,
194234
has_stats=has_stats,
195235
code_ref=code_ref,
196236
**kwargs)
197237

198-
def finish_item(self,
199-
item_id,
200-
end_time,
201-
status,
202-
issue=None,
203-
attributes=None,
204-
**kwargs
205-
):
206-
"""Finish suite/case/step/nested step item.
207-
208-
:param item_id: id of the test item
209-
:param end_time: time in UTC format
210-
:param status: status of the test
211-
:param issue: description of an issue
212-
:param attributes: list of attributes
213-
:param kwargs: other parameters
214-
:return: json message
215-
"""
216-
self._test_manager.finish_test_item(self.api_v2,
217-
item_id,
218-
end_time,
219-
status,
220-
issue=issue,
221-
attributes=attributes[0],
222-
**kwargs)
223-
224-
def save_log(self, log_time, **kwargs):
225-
"""Save logs for test items.
226-
227-
:param log_time: Log time
228-
"""
229-
pass
238+
def terminate(self, *args, **kwargs):
239+
"""Call this to terminate the client."""
240+
self._log_manager.stop()

0 commit comments

Comments
 (0)