blob: 6f4c85c3261df0b2c0aae0a275164a6228c998b1 [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(
Alex Klein1699fab2022-09-08 08:46:06 -060066 local_path="/tmp/aohiwdadoi/delta.bin",
67 remote_uri="gs://something",
68 )
George Engelbrechtfe63c8c2019-08-31 22:51:29 -060069
Alex Klein1699fab2022-09-08 08:46:06 -060070 self.PatchObject(
71 payload, "_DEFAULT_PAYGEN_CACHE_DIR", new=str(self.tempdir)
72 )
Alex Kleinc07a48b2022-08-26 15:58:44 -060073
Alex Klein1699fab2022-09-08 08:46:06 -060074 def testValidateOnly(self):
75 """Basic check that a validate only call does not execute any logic."""
George Engelbrechtfe63c8c2019-08-31 22:51:29 -060076
Alex Klein1699fab2022-09-08 08:46:06 -060077 res = payload.GeneratePayload(
78 self.req, self.result, self.validate_only_config
79 )
80 self.assertEqual(res, controller.RETURN_CODE_VALID_INPUT)
George Engelbrechtfe63c8c2019-08-31 22:51:29 -060081
Alex Klein1699fab2022-09-08 08:46:06 -060082 def testCallSucceeds(self):
83 """Check that a call is made successfully."""
84 # Deep patch the paygen lib, this is a full run through service as well.
85 patch_obj = self.PatchObject(paygen_payload_lib, "PaygenPayload")
86 patch_obj.return_value.Run.return_value = "gs://something"
87 res = payload.GeneratePayload(self.req, self.result, self.api_config)
88 self.assertEqual(res, controller.RETURN_CODE_SUCCESS)
Michael Mortensen85d38402019-12-12 09:50:29 -070089
Alex Klein1699fab2022-09-08 08:46:06 -060090 def testMockError(self):
91 """Test mock error call does not execute any logic, returns error."""
92 patch = self.PatchObject(paygen_payload_lib, "PaygenPayload")
Michael Mortensen85d38402019-12-12 09:50:29 -070093
Alex Klein1699fab2022-09-08 08:46:06 -060094 res = payload.GeneratePayload(
95 self.req, self.result, self.mock_error_config
96 )
97 patch.assert_not_called()
98 self.assertEqual(controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY, res)
Michael Mortensen85d38402019-12-12 09:50:29 -070099
Alex Klein1699fab2022-09-08 08:46:06 -0600100 def testMockCall(self):
101 """Test mock call does not execute any logic, returns success."""
102 patch = self.PatchObject(paygen_payload_lib, "PaygenPayload")
Michael Mortensen85d38402019-12-12 09:50:29 -0700103
Alex Klein1699fab2022-09-08 08:46:06 -0600104 res = payload.GeneratePayload(
105 self.req, self.result, self.mock_call_config
106 )
107 patch.assert_not_called()
108 self.assertEqual(controller.RETURN_CODE_SUCCESS, res)
Greg Edelston629468c2022-02-11 14:54:56 -0700109
Alex Klein1699fab2022-09-08 08:46:06 -0600110 def testMiniOSSuccess(self):
111 """Test a miniOS paygen request."""
112 patch = self.PatchObject(paygen_payload_lib, "PaygenPayload")
113 patch.return_value.Run.return_value = "gs://minios/something"
114 res = payload.GeneratePayload(
115 self.minios_req, self.result, self.api_config
116 )
117 self.assertEqual(res, controller.RETURN_CODE_SUCCESS)
Greg Edelston629468c2022-02-11 14:54:56 -0700118
Alex Klein1699fab2022-09-08 08:46:06 -0600119 def testNoMiniOSPartition(self):
120 """Test a miniOS paygen request on an image with no miniOS part."""
121 patch = self.PatchObject(paygen_payload_lib, "PaygenPayload")
122 patch.side_effect = paygen_payload_lib.NoMiniOSPartitionException
123 response_code = payload.GeneratePayload(
124 self.minios_req, self.result, self.api_config
125 )
126 self.assertEqual(
127 self.result.failure_reason,
128 payload_pb2.GenerationResponse.NOT_MINIOS_COMPATIBLE,
129 )
130 self.assertEqual(
131 response_code,
132 controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE,
133 )