More passing tests.

This commit is contained in:
Itamar Turner-Trauring 2020-08-28 10:53:52 -04:00
parent 1e5f7a9379
commit 5ad5b79cdd
3 changed files with 187 additions and 185 deletions

View File

@ -85,7 +85,7 @@ class ShareFile(object):
seekpos = self._data_offset+offset seekpos = self._data_offset+offset
actuallength = max(0, min(length, self._lease_offset-seekpos)) actuallength = max(0, min(length, self._lease_offset-seekpos))
if actuallength == 0: if actuallength == 0:
return "" return b""
with open(self.home, 'rb') as f: with open(self.home, 'rb') as f:
f.seek(seekpos) f.seek(seekpos)
return f.read(actuallength) return f.read(actuallength)

View File

@ -113,7 +113,7 @@ class MutableShareFile(object):
# start beyond the end of the data return an empty string. # start beyond the end of the data return an empty string.
length = max(0, data_length-offset) length = max(0, data_length-offset)
if length == 0: if length == 0:
return "" return b""
precondition(offset+length <= data_length) precondition(offset+length <= data_length)
f.seek(self.DATA_OFFSET+offset) f.seek(self.DATA_OFFSET+offset)
data = f.read(length) data = f.read(length)
@ -421,18 +421,18 @@ class MutableShareFile(object):
# self._change_container_size() here. # self._change_container_size() here.
def testv_compare(a, op, b): def testv_compare(a, op, b):
assert op in ("lt", "le", "eq", "ne", "ge", "gt") assert op in (b"lt", b"le", b"eq", b"ne", b"ge", b"gt")
if op == "lt": if op == b"lt":
return a < b return a < b
if op == "le": if op == b"le":
return a <= b return a <= b
if op == "eq": if op == b"eq":
return a == b return a == b
if op == "ne": if op == b"ne":
return a != b return a != b
if op == "ge": if op == b"ge":
return a >= b return a >= b
if op == "gt": if op == b"gt":
return a > b return a > b
# never reached # never reached
@ -441,7 +441,7 @@ class EmptyShare(object):
def check_testv(self, testv): def check_testv(self, testv):
test_good = True test_good = True
for (offset, length, operator, specimen) in testv: for (offset, length, operator, specimen) in testv:
data = "" data = b""
if not testv_compare(data, operator, specimen): if not testv_compare(data, operator, specimen):
test_good = False test_good = False
break break

View File

@ -1,4 +1,4 @@
from future.utils import native_str from future.utils import native_str, PY3
import time import time
import os.path import os.path
@ -937,72 +937,72 @@ class MutableServer(unittest.TestCase):
def test_allocate(self): def test_allocate(self):
ss = self.create("test_allocate") ss = self.create("test_allocate")
self.allocate(ss, "si1", "we1", next(self._lease_secret), self.allocate(ss, b"si1", b"we1", next(self._lease_secret),
set([0,1,2]), 100) set([0,1,2]), 100)
read = ss.remote_slot_readv read = ss.remote_slot_readv
self.failUnlessEqual(read("si1", [0], [(0, 10)]), self.failUnlessEqual(read(b"si1", [0], [(0, 10)]),
{0: [""]}) {0: [b""]})
self.failUnlessEqual(read("si1", [], [(0, 10)]), self.failUnlessEqual(read(b"si1", [], [(0, 10)]),
{0: [""], 1: [""], 2: [""]}) {0: [b""], 1: [b""], 2: [b""]})
self.failUnlessEqual(read("si1", [0], [(100, 10)]), self.failUnlessEqual(read(b"si1", [0], [(100, 10)]),
{0: [""]}) {0: [b""]})
# try writing to one # try writing to one
secrets = ( self.write_enabler("we1"), secrets = ( self.write_enabler(b"we1"),
self.renew_secret("we1"), self.renew_secret(b"we1"),
self.cancel_secret("we1") ) self.cancel_secret(b"we1") )
data = "".join([ ("%d" % i) * 10 for i in range(10) ]) data = b"".join([ (b"%d" % i) * 10 for i in range(10) ])
write = ss.remote_slot_testv_and_readv_and_writev write = ss.remote_slot_testv_and_readv_and_writev
answer = write("si1", secrets, answer = write(b"si1", secrets,
{0: ([], [(0,data)], None)}, {0: ([], [(0,data)], None)},
[]) [])
self.failUnlessEqual(answer, (True, {0:[],1:[],2:[]}) ) self.failUnlessEqual(answer, (True, {0:[],1:[],2:[]}) )
self.failUnlessEqual(read("si1", [0], [(0,20)]), self.failUnlessEqual(read(b"si1", [0], [(0,20)]),
{0: ["00000000001111111111"]}) {0: [b"00000000001111111111"]})
self.failUnlessEqual(read("si1", [0], [(95,10)]), self.failUnlessEqual(read(b"si1", [0], [(95,10)]),
{0: ["99999"]}) {0: [b"99999"]})
#self.failUnlessEqual(s0.remote_get_length(), 100) #self.failUnlessEqual(s0.remote_get_length(), 100)
bad_secrets = ("bad write enabler", secrets[1], secrets[2]) bad_secrets = (b"bad write enabler", secrets[1], secrets[2])
f = self.failUnlessRaises(BadWriteEnablerError, f = self.failUnlessRaises(BadWriteEnablerError,
write, "si1", bad_secrets, write, b"si1", bad_secrets,
{}, []) {}, [])
self.failUnlessIn("The write enabler was recorded by nodeid 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'.", f) self.failUnlessIn("The write enabler was recorded by nodeid 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'.", str(f))
# this testv should fail # this testv should fail
answer = write("si1", secrets, answer = write(b"si1", secrets,
{0: ([(0, 12, "eq", "444444444444"), {0: ([(0, 12, b"eq", b"444444444444"),
(20, 5, "eq", "22222"), (20, 5, b"eq", b"22222"),
], ],
[(0, "x"*100)], [(0, b"x"*100)],
None), None),
}, },
[(0,12), (20,5)], [(0,12), (20,5)],
) )
self.failUnlessEqual(answer, (False, self.failUnlessEqual(answer, (False,
{0: ["000000000011", "22222"], {0: [b"000000000011", b"22222"],
1: ["", ""], 1: [b"", b""],
2: ["", ""], 2: [b"", b""],
})) }))
self.failUnlessEqual(read("si1", [0], [(0,100)]), {0: [data]}) self.failUnlessEqual(read(b"si1", [0], [(0,100)]), {0: [data]})
# as should this one # as should this one
answer = write("si1", secrets, answer = write(b"si1", secrets,
{0: ([(10, 5, "lt", "11111"), {0: ([(10, 5, b"lt", b"11111"),
], ],
[(0, "x"*100)], [(0, b"x"*100)],
None), None),
}, },
[(10,5)], [(10,5)],
) )
self.failUnlessEqual(answer, (False, self.failUnlessEqual(answer, (False,
{0: ["11111"], {0: [b"11111"],
1: [""], 1: [b""],
2: [""]}, 2: [b""]},
)) ))
self.failUnlessEqual(read("si1", [0], [(0,100)]), {0: [data]}) self.failUnlessEqual(read(b"si1", [0], [(0,100)]), {0: [data]})
def test_operators(self): def test_operators(self):
@ -1010,201 +1010,201 @@ class MutableServer(unittest.TestCase):
# test both fail+pass, reset data after each one. # test both fail+pass, reset data after each one.
ss = self.create("test_operators") ss = self.create("test_operators")
secrets = ( self.write_enabler("we1"), secrets = ( self.write_enabler(b"we1"),
self.renew_secret("we1"), self.renew_secret(b"we1"),
self.cancel_secret("we1") ) self.cancel_secret(b"we1") )
data = "".join([ ("%d" % i) * 10 for i in range(10) ]) data = b"".join([ (b"%d" % i) * 10 for i in range(10) ])
write = ss.remote_slot_testv_and_readv_and_writev write = ss.remote_slot_testv_and_readv_and_writev
read = ss.remote_slot_readv read = ss.remote_slot_readv
def reset(): def reset():
write("si1", secrets, write(b"si1", secrets,
{0: ([], [(0,data)], None)}, {0: ([], [(0,data)], None)},
[]) [])
reset() reset()
# lt # lt
answer = write("si1", secrets, {0: ([(10, 5, "lt", "11110"), answer = write(b"si1", secrets, {0: ([(10, 5, b"lt", b"11110"),
], ],
[(0, "x"*100)], [(0, b"x"*100)],
None, None,
)}, [(10,5)]) )}, [(10,5)])
self.failUnlessEqual(answer, (False, {0: ["11111"]})) self.failUnlessEqual(answer, (False, {0: [b"11111"]}))
self.failUnlessEqual(read("si1", [0], [(0,100)]), {0: [data]}) self.failUnlessEqual(read(b"si1", [0], [(0,100)]), {0: [data]})
self.failUnlessEqual(read("si1", [], [(0,100)]), {0: [data]}) self.failUnlessEqual(read(b"si1", [], [(0,100)]), {0: [data]})
reset() reset()
answer = write("si1", secrets, {0: ([(10, 5, "lt", "11111"), answer = write(b"si1", secrets, {0: ([(10, 5, b"lt", b"11111"),
], ],
[(0, "x"*100)], [(0, b"x"*100)],
None, None,
)}, [(10,5)]) )}, [(10,5)])
self.failUnlessEqual(answer, (False, {0: ["11111"]})) self.failUnlessEqual(answer, (False, {0: [b"11111"]}))
self.failUnlessEqual(read("si1", [0], [(0,100)]), {0: [data]}) self.failUnlessEqual(read(b"si1", [0], [(0,100)]), {0: [data]})
reset() reset()
answer = write("si1", secrets, {0: ([(10, 5, "lt", "11112"), answer = write(b"si1", secrets, {0: ([(10, 5, b"lt", b"11112"),
], ],
[(0, "y"*100)], [(0, b"y"*100)],
None, None,
)}, [(10,5)]) )}, [(10,5)])
self.failUnlessEqual(answer, (True, {0: ["11111"]})) self.failUnlessEqual(answer, (True, {0: [b"11111"]}))
self.failUnlessEqual(read("si1", [0], [(0,100)]), {0: ["y"*100]}) self.failUnlessEqual(read(b"si1", [0], [(0,100)]), {0: [b"y"*100]})
reset() reset()
# le # le
answer = write("si1", secrets, {0: ([(10, 5, "le", "11110"), answer = write(b"si1", secrets, {0: ([(10, 5, b"le", b"11110"),
], ],
[(0, "x"*100)], [(0, b"x"*100)],
None, None,
)}, [(10,5)]) )}, [(10,5)])
self.failUnlessEqual(answer, (False, {0: ["11111"]})) self.failUnlessEqual(answer, (False, {0: [b"11111"]}))
self.failUnlessEqual(read("si1", [0], [(0,100)]), {0: [data]}) self.failUnlessEqual(read(b"si1", [0], [(0,100)]), {0: [data]})
reset() reset()
answer = write("si1", secrets, {0: ([(10, 5, "le", "11111"), answer = write(b"si1", secrets, {0: ([(10, 5, b"le", b"11111"),
], ],
[(0, "y"*100)], [(0, b"y"*100)],
None, None,
)}, [(10,5)]) )}, [(10,5)])
self.failUnlessEqual(answer, (True, {0: ["11111"]})) self.failUnlessEqual(answer, (True, {0: [b"11111"]}))
self.failUnlessEqual(read("si1", [0], [(0,100)]), {0: ["y"*100]}) self.failUnlessEqual(read(b"si1", [0], [(0,100)]), {0: [b"y"*100]})
reset() reset()
answer = write("si1", secrets, {0: ([(10, 5, "le", "11112"), answer = write(b"si1", secrets, {0: ([(10, 5, b"le", b"11112"),
], ],
[(0, "y"*100)], [(0, b"y"*100)],
None, None,
)}, [(10,5)]) )}, [(10,5)])
self.failUnlessEqual(answer, (True, {0: ["11111"]})) self.failUnlessEqual(answer, (True, {0: [b"11111"]}))
self.failUnlessEqual(read("si1", [0], [(0,100)]), {0: ["y"*100]}) self.failUnlessEqual(read(b"si1", [0], [(0,100)]), {0: [b"y"*100]})
reset() reset()
# eq # eq
answer = write("si1", secrets, {0: ([(10, 5, "eq", "11112"), answer = write(b"si1", secrets, {0: ([(10, 5, b"eq", b"11112"),
], ],
[(0, "x"*100)], [(0, b"x"*100)],
None, None,
)}, [(10,5)]) )}, [(10,5)])
self.failUnlessEqual(answer, (False, {0: ["11111"]})) self.failUnlessEqual(answer, (False, {0: [b"11111"]}))
self.failUnlessEqual(read("si1", [0], [(0,100)]), {0: [data]}) self.failUnlessEqual(read(b"si1", [0], [(0,100)]), {0: [data]})
reset() reset()
answer = write("si1", secrets, {0: ([(10, 5, "eq", "11111"), answer = write(b"si1", secrets, {0: ([(10, 5, b"eq", b"11111"),
], ],
[(0, "y"*100)], [(0, b"y"*100)],
None, None,
)}, [(10,5)]) )}, [(10,5)])
self.failUnlessEqual(answer, (True, {0: ["11111"]})) self.failUnlessEqual(answer, (True, {0: [b"11111"]}))
self.failUnlessEqual(read("si1", [0], [(0,100)]), {0: ["y"*100]}) self.failUnlessEqual(read(b"si1", [0], [(0,100)]), {0: [b"y"*100]})
reset() reset()
# ne # ne
answer = write("si1", secrets, {0: ([(10, 5, "ne", "11111"), answer = write(b"si1", secrets, {0: ([(10, 5, b"ne", b"11111"),
], ],
[(0, "x"*100)], [(0, b"x"*100)],
None, None,
)}, [(10,5)]) )}, [(10,5)])
self.failUnlessEqual(answer, (False, {0: ["11111"]})) self.failUnlessEqual(answer, (False, {0: [b"11111"]}))
self.failUnlessEqual(read("si1", [0], [(0,100)]), {0: [data]}) self.failUnlessEqual(read(b"si1", [0], [(0,100)]), {0: [data]})
reset() reset()
answer = write("si1", secrets, {0: ([(10, 5, "ne", "11112"), answer = write(b"si1", secrets, {0: ([(10, 5, b"ne", b"11112"),
], ],
[(0, "y"*100)], [(0, b"y"*100)],
None, None,
)}, [(10,5)]) )}, [(10,5)])
self.failUnlessEqual(answer, (True, {0: ["11111"]})) self.failUnlessEqual(answer, (True, {0: [b"11111"]}))
self.failUnlessEqual(read("si1", [0], [(0,100)]), {0: ["y"*100]}) self.failUnlessEqual(read(b"si1", [0], [(0,100)]), {0: [b"y"*100]})
reset() reset()
# ge # ge
answer = write("si1", secrets, {0: ([(10, 5, "ge", "11110"), answer = write(b"si1", secrets, {0: ([(10, 5, b"ge", b"11110"),
], ],
[(0, "y"*100)], [(0, b"y"*100)],
None, None,
)}, [(10,5)]) )}, [(10,5)])
self.failUnlessEqual(answer, (True, {0: ["11111"]})) self.failUnlessEqual(answer, (True, {0: [b"11111"]}))
self.failUnlessEqual(read("si1", [0], [(0,100)]), {0: ["y"*100]}) self.failUnlessEqual(read(b"si1", [0], [(0,100)]), {0: [b"y"*100]})
reset() reset()
answer = write("si1", secrets, {0: ([(10, 5, "ge", "11111"), answer = write(b"si1", secrets, {0: ([(10, 5, b"ge", b"11111"),
], ],
[(0, "y"*100)], [(0, b"y"*100)],
None, None,
)}, [(10,5)]) )}, [(10,5)])
self.failUnlessEqual(answer, (True, {0: ["11111"]})) self.failUnlessEqual(answer, (True, {0: [b"11111"]}))
self.failUnlessEqual(read("si1", [0], [(0,100)]), {0: ["y"*100]}) self.failUnlessEqual(read(b"si1", [0], [(0,100)]), {0: [b"y"*100]})
reset() reset()
answer = write("si1", secrets, {0: ([(10, 5, "ge", "11112"), answer = write(b"si1", secrets, {0: ([(10, 5, b"ge", b"11112"),
], ],
[(0, "y"*100)], [(0, b"y"*100)],
None, None,
)}, [(10,5)]) )}, [(10,5)])
self.failUnlessEqual(answer, (False, {0: ["11111"]})) self.failUnlessEqual(answer, (False, {0: [b"11111"]}))
self.failUnlessEqual(read("si1", [0], [(0,100)]), {0: [data]}) self.failUnlessEqual(read(b"si1", [0], [(0,100)]), {0: [data]})
reset() reset()
# gt # gt
answer = write("si1", secrets, {0: ([(10, 5, "gt", "11110"), answer = write(b"si1", secrets, {0: ([(10, 5, b"gt", b"11110"),
], ],
[(0, "y"*100)], [(0, b"y"*100)],
None, None,
)}, [(10,5)]) )}, [(10,5)])
self.failUnlessEqual(answer, (True, {0: ["11111"]})) self.failUnlessEqual(answer, (True, {0: [b"11111"]}))
self.failUnlessEqual(read("si1", [0], [(0,100)]), {0: ["y"*100]}) self.failUnlessEqual(read(b"si1", [0], [(0,100)]), {0: [b"y"*100]})
reset() reset()
answer = write("si1", secrets, {0: ([(10, 5, "gt", "11111"), answer = write(b"si1", secrets, {0: ([(10, 5, b"gt", b"11111"),
], ],
[(0, "x"*100)], [(0, b"x"*100)],
None, None,
)}, [(10,5)]) )}, [(10,5)])
self.failUnlessEqual(answer, (False, {0: ["11111"]})) self.failUnlessEqual(answer, (False, {0: [b"11111"]}))
self.failUnlessEqual(read("si1", [0], [(0,100)]), {0: [data]}) self.failUnlessEqual(read(b"si1", [0], [(0,100)]), {0: [data]})
reset() reset()
answer = write("si1", secrets, {0: ([(10, 5, "gt", "11112"), answer = write(b"si1", secrets, {0: ([(10, 5, b"gt", b"11112"),
], ],
[(0, "x"*100)], [(0, b"x"*100)],
None, None,
)}, [(10,5)]) )}, [(10,5)])
self.failUnlessEqual(answer, (False, {0: ["11111"]})) self.failUnlessEqual(answer, (False, {0: [b"11111"]}))
self.failUnlessEqual(read("si1", [0], [(0,100)]), {0: [data]}) self.failUnlessEqual(read(b"si1", [0], [(0,100)]), {0: [data]})
reset() reset()
# finally, test some operators against empty shares # finally, test some operators against empty shares
answer = write("si1", secrets, {1: ([(10, 5, "eq", "11112"), answer = write(b"si1", secrets, {1: ([(10, 5, b"eq", b"11112"),
], ],
[(0, "x"*100)], [(0, b"x"*100)],
None, None,
)}, [(10,5)]) )}, [(10,5)])
self.failUnlessEqual(answer, (False, {0: ["11111"]})) self.failUnlessEqual(answer, (False, {0: [b"11111"]}))
self.failUnlessEqual(read("si1", [0], [(0,100)]), {0: [data]}) self.failUnlessEqual(read(b"si1", [0], [(0,100)]), {0: [data]})
reset() reset()
def test_readv(self): def test_readv(self):
ss = self.create("test_readv") ss = self.create("test_readv")
secrets = ( self.write_enabler("we1"), secrets = ( self.write_enabler(b"we1"),
self.renew_secret("we1"), self.renew_secret(b"we1"),
self.cancel_secret("we1") ) self.cancel_secret(b"we1") )
data = "".join([ ("%d" % i) * 10 for i in range(10) ]) data = b"".join([ (b"%d" % i) * 10 for i in range(10) ])
write = ss.remote_slot_testv_and_readv_and_writev write = ss.remote_slot_testv_and_readv_and_writev
read = ss.remote_slot_readv read = ss.remote_slot_readv
data = [("%d" % i) * 100 for i in range(3)] data = [(b"%d" % i) * 100 for i in range(3)]
rc = write("si1", secrets, rc = write(b"si1", secrets,
{0: ([], [(0,data[0])], None), {0: ([], [(0,data[0])], None),
1: ([], [(0,data[1])], None), 1: ([], [(0,data[1])], None),
2: ([], [(0,data[2])], None), 2: ([], [(0,data[2])], None),
}, []) }, [])
self.failUnlessEqual(rc, (True, {})) self.failUnlessEqual(rc, (True, {}))
answer = read("si1", [], [(0, 10)]) answer = read(b"si1", [], [(0, 10)])
self.failUnlessEqual(answer, {0: ["0"*10], self.failUnlessEqual(answer, {0: [b"0"*10],
1: ["1"*10], 1: [b"1"*10],
2: ["2"*10]}) 2: [b"2"*10]})
def compare_leases_without_timestamps(self, leases_a, leases_b): def compare_leases_without_timestamps(self, leases_a, leases_b):
self.failUnlessEqual(len(leases_a), len(leases_b)) self.failUnlessEqual(len(leases_a), len(leases_b))
@ -1230,19 +1230,19 @@ class MutableServer(unittest.TestCase):
def test_leases(self): def test_leases(self):
ss = self.create("test_leases") ss = self.create("test_leases")
def secrets(n): def secrets(n):
return ( self.write_enabler("we1"), return ( self.write_enabler(b"we1"),
self.renew_secret("we1-%d" % n), self.renew_secret(b"we1-%d" % n),
self.cancel_secret("we1-%d" % n) ) self.cancel_secret(b"we1-%d" % n) )
data = "".join([ ("%d" % i) * 10 for i in range(10) ]) data = b"".join([ (b"%d" % i) * 10 for i in range(10) ])
write = ss.remote_slot_testv_and_readv_and_writev write = ss.remote_slot_testv_and_readv_and_writev
read = ss.remote_slot_readv read = ss.remote_slot_readv
rc = write("si1", secrets(0), {0: ([], [(0,data)], None)}, []) rc = write(b"si1", secrets(0), {0: ([], [(0,data)], None)}, [])
self.failUnlessEqual(rc, (True, {})) self.failUnlessEqual(rc, (True, {}))
# create a random non-numeric file in the bucket directory, to # create a random non-numeric file in the bucket directory, to
# exercise the code that's supposed to ignore those. # exercise the code that's supposed to ignore those.
bucket_dir = os.path.join(self.workdir("test_leases"), bucket_dir = os.path.join(self.workdir("test_leases"),
"shares", storage_index_to_dir("si1")) "shares", storage_index_to_dir(b"si1"))
f = open(os.path.join(bucket_dir, "ignore_me.txt"), "w") f = open(os.path.join(bucket_dir, "ignore_me.txt"), "w")
f.write("you ought to be ignoring me\n") f.write("you ought to be ignoring me\n")
f.close() f.close()
@ -1251,45 +1251,45 @@ class MutableServer(unittest.TestCase):
self.failUnlessEqual(len(list(s0.get_leases())), 1) self.failUnlessEqual(len(list(s0.get_leases())), 1)
# add-lease on a missing storage index is silently ignored # add-lease on a missing storage index is silently ignored
self.failUnlessEqual(ss.remote_add_lease("si18", "", ""), None) self.failUnlessEqual(ss.remote_add_lease(b"si18", b"", b""), None)
# re-allocate the slots and use the same secrets, that should update # re-allocate the slots and use the same secrets, that should update
# the lease # the lease
write("si1", secrets(0), {0: ([], [(0,data)], None)}, []) write(b"si1", secrets(0), {0: ([], [(0,data)], None)}, [])
self.failUnlessEqual(len(list(s0.get_leases())), 1) self.failUnlessEqual(len(list(s0.get_leases())), 1)
# renew it directly # renew it directly
ss.remote_renew_lease("si1", secrets(0)[1]) ss.remote_renew_lease(b"si1", secrets(0)[1])
self.failUnlessEqual(len(list(s0.get_leases())), 1) self.failUnlessEqual(len(list(s0.get_leases())), 1)
# now allocate them with a bunch of different secrets, to trigger the # now allocate them with a bunch of different secrets, to trigger the
# extended lease code. Use add_lease for one of them. # extended lease code. Use add_lease for one of them.
write("si1", secrets(1), {0: ([], [(0,data)], None)}, []) write(b"si1", secrets(1), {0: ([], [(0,data)], None)}, [])
self.failUnlessEqual(len(list(s0.get_leases())), 2) self.failUnlessEqual(len(list(s0.get_leases())), 2)
secrets2 = secrets(2) secrets2 = secrets(2)
ss.remote_add_lease("si1", secrets2[1], secrets2[2]) ss.remote_add_lease(b"si1", secrets2[1], secrets2[2])
self.failUnlessEqual(len(list(s0.get_leases())), 3) self.failUnlessEqual(len(list(s0.get_leases())), 3)
write("si1", secrets(3), {0: ([], [(0,data)], None)}, []) write(b"si1", secrets(3), {0: ([], [(0,data)], None)}, [])
write("si1", secrets(4), {0: ([], [(0,data)], None)}, []) write(b"si1", secrets(4), {0: ([], [(0,data)], None)}, [])
write("si1", secrets(5), {0: ([], [(0,data)], None)}, []) write(b"si1", secrets(5), {0: ([], [(0,data)], None)}, [])
self.failUnlessEqual(len(list(s0.get_leases())), 6) self.failUnlessEqual(len(list(s0.get_leases())), 6)
all_leases = list(s0.get_leases()) all_leases = list(s0.get_leases())
# and write enough data to expand the container, forcing the server # and write enough data to expand the container, forcing the server
# to move the leases # to move the leases
write("si1", secrets(0), write(b"si1", secrets(0),
{0: ([], [(0,data)], 200), }, {0: ([], [(0,data)], 200), },
[]) [])
# read back the leases, make sure they're still intact. # read back the leases, make sure they're still intact.
self.compare_leases_without_timestamps(all_leases, list(s0.get_leases())) self.compare_leases_without_timestamps(all_leases, list(s0.get_leases()))
ss.remote_renew_lease("si1", secrets(0)[1]) ss.remote_renew_lease(b"si1", secrets(0)[1])
ss.remote_renew_lease("si1", secrets(1)[1]) ss.remote_renew_lease(b"si1", secrets(1)[1])
ss.remote_renew_lease("si1", secrets(2)[1]) ss.remote_renew_lease(b"si1", secrets(2)[1])
ss.remote_renew_lease("si1", secrets(3)[1]) ss.remote_renew_lease(b"si1", secrets(3)[1])
ss.remote_renew_lease("si1", secrets(4)[1]) ss.remote_renew_lease(b"si1", secrets(4)[1])
self.compare_leases_without_timestamps(all_leases, list(s0.get_leases())) self.compare_leases_without_timestamps(all_leases, list(s0.get_leases()))
# get a new copy of the leases, with the current timestamps. Reading # get a new copy of the leases, with the current timestamps. Reading
# data and failing to renew/cancel leases should leave the timestamps # data and failing to renew/cancel leases should leave the timestamps
@ -1300,7 +1300,7 @@ class MutableServer(unittest.TestCase):
# examine the exception thus raised, make sure the old nodeid is # examine the exception thus raised, make sure the old nodeid is
# present, to provide for share migration # present, to provide for share migration
e = self.failUnlessRaises(IndexError, e = self.failUnlessRaises(IndexError,
ss.remote_renew_lease, "si1", ss.remote_renew_lease, b"si1",
secrets(20)[1]) secrets(20)[1])
e_s = str(e) e_s = str(e)
self.failUnlessIn("Unable to renew non-existent lease", e_s) self.failUnlessIn("Unable to renew non-existent lease", e_s)
@ -1310,56 +1310,58 @@ class MutableServer(unittest.TestCase):
self.compare_leases(all_leases, list(s0.get_leases())) self.compare_leases(all_leases, list(s0.get_leases()))
# reading shares should not modify the timestamp # reading shares should not modify the timestamp
read("si1", [], [(0,200)]) read(b"si1", [], [(0,200)])
self.compare_leases(all_leases, list(s0.get_leases())) self.compare_leases(all_leases, list(s0.get_leases()))
write("si1", secrets(0), write(b"si1", secrets(0),
{0: ([], [(200, "make me bigger")], None)}, []) {0: ([], [(200, b"make me bigger")], None)}, [])
self.compare_leases_without_timestamps(all_leases, list(s0.get_leases())) self.compare_leases_without_timestamps(all_leases, list(s0.get_leases()))
write("si1", secrets(0), write(b"si1", secrets(0),
{0: ([], [(500, "make me really bigger")], None)}, []) {0: ([], [(500, b"make me really bigger")], None)}, [])
self.compare_leases_without_timestamps(all_leases, list(s0.get_leases())) self.compare_leases_without_timestamps(all_leases, list(s0.get_leases()))
def test_remove(self): def test_remove(self):
ss = self.create("test_remove") ss = self.create("test_remove")
self.allocate(ss, "si1", "we1", next(self._lease_secret), self.allocate(ss, b"si1", b"we1", next(self._lease_secret),
set([0,1,2]), 100) set([0,1,2]), 100)
readv = ss.remote_slot_readv readv = ss.remote_slot_readv
writev = ss.remote_slot_testv_and_readv_and_writev writev = ss.remote_slot_testv_and_readv_and_writev
secrets = ( self.write_enabler("we1"), secrets = ( self.write_enabler(b"we1"),
self.renew_secret("we1"), self.renew_secret(b"we1"),
self.cancel_secret("we1") ) self.cancel_secret(b"we1") )
# delete sh0 by setting its size to zero # delete sh0 by setting its size to zero
answer = writev("si1", secrets, answer = writev(b"si1", secrets,
{0: ([], [], 0)}, {0: ([], [], 0)},
[]) [])
# the answer should mention all the shares that existed before the # the answer should mention all the shares that existed before the
# write # write
self.failUnlessEqual(answer, (True, {0:[],1:[],2:[]}) ) self.failUnlessEqual(answer, (True, {0:[],1:[],2:[]}) )
# but a new read should show only sh1 and sh2 # but a new read should show only sh1 and sh2
self.failUnlessEqual(readv("si1", [], [(0,10)]), self.failUnlessEqual(readv(b"si1", [], [(0,10)]),
{1: [""], 2: [""]}) {1: [b""], 2: [b""]})
# delete sh1 by setting its size to zero # delete sh1 by setting its size to zero
answer = writev("si1", secrets, answer = writev(b"si1", secrets,
{1: ([], [], 0)}, {1: ([], [], 0)},
[]) [])
self.failUnlessEqual(answer, (True, {1:[],2:[]}) ) self.failUnlessEqual(answer, (True, {1:[],2:[]}) )
self.failUnlessEqual(readv("si1", [], [(0,10)]), self.failUnlessEqual(readv(b"si1", [], [(0,10)]),
{2: [""]}) {2: [b""]})
# delete sh2 by setting its size to zero # delete sh2 by setting its size to zero
answer = writev("si1", secrets, answer = writev(b"si1", secrets,
{2: ([], [], 0)}, {2: ([], [], 0)},
[]) [])
self.failUnlessEqual(answer, (True, {2:[]}) ) self.failUnlessEqual(answer, (True, {2:[]}) )
self.failUnlessEqual(readv("si1", [], [(0,10)]), self.failUnlessEqual(readv(b"si1", [], [(0,10)]),
{}) {})
# and the bucket directory should now be gone # and the bucket directory should now be gone
si = base32.b2a("si1") si = base32.b2a(b"si1")
# note: this is a detail of the storage server implementation, and # note: this is a detail of the storage server implementation, and
# may change in the future # may change in the future
if PY3:
si = si.decode("utf-8")
prefix = si[:2] prefix = si[:2]
prefixdir = os.path.join(self.workdir("test_remove"), "shares", prefix) prefixdir = os.path.join(self.workdir("test_remove"), "shares", prefix)
bucketdir = os.path.join(prefixdir, si) bucketdir = os.path.join(prefixdir, si)
@ -1373,7 +1375,7 @@ class MutableServer(unittest.TestCase):
""" """
ss = self.create("test_writev_without_renew_lease") ss = self.create("test_writev_without_renew_lease")
storage_index = "si2" storage_index = b"si2"
secrets = ( secrets = (
self.write_enabler(storage_index), self.write_enabler(storage_index),
self.renew_secret(storage_index), self.renew_secret(storage_index),
@ -1400,7 +1402,7 @@ class MutableServer(unittest.TestCase):
When ``get_slot_leases`` is called for a slot for which the server has no When ``get_slot_leases`` is called for a slot for which the server has no
shares, it returns an empty iterable. shares, it returns an empty iterable.
""" """
ss = self.create(b"test_get_slot_leases_empty_slot") ss = self.create("test_get_slot_leases_empty_slot")
self.assertEqual( self.assertEqual(
list(ss.get_slot_leases(b"si1")), list(ss.get_slot_leases(b"si1")),
[], [],
@ -1413,7 +1415,7 @@ class MutableServer(unittest.TestCase):
""" """
ss = self.create("test_remove_non_present") ss = self.create("test_remove_non_present")
storage_index = "si1" storage_index = b"si1"
secrets = ( secrets = (
self.write_enabler(storage_index), self.write_enabler(storage_index),
self.renew_secret(storage_index), self.renew_secret(storage_index),
@ -1590,7 +1592,7 @@ class MDMFProxies(unittest.TestCase, ShouldFailMixin):
# and the verification key # and the verification key
data += self.verification_key data += self.verification_key
# Then we'll add in gibberish until we get to the right point. # Then we'll add in gibberish until we get to the right point.
nulls = "".join([" " for i in xrange(len(data), share_data_offset)]) nulls = b"".join([b" " for i in xrange(len(data), share_data_offset)])
data += nulls data += nulls
# Then the share data # Then the share data
@ -1614,7 +1616,7 @@ class MDMFProxies(unittest.TestCase, ShouldFailMixin):
data = self.build_test_mdmf_share(tail_segment, empty) data = self.build_test_mdmf_share(tail_segment, empty)
# Finally, we write the whole thing to the storage server in one # Finally, we write the whole thing to the storage server in one
# pass. # pass.
testvs = [(0, 1, "eq", "")] testvs = [(0, 1, b"eq", b"")]
tws = {} tws = {}
tws[0] = (testvs, [(0, data)], None) tws[0] = (testvs, [(0, data)], None)
readv = [(0, 1)] readv = [(0, 1)]
@ -1655,7 +1657,7 @@ class MDMFProxies(unittest.TestCase, ShouldFailMixin):
sharedata_offset, sharedata_offset,
encprivkey_offset, encprivkey_offset,
eof_offset) eof_offset)
final_share = "".join([prefix, final_share = b"".join([prefix,
offsets, offsets,
self.verification_key, self.verification_key,
self.signature, self.signature,
@ -1680,7 +1682,7 @@ class MDMFProxies(unittest.TestCase, ShouldFailMixin):
# read them. This method writes one, which resembles but is not # read them. This method writes one, which resembles but is not
write = self.ss.remote_slot_testv_and_readv_and_writev write = self.ss.remote_slot_testv_and_readv_and_writev
share = self.build_test_sdmf_share(empty) share = self.build_test_sdmf_share(empty)
testvs = [(0, 1, "eq", "")] testvs = [(0, 1, b"eq", b"")]
tws = {} tws = {}
tws[0] = (testvs, [(0, share)], None) tws[0] = (testvs, [(0, share)], None)
readv = [] readv = []
@ -1690,7 +1692,7 @@ class MDMFProxies(unittest.TestCase, ShouldFailMixin):
def test_read(self): def test_read(self):
self.write_test_share_to_server("si1") self.write_test_share_to_server("si1")
mr = MDMFSlotReadProxy(self.storage_server, "si1", 0) mr = MDMFSlotReadProxy(self.storage_server, b"si1", 0)
# Check that every method equals what we expect it to. # Check that every method equals what we expect it to.
d = defer.succeed(None) d = defer.succeed(None)
def _check_block_and_salt(block_and_salt): def _check_block_and_salt(block_and_salt):
@ -1762,19 +1764,19 @@ class MDMFProxies(unittest.TestCase, ShouldFailMixin):
def test_read_with_different_tail_segment_size(self): def test_read_with_different_tail_segment_size(self):
self.write_test_share_to_server("si1", tail_segment=True) self.write_test_share_to_server("si1", tail_segment=True)
mr = MDMFSlotReadProxy(self.storage_server, "si1", 0) mr = MDMFSlotReadProxy(self.storage_server, b"si1", 0)
d = mr.get_block_and_salt(5) d = mr.get_block_and_salt(5)
def _check_tail_segment(results): def _check_tail_segment(results):
block, salt = results block, salt = results
self.failUnlessEqual(len(block), 1) self.failUnlessEqual(len(block), 1)
self.failUnlessEqual(block, "a") self.failUnlessEqual(block, b"a")
d.addCallback(_check_tail_segment) d.addCallback(_check_tail_segment)
return d return d
def test_get_block_with_invalid_segnum(self): def test_get_block_with_invalid_segnum(self):
self.write_test_share_to_server("si1") self.write_test_share_to_server("si1")
mr = MDMFSlotReadProxy(self.storage_server, "si1", 0) mr = MDMFSlotReadProxy(self.storage_server, b"si1", 0)
d = defer.succeed(None) d = defer.succeed(None)
d.addCallback(lambda ignored: d.addCallback(lambda ignored:
self.shouldFail(LayoutInvalid, "test invalid segnum", self.shouldFail(LayoutInvalid, "test invalid segnum",
@ -1785,7 +1787,7 @@ class MDMFProxies(unittest.TestCase, ShouldFailMixin):
def test_get_encoding_parameters_first(self): def test_get_encoding_parameters_first(self):
self.write_test_share_to_server("si1") self.write_test_share_to_server("si1")
mr = MDMFSlotReadProxy(self.storage_server, "si1", 0) mr = MDMFSlotReadProxy(self.storage_server, b"si1", 0)
d = mr.get_encoding_parameters() d = mr.get_encoding_parameters()
def _check_encoding_parameters(args): def _check_encoding_parameters(args):
(k, n, segment_size, datalen) = args (k, n, segment_size, datalen) = args
@ -1799,7 +1801,7 @@ class MDMFProxies(unittest.TestCase, ShouldFailMixin):
def test_get_seqnum_first(self): def test_get_seqnum_first(self):
self.write_test_share_to_server("si1") self.write_test_share_to_server("si1")
mr = MDMFSlotReadProxy(self.storage_server, "si1", 0) mr = MDMFSlotReadProxy(self.storage_server, b"si1", 0)
d = mr.get_seqnum() d = mr.get_seqnum()
d.addCallback(lambda seqnum: d.addCallback(lambda seqnum:
self.failUnlessEqual(seqnum, 0)) self.failUnlessEqual(seqnum, 0))
@ -1808,7 +1810,7 @@ class MDMFProxies(unittest.TestCase, ShouldFailMixin):
def test_get_root_hash_first(self): def test_get_root_hash_first(self):
self.write_test_share_to_server("si1") self.write_test_share_to_server("si1")
mr = MDMFSlotReadProxy(self.storage_server, "si1", 0) mr = MDMFSlotReadProxy(self.storage_server, b"si1", 0)
d = mr.get_root_hash() d = mr.get_root_hash()
d.addCallback(lambda root_hash: d.addCallback(lambda root_hash:
self.failUnlessEqual(root_hash, self.root_hash)) self.failUnlessEqual(root_hash, self.root_hash))
@ -1817,7 +1819,7 @@ class MDMFProxies(unittest.TestCase, ShouldFailMixin):
def test_get_checkstring_first(self): def test_get_checkstring_first(self):
self.write_test_share_to_server("si1") self.write_test_share_to_server("si1")
mr = MDMFSlotReadProxy(self.storage_server, "si1", 0) mr = MDMFSlotReadProxy(self.storage_server, b"si1", 0)
d = mr.get_checkstring() d = mr.get_checkstring()
d.addCallback(lambda checkstring: d.addCallback(lambda checkstring:
self.failUnlessEqual(checkstring, self.checkstring)) self.failUnlessEqual(checkstring, self.checkstring))
@ -1996,11 +1998,11 @@ class MDMFProxies(unittest.TestCase, ShouldFailMixin):
def serialize_blockhashes(self, blockhashes): def serialize_blockhashes(self, blockhashes):
return "".join(blockhashes) return b"".join(blockhashes)
def serialize_sharehashes(self, sharehashes): def serialize_sharehashes(self, sharehashes):
ret = "".join([struct.pack(">H32s", i, sharehashes[i]) ret = b"".join([struct.pack(">H32s", i, sharehashes[i])
for i in sorted(sharehashes.keys())]) for i in sorted(sharehashes.keys())])
return ret return ret