| 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.""" |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 15 | import platform |
| herbertxue | 975b887 | 2020-02-12 14:41:41 +0800 | [diff] [blame] | 16 | import shutil |
| 17 | import tempfile |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 18 | import unittest |
| chojoyce | 98f07c0 | 2021-03-17 21:47:38 +0800 | [diff] [blame] | 19 | |
| 20 | from unittest import mock |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 21 | |
| 22 | from acloud.internal.lib import driver_test_lib |
| herbertxue | 07293a3 | 2018-11-05 20:40:11 +0800 | [diff] [blame] | 23 | from acloud.internal.lib import utils |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 24 | from acloud.setup import setup_common |
| chojoyce | 1b818bb | 2018-10-03 16:34:57 +0800 | [diff] [blame] | 25 | from acloud.setup.host_setup_runner import AvdPkgInstaller |
| herbertxue | 975b887 | 2020-02-12 14:41:41 +0800 | [diff] [blame] | 26 | from acloud.setup.host_setup_runner import CuttlefishCommonPkgInstaller |
| 27 | from acloud.setup.host_setup_runner import CuttlefishHostSetup |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 28 | |
| 29 | |
| 30 | class CuttlefishHostSetupTest(driver_test_lib.BaseDriverTest): |
| 31 | """Test CuttlsfishHostSetup.""" |
| 32 | |
| Sam Chiu | b6bf79f | 2020-06-09 20:47:42 +0800 | [diff] [blame] | 33 | LSMOD_OUTPUT = """nvidia_modeset 860160 6 nvidia_drm |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 34 | module1 12312 1 |
| 35 | module2 12312 1 |
| 36 | ghash_clmulni_intel 16384 0 |
| 37 | aesni_intel 167936 3 |
| 38 | aes_x86_64 20480 1 aesni_intel |
| Sam Chiu | b6bf79f | 2020-06-09 20:47:42 +0800 | [diff] [blame] | 39 | lrw 16384 1 aesni_intel""" |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 40 | |
| 41 | # pylint: disable=invalid-name |
| 42 | def setUp(self): |
| 43 | """Set up the test.""" |
| 44 | super(CuttlefishHostSetupTest, self).setUp() |
| 45 | self.CuttlefishHostSetup = CuttlefishHostSetup() |
| 46 | |
| 47 | def testShouldRunFalse(self): |
| 48 | """Test ShouldRun returns False.""" |
| herbertxue | 07293a3 | 2018-11-05 20:40:11 +0800 | [diff] [blame] | 49 | self.Patch(utils, "CheckUserInGroups", return_value=True) |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 50 | self.Patch(CuttlefishHostSetup, "_CheckLoadedModules", return_value=True) |
| 51 | self.assertFalse(self.CuttlefishHostSetup.ShouldRun()) |
| 52 | |
| 53 | def testShouldRunTrue(self): |
| 54 | """Test ShouldRun returns True.""" |
| 55 | # 1. Checking groups fails. |
| 56 | self.Patch( |
| herbertxue | 07293a3 | 2018-11-05 20:40:11 +0800 | [diff] [blame] | 57 | utils, "CheckUserInGroups", return_value=False) |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 58 | self.Patch(CuttlefishHostSetup, "_CheckLoadedModules", return_value=True) |
| 59 | self.assertTrue(self.CuttlefishHostSetup.ShouldRun()) |
| 60 | |
| 61 | # 2. Checking modules fails. |
| herbertxue | 07293a3 | 2018-11-05 20:40:11 +0800 | [diff] [blame] | 62 | self.Patch(utils, "CheckUserInGroups", return_value=True) |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 63 | self.Patch( |
| 64 | CuttlefishHostSetup, "_CheckLoadedModules", return_value=False) |
| 65 | self.assertTrue(self.CuttlefishHostSetup.ShouldRun()) |
| 66 | |
| 67 | # pylint: disable=protected-access |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 68 | def testCheckLoadedModules(self): |
| 69 | """Test _CheckLoadedModules.""" |
| 70 | self.Patch( |
| 71 | setup_common, "CheckCmdOutput", return_value=self.LSMOD_OUTPUT) |
| 72 | |
| 73 | # Required modules are all in lsmod should return True. |
| 74 | self.assertTrue( |
| 75 | self.CuttlefishHostSetup._CheckLoadedModules(["module1", "module2"])) |
| 76 | # Required modules are not all in lsmod should return False. |
| 77 | self.assertFalse( |
| 78 | self.CuttlefishHostSetup._CheckLoadedModules(["module1", "module3"])) |
| 79 | |
| 80 | |
| chojoyce | 1b818bb | 2018-10-03 16:34:57 +0800 | [diff] [blame] | 81 | class AvdPkgInstallerTest(driver_test_lib.BaseDriverTest): |
| 82 | """Test AvdPkgInstallerTest.""" |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 83 | |
| 84 | # pylint: disable=invalid-name |
| 85 | def setUp(self): |
| 86 | """Set up the test.""" |
| chojoyce | 1b818bb | 2018-10-03 16:34:57 +0800 | [diff] [blame] | 87 | super(AvdPkgInstallerTest, self).setUp() |
| 88 | self.AvdPkgInstaller = AvdPkgInstaller() |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 89 | |
| 90 | def testShouldNotRun(self): |
| 91 | """Test ShoudRun should raise error in non-linux os.""" |
| 92 | self.Patch(platform, "system", return_value="Mac") |
| chojoyce | 1b818bb | 2018-10-03 16:34:57 +0800 | [diff] [blame] | 93 | self.assertFalse(self.AvdPkgInstaller.ShouldRun()) |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 94 | |
| 95 | |
| herbertxue | 975b887 | 2020-02-12 14:41:41 +0800 | [diff] [blame] | 96 | class CuttlefishCommonPkgInstallerTest(driver_test_lib.BaseDriverTest): |
| 97 | """Test CuttlefishCommonPkgInstallerTest.""" |
| 98 | |
| 99 | # pylint: disable=invalid-name |
| 100 | def setUp(self): |
| 101 | """Set up the test.""" |
| 102 | super(CuttlefishCommonPkgInstallerTest, self).setUp() |
| 103 | self.CuttlefishCommonPkgInstaller = CuttlefishCommonPkgInstaller() |
| 104 | |
| 105 | def testShouldRun(self): |
| 106 | """Test ShoudRun.""" |
| 107 | self.Patch(platform, "system", return_value="Linux") |
| 108 | self.Patch(setup_common, "PackageInstalled", return_value=False) |
| 109 | self.assertTrue(self.CuttlefishCommonPkgInstaller.ShouldRun()) |
| 110 | |
| 111 | @mock.patch.object(shutil, "rmtree") |
| 112 | @mock.patch.object(setup_common, "CheckCmdOutput") |
| 113 | def testRun(self, mock_cmd, mock_rmtree): |
| 114 | """Test Run.""" |
| 115 | fake_tmp_folder = "/tmp/cf-common" |
| 116 | self.Patch(tempfile, "mkdtemp", return_value=fake_tmp_folder) |
| 117 | self.Patch(utils, "GetUserAnswerYes", return_value="y") |
| 118 | self.Patch(CuttlefishCommonPkgInstaller, "ShouldRun", return_value=True) |
| 119 | self.CuttlefishCommonPkgInstaller.Run() |
| 120 | self.assertEqual(mock_cmd.call_count, 1) |
| 121 | mock_rmtree.assert_called_once_with(fake_tmp_folder) |
| 122 | |
| 123 | |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 124 | if __name__ == "__main__": |
| 125 | unittest.main() |