mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 10:10:20 +00:00
Add ssl argument to MongoClient for mongo future returner
This commit is contained in:
parent
bbf86a67be
commit
a3cf72005d
2 changed files with 74 additions and 9 deletions
|
@ -137,6 +137,7 @@ def _get_options(ret=None):
|
|||
attrs = {
|
||||
"host": "host",
|
||||
"port": "port",
|
||||
"ssl": "ssl",
|
||||
"db": "db",
|
||||
"user": "user",
|
||||
"password": "password",
|
||||
|
@ -158,6 +159,7 @@ def _get_conn(ret):
|
|||
|
||||
host = _options.get("host")
|
||||
port = _options.get("port")
|
||||
ssl = _options.get("ssl") or False
|
||||
uri = _options.get("uri")
|
||||
db_ = _options.get("db")
|
||||
user = _options.get("user")
|
||||
|
@ -178,7 +180,7 @@ def _get_conn(ret):
|
|||
mdb = conn.get_database()
|
||||
else:
|
||||
if PYMONGO_VERSION > Version("2.3"):
|
||||
conn = pymongo.MongoClient(host, port, username=user, password=password)
|
||||
conn = pymongo.MongoClient(host, port, username=user, password=password, ssl=ssl)
|
||||
else:
|
||||
if uri:
|
||||
raise salt.exceptions.SaltConfigurationError(
|
||||
|
|
|
@ -1,19 +1,18 @@
|
|||
import pytest
|
||||
|
||||
import salt.exceptions
|
||||
import salt.returners.mongo_future_return as mongo
|
||||
import salt.returners.mongo_future_return as mongo_future_return
|
||||
from salt.utils.versions import Version
|
||||
from tests.support.mock import patch
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def configure_loader_modules():
|
||||
|
||||
fake_opts = {"mongo.host": "fnord", "mongo.port": "fnordport"}
|
||||
return {
|
||||
mongo: {
|
||||
"__opts__": {
|
||||
"mongo.uri": "mongodb://root:pass@localhost27017/salt?authSource=admin"
|
||||
}
|
||||
mongo_future_return: {
|
||||
"__opts__": fake_opts,
|
||||
"__salt__": {"config.option": fake_opts.get},
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -30,6 +29,70 @@ def test_config_exception():
|
|||
"salt.returners.mongo_future_return.PYMONGO_VERSION",
|
||||
Version("4.3.2"),
|
||||
create=True,
|
||||
), patch.dict(mongo.__opts__, opts):
|
||||
), patch.dict(mongo_future_return.__opts__, opts):
|
||||
with pytest.raises(salt.exceptions.SaltConfigurationError):
|
||||
mongo.returner({})
|
||||
mongo_future_return.returner({})
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"expected_ssl, use_ssl",
|
||||
[
|
||||
(True, {"fnord.mongo.ssl": True}),
|
||||
(False, {"fnord.mongo.ssl": False}),
|
||||
(False, {"fnord.mongo.ssl": None}),
|
||||
(False, {}),
|
||||
],
|
||||
)
|
||||
def test_mongo_future_returner_should_correctly_pass_ssl_to_MongoClient_when_ret_is_set(
|
||||
expected_ssl, use_ssl
|
||||
):
|
||||
with patch(
|
||||
"salt.returners.mongo_future_return.pymongo", create=True
|
||||
) as fake_mongo, patch.object(
|
||||
mongo_future_return,
|
||||
"PYMONGO_VERSION",
|
||||
mongo_future_return.Version("99999"),
|
||||
create=True,
|
||||
), patch.dict(
|
||||
"salt.returners.mongo_future_return.__opts__",
|
||||
{
|
||||
**use_ssl,
|
||||
**{"fnord.mongo.host": "fnordfnord", "fnord.mongo.port": "fnordfnordport"},
|
||||
},
|
||||
):
|
||||
mongo_future_return._get_conn(ret={"ret_config": "fnord"})
|
||||
fake_mongo.MongoClient.assert_called_with(
|
||||
host="fnordfnord", port="fnordfnordport", ssl=expected_ssl
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"expected_ssl, use_ssl",
|
||||
[
|
||||
(True, {"mongo.ssl": True}),
|
||||
(False, {"mongo.ssl": False}),
|
||||
(False, {"mongo.ssl": None}),
|
||||
(False, {}),
|
||||
],
|
||||
)
|
||||
def test_mongo_future_returner_should_correctly_pass_ssl_to_MongoClient(
|
||||
expected_ssl, use_ssl
|
||||
):
|
||||
# Here these fnord.X.Y config options should be ignored
|
||||
with patch(
|
||||
"salt.returners.mongo_future_return.pymongo", create=True
|
||||
) as fake_mongo, patch.object(
|
||||
mongo_future_return,
|
||||
"PYMONGO_VERSION",
|
||||
mongo_future_return.Version("99999"),
|
||||
create=True,
|
||||
), patch.dict(
|
||||
"salt.returners.mongo_future_return.__opts__",
|
||||
{
|
||||
**use_ssl,
|
||||
**{"fnord.mongo.host": "fnordfnord", "fnord.mongo.port": "fnordfnordport"},
|
||||
},
|
||||
):
|
||||
mongo_future_return._get_conn(ret=None)
|
||||
fake_mongo.MongoClient.assert_called_with(
|
||||
host="fnord", port="fnordport", ssl=expected_ssl
|
||||
)
|
||||
|
|
Loading…
Add table
Reference in a new issue