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 19 20from unittest import mock 21 22from acloud.internal.lib import driver_test_lib 23from acloud.internal.lib import utils 24from acloud.setup import setup_common 25from acloud.setup.host_setup_runner import AvdPkgInstaller 26from acloud.setup.host_setup_runner import CuttlefishCommonPkgInstaller 27from acloud.setup.host_setup_runner import CuttlefishHostSetup 28 29 30class CuttlefishHostSetupTest(driver_test_lib.BaseDriverTest): 31 """Test CuttlsfishHostSetup.""" 32 33 LSMOD_OUTPUT = """nvidia_modeset 860160 6 nvidia_drm 34module1 12312 1 35module2 12312 1 36ghash_clmulni_intel 16384 0 37aesni_intel 167936 3 38aes_x86_64 20480 1 aesni_intel 39lrw 16384 1 aesni_intel""" 40 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.""" 49 self.Patch(utils, "CheckUserInGroups", return_value=True) 50 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( 57 utils, "CheckUserInGroups", return_value=False) 58 self.Patch(CuttlefishHostSetup, "_CheckLoadedModules", return_value=True) 59 self.assertTrue(self.CuttlefishHostSetup.ShouldRun()) 60 61 # 2. Checking modules fails. 62 self.Patch(utils, "CheckUserInGroups", return_value=True) 63 self.Patch( 64 CuttlefishHostSetup, "_CheckLoadedModules", return_value=False) 65 self.assertTrue(self.CuttlefishHostSetup.ShouldRun()) 66 67 # pylint: disable=protected-access 68 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 81class AvdPkgInstallerTest(driver_test_lib.BaseDriverTest): 82 """Test AvdPkgInstallerTest.""" 83 84 # pylint: disable=invalid-name 85 def setUp(self): 86 """Set up the test.""" 87 super(AvdPkgInstallerTest, self).setUp() 88 self.AvdPkgInstaller = AvdPkgInstaller() 89 90 def testShouldNotRun(self): 91 """Test ShoudRun should raise error in non-linux os.""" 92 self.Patch(platform, "system", return_value="Mac") 93 self.assertFalse(self.AvdPkgInstaller.ShouldRun()) 94 95 96class 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 124if __name__ == "__main__": 125 unittest.main() 126