blob: c263636ecd5ff3f25fb9b733658f8f4471ff0daf [file] [log] [blame]
Mike Frysingerf1ba7ad2022-09-12 05:42:57 -04001# Copyright 2021 The ChromiumOS Authors
Michael Mortensen7335e302021-02-23 10:42:56 -07002# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
Mike Frysingera2527f82023-02-03 04:32:15 -05004
Michael Mortensen7335e302021-02-23 10:42:56 -07005"""Unittests for Firmware operations."""
6
Michael Mortensen7335e302021-02-23 10:42:56 -07007import os
Mike Frysinger166fea02021-02-12 05:30:33 -05008from unittest import mock
Michael Mortensen7335e302021-02-23 10:42:56 -07009
Michael Mortensen7335e302021-02-23 10:42:56 -070010from chromite.api import api_config
11from chromite.api.controller import firmware
12from chromite.api.gen.chromite.api import firmware_pb2
Andrew Luod08045d2021-06-17 12:06:44 -070013from chromite.api.gen.chromiumos import common_pb2
Michael Mortensen7335e302021-02-23 10:42:56 -070014from chromite.lib import constants
15from chromite.lib import cros_build_lib
16from chromite.lib import cros_test_lib
17
18
Alex Klein1699fab2022-09-08 08:46:06 -060019class BuildAllFirmwareTestCase(
20 cros_test_lib.MockTempDirTestCase, api_config.ApiConfigMixin
21):
22 """BuildAllFirmware tests."""
Michael Mortensen7335e302021-02-23 10:42:56 -070023
Alex Klein1699fab2022-09-08 08:46:06 -060024 def setUp(self):
25 self.chroot_path = "/path/to/chroot"
26 self.cros_build_run_patch = self.PatchObject(
27 cros_build_lib,
28 "run",
29 return_value=cros_build_lib.CompletedProcess(returncode=0),
30 )
Michael Mortensen7335e302021-02-23 10:42:56 -070031
Alex Klein1699fab2022-09-08 08:46:06 -060032 def _GetInput(
33 self,
34 chroot_path=None,
35 fw_location=common_pb2.PLATFORM_EC,
36 code_coverage=False,
37 ):
38 """Helper for creating input message."""
39 proto = firmware_pb2.BuildAllFirmwareRequest(
40 firmware_location=fw_location,
41 chroot={"path": chroot_path},
42 code_coverage=code_coverage,
43 )
44 return proto
Michael Mortensen7335e302021-02-23 10:42:56 -070045
Alex Klein1699fab2022-09-08 08:46:06 -060046 def testBuildAllFirmware(self):
Alex Kleinab87ceb2023-01-24 12:00:51 -070047 """Test endpoint by verifying call to cros_build_lib.run."""
Alex Klein1699fab2022-09-08 08:46:06 -060048 for fw_loc in common_pb2.FwLocation.values():
49 fw_path = firmware.get_fw_loc(fw_loc)
50 if not fw_path:
51 continue
52 request = self._GetInput(
53 chroot_path=self.chroot_path,
54 fw_location=fw_loc,
55 code_coverage=True,
56 )
Alex Klein1699fab2022-09-08 08:46:06 -060057 response = firmware_pb2.BuildAllFirmwareResponse()
58 # Call the method under test.
59 firmware.BuildAllFirmware(request, response, self.api_config)
Alex Kleinab87ceb2023-01-24 12:00:51 -070060 # Because we mock out the function, we verify that it is called as
61 # we expect it to be called.
Alex Klein1699fab2022-09-08 08:46:06 -060062 called_function = os.path.join(
63 constants.SOURCE_ROOT, fw_path, "firmware_builder.py"
64 )
65 self.cros_build_run_patch.assert_called_with(
66 [
67 called_function,
68 "--metrics",
69 mock.ANY,
70 "--code-coverage",
71 "build",
72 ],
73 check=False,
74 )
Michael Mortensen7335e302021-02-23 10:42:56 -070075
Alex Klein1699fab2022-09-08 08:46:06 -060076 def testValidateOnly(self):
Alex Kleinab87ceb2023-01-24 12:00:51 -070077 """Verify a validate-only call does not execute any logic."""
Alex Klein1699fab2022-09-08 08:46:06 -060078 for fw_loc in common_pb2.FwLocation.values():
79 if not firmware.get_fw_loc(fw_loc):
80 continue
81 request = self._GetInput(
82 chroot_path=self.chroot_path,
83 fw_location=fw_loc,
84 code_coverage=True,
85 )
86 response = firmware_pb2.BuildAllFirmwareResponse()
87 firmware.BuildAllFirmware(
88 request, response, self.validate_only_config
89 )
90 self.cros_build_run_patch.assert_not_called()
Michael Mortensen515c8892021-02-26 15:37:59 -070091
Alex Klein1699fab2022-09-08 08:46:06 -060092 def testMockCall(self):
Alex Kleinab87ceb2023-01-24 12:00:51 -070093 """Test a mock call does not execute logic, returns mocked value."""
Alex Klein1699fab2022-09-08 08:46:06 -060094 for fw_loc in common_pb2.FwLocation.values():
95 if not firmware.get_fw_loc(fw_loc):
96 continue
97 request = self._GetInput(
98 chroot_path=self.chroot_path,
99 fw_location=fw_loc,
100 code_coverage=True,
101 )
102 response = firmware_pb2.BuildAllFirmwareResponse()
103 firmware.BuildAllFirmware(request, response, self.mock_call_config)
104 self.cros_build_run_patch.assert_not_called()
105 self.assertEqual(len(response.metrics.value), 1)
106 self.assertEqual(response.metrics.value[0].target_name, "foo")
107 self.assertEqual(response.metrics.value[0].platform_name, "bar")
108 self.assertEqual(len(response.metrics.value[0].fw_section), 1)
109 self.assertEqual(
110 response.metrics.value[0].fw_section[0].region, "EC_RO"
111 )
112 self.assertEqual(response.metrics.value[0].fw_section[0].used, 100)
113 self.assertEqual(response.metrics.value[0].fw_section[0].total, 150)