blob: 0028d503feb1c01cc46df316a6c576377ed725aa [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,
53 )
George Engelbrechtfe63c8c2019-08-31 22:51:29 -060054
Alex Klein1699fab2022-09-08 08:46:06 -060055 self.minios_req = payload_pb2.GenerationRequest(
56 tgt_unsigned_image=tgt_image,
57 src_unsigned_image=src_image,
58 bucket="test-destination-bucket",
59 minios=True,
60 verify=True,
61 keyset="update_signer",
62 dryrun=False,
63 )
Greg Edelston629468c2022-02-11 14:54:56 -070064
Alex Klein1699fab2022-09-08 08:46:06 -060065 self.result = payload_pb2.GenerationResponse(
Benjamin Shai3c1a2322023-02-10 17:29:46 +000066 versioned_artifacts=[
67 payload_pb2.GenerationResponse.VersionedArtifact(
68 version=1,
69 local_path="/tmp/aohiwdadoi/delta.bin",
70 remote_uri="gs://something",
71 )
72 ]
Alex Klein1699fab2022-09-08 08:46:06 -060073 )
George Engelbrechtfe63c8c2019-08-31 22:51:29 -060074
Alex Klein1699fab2022-09-08 08:46:06 -060075 self.PatchObject(
76 payload, "_DEFAULT_PAYGEN_CACHE_DIR", new=str(self.tempdir)
77 )
Alex Kleinc07a48b2022-08-26 15:58:44 -060078
Alex Klein1699fab2022-09-08 08:46:06 -060079 def testValidateOnly(self):
80 """Basic check that a validate only call does not execute any logic."""
George Engelbrechtfe63c8c2019-08-31 22:51:29 -060081
Alex Klein1699fab2022-09-08 08:46:06 -060082 res = payload.GeneratePayload(
83 self.req, self.result, self.validate_only_config
84 )
85 self.assertEqual(res, controller.RETURN_CODE_VALID_INPUT)
George Engelbrechtfe63c8c2019-08-31 22:51:29 -060086
Alex Klein1699fab2022-09-08 08:46:06 -060087 def testCallSucceeds(self):
88 """Check that a call is made successfully."""
89 # Deep patch the paygen lib, this is a full run through service as well.
90 patch_obj = self.PatchObject(paygen_payload_lib, "PaygenPayload")
Jae Hoon Kime70c6032023-02-10 02:56:36 +000091 patch_obj.return_value.Run.return_value = {
Benjamin Shai3c1a2322023-02-10 17:29:46 +000092 1: ("/tmp/aohiwdadoi/delta.bin", "gs://something")
Jae Hoon Kime70c6032023-02-10 02:56:36 +000093 }
Alex Klein1699fab2022-09-08 08:46:06 -060094 res = payload.GeneratePayload(self.req, self.result, self.api_config)
95 self.assertEqual(res, controller.RETURN_CODE_SUCCESS)
Michael Mortensen85d38402019-12-12 09:50:29 -070096
Alex Klein1699fab2022-09-08 08:46:06 -060097 def testMockError(self):
98 """Test mock error call does not execute any logic, returns error."""
99 patch = self.PatchObject(paygen_payload_lib, "PaygenPayload")
Michael Mortensen85d38402019-12-12 09:50:29 -0700100
Alex Klein1699fab2022-09-08 08:46:06 -0600101 res = payload.GeneratePayload(
102 self.req, self.result, self.mock_error_config
103 )
104 patch.assert_not_called()
105 self.assertEqual(controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY, res)
Michael Mortensen85d38402019-12-12 09:50:29 -0700106
Alex Klein1699fab2022-09-08 08:46:06 -0600107 def testMockCall(self):
108 """Test mock call does not execute any logic, returns success."""
109 patch = self.PatchObject(paygen_payload_lib, "PaygenPayload")
Michael Mortensen85d38402019-12-12 09:50:29 -0700110
Alex Klein1699fab2022-09-08 08:46:06 -0600111 res = payload.GeneratePayload(
112 self.req, self.result, self.mock_call_config
113 )
114 patch.assert_not_called()
115 self.assertEqual(controller.RETURN_CODE_SUCCESS, res)
Greg Edelston629468c2022-02-11 14:54:56 -0700116
Alex Klein1699fab2022-09-08 08:46:06 -0600117 def testMiniOSSuccess(self):
118 """Test a miniOS paygen request."""
119 patch = self.PatchObject(paygen_payload_lib, "PaygenPayload")
Jae Hoon Kime70c6032023-02-10 02:56:36 +0000120 patch.return_value.Run.return_value = {
Benjamin Shai3c1a2322023-02-10 17:29:46 +0000121 1: ("/tmp/aohiwdadoi/delta.bin", "gs://minios/something")
Jae Hoon Kime70c6032023-02-10 02:56:36 +0000122 }
Alex Klein1699fab2022-09-08 08:46:06 -0600123 res = payload.GeneratePayload(
124 self.minios_req, self.result, self.api_config
125 )
126 self.assertEqual(res, controller.RETURN_CODE_SUCCESS)
Greg Edelston629468c2022-02-11 14:54:56 -0700127
Alex Klein1699fab2022-09-08 08:46:06 -0600128 def testNoMiniOSPartition(self):
129 """Test a miniOS paygen request on an image with no miniOS part."""
130 patch = self.PatchObject(paygen_payload_lib, "PaygenPayload")
131 patch.side_effect = paygen_payload_lib.NoMiniOSPartitionException
132 response_code = payload.GeneratePayload(
133 self.minios_req, self.result, self.api_config
134 )
135 self.assertEqual(
136 self.result.failure_reason,
137 payload_pb2.GenerationResponse.NOT_MINIOS_COMPATIBLE,
138 )
139 self.assertEqual(
140 response_code,
141 controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE,
142 )