forked from pyinfra-dev/pyinfra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_cli.py
192 lines (168 loc) · 5.88 KB
/
test_cli.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
from os import path
from unittest import TestCase
from pyinfra_cli.main import _main
from ..paramiko_util import PatchSSHTestCase
from .util import run_cli
class TestCliEagerFlags(TestCase):
def test_print_help(self):
result = run_cli("--version")
assert result.exit_code == 0, result.stdout
result = run_cli("--help")
assert result.exit_code == 0, result.stdout
class TestOperationCli(PatchSSHTestCase):
def test_invalid_operation_module(self):
result = run_cli(
path.join("tests", "test_cli", "deploy", "inventories", "inventory.py"),
"not_a_module.shell",
)
assert result.exit_code == 1, result.stdout
assert "No such module: not_a_module"
def test_invalid_operation_function(self):
result = run_cli(
path.join("tests", "test_cli", "deploy", "inventories", "inventory.py"),
"server.not_an_operation",
)
assert result.exit_code == 1, result.stdout
assert "No such operation: server.not_an_operation"
def test_deploy_operation(self):
result = run_cli(
"-y",
path.join("tests", "test_cli", "deploy", "inventories", "inventory.py"),
"server.shell",
"echo hi",
)
assert result.exit_code == 0, result.stdout
def test_deploy_operation_with_all(self):
result = run_cli(
"-y",
path.join("tests", "test_cli", "deploy", "inventory_all.py"),
"server.shell",
"echo hi",
)
assert result.exit_code == 0, result.stdout
def test_deploy_operation_json_args(self):
result = run_cli(
"-y",
path.join("tests", "test_cli", "deploy", "inventory_all.py"),
"server.shell",
'[["echo hi"], {}]',
)
assert result.exit_code == 0, result.stdout
class TestFactCli(PatchSSHTestCase):
def test_get_fact(self):
result = run_cli(
path.join("tests", "test_cli", "deploy", "inventories", "inventory.py"),
"fact",
"server.Os",
)
assert result.exit_code == 0, result.stdout
assert '"somehost": null' in result.stdout
def test_get_fact_with_kwargs(self):
result = run_cli(
path.join("tests", "test_cli", "deploy", "inventories", "inventory.py"),
"fact",
"files.File",
"path=.",
)
assert result.exit_code == 0, result.stdout
assert '"somehost": null' in result.stdout
class TestExecCli(PatchSSHTestCase):
def test_exec_command(self):
result = run_cli(
path.join("tests", "test_cli", "deploy", "inventories", "inventory.py"),
"exec",
"--",
"echo hi",
)
assert result.exit_code == 0, result.stdout
def test_exec_command_with_options(self):
result = run_cli(
path.join("tests", "test_cli", "deploy", "inventories", "inventory.py"),
"exec",
"--sudo",
"--sudo-user",
"pyinfra",
"--su-user",
"pyinfrawhat",
"--port",
"1022",
"--user",
"ubuntu",
"--",
"echo hi",
)
assert result.exit_code == 0, result.stdout
def test_exec_command_with_serial(self):
result = run_cli(
path.join("tests", "test_cli", "deploy", "inventories", "inventory.py"),
"exec",
"--serial",
"--",
"echo hi",
)
assert result.exit_code == 0, result.stdout
def test_exec_command_with_no_wait(self):
result = run_cli(
path.join("tests", "test_cli", "deploy", "inventories", "inventory.py"),
"exec",
"--no-wait",
"--",
"echo hi",
)
assert result.exit_code == 0, result.stdout
def test_exec_command_with_debug_operations(self):
result = run_cli(
path.join("tests", "test_cli", "deploy", "inventories", "inventory.py"),
"exec",
"--debug-operations",
"--",
"echo hi",
)
assert result.exit_code == 0, result.stdout
def test_exec_command_with_debug_facts(self):
result = run_cli(
path.join("tests", "test_cli", "deploy", "inventories", "inventory.py"),
"exec",
"--debug-facts",
"--",
"echo hi",
)
assert result.exit_code == 0, result.stdout
class TestDirectMainExecution(PatchSSHTestCase):
"""
These tests are very similar as above, without the click wrappers - basically
here because coverage.py fails to properly detect all the code under the wrapper.
"""
def test_deploy_operation_direct(self):
with self.assertRaises(SystemExit) as e:
_main(
inventory=path.join("tests", "test_deploy", "inventories", "inventory.py"),
operations=["server.shell", "echo hi"],
chdir=None,
group_data=None,
verbosity=0,
ssh_user=None,
ssh_port=None,
ssh_key=None,
ssh_key_password=None,
ssh_password=None,
sudo=False,
sudo_user=None,
use_sudo_password=False,
su_user=None,
parallel=None,
fail_percent=0,
dry=False,
yes=True,
limit=None,
no_wait=False,
serial=False,
shell_executable=None,
data=tuple(),
debug=False,
debug_facts=False,
debug_all=False,
debug_operations=False,
config_filename="config.py",
)
assert e.args == (0,)