Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(412)

Side by Side Diff: tests/swarm_trigger_step_test.py

Issue 22980008: Merge all swarm_*.py scripts into swarming.py. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/tools/swarm_client
Patch Set: Rebase against r219402 Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « tests/swarm_get_results_test.py ('k') | tests/swarming_smoke_test.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 os
8 import StringIO
9 import sys
10 import unittest
11
12 import auto_stub
13
14 ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
15 sys.path.insert(0, ROOT_DIR)
16 import swarm_trigger_step
17
18 FILE_NAME = u'test.isolated'
19 FILE_HASH = u'brodoyouevenhash'
20 TEST_NAME = u'unit_tests'
21 STDOUT_FOR_TRIGGER_LEN = 188
22
23
24 def chromium_tasks(retrieval_url):
25 return [
26 {
27 u'action': [
28 u'python', u'run_isolated.zip',
29 u'--hash', FILE_HASH,
30 u'--remote', retrieval_url + u'default-gzip/',
31 ],
32 u'decorate_output': False,
33 u'test_name': u'Run Test',
34 u'time_out': 600,
35 },
36 {
37 u'action' : [
38 u'python', u'swarm_cleanup.py',
39 ],
40 u'decorate_output': False,
41 u'test_name': u'Clean Up',
42 u'time_out': 600,
43 }
44 ]
45
46
47 def generate_expected_json(
48 shards,
49 os_image,
50 working_dir,
51 data_server,
52 profile):
53 retrieval_url = data_server + '/content/retrieve/'
54 os_value = unicode(swarm_trigger_step.PLATFORM_MAPPING[os_image])
55 expected = {
56 u'cleanup': u'root',
57 u'configurations': [
58 {
59 u'config_name': os_value,
60 u'dimensions': {
61 u'os': os_value,
62 },
63 u'min_instances': shards,
64 },
65 ],
66 u'data': [[retrieval_url + u'default/', u'swarm_data.zip']],
67 u'env_vars': {},
68 u'restart_on_failure': True,
69 u'test_case_name': TEST_NAME,
70 u'tests': chromium_tasks(retrieval_url),
71 u'working_dir': unicode(working_dir),
72 u'priority': 101,
73 }
74 if shards > 1:
75 expected[u'env_vars'][u'GTEST_SHARD_INDEX'] = u'%(instance_index)s'
76 expected[u'env_vars'][u'GTEST_TOTAL_SHARDS'] = u'%(num_instances)s'
77 if profile:
78 expected[u'tests'][0][u'action'].append(u'--verbose')
79 return expected
80
81
82 def MockUrlOpen(url, _data, has_return_value):
83 if '/content/contains' in url:
84 return StringIO.StringIO(has_return_value)
85 return StringIO.StringIO('{}')
86
87
88 def MockUrlOpenHasZip(url, data=None, content_type=None):
89 assert content_type in (None, 'application/json', 'application/octet-stream')
90 return MockUrlOpen(url, data, has_return_value=chr(1))
91
92
93 def MockUrlOpenNoZip(url, data=None, content_type=None):
94 assert content_type in (None, 'application/json', 'application/octet-stream')
95 return MockUrlOpen(url, data, has_return_value=chr(0))
96
97
98 class ManifestTest(auto_stub.TestCase):
99 def setUp(self):
100 self.mock(swarm_trigger_step.time, 'sleep', lambda x: None)
101 self.mock(sys, 'stdout', StringIO.StringIO())
102 self.mock(sys, 'stderr', StringIO.StringIO())
103
104 def tearDown(self):
105 if not self.has_failed():
106 self._check_output('', '')
107 super(ManifestTest, self).tearDown()
108
109 def _check_output(self, out, err):
110 self.assertEqual(out, sys.stdout.getvalue())
111 self.assertEqual(err, sys.stderr.getvalue())
112
113 # Flush their content by mocking them again.
114 self.mock(sys, 'stdout', StringIO.StringIO())
115 self.mock(sys, 'stderr', StringIO.StringIO())
116
117 def test_basic_manifest(self):
118 manifest = swarm_trigger_step.Manifest(
119 manifest_hash=FILE_HASH,
120 test_name=TEST_NAME,
121 shards=2,
122 test_filter='*',
123 os_image='win32',
124 working_dir='swarm_tests',
125 data_server='http://localhost:8081',
126 verbose=False,
127 profile=False,
128 priority=101)
129
130 swarm_trigger_step.chromium_setup(manifest)
131 manifest_json = json.loads(manifest.to_json())
132
133 expected = generate_expected_json(
134 shards=2,
135 os_image='win32',
136 working_dir='swarm_tests',
137 data_server='http://localhost:8081',
138 profile=False)
139 self.assertEqual(expected, manifest_json)
140
141 def test_basic_linux(self):
142 """A basic linux manifest test to ensure that windows specific values
143 aren't used.
144 """
145 manifest = swarm_trigger_step.Manifest(
146 manifest_hash=FILE_HASH,
147 test_name=TEST_NAME,
148 shards=1,
149 test_filter='*',
150 os_image='linux2',
151 working_dir='swarm_tests',
152 data_server='http://localhost:8081',
153 verbose=False,
154 profile=False,
155 priority=101)
156
157 swarm_trigger_step.chromium_setup(manifest)
158 manifest_json = json.loads(manifest.to_json())
159
160 expected = generate_expected_json(
161 shards=1,
162 os_image='linux2',
163 working_dir='swarm_tests',
164 data_server='http://localhost:8081',
165 profile=False)
166 self.assertEqual(expected, manifest_json)
167
168 def test_basic_linux_profile(self):
169 manifest = swarm_trigger_step.Manifest(
170 manifest_hash=FILE_HASH,
171 test_name=TEST_NAME,
172 shards=1,
173 test_filter='*',
174 os_image='linux2',
175 working_dir='swarm_tests',
176 data_server='http://localhost:8081',
177 verbose=False,
178 profile=True,
179 priority=101)
180
181 swarm_trigger_step.chromium_setup(manifest)
182 manifest_json = json.loads(manifest.to_json())
183
184 expected = generate_expected_json(
185 shards=1,
186 os_image='linux2',
187 working_dir='swarm_tests',
188 data_server='http://localhost:8081',
189 profile=True)
190 self.assertEqual(expected, manifest_json)
191
192 def test_process_manifest_success(self):
193 self.mock(swarm_trigger_step.run_isolated, 'url_open', MockUrlOpenNoZip)
194
195 result = swarm_trigger_step.process_manifest(
196 file_sha1=FILE_HASH,
197 test_name=TEST_NAME,
198 shards=1,
199 test_filter='*',
200 os_image='linux2',
201 working_dir='swarm_tests',
202 data_server='http://localhost:8081',
203 swarm_url='http://localhost:8082',
204 verbose=False,
205 profile=False,
206 priority=101)
207 self.assertEqual(0, result)
208
209 # Just assert it printed enough, since it contains variable output.
210 out = sys.stdout.getvalue()
211 self.assertTrue(len(out) > STDOUT_FOR_TRIGGER_LEN)
212 self.assertTrue('Zip file not on server, starting uploading.' in out)
213 self.mock(sys, 'stdout', StringIO.StringIO())
214
215 def test_process_manifest_success_zip_already_uploaded(self):
216 self.mock(swarm_trigger_step.run_isolated, 'url_open', MockUrlOpenHasZip)
217
218 result = swarm_trigger_step.process_manifest(
219 file_sha1=FILE_HASH,
220 test_name=TEST_NAME,
221 shards=1,
222 test_filter='*',
223 os_image='linux2',
224 working_dir='swarm_tests',
225 data_server='http://localhost:8081',
226 swarm_url='http://localhost:8082',
227 verbose=False,
228 profile=False,
229 priority=101)
230 self.assertEqual(0, result)
231
232 # Just assert it printed enough, since it contains variable output.
233 out = sys.stdout.getvalue()
234 self.assertTrue(len(out) > STDOUT_FOR_TRIGGER_LEN)
235 self.assertTrue('Zip file already on server, no need to reupload.' in out)
236 self.mock(sys, 'stdout', StringIO.StringIO())
237
238 def test_no_dir(self):
239 try:
240 swarm_trigger_step.main([])
241 self.fail()
242 except SystemExit as e:
243 self.assertEqual(2, e.code)
244 self._check_output(
245 '',
246 'Usage: swarm_trigger_step_test.py [options]\n\n'
247 'swarm_trigger_step_test.py: error: Must specify the data '
248 'directory\n')
249
250 def test_no_request(self):
251 try:
252 swarm_trigger_step.main(['-d', '.'])
253 self.fail()
254 except SystemExit as e:
255 self.assertEqual(2, e.code)
256 self._check_output(
257 '',
258 'Usage: swarm_trigger_step_test.py [options]\n\n'
259 'swarm_trigger_step_test.py: error: At least one --run_from_hash is '
260 'required.\n')
261
262
263 if __name__ == '__main__':
264 if '-v' in sys.argv:
265 unittest.TestCase.maxDiff = None
266 unittest.main()
OLDNEW
« no previous file with comments | « tests/swarm_get_results_test.py ('k') | tests/swarming_smoke_test.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698