Add apirequest unit tests

Change-Id: If8adc0003b3c3c046a44076dfab0a0268057fb49
This commit is contained in:
Feodor Tersin 2014-11-10 21:22:56 +04:00
parent e4281fab82
commit f396b24ad3
2 changed files with 99 additions and 4 deletions

View File

@ -290,10 +290,10 @@ DOTTED_FAKE_PARAMS = {
'FakeList.2.FakeElemKey': 'fake',
'FakeComplexList.1.FakeElemKey.1.FakeSubElemKey': 'fake',
'FakeComplexList.1.FakeElemKey.2.FakeSubElemKey': 'fake',
'FakeComplexList.1.FakeElemKey1': 'fake',
'FakeComplexList.1.FakeElemKeyOther': 'fake',
'FakeComplexList.2.FakeElemKey.1.FakeSubElemKey': 'fake',
'FakeComplexList.2.FakeElemKey.2.FakeSubElemKey': 'fake',
'FakeComplexList.2.FakeElemKey1': 'fake',
'FakeComplexList.2.FakeElemKeyOther': 'fake',
}
DICT_FAKE_PARAMS = {
'fake_str': 'fake',
@ -305,8 +305,8 @@ DICT_FAKE_PARAMS = {
'fake_complex_list': [
{'fake_elem_key': [{'fake_sub_elem_key': 'fake'},
{'fake_sub_elem_key': 'fake'}],
'fake_elem_key_1': 'fake'},
'fake_elem_key_other': 'fake'},
{'fake_elem_key': [{'fake_sub_elem_key': 'fake'},
{'fake_sub_elem_key': 'fake'}],
'fake_elem_key_1': 'fake'}],
'fake_elem_key_other': 'fake'}],
}

View File

@ -0,0 +1,95 @@
# Copyright 2014 Cloudscaling Group, 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
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import collections
import uuid
from lxml import etree
import mock
from oslotest import base as test_base
from ec2api.api import apirequest
from ec2api.api import ec2client
from ec2api.tests import fakes_request_response as fakes
from ec2api.tests import matchers
from ec2api.tests import tools
class EC2RequesterTestCase(test_base.BaseTestCase):
fake_context_class = collections.namedtuple('FakeContext',
['request_id'])
fake_request_class = collections.namedtuple('FakeRequest',
['params', 'environ'])
def setUp(self):
super(EC2RequesterTestCase, self).setUp()
requester_patcher = mock.patch('ec2api.api.ec2client.EC2Requester')
self.requester = requester_patcher.start().return_value
self.addCleanup(requester_patcher.stop)
controller_patcher = mock.patch('ec2api.api.cloud.CloudController')
self.controller = controller_patcher.start().return_value
self.addCleanup(controller_patcher.stop)
self.fake_context = self.fake_context_class(str(uuid.uuid4()))
def test_invoke_returns_data(self):
self.controller.fake_action.return_value = fakes.DICT_FAKE_RESULT_DATA
api_request = apirequest.APIRequest('FakeAction', 'fake_v1',
{'Param': 'fake'})
result = api_request.invoke(self.fake_context)
self._compare_aws_xml('FakeActionResponse',
'http://ec2.amazonaws.com/doc/fake_v1/',
self.fake_context.request_id,
fakes.DICT_FAKE_RESULT_DATA,
result)
self.controller.fake_action.assert_called_once_with(
self.fake_context, param='fake')
def test_invoke_returns_true(self):
self.controller.fake_action.return_value = True
api_request = apirequest.APIRequest('FakeAction', 'fake_v1',
{'Param': 'fake'})
result = api_request.invoke(self.fake_context)
self._compare_aws_xml('FakeActionResponse',
'http://ec2.amazonaws.com/doc/fake_v1/',
self.fake_context.request_id,
{'return': True},
result)
self.controller.fake_action.assert_called_once_with(
self.fake_context, param='fake')
def test_invoke_prepare_params(self):
api_request = apirequest.APIRequest('FakeAction', 'fake_v1',
fakes.DOTTED_FAKE_PARAMS)
api_request.invoke(self.fake_context)
self.controller.fake_action.assert_called_once_with(
self.fake_context, **fakes.DICT_FAKE_PARAMS)
def _compare_aws_xml(self, root_tag, xmlns, request_id, dict_data,
observed):
# NOTE(ft): we cann't use matchers.XMLMatches since it makes comparison
# based on the order of tags
xml = etree.fromstring(observed)
self.assertEqual(xmlns, xml.nsmap.get(None))
observed_data = ec2client.EC2Client._parse_xml(observed)
expected = {root_tag: tools.update_dict(dict_data,
{'requestId': request_id})}
self.assertThat(observed_data, matchers.DictMatches(expected))