Ticket #1262: start-m-with-test.diff

File start-m-with-test.diff, 10.5 KB (added by warner, at 2010-11-25T08:13:59Z)

better patch: includes test, removes -m from --help when unavailable

  • src/allmydata/scripts/common.py

    diff --git a/src/allmydata/scripts/common.py b/src/allmydata/scripts/common.py
    index 96879b6..5e4bfb9 100644
    a b class BaseOptions(usage.Options): 
    5454class BasedirMixin:
    5555    default_nodedir = _default_nodedir
    5656    allow_multiple = True
     57    # startstop_node.py needs os.fork to implement the "-m" option on "tahoe
     58    # start" option
     59    can_start_multiple = hasattr(os, "fork")
     60
    5761
    5862    optParameters = [
    5963        ["basedir", "C", None, "Same as --node-directory."],
    6064    ]
    61     optFlags = [
    62         ["multiple", "m", "Specify multiple node directories at once."],
    63     ]
     65    optFlags = [ ]
     66    if can_start_multiple:
     67        optFlags.append(["multiple", "m",
     68                         "Specify multiple node directories at once."])
    6469
    6570    def parseArgs(self, *args):
    6671        if self['node-directory'] and self['basedir']:
  • src/allmydata/scripts/startstop_node.py

    diff --git a/src/allmydata/scripts/startstop_node.py b/src/allmydata/scripts/startstop_node.py
    index bc2616e..c76c9d8 100644
    a b class RunOptions(BasedirMixin, BaseOptions): 
    2626
    2727    optParameters = [
    2828        ["node-directory", "d", None, "Specify the directory of the node to be run. [default, for 'tahoe run' only: current directory]"],
    29         ["multiple", "m", None, "['tahoe run' cannot accept multiple node directories]"],
    30     ]
    31 
    32 def do_start(basedir, opts, out=sys.stdout, err=sys.stderr):
     29        ]
     30    optFlags = [ ]
     31    if BasedirMixin.can_start_multiple:
     32        # usage.Options doesn't let us remove flags that we inherit from a
     33        # parent class, so at least provide a --help string that warns people
     34        # away from using it. There is also code (switching on
     35        # allow_multiple) to disable it at runtime.
     36        optFlags.append(["multiple", "m",
     37                         "['tahoe run' cannot accept multiple node directories]"])
     38
     39def do_start(basedir, opts, out=sys.stdout, err=sys.stderr, fork=False):
    3340    print >>out, "STARTING", quote_output(basedir)
    3441    if not os.path.isdir(basedir):
    3542        print >>err, "%s does not look like a directory at all" % quote_output(basedir)
    def do_start(basedir, opts, out=sys.stdout, err=sys.stderr): 
    5764    if opts["profile"]:
    5865        args.extend(["--profile=profiling_results.prof", "--savestats",])
    5966    # now we're committed
     67    if fork:
     68        if os.fork() != 0:
     69            return 0 # parent
     70        # we're in the child
    6071    os.chdir(basedir)
    6172    from twisted.scripts import twistd
    6273    sys.argv = args
    def do_stop(basedir, out=sys.stdout, err=sys.stderr): 
    123134
    124135def start(config, stdout, stderr):
    125136    rc = 0
    126     for basedir in config['basedirs']:
    127         rc = do_start(basedir, config, stdout, stderr) or rc
     137    for basedir in config['basedirs'][:-1]:
     138        # fork before starting all but the last one
     139        rc = do_start(basedir, config, stdout, stderr, fork=True) or rc
     140    # start the last one in the current process, to capture its exit code
     141    rc = do_start(config['basedirs'][-1], config, stdout, stderr, fork=False) or rc
    128142    return rc
    129143
    130144def stop(config, stdout, stderr):
    def restart(config, stdout, stderr): 
    143157    if rc:
    144158        print >>stderr, "not restarting"
    145159        return rc
    146     for basedir in config['basedirs']:
    147         rc = do_start(basedir, config, stdout, stderr) or rc
     160    rc = start(config, stdout, stderr) or rc
    148161    return rc
    149162
    150163def run(config, stdout, stderr):
  • src/allmydata/test/test_runner.py

    diff --git a/src/allmydata/test/test_runner.py b/src/allmydata/test/test_runner.py
    index 6a49f6b..e9a9375 100644
    a b class RunNode(common_util.SignalMixin, unittest.TestCase, pollmixin.PollMixin, 
    578578        d.addBoth(_remove_hotline)
    579579        return d
    580580
     581    def test_multiple_clients(self):
     582        self.skip_if_cannot_daemonize()
     583        basedir = self.workdir("test_client")
     584        # this furl must be syntactically correct, else 'tahoe start' will
     585        # correctly report an error. It doesn't need to point at a real
     586        # introducer.
     587        introducer_furl = "pb://xrndsskn2zuuian5ltnxrte7lnuqdrkz@127.0.0.1:55617/introducer"
     588        c1 = os.path.join(basedir, "c1")
     589        HOTLINE_FILE_1 = os.path.join(c1, "suicide_prevention_hotline")
     590        TWISTD_PID_FILE_1 = os.path.join(c1, "twistd.pid")
     591        PORTNUMFILE_1 = os.path.join(c1, "client.port")
     592        c1_args = ["--quiet", "create-node", "--basedir", c1, "--webport", "0",
     593                   "--introducer", introducer_furl]
     594
     595        c2 = os.path.join(basedir, "c2")
     596        HOTLINE_FILE_2 = os.path.join(c2, "suicide_prevention_hotline")
     597        TWISTD_PID_FILE_2 = os.path.join(c2, "twistd.pid")
     598        PORTNUMFILE_2 = os.path.join(c2, "client.port")
     599        c2_args = ["--quiet", "create-node", "--basedir", c2, "--webport", "0",
     600                   "--introducer", introducer_furl]
     601
     602        d = utils.getProcessOutputAndValue(bintahoe, args=c1_args, env=os.environ)
     603        def _cb1(res):
     604            out, err, rc_or_sig = res
     605            self.failUnlessEqual(rc_or_sig, 0)
     606            return utils.getProcessOutputAndValue(bintahoe, args=c2_args,
     607                                                  env=os.environ)
     608        d.addCallback(_cb1)
     609        def _cb2(res):
     610            out, err, rc_or_sig = res
     611            self.failUnlessEqual(rc_or_sig, 0)
     612            # both nodes have been constructed, but they are not yet running
     613
     614            # By writing this file, we get sixty seconds before the client
     615            # will exit. This insures that even if the 'stop' command doesn't
     616            # work (and the test fails), the client should still terminate.
     617            open(HOTLINE_FILE_1, "w").write("")
     618            open(HOTLINE_FILE_2, "w").write("")
     619            # now it's safe to start the nodes
     620            start_args = ["--quiet", "start", "-m", c1, c2]
     621            return utils.getProcessOutputAndValue(bintahoe, args=start_args,
     622                                                  env=os.environ)
     623        d.addCallback(_cb2)
     624
     625        def _node_has_started():
     626            open(HOTLINE_FILE_1, "w").write("")
     627            open(HOTLINE_FILE_2, "w").write("")
     628            return (os.path.exists(PORTNUMFILE_1)
     629                    and os.path.exists(PORTNUMFILE_2))
     630
     631        def _cb3(res):
     632            out, err, rc_or_sig = res
     633            open(HOTLINE_FILE_1, "w").write("")
     634            open(HOTLINE_FILE_2, "w").write("")
     635            errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
     636            self.failUnlessEqual(rc_or_sig, 0, errstr)
     637            self.failUnlessEqual(out, "", errstr)
     638            # See test_client_no_noise -- for now we ignore noise.
     639            # self.failUnlessEqual(err, "", errstr)
     640
     641            # if 'tahoe start' exited without error, we're guaranteed that it
     642            # successfully loaded the code and tahoe.cfg for the last node
     643            # (argv[-1]), since these happen before twistd.run() calls
     644            # fork(). There made have been a runtime error after fork().
     645            # Also, there may have been an import or config error while
     646            # loading any of the earlier nodes (argv[:-1]), since 'tahoe
     647            # start' must do an additional fork before calling twistd.run()
     648            # on those.
     649
     650            # so watch the filesystem to determine when the nodes have
     651            # started running
     652
     653            return self.poll(_node_has_started)
     654        d.addCallback(_cb3)
     655
     656        def _restart(ign):
     657            # delete the port-number files, then restart the nodes. The new
     658            # instances will re-write those files.
     659            os.unlink(PORTNUMFILE_1)
     660            os.unlink(PORTNUMFILE_2)
     661            restart_args = ["--quiet", "restart", "-m", c1, c2]
     662            return utils.getProcessOutputAndValue(bintahoe, args=restart_args,
     663                                                  env=os.environ)
     664        d.addCallback(_restart)
     665        def _did_restart(res):
     666            out, err, rc_or_sig = res
     667            errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
     668            self.failUnlessEqual(rc_or_sig, 0, errstr)
     669            self.failUnlessEqual(out, "", errstr)
     670            # See test_client_no_noise -- for now we ignore noise.
     671            # self.failUnlessEqual(err, "", errstr)
     672
     673            # we must still poll for the startup process to get far enough
     674            return self.poll(_node_has_started)
     675        d.addCallback(_did_restart)
     676        # when that finishes, we know the nodes have restarted. If 'tahoe
     677        # restart -m' is broken, that will time out.
     678
     679        # now we can kill it. TODO: On a slow machine, the node might kill
     680        # itself before we get a chance too, especially if spawning the
     681        # 'tahoe stop' command takes a while.
     682        def _stop(res):
     683            open(HOTLINE_FILE_1, "w").write("")
     684            open(HOTLINE_FILE_2, "w").write("")
     685            self.failUnless(os.path.exists(TWISTD_PID_FILE_1),
     686                            (TWISTD_PID_FILE_1, os.listdir(c1)))
     687            self.failUnless(os.path.exists(TWISTD_PID_FILE_2),
     688                            (TWISTD_PID_FILE_2, os.listdir(c2)))
     689            stop_args = ["--quiet", "stop", "-m", c1, c2]
     690            return utils.getProcessOutputAndValue(bintahoe, args=stop_args,
     691                                                  env=os.environ)
     692        d.addCallback(_stop)
     693
     694        def _cb4(res):
     695            out, err, rc_or_sig = res
     696            errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
     697            self.failUnlessEqual(rc_or_sig, 0, errstr)
     698            self.failUnlessEqual(out, "", errstr)
     699            # See test_client_no_noise -- for now we ignore noise.
     700            # self.failUnlessEqual(err, "", errstr)
     701
     702            # the parent was supposed to poll and wait until it sees
     703            # twistd.pid go away before it exits, so twistd.pid should be
     704            # gone by now.
     705            self.failIf(os.path.exists(TWISTD_PID_FILE_1))
     706            self.failIf(os.path.exists(TWISTD_PID_FILE_2))
     707        d.addCallback(_cb4)
     708        def _remove_hotline(res):
     709            # always remove these, so that if something went wrong, the nodes
     710            # will quit eventually
     711            os.unlink(HOTLINE_FILE_1)
     712            os.unlink(HOTLINE_FILE_2)
     713            return res
     714        d.addBoth(_remove_hotline)
     715        return d
     716
     717
    581718    def test_baddir(self):
    582719        self.skip_if_cannot_daemonize()
    583720        basedir = self.workdir("test_baddir")