OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright 2015 The LUCI Authors. All rights reserved. | 2 # Copyright 2015 The LUCI Authors. All rights reserved. |
3 # Use of this source code is governed under the Apache License, Version 2.0 | 3 # Use of this source code is governed under the Apache License, Version 2.0 |
4 # that can be found in the LICENSE file. | 4 # that can be found in the LICENSE file. |
5 | 5 |
6 import collections | 6 import collections |
7 import contextlib | 7 import contextlib |
8 import datetime | 8 import datetime |
9 import json | 9 import json |
| 10 import os |
| 11 import shutil |
| 12 import tempfile |
10 import threading | 13 import threading |
11 import time | 14 import time |
12 import unittest | 15 import unittest |
13 import StringIO | 16 import StringIO |
14 | 17 |
15 import test_env | 18 import test_env |
16 | 19 |
17 import libs.logdog.stream | 20 import libs.logdog.stream |
18 import libs.logdog.varint | 21 import libs.logdog.varint |
19 from google.protobuf import json_format as jsonpb | 22 from google.protobuf import json_format as jsonpb |
20 from recipe_engine import recipe_api | 23 from recipe_engine import recipe_api |
21 from recipe_engine import stream | 24 from recipe_engine import stream |
22 from recipe_engine import stream_logdog | 25 from recipe_engine import stream_logdog |
23 | 26 |
24 | 27 |
25 import annotations_pb2 as pb | 28 import annotations_pb2 as pb |
26 | 29 |
27 | 30 |
| 31 @contextlib.contextmanager |
| 32 def tempdir(): |
| 33 tdir = tempfile.mkdtemp(suffix='stream_logdog_test', dir=test_env.BASE_DIR) |
| 34 try: |
| 35 yield tdir |
| 36 finally: |
| 37 shutil.rmtree(tdir) |
| 38 |
| 39 |
28 def _translate_annotation_datagram(dg): | 40 def _translate_annotation_datagram(dg): |
29 """Translate annotation datagram binary data into a Python dict modeled after | 41 """Translate annotation datagram binary data into a Python dict modeled after |
30 the JSONPB projection of the datagram. | 42 the JSONPB projection of the datagram. |
31 | 43 |
32 This is chosen because it allows for easy idiomatic equality assertions in | 44 This is chosen because it allows for easy idiomatic equality assertions in |
33 test cases. | 45 test cases. |
34 | 46 |
35 Args: | 47 Args: |
36 dg (str): The serialized annotation pb.Step datagram. | 48 dg (str): The serialized annotation pb.Step datagram. |
37 """ | 49 """ |
(...skipping 359 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
397 {u'step': { | 409 {u'step': { |
398 u'name': u'bar', | 410 u'name': u'bar', |
399 u'status': u'SUCCESS', | 411 u'status': u'SUCCESS', |
400 u'started': u'2106-06-12T01:02:06Z', | 412 u'started': u'2106-06-12T01:02:06Z', |
401 u'ended': u'2106-06-12T01:02:07Z', | 413 u'ended': u'2106-06-12T01:02:07Z', |
402 }}, | 414 }}, |
403 ], | 415 ], |
404 }, | 416 }, |
405 }) | 417 }) |
406 | 418 |
| 419 def testDumpFinalState(self): |
| 420 self.env.argv = ['fake_program', 'arg0', 'arg1'] |
| 421 self.env.environ['foo'] = 'bar' |
| 422 self.env.cwd = 'CWD' |
| 423 |
| 424 # Create a StreamEngine with an update interval that will trigger each time |
| 425 # _advance_time is called. |
| 426 with tempdir() as tdir: |
| 427 dump_path = os.path.join(tdir, 'dump.bin') |
| 428 with self._new_stream_engine( |
| 429 update_interval=datetime.timedelta(seconds=1), |
| 430 dump_path=dump_path) as se: |
| 431 # Initial stream state (no steps). |
| 432 self.assertEqual(self.client.all_streams(), { |
| 433 u'annotations': { |
| 434 u'name': u'steps', |
| 435 u'started': u'2106-06-12T01:02:03Z', |
| 436 u'command': { |
| 437 u'commandLine': [u'fake_program', u'arg0', u'arg1'], |
| 438 u'cwd': u'CWD', |
| 439 u'environ': {u'foo': u'bar'}, |
| 440 }, |
| 441 }, |
| 442 }) |
| 443 |
| 444 with open(dump_path, 'rb') as fd: |
| 445 step = _translate_annotation_datagram(fd.read()) |
| 446 self.assertEqual(step, { |
| 447 u'name': u'steps', |
| 448 u'status': u'SUCCESS', |
| 449 u'started': u'2106-06-12T01:02:03Z', |
| 450 u'ended': u'2106-06-12T01:02:04Z', |
| 451 u'command': { |
| 452 u'commandLine': [u'fake_program', u'arg0', u'arg1'], |
| 453 u'cwd': u'CWD', |
| 454 u'environ': {u'foo': u'bar'}, |
| 455 }, |
| 456 }) |
| 457 |
407 def testBasicStream(self): | 458 def testBasicStream(self): |
408 self.env.argv = ['fake_program', 'arg0', 'arg1'] | 459 self.env.argv = ['fake_program', 'arg0', 'arg1'] |
409 self.env.environ['foo'] = 'bar' | 460 self.env.environ['foo'] = 'bar' |
410 self.env.cwd = 'CWD' | 461 self.env.cwd = 'CWD' |
411 | 462 |
412 with self._new_stream_engine(name_base='test/base') as se: | 463 with self._new_stream_engine(name_base='test/base') as se: |
413 with self._step_stream(se, | 464 with self._step_stream(se, |
414 name='first step', | 465 name='first step', |
415 cmd=['first', 'step'], | 466 cmd=['first', 'step'], |
416 cwd='FIRST_CWD') as step: | 467 cwd='FIRST_CWD') as step: |
(...skipping 554 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
971 sn = sn.append('#!!! stream name !!!') | 1022 sn = sn.append('#!!! stream name !!!') |
972 self.assertEqual(str(sn), 'base/s______stream_name____') | 1023 self.assertEqual(str(sn), 'base/s______stream_name____') |
973 | 1024 |
974 def testAugmentInvalidStreamNameNormalizes(self): | 1025 def testAugmentInvalidStreamNameNormalizes(self): |
975 sn = stream_logdog._StreamName('base') | 1026 sn = stream_logdog._StreamName('base') |
976 self.assertEqual(str(sn.augment(' !!! other !!! ')), 'base_____other_____') | 1027 self.assertEqual(str(sn.augment(' !!! other !!! ')), 'base_____other_____') |
977 | 1028 |
978 | 1029 |
979 if __name__ == '__main__': | 1030 if __name__ == '__main__': |
980 unittest.main() | 1031 unittest.main() |
OLD | NEW |