blob: eb09b17e742768a312802567f1eed04e7d156438 [file] [log] [blame]
Kuang-che Wu6e4beca2018-06-27 17:45:02 +08001# -*- coding: utf-8 -*-
Kuang-che Wu88875db2017-07-20 10:47:53 +08002# Copyright 2017 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"""Test runner.py script"""
6
7from __future__ import print_function
8import argparse
9import unittest
Kuang-che Wu23192ad2020-03-11 18:12:46 +080010from unittest import mock
Kuang-che Wu88875db2017-07-20 10:47:53 +080011
12import runner
Kuang-che Wue1b329e2018-03-31 11:39:33 +080013from bisect_kit import common
14from bisect_kit import configure
Kuang-che Wu88875db2017-07-20 10:47:53 +080015
16
17@mock.patch('bisect_kit.common.config_logging', mock.Mock())
18class TestRunner(unittest.TestCase):
19 """Test runner."""
20
Kuang-che Wue1b329e2018-03-31 11:39:33 +080021 def tearDown(self):
22 configure.reset()
23
Kuang-che Wu88875db2017-07-20 10:47:53 +080024 def test_argtype_ratio(self):
25 self.assertEqual(runner.argtype_ratio('=1'), ('=', 1.0))
26 with self.assertRaises(argparse.ArgumentTypeError):
27 runner.argtype_ratio('hello')
28 with self.assertRaises(argparse.ArgumentTypeError):
29 runner.argtype_ratio('>5')
30
31 def test_is_meet_finished(self):
32 self.assertEqual(
33 runner.criteria_is_met(('=', 0), dict(new=0, old=0, skip=1), 0, 'new'),
34 None)
35
36 self.assertEqual(
37 runner.criteria_is_met(('=', 0), dict(new=0, old=3), 0, 'new'), True)
38 self.assertEqual(
39 runner.criteria_is_met(('<', 0.4), dict(new=3, old=7), 0, 'new'), True)
40 self.assertEqual(
41 runner.criteria_is_met(('<=', 0.4), dict(new=3, old=7), 0, 'new'), True)
42 self.assertEqual(
43 runner.criteria_is_met(('>', 0.2), dict(new=3, old=7), 0, 'new'), True)
44 self.assertEqual(
45 runner.criteria_is_met(('>=', 0.2), dict(new=3, old=7), 0, 'new'), True)
46
47 def test_is_meet_rest(self):
48 self.assertEqual(
49 runner.criteria_is_met(('=', 0), dict(new=0, old=0, skip=1), 1, 'new'),
50 None)
51
52 # Between 0.45 and 0.63
53 self.assertEqual(
54 runner.criteria_is_met(('>', 0.5), dict(old=4, new=5), 2, 'new'), None)
55 # Between 0.5 and 0.6
56 self.assertEqual(
57 runner.criteria_is_met(('>', 0.5), dict(old=4, new=5), 1, 'new'), None)
58 # Between 0.5 and 0.6
59 self.assertEqual(
60 runner.criteria_is_met(('>=', 0.5), dict(old=4, new=5), 1, 'new'), True)
61 # Between 0.4 and 0.5
62 self.assertEqual(
63 runner.criteria_is_met(('>=', 0.5), dict(old=5, new=4), 1, 'new'), None)
64 # Between 0.4 and 0.5
65 self.assertEqual(
66 runner.criteria_is_met(('<', 0.5), dict(old=5, new=4), 1, 'new'), None)
67 # Between 0.5 and 0.6
68 self.assertEqual(
69 runner.criteria_is_met(('<=', 0.5), dict(old=4, new=5), 1, 'new'), None)
70 # Between 0.83 and 1.0
71 self.assertEqual(
72 runner.criteria_is_met(('=', 1.0), dict(old=0, new=5), 1, 'new'), None)
73
74 # It's certain because the result count is impossible non-integer.
75 self.assertEqual(
76 runner.criteria_is_met(('=', 0.5), dict(old=1, new=1), 1, 'new'), False)
77
78 def test_run_once_returncode(self):
Kuang-che Wue1b329e2018-03-31 11:39:33 +080079 common.init()
Kuang-che Wu88875db2017-07-20 10:47:53 +080080 parser = runner.create_argument_parser()
81 opts = parser.parse_args(['--new', '--returncode=0', 'true'])
82 self.assertEqual(runner.run_once(opts), runner.NEW)
83
84 opts = parser.parse_args(['--new', '--returncode=0', 'false'])
85 self.assertEqual(runner.run_once(opts), runner.OLD)
86
87 opts = parser.parse_args(['--new', '--returncode=0', 'program.not.found'])
Kuang-che Wu0476d1f2019-03-04 19:27:01 +080088 self.assertEqual(runner.run_once(opts), runner.OLD)
Kuang-che Wu88875db2017-07-20 10:47:53 +080089
90 opts = parser.parse_args(['--new', '--returncode=0', 'sh', '-c', 'kill $$'])
91 self.assertEqual(runner.run_once(opts), runner.FATAL)
92
93 def test_run_once_output(self):
Kuang-che Wue1b329e2018-03-31 11:39:33 +080094 common.init()
Kuang-che Wu88875db2017-07-20 10:47:53 +080095 parser = runner.create_argument_parser()
96 opts = parser.parse_args(['--old', '--output', 'OLD', 'echo', 'OLD'])
97 self.assertEqual(runner.run_once(opts), runner.OLD)
98
99 opts = parser.parse_args([
100 '--old', '--output', 'OLD', '--precondition_output', 'foo', 'echo',
101 'foo OLD'
102 ])
103 self.assertEqual(runner.run_once(opts), runner.OLD)
104
105 opts = parser.parse_args([
106 '--old', '--output', 'OLD', '--precondition_output', 'foo', 'echo',
107 'bar OLD'
108 ])
109 self.assertEqual(runner.run_once(opts), runner.SKIP)
110
111 def test_run_once_timeout(self):
Kuang-che Wue1b329e2018-03-31 11:39:33 +0800112 common.init()
Kuang-che Wu88875db2017-07-20 10:47:53 +0800113 parser = runner.create_argument_parser()
114 opts = parser.parse_args(['--new', '--timeout=0.1', 'sleep', '0.11'])
115 self.assertEqual(runner.run_once(opts), runner.NEW)
116
117 opts = parser.parse_args(['--new', '--timeout=0.1', 'sleep', '0'])
118 self.assertEqual(runner.run_once(opts), runner.OLD)
119
120 def test_run_once_terminate_output(self):
Kuang-che Wue1b329e2018-03-31 11:39:33 +0800121 common.init()
Kuang-che Wu88875db2017-07-20 10:47:53 +0800122 parser = runner.create_argument_parser()
123 opts = parser.parse_args([
124 '--new', '--output=hello', '--terminate_output', 'hello', 'sh', '-c',
125 'echo hello; sleep 100; echo world'
126 ])
127 self.assertEqual(runner.run_once(opts), runner.NEW)
128
129 opts = parser.parse_args([
130 '--new', '--output=world', '--terminate_output', 'hello', 'sh', '-c',
131 'echo hello; sleep 100; echo world'
132 ])
133 self.assertEqual(runner.run_once(opts), runner.OLD)
134
135 def test_main(self):
136 self.assertEqual(
137 runner.main(['--new', '--output', 'hello', 'echo', 'hello']),
138 runner.NEW)
139 self.assertEqual(
140 runner.main(['--old', '--output', 'hello', 'echo', 'hello']),
141 runner.OLD)
142 self.assertEqual(
143 runner.main(['--new', '--output', 'hello', 'echo', 'world']),
144 runner.OLD)
145 self.assertEqual(
146 runner.main([
147 '--new', '--output', 'hello', '--precondition_output', 'hello',
148 'echo', 'world'
149 ]), runner.SKIP)
150
151 def test_main_fatal(self):
152 self.assertEqual(
153 runner.main(['--new', '--output', 'hello', '/non/existing/path']),
Kuang-che Wu0476d1f2019-03-04 19:27:01 +0800154 runner.OLD)
Kuang-che Wu88875db2017-07-20 10:47:53 +0800155
156 def test_main_shortcut(self):
157 with mock.patch.object(
158 runner, 'run_once', return_value=runner.OLD) as mock_run_once:
159 self.assertEqual(
160 runner.main(['--new', '--output=hello', '--repeat=5', 'foo']),
161 runner.OLD)
162 self.assertEqual(mock_run_once.call_count, 1)
163
164 with mock.patch.object(
165 runner, 'run_once', return_value=runner.OLD) as mock_run_once:
166 self.assertEqual(
167 runner.main(
168 ['--new', '--output=hello', '--repeat=10', '--ratio=>0.3',
169 'foo']), runner.OLD)
170 self.assertEqual(mock_run_once.call_count, 7)
171
172 with mock.patch.object(
173 runner, 'run_once', return_value=runner.OLD) as mock_run_once:
174 self.assertEqual(
175 runner.main([
176 '--new', '--output=hello', '--repeat=10', '--ratio=>0.3',
177 '--noshortcut', 'foo'
178 ]), runner.OLD)
179 self.assertEqual(mock_run_once.call_count, 10)
180
181
182if __name__ == '__main__':
183 unittest.main()