Skip to content

Commit 3e7d99e

Browse files
zzzeekGerrit Code Review
authored andcommitted
Merge "Coerce port to integer"
2 parents 56d8e3f + 24bb5fb commit 3e7d99e

File tree

3 files changed

+38
-7
lines changed

3 files changed

+38
-7
lines changed

sqlalchemy_collectd/client/plugin.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def __init__(self, url, kwargs):
1818
"collectd_host", "collectd_host", self.config, url, kwargs
1919
)
2020
self._get_argument(
21-
"collectd_port", "collectd_port", self.config, url, kwargs
21+
"collectd_port", "collectd_port", self.config, url, kwargs, int
2222
)
2323
self._get_argument(
2424
"collectd_report_host", "hostname", self.config, url, kwargs
@@ -27,13 +27,17 @@ def __init__(self, url, kwargs):
2727
"collectd_program_name", "progname", self.config, url, kwargs
2828
)
2929

30-
def _get_argument(self, name, dest_name, dest, url, kwargs):
30+
def _get_argument(self, name, dest_name, dest, url, kwargs, coerce_=None):
3131
# favor the URL but pop the name from both
3232
if name in kwargs:
3333
dest[dest_name] = kwargs.pop(name)
34+
if coerce_:
35+
dest[dest_name] = coerce_(dest[dest_name])
3436

3537
if name in url.query:
3638
dest[dest_name] = url.query.pop(name)
39+
if coerce_:
40+
dest[dest_name] = coerce_(dest[dest_name])
3741

3842
def handle_dialect_kwargs(self, dialect_cls, dialect_args):
3943
"""parse and modify dialect kwargs"""

sqlalchemy_collectd/client/tests/test_plugin.py

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,36 @@ def test_start_no_args(self):
1919
def test_start_engine_args(self):
2020
with mock.patch.object(plugin, "start_plugin") as start_plugin:
2121
url = sqla_url.URL("mysql+pymysql://scott:tiger@localhost/")
22-
p = plugin.Plugin(url, {"collectd_host": "127.0.0.1"})
22+
p = plugin.Plugin(
23+
url, {"collectd_host": "127.0.0.1", "collectd_port": 5678}
24+
)
2325
engine = mock.Mock()
2426
p.engine_created(engine)
2527

2628
self.assertEqual(
27-
[mock.call(engine, collectd_host="127.0.0.1")],
29+
[mock.call(engine, collectd_host="127.0.0.1", collectd_port=5678)],
2830
start_plugin.mock_calls,
2931
)
3032

3133
def test_start_url_args(self):
34+
with mock.patch.object(plugin, "start_plugin") as start_plugin:
35+
url = sqla_url.make_url(
36+
"mysql+pymysql://scott:tiger@localhost/"
37+
"?collectd_host=127.0.0.1&somekey=somevalue&collectd_port=1234"
38+
)
39+
kwargs = {"unrelated": "bar"}
40+
p = plugin.Plugin(url, kwargs)
41+
engine = mock.Mock()
42+
p.engine_created(engine)
43+
44+
self.assertEqual(
45+
[mock.call(engine, collectd_host="127.0.0.1", collectd_port=1234)],
46+
start_plugin.mock_calls,
47+
)
48+
self.assertEqual({"somekey": "somevalue"}, url.query)
49+
self.assertEqual({"unrelated": "bar"}, kwargs)
50+
51+
def test_start_url_args_no_port(self):
3252
with mock.patch.object(plugin, "start_plugin") as start_plugin:
3353
url = sqla_url.make_url(
3454
"mysql+pymysql://scott:tiger@localhost/"
@@ -50,16 +70,16 @@ def test_start_both_args(self):
5070
with mock.patch.object(plugin, "start_plugin") as start_plugin:
5171
url = sqla_url.make_url(
5272
"mysql+pymysql://scott:tiger@localhost/"
53-
"?collectd_host=127.0.0.1"
73+
"?collectd_host=127.0.0.1&collectd_port=1234"
5474
)
55-
kwargs = {"collectd_host": "172.18.0.2"}
75+
kwargs = {"collectd_host": "172.18.0.2", "collectd_port": 5678}
5676
p = plugin.Plugin(url, kwargs)
5777
engine = mock.Mock()
5878
p.engine_created(engine)
5979

6080
# argument is popped from both but favors url argument
6181
self.assertEqual(
62-
[mock.call(engine, collectd_host="127.0.0.1")],
82+
[mock.call(engine, collectd_host="127.0.0.1", collectd_port=1234)],
6383
start_plugin.mock_calls,
6484
)
6585
self.assertEqual({}, url.query)

unreleased_changes/3.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.. change::
2+
:tags: bug, config
3+
:tickets: 3
4+
5+
Fixed bug where the port number included in the SQLAlchemy URL with the
6+
collectd_port query string value would not be coerced into an integer,
7+
failing when it is passed to the socket send operation.

0 commit comments

Comments
 (0)