Mike Frysinger | f1ba7ad | 2022-09-12 05:42:57 -0400 | [diff] [blame] | 1 | # Copyright 2021 The ChromiumOS Authors |
Michael Mortensen | 7335e30 | 2021-02-23 10:42:56 -0700 | [diff] [blame] | 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
Mike Frysinger | a2527f8 | 2023-02-03 04:32:15 -0500 | [diff] [blame] | 4 | |
Michael Mortensen | 7335e30 | 2021-02-23 10:42:56 -0700 | [diff] [blame] | 5 | """Unittests for Firmware operations.""" |
| 6 | |
Michael Mortensen | 7335e30 | 2021-02-23 10:42:56 -0700 | [diff] [blame] | 7 | import os |
Mike Frysinger | 166fea0 | 2021-02-12 05:30:33 -0500 | [diff] [blame] | 8 | from unittest import mock |
Michael Mortensen | 7335e30 | 2021-02-23 10:42:56 -0700 | [diff] [blame] | 9 | |
Michael Mortensen | 7335e30 | 2021-02-23 10:42:56 -0700 | [diff] [blame] | 10 | from chromite.api import api_config |
| 11 | from chromite.api.controller import firmware |
| 12 | from chromite.api.gen.chromite.api import firmware_pb2 |
Andrew Luo | d08045d | 2021-06-17 12:06:44 -0700 | [diff] [blame] | 13 | from chromite.api.gen.chromiumos import common_pb2 |
Michael Mortensen | 7335e30 | 2021-02-23 10:42:56 -0700 | [diff] [blame] | 14 | from chromite.lib import constants |
| 15 | from chromite.lib import cros_build_lib |
| 16 | from chromite.lib import cros_test_lib |
| 17 | |
| 18 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 19 | class BuildAllFirmwareTestCase( |
| 20 | cros_test_lib.MockTempDirTestCase, api_config.ApiConfigMixin |
| 21 | ): |
| 22 | """BuildAllFirmware tests.""" |
Michael Mortensen | 7335e30 | 2021-02-23 10:42:56 -0700 | [diff] [blame] | 23 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 24 | 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 Mortensen | 7335e30 | 2021-02-23 10:42:56 -0700 | [diff] [blame] | 31 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 32 | 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 Mortensen | 7335e30 | 2021-02-23 10:42:56 -0700 | [diff] [blame] | 45 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 46 | def testBuildAllFirmware(self): |
Alex Klein | ab87ceb | 2023-01-24 12:00:51 -0700 | [diff] [blame] | 47 | """Test endpoint by verifying call to cros_build_lib.run.""" |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 48 | 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 Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 57 | response = firmware_pb2.BuildAllFirmwareResponse() |
| 58 | # Call the method under test. |
| 59 | firmware.BuildAllFirmware(request, response, self.api_config) |
Alex Klein | ab87ceb | 2023-01-24 12:00:51 -0700 | [diff] [blame] | 60 | # Because we mock out the function, we verify that it is called as |
| 61 | # we expect it to be called. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 62 | 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 Mortensen | 7335e30 | 2021-02-23 10:42:56 -0700 | [diff] [blame] | 75 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 76 | def testValidateOnly(self): |
Alex Klein | ab87ceb | 2023-01-24 12:00:51 -0700 | [diff] [blame] | 77 | """Verify a validate-only call does not execute any logic.""" |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 78 | 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 Mortensen | 515c889 | 2021-02-26 15:37:59 -0700 | [diff] [blame] | 91 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 92 | def testMockCall(self): |
Alex Klein | ab87ceb | 2023-01-24 12:00:51 -0700 | [diff] [blame] | 93 | """Test a mock call does not execute logic, returns mocked value.""" |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 94 | 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) |