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 subprocess
16import unittest
17
18from acloud import errors
19from acloud.internal.lib import driver_test_lib
20from acloud.setup import setup_common
21
22
23class SetupCommonTest(driver_test_lib.BaseDriverTest):
24    """Test HostPkgTaskRunner."""
25    PKG_INFO_INSTALLED = """fake_pkg:
26  Installed: 0.7
27  Candidate: 0.7
28  Version table:
29"""
30    PKG_INFO_NONE_INSTALL = """fake_pkg:
31  Installed: (none)
32  Candidate: 0.7
33  Version table:
34"""
35    PKG_INFO_OLD_VERSION = """fake_pkg:
36  Installed: 0.2
37  Candidate: 0.7
38  Version table:
39"""
40
41    # pylint: disable=invalid-name
42    def testPackageNotInstalled(self):
43        """"Test PackageInstalled return False when Installed status is (None). """
44        self.Patch(
45            setup_common,
46            "CheckCmdOutput",
47            return_value=self.PKG_INFO_NONE_INSTALL)
48
49        self.assertFalse(
50            setup_common.PackageInstalled("fake_package"))
51
52    def testUnableToLocatePackage(self):
53        """"Test PackageInstalled return False if unable to locate package."""
54        self.Patch(
55            setup_common,
56            "CheckCmdOutput",
57            side_effect=subprocess.CalledProcessError(
58                None, "This error means unable to locate package on repository."))
59
60        with self.assertRaises(errors.UnableToLocatePkgOnRepositoryError):
61            setup_common.PackageInstalled("fake_package")
62
63    # pylint: disable=invalid-name
64    def testPackageInstalledForOldVersion(self):
65        """Test PackageInstalled should return True when pkg is out-of-date."""
66        self.Patch(
67            setup_common,
68            "CheckCmdOutput",
69            return_value=self.PKG_INFO_OLD_VERSION)
70
71        self.assertTrue(setup_common.PackageInstalled("fake_package",
72                                                      compare_version=True))
73
74    def testPackageInstalled(self):
75        """Test PackageInstalled should return True when pkg is installed."""
76        self.Patch(
77            setup_common,
78            "CheckCmdOutput",
79            return_value=self.PKG_INFO_INSTALLED)
80
81        self.assertTrue(setup_common.PackageInstalled("fake_package"))
82
83
84if __name__ == "__main__":
85    unittest.main()
86