Alex Klein | eb77ffa | 2019-05-28 14:47:44 -0600 | [diff] [blame^] | 1 | # -*- coding: utf-8 -*- |
| 2 | # Copyright 2019 The Chromium OS Authors. All rights reserved. |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
| 6 | """Packages service tests.""" |
| 7 | |
| 8 | from __future__ import print_function |
| 9 | |
| 10 | import mock |
| 11 | |
| 12 | from chromite.lib import build_target_util |
| 13 | from chromite.lib import chroot_lib |
| 14 | from chromite.lib import cros_test_lib |
| 15 | from chromite.lib import parallel |
| 16 | from chromite.service import packages |
| 17 | |
| 18 | |
| 19 | class UprevBuildTargetsTest(cros_test_lib.RunCommandTestCase): |
| 20 | """uprev_build_targets tests.""" |
| 21 | |
| 22 | def test_invalid_type_fails(self): |
| 23 | """Test invalid type fails.""" |
| 24 | with self.assertRaises(AssertionError): |
| 25 | packages.uprev_build_targets([build_target_util.BuildTarget('foo')], |
| 26 | 'invalid') |
| 27 | |
| 28 | def test_none_type_fails(self): |
| 29 | """Test None type fails.""" |
| 30 | with self.assertRaises(AssertionError): |
| 31 | packages.uprev_build_targets([build_target_util.BuildTarget('foo')], |
| 32 | None) |
| 33 | |
| 34 | |
| 35 | class UprevManagerTest(cros_test_lib.MockTestCase): |
| 36 | """UprevManager tests.""" |
| 37 | |
| 38 | def test_clean_stale_packages_no_chroot(self): |
| 39 | """Test no chroot skip.""" |
| 40 | manager = packages.UprevManager([], None) |
| 41 | patch = self.PatchObject(parallel, 'RunTasksInProcessPool') |
| 42 | |
| 43 | # pylint: disable=protected-access |
| 44 | manager._clean_stale_packages() |
| 45 | |
| 46 | # Make sure we aren't doing any work. |
| 47 | patch.assert_not_called() |
| 48 | |
| 49 | def test_clean_stale_packages_chroot_not_exists(self): |
| 50 | """Cannot run the commands when the chroot does not exist.""" |
| 51 | chroot = chroot_lib.Chroot() |
| 52 | self.PatchObject(chroot, 'exists', return_value=False) |
| 53 | manager = packages.UprevManager([], None, chroot=chroot) |
| 54 | patch = self.PatchObject(parallel, 'RunTasksInProcessPool') |
| 55 | |
| 56 | # pylint: disable=protected-access |
| 57 | manager._clean_stale_packages() |
| 58 | |
| 59 | # Make sure we aren't doing any work. |
| 60 | patch.assert_not_called() |
| 61 | |
| 62 | def test_clean_stale_packages_no_build_targets(self): |
| 63 | """Make sure it behaves as expected with no build targets provided.""" |
| 64 | chroot = chroot_lib.Chroot() |
| 65 | self.PatchObject(chroot, 'exists', return_value=True) |
| 66 | manager = packages.UprevManager([], None, chroot=chroot) |
| 67 | patch = self.PatchObject(parallel, 'RunTasksInProcessPool') |
| 68 | |
| 69 | # pylint: disable=protected-access |
| 70 | manager._clean_stale_packages() |
| 71 | |
| 72 | # Make sure we aren't doing any work. |
| 73 | patch.assert_called_once_with(mock.ANY, [[None]]) |
| 74 | |
| 75 | def test_clean_stale_packages_with_boards(self): |
| 76 | """Test it cleans all boards as well as the chroot.""" |
| 77 | targets = ['board1', 'board2'] |
| 78 | build_targets = [build_target_util.BuildTarget(t) for t in targets] |
| 79 | chroot = chroot_lib.Chroot() |
| 80 | self.PatchObject(chroot, 'exists', return_value=True) |
| 81 | manager = packages.UprevManager([], None, chroot=chroot, |
| 82 | build_targets=build_targets) |
| 83 | patch = self.PatchObject(parallel, 'RunTasksInProcessPool') |
| 84 | |
| 85 | # pylint: disable=protected-access |
| 86 | manager._clean_stale_packages() |
| 87 | |
| 88 | patch.assert_called_once_with(mock.ANY, [[t] for t in targets + [None]]) |