PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/master/buildbot/test/unit/test_www_hooks_bitbucket.py

https://gitlab.com/murder187ss/buildbot
Python | 267 lines | 247 code | 5 blank | 15 comment | 0 complexity | 6751e733213664f34efe9a1e47bd1d15 MD5 | raw file
  1. # This file is part of Buildbot. Buildbot is free software: you can
  2. # redistribute it and/or modify it under the terms of the GNU General Public
  3. # License as published by the Free Software Foundation, version 2.
  4. #
  5. # This program is distributed in the hope that it will be useful, but WITHOUT
  6. # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  7. # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  8. # details.
  9. #
  10. # You should have received a copy of the GNU General Public License along with
  11. # this program; if not, write to the Free Software Foundation, Inc., 51
  12. # Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  13. #
  14. # Copyright Buildbot Team Members
  15. # Copyright Manba Team
  16. import calendar
  17. from twisted.internet.defer import inlineCallbacks
  18. from twisted.trial import unittest
  19. import buildbot.www.change_hook as change_hook
  20. from buildbot.test.fake.web import FakeRequest
  21. from buildbot.test.fake.web import fakeMasterForHooks
  22. gitJsonPayload = """{
  23. "canon_url": "https://bitbucket.org",
  24. "commits": [
  25. {
  26. "author": "marcus",
  27. "branch": "master",
  28. "files": [
  29. {
  30. "file": "somefile.py",
  31. "type": "modified"
  32. }
  33. ],
  34. "message": "Added some more things to somefile.py",
  35. "node": "620ade18607a",
  36. "parents": [
  37. "702c70160afc"
  38. ],
  39. "raw_author": "Marcus Bertrand <marcus@somedomain.com>",
  40. "raw_node": "620ade18607ac42d872b568bb92acaa9a28620e9",
  41. "revision": null,
  42. "size": -1,
  43. "timestamp": "2012-05-30 05:58:56",
  44. "utctimestamp": "2012-05-30 03:58:56+00:00"
  45. }
  46. ],
  47. "repository": {
  48. "absolute_url": "/marcus/project-x/",
  49. "fork": false,
  50. "is_private": true,
  51. "name": "Project X",
  52. "owner": "marcus",
  53. "scm": "git",
  54. "slug": "project-x",
  55. "website": "https://atlassian.com/"
  56. },
  57. "user": "marcus"
  58. }"""
  59. mercurialJsonPayload = """{
  60. "canon_url": "https://bitbucket.org",
  61. "commits": [
  62. {
  63. "author": "marcus",
  64. "branch": "master",
  65. "files": [
  66. {
  67. "file": "somefile.py",
  68. "type": "modified"
  69. }
  70. ],
  71. "message": "Added some more things to somefile.py",
  72. "node": "620ade18607a",
  73. "parents": [
  74. "702c70160afc"
  75. ],
  76. "raw_author": "Marcus Bertrand <marcus@somedomain.com>",
  77. "raw_node": "620ade18607ac42d872b568bb92acaa9a28620e9",
  78. "revision": null,
  79. "size": -1,
  80. "timestamp": "2012-05-30 05:58:56",
  81. "utctimestamp": "2012-05-30 03:58:56+00:00"
  82. }
  83. ],
  84. "repository": {
  85. "absolute_url": "/marcus/project-x/",
  86. "fork": false,
  87. "is_private": true,
  88. "name": "Project X",
  89. "owner": "marcus",
  90. "scm": "hg",
  91. "slug": "project-x",
  92. "website": "https://atlassian.com/"
  93. },
  94. "user": "marcus"
  95. }"""
  96. gitJsonNoCommitsPayload = """{
  97. "canon_url": "https://bitbucket.org",
  98. "commits": [
  99. ],
  100. "repository": {
  101. "absolute_url": "/marcus/project-x/",
  102. "fork": false,
  103. "is_private": true,
  104. "name": "Project X",
  105. "owner": "marcus",
  106. "scm": "git",
  107. "slug": "project-x",
  108. "website": "https://atlassian.com/"
  109. },
  110. "user": "marcus"
  111. }"""
  112. mercurialJsonNoCommitsPayload = """{
  113. "canon_url": "https://bitbucket.org",
  114. "commits": [
  115. ],
  116. "repository": {
  117. "absolute_url": "/marcus/project-x/",
  118. "fork": false,
  119. "is_private": true,
  120. "name": "Project X",
  121. "owner": "marcus",
  122. "scm": "hg",
  123. "slug": "project-x",
  124. "website": "https://atlassian.com/"
  125. },
  126. "user": "marcus"
  127. }"""
  128. class TestChangeHookConfiguredWithBitbucketChange(unittest.TestCase):
  129. """Unit tests for BitBucket Change Hook
  130. """
  131. def setUp(self):
  132. self.change_hook = change_hook.ChangeHookResource(
  133. dialects={'bitbucket': True}, master=fakeMasterForHooks())
  134. @inlineCallbacks
  135. def testGitWithChange(self):
  136. change_dict = {'payload': [gitJsonPayload]}
  137. request = FakeRequest(change_dict)
  138. request.uri = '/change_hook/bitbucket'
  139. request.method = 'POST'
  140. yield request.test_render(self.change_hook)
  141. self.assertEqual(len(self.change_hook.master.addedChanges), 1)
  142. commit = self.change_hook.master.addedChanges[0]
  143. self.assertEqual(commit['files'], ['somefile.py'])
  144. self.assertEqual(
  145. commit['repository'], 'https://bitbucket.org/marcus/project-x/')
  146. self.assertEqual(
  147. calendar.timegm(commit['when_timestamp'].utctimetuple()),
  148. 1338350336
  149. )
  150. self.assertEqual(
  151. commit['author'], 'Marcus Bertrand <marcus@somedomain.com>')
  152. self.assertEqual(
  153. commit['revision'], '620ade18607ac42d872b568bb92acaa9a28620e9')
  154. self.assertEqual(
  155. commit['comments'], 'Added some more things to somefile.py')
  156. self.assertEqual(commit['branch'], 'master')
  157. self.assertEqual(
  158. commit['revlink'],
  159. 'https://bitbucket.org/marcus/project-x/commits/'
  160. '620ade18607ac42d872b568bb92acaa9a28620e9'
  161. )
  162. @inlineCallbacks
  163. def testGitWithNoCommitsPayload(self):
  164. change_dict = {'payload': [gitJsonNoCommitsPayload]}
  165. request = FakeRequest(change_dict)
  166. request.uri = '/change_hook/bitbucket'
  167. request.method = 'POST'
  168. yield request.test_render(self.change_hook)
  169. self.assertEqual(len(self.change_hook.master.addedChanges), 0)
  170. self.assertEqual(request.written, 'no changes found')
  171. @inlineCallbacks
  172. def testMercurialWithChange(self):
  173. change_dict = {'payload': [mercurialJsonPayload]}
  174. request = FakeRequest(change_dict)
  175. request.uri = '/change_hook/bitbucket'
  176. request.method = 'POST'
  177. yield request.test_render(self.change_hook)
  178. self.assertEqual(len(self.change_hook.master.addedChanges), 1)
  179. commit = self.change_hook.master.addedChanges[0]
  180. self.assertEqual(commit['files'], ['somefile.py'])
  181. self.assertEqual(
  182. commit['repository'], 'https://bitbucket.org/marcus/project-x/')
  183. self.assertEqual(
  184. calendar.timegm(commit['when_timestamp'].utctimetuple()),
  185. 1338350336
  186. )
  187. self.assertEqual(
  188. commit['author'], 'Marcus Bertrand <marcus@somedomain.com>')
  189. self.assertEqual(
  190. commit['revision'], '620ade18607ac42d872b568bb92acaa9a28620e9')
  191. self.assertEqual(
  192. commit['comments'], 'Added some more things to somefile.py')
  193. self.assertEqual(commit['branch'], 'master')
  194. self.assertEqual(
  195. commit['revlink'],
  196. 'https://bitbucket.org/marcus/project-x/commits/'
  197. '620ade18607ac42d872b568bb92acaa9a28620e9'
  198. )
  199. @inlineCallbacks
  200. def testMercurialWithNoCommitsPayload(self):
  201. change_dict = {'payload': [mercurialJsonNoCommitsPayload]}
  202. request = FakeRequest(change_dict)
  203. request.uri = '/change_hook/bitbucket'
  204. request.method = 'POST'
  205. yield request.test_render(self.change_hook)
  206. self.assertEqual(len(self.change_hook.master.addedChanges), 0)
  207. self.assertEqual(request.written, 'no changes found')
  208. @inlineCallbacks
  209. def testWithNoJson(self):
  210. request = FakeRequest()
  211. request.uri = '/change_hook/bitbucket'
  212. request.method = 'POST'
  213. yield request.test_render(self.change_hook)
  214. self.assertEqual(len(self.change_hook.master.addedChanges), 0)
  215. self.assertEqual(request.written, 'Error processing changes.')
  216. request.setResponseCode.assert_called_with(
  217. 500, 'Error processing changes.')
  218. self.assertEqual(len(self.flushLoggedErrors()), 1)
  219. @inlineCallbacks
  220. def testGitWithChangeAndProject(self):
  221. change_dict = {
  222. 'payload': [gitJsonPayload],
  223. 'project': ['project-name']}
  224. request = FakeRequest(change_dict)
  225. request.uri = '/change_hook/bitbucket'
  226. request.method = 'POST'
  227. yield request.test_render(self.change_hook)
  228. self.assertEqual(len(self.change_hook.master.addedChanges), 1)
  229. commit = self.change_hook.master.addedChanges[0]
  230. self.assertEqual(commit['project'], 'project-name')