diff --git a/libra/mgm/mgm.py b/libra/mgm/mgm.py index 0a9c234a..b86513d0 100644 --- a/libra/mgm/mgm.py +++ b/libra/mgm/mgm.py @@ -127,10 +127,10 @@ class Server(object): ) return - if resp['status'] not in('200', '203'): + if resp.status_code not in('200', '203'): self.logger.error( 'Error geting status from Nova, error {0}' - .format(resp['status']) + .format(resp.status_code) ) return status = status['server'] diff --git a/libra/mgm/nova.py b/libra/mgm/nova.py index 0ba90510..aa5d2b82 100644 --- a/libra/mgm/nova.py +++ b/libra/mgm/nova.py @@ -103,9 +103,9 @@ class Node(object): nid=node_id, exc=sys.exc_info()[0] ) - if resp['status'] != '204': + if resp.status_code != 204: return False, 'Error deleting node {nid} status {stat}'.format( - node=node_id, stat=resp['status'] + nid=node_id, stat=resp.status_code ) return True, '' @@ -150,9 +150,9 @@ class Node(object): args = {'name': node_name} url = "/servers?{0}".format(urllib.urlencode(args)) resp, body = self.nova.get(url) - if resp['status'] not in ['200', '203']: + if resp.status_code not in [200, 203]: msg = "Error {0} searching for node with name {1}".format( - resp['status'], node_name + resp.status_code, node_name ) raise NotFound(msg) if len(body['servers']) != 1: @@ -165,9 +165,9 @@ class Node(object): args = {'name': image_name} url = "/images?{0}".format(urllib.urlencode(args)) resp, body = self.nova.get(url) - if resp['status'] not in ['200', '203']: + if resp.status_code not in [200, 203]: msg = "Error {0} searching for image with name {1}".format( - resp['status'], image_name + resp.status_code, image_name ) raise NotFound(msg) if len(body['images']) != 1: @@ -179,9 +179,9 @@ class Node(object): """ tries to find a flavor from the name """ url = "/flavors" resp, body = self.nova.get(url) - if resp['status'] not in ['200', '203']: + if resp.status_code not in [200, 203]: msg = "Error {0} searching for flavor with name {1}".format( - resp['status'], flavor_name + resp.status_code, flavor_name ) raise NotFound(msg) for flavor in body['flavors']: diff --git a/tests/test_lbaas_mgm.py b/tests/test_lbaas_mgm.py index 35dcd826..363831dc 100644 --- a/tests/test_lbaas_mgm.py +++ b/tests/test_lbaas_mgm.py @@ -1,19 +1,46 @@ import testtools import logging import mock -import httplib2 +import requests import json import mock_objects from libra.mgm.nova import Node, BuildError -fake_response = httplib2.Response({"status": '200'}) -fake_bad_response = httplib2.Response({"status": '500'}) -fake_del_response = httplib2.Response({"status": '204'}) fake_body = json.dumps({u'server': {u'status': u'ACTIVE', u'updated': u'2012-10-10T11:55:55Z', u'hostId': u'', u'user_id': u'18290556240782', u'name': u'lbass_0', u'links': [{u'href': u'https://az-1.region-a.geo-1.compute.hpcloudsvc.com/v1.1/58012755801586/servers/417773', u'rel': u'self'}, {u'href': u'https://az-1.region-a.geo-1.compute.hpcloudsvc.com/58012755801586/servers/417773', u'rel': u'bookmark'}], u'created': u'2012-10-10T11:55:55Z', u'tenant_id': u'58012755801586', u'image': {u'id': u'8419', u'links': [{u'href': u'https://az-1.region-a.geo-1.compute.hpcloudsvc.com/58012755801586/images/8419', u'rel': u'bookmark'}]}, u'adminPass': u'u2LKPA73msRTxDMC', u'uuid': u'14984389-8cc5-4780-be64-2d31ace662ad', u'accessIPv4': u'', u'metadata': {}, u'accessIPv6': u'', u'key_name': u'default', u'flavor': {u'id': u'100', u'links': [{u'href': u'https://az-1.region-a.geo-1.compute.hpcloudsvc.com/58012755801586/flavors/100', u'rel': u'bookmark'}]}, u'config_drive': u'', u'id': 417773, u'security_groups': [{u'name': u'default', u'links': [{u'href': u'https://az-1.region-a.geo-1.compute.hpcloudsvc.com/v1.1/58012755801586/os-security-groups/4008', u'rel': u'bookmark'}], u'id': 4008}], u'addresses': {}}}) -mock_request = mock.Mock(return_value=(fake_response, fake_body)) -mock_bad_request = mock.Mock(return_value=(fake_bad_response, "")) -mock_del_request = mock.Mock(return_value=(fake_del_response, "")) + + +class TestResponse(requests.Response): + """ + Class used to wrap requests.Response and provide some + convenience to initialize with a dict + """ + + def __init__(self, data): + self._text = None + super(TestResponse, self) + if isinstance(data, dict): + self.status_code = data.get('status', None) + self.headers = data.get('headers', None) + # Fake the text attribute to streamline Response creation + self._text = data.get('text', None) + else: + self.status_code = data + + def __eq__(self, other): + return self.__dict__ == other.__dict__ + + @property + def text(self): + return self._text + + +fake_response = TestResponse({"status": 200, "text": fake_body}) +fake_bad_response = TestResponse({"status": 500, "text": ""}) +fake_del_response = TestResponse({"status": 204, "text": ""}) +mock_request = mock.Mock(return_value=(fake_response)) +mock_bad_request = mock.Mock(return_value=(fake_bad_response)) +mock_del_request = mock.Mock(return_value=(fake_del_response)) class TestLBaaSMgmTask(testtools.TestCase): @@ -36,24 +63,24 @@ class TestLBaaSMgmNova(testtools.TestCase): self.api.nova.auth_token = "token" def testCreateNode(self): - with mock.patch.object(httplib2.Http, "request", mock_request): + with mock.patch.object(requests, "request", mock_request): with mock.patch('time.time', mock.Mock(return_value=1234)): data = self.api.build() self.assertEqual(data['id'], 417773) def testCreateNodeFail(self): - with mock.patch.object(httplib2.Http, "request", mock_bad_request): + with mock.patch.object(requests, "request", mock_bad_request): with mock.patch('time.time', mock.Mock(return_value=1234)): self.assertRaises(BuildError, self.api.build) def testDeleteNodeFail(self): - with mock.patch.object(httplib2.Http, "request", mock_bad_request): + with mock.patch.object(requests, "request", mock_bad_request): with mock.patch('time.time', mock.Mock(return_value=1234)): resp, data = self.api.delete('1234') self.assertFalse(resp) def testDeleteNodeSucceed(self): - with mock.patch.object(httplib2.Http, "request", mock_del_request): + with mock.patch.object(requests, "request", mock_del_request): with mock.patch('time.time', mock.Mock(return_value=1234)): resp, data = self.api.delete('1234') self.assertTrue(resp) diff --git a/tools/pip-requires b/tools/pip-requires index 1d34a7c6..14c4f5c7 100644 --- a/tools/pip-requires +++ b/tools/pip-requires @@ -2,4 +2,4 @@ eventlet gearman python-daemon requests>=1.0.0 -python_novaclient +python_novaclient>=2.11.1 diff --git a/tools/test-requires b/tools/test-requires index 9884b409..a45079ce 100644 --- a/tools/test-requires +++ b/tools/test-requires @@ -1,7 +1,6 @@ fixtures>=0.3.12 pep8 mock -httplib2 pyflakes>=0.6.1 python-subunit sphinx>=1.1.2