def test_render(self): """ L{DeferredResource} uses the request object's C{render} method to render the resource which is the result of the L{Deferred} being handled. """ rendered = [] request = DummyRequest([]) request.render = rendered.append result = resource.Resource() deferredResource = DeferredResource(defer.succeed(result)) deferredResource.render(request) self.assertEqual(rendered, [result])
def rebuild(self, req): b = self.build_status bc = self.builder_control builder_name = b.getBuilder().getName() log.msg("web rebuild of build %s:%s" % (builder_name, b.getNumber())) name = req.args.get("username", ["<unknown>"])[0] comments = req.args.get("comments", ["<no reason specified>"])[0] reason = ("The web-page 'rebuild' button was pressed by " "'%s': %s\n" % (name, comments)) if not bc or not b.isFinished(): log.msg("could not rebuild: bc=%s, isFinished=%s" % (bc, b.isFinished())) # TODO: indicate an error else: bc.resubmitBuild(b, reason) # we're at # http://localhost:8080/builders/NAME/builds/5/rebuild?[args] # Where should we send them? # # Ideally it would be to the per-build page that they just started, # but we don't know the build number for it yet (besides, it might # have to wait for a current build to finish). The next-most # preferred place is somewhere that the user can see tangible # evidence of their build starting (or to see the reason that it # didn't start). This should be the Builder page. r = Redirect("../..") # the Builder's page d = defer.Deferred() reactor.callLater(1, d.callback, r) return DeferredResource(d)
def test_renderNoFailure(self): """ If the L{Deferred} fails, L{DeferredResource} reports the failure via C{processingFailed}, and does not cause an unhandled error to be logged. """ request = DummyRequest([]) d = request.notifyFinish() failure = Failure(RuntimeError()) deferredResource = DeferredResource(defer.fail(failure)) deferredResource.render(request) self.assertEqual(self.failureResultOf(d), failure) del deferredResource gc.collect() errors = self.flushLoggedErrors(RuntimeError) self.assertEqual(errors, [])
def stop(self, req, auth_ok=False): # check if this is allowed if not auth_ok: if not self.getAuthz(req).actionAllowed('stopBuild', req, self.build_status): return Redirect(path_to_authfail(req)) b = self.build_status log.msg("web stopBuild of build %s:%s" % \ (b.getBuilder().getName(), b.getNumber())) name = req.args.get("username", ["<unknown>"])[0] comments = req.args.get("comments", ["<no reason specified>"])[0] # html-quote both the username and comments, just to be safe reason = ("The web-page 'stop build' button was pressed by " "'%s': %s\n" % (html.escape(name), html.escape(comments))) c = interfaces.IControl(self.getBuildmaster(req)) bldrc = c.getBuilder(self.build_status.getBuilder().getName()) if bldrc: bldc = bldrc.getBuild(self.build_status.getNumber()) if bldc: bldc.stopBuild(reason) # we're at http://localhost:8080/svn-hello/builds/5/stop?[args] and # we want to go to: http://localhost:8080/svn-hello r = Redirect(path_to_builder(req, self.build_status.getBuilder())) d = defer.Deferred() reactor.callLater(1, d.callback, r) return DeferredResource(d)
def stop(self, req, auth_ok=False): # check if this is allowed if not auth_ok: return StopBuildActionResource(self.build_status) b = self.build_status log.msg("web stopBuild of build %s:%s" % (b.getBuilder().getName(), b.getNumber())) name = self.getAuthz(req).getUsernameFull(req) comments = _get_comments_from_request(req) # html-quote both the username and comments, just to be safe reason = ("The web-page 'stop build' button was pressed by " "'%s': %s\n" % (html.escape(name), html.escape(comments))) c = interfaces.IControl(self.getBuildmaster(req)) bldrc = c.getBuilder(self.build_status.getBuilder().getName()) if bldrc: bldc = bldrc.getBuild(self.build_status.getNumber()) if bldc: bldc.stopBuild(reason) # we're at http://localhost:8080/svn-hello/builds/5/stop?[args] and # we want to go to: http://localhost:8080/svn-hello r = Redirect(path_to_builder(req, self.build_status.getBuilder())) d = defer.Deferred() reactor.callLater(1, d.callback, r) return DeferredResource(d)
def test_stopservice_ignores_delete_session(self): """ :func:`stopService` will try deleting the session for 1 second and will stop the loop """ self.test_settled() self.client.treq = StubTreq(DeferredResource(Deferred())) d = self.client.stopService() self.assertNoResult(d) self.clock.advance(1) self.assertIsNone(self.successResultOf(d)) # Moving time would fail treq if it tried to heartbeat self.clock.advance(4)
def getResource(self, request): token = request.getCookie('token') if token is None: credentials = Anonymous() else: credentials = Token(token) d = (self._portal.login( credentials, None, ITalker).addCallback(lambda (iface, talker, logout): WSChatFactory( self._chatroom, talker)).addCallback(WebSocketResource)) return DeferredResource(d)
def test_get_times_out(self): """ If get index times out then get_index_total will return None """ # lets start with settled self.test_settled() # setup client that does not return self.client.treq = StubTreq(DeferredResource(Deferred())) # next heartbeat to get index again self.clock.advance(3) # no response self.clock.advance(5) self.assertIsNone(self.client.get_index_total())
def stop(self, req): b = self.build_status c = self.build_control log.msg("web stopBuild of build %s:%s" % \ (b.getBuilder().getName(), b.getNumber())) name = req.args.get("username", ["<unknown>"])[0] comments = req.args.get("comments", ["<no reason specified>"])[0] reason = ("The web-page 'stop build' button was pressed by " "'%s': %s\n" % (name, comments)) c.stopBuild(reason) # we're at http://localhost:8080/svn-hello/builds/5/stop?[args] and # we want to go to: http://localhost:8080/svn-hello r = Redirect("../..") d = defer.Deferred() reactor.callLater(1, d.callback, r) return DeferredResource(d)
def stop(self, req): if self.isUsingUserPasswd(req): if not self.authUser(req): return Redirect("../../../authfailed") b = self.build_status c = self.build_control log.msg("web stopBuild of build %s:%s" % \ (b.getBuilder().getName(), b.getNumber())) name = req.args.get("username", ["<unknown>"])[0] comments = req.args.get("comments", ["<no reason specified>"])[0] # html-quote both the username and comments, just to be safe reason = ("The web-page 'stop build' button was pressed by " "'%s': %s\n" % (html.escape(name), html.escape(comments))) if c: c.stopBuild(reason) # we're at http://localhost:8080/svn-hello/builds/5/stop?[args] and # we want to go to: http://localhost:8080/svn-hello r = Redirect("../..") d = defer.Deferred() reactor.callLater(1, d.callback, r) return DeferredResource(d)
def g(self, name, req): # Bind the method to the instance so it has a better # fullyQualifiedName later on. This is not necessary on Python 3. bound_getChild = getChild.__get__(self, type(self)) action = start_action( action_type=u"allmydata:web:common-getChild", uri=req.uri, method=req.method, name=name, handler=fullyQualifiedName(bound_getChild), ) with action.context(): result = DeferredContext(maybeDeferred(bound_getChild, name, req)) result.addCallbacks( _getChild_done, _getChild_failed, callbackArgs=(self, ), ) result = result.addActionFinish() return DeferredResource(result)
def rebuild(self, req): # check auth if not self.getAuthz(req).actionAllowed( 'forceBuild', req, self.build_status.getBuilder()): return Redirect(path_to_authfail(req)) # get a control object c = interfaces.IControl(self.getBuildmaster(req)) bc = c.getBuilder(self.build_status.getBuilder().getName()) b = self.build_status builder_name = b.getBuilder().getName() log.msg("web rebuild of build %s:%s" % (builder_name, b.getNumber())) name = req.args.get("username", ["<unknown>"])[0] comments = req.args.get("comments", ["<no reason specified>"])[0] reason = ("The web-page 'rebuild' button was pressed by " "'%s': %s\n" % (name, comments)) extraProperties = getAndCheckProperties(req) if not bc or not b.isFinished() or extraProperties is None: log.msg("could not rebuild: bc=%s, isFinished=%s" % (bc, b.isFinished())) # TODO: indicate an error else: d = bc.rebuildBuild(b, reason, extraProperties) d.addErrback(log.err, "while rebuilding a build") # we're at # http://localhost:8080/builders/NAME/builds/5/rebuild?[args] # Where should we send them? # # Ideally it would be to the per-build page that they just started, # but we don't know the build number for it yet (besides, it might # have to wait for a current build to finish). The next-most # preferred place is somewhere that the user can see tangible # evidence of their build starting (or to see the reason that it # didn't start). This should be the Builder page. r = Redirect(path_to_builder(req, self.build_status.getBuilder())) d = defer.Deferred() reactor.callLater(1, d.callback, r) return DeferredResource(d)
def makeResource(expected): resource = Data(expected, b"text/plain") return DeferredResource(succeed(resource))
def makeResource(expected): resource = Data(expected, b"text/plain") intermediate = Data(b"incorrect intermediate", b"text/plain") intermediate.putChild(b"child", resource) return DeferredResource(succeed(intermediate))
def getChild(self, branch, request): return DeferredResource(self._getChild(branch, request))
def render_GET(self, request): d = flattenString(request, MainElement(self.trackingID)) d.addCallback(Data, 'text/html') return DeferredResource(d).render(request)