From e86e0d761f8d84d453c3863d92e60b38b6b30016 Mon Sep 17 00:00:00 2001 From: Itamar Turner-Trauring Date: Fri, 11 Sep 2020 14:49:30 -0400 Subject: [PATCH] Better test coverage for allmydata.storage.immutable. --- src/allmydata/test/test_storage.py | 37 +++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/src/allmydata/test/test_storage.py b/src/allmydata/test/test_storage.py index 4bbbcda30..bba4ae964 100644 --- a/src/allmydata/test/test_storage.py +++ b/src/allmydata/test/test_storage.py @@ -30,7 +30,7 @@ from allmydata import interfaces from allmydata.util import fileutil, hashutil, base32 from allmydata.storage.server import StorageServer from allmydata.storage.mutable import MutableShareFile -from allmydata.storage.immutable import BucketWriter, BucketReader +from allmydata.storage.immutable import BucketWriter, BucketReader, ShareFile from allmydata.storage.common import DataTooLargeError, storage_index_to_dir, \ UnknownMutableContainerVersionError, UnknownImmutableContainerVersionError from allmydata.storage.lease import LeaseInfo @@ -2968,3 +2968,38 @@ class Stats(unittest.TestCase): self.failUnless(output["get"]["95_0_percentile"] is None, output) self.failUnless(output["get"]["99_0_percentile"] is None, output) self.failUnless(output["get"]["99_9_percentile"] is None, output) + + +class ShareFileTests(unittest.TestCase): + """Tests for allmydata.storage.immutable.ShareFile.""" + + def get_sharefile(self): + sf = ShareFile(self.mktemp(), max_size=1000, create=True) + sf.write_share_data(0, b"abc") + sf.write_share_data(2, b"DEF") + # Should be b'abDEF' now. + return sf + + def test_read_write(self): + """Basic writes can be read.""" + sf = self.get_sharefile() + self.assertEqual(sf.read_share_data(0, 3), b"abD") + self.assertEqual(sf.read_share_data(1, 4), b"bDEF") + + def test_reads_beyond_file_end(self): + """Reads beyond the file size are truncated.""" + sf = self.get_sharefile() + self.assertEqual(sf.read_share_data(0, 10), b"abDEF") + self.assertEqual(sf.read_share_data(5, 10), b"") + + def test_too_large_write(self): + """Can't do write larger than file size.""" + sf = self.get_sharefile() + with self.assertRaises(DataTooLargeError): + sf.write_share_data(0, b"x" * 3000) + + def test_no_leases_cancelled(self): + """If no leases were cancelled, IndexError is raised.""" + sf = self.get_sharefile() + with self.assertRaises(IndexError): + sf.cancel_lease(b"garbage")