blob: aa08de2dfdab5fc361cfbc5454524fc0feae90a1 [file] [log] [blame]
Ryan Cui6290f032012-11-20 15:44:43 -08001# Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
Don Garrett25f309a2014-03-19 14:02:12 -07005"""Unittests for cros_generate_sysroot."""
6
Ryan Cui6290f032012-11-20 15:44:43 -08007import os
Mike Frysinger166fea02021-02-12 05:30:33 -05008from unittest import mock
Ryan Cui6290f032012-11-20 15:44:43 -08009
Aviv Keshetb7519e12016-10-04 00:50:00 -070010from chromite.lib import constants
Ryan Cui6290f032012-11-20 15:44:43 -080011from chromite.lib import cros_build_lib
12from chromite.lib import cros_test_lib
Ryan Cui6290f032012-11-20 15:44:43 -080013from chromite.lib import osutils
14from chromite.lib import partial_mock
Chris McDonald2463b9f2020-03-31 15:55:31 -060015from chromite.lib import sudo
Mike Frysinger40ffb532021-02-12 07:36:08 -050016from chromite.scripts import cros_generate_sysroot as cros_gen
Ryan Cui6290f032012-11-20 15:44:43 -080017
18
19Dir = cros_test_lib.Directory
20
21
22class CrosGenMock(partial_mock.PartialMock):
Alex Klein1699fab2022-09-08 08:46:06 -060023 """Helper class to Mock out cros_generate_sysroot.GenerateSysroot."""
Don Garrett25f309a2014-03-19 14:02:12 -070024
Alex Klein1699fab2022-09-08 08:46:06 -060025 TARGET = "chromite.scripts.cros_generate_sysroot.GenerateSysroot"
26 ATTRS = (
27 "_InstallToolchain",
28 "_InstallKernelHeaders",
29 "_InstallBuildDependencies",
30 )
Ryan Cui6290f032012-11-20 15:44:43 -080031
Alex Klein1699fab2022-09-08 08:46:06 -060032 TOOLCHAIN = "toolchain"
33 KERNEL_HEADERS = "kernel_headers"
34 BUILD_DEPS = "build-deps"
Ryan Cui6290f032012-11-20 15:44:43 -080035
Alex Klein1699fab2022-09-08 08:46:06 -060036 def _InstallToolchain(self, inst):
37 osutils.Touch(os.path.join(inst.sysroot, self.TOOLCHAIN))
Ryan Cui6290f032012-11-20 15:44:43 -080038
Alex Klein1699fab2022-09-08 08:46:06 -060039 def _InstallKernelHeaders(self, inst):
40 osutils.Touch(os.path.join(inst.sysroot, self.KERNEL_HEADERS))
Ryan Cui6290f032012-11-20 15:44:43 -080041
Alex Klein1699fab2022-09-08 08:46:06 -060042 def _InstallBuildDependencies(self, inst):
43 osutils.Touch(os.path.join(inst.sysroot, self.BUILD_DEPS))
Ryan Cui6290f032012-11-20 15:44:43 -080044
Alex Klein1699fab2022-09-08 08:46:06 -060045 def VerifyTarball(self, tarball):
46 dir_struct = [
47 Dir(".", []),
48 self.TOOLCHAIN,
49 self.KERNEL_HEADERS,
50 self.BUILD_DEPS,
51 ]
52 cros_test_lib.VerifyTarball(tarball, dir_struct)
Ryan Cui6290f032012-11-20 15:44:43 -080053
54
Alex Klein1699fab2022-09-08 08:46:06 -060055BOARD = "lumpy"
56TAR_NAME = "test.tar.xz"
Ryan Cui6290f032012-11-20 15:44:43 -080057
58
Ryan Cui4d6fca92012-12-13 16:41:56 -080059class OverallTest(cros_test_lib.MockTempDirTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -060060 """Tests for cros_generate_sysroot."""
Ryan Cui6290f032012-11-20 15:44:43 -080061
Alex Klein1699fab2022-09-08 08:46:06 -060062 def setUp(self):
63 self.cg_mock = self.StartPatcher(CrosGenMock())
64 self.PatchObject(
65 sudo.SudoKeepAlive, "_IdentifyTTY", return_value="unknown"
66 )
Ryan Cui6290f032012-11-20 15:44:43 -080067
Alex Klein1699fab2022-09-08 08:46:06 -060068 def testTarballGeneration(self):
69 """End-to-end test of tarball generation."""
70 with mock.patch.object(cros_build_lib, "IsInsideChroot"):
71 cros_build_lib.IsInsideChroot.returnvalue = True
72 cros_gen.main(
73 [
74 "--board",
75 BOARD,
76 "--out-dir",
77 str(self.tempdir),
78 "--out-file",
79 TAR_NAME,
80 "--package",
81 constants.CHROME_CP,
82 ]
83 )
84 self.cg_mock.VerifyTarball(os.path.join(self.tempdir, TAR_NAME))
Ryan Cui6290f032012-11-20 15:44:43 -080085
86
87class InterfaceTest(cros_test_lib.TempDirTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -060088 """Test Parsing and error checking functionality."""
Ryan Cui6290f032012-11-20 15:44:43 -080089
Alex Klein1699fab2022-09-08 08:46:06 -060090 BAD_TARGET_DIR = "/path/to/nowhere"
Ryan Cui6290f032012-11-20 15:44:43 -080091
Alex Klein1699fab2022-09-08 08:46:06 -060092 def _Parse(self, extra_args):
93 return cros_gen.ParseCommandLine(
94 [
95 "--board",
96 BOARD,
97 "--out-dir",
98 str(self.tempdir),
99 "--package",
100 constants.CHROME_CP,
101 ]
102 + extra_args
103 )
Ryan Cui6290f032012-11-20 15:44:43 -0800104
Alex Klein1699fab2022-09-08 08:46:06 -0600105 def testDefaultTargetName(self):
106 """We are getting the right default target name."""
107 options = self._Parse([])
108 self.assertEqual(
109 options.out_file, "sysroot_chromeos-base_chromeos-chrome.tar.xz"
110 )
Ryan Cui6290f032012-11-20 15:44:43 -0800111
Alex Klein1699fab2022-09-08 08:46:06 -0600112 def testMultiplePkgsTargetName(self):
113 """Test getting the right target name with multiple pkgs."""
114 pkgs = "%s virtual/target-os" % constants.CHROME_CP
115 options = cros_gen.ParseCommandLine(
116 [
117 "--board",
118 BOARD,
119 "--out-dir",
120 str(self.tempdir),
121 "--package",
122 pkgs,
123 ]
124 )
Manoj Gupta2c2a2a22018-02-12 13:13:32 -0800125
Alex Klein1699fab2022-09-08 08:46:06 -0600126 self.assertEqual(
127 options.out_file, "sysroot_chromeos-base_chromeos-chrome.tar.xz"
128 )
Manoj Gupta2c2a2a22018-02-12 13:13:32 -0800129
Alex Klein1699fab2022-09-08 08:46:06 -0600130 def testExistingTarget(self):
131 """Erroring out on pre-existing target."""
132 options = self._Parse(["--out-file", TAR_NAME])
133 osutils.Touch(os.path.join(self.tempdir, TAR_NAME))
134 self.assertRaises(
135 cros_build_lib.DieSystemExit, cros_gen.FinishParsing, options
136 )
Ryan Cui6290f032012-11-20 15:44:43 -0800137
Alex Klein1699fab2022-09-08 08:46:06 -0600138 def testNonExisting(self):
139 """Erroring out on non-existent output dir."""
140 options = cros_gen.ParseCommandLine(
141 [
142 "--board",
143 BOARD,
144 "--out-dir",
145 self.BAD_TARGET_DIR,
146 "--package",
147 constants.CHROME_CP,
148 ]
149 )
150 self.assertRaises(
151 cros_build_lib.DieSystemExit, cros_gen.FinishParsing, options
152 )