• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.internal.lib import utils
21from acloud.setup import setup_common
22
23
24class SetupCommonTest(driver_test_lib.BaseDriverTest):
25    """Test HostPkgTaskRunner."""
26    PKG_INFO_INSTALLED = """fake_pkg:
27  Installed: 0.7
28  Candidate: 0.7
29  Version table:
30"""
31    PKG_INFO_NONE_INSTALL = """fake_pkg:
32  Installed: (none)
33  Candidate: 0.7
34  Version table:
35"""
36    PKG_INFO_OLD_VERSION = """fake_pkg:
37  Installed: 0.2
38  Candidate: 0.7
39  Version table:
40"""
41
42    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
66    # 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
77        # 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
85    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
97    def testPackageInstalledForOldVersion(self):
98        """Test PackageInstalled should return True when pkg is out-of-date."""
99        self.Patch(
100            setup_common,
101            "CheckCmdOutput",
102            return_value=self.PKG_INFO_OLD_VERSION)
103
104        self.assertTrue(setup_common.PackageInstalled("fake_package",
105                                                      compare_version=True))
106
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()
119