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 shutil 17import tempfile 18import unittest 19import mock 20 21from acloud.internal.lib import driver_test_lib 22from acloud.internal.lib import utils 23from acloud.setup import setup_common 24from acloud.setup.host_setup_runner import AvdPkgInstaller 25from acloud.setup.host_setup_runner import CuttlefishCommonPkgInstaller 26from acloud.setup.host_setup_runner import CuttlefishHostSetup 27 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.""" 48 self.Patch(utils, "CheckUserInGroups", return_value=True) 49 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( 56 utils, "CheckUserInGroups", return_value=False) 57 self.Patch(CuttlefishHostSetup, "_CheckLoadedModules", return_value=True) 58 self.assertTrue(self.CuttlefishHostSetup.ShouldRun()) 59 60 # 2. Checking modules fails. 61 self.Patch(utils, "CheckUserInGroups", return_value=True) 62 self.Patch( 63 CuttlefishHostSetup, "_CheckLoadedModules", return_value=False) 64 self.assertTrue(self.CuttlefishHostSetup.ShouldRun()) 65 66 # pylint: disable=protected-access 67 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 80class AvdPkgInstallerTest(driver_test_lib.BaseDriverTest): 81 """Test AvdPkgInstallerTest.""" 82 83 # pylint: disable=invalid-name 84 def setUp(self): 85 """Set up the test.""" 86 super(AvdPkgInstallerTest, self).setUp() 87 self.AvdPkgInstaller = AvdPkgInstaller() 88 89 def testShouldNotRun(self): 90 """Test ShoudRun should raise error in non-linux os.""" 91 self.Patch(platform, "system", return_value="Mac") 92 self.assertFalse(self.AvdPkgInstaller.ShouldRun()) 93 94 95class 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 123if __name__ == "__main__": 124 unittest.main() 125