Mike Frysinger | f1ba7ad | 2022-09-12 05:42:57 -0400 | [diff] [blame] | 1 | # Copyright 2019 The ChromiumOS Authors |
Alex Klein | 69339cc | 2019-07-22 14:08:35 -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 | |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 5 | """API config object and related helper functionality.""" |
Alex Klein | 69339cc | 2019-07-22 14:08:35 -0600 | [diff] [blame] | 6 | |
Alex Klein | d815ca6 | 2020-01-10 12:21:30 -0700 | [diff] [blame] | 7 | from chromite.api.gen.chromite.api import build_api_config_pb2 |
| 8 | |
| 9 | |
| 10 | class Error(Exception): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 11 | """Base error class for the module.""" |
Alex Klein | d815ca6 | 2020-01-10 12:21:30 -0700 | [diff] [blame] | 12 | |
| 13 | |
| 14 | class UnknownCallTypeEnumValue(Error): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 15 | """Thrown when the call type enum value in proto is not configured here.""" |
Alex Klein | d815ca6 | 2020-01-10 12:21:30 -0700 | [diff] [blame] | 16 | |
Alex Klein | 69339cc | 2019-07-22 14:08:35 -0600 | [diff] [blame] | 17 | |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 18 | class ApiConfig(object): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 19 | """API Config class.""" |
Alex Klein | 69339cc | 2019-07-22 14:08:35 -0600 | [diff] [blame] | 20 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 21 | # Call type constants. |
| 22 | CALL_TYPE_EXECUTE = 1 |
| 23 | CALL_TYPE_VALIDATE_ONLY = 2 |
| 24 | CALL_TYPE_MOCK_SUCCESS = 3 |
| 25 | CALL_TYPE_MOCK_FAILURE = 4 |
| 26 | CALL_TYPE_MOCK_INVALID = 5 |
Alex Klein | d815ca6 | 2020-01-10 12:21:30 -0700 | [diff] [blame] | 27 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 28 | # Maps the proto enum to the type constants. |
| 29 | TYPE_ENUM_MAP = { |
| 30 | build_api_config_pb2.CALL_TYPE_NONE: CALL_TYPE_EXECUTE, |
| 31 | build_api_config_pb2.CALL_TYPE_EXECUTE: CALL_TYPE_EXECUTE, |
| 32 | build_api_config_pb2.CALL_TYPE_VALIDATE_ONLY: CALL_TYPE_VALIDATE_ONLY, |
| 33 | build_api_config_pb2.CALL_TYPE_MOCK_SUCCESS: CALL_TYPE_MOCK_SUCCESS, |
| 34 | build_api_config_pb2.CALL_TYPE_MOCK_FAILURE: CALL_TYPE_MOCK_FAILURE, |
| 35 | build_api_config_pb2.CALL_TYPE_MOCK_INVALID: CALL_TYPE_MOCK_INVALID, |
| 36 | } |
Alex Klein | d815ca6 | 2020-01-10 12:21:30 -0700 | [diff] [blame] | 37 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 38 | # Maps the type constants to the proto enums. |
| 39 | ENUM_TYPE_MAP = { |
| 40 | CALL_TYPE_EXECUTE: build_api_config_pb2.CALL_TYPE_EXECUTE, |
| 41 | CALL_TYPE_VALIDATE_ONLY: build_api_config_pb2.CALL_TYPE_VALIDATE_ONLY, |
| 42 | CALL_TYPE_MOCK_SUCCESS: build_api_config_pb2.CALL_TYPE_MOCK_SUCCESS, |
| 43 | CALL_TYPE_MOCK_FAILURE: build_api_config_pb2.CALL_TYPE_MOCK_FAILURE, |
| 44 | CALL_TYPE_MOCK_INVALID: build_api_config_pb2.CALL_TYPE_MOCK_INVALID, |
| 45 | } |
Alex Klein | d815ca6 | 2020-01-10 12:21:30 -0700 | [diff] [blame] | 46 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 47 | # The valid call types. |
| 48 | _VALID_CALL_TYPES = tuple(ENUM_TYPE_MAP.keys()) |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 49 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 50 | def __init__(self, call_type=CALL_TYPE_EXECUTE, log_path=None): |
| 51 | assert call_type in self._VALID_CALL_TYPES |
| 52 | self._call_type = call_type |
| 53 | # Explicit `or None` to simplify proto default empty string. |
| 54 | self.log_path = log_path or None |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 55 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 56 | def __eq__(self, other): |
| 57 | if self.__class__ is other.__class__: |
| 58 | return self.__dict__ == other.__dict__ |
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 NotImplemented |
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 | __hash__ = NotImplemented |
Alex Klein | d815ca6 | 2020-01-10 12:21:30 -0700 | [diff] [blame] | 63 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 64 | @property |
| 65 | def do_validation(self): |
| 66 | # We skip validation for all mock calls, so do validation when it's |
| 67 | # anything but a mocked call. |
| 68 | return not (self.mock_call or self.mock_error or self.mock_invalid) |
Alex Klein | d815ca6 | 2020-01-10 12:21:30 -0700 | [diff] [blame] | 69 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 70 | @property |
| 71 | def validate_only(self): |
| 72 | return self._call_type == self.CALL_TYPE_VALIDATE_ONLY |
Alex Klein | d815ca6 | 2020-01-10 12:21:30 -0700 | [diff] [blame] | 73 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 74 | @property |
| 75 | def mock_call(self): |
| 76 | return self._call_type == self.CALL_TYPE_MOCK_SUCCESS |
Alex Klein | d815ca6 | 2020-01-10 12:21:30 -0700 | [diff] [blame] | 77 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 78 | @property |
| 79 | def mock_error(self): |
| 80 | return self._call_type == self.CALL_TYPE_MOCK_FAILURE |
Alex Klein | d815ca6 | 2020-01-10 12:21:30 -0700 | [diff] [blame] | 81 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 82 | @property |
| 83 | def mock_invalid(self): |
| 84 | return self._call_type == self.CALL_TYPE_MOCK_INVALID |
Alex Klein | d1e9e5c | 2020-12-14 12:32:32 -0700 | [diff] [blame] | 85 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 86 | @property |
| 87 | def run_endpoint(self) -> bool: |
| 88 | """Run the endpoint when none of the special calls are invoked.""" |
| 89 | return ( |
| 90 | not self.validate_only |
| 91 | and not self.mock_call |
| 92 | and not self.mock_error |
| 93 | and not self.mock_invalid |
| 94 | ) |
Alex Klein | d815ca6 | 2020-01-10 12:21:30 -0700 | [diff] [blame] | 95 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 96 | def get_proto( |
| 97 | self, for_inside_execution: bool = True |
| 98 | ) -> build_api_config_pb2.BuildApiConfig: |
| 99 | """Get the config as a proto. |
Alex Klein | d815ca6 | 2020-01-10 12:21:30 -0700 | [diff] [blame] | 100 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 101 | Args: |
| 102 | for_inside_execution: Allows avoiding propagating configs that are |
| 103 | irrelevant for the build api process executed inside the chroot. |
| 104 | Enabled by default. |
| 105 | """ |
| 106 | config = build_api_config_pb2.BuildApiConfig() |
| 107 | config.call_type = self.ENUM_TYPE_MAP[self._call_type] |
Alex Klein | d815ca6 | 2020-01-10 12:21:30 -0700 | [diff] [blame] | 108 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 109 | if not for_inside_execution: |
| 110 | # Add values not needed when reexecuting. |
| 111 | config.log_path = self.log_path |
| 112 | |
| 113 | return config |
Alex Klein | d815ca6 | 2020-01-10 12:21:30 -0700 | [diff] [blame] | 114 | |
| 115 | |
Miriam Polzer | aab64a9 | 2021-08-17 11:26:37 +0200 | [diff] [blame] | 116 | def build_config_from_proto( |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 117 | config_proto: build_api_config_pb2.BuildApiConfig, |
| 118 | ) -> ApiConfig: |
| 119 | """Build an ApiConfig instance from a BuildApiConfig message. |
Alex Klein | d815ca6 | 2020-01-10 12:21:30 -0700 | [diff] [blame] | 120 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 121 | Args: |
| 122 | config_proto: The proto config. |
| 123 | """ |
Alex Klein | d815ca6 | 2020-01-10 12:21:30 -0700 | [diff] [blame] | 124 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 125 | if config_proto.call_type not in ApiConfig.TYPE_ENUM_MAP: |
| 126 | raise UnknownCallTypeEnumValue( |
| 127 | "The given protobuf call_type value is not " |
| 128 | "configured in api_config." |
| 129 | ) |
| 130 | return ApiConfig( |
| 131 | call_type=ApiConfig.TYPE_ENUM_MAP[config_proto.call_type], |
| 132 | log_path=config_proto.log_path, |
| 133 | ) |
Alex Klein | 69339cc | 2019-07-22 14:08:35 -0600 | [diff] [blame] | 134 | |
| 135 | |
| 136 | class ApiConfigMixin(object): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 137 | """Mixin to add an API Config factory properties. |
Alex Klein | 69339cc | 2019-07-22 14:08:35 -0600 | [diff] [blame] | 138 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 139 | This is meant to be used for tests to make these configs more uniform across |
| 140 | all the tests since there's very little to change anyway. |
| 141 | """ |
Alex Klein | 69339cc | 2019-07-22 14:08:35 -0600 | [diff] [blame] | 142 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 143 | @property |
| 144 | def api_config(self): |
| 145 | return ApiConfig() |
Alex Klein | 69339cc | 2019-07-22 14:08:35 -0600 | [diff] [blame] | 146 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 147 | @property |
| 148 | def validate_only_config(self): |
| 149 | return ApiConfig(call_type=ApiConfig.CALL_TYPE_VALIDATE_ONLY) |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 150 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 151 | @property |
| 152 | def no_validate_config(self): |
| 153 | return self.mock_call_config |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 154 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 155 | @property |
| 156 | def mock_call_config(self): |
| 157 | return ApiConfig(call_type=ApiConfig.CALL_TYPE_MOCK_SUCCESS) |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 158 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 159 | @property |
| 160 | def mock_error_config(self): |
| 161 | return ApiConfig(call_type=ApiConfig.CALL_TYPE_MOCK_FAILURE) |