OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 2 # Copyright (c) 2011 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 """Unit tests for breakpad.py.""" |
| 7 |
| 8 # pylint: disable=W0403 |
| 9 |
| 10 # Fixes include path. |
| 11 from super_mox import SuperMoxTestBase |
| 12 |
| 13 import breakpad |
| 14 |
| 15 |
| 16 class Breakpad(SuperMoxTestBase): |
| 17 """Setups and tear downs the mocks but doesn't test anything as-is.""" |
| 18 def setUp(self): |
| 19 super(Breakpad, self).setUp() |
| 20 self.mox.StubOutWithMock(breakpad.atexit, 'register') |
| 21 self.mox.StubOutWithMock(breakpad.getpass, 'getuser') |
| 22 self.mox.StubOutWithMock(breakpad.urllib2, 'urlopen') |
| 23 breakpad._HOST_NAME = 'bozo' |
| 24 self._old_sys_argv = breakpad.sys.argv |
| 25 breakpad.sys.argv = ['my_test'] |
| 26 self._old_sys_version = breakpad.sys.version |
| 27 breakpad.sys.version = 'random python' |
| 28 |
| 29 def tearDown(self): |
| 30 breakpad.sys.version = self._old_sys_version |
| 31 breakpad.sys.argv = self._old_sys_argv |
| 32 super(Breakpad, self).tearDown() |
| 33 |
| 34 def testMembersChanged(self): |
| 35 members = [ |
| 36 'CheckForException', 'DEFAULT_URL', 'FormatException', 'Register', |
| 37 'SendProfiling', 'SendStack', |
| 38 'atexit', 'getpass', 'os', 'post', 'socket', 'sys', 'time', 'traceback', |
| 39 'urllib', 'urllib2', |
| 40 ] |
| 41 # If this test fails, you should add the relevant test. |
| 42 self.compareMembers(breakpad, members) |
| 43 |
| 44 def _part_1_setup_mocks(self, exception): |
| 45 breakpad.os.getcwd().AndReturn('/tmp/asdf') |
| 46 breakpad.getpass.getuser().AndReturn('georges') |
| 47 obj = self.mox.CreateMockAnything() |
| 48 kwargs = {} |
| 49 if (breakpad.sys.version_info[0] * 10 + breakpad.sys.version_info[1]) >= 26: |
| 50 kwargs['timeout'] = 4 |
| 51 breakpad.urllib2.urlopen( |
| 52 'https://chromium-status.appspot.com/breakpad', |
| 53 breakpad.urllib.urlencode([('exception', exception)]) + ( |
| 54 '&args=%5B%27my_test%27%5D' |
| 55 '&stack=bar' |
| 56 '&host=bozo' |
| 57 '&version=random+python' |
| 58 '&user=georges' |
| 59 '&cwd=%2Ftmp%2Fasdf'), |
| 60 **kwargs).AndReturn(obj) |
| 61 obj.read().AndReturn('ok') |
| 62 obj.close() |
| 63 |
| 64 def _part_2_verify_stdout(self, exception): |
| 65 self.checkstdout( |
| 66 ( "Sending crash report ...\n" |
| 67 " args: ['my_test']\n" |
| 68 " cwd: /tmp/asdf\n" |
| 69 " exception: %s\n" |
| 70 " host: bozo\n" |
| 71 " stack: bar\n" |
| 72 " user: georges\n" |
| 73 " version: random python\n" |
| 74 "ok\n") % exception) |
| 75 |
| 76 def _check(self, obj, result): |
| 77 self._part_1_setup_mocks(result) |
| 78 self.mox.ReplayAll() |
| 79 breakpad.SendStack(obj, 'bar') |
| 80 self._part_2_verify_stdout(result) |
| 81 |
| 82 def testSendBase(self): |
| 83 self._check('foo', 'foo') |
| 84 |
| 85 def testSendReprThrows(self): |
| 86 class Throws(object): |
| 87 def __repr__(self): |
| 88 raise NotImplementedError() |
| 89 def __str__(self): |
| 90 return '[foo]' |
| 91 self._check(Throws(), '[foo]') |
| 92 |
| 93 def testSendStrThrows(self): |
| 94 class Throws(object): |
| 95 def __repr__(self): |
| 96 return '[foo]' |
| 97 def __str__(self): |
| 98 raise NotImplementedError() |
| 99 self._check(Throws(), '[foo]') |
| 100 |
| 101 def testSendBoth(self): |
| 102 class Both(object): |
| 103 def __repr__(self): |
| 104 return '[foo]' |
| 105 def __str__(self): |
| 106 return '[bar]' |
| 107 self._check(Both(), '[bar]') |
| 108 |
| 109 def testSendException(self): |
| 110 obj = Exception('foo') |
| 111 obj.msg = 'a message' |
| 112 self._check(obj, 'foo\nMsg: a message') |
| 113 |
| 114 |
| 115 |
| 116 if __name__ == '__main__': |
| 117 import unittest |
| 118 unittest.main() |
OLD | NEW |