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