blob: b0ce6a7492352c0c14b9262dd2f78545054babea [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."""
chojoyce3c5ad5c2021-07-05 10:44:14 +080015import os
Sam Chiu81bdc652018-06-29 18:45:08 +080016import platform
herbertxue975b8872020-02-12 14:41:41 +080017import shutil
18import tempfile
Sam Chiu81bdc652018-06-29 18:45:08 +080019import unittest
chojoyce98f07c02021-03-17 21:47:38 +080020
21from unittest import mock
Sam Chiu81bdc652018-06-29 18:45:08 +080022
23from acloud.internal.lib import driver_test_lib
herbertxue07293a32018-11-05 20:40:11 +080024from acloud.internal.lib import utils
Sam Chiu81bdc652018-06-29 18:45:08 +080025from acloud.setup import setup_common
chojoyce1b818bb2018-10-03 16:34:57 +080026from acloud.setup.host_setup_runner import AvdPkgInstaller
herbertxue975b8872020-02-12 14:41:41 +080027from acloud.setup.host_setup_runner import CuttlefishCommonPkgInstaller
28from acloud.setup.host_setup_runner import CuttlefishHostSetup
chojoyce3c5ad5c2021-07-05 10:44:14 +080029from acloud.setup.host_setup_runner import MkcertPkgInstaller
Sam Chiu81bdc652018-06-29 18:45:08 +080030
31
32class CuttlefishHostSetupTest(driver_test_lib.BaseDriverTest):
33 """Test CuttlsfishHostSetup."""
34
Sam Chiub6bf79f2020-06-09 20:47:42 +080035 LSMOD_OUTPUT = """nvidia_modeset 860160 6 nvidia_drm
Sam Chiu81bdc652018-06-29 18:45:08 +080036module1 12312 1
37module2 12312 1
38ghash_clmulni_intel 16384 0
39aesni_intel 167936 3
40aes_x86_64 20480 1 aesni_intel
Sam Chiub6bf79f2020-06-09 20:47:42 +080041lrw 16384 1 aesni_intel"""
Sam Chiu81bdc652018-06-29 18:45:08 +080042
43 # pylint: disable=invalid-name
44 def setUp(self):
45 """Set up the test."""
46 super(CuttlefishHostSetupTest, self).setUp()
47 self.CuttlefishHostSetup = CuttlefishHostSetup()
48
49 def testShouldRunFalse(self):
50 """Test ShouldRun returns False."""
herbertxue07293a32018-11-05 20:40:11 +080051 self.Patch(utils, "CheckUserInGroups", return_value=True)
Sam Chiu81bdc652018-06-29 18:45:08 +080052 self.Patch(CuttlefishHostSetup, "_CheckLoadedModules", return_value=True)
53 self.assertFalse(self.CuttlefishHostSetup.ShouldRun())
54
55 def testShouldRunTrue(self):
56 """Test ShouldRun returns True."""
57 # 1. Checking groups fails.
58 self.Patch(
herbertxue07293a32018-11-05 20:40:11 +080059 utils, "CheckUserInGroups", return_value=False)
Sam Chiu81bdc652018-06-29 18:45:08 +080060 self.Patch(CuttlefishHostSetup, "_CheckLoadedModules", return_value=True)
61 self.assertTrue(self.CuttlefishHostSetup.ShouldRun())
62
63 # 2. Checking modules fails.
herbertxue07293a32018-11-05 20:40:11 +080064 self.Patch(utils, "CheckUserInGroups", return_value=True)
Sam Chiu81bdc652018-06-29 18:45:08 +080065 self.Patch(
66 CuttlefishHostSetup, "_CheckLoadedModules", return_value=False)
67 self.assertTrue(self.CuttlefishHostSetup.ShouldRun())
68
69 # pylint: disable=protected-access
Sam Chiu81bdc652018-06-29 18:45:08 +080070 def testCheckLoadedModules(self):
71 """Test _CheckLoadedModules."""
72 self.Patch(
73 setup_common, "CheckCmdOutput", return_value=self.LSMOD_OUTPUT)
74
75 # Required modules are all in lsmod should return True.
76 self.assertTrue(
77 self.CuttlefishHostSetup._CheckLoadedModules(["module1", "module2"]))
78 # Required modules are not all in lsmod should return False.
79 self.assertFalse(
80 self.CuttlefishHostSetup._CheckLoadedModules(["module1", "module3"]))
81
82
chojoyce1b818bb2018-10-03 16:34:57 +080083class AvdPkgInstallerTest(driver_test_lib.BaseDriverTest):
84 """Test AvdPkgInstallerTest."""
Sam Chiu81bdc652018-06-29 18:45:08 +080085
86 # pylint: disable=invalid-name
87 def setUp(self):
88 """Set up the test."""
chojoyce1b818bb2018-10-03 16:34:57 +080089 super(AvdPkgInstallerTest, self).setUp()
90 self.AvdPkgInstaller = AvdPkgInstaller()
Sam Chiu81bdc652018-06-29 18:45:08 +080091
92 def testShouldNotRun(self):
93 """Test ShoudRun should raise error in non-linux os."""
94 self.Patch(platform, "system", return_value="Mac")
chojoyce1b818bb2018-10-03 16:34:57 +080095 self.assertFalse(self.AvdPkgInstaller.ShouldRun())
Sam Chiu81bdc652018-06-29 18:45:08 +080096
97
herbertxue975b8872020-02-12 14:41:41 +080098class CuttlefishCommonPkgInstallerTest(driver_test_lib.BaseDriverTest):
99 """Test CuttlefishCommonPkgInstallerTest."""
100
101 # pylint: disable=invalid-name
102 def setUp(self):
103 """Set up the test."""
104 super(CuttlefishCommonPkgInstallerTest, self).setUp()
105 self.CuttlefishCommonPkgInstaller = CuttlefishCommonPkgInstaller()
106
107 def testShouldRun(self):
108 """Test ShoudRun."""
109 self.Patch(platform, "system", return_value="Linux")
110 self.Patch(setup_common, "PackageInstalled", return_value=False)
111 self.assertTrue(self.CuttlefishCommonPkgInstaller.ShouldRun())
112
113 @mock.patch.object(shutil, "rmtree")
114 @mock.patch.object(setup_common, "CheckCmdOutput")
115 def testRun(self, mock_cmd, mock_rmtree):
116 """Test Run."""
117 fake_tmp_folder = "/tmp/cf-common"
118 self.Patch(tempfile, "mkdtemp", return_value=fake_tmp_folder)
119 self.Patch(utils, "GetUserAnswerYes", return_value="y")
120 self.Patch(CuttlefishCommonPkgInstaller, "ShouldRun", return_value=True)
121 self.CuttlefishCommonPkgInstaller.Run()
122 self.assertEqual(mock_cmd.call_count, 1)
123 mock_rmtree.assert_called_once_with(fake_tmp_folder)
124
chojoyce3c5ad5c2021-07-05 10:44:14 +0800125class MkcertPkgInstallerTest(driver_test_lib.BaseDriverTest):
126 """Test MkcertPkgInstallerTest."""
127
128 # pylint: disable=invalid-name
129 def setUp(self):
130 """Set up the test."""
131 super(MkcertPkgInstallerTest, self).setUp()
132 self.MkcertPkgInstaller = MkcertPkgInstaller()
133
134 def testShouldRun(self):
135 """Test ShouldRun."""
136 self.Patch(platform, "system", return_value="Linux")
137 self.Patch(os.path, "exists", return_value=False)
138 self.assertTrue(self.MkcertPkgInstaller.ShouldRun())
139
140 @mock.patch.object(setup_common, "CheckCmdOutput")
141 def testRun(self, mock_cmd):
142 """Test Run."""
143 self.Patch(utils, "GetUserAnswerYes", return_value="y")
144 self.Patch(MkcertPkgInstaller, "ShouldRun", return_value=True)
145 self.MkcertPkgInstaller.Run()
146 self.assertEqual(mock_cmd.call_count, 1)
147
herbertxue975b8872020-02-12 14:41:41 +0800148
Sam Chiu81bdc652018-06-29 18:45:08 +0800149if __name__ == "__main__":
150 unittest.main()