xref: /aosp_15_r20/external/toolchain-utils/llvm_tools/chroot_unittest.py (revision 760c253c1ed00ce9abd48f8546f08516e57485fe)
1*760c253cSXin Li#!/usr/bin/env python3
2*760c253cSXin Li# Copyright 2020 The ChromiumOS Authors
3*760c253cSXin Li# Use of this source code is governed by a BSD-style license that can be
4*760c253cSXin Li# found in the LICENSE file.
5*760c253cSXin Li
6*760c253cSXin Li"""Unit tests for chroot helper functions."""
7*760c253cSXin Li
8*760c253cSXin Liimport subprocess
9*760c253cSXin Liimport unittest
10*760c253cSXin Lifrom unittest import mock
11*760c253cSXin Li
12*760c253cSXin Liimport chroot
13*760c253cSXin Li
14*760c253cSXin Li
15*760c253cSXin Li# These are unittests; protected access is OK to a point.
16*760c253cSXin Li# pylint: disable=protected-access
17*760c253cSXin Li
18*760c253cSXin Li
19*760c253cSXin Liclass HelperFunctionsTest(unittest.TestCase):
20*760c253cSXin Li    """Test class for updating LLVM hashes of packages."""
21*760c253cSXin Li
22*760c253cSXin Li    @mock.patch.object(subprocess, "check_output")
23*760c253cSXin Li    def testSucceedsToGetChrootEbuildPathForPackage(self, mock_chroot_command):
24*760c253cSXin Li        package_chroot_path = "/chroot/path/to/package.ebuild"
25*760c253cSXin Li
26*760c253cSXin Li        # Emulate ChrootRunCommandWOutput behavior when a chroot path is found
27*760c253cSXin Li        # for a valid package.
28*760c253cSXin Li        mock_chroot_command.return_value = package_chroot_path
29*760c253cSXin Li
30*760c253cSXin Li        chroot_path = "/test/chroot/path"
31*760c253cSXin Li        package_list = ["new-test/package"]
32*760c253cSXin Li
33*760c253cSXin Li        self.assertEqual(
34*760c253cSXin Li            chroot.GetChrootEbuildPaths(chroot_path, package_list),
35*760c253cSXin Li            [package_chroot_path],
36*760c253cSXin Li        )
37*760c253cSXin Li
38*760c253cSXin Li        mock_chroot_command.assert_called_once()
39*760c253cSXin Li
40*760c253cSXin Li    def testFailedToConvertChrootPathWithInvalidPrefix(self):
41*760c253cSXin Li        chroot_path = "/path/to/chroot"
42*760c253cSXin Li        chroot_file_path = "/src/package.ebuild"
43*760c253cSXin Li
44*760c253cSXin Li        # Verify the exception is raised when a chroot path does not have the
45*760c253cSXin Li        # prefix '/mnt/host/source/'.
46*760c253cSXin Li        with self.assertRaises(ValueError) as err:
47*760c253cSXin Li            chroot.ConvertChrootPathsToAbsolutePaths(
48*760c253cSXin Li                chroot_path, [chroot_file_path]
49*760c253cSXin Li            )
50*760c253cSXin Li
51*760c253cSXin Li        self.assertEqual(
52*760c253cSXin Li            str(err.exception),
53*760c253cSXin Li            "Invalid prefix for the chroot path: " "%s" % chroot_file_path,
54*760c253cSXin Li        )
55*760c253cSXin Li
56*760c253cSXin Li    def testSucceedsToConvertChrootPathToAbsolutePath(self):
57*760c253cSXin Li        chroot_path = "/path/to/chroot"
58*760c253cSXin Li        chroot_file_paths = ["/mnt/host/source/src/package.ebuild"]
59*760c253cSXin Li
60*760c253cSXin Li        expected_abs_path = "/path/to/chroot/src/package.ebuild"
61*760c253cSXin Li
62*760c253cSXin Li        self.assertEqual(
63*760c253cSXin Li            chroot.ConvertChrootPathsToAbsolutePaths(
64*760c253cSXin Li                chroot_path, chroot_file_paths
65*760c253cSXin Li            ),
66*760c253cSXin Li            [expected_abs_path],
67*760c253cSXin Li        )
68*760c253cSXin Li
69*760c253cSXin Li
70*760c253cSXin Liif __name__ == "__main__":
71*760c253cSXin Li    unittest.main()
72