Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/buildbot/buildbot/test/test_runner.py
blob: d94ef5f440abe7147b2297b68579fff45612a515 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392

# this file tests the 'buildbot' command, with its various sub-commands

from twisted.trial import unittest
from twisted.python import usage
import os, shutil, shlex
import sets

from buildbot.scripts import runner, tryclient

class Options(unittest.TestCase):
    optionsFile = "SDFsfsFSdfsfsFSD"

    def make(self, d, key):
        # we use a wacky filename here in case the test code discovers the
        # user's real ~/.buildbot/ directory
        os.makedirs(os.sep.join(d + [".buildbot"]))
        f = open(os.sep.join(d + [".buildbot", self.optionsFile]), "w")
        f.write("key = '%s'\n" % key)
        f.close()

    def check(self, d, key):
        basedir = os.sep.join(d)
        options = runner.loadOptions(self.optionsFile, here=basedir,
                                     home=self.home)
        if key is None:
            self.failIf(options.has_key('key'))
        else:
            self.failUnlessEqual(options['key'], key)

    def testFindOptions(self):
        self.make(["home", "dir1", "dir2", "dir3"], "one")
        self.make(["home", "dir1", "dir2"], "two")
        self.make(["home"], "home")
        self.home = os.path.abspath("home")

        self.check(["home", "dir1", "dir2", "dir3"], "one")
        self.check(["home", "dir1", "dir2"], "two")
        self.check(["home", "dir1"], "home")

        self.home = os.path.abspath("nothome")
        os.makedirs(os.sep.join(["nothome", "dir1"]))
        self.check(["nothome", "dir1"], None)

    def doForce(self, args, expected):
        o = runner.ForceOptions()
        o.parseOptions(args)
        self.failUnlessEqual(o.keys(), expected.keys())
        for k in o.keys():
            self.failUnlessEqual(o[k], expected[k],
                                 "[%s] got %s instead of %s" % (k, o[k],
                                                                expected[k]))

    def testForceOptions(self):
        if not hasattr(shlex, "split"):
            raise unittest.SkipTest("need python>=2.3 for shlex.split")

        exp = {"builder": "b1", "reason": "reason",
               "branch": None, "revision": None}
        self.doForce(shlex.split("b1 reason"), exp)
        self.doForce(shlex.split("b1 'reason'"), exp)
        self.failUnlessRaises(usage.UsageError, self.doForce,
                              shlex.split("--builder b1 'reason'"), exp)
        self.doForce(shlex.split("--builder b1 --reason reason"), exp)
        self.doForce(shlex.split("--builder b1 --reason 'reason'"), exp)
        self.doForce(shlex.split("--builder b1 --reason \"reason\""), exp)
        
        exp['reason'] = "longer reason"
        self.doForce(shlex.split("b1 'longer reason'"), exp)
        self.doForce(shlex.split("b1 longer reason"), exp)
        self.doForce(shlex.split("--reason 'longer reason' b1"), exp)
        

class Create(unittest.TestCase):
    def failUnlessIn(self, substring, string, msg=None):
        # trial provides a version of this that requires python-2.3 to test
        # strings.
        self.failUnless(string.find(substring) != -1, msg)
    def failUnlessExists(self, filename):
        self.failUnless(os.path.exists(filename), "%s should exist" % filename)
    def failIfExists(self, filename):
        self.failIf(os.path.exists(filename), "%s should not exist" % filename)

    def setUp(self):
        self.cwd = os.getcwd()

    def tearDown(self):
        os.chdir(self.cwd)

    def testMaster(self):
        basedir = "test_runner.master"
        options = runner.MasterOptions()
        options.parseOptions(["-q", basedir])
        cwd = os.getcwd()
        runner.createMaster(options)
        os.chdir(cwd)

        tac = os.path.join(basedir, "buildbot.tac")
        self.failUnless(os.path.exists(tac))
        tacfile = open(tac,"rt").read()
        self.failUnlessIn("basedir", tacfile)
        self.failUnlessIn("configfile = r'master.cfg'", tacfile)
        self.failUnlessIn("BuildMaster(basedir, configfile)", tacfile)

        cfg = os.path.join(basedir, "master.cfg")
        self.failIfExists(cfg)
        samplecfg = os.path.join(basedir, "master.cfg.sample")
        self.failUnlessExists(samplecfg)
        cfgfile = open(samplecfg,"rt").read()
        self.failUnlessIn("This is a sample buildmaster config file", cfgfile)

        makefile = os.path.join(basedir, "Makefile.sample")
        self.failUnlessExists(makefile)

        # now verify that running it a second time (with the same options)
        # does the right thing: nothing changes
        runner.createMaster(options)
        os.chdir(cwd)

        self.failIfExists(os.path.join(basedir, "buildbot.tac.new"))
        self.failUnlessExists(os.path.join(basedir, "master.cfg.sample"))

        oldtac = open(os.path.join(basedir, "buildbot.tac"), "rt").read()

        # mutate Makefile.sample, since it should be rewritten
        f = open(os.path.join(basedir, "Makefile.sample"), "rt")
        oldmake = f.read()
        f = open(os.path.join(basedir, "Makefile.sample"), "wt")
        f.write(oldmake)
        f.write("# additional line added\n")
        f.close()

        # also mutate master.cfg.sample
        f = open(os.path.join(basedir, "master.cfg.sample"), "rt")
        oldsamplecfg = f.read()
        f = open(os.path.join(basedir, "master.cfg.sample"), "wt")
        f.write(oldsamplecfg)
        f.write("# additional line added\n")
        f.close()

        # now run it again (with different options)
        options = runner.MasterOptions()
        options.parseOptions(["-q", "--config", "other.cfg", basedir])
        runner.createMaster(options)
        os.chdir(cwd)

        tac = open(os.path.join(basedir, "buildbot.tac"), "rt").read()
        self.failUnlessEqual(tac, oldtac, "shouldn't change existing .tac")
        self.failUnlessExists(os.path.join(basedir, "buildbot.tac.new"))

        make = open(os.path.join(basedir, "Makefile.sample"), "rt").read()
        self.failUnlessEqual(make, oldmake, "*should* rewrite Makefile.sample")

        samplecfg = open(os.path.join(basedir, "master.cfg.sample"),
                         "rt").read()
        self.failUnlessEqual(samplecfg, oldsamplecfg,
                             "*should* rewrite master.cfg.sample")

    def testUpgradeMaster(self):
        # first, create a master, run it briefly, then upgrade it. Nothing
        # should change.
        basedir = "test_runner.master2"
        options = runner.MasterOptions()
        options.parseOptions(["-q", basedir])
        cwd = os.getcwd()
        runner.createMaster(options)
        os.chdir(cwd)

        f = open(os.path.join(basedir, "master.cfg"), "w")
        f.write(open(os.path.join(basedir, "master.cfg.sample"), "r").read())
        f.close()

        # the upgrade process (specifically the verify-master.cfg step) will
        # create any builder status directories that weren't already created.
        # Create those ahead of time.
        os.mkdir(os.path.join(basedir, "full"))

        files1 = self.record_files(basedir)

        # upgrade it
        options = runner.UpgradeMasterOptions()
        options.parseOptions(["--quiet", basedir])
        cwd = os.getcwd()
        runner.upgradeMaster(options)
        os.chdir(cwd)

        files2 = self.record_files(basedir)
        self.failUnlessSameFiles(files1, files2)

        # now make it look like the one that 0.7.5 creates: no public_html
        for fn in os.listdir(os.path.join(basedir, "public_html")):
            os.unlink(os.path.join(basedir, "public_html", fn))
        os.rmdir(os.path.join(basedir, "public_html"))

        # and make sure that upgrading it re-populates public_html
        options = runner.UpgradeMasterOptions()
        options.parseOptions(["-q", basedir])
        cwd = os.getcwd()
        runner.upgradeMaster(options)
        os.chdir(cwd)

        files3 = self.record_files(basedir)
        self.failUnlessSameFiles(files1, files3)

        # now induce an error in master.cfg and make sure that upgrade
        # notices it.
        f = open(os.path.join(basedir, "master.cfg"), "a")
        f.write("raise RuntimeError('catch me please')\n")
        f.close()

        options = runner.UpgradeMasterOptions()
        options.parseOptions(["-q", basedir])
        cwd = os.getcwd()
        rc = runner.upgradeMaster(options)
        os.chdir(cwd)
        self.failUnless(rc != 0, rc)
        # TODO: change the way runner.py works to let us pass in a stderr
        # filehandle, and use a StringIO to capture its output, and make sure
        # the right error messages appear therein.


    def failUnlessSameFiles(self, files1, files2):
        f1 = sets.Set(files1.keys())
        f2 = sets.Set(files2.keys())
        msg = ""
        if f2 - f1:
            msg += "Missing from files1: %s\n" % (list(f2-f1),)
        if f1 - f2:
            msg += "Missing from files2: %s\n" % (list(f1-f2),)
        if msg:
            self.fail(msg)

    def record_files(self, basedir):
        allfiles = {}
        for root, dirs, files in os.walk(basedir):
            for f in files:
                fn = os.path.join(root, f)
                allfiles[fn] = ("FILE", open(fn,"rb").read())
            for d in dirs:
                allfiles[os.path.join(root, d)] = ("DIR",)
        return allfiles


    def testSlave(self):
        basedir = "test_runner.slave"
        options = runner.SlaveOptions()
        options.parseOptions(["-q", basedir, "buildmaster:1234",
                              "botname", "passwd"])
        cwd = os.getcwd()
        runner.createSlave(options)
        os.chdir(cwd)

        tac = os.path.join(basedir, "buildbot.tac")
        self.failUnless(os.path.exists(tac))
        tacfile = open(tac,"rt").read()
        self.failUnlessIn("basedir", tacfile)
        self.failUnlessIn("buildmaster_host = 'buildmaster'", tacfile)
        self.failUnlessIn("port = 1234", tacfile)
        self.failUnlessIn("slavename = 'botname'", tacfile)
        self.failUnlessIn("passwd = 'passwd'", tacfile)
        self.failUnlessIn("keepalive = 600", tacfile)
        self.failUnlessIn("BuildSlave(buildmaster_host, port, slavename",
                          tacfile)

        makefile = os.path.join(basedir, "Makefile.sample")
        self.failUnlessExists(makefile)

        self.failUnlessExists(os.path.join(basedir, "info", "admin"))
        self.failUnlessExists(os.path.join(basedir, "info", "host"))
        # edit one to make sure the later install doesn't change it
        f = open(os.path.join(basedir, "info", "admin"), "wt")
        f.write("updated@buildbot.example.org\n")
        f.close()

        # now verify that running it a second time (with the same options)
        # does the right thing: nothing changes
        runner.createSlave(options)
        os.chdir(cwd)

        self.failIfExists(os.path.join(basedir, "buildbot.tac.new"))
        admin = open(os.path.join(basedir, "info", "admin"), "rt").read()
        self.failUnlessEqual(admin, "updated@buildbot.example.org\n")


        # mutate Makefile.sample, since it should be rewritten
        oldmake = open(os.path.join(basedir, "Makefile.sample"), "rt").read()
        f = open(os.path.join(basedir, "Makefile.sample"), "wt")
        f.write(oldmake)
        f.write("# additional line added\n")
        f.close()
        oldtac = open(os.path.join(basedir, "buildbot.tac"), "rt").read()

        # now run it again (with different options)
        options = runner.SlaveOptions()
        options.parseOptions(["-q", "--keepalive", "30",
                              basedir, "buildmaster:9999",
                              "newbotname", "passwd"])
        runner.createSlave(options)
        os.chdir(cwd)

        tac = open(os.path.join(basedir, "buildbot.tac"), "rt").read()
        self.failUnlessEqual(tac, oldtac, "shouldn't change existing .tac")
        self.failUnlessExists(os.path.join(basedir, "buildbot.tac.new"))
        tacfile = open(os.path.join(basedir, "buildbot.tac.new"),"rt").read()
        self.failUnlessIn("basedir", tacfile)
        self.failUnlessIn("buildmaster_host = 'buildmaster'", tacfile)
        self.failUnlessIn("port = 9999", tacfile)
        self.failUnlessIn("slavename = 'newbotname'", tacfile)
        self.failUnlessIn("passwd = 'passwd'", tacfile)
        self.failUnlessIn("keepalive = 30", tacfile)
        self.failUnlessIn("BuildSlave(buildmaster_host, port, slavename",
                          tacfile)

        make = open(os.path.join(basedir, "Makefile.sample"), "rt").read()
        self.failUnlessEqual(make, oldmake, "*should* rewrite Makefile.sample")

class Try(unittest.TestCase):
    # test some aspects of the 'buildbot try' command
    def makeOptions(self, contents):
        if os.path.exists(".buildbot"):
            shutil.rmtree(".buildbot")
        os.mkdir(".buildbot")
        open(os.path.join(".buildbot", "options"), "w").write(contents)

    def testGetopt1(self):
        opts = "try_connect = 'ssh'\n" + "try_builders = ['a']\n"
        self.makeOptions(opts)
        config = runner.TryOptions()
        config.parseOptions([])
        t = tryclient.Try(config)
        self.failUnlessEqual(t.connect, "ssh")
        self.failUnlessEqual(t.builderNames, ['a'])

    def testGetopt2(self):
        opts = ""
        self.makeOptions(opts)
        config = runner.TryOptions()
        config.parseOptions(['--connect=ssh', '--builder', 'a'])
        t = tryclient.Try(config)
        self.failUnlessEqual(t.connect, "ssh")
        self.failUnlessEqual(t.builderNames, ['a'])

    def testGetopt3(self):
        opts = ""
        self.makeOptions(opts)
        config = runner.TryOptions()
        config.parseOptions(['--connect=ssh',
                             '--builder', 'a', '--builder=b'])
        t = tryclient.Try(config)
        self.failUnlessEqual(t.connect, "ssh")
        self.failUnlessEqual(t.builderNames, ['a', 'b'])

    def testGetopt4(self):
        opts = "try_connect = 'ssh'\n" + "try_builders = ['a']\n"
        self.makeOptions(opts)
        config = runner.TryOptions()
        config.parseOptions(['--builder=b'])
        t = tryclient.Try(config)
        self.failUnlessEqual(t.connect, "ssh")
        self.failUnlessEqual(t.builderNames, ['b'])

    def testGetTopdir(self):
        os.mkdir("gettopdir")
        os.mkdir(os.path.join("gettopdir", "foo"))
        os.mkdir(os.path.join("gettopdir", "foo", "bar"))
        open(os.path.join("gettopdir", "1"),"w").write("1")
        open(os.path.join("gettopdir", "foo", "2"),"w").write("2")
        open(os.path.join("gettopdir", "foo", "bar", "3"),"w").write("3")

        target = os.path.abspath("gettopdir")
        t = tryclient.getTopdir("1", "gettopdir")
        self.failUnlessEqual(os.path.abspath(t), target)
        t = tryclient.getTopdir("1", os.path.join("gettopdir", "foo"))
        self.failUnlessEqual(os.path.abspath(t), target)
        t = tryclient.getTopdir("1", os.path.join("gettopdir", "foo", "bar"))
        self.failUnlessEqual(os.path.abspath(t), target)

        target = os.path.abspath(os.path.join("gettopdir", "foo"))
        t = tryclient.getTopdir("2", os.path.join("gettopdir", "foo"))
        self.failUnlessEqual(os.path.abspath(t), target)
        t = tryclient.getTopdir("2", os.path.join("gettopdir", "foo", "bar"))
        self.failUnlessEqual(os.path.abspath(t), target)

        target = os.path.abspath(os.path.join("gettopdir", "foo", "bar"))
        t = tryclient.getTopdir("3", os.path.join("gettopdir", "foo", "bar"))
        self.failUnlessEqual(os.path.abspath(t), target)

        nonexistent = "nonexistent\n29fis3kq\tBAR"
        # hopefully there won't be a real file with that name between here
        # and the filesystem root.
        self.failUnlessRaises(ValueError, tryclient.getTopdir, nonexistent)