Test and defend against the specific case where the statsd hostname is 'unix'

This commit is contained in:
larribas 2020-07-19 20:40:08 +02:00
parent 15abac7e81
commit 2a16fcd3ce
2 changed files with 22 additions and 3 deletions

View File

@ -495,11 +495,19 @@ def validate_chdir(val):
return path
def validate_address(val):
def validate_statsd_address(val):
val = validate_string(val)
if val is None:
return None
# As of major release 20, util.parse_address would recognize unix:PORT
# as a UDS address, breaking backwards compatibility. We defend against
# that regression here (this is also unit-tested).
# Feel free to remove in the next major release.
unix_hostname_regression = re.match(r'^unix:(\d+)$', val)
if unix_hostname_regression:
return ('unix', int(unix_hostname_regression.group(1)))
try:
address = util.parse_address(val, default_port='8125')
except RuntimeError:
@ -1473,7 +1481,7 @@ class StatsdHost(Setting):
cli = ["--statsd-host"]
meta = "STATSD_ADDR"
default = None
validator = validate_address
validator = validate_statsd_address
desc = """\
The address of the StatsD server to log to.

View File

@ -313,12 +313,23 @@ def test_statsd_host():
assert c.statsd_host == ("localhost", 8125)
c.set("statsd_host", "statsd:7777")
assert c.statsd_host == ("statsd", 7777)
c.set("statsd_host", "unix:/path/to.sock")
c.set("statsd_host", "unix:///path/to.sock")
assert c.statsd_host == "/path/to.sock"
pytest.raises(TypeError, c.set, "statsd_host", 666)
pytest.raises(TypeError, c.set, "statsd_host", "host:string")
def test_statsd_host_with_unix_as_hostname():
# This is a regression test for major release 20. After this release
# we should consider modifying the behavior of util.parse_address to
# simplify gunicorn's code
c = config.Config()
c.set("statsd_host", "unix:7777")
assert c.statsd_host == ("unix", 7777)
c.set("statsd_host", "unix://some.socket")
assert c.statsd_host == "some.socket"
def test_statsd_changes_logger():
c = config.Config()
assert c.logger_class == glogging.Logger