More pecan driver code && unittest

This commit is contained in:
tonytan4ever 2014-07-28 16:48:26 -04:00
parent 7ce901b9db
commit f0483d2913
9 changed files with 157 additions and 42 deletions

View File

@ -85,7 +85,7 @@ class ServicesController(base.ServicesController):
return services
def get(self, project_id):
def get(self, project_id, service_name):
# get the requested service from storage
return ""

View File

@ -13,6 +13,9 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import inspect
import pecan
from pecan import rest
@ -23,3 +26,25 @@ class Controller(rest.RestController):
def add_controller(self, path, controller):
setattr(self, path, controller)
def _handle_patch(self, method, remainder):
'''
Routes ``PATCH`` actions to the appropriate controller.
'''
# route to a patch_all or get if no additional parts are available
if not remainder or remainder == ['']:
controller = self._find_controller('patch_all', 'patch')
if controller:
return controller, []
pecan.abort(404)
controller = getattr(self, remainder[0], None)
if controller and not inspect.ismethod(controller):
return pecan.routing.lookup_controller(controller, remainder[1:])
# finally, check for the regular patch_one/patch requests
controller = self._find_controller('patch_one', 'patch')
if controller:
return controller, remainder
pecan.abort(404)

View File

@ -43,9 +43,3 @@ class RootController(base.Controller):
args.pop(1)
return super(RootController, self)._route(args, request)
@pecan.expose('json')
def get_all(self):
return {
'status': 'up',
}

View File

@ -13,10 +13,11 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import uuid
import json
import pecan
from cdn.openstack.common import local
from cdn.transport.pecan.controllers import base
@ -24,12 +25,41 @@ class ServicesController(base.Controller):
@pecan.expose('json')
def get_all(self):
tenant_id = pecan.request.context.to_dict()['tenant']
context = local.store.context
tenant_id = context.tenant
marker = pecan.request.GET.get('marker')
limit = pecan.request.GET.get('limit')
services_controller = self._driver.manager.services_controller
return services_controller.list(tenant_id)
return services_controller.list(tenant_id, marker, limit)
@pecan.expose('json')
def get_one(self):
tenant_id = pecan.request.context.to_dict()['tenant']
def get_one(self, service_name):
context = local.store.context
tenant_id = context.tenant
services_controller = self._driver.manager.services_controller
return services_controller.list(tenant_id)
return services_controller.get(tenant_id, service_name)
@pecan.expose('json')
def put(self, service_name):
context = local.store.context
tenant_id = context.tenant
services_controller = self._driver.manager.services_controller
service_json = json.loads(pecan.request.body.decode('utf-8'))
return services_controller.create(tenant_id, service_name,
service_json)
@pecan.expose('json')
def delete(self, service_name):
context = local.store.context
tenant_id = context.tenant
services_controller = self._driver.manager.services_controller
return services_controller.delete(tenant_id, service_name)
@pecan.expose('json')
def patch_one(self, service_name):
context = local.store.context
tenant_id = context.tenant
services_controller = self._driver.manager.services_controller
service_json = json.loads(pecan.request.body.decode('utf-8'))
return services_controller.update(tenant_id, service_name,
service_json)

View File

@ -56,8 +56,7 @@ class PecanTransportDriver(transport.Driver):
v1_controller = controllers.V1(self)
root_controller.add_controller('v1.0', v1_controller)
services_controller = controllers.Services(self)
v1_controller.add_controller('services', services_controller)
v1_controller.add_controller('services', controllers.Services(self))
def listen(self):
LOG.info(

View File

@ -1,4 +1,5 @@
[drivers]
provider = mock
transport = pecan
manager = default
storage = mockdb

View File

@ -1,10 +1,59 @@
# Copyright (c) 2014 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 tests.functional.transport.pecan import base
class ServiceControllerTest(base.FunctionalTest):
def test_get_all(self):
pass
#response = self.app.get('/health')
response = self.app.get('/v1.0/0001/services', params={
"marker" : 2,
"limit" : 3
})
#self.assertEqual(204, response.status_code)
self.assertEqual(200, response.status_code)
def test_get_one(self):
response = self.app.get('/v1.0/0001/services/fake_service_name')
self.assertEqual(200, response.status_code)
def test_create(self):
response = self.app.put('/v1.0/0001/services/fake_service_name_2',
params=json.dumps({
"domain": "www.mytest.com"
}), headers = {
"Content-Type" : "application/json"
})
self.assertEqual(200, response.status_code)
def test_update(self):
response = self.app.patch('/v1.0/0001/services/fake_service_name_3',
params=json.dumps({
"domain": "www.mytest.com"
}), headers = {
"Content-Type" : "application/json"
})
self.assertEqual(200, response.status_code)
def test_delete(self):
response = self.app.delete('/v1.0/0001/services/fake_service_name_4')
self.assertEqual(200, response.status_code)

View File

@ -1,24 +0,0 @@
# Copyright (c) 2014 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.
from tests.functional.transport.pecan import base
class ServicesControllerTest(base.FunctionalTest):
def test_get_all(self):
response = self.app.get('/v1.0/00001/services')
self.assertEqual(200, response.status_code)

View File

@ -0,0 +1,41 @@
# Copyright (c) 2014 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 os
import mock
from oslo.config import cfg
import testtools
from cdn.transport import pecan
class PecanTransportDriverTest(testtools.TestCase):
def test_listen(self):
tests_path = os.path.abspath(os.path.dirname(
os.path.dirname(
os.path.dirname(os.path.dirname(__file__)
))))
conf_path = os.path.join(tests_path, 'etc', 'default_functional.conf')
cfg.CONF(args=[], default_config_files=[conf_path])
mock_path = 'cdn.transport.pecan.driver.simple_server'
with mock.patch(mock_path) as mocked_module:
mock_server = mock.Mock()
mocked_module.make_server = mock.Mock(return_value=mock_server)
driver = pecan.Driver(cfg.CONF, None)
driver.listen()
self.assertTrue(mock_server.serve_forever.called)