blob: ea86ac6598c9ffa6ddb732161fdc5f24b8bce313 [file] [log] [blame]
xixuand7b7b7b2017-08-15 15:07:35 -07001# Copyright 2017 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""Module for tot_manager unittests."""
6
7import unittest
8
9import mock
10import tot_manager
11
12
13class FakeTotMilestoneManager(tot_manager.TotMilestoneManager):
14
15 def __init__(self, is_sanity):
16 self.is_sanity = is_sanity
17 self.storage_client = None
18 self.tot = self._tot_milestone()
19
20
21class TotMilestoneManagerTestCase(unittest.TestCase):
22
23 def setUp(self):
24 tot_patcher = mock.patch('tot_manager.TotMilestoneManager')
25 self._mock_tot = tot_patcher.start()
26 self.addCleanup(tot_patcher.stop)
27
28 build_patcher = mock.patch('build_lib.get_latest_cros_build_from_gs')
29 build_patcher.start().return_value = 'R56-abc-def'
30 self.addCleanup(build_patcher.stop)
31
32 def testTotManagerWithSanity(self):
33 """Test TotManager's tot in sanity check."""
34 self._mock_tot.return_value = FakeTotMilestoneManager(True)
35 fake_tot_manager = tot_manager.TotMilestoneManager()
36 self.assertEqual(fake_tot_manager.tot, 'R40')
37
38 def testTotManagerWithoutSanity(self):
39 """Test TotManager's tot without sanity check."""
40 self._mock_tot.return_value = FakeTotMilestoneManager(False)
41 fake_tot_manager = tot_manager.TotMilestoneManager()
42 self.assertEqual(fake_tot_manager.tot, 'R56')
43
44 def testConvertToT(self):
45 """Test convert_tot_spec when given tot_spec is 'tot'."""
46 self._mock_tot.return_value = FakeTotMilestoneManager(False)
47 fake_tot_manager = tot_manager.TotMilestoneManager()
48 self.assertEqual(fake_tot_manager.convert_tot_spec('tot'), 'R56')
49
50 def testConvertToTMinus1(self):
51 """Test convert_tot_spec when given tot_spec is 'tot-1'."""
52 self._mock_tot.return_value = FakeTotMilestoneManager(False)
53 fake_tot_manager = tot_manager.TotMilestoneManager()
54 self.assertEqual(fake_tot_manager.convert_tot_spec('tot-1'), 'R55')
55
56 def testConvertToTMinus2(self):
57 """Test convert_tot_spec when given tot_spec is 'tot-2'."""
58 self._mock_tot.return_value = FakeTotMilestoneManager(False)
59 fake_tot_manager = tot_manager.TotMilestoneManager()
60 self.assertEqual(fake_tot_manager.convert_tot_spec('tot-2'), 'R54')
61
Xinan Lined57b182020-04-14 14:00:17 -070062 def testConvertToTMinus9(self):
63 """Test convert_tot_spec when given tot_spec is 'tot-9'."""
xixuand7b7b7b2017-08-15 15:07:35 -070064 self._mock_tot.return_value = FakeTotMilestoneManager(False)
65 fake_tot_manager = tot_manager.TotMilestoneManager()
Xinan Lined57b182020-04-14 14:00:17 -070066 self.assertEqual(fake_tot_manager.convert_tot_spec('tot-9'), 'R47')
67
68 def testConvertToTMinus10(self):
69 """Test convert_tot_spec when given invalid tot_spec 'tot-10'."""
70 self._mock_tot.return_value = FakeTotMilestoneManager(False)
71 fake_tot_manager = tot_manager.TotMilestoneManager()
72 self.assertRaises(ValueError, fake_tot_manager.convert_tot_spec, 'tot-10')
73
74 def testConvertToTMinus0(self):
75 """Test convert_tot_spec when given invalid tot_spec 'tot-0'."""
76 self._mock_tot.return_value = FakeTotMilestoneManager(False)
77 fake_tot_manager = tot_manager.TotMilestoneManager()
78 self.assertRaises(ValueError, fake_tot_manager.convert_tot_spec, 'tot-0')
xixuand7b7b7b2017-08-15 15:07:35 -070079
80 def testConvertToTBadSpec(self):
81 """Test convert_tot_spec when given tot_spec is unrecognized."""
82 self._mock_tot.return_value = FakeTotMilestoneManager(False)
83 fake_tot_manager = tot_manager.TotMilestoneManager()
84 self.assertRaises(ValueError,
85 fake_tot_manager.convert_tot_spec, 'bad_tot')
86
87 def testCheckSpecsEqualToT(self):
88 """Test check_branch_specs with branch_specs 'tot'."""
89 self._mock_tot.return_value = FakeTotMilestoneManager(False)
90 fake_tot_manager = tot_manager.TotMilestoneManager()
91 self.assertTrue(
92 tot_manager.check_branch_specs(['==tot'], fake_tot_manager))
93
94 def testCheckSpecsLessThanToT(self):
95 """Test check_branch_specs with branch_specs '<=tot-1'."""
96 self._mock_tot.return_value = FakeTotMilestoneManager(False)
97 fake_tot_manager = tot_manager.TotMilestoneManager()
98 self.assertTrue(
99 tot_manager.check_branch_specs(['<=tot-1'], fake_tot_manager))
100
101 def testCheckSpecsMoreThanMilestone(self):
102 """Test check_branch_specs with branch_specs '>=R56'."""
103 self._mock_tot.return_value = FakeTotMilestoneManager(False)
104 fake_tot_manager = tot_manager.TotMilestoneManager()
105 self.assertTrue(
106 tot_manager.check_branch_specs(['>=R56'], fake_tot_manager))
107
108 def testCheckSpecsBareBranch(self):
109 """Test check_branch_specs with branch_specs 'factory'."""
110 self._mock_tot.return_value = FakeTotMilestoneManager(False)
111 fake_tot_manager = tot_manager.TotMilestoneManager()
112 self.assertTrue(
113 tot_manager.check_branch_specs(['factory'], fake_tot_manager))
114
115 def testCheckSpecsBadBranch(self):
116 """Test check_branch_specs fails with branch_specs 'bad_branch'."""
117 self._mock_tot.return_value = FakeTotMilestoneManager(False)
118 fake_tot_manager = tot_manager.TotMilestoneManager()
119 self.assertRaises(ValueError,
120 tot_manager.check_branch_specs,
121 ['bad_branch'],
122 fake_tot_manager)
123
124 def testCheckSpecsBareBranchAndToT(self):
125 """Test check_branch_specs with branch_specs 'factory,>=tot-1'."""
126 self._mock_tot.return_value = FakeTotMilestoneManager(False)
127 fake_tot_manager = tot_manager.TotMilestoneManager()
128 self.assertTrue(
129 tot_manager.check_branch_specs(['factory', '>=tot-1'],
130 fake_tot_manager))
131
132 def testCheckSpecsMoreThanOneNumeric(self):
133 """Test check_branch_specs fails with branch_specs '==tot,>=tot-1'."""
134 self._mock_tot.return_value = FakeTotMilestoneManager(False)
135 fake_tot_manager = tot_manager.TotMilestoneManager()
136 self.assertRaises(ValueError,
137 tot_manager.check_branch_specs,
138 ['==tot', '>=tot-1'],
139 fake_tot_manager)
140
141
142if __name__ == '__main__':
143 unittest.main()