File: test_decorators.py

package info (click to toggle)
fabric 1.14.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 1,240 kB
  • sloc: python: 7,363; makefile: 10
file content (293 lines) | stat: -rw-r--r-- 6,800 bytes parent folder | download | duplicates (4)
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
from __future__ import with_statement

import random
import sys

from nose.tools import eq_, ok_, assert_true, assert_false, assert_equal
import fudge
from fudge import Fake, with_fakes, patched_context

from fabric import decorators, tasks
from fabric.state import env
import fabric # for patching fabric.state.xxx
from fabric.tasks import _parallel_tasks, requires_parallel, execute
from fabric.context_managers import lcd, settings, hide

from utils import mock_streams


#
# Support
#

def fake_function(*args, **kwargs):
    """
    Returns a ``fudge.Fake`` exhibiting function-like attributes.

    Passes in all args/kwargs to the ``fudge.Fake`` constructor. However, if
    ``callable`` or ``expect_call`` kwargs are not given, ``callable`` will be
    set to True by default.
    """
    # Must define __name__ to be compatible with function wrapping mechanisms
    # like @wraps().
    if 'callable' not in kwargs and 'expect_call' not in kwargs:
        kwargs['callable'] = True
    return Fake(*args, **kwargs).has_attr(__name__='fake')



#
# @task
#

def test_task_returns_an_instance_of_wrappedfunctask_object():
    def foo():
        pass
    task = decorators.task(foo)
    ok_(isinstance(task, tasks.WrappedCallableTask))


def test_task_will_invoke_provided_class():
    def foo(): pass
    fake = Fake()
    fake.expects("__init__").with_args(foo)
    fudge.clear_calls()
    fudge.clear_expectations()

    foo = decorators.task(foo, task_class=fake)

    fudge.verify()


def test_task_passes_args_to_the_task_class():
    random_vars = ("some text", random.randint(100, 200))
    def foo(): pass

    fake = Fake()
    fake.expects("__init__").with_args(foo, *random_vars)
    fudge.clear_calls()
    fudge.clear_expectations()

    foo = decorators.task(foo, task_class=fake, *random_vars)
    fudge.verify()


def test_passes_kwargs_to_the_task_class():
    random_vars = {
        "msg": "some text",
        "number": random.randint(100, 200),
    }
    def foo(): pass

    fake = Fake()
    fake.expects("__init__").with_args(foo, **random_vars)
    fudge.clear_calls()
    fudge.clear_expectations()

    foo = decorators.task(foo, task_class=fake, **random_vars)
    fudge.verify()


def test_integration_tests_for_invoked_decorator_with_no_args():
    r = random.randint(100, 200)
    @decorators.task()
    def foo():
        return r

    eq_(r, foo())


def test_integration_tests_for_decorator():
    r = random.randint(100, 200)
    @decorators.task(task_class=tasks.WrappedCallableTask)
    def foo():
        return r

    eq_(r, foo())


def test_original_non_invoked_style_task():
    r = random.randint(100, 200)
    @decorators.task
    def foo():
        return r

    eq_(r, foo())



#
# @runs_once
#

@with_fakes
def test_runs_once_runs_only_once():
    """
    @runs_once prevents decorated func from running >1 time
    """
    func = fake_function(expect_call=True).times_called(1)
    task = decorators.runs_once(func)
    for i in range(2):
        task()


def test_runs_once_returns_same_value_each_run():
    """
    @runs_once memoizes return value of decorated func
    """
    return_value = "foo"
    task = decorators.runs_once(fake_function().returns(return_value))
    for i in range(2):
        eq_(task(), return_value)


@decorators.runs_once
def single_run():
    pass

def test_runs_once():
    assert_false(hasattr(single_run, 'return_value'))
    single_run()
    assert_true(hasattr(single_run, 'return_value'))
    assert_equal(None, single_run())



#
# @serial / @parallel
#


@decorators.serial
def serial():
    pass

@decorators.serial
@decorators.parallel
def serial2():
    pass

@decorators.parallel
@decorators.serial
def serial3():
    pass

@decorators.parallel
def parallel():
    pass

@decorators.parallel(pool_size=20)
def parallel2():
    pass

fake_tasks = {
    'serial': serial,
    'serial2': serial2,
    'serial3': serial3,
    'parallel': parallel,
    'parallel2': parallel2,
}

def parallel_task_helper(actual_tasks, expected):
    commands_to_run = map(lambda x: [x], actual_tasks)
    with patched_context(fabric.state, 'commands', fake_tasks):
        eq_(_parallel_tasks(commands_to_run), expected)

def test_parallel_tasks():
    for desc, task_names, expected in (
        ("One @serial-decorated task == no parallelism",
            ['serial'], False),
        ("One @parallel-decorated task == parallelism",
            ['parallel'], True),
        ("One @parallel-decorated and one @serial-decorated task == paralellism",
            ['parallel', 'serial'], True),
        ("Tasks decorated with both @serial and @parallel count as @parallel",
            ['serial2', 'serial3'], True)
    ):
        parallel_task_helper.description = desc
        yield parallel_task_helper, task_names, expected
        del parallel_task_helper.description

def test_parallel_wins_vs_serial():
    """
    @parallel takes precedence over @serial when both are used on one task
    """
    ok_(requires_parallel(serial2))
    ok_(requires_parallel(serial3))

@mock_streams('stdout')
def test_global_parallel_honors_runs_once():
    """
    fab -P (or env.parallel) should honor @runs_once
    """
    @decorators.runs_once
    def mytask():
        print("yolo") # 'Carpe diem' for stupid people!
    with settings(hide('everything'), parallel=True):
        execute(mytask, hosts=['localhost', '127.0.0.1'])
    result = sys.stdout.getvalue()
    eq_(result, "yolo\n")
    assert result != "yolo\nyolo\n"


#
# @roles
#

@decorators.roles('test')
def use_roles():
    pass

def test_roles():
    assert_true(hasattr(use_roles, 'roles'))
    assert_equal(use_roles.roles, ['test'])



#
# @hosts
#

@decorators.hosts('test')
def use_hosts():
    pass

def test_hosts():
    assert_true(hasattr(use_hosts, 'hosts'))
    assert_equal(use_hosts.hosts, ['test'])



#
# @with_settings
#

def test_with_settings_passes_env_vars_into_decorated_function():
    env.value = True
    random_return = random.randint(1000, 2000)
    def some_task():
        return env.value
    decorated_task = decorators.with_settings(value=random_return)(some_task)
    ok_(some_task(), msg="sanity check")
    eq_(random_return, decorated_task())

def test_with_settings_with_other_context_managers():
    """
    with_settings() should take other context managers, and use them with other
    overrided key/value pairs.
    """
    env.testval1 = "outer 1"
    prev_lcwd = env.lcwd

    def some_task():
        eq_(env.testval1, "inner 1")
        ok_(env.lcwd.endswith("here")) # Should be the side-effect of adding cd to settings

    decorated_task = decorators.with_settings(
        lcd("here"),
        testval1="inner 1"
    )(some_task)
    decorated_task()

    ok_(env.testval1, "outer 1")
    eq_(env.lcwd, prev_lcwd)