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 os 16import platform 17import shutil 18import sys 19import tempfile 20import unittest 21 22from unittest import mock 23 24from acloud.internal.lib import driver_test_lib 25from acloud.internal.lib import utils 26from acloud.setup import mkcert 27from acloud.setup import setup_common 28from acloud.setup.host_setup_runner import AvdPkgInstaller 29from acloud.setup.host_setup_runner import CuttlefishCommonPkgInstaller 30from acloud.setup.host_setup_runner import CuttlefishHostSetup 31from acloud.setup.host_setup_runner import LocalCAHostSetup 32 33class CuttlefishHostSetupTest(driver_test_lib.BaseDriverTest): 34 """Test CuttlsfishHostSetup.""" 35 36 LSMOD_OUTPUT = """nvidia_modeset 860160 6 nvidia_drm 37module1 12312 1 38module2 12312 1 39ghash_clmulni_intel 16384 0 40aesni_intel 167936 3 41aes_x86_64 20480 1 aesni_intel 42lrw 16384 1 aesni_intel""" 43 44 # pylint: disable=invalid-name 45 def setUp(self): 46 """Set up the test.""" 47 super().setUp() 48 self.CuttlefishHostSetup = CuttlefishHostSetup() 49 50 def testShouldRunFalse(self): 51 """Test ShouldRun returns False.""" 52 self.Patch(utils, "CheckUserInGroups", return_value=True) 53 self.Patch(CuttlefishHostSetup, "_CheckLoadedModules", return_value=True) 54 self.assertFalse(self.CuttlefishHostSetup.ShouldRun()) 55 56 def testShouldRunTrue(self): 57 """Test ShouldRun returns True.""" 58 # 1. Checking groups fails. 59 self.Patch( 60 utils, "CheckUserInGroups", return_value=False) 61 self.Patch(CuttlefishHostSetup, "_CheckLoadedModules", return_value=True) 62 self.assertTrue(self.CuttlefishHostSetup.ShouldRun()) 63 64 # 2. Checking modules fails. 65 self.Patch(utils, "CheckUserInGroups", return_value=True) 66 self.Patch( 67 CuttlefishHostSetup, "_CheckLoadedModules", return_value=False) 68 self.assertTrue(self.CuttlefishHostSetup.ShouldRun()) 69 70 self.Patch(platform, "system", return_value="Mac") 71 self.assertFalse(self.CuttlefishHostSetup.ShouldRun()) 72 73 # pylint: disable=no-member 74 def testRun(self): 75 """Test Run.""" 76 self.Patch(CuttlefishHostSetup, "ShouldRun", return_value=True) 77 self.Patch(CuttlefishHostSetup, "_GetProcessorType", 78 return_value="intel") 79 self.Patch(utils, "InteractWithQuestion", return_value="y") 80 self.Patch(setup_common, "CheckCmdOutput") 81 self.CuttlefishHostSetup.Run() 82 setup_common.CheckCmdOutput.assert_called() 83 setup_common.CheckCmdOutput.reset_mock() 84 85 self.Patch(utils, "InteractWithQuestion", return_value="n") 86 self.CuttlefishHostSetup.Run() 87 setup_common.CheckCmdOutput.assert_not_called() 88 89 # pylint: disable=protected-access 90 def testCheckLoadedModules(self): 91 """Test _CheckLoadedModules.""" 92 self.Patch( 93 setup_common, "CheckCmdOutput", return_value=self.LSMOD_OUTPUT) 94 95 # Required modules are all in lsmod should return True. 96 self.assertTrue( 97 self.CuttlefishHostSetup._CheckLoadedModules(["module1", "module2"])) 98 # Required modules are not all in lsmod should return False. 99 self.assertFalse( 100 self.CuttlefishHostSetup._CheckLoadedModules(["module1", "module3"])) 101 102 103class AvdPkgInstallerTest(driver_test_lib.BaseDriverTest): 104 """Test AvdPkgInstallerTest.""" 105 106 # pylint: disable=invalid-name 107 def setUp(self): 108 """Set up the test.""" 109 super().setUp() 110 self.AvdPkgInstaller = AvdPkgInstaller() 111 112 def testShouldRun(self): 113 """Test ShouldRun.""" 114 self.Patch(platform, "system", return_value="Linux") 115 self.Patch(platform, "version", return_value="Unsupport") 116 self.assertFalse(self.AvdPkgInstaller.ShouldRun()) 117 118 def testShouldNotRun(self): 119 """Test ShouldRun should raise error in non-linux os.""" 120 self.Patch(platform, "system", return_value="Mac") 121 self.assertFalse(self.AvdPkgInstaller.ShouldRun()) 122 123 # pylint: disable=no-member 124 def testRun(self): 125 """Test Run.""" 126 self.Patch(platform, "system", return_value="Linux") 127 self.AvdPkgInstaller.PACKAGES = ["pkg1", "pkg2"] 128 self.Patch(setup_common, "PackageInstalled", return_value=False) 129 self.Patch(utils, "GetUserAnswerYes", return_value=True) 130 self.Patch(setup_common, "CheckCmdOutput") 131 self.Patch(setup_common, "InstallPackage") 132 self.AvdPkgInstaller.Run() 133 setup_common.InstallPackage.assert_called() 134 135 self.Patch(utils, "GetUserAnswerYes", return_value=False) 136 self.Patch(sys, "exit") 137 self.AvdPkgInstaller.Run() 138 sys.exit.assert_called_once() 139 140 141class CuttlefishCommonPkgInstallerTest(driver_test_lib.BaseDriverTest): 142 """Test CuttlefishCommonPkgInstallerTest.""" 143 144 # pylint: disable=invalid-name 145 def setUp(self): 146 """Set up the test.""" 147 super().setUp() 148 self.CuttlefishCommonPkgInstaller = CuttlefishCommonPkgInstaller() 149 150 def testShouldRun(self): 151 """Test ShouldRun.""" 152 self.Patch(platform, "system", return_value="Linux") 153 self.Patch(setup_common, "PackageInstalled", return_value=False) 154 self.assertTrue(self.CuttlefishCommonPkgInstaller.ShouldRun()) 155 156 self.Patch(setup_common, "PackageInstalled", return_value=True) 157 self.assertFalse(self.CuttlefishCommonPkgInstaller.ShouldRun()) 158 159 self.Patch(platform, "system", return_value="Mac") 160 self.assertFalse(self.CuttlefishCommonPkgInstaller.ShouldRun()) 161 162 # pylint: disable=no-member 163 @mock.patch.object(shutil, "rmtree") 164 @mock.patch.object(setup_common, "CheckCmdOutput") 165 def testRun(self, mock_cmd, mock_rmtree): 166 """Test Run.""" 167 fake_tmp_folder = "/tmp/cf-common" 168 self.Patch(tempfile, "mkdtemp", return_value=fake_tmp_folder) 169 self.Patch(utils, "GetUserAnswerYes", return_value=True) 170 self.Patch(CuttlefishCommonPkgInstaller, "ShouldRun", return_value=True) 171 self.Patch(setup_common, "IsPackageInAptList", return_value=False) 172 self.CuttlefishCommonPkgInstaller.Run() 173 self.assertEqual(mock_cmd.call_count, 1) 174 mock_rmtree.assert_called_once_with(fake_tmp_folder) 175 # Install cuttlefish-common from rapture 176 self.Patch(setup_common, "IsPackageInAptList", return_value=True) 177 self.Patch(setup_common, "InstallPackage") 178 self.CuttlefishCommonPkgInstaller.Run() 179 setup_common.InstallPackage.assert_called() 180 181 self.Patch(utils, "GetUserAnswerYes", return_value=False) 182 self.Patch(sys, "exit") 183 self.CuttlefishCommonPkgInstaller.Run() 184 sys.exit.assert_called_once() 185 186 187class LocalCAHostSetupTest(driver_test_lib.BaseDriverTest): 188 """Test LocalCAHostSetupTest.""" 189 190 # pylint: disable=invalid-name 191 def setUp(self): 192 """Set up the test.""" 193 super().setUp() 194 self.LocalCAHostSetup = LocalCAHostSetup() 195 196 def testShouldRun(self): 197 """Test ShouldRun.""" 198 self.Patch(platform, "system", return_value="Linux") 199 self.Patch(os.path, "exists", return_value=False) 200 self.assertTrue(self.LocalCAHostSetup.ShouldRun()) 201 202 self.Patch(os.path, "exists", return_value=True) 203 self.assertFalse(self.LocalCAHostSetup.ShouldRun()) 204 205 self.Patch(platform, "system", return_value="Mac") 206 self.Patch(os.path, "exists", return_value=False) 207 self.assertFalse(self.LocalCAHostSetup.ShouldRun()) 208 209 # pylint: disable=no-member 210 def testRun(self): 211 """Test Run.""" 212 self.Patch(utils, "GetUserAnswerYes", return_value=True) 213 self.Patch(LocalCAHostSetup, "ShouldRun", return_value=True) 214 self.Patch(mkcert, "Install") 215 self.LocalCAHostSetup.Run() 216 mkcert.Install.assert_called_once() 217 mkcert.Install.reset_mock() 218 219 self.Patch(LocalCAHostSetup, "ShouldRun", return_value=False) 220 self.LocalCAHostSetup.Run() 221 mkcert.Install.assert_not_called() 222 223 self.Patch(utils, "GetUserAnswerYes", return_value=False) 224 self.Patch(sys, "exit") 225 self.LocalCAHostSetup.Run() 226 mkcert.Install.assert_not_called() 227 228 229if __name__ == "__main__": 230 unittest.main() 231