blob: bc2e8ef4d60b14d65b7087227f4dd8673ea7e945 [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
chojoyce345c2c02021-08-12 18:24:05 +080018import subprocess
herbertxue975b8872020-02-12 14:41:41 +080019import tempfile
Sam Chiu81bdc652018-06-29 18:45:08 +080020import unittest
chojoyce98f07c02021-03-17 21:47:38 +080021
22from unittest import mock
Sam Chiu81bdc652018-06-29 18:45:08 +080023
24from acloud.internal.lib import driver_test_lib
herbertxue07293a32018-11-05 20:40:11 +080025from acloud.internal.lib import utils
Sam Chiu81bdc652018-06-29 18:45:08 +080026from acloud.setup import setup_common
chojoyce1b818bb2018-10-03 16:34:57 +080027from acloud.setup.host_setup_runner import AvdPkgInstaller
herbertxue975b8872020-02-12 14:41:41 +080028from acloud.setup.host_setup_runner import CuttlefishCommonPkgInstaller
29from acloud.setup.host_setup_runner import CuttlefishHostSetup
chojoyce3c5ad5c2021-07-05 10:44:14 +080030from acloud.setup.host_setup_runner import MkcertPkgInstaller
Sam Chiu81bdc652018-06-29 18:45:08 +080031
32
33class CuttlefishHostSetupTest(driver_test_lib.BaseDriverTest):
34 """Test CuttlsfishHostSetup."""
35
Sam Chiub6bf79f2020-06-09 20:47:42 +080036 LSMOD_OUTPUT = """nvidia_modeset 860160 6 nvidia_drm
Sam Chiu81bdc652018-06-29 18:45:08 +080037module1 12312 1
38module2 12312 1
39ghash_clmulni_intel 16384 0
40aesni_intel 167936 3
41aes_x86_64 20480 1 aesni_intel
Sam Chiub6bf79f2020-06-09 20:47:42 +080042lrw 16384 1 aesni_intel"""
Sam Chiu81bdc652018-06-29 18:45:08 +080043
44 # pylint: disable=invalid-name
45 def setUp(self):
46 """Set up the test."""
herbertxued9809d12021-08-04 14:53:33 +080047 super().setUp()
Sam Chiu81bdc652018-06-29 18:45:08 +080048 self.CuttlefishHostSetup = CuttlefishHostSetup()
49
50 def testShouldRunFalse(self):
51 """Test ShouldRun returns False."""
chojoyce345c2c02021-08-12 18:24:05 +080052 self.Patch(CuttlefishHostSetup, "_IsSupportedKvm", return_value=True)
herbertxue07293a32018-11-05 20:40:11 +080053 self.Patch(utils, "CheckUserInGroups", return_value=True)
Sam Chiu81bdc652018-06-29 18:45:08 +080054 self.Patch(CuttlefishHostSetup, "_CheckLoadedModules", return_value=True)
55 self.assertFalse(self.CuttlefishHostSetup.ShouldRun())
56
57 def testShouldRunTrue(self):
58 """Test ShouldRun returns True."""
chojoyce345c2c02021-08-12 18:24:05 +080059 self.Patch(CuttlefishHostSetup, "_IsSupportedKvm", return_value=True)
Sam Chiu81bdc652018-06-29 18:45:08 +080060 # 1. Checking groups fails.
61 self.Patch(
herbertxue07293a32018-11-05 20:40:11 +080062 utils, "CheckUserInGroups", return_value=False)
Sam Chiu81bdc652018-06-29 18:45:08 +080063 self.Patch(CuttlefishHostSetup, "_CheckLoadedModules", return_value=True)
64 self.assertTrue(self.CuttlefishHostSetup.ShouldRun())
65
66 # 2. Checking modules fails.
herbertxue07293a32018-11-05 20:40:11 +080067 self.Patch(utils, "CheckUserInGroups", return_value=True)
Sam Chiu81bdc652018-06-29 18:45:08 +080068 self.Patch(
69 CuttlefishHostSetup, "_CheckLoadedModules", return_value=False)
70 self.assertTrue(self.CuttlefishHostSetup.ShouldRun())
71
72 # pylint: disable=protected-access
chojoyce345c2c02021-08-12 18:24:05 +080073 def testIsSupportedKvm(self):
74 """Test _IsSupportedKvm."""
75 fake_success_message = (
76 " QEMU: Checking for hardware virtualization : PASS\n"
77 " QEMU: Checking if device /dev/kvm exists : PASS\n"
78 " QEMU: Checking if device /dev/kvm is accessible : PASS\n")
79 fake_fail_message = (
80 " QEMU: Checking for hardware virtualization : FAIL"
81 "(Only emulated CPUs are available, performance will be significantly limited)\n"
82 " QEMU: Checking if device /dev/vhost-net exists : PASS\n"
83 " QEMU: Checking if device /dev/net/tun exists : PASS\n")
84 popen = mock.Mock(returncode=None)
85 popen.poll.return_value = None
86 popen.communicate.return_value = (fake_success_message, "stderr")
87 self.Patch(subprocess, "Popen", return_value=popen)
88 self.assertTrue(self.CuttlefishHostSetup._IsSupportedKvm())
89
90 popen.communicate.return_value = (fake_fail_message, "stderr")
91 self.Patch(subprocess, "Popen", return_value=popen)
92 self.assertFalse(self.CuttlefishHostSetup._IsSupportedKvm())
93
94 # pylint: disable=protected-access
Sam Chiu81bdc652018-06-29 18:45:08 +080095 def testCheckLoadedModules(self):
96 """Test _CheckLoadedModules."""
97 self.Patch(
98 setup_common, "CheckCmdOutput", return_value=self.LSMOD_OUTPUT)
99
100 # Required modules are all in lsmod should return True.
101 self.assertTrue(
102 self.CuttlefishHostSetup._CheckLoadedModules(["module1", "module2"]))
103 # Required modules are not all in lsmod should return False.
104 self.assertFalse(
105 self.CuttlefishHostSetup._CheckLoadedModules(["module1", "module3"]))
106
107
chojoyce1b818bb2018-10-03 16:34:57 +0800108class AvdPkgInstallerTest(driver_test_lib.BaseDriverTest):
109 """Test AvdPkgInstallerTest."""
Sam Chiu81bdc652018-06-29 18:45:08 +0800110
111 # pylint: disable=invalid-name
112 def setUp(self):
113 """Set up the test."""
herbertxued9809d12021-08-04 14:53:33 +0800114 super().setUp()
chojoyce1b818bb2018-10-03 16:34:57 +0800115 self.AvdPkgInstaller = AvdPkgInstaller()
Sam Chiu81bdc652018-06-29 18:45:08 +0800116
117 def testShouldNotRun(self):
118 """Test ShoudRun should raise error in non-linux os."""
119 self.Patch(platform, "system", return_value="Mac")
chojoyce1b818bb2018-10-03 16:34:57 +0800120 self.assertFalse(self.AvdPkgInstaller.ShouldRun())
Sam Chiu81bdc652018-06-29 18:45:08 +0800121
122
herbertxue975b8872020-02-12 14:41:41 +0800123class CuttlefishCommonPkgInstallerTest(driver_test_lib.BaseDriverTest):
124 """Test CuttlefishCommonPkgInstallerTest."""
125
126 # pylint: disable=invalid-name
127 def setUp(self):
128 """Set up the test."""
herbertxued9809d12021-08-04 14:53:33 +0800129 super().setUp()
herbertxue975b8872020-02-12 14:41:41 +0800130 self.CuttlefishCommonPkgInstaller = CuttlefishCommonPkgInstaller()
131
132 def testShouldRun(self):
133 """Test ShoudRun."""
134 self.Patch(platform, "system", return_value="Linux")
135 self.Patch(setup_common, "PackageInstalled", return_value=False)
136 self.assertTrue(self.CuttlefishCommonPkgInstaller.ShouldRun())
137
138 @mock.patch.object(shutil, "rmtree")
139 @mock.patch.object(setup_common, "CheckCmdOutput")
140 def testRun(self, mock_cmd, mock_rmtree):
141 """Test Run."""
142 fake_tmp_folder = "/tmp/cf-common"
143 self.Patch(tempfile, "mkdtemp", return_value=fake_tmp_folder)
144 self.Patch(utils, "GetUserAnswerYes", return_value="y")
145 self.Patch(CuttlefishCommonPkgInstaller, "ShouldRun", return_value=True)
146 self.CuttlefishCommonPkgInstaller.Run()
147 self.assertEqual(mock_cmd.call_count, 1)
148 mock_rmtree.assert_called_once_with(fake_tmp_folder)
149
chojoyce3c5ad5c2021-07-05 10:44:14 +0800150class MkcertPkgInstallerTest(driver_test_lib.BaseDriverTest):
151 """Test MkcertPkgInstallerTest."""
152
153 # pylint: disable=invalid-name
154 def setUp(self):
155 """Set up the test."""
herbertxued9809d12021-08-04 14:53:33 +0800156 super().setUp()
chojoyce3c5ad5c2021-07-05 10:44:14 +0800157 self.MkcertPkgInstaller = MkcertPkgInstaller()
158
159 def testShouldRun(self):
160 """Test ShouldRun."""
161 self.Patch(platform, "system", return_value="Linux")
162 self.Patch(os.path, "exists", return_value=False)
163 self.assertTrue(self.MkcertPkgInstaller.ShouldRun())
164
165 @mock.patch.object(setup_common, "CheckCmdOutput")
166 def testRun(self, mock_cmd):
167 """Test Run."""
168 self.Patch(utils, "GetUserAnswerYes", return_value="y")
169 self.Patch(MkcertPkgInstaller, "ShouldRun", return_value=True)
chojoycec6338802021-07-21 16:55:02 +0800170 self.Patch(os, "mkdir")
171 self.Patch(utils, "SetExecutable")
172 self.Patch(utils, "CheckOutput")
chojoyce3c5ad5c2021-07-05 10:44:14 +0800173 self.MkcertPkgInstaller.Run()
174 self.assertEqual(mock_cmd.call_count, 1)
175
herbertxue975b8872020-02-12 14:41:41 +0800176
Sam Chiu81bdc652018-06-29 18:45:08 +0800177if __name__ == "__main__":
178 unittest.main()