blob: 93c5911ced01bbe60f521497e805f64295460a9f [file] [log] [blame]
Mike Frysingerf1ba7ad2022-09-12 05:42:57 -04001# Copyright 2021 The ChromiumOS Authors
Michael Mortensen7335e302021-02-23 10:42:56 -07002# 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
Alex Klein1699fab2022-09-08 08:46:06 -060020class BuildAllFirmwareTestCase(
21 cros_test_lib.MockTempDirTestCase, api_config.ApiConfigMixin
22):
23 """BuildAllFirmware tests."""
Michael Mortensen7335e302021-02-23 10:42:56 -070024
Alex Klein1699fab2022-09-08 08:46:06 -060025 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 Mortensen7335e302021-02-23 10:42:56 -070032
Alex Klein1699fab2022-09-08 08:46:06 -060033 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 Mortensen7335e302021-02-23 10:42:56 -070046
Alex Klein1699fab2022-09-08 08:46:06 -060047 def testBuildAllFirmware(self):
48 """Test invocation of endpoint by verifying call to cros_build_lib.run."""
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
59 # (putting the parsing of the output file in a function) so that we don't
60 # have to mock something as generic as 'json_format.Parse' to avoid an
61 # error on parsing an empty(due to mock call) file.
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)
66 # Because we mock out the function, we verify that it is called as we
67 # expect it to be called.
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 Mortensen7335e302021-02-23 10:42:56 -070083
Alex Klein1699fab2022-09-08 08:46:06 -060084 def testValidateOnly(self):
85 """Sanity check that a validate only call does not execute any logic."""
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 Mortensen515c8892021-02-26 15:37:59 -070099
Alex Klein1699fab2022-09-08 08:46:06 -0600100 def testMockCall(self):
101 """Test that a mock call does not execute logic, returns mocked value."""
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)