[Buildbot-commits] buildbot/buildbot/test emitlogs.py, 1.1, 1.2 runutils.py, 1.10, 1.11 test_shell.py, 1.1, 1.2 test_vc.py, 1.63, 1.64

Brian Warner warner at users.sourceforge.net
Tue Jun 20 08:09:00 UTC 2006


Update of /cvsroot/buildbot/buildbot/buildbot/test
In directory sc8-pr-cvs3.sourceforge.net:/tmp/cvs-serv14657/buildbot/test

Modified Files:
	emitlogs.py runutils.py test_shell.py test_vc.py 
Log Message:
[project @ add support for following multiple LogFiles in a ShellCommand]

Original author: warner at lothar.com
Date: 2006-06-20 04:17:18

Index: emitlogs.py
===================================================================
RCS file: /cvsroot/buildbot/buildbot/buildbot/test/emitlogs.py,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -d -r1.1 -r1.2
--- emitlogs.py	20 Jun 2006 08:08:39 -0000	1.1
+++ emitlogs.py	20 Jun 2006 08:08:58 -0000	1.2
@@ -1,15 +1,23 @@
 #! /usr/bin/python
 
-import os, sys, time
+import sys, time
 
-log2 = open("log2", "wt")
-log3 = open("log3", "wt")
+log2 = open("log2.out", "wt")
+log3 = open("log3.out", "wt")
 
-for i in range(3):
-    sys.stdout.write("this is stdout %d\n" % i)
+def write(i):
     log2.write("this is log2 %d\n" % i)
+    log2.flush()
     log3.write("this is log3 %d\n" % i)
-    time.sleep(1)
+    log3.flush()
+    sys.stdout.write("this is stdout %d\n" % i)
+    sys.stdout.flush()
+
+write(0)
+time.sleep(1)
+write(1)
+sys.stdin.read(1)
+write(2)
 
 log2.close()
 log3.close()

Index: runutils.py
===================================================================
RCS file: /cvsroot/buildbot/buildbot/buildbot/test/runutils.py,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -d -r1.10 -r1.11
--- runutils.py	20 Jun 2006 08:08:51 -0000	1.10
+++ runutils.py	20 Jun 2006 08:08:58 -0000	1.11
@@ -276,16 +276,28 @@
         d = c.doStart()
         return d
 
-    def collectUpdates(self, res):
+    def collectUpdates(self, res=None):
         logs = {}
         for u in self.builder.updates:
             for k in u.keys():
                 if k == "log":
                     logname,data = u[k]
                     oldlog = logs.get(("log",logname), "")
-                    logs[("log",logname)] = oldlog + u[k]
+                    logs[("log",logname)] = oldlog + data
                 elif k == "rc":
                     pass
                 else:
                     logs[k] = logs.get(k, "") + u[k]
         return logs
+
+    def findRC(self):
+        for u in self.builder.updates:
+            if "rc" in u:
+                return u["rc"]
+        return None
+
+    def printStderr(self):
+        for u in self.builder.updates:
+            if "stderr" in u:
+                print u["stderr"]
+

Index: test_shell.py
===================================================================
RCS file: /cvsroot/buildbot/buildbot/buildbot/test/test_shell.py,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -d -r1.1 -r1.2
--- test_shell.py	20 Jun 2006 08:08:39 -0000	1.1
+++ test_shell.py	20 Jun 2006 08:08:58 -0000	1.2
@@ -2,8 +2,10 @@
 
 # test step.ShellCommand and the slave-side commands.ShellCommand
 
-import sys
+import sys, time, os
 from twisted.trial import unittest
+from twisted.internet import reactor, defer
+from twisted.python import util
 from buildbot.process.step import ShellCommand
 from buildbot.slave.commands import SlaveShellCommand
 from buildbot.twcompat import maybeWait
@@ -11,8 +13,10 @@
 
 class SlaveSide(SlaveCommandTestBase, unittest.TestCase):
     def testOne(self):
+        self.setUpBuilder("test_shell.testOne")
+        emitcmd = util.sibpath(__file__, "emit.py")
         args = {
-            'command': [sys.executable, "emit.py", "0"],
+            'command': [sys.executable, emitcmd, "0"],
             'workdir': ".",
             }
         d = self.startCommand(SlaveShellCommand, args)
@@ -32,22 +36,41 @@
         return "".join(lines)
 
     def testLogFiles(self):
-        # emitlogs.py writes one line per second to stdout and two logfiles,
-        # for 3 seconds total.
+        basedir = "test_shell.testLogFiles"
+        self.setUpBuilder(basedir)
+        # emitlogs.py writes two lines to stdout and two logfiles, one second
+        # apart. Then it waits for us to write something to stdin, then it
+        # writes one more line.
+
+        # we write something to the log file first, to exercise the logic
+        # that distinguishes between the old file and the one as modified by
+        # the ShellCommand. We set the timestamp back 5 seconds so that
+        # timestamps can be used to distinguish old from new.
+        log2file = os.path.join(basedir, "log2.out")
+        f = open(log2file, "w")
+        f.write("dummy text\n")
+        f.close()
+        earlier = time.time() - 5
+        os.utime(log2file, (earlier, earlier))
+
         args = {
-            'command': [sys.executable, "emitlogs.py"],
+            'command': [sys.executable,
+                        util.sibpath(__file__, "emitlogs.py")],
             'workdir': ".",
             'logfiles': {"log2": "log2.out",
                          "log3": "log3.out"},
+            'keep_stdin_open': True,
             }
-        d = self.startCommand(SlaveShellCommand, args)
-        # after two seconds, there should be some data in the secondary
-        # logfiles
-
-        # TODO: I want to test that logfiles are being read in a timely
-        # fashion. How can I do this and still have the tests be reliable
-        # under load?
+        finishd = self.startCommand(SlaveShellCommand, args)
+        # The first batch of lines is written immediately. The second is
+        # written after a pause of one second. We poll once per second until
+        # we see both batches.
 
+        self._check_timeout = 10
+        d = self._check_and_wait()
+        def _wait_for_finish(res, finishd):
+            return finishd
+        d.addCallback(_wait_for_finish, finishd)
         d.addCallback(self.collectUpdates)
         def _check(logs):
             self.failUnlessEqual(logs['stdout'], self._generateText("stdout"))
@@ -56,18 +79,35 @@
             self.failUnlessEqual(logs[('log','log3')],
                                  self._generateText("log3"))
         d.addCallback(_check)
+        d.addBoth(self._maybePrintError)
         return maybeWait(d)
-    testLogFiles.todo = "doesn't work yet"
 
+    def _check_and_wait(self, res=None):
+        self._check_timeout -= 1
+        if self._check_timeout <= 0:
+            raise defer.TimeoutError("gave up on command")
+        logs = self.collectUpdates()
+        if logs.get('stdout') == "this is stdout 0\nthis is stdout 1\n":
+            # the emitlogs.py process is now waiting for something to arrive
+            # on stdin
+            self.cmd.command.pp.transport.write("poke\n")
+            return
+        if not self.cmd.running:
+            self.fail("command finished too early")
+        spin = defer.Deferred()
+        spin.addCallback(self._check_and_wait)
+        reactor.callLater(1, spin.callback, None)
+        return spin
+
+    def _maybePrintError(self, res):
+        rc = self.findRC()
+        if rc != 0:
+            print "Command ended with rc=%s" % rc
+            print "STDERR:"
+            self.printStderr()
+        return res
+
+    # MAYBE TODO: a command which appends to an existing logfile should
+    # result in only the new text being sent up to the master. I need to
+    # think about this more first.
 
-def OFF_testLogfiles_1(self, res, ss):
-    logs = {}
-    for l in ss.getLogs():
-        logs[l.getName()] = l
-    self.failUnlessEqual(logs['stdio'].getText(),
-                         self.generateText("stdout"))
-    return
-    self.failUnlessEqual(logs['log2'].getText(),
-                         self.generateText("log2"))
-    self.failUnlessEqual(logs['log3'].getText(),
-                         self.generateText("log3"))

Index: test_vc.py
===================================================================
RCS file: /cvsroot/buildbot/buildbot/buildbot/test/test_vc.py,v
retrieving revision 1.63
retrieving revision 1.64
diff -u -d -r1.63 -r1.64
--- test_vc.py	20 Jun 2006 08:08:17 -0000	1.63
+++ test_vc.py	20 Jun 2006 08:08:58 -0000	1.64
@@ -2354,7 +2354,7 @@
             def sendUpdate(self, status):
                 pass
         c = commands.ShellCommand(FakeBuilder(), command, self.workdir,
-                                  sendRC=False, stdin=p0_diff)
+                                  sendRC=False, initialStdin=p0_diff)
         d = c.start()
         d.addCallback(self._testPatch_1)
         return maybeWait(d)





More information about the Commits mailing list