Merge "Add unit tests to improve coverage"

This commit is contained in:
Jenkins
2016-06-10 15:15:51 +00:00
committed by Gerrit Code Review
11 changed files with 260 additions and 6 deletions

View File

@@ -15,8 +15,8 @@
try:
import ordereddict as collections
except ImportError:
import collections
except ImportError: # pragma: no cover
import collections # pragma: no cover
from poppy.transport.pecan.models.response import link

View File

@@ -22,7 +22,7 @@ from poppy.common import util
class Model(collections.OrderedDict):
'response class for rule'
"""response class for rule."""
def __init__(self, rule):
super(Model, self).__init__()

View File

@@ -64,6 +64,7 @@ def with_schema_pecan(request, schema=None, handler=custom_abort_pecan,
For an HTTP POST or PUT (RFC2616 unsafe methods) request, the schema is
used to validate the request body.
:param request: request object
:param schema: A JSON schema.
:param handler: A Function (Error_handler)
"""

View File

@@ -1,4 +1,7 @@
[drivers]
transport = nonexist
manager = nonexist
storage = nonexist
storage = nonexist
dns = non-exist
distributed_task = non-exist
metrics = non-exist

View File

@@ -22,9 +22,9 @@ import uuid
import ddt
import mock
from oslo_config import cfg
from oslo_context import context as context_utils
import six.moves as sm
from poppy.metrics.blueflood import driver
from poppy.metrics.blueflood.utils import client
@@ -54,6 +54,10 @@ class TestBlueFloodServiceController(base.TestCase):
self.metrics_driver = (
driver.BlueFloodMetricsDriver(self.conf))
# tear down mocked context below, that way it doesn't
# affect other tests that use oslo context
self.addCleanup(sm.reload_module, context_utils)
@ddt.data('requestCount', 'bandwidthOut', 'httpResponseCode_1XX',
'httpResponseCode_2XX', 'httpResponseCode_3XX',
'httpResponseCode_4XX', 'httpResponseCode_5XX')

View File

@@ -18,7 +18,7 @@
},
{ "name": "gif-rules",
"request_url": "/*.gif"
}
}
]
}
],

View File

@@ -0,0 +1,93 @@
# 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
import testtools
from oslo_context import context
from oslo_context import fixture
from webob.exc import HTTPClientError
from poppy.transport.pecan import hooks
class TestContextHook(testtools.TestCase):
def setUp(self):
super(TestContextHook, self).setUp()
self.useFixture(fixture.ClearRequestContext())
self.state = mock.Mock()
self.state.controller.__self__ = mock.MagicMock(autospec=True)
self.state.request = mock.Mock()
self.state.request.headers = {}
self.state.response.headers = {}
self.state.request.environ = {}
self.state.request.host_url = 'https://cdn.com'
self.state.request.path = '/v1.0/endpoint'
oslo_config_patcher = mock.patch('oslo_config.cfg.CONF')
self.mock_cfg = oslo_config_patcher.start()
self.mock_cfg.project_id_in_url = False
self.addCleanup(oslo_config_patcher.stop)
self.hook = hooks.context.ContextHook()
def test_context_in_local_store(self):
"""Mocks Oslo local store to ensure the context is stored.
This test exists to ensure we continue to populate a context in the
Oslo local store, thus allowing Oslo log to pick it up.
"""
tenant = '012345'
self.state.request.headers['X-Project-ID'] = tenant
self.hook.before(self.state)
self.assertIsNotNone(context.get_current())
self.assertIsInstance(
context.get_current(), hooks.context.PoppyRequestContext
)
def test_x_project_id_header(self):
tenant = '012345'
self.state.request.headers['X-Project-ID'] = tenant
self.hook.before(self.state)
self.assertEqual(tenant, context.get_current().tenant)
self.assertEqual(
self.state.request.host_url + '/v1.0',
context.get_current().base_url
)
def test_no_project_id(self):
self.state.request.headers['X-Auth-Token'] = 'auth_token'
self.assertRaises(HTTPClientError, self.hook.before, self.state)
def test_tenant_id_url_with_header_and_injection(self):
self.mock_cfg.project_id_in_url = True
header_tenant = '012345'
url_tenant = '567890'
self.state.request.headers['X-Project-ID'] = header_tenant
self.state.request.path = '/v1.0/' + url_tenant + '/services'
self.hook.before(self.state)
self.assertEqual(header_tenant, context.get_current().tenant)
self.assertEqual(
self.state.request.host_url + '/v1.0/' + header_tenant,
context.get_current().base_url
)

View File

@@ -0,0 +1,101 @@
{
"service_json": {
"name" : "mysite.com",
"domains": [
{"domain": "parsely.sage.com"},
{"domain": "shared.sage.com",
"protocol": "https",
"certificate": "shared"},
{"domain": "san.sage.com",
"protocol": "https",
"certificate": "san"},
{"domain": "rosemary.thyme.net",
"protocol": "http"}
],
"origins": [
{"origin": "mockdomain.com", "ssl": false, "port": 80}
],
"caching": [
{"name": "default", "ttl": 1200 },
{"name": "img-only",
"ttl": 1800,
"rules": [
{ "name": "jpeg-rules",
"request_url": "/*.jpeg"
},
{ "name": "gif-rules",
"request_url": "/*.gif"
}
]
}
],
"restrictions": [
{
"name": "website only",
"rules": [
{
"name": "mocksite.com",
"http_host": "www.mocksite.com"
}
]
},
{
"name": "graphic only",
"rules": [
{
"name": "mockgraphicsite.com",
"referrer": "www.mocksitegraphic.com"
}
]
}
],
"flavor_id" : "standard",
"provider_details": {
"provider_error": {
"provider_service_id": "000",
"access_urls": [
{
"access_url": "mock_access_url",
"operator_url": "mock_operator_url"
},
{
"log_delivery": [
{
"publicURL": "mock_public_log_delivery"
}
]
},
{
"dummy_key": "dummy_value"
}
],
"error_message": "Something error message!"
},
"provider_no_error": {
"provider_service_id": "000",
"access_urls": [
{
"access_url": "mock_access_url",
"operator_url": "mock_operator_url"
},
{
"log_delivery": [
{
"publicURL": "mock_public_log_delivery"
}
]
},
{
"dummy_key": "dummy_value"
}
]
}
}
},
"service_disabled_no_errors": {
"name" : "mysite.com",
"operator_status": "disabled"
}
}

View File

@@ -34,6 +34,20 @@ class TestDNSModel(base.TestCase):
self.assertEqual('false', dns_model['online'])
class TestDistributedTaskModel(base.TestCase):
def setUp(self):
super(TestDistributedTaskModel, self).setUp()
def test_distributed_task_is_alive(self):
distributed_task_model = health.DistributedTaskModel(True)
self.assertEqual('true', distributed_task_model['online'])
def test_distributed_task_is_not_alive(self):
distributed_task_model = health.DistributedTaskModel(False)
self.assertEqual('false', distributed_task_model['online'])
class TestStorageModel(base.TestCase):
def setUp(self):

View File

@@ -0,0 +1,38 @@
# Copyright (c) 2016 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 ddt
import mock
from poppy.model import service as model_service
from poppy.transport.pecan.models.response import service as resp_model
from tests.unit import base
@ddt.ddt
class TestServiceResponseModel(base.TestCase):
def setUp(self):
super(TestServiceResponseModel, self).setUp()
self.controller = mock.Mock()
self.controller.base_url = "http://mock.base.url"
@ddt.file_data('data_service_resp_model.json')
def test_service_response_model(self, service_json):
service_obj = model_service.Service.init_from_dict('000', service_json)
service_model = resp_model.Model(service_obj, self.controller)
self.assertEqual(service_json["name"], service_model['name'])