Skip to content

Commit

Permalink
Fix test polluted by env vars (#5796)
Browse files Browse the repository at this point in the history
  • Loading branch information
MetRonnie authored Oct 31, 2023
1 parent eb4ff48 commit 36a6ce9
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions tests/unit/test_remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,14 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""Test the cylc.flow.remote module."""

from cylc.flow.remote import run_cmd, construct_rsync_over_ssh_cmd, construct_ssh_cmd
import os
from unittest import mock

import pytest

from cylc.flow.remote import (
run_cmd, construct_rsync_over_ssh_cmd, construct_ssh_cmd
)
import cylc.flow


Expand Down Expand Up @@ -90,26 +96,29 @@ def test_construct_rsync_over_ssh_cmd():
]


def test_construct_ssh_cmd_forward_env():
def test_construct_ssh_cmd_forward_env(monkeypatch: pytest.MonkeyPatch):
""" Test for 'ssh forward environment variables'
"""
import os
# Clear CYLC_* env vars as these will show up in the command
for env_var in os.environ:
if env_var.startswith('CYLC'):
monkeypatch.delenv(env_var)

host = 'example.com'
config = {
'ssh command': 'ssh',
'use login shell': None,
'cylc path': None,
'ssh forward environment variables': ['FOO', 'BAZ'],
}
}

# Variable isn't set, no change to command
expect = ['ssh', host, 'env', f'CYLC_VERSION={cylc.flow.__version__}', 'cylc', 'play']
cmd = construct_ssh_cmd(['play'], config, host)
assert cmd == expect

# Variable is set, appears in `env` list
with mock.patch.dict(os.environ, {'FOO': 'BAR'}):
expect = ['ssh', host, 'env', f'CYLC_VERSION={cylc.flow.__version__}', 'FOO=BAR', 'cylc', 'play']
cmd = construct_ssh_cmd(['play'], config, host)
assert cmd == expect
monkeypatch.setenv('FOO', 'BAR')
expect = ['ssh', host, 'env', f'CYLC_VERSION={cylc.flow.__version__}', 'FOO=BAR', 'cylc', 'play']
cmd = construct_ssh_cmd(['play'], config, host)
assert cmd == expect

0 comments on commit 36a6ce9

Please sign in to comment.