Skip to content

Commit

Permalink
Merge pull request #17 from oamg/feat/work-with-worker-env-variables
Browse files Browse the repository at this point in the history
Use env variables passed by rhc during execution command
  • Loading branch information
andywaltlova authored Jun 27, 2024
2 parents 60e46a4 + b59392a commit c47ec2a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
11 changes: 10 additions & 1 deletion scripts/leapp_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
23 changes: 19 additions & 4 deletions tests/test_run.py
Original file line number Diff line number Diff line change
@@ -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 == ""

0 comments on commit c47ec2a

Please sign in to comment.