Mike Frysinger | f1ba7ad | 2022-09-12 05:42:57 -0400 | [diff] [blame] | 1 | # Copyright 2019 The ChromiumOS Authors |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 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 Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 7 | import functools |
| 8 | |
| 9 | from chromite.api import controller |
| 10 | |
| 11 | |
| 12 | def all_responses(faux_result_factory): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 13 | """A decorator to handle all mock responses. |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 14 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 15 | This is syntactic sugar for handling all of the mock response types in a |
| 16 | single function. |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 17 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 18 | Args: |
Alex Klein | a044268 | 2022-10-10 13:47:38 -0600 | [diff] [blame^] | 19 | faux_result_factory: A function with the same signature as a regular |
| 20 | API endpoint function that populates the output with success or |
| 21 | error results, as requested, without executing the endpoint. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 22 | """ |
| 23 | assert faux_result_factory |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 24 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 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) |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 28 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 29 | def _decorator(func): |
| 30 | return err_fn(success_fn(func)) |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 31 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 32 | return _decorator |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 33 | |
| 34 | |
| 35 | def all_empty(func): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 36 | """Decorator to handle all mock responses with an empty output.""" |
| 37 | return empty_error(empty_success(func)) |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 38 | |
| 39 | |
| 40 | def success(faux_result_factory): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 41 | """A decorator to handle mock call responses. |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 42 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 43 | Args: |
Alex Klein | a044268 | 2022-10-10 13:47:38 -0600 | [diff] [blame^] | 44 | faux_result_factory: A function with the same signature as a regular |
| 45 | API endpoint function that populates the output |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 46 | """ |
| 47 | assert faux_result_factory |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 48 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 49 | def decorator(func): |
| 50 | @functools.wraps(func) |
| 51 | def _success(input_proto, output_proto, config, *args, **kwargs): |
| 52 | if config.mock_call: |
| 53 | faux_result_factory( |
| 54 | input_proto, output_proto, config, *args, **kwargs |
| 55 | ) |
| 56 | return controller.RETURN_CODE_SUCCESS |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 57 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 58 | return func(input_proto, output_proto, config, *args, **kwargs) |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 59 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 60 | return _success |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 61 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 62 | return decorator |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 63 | |
| 64 | |
| 65 | def empty_success(func): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 66 | """A decorator to handle mock success responses with empty outputs.""" |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 67 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 68 | @functools.wraps(func) |
| 69 | def _empty_success(input_proto, output_proto, config, *args, **kwargs): |
| 70 | if config.mock_call: |
| 71 | return controller.RETURN_CODE_SUCCESS |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 72 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 73 | return func(input_proto, output_proto, config, *args, **kwargs) |
| 74 | |
| 75 | return _empty_success |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 76 | |
| 77 | |
| 78 | def error(faux_error_factory): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 79 | """A decorator to handle mock error responses.""" |
| 80 | assert faux_error_factory |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 81 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 82 | def decorator(func): |
| 83 | @functools.wraps(func) |
| 84 | def _error(input_proto, output_proto, config, *args, **kwargs): |
| 85 | if config.mock_error: |
| 86 | faux_error_factory( |
| 87 | input_proto, output_proto, config, *args, **kwargs |
| 88 | ) |
| 89 | return controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 90 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 91 | return func(input_proto, output_proto, config, *args, **kwargs) |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 92 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 93 | return _error |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 94 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 95 | return decorator |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 96 | |
| 97 | |
| 98 | def empty_error(func): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 99 | """A decorator to handle mock error responses with empty outputs.""" |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 100 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 101 | @functools.wraps(func) |
| 102 | def _empty_error(input_proto, output_proto, config, *args, **kwargs): |
| 103 | if config.mock_error: |
| 104 | return controller.RETURN_CODE_UNRECOVERABLE |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 105 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 106 | return func(input_proto, output_proto, config, *args, **kwargs) |
| 107 | |
| 108 | return _empty_error |
| 109 | |
Michael Mortensen | 7a860eb | 2019-12-03 20:25:15 -0700 | [diff] [blame] | 110 | |
| 111 | def empty_completed_unsuccessfully_error(func): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 112 | """A decorator to handle mock unsuccessful response with empty outputs.""" |
Michael Mortensen | 7a860eb | 2019-12-03 20:25:15 -0700 | [diff] [blame] | 113 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 114 | @functools.wraps(func) |
| 115 | def _empty_error(input_proto, output_proto, config, *args, **kwargs): |
| 116 | if config.mock_error: |
| 117 | return controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY |
Michael Mortensen | 7a860eb | 2019-12-03 20:25:15 -0700 | [diff] [blame] | 118 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 119 | return func(input_proto, output_proto, config, *args, **kwargs) |
| 120 | |
| 121 | return _empty_error |