CollectorClient: swith to requests

CollecotrClient: use requests library instead of HTTPClient
Implement first real(not example) unit tests
HTTPClient is deprecated

Change-Id: I3cc1af18d5277c64b4a4426876ac39302af319e3
This commit is contained in:
Alexey Stepanov 2016-08-25 12:04:50 +03:00
parent 870fb4ff22
commit 50d6291c17
4 changed files with 163 additions and 20 deletions

View File

View 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')

View File

@ -13,6 +13,8 @@
# under the License.
import json
from warnings import warn
# pylint: disable=import-error
# noinspection PyUnresolvedReferences
from six.moves.urllib import request
@ -23,6 +25,11 @@ class HTTPClientZabbix(object):
"""HTTPClientZabbix.""" # TODO documentation
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.opener = request.build_opener(request.HTTPHandler)

View File

@ -1,4 +1,4 @@
# Copyright 2013 Mirantis, Inc.
# Copyright 2016 Mirantis, Inc.
#
# 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
@ -12,10 +12,11 @@
# License for the specific language governing permissions and limitations
# under the License.
from core.helpers.http import HTTPClientZabbix
# TODO(astepanov): switch to requests library
from __future__ import unicode_literals
import requests
from fuelweb_test import logwrap
from fuelweb_test.helpers.decorators import json_parse
class CollectorClient(object):
@ -23,35 +24,34 @@ class CollectorClient(object):
def __init__(self, collector_ip, endpoint):
url = "http://{0}/{1}".format(collector_ip, endpoint)
self._client = HTTPClientZabbix(url=url)
self.__url = url
super(CollectorClient, self).__init__()
@property
def client(self):
return self._client
def url(self):
return self.__url
def _get(self, endpoint):
return requests.get(url=self.url + endpoint)
@logwrap
@json_parse
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
@json_parse
def get_installation_info(self, master_node_uid):
return self.client.get("/installation_info/{0}".format(
master_node_uid))
return self._get("/installation_info/{0}".format(
master_node_uid)).json()
@logwrap
@json_parse
def get_action_logs(self, master_node_uid):
return self.client.get("/action_logs/{0}".format(
master_node_uid))
return self._get("/action_logs/{0}".format(
master_node_uid)).json()
@logwrap
@json_parse
def get_oswls_by_resource(self, master_node_uid, resource):
return self.client.get("/oswls/{0}/{1}".format(master_node_uid,
resource))
return self._get("/oswls/{0}/{1}".format(master_node_uid,
resource)).json()
@logwrap
def get_oswls_by_resource_data(self, master_node_uid, resource):
@ -65,8 +65,7 @@ class CollectorClient(object):
@logwrap
def get_action_logs_count(self, master_node_uid):
return len([actions['id']
for actions in self.get_action_logs(master_node_uid)])
return len(self.get_action_logs_ids(master_node_uid))
@logwrap
def get_action_logs_additional_info_by_id(