| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 1 | # Copyright 2018 - The Android Open Source Project |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | """Tests for host_setup_runner.""" |
| chojoyce | 3c5ad5c | 2021-07-05 10:44:14 +0800 | [diff] [blame] | 15 | import os |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 16 | import platform |
| herbertxue | 975b887 | 2020-02-12 14:41:41 +0800 | [diff] [blame] | 17 | import shutil |
| chojoyce | f1c15f7 | 2021-10-29 18:34:06 +0800 | [diff] [blame] | 18 | import sys |
| herbertxue | 975b887 | 2020-02-12 14:41:41 +0800 | [diff] [blame] | 19 | import tempfile |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 20 | import unittest |
| chojoyce | 98f07c0 | 2021-03-17 21:47:38 +0800 | [diff] [blame] | 21 | |
| 22 | from unittest import mock |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 23 | |
| 24 | from acloud.internal.lib import driver_test_lib |
| herbertxue | 07293a3 | 2018-11-05 20:40:11 +0800 | [diff] [blame] | 25 | from acloud.internal.lib import utils |
| chojoyce | b081703 | 2022-01-12 18:29:36 +0800 | [diff] [blame] | 26 | from acloud.setup import mkcert |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 27 | from acloud.setup import setup_common |
| chojoyce | 1b818bb | 2018-10-03 16:34:57 +0800 | [diff] [blame] | 28 | from acloud.setup.host_setup_runner import AvdPkgInstaller |
| herbertxue | 975b887 | 2020-02-12 14:41:41 +0800 | [diff] [blame] | 29 | from acloud.setup.host_setup_runner import CuttlefishCommonPkgInstaller |
| 30 | from acloud.setup.host_setup_runner import CuttlefishHostSetup |
| chojoyce | b081703 | 2022-01-12 18:29:36 +0800 | [diff] [blame] | 31 | from acloud.setup.host_setup_runner import LocalCAHostSetup |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 32 | |
| 33 | class CuttlefishHostSetupTest(driver_test_lib.BaseDriverTest): |
| 34 | """Test CuttlsfishHostSetup.""" |
| 35 | |
| Sam Chiu | b6bf79f | 2020-06-09 20:47:42 +0800 | [diff] [blame] | 36 | LSMOD_OUTPUT = """nvidia_modeset 860160 6 nvidia_drm |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 37 | module1 12312 1 |
| 38 | module2 12312 1 |
| 39 | ghash_clmulni_intel 16384 0 |
| 40 | aesni_intel 167936 3 |
| 41 | aes_x86_64 20480 1 aesni_intel |
| Sam Chiu | b6bf79f | 2020-06-09 20:47:42 +0800 | [diff] [blame] | 42 | lrw 16384 1 aesni_intel""" |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 43 | |
| 44 | # pylint: disable=invalid-name |
| 45 | def setUp(self): |
| 46 | """Set up the test.""" |
| herbertxue | d9809d1 | 2021-08-04 14:53:33 +0800 | [diff] [blame] | 47 | super().setUp() |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 48 | self.CuttlefishHostSetup = CuttlefishHostSetup() |
| 49 | |
| 50 | def testShouldRunFalse(self): |
| 51 | """Test ShouldRun returns False.""" |
| herbertxue | 07293a3 | 2018-11-05 20:40:11 +0800 | [diff] [blame] | 52 | self.Patch(utils, "CheckUserInGroups", return_value=True) |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 53 | self.Patch(CuttlefishHostSetup, "_CheckLoadedModules", return_value=True) |
| 54 | self.assertFalse(self.CuttlefishHostSetup.ShouldRun()) |
| 55 | |
| 56 | def testShouldRunTrue(self): |
| 57 | """Test ShouldRun returns True.""" |
| 58 | # 1. Checking groups fails. |
| 59 | self.Patch( |
| herbertxue | 07293a3 | 2018-11-05 20:40:11 +0800 | [diff] [blame] | 60 | utils, "CheckUserInGroups", return_value=False) |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 61 | self.Patch(CuttlefishHostSetup, "_CheckLoadedModules", return_value=True) |
| 62 | self.assertTrue(self.CuttlefishHostSetup.ShouldRun()) |
| 63 | |
| 64 | # 2. Checking modules fails. |
| herbertxue | 07293a3 | 2018-11-05 20:40:11 +0800 | [diff] [blame] | 65 | self.Patch(utils, "CheckUserInGroups", return_value=True) |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 66 | self.Patch( |
| 67 | CuttlefishHostSetup, "_CheckLoadedModules", return_value=False) |
| 68 | self.assertTrue(self.CuttlefishHostSetup.ShouldRun()) |
| 69 | |
| chojoyce | f1c15f7 | 2021-10-29 18:34:06 +0800 | [diff] [blame] | 70 | self.Patch(platform, "system", return_value="Mac") |
| 71 | self.assertFalse(self.CuttlefishHostSetup.ShouldRun()) |
| 72 | |
| 73 | # pylint: disable=no-member |
| 74 | def testRun(self): |
| 75 | """Test Run.""" |
| chojoyce | f1c15f7 | 2021-10-29 18:34:06 +0800 | [diff] [blame] | 76 | self.Patch(CuttlefishHostSetup, "ShouldRun", return_value=True) |
| 77 | self.Patch(utils, "InteractWithQuestion", return_value="y") |
| 78 | self.Patch(setup_common, "CheckCmdOutput") |
| 79 | self.CuttlefishHostSetup.Run() |
| 80 | setup_common.CheckCmdOutput.assert_called() |
| 81 | setup_common.CheckCmdOutput.reset_mock() |
| 82 | |
| 83 | self.Patch(utils, "InteractWithQuestion", return_value="n") |
| 84 | self.CuttlefishHostSetup.Run() |
| 85 | setup_common.CheckCmdOutput.assert_not_called() |
| 86 | |
| chojoyce | 345c2c0 | 2021-08-12 18:24:05 +0800 | [diff] [blame] | 87 | # pylint: disable=protected-access |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 88 | def testCheckLoadedModules(self): |
| 89 | """Test _CheckLoadedModules.""" |
| 90 | self.Patch( |
| 91 | setup_common, "CheckCmdOutput", return_value=self.LSMOD_OUTPUT) |
| 92 | |
| 93 | # Required modules are all in lsmod should return True. |
| 94 | self.assertTrue( |
| 95 | self.CuttlefishHostSetup._CheckLoadedModules(["module1", "module2"])) |
| 96 | # Required modules are not all in lsmod should return False. |
| 97 | self.assertFalse( |
| 98 | self.CuttlefishHostSetup._CheckLoadedModules(["module1", "module3"])) |
| 99 | |
| 100 | |
| chojoyce | 1b818bb | 2018-10-03 16:34:57 +0800 | [diff] [blame] | 101 | class AvdPkgInstallerTest(driver_test_lib.BaseDriverTest): |
| 102 | """Test AvdPkgInstallerTest.""" |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 103 | |
| 104 | # pylint: disable=invalid-name |
| 105 | def setUp(self): |
| 106 | """Set up the test.""" |
| herbertxue | d9809d1 | 2021-08-04 14:53:33 +0800 | [diff] [blame] | 107 | super().setUp() |
| chojoyce | 1b818bb | 2018-10-03 16:34:57 +0800 | [diff] [blame] | 108 | self.AvdPkgInstaller = AvdPkgInstaller() |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 109 | |
| chojoyce | f1c15f7 | 2021-10-29 18:34:06 +0800 | [diff] [blame] | 110 | def testShouldRun(self): |
| 111 | """Test ShouldRun.""" |
| 112 | self.Patch(platform, "system", return_value="Linux") |
| 113 | self.assertFalse(self.AvdPkgInstaller.ShouldRun()) |
| 114 | |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 115 | def testShouldNotRun(self): |
| chojoyce | f1c15f7 | 2021-10-29 18:34:06 +0800 | [diff] [blame] | 116 | """Test ShouldRun should raise error in non-linux os.""" |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 117 | self.Patch(platform, "system", return_value="Mac") |
| chojoyce | 1b818bb | 2018-10-03 16:34:57 +0800 | [diff] [blame] | 118 | self.assertFalse(self.AvdPkgInstaller.ShouldRun()) |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 119 | |
| chojoyce | f1c15f7 | 2021-10-29 18:34:06 +0800 | [diff] [blame] | 120 | # pylint: disable=no-member |
| 121 | def testRun(self): |
| 122 | """Test Run.""" |
| 123 | self.Patch(platform, "system", return_value="Linux") |
| 124 | self.AvdPkgInstaller.PACKAGES = ["pkg1", "pkg2"] |
| 125 | self.Patch(setup_common, "PackageInstalled", return_value=False) |
| 126 | self.Patch(utils, "GetUserAnswerYes", return_value=True) |
| 127 | self.Patch(setup_common, "CheckCmdOutput") |
| 128 | self.Patch(setup_common, "InstallPackage") |
| 129 | self.AvdPkgInstaller.Run() |
| 130 | setup_common.InstallPackage.assert_called() |
| 131 | |
| 132 | self.Patch(utils, "GetUserAnswerYes", return_value=False) |
| 133 | self.Patch(sys, "exit") |
| 134 | self.AvdPkgInstaller.Run() |
| 135 | sys.exit.assert_called_once() |
| 136 | |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 137 | |
| herbertxue | 975b887 | 2020-02-12 14:41:41 +0800 | [diff] [blame] | 138 | class CuttlefishCommonPkgInstallerTest(driver_test_lib.BaseDriverTest): |
| 139 | """Test CuttlefishCommonPkgInstallerTest.""" |
| 140 | |
| 141 | # pylint: disable=invalid-name |
| 142 | def setUp(self): |
| 143 | """Set up the test.""" |
| herbertxue | d9809d1 | 2021-08-04 14:53:33 +0800 | [diff] [blame] | 144 | super().setUp() |
| herbertxue | 975b887 | 2020-02-12 14:41:41 +0800 | [diff] [blame] | 145 | self.CuttlefishCommonPkgInstaller = CuttlefishCommonPkgInstaller() |
| 146 | |
| 147 | def testShouldRun(self): |
| chojoyce | f1c15f7 | 2021-10-29 18:34:06 +0800 | [diff] [blame] | 148 | """Test ShouldRun.""" |
| herbertxue | 975b887 | 2020-02-12 14:41:41 +0800 | [diff] [blame] | 149 | self.Patch(platform, "system", return_value="Linux") |
| 150 | self.Patch(setup_common, "PackageInstalled", return_value=False) |
| 151 | self.assertTrue(self.CuttlefishCommonPkgInstaller.ShouldRun()) |
| 152 | |
| chojoyce | f1c15f7 | 2021-10-29 18:34:06 +0800 | [diff] [blame] | 153 | self.Patch(setup_common, "PackageInstalled", return_value=True) |
| 154 | self.assertFalse(self.CuttlefishCommonPkgInstaller.ShouldRun()) |
| 155 | |
| 156 | self.Patch(platform, "system", return_value="Mac") |
| 157 | self.assertFalse(self.CuttlefishCommonPkgInstaller.ShouldRun()) |
| 158 | |
| 159 | # pylint: disable=no-member |
| herbertxue | 975b887 | 2020-02-12 14:41:41 +0800 | [diff] [blame] | 160 | @mock.patch.object(shutil, "rmtree") |
| 161 | @mock.patch.object(setup_common, "CheckCmdOutput") |
| 162 | def testRun(self, mock_cmd, mock_rmtree): |
| 163 | """Test Run.""" |
| 164 | fake_tmp_folder = "/tmp/cf-common" |
| 165 | self.Patch(tempfile, "mkdtemp", return_value=fake_tmp_folder) |
| chojoyce | f1c15f7 | 2021-10-29 18:34:06 +0800 | [diff] [blame] | 166 | self.Patch(utils, "GetUserAnswerYes", return_value=True) |
| herbertxue | 975b887 | 2020-02-12 14:41:41 +0800 | [diff] [blame] | 167 | self.Patch(CuttlefishCommonPkgInstaller, "ShouldRun", return_value=True) |
| 168 | self.CuttlefishCommonPkgInstaller.Run() |
| 169 | self.assertEqual(mock_cmd.call_count, 1) |
| 170 | mock_rmtree.assert_called_once_with(fake_tmp_folder) |
| 171 | |
| chojoyce | f1c15f7 | 2021-10-29 18:34:06 +0800 | [diff] [blame] | 172 | self.Patch(utils, "GetUserAnswerYes", return_value=False) |
| 173 | self.Patch(sys, "exit") |
| 174 | self.CuttlefishCommonPkgInstaller.Run() |
| 175 | sys.exit.assert_called_once() |
| 176 | |
| 177 | |
| chojoyce | b081703 | 2022-01-12 18:29:36 +0800 | [diff] [blame] | 178 | class LocalCAHostSetupTest(driver_test_lib.BaseDriverTest): |
| 179 | """Test LocalCAHostSetupTest.""" |
| chojoyce | 3c5ad5c | 2021-07-05 10:44:14 +0800 | [diff] [blame] | 180 | |
| 181 | # pylint: disable=invalid-name |
| 182 | def setUp(self): |
| 183 | """Set up the test.""" |
| herbertxue | d9809d1 | 2021-08-04 14:53:33 +0800 | [diff] [blame] | 184 | super().setUp() |
| chojoyce | b081703 | 2022-01-12 18:29:36 +0800 | [diff] [blame] | 185 | self.LocalCAHostSetup = LocalCAHostSetup() |
| chojoyce | 3c5ad5c | 2021-07-05 10:44:14 +0800 | [diff] [blame] | 186 | |
| 187 | def testShouldRun(self): |
| 188 | """Test ShouldRun.""" |
| 189 | self.Patch(platform, "system", return_value="Linux") |
| 190 | self.Patch(os.path, "exists", return_value=False) |
| chojoyce | b081703 | 2022-01-12 18:29:36 +0800 | [diff] [blame] | 191 | self.assertTrue(self.LocalCAHostSetup.ShouldRun()) |
| chojoyce | 3c5ad5c | 2021-07-05 10:44:14 +0800 | [diff] [blame] | 192 | |
| chojoyce | f1c15f7 | 2021-10-29 18:34:06 +0800 | [diff] [blame] | 193 | self.Patch(os.path, "exists", return_value=True) |
| chojoyce | b081703 | 2022-01-12 18:29:36 +0800 | [diff] [blame] | 194 | self.assertFalse(self.LocalCAHostSetup.ShouldRun()) |
| chojoyce | f1c15f7 | 2021-10-29 18:34:06 +0800 | [diff] [blame] | 195 | |
| 196 | self.Patch(platform, "system", return_value="Mac") |
| 197 | self.Patch(os.path, "exists", return_value=False) |
| chojoyce | b081703 | 2022-01-12 18:29:36 +0800 | [diff] [blame] | 198 | self.assertFalse(self.LocalCAHostSetup.ShouldRun()) |
| chojoyce | f1c15f7 | 2021-10-29 18:34:06 +0800 | [diff] [blame] | 199 | |
| 200 | # pylint: disable=no-member |
| chojoyce | b081703 | 2022-01-12 18:29:36 +0800 | [diff] [blame] | 201 | def testRun(self): |
| chojoyce | 3c5ad5c | 2021-07-05 10:44:14 +0800 | [diff] [blame] | 202 | """Test Run.""" |
| chojoyce | f1c15f7 | 2021-10-29 18:34:06 +0800 | [diff] [blame] | 203 | self.Patch(utils, "GetUserAnswerYes", return_value=True) |
| chojoyce | b081703 | 2022-01-12 18:29:36 +0800 | [diff] [blame] | 204 | self.Patch(LocalCAHostSetup, "ShouldRun", return_value=True) |
| 205 | self.Patch(mkcert, "Install") |
| 206 | self.LocalCAHostSetup.Run() |
| 207 | mkcert.Install.assert_called_once() |
| chojoyce | 32ee70e | 2022-02-11 12:39:53 +0800 | [diff] [blame] | 208 | mkcert.Install.reset_mock() |
| chojoyce | f1c15f7 | 2021-10-29 18:34:06 +0800 | [diff] [blame] | 209 | |
| chojoyce | b081703 | 2022-01-12 18:29:36 +0800 | [diff] [blame] | 210 | self.Patch(LocalCAHostSetup, "ShouldRun", return_value=False) |
| 211 | self.LocalCAHostSetup.Run() |
| 212 | mkcert.Install.assert_not_called() |
| chojoyce | f1c15f7 | 2021-10-29 18:34:06 +0800 | [diff] [blame] | 213 | |
| 214 | self.Patch(utils, "GetUserAnswerYes", return_value=False) |
| 215 | self.Patch(sys, "exit") |
| chojoyce | b081703 | 2022-01-12 18:29:36 +0800 | [diff] [blame] | 216 | self.LocalCAHostSetup.Run() |
| chojoyce | 32ee70e | 2022-02-11 12:39:53 +0800 | [diff] [blame] | 217 | mkcert.Install.assert_not_called() |
| chojoyce | 3c5ad5c | 2021-07-05 10:44:14 +0800 | [diff] [blame] | 218 | |
| herbertxue | 975b887 | 2020-02-12 14:41:41 +0800 | [diff] [blame] | 219 | |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 220 | if __name__ == "__main__": |
| 221 | unittest.main() |