windows yaml.safe_load returns None on unreadable files..

This commit is contained in:
meejah 2018-09-11 11:14:41 -06:00
parent 7d34f8f1a1
commit 1ad2174df9
2 changed files with 17 additions and 8 deletions

View File

@ -1,7 +1,7 @@
import os, stat, time, weakref import os, stat, time, weakref
from base64 import urlsafe_b64encode from base64 import urlsafe_b64encode
from functools import partial from functools import partial
from errno import ENOENT from errno import ENOENT, EPERM
from zope.interface import implementer from zope.interface import implementer
from twisted.internet import reactor, defer from twisted.internet import reactor, defer
@ -297,7 +297,11 @@ def create_introducer_clients(config, main_tub):
with introducers_filepath.open() as f: with introducers_filepath.open() as f:
introducers_yaml = yamlutil.safe_load(f) introducers_yaml = yamlutil.safe_load(f)
if introducers_yaml is None: if introducers_yaml is None:
introducers_yaml = {} raise EnvironmentError(
EPERM,
"Can't read '{}'".format(introducers_yaml_filename),
introducers_yaml_filename,
)
introducers = introducers_yaml.get("introducers", {}) introducers = introducers_yaml.get("introducers", {})
log.msg( log.msg(
"found {} introducers in private/introducers.yaml".format( "found {} introducers in private/introducers.yaml".format(

View File

@ -59,13 +59,18 @@ class Node(testutil.SignalMixin, testutil.ReallyEqualMixin, unittest.TestCase):
def test_introducer_clients_unloadable(self): def test_introducer_clients_unloadable(self):
""" """
If introducers.yaml exists but we can't read it for some reason Error if introducers.yaml exists but we can't read it
we'll ignore it.
""" """
basedir = "introducer.IntroducerNode.test_introducer_clients_unloadable" basedir = u"introducer.IntroducerNode.test_introducer_clients_unloadable"
os.mkdir(basedir) os.mkdir(basedir)
os.mkdir(os.path.join(basedir, u"private"))
yaml_fname = os.path.join(basedir, u"private", u"introducers.yaml")
with open(yaml_fname, 'w') as f:
f.write(u'---\n')
os.chmod(yaml_fname, 0o000)
self.addCleanup(lambda: os.chmod(yaml_fname, 0o700))
# just mocking the yaml failure, as "yamlutil.safe_load" only # just mocking the yaml failure, as "yamlutil.safe_load" only
# returns None on some platforms # returns None on some platforms for unreadable files
with patch("allmydata.client.yamlutil") as p: with patch("allmydata.client.yamlutil") as p:
p.safe_load = Mock(return_value=None) p.safe_load = Mock(return_value=None)
@ -73,8 +78,8 @@ class Node(testutil.SignalMixin, testutil.ReallyEqualMixin, unittest.TestCase):
fake_tub = Mock() fake_tub = Mock()
config = read_config(basedir, "portnum") config = read_config(basedir, "portnum")
ic = create_introducer_clients(config, fake_tub) with self.assertRaises(EnvironmentError) as ctx:
self.assertEqual([], ic) create_introducer_clients(config, fake_tub)
@defer.inlineCallbacks @defer.inlineCallbacks
def test_furl(self): def test_furl(self):