• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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."""
15import platform
16import unittest
17
18from acloud.internal.lib import driver_test_lib
19from acloud.internal.lib import utils
20from acloud.setup import setup_common
21from acloud.setup.host_setup_runner import CuttlefishHostSetup
22from acloud.setup.host_setup_runner import AvdPkgInstaller
23
24
25class CuttlefishHostSetupTest(driver_test_lib.BaseDriverTest):
26    """Test CuttlsfishHostSetup."""
27
28    LSMOD_OUTPUT = """nvidia_modeset        860160  6 nvidia_drm
29module1                12312  1
30module2                12312  1
31ghash_clmulni_intel    16384  0
32aesni_intel           167936  3
33aes_x86_64             20480  1 aesni_intel
34lrw                    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."""
44        self.Patch(utils, "CheckUserInGroups", return_value=True)
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(
52            utils, "CheckUserInGroups", return_value=False)
53        self.Patch(CuttlefishHostSetup, "_CheckLoadedModules", return_value=True)
54        self.assertTrue(self.CuttlefishHostSetup.ShouldRun())
55
56        # 2. Checking modules fails.
57        self.Patch(utils, "CheckUserInGroups", return_value=True)
58        self.Patch(
59            CuttlefishHostSetup, "_CheckLoadedModules", return_value=False)
60        self.assertTrue(self.CuttlefishHostSetup.ShouldRun())
61
62    # pylint: disable=protected-access
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
76class AvdPkgInstallerTest(driver_test_lib.BaseDriverTest):
77    """Test AvdPkgInstallerTest."""
78
79    # pylint: disable=invalid-name
80    def setUp(self):
81        """Set up the test."""
82        super(AvdPkgInstallerTest, self).setUp()
83        self.AvdPkgInstaller = AvdPkgInstaller()
84
85    def testShouldNotRun(self):
86        """Test ShoudRun should raise error in non-linux os."""
87        self.Patch(platform, "system", return_value="Mac")
88
89        self.assertFalse(self.AvdPkgInstaller.ShouldRun())
90
91
92if __name__ == "__main__":
93    unittest.main()
94