• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3# Copyright 2020 The Chromium OS Authors. All rights reserved.
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
9from __future__ import print_function
10
11import subprocess
12import unittest
13import unittest.mock as mock
14
15import chroot
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    mock_chroot_command.assert_called_once()
40
41  def testFailedToConvertChrootPathWithInvalidPrefix(self):
42    chroot_path = '/path/to/chroot'
43    chroot_file_path = '/src/package.ebuild'
44
45    # Verify the exception is raised when a chroot path does not have the prefix
46    # '/mnt/host/source/'.
47    with self.assertRaises(ValueError) as err:
48      chroot.ConvertChrootPathsToAbsolutePaths(chroot_path, [chroot_file_path])
49
50    self.assertEqual(
51        str(err.exception), 'Invalid prefix for the chroot path: '
52        '%s' % chroot_file_path)
53
54  def testSucceedsToConvertChrootPathToAbsolutePath(self):
55    chroot_path = '/path/to/chroot'
56    chroot_file_paths = ['/mnt/host/source/src/package.ebuild']
57
58    expected_abs_path = '/path/to/chroot/src/package.ebuild'
59
60    self.assertEqual(
61        chroot.ConvertChrootPathsToAbsolutePaths(
62            chroot_path, chroot_file_paths), [expected_abs_path])
63
64
65if __name__ == '__main__':
66  unittest.main()
67