blob: 3bae1ba8f34d6ba556aff4919b525d2d2c7c9474 [file] [log] [blame]
Mike Frysingerf1ba7ad2022-09-12 05:42:57 -04001# Copyright 2019 The ChromiumOS Authors
Alex Klein2008aee2019-08-20 16:25:27 -06002# 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):
Alex Klein1699fab2022-09-08 08:46:06 -060013 """A decorator to handle all mock responses.
Alex Klein2008aee2019-08-20 16:25:27 -060014
Alex Klein1699fab2022-09-08 08:46:06 -060015 This is syntactic sugar for handling all of the mock response types in a
16 single function.
Alex Klein2008aee2019-08-20 16:25:27 -060017
Alex Klein1699fab2022-09-08 08:46:06 -060018 Args:
Alex Kleina0442682022-10-10 13:47:38 -060019 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 Klein1699fab2022-09-08 08:46:06 -060022 """
23 assert faux_result_factory
Alex Klein2008aee2019-08-20 16:25:27 -060024
Alex Klein1699fab2022-09-08 08:46:06 -060025 # 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 Klein2008aee2019-08-20 16:25:27 -060028
Alex Klein1699fab2022-09-08 08:46:06 -060029 def _decorator(func):
30 return err_fn(success_fn(func))
Alex Klein2008aee2019-08-20 16:25:27 -060031
Alex Klein1699fab2022-09-08 08:46:06 -060032 return _decorator
Alex Klein2008aee2019-08-20 16:25:27 -060033
34
35def all_empty(func):
Alex Klein1699fab2022-09-08 08:46:06 -060036 """Decorator to handle all mock responses with an empty output."""
37 return empty_error(empty_success(func))
Alex Klein2008aee2019-08-20 16:25:27 -060038
39
40def success(faux_result_factory):
Alex Klein1699fab2022-09-08 08:46:06 -060041 """A decorator to handle mock call responses.
Alex Klein2008aee2019-08-20 16:25:27 -060042
Alex Klein1699fab2022-09-08 08:46:06 -060043 Args:
Alex Kleina0442682022-10-10 13:47:38 -060044 faux_result_factory: A function with the same signature as a regular
45 API endpoint function that populates the output
Alex Klein1699fab2022-09-08 08:46:06 -060046 """
47 assert faux_result_factory
Alex Klein2008aee2019-08-20 16:25:27 -060048
Alex Klein1699fab2022-09-08 08:46:06 -060049 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 Klein2008aee2019-08-20 16:25:27 -060057
Alex Klein1699fab2022-09-08 08:46:06 -060058 return func(input_proto, output_proto, config, *args, **kwargs)
Alex Klein2008aee2019-08-20 16:25:27 -060059
Alex Klein1699fab2022-09-08 08:46:06 -060060 return _success
Alex Klein2008aee2019-08-20 16:25:27 -060061
Alex Klein1699fab2022-09-08 08:46:06 -060062 return decorator
Alex Klein2008aee2019-08-20 16:25:27 -060063
64
65def empty_success(func):
Alex Klein1699fab2022-09-08 08:46:06 -060066 """A decorator to handle mock success responses with empty outputs."""
Alex Klein2008aee2019-08-20 16:25:27 -060067
Alex Klein1699fab2022-09-08 08:46:06 -060068 @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 Klein2008aee2019-08-20 16:25:27 -060072
Alex Klein1699fab2022-09-08 08:46:06 -060073 return func(input_proto, output_proto, config, *args, **kwargs)
74
75 return _empty_success
Alex Klein2008aee2019-08-20 16:25:27 -060076
77
78def error(faux_error_factory):
Alex Klein1699fab2022-09-08 08:46:06 -060079 """A decorator to handle mock error responses."""
80 assert faux_error_factory
Alex Klein2008aee2019-08-20 16:25:27 -060081
Alex Klein1699fab2022-09-08 08:46:06 -060082 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 Klein2008aee2019-08-20 16:25:27 -060090
Alex Klein1699fab2022-09-08 08:46:06 -060091 return func(input_proto, output_proto, config, *args, **kwargs)
Alex Klein2008aee2019-08-20 16:25:27 -060092
Alex Klein1699fab2022-09-08 08:46:06 -060093 return _error
Alex Klein2008aee2019-08-20 16:25:27 -060094
Alex Klein1699fab2022-09-08 08:46:06 -060095 return decorator
Alex Klein2008aee2019-08-20 16:25:27 -060096
97
98def empty_error(func):
Alex Klein1699fab2022-09-08 08:46:06 -060099 """A decorator to handle mock error responses with empty outputs."""
Alex Klein2008aee2019-08-20 16:25:27 -0600100
Alex Klein1699fab2022-09-08 08:46:06 -0600101 @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 Klein2008aee2019-08-20 16:25:27 -0600105
Alex Klein1699fab2022-09-08 08:46:06 -0600106 return func(input_proto, output_proto, config, *args, **kwargs)
107
108 return _empty_error
109
Michael Mortensen7a860eb2019-12-03 20:25:15 -0700110
111def empty_completed_unsuccessfully_error(func):
Alex Klein1699fab2022-09-08 08:46:06 -0600112 """A decorator to handle mock unsuccessful response with empty outputs."""
Michael Mortensen7a860eb2019-12-03 20:25:15 -0700113
Alex Klein1699fab2022-09-08 08:46:06 -0600114 @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 Mortensen7a860eb2019-12-03 20:25:15 -0700118
Alex Klein1699fab2022-09-08 08:46:06 -0600119 return func(input_proto, output_proto, config, *args, **kwargs)
120
121 return _empty_error