Michael Mortensen | 7335e30 | 2021-02-23 10:42:56 -0700 | [diff] [blame] | 1 | # Copyright 2021 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 | """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 |
| 14 | from chromite.lib import constants |
| 15 | from chromite.lib import cros_build_lib |
| 16 | from chromite.lib import cros_test_lib |
| 17 | |
| 18 | |
| 19 | class BuildAllFirmwareTestCase(cros_test_lib.MockTempDirTestCase, |
| 20 | api_config.ApiConfigMixin): |
| 21 | """BuildAllFirmware tests.""" |
| 22 | |
| 23 | def setUp(self): |
| 24 | self.chroot_path = '/path/to/chroot' |
| 25 | self.cros_build_run_patch = self.PatchObject( |
| 26 | cros_build_lib, |
| 27 | 'run', |
| 28 | return_value=cros_build_lib.CommandResult(returncode=0)) |
| 29 | |
| 30 | def _GetInput(self, |
| 31 | chroot_path=None, |
| 32 | fw_location=firmware_pb2.PLATFORM_EC, |
| 33 | code_coverage=False): |
| 34 | """Helper for creating input message.""" |
| 35 | proto = firmware_pb2.BuildAllFirmwareRequest( |
| 36 | firmware_location=fw_location, |
| 37 | chroot={'path': chroot_path}, |
| 38 | code_coverage=code_coverage) |
| 39 | return proto |
| 40 | |
| 41 | def testBuildAllFirmware(self): |
| 42 | """Test invocation of endpoint by verifying call to cros_build_lib.run.""" |
| 43 | request = self._GetInput(chroot_path=self.chroot_path, code_coverage=True) |
| 44 | # TODO(mmortensen): Consider refactoring firmware._call_entry code (perhaps |
| 45 | # putting the parsing of the output file in a function) so that we don't |
| 46 | # have to mock something as generic as 'json_format.Parse' to avoid an |
| 47 | # error on parsing an empty(due to mock call) file. |
| 48 | json_format_patch = self.PatchObject(json_format, 'Parse') |
| 49 | response = firmware_pb2.BuildAllFirmwareResponse() |
| 50 | # Call the method under test. |
| 51 | firmware.BuildAllFirmware(request, response, self.api_config) |
| 52 | # Because we mock out the function, we verify that it is called as we |
| 53 | # expect it to be called. |
| 54 | called_function = os.path.join(constants.SOURCE_ROOT, |
| 55 | 'src/platform/ec/firmware_builder.py') |
| 56 | self.cros_build_run_patch.assert_called_with( |
| 57 | [called_function, '--metrics', mock.ANY, '--code-coverage', 'build'], |
| 58 | check=False) |
| 59 | # Verify that we try to parse the metrics file. |
| 60 | json_format_patch.assert_called() |
| 61 | |
| 62 | def testValidateOnly(self): |
| 63 | """Sanity check that a validate only call does not execute any logic.""" |
| 64 | request = self._GetInput(chroot_path=self.chroot_path, code_coverage=True) |
| 65 | response = firmware_pb2.BuildAllFirmwareResponse() |
| 66 | firmware.BuildAllFirmware(request, response, self.validate_only_config) |
| 67 | self.cros_build_run_patch.assert_not_called() |
Michael Mortensen | 515c889 | 2021-02-26 15:37:59 -0700 | [diff] [blame] | 68 | |
| 69 | def testMockCall(self): |
| 70 | """Test that a mock call does not execute logic, returns mocked value.""" |
| 71 | request = self._GetInput(chroot_path=self.chroot_path, code_coverage=True) |
| 72 | response = firmware_pb2.BuildAllFirmwareResponse() |
| 73 | firmware.BuildAllFirmware(request, response, self.mock_call_config) |
| 74 | self.cros_build_run_patch.assert_not_called() |
| 75 | self.assertEqual(len(response.metrics.value), 1) |
| 76 | self.assertEqual(response.metrics.value[0].target_name, 'foo') |
| 77 | self.assertEqual(response.metrics.value[0].platform_name, 'bar') |
| 78 | self.assertEqual(len(response.metrics.value[0].fw_section), 1) |
| 79 | self.assertEqual(response.metrics.value[0].fw_section[0].region, |
| 80 | firmware_pb2.FwBuildMetric.FwSection.EC_RO) |
| 81 | self.assertEqual(response.metrics.value[0].fw_section[0].used, 100) |
| 82 | self.assertEqual(response.metrics.value[0].fw_section[0].total, 150) |