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 |
Michael Mortensen | 7335e30 | 2021-02-23 10:42:56 -0700 | [diff] [blame] | 15 | from chromite.lib import cros_test_lib |
| 16 | |
| 17 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 18 | class BuildAllFirmwareTestCase( |
Mike Frysinger | 5d85a54 | 2023-07-06 16:05:54 -0400 | [diff] [blame] | 19 | cros_test_lib.RunCommandTempDirTestCase, api_config.ApiConfigMixin |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 20 | ): |
| 21 | """BuildAllFirmware tests.""" |
Michael Mortensen | 7335e30 | 2021-02-23 10:42:56 -0700 | [diff] [blame] | 22 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 23 | def setUp(self): |
| 24 | self.chroot_path = "/path/to/chroot" |
Michael Mortensen | 7335e30 | 2021-02-23 10:42:56 -0700 | [diff] [blame] | 25 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 26 | def _GetInput( |
| 27 | self, |
| 28 | chroot_path=None, |
| 29 | fw_location=common_pb2.PLATFORM_EC, |
| 30 | code_coverage=False, |
| 31 | ): |
| 32 | """Helper for creating input message.""" |
| 33 | proto = firmware_pb2.BuildAllFirmwareRequest( |
| 34 | firmware_location=fw_location, |
| 35 | chroot={"path": chroot_path}, |
| 36 | code_coverage=code_coverage, |
| 37 | ) |
| 38 | return proto |
Michael Mortensen | 7335e30 | 2021-02-23 10:42:56 -0700 | [diff] [blame] | 39 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 40 | def testBuildAllFirmware(self): |
Alex Klein | ab87ceb | 2023-01-24 12:00:51 -0700 | [diff] [blame] | 41 | """Test endpoint by verifying call to cros_build_lib.run.""" |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 42 | for fw_loc in common_pb2.FwLocation.values(): |
| 43 | fw_path = firmware.get_fw_loc(fw_loc) |
| 44 | if not fw_path: |
| 45 | continue |
| 46 | request = self._GetInput( |
| 47 | chroot_path=self.chroot_path, |
| 48 | fw_location=fw_loc, |
| 49 | code_coverage=True, |
| 50 | ) |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 51 | response = firmware_pb2.BuildAllFirmwareResponse() |
| 52 | # Call the method under test. |
| 53 | firmware.BuildAllFirmware(request, response, self.api_config) |
Alex Klein | ab87ceb | 2023-01-24 12:00:51 -0700 | [diff] [blame] | 54 | # Because we mock out the function, we verify that it is called as |
| 55 | # we expect it to be called. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 56 | called_function = os.path.join( |
| 57 | constants.SOURCE_ROOT, fw_path, "firmware_builder.py" |
| 58 | ) |
Mike Frysinger | 5d85a54 | 2023-07-06 16:05:54 -0400 | [diff] [blame] | 59 | self.rc.assertCommandCalled( |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 60 | [ |
| 61 | called_function, |
| 62 | "--metrics", |
| 63 | mock.ANY, |
| 64 | "--code-coverage", |
| 65 | "build", |
| 66 | ], |
| 67 | check=False, |
| 68 | ) |
Michael Mortensen | 7335e30 | 2021-02-23 10:42:56 -0700 | [diff] [blame] | 69 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 70 | def testValidateOnly(self): |
Alex Klein | ab87ceb | 2023-01-24 12:00:51 -0700 | [diff] [blame] | 71 | """Verify a validate-only call does not execute any logic.""" |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 72 | for fw_loc in common_pb2.FwLocation.values(): |
| 73 | if not firmware.get_fw_loc(fw_loc): |
| 74 | continue |
| 75 | request = self._GetInput( |
| 76 | chroot_path=self.chroot_path, |
| 77 | fw_location=fw_loc, |
| 78 | code_coverage=True, |
| 79 | ) |
| 80 | response = firmware_pb2.BuildAllFirmwareResponse() |
| 81 | firmware.BuildAllFirmware( |
| 82 | request, response, self.validate_only_config |
| 83 | ) |
Mike Frysinger | 5d85a54 | 2023-07-06 16:05:54 -0400 | [diff] [blame] | 84 | self.assertFalse(self.rc.called) |
Michael Mortensen | 515c889 | 2021-02-26 15:37:59 -0700 | [diff] [blame] | 85 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 86 | def testMockCall(self): |
Alex Klein | ab87ceb | 2023-01-24 12:00:51 -0700 | [diff] [blame] | 87 | """Test a mock call does not execute logic, returns mocked value.""" |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 88 | for fw_loc in common_pb2.FwLocation.values(): |
| 89 | if not firmware.get_fw_loc(fw_loc): |
| 90 | continue |
| 91 | request = self._GetInput( |
| 92 | chroot_path=self.chroot_path, |
| 93 | fw_location=fw_loc, |
| 94 | code_coverage=True, |
| 95 | ) |
| 96 | response = firmware_pb2.BuildAllFirmwareResponse() |
| 97 | firmware.BuildAllFirmware(request, response, self.mock_call_config) |
Mike Frysinger | 5d85a54 | 2023-07-06 16:05:54 -0400 | [diff] [blame] | 98 | self.assertFalse(self.rc.called) |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 99 | self.assertEqual(len(response.metrics.value), 1) |
| 100 | self.assertEqual(response.metrics.value[0].target_name, "foo") |
| 101 | self.assertEqual(response.metrics.value[0].platform_name, "bar") |
| 102 | self.assertEqual(len(response.metrics.value[0].fw_section), 1) |
| 103 | self.assertEqual( |
| 104 | response.metrics.value[0].fw_section[0].region, "EC_RO" |
| 105 | ) |
| 106 | self.assertEqual(response.metrics.value[0].fw_section[0].used, 100) |
| 107 | self.assertEqual(response.metrics.value[0].fw_section[0].total, 150) |