blob: e17e529d3ca9ba7ca6b7727ce3c450973a4182c8 [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
chojoyce8cec5002019-11-04 18:15:09 +080017from six import b
Sam Chiu81bdc652018-06-29 18:45:08 +080018
19from acloud import errors
20from acloud.internal.lib import driver_test_lib
21from acloud.setup import setup_common
22
23
24class SetupCommonTest(driver_test_lib.BaseDriverTest):
25 """Test HostPkgTaskRunner."""
chojoyce8cec5002019-11-04 18:15:09 +080026 PKG_INFO_INSTALLED = b("""fake_pkg:
Sam Chiu81bdc652018-06-29 18:45:08 +080027 Installed: 0.7
28 Candidate: 0.7
29 Version table:
chojoyce8cec5002019-11-04 18:15:09 +080030""")
31 PKG_INFO_NONE_INSTALL = b("""fake_pkg:
Sam Chiu81bdc652018-06-29 18:45:08 +080032 Installed: (none)
33 Candidate: 0.7
34 Version table:
chojoyce8cec5002019-11-04 18:15:09 +080035""")
36 PKG_INFO_OLD_VERSION = b("""fake_pkg:
Sam Chiu81bdc652018-06-29 18:45:08 +080037 Installed: 0.2
38 Candidate: 0.7
39 Version table:
chojoyce8cec5002019-11-04 18:15:09 +080040""")
Sam Chiu81bdc652018-06-29 18:45:08 +080041
42 # pylint: disable=invalid-name
43 def testPackageNotInstalled(self):
44 """"Test PackageInstalled return False when Installed status is (None). """
45 self.Patch(
46 setup_common,
47 "CheckCmdOutput",
48 return_value=self.PKG_INFO_NONE_INSTALL)
49
50 self.assertFalse(
51 setup_common.PackageInstalled("fake_package"))
52
53 def testUnableToLocatePackage(self):
54 """"Test PackageInstalled return False if unable to locate package."""
55 self.Patch(
56 setup_common,
57 "CheckCmdOutput",
58 side_effect=subprocess.CalledProcessError(
59 None, "This error means unable to locate package on repository."))
60
61 with self.assertRaises(errors.UnableToLocatePkgOnRepositoryError):
62 setup_common.PackageInstalled("fake_package")
63
64 # pylint: disable=invalid-name
Sam Chiu404c6032020-01-22 11:43:56 +080065 def testPackageInstalledForOldVersion(self):
66 """Test PackageInstalled should return True when pkg is out-of-date."""
Sam Chiu81bdc652018-06-29 18:45:08 +080067 self.Patch(
68 setup_common,
69 "CheckCmdOutput",
70 return_value=self.PKG_INFO_OLD_VERSION)
71
Sam Chiu404c6032020-01-22 11:43:56 +080072 self.assertTrue(setup_common.PackageInstalled("fake_package",
73 compare_version=True))
Sam Chiu81bdc652018-06-29 18:45:08 +080074
75 def testPackageInstalled(self):
76 """Test PackageInstalled should return True when pkg is installed."""
77 self.Patch(
78 setup_common,
79 "CheckCmdOutput",
80 return_value=self.PKG_INFO_INSTALLED)
81
82 self.assertTrue(setup_common.PackageInstalled("fake_package"))
83
84
85if __name__ == "__main__":
86 unittest.main()