File use-cachedir-for-extension_modules-in-salt-call-bsc-.patch of Package salt

From 2fb453d04b8abe765a964174ae77d57398f2877a Mon Sep 17 00:00:00 2001
From: Victor Zhestkov <vzhestkov@suse.com>
Date: Wed, 25 Sep 2024 14:06:19 +0300
Subject: [PATCH] Use --cachedir for extension_modules in salt-call
 (bsc#1226141)

* Use --cachedir parameter for extension_modules with salt-call

* Add test to check extension_modules value alignment
---
 salt/cli/call.py                         | 11 +++++++-
 salt/config/__init__.py                  |  6 +++++
 tests/pytests/unit/cli/test_salt_call.py | 32 ++++++++++++++++++++++++
 3 files changed, 48 insertions(+), 1 deletion(-)
 create mode 100644 tests/pytests/unit/cli/test_salt_call.py

diff --git a/salt/cli/call.py b/salt/cli/call.py
index 932dc61681..be3ded77e6 100644
--- a/salt/cli/call.py
+++ b/salt/cli/call.py
@@ -3,7 +3,7 @@ import os
 import salt.cli.caller
 import salt.defaults.exitcodes
 import salt.utils.parsers
-from salt.config import _expand_glob_path
+from salt.config import _expand_glob_path, prepend_root_dir
 
 
 class SaltCall(salt.utils.parsers.SaltCallOptionParser):
@@ -37,6 +37,15 @@ class SaltCall(salt.utils.parsers.SaltCallOptionParser):
         if self.options.master:
             self.config["master"] = self.options.master
 
+        if self.options.cachedir and self.config.get(
+            "extension_modules"
+        ) == os.path.join(self.config.get("__cachedir"), "extmods"):
+            # Override `extension_modules`, but only in case if it was autogenerated
+            cache_dir = os.path.abspath(self.options.cachedir)
+            self.config["cachedir"] = cache_dir
+            self.config["extension_modules"] = os.path.join(cache_dir, "extmods")
+            prepend_root_dir(self.config, ["cachedir", "extension_modules"])
+
         caller = salt.cli.caller.Caller.factory(self.config)
 
         if self.options.doc:
diff --git a/salt/config/__init__.py b/salt/config/__init__.py
index 68f2b0f674..b3cd5d85ae 100644
--- a/salt/config/__init__.py
+++ b/salt/config/__init__.py
@@ -1,6 +1,7 @@
 """
 All salt configuration loading and defaults should be in this module
 """
+
 import codecs
 import glob
 import logging
@@ -3841,6 +3842,11 @@ def apply_minion_config(
     _update_ssl_config(opts)
     _update_discovery_config(opts)
 
+    # Store original `cachedir` value, before overriding,
+    # to make overriding more accurate.
+    if "__cachedir" not in opts:
+        opts["__cachedir"] = opts["cachedir"]
+
     return opts
 
 
diff --git a/tests/pytests/unit/cli/test_salt_call.py b/tests/pytests/unit/cli/test_salt_call.py
new file mode 100644
index 0000000000..078f2af70d
--- /dev/null
+++ b/tests/pytests/unit/cli/test_salt_call.py
@@ -0,0 +1,32 @@
+import os
+
+from salt.cli.call import SaltCall
+from tests.support.mock import MagicMock, patch
+
+
+def test_passing_cachedir_to_extension_modules(temp_salt_minion):
+    """
+    Test passing `cachedir` CLI parameter to `extension_modules` opts
+    """
+    test_cache_dir = os.path.join(temp_salt_minion.config["root_dir"], "new_cache_tmp")
+    with patch(
+        "sys.argv",
+        [
+            "salt-call",
+            "--local",
+            "--config-dir",
+            temp_salt_minion.config["root_dir"],
+            "--cachedir",
+            test_cache_dir,
+            "test.true",
+        ],
+    ), patch("salt.utils.verify.verify_files", MagicMock()), patch(
+        "salt._logging.impl.setup_logfile_handler", MagicMock()
+    ):
+        salt_call = SaltCall()
+        with patch("salt.cli.caller.Caller.factory", MagicMock()) as caller_mock:
+            salt_call.run()
+            assert salt_call.config["cachedir"] == test_cache_dir
+            assert salt_call.config["extension_modules"] == os.path.join(
+                test_cache_dir, "extmods"
+            )
-- 
2.46.1

openSUSE Build Service is sponsored by