blob: a40ce2f4904e382ae05a69d562f48fbbd164cb93 [file] [log] [blame]
George Engelbrechtfe63c8c2019-08-31 22:51:29 -06001# Copyright 2019 The Chromium OS Authors. All rights reserved.
2# 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(
66 success=True,
67 local_path="/tmp/aohiwdadoi/delta.bin",
68 remote_uri="gs://something",
69 )
George Engelbrechtfe63c8c2019-08-31 22:51:29 -060070
Alex Klein1699fab2022-09-08 08:46:06 -060071 self.PatchObject(
72 payload, "_DEFAULT_PAYGEN_CACHE_DIR", new=str(self.tempdir)
73 )
Alex Kleinc07a48b2022-08-26 15:58:44 -060074
Alex Klein1699fab2022-09-08 08:46:06 -060075 def testValidateOnly(self):
76 """Basic check that a validate only call does not execute any logic."""
George Engelbrechtfe63c8c2019-08-31 22:51:29 -060077
Alex Klein1699fab2022-09-08 08:46:06 -060078 res = payload.GeneratePayload(
79 self.req, self.result, self.validate_only_config
80 )
81 self.assertEqual(res, controller.RETURN_CODE_VALID_INPUT)
George Engelbrechtfe63c8c2019-08-31 22:51:29 -060082
Alex Klein1699fab2022-09-08 08:46:06 -060083 def testCallSucceeds(self):
84 """Check that a call is made successfully."""
85 # Deep patch the paygen lib, this is a full run through service as well.
86 patch_obj = self.PatchObject(paygen_payload_lib, "PaygenPayload")
87 patch_obj.return_value.Run.return_value = "gs://something"
88 res = payload.GeneratePayload(self.req, self.result, self.api_config)
89 self.assertEqual(res, controller.RETURN_CODE_SUCCESS)
Michael Mortensen85d38402019-12-12 09:50:29 -070090
Alex Klein1699fab2022-09-08 08:46:06 -060091 def testMockError(self):
92 """Test mock error call does not execute any logic, returns error."""
93 patch = self.PatchObject(paygen_payload_lib, "PaygenPayload")
Michael Mortensen85d38402019-12-12 09:50:29 -070094
Alex Klein1699fab2022-09-08 08:46:06 -060095 res = payload.GeneratePayload(
96 self.req, self.result, self.mock_error_config
97 )
98 patch.assert_not_called()
99 self.assertEqual(controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY, res)
Michael Mortensen85d38402019-12-12 09:50:29 -0700100
Alex Klein1699fab2022-09-08 08:46:06 -0600101 def testMockCall(self):
102 """Test mock call does not execute any logic, returns success."""
103 patch = self.PatchObject(paygen_payload_lib, "PaygenPayload")
Michael Mortensen85d38402019-12-12 09:50:29 -0700104
Alex Klein1699fab2022-09-08 08:46:06 -0600105 res = payload.GeneratePayload(
106 self.req, self.result, self.mock_call_config
107 )
108 patch.assert_not_called()
109 self.assertEqual(controller.RETURN_CODE_SUCCESS, res)
Greg Edelston629468c2022-02-11 14:54:56 -0700110
Alex Klein1699fab2022-09-08 08:46:06 -0600111 def testMiniOSSuccess(self):
112 """Test a miniOS paygen request."""
113 patch = self.PatchObject(paygen_payload_lib, "PaygenPayload")
114 patch.return_value.Run.return_value = "gs://minios/something"
115 res = payload.GeneratePayload(
116 self.minios_req, self.result, self.api_config
117 )
118 self.assertEqual(res, controller.RETURN_CODE_SUCCESS)
Greg Edelston629468c2022-02-11 14:54:56 -0700119
Alex Klein1699fab2022-09-08 08:46:06 -0600120 def testNoMiniOSPartition(self):
121 """Test a miniOS paygen request on an image with no miniOS part."""
122 patch = self.PatchObject(paygen_payload_lib, "PaygenPayload")
123 patch.side_effect = paygen_payload_lib.NoMiniOSPartitionException
124 response_code = payload.GeneratePayload(
125 self.minios_req, self.result, self.api_config
126 )
127 self.assertEqual(
128 self.result.failure_reason,
129 payload_pb2.GenerationResponse.NOT_MINIOS_COMPATIBLE,
130 )
131 self.assertEqual(
132 response_code,
133 controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE,
134 )