OLD | NEW |
---|---|
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 """Verify basic usage of sharding_supervisor.""" | 6 """Verify basic usage of sharding_supervisor.""" |
7 | 7 |
8 import difflib | |
8 import os | 9 import os |
9 import subprocess | 10 import subprocess |
10 import sys | 11 import sys |
11 import unittest | 12 import unittest |
12 | 13 |
14 from xml.dom import minidom | |
M-A Ruel
2012/07/10 14:26:50
IMHO, xml.etree.ElementTree is much easier&cleaner
| |
15 | |
13 import sharding_supervisor | 16 import sharding_supervisor |
14 | 17 |
15 SHARDING_SUPERVISOR = os.path.join(os.path.dirname(sys.argv[0]), | 18 SHARDING_SUPERVISOR = os.path.join(os.path.dirname(sys.argv[0]), |
M-A Ruel
2012/07/10 14:26:50
Add
ROOT_DIR = os.path.dirname(os.path.abspath(__f
Mihai Parparita -not on Chrome
2012/07/10 20:15:16
Done.
| |
16 'sharding_supervisor.py') | 19 'sharding_supervisor.py') |
17 DUMMY_TEST = os.path.join(os.path.dirname(sys.argv[0]), 'dummy_test.py') | 20 DUMMY_TEST = os.path.join(os.path.dirname(sys.argv[0]), 'dummy_test.py') |
18 NUM_CORES = sharding_supervisor.DetectNumCores() | 21 NUM_CORES = sharding_supervisor.DetectNumCores() |
19 SHARDS_PER_CORE = sharding_supervisor.SS_DEFAULT_SHARDS_PER_CORE | 22 SHARDS_PER_CORE = sharding_supervisor.SS_DEFAULT_SHARDS_PER_CORE |
20 | 23 |
21 | 24 |
22 def generate_expected_output(start, end, num_shards): | 25 def generate_expected_output(start, end, num_shards): |
23 """Generate the expected stdout and stderr for the dummy test.""" | 26 """Generate the expected stdout and stderr for the dummy test.""" |
24 stdout = '' | 27 stdout = '' |
25 stderr = '' | 28 stderr = '' |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
73 '--total-slaves', str(total_shards), | 76 '--total-slaves', str(total_shards), |
74 '--slave-index', str(index), | 77 '--slave-index', str(index), |
75 DUMMY_TEST], | 78 DUMMY_TEST], |
76 stdout=subprocess.PIPE, stderr=subprocess.PIPE) | 79 stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
77 | 80 |
78 (out, err) = p.communicate() | 81 (out, err) = p.communicate() |
79 self.assertEqual(expected_out, out) | 82 self.assertEqual(expected_out, out) |
80 self.assertEqual(expected_err, err) | 83 self.assertEqual(expected_err, err) |
81 self.assertEqual(0, p.returncode) | 84 self.assertEqual(0, p.returncode) |
82 | 85 |
86 def test_append_to_xml(self): | |
87 test_directory = os.path.abspath(os.path.dirname(__file__)) | |
M-A Ruel
2012/07/10 14:26:50
Move that as a constant at the top of the file, si
| |
88 shard_xml_path = os.path.join(test_directory, 'gtest_results.xml') | |
M-A Ruel
2012/07/10 14:26:50
Please move all the test data files in a 'data' su
Mihai Parparita -not on Chrome
2012/07/10 20:15:16
Done.
| |
89 expected_xml_path = \ | |
M-A Ruel
2012/07/10 14:26:50
expected_xml_path = os.path.join(
...
we don't
Mihai Parparita -not on Chrome
2012/07/10 20:15:16
Done.
| |
90 os.path.join(test_directory, 'gtest_results_expected.xml') | |
91 merged_xml = sharding_supervisor.AppendToXML(None, shard_xml_path, 0) | |
92 merged_xml = sharding_supervisor.AppendToXML(merged_xml, shard_xml_path, 1) | |
93 | |
94 with open(expected_xml_path) as expected_xml_file: | |
95 expected_xml = minidom.parse(expected_xml_file) | |
96 | |
97 # Serialize XML to a list of strings that is consistently formatted | |
98 # (ignoring whitespace between elements) so that it may be compared. | |
99 def _serialize_xml(xml): | |
100 def _remove_whitespace_and_comments(xml): | |
101 children_to_remove = [] | |
102 for child in xml.childNodes: | |
103 if child.nodeType == minidom.Node.TEXT_NODE and \ | |
M-A Ruel
2012/07/10 14:26:50
same
Mihai Parparita -not on Chrome
2012/07/10 20:15:16
Done (and a bunch more that were already in the fi
| |
104 not child.data.strip(): | |
105 children_to_remove.append(child) | |
106 elif child.nodeType == minidom.Node.COMMENT_NODE: | |
107 children_to_remove.append(child) | |
108 elif child.nodeType == minidom.Node.ELEMENT_NODE: | |
109 _remove_whitespace_and_comments(child) | |
110 | |
111 for child in children_to_remove: | |
112 xml.removeChild(child) | |
113 | |
114 _remove_whitespace_and_comments(xml) | |
115 return xml.toprettyxml(indent=' ').splitlines() | |
116 | |
117 diff = list(difflib.unified_diff( | |
118 _serialize_xml(expected_xml), | |
119 _serialize_xml(merged_xml), | |
120 fromfile='gtest_results_expected.xml', | |
121 tofile='gtest_results_actual.xml')) | |
122 if diff: | |
123 self.fail('Did not merge results XML correctly:\n' + '\n'.join(diff)) | |
M-A Ruel
2012/07/10 14:26:50
alignment
Mihai Parparita -not on Chrome
2012/07/10 20:15:16
Done.
| |
83 | 124 |
M-A Ruel
2012/07/10 14:26:50
2 vertical lines between file-level symbols.
Mihai Parparita -not on Chrome
2012/07/10 20:15:16
Done.
| |
84 if __name__ == '__main__': | 125 if __name__ == '__main__': |
85 unittest.main() | 126 unittest.main() |
OLD | NEW |