forked from benoitc/gunicorn
/
003-test-config.py
223 lines (189 loc) · 5.79 KB
/
003-test-config.py
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
# -*- coding: utf-8 -
#
# This file is part of gunicorn released under the MIT license.
# See the NOTICE for more information.
from nose.plugins.skip import SkipTest
import t
import os
import sys
from gunicorn import config
from gunicorn.app.base import Application
from gunicorn.workers.sync import SyncWorker
dirname = os.path.dirname(__file__)
def cfg_file():
return os.path.join(dirname, "config", "test_cfg.py")
def paster_ini():
return os.path.join(dirname, "..", "examples", "frameworks", "pylonstest", "nose.ini")
def PasterApp():
try:
from paste.deploy import loadapp, loadwsgi
except ImportError:
raise SkipTest()
from gunicorn.app.pasterapp import PasterApplication
return PasterApplication("no_usage")
class AltArgs(object):
def __init__(self, args=None):
self.args = args or []
self.orig = sys.argv
def __enter__(self):
sys.argv = self.args
def __exit__(self, exc_type, exc_inst, traceback):
sys.argv = self.orig
class NoConfigApp(Application):
def __init__(self):
super(NoConfigApp, self).__init__("no_usage")
def init(self, parser, opts, args):
pass
def load(self):
pass
def test_defaults():
c = config.Config()
for s in config.KNOWN_SETTINGS:
t.eq(s.default, c.settings[s.name].get())
def test_property_access():
c = config.Config()
for s in config.KNOWN_SETTINGS:
getattr(c, s.name)
# Class was loaded
t.eq(c.worker_class, SyncWorker)
# Debug affects workers
t.eq(c.workers, 1)
c.set("workers", 3)
t.eq(c.workers, 3)
# Address is parsed
t.eq(c.address, ("127.0.0.1", 8000))
# User and group defaults
t.eq(os.geteuid(), c.uid)
t.eq(os.getegid(), c.gid)
# Proc name
t.eq("gunicorn", c.proc_name)
# Not a config property
t.raises(AttributeError, getattr, c, "foo")
# Force to be not an error
class Baz(object):
def get(self):
return 3.14
c.settings["foo"] = Baz()
t.eq(c.foo, 3.14)
# Attempt to set a cfg not via c.set
t.raises(AttributeError, setattr, c, "proc_name", "baz")
# No setting for name
t.raises(AttributeError, c.set, "baz", "bar")
def test_bool_validation():
c = config.Config()
t.eq(c.debug, False)
c.set("debug", True)
t.eq(c.debug, True)
c.set("debug", "true")
t.eq(c.debug, True)
c.set("debug", "false")
t.eq(c.debug, False)
t.raises(ValueError, c.set, "debug", "zilch")
t.raises(TypeError, c.set, "debug", 4)
def test_pos_int_validation():
c = config.Config()
t.eq(c.workers, 1)
c.set("workers", 4)
t.eq(c.workers, 4)
c.set("workers", "5")
t.eq(c.workers, 5)
c.set("workers", "0xFF")
t.eq(c.workers, 255)
c.set("workers", True)
t.eq(c.workers, 1) # Yes. That's right...
t.raises(ValueError, c.set, "workers", -21)
t.raises(TypeError, c.set, "workers", c)
def test_str_validation():
c = config.Config()
t.eq(c.proc_name, "gunicorn")
c.set("proc_name", " foo ")
t.eq(c.proc_name, "foo")
t.raises(TypeError, c.set, "proc_name", 2)
def test_callable_validation():
c = config.Config()
def func(a, b):
pass
c.set("pre_fork", func)
t.eq(c.pre_fork, func)
t.raises(TypeError, c.set, "pre_fork", 1)
t.raises(TypeError, c.set, "pre_fork", lambda x: True)
def test_cmd_line():
args = AltArgs(["prog_name", "-b", "blargh"])
try:
args.__enter__()
app = NoConfigApp()
t.eq(app.cfg.bind, "blargh")
finally:
args.__exit__(*sys.exc_info())
args = AltArgs(["prog_name", "-w", "3"])
try:
args.__enter__()
app = NoConfigApp()
t.eq(app.cfg.workers, 3)
finally:
args.__exit__(*sys.exc_info())
args = AltArgs(["prog_name", "--debug"])
try:
args.__enter__()
app = NoConfigApp()
t.eq(app.cfg.debug, True)
finally:
args.__exit__(*sys.exc_info())
def test_app_config():
args = AltArgs()
try:
args.__enter__()
app = NoConfigApp()
finally:
args.__exit__(*sys.exc_info())
for s in config.KNOWN_SETTINGS:
t.eq(s.default, app.cfg.settings[s.name].get())
def test_load_config():
args = AltArgs(["prog_name", "-c", cfg_file()])
try:
args.__enter__()
app = NoConfigApp()
finally:
args.__exit__(*sys.exc_info())
t.eq(app.cfg.bind, "unix:/tmp/bar/baz")
t.eq(app.cfg.workers, 3)
t.eq(app.cfg.proc_name, "fooey")
def test_cli_overrides_config():
args = AltArgs(["prog_name", "-c", cfg_file(), "-b", "blarney"])
try:
args.__enter__()
app = NoConfigApp()
t.eq(app.cfg.bind, "blarney")
t.eq(app.cfg.proc_name, "fooey")
finally:
args.__exit__(*sys.exc_info())
def test_paster_config():
args = AltArgs(["prog_name", paster_ini()])
try:
args.__enter__()
app = PasterApp()
t.eq(app.cfg.bind, "192.168.0.1:80")
t.eq(app.cfg.proc_name, "brim")
t.eq("ignore_me" in app.cfg.settings, False)
finally:
args.__exit__(*sys.exc_info())
def test_cfg_over_paster():
args = AltArgs(["prog_name", "-c", cfg_file(), paster_ini()])
try:
args.__enter__()
app = PasterApp()
t.eq(app.cfg.bind, "unix:/tmp/bar/baz")
t.eq(app.cfg.proc_name, "fooey")
t.eq(app.cfg.default_proc_name, "blurgh")
finally:
args.__exit__(*sys.exc_info())
def test_cli_cfg_paster():
args = AltArgs(["prog_name", "-c", cfg_file(), "-b", "whee", paster_ini()])
try:
args.__enter__()
app = PasterApp()
t.eq(app.cfg.bind, "whee")
t.eq(app.cfg.proc_name, "fooey")
t.eq(app.cfg.default_proc_name, "blurgh")
finally:
args.__exit__(*sys.exc_info())