CollectorClient: swith to requests
CollecotrClient: use requests library instead of HTTPClient
Implement first real(not example) unit tests
HTTPClient is deprecated
Change-Id: I3cc1af18d5277c64b4a4426876ac39302af319e3
(cherry picked from commit 50d6291
)
This commit is contained in:
parent
c422f12492
commit
eda998dc98
0
core/_tests/models/__init__.py
Normal file
0
core/_tests/models/__init__.py
Normal file
137
core/_tests/models/test_collector_client.py
Normal file
137
core/_tests/models/test_collector_client.py
Normal file
@ -0,0 +1,137 @@
|
|||||||
|
from __future__ import absolute_import
|
||||||
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
|
import unittest
|
||||||
|
|
||||||
|
from mock import patch
|
||||||
|
from mock import call
|
||||||
|
|
||||||
|
from core.models.collector_client import CollectorClient
|
||||||
|
|
||||||
|
ip = '127.0.0.1'
|
||||||
|
endpoint = 'fake'
|
||||||
|
url = "http://{0}/{1}".format(ip, endpoint)
|
||||||
|
|
||||||
|
|
||||||
|
@patch('requests.get')
|
||||||
|
class TestCollectorClient(unittest.TestCase):
|
||||||
|
def setUp(self):
|
||||||
|
self.client = CollectorClient(collector_ip=ip, endpoint=endpoint)
|
||||||
|
|
||||||
|
def test_init(self, get):
|
||||||
|
self.assertEqual(self.client.url, url)
|
||||||
|
get.assert_not_called()
|
||||||
|
|
||||||
|
def test_get(self, get):
|
||||||
|
tgt = '/tst'
|
||||||
|
self.client._get(tgt)
|
||||||
|
get.assert_called_once_with(url=url + tgt)
|
||||||
|
|
||||||
|
def test_get_oswls(self, get):
|
||||||
|
master_node_uid = '0'
|
||||||
|
self.client.get_oswls(master_node_uid=master_node_uid)
|
||||||
|
get.assert_has_calls((
|
||||||
|
call(url=url + '/oswls/{0}'.format(master_node_uid)),
|
||||||
|
call().json(),
|
||||||
|
))
|
||||||
|
|
||||||
|
def test_get_installation_info(self, get):
|
||||||
|
master_node_uid = '0'
|
||||||
|
self.client.get_installation_info(master_node_uid=master_node_uid)
|
||||||
|
get.assert_has_calls((
|
||||||
|
call(url=url + '/installation_info/{0}'.format(
|
||||||
|
master_node_uid)),
|
||||||
|
call().json(),
|
||||||
|
))
|
||||||
|
|
||||||
|
def test_get_action_logs(self, get):
|
||||||
|
master_node_uid = '0'
|
||||||
|
self.client.get_action_logs(master_node_uid=master_node_uid)
|
||||||
|
get.assert_has_calls((
|
||||||
|
call(url=url + '/action_logs/{0}'.format(master_node_uid)),
|
||||||
|
call().json(),
|
||||||
|
))
|
||||||
|
|
||||||
|
def test_get_oswls_by_resource(self, get):
|
||||||
|
master_node_uid = '0'
|
||||||
|
resource = '1'
|
||||||
|
self.client.get_oswls_by_resource(
|
||||||
|
master_node_uid=master_node_uid,
|
||||||
|
resource=resource
|
||||||
|
)
|
||||||
|
get.assert_has_calls((
|
||||||
|
call(url=url + "/oswls/{0}/{1}".format(master_node_uid, resource)),
|
||||||
|
call().json(),
|
||||||
|
))
|
||||||
|
|
||||||
|
@patch(
|
||||||
|
'core.models.collector_client.CollectorClient.get_oswls_by_resource',
|
||||||
|
return_value={
|
||||||
|
'objs': [
|
||||||
|
{'resource_data': 'test0'},
|
||||||
|
{'resource_data': 'test1'},
|
||||||
|
]
|
||||||
|
}
|
||||||
|
)
|
||||||
|
def test_get_oswls_by_resource_data(self, get_oswls, get):
|
||||||
|
master_node_uid = '0'
|
||||||
|
resource = '1'
|
||||||
|
result = self.client.get_oswls_by_resource_data(
|
||||||
|
master_node_uid=master_node_uid,
|
||||||
|
resource=resource
|
||||||
|
)
|
||||||
|
get_oswls.assert_called_once_with(
|
||||||
|
master_node_uid,
|
||||||
|
resource
|
||||||
|
)
|
||||||
|
self.assertEqual(result, 'test0')
|
||||||
|
|
||||||
|
@patch(
|
||||||
|
'core.models.collector_client.CollectorClient.get_action_logs',
|
||||||
|
return_value=[
|
||||||
|
{'id': 0, 'body': {'additional_info': 'test0'}},
|
||||||
|
{'id': 1, 'body': {'additional_info': 'test1'}},
|
||||||
|
{'id': 2, 'body': {'additional_info': 'test2'}},
|
||||||
|
]
|
||||||
|
)
|
||||||
|
def test_get_action_logs_ids(self, logs, get):
|
||||||
|
master_node_uid = 0
|
||||||
|
result = self.client.get_action_logs_ids(master_node_uid)
|
||||||
|
logs.assert_called_once_with(master_node_uid)
|
||||||
|
self.assertEqual(result, [0, 1, 2])
|
||||||
|
|
||||||
|
@patch(
|
||||||
|
'core.models.collector_client.CollectorClient.get_action_logs',
|
||||||
|
return_value=[
|
||||||
|
{'id': 0, 'body': {'additional_info': 'test0'}},
|
||||||
|
{'id': 1, 'body': {'additional_info': 'test1'}},
|
||||||
|
{'id': 2, 'body': {'additional_info': 'test2'}},
|
||||||
|
]
|
||||||
|
)
|
||||||
|
def test_get_action_logs_additional_info_by_id(self, logs, get):
|
||||||
|
master_node_uid = 0
|
||||||
|
action_id = 1
|
||||||
|
result = self.client.get_action_logs_additional_info_by_id(
|
||||||
|
master_node_uid, action_id)
|
||||||
|
logs.assert_called_once_with(master_node_uid)
|
||||||
|
self.assertEqual(result, ['test1'])
|
||||||
|
|
||||||
|
@patch(
|
||||||
|
'core.models.collector_client.CollectorClient.get_action_logs_ids',
|
||||||
|
return_value=[0, 1, 2]
|
||||||
|
)
|
||||||
|
def test_get_action_logs_count(self, get_ids, get):
|
||||||
|
master_node_uid = 0
|
||||||
|
result = self.client.get_action_logs_count(master_node_uid)
|
||||||
|
get_ids.assert_called_once_with(master_node_uid)
|
||||||
|
self.assertEqual(result, 3)
|
||||||
|
|
||||||
|
@patch(
|
||||||
|
'core.models.collector_client.CollectorClient.get_installation_info',
|
||||||
|
return_value={'structure': 'test_result'}
|
||||||
|
)
|
||||||
|
def test_get_installation_info_data(self, get_inst_info, get):
|
||||||
|
master_node_uid = 0
|
||||||
|
result = self.client.get_installation_info_data(master_node_uid)
|
||||||
|
get_inst_info.assert_called_once_with(master_node_uid)
|
||||||
|
self.assertEqual(result, 'test_result')
|
@ -13,6 +13,8 @@
|
|||||||
# under the License.
|
# under the License.
|
||||||
|
|
||||||
import json
|
import json
|
||||||
|
from warnings import warn
|
||||||
|
|
||||||
# pylint: disable=import-error
|
# pylint: disable=import-error
|
||||||
# noinspection PyUnresolvedReferences
|
# noinspection PyUnresolvedReferences
|
||||||
from six.moves.urllib import request
|
from six.moves.urllib import request
|
||||||
@ -23,6 +25,11 @@ class HTTPClientZabbix(object):
|
|||||||
"""HTTPClientZabbix.""" # TODO documentation
|
"""HTTPClientZabbix.""" # TODO documentation
|
||||||
|
|
||||||
def __init__(self, url):
|
def __init__(self, url):
|
||||||
|
warn(
|
||||||
|
'HTTPClientZabbix is deprecated and not used now. '
|
||||||
|
'It will be dropped in short term period.',
|
||||||
|
DeprecationWarning
|
||||||
|
)
|
||||||
self.url = url
|
self.url = url
|
||||||
self.opener = request.build_opener(request.HTTPHandler)
|
self.opener = request.build_opener(request.HTTPHandler)
|
||||||
|
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
# Copyright 2013 Mirantis, Inc.
|
# Copyright 2016 Mirantis, Inc.
|
||||||
#
|
#
|
||||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||||
# not use this file except in compliance with the License. You may obtain
|
# not use this file except in compliance with the License. You may obtain
|
||||||
@ -12,10 +12,11 @@
|
|||||||
# License for the specific language governing permissions and limitations
|
# License for the specific language governing permissions and limitations
|
||||||
# under the License.
|
# under the License.
|
||||||
|
|
||||||
from core.helpers.http import HTTPClientZabbix
|
from __future__ import unicode_literals
|
||||||
# TODO(astepanov): switch to requests library
|
|
||||||
|
import requests
|
||||||
|
|
||||||
from fuelweb_test import logwrap
|
from fuelweb_test import logwrap
|
||||||
from fuelweb_test.helpers.decorators import json_parse
|
|
||||||
|
|
||||||
|
|
||||||
class CollectorClient(object):
|
class CollectorClient(object):
|
||||||
@ -23,35 +24,34 @@ class CollectorClient(object):
|
|||||||
|
|
||||||
def __init__(self, collector_ip, endpoint):
|
def __init__(self, collector_ip, endpoint):
|
||||||
url = "http://{0}/{1}".format(collector_ip, endpoint)
|
url = "http://{0}/{1}".format(collector_ip, endpoint)
|
||||||
self._client = HTTPClientZabbix(url=url)
|
self.__url = url
|
||||||
super(CollectorClient, self).__init__()
|
super(CollectorClient, self).__init__()
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def client(self):
|
def url(self):
|
||||||
return self._client
|
return self.__url
|
||||||
|
|
||||||
|
def _get(self, endpoint):
|
||||||
|
return requests.get(url=self.url + endpoint)
|
||||||
|
|
||||||
@logwrap
|
@logwrap
|
||||||
@json_parse
|
|
||||||
def get_oswls(self, master_node_uid):
|
def get_oswls(self, master_node_uid):
|
||||||
return self.client.get("/oswls/{0}".format(master_node_uid))
|
return self._get("/oswls/{0}".format(master_node_uid)).json()
|
||||||
|
|
||||||
@logwrap
|
@logwrap
|
||||||
@json_parse
|
|
||||||
def get_installation_info(self, master_node_uid):
|
def get_installation_info(self, master_node_uid):
|
||||||
return self.client.get("/installation_info/{0}".format(
|
return self._get("/installation_info/{0}".format(
|
||||||
master_node_uid))
|
master_node_uid)).json()
|
||||||
|
|
||||||
@logwrap
|
@logwrap
|
||||||
@json_parse
|
|
||||||
def get_action_logs(self, master_node_uid):
|
def get_action_logs(self, master_node_uid):
|
||||||
return self.client.get("/action_logs/{0}".format(
|
return self._get("/action_logs/{0}".format(
|
||||||
master_node_uid))
|
master_node_uid)).json()
|
||||||
|
|
||||||
@logwrap
|
@logwrap
|
||||||
@json_parse
|
|
||||||
def get_oswls_by_resource(self, master_node_uid, resource):
|
def get_oswls_by_resource(self, master_node_uid, resource):
|
||||||
return self.client.get("/oswls/{0}/{1}".format(master_node_uid,
|
return self._get("/oswls/{0}/{1}".format(master_node_uid,
|
||||||
resource))
|
resource)).json()
|
||||||
|
|
||||||
@logwrap
|
@logwrap
|
||||||
def get_oswls_by_resource_data(self, master_node_uid, resource):
|
def get_oswls_by_resource_data(self, master_node_uid, resource):
|
||||||
@ -65,8 +65,7 @@ class CollectorClient(object):
|
|||||||
|
|
||||||
@logwrap
|
@logwrap
|
||||||
def get_action_logs_count(self, master_node_uid):
|
def get_action_logs_count(self, master_node_uid):
|
||||||
return len([actions['id']
|
return len(self.get_action_logs_ids(master_node_uid))
|
||||||
for actions in self.get_action_logs(master_node_uid)])
|
|
||||||
|
|
||||||
@logwrap
|
@logwrap
|
||||||
def get_action_logs_additional_info_by_id(
|
def get_action_logs_additional_info_by_id(
|
||||||
|
Loading…
Reference in New Issue
Block a user