]> git.proxmox.com Git - ceph.git/blob - ceph/src/s3select/rapidjson/thirdparty/gtest/googletest/test/gtest_xml_output_unittest.py
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / s3select / rapidjson / thirdparty / gtest / googletest / test / gtest_xml_output_unittest.py
1 #!/usr/bin/env python
2 #
3 # Copyright 2006, Google Inc.
4 # All rights reserved.
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions are
8 # met:
9 #
10 # * Redistributions of source code must retain the above copyright
11 # notice, this list of conditions and the following disclaimer.
12 # * Redistributions in binary form must reproduce the above
13 # copyright notice, this list of conditions and the following disclaimer
14 # in the documentation and/or other materials provided with the
15 # distribution.
16 # * Neither the name of Google Inc. nor the names of its
17 # contributors may be used to endorse or promote products derived from
18 # this software without specific prior written permission.
19 #
20 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
32 """Unit test for the gtest_xml_output module"""
33
34 import datetime
35 import errno
36 import os
37 import re
38 import sys
39 from xml.dom import minidom, Node
40
41 import gtest_test_utils
42 import gtest_xml_test_utils
43
44 GTEST_FILTER_FLAG = '--gtest_filter'
45 GTEST_LIST_TESTS_FLAG = '--gtest_list_tests'
46 GTEST_OUTPUT_FLAG = '--gtest_output'
47 GTEST_DEFAULT_OUTPUT_FILE = 'test_detail.xml'
48 GTEST_PROGRAM_NAME = 'gtest_xml_output_unittest_'
49
50 # The environment variables for test sharding.
51 TOTAL_SHARDS_ENV_VAR = 'GTEST_TOTAL_SHARDS'
52 SHARD_INDEX_ENV_VAR = 'GTEST_SHARD_INDEX'
53 SHARD_STATUS_FILE_ENV_VAR = 'GTEST_SHARD_STATUS_FILE'
54
55 SUPPORTS_STACK_TRACES = False
56
57 if SUPPORTS_STACK_TRACES:
58 STACK_TRACE_TEMPLATE = '\nStack trace:\n*'
59 else:
60 STACK_TRACE_TEMPLATE = ''
61
62 EXPECTED_NON_EMPTY_XML = """<?xml version="1.0" encoding="UTF-8"?>
63 <testsuites tests="23" failures="4" disabled="2" errors="0" time="*" timestamp="*" name="AllTests" ad_hoc_property="42">
64 <testsuite name="SuccessfulTest" tests="1" failures="0" disabled="0" errors="0" time="*">
65 <testcase name="Succeeds" status="run" time="*" classname="SuccessfulTest"/>
66 </testsuite>
67 <testsuite name="FailedTest" tests="1" failures="1" disabled="0" errors="0" time="*">
68 <testcase name="Fails" status="run" time="*" classname="FailedTest">
69 <failure message="gtest_xml_output_unittest_.cc:*&#x0A;Expected equality of these values:&#x0A; 1&#x0A; 2" type=""><![CDATA[gtest_xml_output_unittest_.cc:*
70 Expected equality of these values:
71 1
72 2%(stack)s]]></failure>
73 </testcase>
74 </testsuite>
75 <testsuite name="MixedResultTest" tests="3" failures="1" disabled="1" errors="0" time="*">
76 <testcase name="Succeeds" status="run" time="*" classname="MixedResultTest"/>
77 <testcase name="Fails" status="run" time="*" classname="MixedResultTest">
78 <failure message="gtest_xml_output_unittest_.cc:*&#x0A;Expected equality of these values:&#x0A; 1&#x0A; 2" type=""><![CDATA[gtest_xml_output_unittest_.cc:*
79 Expected equality of these values:
80 1
81 2%(stack)s]]></failure>
82 <failure message="gtest_xml_output_unittest_.cc:*&#x0A;Expected equality of these values:&#x0A; 2&#x0A; 3" type=""><![CDATA[gtest_xml_output_unittest_.cc:*
83 Expected equality of these values:
84 2
85 3%(stack)s]]></failure>
86 </testcase>
87 <testcase name="DISABLED_test" status="notrun" time="*" classname="MixedResultTest"/>
88 </testsuite>
89 <testsuite name="XmlQuotingTest" tests="1" failures="1" disabled="0" errors="0" time="*">
90 <testcase name="OutputsCData" status="run" time="*" classname="XmlQuotingTest">
91 <failure message="gtest_xml_output_unittest_.cc:*&#x0A;Failed&#x0A;XML output: &lt;?xml encoding=&quot;utf-8&quot;&gt;&lt;top&gt;&lt;![CDATA[cdata text]]&gt;&lt;/top&gt;" type=""><![CDATA[gtest_xml_output_unittest_.cc:*
92 Failed
93 XML output: <?xml encoding="utf-8"><top><![CDATA[cdata text]]>]]&gt;<![CDATA[</top>%(stack)s]]></failure>
94 </testcase>
95 </testsuite>
96 <testsuite name="InvalidCharactersTest" tests="1" failures="1" disabled="0" errors="0" time="*">
97 <testcase name="InvalidCharactersInMessage" status="run" time="*" classname="InvalidCharactersTest">
98 <failure message="gtest_xml_output_unittest_.cc:*&#x0A;Failed&#x0A;Invalid characters in brackets []" type=""><![CDATA[gtest_xml_output_unittest_.cc:*
99 Failed
100 Invalid characters in brackets []%(stack)s]]></failure>
101 </testcase>
102 </testsuite>
103 <testsuite name="DisabledTest" tests="1" failures="0" disabled="1" errors="0" time="*">
104 <testcase name="DISABLED_test_not_run" status="notrun" time="*" classname="DisabledTest"/>
105 </testsuite>
106 <testsuite name="PropertyRecordingTest" tests="4" failures="0" disabled="0" errors="0" time="*" SetUpTestCase="yes" TearDownTestCase="aye">
107 <testcase name="OneProperty" status="run" time="*" classname="PropertyRecordingTest">
108 <properties>
109 <property name="key_1" value="1"/>
110 </properties>
111 </testcase>
112 <testcase name="IntValuedProperty" status="run" time="*" classname="PropertyRecordingTest">
113 <properties>
114 <property name="key_int" value="1"/>
115 </properties>
116 </testcase>
117 <testcase name="ThreeProperties" status="run" time="*" classname="PropertyRecordingTest">
118 <properties>
119 <property name="key_1" value="1"/>
120 <property name="key_2" value="2"/>
121 <property name="key_3" value="3"/>
122 </properties>
123 </testcase>
124 <testcase name="TwoValuesForOneKeyUsesLastValue" status="run" time="*" classname="PropertyRecordingTest">
125 <properties>
126 <property name="key_1" value="2"/>
127 </properties>
128 </testcase>
129 </testsuite>
130 <testsuite name="NoFixtureTest" tests="3" failures="0" disabled="0" errors="0" time="*">
131 <testcase name="RecordProperty" status="run" time="*" classname="NoFixtureTest">
132 <properties>
133 <property name="key" value="1"/>
134 </properties>
135 </testcase>
136 <testcase name="ExternalUtilityThatCallsRecordIntValuedProperty" status="run" time="*" classname="NoFixtureTest">
137 <properties>
138 <property name="key_for_utility_int" value="1"/>
139 </properties>
140 </testcase>
141 <testcase name="ExternalUtilityThatCallsRecordStringValuedProperty" status="run" time="*" classname="NoFixtureTest">
142 <properties>
143 <property name="key_for_utility_string" value="1"/>
144 </properties>
145 </testcase>
146 </testsuite>
147 <testsuite name="Single/ValueParamTest" tests="4" failures="0" disabled="0" errors="0" time="*">
148 <testcase name="HasValueParamAttribute/0" value_param="33" status="run" time="*" classname="Single/ValueParamTest" />
149 <testcase name="HasValueParamAttribute/1" value_param="42" status="run" time="*" classname="Single/ValueParamTest" />
150 <testcase name="AnotherTestThatHasValueParamAttribute/0" value_param="33" status="run" time="*" classname="Single/ValueParamTest" />
151 <testcase name="AnotherTestThatHasValueParamAttribute/1" value_param="42" status="run" time="*" classname="Single/ValueParamTest" />
152 </testsuite>
153 <testsuite name="TypedTest/0" tests="1" failures="0" disabled="0" errors="0" time="*">
154 <testcase name="HasTypeParamAttribute" type_param="*" status="run" time="*" classname="TypedTest/0" />
155 </testsuite>
156 <testsuite name="TypedTest/1" tests="1" failures="0" disabled="0" errors="0" time="*">
157 <testcase name="HasTypeParamAttribute" type_param="*" status="run" time="*" classname="TypedTest/1" />
158 </testsuite>
159 <testsuite name="Single/TypeParameterizedTestCase/0" tests="1" failures="0" disabled="0" errors="0" time="*">
160 <testcase name="HasTypeParamAttribute" type_param="*" status="run" time="*" classname="Single/TypeParameterizedTestCase/0" />
161 </testsuite>
162 <testsuite name="Single/TypeParameterizedTestCase/1" tests="1" failures="0" disabled="0" errors="0" time="*">
163 <testcase name="HasTypeParamAttribute" type_param="*" status="run" time="*" classname="Single/TypeParameterizedTestCase/1" />
164 </testsuite>
165 </testsuites>""" % {'stack': STACK_TRACE_TEMPLATE}
166
167 EXPECTED_FILTERED_TEST_XML = """<?xml version="1.0" encoding="UTF-8"?>
168 <testsuites tests="1" failures="0" disabled="0" errors="0" time="*"
169 timestamp="*" name="AllTests" ad_hoc_property="42">
170 <testsuite name="SuccessfulTest" tests="1" failures="0" disabled="0"
171 errors="0" time="*">
172 <testcase name="Succeeds" status="run" time="*" classname="SuccessfulTest"/>
173 </testsuite>
174 </testsuites>"""
175
176 EXPECTED_SHARDED_TEST_XML = """<?xml version="1.0" encoding="UTF-8"?>
177 <testsuites tests="3" failures="0" disabled="0" errors="0" time="*" timestamp="*" name="AllTests" ad_hoc_property="42">
178 <testsuite name="SuccessfulTest" tests="1" failures="0" disabled="0" errors="0" time="*">
179 <testcase name="Succeeds" status="run" time="*" classname="SuccessfulTest"/>
180 </testsuite>
181 <testsuite name="NoFixtureTest" tests="1" failures="0" disabled="0" errors="0" time="*">
182 <testcase name="RecordProperty" status="run" time="*" classname="NoFixtureTest">
183 <properties>
184 <property name="key" value="1"/>
185 </properties>
186 </testcase>
187 </testsuite>
188 <testsuite name="Single/ValueParamTest" tests="1" failures="0" disabled="0" errors="0" time="*">
189 <testcase name="AnotherTestThatHasValueParamAttribute/1" value_param="42" status="run" time="*" classname="Single/ValueParamTest" />
190 </testsuite>
191 </testsuites>"""
192
193 EXPECTED_EMPTY_XML = """<?xml version="1.0" encoding="UTF-8"?>
194 <testsuites tests="0" failures="0" disabled="0" errors="0" time="*"
195 timestamp="*" name="AllTests">
196 </testsuites>"""
197
198 GTEST_PROGRAM_PATH = gtest_test_utils.GetTestExecutablePath(GTEST_PROGRAM_NAME)
199
200 SUPPORTS_TYPED_TESTS = 'TypedTest' in gtest_test_utils.Subprocess(
201 [GTEST_PROGRAM_PATH, GTEST_LIST_TESTS_FLAG], capture_stderr=False).output
202
203
204 class GTestXMLOutputUnitTest(gtest_xml_test_utils.GTestXMLTestCase):
205 """
206 Unit test for Google Test's XML output functionality.
207 """
208
209 # This test currently breaks on platforms that do not support typed and
210 # type-parameterized tests, so we don't run it under them.
211 if SUPPORTS_TYPED_TESTS:
212 def testNonEmptyXmlOutput(self):
213 """
214 Runs a test program that generates a non-empty XML output, and
215 tests that the XML output is expected.
216 """
217 self._TestXmlOutput(GTEST_PROGRAM_NAME, EXPECTED_NON_EMPTY_XML, 1)
218
219 def testEmptyXmlOutput(self):
220 """Verifies XML output for a Google Test binary without actual tests.
221
222 Runs a test program that generates an empty XML output, and
223 tests that the XML output is expected.
224 """
225
226 self._TestXmlOutput('gtest_no_test_unittest', EXPECTED_EMPTY_XML, 0)
227
228 def testTimestampValue(self):
229 """Checks whether the timestamp attribute in the XML output is valid.
230
231 Runs a test program that generates an empty XML output, and checks if
232 the timestamp attribute in the testsuites tag is valid.
233 """
234 actual = self._GetXmlOutput('gtest_no_test_unittest', [], {}, 0)
235 date_time_str = actual.documentElement.getAttributeNode('timestamp').value
236 # datetime.strptime() is only available in Python 2.5+ so we have to
237 # parse the expected datetime manually.
238 match = re.match(r'(\d+)-(\d\d)-(\d\d)T(\d\d):(\d\d):(\d\d)', date_time_str)
239 self.assertTrue(
240 re.match,
241 'XML datettime string %s has incorrect format' % date_time_str)
242 date_time_from_xml = datetime.datetime(
243 year=int(match.group(1)), month=int(match.group(2)),
244 day=int(match.group(3)), hour=int(match.group(4)),
245 minute=int(match.group(5)), second=int(match.group(6)))
246
247 time_delta = abs(datetime.datetime.now() - date_time_from_xml)
248 # timestamp value should be near the current local time
249 self.assertTrue(time_delta < datetime.timedelta(seconds=600),
250 'time_delta is %s' % time_delta)
251 actual.unlink()
252
253 def testDefaultOutputFile(self):
254 """
255 Confirms that Google Test produces an XML output file with the expected
256 default name if no name is explicitly specified.
257 """
258 output_file = os.path.join(gtest_test_utils.GetTempDir(),
259 GTEST_DEFAULT_OUTPUT_FILE)
260 gtest_prog_path = gtest_test_utils.GetTestExecutablePath(
261 'gtest_no_test_unittest')
262 try:
263 os.remove(output_file)
264 except OSError, e:
265 if e.errno != errno.ENOENT:
266 raise
267
268 p = gtest_test_utils.Subprocess(
269 [gtest_prog_path, '%s=xml' % GTEST_OUTPUT_FLAG],
270 working_dir=gtest_test_utils.GetTempDir())
271 self.assert_(p.exited)
272 self.assertEquals(0, p.exit_code)
273 self.assert_(os.path.isfile(output_file))
274
275 def testSuppressedXmlOutput(self):
276 """
277 Tests that no XML file is generated if the default XML listener is
278 shut down before RUN_ALL_TESTS is invoked.
279 """
280
281 xml_path = os.path.join(gtest_test_utils.GetTempDir(),
282 GTEST_PROGRAM_NAME + 'out.xml')
283 if os.path.isfile(xml_path):
284 os.remove(xml_path)
285
286 command = [GTEST_PROGRAM_PATH,
287 '%s=xml:%s' % (GTEST_OUTPUT_FLAG, xml_path),
288 '--shut_down_xml']
289 p = gtest_test_utils.Subprocess(command)
290 if p.terminated_by_signal:
291 # p.signal is available only if p.terminated_by_signal is True.
292 self.assertFalse(
293 p.terminated_by_signal,
294 '%s was killed by signal %d' % (GTEST_PROGRAM_NAME, p.signal))
295 else:
296 self.assert_(p.exited)
297 self.assertEquals(1, p.exit_code,
298 "'%s' exited with code %s, which doesn't match "
299 'the expected exit code %s.'
300 % (command, p.exit_code, 1))
301
302 self.assert_(not os.path.isfile(xml_path))
303
304 def testFilteredTestXmlOutput(self):
305 """Verifies XML output when a filter is applied.
306
307 Runs a test program that executes only some tests and verifies that
308 non-selected tests do not show up in the XML output.
309 """
310
311 self._TestXmlOutput(GTEST_PROGRAM_NAME, EXPECTED_FILTERED_TEST_XML, 0,
312 extra_args=['%s=SuccessfulTest.*' % GTEST_FILTER_FLAG])
313
314 def testShardedTestXmlOutput(self):
315 """Verifies XML output when run using multiple shards.
316
317 Runs a test program that executes only one shard and verifies that tests
318 from other shards do not show up in the XML output.
319 """
320
321 self._TestXmlOutput(
322 GTEST_PROGRAM_NAME,
323 EXPECTED_SHARDED_TEST_XML,
324 0,
325 extra_env={SHARD_INDEX_ENV_VAR: '0',
326 TOTAL_SHARDS_ENV_VAR: '10'})
327
328 def _GetXmlOutput(self, gtest_prog_name, extra_args, extra_env,
329 expected_exit_code):
330 """
331 Returns the xml output generated by running the program gtest_prog_name.
332 Furthermore, the program's exit code must be expected_exit_code.
333 """
334 xml_path = os.path.join(gtest_test_utils.GetTempDir(),
335 gtest_prog_name + 'out.xml')
336 gtest_prog_path = gtest_test_utils.GetTestExecutablePath(gtest_prog_name)
337
338 command = ([gtest_prog_path, '%s=xml:%s' % (GTEST_OUTPUT_FLAG, xml_path)] +
339 extra_args)
340 environ_copy = os.environ.copy()
341 if extra_env:
342 environ_copy.update(extra_env)
343 p = gtest_test_utils.Subprocess(command, env=environ_copy)
344
345 if p.terminated_by_signal:
346 self.assert_(False,
347 '%s was killed by signal %d' % (gtest_prog_name, p.signal))
348 else:
349 self.assert_(p.exited)
350 self.assertEquals(expected_exit_code, p.exit_code,
351 "'%s' exited with code %s, which doesn't match "
352 'the expected exit code %s.'
353 % (command, p.exit_code, expected_exit_code))
354 actual = minidom.parse(xml_path)
355 return actual
356
357 def _TestXmlOutput(self, gtest_prog_name, expected_xml,
358 expected_exit_code, extra_args=None, extra_env=None):
359 """
360 Asserts that the XML document generated by running the program
361 gtest_prog_name matches expected_xml, a string containing another
362 XML document. Furthermore, the program's exit code must be
363 expected_exit_code.
364 """
365
366 actual = self._GetXmlOutput(gtest_prog_name, extra_args or [],
367 extra_env or {}, expected_exit_code)
368 expected = minidom.parseString(expected_xml)
369 self.NormalizeXml(actual.documentElement)
370 self.AssertEquivalentNodes(expected.documentElement,
371 actual.documentElement)
372 expected.unlink()
373 actual.unlink()
374
375
376 if __name__ == '__main__':
377 os.environ['GTEST_STACK_TRACE_DEPTH'] = '1'
378 gtest_test_utils.Main()