Ground work for unit testing transport controller

Change-Id: Id0c544b737beb966e70a9ca0694060fd9bd08436
This commit is contained in:
Isaac Mungai 2016-08-19 15:18:14 -04:00
parent 6fa8a5959a
commit a465ef323b
3 changed files with 186 additions and 0 deletions

View File

@ -0,0 +1,90 @@
# Copyright (c) 2015 Rackspace, 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 mock
from oslo_context import context as context_utils
import six.moves as sm
import testtools
from poppy.transport.pecan import controllers
from poppy.transport.pecan.controllers import v1
from poppy.transport.validators import helpers
class BasePecanControllerUnitTest(testtools.TestCase):
def setUp(self, controller):
"""Engages all patches for unit testing controllers.
Patches the request, response, request context, and deserialization
decorator to satisfy all controller dependencies for unit testing.
:returns: None
"""
super(BasePecanControllerUnitTest, self).setUp()
self.addCleanup(
sm.reload_module,
controllers
)
self.addCleanup(
sm.reload_module,
v1
)
self.addCleanup(
sm.reload_module,
controller
)
self.addCleanup(
sm.reload_module,
context_utils
)
self.addCleanup(
sm.reload_module,
helpers
)
self.driver = mock.MagicMock()
self.response = mock.Mock()
context = mock.Mock()
context.tenant = '000000001'
context.user = 'user_id'
context_utils.get_current = context
context_utils.get_current.return_value = context
pecan_request_patcher = mock.patch('pecan.request')
self.request = pecan_request_patcher.start()
self.request.host_url = 'test_url'
self.request.base_url = 'test_url'
pecan_response_patcher = mock.patch('pecan.response')
self.response = pecan_response_patcher.start()
self.response.headers = {}
deco_patcher = mock.patch('poppy.transport.validators.helpers')
deco_patcher.start()
# Reload to engage patches
sm.reload_module(controller)
sm.reload_module(v1)
sm.reload_module(controllers)
sm.reload_module(helpers)
# self.addCleanup(deco_patcher.stop)
self.addCleanup(deco_patcher.stop)
self.addCleanup(pecan_response_patcher.stop)
self.addCleanup(pecan_request_patcher.stop)

View File

@ -0,0 +1,96 @@
# Copyright (c) 2015 Rackspace, 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 json
from webob import exc
from poppy.common import errors
from poppy.transport.pecan import controllers
import poppy.transport.pecan.controllers.v1.admin
from tests.unit.transport.pecan.controllers import base
class DomainMigrationControllerTests(base.BasePecanControllerUnitTest):
def setUp(self):
super(DomainMigrationControllerTests, self).setUp(
poppy.transport.pecan.controllers.v1.admin
)
self.controller = controllers.v1.admin.DomainMigrationController(
self.driver
)
self.manager = self.driver.manager
def test_migrate_domain_service_not_found(self):
payload = {
"project_id": "12345",
"service_id": "abcdef",
"domain_name": "www.mywebsite.com",
"new_cert": "scdn1.secure6.raxcdn.com.edgekey.net",
"cert_status": "create_in_progress"
}
self.request.body = bytearray(json.dumps(payload), encoding='utf-8')
self.manager.services_controller.migrate_domain.side_effect = (
errors.ServiceNotFound("Mock -- Couldn't find service!")
)
self.assertRaises(exc.HTTPNotFound, self.controller.post, payload)
def test_migrate_domain_raises_value_error(self):
payload = {
"project_id": "12345",
"service_id": "abcdef",
"domain_name": "www.mywebsite.com",
"new_cert": "scdn1.secure6.raxcdn.com.edgekey.net",
"cert_status": "create_in_progress"
}
self.request.body = bytearray(json.dumps(payload), encoding='utf-8')
self.manager.services_controller.migrate_domain.side_effect = (
ValueError("Mock -- Couldn't find service provider details!")
)
self.assertRaises(exc.HTTPNotFound, self.controller.post, payload)
class BackgroundJobControllerTests(base.BasePecanControllerUnitTest):
def setUp(self):
super(BackgroundJobControllerTests, self).setUp(
poppy.transport.pecan.controllers.v1.admin
)
self.controller = controllers.v1.admin.BackgroundJobController(
self.driver
)
self.manager = self.driver.manager
def test_background_job_not_implemented(self):
payload = {
"job_type": "not_implemented_job_type",
}
self.request.body = bytearray(json.dumps(payload), encoding='utf-8')
self.manager.background_job_controller.post_job.side_effect = (
NotImplementedError("Mock -- Job type not implemented!")
)
self.assertRaises(
exc.HTTPClientError,
self.controller.post,
payload
)