Skip to content

Commit

Permalink
Test and defend against the specific case where the statsd hostname i…
Browse files Browse the repository at this point in the history
…s 'unix'
  • Loading branch information
larribas committed Jul 20, 2020
1 parent 15abac7 commit 2a16fcd
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
12 changes: 10 additions & 2 deletions gunicorn/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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.
Expand Down
13 changes: 12 additions & 1 deletion tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 2a16fcd

Please sign in to comment.