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
|
# -*- coding: utf-8 -*-
# thumbor imaging service
# https://github.com/thumbor/thumbor/wiki
# Licensed under the MIT license:
# http://www.opensource.org/licenses/mit-license
# Copyright (c) 2011 globo.com thumbor@googlegroups.com
from unittest import TestCase, mock
from preggy import expect
import thumbor.server
from tests.fixtures.custom_error_handler import (
ErrorHandler as CustomErrorHandler,
)
from thumbor.app import ThumborServiceApp
from thumbor.config import Config
from thumbor.server import (
configure_log,
get_application,
get_as_integer,
get_config,
get_context,
get_importer,
main,
run_server,
validate_config,
)
class ServerTestCase(TestCase):
def test_can_get_value_as_integer(self):
expect(get_as_integer("1")).to_equal(1)
expect(get_as_integer("a")).to_be_null()
expect(get_as_integer("")).to_be_null()
expect(get_as_integer(None)).to_be_null()
def test_can_get_config_from_path(self):
config = get_config("./tests/fixtures/thumbor_config_server_test.conf")
with mock.patch.dict("os.environ", {"ENGINE": "test"}):
expect(config).not_to_be_null()
expect(config.ALLOWED_SOURCES).to_be_like(["mydomain.com"])
expect(config.ENGINE).to_be_like("thumbor.engines.pil")
def test_can_get_config_with_env_enabled(self):
config = get_config(
"./tests/fixtures/thumbor_config_server_test.conf", True
)
with mock.patch.dict("os.environ", {"ENGINE": "test"}):
expect(config).not_to_be_null()
expect(config.ALLOWED_SOURCES).to_be_like(["mydomain.com"])
expect(config.ENGINE).to_be_like("test")
@mock.patch("logging.basicConfig")
def test_can_configure_log_from_config(self, basic_config_mock):
conf = Config()
configure_log(conf, "DEBUG")
params = {
"datefmt": "%Y-%m-%d %H:%M:%S",
"level": 10,
"format": "%(asctime)s %(name)s:%(levelname)s %(message)s",
}
basic_config_mock.assert_called_with(**params)
@mock.patch("logging.config.dictConfig")
def test_can_configure_log_from_dict_config(self, dict_config_mock):
conf = Config(THUMBOR_LOG_CONFIG={"level": "INFO"})
configure_log(conf, "DEBUG")
params = {
"level": "INFO",
}
dict_config_mock.assert_called_with(params)
def test_can_import_default_modules(self):
conf = Config()
importer = get_importer(conf)
expect(importer).not_to_be_null()
expect(importer.filters).not_to_be_empty()
def test_can_import_with_custom_error_handler_class(self):
conf = Config(
USE_CUSTOM_ERROR_HANDLING=True,
ERROR_HANDLER_MODULE="tests.fixtures.custom_error_handler",
)
importer = get_importer(conf)
expect(importer).not_to_be_null()
expect(importer.error_handler_class).not_to_be_null()
expect(importer.error_handler_class).to_be_instance_of(
CustomErrorHandler
)
def test_validate_config_security_key(self):
server_parameters = mock.Mock(security_key=None)
conf = Config(SECURITY_KEY=None)
with expect.error_to_happen(
RuntimeError,
message="No security key was found for this instance of thumbor. "
"Please provide one using the conf file or a security key file.",
):
validate_config(conf, server_parameters)
def test_validate_config_security_key_from_config(self):
server_parameters = mock.Mock(security_key=None)
conf = Config(SECURITY_KEY="something")
validate_config(conf, server_parameters)
expect(server_parameters.security_key).to_equal("something")
@mock.patch.object(thumbor.server, "which")
def test_validate_gifsicle_path(self, which_mock):
server_parameters = mock.Mock(security_key=None)
conf = Config(SECURITY_KEY="test", USE_GIFSICLE_ENGINE=True)
which_mock.return_value = "/usr/bin/gifsicle"
validate_config(conf, server_parameters)
expect(server_parameters.gifsicle_path).to_equal("/usr/bin/gifsicle")
@mock.patch.object(thumbor.server, "which")
def test_validate_null_gifsicle_path(self, which_mock):
server_parameters = mock.Mock(security_key=None)
conf = Config(SECURITY_KEY="test", USE_GIFSICLE_ENGINE=True)
which_mock.return_value = None
with expect.error_to_happen(
RuntimeError,
message="If using USE_GIFSICLE_ENGINE configuration to True, "
"the `gifsicle` binary must be in the PATH and must be an executable.",
):
validate_config(conf, server_parameters)
def test_get_context(self):
server_parameters = mock.Mock(
security_key=None, app_class="thumbor.app.ThumborServiceApp"
)
conf = Config(SECURITY_KEY="test")
importer = get_importer(conf)
context = get_context(server_parameters, conf, importer)
expect(context).not_to_be_null()
def test_get_application(self):
server_parameters = mock.Mock(
security_key=None, app_class="thumbor.app.ThumborServiceApp"
)
conf = Config(SECURITY_KEY="test")
importer = get_importer(conf)
context = get_context(server_parameters, conf, importer)
app = get_application(context)
expect(app).not_to_be_null()
expect(app).to_be_instance_of(ThumborServiceApp)
@mock.patch.object(thumbor.server, "HTTPServer")
def test_can_run_server_with_default_params(self, server_mock):
application = mock.Mock()
context = mock.Mock()
context.server = mock.Mock(
fd=None, port=1234, ip="0.0.0.0", processes=1
)
server_instance_mock = mock.Mock()
server_mock.return_value = server_instance_mock
run_server(application, context)
server_instance_mock.bind.assert_called_with(1234, "0.0.0.0")
server_instance_mock.start.assert_called_with(1)
@mock.patch.object(thumbor.server, "HTTPServer")
def test_can_run_server_with_multiple_processes(self, server_mock):
application = mock.Mock()
context = mock.Mock()
context.server = mock.Mock(
fd=None, port=1234, ip="0.0.0.0", processes=5
)
server_instance_mock = mock.Mock()
server_mock.return_value = server_instance_mock
run_server(application, context)
server_instance_mock.start.assert_called_with(5)
@mock.patch.object(thumbor.server, "HTTPServer")
@mock.patch.object(thumbor.server, "socket", autospec=True)
def test_can_run_server_with_fd(self, socket_mock, server_mock):
application = mock.Mock()
context = mock.Mock()
context.server = mock.Mock(fd=11, port=1234, ip="0.0.0.0", processes=1)
server_instance_mock = mock.Mock()
server_mock.return_value = server_instance_mock
run_server(application, context)
socket_mock.assert_called_with(fileno=11)
server_instance_mock.add_socket.assert_called_with(
socket_mock.return_value
)
server_instance_mock.start.assert_called_with(1)
@mock.patch.object(thumbor.server, "HTTPServer")
@mock.patch.object(thumbor.server, "socket", autospec=True)
def test_can_run_server_with_fd_non_blocking(
self, socket_mock, server_mock
):
server_parameters = mock.Mock(security_key=None)
conf = Config(SECURITY_KEY="test", NON_BLOCKING_SOCKETS=True)
importer = get_importer(conf)
context = get_context(server_parameters, conf, importer)
context.server = mock.Mock(fd=11, port=1234, ip="0.0.0.0", processes=1)
server_instance_mock = mock.Mock()
server_mock.return_value = server_instance_mock
application = mock.Mock()
run_server(application, context)
socket_mock.assert_called_with(fileno=11)
socket_mock.return_value.setblocking.assert_called_with(False)
server_instance_mock.add_socket.assert_called_with(
socket_mock.return_value
)
server_instance_mock.start.assert_called_with(1)
@mock.patch.object(thumbor.server, "HTTPServer")
@mock.patch.object(thumbor.server, "bind_unix_socket")
def test_can_run_server_with_unix_socket(
self, bind_unix_socket, server_mock
):
application = mock.Mock()
context = mock.Mock()
context.server = mock.Mock(
fd="/path/bin", port=1234, ip="0.0.0.0", processes=1
)
server_instance_mock = mock.Mock()
server_mock.return_value = server_instance_mock
bind_unix_socket.return_value = "socket mock"
run_server(application, context)
bind_unix_socket.assert_called_with("/path/bin")
server_instance_mock.add_socket.assert_called_with("socket mock")
server_instance_mock.start.assert_called_with(1)
@mock.patch.object(thumbor.server, "HTTPServer")
def test_run_server_returns_server(self, server_mock):
application = mock.Mock()
context = mock.Mock()
context.server = mock.Mock(fd=None, port=1234, ip="0.0.0.0")
server_instance_mock = mock.Mock()
server_mock.return_value = server_instance_mock
server = run_server(application, context)
self.assertEqual(server, server_instance_mock)
@mock.patch.object(thumbor.server, "setup_signal_handler")
@mock.patch.object(thumbor.server, "HTTPServer")
@mock.patch.object(thumbor.server, "get_server_parameters")
@mock.patch("tornado.ioloop.IOLoop.instance", create=True)
def test_can_run_main(
self,
ioloop_mock,
get_server_parameters_mock,
server_mock,
setup_signal_handler_mock,
):
server_parameters = mock.Mock(
config_path="./tests/fixtures/thumbor_config_server_test.conf",
log_level="DEBUG",
debug=False,
security_key="sec",
app_class="thumbor.app.ThumborServiceApp",
fd=None,
ip="0.0.0.0",
port=1234,
)
get_server_parameters_mock.return_value = server_parameters
ioloop_instance_mock = mock.Mock()
ioloop_mock.return_value = ioloop_instance_mock
main()
ioloop_instance_mock.start.assert_any_call()
self.assertTrue(setup_signal_handler_mock.called)
self.assertTrue(server_mock.called)
def cleanup(self):
ServerTestCase.cleanup_called = True
|