Pre-commit black + isort + pyupgrade

This commit is contained in:
Damien Degois 2023-12-20 15:05:03 +01:00 committed by Daniel Wozniak
parent d20f57852b
commit dbc8b4cf66
12 changed files with 99 additions and 124 deletions

View file

@ -126,21 +126,6 @@ def db_list(
""" """
List all MongoDB databases. List all MongoDB databases.
user
The user to authenticate with, if necessary.
password
The password to authenticate with, if necessary.
host
The host running MongoDB.
port
The port MongoDB is listening on.
authdb
The database to authenticate against, if not "admin".
user user
The user to connect to MongoDB as. Default is None. The user to connect to MongoDB as. Default is None.
@ -159,12 +144,12 @@ def db_list(
ssl ssl
Whether or not to connect to MongoDB over SSL. Default ``False``. Whether or not to connect to MongoDB over SSL. Default ``False``.
.. versionadded:: 3006 .. versionadded:: 3008.0
verify_ssl verify_ssl
Whether or not to verify the server cert when connecting. Default True. Whether or not to verify the server cert when connecting. Default True.
.. versionadded:: 3006 .. versionadded:: 3008.0
CLI Example: CLI Example:
@ -200,24 +185,6 @@ def db_exists(
""" """
Checks if a database exists in MongoDB. Checks if a database exists in MongoDB.
name
The name of the database to check.
user
The user to authenticate with, if necessary.
password
The password to authenticate with, if necessary.
host
The host running MongoDB.
port
The port MongoDB is listening on.
authdb
The database to authenticate against, if not "admin".
name name
The name of the database to check for. The name of the database to check for.
@ -239,12 +206,12 @@ def db_exists(
ssl ssl
Whether or not to connect to MongoDB over SSL. Default ``False``. Whether or not to connect to MongoDB over SSL. Default ``False``.
.. versionadded:: 3006 .. versionadded:: 3008.0
verify_ssl verify_ssl
Whether or not to verify the server cert when connecting. Default True. Whether or not to verify the server cert when connecting. Default True.
.. versionadded:: 3006 .. versionadded:: 3008.0
CLI Example: CLI Example:
@ -275,24 +242,6 @@ def db_remove(
""" """
Remove a MongoDB database. Remove a MongoDB database.
name
Name of the MongoDB database to remove.
user
The user to authenticate with, if necessary.
password
The password to authenticate with, if necessary.
host
The host running MongoDB.
port
The port MongoDB is listening on.
authdb
The database to authenticate against, if not "admin".
name name
The name of the database to remove. The name of the database to remove.
@ -314,12 +263,12 @@ def db_remove(
ssl ssl
Whether or not to connect to MongoDB over SSL. Default ``False``. Whether or not to connect to MongoDB over SSL. Default ``False``.
.. versionadded:: 3006 .. versionadded:: 3008.0
verify_ssl verify_ssl
Whether or not to verify the server cert when connecting. Default True. Whether or not to verify the server cert when connecting. Default True.
.. versionadded:: 3006 .. versionadded:: 3008.0
CLI Example: CLI Example:
@ -378,12 +327,12 @@ def version(
ssl ssl
Whether or not to connect to MongoDB over SSL. Default ``False``. Whether or not to connect to MongoDB over SSL. Default ``False``.
.. versionadded:: 3006 .. versionadded:: 3008.0
verify_ssl verify_ssl
Whether or not to verify the server cert when connecting. Default True. Whether or not to verify the server cert when connecting. Default True.
.. versionadded:: 3006 .. versionadded:: 3008.0
CLI Example: CLI Example:
@ -397,7 +346,7 @@ def version(
user, password, host, port, authdb=authdb, ssl=ssl, verify_ssl=verify_ssl user, password, host, port, authdb=authdb, ssl=ssl, verify_ssl=verify_ssl
) )
if not conn: if not conn:
err_msg = "Failed to connect to MongoDB database {}:{}".format(host, port) err_msg = f"Failed to connect to MongoDB database {host}:{port}"
log.error(err_msg) log.error(err_msg)
return (False, err_msg) return (False, err_msg)
@ -447,12 +396,12 @@ def user_find(
ssl ssl
Whether or not to connect to MongoDB over SSL. Default ``False``. Whether or not to connect to MongoDB over SSL. Default ``False``.
.. versionadded:: 3006 .. versionadded:: 3008.0
verify_ssl verify_ssl
Whether or not to verify the server cert when connecting. Default True. Whether or not to verify the server cert when connecting. Default True.
.. versionadded:: 3006 .. versionadded:: 3008.0
CLI Example: CLI Example:
@ -464,7 +413,7 @@ def user_find(
user, password, host, port, authdb=authdb, ssl=ssl, verify_ssl=verify_ssl user, password, host, port, authdb=authdb, ssl=ssl, verify_ssl=verify_ssl
) )
if not conn: if not conn:
err_msg = "Failed to connect to MongoDB database {}:{}".format(host, port) err_msg = f"Failed to connect to MongoDB database {host}:{port}"
log.error(err_msg) log.error(err_msg)
return (False, err_msg) return (False, err_msg)
@ -510,12 +459,12 @@ def user_list(
ssl ssl
Whether or not to connect to MongoDB over SSL. Default ``False``. Whether or not to connect to MongoDB over SSL. Default ``False``.
.. versionadded:: 3006 .. versionadded:: 3008.0
verify_ssl verify_ssl
Whether or not to verify the server cert when connecting. Default True. Whether or not to verify the server cert when connecting. Default True.
.. versionadded:: 3006 .. versionadded:: 3008.0
CLI Example: CLI Example:
@ -586,12 +535,12 @@ def user_exists(
ssl ssl
Whether or not to connect to MongoDB over SSL. Default ``False``. Whether or not to connect to MongoDB over SSL. Default ``False``.
.. versionadded:: 3006 .. versionadded:: 3008.0
verify_ssl verify_ssl
Whether or not to verify the server cert when connecting. Default True. Whether or not to verify the server cert when connecting. Default True.
.. versionadded:: 3006 .. versionadded:: 3008.0
CLI Example: CLI Example:
@ -659,12 +608,12 @@ def user_create(
ssl ssl
Whether or not to connect to MongoDB over SSL. Default ``False``. Whether or not to connect to MongoDB over SSL. Default ``False``.
.. versionadded:: 3006 .. versionadded:: 3008.0
verify_ssl verify_ssl
Whether or not to verify the server cert when connecting. Default True. Whether or not to verify the server cert when connecting. Default True.
.. versionadded:: 3006 .. versionadded:: 3008.0
CLI Example: CLI Example:
@ -728,12 +677,12 @@ def user_remove(
ssl ssl
Whether or not to connect to MongoDB over SSL. Default ``False``. Whether or not to connect to MongoDB over SSL. Default ``False``.
.. versionadded:: 3006 .. versionadded:: 3008.0
verify_ssl verify_ssl
Whether or not to verify the server cert when connecting. Default True. Whether or not to verify the server cert when connecting. Default True.
.. versionadded:: 3006 .. versionadded:: 3008.0
CLI Example: CLI Example:
@ -741,7 +690,9 @@ def user_remove(
salt '*' mongodb.user_remove <name> <user> <password> <host> <port> <database> <authdb> <ssl> salt '*' mongodb.user_remove <name> <user> <password> <host> <port> <database> <authdb> <ssl>
""" """
conn = _connect(user, password, host, port, authdb=authdb, ssl=ssl, verify_ssl=verify_ssl) conn = _connect(
user, password, host, port, authdb=authdb, ssl=ssl, verify_ssl=verify_ssl
)
if not conn: if not conn:
return "Failed to connect to mongo database" return "Failed to connect to mongo database"
@ -800,12 +751,12 @@ def user_roles_exists(
ssl ssl
Whether or not to connect to MongoDB over SSL. Default ``False``. Whether or not to connect to MongoDB over SSL. Default ``False``.
.. versionadded:: 3006 .. versionadded:: 3008.0
verify_ssl verify_ssl
Whether or not to verify the server cert when connecting. Default True. Whether or not to verify the server cert when connecting. Default True.
.. versionadded:: 3006 .. versionadded:: 3008.0
CLI Examples: CLI Examples:
@ -888,12 +839,12 @@ def user_grant_roles(
ssl ssl
Whether or not to connect to MongoDB over SSL. Default ``False``. Whether or not to connect to MongoDB over SSL. Default ``False``.
.. versionadded:: 3006 .. versionadded:: 3008.0
verify_ssl verify_ssl
Whether or not to verify the server cert when connecting. Default True. Whether or not to verify the server cert when connecting. Default True.
.. versionadded:: 3006 .. versionadded:: 3008.0
CLI Examples: CLI Examples:
@ -973,12 +924,12 @@ def user_revoke_roles(
ssl ssl
Whether or not to connect to MongoDB over SSL. Default ``False``. Whether or not to connect to MongoDB over SSL. Default ``False``.
.. versionadded:: 3006 .. versionadded:: 3008.0
verify_ssl verify_ssl
Whether or not to verify the server cert when connecting. Default True. Whether or not to verify the server cert when connecting. Default True.
.. versionadded:: 3006 .. versionadded:: 3008.0
CLI Examples: CLI Examples:
@ -1051,9 +1002,13 @@ def collection_create(
ssl ssl
Whether or not to connect to MongoDB over SSL. Default ``False``. Whether or not to connect to MongoDB over SSL. Default ``False``.
.. versionadded:: 3008.0
verify_ssl verify_ssl
Whether or not to verify the server cert when connecting. Default True. Whether or not to verify the server cert when connecting. Default True.
.. versionadded:: 3008.0
CLI Example: CLI Example:
.. code-block:: bash .. code-block:: bash
@ -1061,7 +1016,9 @@ def collection_create(
salt '*' mongodb.collection_create mycollection <user> <password> <host> <port> <database> salt '*' mongodb.collection_create mycollection <user> <password> <host> <port> <database>
""" """
conn = _connect(user, password, host, port, database, authdb, ssl=ssl, verify_ssl=verify_ssl) conn = _connect(
user, password, host, port, database, authdb, ssl=ssl, verify_ssl=verify_ssl
)
if not conn: if not conn:
return "Failed to connect to mongo database" return "Failed to connect to mongo database"
@ -1114,9 +1071,13 @@ def collection_drop(
ssl ssl
Whether or not to connect to MongoDB over SSL. Default ``False``. Whether or not to connect to MongoDB over SSL. Default ``False``.
.. versionadded:: 3008.0
verify_ssl verify_ssl
Whether or not to verify the server cert when connecting. Default True. Whether or not to verify the server cert when connecting. Default True.
.. versionadded:: 3008.0
CLI Example: CLI Example:
.. code-block:: bash .. code-block:: bash
@ -1124,7 +1085,9 @@ def collection_drop(
salt '*' mongodb.collection_drop mycollection <user> <password> <host> <port> <database> salt '*' mongodb.collection_drop mycollection <user> <password> <host> <port> <database>
""" """
conn = _connect(user, password, host, port, database, authdb, ssl=ssl, verify_ssl=verify_ssl) conn = _connect(
user, password, host, port, database, authdb, ssl=ssl, verify_ssl=verify_ssl
)
if not conn: if not conn:
return "Failed to connect to mongo database" return "Failed to connect to mongo database"
@ -1173,9 +1136,13 @@ def collections_list(
ssl ssl
Whether or not to connect to MongoDB over SSL. Default ``False``. Whether or not to connect to MongoDB over SSL. Default ``False``.
.. versionadded:: 3008.0
verify_ssl verify_ssl
Whether or not to verify the server cert when connecting. Default True. Whether or not to verify the server cert when connecting. Default True.
.. versionadded:: 3008.0
CLI Example: CLI Example:
.. code-block:: bash .. code-block:: bash
@ -1183,7 +1150,9 @@ def collections_list(
salt '*' mongodb.collections_list mycollection <user> <password> <host> <port> <database> salt '*' mongodb.collections_list mycollection <user> <password> <host> <port> <database>
""" """
conn = _connect(user, password, host, port, database, authdb, ssl=ssl, verify_ssl=verify_ssl) conn = _connect(
user, password, host, port, database, authdb, ssl=ssl, verify_ssl=verify_ssl
)
if not conn: if not conn:
return "Failed to connect to mongo database" return "Failed to connect to mongo database"
@ -1239,12 +1208,12 @@ def insert(
ssl ssl
Whether or not to connect to MongoDB over SSL. Default ``False``. Whether or not to connect to MongoDB over SSL. Default ``False``.
.. versionadded:: 3006 .. versionadded:: 3008.0
verify_ssl verify_ssl
Whether or not to verify the server cert when connecting. Default True. Whether or not to verify the server cert when connecting. Default True.
.. versionadded:: 3006 .. versionadded:: 3008.0
CLI Example: CLI Example:
@ -1323,12 +1292,12 @@ def update_one(
ssl ssl
Whether or not to connect to MongoDB over SSL. Default ``False``. Whether or not to connect to MongoDB over SSL. Default ``False``.
.. versionadded:: 3006 .. versionadded:: 3008.0
verify_ssl verify_ssl
Whether or not to verify the server cert when connecting. Default True. Whether or not to verify the server cert when connecting. Default True.
.. versionadded:: 3006 .. versionadded:: 3008.0
CLI Example: CLI Example:
@ -1368,7 +1337,9 @@ def update_one(
_update_doc = document[1] _update_doc = document[1]
# need a string to perform the test, so using objs[0] # need a string to perform the test, so using objs[0]
test_f = find(collection, objs[0], user, password, host, port, database, authdb, ssl) test_f = find(
collection, objs[0], user, password, host, port, database, authdb, ssl
)
if not isinstance(test_f, list): if not isinstance(test_f, list):
return "The find result is not well formatted. An error appears; cannot update." return "The find result is not well formatted. An error appears; cannot update."
elif not test_f: elif not test_f:
@ -1382,7 +1353,7 @@ def update_one(
col = getattr(mdb, collection) col = getattr(mdb, collection)
ids = col.update_one(_id_field, {"$set": _update_doc}) ids = col.update_one(_id_field, {"$set": _update_doc})
nb_mod = ids.modified_count nb_mod = ids.modified_count
return "{} objects updated".format(nb_mod) return f"{nb_mod} objects updated"
except pymongo.errors.PyMongoError as err: except pymongo.errors.PyMongoError as err:
log.error("Updating object %s failed with error %s", objects, err) log.error("Updating object %s failed with error %s", objects, err)
return err return err
@ -1430,12 +1401,12 @@ def find(
ssl ssl
Whether or not to connect to MongoDB over SSL. Default ``False``. Whether or not to connect to MongoDB over SSL. Default ``False``.
.. versionadded:: 3006 .. versionadded:: 3008.0
verify_ssl verify_ssl
Whether or not to verify the server cert when connecting. Default True. Whether or not to verify the server cert when connecting. Default True.
.. versionadded:: 3006 .. versionadded:: 3008.0
CLI Example: CLI Example:
@ -1520,12 +1491,12 @@ def remove(
ssl ssl
Whether or not to connect to MongoDB over SSL. Default ``False``. Whether or not to connect to MongoDB over SSL. Default ``False``.
.. versionadded:: 3006 .. versionadded:: 3008.0
verify_ssl verify_ssl
Whether or not to verify the server cert when connecting. Default True. Whether or not to verify the server cert when connecting. Default True.
.. versionadded:: 3006 .. versionadded:: 3008.0
CLI Example: CLI Example:
@ -1559,7 +1530,7 @@ def remove(
for count in range(0, w): for count in range(0, w):
res = col.delete_one(query) res = col.delete_one(query)
deleted_count += res.deleted_count deleted_count += res.deleted_count
return "{} objects removed".format(deleted_count) return f"{deleted_count} objects removed"
except pymongo.errors.PyMongoError as err: except pymongo.errors.PyMongoError as err:
log.error("Removing objects failed with error: %s", _get_error_message(err)) log.error("Removing objects failed with error: %s", _get_error_message(err))
return _get_error_message(err) return _get_error_message(err)

View file

@ -180,7 +180,9 @@ def _get_conn(ret):
mdb = conn.get_database() mdb = conn.get_database()
else: else:
if PYMONGO_VERSION > Version("2.3"): if PYMONGO_VERSION > Version("2.3"):
conn = pymongo.MongoClient(host, port, username=user, password=password, ssl=ssl) conn = pymongo.MongoClient(
host, port, username=user, password=password, ssl=ssl
)
else: else:
if uri: if uri:
raise salt.exceptions.SaltConfigurationError( raise salt.exceptions.SaltConfigurationError(

View file

@ -138,7 +138,7 @@ def _get_conn(ret):
# pymongo versions < 2.3 until then there are # pymongo versions < 2.3 until then there are
# a bunch of these sections that need to be supported # a bunch of these sections that need to be supported
if PYMONGO_VERSION > _LooseVersion("2.3"): if PYMONGO_VERSION > Version("2.3"):
conn = pymongo.MongoClient(host=host, port=port, ssl=ssl) conn = pymongo.MongoClient(host=host, port=port, ssl=ssl)
else: else:
conn = pymongo.Connection(host, port) conn = pymongo.Connection(host, port)

View file

@ -53,12 +53,12 @@ def absent(
ssl ssl
Whether or not to use SSL to connect to mongodb. Default False. Whether or not to use SSL to connect to mongodb. Default False.
.. versionadded:: 3005.0 .. versionadded:: 3008.0
verify_ssl verify_ssl
Whether or not to verify the server cert when connecting. Default True. Whether or not to verify the server cert when connecting. Default True.
.. versionadded:: 3005.0 .. versionadded:: 3008.0
""" """
ret = {"name": name, "changes": {}, "result": True, "comment": ""} ret = {"name": name, "changes": {}, "result": True, "comment": ""}
@ -88,9 +88,9 @@ def absent(
ssl=ssl, ssl=ssl,
verify_ssl=verify_ssl, verify_ssl=verify_ssl,
): ):
ret["comment"] = "Database {} has been removed".format(name) ret["comment"] = f"Database {name} has been removed"
ret["changes"][name] = "Absent" ret["changes"][name] = "Absent"
return ret return ret
ret["comment"] = "Database {} is not present".format(name) ret["comment"] = f"Database {name} is not present"
return ret return ret

View file

@ -64,12 +64,12 @@ def present(
ssl ssl
Whether or not to use SSL to connect to mongodb. Default False. Whether or not to use SSL to connect to mongodb. Default False.
.. versionadded:: 3005.0 .. versionadded:: 3008.0
verify_ssl verify_ssl
Whether or not to verify the server cert when connecting. Default True. Whether or not to verify the server cert when connecting. Default True.
.. versionadded:: 3005.0 .. versionadded:: 3008.0
Example: Example:
@ -93,7 +93,7 @@ def present(
"name": name, "name": name,
"changes": {}, "changes": {},
"result": True, "result": True,
"comment": "User {} is already present".format(name), "comment": f"User {name} is already present",
} }
# setup default empty roles if not provided to preserve previous API interface # setup default empty roles if not provided to preserve previous API interface
@ -105,7 +105,7 @@ def present(
port = int(port or __salt__["config.option"]("mongodb.port")) port = int(port or __salt__["config.option"]("mongodb.port"))
except TypeError: except TypeError:
ret["result"] = False ret["result"] = False
ret["comment"] = "Port ({!r}) is not an integer.".format(port) ret["comment"] = f"Port ({port!r}) is not an integer."
return ret return ret
# check if user exists # check if user exists
@ -126,7 +126,7 @@ def present(
# users= (False, 'not authorized on admin to execute command { usersInfo: "root" }') # users= (False, 'not authorized on admin to execute command { usersInfo: "root" }')
if not users[0]: if not users[0]:
ret["result"] = False ret["result"] = False
ret["comment"] = "Mongo Err: {}".format(users[1]) ret["comment"] = f"Mongo Err: {users[1]}"
return ret return ret
# check each user occurrence # check each user occurrence
@ -174,7 +174,7 @@ def present(
if __opts__["test"]: if __opts__["test"]:
ret["result"] = None ret["result"] = None
ret["comment"] = "User {} is not present and needs to be created".format(name) ret["comment"] = f"User {name} is not present and needs to be created"
return ret return ret
# The user is not present, make it! # The user is not present, make it!
if __salt__["mongodb.user_create"]( if __salt__["mongodb.user_create"](
@ -190,10 +190,10 @@ def present(
ssl=ssl, ssl=ssl,
verify_ssl=verify_ssl, verify_ssl=verify_ssl,
): ):
ret["comment"] = "User {} has been created".format(name) ret["comment"] = f"User {name} has been created"
ret["changes"][name] = "Present" ret["changes"][name] = "Present"
else: else:
ret["comment"] = "Failed to create database {}".format(name) ret["comment"] = f"Failed to create database {name}"
ret["result"] = False ret["result"] = False
return ret return ret
@ -238,12 +238,12 @@ def absent(
ssl ssl
Whether or not to use SSL to connect to mongodb. Default False. Whether or not to use SSL to connect to mongodb. Default False.
.. versionadded:: 3005.0 .. versionadded:: 3008.0
verify_ssl verify_ssl
Whether or not to verify the server cert when connecting. Default True. Whether or not to verify the server cert when connecting. Default True.
.. versionadded:: 3005.0 .. versionadded:: 3008.0
""" """
ret = {"name": name, "changes": {}, "result": True, "comment": ""} ret = {"name": name, "changes": {}, "result": True, "comment": ""}
@ -262,7 +262,7 @@ def absent(
if user_exists is True: if user_exists is True:
if __opts__["test"]: if __opts__["test"]:
ret["result"] = None ret["result"] = None
ret["comment"] = "User {} is present and needs to be removed".format(name) ret["comment"] = f"User {name} is present and needs to be removed"
return ret return ret
if __salt__["mongodb.user_remove"]( if __salt__["mongodb.user_remove"](
name, name,
@ -275,7 +275,7 @@ def absent(
ssl=ssl, ssl=ssl,
verify_ssl=verify_ssl, verify_ssl=verify_ssl,
): ):
ret["comment"] = "User {} has been removed".format(name) ret["comment"] = f"User {name} has been removed"
ret["changes"][name] = "Absent" ret["changes"][name] = "Absent"
return ret return ret
@ -287,5 +287,5 @@ def absent(
return ret return ret
# fallback # fallback
ret["comment"] = "User {} is not present".format(name) ret["comment"] = f"User {name} is not present"
return ret return ret

View file

@ -36,7 +36,7 @@ def ensure_deps(states):
) )
assert ( assert (
installation_result.result is True installation_result.result is True
), "unable to pip install requirements {}".format(installation_result.comment) ), f"unable to pip install requirements {installation_result.comment}"
@pytest.fixture(scope="module") @pytest.fixture(scope="module")

View file

@ -4,8 +4,8 @@
import pytest import pytest
import salt.modules.mongodb as mongodb
import salt.modules.mongodb import salt.modules.mongodb
import salt.modules.mongodb as mongodb
from tests.support.mock import MagicMock, patch from tests.support.mock import MagicMock, patch
try: try:
@ -722,6 +722,7 @@ def test_remove():
expected = "1 objects removed" expected = "1 objects removed"
assert ret == expected assert ret == expected
@pytest.fixture(autouse=True) @pytest.fixture(autouse=True)
def fake_pymongo(): def fake_pymongo():
salt.modules.mongodb.pymongo = MagicMock() salt.modules.mongodb.pymongo = MagicMock()

View file

@ -33,6 +33,7 @@ def test_config_exception():
with pytest.raises(salt.exceptions.SaltConfigurationError): with pytest.raises(salt.exceptions.SaltConfigurationError):
mongo_future_return.returner({}) mongo_future_return.returner({})
@pytest.mark.parametrize( @pytest.mark.parametrize(
"expected_ssl, use_ssl", "expected_ssl, use_ssl",
[ [

View file

@ -32,7 +32,7 @@ def test_mongo_returner_should_correctly_pass_ssl_to_MongoClient_when_ret_is_set
) as fake_mongo, patch.object( ) as fake_mongo, patch.object(
mongo_return, mongo_return,
"PYMONGO_VERSION", "PYMONGO_VERSION",
mongo_return._LooseVersion("99999"), mongo_return.Version("99999"),
create=True, create=True,
), patch.dict( ), patch.dict(
"salt.returners.mongo_return.__opts__", "salt.returners.mongo_return.__opts__",
@ -63,7 +63,7 @@ def test_mongo_returner_should_correctly_pass_ssl_to_MongoClient(expected_ssl, u
) as fake_mongo, patch.object( ) as fake_mongo, patch.object(
mongo_return, mongo_return,
"PYMONGO_VERSION", "PYMONGO_VERSION",
mongo_return._LooseVersion("99999"), mongo_return.Version("99999"),
create=True, create=True,
), patch.dict( ), patch.dict(
"salt.returners.mongo_return.__opts__", "salt.returners.mongo_return.__opts__",

View file

@ -45,16 +45,16 @@ def test_absent():
{"mongodb.db_exists": mock, "mongodb.db_remove": mock_t}, {"mongodb.db_exists": mock, "mongodb.db_remove": mock_t},
): ):
with patch.dict(mongodb_database.__opts__, {"test": True}): with patch.dict(mongodb_database.__opts__, {"test": True}):
comt = "Database {} is present and needs to be removed".format(name) comt = f"Database {name} is present and needs to be removed"
ret.update({"comment": comt}) ret.update({"comment": comt})
assert mongodb_database.absent(name) == ret assert mongodb_database.absent(name) == ret
with patch.dict(mongodb_database.__opts__, {"test": False}): with patch.dict(mongodb_database.__opts__, {"test": False}):
comt = "Database {} has been removed".format(name) comt = f"Database {name} has been removed"
ret.update({"comment": comt, "result": True, "changes": {"mydb": "Absent"}}) ret.update({"comment": comt, "result": True, "changes": {"mydb": "Absent"}})
assert mongodb_database.absent(name) == ret assert mongodb_database.absent(name) == ret
comt = "Database {} is not present".format(name) comt = f"Database {name} is not present"
ret.update({"comment": comt, "changes": {}}) ret.update({"comment": comt, "changes": {}})
assert mongodb_database.absent(name) == ret assert mongodb_database.absent(name) == ret

View file

@ -58,12 +58,12 @@ def test_present():
assert mongodb_user.present(name, passwd) == ret assert mongodb_user.present(name, passwd) == ret
with patch.dict(mongodb_user.__opts__, {"test": True}): with patch.dict(mongodb_user.__opts__, {"test": True}):
comt = "User {} is not present and needs to be created".format(name) comt = f"User {name} is not present and needs to be created"
ret.update({"comment": comt, "result": None}) ret.update({"comment": comt, "result": None})
assert mongodb_user.present(name, passwd) == ret assert mongodb_user.present(name, passwd) == ret
with patch.dict(mongodb_user.__opts__, {"test": False}): with patch.dict(mongodb_user.__opts__, {"test": False}):
comt = "User {} has been created".format(name) comt = f"User {name} has been created"
ret.update({"comment": comt, "result": True, "changes": {name: "Present"}}) ret.update({"comment": comt, "result": True, "changes": {name: "Present"}})
assert mongodb_user.present(name, passwd) == ret assert mongodb_user.present(name, passwd) == ret
@ -83,16 +83,16 @@ def test_absent():
{"mongodb.user_exists": mock, "mongodb.user_remove": mock_t}, {"mongodb.user_exists": mock, "mongodb.user_remove": mock_t},
): ):
with patch.dict(mongodb_user.__opts__, {"test": True}): with patch.dict(mongodb_user.__opts__, {"test": True}):
comt = "User {} is present and needs to be removed".format(name) comt = f"User {name} is present and needs to be removed"
ret.update({"comment": comt, "result": None}) ret.update({"comment": comt, "result": None})
assert mongodb_user.absent(name) == ret assert mongodb_user.absent(name) == ret
with patch.dict(mongodb_user.__opts__, {"test": False}): with patch.dict(mongodb_user.__opts__, {"test": False}):
comt = "User {} has been removed".format(name) comt = f"User {name} has been removed"
ret.update({"comment": comt, "result": True, "changes": {name: "Absent"}}) ret.update({"comment": comt, "result": True, "changes": {name: "Absent"}})
assert mongodb_user.absent(name) == ret assert mongodb_user.absent(name) == ret
comt = "User {} is not present".format(name) comt = f"User {name} is not present"
ret.update({"comment": comt, "result": True, "changes": {}}) ret.update({"comment": comt, "result": True, "changes": {}})
assert mongodb_user.absent(name) == ret assert mongodb_user.absent(name) == ret
@ -115,7 +115,7 @@ def test_when_absent_is_called_it_should_pass_the_correct_ssl_argument_to_MongoC
expected_ssl, expected_allow_invalid, absent_kwargs expected_ssl, expected_allow_invalid, absent_kwargs
): ):
with patch.dict(mongodb_user.__opts__, {"test": False}), patch( with patch.dict(mongodb_user.__opts__, {"test": False}), patch(
"salt.modules.mongodb._LooseVersion", autospec=True, return_value=4 "salt.modules.mongodb.Version", autospec=True, return_value=4
): ):
salt.modules.mongodb.pymongo.database.Database.return_value.command.return_value = { salt.modules.mongodb.pymongo.database.Database.return_value.command.return_value = {
"users": [ "users": [