OLD | NEW |
| (Empty) |
1 #!/usr/bin/env python | |
2 # Copyright (c) 2012 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 json | |
7 import logging | |
8 import os | |
9 import sys | |
10 import time | |
11 import unittest | |
12 | |
13 ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) | |
14 sys.path.insert(0, ROOT_DIR) | |
15 | |
16 import run_test_from_archive | |
17 | |
18 | |
19 class RemoteTest(run_test_from_archive.Remote): | |
20 @staticmethod | |
21 def get_file_handler(_): | |
22 def upload_file(item, _dest): | |
23 if type(item) == type(Exception) and issubclass(item, Exception): | |
24 raise item() | |
25 elif isinstance(item, int): | |
26 time.sleep(int(item) / 100) | |
27 return upload_file | |
28 | |
29 | |
30 class RunTestFromArchiveTest(unittest.TestCase): | |
31 def test_load_manifest_empty(self): | |
32 m = run_test_from_archive.load_manifest('{}') | |
33 self.assertEquals({}, m) | |
34 | |
35 def test_load_manifest_good(self): | |
36 data = { | |
37 u'command': [u'foo', u'bar'], | |
38 u'files': { | |
39 u'a': { | |
40 u'link': u'somewhere', | |
41 u'mode': 123, | |
42 u'timestamp': 456, | |
43 }, | |
44 u'b': { | |
45 u'mode': 123, | |
46 u'sha-1': u'0123456789abcdef0123456789abcdef01234567' | |
47 } | |
48 }, | |
49 u'includes': [u'0123456789abcdef0123456789abcdef01234567'], | |
50 u'os': run_test_from_archive.get_flavor(), | |
51 u'read_only': False, | |
52 u'relative_cwd': u'somewhere_else' | |
53 } | |
54 m = run_test_from_archive.load_manifest(json.dumps(data)) | |
55 self.assertEquals(data, m) | |
56 | |
57 def test_load_manifest_bad(self): | |
58 data = { | |
59 u'files': { | |
60 u'a': { | |
61 u'link': u'somewhere', | |
62 u'sha-1': u'0123456789abcdef0123456789abcdef01234567' | |
63 } | |
64 }, | |
65 } | |
66 try: | |
67 run_test_from_archive.load_manifest(json.dumps(data)) | |
68 self.fail() | |
69 except run_test_from_archive.ConfigError: | |
70 pass | |
71 | |
72 def test_load_manifest_os_only(self): | |
73 data = { | |
74 u'os': run_test_from_archive.get_flavor(), | |
75 } | |
76 m = run_test_from_archive.load_manifest(json.dumps(data)) | |
77 self.assertEquals(data, m) | |
78 | |
79 def test_load_manifest_os_bad(self): | |
80 data = { | |
81 u'os': 'foo', | |
82 } | |
83 try: | |
84 run_test_from_archive.load_manifest(json.dumps(data)) | |
85 self.fail() | |
86 except run_test_from_archive.ConfigError: | |
87 pass | |
88 | |
89 def test_remote_no_errors(self): | |
90 files_to_handle = 50 | |
91 remote = RemoteTest('') | |
92 | |
93 for i in range(files_to_handle): | |
94 remote.add_item(run_test_from_archive.Remote.MED, i, i) | |
95 | |
96 for i in range(files_to_handle): | |
97 self.assertNotEqual(-1, remote.get_result()) | |
98 self.assertEqual(None, remote.next_exception()) | |
99 remote.join() | |
100 | |
101 def test_remote_with_errors(self): | |
102 remote = RemoteTest('') | |
103 | |
104 remote.add_item(run_test_from_archive.Remote.MED, IOError, '') | |
105 remote.add_item(run_test_from_archive.Remote.MED, Exception, '') | |
106 remote.join() | |
107 | |
108 self.assertNotEqual(None, remote.next_exception()) | |
109 self.assertNotEqual(None, remote.next_exception()) | |
110 self.assertEqual(None, remote.next_exception()) | |
111 | |
112 | |
113 if __name__ == '__main__': | |
114 logging.basicConfig( | |
115 level=(logging.DEBUG if '-v' in sys.argv else logging.ERROR)) | |
116 unittest.main() | |
OLD | NEW |