blob: 2f74b6ab3d078b91b6ce2b6615e288546aa31bbf [file] [log] [blame]
Michael Mortensen7335e302021-02-23 10:42:56 -07001# 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 Mortensen7335e302021-02-23 10:42:56 -07006import os
Mike Frysinger166fea02021-02-12 05:30:33 -05007from unittest import mock
Michael Mortensen7335e302021-02-23 10:42:56 -07008
Mike Frysinger2c024062021-05-22 15:43:22 -04009from chromite.third_party.google.protobuf import json_format
Michael Mortensen7335e302021-02-23 10:42:56 -070010
11from chromite.api import api_config
12from chromite.api.controller import firmware
13from chromite.api.gen.chromite.api import firmware_pb2
Andrew Luod08045d2021-06-17 12:06:44 -070014from chromite.api.gen.chromiumos import common_pb2
Michael Mortensen7335e302021-02-23 10:42:56 -070015from chromite.lib import constants
16from chromite.lib import cros_build_lib
17from chromite.lib import cros_test_lib
18
19
20class BuildAllFirmwareTestCase(cros_test_lib.MockTempDirTestCase,
21 api_config.ApiConfigMixin):
22 """BuildAllFirmware tests."""
23
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.CommandResult(returncode=0))
30
31 def _GetInput(self,
32 chroot_path=None,
Andrew Luod08045d2021-06-17 12:06:44 -070033 fw_location=common_pb2.PLATFORM_EC,
Michael Mortensen7335e302021-02-23 10:42:56 -070034 code_coverage=False):
35 """Helper for creating input message."""
36 proto = firmware_pb2.BuildAllFirmwareRequest(
37 firmware_location=fw_location,
38 chroot={'path': chroot_path},
39 code_coverage=code_coverage)
40 return proto
41
42 def testBuildAllFirmware(self):
43 """Test invocation of endpoint by verifying call to cros_build_lib.run."""
44 request = self._GetInput(chroot_path=self.chroot_path, code_coverage=True)
45 # TODO(mmortensen): Consider refactoring firmware._call_entry code (perhaps
46 # putting the parsing of the output file in a function) so that we don't
47 # have to mock something as generic as 'json_format.Parse' to avoid an
48 # error on parsing an empty(due to mock call) file.
49 json_format_patch = self.PatchObject(json_format, 'Parse')
50 response = firmware_pb2.BuildAllFirmwareResponse()
51 # Call the method under test.
52 firmware.BuildAllFirmware(request, response, self.api_config)
53 # Because we mock out the function, we verify that it is called as we
54 # expect it to be called.
55 called_function = os.path.join(constants.SOURCE_ROOT,
56 'src/platform/ec/firmware_builder.py')
57 self.cros_build_run_patch.assert_called_with(
58 [called_function, '--metrics', mock.ANY, '--code-coverage', 'build'],
59 check=False)
60 # Verify that we try to parse the metrics file.
61 json_format_patch.assert_called()
62
63 def testValidateOnly(self):
64 """Sanity check that a validate only call does not execute any logic."""
65 request = self._GetInput(chroot_path=self.chroot_path, code_coverage=True)
66 response = firmware_pb2.BuildAllFirmwareResponse()
67 firmware.BuildAllFirmware(request, response, self.validate_only_config)
68 self.cros_build_run_patch.assert_not_called()
Michael Mortensen515c8892021-02-26 15:37:59 -070069
70 def testMockCall(self):
71 """Test that a mock call does not execute logic, returns mocked value."""
72 request = self._GetInput(chroot_path=self.chroot_path, code_coverage=True)
73 response = firmware_pb2.BuildAllFirmwareResponse()
74 firmware.BuildAllFirmware(request, response, self.mock_call_config)
75 self.cros_build_run_patch.assert_not_called()
76 self.assertEqual(len(response.metrics.value), 1)
77 self.assertEqual(response.metrics.value[0].target_name, 'foo')
78 self.assertEqual(response.metrics.value[0].platform_name, 'bar')
79 self.assertEqual(len(response.metrics.value[0].fw_section), 1)
Andrew Luod08045d2021-06-17 12:06:44 -070080 self.assertEqual(response.metrics.value[0].fw_section[0].region, 'EC_RO')
Michael Mortensen515c8892021-02-26 15:37:59 -070081 self.assertEqual(response.metrics.value[0].fw_section[0].used, 100)
82 self.assertEqual(response.metrics.value[0].fw_section[0].total, 150)