blob: e37ac121dbff43a375976b5d54af5d48efb2b48f [file] [log] [blame]
Michael Mortensen7335e302021-02-23 10:42:56 -07001# -*- coding: utf-8 -*-
2# Copyright 2021 The Chromium OS Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5"""Unittests for Firmware operations."""
6
7from __future__ import print_function
8
9import os
10
11import mock
12
13from google.protobuf import json_format
14
15from chromite.api import api_config
16from chromite.api.controller import firmware
17from chromite.api.gen.chromite.api import firmware_pb2
18from chromite.lib import constants
19from chromite.lib import cros_build_lib
20from chromite.lib import cros_test_lib
21
22
23class BuildAllFirmwareTestCase(cros_test_lib.MockTempDirTestCase,
24 api_config.ApiConfigMixin):
25 """BuildAllFirmware tests."""
26
27 def setUp(self):
28 self.chroot_path = '/path/to/chroot'
29 self.cros_build_run_patch = self.PatchObject(
30 cros_build_lib,
31 'run',
32 return_value=cros_build_lib.CommandResult(returncode=0))
33
34 def _GetInput(self,
35 chroot_path=None,
36 fw_location=firmware_pb2.PLATFORM_EC,
37 code_coverage=False):
38 """Helper for creating input message."""
39 proto = firmware_pb2.BuildAllFirmwareRequest(
40 firmware_location=fw_location,
41 chroot={'path': chroot_path},
42 code_coverage=code_coverage)
43 return proto
44
45 def testBuildAllFirmware(self):
46 """Test invocation of endpoint by verifying call to cros_build_lib.run."""
47 request = self._GetInput(chroot_path=self.chroot_path, code_coverage=True)
48 # TODO(mmortensen): Consider refactoring firmware._call_entry code (perhaps
49 # putting the parsing of the output file in a function) so that we don't
50 # have to mock something as generic as 'json_format.Parse' to avoid an
51 # error on parsing an empty(due to mock call) file.
52 json_format_patch = self.PatchObject(json_format, 'Parse')
53 response = firmware_pb2.BuildAllFirmwareResponse()
54 # Call the method under test.
55 firmware.BuildAllFirmware(request, response, self.api_config)
56 # Because we mock out the function, we verify that it is called as we
57 # expect it to be called.
58 called_function = os.path.join(constants.SOURCE_ROOT,
59 'src/platform/ec/firmware_builder.py')
60 self.cros_build_run_patch.assert_called_with(
61 [called_function, '--metrics', mock.ANY, '--code-coverage', 'build'],
62 check=False)
63 # Verify that we try to parse the metrics file.
64 json_format_patch.assert_called()
65
66 def testValidateOnly(self):
67 """Sanity check that a validate only call does not execute any logic."""
68 request = self._GetInput(chroot_path=self.chroot_path, code_coverage=True)
69 response = firmware_pb2.BuildAllFirmwareResponse()
70 firmware.BuildAllFirmware(request, response, self.validate_only_config)
71 self.cros_build_run_patch.assert_not_called()