blob: 3de435905123cc247834bd41e14313de94a6d8e5 [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
62 def testConvertToTMinusMore(self):
63 """Test convert_tot_spec when given tot_spec is 'tot-3'."""
64 self._mock_tot.return_value = FakeTotMilestoneManager(False)
65 fake_tot_manager = tot_manager.TotMilestoneManager()
66 self.assertEqual(fake_tot_manager.convert_tot_spec('tot-3'), 'R56')
67
68 def testConvertToTBadSpec(self):
69 """Test convert_tot_spec when given tot_spec is unrecognized."""
70 self._mock_tot.return_value = FakeTotMilestoneManager(False)
71 fake_tot_manager = tot_manager.TotMilestoneManager()
72 self.assertRaises(ValueError,
73 fake_tot_manager.convert_tot_spec, 'bad_tot')
74
75 def testCheckSpecsEqualToT(self):
76 """Test check_branch_specs with branch_specs 'tot'."""
77 self._mock_tot.return_value = FakeTotMilestoneManager(False)
78 fake_tot_manager = tot_manager.TotMilestoneManager()
79 self.assertTrue(
80 tot_manager.check_branch_specs(['==tot'], fake_tot_manager))
81
82 def testCheckSpecsLessThanToT(self):
83 """Test check_branch_specs with branch_specs '<=tot-1'."""
84 self._mock_tot.return_value = FakeTotMilestoneManager(False)
85 fake_tot_manager = tot_manager.TotMilestoneManager()
86 self.assertTrue(
87 tot_manager.check_branch_specs(['<=tot-1'], fake_tot_manager))
88
89 def testCheckSpecsMoreThanMilestone(self):
90 """Test check_branch_specs with branch_specs '>=R56'."""
91 self._mock_tot.return_value = FakeTotMilestoneManager(False)
92 fake_tot_manager = tot_manager.TotMilestoneManager()
93 self.assertTrue(
94 tot_manager.check_branch_specs(['>=R56'], fake_tot_manager))
95
96 def testCheckSpecsBareBranch(self):
97 """Test check_branch_specs with branch_specs 'factory'."""
98 self._mock_tot.return_value = FakeTotMilestoneManager(False)
99 fake_tot_manager = tot_manager.TotMilestoneManager()
100 self.assertTrue(
101 tot_manager.check_branch_specs(['factory'], fake_tot_manager))
102
103 def testCheckSpecsBadBranch(self):
104 """Test check_branch_specs fails with branch_specs 'bad_branch'."""
105 self._mock_tot.return_value = FakeTotMilestoneManager(False)
106 fake_tot_manager = tot_manager.TotMilestoneManager()
107 self.assertRaises(ValueError,
108 tot_manager.check_branch_specs,
109 ['bad_branch'],
110 fake_tot_manager)
111
112 def testCheckSpecsBareBranchAndToT(self):
113 """Test check_branch_specs with branch_specs 'factory,>=tot-1'."""
114 self._mock_tot.return_value = FakeTotMilestoneManager(False)
115 fake_tot_manager = tot_manager.TotMilestoneManager()
116 self.assertTrue(
117 tot_manager.check_branch_specs(['factory', '>=tot-1'],
118 fake_tot_manager))
119
120 def testCheckSpecsMoreThanOneNumeric(self):
121 """Test check_branch_specs fails with branch_specs '==tot,>=tot-1'."""
122 self._mock_tot.return_value = FakeTotMilestoneManager(False)
123 fake_tot_manager = tot_manager.TotMilestoneManager()
124 self.assertRaises(ValueError,
125 tot_manager.check_branch_specs,
126 ['==tot', '>=tot-1'],
127 fake_tot_manager)
128
129
130if __name__ == '__main__':
131 unittest.main()