File figure-out-python-interpreter-to-use-inside-containe.patch of Package salt.23536

From 271826b0baa6b2281bc2eac9118a0fcc4675f106 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pablo=20Su=C3=A1rez=20Hern=C3=A1ndez?=
 <psuarezhernandez@suse.com>
Date: Wed, 19 May 2021 16:24:27 +0100
Subject: [PATCH] Figure out Python interpreter to use inside
 containers

Fix unit test for dockermod.call function
---
 salt/modules/dockermod.py            | 28 +++++++++++++++++++++++---
 tests/unit/modules/test_dockermod.py | 30 +++++++++++++++-------------
 2 files changed, 41 insertions(+), 17 deletions(-)

diff --git a/salt/modules/dockermod.py b/salt/modules/dockermod.py
index 176b4db926..cad307e7af 100644
--- a/salt/modules/dockermod.py
+++ b/salt/modules/dockermod.py
@@ -217,7 +217,6 @@ import re
 import shutil
 import string
 import subprocess
-import sys
 import time
 import uuid
 
@@ -6865,9 +6864,32 @@ def call(name, function, *args, **kwargs):
         name, thin_path, os.path.join(thin_dest_path, os.path.basename(thin_path))
     )
 
+    # figure out available python interpreter inside the container
+    pycmds = (
+        "python3",
+        "/usr/libexec/platform-python",
+        "python27",
+        "python2.7",
+        "python26",
+        "python2.6",
+        "python2",
+        "python",
+    )
+    container_python_bin = None
+    for py_cmd in pycmds:
+        cmd = [py_cmd] + ["--version"]
+        ret = run_all(name, subprocess.list2cmdline(cmd))
+        if ret["retcode"] == 0:
+            container_python_bin = py_cmd
+            break
+    if not container_python_bin:
+        raise CommandExecutionError(
+            "Python interpreter cannot be found inside the container. Make sure Python is installed in the container"
+        )
+
     # untar archive
     untar_cmd = [
-        "python",
+        container_python_bin,
         "-c",
         ("import tarfile; " 'tarfile.open("{0}/{1}").extractall(path="{0}")').format(
             thin_dest_path, os.path.basename(thin_path)
@@ -6880,7 +6902,7 @@ def call(name, function, *args, **kwargs):
     try:
         salt_argv = (
             [
-                "python{0}".format(sys.version_info[0]),
+                container_python_bin,
                 os.path.join(thin_dest_path, "salt-call"),
                 "--metadata",
                 "--local",
diff --git a/tests/unit/modules/test_dockermod.py b/tests/unit/modules/test_dockermod.py
index 48526acb71..ebe97a83f5 100644
--- a/tests/unit/modules/test_dockermod.py
+++ b/tests/unit/modules/test_dockermod.py
@@ -1049,33 +1049,35 @@ class DockerTestCase(TestCase, LoaderModuleMockMixin):
         # [ call(name, [args]), ...
         self.maxDiff = None
         self.assertIn("mkdir", docker_run_all_mock.mock_calls[0][1][1])
-        self.assertIn("mkdir", docker_run_all_mock.mock_calls[4][1][1])
+        self.assertIn("mkdir", docker_run_all_mock.mock_calls[5][1][1])
         self.assertNotEqual(
             docker_run_all_mock.mock_calls[0][1][1],
-            docker_run_all_mock.mock_calls[4][1][1],
+            docker_run_all_mock.mock_calls[5][1][1],
         )
 
-        self.assertIn("salt-call", docker_run_all_mock.mock_calls[2][1][1])
-        self.assertIn("salt-call", docker_run_all_mock.mock_calls[6][1][1])
+        self.assertEqual("python3 --version", docker_run_all_mock.mock_calls[1][1][1])
+
+        self.assertIn("salt-call", docker_run_all_mock.mock_calls[3][1][1])
+        self.assertIn("salt-call", docker_run_all_mock.mock_calls[8][1][1])
         self.assertNotEqual(
-            docker_run_all_mock.mock_calls[2][1][1],
-            docker_run_all_mock.mock_calls[6][1][1],
+            docker_run_all_mock.mock_calls[3][1][1],
+            docker_run_all_mock.mock_calls[8][1][1],
         )
 
         # check thin untar
-        self.assertIn("tarfile", docker_run_all_mock.mock_calls[1][1][1])
-        self.assertIn("tarfile", docker_run_all_mock.mock_calls[5][1][1])
+        self.assertIn("tarfile", docker_run_all_mock.mock_calls[2][1][1])
+        self.assertIn("tarfile", docker_run_all_mock.mock_calls[7][1][1])
         self.assertNotEqual(
-            docker_run_all_mock.mock_calls[1][1][1],
-            docker_run_all_mock.mock_calls[5][1][1],
+            docker_run_all_mock.mock_calls[2][1][1],
+            docker_run_all_mock.mock_calls[7][1][1],
         )
 
         # check directory cleanup
-        self.assertIn("rm -rf", docker_run_all_mock.mock_calls[3][1][1])
-        self.assertIn("rm -rf", docker_run_all_mock.mock_calls[7][1][1])
+        self.assertIn("rm -rf", docker_run_all_mock.mock_calls[4][1][1])
+        self.assertIn("rm -rf", docker_run_all_mock.mock_calls[9][1][1])
         self.assertNotEqual(
-            docker_run_all_mock.mock_calls[3][1][1],
-            docker_run_all_mock.mock_calls[7][1][1],
+            docker_run_all_mock.mock_calls[4][1][1],
+            docker_run_all_mock.mock_calls[9][1][1],
         )
 
         self.assertEqual({"retcode": 0, "comment": "container cmd"}, ret)
-- 
2.31.1


openSUSE Build Service is sponsored by