Do more path stuff with FilePath

This commit is contained in:
Jean-Paul Calderone 2020-11-26 20:53:57 -05:00
parent a978fcf433
commit 805378ef11
7 changed files with 86 additions and 72 deletions

View File

@ -124,7 +124,7 @@ def write_introducer(basedir, petname, furl):
Overwrite the node's ``introducers.yaml`` with a file containing the given Overwrite the node's ``introducers.yaml`` with a file containing the given
introducer information. introducer information.
""" """
FilePath(basedir).child(b"private").child(b"introducers.yaml").setContent( basedir.child(b"private").child(b"introducers.yaml").setContent(
safe_dump({ safe_dump({
"introducers": { "introducers": {
petname: { petname: {

View File

@ -5,6 +5,9 @@ import json
from twisted.internet import reactor, defer from twisted.internet import reactor, defer
from twisted.python.usage import UsageError from twisted.python.usage import UsageError
from twisted.python.filepath import (
FilePath,
)
from allmydata.scripts.common import ( from allmydata.scripts.common import (
BasedirOptions, BasedirOptions,
@ -308,7 +311,7 @@ def write_client_config(c, config):
introducer = config.get("introducer", None) introducer = config.get("introducer", None)
if introducer is not None: if introducer is not None:
write_introducer( write_introducer(
config["basedir"], FilePath(config["basedir"]),
"default", "default",
introducer, introducer,
) )

View File

@ -8,6 +8,9 @@ if PY2:
from future.builtins import str # noqa: F401 from future.builtins import str # noqa: F401
from six.moves import cStringIO as StringIO from six.moves import cStringIO as StringIO
from twisted.python.filepath import (
FilePath,
)
from twisted.internet import defer, reactor, protocol, error from twisted.internet import defer, reactor, protocol, error
from twisted.application import service, internet from twisted.application import service, internet
from twisted.web import client as tw_client from twisted.web import client as tw_client
@ -184,15 +187,17 @@ class SystemFramework(pollmixin.PollMixin):
self.introducer_furl = self.introducer.introducer_url self.introducer_furl = self.introducer.introducer_url
def make_nodes(self): def make_nodes(self):
root = FilePath(self.testdir)
self.nodes = [] self.nodes = []
for i in range(self.numnodes): for i in range(self.numnodes):
nodedir = os.path.join(self.testdir, "node%d" % i) nodedir = root.child("node%d" % (i,))
os.makedirs(nodedir + b"/private") private = nodedir.child("private")
private.makedirs()
write_introducer(nodedir, "default", self.introducer_url) write_introducer(nodedir, "default", self.introducer_url)
f = open(os.path.join(nodedir, "tahoe.cfg"), "w") config = (
f.write("[client]\n" "[client]\n"
"shares.happy = 1\n" "shares.happy = 1\n"
"[storage]\n" "[storage]\n"
) )
# the only tests for which we want the internal nodes to actually # the only tests for which we want the internal nodes to actually
# retain shares are the ones where somebody's going to download # retain shares are the ones where somebody's going to download
@ -204,13 +209,13 @@ class SystemFramework(pollmixin.PollMixin):
# for these tests, we tell the storage servers to pretend to # for these tests, we tell the storage servers to pretend to
# accept shares, but really just throw them out, since we're # accept shares, but really just throw them out, since we're
# only testing upload and not download. # only testing upload and not download.
f.write("debug_discard = true\n") config += "debug_discard = true\n"
if self.mode in ("receive",): if self.mode in ("receive",):
# for this mode, the client-under-test gets all the shares, # for this mode, the client-under-test gets all the shares,
# so our internal nodes can refuse requests # so our internal nodes can refuse requests
f.write("readonly = true\n") config += "readonly = true\n"
f.close() nodedir.child("tahoe.cfg").setContent(config)
c = client.Client(basedir=nodedir) c = client.Client(basedir=nodedir.path)
c.setServiceParent(self) c.setServiceParent(self)
self.nodes.append(c) self.nodes.append(c)
# the peers will start running, eventually they will connect to each # the peers will start running, eventually they will connect to each

View File

@ -251,7 +251,7 @@ class UseNode(object):
) )
write_introducer( write_introducer(
self.basedir.asBytesMode().path, self.basedir,
"default", "default",
self.introducer_furl, self.introducer_furl,
) )

View File

@ -667,11 +667,11 @@ class AnonymousStorage(SyncTestCase):
""" """
If anonymous storage access is enabled then the client announces it. If anonymous storage access is enabled then the client announces it.
""" """
basedir = self.id() basedir = FilePath(self.id())
os.makedirs(basedir + b"/private") basedir.child("private").makedirs()
write_introducer(basedir, "someintroducer", SOME_FURL) write_introducer(basedir, "someintroducer", SOME_FURL)
config = client.config_from_string( config = client.config_from_string(
basedir, basedir.path,
"tub.port", "tub.port",
BASECONFIG + ( BASECONFIG + (
"[storage]\n" "[storage]\n"
@ -687,7 +687,7 @@ class AnonymousStorage(SyncTestCase):
get_published_announcements(node), get_published_announcements(node),
MatchesListwise([ MatchesListwise([
matches_storage_announcement( matches_storage_announcement(
basedir, basedir.path,
anonymous=True, anonymous=True,
), ),
]), ]),
@ -699,11 +699,11 @@ class AnonymousStorage(SyncTestCase):
If anonymous storage access is disabled then the client does not announce If anonymous storage access is disabled then the client does not announce
it nor does it write a fURL for it to beneath the node directory. it nor does it write a fURL for it to beneath the node directory.
""" """
basedir = self.id() basedir = FilePath(self.id())
os.makedirs(basedir + b"/private") basedir.child("private").makedirs()
write_introducer(basedir, "someintroducer", SOME_FURL) write_introducer(basedir, "someintroducer", SOME_FURL)
config = client.config_from_string( config = client.config_from_string(
basedir, basedir.path,
"tub.port", "tub.port",
BASECONFIG + ( BASECONFIG + (
"[storage]\n" "[storage]\n"
@ -719,7 +719,7 @@ class AnonymousStorage(SyncTestCase):
get_published_announcements(node), get_published_announcements(node),
MatchesListwise([ MatchesListwise([
matches_storage_announcement( matches_storage_announcement(
basedir, basedir.path,
anonymous=False, anonymous=False,
), ),
]), ]),
@ -737,10 +737,10 @@ class AnonymousStorage(SyncTestCase):
possible to reach the anonymous storage server via the originally possible to reach the anonymous storage server via the originally
published fURL. published fURL.
""" """
basedir = self.id() basedir = FilePath(self.id())
os.makedirs(basedir + b"/private") basedir.child("private").makedirs()
enabled_config = client.config_from_string( enabled_config = client.config_from_string(
basedir, basedir.path,
"tub.port", "tub.port",
BASECONFIG + ( BASECONFIG + (
"[storage]\n" "[storage]\n"
@ -764,7 +764,7 @@ class AnonymousStorage(SyncTestCase):
) )
disabled_config = client.config_from_string( disabled_config = client.config_from_string(
basedir, basedir.path,
"tub.port", "tub.port",
BASECONFIG + ( BASECONFIG + (
"[storage]\n" "[storage]\n"
@ -956,22 +956,24 @@ class Run(unittest.TestCase, testutil.StallMixin):
A configuration consisting only of an introducer can be turned into a A configuration consisting only of an introducer can be turned into a
client node. client node.
""" """
basedir = "test_client.Run.test_loadable" basedir = FilePath("test_client.Run.test_loadable")
os.makedirs(basedir + b"/private") private = basedir.child("private")
private.makedirs()
dummy = "pb://wl74cyahejagspqgy4x5ukrvfnevlknt@127.0.0.1:58889/bogus" dummy = "pb://wl74cyahejagspqgy4x5ukrvfnevlknt@127.0.0.1:58889/bogus"
write_introducer(basedir, "someintroducer", dummy) write_introducer(basedir, "someintroducer", dummy)
fileutil.write(os.path.join(basedir, "tahoe.cfg"), BASECONFIG) basedir.child("tahoe.cfg").setContent(BASECONFIG)
fileutil.write(os.path.join(basedir, client._Client.EXIT_TRIGGER_FILE), "") basedir.child(client._Client.EXIT_TRIGGER_FILE).touch()
yield client.create_client(basedir) yield client.create_client(basedir.path)
@defer.inlineCallbacks @defer.inlineCallbacks
def test_reloadable(self): def test_reloadable(self):
basedir = "test_client.Run.test_reloadable" basedir = FilePath("test_client.Run.test_reloadable")
os.makedirs(basedir + b"/private") private = basedir.child("private")
private.makedirs()
dummy = "pb://wl74cyahejagspqgy4x5ukrvfnevlknt@127.0.0.1:58889/bogus" dummy = "pb://wl74cyahejagspqgy4x5ukrvfnevlknt@127.0.0.1:58889/bogus"
write_introducer(basedir, "someintroducer", dummy) write_introducer(basedir, "someintroducer", dummy)
fileutil.write(os.path.join(basedir, "tahoe.cfg"), BASECONFIG) basedir.child("tahoe.cfg").setContent(BASECONFIG)
c1 = yield client.create_client(basedir) c1 = yield client.create_client(basedir.path)
c1.setServiceParent(self.sparent) c1.setServiceParent(self.sparent)
# delay to let the service start up completely. I'm not entirely sure # delay to let the service start up completely. I'm not entirely sure
@ -993,7 +995,7 @@ class Run(unittest.TestCase, testutil.StallMixin):
# also change _check_exit_trigger to use it instead of a raw # also change _check_exit_trigger to use it instead of a raw
# reactor.stop, also instrument the shutdown event in an # reactor.stop, also instrument the shutdown event in an
# attribute that we can check.) # attribute that we can check.)
c2 = yield client.create_client(basedir) c2 = yield client.create_client(basedir.path)
c2.setServiceParent(self.sparent) c2.setServiceParent(self.sparent)
yield c2.disownServiceParent() yield c2.disownServiceParent()
@ -1132,8 +1134,8 @@ class StorageAnnouncementTests(SyncTestCase):
""" """
def setUp(self): def setUp(self):
super(StorageAnnouncementTests, self).setUp() super(StorageAnnouncementTests, self).setUp()
self.basedir = self.useFixture(TempDir()).path self.basedir = FilePath(self.useFixture(TempDir()).path)
create_node_dir(self.basedir, u"") create_node_dir(self.basedir.path, u"")
# Write an introducer configuration or we can't observer # Write an introducer configuration or we can't observer
# announcements. # announcements.
write_introducer(self.basedir, "someintroducer", SOME_FURL) write_introducer(self.basedir, "someintroducer", SOME_FURL)
@ -1164,7 +1166,7 @@ enabled = {storage_enabled}
No storage announcement is published if storage is not enabled. No storage announcement is published if storage is not enabled.
""" """
config = client.config_from_string( config = client.config_from_string(
self.basedir, self.basedir.path,
"tub.port", "tub.port",
self.get_config(storage_enabled=False), self.get_config(storage_enabled=False),
) )
@ -1186,7 +1188,7 @@ enabled = {storage_enabled}
storage is enabled. storage is enabled.
""" """
config = client.config_from_string( config = client.config_from_string(
self.basedir, self.basedir.path,
"tub.port", "tub.port",
self.get_config(storage_enabled=True), self.get_config(storage_enabled=True),
) )
@ -1203,7 +1205,7 @@ enabled = {storage_enabled}
# Match the following list (of one element) ... # Match the following list (of one element) ...
MatchesListwise([ MatchesListwise([
# The only element in the list ... # The only element in the list ...
matches_storage_announcement(self.basedir), matches_storage_announcement(self.basedir.path),
]), ]),
)), )),
) )
@ -1218,7 +1220,7 @@ enabled = {storage_enabled}
value = u"thing" value = u"thing"
config = client.config_from_string( config = client.config_from_string(
self.basedir, self.basedir.path,
"tub.port", "tub.port",
self.get_config( self.get_config(
storage_enabled=True, storage_enabled=True,
@ -1238,7 +1240,7 @@ enabled = {storage_enabled}
get_published_announcements, get_published_announcements,
MatchesListwise([ MatchesListwise([
matches_storage_announcement( matches_storage_announcement(
self.basedir, self.basedir.path,
options=[ options=[
matches_dummy_announcement( matches_dummy_announcement(
u"tahoe-lafs-dummy-v1", u"tahoe-lafs-dummy-v1",
@ -1259,7 +1261,7 @@ enabled = {storage_enabled}
self.useFixture(UseTestPlugins()) self.useFixture(UseTestPlugins())
config = client.config_from_string( config = client.config_from_string(
self.basedir, self.basedir.path,
"tub.port", "tub.port",
self.get_config( self.get_config(
storage_enabled=True, storage_enabled=True,
@ -1281,7 +1283,7 @@ enabled = {storage_enabled}
get_published_announcements, get_published_announcements,
MatchesListwise([ MatchesListwise([
matches_storage_announcement( matches_storage_announcement(
self.basedir, self.basedir.path,
options=[ options=[
matches_dummy_announcement( matches_dummy_announcement(
u"tahoe-lafs-dummy-v1", u"tahoe-lafs-dummy-v1",
@ -1307,7 +1309,7 @@ enabled = {storage_enabled}
self.useFixture(UseTestPlugins()) self.useFixture(UseTestPlugins())
config = client.config_from_string( config = client.config_from_string(
self.basedir, self.basedir.path,
"tub.port", "tub.port",
self.get_config( self.get_config(
storage_enabled=True, storage_enabled=True,
@ -1343,7 +1345,7 @@ enabled = {storage_enabled}
self.useFixture(UseTestPlugins()) self.useFixture(UseTestPlugins())
config = client.config_from_string( config = client.config_from_string(
self.basedir, self.basedir.path,
"tub.port", "tub.port",
self.get_config( self.get_config(
storage_enabled=True, storage_enabled=True,
@ -1359,7 +1361,7 @@ enabled = {storage_enabled}
get_published_announcements, get_published_announcements,
MatchesListwise([ MatchesListwise([
matches_storage_announcement( matches_storage_announcement(
self.basedir, self.basedir.path,
options=[ options=[
matches_dummy_announcement( matches_dummy_announcement(
u"tahoe-lafs-dummy-v1", u"tahoe-lafs-dummy-v1",
@ -1381,7 +1383,7 @@ enabled = {storage_enabled}
self.useFixture(UseTestPlugins()) self.useFixture(UseTestPlugins())
config = client.config_from_string( config = client.config_from_string(
self.basedir, self.basedir.path,
"tub.port", "tub.port",
self.get_config( self.get_config(
storage_enabled=True, storage_enabled=True,
@ -1408,7 +1410,7 @@ enabled = {storage_enabled}
available on the system. available on the system.
""" """
config = client.config_from_string( config = client.config_from_string(
self.basedir, self.basedir.path,
"tub.port", "tub.port",
self.get_config( self.get_config(
storage_enabled=True, storage_enabled=True,

View File

@ -795,21 +795,24 @@ class Announcements(AsyncTestCase):
Announcements received by an introducer client are written to that Announcements received by an introducer client are written to that
introducer client's cache file. introducer client's cache file.
""" """
basedir = "introducer/ClientSeqnums/test_client_cache_1" basedir = FilePath("introducer/ClientSeqnums/test_client_cache_1")
fileutil.make_dirs(basedir + b"/private") private = basedir.child("private")
private.makedirs()
write_introducer(basedir, "default", "nope") write_introducer(basedir, "default", "nope")
cache_filepath = FilePath(os.path.join(basedir, "private", cache_filepath = basedir.descendant([
"introducer_default_cache.yaml")) "private",
"introducer_default_cache.yaml",
])
# if storage is enabled, the Client will publish its storage server # if storage is enabled, the Client will publish its storage server
# during startup (although the announcement will wait in a queue # during startup (although the announcement will wait in a queue
# until the introducer connection is established). To avoid getting # until the introducer connection is established). To avoid getting
# confused by this, disable storage. # confused by this, disable storage.
with open(os.path.join(basedir, "tahoe.cfg"), "w") as f: with basedir.child("tahoe.cfg").open("w") as f:
f.write("[storage]\n") f.write("[storage]\n")
f.write("enabled = false\n") f.write("enabled = false\n")
c = yield create_client(basedir) c = yield create_client(basedir.path)
ic = c.introducer_clients[0] ic = c.introducer_clients[0]
private_key, public_key = ed25519.create_signing_keypair() private_key, public_key = ed25519.create_signing_keypair()
public_key_str = remove_prefix(ed25519.string_from_verifying_key(public_key), "pub-") public_key_str = remove_prefix(ed25519.string_from_verifying_key(public_key), "pub-")
@ -875,7 +878,7 @@ class Announcements(AsyncTestCase):
self.failUnlessEqual(announcements[public_key_str2]["anonymous-storage-FURL"], self.failUnlessEqual(announcements[public_key_str2]["anonymous-storage-FURL"],
furl3) furl3)
c2 = yield create_client(basedir) c2 = yield create_client(basedir.path)
c2.introducer_clients[0]._load_announcements() c2.introducer_clients[0]._load_announcements()
yield flushEventualQueue() yield flushEventualQueue()
self.assertEqual(c2.storage_broker.get_all_serverids(), self.assertEqual(c2.storage_broker.get_all_serverids(),
@ -885,26 +888,24 @@ class ClientSeqnums(AsyncBrokenTestCase):
@defer.inlineCallbacks @defer.inlineCallbacks
def test_client(self): def test_client(self):
basedir = "introducer/ClientSeqnums/test_client" basedir = FilePath("introducer/ClientSeqnums/test_client")
fileutil.make_dirs(basedir + b"/private") private = basedir.child("private")
private.makedirs()
write_introducer(basedir, "default", "nope") write_introducer(basedir, "default", "nope")
# if storage is enabled, the Client will publish its storage server # if storage is enabled, the Client will publish its storage server
# during startup (although the announcement will wait in a queue # during startup (although the announcement will wait in a queue
# until the introducer connection is established). To avoid getting # until the introducer connection is established). To avoid getting
# confused by this, disable storage. # confused by this, disable storage.
f = open(os.path.join(basedir, "tahoe.cfg"), "w") with basedir.child("tahoe.cfg").open("w") as f:
f.write("[storage]\n") f.write("[storage]\n")
f.write("enabled = false\n") f.write("enabled = false\n")
f.close()
c = yield create_client(basedir) c = yield create_client(basedir.path)
ic = c.introducer_clients[0] ic = c.introducer_clients[0]
outbound = ic._outbound_announcements outbound = ic._outbound_announcements
published = ic._published_announcements published = ic._published_announcements
def read_seqnum(): def read_seqnum():
f = open(os.path.join(basedir, "announcement-seqnum")) seqnum = basedir.child("announcement-seqnum").getContent()
seqnum = f.read().strip()
f.close()
return int(seqnum) return int(seqnum)
ic.publish("sA", {"key": "value1"}, c._node_private_key) ic.publish("sA", {"key": "value1"}, c._node_private_key)

View File

@ -33,6 +33,9 @@ from allmydata.mutable.publish import MutableData
from foolscap.api import DeadReferenceError, fireEventually, flushEventualQueue from foolscap.api import DeadReferenceError, fireEventually, flushEventualQueue
from twisted.python.failure import Failure from twisted.python.failure import Failure
from twisted.python.filepath import (
FilePath,
)
from .common import ( from .common import (
TEST_RSA_KEY_SIZE, TEST_RSA_KEY_SIZE,
@ -903,21 +906,21 @@ class SystemTestMixin(pollmixin.PollMixin, testutil.StallMixin):
# usually this node is *not* parented to our self.sparent, so we can # usually this node is *not* parented to our self.sparent, so we can
# shut it down separately from the rest, to exercise the # shut it down separately from the rest, to exercise the
# connection-lost code # connection-lost code
basedir = self.getdir("client%d" % client_num) basedir = FilePath(self.getdir("client%d" % client_num))
if not os.path.isdir(basedir): basedir.makedirs()
fileutil.make_dirs(basedir)
config = "[client]\n" config = "[client]\n"
if helper_furl: if helper_furl:
config += "helper.furl = %s\n" % helper_furl config += "helper.furl = %s\n" % helper_furl
fileutil.write(os.path.join(basedir, 'tahoe.cfg'), config) basedir.child("tahoe.cfg").setContent(config)
os.makedirs(basedir + b"/private") private = basedir.child("private")
private.makedirs()
write_introducer( write_introducer(
basedir, basedir,
"default", "default",
self.introducer_furl, self.introducer_furl,
) )
c = yield client.create_client(basedir) c = yield client.create_client(basedir.path)
self.clients.append(c) self.clients.append(c)
c.set_default_mutable_keysize(TEST_RSA_KEY_SIZE) c.set_default_mutable_keysize(TEST_RSA_KEY_SIZE)
self.numclients += 1 self.numclients += 1