| OLD | NEW |
| (Empty) | |
| 1 #!/usr/bin/env python |
| 2 # Copyright 2014 The Chromium 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 import Queue |
| 7 import StringIO |
| 8 import json |
| 9 import logging |
| 10 import sys |
| 11 import threading |
| 12 import unittest |
| 13 |
| 14 import test_env # pylint: disable=W0403,W0611 |
| 15 |
| 16 # In depot_tools/ |
| 17 from testing_support import auto_stub |
| 18 |
| 19 from slave.swarming import swarming_run_shim |
| 20 |
| 21 |
| 22 # pylint: disable=W0212 |
| 23 |
| 24 |
| 25 class StateMachine(object): |
| 26 """State machine to coordinate steps accross multiple threads.""" |
| 27 def __init__(self): |
| 28 self._step = 0 |
| 29 self.condition = threading.Condition() |
| 30 |
| 31 def increment(self): |
| 32 with self.condition: |
| 33 self._step += 1 |
| 34 logging.info('Switching machine to step %d', self._step) |
| 35 self.condition.notify_all() |
| 36 |
| 37 def wait_for(self, value): |
| 38 while True: |
| 39 with self.condition: |
| 40 if self._step == value: |
| 41 return |
| 42 |
| 43 |
| 44 class SwarmingRunTest(auto_stub.TestCase): |
| 45 def test_stream(self): |
| 46 cmd = [ |
| 47 sys.executable, |
| 48 '-c', |
| 49 'import time; print("a"); time.sleep(0.1); print("b")', |
| 50 ] |
| 51 actual = list(swarming_run_shim.stream_process(cmd)) |
| 52 self.assertEqual(['a\n', 'b\n', 0], actual) |
| 53 |
| 54 def test_one(self): |
| 55 cmds = [] |
| 56 def stream_process_mock(cmd): |
| 57 cmds.append(cmd) |
| 58 yield 'a\n' |
| 59 yield 'b\n' |
| 60 yield 'c\n' |
| 61 yield 0 |
| 62 |
| 63 def find_client_mock(path): |
| 64 return '/doesn\'t/exist/' |
| 65 |
| 66 self.mock(swarming_run_shim, 'stream_process', stream_process_mock) |
| 67 self.mock(swarming_run_shim.swarming_utils, 'find_client', find_client_mock) |
| 68 self.mock(swarming_run_shim.swarming_utils, 'get_version', lambda _: (0, 4)) |
| 69 self.mock(sys, 'stdout', StringIO.StringIO()) |
| 70 |
| 71 cmd = [ |
| 72 '--swarming', 'http://localhost:1', |
| 73 '--isolate-server', 'http://localhost:2', |
| 74 ] |
| 75 props = { |
| 76 'target_os': 'darwin', |
| 77 'swarm_hashes': {'base_test': '1234'}, |
| 78 'run_default_swarm_tests': ['base_test'], |
| 79 'testfilter': ['base_test_swarm'], |
| 80 'buildbotURL': 'http://build.chromium.org/p/chromium.win/' |
| 81 } |
| 82 cmd.extend(('--build-properties', json.dumps(props))) |
| 83 self.assertEqual(0, swarming_run_shim.main(cmd)) |
| 84 expected_cmd = [ |
| 85 sys.executable, |
| 86 "/doesn't/exist/swarming.py", |
| 87 'run', |
| 88 '--swarming', 'http://localhost:1', |
| 89 '--isolate-server', 'http://localhost:2', |
| 90 '--priority', '10', |
| 91 '--shards', '1', |
| 92 '--task-name', u'base_test/Mac/1234', |
| 93 u'1234', |
| 94 '--dimension', 'os', 'Mac', |
| 95 ] |
| 96 self.assertEqual([expected_cmd], cmds) |
| 97 expected = ( |
| 98 'Selected tests:\n base_test\nSelected OS: Mac\n' |
| 99 '\n@@@SEED_STEP base_test@@@\n' |
| 100 '\n@@@STEP_CURSOR base_test@@@\n' |
| 101 '\n@@@STEP_TEXT@Mac@@@\n' |
| 102 '\n@@@STEP_TEXT@1234@@@\n' |
| 103 '\n@@@STEP_CURSOR base_test@@@\n' |
| 104 'a\nb\nc\n' |
| 105 '\n@@@STEP_CLOSED@@@\n' |
| 106 ) |
| 107 self.assertEqual(expected, sys.stdout.getvalue()) |
| 108 |
| 109 def test_default(self): |
| 110 def stream_process_mock(cmd): |
| 111 yield 'a\n' |
| 112 yield 0 |
| 113 self.mock(swarming_run_shim, 'stream_process', stream_process_mock) |
| 114 self.mock(swarming_run_shim.swarming_utils, 'find_client', lambda _: '/a') |
| 115 self.mock(swarming_run_shim.swarming_utils, 'get_version', lambda _: (0, 4)) |
| 116 self.mock(sys, 'stdout', StringIO.StringIO()) |
| 117 |
| 118 cmd = [ |
| 119 '--swarming', 'http://localhost:1', |
| 120 '--isolate-server', 'http://localhost:2', |
| 121 ] |
| 122 # Not a try server, missing most build properties. |
| 123 props = { |
| 124 'swarm_hashes': {'base_test': '1234'}, |
| 125 } |
| 126 cmd.extend(('--build-properties', json.dumps(props))) |
| 127 self.assertEqual(0, swarming_run_shim.main(cmd)) |
| 128 expected = ( |
| 129 # This test only passes on linux. |
| 130 'Selected tests:\n base_test\nSelected OS: Linux\n' |
| 131 '\n@@@SEED_STEP base_test@@@\n' |
| 132 '\n@@@STEP_CURSOR base_test@@@\n' |
| 133 '\n@@@STEP_TEXT@Linux@@@\n' |
| 134 '\n@@@STEP_TEXT@1234@@@\n' |
| 135 '\n@@@STEP_CURSOR base_test@@@\n' |
| 136 'a\n' |
| 137 '\n@@@STEP_CLOSED@@@\n' |
| 138 ) |
| 139 self.assertEqual(expected, sys.stdout.getvalue()) |
| 140 |
| 141 def test_three(self): |
| 142 out = Queue.Queue() |
| 143 def drive_many( |
| 144 client, version, swarming_server, isolate_server, priority, dimensions, |
| 145 steps): |
| 146 return swarming_run_shim._drive_many( |
| 147 client, version, swarming_server, isolate_server, priority, dimensions, |
| 148 steps, out) |
| 149 self.mock(swarming_run_shim, 'drive_many', drive_many) |
| 150 |
| 151 lock = threading.Lock() |
| 152 cmds = set() |
| 153 step = StateMachine() |
| 154 def stream_process_mock(cmd): |
| 155 """The unlocking pattern is: |
| 156 0. base_test outputs 2 lines and complete. |
| 157 1. slow_test outputs 1 line |
| 158 2. bloa_test outputs 1 line |
| 159 3. slow_test outputs 1 line |
| 160 4. bloa_test outputs 1 line |
| 161 5. slow_test complete |
| 162 6. bloa_test complete |
| 163 """ |
| 164 with lock: |
| 165 cmds.add(tuple(cmd)) |
| 166 if 'base_test/Mac/1234' in cmd: |
| 167 step.wait_for(0) |
| 168 yield 'base1\n' |
| 169 out.join() |
| 170 yield 'base2\n' |
| 171 out.join() |
| 172 yield 0 |
| 173 out.join() |
| 174 step.increment() |
| 175 elif 'slow_test/Mac/4321' in cmd: |
| 176 step.wait_for(1) |
| 177 yield 'slow1\n' |
| 178 out.join() |
| 179 step.increment() |
| 180 |
| 181 step.wait_for(3) |
| 182 yield 'slow2\n' |
| 183 out.join() |
| 184 step.increment() |
| 185 |
| 186 step.wait_for(5) |
| 187 yield 1 |
| 188 out.join() |
| 189 step.increment() |
| 190 elif 'bloa_test/Mac/0000' in cmd: |
| 191 step.wait_for(2) |
| 192 yield 'bloated1\n' |
| 193 out.join() |
| 194 step.increment() |
| 195 |
| 196 step.wait_for(4) |
| 197 yield 'bloated2\n' |
| 198 out.join() |
| 199 yield 'bloated3\n' |
| 200 out.join() |
| 201 step.increment() |
| 202 |
| 203 step.wait_for(6) |
| 204 yield 0 |
| 205 out.join() |
| 206 step.increment() |
| 207 else: |
| 208 logging.info('OOOPS') |
| 209 self.fail() |
| 210 |
| 211 def find_client_mock(path): |
| 212 return '/doesn\'t/exist/' |
| 213 |
| 214 self.mock(swarming_run_shim, 'stream_process', stream_process_mock) |
| 215 self.mock(swarming_run_shim.swarming_utils, 'find_client', find_client_mock) |
| 216 self.mock(swarming_run_shim.swarming_utils, 'get_version', lambda _: (0, 4)) |
| 217 self.mock(sys, 'stdout', StringIO.StringIO()) |
| 218 |
| 219 cmd = [ |
| 220 '--swarming', 'http://localhost:1', |
| 221 '--isolate-server', 'http://localhost:2', |
| 222 ] |
| 223 props = { |
| 224 'target_os': 'darwin', |
| 225 'swarm_hashes': { |
| 226 'base_test': '1234', |
| 227 'slow_test': '4321', |
| 228 'bloa_test': '0000', |
| 229 }, |
| 230 'run_default_swarm_tests': ['base_test', 'slow_test'], |
| 231 'testfilter': ['defaulttests', 'bloa_test_swarm'], |
| 232 } |
| 233 cmd.extend(('--build-properties', json.dumps(props))) |
| 234 self.assertEqual(0, swarming_run_shim.main(cmd)) |
| 235 expected_cmds = set([ |
| 236 ( |
| 237 sys.executable, |
| 238 "/doesn't/exist/swarming.py", |
| 239 'run', |
| 240 '--swarming', 'http://localhost:1', |
| 241 '--isolate-server', 'http://localhost:2', |
| 242 '--priority', '200', |
| 243 '--shards', '1', |
| 244 '--task-name', u'base_test/Mac/1234', |
| 245 u'1234', |
| 246 '--dimension', 'os', 'Mac', |
| 247 ), |
| 248 ( |
| 249 sys.executable, |
| 250 "/doesn't/exist/swarming.py", |
| 251 'run', |
| 252 '--swarming', 'http://localhost:1', |
| 253 '--isolate-server', 'http://localhost:2', |
| 254 '--priority', '200', |
| 255 '--shards', '1', |
| 256 '--task-name', u'slow_test/Mac/4321', |
| 257 u'4321', |
| 258 '--dimension', 'os', 'Mac', |
| 259 ), |
| 260 ( |
| 261 sys.executable, |
| 262 "/doesn't/exist/swarming.py", |
| 263 'run', |
| 264 '--swarming', 'http://localhost:1', |
| 265 '--isolate-server', 'http://localhost:2', |
| 266 '--priority', '200', |
| 267 '--shards', '1', |
| 268 '--task-name', u'bloa_test/Mac/0000', |
| 269 u'0000', |
| 270 '--dimension', 'os', 'Mac', |
| 271 ), |
| 272 ]) |
| 273 self.assertEqual(expected_cmds, cmds) |
| 274 actual = sys.stdout.getvalue() |
| 275 header = ( |
| 276 'Selected tests:\n base_test\n bloa_test\n slow_test\n' |
| 277 'Selected OS: Mac\n') |
| 278 self.assertEqual(header, actual[:len(header)]) |
| 279 actual = actual[len(header):] |
| 280 # Sadly, master/chromium_step.py AnnotationObserver is hard to extract so |
| 281 # we have to parse manually. |
| 282 expected = ( |
| 283 u'\n@@@SEED_STEP base_test@@@\n' |
| 284 u'\n@@@SEED_STEP bloa_test@@@\n' |
| 285 u'\n@@@SEED_STEP slow_test@@@\n' |
| 286 u'\n@@@STEP_CURSOR base_test@@@\n' |
| 287 u'\n@@@STEP_TEXT@Mac@@@\n' |
| 288 u'\n@@@STEP_TEXT@1234@@@\n' |
| 289 u'\n@@@STEP_CURSOR bloa_test@@@\n' |
| 290 u'\n@@@STEP_TEXT@Mac@@@\n' |
| 291 u'\n@@@STEP_TEXT@0000@@@\n' |
| 292 u'\n@@@STEP_CURSOR slow_test@@@\n' |
| 293 u'\n@@@STEP_TEXT@Mac@@@\n' |
| 294 u'\n@@@STEP_TEXT@4321@@@\n' |
| 295 u'\n@@@STEP_CURSOR base_test@@@\n' |
| 296 u'base1\nbase2\n' |
| 297 u'\n@@@STEP_CLOSED@@@\n' |
| 298 u'\n@@@STEP_CURSOR slow_test@@@\n' |
| 299 u'slow1\n' |
| 300 u'\n@@@STEP_CURSOR bloa_test@@@\n' |
| 301 u'bloated1\n' |
| 302 u'\n@@@STEP_CURSOR slow_test@@@\n' |
| 303 u'slow2\n' |
| 304 u'\n@@@STEP_CURSOR bloa_test@@@\n' |
| 305 u'bloated2\n' |
| 306 u'bloated3\n' |
| 307 u'\n@@@STEP_CURSOR slow_test@@@\n' |
| 308 u'\n@@@STEP_FAILURE@@@\n' |
| 309 u'\n@@@STEP_CLOSED@@@\n' |
| 310 u'\n@@@STEP_CURSOR bloa_test@@@\n' |
| 311 u'\n@@@STEP_CLOSED@@@\n' |
| 312 ) |
| 313 self.assertEqual(expected.splitlines(), actual.splitlines()) |
| 314 |
| 315 |
| 316 if __name__ == '__main__': |
| 317 logging.basicConfig( |
| 318 level=logging.DEBUG if '-v' in sys.argv else logging.ERROR) |
| 319 if '-v' in sys.argv: |
| 320 unittest.TestCase.maxDiff = None |
| 321 unittest.main() |
| OLD | NEW |