diff --git a/salt/tops/mongo.py b/salt/tops/mongo.py index af48980fdc2..b7b316549dd 100644 --- a/salt/tops/mongo.py +++ b/salt/tops/mongo.py @@ -95,6 +95,7 @@ def top(**kwargs): """ host = __opts__["mongo.host"] port = __opts__["mongo.port"] + ssl = __opts__.get("mongo.ssl") or False collection = __opts__["master_tops"]["mongo"].get("collection", "tops") id_field = __opts__["master_tops"]["mongo"].get("id_field", "_id") re_pattern = __opts__["master_tops"]["mongo"].get("re_pattern", "") @@ -105,7 +106,7 @@ def top(**kwargs): ) log.info("connecting to %s:%s for mongo ext_tops", host, port) - conn = pymongo.MongoClient(host, port) + conn = pymongo.MongoClient(host=host, port=port, ssl=ssl) log.debug("using database '%s'", __opts__["mongo.db"]) mdb = conn[__opts__["mongo.db"]] diff --git a/tests/pytests/unit/tops/test_mongo.py b/tests/pytests/unit/tops/test_mongo.py new file mode 100644 index 00000000000..f0561e863aa --- /dev/null +++ b/tests/pytests/unit/tops/test_mongo.py @@ -0,0 +1,28 @@ +import pytest +import salt.tops.mongo +from tests.support.mock import patch + + +@pytest.mark.parametrize( + "expected_ssl, use_ssl", + [ + (True, True), + (False, False), + (False, None), + ], +) +def test_tops_should_correctly_pass_ssl_arg_to_MongoClient(expected_ssl, use_ssl): + salt.tops.mongo.HAS_PYMONGO = True + with patch("salt.tops.mongo.pymongo", create=True) as fake_pymongo, patch.dict( + "salt.tops.mongo.__opts__", + { + "master_tops": {"mongo": {}}, + "mongo.host": "fnord", + "mongo.port": "fnord", + "mongo.ssl": use_ssl, + }, + ): + salt.tops.mongo.top(opts={"id": "fnord"}) + fake_pymongo.MongoClient.assert_called_with( + host="fnord", port="fnord", ssl=expected_ssl + )