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