forked from pyinfra-dev/pyinfra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_chroot.py
178 lines (135 loc) · 5.77 KB
/
test_chroot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# encoding: utf-8
import shlex
from subprocess import PIPE
from unittest import TestCase
from unittest.mock import MagicMock, mock_open, patch
from pyinfra.api import Config, State
from pyinfra.api.connect import connect_all
from pyinfra.api.exceptions import PyinfraError
from pyinfra.connectors.util import make_unix_command
from ..util import make_inventory
def fake_chroot_shell(command, splitlines=None):
if command == "chroot /not-a-chroot ls":
return True
raise PyinfraError("Invalid command: {0}".format(command))
@patch("pyinfra.connectors.chroot.local.shell", fake_chroot_shell)
@patch("pyinfra.connectors.chroot.mkstemp", lambda: (None, "__tempfile__"))
@patch("pyinfra.connectors.chroot.os.remove", lambda f: None)
@patch("pyinfra.connectors.chroot.open", mock_open(read_data="test!"), create=True)
@patch("pyinfra.api.util.open", mock_open(read_data="test!"), create=True)
class TestChrootConnector(TestCase):
def setUp(self):
self.fake_popen_patch = patch("pyinfra.connectors.util.Popen")
self.fake_popen_mock = self.fake_popen_patch.start()
def tearDown(self):
self.fake_popen_patch.stop()
def test_connect_all(self):
inventory = make_inventory(hosts=("@chroot/not-a-chroot",))
state = State(inventory, Config())
connect_all(state)
assert len(state.active_hosts) == 1
def test_connect_host(self):
inventory = make_inventory(hosts=("@chroot/not-a-chroot",))
state = State(inventory, Config())
host = inventory.get_host("@chroot/not-a-chroot")
host.connect(reason=True)
assert len(state.active_hosts) == 0
def test_connect_all_error(self):
inventory = make_inventory(hosts=("@chroot/a-broken-chroot",))
state = State(inventory, Config())
with self.assertRaises(PyinfraError):
connect_all(state)
def test_run_shell_command(self):
inventory = make_inventory(hosts=("@chroot/not-a-chroot",))
State(inventory, Config())
host = inventory.get_host("@chroot/not-a-chroot")
host.connect()
command = "echo hoi"
self.fake_popen_mock().returncode = 0
out = host.run_shell_command(
command,
_stdin="hello",
_get_pty=True,
print_output=True,
)
assert len(out) == 2
assert out[0] is True
command = make_unix_command(command).get_raw_value()
command = shlex.quote(command)
docker_command = "chroot /not-a-chroot sh -c {0}".format(command)
shell_command = make_unix_command(docker_command).get_raw_value()
self.fake_popen_mock.assert_called_with(
shell_command,
shell=True,
stdout=PIPE,
stderr=PIPE,
stdin=PIPE,
)
def test_run_shell_command_success_exit_codes(self):
inventory = make_inventory(hosts=("@chroot/not-a-chroot",))
state = State(inventory, Config())
connect_all(state)
host = inventory.get_host("@chroot/not-a-chroot")
command = "echo hoi"
self.fake_popen_mock().returncode = 1
out = host.run_shell_command(command, _success_exit_codes=[1])
assert len(out) == 2
assert out[0] is True
def test_run_shell_command_error(self):
inventory = make_inventory(hosts=("@chroot/not-a-chroot",))
state = State(inventory, Config())
connect_all(state)
host = inventory.get_host("@chroot/not-a-chroot")
command = "echo hoi"
self.fake_popen_mock().returncode = 1
out = host.run_shell_command(command)
assert len(out) == 2
assert out[0] is False
def test_put_file(self):
inventory = make_inventory(hosts=("@chroot/not-a-chroot",))
state = State(inventory, Config())
connect_all(state)
host = inventory.get_host("@chroot/not-a-chroot")
fake_process = MagicMock(returncode=0)
self.fake_popen_mock.return_value = fake_process
host.put_file("not-a-file", "not-another-file", print_output=True)
self.fake_popen_mock.assert_called_with(
"sh -c 'cp __tempfile__ /not-a-chroot/not-another-file'",
shell=True,
stdout=PIPE,
stderr=PIPE,
stdin=PIPE,
)
def test_put_file_error(self):
inventory = make_inventory(hosts=("@chroot/not-a-chroot",))
state = State(inventory, Config())
connect_all(state)
host = inventory.get_host("@chroot/not-a-chroot")
fake_process = MagicMock(returncode=1)
self.fake_popen_mock.return_value = fake_process
with self.assertRaises(IOError):
host.put_file("not-a-file", "not-another-file", print_output=True)
def test_get_file(self):
inventory = make_inventory(hosts=("@chroot/not-a-chroot",))
state = State(inventory, Config())
connect_all(state)
host = inventory.get_host("@chroot/not-a-chroot")
fake_process = MagicMock(returncode=0)
self.fake_popen_mock.return_value = fake_process
host.get_file("not-a-file", "not-another-file", print_output=True)
self.fake_popen_mock.assert_called_with(
"sh -c 'cp /not-a-chroot/not-a-file __tempfile__'",
shell=True,
stdout=PIPE,
stderr=PIPE,
stdin=PIPE,
)
def test_get_file_error(self):
inventory = make_inventory(hosts=("@chroot/not-a-chroot",))
state = State(inventory, Config())
connect_all(state)
host = inventory.get_host("@chroot/not-a-chroot")
fake_process = MagicMock(returncode=1)
self.fake_popen_mock.return_value = fake_process
with self.assertRaises(IOError):
host.get_file("not-a-file", "not-another-file", print_output=True)