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. |
| 4 | """Unittests for Firmware operations.""" |
| 5 | |
Michael Mortensen | 7335e30 | 2021-02-23 10:42:56 -0700 | [diff] [blame] | 6 | import os |
Mike Frysinger | 166fea0 | 2021-02-12 05:30:33 -0500 | [diff] [blame] | 7 | from unittest import mock |
Michael Mortensen | 7335e30 | 2021-02-23 10:42:56 -0700 | [diff] [blame] | 8 | |
Mike Frysinger | 2c02406 | 2021-05-22 15:43:22 -0400 | [diff] [blame] | 9 | from chromite.third_party.google.protobuf import json_format |
Michael Mortensen | 7335e30 | 2021-02-23 10:42:56 -0700 | [diff] [blame] | 10 | |
| 11 | from chromite.api import api_config |
| 12 | from chromite.api.controller import firmware |
| 13 | from chromite.api.gen.chromite.api import firmware_pb2 |
Andrew Luo | d08045d | 2021-06-17 12:06:44 -0700 | [diff] [blame] | 14 | from chromite.api.gen.chromiumos import common_pb2 |
Michael Mortensen | 7335e30 | 2021-02-23 10:42:56 -0700 | [diff] [blame] | 15 | from chromite.lib import constants |
| 16 | from chromite.lib import cros_build_lib |
| 17 | from chromite.lib import cros_test_lib |
| 18 | |
| 19 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 20 | class BuildAllFirmwareTestCase( |
| 21 | cros_test_lib.MockTempDirTestCase, api_config.ApiConfigMixin |
| 22 | ): |
| 23 | """BuildAllFirmware tests.""" |
Michael Mortensen | 7335e30 | 2021-02-23 10:42:56 -0700 | [diff] [blame] | 24 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 25 | def setUp(self): |
| 26 | self.chroot_path = "/path/to/chroot" |
| 27 | self.cros_build_run_patch = self.PatchObject( |
| 28 | cros_build_lib, |
| 29 | "run", |
| 30 | return_value=cros_build_lib.CompletedProcess(returncode=0), |
| 31 | ) |
Michael Mortensen | 7335e30 | 2021-02-23 10:42:56 -0700 | [diff] [blame] | 32 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 33 | def _GetInput( |
| 34 | self, |
| 35 | chroot_path=None, |
| 36 | fw_location=common_pb2.PLATFORM_EC, |
| 37 | code_coverage=False, |
| 38 | ): |
| 39 | """Helper for creating input message.""" |
| 40 | proto = firmware_pb2.BuildAllFirmwareRequest( |
| 41 | firmware_location=fw_location, |
| 42 | chroot={"path": chroot_path}, |
| 43 | code_coverage=code_coverage, |
| 44 | ) |
| 45 | return proto |
Michael Mortensen | 7335e30 | 2021-02-23 10:42:56 -0700 | [diff] [blame] | 46 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 47 | def testBuildAllFirmware(self): |
Alex Klein | ab87ceb | 2023-01-24 12:00:51 -0700 | [diff] [blame] | 48 | """Test endpoint by verifying call to cros_build_lib.run.""" |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 49 | for fw_loc in common_pb2.FwLocation.values(): |
| 50 | fw_path = firmware.get_fw_loc(fw_loc) |
| 51 | if not fw_path: |
| 52 | continue |
| 53 | request = self._GetInput( |
| 54 | chroot_path=self.chroot_path, |
| 55 | fw_location=fw_loc, |
| 56 | code_coverage=True, |
| 57 | ) |
| 58 | # TODO(mmortensen): Consider refactoring firmware._call_entry code |
Alex Klein | ab87ceb | 2023-01-24 12:00:51 -0700 | [diff] [blame] | 59 | # (putting the parsing of the output file in a function) so that we |
| 60 | # don't have to mock something as generic as 'json_format.Parse' to |
| 61 | # avoid an error on parsing an empty(due to mock call) file. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 62 | json_format_patch = self.PatchObject(json_format, "Parse") |
| 63 | response = firmware_pb2.BuildAllFirmwareResponse() |
| 64 | # Call the method under test. |
| 65 | firmware.BuildAllFirmware(request, response, self.api_config) |
Alex Klein | ab87ceb | 2023-01-24 12:00:51 -0700 | [diff] [blame] | 66 | # Because we mock out the function, we verify that it is called as |
| 67 | # we expect it to be called. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 68 | called_function = os.path.join( |
| 69 | constants.SOURCE_ROOT, fw_path, "firmware_builder.py" |
| 70 | ) |
| 71 | self.cros_build_run_patch.assert_called_with( |
| 72 | [ |
| 73 | called_function, |
| 74 | "--metrics", |
| 75 | mock.ANY, |
| 76 | "--code-coverage", |
| 77 | "build", |
| 78 | ], |
| 79 | check=False, |
| 80 | ) |
| 81 | # Verify that we try to parse the metrics file. |
| 82 | json_format_patch.assert_called() |
Michael Mortensen | 7335e30 | 2021-02-23 10:42:56 -0700 | [diff] [blame] | 83 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 84 | def testValidateOnly(self): |
Alex Klein | ab87ceb | 2023-01-24 12:00:51 -0700 | [diff] [blame] | 85 | """Verify a validate-only call does not execute any logic.""" |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 86 | for fw_loc in common_pb2.FwLocation.values(): |
| 87 | if not firmware.get_fw_loc(fw_loc): |
| 88 | continue |
| 89 | request = self._GetInput( |
| 90 | chroot_path=self.chroot_path, |
| 91 | fw_location=fw_loc, |
| 92 | code_coverage=True, |
| 93 | ) |
| 94 | response = firmware_pb2.BuildAllFirmwareResponse() |
| 95 | firmware.BuildAllFirmware( |
| 96 | request, response, self.validate_only_config |
| 97 | ) |
| 98 | self.cros_build_run_patch.assert_not_called() |
Michael Mortensen | 515c889 | 2021-02-26 15:37:59 -0700 | [diff] [blame] | 99 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 100 | def testMockCall(self): |
Alex Klein | ab87ceb | 2023-01-24 12:00:51 -0700 | [diff] [blame] | 101 | """Test a mock call does not execute logic, returns mocked value.""" |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 102 | for fw_loc in common_pb2.FwLocation.values(): |
| 103 | if not firmware.get_fw_loc(fw_loc): |
| 104 | continue |
| 105 | request = self._GetInput( |
| 106 | chroot_path=self.chroot_path, |
| 107 | fw_location=fw_loc, |
| 108 | code_coverage=True, |
| 109 | ) |
| 110 | response = firmware_pb2.BuildAllFirmwareResponse() |
| 111 | firmware.BuildAllFirmware(request, response, self.mock_call_config) |
| 112 | self.cros_build_run_patch.assert_not_called() |
| 113 | self.assertEqual(len(response.metrics.value), 1) |
| 114 | self.assertEqual(response.metrics.value[0].target_name, "foo") |
| 115 | self.assertEqual(response.metrics.value[0].platform_name, "bar") |
| 116 | self.assertEqual(len(response.metrics.value[0].fw_section), 1) |
| 117 | self.assertEqual( |
| 118 | response.metrics.value[0].fw_section[0].region, "EC_RO" |
| 119 | ) |
| 120 | self.assertEqual(response.metrics.value[0].fw_section[0].used, 100) |
| 121 | self.assertEqual(response.metrics.value[0].fw_section[0].total, 150) |