Debian Codegraph @

tests/unit/test_remote.py

raw
1 # THIS FILE IS PART OF THE CYLC WORKFLOW ENGINE.
2 # Copyright (C) NIWA & British Crown (Met Office) & Contributors.
3 #
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 """Test the cylc.flow.remote module."""
17
18 import os
19
20 import pytest
21
22 from cylc.flow.remote import (
23 run_cmd, construct_rsync_over_ssh_cmd, construct_ssh_cmd
24 )
25 import cylc.flow
26
27
28 def test_run_cmd_stdin_str():
29 """Test passing stdin as a string."""
30 proc = run_cmd(
31 ['sed', 's/foo/bar/'],
32 stdin_str='1foo2',
33 capture_process=True
34 )
35 assert [s.strip() for s in proc.communicate()] == [
36 '1bar2',
37 ''
38 ]
39
40
41 def test_run_cmd_stdin_file(tmp_path):
42 """Test passing stdin as a file."""
43 tmp_path = tmp_path / 'stdin'
44 with tmp_path.open('w+') as tmp_file:
45 tmp_file.write('1foo2')
46 tmp_file = tmp_path.open('rb')
47 proc = run_cmd(
48 ['sed', 's/foo/bar/'],
49 stdin=tmp_file,
50 capture_process=True
51 )
52 assert [s.strip() for s in proc.communicate()] == [
53 '1bar2',
54 ''
55 ]
56
57
58 def test_construct_rsync_over_ssh_cmd():
59 """Function against known good output.
60 """
61 cmd, host = construct_rsync_over_ssh_cmd(
62 '/foo',
63 '/bar',
64 {
65 'rsync command': 'rsync command',
66 'hosts': ['miklegard'],
67 'ssh command': 'strange_ssh',
68 'selection': {'method': 'definition order'},
69 'name': 'testplat'
70 }
71 )
72 assert host == 'miklegard'
73 assert cmd == [
74 'rsync',
75 'command',
76 '--delete',
77 '--rsh=strange_ssh',
78 '--include=/.service/',
79 '--include=/.service/server.key',
80 '-a',
81 '--checksum',
82 '--out-format=%o %n%L',
83 '--no-t',
84 '--exclude=/log',
85 '--exclude=/share',
86 '--exclude=/work',
87 '--include=/ana/***',
88 '--include=/app/***',
89 '--include=/bin/***',
90 '--include=/etc/***',
91 '--include=/lib/***',
92 '--exclude=*',
93 '/foo/',
94 'miklegard:/bar/',
95 ]
96
97
98 def test_construct_ssh_cmd_forward_env(monkeypatch: pytest.MonkeyPatch):
99 """ Test for 'ssh forward environment variables'
100 """
101 # Clear CYLC_* env vars as these will show up in the command
102 for env_var in os.environ:
103 if env_var.startswith('CYLC'):
104 monkeypatch.delenv(env_var)
105
106 host = 'example.com'
107 config = {
108 'ssh command': 'ssh',
109 'use login shell': None,
110 'cylc path': None,
111 'ssh forward environment variables': ['FOO', 'BAZ'],
112 }
113
114 # Variable isn't set, no change to command
115 expect = [
116 'ssh',
117 host,
118 'env',
119 f'CYLC_VERSION={cylc.flow.__version__}',
120 'cylc',
121 'play',
122 ]
123 cmd = construct_ssh_cmd(['play'], config, host)
124 assert cmd == expect
125
126 # Variable is set, appears in `env` list
127 monkeypatch.setenv('FOO', 'BAR')
128 expect = [
129 'ssh',
130 host,
131 'env',
132 f'CYLC_VERSION={cylc.flow.__version__}',
133 'FOO=BAR',
134 'cylc',
135 'play',
136 ]
137 cmd = construct_ssh_cmd(['play'], config, host)
138 assert cmd == expect