diff --git a/scripts/leapp_script.py b/scripts/leapp_script.py index 4975176..6da9b73 100644 --- a/scripts/leapp_script.py +++ b/scripts/leapp_script.py @@ -294,8 +294,17 @@ def remove_previous_reports(): def execute_operation(command): + new_env = {} + for key, value in os.environ.items(): + valid_prefix = "RHC_WORKER_" + if key.startswith(valid_prefix): + # This also removes multiple valid prefixes + new_env[key.replace(valid_prefix, "")] = value + else: + new_env[key] = value + print("Executing {} ...".format(SCRIPT_TYPE.title())) - output, _ = run_subprocess(command) + output, _ = run_subprocess(command, env=new_env) return output diff --git a/tests/test_run.py b/tests/test_run.py index a18167f..67902ea 100644 --- a/tests/test_run.py +++ b/tests/test_run.py @@ -1,10 +1,25 @@ -from mock import patch +from mock import patch, ANY from scripts.leapp_script import execute_operation @patch("scripts.leapp_script.run_subprocess", return_value=(b"", 0)) -def test_run_leapp_upgrade(mock_popen): - execute_operation(["fake command"]) +def test_execute_simple_command(mock_popen): + output = execute_operation(["fake command"]) - mock_popen.assert_called_once_with(["fake command"]) + mock_popen.assert_called_once_with(["fake command"], env=ANY) + assert output == "" + + +def test_execure_custom_variables(): + mock_env = {"FOO": "BAR", "BAR": "BAZ", "RHC_WORKER_LALA": "LAND"} + command = ["/usr/bin/leapp", "preupgrade"] + with patch("os.environ", mock_env), patch( + "scripts.leapp_script.run_subprocess", return_value=(b"", 0) + ) as mock_popen: + result = execute_operation(command) + mock_popen.assert_called_once_with( + command, + env={"FOO": "BAR", "BAR": "BAZ", "LALA": "LAND"}, + ) + assert result == ""