1# Copyright 2021 - 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_cleanup_runner.""" 15import os 16import platform 17import unittest 18import subprocess 19 20from unittest import mock 21 22from acloud.hostcleanup import host_cleanup_runner 23from acloud.hostcleanup.host_cleanup_runner import PackagesUninstaller 24from acloud.internal.lib import driver_test_lib 25from acloud.internal.lib import utils 26from acloud.setup import setup_common 27 28# pylint: disable=no-member 29class PackagesUninstallerTest(driver_test_lib.BaseDriverTest): 30 """Test PackagesUninstallerTest.""" 31 32 # pylint: disable=invalid-name 33 def setUp(self): 34 """Set up the test.""" 35 super().setUp() 36 mock_stty = mock.MagicMock() 37 mock_stty.read.return_value = "20 80" 38 self.Patch(os, "popen", return_value=mock_stty) 39 40 self.Patch(setup_common, "PackageInstalled", return_value=True) 41 self.PackagesUninstaller = PackagesUninstaller() 42 43 def testShouldRun(self): 44 """Test ShouldRun.""" 45 self.Patch(platform, "system", return_value="Linux") 46 self.assertTrue(self.PackagesUninstaller.ShouldRun()) 47 48 self.Patch(platform, "system", return_value="Mac") 49 self.assertFalse(self.PackagesUninstaller.ShouldRun()) 50 51 self.Patch(platform, "system", return_value="Linux") 52 self.Patch(setup_common, "PackageInstalled", return_value=False) 53 self.PackagesUninstaller = PackagesUninstaller() 54 self.assertFalse(self.PackagesUninstaller.ShouldRun()) 55 56 def testRun(self): 57 """Test Run.""" 58 self.Patch(utils, "InteractWithQuestion", return_value="y") 59 self.Patch(PackagesUninstaller, "ShouldRun", return_value=True) 60 self.Patch(setup_common, "CheckCmdOutput") 61 self.PackagesUninstaller.Run() 62 setup_common.CheckCmdOutput.assert_called() 63 setup_common.CheckCmdOutput.reset_mock() 64 65 self.Patch(utils, "InteractWithQuestion", return_value="n") 66 self.PackagesUninstaller.Run() 67 setup_common.CheckCmdOutput.assert_not_called() 68 69 def testRun_RaiseException(self): 70 """Test raise exception.""" 71 self.Patch(utils, "InteractWithQuestion", return_value="y") 72 self.Patch(PackagesUninstaller, "ShouldRun", return_value=True) 73 self.Patch(subprocess, "check_output", 74 side_effect=subprocess.CalledProcessError( 75 None, "raise err.")) 76 self.Patch(host_cleanup_runner.logger, "error") 77 self.PackagesUninstaller.Run() 78 host_cleanup_runner.logger.error.assert_called() 79 80 81if __name__ == "__main__": 82 unittest.main() 83