blob: adf8bf16a3b9aeefd83fa1ee54ddcb8ab767f74e [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."""
Azizur Rahmanc6cf6e92021-10-21 18:58:19 +000044 for fw_loc in common_pb2.FwLocation.values():
45 fw_path = firmware.get_fw_loc(fw_loc)
46 if not fw_path:
47 continue
48 request = self._GetInput(chroot_path=self.chroot_path,
49 fw_location=fw_loc,
50 code_coverage=True)
51 # TODO(mmortensen): Consider refactoring firmware._call_entry code
52 # (putting the parsing of the output file in a function) so that we don't
53 # have to mock something as generic as 'json_format.Parse' to avoid an
54 # error on parsing an empty(due to mock call) file.
55 json_format_patch = self.PatchObject(json_format, 'Parse')
56 response = firmware_pb2.BuildAllFirmwareResponse()
57 # Call the method under test.
58 firmware.BuildAllFirmware(request, response, self.api_config)
59 # Because we mock out the function, we verify that it is called as we
60 # expect it to be called.
61 called_function = os.path.join(constants.SOURCE_ROOT,
62 fw_path, 'firmware_builder.py')
63 self.cros_build_run_patch.assert_called_with(
64 [called_function, '--metrics', mock.ANY, '--code-coverage', 'build'],
65 check=False)
66 # Verify that we try to parse the metrics file.
67 json_format_patch.assert_called()
Michael Mortensen7335e302021-02-23 10:42:56 -070068
69 def testValidateOnly(self):
70 """Sanity check that a validate only call does not execute any logic."""
Azizur Rahmanc6cf6e92021-10-21 18:58:19 +000071 for fw_loc in common_pb2.FwLocation.values():
72 if not firmware.get_fw_loc(fw_loc):
73 continue
74 request = self._GetInput(chroot_path=self.chroot_path,
75 fw_location=fw_loc,
76 code_coverage=True)
77 response = firmware_pb2.BuildAllFirmwareResponse()
78 firmware.BuildAllFirmware(request, response, self.validate_only_config)
79 self.cros_build_run_patch.assert_not_called()
Michael Mortensen515c8892021-02-26 15:37:59 -070080
81 def testMockCall(self):
82 """Test that a mock call does not execute logic, returns mocked value."""
Azizur Rahmanc6cf6e92021-10-21 18:58:19 +000083 for fw_loc in common_pb2.FwLocation.values():
84 if not firmware.get_fw_loc(fw_loc):
85 continue
86 request = self._GetInput(chroot_path=self.chroot_path,
87 fw_location=fw_loc,
88 code_coverage=True)
89 response = firmware_pb2.BuildAllFirmwareResponse()
90 firmware.BuildAllFirmware(request, response, self.mock_call_config)
91 self.cros_build_run_patch.assert_not_called()
92 self.assertEqual(len(response.metrics.value), 1)
93 self.assertEqual(response.metrics.value[0].target_name, 'foo')
94 self.assertEqual(response.metrics.value[0].platform_name, 'bar')
95 self.assertEqual(len(response.metrics.value[0].fw_section), 1)
96 self.assertEqual(response.metrics.value[0].fw_section[0].region, 'EC_RO')
97 self.assertEqual(response.metrics.value[0].fw_section[0].used, 100)
98 self.assertEqual(response.metrics.value[0].fw_section[0].total, 150)