web: big cleanup of URL generation, to use relative links everywhere
[buildbot.git] / buildbot / test / test_webparts.py
blob6df8b18d4f69aabac1328102c16e7db2f99ceef3
2 import os
3 from twisted.trial import unittest
4 from twisted.internet import defer
5 from twisted.web import client
6 from twisted.web.error import Error as WebError
7 from buildbot.slave.commands import rmdirRecursive
8 from buildbot.status import html
9 from test_web import BaseWeb, base_config, ConfiguredMaster
10 from buildbot.scripts import runner
12 class Webparts(BaseWeb, unittest.TestCase):
14 def find_webstatus(self, master):
15 return filter(lambda child: isinstance(child, html.WebStatus),
16 list(master))
18 def startMaster(self, extraconfig):
19 config = base_config + extraconfig
20 rmdirRecursive("test_webparts")
21 os.mkdir("test_webparts")
22 runner.upgradeMaster({'basedir': "test_webparts",
23 'quiet': True,
25 self.master = m = ConfiguredMaster("test_webparts", config)
26 m.startService()
27 # hack to find out what randomly-assigned port it is listening on
28 port = list(self.find_webstatus(m)[0])[0]._port.getHost().port
29 self.baseurl = "http://localhost:%d/" % port
31 def getAndCheck(self, url, substring, show=False):
32 d = client.getPage(url)
33 def _show_weberror(why):
34 why.trap(WebError)
35 self.fail("error for %s: %s" % (url, why))
36 d.addErrback(_show_weberror)
37 d.addCallback(self._getAndCheck, substring, show)
38 return d
39 def _getAndCheck(self, page, substring, show):
40 if show:
41 print page
42 self.failUnlessIn(substring, page,
43 "Couldn't find substring '%s' in page:\n%s" %
44 (substring, page))
46 def testInit(self):
47 extraconfig = """
48 from twisted.web import static
49 ws = html.WebStatus(http_port=0)
50 c['status'] = [ws]
51 ws.putChild('child.html', static.Data('I am the child', 'text/plain'))
52 """
53 self.startMaster(extraconfig)
54 d = self.getAndCheck(self.baseurl + "child.html",
55 "I am the child")
56 return d
57 testInit.timeout = 10
59 def testStatic(self):
60 extraconfig = """
61 from twisted.web import static
62 ws = html.WebStatus(http_port=0)
63 c['status'] = [ws]
64 ws.putChild('child.html', static.Data('I am the child', 'text/plain'))
65 """
66 self.startMaster(extraconfig)
67 os.mkdir(os.path.join("test_webparts", "public_html", "subdir"))
68 f = open(os.path.join("test_webparts", "public_html", "foo.html"), "wt")
69 f.write("see me foo\n")
70 f.close()
71 f = open(os.path.join("test_webparts", "public_html", "subdir",
72 "bar.html"), "wt")
73 f.write("see me subdir/bar\n")
74 f.close()
75 d = self.getAndCheck(self.baseurl + "child.html", "I am the child")
76 d.addCallback(lambda res:
77 self.getAndCheck(self.baseurl+"foo.html",
78 "see me foo"))
79 d.addCallback(lambda res:
80 self.getAndCheck(self.baseurl+"subdir/bar.html",
81 "see me subdir/bar"))
82 return d
84 def _check(self, res, suburl, substring, show=False):
85 d = self.getAndCheck(self.baseurl + suburl, substring, show)
86 return d
88 def testPages(self):
89 extraconfig = """
90 ws = html.WebStatus(http_port=0)
91 c['status'] = [ws]
92 """
93 self.startMaster(extraconfig)
94 d = defer.succeed(None)
95 d.addCallback(self._check, "", "Welcome to the Buildbot")
96 d.addCallback(self._check, "waterfall", "current activity")
97 d.addCallback(self._check, "about", "Buildbot is a free software")
98 d.addCallback(self._check, "changes", "PBChangeSource listener")
99 d.addCallback(self._check, "buildslaves", "Build Slaves")
100 d.addCallback(self._check, "one_line_per_build",
101 "Last 20 finished builds")
102 d.addCallback(self._check, "one_box_per_builder", "Latest builds")
103 d.addCallback(self._check, "builders", "Builders")
104 d.addCallback(self._check, "builders/builder1", "Builder: builder1")
105 d.addCallback(self._check, "builders/builder1/builds", "") # dummy
106 # TODO: the pages beyond here would be great to test, but that would
107 # require causing a build to complete.
108 #d.addCallback(self._check, "builders/builder1/builds/1", "")
109 #d.addCallback(self._check, "builders/builder1/builds/1/steps", "")
110 #d.addCallback(self._check,
111 # "builders/builder1/builds/1/steps/compile", "")
112 #d.addCallback(self._check,
113 # "builders/builder1/builds/1/steps/compile/logs", "")
114 #d.addCallback(self._check,
115 # "builders/builder1/builds/1/steps/compile/logs/stdio","")
116 #d.addCallback(self._check,
117 # "builders/builder1/builds/1/steps/compile/logs/stdio/text", "")
118 return d