| 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 |
| chojoyce | 8cec500 | 2019-11-04 18:15:09 +0800 | [diff] [blame^] | 17 | from six import b |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 18 | |
| 19 | from acloud import errors |
| 20 | from acloud.internal.lib import driver_test_lib |
| 21 | from acloud.setup import setup_common |
| 22 | |
| 23 | |
| 24 | class SetupCommonTest(driver_test_lib.BaseDriverTest): |
| 25 | """Test HostPkgTaskRunner.""" |
| chojoyce | 8cec500 | 2019-11-04 18:15:09 +0800 | [diff] [blame^] | 26 | PKG_INFO_INSTALLED = b("""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: |
| chojoyce | 8cec500 | 2019-11-04 18:15:09 +0800 | [diff] [blame^] | 30 | """) |
| 31 | PKG_INFO_NONE_INSTALL = b("""fake_pkg: |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 32 | Installed: (none) |
| 33 | Candidate: 0.7 |
| 34 | Version table: |
| chojoyce | 8cec500 | 2019-11-04 18:15:09 +0800 | [diff] [blame^] | 35 | """) |
| 36 | PKG_INFO_OLD_VERSION = b("""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: |
| chojoyce | 8cec500 | 2019-11-04 18:15:09 +0800 | [diff] [blame^] | 40 | """) |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 41 | |
| 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 Chiu | 404c603 | 2020-01-22 11:43:56 +0800 | [diff] [blame] | 65 | def testPackageInstalledForOldVersion(self): |
| 66 | """Test PackageInstalled should return True when pkg is out-of-date.""" |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 67 | self.Patch( |
| 68 | setup_common, |
| 69 | "CheckCmdOutput", |
| 70 | return_value=self.PKG_INFO_OLD_VERSION) |
| 71 | |
| Sam Chiu | 404c603 | 2020-01-22 11:43:56 +0800 | [diff] [blame] | 72 | self.assertTrue(setup_common.PackageInstalled("fake_package", |
| 73 | compare_version=True)) |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 74 | |
| 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 | |
| 85 | if __name__ == "__main__": |
| 86 | unittest.main() |