Skip to content

Commit

Permalink
* slave/buildslave/commands/mtn.py (_dovccmd): Take a function argument
Browse files Browse the repository at this point in the history
  instead of using the callback, which frees up the callback argument for
  better use.  Also, make pulling a conditional.
  (parseGotRevision): Split it two, one part that sets up things for _dovccmd,
  and the other that performs the actual command to get the base revision.
  This makes testing much easier.
  * slave/buildslave/commands/mtn.py: Add a testof parseGotRevision.
  • Loading branch information
levitte committed Apr 4, 2011
1 parent 79f5c11 commit 759b309
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 25 deletions.
44 changes: 26 additions & 18 deletions slave/buildslave/commands/mtn.py
Expand Up @@ -65,32 +65,39 @@ def cont(res):
return d

def doVCUpdate(self):
return self._dovccmd(self._update)
return self._dovccmd(self._update, True)

def doVCFull(self):
return self._dovccmd(self._checkout)
return self._dovccmd(self._checkout, True)

def _fullSrcdir(self):
return os.path.join(self.builder.basedir, self.srcdir)

def sourcedirIsUpdateable(self):
return os.path.isdir(os.path.join(self._fullSrcdir(), "_MTN"))

def _dovccmd(self, cb=None, **kwargs):
command = [self.mtn, 'pull', self.sourcedata,
'--db', self.database]
if self.progress:
command.extend(['--ticker=dot'])
def _dovccmd(self, fn, dopull, cb=None, **kwargs):
if dopull:
command = [self.mtn, 'pull', self.sourcedata,
'--db', self.database]
if self.progress:
command.extend(['--ticker=dot'])
else:
command.extend(['--ticker=none'])
c = runprocess.RunProcess(self.builder, command,
self.builder.basedir,
environ=self.env, sendRC=False,
timeout=self.timeout,
maxTime=self.maxTime,
keepStdout=True, usePTY=False)
self.sendStatus({"header": "pulling %s from %s\n"
% (self.branch, self.sourcedata)})
self.command = c
d = c.start()
d.addCallback(self._abandonOnFailure)
d.addCallback(fn)
else:
command.extend(['--ticker=none'])
c = runprocess.RunProcess(self.builder, command, self.builder.basedir,
environ=self.env, sendRC=False,
timeout=self.timeout, maxTime=self.maxTime,
keepStdout=True, usePTY=False)
self.sendStatus({"header": "pulling %s from %s\n"
% (self.branch, self.sourcedata)})
self.command = c
d = c.start()
d = fn(None)
if cb:
d.addCallback(cb)
return d
Expand Down Expand Up @@ -172,14 +179,15 @@ def _parse(res):
if len(hash) != 40:
return None
return hash
return self._dovccmd(self._get_base_revision, False, _parse)

def _get_base_revision(self, res):
c = runprocess.RunProcess(self.builder,
[self.mtn, 'automate', 'select', 'w:'],
self._fullSrcdir(),
sendRC=False,
timeout=self.timeout, maxTime=self.maxTime,
keepStdout=True, usePTY=False)
self.command = c
d = c.start()
d.addCallback(self._abandonOnFailure)
d.addCallback(_parse)
return d
49 changes: 42 additions & 7 deletions slave/buildslave/test/unit/test_commands_mtn.py
Expand Up @@ -14,14 +14,18 @@
# Copyright Buildbot Team Members

import os
import mock

from twisted.trial import unittest
from twisted.internet import defer

from buildslave.test.fake.runprocess import Expect
from buildslave.test.util.sourcecommand import SourceCommandTestMixin
from buildslave.commands import mtn

class TestMonotone(SourceCommandTestMixin, unittest.TestCase):
repourl='mtn://code.monotone.ca/sandbox'
branch='ca.monotone.sandbox.buildbot'

def setUp(self):
self.setUpCommand()
Expand All @@ -30,17 +34,14 @@ def tearDown(self):
self.tearDownCommand()

def test_simple(self):
repourl='mtn://code.monotone.ca/sandbox'
branch='ca.monotone.sandbox.buildbot'

self.patch_getCommand('mtn', 'path/to/mtn')
self.clean_environ()
self.make_command(mtn.Monotone, dict(
workdir='workdir',
mode='copy',
revision=None,
repourl=repourl,
branch=branch
repourl=self.repourl,
branch=self.branch
))

exp_environ = dict(PWD='.', LC_MESSAGES='C')
Expand All @@ -54,7 +55,7 @@ def test_simple(self):
self.basedir) + 0,
Expect([ 'clobber', 'source' ],
self.basedir) + 0,
Expect(['path/to/mtn', 'pull', repourl+"?"+branch,
Expect(['path/to/mtn', 'pull', self.repourl+"?"+self.branch,
'--db', os.path.join(self.basedir, 'db.mtn'),
'--ticker=none'],
self.basedir,
Expand All @@ -78,6 +79,40 @@ def test_simple(self):
self.patch_runprocess(*expects)

d = self.run_command()
d.addCallback(self.check_sourcedata, repourl+"?"+branch)
d.addCallback(self.check_sourcedata, self.repourl+"?"+self.branch)
return d

# Testing parseGotRevision
def do_test_parseGotRevision(self, stdout, exp):
self.make_command(mtn.Monotone, dict(
workdir='workdir',
repourl=self.repourl,
branch=self.branch
))
def _dovccmd(fn, dopull, callback=None, keepStdout=False):
#self.assertTrue(keepStdout)
self.cmd.command = mock.Mock()
self.cmd.command.stdout = stdout
d = defer.succeed(None)
d.addCallback(callback)
return d
self.cmd._dovccmd = _dovccmd
self.cmd.srcdir = self.cmd.workdir

d = self.cmd.parseGotRevision()
def check(res):
self.assertEqual(res, exp)
d.addCallback(check)
return d

def test_parseGotRevision_bogus(self):
return self.do_test_parseGotRevision("mtn: misuse: no match for selection '1234'\n", None)

def test_parseGotRevision_wrong_length(self):
return self.do_test_parseGotRevision("\n1234abcd\n", None)

def test_parseGotRevision_ok(self):
return self.do_test_parseGotRevision(
"\n4026d33b0532b11f36b0875f63699adfa8ee8662\n",
"4026d33b0532b11f36b0875f63699adfa8ee8662")

0 comments on commit 759b309

Please sign in to comment.