1 # -*- test-case-name: buildbot.test.test_properties -*-
5 from twisted
.trial
import unittest
7 from buildbot
.twcompat
import maybeWait
8 from buildbot
.sourcestamp
import SourceStamp
9 from buildbot
.process
import base
10 from buildbot
.steps
.shell
import ShellCommand
, WithProperties
11 from buildbot
.status
import builder
12 from buildbot
.slave
.commands
import rmdirRecursive
13 from buildbot
.test
.runutils
import RunMixin
15 class MyBuildStep(ShellCommand
):
16 def _interpolateProperties(self
, command
):
17 command
= ["tar", "czf",
18 "build-%s.tar.gz" % self
.getProperty("revision"),
20 return ShellCommand
._interpolateProperties
(self
, command
)
30 class FakeSlaveBuilder
:
32 def getSlaveCommandVersion(self
, command
, oldversion
=None):
35 class Interpolate(unittest
.TestCase
):
37 self
.builder
= FakeBuilder()
38 self
.builder_status
= builder
.BuilderStatus("fakebuilder")
39 self
.builder_status
.basedir
= "test_properties"
40 self
.builder_status
.nextBuildNumber
= 5
41 rmdirRecursive(self
.builder_status
.basedir
)
42 os
.mkdir(self
.builder_status
.basedir
)
43 self
.build_status
= self
.builder_status
.newBuild()
44 req
= base
.BuildRequest("reason", SourceStamp(branch
="branch2",
46 self
.build
= base
.Build([req
])
47 self
.build
.setBuilder(self
.builder
)
48 self
.build
.setupStatus(self
.build_status
)
49 self
.build
.setupSlaveBuilder(FakeSlaveBuilder())
51 def testWithProperties(self
):
52 self
.build
.setProperty("revision", 47)
53 self
.failUnlessEqual(self
.build_status
.getProperty("revision"), 47)
54 c
= ShellCommand(workdir
=dir, build
=self
.build
,
55 command
=["tar", "czf",
56 WithProperties("build-%s.tar.gz",
59 cmd
= c
._interpolateProperties
(c
.command
)
60 self
.failUnlessEqual(cmd
,
61 ["tar", "czf", "build-47.tar.gz", "source"])
63 def testWithPropertiesDict(self
):
64 self
.build
.setProperty("other", "foo")
65 self
.build
.setProperty("missing", None)
66 c
= ShellCommand(workdir
=dir, build
=self
.build
,
67 command
=["tar", "czf",
68 WithProperties("build-%(other)s.tar.gz"),
70 cmd
= c
._interpolateProperties
(c
.command
)
71 self
.failUnlessEqual(cmd
,
72 ["tar", "czf", "build-foo.tar.gz", "source"])
74 def testWithPropertiesEmpty(self
):
75 self
.build
.setProperty("empty", None)
76 c
= ShellCommand(workdir
=dir, build
=self
.build
,
77 command
=["tar", "czf",
78 WithProperties("build-%(empty)s.tar.gz"),
80 cmd
= c
._interpolateProperties
(c
.command
)
81 self
.failUnlessEqual(cmd
,
82 ["tar", "czf", "build-.tar.gz", "source"])
84 def testCustomBuildStep(self
):
85 c
= MyBuildStep(workdir
=dir, build
=self
.build
)
86 cmd
= c
._interpolateProperties
(c
.command
)
87 self
.failUnlessEqual(cmd
,
88 ["tar", "czf", "build-1234.tar.gz", "source"])
90 def testSourceStamp(self
):
91 c
= ShellCommand(workdir
=dir, build
=self
.build
,
93 WithProperties("%s-dir", "branch"),
94 WithProperties("%s-rev", "revision"),
96 cmd
= c
._interpolateProperties
(c
.command
)
97 self
.failUnlessEqual(cmd
,
98 ["touch", "branch2-dir", "1234-rev"])
100 def testSlaveName(self
):
101 c
= ShellCommand(workdir
=dir, build
=self
.build
,
103 WithProperties("%s-slave", "slavename"),
105 cmd
= c
._interpolateProperties
(c
.command
)
106 self
.failUnlessEqual(cmd
,
107 ["touch", "bot12-slave"])
109 def testBuildNumber(self
):
110 c
= ShellCommand(workdir
=dir, build
=self
.build
,
112 WithProperties("build-%d", "buildnumber"),
113 WithProperties("builder-%s", "buildername"),
115 cmd
= c
._interpolateProperties
(c
.command
)
116 self
.failUnlessEqual(cmd
,
117 ["touch", "build-5", "builder-fakebuilder"])
121 from buildbot.process import factory
122 from buildbot.steps.shell import ShellCommand, WithProperties
125 BuildmasterConfig = c = {}
126 c['bots'] = [('bot1', 'sekrit')]
129 c['slavePortnum'] = 0
131 # Note: when run against twisted-1.3.0, this locks up about 5% of the time. I
132 # suspect that a command with no output that finishes quickly triggers a race
133 # condition in 1.3.0's process-reaping code. The 'touch' process becomes a
134 # zombie and the step never completes. To keep this from messing up the unit
135 # tests too badly, this step runs with a reduced timeout.
137 f1 = factory.BuildFactory([s(ShellCommand,
140 WithProperties('%s-slave', 'slavename'),
146 b1 = {'name': 'full1', 'slavename': 'bot1', 'builddir': 'bd1', 'factory': f1}
151 class Run(RunMixin
, unittest
.TestCase
):
152 def testInterpolate(self
):
153 # run an actual build with a step that interpolates a build property
154 d
= self
.master
.loadConfig(run_config
)
155 d
.addCallback(lambda res
: self
.master
.startService())
156 d
.addCallback(lambda res
: self
.connectOneSlave("bot1"))
157 d
.addCallback(lambda res
: self
.requestBuild("full1"))
158 d
.addCallback(self
.failUnlessBuildSucceeded
)
159 def _check_touch(res
):
160 f
= os
.path
.join("slavebase-bot1", "bd1", "bot1-slave")
161 self
.failUnless(os
.path
.exists(f
))
163 d
.addCallback(_check_touch
)
167 # we test got_revision in test_vc