Skip to content

Commit

Permalink
using context managment in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tpltnt committed Mar 2, 2018
1 parent fa51395 commit af1ca2d
Showing 1 changed file with 43 additions and 41 deletions.
84 changes: 43 additions & 41 deletions src/allmydata/test/test_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,12 @@ def _test_location(self, basedir, expected_addresses, tub_port=None, tub_locatio
self.patch(iputil, 'get_local_addresses_sync',
lambda: local_addresses)

n = TestNode(basedir)
n.setServiceParent(self.parent)
furl = n.tub.registerReference(n)
for address in expected_addresses:
self.failUnlessIn(address, furl)
with TestNode(basedir) as n:
n.setServiceParent(self.parent)
furl = n.tub.registerReference(n)
for address in expected_addresses:
self.failUnlessIn(address, furl)


def test_location1(self):
return self._test_location(basedir="test_node/test_location1",
Expand Down Expand Up @@ -115,10 +116,10 @@ def test_tahoe_cfg_utf8(self):
f.write(u"nickname = \u2621\n".encode('utf-8'))
f.close()

n = TestNode(basedir)
n.setServiceParent(self.parent)
self.failUnlessEqual(n.get_config("node", "nickname").decode('utf-8'),
u"\u2621")
with TestNode(basedir) as n:
n.setServiceParent(self.parent)
self.failUnlessEqual(n.get_config("node", "nickname").decode('utf-8'),
u"\u2621")

def test_tahoe_cfg_hash_in_name(self):
basedir = "test_node/test_cfg_hash_in_name"
Expand All @@ -128,8 +129,8 @@ def test_tahoe_cfg_hash_in_name(self):
f.write("[node]\n")
f.write("nickname = %s\n" % (nickname,))
f.close()
n = TestNode(basedir)
self.failUnless(n.nickname == nickname)
with TestNode(basedir) as n:
self.failUnless(n.nickname == nickname)

def test_private_config(self):
basedir = "test_node/test_private_config"
Expand All @@ -139,25 +140,25 @@ def test_private_config(self):
f.write("secret")
f.close()

n = TestNode(basedir)
self.failUnlessEqual(n.get_private_config("already"), "secret")
self.failUnlessEqual(n.get_private_config("not", "default"), "default")
self.failUnlessRaises(MissingConfigEntry, n.get_private_config, "not")
value = n.get_or_create_private_config("new", "start")
self.failUnlessEqual(value, "start")
self.failUnlessEqual(n.get_private_config("new"), "start")
counter = []
def make_newer():
counter.append("called")
return "newer"
value = n.get_or_create_private_config("newer", make_newer)
self.failUnlessEqual(len(counter), 1)
self.failUnlessEqual(value, "newer")
self.failUnlessEqual(n.get_private_config("newer"), "newer")

value = n.get_or_create_private_config("newer", make_newer)
self.failUnlessEqual(len(counter), 1) # don't call unless necessary
self.failUnlessEqual(value, "newer")
with TestNode(basedir) as n:
self.failUnlessEqual(n.get_private_config("already"), "secret")
self.failUnlessEqual(n.get_private_config("not", "default"), "default")
self.failUnlessRaises(MissingConfigEntry, n.get_private_config, "not")
value = n.get_or_create_private_config("new", "start")
self.failUnlessEqual(value, "start")
self.failUnlessEqual(n.get_private_config("new"), "start")
counter = []
def make_newer():
counter.append("called")
return "newer"
value = n.get_or_create_private_config("newer", make_newer)
self.failUnlessEqual(len(counter), 1)
self.failUnlessEqual(value, "newer")
self.failUnlessEqual(n.get_private_config("newer"), "newer")

value = n.get_or_create_private_config("newer", make_newer)
self.failUnlessEqual(len(counter), 1) # don't call unless necessary
self.failUnlessEqual(value, "newer")

def test_timestamp(self):
# this modified logger doesn't seem to get used during the tests,
Expand All @@ -172,9 +173,9 @@ def test_timestamp(self):
def test_secrets_dir(self):
basedir = "test_node/test_secrets_dir"
fileutil.make_dirs(basedir)
n = TestNode(basedir)
self.failUnless(isinstance(n, TestNode))
self.failUnless(os.path.exists(os.path.join(basedir, "private")))
with TestNode(basedir) as n:
self.failUnless(isinstance(n, TestNode))
self.failUnless(os.path.exists(os.path.join(basedir, "private")))

def test_secrets_dir_protected(self):
if "win32" in sys.platform.lower() or "cygwin" in sys.platform.lower():
Expand All @@ -184,12 +185,12 @@ def test_secrets_dir_protected(self):
raise unittest.SkipTest("We don't know how to set permissions on Windows.")
basedir = "test_node/test_secrets_dir_protected"
fileutil.make_dirs(basedir)
n = TestNode(basedir)
self.failUnless(isinstance(n, TestNode))
privdir = os.path.join(basedir, "private")
st = os.stat(privdir)
bits = stat.S_IMODE(st[stat.ST_MODE])
self.failUnless(bits & 0001 == 0, bits)
with TestNode(basedir) as n:
self.failUnless(isinstance(n, TestNode))
privdir = os.path.join(basedir, "private")
st = os.stat(privdir)
bits = stat.S_IMODE(st[stat.ST_MODE])
self.failUnless(bits & 0001 == 0, bits)

def test_logdir_is_str(self):
basedir = "test_node/test_logdir_is_str"
Expand All @@ -202,8 +203,9 @@ def call_setLogDir(logdir):
self.failUnless(isinstance(logdir, str), logdir)
self.patch(foolscap.logging.log, 'setLogDir', call_setLogDir)

TestNode(basedir)
self.failUnless(ns.called)
with TestNode(basedir) as n:
self.failUnless(ns.called)


class EmptyNode(Node):
def __init__(self):
Expand Down

0 comments on commit af1ca2d

Please sign in to comment.