1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
|
# Copyright (c) 2010-2015 OpenStack Foundation
#
# 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 contextlib
from unittest import mock
import time
import unittest
import os
from swift.common.base_storage_server import BaseStorageServer, \
timing_stats, labeled_timing_stats
from swift.common.swob import Request, Response, HTTPInsufficientStorage
from test.debug_logger import debug_logger, debug_labeled_statsd_client
from tempfile import mkdtemp
from swift import __version__ as swift_version
from swift.common.utils import get_logger, public, replication
from shutil import rmtree
class FakeOPTIONS(BaseStorageServer):
server_type = 'test-server'
def __init__(self, conf, logger=None):
super(FakeOPTIONS, self).__init__(conf)
self.logger = logger or get_logger(conf, log_route='test-server')
class FakeANOTHER(FakeOPTIONS):
@public
def ANOTHER(self):
"""this is to test adding to allowed_methods"""
pass
@replication
@public
def REPLICATE(self):
"""this is to test replication_server"""
pass
@public
@replication
def REPLICATE2(self):
"""this is to test replication_server"""
pass
class MockLabeledTimingController(object):
def __init__(self, status, extra_labels=None):
self.statsd = debug_labeled_statsd_client({})
self.status = status
self.extra_labels = extra_labels or {}
def _update_labels(self, req, labels):
labels.update(self.extra_labels)
@labeled_timing_stats(metric='my_timing_metric')
def handle_req(self, req, timing_stats_labels):
self._update_labels(req, timing_stats_labels)
if isinstance(self.status, Exception):
raise self.status
return Response(status=self.status)
class TestLabeledTimingStatsDecorator(unittest.TestCase):
@contextlib.contextmanager
def _patch_time(self):
now = time.time()
with mock.patch('swift.common.utils.time.time', return_value=now):
yield now
def test_labeled_timing_stats_get_200(self):
req = Request.blank('/v1/a/c/o')
mock_controller = MockLabeledTimingController(200)
with self._patch_time() as now:
mock_controller.handle_req(req)
self.assertEqual(
{'timing_since': [(('my_timing_metric', now), {
'labels': {
'method': 'GET',
'status': 200,
}
})]},
mock_controller.statsd.calls)
def test_labeled_timing_stats_head_500(self):
req = Request.blank('/v1/a/c/o', method='HEAD')
mock_controller = MockLabeledTimingController(500)
with self._patch_time() as now:
mock_controller.handle_req(req)
self.assertEqual(
{'timing_since': [(('my_timing_metric', now), {
'labels': {
'method': 'HEAD',
'status': 500,
}
})]},
mock_controller.statsd.calls)
def test_labeled_timing_stats_head_507_exception(self):
req = Request.blank('/v1/a/c/o', method='HEAD')
mock_controller = MockLabeledTimingController(
HTTPInsufficientStorage())
with self._patch_time() as now:
mock_controller.handle_req(req)
self.assertEqual(
{'timing_since': [(('my_timing_metric', now), {
'labels': {
'method': 'HEAD',
'status': 507,
}
})]},
mock_controller.statsd.calls)
def test_labeled_timing_stats_extra_labels(self):
req = Request.blank('/v1/AUTH_test/c/o')
mock_controller = MockLabeledTimingController(
206, extra_labels={'account': 'AUTH_test'})
with self._patch_time() as now:
mock_controller.handle_req(req)
self.assertEqual(
{'timing_since': [(('my_timing_metric', now), {
'labels': {
'account': 'AUTH_test',
'method': 'GET',
'status': 206,
}
})]},
mock_controller.statsd.calls)
def test_labeled_timing_stats_can_not_override_status(self):
req = Request.blank('/v1/AUTH_test/c/o')
mock_controller = MockLabeledTimingController(
404, extra_labels={'status': 200})
with self._patch_time() as now:
mock_controller.handle_req(req)
self.assertEqual(
{'timing_since': [(('my_timing_metric', now), {
'labels': {
'method': 'GET',
'status': 404,
}
})]},
mock_controller.statsd.calls)
def test_labeled_timing_stats_can_not_override_method(self):
req = Request.blank('/v1/AUTH_test/c/o', method='POST')
mock_controller = MockLabeledTimingController(
412, extra_labels={'method': 'GET'})
with self._patch_time() as now:
mock_controller.handle_req(req)
self.assertEqual(
{'timing_since': [(('my_timing_metric', now), {
'labels': {
'method': 'POST',
'status': 412,
}
})]},
mock_controller.statsd.calls)
def test_labeled_timing_stats_really_can_not_override_method(self):
class MutilatingController(MockLabeledTimingController):
def _update_labels(self, req, labels):
req.method = 'BANANA'
req = Request.blank('/v1/AUTH_test/c/o', method='POST')
mock_controller = MutilatingController(412)
with self._patch_time() as now:
mock_controller.handle_req(req)
self.assertEqual('BANANA', req.method)
self.assertEqual(
{'timing_since': [(('my_timing_metric', now), {
'labels': {
'method': 'POST',
'status': 412,
}
})]},
mock_controller.statsd.calls)
def test_labeled_timing_stats_cannot_remove_labels(self):
class MutilatingController(MockLabeledTimingController):
def _update_labels(self, req, labels):
labels.clear()
req = Request.blank('/v1/AUTH_test/c/o', method='DELETE')
mock_controller = MutilatingController('42 bad stuff')
with self._patch_time() as now:
mock_controller.handle_req(req)
self.assertEqual(
{'timing_since': [(('my_timing_metric', now), {
'labels': {
'method': 'DELETE',
# resp.status_int knows how to do it
'status': 42,
}
})]},
mock_controller.statsd.calls)
class TestTimingStatsDecorators(unittest.TestCase):
def test_timing_stats(self):
class MockController(object):
def __init__(mock_self, status):
mock_self.status = status
mock_self.logger = debug_logger()
@timing_stats()
def METHOD(mock_self):
if isinstance(mock_self.status, Exception):
raise mock_self.status
return Response(status=mock_self.status)
now = time.time()
mock_controller = MockController(200)
with mock.patch('swift.common.utils.time.time', return_value=now):
mock_controller.METHOD()
self.assertEqual({'timing_since': [(('METHOD.timing', now), {})]},
mock_controller.logger.statsd_client.calls)
mock_controller = MockController(400)
with mock.patch('swift.common.utils.time.time', return_value=now):
mock_controller.METHOD()
self.assertEqual({'timing_since': [(('METHOD.timing', now), {})]},
mock_controller.logger.statsd_client.calls)
mock_controller = MockController(404)
with mock.patch('swift.common.utils.time.time', return_value=now):
mock_controller.METHOD()
self.assertEqual({'timing_since': [(('METHOD.timing', now), {})]},
mock_controller.logger.statsd_client.calls)
mock_controller = MockController(412)
with mock.patch('swift.common.utils.time.time', return_value=now):
mock_controller.METHOD()
self.assertEqual({'timing_since': [(('METHOD.timing', now), {})]},
mock_controller.logger.statsd_client.calls)
mock_controller = MockController(416)
with mock.patch('swift.common.utils.time.time', return_value=now):
mock_controller.METHOD()
self.assertEqual({'timing_since': [(('METHOD.timing', now), {})]},
mock_controller.logger.statsd_client.calls)
mock_controller = MockController(500)
with mock.patch('swift.common.utils.time.time', return_value=now):
mock_controller.METHOD()
self.assertEqual(
{'timing_since': [(('METHOD.errors.timing', now), {})]},
mock_controller.logger.statsd_client.calls)
mock_controller = MockController(507)
with mock.patch('swift.common.utils.time.time', return_value=now):
mock_controller.METHOD()
self.assertEqual(
{'timing_since': [(('METHOD.errors.timing', now), {})]},
mock_controller.logger.statsd_client.calls)
mock_controller = MockController(
HTTPInsufficientStorage())
with mock.patch('swift.common.utils.time.time', return_value=now):
mock_controller.METHOD()
self.assertEqual(
{'timing_since': [(('METHOD.errors.timing', now), {})]},
mock_controller.logger.statsd_client.calls)
class TestBaseStorageServer(unittest.TestCase):
"""Test swift.common.base_storage_server"""
def setUp(self):
self.tmpdir = mkdtemp()
self.testdir = os.path.join(self.tmpdir,
'tmp_test_base_storage_server')
def tearDown(self):
"""Tear down for testing swift.common.base_storage_server"""
rmtree(self.tmpdir)
def test_server_type(self):
conf = {'devices': self.testdir, 'mount_check': 'false'}
baseserver = BaseStorageServer(conf)
msg = 'Storage nodes have not implemented the Server type.'
try:
baseserver.server_type
except NotImplementedError as e:
self.assertEqual(str(e), msg)
def test_allowed_methods(self):
conf = {'devices': self.testdir, 'mount_check': 'false',
'replication_server': 'false'}
# test what's available in the base class
allowed_methods_test = FakeOPTIONS(conf).allowed_methods
self.assertEqual(allowed_methods_test, ['OPTIONS'])
# test that a subclass can add allowed methods
allowed_methods_test = FakeANOTHER(conf).allowed_methods
allowed_methods_test.sort()
self.assertEqual(allowed_methods_test, [
'ANOTHER', 'OPTIONS'])
conf = {'devices': self.testdir, 'mount_check': 'false',
'replication_server': 'true'}
# test what's available in the base class
allowed_methods_test = FakeOPTIONS(conf).allowed_methods
self.assertEqual(allowed_methods_test, ['OPTIONS'])
# test that a subclass can add allowed methods
allowed_methods_test = FakeANOTHER(conf).allowed_methods
self.assertEqual(allowed_methods_test, [
'ANOTHER', 'OPTIONS', 'REPLICATE', 'REPLICATE2'])
conf = {'devices': self.testdir, 'mount_check': 'false'}
# test what's available in the base class
allowed_methods_test = FakeOPTIONS(conf).allowed_methods
self.assertEqual(allowed_methods_test, ['OPTIONS'])
# test that a subclass can add allowed methods
allowed_methods_test = FakeANOTHER(conf).allowed_methods
allowed_methods_test.sort()
self.assertEqual(allowed_methods_test, [
'ANOTHER', 'OPTIONS', 'REPLICATE', 'REPLICATE2'])
def test_OPTIONS_error(self):
msg = 'Storage nodes have not implemented the Server type.'
conf = {'devices': self.testdir, 'mount_check': 'false',
'replication_server': 'false'}
baseserver = BaseStorageServer(conf)
req = Request.blank('/sda1/p/a/c/o', {'REQUEST_METHOD': 'OPTIONS'})
req.content_length = 0
try:
baseserver.OPTIONS(req)
except NotImplementedError as e:
self.assertEqual(str(e), msg)
def test_OPTIONS(self):
conf = {'devices': self.testdir, 'mount_check': 'false',
'replication_server': 'false'}
req = Request.blank('/sda1/p/a/c/o', {'REQUEST_METHOD': 'OPTIONS'})
req.content_length = 0
resp = FakeOPTIONS(conf).OPTIONS(req)
self.assertEqual(resp.headers['Allow'], 'OPTIONS')
self.assertEqual(resp.headers['Server'],
'test-server/' + swift_version)
|