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