From 1b54843abe5fad0bac844d6d5d9707df3e501aae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20Su=C3=A1rez=20Hern=C3=A1ndez?= 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 ab2296a945..6d60a9a5aa 100644 --- a/salt/modules/dockermod.py +++ b/salt/modules/dockermod.py @@ -209,7 +209,6 @@ import re import shutil import string import subprocess -import sys import time import uuid @@ -6728,9 +6727,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( @@ -6744,7 +6766,7 @@ def call(name, function, *args, **kwargs): try: salt_argv = ( [ - "python{}".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 2c3665de85..fcedaf9272 100644 --- a/tests/unit/modules/test_dockermod.py +++ b/tests/unit/modules/test_dockermod.py @@ -987,33 +987,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.33.0