"Fossies" - the Fresh Open Source Software Archive

Member "buildbot-2.5.1/buildbot/test/unit/test_data_builds.py" (24 Nov 2019, 14318 Bytes) of package /linux/misc/buildbot-2.5.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code 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 
   16 
   17 import mock
   18 
   19 from twisted.internet import defer
   20 from twisted.trial import unittest
   21 
   22 from buildbot.data import builds
   23 from buildbot.data import resultspec
   24 from buildbot.test.fake import fakedb
   25 from buildbot.test.fake import fakemaster
   26 from buildbot.test.util import endpoint
   27 from buildbot.test.util import interfaces
   28 from buildbot.test.util.misc import TestReactorMixin
   29 from buildbot.util import epoch2datetime
   30 
   31 
   32 class BuildEndpoint(endpoint.EndpointMixin, unittest.TestCase):
   33 
   34     endpointClass = builds.BuildEndpoint
   35     resourceTypeClass = builds.Build
   36 
   37     def setUp(self):
   38         self.setUpEndpoint()
   39         self.db.insertTestData([
   40             fakedb.Builder(id=77, name='builder77'),
   41             fakedb.Master(id=88),
   42             fakedb.Worker(id=13, name='wrk'),
   43             fakedb.Buildset(id=8822),
   44             fakedb.BuildRequest(id=82, buildsetid=8822, builderid=77),
   45             fakedb.Build(id=13, builderid=77, masterid=88, workerid=13,
   46                          buildrequestid=82, number=3),
   47             fakedb.Build(id=14, builderid=77, masterid=88, workerid=13,
   48                          buildrequestid=82, number=4),
   49             fakedb.Build(id=15, builderid=77, masterid=88, workerid=13,
   50                          buildrequestid=82, number=5),
   51         ])
   52 
   53     def tearDown(self):
   54         self.tearDownEndpoint()
   55 
   56     @defer.inlineCallbacks
   57     def test_get_existing(self):
   58         build = yield self.callGet(('builds', 14))
   59         self.validateData(build)
   60         self.assertEqual(build['number'], 4)
   61 
   62     @defer.inlineCallbacks
   63     def test_get_missing(self):
   64         build = yield self.callGet(('builds', 9999))
   65         self.assertEqual(build, None)
   66 
   67     @defer.inlineCallbacks
   68     def test_get_missing_builder_number(self):
   69         build = yield self.callGet(('builders', 999, 'builds', 4))
   70         self.assertEqual(build, None)
   71 
   72     @defer.inlineCallbacks
   73     def test_get_builder_missing_number(self):
   74         build = yield self.callGet(('builders', 77, 'builds', 44))
   75         self.assertEqual(build, None)
   76 
   77     @defer.inlineCallbacks
   78     def test_get_builder_number(self):
   79         build = yield self.callGet(('builders', 77, 'builds', 5))
   80         self.validateData(build)
   81         self.assertEqual(build['buildid'], 15)
   82 
   83     @defer.inlineCallbacks
   84     def test_get_buildername_number(self):
   85         build = yield self.callGet(('builders', 'builder77', 'builds', 5))
   86         self.validateData(build)
   87         self.assertEqual(build['buildid'], 15)
   88 
   89     @defer.inlineCallbacks
   90     def test_get_buildername_not_existing_number(self):
   91         build = yield self.callGet(('builders', 'builder77_nope', 'builds', 5))
   92         self.assertEqual(build, None)
   93 
   94     @defer.inlineCallbacks
   95     def test_properties_injection(self):
   96         resultSpec = resultspec.OptimisedResultSpec(
   97             filters=[resultspec.Filter('property', 'eq', [False])])
   98         build = yield self.callGet(('builders', 77, 'builds', 5), resultSpec=resultSpec)
   99         self.validateData(build)
  100         self.assertIn('properties', build)
  101 
  102     @defer.inlineCallbacks
  103     def test_action_stop(self):
  104         yield self.callControl("stop", {}, ('builders', 77, 'builds', 5))
  105         self.master.mq.assertProductions(
  106             [(('control', 'builds', '15', 'stop'), {'reason': 'no reason'})])
  107 
  108     @defer.inlineCallbacks
  109     def test_action_stop_reason(self):
  110         yield self.callControl("stop", {'reason': 'because'}, ('builders', 77, 'builds', 5))
  111         self.master.mq.assertProductions(
  112             [(('control', 'builds', '15', 'stop'), {'reason': 'because'})])
  113 
  114     @defer.inlineCallbacks
  115     def test_action_rebuild(self):
  116         self.patch(self.master.data.updates, "rebuildBuildrequest",
  117                    mock.Mock(spec=self.master.data.updates.rebuildBuildrequest, return_value=(1, [2])))
  118         r = yield self.callControl("rebuild", {}, ('builders', 77, 'builds', 5))
  119         self.assertEqual(r, (1, [2]))
  120 
  121         buildrequest = yield self.master.data.get(('buildrequests', 82))
  122         self.master.data.updates.rebuildBuildrequest.assert_called_with(
  123             buildrequest)
  124 
  125 
  126 class BuildsEndpoint(endpoint.EndpointMixin, unittest.TestCase):
  127 
  128     endpointClass = builds.BuildsEndpoint
  129     resourceTypeClass = builds.Build
  130 
  131     def setUp(self):
  132         self.setUpEndpoint()
  133         self.db.insertTestData([
  134             fakedb.Builder(id=77, name='builder77'),
  135             fakedb.Builder(id=78, name='builder78'),
  136             fakedb.Builder(id=79, name='builder79'),
  137             fakedb.Master(id=88),
  138             fakedb.Worker(id=13, name='wrk'),
  139             fakedb.Buildset(id=8822),
  140             fakedb.BuildRequest(id=82, buildsetid=8822),
  141             fakedb.Build(id=13, builderid=77, masterid=88, workerid=13,
  142                          buildrequestid=82, number=3),
  143             fakedb.Build(id=14, builderid=77, masterid=88, workerid=13,
  144                          buildrequestid=82, number=4),
  145             fakedb.Build(id=15, builderid=78, masterid=88, workerid=12,
  146                          buildrequestid=83, number=5, complete_at=1),
  147             fakedb.Build(id=16, builderid=79, masterid=88, workerid=12,
  148                          buildrequestid=84, number=6, complete_at=1),
  149         ])
  150 
  151     def tearDown(self):
  152         self.tearDownEndpoint()
  153 
  154     @defer.inlineCallbacks
  155     def test_get_all(self):
  156         builds = yield self.callGet(('builds',))
  157         [self.validateData(build) for build in builds]
  158         self.assertEqual(sorted([b['number'] for b in builds]),
  159                          [3, 4, 5, 6])
  160 
  161     @defer.inlineCallbacks
  162     def test_get_builder(self):
  163         builds = yield self.callGet(('builders', 78, 'builds'))
  164         [self.validateData(build) for build in builds]
  165         self.assertEqual(sorted([b['number'] for b in builds]), [5])
  166 
  167     @defer.inlineCallbacks
  168     def test_get_buildername(self):
  169         builds = yield self.callGet(('builders', 'builder78', 'builds'))
  170         [self.validateData(build) for build in builds]
  171         self.assertEqual(sorted([b['number'] for b in builds]), [5])
  172 
  173     @defer.inlineCallbacks
  174     def test_get_buildername_not_existing(self):
  175         builds = yield self.callGet(('builders', 'builder78_nope', 'builds'))
  176         self.assertEqual(builds, [])
  177 
  178     @defer.inlineCallbacks
  179     def test_get_buildrequest(self):
  180         builds = yield self.callGet(('buildrequests', 82, 'builds'))
  181         [self.validateData(build) for build in builds]
  182         self.assertEqual(sorted([b['number'] for b in builds]), [3, 4])
  183 
  184     @defer.inlineCallbacks
  185     def test_get_buildrequest_not_existing(self):
  186         builds = yield self.callGet(('buildrequests', 899, 'builds'))
  187         self.assertEqual(builds, [])
  188 
  189     @defer.inlineCallbacks
  190     def test_get_buildrequest_via_filter(self):
  191         resultSpec = resultspec.OptimisedResultSpec(
  192             filters=[resultspec.Filter('buildrequestid', 'eq', [82])])
  193         builds = yield self.callGet(('builds',), resultSpec=resultSpec)
  194         [self.validateData(build) for build in builds]
  195         self.assertEqual(sorted([b['number'] for b in builds]), [3, 4])
  196 
  197     @defer.inlineCallbacks
  198     def test_get_buildrequest_via_filter_with_string(self):
  199         resultSpec = resultspec.OptimisedResultSpec(
  200             filters=[resultspec.Filter('buildrequestid', 'eq', ['82'])])
  201         builds = yield self.callGet(('builds',), resultSpec=resultSpec)
  202         [self.validateData(build) for build in builds]
  203         self.assertEqual(sorted([b['number'] for b in builds]), [3, 4])
  204 
  205     @defer.inlineCallbacks
  206     def test_get_worker(self):
  207         builds = yield self.callGet(('workers', 13, 'builds'))
  208         [self.validateData(build) for build in builds]
  209         self.assertEqual(sorted([b['number'] for b in builds]), [3, 4])
  210 
  211     @defer.inlineCallbacks
  212     def test_get_complete(self):
  213         resultSpec = resultspec.OptimisedResultSpec(
  214             filters=[resultspec.Filter('complete', 'eq', [False])])
  215         builds = yield self.callGet(('builds',), resultSpec=resultSpec)
  216         [self.validateData(build) for build in builds]
  217         self.assertEqual(sorted([b['number'] for b in builds]), [3, 4])
  218 
  219     @defer.inlineCallbacks
  220     def test_get_complete_at(self):
  221         resultSpec = resultspec.OptimisedResultSpec(
  222             filters=[resultspec.Filter('complete_at', 'eq', [None])])
  223         builds = yield self.callGet(('builds',), resultSpec=resultSpec)
  224         [self.validateData(build) for build in builds]
  225         self.assertEqual(sorted([b['number'] for b in builds]), [3, 4])
  226 
  227     @defer.inlineCallbacks
  228     def test_properties_injection(self):
  229         resultSpec = resultspec.OptimisedResultSpec(
  230             filters=[resultspec.Filter('property', 'eq', [False])])
  231         builds = yield self.callGet(('builds',), resultSpec=resultSpec)
  232         for b in builds:
  233             self.validateData(b)
  234             self.assertIn('properties', b)
  235 
  236     @defer.inlineCallbacks
  237     def test_get_filter_eq(self):
  238         resultSpec = resultspec.OptimisedResultSpec(
  239             filters=[resultspec.Filter('builderid', 'eq', [78, 79])])
  240         builds = yield self.callGet(('builds',), resultSpec=resultSpec)
  241         [self.validateData(b) for b in builds]
  242         self.assertEqual(sorted([b['number'] for b in builds]), [5, 6])
  243 
  244     @defer.inlineCallbacks
  245     def test_get_filter_ne(self):
  246         resultSpec = resultspec.OptimisedResultSpec(
  247             filters=[resultspec.Filter('builderid', 'ne', [78, 79])])
  248         builds = yield self.callGet(('builds',), resultSpec=resultSpec)
  249         [self.validateData(b) for b in builds]
  250         self.assertEqual(sorted([b['number'] for b in builds]), [3, 4])
  251 
  252 
  253 class Build(interfaces.InterfaceTests, TestReactorMixin, unittest.TestCase):
  254     new_build_event = {'builderid': 10,
  255                        'buildid': 100,
  256                        'buildrequestid': 13,
  257                        'workerid': 20,
  258                        'complete': False,
  259                        'complete_at': None,
  260                        'masterid': 824,
  261                        'number': 1,
  262                        'results': None,
  263                        'started_at': epoch2datetime(1),
  264                        'state_string': 'created',
  265                        'properties': {}}
  266 
  267     def setUp(self):
  268         self.setUpTestReactor()
  269         self.master = fakemaster.make_master(self, wantMq=True, wantDb=True,
  270                                              wantData=True)
  271         self.rtype = builds.Build(self.master)
  272 
  273     @defer.inlineCallbacks
  274     def do_test_callthrough(self, dbMethodName, method, exp_args=None,
  275                             exp_kwargs=None, *args, **kwargs):
  276         rv = (1, 2)
  277         m = mock.Mock(return_value=defer.succeed(rv))
  278         setattr(self.master.db.builds, dbMethodName, m)
  279         res = yield method(*args, **kwargs)
  280         self.assertIdentical(res, rv)
  281         m.assert_called_with(*(exp_args or args), **(exp_kwargs or kwargs))
  282 
  283     @defer.inlineCallbacks
  284     def do_test_event(self, method, exp_events=None,
  285                       *args, **kwargs):
  286         self.reactor.advance(1)
  287         if exp_events is None:
  288             exp_events = []
  289         yield method(*args, **kwargs)
  290         self.master.mq.assertProductions(exp_events)
  291 
  292     def test_signature_newBuild(self):
  293         @self.assertArgSpecMatches(
  294             self.master.data.updates.addBuild,  # fake
  295             self.rtype.addBuild)  # real
  296         def newBuild(self, builderid, buildrequestid, workerid):
  297             pass
  298 
  299     def test_newBuild(self):
  300         return self.do_test_callthrough('addBuild', self.rtype.addBuild,
  301                                         builderid=10, buildrequestid=13, workerid=20,
  302                                         exp_kwargs=dict(builderid=10, buildrequestid=13,
  303                                                         workerid=20, masterid=self.master.masterid,
  304                                                         state_string='created'))
  305 
  306     def test_newBuildEvent(self):
  307 
  308         @defer.inlineCallbacks
  309         def addBuild(*args, **kwargs):
  310             buildid, _ = yield self.rtype.addBuild(*args, **kwargs)
  311             yield self.rtype.generateNewBuildEvent(buildid)
  312             return None
  313 
  314         return self.do_test_event(addBuild,
  315                                   builderid=10, buildrequestid=13, workerid=20,
  316                                   exp_events=[(('builders', '10', 'builds', '1', 'new'), self.new_build_event),
  317                                               (('builds', '100', 'new'),
  318                                                self.new_build_event),
  319                                               (('workers', '20', 'builds', '100', 'new'), self.new_build_event)])
  320 
  321     def test_signature_setBuildStateString(self):
  322         @self.assertArgSpecMatches(
  323             self.master.data.updates.setBuildStateString,  # fake
  324             self.rtype.setBuildStateString)  # real
  325         def setBuildStateString(self, buildid, state_string):
  326             pass
  327 
  328     def test_setBuildStateString(self):
  329         return self.do_test_callthrough('setBuildStateString',
  330                                         self.rtype.setBuildStateString,
  331                                         buildid=10, state_string='a b')
  332 
  333     def test_signature_finishBuild(self):
  334         @self.assertArgSpecMatches(
  335             self.master.data.updates.finishBuild,  # fake
  336             self.rtype.finishBuild)  # real
  337         def finishBuild(self, buildid, results):
  338             pass
  339 
  340     def test_finishBuild(self):
  341         return self.do_test_callthrough('finishBuild', self.rtype.finishBuild,
  342                                         buildid=15, results=3)