| 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.""" |
| 15 | import grp |
| 16 | import os |
| 17 | import platform |
| 18 | import unittest |
| 19 | import mock |
| 20 | |
| 21 | from acloud.internal.lib import driver_test_lib |
| 22 | from acloud.setup import setup_common |
| 23 | from acloud.setup.host_setup_runner import CuttlefishHostSetup |
| chojoyce | 1b818bb | 2018-10-03 16:34:57 +0800 | [diff] [blame] | 24 | from acloud.setup.host_setup_runner import AvdPkgInstaller |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 25 | |
| 26 | |
| 27 | class CuttlefishHostSetupTest(driver_test_lib.BaseDriverTest): |
| 28 | """Test CuttlsfishHostSetup.""" |
| 29 | |
| 30 | LSMOD_OUTPUT = """nvidia_modeset 860160 6 nvidia_drm |
| 31 | module1 12312 1 |
| 32 | module2 12312 1 |
| 33 | ghash_clmulni_intel 16384 0 |
| 34 | aesni_intel 167936 3 |
| 35 | aes_x86_64 20480 1 aesni_intel |
| 36 | lrw 16384 1 aesni_intel""" |
| 37 | |
| 38 | # pylint: disable=invalid-name |
| 39 | def setUp(self): |
| 40 | """Set up the test.""" |
| 41 | super(CuttlefishHostSetupTest, self).setUp() |
| 42 | self.CuttlefishHostSetup = CuttlefishHostSetup() |
| 43 | |
| 44 | def testShouldRunFalse(self): |
| 45 | """Test ShouldRun returns False.""" |
| 46 | self.Patch(CuttlefishHostSetup, "_CheckUserInGroups", return_value=True) |
| 47 | self.Patch(CuttlefishHostSetup, "_CheckLoadedModules", return_value=True) |
| 48 | self.assertFalse(self.CuttlefishHostSetup.ShouldRun()) |
| 49 | |
| 50 | def testShouldRunTrue(self): |
| 51 | """Test ShouldRun returns True.""" |
| 52 | # 1. Checking groups fails. |
| 53 | self.Patch( |
| 54 | CuttlefishHostSetup, "_CheckUserInGroups", return_value=False) |
| 55 | self.Patch(CuttlefishHostSetup, "_CheckLoadedModules", return_value=True) |
| 56 | self.assertTrue(self.CuttlefishHostSetup.ShouldRun()) |
| 57 | |
| 58 | # 2. Checking modules fails. |
| 59 | self.Patch(CuttlefishHostSetup, "_CheckUserInGroups", return_value=True) |
| 60 | self.Patch( |
| 61 | CuttlefishHostSetup, "_CheckLoadedModules", return_value=False) |
| 62 | self.assertTrue(self.CuttlefishHostSetup.ShouldRun()) |
| 63 | |
| 64 | # pylint: disable=protected-access |
| 65 | def testCheckUserInGroups(self): |
| 66 | """Test _CheckUserInGroups.""" |
| 67 | self.Patch(os, "getgroups", return_value=[1, 2, 3]) |
| 68 | gr1 = mock.MagicMock() |
| 69 | gr1.gr_name = "fake_gr_1" |
| 70 | gr2 = mock.MagicMock() |
| 71 | gr2.gr_name = "fake_gr_2" |
| 72 | gr3 = mock.MagicMock() |
| 73 | gr3.gr_name = "fake_gr_3" |
| 74 | self.Patch(grp, "getgrgid", side_effect=[gr1, gr2, gr3]) |
| 75 | |
| 76 | # User in all required groups should return true. |
| 77 | self.assertTrue( |
| 78 | self.CuttlefishHostSetup._CheckUserInGroups( |
| 79 | ["fake_gr_1", "fake_gr_2"])) |
| 80 | |
| 81 | # User not in all required groups should return False. |
| 82 | self.Patch(grp, "getgrgid", side_effect=[gr1, gr2, gr3]) |
| 83 | self.assertFalse( |
| 84 | self.CuttlefishHostSetup._CheckUserInGroups( |
| 85 | ["fake_gr_1", "fake_gr_4"])) |
| 86 | |
| 87 | def testCheckLoadedModules(self): |
| 88 | """Test _CheckLoadedModules.""" |
| 89 | self.Patch( |
| 90 | setup_common, "CheckCmdOutput", return_value=self.LSMOD_OUTPUT) |
| 91 | |
| 92 | # Required modules are all in lsmod should return True. |
| 93 | self.assertTrue( |
| 94 | self.CuttlefishHostSetup._CheckLoadedModules(["module1", "module2"])) |
| 95 | # Required modules are not all in lsmod should return False. |
| 96 | self.assertFalse( |
| 97 | self.CuttlefishHostSetup._CheckLoadedModules(["module1", "module3"])) |
| 98 | |
| 99 | |
| chojoyce | 1b818bb | 2018-10-03 16:34:57 +0800 | [diff] [blame] | 100 | class AvdPkgInstallerTest(driver_test_lib.BaseDriverTest): |
| 101 | """Test AvdPkgInstallerTest.""" |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 102 | |
| 103 | # pylint: disable=invalid-name |
| 104 | def setUp(self): |
| 105 | """Set up the test.""" |
| chojoyce | 1b818bb | 2018-10-03 16:34:57 +0800 | [diff] [blame] | 106 | super(AvdPkgInstallerTest, self).setUp() |
| 107 | self.AvdPkgInstaller = AvdPkgInstaller() |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 108 | |
| 109 | def testShouldNotRun(self): |
| 110 | """Test ShoudRun should raise error in non-linux os.""" |
| 111 | self.Patch(platform, "system", return_value="Mac") |
| 112 | |
| chojoyce | 1b818bb | 2018-10-03 16:34:57 +0800 | [diff] [blame] | 113 | self.assertFalse(self.AvdPkgInstaller.ShouldRun()) |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 114 | |
| 115 | |
| 116 | if __name__ == "__main__": |
| 117 | unittest.main() |