コード例 #1
0
    def setUp(self):
        self.basedir = os.path.abspath("basedir")
        if os.path.exists(self.basedir):
            shutil.rmtree(self.basedir)
        os.makedirs(self.basedir)

        self.real_bot = base.BotBase(self.basedir, False)
        self.real_bot.startService()

        self.bot = FakeRemote(self.real_bot)
コード例 #2
0
    def setUp(self):
        self.basedir = os.path.abspath("basedir")
        if os.path.exists(self.basedir):
            shutil.rmtree(self.basedir)
        os.makedirs(self.basedir)

        self.bot = base.BotBase(self.basedir, False)
        self.bot.startService()

        # get a WorkerForBuilder object from the bot and wrap it as a fake remote
        builders = yield self.bot.remote_setBuilderList([('sb', 'sb')])
        self.sb = FakeRemote(builders['sb'])

        self.setUpCommand()
コード例 #3
0
    def test_simple(self):
        self.fakemaster.count_reads = True  # get actual byte counts
        self.fakemaster.data = test_data = '1234' * 13
        assert (len(self.fakemaster.data) == 52)

        self.make_command(
            transfer.WorkerFileDownloadCommand,
            dict(
                workdir='.',
                slavedest='data',
                reader=FakeRemote(self.fakemaster),
                maxsize=None,
                blocksize=32,
                mode=0o777,
            ))

        d = self.run_command()

        def check(_):
            self.assertUpdates(
                ['read 32', 'read 32', 'read 32', 'close', {
                    'rc': 0
                }])
            datafile = os.path.join(self.basedir, 'data')
            self.assertTrue(os.path.exists(datafile))
            self.assertEqual(open(datafile).read(), test_data)
            if runtime.platformType != 'win32':
                self.assertEqual(os.stat(datafile).st_mode & 0o777, 0o777)

        d.addCallback(check)
        return d
コード例 #4
0
    def test_mkdir(self):
        self.fakemaster.data = test_data = b'hi'

        self.make_command(
            transfer.WorkerFileDownloadCommand,
            dict(
                workdir='workdir',
                workerdest=os.path.join('subdir', 'data'),
                reader=FakeRemote(self.fakemaster),
                maxsize=None,
                blocksize=32,
                mode=0o777,
            ))

        d = self.run_command()

        def check(_):
            self.assertUpdates(['read(s)', 'close', {'rc': 0}])
            datafile = os.path.join(self.basedir, 'workdir', 'subdir', 'data')
            self.assertTrue(os.path.exists(datafile))
            with open(datafile, mode="rb") as f:
                datafileContent = f.read()
            self.assertEqual(datafileContent, test_data)

        d.addCallback(check)
        return d
コード例 #5
0
    def test_truncated(self):
        self.fakemaster.data = test_data = b'tenchars--' * 10

        self.make_command(
            transfer.WorkerFileDownloadCommand,
            dict(
                workdir='.',
                workerdest='data',
                reader=FakeRemote(self.fakemaster),
                maxsize=50,
                blocksize=32,
                mode=0o777,
            ))

        yield self.run_command()

        self.assertUpdates([
            'read(s)', 'close', {
                'rc':
                1,
                'stderr':
                "Maximum filesize reached, truncating file '{0}'".format(
                    os.path.join(self.basedir, '.', 'data'))
            }
        ])
        datafile = os.path.join(self.basedir, 'data')
        self.assertTrue(os.path.exists(datafile))
        with open(datafile, mode="rb") as f:
            data = f.read()
        self.assertEqual(data, test_data[:50])
コード例 #6
0
    def test_failure(self):
        self.fakemaster.data = 'hi'

        os.makedirs(os.path.join(self.basedir, 'dir'))
        self.make_command(
            transfer.WorkerFileDownloadCommand,
            dict(
                workdir='.',
                workerdest='dir',  # but that's a directory!
                reader=FakeRemote(self.fakemaster),
                maxsize=None,
                blocksize=32,
                mode=0o777,
            ))

        yield self.run_command()

        self.assertUpdates([
            'close', {
                'rc':
                1,
                'stderr':
                "Cannot open file '{0}' for download".format(
                    os.path.join(self.basedir, '.', 'dir'))
            }
        ])
コード例 #7
0
    def test_timestamp(self):
        self.fakemaster.count_writes = True  # get actual byte counts
        timestamp = (os.path.getatime(self.datafile),
                     os.path.getmtime(self.datafile))

        self.make_command(
            transfer.WorkerFileUploadCommand,
            dict(
                workdir='workdir',
                slavesrc='data',
                writer=FakeRemote(self.fakemaster),
                maxsize=1000,
                blocksize=64,
                keepstamp=True,
            ))

        d = self.run_command()

        def check(_):
            self.assertUpdates([{
                'header': 'sending %s' % self.datafile
            }, 'write 64', 'write 64', 'write 52', 'close',
                                'utime - %s' % timestamp[0], {
                                    'rc': 0
                                }])

        d.addCallback(check)
        return d
コード例 #8
0
    def test_missing(self):
        self.make_command(
            transfer.WorkerFileUploadCommand,
            dict(
                workdir='workdir',
                slavesrc='data-nosuch',
                writer=FakeRemote(self.fakemaster),
                maxsize=100,
                blocksize=64,
                keepstamp=False,
            ))

        d = self.run_command()

        def check(_):
            df = self.datafile + "-nosuch"
            self.assertUpdates([{
                'header': 'sending %s' % df
            }, 'close', {
                'rc':
                1,
                'stderr':
                "Cannot open file '%s' for upload" % df
            }])

        d.addCallback(check)
        return d
コード例 #9
0
    def test_interrupted(self):
        self.fakemaster.delay_write = True  # write very slowly

        self.make_command(
            transfer.WorkerFileUploadCommand,
            dict(
                workdir='workdir',
                workersrc='data',
                writer=FakeRemote(self.fakemaster),
                maxsize=100,
                blocksize=2,
                keepstamp=False,
            ))

        d = self.run_command()

        # wait a jiffy..
        interrupt_d = defer.Deferred()
        reactor.callLater(0.01, interrupt_d.callback, None)

        # and then interrupt the step
        def do_interrupt(_):
            return self.cmd.interrupt()

        interrupt_d.addCallback(do_interrupt)

        yield defer.DeferredList([d, interrupt_d])

        self.assertUpdates([{
            'header': 'sending {0}\n'.format(self.datafile)
        }, 'write(s)', 'close', {
            'rc': 1
        }])
コード例 #10
0
    def test_interrupted(self):
        self.fakemaster.data = 'tenchars--' * 100  # 1k
        self.fakemaster.delay_read = True  # read veery slowly

        self.make_command(
            transfer.WorkerFileDownloadCommand,
            dict(
                workdir='.',
                slavedest='data',
                reader=FakeRemote(self.fakemaster),
                maxsize=100,
                blocksize=2,
                mode=0o777,
            ))

        d = self.run_command()

        # wait a jiffy..
        interrupt_d = defer.Deferred()
        reactor.callLater(0.01, interrupt_d.callback, None)

        # and then interrupt the step
        def do_interrupt(_):
            return self.cmd.interrupt()

        interrupt_d.addCallback(do_interrupt)

        dl = defer.DeferredList([d, interrupt_d])

        def check(_):
            self.assertUpdates(['read(s)', 'close', {'rc': 1}])

        dl.addCallback(check)
        return dl
コード例 #11
0
    def test_truncated(self):
        self.fakemaster.count_writes = True  # get actual byte counts

        self.make_command(
            transfer.WorkerFileUploadCommand,
            dict(
                workdir='workdir',
                workersrc='data',
                writer=FakeRemote(self.fakemaster),
                maxsize=100,
                blocksize=64,
                keepstamp=False,
            ))

        yield self.run_command()

        self.assertUpdates([{
            'header': 'sending {0}\n'.format(self.datafile)
        }, 'write 64', 'write 36', 'close', {
            'rc':
            1,
            'stderr':
            "Maximum filesize reached, truncating file '{0}'".format(
                self.datafile)
        }])
コード例 #12
0
    def test_simple(self):
        self.fakemaster.count_writes = True  # get actual byte counts

        self.make_command(
            transfer.WorkerFileUploadCommand,
            dict(
                workdir='workdir',
                workersrc='data',
                writer=FakeRemote(self.fakemaster),
                maxsize=1000,
                blocksize=64,
                keepstamp=False,
            ))

        d = self.run_command()

        def check(_):
            self.assertUpdates([{
                'header': 'sending {0}'.format(self.datafile)
            }, 'write 64', 'write 64', 'write 52', 'close', {
                'rc': 0
            }])

        d.addCallback(check)
        return d
コード例 #13
0
ファイル: test_bot.py プロジェクト: passysosysmas/buildbot
    def test_startCommand_interruptCommand(self):
        # set up a fake step to receive updates
        st = FakeStep()

        # patch runprocess to pretend to sleep (it will really just hang forever,
        # except that we interrupt it)
        self.patch_runprocess(
            Expect(['sleep', '10'], os.path.join(
                self.basedir, 'wfb', 'workdir')) +
            {'hdr': 'headers'} +
            {'wait': True}
        )

        yield self.wfb.callRemote("startCommand", FakeRemote(st),
                                  "13", "shell", dict(command=['sleep', '10'],
                                                      workdir='workdir'))

        # wait a jiffy..
        d = defer.Deferred()
        reactor.callLater(0.01, d.callback, None)
        yield d

        # and then interrupt the step
        yield self.wfb.callRemote("interruptCommand", "13", "tl/dr")

        yield st.wait_for_finish()

        self.assertEqual(st.actions, [
            ['update', [[{'hdr': 'headers'}, 0]]],
            ['update', [[{'hdr': 'killing'}, 0]]],
            ['update', [[{'rc': -1}, 0]]],
            ['complete', None],
        ])
コード例 #14
0
    def test_startCommand(self):
        # set up a fake step to receive updates
        st = FakeStep()

        # patch runprocess to handle the 'echo', below
        self.patch_runprocess(
            Expect(['echo', 'hello'],
                   os.path.join(self.basedir, 'wfb', 'workdir')) +
            {'hdr': 'headers'} + {'stdout': 'hello\n'} + {'rc': 0} + 0, )

        yield self.wfb.callRemote(
            "startCommand", FakeRemote(st), "13", "shell",
            dict(command=['echo', 'hello'], workdir='workdir'))
        yield st.wait_for_finish()

        def check(_):
            self.assertEqual(st.actions, [
                ['update', [[{
                    'hdr': 'headers'
                }, 0]]],
                ['update', [[{
                    'stdout': 'hello\n'
                }, 0]]],
                ['update', [[{
                    'rc': 0
                }, 0]]],
                ['update', [[{
                    'elapsed': 1
                }, 0]]],
                ['complete', None],
            ])
コード例 #15
0
    def test_truncated(self):
        self.fakemaster.data = test_data = 'tenchars--' * 10

        self.make_command(
            transfer.WorkerFileDownloadCommand,
            dict(
                workdir='.',
                slavedest='data',
                reader=FakeRemote(self.fakemaster),
                maxsize=50,
                blocksize=32,
                mode=0o777,
            ))

        d = self.run_command()

        def check(_):
            self.assertUpdates([
                'read(s)', 'close', {
                    'rc':
                    1,
                    'stderr':
                    "Maximum filesize reached, truncating file '%s'" %
                    os.path.join(self.basedir, '.', 'data')
                }
            ])
            datafile = os.path.join(self.basedir, 'data')
            self.assertTrue(os.path.exists(datafile))
            self.assertEqual(open(datafile).read(), test_data[:50])

        d.addCallback(check)
        return d
コード例 #16
0
    def test_out_of_space_unpack(self):
        self.fakemaster.keep_data = True
        self.fakemaster.unpack_fail = True

        self.make_command(
            transfer.WorkerDirectoryUploadCommand,
            dict(workdir='workdir',
                 slavesrc='data',
                 writer=FakeRemote(self.fakemaster),
                 maxsize=None,
                 blocksize=512,
                 compress=None))

        d = self.run_command()
        self.assertFailure(d, RuntimeError)

        def check(_):
            self.assertUpdates([{
                'header': 'sending %s' % self.datadir
            }, 'write(s)', 'unpack', {
                'rc': 1
            }])

        d.addCallback(check)

        return d
コード例 #17
0
    def test_failure(self):
        self.fakemaster.data = 'hi'

        os.makedirs(os.path.join(self.basedir, 'dir'))
        self.make_command(
            transfer.WorkerFileDownloadCommand,
            dict(
                workdir='.',
                slavedest='dir',  # but that's a directory!
                reader=FakeRemote(self.fakemaster),
                maxsize=None,
                blocksize=32,
                mode=0o777,
            ))

        d = self.run_command()

        def check(_):
            self.assertUpdates([
                'close', {
                    'rc':
                    1,
                    'stderr':
                    "Cannot open file '%s' for download" %
                    os.path.join(self.basedir, '.', 'dir')
                }
            ])

        d.addCallback(check)
        return d
コード例 #18
0
    def test_truncated(self):
        self.fakemaster.count_writes = True  # get actual byte counts

        self.make_command(
            transfer.WorkerFileUploadCommand,
            dict(
                workdir='workdir',
                slavesrc='data',
                writer=FakeRemote(self.fakemaster),
                maxsize=100,
                blocksize=64,
                keepstamp=False,
            ))

        d = self.run_command()

        def check(_):
            self.assertUpdates([{
                'header': 'sending %s' % self.datafile
            }, 'write 64', 'write 36', 'close', {
                'rc':
                1,
                'stderr':
                "Maximum filesize reached, truncating file '%s'" %
                self.datafile
            }])

        d.addCallback(check)
        return d
コード例 #19
0
    def test_simple(self):
        self.fakemaster.count_reads = True  # get actual byte counts
        self.fakemaster.data = test_data = b'1234' * 13
        assert (len(self.fakemaster.data) == 52)

        self.make_command(
            transfer.WorkerFileDownloadCommand,
            dict(
                workdir='.',
                workerdest='data',
                reader=FakeRemote(self.fakemaster),
                maxsize=None,
                blocksize=32,
                mode=0o777,
            ))

        yield self.run_command()

        self.assertUpdates(
            ['read 32', 'read 32', 'read 32', 'close', {
                'rc': 0
            }])
        datafile = os.path.join(self.basedir, 'data')
        self.assertTrue(os.path.exists(datafile))
        with open(datafile, mode="rb") as f:
            datafileContent = f.read()
        self.assertEqual(datafileContent, test_data)
        if runtime.platformType != 'win32':
            self.assertEqual(os.stat(datafile).st_mode & 0o777, 0o777)
コード例 #20
0
    def test_out_of_space(self):
        self.fakemaster.write_out_of_space_at = 70
        self.fakemaster.count_writes = True  # get actual byte counts

        self.make_command(
            transfer.WorkerFileUploadCommand,
            dict(
                workdir='workdir',
                slavesrc='data',
                writer=FakeRemote(self.fakemaster),
                maxsize=1000,
                blocksize=64,
                keepstamp=False,
            ))

        d = self.run_command()
        self.assertFailure(d, RuntimeError)

        def check(_):
            self.assertUpdates([{
                'header': 'sending %s' % self.datafile
            }, 'write 64', 'close', {
                'rc': 1
            }])

        d.addCallback(check)
        return d
コード例 #21
0
    def test_simple(self, compress=None):
        self.fakemaster.keep_data = True

        self.make_command(transfer.WorkerDirectoryUploadCommand, dict(
            workdir='workdir',
            workersrc='data',
            writer=FakeRemote(self.fakemaster),
            maxsize=None,
            blocksize=512,
            compress=compress,
        ))

        d = self.run_command()

        def check(_):
            self.assertUpdates([
                {'header': 'sending %s' % self.datadir},
                'write(s)', 'unpack',  # note no 'close"
                {'rc': 0}
            ])
        d.addCallback(check)

        def check_tarfile(_):
            f = io.BytesIO(self.fakemaster.data)
            a = tarfile.open(fileobj=f, name='check.tar', mode="r")
            exp_names = ['.', 'aa', 'bb']
            got_names = [n.rstrip('/') for n in a.getnames()]
            # py27 uses '' instead of '.'
            got_names = sorted([n or '.' for n in got_names])
            self.assertEqual(got_names, exp_names, "expected archive contents")
            a.close()
            f.close()
        d.addCallback(check_tarfile)

        return d
コード例 #22
0
ファイル: test_bot.py プロジェクト: dinatale2/buildbot
    def setUp(self):
        self.basedir = os.path.abspath("basedir")
        if os.path.exists(self.basedir):
            shutil.rmtree(self.basedir)
        os.makedirs(self.basedir)

        self.real_bot = base.BotBase(self.basedir, False)
        self.real_bot.startService()

        self.bot = FakeRemote(self.real_bot)
コード例 #23
0
    def setUp(self):
        self.basedir = os.path.abspath("basedir")
        if os.path.exists(self.basedir):
            shutil.rmtree(self.basedir)
        os.makedirs(self.basedir)

        # create test-release-file
        with open("{}/test-release-file".format(self.basedir), "w") as fout:
            fout.write("""
# unit test release file
OS_NAME="Test"
VERSION="1.0"
ID=test
ID_LIKE=generic
PRETTY_NAME="Test 1.0 Generic"
VERSION_ID="1"
""")
        self.real_bot = base.BotBase(self.basedir, False)
        self.real_bot.setOsReleaseFile("{}/test-release-file".format(
            self.basedir))
        self.real_bot.startService()

        self.bot = FakeRemote(self.real_bot)
コード例 #24
0
ファイル: test_bot.py プロジェクト: BeiNanWoo/buildbot
    def setUp(self):
        self.basedir = os.path.abspath("basedir")
        if os.path.exists(self.basedir):
            shutil.rmtree(self.basedir)
        os.makedirs(self.basedir)

        self.bot = base.BotBase(self.basedir, False)
        self.bot.startService()

        # get a WorkerForBuilder object from the bot and wrap it as a fake remote
        builders = yield self.bot.remote_setBuilderList([('sb', 'sb')])
        self.sb = FakeRemote(builders['sb'])

        self.setUpCommand()
コード例 #25
0
    def test_out_of_space_unpack(self):
        self.fakemaster.keep_data = True
        self.fakemaster.unpack_fail = True

        self.make_command(
            transfer.WorkerDirectoryUploadCommand,
            dict(workdir='workdir',
                 workersrc='data',
                 writer=FakeRemote(self.fakemaster),
                 maxsize=None,
                 blocksize=512,
                 compress=None))

        yield self.assertFailure(self.run_command(), RuntimeError)

        self.assertUpdates([{
            'header': 'sending {0}\n'.format(self.datadir)
        }, 'write(s)', 'unpack', {
            'rc': 1
        }])
コード例 #26
0
    def test_startCommand_failure(self):
        # set up a fake step to receive updates
        st = FakeStep()

        # patch runprocess to generate a failure
        self.patch_runprocess(
            Expect(['sleep', '10'], os.path.join(self.basedir, 'wfb',
                                                 'workdir')) +
            failure.Failure(Exception("Oops")))
        # patch the log.err, otherwise trial will think something *actually*
        # failed
        self.patch(log, "err", lambda f: None)

        yield self.wfb.callRemote(
            "startCommand", FakeRemote(st), "13", "shell",
            dict(command=['sleep', '10'], workdir='workdir'))

        yield st.wait_for_finish()

        self.assertEqual(st.actions[1][0], 'complete')
        self.assertTrue(isinstance(st.actions[1][1], failure.Failure))
コード例 #27
0
    def test_out_of_space(self):
        self.fakemaster.write_out_of_space_at = 70
        self.fakemaster.count_writes = True  # get actual byte counts

        self.make_command(
            transfer.WorkerFileUploadCommand,
            dict(
                workdir='workdir',
                workersrc='data',
                writer=FakeRemote(self.fakemaster),
                maxsize=1000,
                blocksize=64,
                keepstamp=False,
            ))

        yield self.assertFailure(self.run_command(), RuntimeError)

        self.assertUpdates([{
            'header': 'sending {0}\n'.format(self.datafile)
        }, 'write 64', 'close', {
            'rc': 1
        }])
コード例 #28
0
    def test_missing(self):
        self.make_command(
            transfer.WorkerFileUploadCommand,
            dict(
                workdir='workdir',
                workersrc='data-nosuch',
                writer=FakeRemote(self.fakemaster),
                maxsize=100,
                blocksize=64,
                keepstamp=False,
            ))

        yield self.run_command()

        df = self.datafile + "-nosuch"
        self.assertUpdates([{
            'header': 'sending {0}\n'.format(df)
        }, 'close', {
            'rc':
            1,
            'stderr':
            "Cannot open file '{0}' for upload".format(df)
        }])
コード例 #29
0
    def test_timestamp(self):
        self.fakemaster.count_writes = True  # get actual byte counts
        timestamp = (os.path.getatime(self.datafile),
                     os.path.getmtime(self.datafile))

        self.make_command(
            transfer.WorkerFileUploadCommand,
            dict(
                workdir='workdir',
                workersrc='data',
                writer=FakeRemote(self.fakemaster),
                maxsize=1000,
                blocksize=64,
                keepstamp=True,
            ))

        yield self.run_command()

        self.assertUpdates([{
            'header': 'sending {0}\n'.format(self.datafile)
        }, 'write 64', 'write 64', 'write 52', 'close',
                            'utime - {0}'.format(timestamp[0]), {
                                'rc': 0
                            }])
コード例 #30
0
ファイル: test_bot.py プロジェクト: dinatale2/buildbot
class TestBot(unittest.TestCase):

    def setUp(self):
        self.basedir = os.path.abspath("basedir")
        if os.path.exists(self.basedir):
            shutil.rmtree(self.basedir)
        os.makedirs(self.basedir)

        self.real_bot = base.BotBase(self.basedir, False)
        self.real_bot.startService()

        self.bot = FakeRemote(self.real_bot)

    def tearDown(self):
        d = defer.succeed(None)
        if self.real_bot and self.real_bot.running:
            d.addCallback(lambda _: self.real_bot.stopService())
        if os.path.exists(self.basedir):
            shutil.rmtree(self.basedir)
        return d

    def test_getCommands(self):
        d = self.bot.callRemote("getCommands")

        def check(cmds):
            # just check that 'shell' is present..
            self.assertTrue('shell' in cmds)
        d.addCallback(check)
        return d

    def test_getVersion(self):
        d = self.bot.callRemote("getVersion")

        def check(vers):
            self.assertEqual(vers, buildbot_worker.version)
        d.addCallback(check)
        return d

    def test_getWorkerInfo(self):
        infodir = os.path.join(self.basedir, "info")
        os.makedirs(infodir)
        open(os.path.join(infodir, "admin"), "w").write("testy!")
        open(os.path.join(infodir, "foo"), "w").write("bar")
        open(os.path.join(infodir, "environ"), "w").write("something else")

        d = self.bot.callRemote("getWorkerInfo")

        def check(info):
            self.assertEqual(info, dict(
                admin='testy!', foo='bar',
                environ=os.environ, system=os.name, basedir=self.basedir,
                worker_commands=self.real_bot.remote_getCommands(),
                version=self.real_bot.remote_getVersion(),
                numcpus=multiprocessing.cpu_count()))
        d.addCallback(check)
        return d

    def test_getWorkerInfo_nodir(self):
        d = self.bot.callRemote("getWorkerInfo")

        def check(info):
            self.assertEqual(set(info.keys()), set(
                ['environ', 'system', 'numcpus', 'basedir', 'worker_commands', 'version']))
        d.addCallback(check)
        return d

    def test_setBuilderList_empty(self):
        d = self.bot.callRemote("setBuilderList", [])

        def check(builders):
            self.assertEqual(builders, {})
        d.addCallback(check)
        return d

    def test_setBuilderList_single(self):
        d = self.bot.callRemote("setBuilderList", [('mybld', 'myblddir')])

        def check(builders):
            self.assertEqual(list(builders), ['mybld'])
            self.assertTrue(
                os.path.exists(os.path.join(self.basedir, 'myblddir')))
            # note that we test the WorkerForBuilder instance below
        d.addCallback(check)
        return d

    def test_setBuilderList_updates(self):
        d = defer.succeed(None)

        workerforbuilders = {}

        def add_my(_):
            d = self.bot.callRemote("setBuilderList", [
                ('mybld', 'myblddir')])

            def check(builders):
                self.assertEqual(list(builders), ['mybld'])
                self.assertTrue(
                    os.path.exists(os.path.join(self.basedir, 'myblddir')))
                workerforbuilders['my'] = builders['mybld']
            d.addCallback(check)
            return d
        d.addCallback(add_my)

        def add_your(_):
            d = self.bot.callRemote("setBuilderList", [
                ('mybld', 'myblddir'), ('yourbld', 'yourblddir')])

            def check(builders):
                self.assertEqual(
                    sorted(builders.keys()), sorted(['mybld', 'yourbld']))
                self.assertTrue(
                    os.path.exists(os.path.join(self.basedir, 'myblddir')))
                self.assertTrue(
                    os.path.exists(os.path.join(self.basedir, 'yourblddir')))
                # 'my' should still be the same WorkerForBuilder object
                self.assertEqual(
                    id(workerforbuilders['my']), id(builders['mybld']))
                workerforbuilders['your'] = builders['yourbld']
            d.addCallback(check)
            return d
        d.addCallback(add_your)

        def remove_my(_):
            d = self.bot.callRemote("setBuilderList", [
                ('yourbld', 'yourblddir2')])  # note new builddir

            def check(builders):
                self.assertEqual(sorted(builders.keys()), sorted(['yourbld']))
                # note that build dirs are not deleted..
                self.assertTrue(
                    os.path.exists(os.path.join(self.basedir, 'myblddir')))
                self.assertTrue(
                    os.path.exists(os.path.join(self.basedir, 'yourblddir')))
                self.assertTrue(
                    os.path.exists(os.path.join(self.basedir, 'yourblddir2')))
                # 'your' should still be the same WorkerForBuilder object
                self.assertEqual(
                    id(workerforbuilders['your']), id(builders['yourbld']))
            d.addCallback(check)
            return d
        d.addCallback(remove_my)

        def add_and_remove(_):
            d = self.bot.callRemote("setBuilderList", [
                ('theirbld', 'theirblddir')])

            def check(builders):
                self.assertEqual(sorted(builders.keys()), sorted(['theirbld']))
                self.assertTrue(
                    os.path.exists(os.path.join(self.basedir, 'myblddir')))
                self.assertTrue(
                    os.path.exists(os.path.join(self.basedir, 'yourblddir')))
                self.assertTrue(
                    os.path.exists(os.path.join(self.basedir, 'theirblddir')))
            d.addCallback(check)
            return d
        d.addCallback(add_and_remove)

        return d

    def test_shutdown(self):
        d1 = defer.Deferred()
        self.patch(reactor, "stop", lambda: d1.callback(None))
        d2 = self.bot.callRemote("shutdown")
        # don't return until both the shutdown method has returned, and
        # reactor.stop has been called
        return defer.gatherResults([d1, d2])
コード例 #31
0
ファイル: test_bot.py プロジェクト: dinatale2/buildbot
class TestWorkerForBuilder(command.CommandTestMixin, unittest.TestCase):

    @defer.inlineCallbacks
    def setUp(self):
        self.basedir = os.path.abspath("basedir")
        if os.path.exists(self.basedir):
            shutil.rmtree(self.basedir)
        os.makedirs(self.basedir)

        self.bot = base.BotBase(self.basedir, False)
        self.bot.startService()

        # get a WorkerForBuilder object from the bot and wrap it as a fake
        # remote
        builders = yield self.bot.remote_setBuilderList([('wfb', 'wfb')])
        self.wfb = FakeRemote(builders['wfb'])

        self.setUpCommand()

    def tearDown(self):
        self.tearDownCommand()

        d = defer.succeed(None)
        if self.bot and self.bot.running:
            d.addCallback(lambda _: self.bot.stopService())
        if os.path.exists(self.basedir):
            shutil.rmtree(self.basedir)
        return d

    def test_print(self):
        return self.wfb.callRemote("print", "Hello, WorkerForBuilder.")

    def test_setMaster(self):
        # not much to check here - what the WorkerForBuilder does with the
        # master is not part of the interface (and, in fact, it does very
        # little)
        return self.wfb.callRemote("setMaster", mock.Mock())

    def test_startBuild(self):
        return self.wfb.callRemote("startBuild")

    def test_startCommand(self):
        # set up a fake step to receive updates
        st = FakeStep()

        # patch runprocess to handle the 'echo', below
        self.patch_runprocess(
            Expect(['echo', 'hello'], os.path.join(
                self.basedir, 'wfb', 'workdir')) +
            {'hdr': 'headers'} +
            {'stdout': 'hello\n'} +
            {'rc': 0} +
            0,
        )

        d = defer.succeed(None)

        def do_start(_):
            return self.wfb.callRemote("startCommand", FakeRemote(st),
                                       "13", "shell", dict(
                command=['echo', 'hello'],
                workdir='workdir'))
        d.addCallback(do_start)
        d.addCallback(lambda _: st.wait_for_finish())

        def check(_):
            self.assertEqual(st.actions, [
                ['update', [[{'hdr': 'headers'}, 0]]],
                ['update', [[{'stdout': 'hello\n'}, 0]]],
                ['update', [[{'rc': 0}, 0]]],
                ['update', [[{'elapsed': 1}, 0]]],
                ['complete', None],
            ])
        d.addCallback(check)
        return d

    def test_startCommand_interruptCommand(self):
        # set up a fake step to receive updates
        st = FakeStep()

        # patch runprocess to pretend to sleep (it will really just hang forever,
        # except that we interrupt it)
        self.patch_runprocess(
            Expect(['sleep', '10'], os.path.join(
                self.basedir, 'wfb', 'workdir')) +
            {'hdr': 'headers'} +
            {'wait': True}
        )

        d = defer.succeed(None)

        def do_start(_):
            return self.wfb.callRemote("startCommand", FakeRemote(st),
                                       "13", "shell", dict(
                command=['sleep', '10'],
                workdir='workdir'))
        d.addCallback(do_start)

        # wait a jiffy..
        def do_wait(_):
            d = defer.Deferred()
            reactor.callLater(0.01, d.callback, None)
            return d
        d.addCallback(do_wait)

        # and then interrupt the step
        def do_interrupt(_):
            return self.wfb.callRemote("interruptCommand", "13", "tl/dr")
        d.addCallback(do_interrupt)

        d.addCallback(lambda _: st.wait_for_finish())

        def check(_):
            self.assertEqual(st.actions, [
                ['update', [[{'hdr': 'headers'}, 0]]],
                ['update', [[{'hdr': 'killing'}, 0]]],
                ['update', [[{'rc': -1}, 0]]],
                ['complete', None],
            ])
        d.addCallback(check)
        return d

    def test_startCommand_failure(self):
        # set up a fake step to receive updates
        st = FakeStep()

        # patch runprocess to generate a failure
        self.patch_runprocess(
            Expect(['sleep', '10'], os.path.join(
                self.basedir, 'wfb', 'workdir')) +
            failure.Failure(Exception("Oops"))
        )
        # patch the log.err, otherwise trial will think something *actually*
        # failed
        self.patch(log, "err", lambda f: None)

        d = defer.succeed(None)

        def do_start(_):
            return self.wfb.callRemote("startCommand", FakeRemote(st),
                                       "13", "shell", dict(
                command=['sleep', '10'],
                workdir='workdir'))
        d.addCallback(do_start)
        d.addCallback(lambda _: st.wait_for_finish())

        def check(_):
            self.assertEqual(st.actions[1][0], 'complete')
            self.assertTrue(isinstance(st.actions[1][1], failure.Failure))
        d.addCallback(check)
        return d

    @defer.inlineCallbacks
    def test_startCommand_missing_args(self):
        # set up a fake step to receive updates
        st = FakeStep()

        def do_start():
            return self.wfb.callRemote("startCommand", FakeRemote(st),
                                       "13", "shell", dict())

        yield self.assertFailure(do_start(), ValueError)
コード例 #32
0
 def do_start():
     return self.wfb.callRemote("startCommand", FakeRemote(st), "13",
                                "invalid command", dict())
コード例 #33
0
class TestBot(unittest.TestCase):
    def setUp(self):
        self.basedir = os.path.abspath("basedir")
        if os.path.exists(self.basedir):
            shutil.rmtree(self.basedir)
        os.makedirs(self.basedir)

        self.real_bot = base.BotBase(self.basedir, False)
        self.real_bot.startService()

        self.bot = FakeRemote(self.real_bot)

    def tearDown(self):
        d = defer.succeed(None)
        if self.real_bot and self.real_bot.running:
            d.addCallback(lambda _: self.real_bot.stopService())
        if os.path.exists(self.basedir):
            shutil.rmtree(self.basedir)
        return d

    def test_getCommands(self):
        d = self.bot.callRemote("getCommands")

        def check(cmds):
            # just check that 'shell' is present..
            self.assertTrue('shell' in cmds)

        d.addCallback(check)
        return d

    def test_getVersion(self):
        d = self.bot.callRemote("getVersion")

        def check(vers):
            self.assertEqual(vers, buildbot_worker.version)

        d.addCallback(check)
        return d

    def test_getWorkerInfo(self):
        infodir = os.path.join(self.basedir, "info")
        os.makedirs(infodir)
        with open(os.path.join(infodir, "admin"), "w") as f:
            f.write("testy!")
        with open(os.path.join(infodir, "foo"), "w") as f:
            f.write("bar")
        with open(os.path.join(infodir, "environ"), "w") as f:
            f.write("something else")

        d = self.bot.callRemote("getWorkerInfo")

        def check(info):
            self.assertEqual(
                info,
                dict(admin='testy!',
                     foo='bar',
                     environ=os.environ,
                     system=os.name,
                     basedir=self.basedir,
                     worker_commands=self.real_bot.remote_getCommands(),
                     version=self.real_bot.remote_getVersion(),
                     numcpus=multiprocessing.cpu_count()))

        d.addCallback(check)
        return d

    def test_getWorkerInfo_nodir(self):
        d = self.bot.callRemote("getWorkerInfo")

        def check(info):
            self.assertEqual(
                set(info.keys()),
                set([
                    'environ', 'system', 'numcpus', 'basedir',
                    'worker_commands', 'version'
                ]))

        d.addCallback(check)
        return d

    def test_setBuilderList_empty(self):
        d = self.bot.callRemote("setBuilderList", [])

        def check(builders):
            self.assertEqual(builders, {})

        d.addCallback(check)
        return d

    def test_setBuilderList_single(self):
        d = self.bot.callRemote("setBuilderList", [('mybld', 'myblddir')])

        def check(builders):
            self.assertEqual(list(builders), ['mybld'])
            self.assertTrue(
                os.path.exists(os.path.join(self.basedir, 'myblddir')))
            # note that we test the WorkerForBuilder instance below

        d.addCallback(check)
        return d

    def test_setBuilderList_updates(self):
        d = defer.succeed(None)

        workerforbuilders = {}

        def add_my(_):
            d = self.bot.callRemote("setBuilderList", [('mybld', 'myblddir')])

            def check(builders):
                self.assertEqual(list(builders), ['mybld'])
                self.assertTrue(
                    os.path.exists(os.path.join(self.basedir, 'myblddir')))
                workerforbuilders['my'] = builders['mybld']

            d.addCallback(check)
            return d

        d.addCallback(add_my)

        def add_your(_):
            d = self.bot.callRemote("setBuilderList",
                                    [('mybld', 'myblddir'),
                                     ('yourbld', 'yourblddir')])

            def check(builders):
                self.assertEqual(sorted(builders.keys()),
                                 sorted(['mybld', 'yourbld']))
                self.assertTrue(
                    os.path.exists(os.path.join(self.basedir, 'myblddir')))
                self.assertTrue(
                    os.path.exists(os.path.join(self.basedir, 'yourblddir')))
                # 'my' should still be the same WorkerForBuilder object
                self.assertEqual(id(workerforbuilders['my']),
                                 id(builders['mybld']))
                workerforbuilders['your'] = builders['yourbld']
                self.assertTrue(
                    repr(workerforbuilders['your']).startswith(
                        "<WorkerForBuilder 'yourbld' at "))

            d.addCallback(check)
            return d

        d.addCallback(add_your)

        def remove_my(_):
            d = self.bot.callRemote(
                "setBuilderList",
                [('yourbld', 'yourblddir2')])  # note new builddir

            def check(builders):
                self.assertEqual(sorted(builders.keys()), sorted(['yourbld']))
                # note that build dirs are not deleted..
                self.assertTrue(
                    os.path.exists(os.path.join(self.basedir, 'myblddir')))
                self.assertTrue(
                    os.path.exists(os.path.join(self.basedir, 'yourblddir')))
                self.assertTrue(
                    os.path.exists(os.path.join(self.basedir, 'yourblddir2')))
                # 'your' should still be the same WorkerForBuilder object
                self.assertEqual(id(workerforbuilders['your']),
                                 id(builders['yourbld']))

            d.addCallback(check)
            return d

        d.addCallback(remove_my)

        def add_and_remove(_):
            d = self.bot.callRemote("setBuilderList",
                                    [('theirbld', 'theirblddir')])

            def check(builders):
                self.assertEqual(sorted(builders.keys()), sorted(['theirbld']))
                self.assertTrue(
                    os.path.exists(os.path.join(self.basedir, 'myblddir')))
                self.assertTrue(
                    os.path.exists(os.path.join(self.basedir, 'yourblddir')))
                self.assertTrue(
                    os.path.exists(os.path.join(self.basedir, 'theirblddir')))

            d.addCallback(check)
            return d

        d.addCallback(add_and_remove)

        return d

    def test_shutdown(self):
        d1 = defer.Deferred()
        self.patch(reactor, "stop", lambda: d1.callback(None))
        d2 = self.bot.callRemote("shutdown")
        # don't return until both the shutdown method has returned, and
        # reactor.stop has been called
        return defer.gatherResults([d1, d2])
コード例 #34
0
 def do_start(_):
     return self.wfb.callRemote(
         "startCommand", FakeRemote(st), "13", "shell",
         dict(command=['sleep', '10'], workdir='workdir'))