PageRenderTime 93ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/master/buildbot/test/unit/test_data_changesources.py

https://gitlab.com/murder187ss/buildbot
Python | 232 lines | 206 code | 11 blank | 15 comment | 0 complexity | 3d2c0d862f098d6fcc8c38e98aa669fd 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. import mock
  16. from twisted.internet import defer
  17. from twisted.python import failure
  18. from twisted.trial import unittest
  19. from buildbot.data import changesources
  20. from buildbot.db.changesources import ChangeSourceAlreadyClaimedError
  21. from buildbot.test.fake import fakedb
  22. from buildbot.test.fake import fakemaster
  23. from buildbot.test.util import endpoint
  24. from buildbot.test.util import interfaces
  25. class ChangeSourceEndpoint(endpoint.EndpointMixin, unittest.TestCase):
  26. endpointClass = changesources.ChangeSourceEndpoint
  27. resourceTypeClass = changesources.ChangeSource
  28. def setUp(self):
  29. self.setUpEndpoint()
  30. self.db.insertTestData([
  31. fakedb.Master(id=22, active=0),
  32. fakedb.Master(id=33, active=1),
  33. fakedb.ChangeSource(id=13, name='some:changesource'),
  34. fakedb.ChangeSourceMaster(changesourceid=13, masterid=None),
  35. fakedb.ChangeSource(id=14, name='other:changesource'),
  36. fakedb.ChangeSourceMaster(changesourceid=14, masterid=22),
  37. fakedb.ChangeSource(id=15, name='another:changesource'),
  38. fakedb.ChangeSourceMaster(changesourceid=15, masterid=33),
  39. ])
  40. def tearDown(self):
  41. self.tearDownEndpoint()
  42. def test_get_existing(self):
  43. """get an existing changesource by id"""
  44. d = self.callGet(('changesources', 14))
  45. @d.addCallback
  46. def check(changesource):
  47. self.validateData(changesource)
  48. self.assertEqual(changesource['name'], 'other:changesource')
  49. return d
  50. def test_get_no_master(self):
  51. """get a changesource with no master"""
  52. d = self.callGet(('changesources', 13))
  53. @d.addCallback
  54. def check(changesource):
  55. self.validateData(changesource)
  56. self.assertEqual(changesource['master'], None),
  57. return d
  58. def test_get_masterid_existing(self):
  59. """get an existing changesource by id on certain master"""
  60. d = self.callGet(('masters', 22, 'changesources', 14))
  61. @d.addCallback
  62. def check(changesource):
  63. self.validateData(changesource)
  64. self.assertEqual(changesource['name'], 'other:changesource')
  65. return d
  66. def test_get_masterid_no_match(self):
  67. """get an existing changesource by id on the wrong master"""
  68. d = self.callGet(('masters', 33, 'changesources', 13))
  69. @d.addCallback
  70. def check(changesource):
  71. self.assertEqual(changesource, None)
  72. return d
  73. def test_get_masterid_missing(self):
  74. """get an existing changesource by id on an invalid master"""
  75. d = self.callGet(('masters', 25, 'changesources', 13))
  76. @d.addCallback
  77. def check(changesource):
  78. self.assertEqual(changesource, None)
  79. return d
  80. def test_get_missing(self):
  81. """get an invalid changesource by id"""
  82. d = self.callGet(('changesources', 99))
  83. @d.addCallback
  84. def check(changesource):
  85. self.assertEqual(changesource, None)
  86. return d
  87. class ChangeSourcesEndpoint(endpoint.EndpointMixin, unittest.TestCase):
  88. endpointClass = changesources.ChangeSourcesEndpoint
  89. resourceTypeClass = changesources.ChangeSource
  90. def setUp(self):
  91. self.setUpEndpoint()
  92. self.db.insertTestData([
  93. fakedb.Master(id=22, active=0),
  94. fakedb.Master(id=33, active=1),
  95. fakedb.ChangeSource(id=13, name='some:changesource'),
  96. fakedb.ChangeSourceMaster(changesourceid=13, masterid=None),
  97. fakedb.ChangeSource(id=14, name='other:changesource'),
  98. fakedb.ChangeSourceMaster(changesourceid=14, masterid=22),
  99. fakedb.ChangeSource(id=15, name='another:changesource'),
  100. fakedb.ChangeSourceMaster(changesourceid=15, masterid=33),
  101. fakedb.ChangeSource(id=16, name='wholenother:changesource'),
  102. fakedb.ChangeSourceMaster(changesourceid=16, masterid=33),
  103. ])
  104. def tearDown(self):
  105. self.tearDownEndpoint()
  106. def test_get(self):
  107. d = self.callGet(('changesources',))
  108. @d.addCallback
  109. def check(changesources):
  110. [self.validateData(cs) for cs in changesources]
  111. self.assertEqual(sorted([m['changesourceid'] for m in changesources]),
  112. [13, 14, 15, 16])
  113. return d
  114. def test_get_masterid(self):
  115. d = self.callGet(('masters', 33, 'changesources'))
  116. @d.addCallback
  117. def check(changesources):
  118. [self.validateData(cs) for cs in changesources]
  119. self.assertEqual(sorted([m['changesourceid'] for m in changesources]),
  120. [15, 16])
  121. return d
  122. def test_get_masterid_missing(self):
  123. d = self.callGet(('masters', 23, 'changesources'))
  124. @d.addCallback
  125. def check(changesources):
  126. self.assertEqual(changesources, [])
  127. return d
  128. class ChangeSource(interfaces.InterfaceTests, unittest.TestCase):
  129. def setUp(self):
  130. self.master = fakemaster.make_master(wantMq=True, wantDb=True,
  131. wantData=True, testcase=self)
  132. self.rtype = changesources.ChangeSource(self.master)
  133. def test_signature_findChangeSourceId(self):
  134. @self.assertArgSpecMatches(
  135. self.master.data.updates.findChangeSourceId, # fake
  136. self.rtype.findChangeSourceId) # real
  137. def findChangeSourceId(self, name):
  138. pass
  139. @defer.inlineCallbacks
  140. def test_findChangeSourceId(self):
  141. self.master.db.changesources.findChangeSourceId = mock.Mock(
  142. return_value=defer.succeed(10))
  143. self.assertEqual((yield self.rtype.findChangeSourceId(u'cs')), 10)
  144. self.master.db.changesources.findChangeSourceId.assert_called_with(u'cs')
  145. def test_signature_trySetChangeSourceMaster(self):
  146. @self.assertArgSpecMatches(
  147. self.master.data.updates.trySetChangeSourceMaster, # fake
  148. self.rtype.trySetChangeSourceMaster) # real
  149. def trySetChangeSourceMaster(self, changesourceid, masterid):
  150. pass
  151. @defer.inlineCallbacks
  152. def test_trySetChangeSourceMaster_succeeds(self):
  153. self.master.db.changesources.setChangeSourceMaster = mock.Mock(
  154. return_value=defer.succeed(None))
  155. yield self.rtype.trySetChangeSourceMaster(10, 20)
  156. self.master.db.changesources.setChangeSourceMaster.assert_called_with(10, 20)
  157. @defer.inlineCallbacks
  158. def test_trySetChangeSourceMaster_fails(self):
  159. d = defer.fail(failure.Failure(
  160. ChangeSourceAlreadyClaimedError('oh noes')))
  161. self.master.db.changesources.setChangeSourceMaster = mock.Mock(
  162. return_value=d)
  163. result = yield self.rtype.trySetChangeSourceMaster(10, 20)
  164. self.assertFalse(result)
  165. @defer.inlineCallbacks
  166. def test_trySetChangeSourceMaster_raisesOddException(self):
  167. d = defer.fail(failure.Failure(RuntimeError('oh noes')))
  168. self.master.db.changesources.setChangeSourceMaster = mock.Mock(
  169. return_value=d)
  170. try:
  171. yield self.rtype.trySetChangeSourceMaster(10, 20)
  172. except RuntimeError:
  173. pass
  174. else:
  175. self.fail("The RuntimeError did not propogate")
  176. @defer.inlineCallbacks
  177. def test__masterDeactivated(self):
  178. yield self.master.db.insertTestData([
  179. fakedb.Master(id=22, active=0),
  180. fakedb.ChangeSource(id=13, name='some:changesource'),
  181. fakedb.ChangeSourceMaster(changesourceid=13, masterid=22),
  182. fakedb.ChangeSource(id=14, name='other:changesource'),
  183. fakedb.ChangeSourceMaster(changesourceid=14, masterid=22),
  184. ])
  185. yield self.rtype._masterDeactivated(22)
  186. self.master.db.changesources.assertChangeSourceMaster(13, None)
  187. self.master.db.changesources.assertChangeSourceMaster(14, None)