blob: 4332f00361c724282ed3bca5319e7e85b8a5d595 [file] [log] [blame]
Mike Frysingerf1ba7ad2022-09-12 05:42:57 -04001# Copyright 2019 The ChromiumOS Authors
George Engelbrechtfe63c8c2019-08-31 22:51:29 -06002# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""Payload operations."""
6
George Engelbrechtfe63c8c2019-08-31 22:51:29 -06007from chromite.api import api_config
8from chromite.api import controller
9from chromite.api.controller import payload
10from chromite.api.gen.chromite.api import payload_pb2
11from chromite.api.gen.chromiumos import common_pb2
12from chromite.lib import cros_test_lib
13from chromite.lib.paygen import paygen_payload_lib
14
15
Alex Klein1699fab2022-09-08 08:46:06 -060016class PayloadApiTests(
17 cros_test_lib.MockTempDirTestCase, api_config.ApiConfigMixin
18):
19 """Unittests for PayloadApi."""
George Engelbrechtfe63c8c2019-08-31 22:51:29 -060020
Alex Klein1699fab2022-09-08 08:46:06 -060021 def setUp(self):
22 self.response = payload_pb2.GenerationResponse()
George Engelbrechtfe63c8c2019-08-31 22:51:29 -060023
Alex Klein1699fab2022-09-08 08:46:06 -060024 src_build = payload_pb2.Build(
25 version="1.0.0",
26 bucket="test",
27 channel="test-channel",
28 build_target=common_pb2.BuildTarget(name="cave"),
29 )
George Engelbrechtfe63c8c2019-08-31 22:51:29 -060030
Alex Klein1699fab2022-09-08 08:46:06 -060031 src_image = payload_pb2.UnsignedImage(
32 build=src_build, image_type=6, milestone="R70"
33 )
George Engelbrechtfe63c8c2019-08-31 22:51:29 -060034
Alex Klein1699fab2022-09-08 08:46:06 -060035 tgt_build = payload_pb2.Build(
36 version="2.0.0",
37 bucket="test",
38 channel="test-channel",
39 build_target=common_pb2.BuildTarget(name="cave"),
40 )
George Engelbrechtfe63c8c2019-08-31 22:51:29 -060041
Alex Klein1699fab2022-09-08 08:46:06 -060042 tgt_image = payload_pb2.UnsignedImage(
43 build=tgt_build, image_type=6, milestone="R70"
44 )
George Engelbrechtfe63c8c2019-08-31 22:51:29 -060045
Alex Klein1699fab2022-09-08 08:46:06 -060046 self.req = payload_pb2.GenerationRequest(
47 tgt_unsigned_image=tgt_image,
48 src_unsigned_image=src_image,
49 bucket="test-destination-bucket",
50 verify=True,
51 keyset="update_signer",
52 dryrun=False,
Brian Norris351745f2023-08-25 15:23:38 -070053 result_path=common_pb2.ResultPath(
54 path=common_pb2.Path(
55 path=str(self.tempdir / "results"),
56 location=common_pb2.Path.OUTSIDE,
57 )
58 ),
Alex Klein1699fab2022-09-08 08:46:06 -060059 )
George Engelbrechtfe63c8c2019-08-31 22:51:29 -060060
Alex Klein1699fab2022-09-08 08:46:06 -060061 self.minios_req = payload_pb2.GenerationRequest(
62 tgt_unsigned_image=tgt_image,
63 src_unsigned_image=src_image,
64 bucket="test-destination-bucket",
65 minios=True,
66 verify=True,
67 keyset="update_signer",
68 dryrun=False,
Brian Norris351745f2023-08-25 15:23:38 -070069 result_path=common_pb2.ResultPath(
70 path=common_pb2.Path(
71 path=str(self.tempdir / "results"),
72 location=common_pb2.Path.OUTSIDE,
73 )
74 ),
Alex Klein1699fab2022-09-08 08:46:06 -060075 )
Greg Edelston629468c2022-02-11 14:54:56 -070076
Alex Klein1699fab2022-09-08 08:46:06 -060077 self.result = payload_pb2.GenerationResponse(
Benjamin Shai3c1a2322023-02-10 17:29:46 +000078 versioned_artifacts=[
79 payload_pb2.GenerationResponse.VersionedArtifact(
80 version=1,
Brian Norris351745f2023-08-25 15:23:38 -070081 file_path=common_pb2.Path(
82 path="/tmp/aohiwdadoi/delta.bin",
83 location=common_pb2.Path.INSIDE,
84 ),
Benjamin Shai3c1a2322023-02-10 17:29:46 +000085 remote_uri="gs://something",
86 )
87 ]
Alex Klein1699fab2022-09-08 08:46:06 -060088 )
George Engelbrechtfe63c8c2019-08-31 22:51:29 -060089
Alex Klein1699fab2022-09-08 08:46:06 -060090 self.PatchObject(
91 payload, "_DEFAULT_PAYGEN_CACHE_DIR", new=str(self.tempdir)
92 )
Alex Kleinc07a48b2022-08-26 15:58:44 -060093
Alex Klein1699fab2022-09-08 08:46:06 -060094 def testValidateOnly(self):
95 """Basic check that a validate only call does not execute any logic."""
George Engelbrechtfe63c8c2019-08-31 22:51:29 -060096
Alex Klein1699fab2022-09-08 08:46:06 -060097 res = payload.GeneratePayload(
98 self.req, self.result, self.validate_only_config
99 )
100 self.assertEqual(res, controller.RETURN_CODE_VALID_INPUT)
George Engelbrechtfe63c8c2019-08-31 22:51:29 -0600101
Alex Klein1699fab2022-09-08 08:46:06 -0600102 def testCallSucceeds(self):
103 """Check that a call is made successfully."""
104 # Deep patch the paygen lib, this is a full run through service as well.
105 patch_obj = self.PatchObject(paygen_payload_lib, "PaygenPayload")
Jae Hoon Kime70c6032023-02-10 02:56:36 +0000106 patch_obj.return_value.Run.return_value = {
Benjamin Shai3c1a2322023-02-10 17:29:46 +0000107 1: ("/tmp/aohiwdadoi/delta.bin", "gs://something")
Jae Hoon Kime70c6032023-02-10 02:56:36 +0000108 }
Alex Klein1699fab2022-09-08 08:46:06 -0600109 res = payload.GeneratePayload(self.req, self.result, self.api_config)
110 self.assertEqual(res, controller.RETURN_CODE_SUCCESS)
Michael Mortensen85d38402019-12-12 09:50:29 -0700111
Alex Klein1699fab2022-09-08 08:46:06 -0600112 def testMockError(self):
113 """Test mock error call does not execute any logic, returns error."""
114 patch = self.PatchObject(paygen_payload_lib, "PaygenPayload")
Michael Mortensen85d38402019-12-12 09:50:29 -0700115
Alex Klein1699fab2022-09-08 08:46:06 -0600116 res = payload.GeneratePayload(
117 self.req, self.result, self.mock_error_config
118 )
119 patch.assert_not_called()
120 self.assertEqual(controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY, res)
Michael Mortensen85d38402019-12-12 09:50:29 -0700121
Alex Klein1699fab2022-09-08 08:46:06 -0600122 def testMockCall(self):
123 """Test mock call does not execute any logic, returns success."""
124 patch = self.PatchObject(paygen_payload_lib, "PaygenPayload")
Michael Mortensen85d38402019-12-12 09:50:29 -0700125
Alex Klein1699fab2022-09-08 08:46:06 -0600126 res = payload.GeneratePayload(
127 self.req, self.result, self.mock_call_config
128 )
129 patch.assert_not_called()
130 self.assertEqual(controller.RETURN_CODE_SUCCESS, res)
Greg Edelston629468c2022-02-11 14:54:56 -0700131
Alex Klein1699fab2022-09-08 08:46:06 -0600132 def testMiniOSSuccess(self):
133 """Test a miniOS paygen request."""
134 patch = self.PatchObject(paygen_payload_lib, "PaygenPayload")
Jae Hoon Kime70c6032023-02-10 02:56:36 +0000135 patch.return_value.Run.return_value = {
Benjamin Shai3c1a2322023-02-10 17:29:46 +0000136 1: ("/tmp/aohiwdadoi/delta.bin", "gs://minios/something")
Jae Hoon Kime70c6032023-02-10 02:56:36 +0000137 }
Alex Klein1699fab2022-09-08 08:46:06 -0600138 res = payload.GeneratePayload(
139 self.minios_req, self.result, self.api_config
140 )
141 self.assertEqual(res, controller.RETURN_CODE_SUCCESS)
Greg Edelston629468c2022-02-11 14:54:56 -0700142
Alex Klein1699fab2022-09-08 08:46:06 -0600143 def testNoMiniOSPartition(self):
144 """Test a miniOS paygen request on an image with no miniOS part."""
145 patch = self.PatchObject(paygen_payload_lib, "PaygenPayload")
146 patch.side_effect = paygen_payload_lib.NoMiniOSPartitionException
147 response_code = payload.GeneratePayload(
148 self.minios_req, self.result, self.api_config
149 )
150 self.assertEqual(
151 self.result.failure_reason,
152 payload_pb2.GenerationResponse.NOT_MINIOS_COMPATIBLE,
153 )
154 self.assertEqual(
155 response_code,
156 controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE,
157 )
Jack Neus2577e2d2023-04-06 16:14:01 +0000158
159 def testNoMiniOSPartitionMismatch(self):
160 """Test a miniOS paygen request with a partition count mismatch."""
161 patch = self.PatchObject(paygen_payload_lib, "PaygenPayload")
Greg Edelstonbccc22f2023-08-14 10:01:34 -0600162 patch.side_effect = paygen_payload_lib.MiniOSPartitionMismatchException
Jack Neus2577e2d2023-04-06 16:14:01 +0000163 response_code = payload.GeneratePayload(
164 self.minios_req, self.result, self.api_config
165 )
166 self.assertEqual(
167 self.result.failure_reason,
168 payload_pb2.GenerationResponse.MINIOS_COUNT_MISMATCH,
169 )
170 self.assertEqual(
171 response_code,
172 controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE,
173 )