blob: a24bc761969b028d132ca7bb7d675c417a81e902 [file] [log] [blame]
Alex Klein2008aee2019-08-20 16:25:27 -06001# Copyright 2019 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""Decorators to help handle mock calls and errors in the Build API."""
6
Alex Klein2008aee2019-08-20 16:25:27 -06007import functools
8
9from chromite.api import controller
10
11
12def all_responses(faux_result_factory):
13 """A decorator to handle all mock responses.
14
15 This is syntactic sugar for handling all of the mock response types in a
16 single function.
17
18 Args:
19 faux_result_factory: A function with the same signature as a regular
20 API endpoint function that populates the output with success or error
21 results, as requested, without executing the endpoint.
22 """
23 assert faux_result_factory
24
25 # Get the decorators for each of the mock types so we can compose them.
26 success_fn = success(faux_result_factory)
27 err_fn = error(faux_result_factory)
28
29 def _decorator(func):
30 return err_fn(success_fn(func))
31
32 return _decorator
33
34
35def all_empty(func):
36 """Decorator to handle all mock responses with an empty output."""
37 return empty_error(empty_success(func))
38
39
40def success(faux_result_factory):
41 """A decorator to handle mock call responses.
42
43 Args:
44 faux_result_factory: A function with the same signature as a regular
45 API endpoint function that populates the output
46 """
47 assert faux_result_factory
48
49 def decorator(func):
50
51 @functools.wraps(func)
52 def _success(input_proto, output_proto, config, *args, **kwargs):
53 if config.mock_call:
54 faux_result_factory(input_proto, output_proto, config, *args, **kwargs)
55 return controller.RETURN_CODE_SUCCESS
56
57 return func(input_proto, output_proto, config, *args, **kwargs)
58
59 return _success
60
61 return decorator
62
63
64def empty_success(func):
65 """A decorator to handle mock success responses with empty outputs."""
66 @functools.wraps(func)
67 def _empty_success(input_proto, output_proto, config, *args, **kwargs):
68 if config.mock_call:
69 return controller.RETURN_CODE_SUCCESS
70
71 return func(input_proto, output_proto, config, *args, **kwargs)
72
73 return _empty_success
74
75
76def error(faux_error_factory):
77 """A decorator to handle mock error responses."""
78 assert faux_error_factory
79
80 def decorator(func):
81
82 @functools.wraps(func)
83 def _error(input_proto, output_proto, config, *args, **kwargs):
84 if config.mock_error:
85 faux_error_factory(input_proto, output_proto, config, *args, **kwargs)
86 return controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE
87
88 return func(input_proto, output_proto, config, *args, **kwargs)
89
90 return _error
91
92 return decorator
93
94
95def empty_error(func):
96 """A decorator to handle mock error responses with empty outputs."""
97 @functools.wraps(func)
98 def _empty_error(input_proto, output_proto, config, *args, **kwargs):
99 if config.mock_error:
100 return controller.RETURN_CODE_UNRECOVERABLE
101
102 return func(input_proto, output_proto, config, *args, **kwargs)
103
104 return _empty_error
Michael Mortensen7a860eb2019-12-03 20:25:15 -0700105
106def empty_completed_unsuccessfully_error(func):
107 """A decorator to handle mock unsuccessful response with empty outputs."""
108 @functools.wraps(func)
109 def _empty_error(input_proto, output_proto, config, *args, **kwargs):
110 if config.mock_error:
111 return controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY
112
113 return func(input_proto, output_proto, config, *args, **kwargs)
114
115 return _empty_error