| 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 |
| 16 | import unittest |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 17 | |
| 18 | from acloud.internal.lib import driver_test_lib |
| herbertxue | 07293a3 | 2018-11-05 20:40:11 +0800 | [diff] [blame] | 19 | from acloud.internal.lib import utils |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 20 | from acloud.setup import setup_common |
| 21 | from acloud.setup.host_setup_runner import CuttlefishHostSetup |
| chojoyce | 1b818bb | 2018-10-03 16:34:57 +0800 | [diff] [blame] | 22 | from acloud.setup.host_setup_runner import AvdPkgInstaller |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 23 | |
| 24 | |
| 25 | class CuttlefishHostSetupTest(driver_test_lib.BaseDriverTest): |
| 26 | """Test CuttlsfishHostSetup.""" |
| 27 | |
| 28 | LSMOD_OUTPUT = """nvidia_modeset 860160 6 nvidia_drm |
| 29 | module1 12312 1 |
| 30 | module2 12312 1 |
| 31 | ghash_clmulni_intel 16384 0 |
| 32 | aesni_intel 167936 3 |
| 33 | aes_x86_64 20480 1 aesni_intel |
| 34 | lrw 16384 1 aesni_intel""" |
| 35 | |
| 36 | # pylint: disable=invalid-name |
| 37 | def setUp(self): |
| 38 | """Set up the test.""" |
| 39 | super(CuttlefishHostSetupTest, self).setUp() |
| 40 | self.CuttlefishHostSetup = CuttlefishHostSetup() |
| 41 | |
| 42 | def testShouldRunFalse(self): |
| 43 | """Test ShouldRun returns False.""" |
| herbertxue | 07293a3 | 2018-11-05 20:40:11 +0800 | [diff] [blame] | 44 | self.Patch(utils, "CheckUserInGroups", return_value=True) |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 45 | self.Patch(CuttlefishHostSetup, "_CheckLoadedModules", return_value=True) |
| 46 | self.assertFalse(self.CuttlefishHostSetup.ShouldRun()) |
| 47 | |
| 48 | def testShouldRunTrue(self): |
| 49 | """Test ShouldRun returns True.""" |
| 50 | # 1. Checking groups fails. |
| 51 | self.Patch( |
| herbertxue | 07293a3 | 2018-11-05 20:40:11 +0800 | [diff] [blame] | 52 | utils, "CheckUserInGroups", return_value=False) |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 53 | self.Patch(CuttlefishHostSetup, "_CheckLoadedModules", return_value=True) |
| 54 | self.assertTrue(self.CuttlefishHostSetup.ShouldRun()) |
| 55 | |
| 56 | # 2. Checking modules fails. |
| herbertxue | 07293a3 | 2018-11-05 20:40:11 +0800 | [diff] [blame] | 57 | self.Patch(utils, "CheckUserInGroups", return_value=True) |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 58 | self.Patch( |
| 59 | CuttlefishHostSetup, "_CheckLoadedModules", return_value=False) |
| 60 | self.assertTrue(self.CuttlefishHostSetup.ShouldRun()) |
| 61 | |
| 62 | # pylint: disable=protected-access |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 63 | def testCheckLoadedModules(self): |
| 64 | """Test _CheckLoadedModules.""" |
| 65 | self.Patch( |
| 66 | setup_common, "CheckCmdOutput", return_value=self.LSMOD_OUTPUT) |
| 67 | |
| 68 | # Required modules are all in lsmod should return True. |
| 69 | self.assertTrue( |
| 70 | self.CuttlefishHostSetup._CheckLoadedModules(["module1", "module2"])) |
| 71 | # Required modules are not all in lsmod should return False. |
| 72 | self.assertFalse( |
| 73 | self.CuttlefishHostSetup._CheckLoadedModules(["module1", "module3"])) |
| 74 | |
| 75 | |
| chojoyce | 1b818bb | 2018-10-03 16:34:57 +0800 | [diff] [blame] | 76 | class AvdPkgInstallerTest(driver_test_lib.BaseDriverTest): |
| 77 | """Test AvdPkgInstallerTest.""" |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 78 | |
| 79 | # pylint: disable=invalid-name |
| 80 | def setUp(self): |
| 81 | """Set up the test.""" |
| chojoyce | 1b818bb | 2018-10-03 16:34:57 +0800 | [diff] [blame] | 82 | super(AvdPkgInstallerTest, self).setUp() |
| 83 | self.AvdPkgInstaller = AvdPkgInstaller() |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 84 | |
| 85 | def testShouldNotRun(self): |
| 86 | """Test ShoudRun should raise error in non-linux os.""" |
| 87 | self.Patch(platform, "system", return_value="Mac") |
| 88 | |
| chojoyce | 1b818bb | 2018-10-03 16:34:57 +0800 | [diff] [blame] | 89 | self.assertFalse(self.AvdPkgInstaller.ShouldRun()) |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 90 | |
| 91 | |
| 92 | if __name__ == "__main__": |
| 93 | unittest.main() |