blob: a1a2efd7672b93168d4d9939e1faba27a7e9706a [file] [log] [blame]
Alex Kleineb77ffa2019-05-28 14:47:44 -06001# -*- 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
8from __future__ import print_function
9
10import mock
11
12from chromite.lib import build_target_util
13from chromite.lib import chroot_lib
14from chromite.lib import cros_test_lib
15from chromite.lib import parallel
16from chromite.service import packages
17
18
Alex Kleineb77ffa2019-05-28 14:47:44 -060019class 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
David Burger1e0fe232019-07-01 14:52:07 -060035
36class GetBestVisibleTest(cros_test_lib.MockTestCase):
37 """get_best_visible tests."""
38
39 def test_empty_atom_fails(self):
40 with self.assertRaises(AssertionError):
41 packages.get_best_visible('')
42
43
Alex Kleineb77ffa2019-05-28 14:47:44 -060044class UprevManagerTest(cros_test_lib.MockTestCase):
45 """UprevManager tests."""
46
47 def test_clean_stale_packages_no_chroot(self):
48 """Test no chroot skip."""
49 manager = packages.UprevManager([], None)
50 patch = self.PatchObject(parallel, 'RunTasksInProcessPool')
51
52 # pylint: disable=protected-access
53 manager._clean_stale_packages()
54
55 # Make sure we aren't doing any work.
56 patch.assert_not_called()
57
58 def test_clean_stale_packages_chroot_not_exists(self):
59 """Cannot run the commands when the chroot does not exist."""
60 chroot = chroot_lib.Chroot()
61 self.PatchObject(chroot, 'exists', return_value=False)
62 manager = packages.UprevManager([], None, chroot=chroot)
63 patch = self.PatchObject(parallel, 'RunTasksInProcessPool')
64
65 # pylint: disable=protected-access
66 manager._clean_stale_packages()
67
68 # Make sure we aren't doing any work.
69 patch.assert_not_called()
70
71 def test_clean_stale_packages_no_build_targets(self):
72 """Make sure it behaves as expected with no build targets provided."""
73 chroot = chroot_lib.Chroot()
74 self.PatchObject(chroot, 'exists', return_value=True)
75 manager = packages.UprevManager([], None, chroot=chroot)
76 patch = self.PatchObject(parallel, 'RunTasksInProcessPool')
77
78 # pylint: disable=protected-access
79 manager._clean_stale_packages()
80
81 # Make sure we aren't doing any work.
82 patch.assert_called_once_with(mock.ANY, [[None]])
83
84 def test_clean_stale_packages_with_boards(self):
85 """Test it cleans all boards as well as the chroot."""
86 targets = ['board1', 'board2']
87 build_targets = [build_target_util.BuildTarget(t) for t in targets]
88 chroot = chroot_lib.Chroot()
89 self.PatchObject(chroot, 'exists', return_value=True)
90 manager = packages.UprevManager([], None, chroot=chroot,
91 build_targets=build_targets)
92 patch = self.PatchObject(parallel, 'RunTasksInProcessPool')
93
94 # pylint: disable=protected-access
95 manager._clean_stale_packages()
96
97 patch.assert_called_once_with(mock.ANY, [[t] for t in targets + [None]])