Skip to content

Commit 46b0f91

Browse files
authored
Move functionality for getting system info to helpers
1 parent f960f6c commit 46b0f91

File tree

6 files changed

+79
-79
lines changed

6 files changed

+79
-79
lines changed

reportportal_client/helpers.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
"""
1414

1515
import logging
16+
from pkg_resources import DistributionNotFound, get_distribution
17+
from platform import machine, processor, system
1618

1719
logger = logging.getLogger(__name__)
1820

@@ -44,3 +46,31 @@ def gen_attributes(rp_attributes):
4446
logger.debug('Failed to process "{0}" attribute, attribute value'
4547
' should not be empty.'.format(rp_attr))
4648
return attrs
49+
50+
51+
def get_launch_sys_attrs():
52+
"""Generate attributes for the launch containing system information.
53+
54+
:return: dict {'os': 'Windows',
55+
'cpu': 'AMD',
56+
'machine': 'Windows10_pc'}
57+
"""
58+
return {
59+
'os': system(),
60+
'cpu': processor() or 'unknown',
61+
'machine': machine(),
62+
'system': True # This one is the flag for RP to hide these attributes
63+
}
64+
65+
66+
def get_package_version(package_name):
67+
"""Get version of the given package.
68+
69+
:param package_name: Name of the package
70+
:return: Version of the package
71+
"""
72+
try:
73+
package_version = get_distribution(package_name).version
74+
except DistributionNotFound:
75+
package_version = 'not found'
76+
return package_version

reportportal_client/helpers.pyi

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
from logging import Logger
2-
from typing import Dict, List
2+
from typing import Dict, List, Text
33

44
logger: Logger
55

66
def gen_attributes(rp_attributes: List) -> List[Dict]: ...
7+
8+
def get_launch_sys_attrs() -> Dict[Text]: ...
9+
10+
def get_package_version(package_name: Text) -> Text: ...

reportportal_client/service.py

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@
2020
import requests
2121
import uuid
2222
import logging
23-
import pkg_resources
24-
import platform
2523

2624
import six
2725
from six.moves.collections_abc import Mapping
@@ -532,29 +530,3 @@ def log_batch(self, log_data, item_id=None, force=False):
532530
continue
533531
else:
534532
raise
535-
536-
@staticmethod
537-
def get_system_information(agent_name='agent_name'):
538-
"""
539-
Get system information about agent, os, cpu, system, etc.
540-
541-
:param agent_name: Name of the agent: pytest-reportportal,
542-
roborframework-reportportal,
543-
nosetest-reportportal,
544-
behave-reportportal
545-
:return: dict {'agent': pytest-pytest 5.0.5,
546-
'os': 'Windows',
547-
'cpu': 'AMD',
548-
'machine': "Windows10_pc"}
549-
"""
550-
try:
551-
agent_version = pkg_resources.get_distribution(
552-
agent_name).version
553-
agent = '{0}-{1}'.format(agent_name, agent_version)
554-
except pkg_resources.DistributionNotFound:
555-
agent = 'not found'
556-
557-
return {'agent': agent,
558-
'os': platform.system(),
559-
'cpu': platform.processor() or 'unknown',
560-
'machine': platform.machine()}

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from setuptools import setup, find_packages
44

5-
__version__ = '5.0.5'
5+
__version__ = '5.0.6'
66

77
with open('requirements.txt') as f:
88
requirements = f.read().splitlines()

tests/test_helpers.py

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,51 @@
11
"""This modules contains unit tests for the helpers module."""
22

3-
from reportportal_client.helpers import gen_attributes
3+
from six.moves import mock
4+
5+
from reportportal_client.helpers import (
6+
gen_attributes,
7+
get_launch_sys_attrs,
8+
get_package_version
9+
)
410

511

612
def test_gen_attributes():
713
"""Test functionality of the gen_attributes function."""
814
expected_out = [{'value': 'Tag'}, {'key': 'Key', 'value': 'Value'}]
915
out = gen_attributes(['Tag', 'Key:Value', ''])
1016
assert expected_out == out
17+
18+
19+
@mock.patch('reportportal_client.helpers.system',
20+
mock.Mock(return_value='linux'))
21+
@mock.patch('reportportal_client.helpers.machine',
22+
mock.Mock(return_value='Windows-PC'))
23+
@mock.patch('reportportal_client.helpers.processor',
24+
mock.Mock(return_value='amd'))
25+
def test_get_launch_sys_attrs():
26+
"""Test for validate get_launch_sys_attrs function."""
27+
expected_result = {'cpu': 'amd',
28+
'machine': 'Windows-PC',
29+
'os': 'linux',
30+
'system': True}
31+
assert get_launch_sys_attrs() == expected_result
32+
33+
34+
@mock.patch('reportportal_client.helpers.system', mock.Mock())
35+
@mock.patch('reportportal_client.helpers.machine', mock.Mock())
36+
@mock.patch('reportportal_client.helpers.processor',
37+
mock.Mock(return_value=''))
38+
def test_get_launch_sys_attrs_docker():
39+
"""Test that cpu key value is not empty.
40+
41+
platform.processor() returns empty string in case it was called
42+
inside of the Docker container. API does not allow empty values
43+
for the attributes.
44+
"""
45+
result = get_launch_sys_attrs()
46+
assert result['cpu'] == 'unknown'
47+
48+
49+
def test_get_package_version():
50+
"""Test for the get_package_version() function-helper."""
51+
assert get_package_version('noname') == 'not found'

tests/test_service.py

Lines changed: 1 addition & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
"""This modules includes unit tests for the service.py module."""
22

33
from datetime import datetime
4-
from pkg_resources import DistributionNotFound
54

65
from delayed_assert import assert_expectations, expect
76
import pytest
@@ -14,8 +13,7 @@
1413
_get_id,
1514
_get_json,
1615
_get_messages,
17-
_get_msg,
18-
ReportPortalService
16+
_get_msg
1917
)
2018

2119

@@ -236,51 +234,6 @@ def test_get_launch_ui_url_no_id(self, rp_service, monkeypatch):
236234
assert url == '{0}/ui/#{1}/launches/all'.format(rp_service.endpoint,
237235
rp_service.project)
238236

239-
@mock.patch('platform.system', mock.Mock(return_value='linux'))
240-
@mock.patch('platform.machine', mock.Mock(return_value='Windows-PC'))
241-
@mock.patch('platform.processor', mock.Mock(return_value='amd'))
242-
@mock.patch('pkg_resources.get_distribution')
243-
def test_get_system_information(self, distro):
244-
"""Test for validate get_system_information."""
245-
distro.return_value.version = '5.0'
246-
expected_result = {'agent': 'reportportal-client-5.0',
247-
'cpu': 'amd',
248-
'machine': 'Windows-PC',
249-
'os': 'linux'}
250-
cond = (ReportPortalService.get_system_information(
251-
'reportportal-client') == expected_result)
252-
assert cond
253-
254-
@mock.patch('platform.system', mock.Mock())
255-
@mock.patch('platform.machine', mock.Mock())
256-
@mock.patch('platform.processor', mock.Mock(return_value=''))
257-
@mock.patch('pkg_resources.get_distribution', mock.Mock())
258-
def test_get_system_information_docker(self):
259-
"""Test that cpu key value is not empty.
260-
261-
platform.processor() returns empty string in case it was called
262-
inside of the Docker container. API does not allow empty values
263-
for the attributes.
264-
"""
265-
result = ReportPortalService.get_system_information('pytest')
266-
assert result['cpu'] == 'unknown'
267-
268-
@mock.patch('platform.system', mock.Mock(return_value='linux'))
269-
@mock.patch('platform.machine', mock.Mock(return_value='Windows-PC'))
270-
@mock.patch('platform.processor', mock.Mock(return_value='amd'))
271-
@mock.patch('pkg_resources.get_distribution',
272-
mock.Mock(side_effect=DistributionNotFound))
273-
def test_get_system_information_without_pkg(self):
274-
"""Test in negative form for validate get_system_information."""
275-
expected_result = {'agent': 'not found',
276-
'cpu': 'amd',
277-
'machine': 'Windows-PC',
278-
'os': 'linux'}
279-
280-
cond = (ReportPortalService.get_system_information('pytest')
281-
== expected_result)
282-
assert cond
283-
284237
@mock.patch('reportportal_client.service._get_data',
285238
mock.Mock(return_value={'id': 123}))
286239
def test_start_item(self, rp_service):

0 commit comments

Comments
 (0)