Skip to content

Commit 89850c2

Browse files
author
Anton Lysenko
authored
Implement interface for collection of the system information
1 parent 045f016 commit 89850c2

File tree

2 files changed

+80
-16
lines changed

2 files changed

+80
-16
lines changed

reportportal_client/service.py

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@
1414
limitations under the License.
1515
"""
1616

17-
1817
import collections
1918
import json
2019
import requests
2120
import uuid
2221
import logging
22+
import pkg_resources
23+
import platform
2324

2425
import six
2526
from requests.adapters import HTTPAdapter
@@ -42,14 +43,15 @@ def _convert_string(value):
4243
return str(value)
4344

4445

45-
def _list_to_payload(dictionary):
46+
def _dict_to_payload(dictionary):
4647
"""Convert dict to list of dicts.
4748
4849
:param dictionary: initial dict
4950
:return list: list of dicts
5051
"""
52+
system = dictionary.pop("system", False)
5153
return [
52-
{"key": key, "value": _convert_string(value)}
54+
{"key": key, "value": _convert_string(value), "system": system}
5355
for key, value in sorted(dictionary.items())
5456
]
5557

@@ -206,7 +208,7 @@ def start_launch(self,
206208
**kwargs):
207209
"""Start a new launch with the given parameters."""
208210
if attributes is not None:
209-
attributes = _list_to_payload(attributes)
211+
attributes = _dict_to_payload(attributes)
210212
data = {
211213
"name": name,
212214
"description": description,
@@ -261,9 +263,9 @@ def start_test_item(self,
261263
}
262264
"""
263265
if attributes:
264-
attributes = _list_to_payload(attributes)
266+
attributes = _dict_to_payload(attributes)
265267
if parameters:
266-
parameters = _list_to_payload(parameters)
268+
parameters = _dict_to_payload(parameters)
267269

268270
data = {
269271
"name": name,
@@ -309,7 +311,7 @@ def finish_test_item(self,
309311
issue = {"issue_type": "NOT_ISSUE"}
310312

311313
if attributes:
312-
attributes = _list_to_payload(attributes)
314+
attributes = _dict_to_payload(attributes)
313315

314316
data = {
315317
"endTime": end_time,
@@ -426,3 +428,28 @@ def log_batch(self, log_data, item_id=None):
426428
logger.debug("log_batch response: %s", r.text)
427429

428430
return _get_data(r)
431+
432+
@staticmethod
433+
def get_system_information(agent_name='agent_name'):
434+
"""
435+
Get system information about agent, os, cpu, system, etc.
436+
437+
:param agent_name: Name of the agent: pytest-reportportal,
438+
roborframework-reportportal,
439+
nosetest-reportportal,
440+
behave-reportportal
441+
:return: dict {'agent': pytest-pytest 5.0.5,
442+
'os': 'Windows',
443+
'cpu': 'AMD',
444+
'machine': "Windows10_pc"}
445+
"""
446+
try:
447+
agent_version = pkg_resources.get_distribution(agent_name)
448+
agent = '{0}-{1}'.format(agent_name, agent_version)
449+
except pkg_resources.DistributionNotFound:
450+
agent = 'not found'
451+
452+
return {'agent': agent,
453+
'os': platform.system(),
454+
'cpu': platform.processor(),
455+
'machine': platform.machine()}

tests/test_service.py

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
"""This modules includes unit tests for the service.py module."""
22

3-
43
from datetime import datetime
5-
from six.moves import mock
4+
from pkg_resources import DistributionNotFound
65

76
from delayed_assert import expect, assert_expectations
7+
import pytest
8+
from six.moves import mock
89

910
from reportportal_client.service import (
1011
_convert_string,
12+
_dict_to_payload,
1113
_get_data,
1214
_get_id,
1315
_get_json,
1416
_get_messages,
1517
_get_msg,
16-
_list_to_payload
18+
ReportPortalService
1719
)
1820

1921

@@ -26,12 +28,13 @@ def test_check_convert_to_string(self):
2628
expect(lambda: isinstance(_convert_string("Hello world"), str))
2729
assert_expectations()
2830

29-
def test_list_to_payload(self):
30-
"""Test convert dict to list of dicts."""
31-
initial_dict = {'key': "value", 'key1': 'value1'}
32-
expected_list = [{'key': 'key', 'value': 'value'},
33-
{'key': 'key1', 'value': 'value1'}]
34-
assert _list_to_payload(initial_dict) == expected_list
31+
@pytest.mark.parametrize('system', [True, False])
32+
def test_dict_to_payload_with_system_key(self, system):
33+
"""Test convert dict to list of dicts with key system."""
34+
initial_dict = {"aa": 1, "b": 2, "system": system}
35+
expected_list = [{'key': 'aa', 'value': '1', 'system': system},
36+
{'key': 'b', 'value': '2', 'system': system}]
37+
assert _dict_to_payload(initial_dict) == expected_list
3538

3639
def test_get_id(self, response):
3740
"""Test for the get_id function."""
@@ -82,3 +85,37 @@ def test_finish_launch(self, mock_get, rp_service):
8285
mock_get.return_value = {"id": 111}
8386
_get_msg = rp_service.finish_launch('name', datetime.now().isoformat())
8487
assert _get_msg == {"id": 111}
88+
89+
@mock.patch('platform.system', mock.Mock(return_value='linux'))
90+
@mock.patch('platform.machine', mock.Mock(return_value='Windows-PC'))
91+
@mock.patch('platform.processor', mock.Mock(return_value='amd'))
92+
@mock.patch('pkg_resources.get_distribution',
93+
mock.Mock(return_value='pytest 5.0'))
94+
def test_get_system_information(self):
95+
"""Test for validate get_system_information."""
96+
97+
expected_result = {'agent': 'pytest-pytest 5.0',
98+
'cpu': 'amd',
99+
'machine': 'Windows-PC',
100+
'os': 'linux'}
101+
102+
cond = (ReportPortalService.get_system_information('pytest')
103+
== expected_result)
104+
assert cond
105+
106+
@mock.patch('platform.system', mock.Mock(return_value='linux'))
107+
@mock.patch('platform.machine', mock.Mock(return_value='Windows-PC'))
108+
@mock.patch('platform.processor', mock.Mock(return_value='amd'))
109+
@mock.patch('pkg_resources.get_distribution',
110+
mock.Mock(side_effect=DistributionNotFound))
111+
def test_get_system_information_without_pkg(self):
112+
"""Test in negative form for validate get_system_information."""
113+
114+
expected_result = {'agent': 'not found',
115+
'cpu': 'amd',
116+
'machine': 'Windows-PC',
117+
'os': 'linux'}
118+
119+
cond = (ReportPortalService.get_system_information('pytest')
120+
== expected_result)
121+
assert cond

0 commit comments

Comments
 (0)