Skip to content

Commit

Permalink
Merge pull request #211 from reportportal/develop
Browse files Browse the repository at this point in the history
Release
  • Loading branch information
HardNorth authored May 24, 2023
2 parents b0f5a4f + 1a6123e commit be275f3
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 19 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
# Changelog

## [Unreleased]
### Added
- `RPClient.clone()` method, by @HardNorth
### Fixed
- Client crash in case of Client ID reading error, by @HardNorth

## [5.3.2]
### Fixed
- Client crash in case of Client ID saving error, by @HardNorth

Expand Down
40 changes: 32 additions & 8 deletions reportportal_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ def __init__(self,
self.log_batch_payload_size = log_batch_payload_size
self.token = token
self.verify_ssl = verify_ssl
self.retries = retries
self.max_pool_size = max_pool_size
self.http_timeout = http_timeout
self.session = requests.Session()
self.step_reporter = StepReporter(self)
Expand Down Expand Up @@ -167,7 +169,7 @@ def finish_test_item(self,
"""Finish suite/case/step/nested step item.
:param item_id: ID of the test item
:param end_time: Test item end time
:param end_time: The item end time
:param status: Test status. Allowable values: "passed",
"failed", "stopped", "skipped", "interrupted",
"cancelled" or None
Expand Down Expand Up @@ -197,7 +199,6 @@ def finish_test_item(self,
verify_ssl=self.verify_ssl).make()
if not response:
return
# noinspection PyUnresolvedReferences
self._item_stack.pop() if len(self._item_stack) > 0 else None
logger.debug('finish_test_item - ID: %s', item_id)
logger.debug('response message: %s', response.message)
Expand Down Expand Up @@ -281,9 +282,9 @@ def log(self, time, message, level=None, attachment=None, item_id=None):
"""Send log message to the Report Portal.
:param time: Time in UTC
:param message: Log message
:param message: Log message text
:param level: Message's log level
:param attachment: Message attachments
:param attachment: Message's attachments
:param item_id: ID of the RP item the message belongs to
"""
self._log_manager.log(time, message, level, attachment, item_id)
Expand Down Expand Up @@ -370,7 +371,7 @@ def start_test_item(self,
"""Start case/step/nested step item.
:param name: Name of the test item
:param start_time: Test item start time
:param start_time: The item start time
:param item_type: Type of the test item. Allowable values:
"suite", "story", "test", "scenario", "step",
"before_class", "before_groups",
Expand All @@ -379,7 +380,7 @@ def start_test_item(self,
"after_method", "after_suite", "after_test"
:param attributes: Test item attributes
:param code_ref: Physical location of the test item
:param description: Test item description
:param description: The item description
:param has_stats: Set to False if test item is nested step
:param parameters: Set of parameters (for parametrized test items)
:param parent_item_id: An ID of a parent SUITE / STEP
Expand Down Expand Up @@ -418,7 +419,6 @@ def start_test_item(self,
item_id = response.id
if item_id is not NOT_FOUND:
logger.debug('start_test_item - ID: %s', item_id)
# noinspection PyUnresolvedReferences
self._item_stack.append(item_id)
else:
logger.warning('start_test_item - invalid response: %s',
Expand Down Expand Up @@ -452,5 +452,29 @@ def update_test_item(self, item_uuid, attributes=None, description=None):

def current_item(self):
"""Retrieve the last item reported by the client."""
# noinspection PyUnresolvedReferences
return self._item_stack[-1] if len(self._item_stack) > 0 else None

def clone(self):
"""Clone the client object, set current Item ID as cloned item ID.
:returns: Cloned client object
:rtype: RPClient
"""
cloned = RPClient(
endpoint=self.endpoint,
project=self.project,
token=self.token,
log_batch_size=self.log_batch_size,
is_skipped_an_issue=self.is_skipped_an_issue,
verify_ssl=self.verify_ssl,
retries=self.retries,
max_pool_size=self.max_pool_size,
launch_id=self.launch_id,
http_timeout=self.http_timeout,
log_batch_payload_size=self.log_batch_payload_size,
mode=self.mode
)
current_item = self.current_item()
if current_item:
cloned._item_stack.append(current_item)
return cloned
8 changes: 7 additions & 1 deletion reportportal_client/client.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,14 @@ class RPClient:
project: Text = ...
token: Text = ...
verify_ssl: bool = ...
retries: int = ...
max_pool_size: int = ...
http_timeout: Union[float, Tuple[float, float]] = ...
session: Session = ...
step_reporter: StepReporter = ...
mode: str = ...
_skip_analytics: Text = ...
_item_stack: List[Text] = ...

def __init__(
self,
Expand All @@ -41,7 +44,8 @@ class RPClient:
max_pool_size: int = ...,
launch_id: Text = ...,
http_timeout: Union[float, Tuple[float, float]] = ...,
log_batch_payload_size: int = ...
log_batch_payload_size: int = ...,
mode: str = ...
) -> None: ...

def finish_launch(self,
Expand Down Expand Up @@ -106,3 +110,5 @@ class RPClient:
def current_item(self) -> Text: ...

def start(self) -> None : ...

def clone(self) -> RPClient: ...
7 changes: 6 additions & 1 deletion reportportal_client/services/client_id.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,12 @@ def _store_client_id(client_id):

def get_client_id():
"""Return unique client ID of the instance, generate new if not exists."""
client_id = _read_client_id()
client_id = None
try:
client_id = _read_client_id()
except (PermissionError, IOError) as error:
logger.exception('[%s] Unknown exception has occurred. '
'Skipping client ID reading.', error)
if not client_id:
client_id = str(uuid4())
try:
Expand Down
12 changes: 6 additions & 6 deletions reportportal_client/services/statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def send_event(event_name, agent_name, agent_version):
:param agent_version: Version of the agent
"""
client_name, client_version = _get_client_info()
params = {
request_params = {
'client_name': client_name,
'client_version': client_version,
'interpreter': _get_platform_info(),
Expand All @@ -63,24 +63,24 @@ def send_event(event_name, agent_name, agent_version):
}

if agent_name:
params['agent_name'] = agent_name
request_params['agent_name'] = agent_name
if agent_version:
params['agent_version'] = agent_version
request_params['agent_version'] = agent_version

payload = {
'client_id': get_client_id(),
'events': [{
'name': event_name,
'params': params
'params': request_params
}]
}
headers = {'User-Agent': 'python-requests'}
params = {
query_params = {
'measurement_id': ID,
'api_secret': KEY
}
try:
return requests.post(url=ENDPOINT, json=payload, headers=headers,
params=params)
params=query_params)
except requests.exceptions.RequestException as err:
logger.debug('Failed to send data to Statistics service: %s', str(err))
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
aenum
delayed_assert
requests>=2.23.0
six>=1.15.0
requests>=2.27.1
six>=1.16.0
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from setuptools import setup, find_packages

__version__ = '5.3.2'
__version__ = '5.3.3'

TYPE_STUBS = ['*.pyi']

Expand Down
27 changes: 27 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,30 @@ def test_statistics(send_event, getenv):
client.session = mock.Mock()
client.start_launch('Test Launch', timestamp())
assert mock.call('start_launch', None, None) in send_event.mock_calls


def test_clone():
args = ['http://endpoint', 'project', 'token']
kwargs = {'log_batch_size': 30, 'is_skipped_an_issue': False,
'verify_ssl': False, 'retries': 5,
'max_pool_size': 30, 'launch_id': 'test-123',
'http_timeout': (30, 30),
'log_batch_payload_size': 1000000, 'mode': 'DEBUG'}
client = RPClient(*args, **kwargs)
client._item_stack.append('test-321')
client._item_stack.append('test-322')
cloned = client.clone()
assert cloned is not None and client is not cloned
assert cloned.endpoint == args[0] and cloned.project == args[
1] and cloned.token == args[2]
assert cloned.log_batch_size == kwargs[
'log_batch_size'] and cloned.is_skipped_an_issue == kwargs[
'is_skipped_an_issue'] and cloned.verify_ssl == kwargs[
'verify_ssl'] and cloned.retries == kwargs[
'retries'] and cloned.max_pool_size == kwargs[
'max_pool_size'] and cloned.launch_id == kwargs[
'launch_id'] and cloned.http_timeout == kwargs[
'http_timeout'] and cloned.log_batch_payload_size == kwargs[
'log_batch_payload_size'] and cloned.mode == kwargs['mode']
assert len(cloned._item_stack) == 1 \
and client.current_item() == cloned.current_item()

0 comments on commit be275f3

Please sign in to comment.