| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 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.""" |
| 15 | import subprocess |
| 16 | import unittest |
| 17 | |
| 18 | from acloud import errors |
| 19 | from acloud.internal.lib import driver_test_lib |
| herbertxue | f1188f6 | 2021-10-29 17:33:22 +0800 | [diff] [blame] | 20 | from acloud.internal.lib import utils |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 21 | from acloud.setup import setup_common |
| 22 | |
| 23 | |
| 24 | class SetupCommonTest(driver_test_lib.BaseDriverTest): |
| 25 | """Test HostPkgTaskRunner.""" |
| Sam Chiu | b6bf79f | 2020-06-09 20:47:42 +0800 | [diff] [blame] | 26 | PKG_INFO_INSTALLED = """fake_pkg: |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 27 | Installed: 0.7 |
| 28 | Candidate: 0.7 |
| 29 | Version table: |
| Sam Chiu | b6bf79f | 2020-06-09 20:47:42 +0800 | [diff] [blame] | 30 | """ |
| 31 | PKG_INFO_NONE_INSTALL = """fake_pkg: |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 32 | Installed: (none) |
| 33 | Candidate: 0.7 |
| 34 | Version table: |
| Sam Chiu | b6bf79f | 2020-06-09 20:47:42 +0800 | [diff] [blame] | 35 | """ |
| 36 | PKG_INFO_OLD_VERSION = """fake_pkg: |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 37 | Installed: 0.2 |
| 38 | Candidate: 0.7 |
| 39 | Version table: |
| Sam Chiu | b6bf79f | 2020-06-09 20:47:42 +0800 | [diff] [blame] | 40 | """ |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 41 | |
| herbertxue | f1188f6 | 2021-10-29 17:33:22 +0800 | [diff] [blame] | 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 | |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 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 | |
| herbertxue | f1188f6 | 2021-10-29 17:33:22 +0800 | [diff] [blame] | 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 | |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 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 |
| Sam Chiu | 404c603 | 2020-01-22 11:43:56 +0800 | [diff] [blame] | 97 | def testPackageInstalledForOldVersion(self): |
| 98 | """Test PackageInstalled should return True when pkg is out-of-date.""" |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 99 | self.Patch( |
| 100 | setup_common, |
| 101 | "CheckCmdOutput", |
| 102 | return_value=self.PKG_INFO_OLD_VERSION) |
| 103 | |
| Sam Chiu | 404c603 | 2020-01-22 11:43:56 +0800 | [diff] [blame] | 104 | self.assertTrue(setup_common.PackageInstalled("fake_package", |
| 105 | compare_version=True)) |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 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 | |
| 117 | if __name__ == "__main__": |
| 118 | unittest.main() |