blob: 111540e26953b94609c2346f7305f7f784e6ec1c [file] [log] [blame]
Sam Chiu81bdc652018-06-29 18:45:08 +08001# 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 Chiu81bdc652018-06-29 18:45:08 +080015import platform
herbertxue975b8872020-02-12 14:41:41 +080016import shutil
17import tempfile
Sam Chiu81bdc652018-06-29 18:45:08 +080018import unittest
herbertxue975b8872020-02-12 14:41:41 +080019import mock
Sam Chiu81bdc652018-06-29 18:45:08 +080020
21from acloud.internal.lib import driver_test_lib
herbertxue07293a32018-11-05 20:40:11 +080022from acloud.internal.lib import utils
Sam Chiu81bdc652018-06-29 18:45:08 +080023from acloud.setup import setup_common
chojoyce1b818bb2018-10-03 16:34:57 +080024from acloud.setup.host_setup_runner import AvdPkgInstaller
herbertxue975b8872020-02-12 14:41:41 +080025from acloud.setup.host_setup_runner import CuttlefishCommonPkgInstaller
26from acloud.setup.host_setup_runner import CuttlefishHostSetup
Sam Chiu81bdc652018-06-29 18:45:08 +080027
28
29class CuttlefishHostSetupTest(driver_test_lib.BaseDriverTest):
30 """Test CuttlsfishHostSetup."""
31
32 LSMOD_OUTPUT = """nvidia_modeset 860160 6 nvidia_drm
33module1 12312 1
34module2 12312 1
35ghash_clmulni_intel 16384 0
36aesni_intel 167936 3
37aes_x86_64 20480 1 aesni_intel
38lrw 16384 1 aesni_intel"""
39
40 # pylint: disable=invalid-name
41 def setUp(self):
42 """Set up the test."""
43 super(CuttlefishHostSetupTest, self).setUp()
44 self.CuttlefishHostSetup = CuttlefishHostSetup()
45
46 def testShouldRunFalse(self):
47 """Test ShouldRun returns False."""
herbertxue07293a32018-11-05 20:40:11 +080048 self.Patch(utils, "CheckUserInGroups", return_value=True)
Sam Chiu81bdc652018-06-29 18:45:08 +080049 self.Patch(CuttlefishHostSetup, "_CheckLoadedModules", return_value=True)
50 self.assertFalse(self.CuttlefishHostSetup.ShouldRun())
51
52 def testShouldRunTrue(self):
53 """Test ShouldRun returns True."""
54 # 1. Checking groups fails.
55 self.Patch(
herbertxue07293a32018-11-05 20:40:11 +080056 utils, "CheckUserInGroups", return_value=False)
Sam Chiu81bdc652018-06-29 18:45:08 +080057 self.Patch(CuttlefishHostSetup, "_CheckLoadedModules", return_value=True)
58 self.assertTrue(self.CuttlefishHostSetup.ShouldRun())
59
60 # 2. Checking modules fails.
herbertxue07293a32018-11-05 20:40:11 +080061 self.Patch(utils, "CheckUserInGroups", return_value=True)
Sam Chiu81bdc652018-06-29 18:45:08 +080062 self.Patch(
63 CuttlefishHostSetup, "_CheckLoadedModules", return_value=False)
64 self.assertTrue(self.CuttlefishHostSetup.ShouldRun())
65
66 # pylint: disable=protected-access
Sam Chiu81bdc652018-06-29 18:45:08 +080067 def testCheckLoadedModules(self):
68 """Test _CheckLoadedModules."""
69 self.Patch(
70 setup_common, "CheckCmdOutput", return_value=self.LSMOD_OUTPUT)
71
72 # Required modules are all in lsmod should return True.
73 self.assertTrue(
74 self.CuttlefishHostSetup._CheckLoadedModules(["module1", "module2"]))
75 # Required modules are not all in lsmod should return False.
76 self.assertFalse(
77 self.CuttlefishHostSetup._CheckLoadedModules(["module1", "module3"]))
78
79
chojoyce1b818bb2018-10-03 16:34:57 +080080class AvdPkgInstallerTest(driver_test_lib.BaseDriverTest):
81 """Test AvdPkgInstallerTest."""
Sam Chiu81bdc652018-06-29 18:45:08 +080082
83 # pylint: disable=invalid-name
84 def setUp(self):
85 """Set up the test."""
chojoyce1b818bb2018-10-03 16:34:57 +080086 super(AvdPkgInstallerTest, self).setUp()
87 self.AvdPkgInstaller = AvdPkgInstaller()
Sam Chiu81bdc652018-06-29 18:45:08 +080088
89 def testShouldNotRun(self):
90 """Test ShoudRun should raise error in non-linux os."""
91 self.Patch(platform, "system", return_value="Mac")
chojoyce1b818bb2018-10-03 16:34:57 +080092 self.assertFalse(self.AvdPkgInstaller.ShouldRun())
Sam Chiu81bdc652018-06-29 18:45:08 +080093
94
herbertxue975b8872020-02-12 14:41:41 +080095class CuttlefishCommonPkgInstallerTest(driver_test_lib.BaseDriverTest):
96 """Test CuttlefishCommonPkgInstallerTest."""
97
98 # pylint: disable=invalid-name
99 def setUp(self):
100 """Set up the test."""
101 super(CuttlefishCommonPkgInstallerTest, self).setUp()
102 self.CuttlefishCommonPkgInstaller = CuttlefishCommonPkgInstaller()
103
104 def testShouldRun(self):
105 """Test ShoudRun."""
106 self.Patch(platform, "system", return_value="Linux")
107 self.Patch(setup_common, "PackageInstalled", return_value=False)
108 self.assertTrue(self.CuttlefishCommonPkgInstaller.ShouldRun())
109
110 @mock.patch.object(shutil, "rmtree")
111 @mock.patch.object(setup_common, "CheckCmdOutput")
112 def testRun(self, mock_cmd, mock_rmtree):
113 """Test Run."""
114 fake_tmp_folder = "/tmp/cf-common"
115 self.Patch(tempfile, "mkdtemp", return_value=fake_tmp_folder)
116 self.Patch(utils, "GetUserAnswerYes", return_value="y")
117 self.Patch(CuttlefishCommonPkgInstaller, "ShouldRun", return_value=True)
118 self.CuttlefishCommonPkgInstaller.Run()
119 self.assertEqual(mock_cmd.call_count, 1)
120 mock_rmtree.assert_called_once_with(fake_tmp_folder)
121
122
Sam Chiu81bdc652018-06-29 18:45:08 +0800123if __name__ == "__main__":
124 unittest.main()