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