breakpad_unittest.py 3.6 KB

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