blob: e435be678f22ac6236c3e084eb967fe3f57dd6a1 [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
chojoyce8cec5002019-11-04 18:15:09 +080020from six import b
Sam Chiu81bdc652018-06-29 18:45:08 +080021
22from acloud.internal.lib import driver_test_lib
herbertxue07293a32018-11-05 20:40:11 +080023from acloud.internal.lib import utils
Sam Chiu81bdc652018-06-29 18:45:08 +080024from acloud.setup import setup_common
chojoyce1b818bb2018-10-03 16:34:57 +080025from acloud.setup.host_setup_runner import AvdPkgInstaller
herbertxue975b8872020-02-12 14:41:41 +080026from acloud.setup.host_setup_runner import CuttlefishCommonPkgInstaller
27from acloud.setup.host_setup_runner import CuttlefishHostSetup
Sam Chiu81bdc652018-06-29 18:45:08 +080028
29
30class CuttlefishHostSetupTest(driver_test_lib.BaseDriverTest):
31 """Test CuttlsfishHostSetup."""
32
chojoyce8cec5002019-11-04 18:15:09 +080033 LSMOD_OUTPUT = b("""nvidia_modeset 860160 6 nvidia_drm
Sam Chiu81bdc652018-06-29 18:45:08 +080034module1 12312 1
35module2 12312 1
36ghash_clmulni_intel 16384 0
37aesni_intel 167936 3
38aes_x86_64 20480 1 aesni_intel
chojoyce8cec5002019-11-04 18:15:09 +080039lrw 16384 1 aesni_intel""")
Sam Chiu81bdc652018-06-29 18:45:08 +080040
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."""
herbertxue07293a32018-11-05 20:40:11 +080049 self.Patch(utils, "CheckUserInGroups", return_value=True)
Sam Chiu81bdc652018-06-29 18:45:08 +080050 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(
herbertxue07293a32018-11-05 20:40:11 +080057 utils, "CheckUserInGroups", return_value=False)
Sam Chiu81bdc652018-06-29 18:45:08 +080058 self.Patch(CuttlefishHostSetup, "_CheckLoadedModules", return_value=True)
59 self.assertTrue(self.CuttlefishHostSetup.ShouldRun())
60
61 # 2. Checking modules fails.
herbertxue07293a32018-11-05 20:40:11 +080062 self.Patch(utils, "CheckUserInGroups", return_value=True)
Sam Chiu81bdc652018-06-29 18:45:08 +080063 self.Patch(
64 CuttlefishHostSetup, "_CheckLoadedModules", return_value=False)
65 self.assertTrue(self.CuttlefishHostSetup.ShouldRun())
66
67 # pylint: disable=protected-access
Sam Chiu81bdc652018-06-29 18:45:08 +080068 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
chojoyce1b818bb2018-10-03 16:34:57 +080081class AvdPkgInstallerTest(driver_test_lib.BaseDriverTest):
82 """Test AvdPkgInstallerTest."""
Sam Chiu81bdc652018-06-29 18:45:08 +080083
84 # pylint: disable=invalid-name
85 def setUp(self):
86 """Set up the test."""
chojoyce1b818bb2018-10-03 16:34:57 +080087 super(AvdPkgInstallerTest, self).setUp()
88 self.AvdPkgInstaller = AvdPkgInstaller()
Sam Chiu81bdc652018-06-29 18:45:08 +080089
90 def testShouldNotRun(self):
91 """Test ShoudRun should raise error in non-linux os."""
92 self.Patch(platform, "system", return_value="Mac")
chojoyce1b818bb2018-10-03 16:34:57 +080093 self.assertFalse(self.AvdPkgInstaller.ShouldRun())
Sam Chiu81bdc652018-06-29 18:45:08 +080094
95
herbertxue975b8872020-02-12 14:41:41 +080096class 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 Chiu81bdc652018-06-29 18:45:08 +0800124if __name__ == "__main__":
125 unittest.main()