forked from goosemo/fabric
/
test_main.py
212 lines (175 loc) · 5.37 KB
/
test_main.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import sys
import copy
from fudge import Fake
from nose.tools import eq_, raises
from fabric.decorators import hosts, roles
from fabric.main import (get_hosts, parse_arguments, _merge, _escape_split,
load_fabfile)
import fabric.state
from fabric.state import _AttributeDict
from utils import mock_streams
from utils import with_patched_state_env
def test_argument_parsing():
for args, output in [
# Basic
('abc', ('abc', [], {}, [], [])),
# Arg
('ab:c', ('ab', ['c'], {}, [], [])),
# Kwarg
('a:b=c', ('a', [], {'b':'c'}, [], [])),
# Arg and kwarg
('a:b=c,d', ('a', ['d'], {'b':'c'}, [], [])),
# Multiple kwargs
('a:b=c,d=e', ('a', [], {'b':'c','d':'e'}, [], [])),
# Host
('abc:host=foo', ('abc', [], {}, ['foo'], [])),
# Hosts with single host
('abc:hosts=foo', ('abc', [], {}, ['foo'], [])),
# Hosts with multiple hosts
# Note: in a real shell, one would need to quote or escape "foo;bar".
# But in pure-Python that would get interpreted literally, so we don't.
('abc:hosts=foo;bar', ('abc', [], {}, ['foo', 'bar'], [])),
# Empty string args
("task:x=y,z=", ('task', [], {'x': 'y', 'z': ''}, [], [])),
("task:foo,,x=y", ('task', ['foo', ''], {'x': 'y'}, [], [])),
]:
yield eq_, parse_arguments([args]), [output]
def eq_hosts(command, host_list):
eq_(set(get_hosts(command, [], [])), set(host_list))
def test_hosts_decorator_by_itself():
"""
Use of @hosts only
"""
host_list = ['a', 'b']
@hosts(*host_list)
def command():
pass
eq_hosts(command, host_list)
fake_roles = {
'r1': ['a', 'b'],
'r2': ['b', 'c']
}
@with_patched_state_env({'roledefs': fake_roles})
def test_roles_decorator_by_itself():
"""
Use of @roles only
"""
@roles('r1')
def command():
pass
eq_hosts(command, ['a', 'b'])
@with_patched_state_env({'roledefs': fake_roles})
def test_hosts_and_roles_together():
"""
Use of @roles and @hosts together results in union of both
"""
@roles('r1', 'r2')
@hosts('a')
def command():
pass
eq_hosts(command, ['a', 'b', 'c'])
tuple_roles = {
'r1': ('a', 'b'),
'r2': ('b', 'c'),
}
@with_patched_state_env({'roledefs': tuple_roles})
def test_roles_as_tuples():
"""
Test that a list of roles as a tuple succeeds
"""
@roles('r1')
def command():
pass
eq_hosts(command, ['a', 'b'])
@with_patched_state_env({'hosts': ('foo', 'bar')})
def test_hosts_as_tuples():
"""
Test that a list of hosts as a tuple succeeds
"""
def command():
pass
eq_hosts(command, ['foo', 'bar'])
@with_patched_state_env({'hosts': ['foo']})
def test_hosts_decorator_overrides_env_hosts():
"""
If @hosts is used it replaces any env.hosts value
"""
@hosts('bar')
def command():
pass
eq_hosts(command, ['bar'])
assert 'foo' not in get_hosts(command, [], [])
def test_hosts_decorator_expands_single_iterable():
"""
@hosts(iterable) should behave like @hosts(*iterable)
"""
host_list = ['foo', 'bar']
@hosts(host_list)
def command():
pass
eq_(command.hosts, host_list)
def test_roles_decorator_expands_single_iterable():
"""
@roles(iterable) should behave like @roles(*iterable)
"""
role_list = ['foo', 'bar']
@roles(role_list)
def command():
pass
eq_(command.roles, role_list)
@with_patched_state_env({'roledefs': fake_roles})
@raises(SystemExit)
@mock_streams('stderr')
def test_aborts_on_nonexistent_roles():
"""
Aborts if any given roles aren't found
"""
_merge([], ['badrole'])
lazy_role = {'r1': lambda: ['a', 'b']}
@with_patched_state_env({'roledefs': lazy_role})
def test_lazy_roles():
"""
Roles may be callables returning lists, as well as regular lists
"""
@roles('r1')
def command():
pass
eq_hosts(command, ['a', 'b'])
def test_escaped_task_arg_split():
"""
Allow backslashes to escape the task argument separator character
"""
argstr = r"foo,bar\,biz\,baz,what comes after baz?"
eq_(
_escape_split(',', argstr),
['foo', 'bar,biz,baz', 'what comes after baz?']
)
def run_load_fabfile(path, sys_path):
# Module-esque object
fake_module = Fake().has_attr(__dict__={})
# Fake __import__
importer = Fake(callable=True).returns(fake_module)
# Snapshot sys.path for restore
orig_path = copy.copy(sys.path)
# Update with fake path
sys.path = sys_path
# Test for side effects
load_fabfile(path, importer=importer)
eq_(sys.path, sys_path)
# Restore
sys.path = orig_path
def test_load_fabfile_should_not_remove_real_path_elements():
for fabfile_path, sys_dot_path in (
# Directory not in path
('subdir/fabfile.py', ['not_subdir']),
('fabfile.py', ['nope']),
# Directory in path, but not at front
('subdir/fabfile.py', ['not_subdir', 'subdir']),
('fabfile.py', ['not_subdir', '']),
('fabfile.py', ['not_subdir', '', 'also_not_subdir']),
# Directory in path, and at front already
('subdir/fabfile.py', ['subdir']),
('subdir/fabfile.py', ['subdir', 'not_subdir']),
('fabfile.py', ['', 'some_dir', 'some_other_dir']),
):
yield run_load_fabfile, fabfile_path, sys_dot_path