~ubuntuone-hackers/conn-check/trunk

« back to all changes in this revision

Viewing changes to conn_check/check_impl.py

[r=wesmason] Add firewall rule yaml output options

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
 
13
13
 
14
14
def maybeDeferred(f, *args, **kwargs):
15
 
    """Wrapper to Twisted's maybeDeferred so we can add a reference to the
16
 
    wrapped deferred instance inside the original function dict."""
 
15
    """Wrapper to Twisted's maybeDeferred.
 
16
 
 
17
    Adds a reference to the wrapped deferred instance inside the original
 
18
    function dict so we can track if something is a deferred.
 
19
    """
17
20
    deferred = _maybeDeferred(f, *args, **kwargs)
18
21
    f.func_dict['deferred'] = deferred
19
22
    return deferred
20
23
 
21
24
 
22
25
def maybeDeferToThread(f, *args, **kwargs):
23
 
    """
24
 
    Call the function C{f} using a thread from the given threadpool and return
25
 
    the result as a Deferred.
 
26
    """Call the function C{f} using a thread from the given threadpool
 
27
 
 
28
    Return sthe result as a Deferred.
26
29
 
27
30
    @param f: The function to call. May return a deferred.
28
31
    @param *args: positional arguments to pass to f.
64
67
        results.notify_start, then either results.notify_success or
65
68
        results.notify_failure.
66
69
        """
67
 
        raise NotImplementedError("%r.check not implemented" % type(self))
 
70
        raise NotImplementedError("{}.check not implemented".format(
 
71
                                  type(self)))
68
72
 
69
73
    def skip(self, pattern, results):
70
74
        """Indicate that this check has been skipped.
72
76
        If the pattern matches and this is a leaf node in the check tree,
73
77
        implementations of Check.skip should call results.notify_skip.
74
78
        """
75
 
        raise NotImplementedError("%r.skip not implemented" % type(self))
 
79
        raise NotImplementedError("{}.skip not implemented".format(
 
80
                                  type(self)))
76
81
 
77
82
 
78
83
class ConditionalCheck(Check):
123
128
 
124
129
    def make_name(self, name):
125
130
        """Make a name by prepending the prefix."""
126
 
        return "%s%s" % (self.prefix, name)
 
131
        return "{}{}".format(self.prefix, name)
127
132
 
128
133
    def notify_skip(self, name):
129
134
        """Register a check being skipped."""
140
145
    def notify_failure(self, name, info, exc_info, duration):
141
146
        """Register failure."""
142
147
        self.wrapped.notify_failure(self.make_name(name),
143
 
                                      info, exc_info, duration)
 
148
                                    info, exc_info, duration)
144
149
 
145
150
 
146
151
class FailureCountingResultWrapper(ResultTracker):
261
266
 
262
267
 
263
268
@inlineCallbacks
 
269
def skipping_strategy(subchecks, pattern, results):
 
270
    """Strategy used to print checks out by just skipping them all."""
 
271
    # We need at least one yield to make this a generator
 
272
    yield
 
273
    for subcheck in subchecks:
 
274
        subcheck.skip(pattern, results)
 
275
 
 
276
 
 
277
@inlineCallbacks
264
278
def sequential_strategy(subchecks, pattern, results):
265
279
    """Run subchecks sequentially, skipping checks after the first failure.
266
280
 
295
309
    return DeferredList(deferreds)
296
310
 
297
311
 
 
312
def skipping_check(subchecks):
 
313
    """Return a check that skips everything, used for printing checks."""
 
314
    return MultiCheck(subchecks=subchecks, strategy=skipping_strategy)
 
315
 
 
316
 
298
317
def parallel_check(subchecks):
299
318
    """Return a check that runs the given subchecks in parallel."""
300
319
    return MultiCheck(subchecks=subchecks, strategy=parallel_strategy)
319
338
    args = list(args)
320
339
    check = args.pop(-1)
321
340
    path = ".".join(args)
322
 
    return PrefixCheckWrapper(wrapped=check, prefix="%s:" % (path,))
 
341
    return PrefixCheckWrapper(wrapped=check, prefix="{}:".format(path))
323
342
 
324
343
 
325
344
def make_check(name, check, info=None, blocking=False):
330
349
def guard_check(check, predicate):
331
350
    """Wrap a check so that it is skipped unless the predicate is true."""
332
351
    return ConditionalCheck(wrapped=check, predicate=predicate)
333