From c74c37372bf46fefbd8ae932c05827544beb2923 Mon Sep 17 00:00:00 2001 From: Damien Degois Date: Thu, 21 Dec 2023 07:53:10 +0100 Subject: [PATCH] Adapt tests --- salt/pillar/mongo.py | 11 +---------- tests/pytests/unit/pillar/test_mongo.py | 16 +++++++--------- .../unit/returners/test_mongo_future_return.py | 8 ++++++-- .../pytests/unit/states/test_mongodb_database.py | 4 ++-- 4 files changed, 16 insertions(+), 23 deletions(-) diff --git a/salt/pillar/mongo.py b/salt/pillar/mongo.py index 956fee078bb..2005987d221 100644 --- a/salt/pillar/mongo.py +++ b/salt/pillar/mongo.py @@ -132,11 +132,6 @@ def ext_pillar( host = __opts__["mongo.host"] port = __opts__["mongo.port"] ssl = __opts__.get("mongo.ssl") or False - log.info("connecting to %s:%s for mongo ext_pillar", host, port) - conn = pymongo.MongoClient(host=host, port=port, ssl=ssl) - - log.debug("using database '%s'", __opts__["mongo.db"]) - mdb = conn[__opts__["mongo.db"]] uri = __opts__.get("mongo.uri") host = __opts__.get("mongo.host") @@ -144,10 +139,6 @@ def ext_pillar( user = __opts__.get("mongo.user") password = __opts__.get("mongo.password") - if user and password: - log.debug("authenticating as '%s'", user) - mdb.authenticate(user, password) - db = __opts__.get("mongo.db") if uri: @@ -164,7 +155,7 @@ def ext_pillar( else: log.info("connecting to %s:%s for mongo ext_pillar", host, port) conn = pymongo.MongoClient( - host=host, port=port, username=user, password=password + host=host, port=port, username=user, password=password, ssl=ssl ) log.debug("using database '%s'", db) diff --git a/tests/pytests/unit/pillar/test_mongo.py b/tests/pytests/unit/pillar/test_mongo.py index 9e5072193ca..cf22cdc2450 100644 --- a/tests/pytests/unit/pillar/test_mongo.py +++ b/tests/pytests/unit/pillar/test_mongo.py @@ -7,14 +7,7 @@ from tests.support.mock import patch @pytest.fixture def configure_loader_modules(): - - return { - mongo: { - "__opts__": { - "mongo.uri": "mongodb://root:pass@localhost27017/salt?authSource=admin" - } - } - } + return {mongo: {"__opts__": {}}} def test_config_exception(): @@ -23,6 +16,7 @@ def test_config_exception(): "mongo.port": 27017, "mongo.user": "root", "mongo.password": "pass", + "mongo.db": "admin", "mongo.uri": "mongodb://root:pass@localhost27017/salt?authSource=admin", } with patch.dict(mongo.__opts__, opts): @@ -46,5 +40,9 @@ def test_mongo_pillar_should_use_ssl_when_set_in_opts(expected_ssl, use_ssl): ), patch("salt.pillar.mongo.pymongo", create=True) as fake_mongo: mongo.ext_pillar(minion_id="blarp", pillar=None) fake_mongo.MongoClient.assert_called_with( - host="fnord", port="fnordport", ssl=expected_ssl + host="fnord", + port="fnordport", + username=None, + password=None, + ssl=expected_ssl, ) diff --git a/tests/pytests/unit/returners/test_mongo_future_return.py b/tests/pytests/unit/returners/test_mongo_future_return.py index e3e6707afa6..876803bf847 100644 --- a/tests/pytests/unit/returners/test_mongo_future_return.py +++ b/tests/pytests/unit/returners/test_mongo_future_return.py @@ -62,7 +62,11 @@ def test_mongo_future_returner_should_correctly_pass_ssl_to_MongoClient_when_ret ): mongo_future_return._get_conn(ret={"ret_config": "fnord"}) fake_mongo.MongoClient.assert_called_with( - host="fnordfnord", port="fnordfnordport", ssl=expected_ssl + "fnordfnord", + "fnordfnordport", + username=None, + password=None, + ssl=expected_ssl, ) @@ -95,5 +99,5 @@ def test_mongo_future_returner_should_correctly_pass_ssl_to_MongoClient( ): mongo_future_return._get_conn(ret=None) fake_mongo.MongoClient.assert_called_with( - host="fnord", port="fnordport", ssl=expected_ssl + "fnord", "fnordport", username=None, password=None, ssl=expected_ssl ) diff --git a/tests/pytests/unit/states/test_mongodb_database.py b/tests/pytests/unit/states/test_mongodb_database.py index db93bf3063b..f0eaf2ce00d 100644 --- a/tests/pytests/unit/states/test_mongodb_database.py +++ b/tests/pytests/unit/states/test_mongodb_database.py @@ -78,7 +78,7 @@ def test_when_mongodb_database_remove_is_called_it_should_correctly_pass_ssl_arg expected_ssl, expected_allow_invalid, absent_kwargs ): # database from params needs to be in this return_value - salt.modules.mongodb.pymongo.MongoClient.return_value.database_names.return_value = [ + salt.modules.mongodb.pymongo.MongoClient.return_value.list_database_names.return_value = [ "foo", "bar", "some_database", @@ -98,7 +98,7 @@ def test_when_mongodb_database_remove_is_called_it_should_correctly_pass_ssl_arg # from the mock call list, but it didn't. There's probably some # other way to ensure that database_names/drop_database is out of # the MongoClient mock call list, but it was taking too long. - call().database_names(), + call().list_database_names(), call( host="mongodb.example.net", port=1982,