|
14 | 14 | See the License for the specific language governing permissions and
|
15 | 15 | limitations under the License.
|
16 | 16 | """
|
| 17 | +import logging |
| 18 | + |
| 19 | +import requests |
| 20 | +from requests.adapters import HTTPAdapter |
| 21 | + |
| 22 | +from reportportal_client.core.rp_requests import ( |
| 23 | + HttpRequest, |
| 24 | + LaunchStartRequest, |
| 25 | + LaunchFinishRequest |
| 26 | +) |
| 27 | +from reportportal_client.core.test_manager import TestManager |
| 28 | +from reportportal_client.helpers import uri_join |
| 29 | + |
| 30 | +logger = logging.getLogger(__name__) |
| 31 | +logger.addHandler(logging.NullHandler()) |
17 | 32 |
|
18 | 33 |
|
19 | 34 | class RPClient(object):
|
20 | 35 | """Report portal client."""
|
21 | 36 |
|
22 |
| - def __init__(self, base_url, username, password, project_name, api_version, |
23 |
| - **kwargs): |
| 37 | + def __init__(self, |
| 38 | + endpoint, |
| 39 | + project, |
| 40 | + token, |
| 41 | + log_batch_size=20, |
| 42 | + is_skipped_an_issue=True, |
| 43 | + verify_ssl=True, |
| 44 | + retries=None, |
| 45 | + max_pool_size=50, |
| 46 | + launch_id=None, |
| 47 | + ): |
24 | 48 | """Initialize required attributes.
|
25 | 49 |
|
26 |
| - :param base_url: Base url of Report Portal |
27 |
| - :param username: Username |
28 |
| - :param password: Password |
29 |
| - :param project_name: Name of project |
30 |
| - :param api_version: Version of API |
31 |
| - """ |
32 |
| - self._launch_uuid = None |
33 |
| - self._token = None |
34 |
| - self.api_version = api_version |
35 |
| - self.base_url = base_url |
36 |
| - self.password = password |
37 |
| - self.port = kwargs.get('port', None) |
38 |
| - self.project_name = project_name |
39 |
| - self.username = username |
40 |
| - |
41 |
| - @property |
42 |
| - def launch_uuid(self): |
43 |
| - """Get launch uuid.""" |
44 |
| - return self._launch_uuid |
45 |
| - |
46 |
| - @property |
47 |
| - def token(self): |
48 |
| - """Get the token.""" |
49 |
| - return self._token |
50 |
| - |
51 |
| - def _request(self, uri, token, **kwargs): |
52 |
| - """Make Rest calls with necessary params. |
53 |
| -
|
54 |
| - :param uri: Request URI |
55 |
| - :param token: Access token |
56 |
| - :return: :class:`Response <Response>` object |
| 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. |
57 | 61 | """
|
58 |
| - |
59 |
| - def start_launch(self, name, start_time, **kwargs): |
60 |
| - """Start launch. |
| 62 | + self._batch_logs = [] |
| 63 | + self.endpoint = endpoint |
| 64 | + self.log_batch_size = log_batch_size |
| 65 | + self.project = project |
| 66 | + self.token = token |
| 67 | + self.launch_id = launch_id |
| 68 | + 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 | + |
| 80 | + self.session = requests.Session() |
| 81 | + if retries: |
| 82 | + self.session.mount('https://', HTTPAdapter( |
| 83 | + max_retries=retries, pool_maxsize=max_pool_size)) |
| 84 | + self.session.mount('http://', HTTPAdapter( |
| 85 | + max_retries=retries, pool_maxsize=max_pool_size)) |
| 86 | + self.session.headers["Authorization"] = "bearer {0}".format(self.token) |
| 87 | + |
| 88 | + self._test_manager = TestManager(self.session, |
| 89 | + self.endpoint, |
| 90 | + project, |
| 91 | + self.launch_id) |
| 92 | + |
| 93 | + def start_launch(self, |
| 94 | + name, |
| 95 | + start_time, |
| 96 | + description=None, |
| 97 | + attributes=None, |
| 98 | + mode=None, |
| 99 | + rerun=False, |
| 100 | + rerun_of=None, |
| 101 | + **kwargs |
| 102 | + ): |
| 103 | + """Start a new launch with the given parameters. |
61 | 104 |
|
62 | 105 | :param name: Name of launch
|
63 | 106 | :param start_time: Launch start time
|
| 107 | + :param description: Launch description |
| 108 | + :param attributes: Launch attributes |
| 109 | + :param mode: Launch mode |
| 110 | + :param rerun: Launch rerun |
| 111 | + :param rerun_of: Items to rerun in launch |
64 | 112 | """
|
65 |
| - # uri = f'/api/{self.api_version}/{self.project_name}/launch' |
| 113 | + url = uri_join(self.base_url_v2, "launch") |
| 114 | + |
| 115 | + request_payload = LaunchStartRequest( |
| 116 | + name=name, |
| 117 | + start_time=start_time, |
| 118 | + attributes=attributes, |
| 119 | + description=description, |
| 120 | + mode=mode, |
| 121 | + rerun=rerun, |
| 122 | + rerun_of=rerun_of, |
| 123 | + **kwargs |
| 124 | + ).payload |
| 125 | + |
| 126 | + response = HttpRequest(self.session.post, |
| 127 | + url=url, |
| 128 | + json=request_payload, |
| 129 | + verify=self.verify_ssl).make() |
| 130 | + self._test_manager.launch_id = self.launch_id = response.id |
| 131 | + logger.debug("start_launch - ID: %s", self.launch_id) |
| 132 | + return self.launch_id |
| 133 | + |
| 134 | + def finish_launch(self, |
| 135 | + end_time, |
| 136 | + status=None, |
| 137 | + attributes=None, |
| 138 | + **kwargs): |
| 139 | + """Finish launch. |
66 | 140 |
|
67 |
| - def start_item(self, name, start_time, item_type, parent_uuid=None, |
68 |
| - **kwargs): |
| 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 | + ): |
69 | 174 | """Start case/step/nested step item.
|
70 | 175 |
|
71 |
| - :param name: Name of test item |
72 |
| - :param start_time: Test item start time |
73 |
| - :param item_type: Type of test item |
74 |
| - :param parent_uuid: Parent test item UUID |
| 176 | + :param name: Name of test item |
| 177 | + :param start_time: Test item start time |
| 178 | + :param item_type: Type of test item |
| 179 | + :param description: Test item description |
| 180 | + :param attributes: Test item attributes |
| 181 | + :param parameters: Test item parameters |
| 182 | + :param parent_item_id: Parent test item UUID |
| 183 | + :param has_stats: Does test item has stats or not |
| 184 | + :param code_ref: Test item code reference |
75 | 185 | """
|
76 |
| - # uri = f'/api/{self.api_version}/{self.project_name}/item/ |
77 |
| - # {parent_uuid}' |
78 |
| - |
79 |
| - def finish_item(self, item_uuid, end_time, **kwargs): |
| 186 | + return self._test_manager.start_test_item(self.api_v2, |
| 187 | + name, |
| 188 | + start_time, |
| 189 | + item_type, |
| 190 | + description=description, |
| 191 | + attributes=attributes[0], |
| 192 | + parameters=parameters, |
| 193 | + parent_uuid=parent_item_id, |
| 194 | + has_stats=has_stats, |
| 195 | + code_ref=code_ref, |
| 196 | + **kwargs) |
| 197 | + |
| 198 | + def finish_item(self, |
| 199 | + item_id, |
| 200 | + end_time, |
| 201 | + status, |
| 202 | + issue=None, |
| 203 | + attributes=None, |
| 204 | + **kwargs |
| 205 | + ): |
80 | 206 | """Finish suite/case/step/nested step item.
|
81 | 207 |
|
82 |
| - :param end_time: Item end time |
83 |
| - :param item_uuid: Item UUID |
84 |
| - """ |
85 |
| - # uri = f'/api/{self.api_version}/{self.project_name}/item/{item_uuid}' |
86 |
| - |
87 |
| - def finish_launch(self, end_time, **kwargs): |
88 |
| - """Finish launch. |
89 |
| -
|
90 |
| - :param end_time: Launch end time |
| 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 |
91 | 215 | """
|
92 |
| - # uri = f'/api/{self.api_version}/{self.project_name}/launch' \ |
93 |
| - # f'/{self.launch_uuid}/finish' |
| 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) |
94 | 223 |
|
95 | 224 | def save_log(self, log_time, **kwargs):
|
96 | 225 | """Save logs for test items.
|
97 | 226 |
|
98 | 227 | :param log_time: Log time
|
99 | 228 | """
|
100 |
| - # uri = f'/api/{self.api_version}/{self.project_name}/log' |
| 229 | + pass |
0 commit comments