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 """Verify basic usage of sharding_supervisor.""" |
| 7 |
| 8 import os |
| 9 import subprocess |
| 10 import sys |
| 11 import unittest |
| 12 |
| 13 import sharding_supervisor |
| 14 |
| 15 SHARDING_SUPERVISOR = os.path.join(os.path.dirname(sys.argv[0]), |
| 16 'sharding_supervisor.py') |
| 17 DUMMY_TEST = os.path.join(os.path.dirname(sys.argv[0]), 'dummy_test.py') |
| 18 NUM_CORES = sharding_supervisor.DetectNumCores() |
| 19 SHARDS_PER_CORE = sharding_supervisor.SS_DEFAULT_SHARDS_PER_CORE |
| 20 |
| 21 |
| 22 def generate_expected_output(start, end, num_shards): |
| 23 """Generate the expected stdout and stderr for the dummy test.""" |
| 24 stdout = '' |
| 25 for i in range(start, end): |
| 26 stdout += 'Running shard %d of %d\n' % (i, num_shards) |
| 27 stderr = '\nALL SHARDS PASSED!\nALL TESTS PASSED!\n' |
| 28 |
| 29 return (stdout, stderr) |
| 30 |
| 31 |
| 32 class ShardingSupervisorUnittest(unittest.TestCase): |
| 33 def test_basic_run(self): |
| 34 # Default test. |
| 35 expected_shards = NUM_CORES * SHARDS_PER_CORE |
| 36 (expected_out, expected_err) = \ |
| 37 generate_expected_output(0, expected_shards, expected_shards) |
| 38 p = subprocess.Popen([SHARDING_SUPERVISOR, '--no-color', DUMMY_TEST], |
| 39 stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 40 |
| 41 (out, err) = p.communicate() |
| 42 self.assertEqual(expected_out, out) |
| 43 self.assertEqual(expected_err, err) |
| 44 self.assertEqual(0, p.returncode) |
| 45 |
| 46 def test_shard_per_core(self): |
| 47 """Test the --shards_per_core parameter.""" |
| 48 expected_shards = NUM_CORES * 25 |
| 49 (expected_out, expected_err) = \ |
| 50 generate_expected_output(0, expected_shards, expected_shards) |
| 51 p = subprocess.Popen([SHARDING_SUPERVISOR, '--no-color', |
| 52 '--shards_per_core', '25', DUMMY_TEST], |
| 53 stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 54 |
| 55 (out, err) = p.communicate() |
| 56 self.assertEqual(expected_out, out) |
| 57 self.assertEqual(expected_err, err) |
| 58 self.assertEqual(0, p.returncode) |
| 59 |
| 60 def test_slave_sharding(self): |
| 61 """Test the --total-slaves and --slave-index parameters.""" |
| 62 total_shards = 6 |
| 63 expected_shards = NUM_CORES * SHARDS_PER_CORE * total_shards |
| 64 |
| 65 # Test every single index to make sure they run correctly. |
| 66 for index in range(total_shards): |
| 67 begin = NUM_CORES * SHARDS_PER_CORE * index |
| 68 end = begin + NUM_CORES * SHARDS_PER_CORE |
| 69 (expected_out, expected_err) = \ |
| 70 generate_expected_output(begin, end, expected_shards) |
| 71 p = subprocess.Popen([SHARDING_SUPERVISOR, '--no-color', |
| 72 '--total-slaves', str(total_shards), |
| 73 '--slave-index', str(index), |
| 74 DUMMY_TEST], |
| 75 stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 76 |
| 77 (out, err) = p.communicate() |
| 78 self.assertEqual(expected_out, out) |
| 79 self.assertEqual(expected_err, err) |
| 80 self.assertEqual(0, p.returncode) |
| 81 |
| 82 |
| 83 if __name__ == '__main__': |
| 84 unittest.main() |
OLD | NEW |