blob: 1f470a744040c5ab5be91eb33f51bf2595ffcd7c [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."""
15import subprocess
16import unittest
17
18from acloud import errors
19from acloud.internal.lib import driver_test_lib
herbertxuef1188f62021-10-29 17:33:22 +080020from acloud.internal.lib import utils
Sam Chiu81bdc652018-06-29 18:45:08 +080021from acloud.setup import setup_common
22
23
24class SetupCommonTest(driver_test_lib.BaseDriverTest):
25 """Test HostPkgTaskRunner."""
Sam Chiub6bf79f2020-06-09 20:47:42 +080026 PKG_INFO_INSTALLED = """fake_pkg:
Sam Chiu81bdc652018-06-29 18:45:08 +080027 Installed: 0.7
28 Candidate: 0.7
29 Version table:
Sam Chiub6bf79f2020-06-09 20:47:42 +080030"""
31 PKG_INFO_NONE_INSTALL = """fake_pkg:
Sam Chiu81bdc652018-06-29 18:45:08 +080032 Installed: (none)
33 Candidate: 0.7
34 Version table:
Sam Chiub6bf79f2020-06-09 20:47:42 +080035"""
36 PKG_INFO_OLD_VERSION = """fake_pkg:
Sam Chiu81bdc652018-06-29 18:45:08 +080037 Installed: 0.2
38 Candidate: 0.7
39 Version table:
Sam Chiub6bf79f2020-06-09 20:47:42 +080040"""
Sam Chiu81bdc652018-06-29 18:45:08 +080041
herbertxuef1188f62021-10-29 17:33:22 +080042 def setUp(self):
43 """Create mock objects."""
44 super().setUp()
45 self._mock_checkoutput = self.Patch(utils, "CheckOutput")
46
47 def testCheckCmdOutput(self):
48 """Test CheckCmdOutput."""
49 cmd = "fake_command"
50 setup_common.CheckCmdOutput(cmd)
51 self._mock_checkoutput.assert_called_once_with(cmd)
52
53 def testInstallPackage(self):
54 """Test InstallPackage."""
55 package = "fake_pkg"
56 self.Patch(setup_common, "PackageInstalled", return_value=True)
57 setup_common.InstallPackage(package)
58 self._mock_checkoutput.assert_called_once_with(
59 "sudo apt-get --assume-yes install fake_pkg",
60 shell=True, stderr=subprocess.STDOUT)
61
62 self.Patch(setup_common, "PackageInstalled", return_value=False)
63 with self.assertRaises(errors.PackageInstallError):
64 setup_common.InstallPackage(package)
65
Sam Chiu81bdc652018-06-29 18:45:08 +080066 # pylint: disable=invalid-name
67 def testPackageNotInstalled(self):
68 """"Test PackageInstalled return False when Installed status is (None). """
69 self.Patch(
70 setup_common,
71 "CheckCmdOutput",
72 return_value=self.PKG_INFO_NONE_INSTALL)
73
74 self.assertFalse(
75 setup_common.PackageInstalled("fake_package"))
76
herbertxuef1188f62021-10-29 17:33:22 +080077 # Test with the package didn't install in host.
78 self.Patch(
79 setup_common,
80 "CheckCmdOutput",
81 return_value="")
82 self.assertFalse(
83 setup_common.PackageInstalled("fake_package"))
84
Sam Chiu81bdc652018-06-29 18:45:08 +080085 def testUnableToLocatePackage(self):
86 """"Test PackageInstalled return False if unable to locate package."""
87 self.Patch(
88 setup_common,
89 "CheckCmdOutput",
90 side_effect=subprocess.CalledProcessError(
91 None, "This error means unable to locate package on repository."))
92
93 with self.assertRaises(errors.UnableToLocatePkgOnRepositoryError):
94 setup_common.PackageInstalled("fake_package")
95
96 # pylint: disable=invalid-name
Sam Chiu404c6032020-01-22 11:43:56 +080097 def testPackageInstalledForOldVersion(self):
98 """Test PackageInstalled should return True when pkg is out-of-date."""
Sam Chiu81bdc652018-06-29 18:45:08 +080099 self.Patch(
100 setup_common,
101 "CheckCmdOutput",
102 return_value=self.PKG_INFO_OLD_VERSION)
103
Sam Chiu404c6032020-01-22 11:43:56 +0800104 self.assertTrue(setup_common.PackageInstalled("fake_package",
105 compare_version=True))
Sam Chiu81bdc652018-06-29 18:45:08 +0800106
107 def testPackageInstalled(self):
108 """Test PackageInstalled should return True when pkg is installed."""
109 self.Patch(
110 setup_common,
111 "CheckCmdOutput",
112 return_value=self.PKG_INFO_INSTALLED)
113
114 self.assertTrue(setup_common.PackageInstalled("fake_package"))
115
116
117if __name__ == "__main__":
118 unittest.main()