From 9b18949c9116b9f2ea0fa499e7524688e28769d4 Mon Sep 17 00:00:00 2001 From: Daira Hopwood Date: Mon, 15 Apr 2013 20:47:14 +0100 Subject: [PATCH] Fixes to tests. Some tests are applied to multiple backends. Signed-off-by: Daira Hopwood --- src/allmydata/test/check_speed.py | 2 +- src/allmydata/test/test_checker.py | 79 +- src/allmydata/test/test_cli.py | 32 +- src/allmydata/test/test_client.py | 178 +- src/allmydata/test/test_crawler.py | 182 +- src/allmydata/test/test_deepcheck.py | 30 +- src/allmydata/test/test_download.py | 280 +- src/allmydata/test/test_encode.py | 2 +- src/allmydata/test/test_hung_server.py | 76 +- src/allmydata/test/test_immutable.py | 4 +- src/allmydata/test/test_mutable.py | 58 +- src/allmydata/test/test_node.py | 1 - src/allmydata/test/test_repairer.py | 70 +- src/allmydata/test/test_storage.py | 3727 ++++++++++++++---------- src/allmydata/test/test_system.py | 375 ++- src/allmydata/test/test_upload.py | 104 +- src/allmydata/test/test_web.py | 85 +- 17 files changed, 3045 insertions(+), 2240 deletions(-) diff --git a/src/allmydata/test/check_speed.py b/src/allmydata/test/check_speed.py index c0b0ad80..1eeca949 100644 --- a/src/allmydata/test/check_speed.py +++ b/src/allmydata/test/check_speed.py @@ -76,7 +76,7 @@ class SpeedTest: return d def measure_rtt(self, res): - # use RIClient.get_nodeid() to measure the foolscap-level RTT + # measure the foolscap-level RTT d = self.client_rref.callRemote("measure_peer_response_time") def _got(res): assert len(res) # need at least one peer diff --git a/src/allmydata/test/test_checker.py b/src/allmydata/test/test_checker.py index e95c3958..67329843 100644 --- a/src/allmydata/test/test_checker.py +++ b/src/allmydata/test/test_checker.py @@ -1,20 +1,20 @@ import simplejson -import os.path, shutil + from twisted.trial import unittest from twisted.internet import defer + from allmydata import check_results, uri -from allmydata import uri as tahoe_uri from allmydata.util import base32 from allmydata.web import check_results as web_check_results from allmydata.storage_client import StorageFarmBroker, NativeStorageServer -from allmydata.storage.server import storage_index_to_dir from allmydata.monitor import Monitor from allmydata.test.no_network import GridTestMixin from allmydata.immutable.upload import Data from allmydata.test.common_web import WebRenderingMixin from allmydata.mutable.publish import MutableData + class FakeClient: def get_storage_broker(self): return self.storage_broker @@ -315,53 +315,23 @@ class WebResultsRendering(unittest.TestCase, WebRenderingMixin): class BalancingAct(GridTestMixin, unittest.TestCase): # test for #1115 regarding the 'count-good-share-hosts' metric - - def add_server(self, server_number, readonly=False): - assert self.g, "I tried to find a grid at self.g, but failed" - ss = self.g.make_server(server_number, readonly) - #log.msg("just created a server, number: %s => %s" % (server_number, ss,)) - self.g.add_server(server_number, ss) - - def add_server_with_share(self, server_number, uri, share_number=None, - readonly=False): - self.add_server(server_number, readonly) - if share_number is not None: - self.copy_share_to_server(uri, share_number, server_number) - - def copy_share_to_server(self, uri, share_number, server_number): - ss = self.g.servers_by_number[server_number] - # Copy share i from the directory associated with the first - # storage server to the directory associated with this one. - assert self.g, "I tried to find a grid at self.g, but failed" - assert self.shares, "I tried to find shares at self.shares, but failed" - old_share_location = self.shares[share_number][2] - new_share_location = os.path.join(ss.storedir, "shares") - si = tahoe_uri.from_string(self.uri).get_storage_index() - new_share_location = os.path.join(new_share_location, - storage_index_to_dir(si)) - if not os.path.exists(new_share_location): - os.makedirs(new_share_location) - new_share_location = os.path.join(new_share_location, - str(share_number)) - if old_share_location != new_share_location: - shutil.copy(old_share_location, new_share_location) - shares = self.find_uri_shares(uri) - # Make sure that the storage server has the share. - self.failUnless((share_number, ss.my_nodeid, new_share_location) - in shares) - - def _pretty_shares_chart(self, uri): + def _print_pretty_shares_chart(self, res): # Servers are labeled A-Z, shares are labeled 0-9 letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' assert len(self.g.servers_by_number) < len(letters), \ "This little printing function is only meant for < 26 servers" - shares_chart = {} - names = dict(zip([ss.my_nodeid + names = dict(zip([ss.get_serverid() for _,ss in self.g.servers_by_number.iteritems()], letters)) - for shnum, serverid, _ in self.find_uri_shares(uri): - shares_chart.setdefault(shnum, []).append(names[serverid]) - return shares_chart + d = self.find_uri_shares(self.uri) + def _got(shares): + shares_chart = {} + for shnum, serverid, _ in shares: + shares_chart.setdefault(shnum, []).append(names[serverid]) + print shares_chart + return res + d.addCallback(_got) + return d def test_good_share_hosts(self): self.basedir = "checker/BalancingAct/1115" @@ -383,18 +353,18 @@ class BalancingAct(GridTestMixin, unittest.TestCase): self.shares = shares d.addCallback(_store_shares) - def add_three(_, i): - # Add a new server with just share 3 - self.add_server_with_share(i, self.uri, 3) - #print self._pretty_shares_chart(self.uri) - for i in range(1,5): - d.addCallback(add_three, i) - - def _check_and_repair(_): - return self.imm.check_and_repair(Monitor()) + def _layout(ign): + # Add servers with just share 3 + for i in range(1, 5): + self.add_server_with_share(self.uri, server_number=i, share_number=3) + d.addCallback(_layout) + #d.addCallback(self._print_pretty_shares_chart) + def _check_and_repair(ign): + d2 = self.imm.check_and_repair(Monitor()) + #d2.addCallback(self._print_pretty_shares_chart) + return d2 def _check_counts(crr, shares_good, good_share_hosts): prr = crr.get_post_repair_results() - #print self._pretty_shares_chart(self.uri) self.failUnlessEqual(prr.get_share_counter_good(), shares_good) self.failUnlessEqual(prr.get_host_counter_good_shares(), good_share_hosts) @@ -418,6 +388,7 @@ class BalancingAct(GridTestMixin, unittest.TestCase): d.addCallback(_check_counts, 0, 0) return d + class AddLease(GridTestMixin, unittest.TestCase): # test for #875, in which failures in the add-lease call cause # false-negatives in the checker diff --git a/src/allmydata/test/test_cli.py b/src/allmydata/test/test_cli.py index 591ebf32..61f5fff7 100644 --- a/src/allmydata/test/test_cli.py +++ b/src/allmydata/test/test_cli.py @@ -2912,20 +2912,19 @@ class Check(GridTestMixin, CLITestMixin, unittest.TestCase): d.addCallback(lambda ign: self.do_cli("check", "--raw", self.lit_uri)) d.addCallback(_check_lit_raw) - def _clobber_shares(ignored): + d.addCallback(lambda ign: self.find_uri_shares(self.uri)) + def _clobber_shares(shares): # delete one, corrupt a second - shares = self.find_uri_shares(self.uri) self.failUnlessReallyEqual(len(shares), 10) - os.unlink(shares[0][2]) - cso = debug.CorruptShareOptions() - cso.stdout = StringIO() - cso.parseOptions([shares[1][2]]) + fileutil.remove(shares[0][2]) + stdout = StringIO() + sharefile = shares[1][2] storage_index = uri.from_string(self.uri).get_storage_index() self._corrupt_share_line = " server %s, SI %s, shnum %d" % \ (base32.b2a(shares[1][1]), base32.b2a(storage_index), shares[1][0]) - debug.corrupt_share(cso) + debug.do_corrupt_share(stdout, sharefile) d.addCallback(_clobber_shares) d.addCallback(lambda ign: self.do_cli("check", "--verify", self.uri)) @@ -3051,22 +3050,23 @@ class Check(GridTestMixin, CLITestMixin, unittest.TestCase): self.failUnlessIn(" 317-1000 : 1 (1000 B, 1000 B)", lines) d.addCallback(_check_stats) - def _clobber_shares(ignored): - shares = self.find_uri_shares(self.uris[u"g\u00F6\u00F6d"]) + d.addCallback(lambda ign: self.find_uri_shares(self.uris[u"g\u00F6\u00F6d"])) + def _clobber_shares(shares): self.failUnlessReallyEqual(len(shares), 10) - os.unlink(shares[0][2]) + fileutil.remove(shares[0][2]) + d.addCallback(_clobber_shares) - shares = self.find_uri_shares(self.uris["mutable"]) - cso = debug.CorruptShareOptions() - cso.stdout = StringIO() - cso.parseOptions([shares[1][2]]) + d.addCallback(lambda ign: self.find_uri_shares(self.uris["mutable"])) + def _clobber_mutable_shares(shares): + stdout = StringIO() + sharefile = shares[1][2] storage_index = uri.from_string(self.uris["mutable"]).get_storage_index() self._corrupt_share_line = " corrupt: server %s, SI %s, shnum %d" % \ (base32.b2a(shares[1][1]), base32.b2a(storage_index), shares[1][0]) - debug.corrupt_share(cso) - d.addCallback(_clobber_shares) + debug.do_corrupt_share(stdout, sharefile) + d.addCallback(_clobber_mutable_shares) # root # root/g\u00F6\u00F6d [9 shares] diff --git a/src/allmydata/test/test_client.py b/src/allmydata/test/test_client.py index d5ee894c..ba5b6171 100644 --- a/src/allmydata/test/test_client.py +++ b/src/allmydata/test/test_client.py @@ -3,9 +3,11 @@ from twisted.trial import unittest from twisted.application import service import allmydata -from allmydata.node import InvalidValueError, OldConfigError, OldConfigOptionError, MissingConfigEntry +from allmydata.node import OldConfigError, OldConfigOptionError, InvalidValueError, MissingConfigEntry from allmydata import client from allmydata.storage_client import StorageFarmBroker +from allmydata.storage.backends.disk.disk_backend import DiskBackend +from allmydata.storage.backends.cloud.cloud_backend import CloudBackend from allmydata.util import base32, fileutil from allmydata.interfaces import IFilesystemNode, IFileNode, \ IImmutableFileNode, IMutableFileNode, IDirectoryNode @@ -14,6 +16,7 @@ import allmydata.test.common_util as testutil import mock + BASECONFIG = ("[client]\n" "introducer.furl = \n" ) @@ -26,9 +29,11 @@ class Basic(testutil.ReallyEqualMixin, unittest.TestCase): def test_loadable(self): basedir = "test_client.Basic.test_loadable" os.mkdir(basedir) - fileutil.write(os.path.join(basedir, "tahoe.cfg"), \ - BASECONFIG) - client.Client(basedir) + fileutil.write(os.path.join(basedir, "tahoe.cfg"), + BASECONFIG) + c = client.Client(basedir) + server = c.getServiceNamed("storage") + self.failUnless(isinstance(server.backend, DiskBackend), server.backend) @mock.patch('twisted.python.log.msg') def test_error_on_old_config_files(self, mock_log_msg): @@ -68,8 +73,8 @@ class Basic(testutil.ReallyEqualMixin, unittest.TestCase): def test_secrets(self): basedir = "test_client.Basic.test_secrets" os.mkdir(basedir) - fileutil.write(os.path.join(basedir, "tahoe.cfg"), \ - BASECONFIG) + fileutil.write(os.path.join(basedir, "tahoe.cfg"), + BASECONFIG) c = client.Client(basedir) secret_fname = os.path.join(basedir, "private", "secret") self.failUnless(os.path.exists(secret_fname), secret_fname) @@ -97,59 +102,158 @@ class Basic(testutil.ReallyEqualMixin, unittest.TestCase): def test_reserved_1(self): basedir = "client.Basic.test_reserved_1" os.mkdir(basedir) - fileutil.write(os.path.join(basedir, "tahoe.cfg"), \ - BASECONFIG + \ - "[storage]\n" + \ - "enabled = true\n" + \ - "reserved_space = 1000\n") + fileutil.write(os.path.join(basedir, "tahoe.cfg"), + BASECONFIG + + "[storage]\n" + + "enabled = true\n" + + "reserved_space = 1000\n") c = client.Client(basedir) - self.failUnlessEqual(c.getServiceNamed("storage").reserved_space, 1000) + server = c.getServiceNamed("storage") + self.failUnlessReallyEqual(server.backend._reserved_space, 1000) def test_reserved_2(self): basedir = "client.Basic.test_reserved_2" os.mkdir(basedir) - fileutil.write(os.path.join(basedir, "tahoe.cfg"), \ - BASECONFIG + \ - "[storage]\n" + \ - "enabled = true\n" + \ - "reserved_space = 10K\n") + fileutil.write(os.path.join(basedir, "tahoe.cfg"), + BASECONFIG + + "[storage]\n" + + "enabled = true\n" + + "reserved_space = 10K\n") c = client.Client(basedir) - self.failUnlessEqual(c.getServiceNamed("storage").reserved_space, 10*1000) + server = c.getServiceNamed("storage") + self.failUnlessReallyEqual(server.backend._reserved_space, 10*1000) def test_reserved_3(self): basedir = "client.Basic.test_reserved_3" os.mkdir(basedir) - fileutil.write(os.path.join(basedir, "tahoe.cfg"), \ - BASECONFIG + \ - "[storage]\n" + \ - "enabled = true\n" + \ - "reserved_space = 5mB\n") + fileutil.write(os.path.join(basedir, "tahoe.cfg"), + BASECONFIG + + "[storage]\n" + + "enabled = true\n" + + "reserved_space = 5mB\n") c = client.Client(basedir) - self.failUnlessEqual(c.getServiceNamed("storage").reserved_space, - 5*1000*1000) + server = c.getServiceNamed("storage") + self.failUnlessReallyEqual(server.backend._reserved_space, 5*1000*1000) def test_reserved_4(self): basedir = "client.Basic.test_reserved_4" os.mkdir(basedir) - fileutil.write(os.path.join(basedir, "tahoe.cfg"), \ - BASECONFIG + \ - "[storage]\n" + \ - "enabled = true\n" + \ - "reserved_space = 78Gb\n") + fileutil.write(os.path.join(basedir, "tahoe.cfg"), + BASECONFIG + + "[storage]\n" + + "enabled = true\n" + + "reserved_space = 78Gb\n") + c = client.Client(basedir) + server = c.getServiceNamed("storage") + self.failUnlessReallyEqual(server.backend._reserved_space, 78*1000*1000*1000) + + def test_reserved_default(self): + # This is testing the default when 'reserved_space' is not present, not + # the default for a newly created node. + basedir = "client.Basic.test_reserved_default" + os.mkdir(basedir) + fileutil.write(os.path.join(basedir, "tahoe.cfg"), + BASECONFIG + + "[storage]\n" + + "enabled = true\n") c = client.Client(basedir) - self.failUnlessEqual(c.getServiceNamed("storage").reserved_space, - 78*1000*1000*1000) + server = c.getServiceNamed("storage") + self.failUnlessReallyEqual(server.backend._reserved_space, 0) def test_reserved_bad(self): basedir = "client.Basic.test_reserved_bad" os.mkdir(basedir) - fileutil.write(os.path.join(basedir, "tahoe.cfg"), \ - BASECONFIG + \ - "[storage]\n" + \ - "enabled = true\n" + \ - "reserved_space = bogus\n") + fileutil.write(os.path.join(basedir, "tahoe.cfg"), + BASECONFIG + + "[storage]\n" + + "enabled = true\n" + + "reserved_space = bogus\n") + self.failUnlessRaises(InvalidValueError, client.Client, basedir) + + def _write_s3secret(self, basedir, secret="dummy"): + os.mkdir(os.path.join(basedir, "private")) + fileutil.write(os.path.join(basedir, "private", "s3secret"), secret) + + @mock.patch('allmydata.storage.backends.cloud.s3.s3_container.S3Container') + def test_s3_config_good_defaults(self, mock_S3Container): + basedir = "client.Basic.test_s3_config_good_defaults" + os.mkdir(basedir) + self._write_s3secret(basedir) + config = (BASECONFIG + + "[storage]\n" + + "enabled = true\n" + + "backend = cloud.s3\n" + + "s3.access_key_id = keyid\n" + + "s3.bucket = test\n") + fileutil.write(os.path.join(basedir, "tahoe.cfg"), config) + + c = client.Client(basedir) + mock_S3Container.assert_called_with("keyid", "dummy", "http://s3.amazonaws.com", "test", None, None) + server = c.getServiceNamed("storage") + self.failUnless(isinstance(server.backend, CloudBackend), server.backend) + + mock_S3Container.reset_mock() + fileutil.write(os.path.join(basedir, "private", "s3producttoken"), "{ProductToken}") + self.failUnlessRaises(InvalidValueError, client.Client, basedir) + + mock_S3Container.reset_mock() + fileutil.write(os.path.join(basedir, "private", "s3usertoken"), "{UserToken}") + fileutil.write(os.path.join(basedir, "tahoe.cfg"), config + "s3.url = http://s3.example.com\n") + + c = client.Client(basedir) + mock_S3Container.assert_called_with("keyid", "dummy", "http://s3.example.com", "test", + "{UserToken}", "{ProductToken}") + + def test_s3_readonly_bad(self): + basedir = "client.Basic.test_s3_readonly_bad" + os.mkdir(basedir) + self._write_s3secret(basedir) + fileutil.write(os.path.join(basedir, "tahoe.cfg"), + BASECONFIG + + "[storage]\n" + + "enabled = true\n" + + "readonly = true\n" + + "backend = cloud.s3\n" + + "s3.access_key_id = keyid\n" + + "s3.bucket = test\n") self.failUnlessRaises(InvalidValueError, client.Client, basedir) + def test_s3_config_no_access_key_id(self): + basedir = "client.Basic.test_s3_config_no_access_key_id" + os.mkdir(basedir) + self._write_s3secret(basedir) + fileutil.write(os.path.join(basedir, "tahoe.cfg"), + BASECONFIG + + "[storage]\n" + + "enabled = true\n" + + "backend = cloud.s3\n" + + "s3.bucket = test\n") + self.failUnlessRaises(MissingConfigEntry, client.Client, basedir) + + def test_s3_config_no_bucket(self): + basedir = "client.Basic.test_s3_config_no_bucket" + os.mkdir(basedir) + self._write_s3secret(basedir) + fileutil.write(os.path.join(basedir, "tahoe.cfg"), + BASECONFIG + + "[storage]\n" + + "enabled = true\n" + + "backend = cloud.s3\n" + + "s3.access_key_id = keyid\n") + self.failUnlessRaises(MissingConfigEntry, client.Client, basedir) + + def test_s3_config_no_s3secret(self): + basedir = "client.Basic.test_s3_config_no_s3secret" + os.mkdir(basedir) + fileutil.write(os.path.join(basedir, "tahoe.cfg"), + BASECONFIG + + "[storage]\n" + + "enabled = true\n" + + "backend = cloud.s3\n" + + "s3.access_key_id = keyid\n" + + "s3.bucket = test\n") + self.failUnlessRaises(MissingConfigEntry, client.Client, basedir) + def test_expire_mutable_false_unsupported(self): basedir = "client.Basic.test_expire_mutable_false_unsupported" os.mkdir(basedir) diff --git a/src/allmydata/test/test_crawler.py b/src/allmydata/test/test_crawler.py index 5b8e2ab9..2ad42a6c 100644 --- a/src/allmydata/test/test_crawler.py +++ b/src/allmydata/test/test_crawler.py @@ -1,17 +1,21 @@ import time import os.path + from twisted.trial import unittest from twisted.application import service from twisted.internet import defer from foolscap.api import fireEventually +from allmydata.util.deferredutil import gatherResults -from allmydata.util import fileutil, hashutil +from allmydata.util import hashutil from allmydata.storage.server import StorageServer, si_b2a -from allmydata.storage.crawler import ShareCrawler +from allmydata.storage.crawler import ShareCrawler, TimeSliceExceeded +from allmydata.storage.backends.disk.disk_backend import DiskBackend +from allmydata.storage.backends.cloud.cloud_backend import CloudBackend +from allmydata.storage.backends.cloud.mock_cloud import MockContainer -from allmydata.test.test_storage import FakeCanary -from allmydata.test.common import CrawlerTestMixin +from allmydata.test.common import CrawlerTestMixin, FakeCanary from allmydata.test.common_util import StallMixin @@ -23,8 +27,12 @@ class EnumeratingCrawler(ShareCrawler): ShareCrawler.__init__(self, *args, **kwargs) self.sharesets = [] - def process_bucket(self, cycle, prefix, prefixdir, storage_index_b32): - self.sharesets.append(storage_index_b32) + def process_prefix(self, cycle, prefix, start_slice): + d = self.backend.get_sharesets_for_prefix(prefix) + def _got_sharesets(sharesets): + self.sharesets += [s.get_storage_index_string() for s in sharesets] + d.addCallback(_got_sharesets) + return d class ConsumingCrawler(ShareCrawler): @@ -39,12 +47,21 @@ class ConsumingCrawler(ShareCrawler): self.cycles = 0 self.last_yield = 0.0 - def process_bucket(self, cycle, prefix, prefixdir, storage_index_b32): - start = time.time() - time.sleep(0.05) - elapsed = time.time() - start - self.accumulated += elapsed - self.last_yield += elapsed + def process_prefix(self, cycle, prefix, start_slice): + # XXX I don't know whether this behaviour makes sense for the test + # that uses it any more. + d = self.backend.get_sharesets_for_prefix(prefix) + def _got_sharesets(sharesets): + for shareset in sharesets: + start = time.time() + time.sleep(0.05) + elapsed = time.time() - start + self.accumulated += elapsed + self.last_yield += elapsed + if self.clock.seconds() >= start_slice + self.cpu_slice: + raise TimeSliceExceeded() + d.addCallback(_got_sharesets) + return d def finished_cycle(self, cycle): self.cycles += 1 @@ -53,7 +70,7 @@ class ConsumingCrawler(ShareCrawler): self.last_yield = 0.0 -class Basic(unittest.TestCase, StallMixin, CrawlerTestMixin): +class CrawlerTest(StallMixin, CrawlerTestMixin): def setUp(self): self.s = service.MultiService() self.s.startService() @@ -68,75 +85,78 @@ class Basic(unittest.TestCase, StallMixin, CrawlerTestMixin): def cs(self, i, serverid): return hashutil.bucket_cancel_secret_hash(str(i), serverid) - def create(self, basedir): - self.basedir = basedir - fileutil.make_dirs(basedir) + def create(self, name): + self.basedir = os.path.join("crawler", self.__class__.__name__, name) self.serverid = "\x00" * 20 - server = StorageServer(basedir, self.serverid) + backend = self.make_backend(self.basedir) + server = StorageServer(self.serverid, backend, self.basedir) server.setServiceParent(self.s) return server def write(self, i, aa, serverid, tail=0): si = self.si(i) si = si[:-1] + chr(tail) - had,made = aa.remote_allocate_buckets(si, - self.rs(i, serverid), - self.cs(i, serverid), - set([0]), 99, FakeCanary()) - made[0].remote_write(0, "data") - made[0].remote_close() - return si_b2a(si) + d = defer.succeed(None) + d.addCallback(lambda ign: aa.remote_allocate_buckets(si, + self.rs(i, serverid), + self.cs(i, serverid), + set([0]), 99, FakeCanary())) + def _allocated( (had, made) ): + d2 = defer.succeed(None) + d2.addCallback(lambda ign: made[0].remote_write(0, "data")) + d2.addCallback(lambda ign: made[0].remote_close()) + d2.addCallback(lambda ign: si_b2a(si)) + return d2 + d.addCallback(_allocated) + return d def test_service(self): - server = self.create("crawler/Basic/service") + server = self.create("test_service") aa = server.get_accountant().get_anonymous_account() - sis = [self.write(i, aa, self.serverid) for i in range(10)] - - statefile = os.path.join(self.basedir, "statefile") - c = EnumeratingCrawler(server, statefile) - c.setServiceParent(self.s) + d = gatherResults([self.write(i, aa, self.serverid) for i in range(10)]) + def _writes_done(sis): + statefile = os.path.join(self.basedir, "statefile") + c = EnumeratingCrawler(server.backend, statefile) + c.setServiceParent(self.s) - # it should be legal to call get_state() and get_progress() right - # away, even before the first tick is performed. No work should have - # been done yet. - s = c.get_state() - p = c.get_progress() - self.failUnlessEqual(s["last-complete-prefix"], None) - self.failUnlessEqual(s["current-cycle"], None) - self.failUnlessEqual(p["cycle-in-progress"], False) - - d = self._after_prefix(None, 'sg', c) - def _after_sg_prefix(state): + # it should be legal to call get_state() and get_progress() right + # away, even before the first tick is performed. No work should have + # been done yet. + s = c.get_state() p = c.get_progress() - self.failUnlessEqual(p["cycle-in-progress"], True) - pct = p["cycle-complete-percentage"] - # After the 'sg' prefix, we happen to be 76.17% complete and to - # have processed 6 sharesets. As long as we create shares in - # deterministic order, this will continue to be true. - self.failUnlessEqual(int(pct), 76) - self.failUnlessEqual(len(c.sharesets), 6) - - return c.set_hook('after_cycle') - d.addCallback(_after_sg_prefix) - - def _after_first_cycle(ignored): - self.failUnlessEqual(sorted(sis), sorted(c.sharesets)) - d.addCallback(_after_first_cycle) - d.addBoth(self._wait_for_yield, c) - - # Check that a new crawler picks up on the state file correctly. - def _new_crawler(ign): - c2 = EnumeratingCrawler(server, statefile) - c2.setServiceParent(self.s) - - d2 = c2.set_hook('after_cycle') - def _after_first_cycle2(ignored): - self.failUnlessEqual(sorted(sis), sorted(c2.sharesets)) - d2.addCallback(_after_first_cycle2) - d2.addBoth(self._wait_for_yield, c2) - return d2 - d.addCallback(_new_crawler) + self.failUnlessEqual(s["last-complete-prefix"], None) + self.failUnlessEqual(s["current-cycle"], None) + self.failUnlessEqual(p["cycle-in-progress"], False) + + d2 = self._after_prefix(None, 'sg', c) + def _after_sg_prefix(state): + p = c.get_progress() + self.failUnlessEqual(p["cycle-in-progress"], True) + pct = p["cycle-complete-percentage"] + # After the 'sg' prefix, we happen to be 76.17% complete and to + # have processed 6 sharesets. As long as we create shares in + # deterministic order, this will continue to be true. + self.failUnlessEqual(int(pct), 76) + self.failUnlessEqual(len(c.sharesets), 6) + + return c.set_hook('after_cycle') + d2.addCallback(_after_sg_prefix) + + d2.addCallback(lambda ign: self.failUnlessEqual(sorted(sis), sorted(c.sharesets))) + d2.addBoth(self._wait_for_yield, c) + + # Check that a new crawler picks up on the state file correctly. + def _new_crawler(ign): + c_new = EnumeratingCrawler(server.backend, statefile) + c_new.setServiceParent(self.s) + + d3 = c_new.set_hook('after_cycle') + d3.addCallback(lambda ign: self.failUnlessEqual(sorted(sis), sorted(c_new.sharesets))) + d3.addBoth(self._wait_for_yield, c_new) + return d3 + d2.addCallback(_new_crawler) + d.addCallback(_writes_done) return d def OFF_test_cpu_usage(self): @@ -146,14 +166,17 @@ class Basic(unittest.TestCase, StallMixin, CrawlerTestMixin): # Crawler is accomplishing it's run-slowly goals, re-enable this test # and read the stdout when it runs. - server = self.create("crawler/Basic/cpu_usage") + # FIXME: it should be possible to make this test run deterministically + # by passing a Clock into the crawler. + + server = self.create("test_cpu_usage") aa = server.get_accountant().get_anonymous_account() for i in range(10): self.write(i, aa, self.serverid) statefile = os.path.join(self.basedir, "statefile") - c = ConsumingCrawler(server, statefile) + c = ConsumingCrawler(server.backend, statefile) c.setServiceParent(self.s) # This will run as fast as it can, consuming about 50ms per call to @@ -189,14 +212,14 @@ class Basic(unittest.TestCase, StallMixin, CrawlerTestMixin): return d def test_empty_subclass(self): - server = self.create("crawler/Basic/empty_subclass") + server = self.create("test_empty_subclass") aa = server.get_accountant().get_anonymous_account() for i in range(10): self.write(i, aa, self.serverid) statefile = os.path.join(self.basedir, "statefile") - c = ShareCrawler(server, statefile) + c = ShareCrawler(server.backend, statefile) c.slow_start = 0 c.setServiceParent(self.s) @@ -208,14 +231,14 @@ class Basic(unittest.TestCase, StallMixin, CrawlerTestMixin): return d def test_oneshot(self): - server = self.create("crawler/Basic/oneshot") + server = self.create("test_oneshot") aa = server.get_accountant().get_anonymous_account() for i in range(30): self.write(i, aa, self.serverid) statefile = os.path.join(self.basedir, "statefile") - c = EnumeratingCrawler(server, statefile) + c = EnumeratingCrawler(server.backend, statefile) c.setServiceParent(self.s) d = c.set_hook('after_cycle') @@ -235,3 +258,12 @@ class Basic(unittest.TestCase, StallMixin, CrawlerTestMixin): d.addCallback(_check) return d + +class CrawlerTestWithDiskBackend(CrawlerTest, unittest.TestCase): + def make_backend(self, basedir): + return DiskBackend(basedir) + + +class CrawlerTestWithMockCloudBackend(CrawlerTest, unittest.TestCase): + def make_backend(self, basedir): + return CloudBackend(MockContainer(basedir)) diff --git a/src/allmydata/test/test_deepcheck.py b/src/allmydata/test/test_deepcheck.py index 98f2a528..2d61fc1d 100644 --- a/src/allmydata/test/test_deepcheck.py +++ b/src/allmydata/test/test_deepcheck.py @@ -20,6 +20,8 @@ from allmydata.test.common import ErrorMixin, _corrupt_mutable_share_data, \ ShouldFailMixin from allmydata.test.common_util import StallMixin from allmydata.test.no_network import GridTestMixin +from allmydata.scripts import debug + timeout = 2400 # One of these took 1046.091s on Zandr's ARM box. @@ -63,8 +65,8 @@ class MutableChecker(GridTestMixin, unittest.TestCase, ErrorMixin): def _stash_and_corrupt(node): self.node = node self.fileurl = "uri/" + urllib.quote(node.get_uri()) - self.corrupt_shares_numbered(node.get_uri(), [0], - _corrupt_mutable_share_data) + return self.corrupt_shares_numbered(node.get_uri(), [0], + _corrupt_mutable_share_data) d.addCallback(_stash_and_corrupt) # now make sure the webapi verifier notices it d.addCallback(lambda ign: self.GET(self.fileurl+"?t=check&verify=true", @@ -898,8 +900,6 @@ class DeepCheckWebBad(DeepCheckBase, unittest.TestCase): d.addErrback(self.explain_error) return d - - def set_up_damaged_tree(self): # 6.4s @@ -984,24 +984,20 @@ class DeepCheckWebBad(DeepCheckBase, unittest.TestCase): return d - def _run_cli(self, argv): - stdout, stderr = StringIO(), StringIO() - # this can only do synchronous operations - assert argv[0] == "debug" - runner.runner(argv, run_by_human=False, stdout=stdout, stderr=stderr) - return stdout.getvalue() - def _delete_some_shares(self, node): - self.delete_shares_numbered(node.get_uri(), [0,1]) + return self.delete_shares_numbered(node.get_uri(), [0,1]) def _corrupt_some_shares(self, node): - for (shnum, serverid, sharefile) in self.find_uri_shares(node.get_uri()): - if shnum in (0,1): - self._run_cli(["debug", "corrupt-share", sharefile]) + d = self.find_uri_shares(node.get_uri()) + def _got_shares(sharelist): + for (shnum, serverid, sharefile) in sharelist: + if shnum in (0,1): + debug.do_corrupt_share(StringIO(), sharefile) + d.addCallback(_got_shares) + return d def _delete_most_shares(self, node): - self.delete_shares_numbered(node.get_uri(), range(1,10)) - + return self.delete_shares_numbered(node.get_uri(), range(1,10)) def check_is_healthy(self, cr, where): try: diff --git a/src/allmydata/test/test_download.py b/src/allmydata/test/test_download.py index 710d98ed..7de2ea8e 100644 --- a/src/allmydata/test/test_download.py +++ b/src/allmydata/test/test_download.py @@ -4,10 +4,13 @@ # shares from a previous version. import os + from twisted.trial import unittest from twisted.internet import defer, reactor +from foolscap.eventual import eventually, fireEventually, flushEventualQueue +from allmydata.util.deferredutil import async_iterate + from allmydata import uri -from allmydata.storage.server import storage_index_to_dir from allmydata.util import base32, fileutil, spans, log, hashutil from allmydata.util.consumer import download_to_data, MemoryConsumer from allmydata.immutable import upload, layout @@ -20,7 +23,7 @@ from allmydata.immutable.downloader.common import BadSegmentNumberError, \ from allmydata.immutable.downloader.status import DownloadStatus from allmydata.immutable.downloader.fetcher import SegmentFetcher from allmydata.codec import CRSDecoder -from foolscap.eventual import eventually, fireEventually, flushEventualQueue + plaintext = "This is a moderate-sized file.\n" * 10 mutable_plaintext = "This is a moderate-sized mutable file.\n" * 10 @@ -84,90 +87,71 @@ class _Base(GridTestMixin, ShouldFailMixin): u = upload.Data(plaintext, None) d = self.c0.upload(u) f = open("stored_shares.py", "w") + + def _write_py(uri): + si = uri.from_string(uri).get_storage_index() + def _each_server( (i,ss,ssdir) ): + sharemap = {} + shareset = ss.backend.get_shareset(si) + d2 = shareset.get_shares() + def _got_shares( (shares, corrupted) ): + assert len(corrupted) == 0, (shares, corrupted) + for share in shares: + sharedata = fileutil.read(share._get_path()) + sharemap[share.get_shnum()] = sharedata + + fileutil.remove(shareset._get_sharedir()) + if sharemap: + f.write(' %d: { # client[%d]\n' % (i, i)) + for shnum in sorted(sharemap.keys()): + f.write(' %d: base32.a2b("%s"),\n' % + (shnum, base32.b2a(sharemap[shnum]))) + f.write(' },\n') + return True + d2.addCallback(_got_shares) + return d2 + + d = async_iterate(_each_server, self.iterate_servers()) + d.addCallback(lambda ign: f.write('}\n')) + return d + def _created_immutable(ur): # write the generated shares and URI to a file, which can then be # incorporated into this one next time. f.write('immutable_uri = "%s"\n' % ur.get_uri()) f.write('immutable_shares = {\n') - si = uri.from_string(ur.get_uri()).get_storage_index() - si_dir = storage_index_to_dir(si) - for (i,ss,ssdir) in self.iterate_servers(): - sharedir = os.path.join(ssdir, "shares", si_dir) - shares = {} - for fn in os.listdir(sharedir): - shnum = int(fn) - sharedata = open(os.path.join(sharedir, fn), "rb").read() - shares[shnum] = sharedata - fileutil.rm_dir(sharedir) - if shares: - f.write(' %d: { # client[%d]\n' % (i, i)) - for shnum in sorted(shares.keys()): - f.write(' %d: base32.a2b("%s"),\n' % - (shnum, base32.b2a(shares[shnum]))) - f.write(' },\n') - f.write('}\n') - f.write('\n') - + return _write_py(ur.get_uri()) d.addCallback(_created_immutable) d.addCallback(lambda ignored: self.c0.create_mutable_file(mutable_plaintext)) def _created_mutable(n): + f.write('\n') f.write('mutable_uri = "%s"\n' % n.get_uri()) f.write('mutable_shares = {\n') - si = uri.from_string(n.get_uri()).get_storage_index() - si_dir = storage_index_to_dir(si) - for (i,ss,ssdir) in self.iterate_servers(): - sharedir = os.path.join(ssdir, "shares", si_dir) - shares = {} - for fn in os.listdir(sharedir): - shnum = int(fn) - sharedata = open(os.path.join(sharedir, fn), "rb").read() - shares[shnum] = sharedata - fileutil.rm_dir(sharedir) - if shares: - f.write(' %d: { # client[%d]\n' % (i, i)) - for shnum in sorted(shares.keys()): - f.write(' %d: base32.a2b("%s"),\n' % - (shnum, base32.b2a(shares[shnum]))) - f.write(' },\n') - f.write('}\n') - - f.close() + return _write_py(n.get_uri()) d.addCallback(_created_mutable) def _done(ignored): f.close() - d.addCallback(_done) + d.addBoth(_done) return d + def _write_shares(self, fileuri, shares): + si = uri.from_string(fileuri).get_storage_index() + for i in shares: + shares_for_server = shares[i] + for shnum in shares_for_server: + share_dir = self.get_server(i).backend.get_shareset(si)._get_sharedir() + fileutil.make_dirs(share_dir) + fileutil.write(os.path.join(share_dir, str(shnum)), shares_for_server[shnum]) + def load_shares(self, ignored=None): # this uses the data generated by create_shares() to populate the # storage servers with pre-generated shares - si = uri.from_string(immutable_uri).get_storage_index() - si_dir = storage_index_to_dir(si) - for i in immutable_shares: - shares = immutable_shares[i] - for shnum in shares: - dn = os.path.join(self.get_serverdir(i), "shares", si_dir) - fileutil.make_dirs(dn) - fn = os.path.join(dn, str(shnum)) - f = open(fn, "wb") - f.write(shares[shnum]) - f.close() - - si = uri.from_string(mutable_uri).get_storage_index() - si_dir = storage_index_to_dir(si) - for i in mutable_shares: - shares = mutable_shares[i] - for shnum in shares: - dn = os.path.join(self.get_serverdir(i), "shares", si_dir) - fileutil.make_dirs(dn) - fn = os.path.join(dn, str(shnum)) - f = open(fn, "wb") - f.write(shares[shnum]) - f.close() + self._write_shares(immutable_uri, immutable_shares) + self._write_shares(mutable_uri, mutable_shares) def download_immutable(self, ignored=None): n = self.c0.create_node_from_uri(immutable_uri) @@ -188,6 +172,7 @@ class _Base(GridTestMixin, ShouldFailMixin): d.addCallback(_got_data) return d + class DownloadTest(_Base, unittest.TestCase): timeout = 2400 # It takes longer than 240 seconds on Zandr's ARM box. def test_download(self): @@ -210,7 +195,6 @@ class DownloadTest(_Base, unittest.TestCase): self.load_shares() si = uri.from_string(immutable_uri).get_storage_index() - si_dir = storage_index_to_dir(si) n = self.c0.create_node_from_uri(immutable_uri) d = download_to_data(n) @@ -222,13 +206,15 @@ class DownloadTest(_Base, unittest.TestCase): # find the three shares that were used, and delete them. Then # download again, forcing the downloader to fail over to other # shares + d2 = defer.succeed(None) for s in n._cnode._node._shares: for clientnum in immutable_shares: for shnum in immutable_shares[clientnum]: if s._shnum == shnum: - fn = os.path.join(self.get_serverdir(clientnum), - "shares", si_dir, str(shnum)) - os.unlink(fn) + d2.addCallback(lambda ign, clientnum=clientnum, shnum=shnum: + self.get_server(clientnum).backend.get_shareset(si).get_share(shnum)) + d2.addCallback(lambda share: share.unlink()) + return d2 d.addCallback(_clobber_some_shares) d.addCallback(lambda ign: download_to_data(n)) d.addCallback(_got_data) @@ -237,27 +223,29 @@ class DownloadTest(_Base, unittest.TestCase): # delete all but one of the shares that are still alive live_shares = [s for s in n._cnode._node._shares if s.is_alive()] save_me = live_shares[0]._shnum + d2 = defer.succeed(None) for clientnum in immutable_shares: for shnum in immutable_shares[clientnum]: if shnum == save_me: continue - fn = os.path.join(self.get_serverdir(clientnum), - "shares", si_dir, str(shnum)) - if os.path.exists(fn): - os.unlink(fn) + d2.addCallback(lambda ign, clientnum=clientnum, shnum=shnum: + self.get_server(clientnum).backend.get_shareset(si).get_share(shnum)) + def _eb(f): + f.trap(EnvironmentError) + d2.addCallbacks(lambda share: share.unlink(), _eb) + # now the download should fail with NotEnoughSharesError - return self.shouldFail(NotEnoughSharesError, "1shares", None, - download_to_data, n) + d2.addCallback(lambda ign: self.shouldFail(NotEnoughSharesError, "1shares", None, + download_to_data, n)) + return d2 d.addCallback(_clobber_most_shares) def _clobber_all_shares(ign): # delete the last remaining share for clientnum in immutable_shares: for shnum in immutable_shares[clientnum]: - fn = os.path.join(self.get_serverdir(clientnum), - "shares", si_dir, str(shnum)) - if os.path.exists(fn): - os.unlink(fn) + share_dir = self.get_server(clientnum).backend.get_shareset(si)._get_sharedir() + fileutil.remove(os.path.join(share_dir, str(shnum))) # now a new download should fail with NoSharesError. We want a # new ImmutableFileNode so it will forget about the old shares. # If we merely called create_node_from_uri() without first @@ -834,22 +822,22 @@ class DownloadTest(_Base, unittest.TestCase): # will report two shares, and the ShareFinder will handle the # duplicate by attaching both to the same CommonShare instance. si = uri.from_string(immutable_uri).get_storage_index() - si_dir = storage_index_to_dir(si) - sh0_file = [sharefile - for (shnum, serverid, sharefile) - in self.find_uri_shares(immutable_uri) - if shnum == 0][0] - sh0_data = open(sh0_file, "rb").read() - for clientnum in immutable_shares: - if 0 in immutable_shares[clientnum]: - continue - cdir = self.get_serverdir(clientnum) - target = os.path.join(cdir, "shares", si_dir, "0") - outf = open(target, "wb") - outf.write(sh0_data) - outf.close() - d = self.download_immutable() + d = defer.succeed(None) + d.addCallback(lambda ign: self.find_uri_shares(immutable_uri)) + def _duplicate(sharelist): + sh0_file = [sharefile for (shnum, serverid, sharefile) in sharelist + if shnum == 0][0] + sh0_data = fileutil.read(sh0_file) + for clientnum in immutable_shares: + if 0 in immutable_shares[clientnum]: + continue + cdir = self.get_server(clientnum).backend.get_shareset(si)._get_sharedir() + fileutil.make_dirs(cdir) + fileutil.write(os.path.join(cdir, str(shnum)), sh0_data) + d.addCallback(_duplicate) + + d.addCallback(lambda ign: self.download_immutable()) return d def test_verifycap(self): @@ -934,13 +922,13 @@ class Corruption(_Base, unittest.TestCase): log.msg("corrupt %d" % which) def _corruptor(s, debug=False): return s[:which] + chr(ord(s[which])^0x01) + s[which+1:] - self.corrupt_shares_numbered(imm_uri, [0], _corruptor) + return self.corrupt_shares_numbered(imm_uri, [0], _corruptor) def _corrupt_set(self, ign, imm_uri, which, newvalue): log.msg("corrupt %d" % which) def _corruptor(s, debug=False): return s[:which] + chr(newvalue) + s[which+1:] - self.corrupt_shares_numbered(imm_uri, [0], _corruptor) + return self.corrupt_shares_numbered(imm_uri, [0], _corruptor) def test_each_byte(self): # Setting catalog_detection=True performs an exhaustive test of the @@ -951,6 +939,7 @@ class Corruption(_Base, unittest.TestCase): # (since we don't need every byte of the share). That takes 50s to # run on my laptop and doesn't have any actual asserts, so we don't # normally do that. + # XXX this has bitrotted (before v1.8.2) and gives an AttributeError. self.catalog_detection = False self.basedir = "download/Corruption/each_byte" @@ -999,12 +988,10 @@ class Corruption(_Base, unittest.TestCase): d.addCallback(_got_data) return d - d = self.c0.upload(u) def _uploaded(ur): imm_uri = ur.get_uri() - self.shares = self.copy_shares(imm_uri) - d = defer.succeed(None) + # 'victims' is a list of corruption tests to run. Each one flips # the low-order bit of the specified offset in the share file (so # offset=0 is the MSB of the container version, offset=15 is the @@ -1048,23 +1035,32 @@ class Corruption(_Base, unittest.TestCase): [(i, "need-4th") for i in need_4th_victims]) if self.catalog_detection: corrupt_me = [(i, "") for i in range(len(self.sh0_orig))] - for i,expected in corrupt_me: - # All these tests result in a successful download. What we're - # measuring is how many shares the downloader had to use. - d.addCallback(self._corrupt_flip, imm_uri, i) - d.addCallback(_download, imm_uri, i, expected) - d.addCallback(lambda ign: self.restore_all_shares(self.shares)) - d.addCallback(fireEventually) - corrupt_values = [(3, 2, "no-sh0"), - (15, 2, "need-4th"), # share looks v2 - ] - for i,newvalue,expected in corrupt_values: - d.addCallback(self._corrupt_set, imm_uri, i, newvalue) - d.addCallback(_download, imm_uri, i, expected) - d.addCallback(lambda ign: self.restore_all_shares(self.shares)) - d.addCallback(fireEventually) - return d + + d2 = defer.succeed(None) + d2.addCallback(lambda ign: self.copy_shares(imm_uri)) + def _copied(copied_shares): + d3 = defer.succeed(None) + + for i, expected in corrupt_me: + # All these tests result in a successful download. What we're + # measuring is how many shares the downloader had to use. + d3.addCallback(self._corrupt_flip, imm_uri, i) + d3.addCallback(_download, imm_uri, i, expected) + d3.addCallback(lambda ign: self.restore_all_shares(copied_shares)) + d3.addCallback(fireEventually) + corrupt_values = [(3, 2, "no-sh0"), + (15, 2, "need-4th"), # share looks v2 + ] + for i, newvalue, expected in corrupt_values: + d3.addCallback(self._corrupt_set, imm_uri, i, newvalue) + d3.addCallback(_download, imm_uri, i, expected) + d3.addCallback(lambda ign: self.restore_all_shares(copied_shares)) + d3.addCallback(fireEventually) + return d3 + d2.addCallback(_copied) + return d2 d.addCallback(_uploaded) + def _show_results(ign): print print ("of [0:%d], corruption ignored in %s" % @@ -1100,8 +1096,6 @@ class Corruption(_Base, unittest.TestCase): d = self.c0.upload(u) def _uploaded(ur): imm_uri = ur.get_uri() - self.shares = self.copy_shares(imm_uri) - corrupt_me = [(48, "block data", "Last failure: None"), (600+2*32, "block_hashes[2]", "BadHashError"), (376+2*32, "crypttext_hash_tree[2]", "BadHashError"), @@ -1115,25 +1109,31 @@ class Corruption(_Base, unittest.TestCase): assert not n._cnode._node._shares return download_to_data(n) - d = defer.succeed(None) - for i,which,substring in corrupt_me: - # All these tests result in a failed download. - d.addCallback(self._corrupt_flip_all, imm_uri, i) - d.addCallback(lambda ign, which=which, substring=substring: - self.shouldFail(NoSharesError, which, - substring, - _download, imm_uri)) - d.addCallback(lambda ign: self.restore_all_shares(self.shares)) - d.addCallback(fireEventually) - return d + d2 = defer.succeed(None) + d2.addCallback(lambda ign: self.copy_shares(imm_uri)) + def _copied(copied_shares): + d3 = defer.succeed(None) + + for i, which, substring in corrupt_me: + # All these tests result in a failed download. + d3.addCallback(self._corrupt_flip_all, imm_uri, i) + d3.addCallback(lambda ign, which=which, substring=substring: + self.shouldFail(NoSharesError, which, + substring, + _download, imm_uri)) + d3.addCallback(lambda ign: self.restore_all_shares(copied_shares)) + d3.addCallback(fireEventually) + return d3 + d2.addCallback(_copied) + return d2 d.addCallback(_uploaded) - return d def _corrupt_flip_all(self, ign, imm_uri, which): def _corruptor(s, debug=False): return s[:which] + chr(ord(s[which])^0x01) + s[which+1:] - self.corrupt_all_shares(imm_uri, _corruptor) + return self.corrupt_all_shares(imm_uri, _corruptor) + class DownloadV2(_Base, unittest.TestCase): # tests which exercise v2-share code. They first upload a file with @@ -1203,17 +1203,17 @@ class DownloadV2(_Base, unittest.TestCase): d = self.c0.upload(u) def _uploaded(ur): imm_uri = ur.get_uri() - def _do_corrupt(which, newvalue): - def _corruptor(s, debug=False): - return s[:which] + chr(newvalue) + s[which+1:] - self.corrupt_shares_numbered(imm_uri, [0], _corruptor) - _do_corrupt(12+3, 0x00) - n = self.c0.create_node_from_uri(imm_uri) - d = download_to_data(n) - def _got_data(data): - self.failUnlessEqual(data, plaintext) - d.addCallback(_got_data) - return d + which = 12+3 + newvalue = 0x00 + def _corruptor(s, debug=False): + return s[:which] + chr(newvalue) + s[which+1:] + + d2 = defer.succeed(None) + d2.addCallback(lambda ign: self.corrupt_shares_numbered(imm_uri, [0], _corruptor)) + d2.addCallback(lambda ign: self.c0.create_node_from_uri(imm_uri)) + d2.addCallback(lambda n: download_to_data(n)) + d2.addCallback(lambda data: self.failUnlessEqual(data, plaintext)) + return d2 d.addCallback(_uploaded) return d diff --git a/src/allmydata/test/test_encode.py b/src/allmydata/test/test_encode.py index c908e974..c3ad2e9f 100644 --- a/src/allmydata/test/test_encode.py +++ b/src/allmydata/test/test_encode.py @@ -131,7 +131,7 @@ class FakeBucketReaderWriterProxy: d.addCallback(_try) return d - def get_share_hashes(self, at_least_these=()): + def get_share_hashes(self): d = self._start() def _try(unused=None): if self.mode == "bad sharehash": diff --git a/src/allmydata/test/test_hung_server.py b/src/allmydata/test/test_hung_server.py index df7ee664..637c3840 100644 --- a/src/allmydata/test/test_hung_server.py +++ b/src/allmydata/test/test_hung_server.py @@ -1,14 +1,13 @@ -# -*- coding: utf-8 -*- -import os, shutil +import os + from twisted.trial import unittest from twisted.internet import defer -from allmydata import uri + from allmydata.util.consumer import download_to_data from allmydata.immutable import upload from allmydata.mutable.common import UnrecoverableFileError from allmydata.mutable.publish import MutableData -from allmydata.storage.common import storage_index_to_dir from allmydata.test.no_network import GridTestMixin from allmydata.test.common import ShouldFailMixin from allmydata.util.pollmixin import PollMixin @@ -17,9 +16,10 @@ from allmydata.interfaces import NotEnoughSharesError immutable_plaintext = "data" * 10000 mutable_plaintext = "muta" * 10000 + class HungServerDownloadTest(GridTestMixin, ShouldFailMixin, PollMixin, unittest.TestCase): - # Many of these tests take around 60 seconds on François's ARM buildslave: + # Many of these tests take around 60 seconds on Franc,ois's ARM buildslave: # http://tahoe-lafs.org/buildbot/builders/FranXois%20lenny-armv5tel # allmydata.test.test_hung_server.HungServerDownloadTest.test_2_good_8_broken_duplicate_share_fail # once ERRORed after 197 seconds on Midnight Magic's NetBSD buildslave: @@ -29,16 +29,16 @@ class HungServerDownloadTest(GridTestMixin, ShouldFailMixin, PollMixin, timeout = 240 def _break(self, servers): - for (id, ss) in servers: - self.g.break_server(id) + for ss in servers: + self.g.break_server(ss.original.get_serverid()) def _hang(self, servers, **kwargs): - for (id, ss) in servers: - self.g.hang_server(id, **kwargs) + for ss in servers: + self.g.hang_server(ss.original.get_serverid(), **kwargs) def _unhang(self, servers, **kwargs): - for (id, ss) in servers: - self.g.unhang_server(id, **kwargs) + for ss in servers: + self.g.unhang_server(ss.original.get_serverid(), **kwargs) def _hang_shares(self, shnums, **kwargs): # hang all servers who are holding the given shares @@ -50,46 +50,29 @@ class HungServerDownloadTest(GridTestMixin, ShouldFailMixin, PollMixin, hung_serverids.add(i_serverid) def _delete_all_shares_from(self, servers): - serverids = [id for (id, ss) in servers] + serverids = [ss.original.get_serverid() for ss in servers] for (i_shnum, i_serverid, i_sharefile) in self.shares: if i_serverid in serverids: os.unlink(i_sharefile) def _corrupt_all_shares_in(self, servers, corruptor_func): - serverids = [id for (id, ss) in servers] + serverids = [ss.original.get_serverid() for ss in servers] for (i_shnum, i_serverid, i_sharefile) in self.shares: if i_serverid in serverids: - self._corrupt_share((i_shnum, i_sharefile), corruptor_func) + self.corrupt_share((i_shnum, i_serverid, i_sharefile), corruptor_func) def _copy_all_shares_from(self, from_servers, to_server): - serverids = [id for (id, ss) in from_servers] + serverids = [ss.original.get_serverid() for ss in from_servers] for (i_shnum, i_serverid, i_sharefile) in self.shares: if i_serverid in serverids: - self._copy_share((i_shnum, i_sharefile), to_server) - - def _copy_share(self, share, to_server): - (sharenum, sharefile) = share - (id, ss) = to_server - original_server = ss.original.server - shares_dir = os.path.join(original_server.storedir, "shares") - si = uri.from_string(self.uri).get_storage_index() - si_dir = os.path.join(shares_dir, storage_index_to_dir(si)) - if not os.path.exists(si_dir): - os.makedirs(si_dir) - new_sharefile = os.path.join(si_dir, str(sharenum)) - shutil.copy(sharefile, new_sharefile) - self.shares = self.find_uri_shares(self.uri) - # Make sure that the storage server has the share. - self.failUnlessIn((sharenum, original_server.get_nodeid(), new_sharefile), self.shares) - - def _corrupt_share(self, share, corruptor_func): - (sharenum, sharefile) = share - data = open(sharefile, "rb").read() - newdata = corruptor_func(data) - os.unlink(sharefile) - wf = open(sharefile, "wb") - wf.write(newdata) - wf.close() + self.copy_share((i_shnum, i_serverid, i_sharefile), self.uri, + to_server.original.server) + + d = self.find_uri_shares(self.uri) + def _got_shares(shares): + self.shares = shares + d.addCallback(_got_shares) + return d def _set_up(self, mutable, testdir, num_clients=1, num_servers=10): self.mutable = mutable @@ -102,8 +85,8 @@ class HungServerDownloadTest(GridTestMixin, ShouldFailMixin, PollMixin, self.c0 = self.g.clients[0] nm = self.c0.nodemaker - self.servers = sorted([(s.get_serverid(), s.get_rref()) - for s in nm.storage_broker.get_connected_servers()]) + unsorted = [(s.get_serverid(), s.get_rref()) for s in nm.storage_broker.get_connected_servers()] + self.servers = [ss for (id, ss) in sorted(unsorted)] self.servers = self.servers[5:] + self.servers[:5] if mutable: @@ -111,15 +94,18 @@ class HungServerDownloadTest(GridTestMixin, ShouldFailMixin, PollMixin, d = nm.create_mutable_file(uploadable) def _uploaded_mutable(node): self.uri = node.get_uri() - self.shares = self.find_uri_shares(self.uri) d.addCallback(_uploaded_mutable) else: data = upload.Data(immutable_plaintext, convergence="") d = self.c0.upload(data) def _uploaded_immutable(upload_res): self.uri = upload_res.get_uri() - self.shares = self.find_uri_shares(self.uri) d.addCallback(_uploaded_immutable) + + d.addCallback(lambda ign: self.find_uri_shares(self.uri)) + def _got_shares(shares): + self.shares = shares + d.addCallback(_got_shares) return d def _start_download(self): @@ -264,7 +250,7 @@ class HungServerDownloadTest(GridTestMixin, ShouldFailMixin, PollMixin, # stuck-but-not-overdue, and 4 live requests. All 4 live requests # will retire before the download is complete and the ShareFinder # is shut off. That will leave 4 OVERDUE and 1 - # stuck-but-not-overdue, for a total of 5 requests in in + # stuck-but-not-overdue, for a total of 5 requests in # _sf.pending_requests for t in self._sf.overdue_timers.values()[:4]: t.reset(-1.0) diff --git a/src/allmydata/test/test_immutable.py b/src/allmydata/test/test_immutable.py index 963cedae..45450d61 100644 --- a/src/allmydata/test/test_immutable.py +++ b/src/allmydata/test/test_immutable.py @@ -237,7 +237,7 @@ class Test(GridTestMixin, unittest.TestCase, common.ShouldFailMixin): d = self.startup("download_from_only_3_shares_with_good_crypttext_hash") def _corrupt_7(ign): c = common._corrupt_offset_of_block_hashes_to_truncate_crypttext_hashes - self.corrupt_shares_numbered(self.uri, self._shuffled(7), c) + return self.corrupt_shares_numbered(self.uri, self._shuffled(7), c) d.addCallback(_corrupt_7) d.addCallback(self._download_and_check_plaintext) return d @@ -264,7 +264,7 @@ class Test(GridTestMixin, unittest.TestCase, common.ShouldFailMixin): d = self.startup("download_abort_if_too_many_corrupted_shares") def _corrupt_8(ign): c = common._corrupt_sharedata_version_number - self.corrupt_shares_numbered(self.uri, self._shuffled(8), c) + return self.corrupt_shares_numbered(self.uri, self._shuffled(8), c) d.addCallback(_corrupt_8) def _try_download(ign): start_reads = self._count_reads() diff --git a/src/allmydata/test/test_mutable.py b/src/allmydata/test/test_mutable.py index 6ee1b3f8..7c613ec3 100644 --- a/src/allmydata/test/test_mutable.py +++ b/src/allmydata/test/test_mutable.py @@ -17,7 +17,6 @@ from allmydata.test.no_network import GridTestMixin from foolscap.api import eventually, fireEventually from foolscap.logging import log from allmydata.storage_client import StorageFarmBroker -from allmydata.storage.common import storage_index_to_dir from allmydata.scripts import debug from allmydata.mutable.filenode import MutableFileNode, BackoffAgent @@ -1905,7 +1904,7 @@ class Checker(unittest.TestCase, CheckerMixin, PublishMixin): class Repair(unittest.TestCase, PublishMixin, ShouldFailMixin): - def get_shares(self, s): + def get_all_shares(self, s): all_shares = {} # maps (peerid, shnum) to share data for peerid in s._peers: shares = s._peers[peerid] @@ -1915,7 +1914,7 @@ class Repair(unittest.TestCase, PublishMixin, ShouldFailMixin): return all_shares def copy_shares(self, ignored=None): - self.old_shares.append(self.get_shares(self._storage)) + self.old_shares.append(self.get_all_shares(self._storage)) def test_repair_nop(self): self.old_shares = [] @@ -2694,7 +2693,6 @@ class Problems(GridTestMixin, unittest.TestCase, testutil.ShouldFailMixin): nm.create_mutable_file, MutableData("contents")) return d - def test_privkey_query_error(self): # when a servermap is updated with MODE_WRITE, it tries to get the # privkey. Something might go wrong during this query attempt. @@ -2812,12 +2810,10 @@ class Problems(GridTestMixin, unittest.TestCase, testutil.ShouldFailMixin): for share, shnum in [(TEST_1654_SH0, 0), (TEST_1654_SH1, 1)]: sharedata = base64.b64decode(share) - storedir = self.get_serverdir(shnum) - storage_path = os.path.join(storedir, "shares", - storage_index_to_dir(si)) - fileutil.make_dirs(storage_path) - fileutil.write(os.path.join(storage_path, "%d" % shnum), - sharedata) + # This must be a disk backend. + storage_dir = self.get_server(shnum).backend.get_shareset(si)._get_sharedir() + fileutil.make_dirs(storage_dir) + fileutil.write(os.path.join(storage_dir, str(shnum)), sharedata) nm = self.g.clients[0].nodemaker n = nm.create_from_cap(TEST_1654_CAP) @@ -3112,7 +3108,7 @@ class Version(GridTestMixin, unittest.TestCase, testutil.ShouldFailMixin, \ fso = debug.FindSharesOptions() storage_index = base32.b2a(n.get_storage_index()) fso.si_s = storage_index - fso.nodedirs = [unicode(os.path.dirname(os.path.abspath(storedir))) + fso.nodedirs = [os.path.dirname(storedir) for (i,ss,storedir) in self.iterate_servers()] fso.stdout = StringIO() @@ -3120,7 +3116,8 @@ class Version(GridTestMixin, unittest.TestCase, testutil.ShouldFailMixin, \ debug.find_shares(fso) sharefiles = fso.stdout.getvalue().splitlines() expected = self.nm.default_encoding_parameters["n"] - self.failUnlessEqual(len(sharefiles), expected) + self.failUnlessEqual(len(sharefiles), expected, + str((fso.stdout.getvalue(), fso.stderr.getvalue()))) do = debug.DumpOptions() do["filename"] = sharefiles[0] @@ -3145,6 +3142,7 @@ class Version(GridTestMixin, unittest.TestCase, testutil.ShouldFailMixin, \ cso.stderr = StringIO() debug.catalog_shares(cso) shares = cso.stdout.getvalue().splitlines() + self.failIf(len(shares) < 1, shares) oneshare = shares[0] # all shares should be MDMF self.failIf(oneshare.startswith("UNKNOWN"), oneshare) self.failUnless(oneshare.startswith("MDMF"), oneshare) @@ -3730,6 +3728,7 @@ class Interoperability(GridTestMixin, unittest.TestCase, testutil.ShouldFailMixi sdmf_old_shares[9] = "VGFob2UgbXV0YWJsZSBjb250YWluZXIgdjEKdQlEA47ESLbTdKdpLJXCpBxd5OH239tl5hvAiz1dvGdE5rIOpf8cbfxbPcwNF+Y5dM92uBVbmV6KAAAAAAAAB/wAAAAAAAAJ0AAAAAFOWSw7jSx7WXzaMpdleJYXwYsRCV82jNA5oex9m2YhXSnb2POh+vvC1LE1NAfRc9GOb2zQG84Xdsx1Jub2brEeKkyt0sRIttN0p2kslcKkHF3k4fbf22XmAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABamJprL6ecrsOoFKdrXUmWveLq8nzEGDOjFnyK9detI3noX3uyK2MwSnFdAfyN0tuAwoAAAAAAAAAFQAAAAAAAAAVAAABjwAAAo8AAAMXAAADNwAAAAAAAAM+AAAAAAAAB/wwggEgMA0GCSqGSIb3DQEBAQUAA4IBDQAwggEIAoIBAQC1IkainlJF12IBXBQdpRK1zXB7a26vuEYqRmQM09YjC6sQjCs0F2ICk8n9m/2Kw4l16eIEboB2Au9pODCE+u/dEAakEFh4qidTMn61rbGUbsLK8xzuWNW22ezzz9/nPia0HDrulXt51/FYtfnnAuD1RJGXJv/8tDllE9FL/18TzlH4WuB6Fp8FTgv7QdbZAfWJHDGFIpVCJr1XxOCsSZNFJIqGwZnD2lsChiWw5OJDbKd8otqN1hIbfHyMyfMOJ/BzRzvZXaUt4Dv5nf93EmQDWClxShRwpuX/NkZ5B2K9OFonFTbOCexm/MjMAdCBqebKKaiHFkiknUCn9eJQpZ5bAgERgV50VKj+AVTDfgTpqfO2vfo4wrufi6ZBb8QV7hllhUFBjYogQ9C96dnS7skv0s+cqFuUjwMILr5/rsbEmEMGvl0T0ytyAbtlXuowEFVj/YORNknM4yjY72YUtEPTlMpk0Cis7aIgTvu5qWMPER26PMApZuRqiwRsGIkaJIvOVOTHHjFYe3/YzdMkc7OZtqRMfQLtwVl2/zKQQV8b/a9vaT6q3mRLRd4P3esaAFe/+7sR/t+9tmB+a8kxtKM6kmaVQJMbXJZ4aoHGfeLX0m35Rcvu2Bmph7QfSDjk/eaE3q55zYSoGWShmlhlw4Kwg84sMuhmcVhLvo0LovR8bKmbdgABUSzNKiMx0E91q51/WH6ASL0fDEOLef9oxuyBX5F5cpoABojmWkDX3k3FKfgNHIeptE3lxB8HHzxDfSD250psyfNCAAwGsKbMxbmI2NpdTozZ3SICrySwgGkatA1gsDOJmOnTzgAXVnLiODzHiLFAI/MsXcR71fmvb7UghLA1b8pq66KAyl+aopjsD29AKG5hrXt9hLIp6shvfrzaPGIid5C8IxYIrjgBj1YohGgDE0Wua7Lx6Bnad5n91qmHAnwSEJE5YIhQM634omd6cq9Wk4seJCUIn+ucoknrpxp0IR9QMxpKSMRHRUg2K8ZegnY3YqFunRZKCfsq9ufQEKgjZN12AFqi551KPBdn4/3V5HK6xTv0P4robSsE/BvuIfByvRf/W7ZrDx+CFC4EEcsBOACOZCrkhhqd5TkYKbe9RA+vs56+9N5qZGurkxcoKviiyEncxvTuShD65DK/6x6kMDMgQv/EdZDI3x9GtHTnRBYXwDGnPJ19w+q2zC3e2XarbxTGYQIPEC5mYx0gAA0sbjf018NGfwBhl6SB54iGsa8uLvR3jHv6OSRJgwxL6j7P0Ts4Hv2EtO12P0Lv21pwi3JC1O/WviSrKCvrQD5lMHL9Uym3hwFi2zu0mqwZvxOAbGy7kfOPXkLYKOHTZLthzKj3PsdjeceWBfYIvPGKYcd6wDr36d1aXSYS4IWeApTS2AQ2lu0DUcgSefAvsA8NkgOklvJY1cjTMSg6j6cxQo48Bvl8RAWGLbr4h2S/8KwDGxwLsSv0Gop/gnFc3GzCsmL0EkEyHHWkCA8YRXCghfW80KLDV495ff7yF5oiwK56GniqowZ3RG9Jxp5MXoJQgsLV1VMQFMAmsY69yz8eoxRH3wl9L0dMyndLulhWWzNwPMQ2I0yAWdzA/pksVmwTJTFenB3MHCiWc5rEwJ3yofe6NZZnZQrYyL9r1TNnVwfTwRUiykPiLSk4x9Mi6DX7RamDAxc8u3gDVfjPsTOTagBOEGUWlGAL54KE/E6sgCQ5DEAt12chk8AxbjBFLPgV+/idrzS0lZHOL+IVBI9D0i3Bq1yZcSIqcjZB0M3IbxbPm4gLAYOWEiTUN2ecsEHHg9nt6rhgffVoqSbCCFPbpC0xf7WOC3+BQORIZECOCC7cUAciXq3xn+GuxpFE40RWRJeKAK7bBQ21X89ABIXlQFkFddZ9kRvlZ2Pnl0oeF+2pjnZu0Yc2czNfZEQF2P7BKIdLrgMgxG89snxAY8qAYTCKyQw6xTG87wkjDcpy1wzsZLP3WsOuO7cAm7b27xU0jRKq8Cw4d1hDoyRG+RdS53F8RFJzVMaNNYgxU2tfRwUvXpTRXiOheeRVvh25+YGVnjakUXjx/dSDnOw4ETHGHD+7styDkeSfc3BdSZxswzc6OehgMI+xsCxeeRym15QUm9hxvg8X7Bfz/0WulgFwgzrm11TVynZYOmvyHpiZKoqQyQyKahIrfhwuchCr7lMsZ4a+umIkNkKxCLZnI+T7jd+eGFMgKItjz3kTTxRl3IhaJG3LbPmwRUJynMxQKdMi4Uf0qy0U7+i8hIJ9m50QXc+3tw2bwDSbx22XYJ9Wf14gxx5G5SPTb1JVCbhe4fxNt91xIxCow2zk62tzbYfRe6dfmDmgYHkv2PIEtMJZK8iKLDjFfu2ZUxsKT2A5g1q17og6o9MeXeuFS3mzJXJYFQZd+3UzlFR9qwkFkby9mg5y4XSeMvRLOHPt/H/r5SpEqBE6a9MadZYt61FBV152CUEzd43ihXtrAa0XH9HdsiySBcWI1SpM3mv9rRP0DiLjMUzHw/K1D8TE2f07zW4t/9kvE11tFj/NpICixQAAAAA=" sdmf_old_cap = "URI:SSK:gmjgofw6gan57gwpsow6gtrz3e:5adm6fayxmu3e4lkmfvt6lkkfix34ai2wop2ioqr4bgvvhiol3kq" sdmf_old_contents = "This is a test file.\n" + def copy_sdmf_shares(self): # We'll basically be short-circuiting the upload process. servernums = self.g.servers_by_number.keys() @@ -3741,28 +3740,33 @@ class Interoperability(GridTestMixin, unittest.TestCase, testutil.ShouldFailMixi si = cap.get_storage_index() # Now execute each assignment by writing the storage. - for (share, servernum) in assignments: - sharedata = base64.b64decode(self.sdmf_old_shares[share]) - storedir = self.get_serverdir(servernum) - storage_path = os.path.join(storedir, "shares", - storage_index_to_dir(si)) - fileutil.make_dirs(storage_path) - fileutil.write(os.path.join(storage_path, "%d" % share), - sharedata) + for (shnum, servernum) in assignments: + sharedata = base64.b64decode(self.sdmf_old_shares[shnum]) + # This must be a disk backend. + storage_dir = self.get_server(servernum).backend.get_shareset(si)._get_sharedir() + fileutil.make_dirs(storage_dir) + fileutil.write(os.path.join(storage_dir, str(shnum)), sharedata) + # ...and verify that the shares are there. - shares = self.find_uri_shares(self.sdmf_old_cap) - assert len(shares) == 10 + d = self.find_uri_shares(self.sdmf_old_cap) + def _got_shares(shares): + assert len(shares) == 10 + d.addCallback(_got_shares) + return d def test_new_downloader_can_read_old_shares(self): self.basedir = "mutable/Interoperability/new_downloader_can_read_old_shares" self.set_up_grid() - self.copy_sdmf_shares() - nm = self.g.clients[0].nodemaker - n = nm.create_from_cap(self.sdmf_old_cap) - d = n.download_best_version() - d.addCallback(self.failUnlessEqual, self.sdmf_old_contents) + d = self.copy_sdmf_shares() + def _create_node(ign): + nm = self.g.clients[0].nodemaker + return nm.create_from_cap(self.sdmf_old_cap) + d.addCallback(_create_node) + d.addCallback(lambda n: n.download_best_version()) + d.addCallback(lambda res: self.failUnlessEqual(res, self.sdmf_old_contents)) return d + class DifferentEncoding(unittest.TestCase): def setUp(self): self._storage = s = FakeStorage() diff --git a/src/allmydata/test/test_node.py b/src/allmydata/test/test_node.py index 72d6ef8c..41197495 100644 --- a/src/allmydata/test/test_node.py +++ b/src/allmydata/test/test_node.py @@ -97,7 +97,6 @@ class TestCase(testutil.SignalMixin, unittest.TestCase): 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") diff --git a/src/allmydata/test/test_repairer.py b/src/allmydata/test/test_repairer.py index 41bf794f..1849db1a 100644 --- a/src/allmydata/test/test_repairer.py +++ b/src/allmydata/test/test_repairer.py @@ -1,21 +1,26 @@ -# -*- coding: utf-8 -*- + +import random + +from twisted.internet import defer +from twisted.trial import unittest + from allmydata.test import common from allmydata.monitor import Monitor from allmydata import check_results from allmydata.interfaces import NotEnoughSharesError +from allmydata.check_results import CheckAndRepairResults from allmydata.immutable import upload +from allmydata.util import fileutil from allmydata.util.consumer import download_to_data -from twisted.internet import defer -from twisted.trial import unittest -import random from allmydata.test.no_network import GridTestMixin + # We'll allow you to pass this test even if you trigger eighteen times as # many disk reads and block fetches as would be optimal. READ_LEEWAY = 18 MAX_DELTA_READS = 10 * READ_LEEWAY # N = 10 -timeout=240 # François's ARM box timed out after 120 seconds of Verifier.test_corrupt_crypttext_hashtree +timeout=240 # Franc,ois's ARM box timed out after 120 seconds of Verifier.test_corrupt_crypttext_hashtree class RepairTestMixin: def failUnlessIsInstance(self, x, xtype): @@ -86,10 +91,7 @@ class Verifier(GridTestMixin, unittest.TestCase, RepairTestMixin): self.failIfBigger(delta_reads, 0) d.addCallback(_check) - def _remove_all(ignored): - for sh in self.find_uri_shares(self.uri): - self.delete_share(sh) - d.addCallback(_remove_all) + d.addCallback(lambda ign: self.delete_all_shares(self.uri)) d.addCallback(lambda ignored: self._stash_counts()) d.addCallback(lambda ignored: @@ -175,6 +177,7 @@ class Verifier(GridTestMixin, unittest.TestCase, RepairTestMixin): self.basedir = "repairer/Verifier/corrupt_file_verno" return self._help_test_verify(common._corrupt_file_version_number, self.judge_visible_corruption) + test_corrupt_file_verno.todo = "Behaviour changed for corrupted shares; test is probably now invalid." def judge_share_version_incompatibility(self, vr): # corruption of the share version (inside the container, the 1/2 @@ -401,25 +404,22 @@ class Repairer(GridTestMixin, unittest.TestCase, RepairTestMixin, Monitor(), verify=False)) # test share corruption - def _test_corrupt(ignored): + d.addCallback(lambda ign: self.find_uri_shares(self.uri)) + def _test_corrupt(shares): olddata = {} - shares = self.find_uri_shares(self.uri) for (shnum, serverid, sharefile) in shares: - olddata[ (shnum, serverid) ] = open(sharefile, "rb").read() + olddata[ (shnum, serverid) ] = fileutil.read(sharefile) for sh in shares: self.corrupt_share(sh, common._corrupt_uri_extension) for (shnum, serverid, sharefile) in shares: - newdata = open(sharefile, "rb").read() + newdata = fileutil.read(sharefile) self.failIfEqual(olddata[ (shnum, serverid) ], newdata) d.addCallback(_test_corrupt) - def _remove_all(ignored): - for sh in self.find_uri_shares(self.uri): - self.delete_share(sh) - d.addCallback(_remove_all) - d.addCallback(lambda ignored: self.find_uri_shares(self.uri)) - d.addCallback(lambda shares: self.failUnlessEqual(shares, [])) + d.addCallback(lambda ign: self.delete_all_shares(self.uri)) + d.addCallback(lambda ign: self.find_uri_shares(self.uri)) + d.addCallback(lambda shares: self.failUnlessEqual(shares, [])) return d def test_repair_from_deletion_of_1(self): @@ -445,13 +445,12 @@ class Repairer(GridTestMixin, unittest.TestCase, RepairTestMixin, self.failIfBigger(delta_allocates, DELTA_WRITES_PER_SHARE) self.failIf(pre.is_healthy()) self.failUnless(post.is_healthy()) - - # Now we inspect the filesystem to make sure that it has 10 - # shares. - shares = self.find_uri_shares(self.uri) - self.failIf(len(shares) < 10) d.addCallback(_check_results) + # Now we inspect the filesystem to make sure that it has 10 shares. + d.addCallback(lambda ign: self.find_uri_shares(self.uri)) + d.addCallback(lambda shares: self.failIf(len(shares) < 10)) + d.addCallback(lambda ignored: self.c0_filenode.check(Monitor(), verify=True)) d.addCallback(lambda vr: self.failUnless(vr.is_healthy())) @@ -491,12 +490,12 @@ class Repairer(GridTestMixin, unittest.TestCase, RepairTestMixin, self.failIfBigger(delta_allocates, (DELTA_WRITES_PER_SHARE * 7)) self.failIf(pre.is_healthy()) self.failUnless(post.is_healthy(), post.as_dict()) - - # Make sure we really have 10 shares. - shares = self.find_uri_shares(self.uri) - self.failIf(len(shares) < 10) d.addCallback(_check_results) + # Now we inspect the filesystem to make sure that it has 10 shares. + d.addCallback(lambda ign: self.find_uri_shares(self.uri)) + d.addCallback(lambda shares: self.failIf(len(shares) < 10)) + d.addCallback(lambda ignored: self.c0_filenode.check(Monitor(), verify=True)) d.addCallback(lambda vr: self.failUnless(vr.is_healthy())) @@ -526,7 +525,7 @@ class Repairer(GridTestMixin, unittest.TestCase, RepairTestMixin, # happiness setting. def _delete_some_servers(ignored): for i in xrange(7): - self.g.remove_server(self.g.servers_by_number[i].my_nodeid) + self.remove_server(i) assert len(self.g.servers_by_number) == 3 @@ -619,8 +618,8 @@ class Repairer(GridTestMixin, unittest.TestCase, RepairTestMixin, # are two shares that it should upload, if the server fails # to serve the first share. self.failIf(after_repair_allocates - before_repair_allocates > (DELTA_WRITES_PER_SHARE * 2), (after_repair_allocates, before_repair_allocates)) - self.failIf(prerepairres.is_healthy(), (prerepairres.data, corruptor_func)) - self.failUnless(postrepairres.is_healthy(), (postrepairres.data, corruptor_func)) + self.failIf(prerepairres.is_healthy(), (prerepairres.get_data(), corruptor_func)) + self.failUnless(postrepairres.is_healthy(), (postrepairres.get_data(), corruptor_func)) # Now we inspect the filesystem to make sure that it has 10 # shares. @@ -710,14 +709,17 @@ class Repairer(GridTestMixin, unittest.TestCase, RepairTestMixin, # Cause one of the servers to not respond during the pre-repair # filecheck, but then *do* respond to the post-repair filecheck. ss = self.g.servers_by_number[0] - self.g.break_server(ss.get_nodeid(), count=1) + self.g.break_server(ss.get_serverid(), count=1) - shares = self.find_uri_shares(self.uri) + return self.find_uri_shares(self.uri) + d.addCallback(_then) + def _got_shares(shares): self.failUnlessEqual(len(shares), 10) self.delete_shares_numbered(self.uri, [9]) return self.c0_filenode.check_and_repair(Monitor()) - d.addCallback(_then) + d.addCallback(_got_shares) def _check(rr): + self.failUnlessIsInstance(rr, CheckAndRepairResults) prr = rr.get_post_repair_results() # We expect the repair to have restored all shares... diff --git a/src/allmydata/test/test_storage.py b/src/allmydata/test/test_storage.py index 0e82cc7f..ce97fe76 100644 --- a/src/allmydata/test/test_storage.py +++ b/src/allmydata/test/test_storage.py @@ -1,22 +1,31 @@ -import time, os.path, platform, stat, re, simplejson, struct, shutil -import mock +import time, os.path, platform, re, simplejson, struct, itertools +from collections import deque +import mock from twisted.trial import unittest -from twisted.internet import defer, reactor -from twisted.application import service -from foolscap.api import fireEventually -import itertools +from twisted.internet import defer +from allmydata.util.deferredutil import for_items + +from twisted.python.failure import Failure +from foolscap.logging.log import OPERATIONAL, INFREQUENT, WEIRD +from foolscap.logging.web import LogEvent from allmydata import interfaces from allmydata.util import fileutil, hashutil, base32, time_format from allmydata.storage.server import StorageServer -from allmydata.storage.backends.disk.mutable import MutableShareFile -from allmydata.storage.backends.disk.immutable import ShareFile +from allmydata.storage.backends.null.null_backend import NullBackend +from allmydata.storage.backends.disk.disk_backend import DiskBackend +from allmydata.storage.backends.disk.immutable import load_immutable_disk_share, \ + create_immutable_disk_share, ImmutableDiskShare +from allmydata.storage.backends.disk.mutable import create_mutable_disk_share, MutableDiskShare +from allmydata.storage.backends.cloud.cloud_backend import CloudBackend +from allmydata.storage.backends.cloud import mock_cloud, cloud_common +from allmydata.storage.backends.cloud.mock_cloud import MockContainer, MockServiceError, \ + ContainerItem, ContainerListing from allmydata.storage.bucket import BucketWriter, BucketReader -from allmydata.storage.common import DataTooLargeError, storage_index_to_dir, \ - UnknownMutableContainerVersionError, UnknownImmutableContainerVersionError +from allmydata.storage.common import DataTooLargeError, storage_index_to_dir from allmydata.storage.leasedb import SHARETYPE_IMMUTABLE, SHARETYPE_MUTABLE from allmydata.storage.expiration import ExpirationPolicy from allmydata.immutable.layout import WriteBucketProxy, WriteBucketProxy_v2, \ @@ -29,17 +38,18 @@ from allmydata.mutable.layout import MDMFSlotWriteProxy, MDMFSlotReadProxy, \ SIGNATURE_SIZE, \ VERIFICATION_KEY_SIZE, \ SHARE_HASH_CHAIN_SIZE -from allmydata.interfaces import BadWriteEnablerError -from allmydata.test.common import LoggingServiceParent, ShouldFailMixin, CrawlerTestMixin +from allmydata.interfaces import BadWriteEnablerError, RIStorageServer +from allmydata.test.common import LoggingServiceParent, ShouldFailMixin, CrawlerTestMixin, \ + FakeCanary from allmydata.test.common_util import ReallyEqualMixin from allmydata.test.common_web import WebRenderingMixin from allmydata.test.no_network import NoNetworkServer from allmydata.web.storage import StorageStatus, remove_prefix -class Marker: - pass class FakeAccount: + def __init__(self, server): + self.server = server def add_share(self, storage_index, shnum, used_space, sharetype, commit=True): pass def add_or_renew_default_lease(self, storage_index, shnum, commit=True): @@ -47,21 +57,6 @@ class FakeAccount: def mark_share_as_stable(self, storage_index, shnum, used_space, commit=True): pass -class FakeCanary: - def __init__(self, ignore_disconnectors=False): - self.ignore = ignore_disconnectors - self.disconnectors = {} - def notifyOnDisconnect(self, f, *args, **kwargs): - if self.ignore: - return - m = Marker() - self.disconnectors[m] = (f, args, kwargs) - return m - def dontNotifyOnDisconnect(self, marker): - if self.ignore: - return - del self.disconnectors[marker] - class FakeStatsProvider: def count(self, name, delta=1): pass @@ -69,46 +64,88 @@ class FakeStatsProvider: pass -class BucketTestMixin: +class ServiceParentMixin: + def setUp(self): + self.sparent = LoggingServiceParent() + self.sparent.startService() + self._lease_secret = itertools.count() + + def tearDown(self): + return self.sparent.stopService() + + +class WorkdirMixin: + def workdir(self, name): + return os.path.join("storage", self.__class__.__name__, name) + + +class BucketTestMixin(WorkdirMixin): + def make_workdir(self, name): + basedir = self.workdir(name) + tmpdir = os.path.join(basedir, "tmp") + incoming = os.path.join(tmpdir, "bucket") + final = os.path.join(basedir, "bucket") + fileutil.make_dirs(tmpdir) + return incoming, final + def bucket_writer_closed(self, bw, consumed): pass + def add_latency(self, category, latency): pass + def count(self, name, delta=1): pass class Bucket(BucketTestMixin, unittest.TestCase): - def make_workdir(self, name): - basedir = os.path.join("storage", "Bucket", name) - incoming = os.path.join(basedir, "tmp", "bucket") - final = os.path.join(basedir, "bucket") - fileutil.make_dirs(basedir) - fileutil.make_dirs(os.path.join(basedir, "tmp")) - return incoming, final - def test_create(self): incoming, final = self.make_workdir("test_create") - bw = BucketWriter(self, FakeAccount(), "si1", 0, incoming, final, 200, FakeCanary()) - bw.remote_write(0, "a"*25) - bw.remote_write(25, "b"*25) - bw.remote_write(50, "c"*25) - bw.remote_write(75, "d"*7) - bw.remote_close() + account = FakeAccount(self) + d = defer.succeed(None) + d.addCallback(lambda ign: create_immutable_disk_share(incoming, final, allocated_data_length=200, + storage_index="si1", shnum=0)) + def _got_share(share): + bw = BucketWriter(account, share, FakeCanary()) + d2 = defer.succeed(None) + d2.addCallback(lambda ign: bw.remote_write(0, "a"*25)) + d2.addCallback(lambda ign: bw.remote_write(25, "b"*25)) + d2.addCallback(lambda ign: bw.remote_write(50, "c"*25)) + d2.addCallback(lambda ign: bw.remote_write(75, "d"*7)) + d2.addCallback(lambda ign: bw.remote_close()) + return d2 + d.addCallback(_got_share) + return d def test_readwrite(self): incoming, final = self.make_workdir("test_readwrite") - bw = BucketWriter(self, FakeAccount(), "si1", 0, incoming, final, 200, FakeCanary()) - bw.remote_write(0, "a"*25) - bw.remote_write(25, "b"*25) - bw.remote_write(50, "c"*7) # last block may be short - bw.remote_close() - - # now read from it - br = BucketReader(self, bw.finalhome) - self.failUnlessEqual(br.remote_read(0, 25), "a"*25) - self.failUnlessEqual(br.remote_read(25, 25), "b"*25) - self.failUnlessEqual(br.remote_read(50, 7 ), "c"*7 ) + account = FakeAccount(self) + d = defer.succeed(None) + d.addCallback(lambda ign: create_immutable_disk_share(incoming, final, allocated_data_length=200, + storage_index="si1", shnum=0)) + def _got_share(share): + bw = BucketWriter(account, share, FakeCanary()) + d2 = defer.succeed(None) + d2.addCallback(lambda ign: bw.remote_write(0, "a"*25)) + d2.addCallback(lambda ign: bw.remote_write(25, "b"*25)) + d2.addCallback(lambda ign: bw.remote_write(50, "c"*7)) # last block may be short + d2.addCallback(lambda ign: bw.remote_close()) + + # now read from it + def _read(ign): + br = BucketReader(account, share) + d3 = defer.succeed(None) + d3.addCallback(lambda ign: br.remote_read(0, 25)) + d3.addCallback(lambda res: self.failUnlessEqual(res, "a"*25)) + d3.addCallback(lambda ign: br.remote_read(25, 25)) + d3.addCallback(lambda res: self.failUnlessEqual(res, "b"*25)) + d3.addCallback(lambda ign: br.remote_read(50, 7)) + d3.addCallback(lambda res: self.failUnlessEqual(res, "c"*7)) + return d3 + d2.addCallback(_read) + return d2 + d.addCallback(_got_share) + return d def test_read_past_end_of_share_data(self): # test vector for immutable files (hard-coded contents of an immutable share @@ -121,23 +158,35 @@ class Bucket(BucketTestMixin, unittest.TestCase): # -- see allmydata/immutable/layout.py . This test, which is # simulating a client, just sends 'a'. share_data = 'a' - extra_data = 'b' * ShareFile.LEASE_SIZE + extra_data = 'b' * ImmutableDiskShare.LEASE_SIZE share_file_data = containerdata + share_data + extra_data incoming, final = self.make_workdir("test_read_past_end_of_share_data") fileutil.write(final, share_file_data) - - mockstorageserver = mock.Mock() - - # Now read from it. - br = BucketReader(mockstorageserver, final) - - self.failUnlessEqual(br.remote_read(0, len(share_data)), share_data) - - # Read past the end of share data by 1 byte. - result_of_read = br.remote_read(0, len(share_data)+1) - self.failUnlessEqual(result_of_read, share_data) + d = defer.succeed(None) + d.addCallback(lambda ign: load_immutable_disk_share(final)) + def _got_share(share): + mockstorageserver = mock.Mock() + account = FakeAccount(mockstorageserver) + + # Now read from it. + br = BucketReader(account, share) + + d2 = br.remote_read(0, len(share_data)) + d2.addCallback(lambda res: self.failUnlessEqual(res, share_data)) + + # Read past the end of share data to get the cancel secret. + read_length = len(share_data) + len(extra_data) + d2.addCallback(lambda ign: br.remote_read(0, read_length)) + d2.addCallback(lambda res: self.failUnlessEqual(res, share_data)) + + # Read past the end of share data by 1 byte. + d2.addCallback(lambda ign: br.remote_read(0, len(share_data)+1)) + d2.addCallback(lambda res: self.failUnlessEqual(res, share_data)) + return d2 + d.addCallback(_got_share) + return d class RemoteBucket: @@ -160,26 +209,32 @@ class RemoteBucket: class BucketProxy(BucketTestMixin, unittest.TestCase): def make_bucket(self, name, size): - basedir = os.path.join("storage", "BucketProxy", name) - incoming = os.path.join(basedir, "tmp", "bucket") - final = os.path.join(basedir, "bucket") - fileutil.make_dirs(basedir) - fileutil.make_dirs(os.path.join(basedir, "tmp")) - si = "si1" - bw = BucketWriter(self, FakeAccount(), si, 0, incoming, final, size, FakeCanary()) - rb = RemoteBucket() - rb.target = bw - return bw, rb, final + incoming, final = self.make_workdir(name) + account = FakeAccount(self) + + d = defer.succeed(None) + d.addCallback(lambda ign: create_immutable_disk_share(incoming, final, size, + storage_index="si1", shnum=0)) + def _got_share(share): + bw = BucketWriter(account, share, FakeCanary()) + rb = RemoteBucket() + rb.target = bw + return bw, rb, final + d.addCallback(_got_share) + return d def test_create(self): - bw, rb, sharefname = self.make_bucket("test_create", 500) - bp = WriteBucketProxy(rb, None, - data_size=300, - block_size=10, - num_segments=5, - num_share_hashes=3, - uri_extension_size_max=500) - self.failUnless(interfaces.IStorageBucketWriter.providedBy(bp), bp) + d = self.make_bucket("test_create", 500) + def _made_bucket( (bw, rb, sharefile) ): + bp = WriteBucketProxy(rb, None, + data_size=300, + block_size=10, + num_segments=5, + num_share_hashes=3, + uri_extension_size_max=500) + self.failUnless(interfaces.IStorageBucketWriter.providedBy(bp), bp) + d.addCallback(_made_bucket) + return d def _do_test_readwrite(self, name, header_size, wbp_class, rbp_class): # Let's pretend each share has 100 bytes of data, and that there are @@ -203,28 +258,33 @@ class BucketProxy(BucketTestMixin, unittest.TestCase): for i in (1,9,13)] uri_extension = "s" + "E"*498 + "e" - bw, rb, sharefname = self.make_bucket(name, sharesize) - bp = wbp_class(rb, None, - data_size=95, - block_size=25, - num_segments=4, - num_share_hashes=3, - uri_extension_size_max=len(uri_extension)) - - d = bp.put_header() - d.addCallback(lambda res: bp.put_block(0, "a"*25)) - d.addCallback(lambda res: bp.put_block(1, "b"*25)) - d.addCallback(lambda res: bp.put_block(2, "c"*25)) - d.addCallback(lambda res: bp.put_block(3, "d"*20)) - d.addCallback(lambda res: bp.put_crypttext_hashes(crypttext_hashes)) - d.addCallback(lambda res: bp.put_block_hashes(block_hashes)) - d.addCallback(lambda res: bp.put_share_hashes(share_hashes)) - d.addCallback(lambda res: bp.put_uri_extension(uri_extension)) - d.addCallback(lambda res: bp.close()) + d = self.make_bucket(name, sharesize) + def _made_bucket( (bw, rb, sharefile) ): + bp = wbp_class(rb, None, + data_size=95, + block_size=25, + num_segments=4, + num_share_hashes=3, + uri_extension_size_max=len(uri_extension)) + + d2 = bp.put_header() + d2.addCallback(lambda ign: bp.put_block(0, "a"*25)) + d2.addCallback(lambda ign: bp.put_block(1, "b"*25)) + d2.addCallback(lambda ign: bp.put_block(2, "c"*25)) + d2.addCallback(lambda ign: bp.put_block(3, "d"*20)) + d2.addCallback(lambda ign: bp.put_crypttext_hashes(crypttext_hashes)) + d2.addCallback(lambda ign: bp.put_block_hashes(block_hashes)) + d2.addCallback(lambda ign: bp.put_share_hashes(share_hashes)) + d2.addCallback(lambda ign: bp.put_uri_extension(uri_extension)) + d2.addCallback(lambda ign: bp.close()) + + d2.addCallback(lambda ign: load_immutable_disk_share(sharefile)) + return d2 + d.addCallback(_made_bucket) # now read everything back - def _start_reading(res): - br = BucketReader(self, sharefname) + def _start_reading(share): + br = BucketReader(FakeAccount(self), share) rb = RemoteBucket() rb.target = br server = NoNetworkServer("abc", None) @@ -232,30 +292,26 @@ class BucketProxy(BucketTestMixin, unittest.TestCase): self.failUnlessIn("to peer", repr(rbp)) self.failUnless(interfaces.IStorageBucketReader.providedBy(rbp), rbp) - d1 = rbp.get_block_data(0, 25, 25) - d1.addCallback(lambda res: self.failUnlessEqual(res, "a"*25)) - d1.addCallback(lambda res: rbp.get_block_data(1, 25, 25)) - d1.addCallback(lambda res: self.failUnlessEqual(res, "b"*25)) - d1.addCallback(lambda res: rbp.get_block_data(2, 25, 25)) - d1.addCallback(lambda res: self.failUnlessEqual(res, "c"*25)) - d1.addCallback(lambda res: rbp.get_block_data(3, 25, 20)) - d1.addCallback(lambda res: self.failUnlessEqual(res, "d"*20)) - - d1.addCallback(lambda res: rbp.get_crypttext_hashes()) - d1.addCallback(lambda res: - self.failUnlessEqual(res, crypttext_hashes)) - d1.addCallback(lambda res: rbp.get_block_hashes(set(range(4)))) - d1.addCallback(lambda res: self.failUnlessEqual(res, block_hashes)) - d1.addCallback(lambda res: rbp.get_share_hashes()) - d1.addCallback(lambda res: self.failUnlessEqual(res, share_hashes)) - d1.addCallback(lambda res: rbp.get_uri_extension()) - d1.addCallback(lambda res: - self.failUnlessEqual(res, uri_extension)) - - return d1 - + d2 = defer.succeed(None) + d2.addCallback(lambda ign: rbp.get_block_data(0, 25, 25)) + d2.addCallback(lambda res: self.failUnlessEqual(res, "a"*25)) + d2.addCallback(lambda ign: rbp.get_block_data(1, 25, 25)) + d2.addCallback(lambda res: self.failUnlessEqual(res, "b"*25)) + d2.addCallback(lambda ign: rbp.get_block_data(2, 25, 25)) + d2.addCallback(lambda res: self.failUnlessEqual(res, "c"*25)) + d2.addCallback(lambda ign: rbp.get_block_data(3, 25, 20)) + d2.addCallback(lambda res: self.failUnlessEqual(res, "d"*20)) + + d2.addCallback(lambda ign: rbp.get_crypttext_hashes()) + d2.addCallback(lambda res: self.failUnlessEqual(res, crypttext_hashes)) + d2.addCallback(lambda ign: rbp.get_block_hashes(set(range(4)))) + d2.addCallback(lambda res: self.failUnlessEqual(res, block_hashes)) + d2.addCallback(lambda ign: rbp.get_share_hashes()) + d2.addCallback(lambda res: self.failUnlessEqual(res, share_hashes)) + d2.addCallback(lambda ign: rbp.get_uri_extension()) + d2.addCallback(lambda res: self.failUnlessEqual(res, uri_extension)) + return d2 d.addCallback(_start_reading) - return d def test_readwrite_v1(self): @@ -266,30 +322,111 @@ class BucketProxy(BucketTestMixin, unittest.TestCase): return self._do_test_readwrite("test_readwrite_v2", 0x44, WriteBucketProxy_v2, ReadBucketProxy) -class Server(unittest.TestCase): - def setUp(self): - self.sparent = LoggingServiceParent() - self.sparent.startService() - self._lease_secret = itertools.count() - def tearDown(self): - return self.sparent.stopService() +class Seek(unittest.TestCase, WorkdirMixin): + def test_seek(self): + basedir = self.workdir("test_seek") + fileutil.make_dirs(basedir) + filename = os.path.join(basedir, "testfile") + fileutil.write(filename, "start") + # mode="w" allows seeking-to-create-holes, but truncates pre-existing + # files. mode="a" preserves previous contents but does not allow + # seeking-to-create-holes. mode="r+" allows both. + f = open(filename, "rb+") + try: + f.seek(100) + f.write("100") + finally: + f.close() - def workdir(self, name): - basedir = os.path.join("storage", "Server", name) - return basedir + filelen = os.stat(filename).st_size + self.failUnlessEqual(filelen, 100+3) + f2 = open(filename, "rb") + try: + self.failUnlessEqual(f2.read(5), "start") + finally: + f2.close() - def create(self, name, reserved_space=0, klass=StorageServer): - workdir = self.workdir(name) - server = klass(workdir, "\x00" * 20, reserved_space=reserved_space, - stats_provider=FakeStatsProvider()) - server.setServiceParent(self.sparent) - return server + +class CloudCommon(unittest.TestCase, ShouldFailMixin, WorkdirMixin): + def test_concat(self): + x = deque([[1, 2], (), xrange(3, 6)]) + self.failUnlessEqual(cloud_common.concat(x), [1, 2, 3, 4, 5]) + + def test_list_objects_truncated_badly(self): + # If a container misbehaves by not producing listings with increasing keys, + # that should cause an incident. + basedir = self.workdir("test_list_objects_truncated_badly") + fileutil.make_dirs(basedir) + + class BadlyTruncatingMockContainer(MockContainer): + def _list_some_objects(self, container_name, prefix='', marker=None): + contents = [ContainerItem("", None, "", 0, None, None)] + return defer.succeed(ContainerListing(container_name, "", "", 0, "true", contents)) + + s = {"level": 0} + def call_log_msg(*args, **kwargs): + s["level"] = max(s["level"], kwargs["level"]) + self.patch(cloud_common.log, 'msg', call_log_msg) + + container = BadlyTruncatingMockContainer(basedir) + d = self.shouldFail(AssertionError, + 'truncated badly', "Not making progress in list_objects", + lambda: container.list_objects(prefix="")) + d.addCallback(lambda ign: self.failUnless(s["level"] >= WEIRD, s["level"])) + return d + + def test_cloud_share_base(self): + basedir = self.workdir("test_cloud_share_base") + fileutil.make_dirs(basedir) + + container = MockContainer(basedir) + base = cloud_common.CloudShareBase(container, "si1", 1) + base._data_length = 42 + base._total_size = 100 + + self.failUnlessIn("CloudShareBase", repr(base)) + self.failUnlessEqual(base.get_storage_index(), "si1") + self.failUnlessEqual(base.get_storage_index_string(), "onutc") + self.failUnlessEqual(base.get_shnum(), 1) + self.failUnlessEqual(base.get_data_length(), 42) + self.failUnlessEqual(base.get_size(), 100) + self.failUnlessEqual(os.path.normpath(base._get_path()), + os.path.normpath(os.path.join(basedir, "shares", "on", "onutc", "1"))) + + # TODO: test cloud_common.delete_chunks + + +class ServerMixin: + def allocate(self, account, storage_index, sharenums, size, canary=None): + # These secrets are not used, but clients still provide them. + renew_secret = hashutil.tagged_hash("blah", "%d" % self._lease_secret.next()) + cancel_secret = hashutil.tagged_hash("blah", "%d" % self._lease_secret.next()) + if not canary: + canary = FakeCanary() + return defer.maybeDeferred(account.remote_allocate_buckets, + storage_index, renew_secret, cancel_secret, + sharenums, size, canary) + + def _write_and_close(self, ign, i, bw): + d = defer.succeed(None) + d.addCallback(lambda ign: bw.remote_write(0, "%25d" % i)) + d.addCallback(lambda ign: bw.remote_close()) + return d + + def _close_writer(self, ign, i, bw): + return bw.remote_close() + + def _abort_writer(self, ign, i, bw): + return bw.remote_abort() +class ServerTest(ServerMixin, ShouldFailMixin): def test_create(self): - self.create("test_create") + server = self.create("test_create") + aa = server.get_accountant().get_anonymous_account() + self.failUnless(RIStorageServer.providedBy(aa), aa) def test_declares_fixed_1528(self): server = self.create("test_declares_fixed_1528") @@ -299,6 +436,16 @@ class Server(unittest.TestCase): sv1 = ver['http://allmydata.org/tahoe/protocols/storage/v1'] self.failUnless(sv1.get('prevents-read-past-end-of-share-data'), sv1) + def test_has_immutable_readv(self): + server = self.create("test_has_immutable_readv") + aa = server.get_accountant().get_anonymous_account() + + ver = aa.remote_get_version() + sv1 = ver['http://allmydata.org/tahoe/protocols/storage/v1'] + self.failUnless(sv1.get('has-immutable-readv'), sv1) + + # TODO: test that we actually support it + def test_declares_maximum_share_sizes(self): server = self.create("test_declares_maximum_share_sizes") aa = server.get_accountant().get_anonymous_account() @@ -308,39 +455,31 @@ class Server(unittest.TestCase): self.failUnlessIn('maximum-immutable-share-size', sv1) self.failUnlessIn('maximum-mutable-share-size', sv1) - def allocate(self, aa, storage_index, sharenums, size, canary=None): - renew_secret = hashutil.tagged_hash("blah", "%d" % self._lease_secret.next()) - cancel_secret = hashutil.tagged_hash("blah", "%d" % self._lease_secret.next()) - if not canary: - canary = FakeCanary() - return aa.remote_allocate_buckets(storage_index, - renew_secret, cancel_secret, - sharenums, size, canary) - - def test_large_share(self): - syslow = platform.system().lower() - if 'cygwin' in syslow or 'windows' in syslow or 'darwin' in syslow: - raise unittest.SkipTest("If your filesystem doesn't support efficient sparse files then it is very expensive (Mac OS X and Windows don't support efficient sparse files).") - - avail = fileutil.get_available_space('.', 512*2**20) - if avail <= 4*2**30: - raise unittest.SkipTest("This test will spuriously fail if you have less than 4 GiB free on your filesystem.") - - server = self.create("test_large_share") + def test_create_share(self): + server = self.create("test_create_share") + backend = server.backend aa = server.get_accountant().get_anonymous_account() - already,writers = self.allocate(aa, "allocate", [0], 2**32+2) - self.failUnlessEqual(already, set()) - self.failUnlessEqual(set(writers.keys()), set([0])) - - shnum, bucket = writers.items()[0] - # This test is going to hammer your filesystem if it doesn't make a sparse file for this. :-( - bucket.remote_write(2**32, "ab") - bucket.remote_close() - - readers = aa.remote_get_buckets("allocate") - reader = readers[shnum] - self.failUnlessEqual(reader.remote_read(2**32, 2), "ab") + d = self.allocate(aa, "si1", [0], 75) + def _allocated( (already, writers) ): + self.failUnlessEqual(already, set()) + self.failUnlessEqual(set(writers.keys()), set([0])) + + d2 = defer.succeed(None) + d2.addCallback(lambda ign: writers[0].remote_write(0, "data")) + d2.addCallback(lambda ign: writers[0].remote_close()) + + d2.addCallback(lambda ign: backend.get_shareset("si1").get_share(0)) + d2.addCallback(lambda share: self.failUnless(interfaces.IShareForReading.providedBy(share))) + + d2.addCallback(lambda ign: backend.get_shareset("si1").get_shares()) + def _check( (shares, corrupted) ): + self.failUnlessEqual(len(shares), 1, str(shares)) + self.failUnlessEqual(len(corrupted), 0, str(corrupted)) + d2.addCallback(_check) + return d2 + d.addCallback(_allocated) + return d def test_dont_overfill_dirs(self): """ @@ -350,58 +489,28 @@ class Server(unittest.TestCase): """ server = self.create("test_dont_overfill_dirs") aa = server.get_accountant().get_anonymous_account() - - already, writers = self.allocate(aa, "storageindex", [0], 10) - for i, wb in writers.items(): - wb.remote_write(0, "%10d" % i) - wb.remote_close() storedir = os.path.join(self.workdir("test_dont_overfill_dirs"), "shares") - children_of_storedir = set(os.listdir(storedir)) - - # Now store another one under another storageindex that has leading - # chars the same as the first storageindex. - already, writers = self.allocate(aa, "storageindey", [0], 10) - for i, wb in writers.items(): - wb.remote_write(0, "%10d" % i) - wb.remote_close() - storedir = os.path.join(self.workdir("test_dont_overfill_dirs"), - "shares") - new_children_of_storedir = set(os.listdir(storedir)) - self.failUnlessEqual(children_of_storedir, new_children_of_storedir) - - def test_remove_incoming(self): - server = self.create("test_remove_incoming") - aa = server.get_accountant().get_anonymous_account() - - already, writers = self.allocate(aa, "vid", range(3), 10) - for i,wb in writers.items(): - wb.remote_write(0, "%10d" % i) - wb.remote_close() - incoming_share_dir = wb.incominghome - incoming_bucket_dir = os.path.dirname(incoming_share_dir) - incoming_prefix_dir = os.path.dirname(incoming_bucket_dir) - incoming_dir = os.path.dirname(incoming_prefix_dir) - self.failIf(os.path.exists(incoming_bucket_dir), incoming_bucket_dir) - self.failIf(os.path.exists(incoming_prefix_dir), incoming_prefix_dir) - self.failUnless(os.path.exists(incoming_dir), incoming_dir) - - def test_abort(self): - # remote_abort, when called on a writer, should make sure that - # the allocated size of the bucket is not counted by the storage - # server when accounting for space. - server = self.create("test_abort") - aa = server.get_accountant().get_anonymous_account() - - already, writers = self.allocate(aa, "allocate", [0, 1, 2], 150) - self.failIfEqual(server.allocated_size(), 0) - # Now abort the writers. - for writer in writers.itervalues(): - writer.remote_abort() - self.failUnlessEqual(server.allocated_size(), 0) + def _write_and_get_children( (already, writers) ): + d = for_items(self._write_and_close, writers) + d.addCallback(lambda ign: sorted(fileutil.listdir(storedir))) + return d + + d = self.allocate(aa, "storageindex", [0], 25) + d.addCallback(_write_and_get_children) + + def _got_children(children_of_storedir): + # Now store another one under another storageindex that has leading + # chars the same as the first storageindex. + d2 = self.allocate(aa, "storageindey", [0], 25) + d2.addCallback(_write_and_get_children) + d2.addCallback(lambda res: self.failUnlessEqual(res, children_of_storedir)) + return d2 + d.addCallback(_got_children) + return d - def test_allocate(self): + def OFF_test_allocate(self): server = self.create("test_allocate") aa = server.get_accountant().get_anonymous_account() @@ -455,130 +564,210 @@ class Server(unittest.TestCase): for i,wb in writers.items(): wb.remote_abort() - def test_bad_container_version(self): - server = self.create("test_bad_container_version") + # The following share file content was generated with + # storage.immutable.ShareFile from Tahoe-LAFS v1.8.2 + # with share data == 'a'. The total size of this input + # is 85 bytes. + shareversionnumber = '\x00\x00\x00\x01' + sharedatalength = '\x00\x00\x00\x01' + numberofleases = '\x00\x00\x00\x01' + shareinputdata = 'a' + ownernumber = '\x00\x00\x00\x00' + renewsecret = 'x'*32 + cancelsecret = 'y'*32 + expirationtime = '\x00(\xde\x80' + nextlease = '' + containerdata = shareversionnumber + sharedatalength + numberofleases + client_data = (shareinputdata + ownernumber + renewsecret + + cancelsecret + expirationtime + nextlease) + share_data = containerdata + client_data + testnodeid = 'testnodeidxxxxxxxxxx' + + def test_write_and_read_share(self): + """ + Write a new share, read it, and test the server and backends' + handling of simultaneous and successive attempts to write the same + share. + """ + server = self.create("test_write_and_read_share") aa = server.get_accountant().get_anonymous_account() + canary = FakeCanary() - a,w = self.allocate(aa, "si1", [0], 10) - w[0].remote_write(0, "\xff"*10) - w[0].remote_close() - - fn = os.path.join(server.sharedir, storage_index_to_dir("si1"), "0") - f = open(fn, "rb+") - f.seek(0) - f.write(struct.pack(">L", 0)) # this is invalid: minimum used is v1 - f.close() - - aa.remote_get_buckets("allocate") - - e = self.failUnlessRaises(UnknownImmutableContainerVersionError, - aa.remote_get_buckets, "si1") - self.failUnlessIn(" had version 0 but we wanted 1", str(e)) + shareset = server.backend.get_shareset('teststorage_index') + self.failIf(shareset.has_incoming(0)) + + # Populate incoming with the sharenum: 0. + d = aa.remote_allocate_buckets('teststorage_index', 'x'*32, 'y'*32, frozenset((0,)), 1, canary) + def _allocated( (already, writers) ): + # This is a white-box test: Inspect incoming and fail unless the sharenum: 0 is listed there. + self.failUnless(shareset.has_incoming(0)) + + # Attempt to create a second share writer with the same sharenum. + d2 = aa.remote_allocate_buckets('teststorage_index', 'x'*32, 'y'*32, frozenset((0,)), 1, canary) + + # Show that no sharewriter results from a remote_allocate_buckets + # with the same si and sharenum, until BucketWriter.remote_close() + # has been called. + d2.addCallback(lambda (already2, writers2): self.failIf(writers2)) + + # Test allocated size. + d2.addCallback(lambda ign: server.allocated_size()) + d2.addCallback(lambda space: self.failUnlessEqual(space, 1)) + + # Write 'a' to shnum 0. Only tested together with close and read. + d2.addCallback(lambda ign: writers[0].remote_write(0, 'a')) + + # Preclose: Inspect final, failUnless nothing there. + d2.addCallback(lambda ign: server.backend.get_shareset('teststorage_index').get_shares()) + def _check( (shares, corrupted) ): + self.failUnlessEqual(len(shares), 0, str(shares)) + self.failUnlessEqual(len(corrupted), 0, str(corrupted)) + d2.addCallback(_check) + + d2.addCallback(lambda ign: writers[0].remote_close()) + + # Postclose: fail unless written data is in final. + d2.addCallback(lambda ign: server.backend.get_shareset('teststorage_index').get_shares()) + def _got_shares( (sharesinfinal, corrupted) ): + self.failUnlessEqual(len(sharesinfinal), 1, str(sharesinfinal)) + self.failUnlessEqual(len(corrupted), 0, str(corrupted)) + + d3 = defer.succeed(None) + d3.addCallback(lambda ign: sharesinfinal[0].read_share_data(0, 73)) + d3.addCallback(lambda contents: self.failUnlessEqual(contents, self.shareinputdata)) + return d3 + d2.addCallback(_got_shares) + + # Exercise the case that the share we're asking to allocate is + # already (completely) uploaded. + d2.addCallback(lambda ign: aa.remote_allocate_buckets('teststorage_index', + 'x'*32, 'y'*32, set((0,)), 1, canary)) + return d2 + d.addCallback(_allocated) + return d - def test_disconnect(self): - # simulate a disconnection - server = self.create("test_disconnect") + def test_read_old_share(self): + """ + This tests whether the code correctly finds and reads shares written out by + pre-pluggable-backends (Tahoe-LAFS <= v1.8.2) servers. There is a similar test + in test_download, but that one is from the perspective of the client and exercises + a deeper stack of code. This one is for exercising just the StorageServer and backend. + """ + server = self.create("test_read_old_share") aa = server.get_accountant().get_anonymous_account() - canary = FakeCanary() - already,writers = self.allocate(aa, "disconnect", [0,1,2], 75, canary) - self.failUnlessEqual(already, set()) - self.failUnlessEqual(set(writers.keys()), set([0,1,2])) - for (f,args,kwargs) in canary.disconnectors.values(): - f(*args, **kwargs) - del already - del writers - - # that ought to delete the incoming shares - already,writers = self.allocate(aa, "disconnect", [0,1,2], 75) - self.failUnlessEqual(already, set()) - self.failUnlessEqual(set(writers.keys()), set([0,1,2])) - - @mock.patch('allmydata.util.fileutil.get_disk_stats') - def test_reserved_space(self, mock_get_disk_stats): - reserved_space=10000 - mock_get_disk_stats.return_value = { - 'free_for_nonroot': 15000, - 'avail': max(15000 - reserved_space, 0), - } + # Contruct a file with the appropriate contents. + datalen = len(self.share_data) + sharedir = server.backend.get_shareset('teststorage_index')._get_sharedir() + fileutil.make_dirs(sharedir) + fileutil.write(os.path.join(sharedir, "0"), self.share_data) + + # Now begin the test. + d = aa.remote_get_buckets('teststorage_index') + def _got_buckets(bs): + self.failUnlessEqual(len(bs), 1) + self.failUnlessIn(0, bs) + b = bs[0] + + d2 = defer.succeed(None) + d2.addCallback(lambda ign: b.remote_read(0, datalen)) + d2.addCallback(lambda res: self.failUnlessEqual(res, self.shareinputdata)) + + # If you try to read past the end you get as much input data as is there. + d2.addCallback(lambda ign: b.remote_read(0, datalen+20)) + d2.addCallback(lambda res: self.failUnlessEqual(res, self.shareinputdata)) + + # If you start reading past the end of the file you get the empty string. + d2.addCallback(lambda ign: b.remote_read(datalen+1, 3)) + d2.addCallback(lambda res: self.failUnlessEqual(res, '')) + return d2 + d.addCallback(_got_buckets) + return d - server = self.create("test_reserved_space", reserved_space=reserved_space) + def test_bad_container_version(self): + server = self.create("test_bad_container_version") aa = server.get_accountant().get_anonymous_account() - # 15k available, 10k reserved, leaves 5k for shares - - # a newly created and filled share incurs this much overhead, beyond - # the size we request. - OVERHEAD = 3*4 - LEASE_SIZE = 4+32+32+4 - canary = FakeCanary(True) - already,writers = self.allocate(aa, "vid1", [0,1,2], 1000, canary) - self.failUnlessEqual(len(writers), 3) - # now the StorageServer should have 3000 bytes provisionally - # allocated, allowing only 2000 more to be claimed - self.failUnlessEqual(len(server._active_writers), 3) - - # allocating 1001-byte shares only leaves room for one - already2,writers2 = self.allocate(aa, "vid2", [0,1,2], 1001, canary) - self.failUnlessEqual(len(writers2), 1) - self.failUnlessEqual(len(server._active_writers), 4) - - # we abandon the first set, so their provisional allocation should be - # returned - del already - del writers - self.failUnlessEqual(len(server._active_writers), 1) - # now we have a provisional allocation of 1001 bytes - - # and we close the second set, so their provisional allocation should - # become real, long-term allocation, and grows to include the - # overhead. - for bw in writers2.values(): - bw.remote_write(0, "a"*25) - bw.remote_close() - del already2 - del writers2 - del bw - self.failUnlessEqual(len(server._active_writers), 0) - - allocated = 1001 + OVERHEAD + LEASE_SIZE - - # we have to manually increase available, since we're not doing real - # disk measurements - mock_get_disk_stats.return_value = { - 'free_for_nonroot': 15000 - allocated, - 'avail': max(15000 - allocated - reserved_space, 0), - } + d = self.allocate(aa, "allocate", [0,1], 20) + def _allocated( (already, writers) ): + d2 = defer.succeed(None) + d2.addCallback(lambda ign: writers[0].remote_write(0, "\xff"*10)) + d2.addCallback(lambda ign: writers[0].remote_close()) + d2.addCallback(lambda ign: writers[1].remote_write(1, "\xaa"*10)) + d2.addCallback(lambda ign: writers[1].remote_close()) + return d2 + d.addCallback(_allocated) + + d.addCallback(lambda ign: server.backend.get_shareset("allocate").get_share(0)) + def _write_invalid_version(share0): + f = open(share0._get_path(), "rb+") + try: + f.seek(0) + f.write(struct.pack(">L", 0)) # this is invalid: minimum used is v1 + finally: + f.close() + d.addCallback(_write_invalid_version) + + # This should ignore the corrupted share; see ticket #1566. + d.addCallback(lambda ign: aa.remote_get_buckets("allocate")) + d.addCallback(lambda b: self.failUnlessEqual(set(b.keys()), set([1]))) + + # Also if there are only corrupted shares. + d.addCallback(lambda ign: server.backend.get_shareset("allocate").get_share(1)) + d.addCallback(lambda share: share.unlink()) + d.addCallback(lambda ign: aa.remote_get_buckets("allocate")) + d.addCallback(lambda b: self.failUnlessEqual(b, {})) + return d - # now there should be ALLOCATED=1001+12+72=1085 bytes allocated, and - # 5000-1085=3915 free, therefore we can fit 39 100byte shares - already3,writers3 = self.allocate(aa, "vid3", range(100), 100, canary) - self.failUnlessEqual(len(writers3), 39) - self.failUnlessEqual(len(server._active_writers), 39) + def test_advise_corruption(self): + server = self.create("test_advise_corruption") + aa = server.get_accountant().get_anonymous_account() - del already3 - del writers3 - self.failUnlessEqual(len(server._active_writers), 0) - server.disownServiceParent() - del server + si0_s = base32.b2a("si0") + aa.remote_advise_corrupt_share("immutable", "si0", 0, + "This share smells funny.\n") + reportdir = os.path.join(server._statedir, "corruption-advisories") + self.failUnless(os.path.exists(reportdir), reportdir) + reports = fileutil.listdir(reportdir) + self.failUnlessEqual(len(reports), 1) + report_si0 = reports[0] + self.failUnlessIn(si0_s, str(report_si0)) + report = fileutil.read(os.path.join(reportdir, report_si0)) - def test_seek(self): - basedir = self.workdir("test_seek_behavior") - fileutil.make_dirs(basedir) - filename = os.path.join(basedir, "testfile") - fileutil.write(filename, "start") + self.failUnlessIn("type: immutable", report) + self.failUnlessIn("storage_index: %s" % si0_s, report) + self.failUnlessIn("share_number: 0", report) + self.failUnlessIn("This share smells funny.", report) - # mode="w" allows seeking-to-create-holes, but truncates pre-existing - # files. mode="a" preserves previous contents but does not allow - # seeking-to-create-holes. mode="r+" allows both. - f = open(filename, "rb+") - f.seek(100) - f.write("100") - f.close() - filelen = os.stat(filename)[stat.ST_SIZE] - self.failUnlessEqual(filelen, 100+3) - f2 = open(filename, "rb") - self.failUnlessEqual(f2.read(5), "start") + # test the RIBucketWriter version too + si1_s = base32.b2a("si1") + d = self.allocate(aa, "si1", [1], 75) + def _allocated( (already, writers) ): + self.failUnlessEqual(already, set()) + self.failUnlessEqual(set(writers.keys()), set([1])) + + d2 = defer.succeed(None) + d2.addCallback(lambda ign: writers[1].remote_write(0, "data")) + d2.addCallback(lambda ign: writers[1].remote_close()) + + d2.addCallback(lambda ign: aa.remote_get_buckets("si1")) + def _got_buckets(b): + self.failUnlessEqual(set(b.keys()), set([1])) + b[1].remote_advise_corrupt_share("This share tastes like dust.\n") + + reports = fileutil.listdir(reportdir) + self.failUnlessEqual(len(reports), 2) + report_si1 = [r for r in reports if si1_s in r][0] + report = fileutil.read(os.path.join(reportdir, report_si1)) + + self.failUnlessIn("type: immutable", report) + self.failUnlessIn("storage_index: %s" % (si1_s,), report) + self.failUnlessIn("share_number: 1", report) + self.failUnlessIn("This share tastes like dust.", report) + d2.addCallback(_got_buckets) + return d2 + d.addCallback(_allocated) + return d def compare_leases(self, leases_a, leases_b, with_timestamps=True): self.failUnlessEqual(len(leases_a), len(leases_b)) @@ -590,8 +779,8 @@ class Server(unittest.TestCase): self.failUnlessEqual(a.renewal_time, b.renewal_time) self.failUnlessEqual(a.expiration_time, b.expiration_time) - def test_leases(self): - server = self.create("test_leases") + def OFF_test_immutable_leases(self): + server = self.create("test_immutable_leases") aa = server.get_accountant().get_anonymous_account() sa = server.get_accountant().get_starter_account() @@ -646,92 +835,8 @@ class Server(unittest.TestCase): sa.remote_renew_lease("si1", "") self.compare_leases(all_leases2, sa.get_leases("si1"), with_timestamps=False) - def test_readonly(self): - workdir = self.workdir("test_readonly") - server = StorageServer(workdir, "\x00" * 20, readonly_storage=True) - server.setServiceParent(self.sparent) - aa = server.get_accountant().get_anonymous_account() - - already,writers = self.allocate(aa, "vid", [0,1,2], 75) - self.failUnlessEqual(already, set()) - self.failUnlessEqual(writers, {}) - - stats = server.get_stats() - self.failUnlessEqual(stats["storage_server.accepting_immutable_shares"], 0) - if "storage_server.disk_avail" in stats: - # Some platforms may not have an API to get disk stats. - # But if there are stats, readonly_storage means disk_avail=0 - self.failUnlessEqual(stats["storage_server.disk_avail"], 0) - - def test_advise_corruption(self): - workdir = self.workdir("test_advise_corruption") - server = StorageServer(workdir, "\x00" * 20) - server.setServiceParent(self.sparent) - aa = server.get_accountant().get_anonymous_account() - - si0_s = base32.b2a("si0") - aa.remote_advise_corrupt_share("immutable", "si0", 0, - "This share smells funny.\n") - reportdir = os.path.join(workdir, "corruption-advisories") - reports = os.listdir(reportdir) - self.failUnlessEqual(len(reports), 1) - report_si0 = reports[0] - self.failUnlessIn(si0_s, report_si0) - f = open(os.path.join(reportdir, report_si0), "r") - report = f.read() - f.close() - self.failUnlessIn("type: immutable", report) - self.failUnlessIn("storage_index: %s" % si0_s, report) - self.failUnlessIn("share_number: 0", report) - self.failUnlessIn("This share smells funny.", report) - - # test the RIBucketWriter version too - si1_s = base32.b2a("si1") - already,writers = self.allocate(aa, "si1", [1], 75) - self.failUnlessEqual(already, set()) - self.failUnlessEqual(set(writers.keys()), set([1])) - writers[1].remote_write(0, "data") - writers[1].remote_close() - - b = aa.remote_get_buckets("si1") - self.failUnlessEqual(set(b.keys()), set([1])) - b[1].remote_advise_corrupt_share("This share tastes like dust.\n") - - reports = os.listdir(reportdir) - self.failUnlessEqual(len(reports), 2) - report_si1 = [r for r in reports if si1_s in r][0] - f = open(os.path.join(reportdir, report_si1), "r") - report = f.read() - f.close() - self.failUnlessIn("type: immutable", report) - self.failUnlessIn("storage_index: %s" % si1_s, report) - self.failUnlessIn("share_number: 1", report) - self.failUnlessIn("This share tastes like dust.", report) - - -class MutableServer(unittest.TestCase): - def setUp(self): - self.sparent = LoggingServiceParent() - self._lease_secret = itertools.count() - - def tearDown(self): - return self.sparent.stopService() - - - def workdir(self, name): - basedir = os.path.join("storage", "MutableServer", name) - return basedir - - def create(self, name): - workdir = self.workdir(name) - server = StorageServer(workdir, "\x00" * 20) - server.setServiceParent(self.sparent) - return server - - def test_create(self): - self.create("test_create") - +class MutableServerMixin: def write_enabler(self, we_tag): return hashutil.tagged_hash("we_blah", we_tag) @@ -741,89 +846,114 @@ class MutableServer(unittest.TestCase): def cancel_secret(self, tag): return hashutil.tagged_hash("cancel_blah", str(tag)) - def allocate(self, aa, storage_index, we_tag, lease_tag, sharenums, size): + def allocate(self, aa, storage_index, we_tag, sharenums, size): write_enabler = self.write_enabler(we_tag) + + # These secrets are not used, but clients still provide them. + lease_tag = "%d" % (self._lease_secret.next(),) renew_secret = self.renew_secret(lease_tag) cancel_secret = self.cancel_secret(lease_tag) + rstaraw = aa.remote_slot_testv_and_readv_and_writev testandwritev = dict( [ (shnum, ([], [], None) ) - for shnum in sharenums ] ) + for shnum in sharenums ] ) readv = [] - rc = rstaraw(storage_index, - (write_enabler, renew_secret, cancel_secret), - testandwritev, - readv) - (did_write, readv_data) = rc - self.failUnless(did_write) - self.failUnless(isinstance(readv_data, dict)) - self.failUnlessEqual(len(readv_data), 0) + + d = defer.succeed(None) + d.addCallback(lambda ign: rstaraw(storage_index, + (write_enabler, renew_secret, cancel_secret), + testandwritev, + readv)) + def _check( (did_write, readv_data) ): + self.failUnless(did_write) + self.failUnless(isinstance(readv_data, dict)) + self.failUnlessEqual(len(readv_data), 0) + d.addCallback(_check) + return d +class MutableServerTest(MutableServerMixin, ShouldFailMixin): + def test_create(self): + server = self.create("test_create") + aa = server.get_accountant().get_anonymous_account() + self.failUnless(RIStorageServer.providedBy(aa), aa) + def test_bad_magic(self): server = self.create("test_bad_magic") aa = server.get_accountant().get_anonymous_account() - - self.allocate(aa, "si1", "we1", self._lease_secret.next(), set([0]), 10) - fn = os.path.join(server.sharedir, storage_index_to_dir("si1"), "0") - f = open(fn, "rb+") - f.seek(0) - f.write("BAD MAGIC") - f.close() read = aa.remote_slot_readv - e = self.failUnlessRaises(UnknownMutableContainerVersionError, - read, "si1", [0], [(0,10)]) - self.failUnlessIn(" had magic ", str(e)) - self.failUnlessIn(" but we wanted ", str(e)) + + d = self.allocate(aa, "si1", "we1", set([0,1]), 10) + d.addCallback(lambda ign: server.backend.get_shareset("si1").get_share(0)) + def _got_share(share0): + f = open(share0._get_path(), "rb+") + try: + f.seek(0) + f.write("BAD MAGIC") + finally: + f.close() + d.addCallback(_got_share) + + # This should ignore the corrupted share; see ticket #1566. + d.addCallback(lambda ign: read("si1", [0,1], [(0,10)]) ) + d.addCallback(lambda res: self.failUnlessEqual(res, {1: ['']})) + + # Also if there are only corrupted shares. + d.addCallback(lambda ign: server.backend.get_shareset("si1").get_share(1)) + d.addCallback(lambda share: share.unlink()) + d.addCallback(lambda ign: read("si1", [0], [(0,10)]) ) + d.addCallback(lambda res: self.failUnlessEqual(res, {})) + return d def test_container_size(self): server = self.create("test_container_size") aa = server.get_accountant().get_anonymous_account() - - self.allocate(aa, "si1", "we1", self._lease_secret.next(), - set([0,1,2]), 100) read = aa.remote_slot_readv rstaraw = aa.remote_slot_testv_and_readv_and_writev secrets = ( self.write_enabler("we1"), self.renew_secret("we1"), self.cancel_secret("we1") ) data = "".join([ ("%d" % i) * 10 for i in range(10) ]) - answer = rstaraw("si1", secrets, - {0: ([], [(0,data)], len(data)+12)}, - []) - self.failUnlessEqual(answer, (True, {0:[],1:[],2:[]}) ) + + d = self.allocate(aa, "si1", "we1", set([0,1,2]), 100) + d.addCallback(lambda ign: rstaraw("si1", secrets, + {0: ([], [(0,data)], len(data)+12)}, + [])) + d.addCallback(lambda res: self.failUnlessEqual(res, (True, {0:[],1:[],2:[]}) )) # Trying to make the container too large (by sending a write vector # whose offset is too high) will raise an exception. - TOOBIG = MutableShareFile.MAX_SIZE + 10 - self.failUnlessRaises(DataTooLargeError, - rstaraw, "si1", secrets, - {0: ([], [(TOOBIG,data)], None)}, - []) + TOOBIG = MutableDiskShare.MAX_SIZE + 10 + d.addCallback(lambda ign: self.shouldFail(DataTooLargeError, + 'make container too large', None, + lambda: rstaraw("si1", secrets, + {0: ([], [(TOOBIG,data)], None)}, + []) )) - answer = rstaraw("si1", secrets, - {0: ([], [(0,data)], None)}, - []) - self.failUnlessEqual(answer, (True, {0:[],1:[],2:[]}) ) + d.addCallback(lambda ign: rstaraw("si1", secrets, + {0: ([], [(0,data)], None)}, + [])) + d.addCallback(lambda res: self.failUnlessEqual(res, (True, {0:[],1:[],2:[]}) )) - read_answer = read("si1", [0], [(0,10)]) - self.failUnlessEqual(read_answer, {0: [data[:10]]}) + d.addCallback(lambda ign: read("si1", [0], [(0,10)])) + d.addCallback(lambda res: self.failUnlessEqual(res, {0: [data[:10]]})) # Sending a new_length shorter than the current length truncates the # data. - answer = rstaraw("si1", secrets, - {0: ([], [], 9)}, - []) - read_answer = read("si1", [0], [(0,10)]) - self.failUnlessEqual(read_answer, {0: [data[:9]]}) + d.addCallback(lambda ign: rstaraw("si1", secrets, + {0: ([], [], 9)}, + [])) + d.addCallback(lambda ign: read("si1", [0], [(0,10)])) + d.addCallback(lambda res: self.failUnlessEqual(res, {0: [data[:9]]})) # Sending a new_length longer than the current length doesn't change # the data. - answer = rstaraw("si1", secrets, - {0: ([], [], 20)}, - []) - assert answer == (True, {0:[],1:[],2:[]}) - read_answer = read("si1", [0], [(0, 20)]) - self.failUnlessEqual(read_answer, {0: [data[:9]]}) + d.addCallback(lambda ign: rstaraw("si1", secrets, + {0: ([], [], 20)}, + [])) + d.addCallback(lambda res: self.failUnlessEqual(res, (True, {0:[],1:[],2:[]}) )) + d.addCallback(lambda ign: read("si1", [0], [(0, 20)])) + d.addCallback(lambda res: self.failUnlessEqual(res, {0: [data[:9]]})) # Sending a write vector whose start is after the end of the current # data doesn't reveal "whatever was there last time" (palimpsest), @@ -831,120 +961,119 @@ class MutableServer(unittest.TestCase): # To test this, we fill the data area with a recognizable pattern. pattern = ''.join([chr(i) for i in range(100)]) - answer = rstaraw("si1", secrets, - {0: ([], [(0, pattern)], None)}, - []) - assert answer == (True, {0:[],1:[],2:[]}) + d.addCallback(lambda ign: rstaraw("si1", secrets, + {0: ([], [(0, pattern)], None)}, + [])) + d.addCallback(lambda res: self.failUnlessEqual(res, (True, {0:[],1:[],2:[]}) )) # Then truncate the data... - answer = rstaraw("si1", secrets, - {0: ([], [], 20)}, - []) - assert answer == (True, {0:[],1:[],2:[]}) + d.addCallback(lambda ign: rstaraw("si1", secrets, + {0: ([], [], 20)}, + [])) + d.addCallback(lambda res: self.failUnlessEqual(res, (True, {0:[],1:[],2:[]}) )) # Just confirm that you get an empty string if you try to read from # past the (new) endpoint now. - answer = rstaraw("si1", secrets, - {0: ([], [], None)}, - [(20, 1980)]) - self.failUnlessEqual(answer, (True, {0:[''],1:[''],2:['']})) + d.addCallback(lambda ign: rstaraw("si1", secrets, + {0: ([], [], None)}, + [(20, 1980)])) + d.addCallback(lambda res: self.failUnlessEqual(res, (True, {0:[''],1:[''],2:['']}) )) # Then the extend the file by writing a vector which starts out past # the end... - answer = rstaraw("si1", secrets, - {0: ([], [(50, 'hellothere')], None)}, - []) - assert answer == (True, {0:[],1:[],2:[]}) + d.addCallback(lambda ign: rstaraw("si1", secrets, + {0: ([], [(50, 'hellothere')], None)}, + [])) + d.addCallback(lambda res: self.failUnlessEqual(res, (True, {0:[],1:[],2:[]}) )) # Now if you read the stuff between 20 (where we earlier truncated) # and 50, it had better be all zeroes. - answer = rstaraw("si1", secrets, - {0: ([], [], None)}, - [(20, 30)]) - self.failUnlessEqual(answer, (True, {0:['\x00'*30],1:[''],2:['']})) + d.addCallback(lambda ign: rstaraw("si1", secrets, + {0: ([], [], None)}, + [(20, 30)])) + d.addCallback(lambda res: self.failUnlessEqual(res, (True, {0:['\x00'*30],1:[''],2:['']}) )) # Also see if the server explicitly declares that it supports this # feature. - ver = aa.remote_get_version() - storage_v1_ver = ver["http://allmydata.org/tahoe/protocols/storage/v1"] - self.failUnless(storage_v1_ver.get("fills-holes-with-zero-bytes")) + d.addCallback(lambda ign: aa.remote_get_version()) + def _check_declaration(ver): + storage_v1_ver = ver["http://allmydata.org/tahoe/protocols/storage/v1"] + self.failUnless(storage_v1_ver.get("fills-holes-with-zero-bytes")) + d.addCallback(_check_declaration) # If the size is dropped to zero the share is deleted. - answer = rstaraw("si1", secrets, - {0: ([], [(0,data)], 0)}, - []) - self.failUnlessEqual(answer, (True, {0:[],1:[],2:[]}) ) + d.addCallback(lambda ign: rstaraw("si1", secrets, + {0: ([], [(0,data)], 0)}, + [])) + d.addCallback(lambda res: self.failUnlessEqual(res, (True, {0:[],1:[],2:[]}) )) - read_answer = read("si1", [0], [(0,10)]) - self.failUnlessEqual(read_answer, {}) + d.addCallback(lambda ign: read("si1", [0], [(0,10)])) + d.addCallback(lambda res: self.failUnlessEqual(res, {})) + return d def test_allocate(self): server = self.create("test_allocate") aa = server.get_accountant().get_anonymous_account() + read = aa.remote_slot_readv + write = aa.remote_slot_testv_and_readv_and_writev - self.allocate(aa, "si1", "we1", self._lease_secret.next(), - set([0,1,2]), 100) + d = self.allocate(aa, "si1", "we1", set([0,1,2]), 100) - read = aa.remote_slot_readv - self.failUnlessEqual(read("si1", [0], [(0, 10)]), - {0: [""]}) - self.failUnlessEqual(read("si1", [], [(0, 10)]), - {0: [""], 1: [""], 2: [""]}) - self.failUnlessEqual(read("si1", [0], [(100, 10)]), - {0: [""]}) + d.addCallback(lambda ign: read("si1", [0], [(0, 10)])) + d.addCallback(lambda res: self.failUnlessEqual(res, {0: [""]})) + d.addCallback(lambda ign: read("si1", [], [(0, 10)])) + d.addCallback(lambda res: self.failUnlessEqual(res, {0: [""], 1: [""], 2: [""]})) + d.addCallback(lambda ign: read("si1", [0], [(100, 10)])) + d.addCallback(lambda res: self.failUnlessEqual(res, {0: [""]})) # try writing to one secrets = ( self.write_enabler("we1"), self.renew_secret("we1"), self.cancel_secret("we1") ) data = "".join([ ("%d" % i) * 10 for i in range(10) ]) - write = aa.remote_slot_testv_and_readv_and_writev - answer = write("si1", secrets, - {0: ([], [(0,data)], None)}, - []) - self.failUnlessEqual(answer, (True, {0:[],1:[],2:[]}) ) - self.failUnlessEqual(read("si1", [0], [(0,20)]), - {0: ["00000000001111111111"]}) - self.failUnlessEqual(read("si1", [0], [(95,10)]), - {0: ["99999"]}) - #self.failUnlessEqual(s0.remote_get_length(), 100) + d.addCallback(lambda ign: write("si1", secrets, + {0: ([], [(0,data)], None)}, + [])) + d.addCallback(lambda res: self.failUnlessEqual(res, (True, {0:[],1:[],2:[]}) )) + + d.addCallback(lambda ign: read("si1", [0], [(0,20)])) + d.addCallback(lambda res: self.failUnlessEqual(res, {0: ["00000000001111111111"]})) + d.addCallback(lambda ign: read("si1", [0], [(95,10)])) + d.addCallback(lambda res: self.failUnlessEqual(res, {0: ["99999"]})) + #d.addCallback(lambda ign: s0.remote_get_length()) + #d.addCallback(lambda res: self.failUnlessEqual(res, 100)) bad_secrets = ("bad write enabler", secrets[1], secrets[2]) - f = self.failUnlessRaises(BadWriteEnablerError, - write, "si1", bad_secrets, - {}, []) - self.failUnlessIn("The write enabler was recorded by nodeid 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'.", f) + d.addCallback(lambda ign: self.shouldFail(BadWriteEnablerError, 'bad write enabler', + "The write enabler was recorded by nodeid " + "'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'.", + lambda: write("si1", bad_secrets, {}, []) )) # this testv should fail - answer = write("si1", secrets, - {0: ([(0, 12, "eq", "444444444444"), - (20, 5, "eq", "22222"), - ], - [(0, "x"*100)], - None), - }, - [(0,12), (20,5)], - ) - self.failUnlessEqual(answer, (False, - {0: ["000000000011", "22222"], - 1: ["", ""], - 2: ["", ""], - })) - self.failUnlessEqual(read("si1", [0], [(0,100)]), {0: [data]}) + d.addCallback(lambda ign: write("si1", secrets, + {0: ([(0, 12, "eq", "444444444444"), + (20, 5, "eq", "22222"),], + [(0, "x"*100)], + None)}, + [(0,12), (20,5)])) + d.addCallback(lambda res: self.failUnlessEqual(res, (False, + {0: ["000000000011", "22222"], + 1: ["", ""], + 2: ["", ""]}) )) + d.addCallback(lambda ign: read("si1", [0], [(0,100)])) + d.addCallback(lambda res: self.failUnlessEqual(res, {0: [data]})) # as should this one - answer = write("si1", secrets, - {0: ([(10, 5, "lt", "11111"), - ], - [(0, "x"*100)], - None), - }, - [(10,5)], - ) - self.failUnlessEqual(answer, (False, - {0: ["11111"], - 1: [""], - 2: [""]}, - )) - self.failUnlessEqual(read("si1", [0], [(0,100)]), {0: [data]}) + d.addCallback(lambda ign: write("si1", secrets, + {0: ([(10, 5, "lt", "11111"),], + [(0, "x"*100)], + None)}, + [(10,5)])) + d.addCallback(lambda res: self.failUnlessEqual(res, (False, + {0: ["11111"], + 1: [""], + 2: [""]}) )) + d.addCallback(lambda ign: read("si1", [0], [(0,100)])) + d.addCallback(lambda res: self.failUnlessEqual(res, {0: [data]})) + return d def test_operators(self): # test operators, the data we're comparing is '11111' in all cases. @@ -959,173 +1088,176 @@ class MutableServer(unittest.TestCase): write = aa.remote_slot_testv_and_readv_and_writev read = aa.remote_slot_readv - def reset(): - write("si1", secrets, - {0: ([], [(0,data)], None)}, - []) + def _reset(ign): + return write("si1", secrets, + {0: ([], [(0,data)], None)}, + []) - reset() + d = defer.succeed(None) + d.addCallback(_reset) # lt - answer = write("si1", secrets, {0: ([(10, 5, "lt", "11110"), - ], - [(0, "x"*100)], - None, - )}, [(10,5)]) - self.failUnlessEqual(answer, (False, {0: ["11111"]})) - self.failUnlessEqual(read("si1", [0], [(0,100)]), {0: [data]}) - self.failUnlessEqual(read("si1", [], [(0,100)]), {0: [data]}) - reset() - - answer = write("si1", secrets, {0: ([(10, 5, "lt", "11111"), - ], - [(0, "x"*100)], - None, - )}, [(10,5)]) - self.failUnlessEqual(answer, (False, {0: ["11111"]})) - self.failUnlessEqual(read("si1", [0], [(0,100)]), {0: [data]}) - reset() - - answer = write("si1", secrets, {0: ([(10, 5, "lt", "11112"), - ], - [(0, "y"*100)], - None, - )}, [(10,5)]) - self.failUnlessEqual(answer, (True, {0: ["11111"]})) - self.failUnlessEqual(read("si1", [0], [(0,100)]), {0: ["y"*100]}) - reset() + d.addCallback(lambda ign: write("si1", secrets, {0: ([(10, 5, "lt", "11110"),], + [(0, "x"*100)], + None, + )}, [(10,5)])) + d.addCallback(lambda res: self.failUnlessEqual(res, (False, {0: ["11111"]}) )) + d.addCallback(lambda ign: read("si1", [0], [(0,100)])) + d.addCallback(lambda res: self.failUnlessEqual(res, {0: [data]})) + d.addCallback(lambda ign: read("si1", [], [(0,100)])) + d.addCallback(lambda res: self.failUnlessEqual(res, {0: [data]})) + d.addCallback(_reset) + + d.addCallback(lambda ign: write("si1", secrets, {0: ([(10, 5, "lt", "11111"),], + [(0, "x"*100)], + None, + )}, [(10,5)])) + d.addCallback(lambda res: self.failUnlessEqual(res, (False, {0: ["11111"]}) )) + d.addCallback(lambda ign: read("si1", [0], [(0,100)])) + d.addCallback(lambda res: self.failUnlessEqual(res, {0: [data]})) + d.addCallback(_reset) + + d.addCallback(lambda ign: write("si1", secrets, {0: ([(10, 5, "lt", "11112"),], + [(0, "y"*100)], + None, + )}, [(10,5)])) + d.addCallback(lambda res: self.failUnlessEqual(res, (True, {0: ["11111"]}) )) + d.addCallback(lambda ign: read("si1", [0], [(0,100)])) + d.addCallback(lambda res: self.failUnlessEqual(res, {0: ["y"*100]})) + d.addCallback(_reset) # le - answer = write("si1", secrets, {0: ([(10, 5, "le", "11110"), - ], - [(0, "x"*100)], - None, - )}, [(10,5)]) - self.failUnlessEqual(answer, (False, {0: ["11111"]})) - self.failUnlessEqual(read("si1", [0], [(0,100)]), {0: [data]}) - reset() - - answer = write("si1", secrets, {0: ([(10, 5, "le", "11111"), - ], - [(0, "y"*100)], - None, - )}, [(10,5)]) - self.failUnlessEqual(answer, (True, {0: ["11111"]})) - self.failUnlessEqual(read("si1", [0], [(0,100)]), {0: ["y"*100]}) - reset() - - answer = write("si1", secrets, {0: ([(10, 5, "le", "11112"), - ], - [(0, "y"*100)], - None, - )}, [(10,5)]) - self.failUnlessEqual(answer, (True, {0: ["11111"]})) - self.failUnlessEqual(read("si1", [0], [(0,100)]), {0: ["y"*100]}) - reset() + d.addCallback(lambda ign: write("si1", secrets, {0: ([(10, 5, "le", "11110"),], + [(0, "x"*100)], + None, + )}, [(10,5)])) + d.addCallback(lambda res: self.failUnlessEqual(res, (False, {0: ["11111"]}) )) + d.addCallback(lambda ign: read("si1", [0], [(0,100)])) + d.addCallback(lambda res: self.failUnlessEqual(res, {0: [data]})) + d.addCallback(_reset) + + d.addCallback(lambda ign: write("si1", secrets, {0: ([(10, 5, "le", "11111"),], + [(0, "y"*100)], + None, + )}, [(10,5)])) + d.addCallback(lambda res: self.failUnlessEqual(res, (True, {0: ["11111"]}) )) + d.addCallback(lambda ign: read("si1", [0], [(0,100)])) + d.addCallback(lambda res: self.failUnlessEqual(res, {0: ["y"*100]})) + d.addCallback(_reset) + + d.addCallback(lambda ign: write("si1", secrets, {0: ([(10, 5, "le", "11112"),], + [(0, "y"*100)], + None, + )}, [(10,5)])) + d.addCallback(lambda res: self.failUnlessEqual(res, (True, {0: ["11111"]}) )) + d.addCallback(lambda ign: read("si1", [0], [(0,100)])) + d.addCallback(lambda res: self.failUnlessEqual(res, {0: ["y"*100]})) + d.addCallback(_reset) # eq - answer = write("si1", secrets, {0: ([(10, 5, "eq", "11112"), - ], - [(0, "x"*100)], - None, - )}, [(10,5)]) - self.failUnlessEqual(answer, (False, {0: ["11111"]})) - self.failUnlessEqual(read("si1", [0], [(0,100)]), {0: [data]}) - reset() - - answer = write("si1", secrets, {0: ([(10, 5, "eq", "11111"), - ], - [(0, "y"*100)], - None, - )}, [(10,5)]) - self.failUnlessEqual(answer, (True, {0: ["11111"]})) - self.failUnlessEqual(read("si1", [0], [(0,100)]), {0: ["y"*100]}) - reset() + d.addCallback(lambda ign: write("si1", secrets, {0: ([(10, 5, "eq", "11112"),], + [(0, "x"*100)], + None, + )}, [(10,5)])) + d.addCallback(lambda res: self.failUnlessEqual(res, (False, {0: ["11111"]}) )) + d.addCallback(lambda ign: read("si1", [0], [(0,100)])) + d.addCallback(lambda res: self.failUnlessEqual(res, {0: [data]})) + d.addCallback(_reset) + + d.addCallback(lambda ign: write("si1", secrets, {0: ([(10, 5, "eq", "11111"),], + [(0, "y"*100)], + None, + )}, [(10,5)])) + d.addCallback(lambda res: self.failUnlessEqual(res, (True, {0: ["11111"]}) )) + d.addCallback(lambda ign: read("si1", [0], [(0,100)])) + d.addCallback(lambda res: self.failUnlessEqual(res, {0: ["y"*100]})) + d.addCallback(_reset) # ne - answer = write("si1", secrets, {0: ([(10, 5, "ne", "11111"), - ], - [(0, "x"*100)], - None, - )}, [(10,5)]) - self.failUnlessEqual(answer, (False, {0: ["11111"]})) - self.failUnlessEqual(read("si1", [0], [(0,100)]), {0: [data]}) - reset() - - answer = write("si1", secrets, {0: ([(10, 5, "ne", "11112"), - ], - [(0, "y"*100)], - None, - )}, [(10,5)]) - self.failUnlessEqual(answer, (True, {0: ["11111"]})) - self.failUnlessEqual(read("si1", [0], [(0,100)]), {0: ["y"*100]}) - reset() + d.addCallback(lambda ign: write("si1", secrets, {0: ([(10, 5, "ne", "11111"),], + [(0, "x"*100)], + None, + )}, [(10,5)])) + d.addCallback(lambda res: self.failUnlessEqual(res, (False, {0: ["11111"]}) )) + d.addCallback(lambda ign: read("si1", [0], [(0,100)])) + d.addCallback(lambda res: self.failUnlessEqual(res, {0: [data]})) + d.addCallback(_reset) + + d.addCallback(lambda ign: write("si1", secrets, {0: ([(10, 5, "ne", "11112"),], + [(0, "y"*100)], + None, + )}, [(10,5)])) + d.addCallback(lambda res: self.failUnlessEqual(res, (True, {0: ["11111"]}) )) + d.addCallback(lambda ign: read("si1", [0], [(0,100)])) + d.addCallback(lambda res: self.failUnlessEqual(res, {0: ["y"*100]})) + d.addCallback(_reset) # ge - answer = write("si1", secrets, {0: ([(10, 5, "ge", "11110"), - ], - [(0, "y"*100)], - None, - )}, [(10,5)]) - self.failUnlessEqual(answer, (True, {0: ["11111"]})) - self.failUnlessEqual(read("si1", [0], [(0,100)]), {0: ["y"*100]}) - reset() - - answer = write("si1", secrets, {0: ([(10, 5, "ge", "11111"), - ], - [(0, "y"*100)], - None, - )}, [(10,5)]) - self.failUnlessEqual(answer, (True, {0: ["11111"]})) - self.failUnlessEqual(read("si1", [0], [(0,100)]), {0: ["y"*100]}) - reset() - - answer = write("si1", secrets, {0: ([(10, 5, "ge", "11112"), - ], - [(0, "y"*100)], - None, - )}, [(10,5)]) - self.failUnlessEqual(answer, (False, {0: ["11111"]})) - self.failUnlessEqual(read("si1", [0], [(0,100)]), {0: [data]}) - reset() + d.addCallback(lambda ign: write("si1", secrets, {0: ([(10, 5, "ge", "11110"),], + [(0, "y"*100)], + None, + )}, [(10,5)])) + d.addCallback(lambda res: self.failUnlessEqual(res, (True, {0: ["11111"]}) )) + d.addCallback(lambda ign: read("si1", [0], [(0,100)])) + d.addCallback(lambda res: self.failUnlessEqual(res, {0: ["y"*100]})) + d.addCallback(_reset) + + d.addCallback(lambda ign: write("si1", secrets, {0: ([(10, 5, "ge", "11111"),], + [(0, "y"*100)], + None, + )}, [(10,5)])) + d.addCallback(lambda res: self.failUnlessEqual(res, (True, {0: ["11111"]}) )) + d.addCallback(lambda ign: read("si1", [0], [(0,100)])) + d.addCallback(lambda res: self.failUnlessEqual(res, {0: ["y"*100]})) + d.addCallback(_reset) + + d.addCallback(lambda ign: write("si1", secrets, {0: ([(10, 5, "ge", "11112"),], + [(0, "y"*100)], + None, + )}, [(10,5)])) + d.addCallback(lambda res: self.failUnlessEqual(res, (False, {0: ["11111"]}) )) + d.addCallback(lambda ign: read("si1", [0], [(0,100)])) + d.addCallback(lambda res: self.failUnlessEqual(res, {0: [data]})) + d.addCallback(_reset) # gt - answer = write("si1", secrets, {0: ([(10, 5, "gt", "11110"), - ], - [(0, "y"*100)], - None, - )}, [(10,5)]) - self.failUnlessEqual(answer, (True, {0: ["11111"]})) - self.failUnlessEqual(read("si1", [0], [(0,100)]), {0: ["y"*100]}) - reset() - - answer = write("si1", secrets, {0: ([(10, 5, "gt", "11111"), - ], - [(0, "x"*100)], - None, - )}, [(10,5)]) - self.failUnlessEqual(answer, (False, {0: ["11111"]})) - self.failUnlessEqual(read("si1", [0], [(0,100)]), {0: [data]}) - reset() - - answer = write("si1", secrets, {0: ([(10, 5, "gt", "11112"), - ], - [(0, "x"*100)], - None, - )}, [(10,5)]) - self.failUnlessEqual(answer, (False, {0: ["11111"]})) - self.failUnlessEqual(read("si1", [0], [(0,100)]), {0: [data]}) - reset() + d.addCallback(lambda ign: write("si1", secrets, {0: ([(10, 5, "gt", "11110"),], + [(0, "y"*100)], + None, + )}, [(10,5)])) + d.addCallback(lambda res: self.failUnlessEqual(res, (True, {0: ["11111"]}) )) + d.addCallback(lambda ign: read("si1", [0], [(0,100)])) + d.addCallback(lambda res: self.failUnlessEqual(res, {0: ["y"*100]})) + d.addCallback(_reset) + + d.addCallback(lambda ign: write("si1", secrets, {0: ([(10, 5, "gt", "11111"),], + [(0, "x"*100)], + None, + )}, [(10,5)])) + d.addCallback(lambda res: self.failUnlessEqual(res, (False, {0: ["11111"]}) )) + d.addCallback(lambda ign: read("si1", [0], [(0,100)])) + d.addCallback(lambda res: self.failUnlessEqual(res, {0: [data]})) + d.addCallback(_reset) + + d.addCallback(lambda ign: write("si1", secrets, {0: ([(10, 5, "gt", "11112"),], + [(0, "x"*100)], + None, + )}, [(10,5)])) + d.addCallback(lambda res: self.failUnlessEqual(res, (False, {0: ["11111"]}) )) + d.addCallback(lambda ign: read("si1", [0], [(0,100)])) + d.addCallback(lambda res: self.failUnlessEqual(res, {0: [data]})) + d.addCallback(_reset) # finally, test some operators against empty shares - answer = write("si1", secrets, {1: ([(10, 5, "eq", "11112"), - ], - [(0, "x"*100)], - None, - )}, [(10,5)]) - self.failUnlessEqual(answer, (False, {0: ["11111"]})) - self.failUnlessEqual(read("si1", [0], [(0,100)]), {0: [data]}) - reset() + d.addCallback(lambda ign: write("si1", secrets, {1: ([(10, 5, "eq", "11112"),], + [(0, "x"*100)], + None, + )}, [(10,5)])) + d.addCallback(lambda res: self.failUnlessEqual(res, (False, {0: ["11111"]}) )) + d.addCallback(lambda ign: read("si1", [0], [(0,100)])) + d.addCallback(lambda res: self.failUnlessEqual(res, {0: [data]})) + d.addCallback(_reset) + return d def test_readv(self): server = self.create("test_readv") @@ -1138,17 +1270,148 @@ class MutableServer(unittest.TestCase): write = aa.remote_slot_testv_and_readv_and_writev read = aa.remote_slot_readv data = [("%d" % i) * 100 for i in range(3)] - rc = write("si1", secrets, - {0: ([], [(0,data[0])], None), - 1: ([], [(0,data[1])], None), - 2: ([], [(0,data[2])], None), - }, []) - self.failUnlessEqual(rc, (True, {})) - - answer = read("si1", [], [(0, 10)]) - self.failUnlessEqual(answer, {0: ["0"*10], - 1: ["1"*10], - 2: ["2"*10]}) + + d = defer.succeed(None) + d.addCallback(lambda ign: write("si1", secrets, + {0: ([], [(0,data[0])], None), + 1: ([], [(0,data[1])], None), + 2: ([], [(0,data[2])], None), + }, [])) + d.addCallback(lambda res: self.failUnlessEqual(res, (True, {}) )) + + d.addCallback(lambda ign: read("si1", [], [(0, 10)])) + d.addCallback(lambda res: self.failUnlessEqual(res, {0: ["0"*10], + 1: ["1"*10], + 2: ["2"*10]})) + return d + + def test_writev(self): + # This is run for both the disk and cloud backends, but it is particularly + # designed to exercise the cloud backend's implementation of chunking for + # mutable shares, assuming that PREFERRED_CHUNK_SIZE has been patched to 500. + # Note that the header requires 472 bytes, so only the first 28 bytes of data are + # in the first chunk. + + server = self.create("test_writev") + aa = server.get_accountant().get_anonymous_account() + read = aa.remote_slot_readv + rstaraw = aa.remote_slot_testv_and_readv_and_writev + secrets = ( self.write_enabler("we1"), + self.renew_secret("we1"), + self.cancel_secret("we1") ) + + def _check(ign, writev, expected_data, expected_write_loads, expected_write_stores, + expected_read_loads, should_exist): + d2 = rstaraw("si1", secrets, {0: writev}, []) + if should_exist: + d2.addCallback(lambda res: self.failUnlessEqual(res, (True, {0:[]}) )) + else: + d2.addCallback(lambda res: self.failUnlessEqual(res, (True, {}) )) + d2.addCallback(lambda ign: self.check_load_store_counts(expected_write_loads, + expected_write_stores)) + d2.addCallback(lambda ign: self.reset_load_store_counts()) + + d2.addCallback(lambda ign: read("si1", [0], [(0, len(expected_data) + 1)])) + if expected_data == "": + d2.addCallback(lambda res: self.failUnlessEqual(res, {})) + else: + d2.addCallback(lambda res: self.failUnlessEqual(res, {0: [expected_data]})) + d2.addCallback(lambda ign: self.check_load_store_counts(expected_read_loads, 0)) + d2.addCallback(lambda ign: self.reset_load_store_counts()) + return d2 + + self.reset_load_store_counts() + d = self.allocate(aa, "si1", "we1", set([0]), 2725) + d.addCallback(_check, ([], [(0, "a"*10)], None), + "a"*10, + 1, 2, 1, True) + d.addCallback(_check, ([], [(20, "b"*18)], None), + "a"*10 + "\x00"*10 + "b"*18, + 1, 2, 2, True) + d.addCallback(_check, ([], [(1038, "c")], None), + "a"*10 + "\x00"*10 + "b"*18 + "\x00"*(490+500+10) + "c", + 2, 4, 4, True) + d.addCallback(_check, ([], [(0, "d"*1038)], None), + "d"*1038 + "c", + 2, 4, 4, True) + d.addCallback(_check, ([], [(2167, "a"*54)], None), + "d"*1038 + "c" + "\x00"*1128 + "a"*54, + 2, 4, 6, True) + # This pattern was observed from the MDMF publisher in v1.9.1. + # Notice the duplicated write of length 41 bytes at offset 0. + d.addCallback(_check, ([], [(2167, "e"*54), (123, "f"*347), (2221, "g"*32), (470, "h"*136), + (0, "i"*41), (606, "j"*66), (672, "k"*93), (59, "l"*64), + (41, "m"*18), (0, "i"*41)], None), + "i"*41 + "m"*18 + "l"*64 + "f"*347 + "h"*136 + "j"*66 + "k"*93 + "d"*273 + "c" + "\x00"*1128 + + "e"*54 + "g"*32, + 4, 4, 6, True) + # This should delete all chunks. + d.addCallback(_check, ([], [], 0), + "", + 1, 0, 0, True) + d.addCallback(_check, ([], [(2167, "e"*54), (123, "f"*347), (2221, "g"*32), (470, "h"*136), + (0, "i"*41), (606, "j"*66), (672, "k"*93), (59, "l"*64), + (41, "m"*18), (0, "i"*41)], None), + "i"*41 + "m"*18 + "l"*64 + "f"*347 + "h"*136 + "j"*66 + "k"*93 + "\x00"*1402 + + "e"*54 + "g"*32, + 0, 7, 6, False) + return d + + def test_remove(self): + server = self.create("test_remove") + aa = server.get_accountant().get_anonymous_account() + readv = aa.remote_slot_readv + writev = aa.remote_slot_testv_and_readv_and_writev + secrets = ( self.write_enabler("we1"), + self.renew_secret("we1"), + self.cancel_secret("we1") ) + + d = defer.succeed(None) + d.addCallback(lambda ign: self.allocate(aa, "si1", "we1", set([0,1,2]), 100)) + # delete sh0 by setting its size to zero + d.addCallback(lambda ign: writev("si1", secrets, + {0: ([], [], 0)}, + [])) + # the answer should mention all the shares that existed before the + # write + d.addCallback(lambda res: self.failUnlessEqual(res, (True, {0:[],1:[],2:[]}) )) + # but a new read should show only sh1 and sh2 + d.addCallback(lambda ign: readv("si1", [], [(0,10)])) + d.addCallback(lambda res: self.failUnlessEqual(res, {1: [""], 2: [""]})) + + # delete sh1 by setting its size to zero + d.addCallback(lambda ign: writev("si1", secrets, + {1: ([], [], 0)}, + [])) + d.addCallback(lambda res: self.failUnlessEqual(res, (True, {1:[],2:[]}) )) + d.addCallback(lambda ign: readv("si1", [], [(0,10)])) + d.addCallback(lambda res: self.failUnlessEqual(res, {2: [""]})) + + # delete sh2 by setting its size to zero + d.addCallback(lambda ign: writev("si1", secrets, + {2: ([], [], 0)}, + [])) + d.addCallback(lambda res: self.failUnlessEqual(res, (True, {2:[]}) )) + d.addCallback(lambda ign: readv("si1", [], [(0,10)])) + d.addCallback(lambda res: self.failUnlessEqual(res, {})) + + d.addCallback(lambda ign: server.backend.get_shareset("si1").get_overhead()) + d.addCallback(lambda overhead: self.failUnlessEqual(overhead, 0)) + + # and the shareset directory should now be gone. This check is only + # applicable to the disk backend. + def _check_gone(ign): + si = base32.b2a("si1") + # note: this is a detail of the disk backend, and may change in the future + prefix = si[:2] + prefixdir = os.path.join(self.workdir("test_remove"), "shares", prefix) + sidir = os.path.join(prefixdir, si) + self.failUnless(os.path.exists(prefixdir), prefixdir) + self.failIf(os.path.exists(sidir), sidir) + + if isinstance(server.backend, DiskBackend): + d.addCallback(_check_gone) + return d def compare_leases(self, leases_a, leases_b, with_timestamps=True): self.failUnlessEqual(len(leases_a), len(leases_b)) @@ -1160,8 +1423,8 @@ class MutableServer(unittest.TestCase): self.failUnlessEqual(a.renewal_time, b.renewal_time) self.failUnlessEqual(a.expiration_time, b.expiration_time) - def test_leases(self): - server = self.create("test_leases") + def test_mutable_leases(self): + server = self.create("test_mutable_leases") aa = server.get_accountant().get_anonymous_account() sa = server.get_accountant().get_starter_account() @@ -1170,11 +1433,13 @@ class MutableServer(unittest.TestCase): self.renew_secret("we1-%d" % n), self.cancel_secret("we1-%d" % n) ) data = "".join([ ("%d" % i) * 10 for i in range(10) ]) - write = aa.remote_slot_testv_and_readv_and_writev - write2 = sa.remote_slot_testv_and_readv_and_writev + aa_write = aa.remote_slot_testv_and_readv_and_writev + sa_write = sa.remote_slot_testv_and_readv_and_writev read = aa.remote_slot_readv - rc = write("si0", secrets(0), {0: ([], [(0,data)], None)}, []) - self.failUnlessEqual(rc, (True, {})) + + # There is no such method as remote_cancel_lease -- see ticket #1528. + self.failIf(hasattr(aa, 'remote_cancel_lease'), + "aa should not have a 'remote_cancel_lease' method/attribute") # create a random non-numeric file in the bucket directory, to # exercise the code that's supposed to ignore those. @@ -1184,8 +1449,8 @@ class MutableServer(unittest.TestCase): fileutil.write(os.path.join(bucket_dir, "ignore_me.txt"), "you ought to be ignoring me\n") - s0 = MutableShareFile(os.path.join(bucket_dir, "0")) - s0.create("nodeid", secrets(0)[0]) + create_mutable_disk_share(os.path.join(bucket_dir, "0"), server.get_serverid(), + secrets(0)[0], storage_index="six", shnum=0) aa.add_share("six", 0, 0, SHARETYPE_MUTABLE) # adding a share does not immediately add a lease @@ -1194,101 +1459,535 @@ class MutableServer(unittest.TestCase): aa.add_or_renew_default_lease("six", 0) self.failUnlessEqual(len(aa.get_leases("six")), 1) + d = defer.succeed(None) + + d.addCallback(lambda ign: aa_write("si0", secrets(1), {0: ([], [(0,data)], None)}, [])) + d.addCallback(lambda res: self.failUnlessEqual(res, (True, {}))) + # add-lease on a missing storage index is silently ignored - self.failUnlessEqual(aa.remote_add_lease("si18", "", ""), None) - self.failUnlessEqual(len(aa.get_leases("si18")), 0) + d.addCallback(lambda ign: aa.remote_add_lease("si18", "", "")) + d.addCallback(lambda res: self.failUnless(res is None, res)) + d.addCallback(lambda ign: self.failUnlessEqual(len(aa.get_leases("si18")), 0)) - # update the lease by writing - write("si1", secrets(0), {0: ([], [(0,data)], None)}, []) - self.failUnlessEqual(len(aa.get_leases("si1")), 1) + # create a lease by writing + d.addCallback(lambda ign: aa_write("si1", secrets(2), {0: ([], [(0,data)], None)}, [])) + d.addCallback(lambda ign: self.failUnlessEqual(len(aa.get_leases("si1")), 1)) # renew it directly - aa.remote_renew_lease("si1", secrets(0)[1]) - self.failUnlessEqual(len(aa.get_leases("si1")), 1) + d.addCallback(lambda ign: aa.remote_renew_lease("si1", secrets(2)[1])) + d.addCallback(lambda ign: self.failUnlessEqual(len(aa.get_leases("si1")), 1)) # now allocate another lease using a different account - write2("si1", secrets(1), {0: ([], [(0,data)], None)}, []) - self.failUnlessEqual(len(aa.get_leases("si1")), 1) - self.failUnlessEqual(len(sa.get_leases("si1")), 1) + d.addCallback(lambda ign: sa_write("si1", secrets(3), {0: ([], [(0,data)], None)}, [])) + def _check(ign): + aa_leases = aa.get_leases("si1") + sa_leases = sa.get_leases("si1") + + self.failUnlessEqual(len(aa_leases), 1) + self.failUnlessEqual(len(sa_leases), 1) + + d2 = defer.succeed(None) + d2.addCallback(lambda ign: aa.remote_renew_lease("si1", secrets(2)[1])) + d2.addCallback(lambda ign: self.compare_leases(aa_leases, aa.get_leases("si1"), + with_timestamps=False)) + + d2.addCallback(lambda ign: sa.remote_renew_lease("si1", "shouldn't matter")) + d2.addCallback(lambda ign: self.compare_leases(sa_leases, sa.get_leases("si1"), + with_timestamps=False)) + + # Get a new copy of the leases, with the current timestamps. Reading + # data should leave the timestamps alone. + d2.addCallback(lambda ign: aa.get_leases("si1")) + def _check2(new_aa_leases): + # reading shares should not modify the timestamp + d3 = read("si1", [], [(0, 200)]) + d3.addCallback(lambda ign: self.compare_leases(new_aa_leases, aa.get_leases("si1"), + with_timestamps=False)) + + d3.addCallback(lambda ign: aa_write("si1", secrets(2), + {0: ([], [(500, "make me bigger")], None)}, [])) + d3.addCallback(lambda ign: self.compare_leases(new_aa_leases, aa.get_leases("si1"), + with_timestamps=False)) + return d3 + d2.addCallback(_check2) + return d2 + d.addCallback(_check) + return d + - aa_leases = aa.get_leases("si1") - sa_leases = sa.get_leases("si1") +class ServerWithNullBackend(ServiceParentMixin, WorkdirMixin, ServerMixin, unittest.TestCase): + def test_null_backend(self): + workdir = self.workdir("test_null_backend") + backend = NullBackend() + server = StorageServer("\x00" * 20, backend, workdir) + server.setServiceParent(self.sparent) + aa = server.get_accountant().get_anonymous_account() - aa.remote_renew_lease("si1", secrets(0)[1]) - self.compare_leases(aa_leases, aa.get_leases("si1"), with_timestamps=False) + d = self.allocate(aa, "vid", [0,1,2], 75) + def _allocated( (already, writers) ): + self.failUnlessEqual(already, set()) + self.failUnlessEqual(set(writers.keys()), set([0,1,2])) + + d2 = for_items(self._write_and_close, writers) + + # The shares should be present but have no data. + d2.addCallback(lambda ign: aa.remote_get_buckets("vid")) + def _check(buckets): + self.failUnlessEqual(set(buckets.keys()), set([0,1,2])) + d3 = defer.succeed(None) + d3.addCallback(lambda ign: buckets[0].remote_read(0, 25)) + d3.addCallback(lambda res: self.failUnlessEqual(res, "")) + return d3 + d2.addCallback(_check) + return d2 + d.addCallback(_allocated) + return d - sa.remote_renew_lease("si1", secrets(1)[1]) - self.compare_leases(sa_leases, sa.get_leases("si1"), with_timestamps=False) - # get a new copy of the leases, with the current timestamps. Reading - # data should leave the timestamps alone. - aa_leases = aa.get_leases("si1") +class WithMockCloudBackend(ServiceParentMixin, WorkdirMixin): + def create(self, name, detached=False, readonly=False, reserved_space=0, klass=StorageServer): + assert not readonly + workdir = self.workdir(name) + self._container = MockContainer(workdir) + backend = CloudBackend(self._container) + server = klass("\x00" * 20, backend, workdir, + stats_provider=FakeStatsProvider()) + if not detached: + server.setServiceParent(self.sparent) + return server - # reading shares should not modify the timestamp - read("si1", [], [(0,200)]) - self.compare_leases(aa_leases, aa.get_leases("si1")) + def reset_load_store_counts(self): + self._container.reset_load_store_counts() - write("si1", secrets(0), - {0: ([], [(200, "make me bigger")], None)}, []) - self.compare_leases(aa_leases, aa.get_leases("si1"), with_timestamps=False) + def check_load_store_counts(self, expected_load_count, expected_store_count): + self.failUnlessEqual((self._container.get_load_count(), self._container.get_store_count()), + (expected_load_count, expected_store_count)) - write("si1", secrets(0), - {0: ([], [(500, "make me really bigger")], None)}, []) - self.compare_leases(aa_leases, aa.get_leases("si1"), with_timestamps=False) - def test_remove(self): - server = self.create("test_remove") +class WithDiskBackend(ServiceParentMixin, WorkdirMixin): + def create(self, name, detached=False, readonly=False, reserved_space=0, klass=StorageServer): + workdir = self.workdir(name) + backend = DiskBackend(workdir, readonly=readonly, reserved_space=reserved_space) + server = klass("\x00" * 20, backend, workdir, + stats_provider=FakeStatsProvider()) + if not detached: + server.setServiceParent(self.sparent) + return server + + def reset_load_store_counts(self): + pass + + def check_load_store_counts(self, expected_loads, expected_stores): + pass + + +class ServerWithMockCloudBackend(WithMockCloudBackend, ServerTest, unittest.TestCase): + def setUp(self): + ServiceParentMixin.setUp(self) + + # A smaller chunk size causes the tests to exercise more cases in the chunking implementation. + self.patch(cloud_common, 'PREFERRED_CHUNK_SIZE', 500) + + # This causes ContainerListMixin to be exercised. + self.patch(mock_cloud, 'MAX_KEYS', 2) + + def test_bad_container_version(self): + return ServerTest.test_bad_container_version(self) + test_bad_container_version.todo = "The cloud backend hasn't been modified to fix ticket #1566." + + + def _describe_level(self, level): + return getattr(LogEvent, 'LEVELMAP', {}).get(level, str(level)) + + def _test_cloud_retry(self, name, failure_count, levels): + self.patch(cloud_common, 'BACKOFF_SECONDS_FOR_5XX', (0, 0.1, 0.2)) + + t = {'count': 0} + old_put_object = MockContainer._put_object + def call_put_object(self, ign, object_name, data, content_type=None, metadata={}): + t['count'] += 1 + if t['count'] <= failure_count: + return defer.fail(MockServiceError("XML", 500, "Internal error", "response")) + else: + return old_put_object(self, ign, object_name, data, content_type=content_type, metadata=metadata) + self.patch(MockContainer, '_put_object', call_put_object) + + def call_log_msg(*args, **kwargs): + # the log message and parameters should not include the data + self.failIfIn("%25d" % (0,), repr( (args, kwargs) )) + level = kwargs.get("level", OPERATIONAL) + if level > OPERATIONAL: + levels.append(level) + self.patch(cloud_common.log, 'msg', call_log_msg) + + server = self.create(name) aa = server.get_accountant().get_anonymous_account() - self.allocate(aa, "si1", "we1", self._lease_secret.next(), - set([0,1,2]), 100) - readv = aa.remote_slot_readv - writev = aa.remote_slot_testv_and_readv_and_writev - secrets = ( self.write_enabler("we1"), - self.renew_secret("we1"), - self.cancel_secret("we1") ) - # delete sh0 by setting its size to zero - answer = writev("si1", secrets, - {0: ([], [], 0)}, - []) - # the answer should mention all the shares that existed before the - # write - self.failUnlessEqual(answer, (True, {0:[],1:[],2:[]}) ) - # but a new read should show only sh1 and sh2 - self.failUnlessEqual(readv("si1", [], [(0,10)]), - {1: [""], 2: [""]}) + d = self.allocate(aa, "vid", [0], 75) + d.addCallback(lambda (already, writers): for_items(self._write_and_close, writers)) + return d + + def test_cloud_retry_fail(self): + levels = [] # list of logging levels above OPERATIONAL for calls to log.msg + d = self._test_cloud_retry("test_cloud_retry_fail", 4, levels) + # shouldFail would check repr(res.value.args[0]) which is not what we want + def done(res): + if isinstance(res, Failure): + res.trap(cloud_common.CloudError) + self.failUnlessIn(", 500, 'Internal error', 'response')", str(res.value)) + # the stringified exception should not include the data + self.failIfIn("%25d" % (0,), str(res.value)) + desc = ", ".join(map(self._describe_level, levels)) + self.failUnlessEqual(levels, [INFREQUENT]*4 + [WEIRD], desc) + else: + self.fail("was supposed to raise CloudError, not get %r" % (res,)) + d.addBoth(done) + return d + + def test_cloud_retry_succeed(self): + levels = [] # list of logging levels above OPERATIONAL for calls to log.msg + d = self._test_cloud_retry("test_cloud_retry_succeed", 3, levels) + def done(res): + desc = ", ".join(map(self._describe_level, levels)) + self.failUnlessEqual(levels, [INFREQUENT]*3 + [WEIRD], desc) + d.addCallback(done) + return d + + +class ServerWithDiskBackend(WithDiskBackend, ServerTest, unittest.TestCase): + # The following tests are for behaviour that is only supported by a disk backend. + + def test_readonly(self): + server = self.create("test_readonly", readonly=True) + aa = server.get_accountant().get_anonymous_account() + + d = self.allocate(aa, "vid", [0,1,2], 75) + def _allocated( (already, writers) ): + self.failUnlessEqual(already, set()) + self.failUnlessEqual(writers, {}) + + stats = server.get_stats() + self.failUnlessEqual(stats["storage_server.accepting_immutable_shares"], 0) + if "storage_server.disk_avail" in stats: + # Some platforms may not have an API to get disk stats. + # But if there are stats, readonly_storage means disk_avail=0 + self.failUnlessEqual(stats["storage_server.disk_avail"], 0) + d.addCallback(_allocated) + return d + + def test_large_share(self): + syslow = platform.system().lower() + if 'cygwin' in syslow or 'windows' in syslow or 'darwin' in syslow: + raise unittest.SkipTest("If your filesystem doesn't support efficient sparse files then it is very expensive (Mac OS X and Windows don't support efficient sparse files).") + + avail = fileutil.get_available_space('.', 512*2**20) + if avail <= 4*2**30: + raise unittest.SkipTest("This test will spuriously fail if you have less than 4 GiB free on your filesystem.") + + server = self.create("test_large_share") + aa = server.get_accountant().get_anonymous_account() + + d = self.allocate(aa, "allocate", [0], 2**32+2) + def _allocated( (already, writers) ): + self.failUnlessEqual(already, set()) + self.failUnlessEqual(set(writers.keys()), set([0])) + + shnum, bucket = writers.items()[0] + + # This test is going to hammer your filesystem if it doesn't make a sparse file for this. :-( + d2 = defer.succeed(None) + d2.addCallback(lambda ign: bucket.remote_write(2**32, "ab")) + d2.addCallback(lambda ign: bucket.remote_close()) + + d2.addCallback(lambda ign: aa.remote_get_buckets("allocate")) + d2.addCallback(lambda readers: readers[shnum].remote_read(2**32, 2)) + d2.addCallback(lambda res: self.failUnlessEqual(res, "ab")) + return d2 + d.addCallback(_allocated) + return d + + def test_remove_incoming(self): + server = self.create("test_remove_incoming") + aa = server.get_accountant().get_anonymous_account() + + d = self.allocate(aa, "vid", range(3), 25) + def _write_and_check( (already, writers) ): + d2 = defer.succeed(None) + for i, bw in sorted(writers.items()): + incoming_share_home = bw._share._get_path() + d2.addCallback(self._write_and_close, i, bw) + + def _check(ign): + incoming_si_dir = os.path.dirname(incoming_share_home) + incoming_prefix_dir = os.path.dirname(incoming_si_dir) + incoming_dir = os.path.dirname(incoming_prefix_dir) + + self.failIf(os.path.exists(incoming_si_dir), incoming_si_dir) + self.failIf(os.path.exists(incoming_prefix_dir), incoming_prefix_dir) + self.failUnless(os.path.exists(incoming_dir), incoming_dir) + d2.addCallback(_check) + return d2 + d.addCallback(_write_and_check) + return d + + def test_abort(self): + # remote_abort, when called on a writer, should make sure that + # the allocated size of the bucket is not counted by the storage + # server when accounting for space. + server = self.create("test_abort") + aa = server.get_accountant().get_anonymous_account() + + d = self.allocate(aa, "allocate", [0, 1, 2], 150) + def _allocated( (already, writers) ): + self.failIfEqual(server.allocated_size(), 0) + + # Now abort the writers. + d2 = for_items(self._abort_writer, writers) + d2.addCallback(lambda ign: self.failUnlessEqual(server.allocated_size(), 0)) + return d2 + d.addCallback(_allocated) + return d + + def test_disconnect(self): + # simulate a disconnection + server = self.create("test_disconnect") + aa = server.get_accountant().get_anonymous_account() + canary = FakeCanary() + + d = self.allocate(aa, "disconnect", [0,1,2], 75, canary) + def _allocated( (already, writers) ): + self.failUnlessEqual(already, set()) + self.failUnlessEqual(set(writers.keys()), set([0,1,2])) + for (f,args,kwargs) in canary.disconnectors.values(): + f(*args, **kwargs) + d.addCallback(_allocated) + + # returning from _allocated ought to delete the incoming shares + d.addCallback(lambda ign: self.allocate(aa, "disconnect", [0,1,2], 75)) + def _allocated2( (already, writers) ): + self.failUnlessEqual(already, set()) + self.failUnlessEqual(set(writers.keys()), set([0,1,2])) + d.addCallback(_allocated2) + return d + + @mock.patch('allmydata.util.fileutil.get_disk_stats') + def test_reserved_space(self, mock_get_disk_stats): + reserved_space=10000 + mock_get_disk_stats.return_value = { + 'free_for_nonroot': 15000, + 'avail': max(15000 - reserved_space, 0), + } + + server = self.create("test_reserved_space", reserved_space=reserved_space) + aa = server.get_accountant().get_anonymous_account() + + # 15k available, 10k reserved, leaves 5k for shares + + # a newly created and filled share incurs this much overhead, beyond + # the size we request. + OVERHEAD = 3*4 + LEASE_SIZE = 4+32+32+4 + canary = FakeCanary(True) + + d = self.allocate(aa, "vid1", [0,1,2], 1000, canary) + def _allocated( (already, writers) ): + self.failUnlessEqual(len(writers), 3) + # now the StorageServer should have 3000 bytes provisionally + # allocated, allowing only 2000 more to be claimed + self.failUnlessEqual(len(server._active_writers), 3) + self.writers = writers + del already + + # allocating 1001-byte shares only leaves room for one + d2 = self.allocate(aa, "vid2", [0,1,2], 1001, canary) + def _allocated2( (already2, writers2) ): + self.failUnlessEqual(len(writers2), 1) + self.failUnlessEqual(len(server._active_writers), 4) + + # we abandon the first set, so their provisional allocation should be + # returned + d3 = for_items(self._abort_writer, self.writers) + #def _del_writers(ign): + # del self.writers + #d3.addCallback(_del_writers) + d3.addCallback(lambda ign: self.failUnlessEqual(len(server._active_writers), 1)) + + # and we close the second set, so their provisional allocation should + # become real, long-term allocation, and grows to include the + # overhead. + d3.addCallback(lambda ign: for_items(self._write_and_close, writers2)) + d3.addCallback(lambda ign: self.failUnlessEqual(len(server._active_writers), 0)) + return d3 + d2.addCallback(_allocated2) + + allocated = 1001 + OVERHEAD + LEASE_SIZE + + # we have to manually increase available, since we're not doing real + # disk measurements + def _mock(ign): + mock_get_disk_stats.return_value = { + 'free_for_nonroot': 15000 - allocated, + 'avail': max(15000 - allocated - reserved_space, 0), + } + d2.addCallback(_mock) + + # now there should be ALLOCATED=1001+12+72=1085 bytes allocated, and + # 5000-1085=3915 free, therefore we can fit 39 100byte shares + d2.addCallback(lambda ign: self.allocate(aa, "vid3", range(100), 100, canary)) + def _allocated3( (already3, writers3) ): + self.failUnlessEqual(len(writers3), 39) + self.failUnlessEqual(len(server._active_writers), 39) + + d3 = for_items(self._abort_writer, writers3) + d3.addCallback(lambda ign: self.failUnlessEqual(len(server._active_writers), 0)) + d3.addCallback(lambda ign: server.disownServiceParent()) + return d3 + d2.addCallback(_allocated3) + d.addCallback(_allocated) + return d + + def OFF_test_immutable_leases(self): + server = self.create("test_immutable_leases") + aa = server.get_accountant().get_anonymous_account() + canary = FakeCanary() + sharenums = range(5) + size = 100 - # delete sh1 by setting its size to zero - answer = writev("si1", secrets, - {1: ([], [], 0)}, - []) - self.failUnlessEqual(answer, (True, {1:[],2:[]}) ) - self.failUnlessEqual(readv("si1", [], [(0,10)]), - {2: [""]}) + rs = [] + cs = [] + for i in range(6): + rs.append(hashutil.tagged_hash("blah", "%d" % self._lease_secret.next())) + cs.append(hashutil.tagged_hash("blah", "%d" % self._lease_secret.next())) + + d = aa.remote_allocate_buckets("si0", rs[0], cs[0], + sharenums, size, canary) + def _allocated( (already, writers) ): + self.failUnlessEqual(len(already), 0) + self.failUnlessEqual(len(writers), 5) + + d2 = for_items(self._close_writer, writers) + + d2.addCallback(lambda ign: list(aa.get_leases("si0"))) + d2.addCallback(lambda leases: self.failUnlessEqual(len(leases), 1)) + + d2.addCallback(lambda ign: aa.remote_allocate_buckets("si1", rs[1], cs[1], + sharenums, size, canary)) + return d2 + d.addCallback(_allocated) + + def _allocated2( (already, writers) ): + d2 = for_items(self._close_writer, writers) + + # take out a second lease on si1 + d2.addCallback(lambda ign: aa.remote_allocate_buckets("si1", rs[2], cs[2], + sharenums, size, canary)) + return d2 + d.addCallback(_allocated2) + + def _allocated2a( (already, writers) ): + self.failUnlessEqual(len(already), 5) + self.failUnlessEqual(len(writers), 0) + + d2 = defer.succeed(None) + d2.addCallback(lambda ign: list(aa.get_leases("si1"))) + d2.addCallback(lambda leases: self.failUnlessEqual(len(leases), 2)) + + # and a third lease, using add-lease + d2.addCallback(lambda ign: aa.remote_add_lease("si1", rs[3], cs[3])) + + d2.addCallback(lambda ign: list(aa.get_leases("si1"))) + d2.addCallback(lambda leases: self.failUnlessEqual(len(leases), 3)) + + # add-lease on a missing storage index is silently ignored + d2.addCallback(lambda ign: aa.remote_add_lease("si18", "", "")) + d2.addCallback(lambda res: self.failUnlessEqual(res, None)) + + # check that si0 is readable + d2.addCallback(lambda ign: aa.remote_get_buckets("si0")) + d2.addCallback(lambda readers: self.failUnlessEqual(len(readers), 5)) + + # renew the first lease. Only the proper renew_secret should work + d2.addCallback(lambda ign: aa.remote_renew_lease("si0", rs[0])) + d2.addCallback(lambda ign: self.shouldFail(IndexError, 'wrong secret 1', None, + lambda: aa.remote_renew_lease("si0", cs[0]) )) + d2.addCallback(lambda ign: self.shouldFail(IndexError, 'wrong secret 2', None, + lambda: aa.remote_renew_lease("si0", rs[1]) )) + + # check that si0 is still readable + d2.addCallback(lambda ign: aa.remote_get_buckets("si0")) + d2.addCallback(lambda readers: self.failUnlessEqual(len(readers), 5)) + + # There is no such method as remote_cancel_lease for now -- see + # ticket #1528. + d2.addCallback(lambda ign: self.failIf(hasattr(aa, 'remote_cancel_lease'), + "aa should not have a 'remote_cancel_lease' method/attribute")) + + # test overlapping uploads + d2.addCallback(lambda ign: aa.remote_allocate_buckets("si3", rs[4], cs[4], + sharenums, size, canary)) + return d2 + d.addCallback(_allocated2a) + + def _allocated4( (already, writers) ): + self.failUnlessEqual(len(already), 0) + self.failUnlessEqual(len(writers), 5) + + d2 = defer.succeed(None) + d2.addCallback(lambda ign: aa.remote_allocate_buckets("si3", rs[5], cs[5], + sharenums, size, canary)) + def _allocated5( (already2, writers2) ): + self.failUnlessEqual(len(already2), 0) + self.failUnlessEqual(len(writers2), 0) + + d3 = for_items(self._close_writer, writers) + + d3.addCallback(lambda ign: list(aa.get_leases("si3"))) + d3.addCallback(lambda leases: self.failUnlessEqual(len(leases), 1)) + + d3.addCallback(lambda ign: aa.remote_allocate_buckets("si3", rs[5], cs[5], + sharenums, size, canary)) + return d3 + d2.addCallback(_allocated5) + + def _allocated6( (already3, writers3) ): + self.failUnlessEqual(len(already3), 5) + self.failUnlessEqual(len(writers3), 0) + + d3 = defer.succeed(None) + d3.addCallback(lambda ign: list(aa.get_leases("si3"))) + d3.addCallback(lambda leases: self.failUnlessEqual(len(leases), 2)) + return d3 + d2.addCallback(_allocated6) + return d2 + d.addCallback(_allocated4) + return d - # delete sh2 by setting its size to zero - answer = writev("si1", secrets, - {2: ([], [], 0)}, - []) - self.failUnlessEqual(answer, (True, {2:[]}) ) - self.failUnlessEqual(readv("si1", [], [(0,10)]), - {}) - # and the bucket directory should now be gone - si = base32.b2a("si1") - # note: this is a detail of the storage server implementation, and - # may change in the future - prefix = si[:2] - prefixdir = os.path.join(self.workdir("test_remove"), "shares", prefix) - bucketdir = os.path.join(prefixdir, si) - self.failUnless(os.path.exists(prefixdir), prefixdir) - self.failIf(os.path.exists(bucketdir), bucketdir) - - -class MDMFProxies(unittest.TestCase, ShouldFailMixin): + +class MutableServerWithMockCloudBackend(WithMockCloudBackend, MutableServerTest, unittest.TestCase): def setUp(self): - self.sparent = LoggingServiceParent() + ServiceParentMixin.setUp(self) + + # A smaller chunk size causes the tests to exercise more cases in the chunking implementation. + self.patch(cloud_common, 'PREFERRED_CHUNK_SIZE', 500) + + # This causes ContainerListMixin to be exercised. + self.patch(mock_cloud, 'MAX_KEYS', 2) + + def test_bad_magic(self): + return MutableServerTest.test_bad_magic(self) + test_bad_magic.todo = "The cloud backend hasn't been modified to fix ticket #1566." + + +class MutableServerWithDiskBackend(WithDiskBackend, MutableServerTest, unittest.TestCase): + # There are no mutable tests specific to a disk backend. + pass + + +class MDMFProxies(WithDiskBackend, ShouldFailMixin, unittest.TestCase): + def init(self, name): self._lease_secret = itertools.count() - self.aa = self.create("MDMFProxies storage test server") + self.server = self.create(name) + self.aa = self.server.get_accountant().get_anonymous_account() self.rref = RemoteBucket() self.rref.target = self.aa self.secrets = (self.write_enabler("we_secret"), @@ -1314,11 +2013,6 @@ class MDMFProxies(unittest.TestCase, ShouldFailMixin): # header. self.salt_hash_tree_s = self.serialize_blockhashes(self.salt_hash_tree[1:]) - def tearDown(self): - self.sparent.stopService() - shutil.rmtree(self.workdir("MDMFProxies storage test server")) - - def write_enabler(self, we_tag): return hashutil.tagged_hash("we_blah", we_tag) @@ -1328,16 +2022,6 @@ class MDMFProxies(unittest.TestCase, ShouldFailMixin): def cancel_secret(self, tag): return hashutil.tagged_hash("cancel_blah", str(tag)) - def workdir(self, name): - basedir = os.path.join("storage", "MutableServer", name) - return basedir - - def create(self, name): - workdir = self.workdir(name) - server = StorageServer(workdir, "\x00" * 20) - server.setServiceParent(self.sparent) - return server.get_accountant().get_anonymous_account() - def build_test_mdmf_share(self, tail_segment=False, empty=False): # Start with the checkstring data = struct.pack(">BQ32s", @@ -1453,8 +2137,9 @@ class MDMFProxies(unittest.TestCase, ShouldFailMixin): tws = {} tws[0] = (testvs, [(0, data)], None) readv = [(0, 1)] - results = write(storage_index, self.secrets, tws, readv) - self.failUnless(results[0]) + d = write(storage_index, self.secrets, tws, readv) + d.addCallback(lambda res: self.failUnless(res[0])) + return d def build_test_sdmf_share(self, empty=False): if empty: @@ -1518,15 +2203,19 @@ class MDMFProxies(unittest.TestCase, ShouldFailMixin): tws = {} tws[0] = (testvs, [(0, share)], None) readv = [] - results = write(storage_index, self.secrets, tws, readv) - self.failUnless(results[0]) + d = write(storage_index, self.secrets, tws, readv) + d.addCallback(lambda res: self.failUnless(res[0])) + return d def test_read(self): - self.write_test_share_to_server("si1") + self.init("test_read") + mr = MDMFSlotReadProxy(self.rref, "si1", 0) - # Check that every method equals what we expect it to. d = defer.succeed(None) + d.addCallback(lambda ign: self.write_test_share_to_server("si1")) + + # Check that every method equals what we expect it to. def _check_block_and_salt((block, salt)): self.failUnlessEqual(block, self.block) self.failUnlessEqual(salt, self.salt) @@ -1592,9 +2281,13 @@ class MDMFProxies(unittest.TestCase, ShouldFailMixin): return d def test_read_with_different_tail_segment_size(self): - self.write_test_share_to_server("si1", tail_segment=True) + self.init("test_read_with_different_tail_segment_size") + mr = MDMFSlotReadProxy(self.rref, "si1", 0) - d = mr.get_block_and_salt(5) + d = defer.succeed(None) + d.addCallback(lambda ign: self.write_test_share_to_server("si1", tail_segment=True)) + + d.addCallback(lambda ign: mr.get_block_and_salt(5)) def _check_tail_segment(results): block, salt = results self.failUnlessEqual(len(block), 1) @@ -1603,9 +2296,11 @@ class MDMFProxies(unittest.TestCase, ShouldFailMixin): return d def test_get_block_with_invalid_segnum(self): - self.write_test_share_to_server("si1") + self.init("test_get_block_with_invalid_segnum") + mr = MDMFSlotReadProxy(self.rref, "si1", 0) d = defer.succeed(None) + d.addCallback(lambda ign: self.write_test_share_to_server("si1")) d.addCallback(lambda ignored: self.shouldFail(LayoutInvalid, "test invalid segnum", None, @@ -1613,9 +2308,12 @@ class MDMFProxies(unittest.TestCase, ShouldFailMixin): return d def test_get_encoding_parameters_first(self): - self.write_test_share_to_server("si1") + self.init("test_get_encoding_parameters_first") + mr = MDMFSlotReadProxy(self.rref, "si1", 0) - d = mr.get_encoding_parameters() + d = defer.succeed(None) + d.addCallback(lambda ign: self.write_test_share_to_server("si1")) + d.addCallback(lambda ign: mr.get_encoding_parameters()) def _check_encoding_parameters((k, n, segment_size, datalen)): self.failUnlessEqual(k, 3) self.failUnlessEqual(n, 10) @@ -1625,30 +2323,41 @@ class MDMFProxies(unittest.TestCase, ShouldFailMixin): return d def test_get_seqnum_first(self): - self.write_test_share_to_server("si1") + self.init("test_get_seqnum_first") + mr = MDMFSlotReadProxy(self.rref, "si1", 0) - d = mr.get_seqnum() + d = defer.succeed(None) + d.addCallback(lambda ign: self.write_test_share_to_server("si1")) + d.addCallback(lambda ign: mr.get_seqnum()) d.addCallback(lambda seqnum: self.failUnlessEqual(seqnum, 0)) return d def test_get_root_hash_first(self): - self.write_test_share_to_server("si1") + self.init("test_root_hash_first") + mr = MDMFSlotReadProxy(self.rref, "si1", 0) - d = mr.get_root_hash() + d = defer.succeed(None) + d.addCallback(lambda ign: self.write_test_share_to_server("si1")) + d.addCallback(lambda ign: mr.get_root_hash()) d.addCallback(lambda root_hash: self.failUnlessEqual(root_hash, self.root_hash)) return d def test_get_checkstring_first(self): - self.write_test_share_to_server("si1") + self.init("test_checkstring_first") + mr = MDMFSlotReadProxy(self.rref, "si1", 0) - d = mr.get_checkstring() + d = defer.succeed(None) + d.addCallback(lambda ign: self.write_test_share_to_server("si1")) + d.addCallback(lambda ign: mr.get_checkstring()) d.addCallback(lambda checkstring: self.failUnlessEqual(checkstring, self.checkstring)) return d def test_write_read_vectors(self): + self.init("test_write_read_vectors") + # When writing for us, the storage server will return to us a # read vector, along with its result. If a write fails because # the test vectors failed, this read vector can help us to @@ -1664,6 +2373,7 @@ class MDMFProxies(unittest.TestCase, ShouldFailMixin): mw.put_root_hash(self.root_hash) mw.put_signature(self.signature) mw.put_verification_key(self.verification_key) + d = mw.finish_publishing() def _then(results): self.failUnless(len(results), 2) @@ -1687,6 +2397,8 @@ class MDMFProxies(unittest.TestCase, ShouldFailMixin): return d def test_private_key_after_share_hash_chain(self): + self.init("test_private_key_after_share_hash_chain") + mw = self._make_new_mw("si1", 0) d = defer.succeed(None) for i in xrange(6): @@ -1705,6 +2417,8 @@ class MDMFProxies(unittest.TestCase, ShouldFailMixin): return d def test_signature_after_verification_key(self): + self.init("test_signature_after_verification_key") + mw = self._make_new_mw("si1", 0) d = defer.succeed(None) # Put everything up to and including the verification key. @@ -1731,6 +2445,8 @@ class MDMFProxies(unittest.TestCase, ShouldFailMixin): return d def test_uncoordinated_write(self): + self.init("test_uncoordinated_write") + # Make two mutable writers, both pointing to the same storage # server, both at the same storage index, and try writing to the # same share. @@ -1763,6 +2479,8 @@ class MDMFProxies(unittest.TestCase, ShouldFailMixin): return d def test_invalid_salt_size(self): + self.init("test_invalid_salt_size") + # Salts need to be 16 bytes in size. Writes that attempt to # write more or less than this should be rejected. mw = self._make_new_mw("si1", 0) @@ -1781,6 +2499,8 @@ class MDMFProxies(unittest.TestCase, ShouldFailMixin): return d def test_write_test_vectors(self): + self.init("test_write_test_vectors") + # If we give the write proxy a bogus test vector at # any point during the process, it should fail to write when we # tell it to write. @@ -1824,6 +2544,8 @@ class MDMFProxies(unittest.TestCase, ShouldFailMixin): def test_write(self): + self.init("test_write") + # This translates to a file with 6 6-byte segments, and with 2-byte # blocks. mw = self._make_new_mw("si1", 0) @@ -1846,103 +2568,90 @@ class MDMFProxies(unittest.TestCase, ShouldFailMixin): mw.put_root_hash(self.root_hash) mw.put_signature(self.signature) mw.put_verification_key(self.verification_key) + d = mw.finish_publishing() - def _check_publish(results): - self.failUnlessEqual(len(results), 2) - result, ign = results - self.failUnless(result, "publish failed") - for i in xrange(6): - self.failUnlessEqual(read("si1", [0], [(expected_sharedata_offset + (i * written_block_size), written_block_size)]), - {0: [written_block]}) + d.addCallback(lambda (result, ign): self.failUnless(result, "publish failed")) + + for i in xrange(6): + d.addCallback(lambda ign, i=i: read("si1", [0], + [(expected_sharedata_offset + (i * written_block_size), + written_block_size)])) + d.addCallback(lambda res: self.failUnlessEqual(res, {0: [written_block]})) - self.failUnlessEqual(len(self.encprivkey), 7) - self.failUnlessEqual(read("si1", [0], [(expected_private_key_offset, 7)]), - {0: [self.encprivkey]}) + d.addCallback(lambda ign: self.failUnlessEqual(len(self.encprivkey), 7)) + d.addCallback(lambda ign: read("si1", [0], [(expected_private_key_offset, 7)])) + d.addCallback(lambda res: self.failUnlessEqual(res, {0: [self.encprivkey]})) - expected_block_hash_offset = expected_sharedata_offset + \ - (6 * written_block_size) - self.failUnlessEqual(len(self.block_hash_tree_s), 32 * 6) - self.failUnlessEqual(read("si1", [0], [(expected_block_hash_offset, 32 * 6)]), - {0: [self.block_hash_tree_s]}) + expected_block_hash_offset = expected_sharedata_offset + (6 * written_block_size) + d.addCallback(lambda ign: self.failUnlessEqual(len(self.block_hash_tree_s), 32 * 6)) + d.addCallback(lambda ign, ebho=expected_block_hash_offset: + read("si1", [0], [(ebho, 32 * 6)])) + d.addCallback(lambda res: self.failUnlessEqual(res, {0: [self.block_hash_tree_s]})) expected_share_hash_offset = expected_private_key_offset + len(self.encprivkey) - self.failUnlessEqual(read("si1", [0],[(expected_share_hash_offset, (32 + 2) * 6)]), - {0: [self.share_hash_chain_s]}) + d.addCallback(lambda ign, esho=expected_share_hash_offset: + read("si1", [0], [(esho, (32 + 2) * 6)])) + d.addCallback(lambda res: self.failUnlessEqual(res, {0: [self.share_hash_chain_s]})) + + d.addCallback(lambda ign: read("si1", [0], [(9, 32)])) + d.addCallback(lambda res: self.failUnlessEqual(res, {0: [self.root_hash]})) - self.failUnlessEqual(read("si1", [0], [(9, 32)]), - {0: [self.root_hash]}) - expected_signature_offset = expected_share_hash_offset + \ - len(self.share_hash_chain_s) - self.failUnlessEqual(len(self.signature), 9) - self.failUnlessEqual(read("si1", [0], [(expected_signature_offset, 9)]), - {0: [self.signature]}) + expected_signature_offset = expected_share_hash_offset + len(self.share_hash_chain_s) + d.addCallback(lambda ign: self.failUnlessEqual(len(self.signature), 9)) + d.addCallback(lambda ign, esigo=expected_signature_offset: + read("si1", [0], [(esigo, 9)])) + d.addCallback(lambda res: self.failUnlessEqual(res, {0: [self.signature]})) expected_verification_key_offset = expected_signature_offset + len(self.signature) - self.failUnlessEqual(len(self.verification_key), 6) - self.failUnlessEqual(read("si1", [0], [(expected_verification_key_offset, 6)]), - {0: [self.verification_key]}) - - signable = mw.get_signable() - verno, seq, roothash, k, n, segsize, datalen = \ - struct.unpack(">BQ32sBBQQ", - signable) - self.failUnlessEqual(verno, 1) - self.failUnlessEqual(seq, 0) - self.failUnlessEqual(roothash, self.root_hash) - self.failUnlessEqual(k, 3) - self.failUnlessEqual(n, 10) - self.failUnlessEqual(segsize, 6) - self.failUnlessEqual(datalen, 36) - expected_eof_offset = expected_block_hash_offset + \ - len(self.block_hash_tree_s) - - # Check the version number to make sure that it is correct. - expected_version_number = struct.pack(">B", 1) - self.failUnlessEqual(read("si1", [0], [(0, 1)]), - {0: [expected_version_number]}) - # Check the sequence number to make sure that it is correct - expected_sequence_number = struct.pack(">Q", 0) - self.failUnlessEqual(read("si1", [0], [(1, 8)]), - {0: [expected_sequence_number]}) - # Check that the encoding parameters (k, N, segement size, data - # length) are what they should be. These are 3, 10, 6, 36 - expected_k = struct.pack(">B", 3) - self.failUnlessEqual(read("si1", [0], [(41, 1)]), - {0: [expected_k]}) - expected_n = struct.pack(">B", 10) - self.failUnlessEqual(read("si1", [0], [(42, 1)]), - {0: [expected_n]}) - expected_segment_size = struct.pack(">Q", 6) - self.failUnlessEqual(read("si1", [0], [(43, 8)]), - {0: [expected_segment_size]}) - expected_data_length = struct.pack(">Q", 36) - self.failUnlessEqual(read("si1", [0], [(51, 8)]), - {0: [expected_data_length]}) - expected_offset = struct.pack(">Q", expected_private_key_offset) - self.failUnlessEqual(read("si1", [0], [(59, 8)]), - {0: [expected_offset]}) - expected_offset = struct.pack(">Q", expected_share_hash_offset) - self.failUnlessEqual(read("si1", [0], [(67, 8)]), - {0: [expected_offset]}) - expected_offset = struct.pack(">Q", expected_signature_offset) - self.failUnlessEqual(read("si1", [0], [(75, 8)]), - {0: [expected_offset]}) - expected_offset = struct.pack(">Q", expected_verification_key_offset) - self.failUnlessEqual(read("si1", [0], [(83, 8)]), - {0: [expected_offset]}) - expected_offset = struct.pack(">Q", expected_verification_key_offset + len(self.verification_key)) - self.failUnlessEqual(read("si1", [0], [(91, 8)]), - {0: [expected_offset]}) - expected_offset = struct.pack(">Q", expected_sharedata_offset) - self.failUnlessEqual(read("si1", [0], [(99, 8)]), - {0: [expected_offset]}) - expected_offset = struct.pack(">Q", expected_block_hash_offset) - self.failUnlessEqual(read("si1", [0], [(107, 8)]), - {0: [expected_offset]}) - expected_offset = struct.pack(">Q", expected_eof_offset) - self.failUnlessEqual(read("si1", [0], [(115, 8)]), - {0: [expected_offset]}) - d.addCallback(_check_publish) + d.addCallback(lambda ign: self.failUnlessEqual(len(self.verification_key), 6)) + d.addCallback(lambda ign, evko=expected_verification_key_offset: + read("si1", [0], [(evko, 6)])) + d.addCallback(lambda res: self.failUnlessEqual(res, {0: [self.verification_key]})) + + def _check_other_fields(ign, ebho=expected_block_hash_offset, + esho=expected_share_hash_offset, + esigo=expected_signature_offset, + evko=expected_verification_key_offset): + signable = mw.get_signable() + verno, seq, roothash, k, N, segsize, datalen = struct.unpack(">BQ32sBBQQ", + signable) + self.failUnlessEqual(verno, 1) + self.failUnlessEqual(seq, 0) + self.failUnlessEqual(roothash, self.root_hash) + self.failUnlessEqual(k, 3) + self.failUnlessEqual(N, 10) + self.failUnlessEqual(segsize, 6) + self.failUnlessEqual(datalen, 36) + + def _check_field(res, offset, fmt, which, value): + encoded = struct.pack(fmt, value) + d3 = defer.succeed(None) + d3.addCallback(lambda ign: read("si1", [0], [(offset, len(encoded))])) + d3.addCallback(lambda res: self.failUnlessEqual(res, {0: [encoded]}, which)) + return d3 + + d2 = defer.succeed(None) + d2.addCallback(_check_field, 0, ">B", "version number", verno) + d2.addCallback(_check_field, 1, ">Q", "sequence number", seq) + d2.addCallback(_check_field, 41, ">B", "k", k) + d2.addCallback(_check_field, 42, ">B", "N", N) + d2.addCallback(_check_field, 43, ">Q", "segment size", segsize) + d2.addCallback(_check_field, 51, ">Q", "data length", datalen) + d2.addCallback(_check_field, 59, ">Q", "private key offset", + expected_private_key_offset) + d2.addCallback(_check_field, 67, ">Q", "share hash offset", esho) + d2.addCallback(_check_field, 75, ">Q", "signature offset", esigo) + d2.addCallback(_check_field, 83, ">Q", "verification key offset", evko) + d2.addCallback(_check_field, 91, ">Q", "end of verification key", + evko + len(self.verification_key)) + d2.addCallback(_check_field, 99, ">Q", "sharedata offset", + expected_sharedata_offset) + d2.addCallback(_check_field, 107, ">Q", "block hash offset", ebho) + d2.addCallback(_check_field, 115, ">Q", "eof offset", + ebho + len(self.block_hash_tree_s)) + return d2 + d.addCallback(_check_other_fields) + return d @@ -1956,6 +2665,8 @@ class MDMFProxies(unittest.TestCase, ShouldFailMixin): return mw def test_write_rejected_with_too_many_blocks(self): + self.init("test_write_rejected_with_too_many_blocks") + mw = self._make_new_mw("si0", 0) # Try writing too many blocks. We should not be able to write @@ -1972,6 +2683,8 @@ class MDMFProxies(unittest.TestCase, ShouldFailMixin): return d def test_write_rejected_with_invalid_salt(self): + self.init("test_write_rejected_with_invalid_salt") + # Try writing an invalid salt. Salts are 16 bytes -- any more or # less should cause an error. mw = self._make_new_mw("si1", 0) @@ -1983,6 +2696,8 @@ class MDMFProxies(unittest.TestCase, ShouldFailMixin): return d def test_write_rejected_with_invalid_root_hash(self): + self.init("test_write_rejected_with_invalid_root_hash") + # Try writing an invalid root hash. This should be SHA256d, and # 32 bytes long as a result. mw = self._make_new_mw("si2", 0) @@ -2008,6 +2723,8 @@ class MDMFProxies(unittest.TestCase, ShouldFailMixin): return d def test_write_rejected_with_invalid_blocksize(self): + self.init("test_write_rejected_with_invalid_blocksize") + # The blocksize implied by the writer that we get from # _make_new_mw is 2bytes -- any more or any less than this # should be cause for failure, unless it is the tail segment, in @@ -2041,6 +2758,8 @@ class MDMFProxies(unittest.TestCase, ShouldFailMixin): return d def test_write_enforces_order_constraints(self): + self.init("test_write_enforces_order_constraints") + # We require that the MDMFSlotWriteProxy be interacted with in a # specific way. # That way is: @@ -2063,8 +2782,8 @@ class MDMFProxies(unittest.TestCase, ShouldFailMixin): # Write some shares d = defer.succeed(None) for i in xrange(6): - d.addCallback(lambda ignored, i=i: - mw0.put_block(self.block, i, self.salt)) + d.addCallback(lambda ign, i=i: + mw0.put_block(self.block, i, self.salt)) # Try to write the share hash chain without writing the # encrypted private key @@ -2072,10 +2791,10 @@ class MDMFProxies(unittest.TestCase, ShouldFailMixin): self.shouldFail(LayoutInvalid, "share hash chain before " "private key", None, - mw0.put_sharehashes, self.share_hash_chain)) + lambda: mw0.put_sharehashes(self.share_hash_chain) )) + # Write the private key. - d.addCallback(lambda ignored: - mw0.put_encprivkey(self.encprivkey)) + d.addCallback(lambda ign: mw0.put_encprivkey(self.encprivkey)) # Now write the block hashes and try again d.addCallback(lambda ignored: @@ -2085,7 +2804,8 @@ class MDMFProxies(unittest.TestCase, ShouldFailMixin): # be able to sign it. d.addCallback(lambda ignored: self.shouldFail(LayoutInvalid, "signature before root hash", - None, mw0.put_signature, self.signature)) + None, + lambda: mw0.put_signature(self.signature) )) d.addCallback(lambda ignored: self.failUnlessRaises(LayoutInvalid, mw0.get_signable)) @@ -2094,24 +2814,22 @@ class MDMFProxies(unittest.TestCase, ShouldFailMixin): # verification key. d.addCallback(lambda ignored: self.shouldFail(LayoutInvalid, "key before signature", - None, mw0.put_verification_key, - self.verification_key)) + None, + lambda: mw0.put_verification_key(self.verification_key) )) # Now write the share hashes. - d.addCallback(lambda ignored: - mw0.put_sharehashes(self.share_hash_chain)) + d.addCallback(lambda ign: mw0.put_sharehashes(self.share_hash_chain)) + # We should be able to write the root hash now too - d.addCallback(lambda ignored: - mw0.put_root_hash(self.root_hash)) + d.addCallback(lambda ign: mw0.put_root_hash(self.root_hash)) # We should still be unable to put the verification key d.addCallback(lambda ignored: self.shouldFail(LayoutInvalid, "key before signature", - None, mw0.put_verification_key, - self.verification_key)) + None, + lambda: mw0.put_verification_key(self.verification_key) )) - d.addCallback(lambda ignored: - mw0.put_signature(self.signature)) + d.addCallback(lambda ign: mw0.put_signature(self.signature)) # We shouldn't be able to write the offsets to the remote server # until the offset table is finished; IOW, until we have written @@ -2126,6 +2844,8 @@ class MDMFProxies(unittest.TestCase, ShouldFailMixin): return d def test_end_to_end(self): + self.init("test_end_to_end") + mw = self._make_new_mw("si1", 0) # Write a share using the mutable writer, and make sure that the # reader knows how to read everything back to us. @@ -2209,26 +2929,28 @@ class MDMFProxies(unittest.TestCase, ShouldFailMixin): return d def test_is_sdmf(self): + self.init("test_is_sdmf") + # The MDMFSlotReadProxy should also know how to read SDMF files, # since it will encounter them on the grid. Callers use the # is_sdmf method to test this. - self.write_sdmf_share_to_server("si1") mr = MDMFSlotReadProxy(self.rref, "si1", 0) - d = mr.is_sdmf() - d.addCallback(lambda issdmf: - self.failUnless(issdmf)) + d = defer.succeed(None) + d.addCallback(lambda ign: self.write_sdmf_share_to_server("si1")) + d.addCallback(lambda ign: mr.is_sdmf()) + d.addCallback(lambda issdmf: self.failUnless(issdmf)) return d def test_reads_sdmf(self): + self.init("test_reads_sdmf") + # The slot read proxy should, naturally, know how to tell us # about data in the SDMF format - self.write_sdmf_share_to_server("si1") mr = MDMFSlotReadProxy(self.rref, "si1", 0) d = defer.succeed(None) - d.addCallback(lambda ignored: - mr.is_sdmf()) - d.addCallback(lambda issdmf: - self.failUnless(issdmf)) + d.addCallback(lambda ign: self.write_sdmf_share_to_server("si1")) + d.addCallback(lambda ign: mr.is_sdmf()) + d.addCallback(lambda issdmf: self.failUnless(issdmf)) # What do we need to read? # - The sharedata @@ -2289,16 +3011,16 @@ class MDMFProxies(unittest.TestCase, ShouldFailMixin): return d def test_only_reads_one_segment_sdmf(self): + self.init("test_only_reads_one_segment_sdmf") + # SDMF shares have only one segment, so it doesn't make sense to # read more segments than that. The reader should know this and # complain if we try to do that. - self.write_sdmf_share_to_server("si1") mr = MDMFSlotReadProxy(self.rref, "si1", 0) d = defer.succeed(None) - d.addCallback(lambda ignored: - mr.is_sdmf()) - d.addCallback(lambda issdmf: - self.failUnless(issdmf)) + d.addCallback(lambda ign: self.write_sdmf_share_to_server("si1")) + d.addCallback(lambda ign: mr.is_sdmf()) + d.addCallback(lambda issdmf: self.failUnless(issdmf)) d.addCallback(lambda ignored: self.shouldFail(LayoutInvalid, "test bad segment", None, @@ -2306,18 +3028,21 @@ class MDMFProxies(unittest.TestCase, ShouldFailMixin): return d def test_read_with_prefetched_mdmf_data(self): + self.init("test_read_with_prefetched_mdmf_data") + # The MDMFSlotReadProxy will prefill certain fields if you pass # it data that you have already fetched. This is useful for # cases like the Servermap, which prefetches ~2kb of data while # finding out which shares are on the remote peer so that it # doesn't waste round trips. mdmf_data = self.build_test_mdmf_share() - self.write_test_share_to_server("si1") def _make_mr(ignored, length): mr = MDMFSlotReadProxy(self.rref, "si1", 0, mdmf_data[:length]) return mr d = defer.succeed(None) + d.addCallback(lambda ign: self.write_test_share_to_server("si1")) + # This should be enough to fill in both the encoding parameters # and the table of offsets, which will complete the version # information tuple. @@ -2353,6 +3078,7 @@ class MDMFProxies(unittest.TestCase, ShouldFailMixin): self.failUnlessEqual(expected_prefix, prefix) self.failUnlessEqual(self.rref.read_count, 0) d.addCallback(_check_verinfo) + # This is not enough data to read a block and a share, so the # wrapper should attempt to read this from the remote server. d.addCallback(_make_mr, 123) @@ -2362,6 +3088,7 @@ class MDMFProxies(unittest.TestCase, ShouldFailMixin): self.failUnlessEqual(block, self.block) self.failUnlessEqual(salt, self.salt) self.failUnlessEqual(self.rref.read_count, 1) + # This should be enough data to read one block. d.addCallback(_make_mr, 123 + PRIVATE_KEY_SIZE + SIGNATURE_SIZE + VERIFICATION_KEY_SIZE + SHARE_HASH_CHAIN_SIZE + 140) d.addCallback(lambda mr: @@ -2370,13 +3097,16 @@ class MDMFProxies(unittest.TestCase, ShouldFailMixin): return d def test_read_with_prefetched_sdmf_data(self): + self.init("test_read_with_prefetched_sdmf_data") + sdmf_data = self.build_test_sdmf_share() - self.write_sdmf_share_to_server("si1") def _make_mr(ignored, length): mr = MDMFSlotReadProxy(self.rref, "si1", 0, sdmf_data[:length]) return mr d = defer.succeed(None) + d.addCallback(lambda ign: self.write_sdmf_share_to_server("si1")) + # This should be enough to get us the encoding parameters, # offset table, and everything else we need to build a verinfo # string. @@ -2433,14 +3163,16 @@ class MDMFProxies(unittest.TestCase, ShouldFailMixin): return d def test_read_with_empty_mdmf_file(self): + self.init("test_read_with_empty_mdmf_file") + # Some tests upload a file with no contents to test things # unrelated to the actual handling of the content of the file. # The reader should behave intelligently in these cases. - self.write_test_share_to_server("si1", empty=True) mr = MDMFSlotReadProxy(self.rref, "si1", 0) + d = defer.succeed(None) + d.addCallback(lambda ign: self.write_test_share_to_server("si1", empty=True)) # We should be able to get the encoding parameters, and they # should be correct. - d = defer.succeed(None) d.addCallback(lambda ignored: mr.get_encoding_parameters()) def _check_encoding_parameters(params): @@ -2461,11 +3193,13 @@ class MDMFProxies(unittest.TestCase, ShouldFailMixin): return d def test_read_with_empty_sdmf_file(self): - self.write_sdmf_share_to_server("si1", empty=True) + self.init("test_read_with_empty_sdmf_file") + mr = MDMFSlotReadProxy(self.rref, "si1", 0) + d = defer.succeed(None) + d.addCallback(lambda ign: self.write_sdmf_share_to_server("si1", empty=True)) # We should be able to get the encoding parameters, and they # should be correct - d = defer.succeed(None) d.addCallback(lambda ignored: mr.get_encoding_parameters()) def _check_encoding_parameters(params): @@ -2486,10 +3220,12 @@ class MDMFProxies(unittest.TestCase, ShouldFailMixin): return d def test_verinfo_with_sdmf_file(self): - self.write_sdmf_share_to_server("si1") + self.init("test_verinfo_with_sdmf_file") + mr = MDMFSlotReadProxy(self.rref, "si1", 0) # We should be able to get the version information. d = defer.succeed(None) + d.addCallback(lambda ign: self.write_sdmf_share_to_server("si1")) d.addCallback(lambda ignored: mr.get_verinfo()) def _check_verinfo(verinfo): @@ -2526,9 +3262,11 @@ class MDMFProxies(unittest.TestCase, ShouldFailMixin): return d def test_verinfo_with_mdmf_file(self): - self.write_test_share_to_server("si1") + self.init("test_verinfo_with_mdmf_file") + mr = MDMFSlotReadProxy(self.rref, "si1", 0) d = defer.succeed(None) + d.addCallback(lambda ign: self.write_test_share_to_server("si1")) d.addCallback(lambda ignored: mr.get_verinfo()) def _check_verinfo(verinfo): @@ -2545,7 +3283,7 @@ class MDMFProxies(unittest.TestCase, ShouldFailMixin): offsets) = verinfo self.failUnlessEqual(seqnum, 0) self.failUnlessEqual(root_hash, self.root_hash) - self.failIf(IV) + self.failIf(IV, IV) self.failUnlessEqual(segsize, 6) self.failUnlessEqual(datalen, 36) self.failUnlessEqual(k, 3) @@ -2564,6 +3302,8 @@ class MDMFProxies(unittest.TestCase, ShouldFailMixin): return d def test_sdmf_writer(self): + self.init("test_sdmf_writer") + # Go through the motions of writing an SDMF share to the storage # server. Then read the storage server to see that the share got # written in the way that we think it should have. @@ -2598,93 +3338,70 @@ class MDMFProxies(unittest.TestCase, ShouldFailMixin): # Now finish publishing d = sdmfr.finish_publishing() - def _then(ignored): - self.failUnlessEqual(self.rref.write_count, 1) - read = self.aa.remote_slot_readv - self.failUnlessEqual(read("si1", [0], [(0, len(data))]), - {0: [data]}) - d.addCallback(_then) + d.addCallback(lambda ign: self.failUnlessEqual(self.rref.write_count, 1)) + d.addCallback(lambda ign: self.aa.remote_slot_readv("si1", [0], [(0, len(data))])) + d.addCallback(lambda res: self.failUnlessEqual(res, {0: [data]})) return d def test_sdmf_writer_preexisting_share(self): - data = self.build_test_sdmf_share() - self.write_sdmf_share_to_server("si1") - - # Now there is a share on the storage server. To successfully - # write, we need to set the checkstring correctly. When we - # don't, no write should occur. - sdmfw = SDMFSlotWriteProxy(0, - self.rref, - "si1", - self.secrets, - 1, 3, 10, 36, 36) - sdmfw.put_block(self.blockdata, 0, self.salt) - - # Put the encprivkey - sdmfw.put_encprivkey(self.encprivkey) - - # Put the block and share hash chains - sdmfw.put_blockhashes(self.block_hash_tree) - sdmfw.put_sharehashes(self.share_hash_chain) - - # Put the root hash - sdmfw.put_root_hash(self.root_hash) - - # Put the signature - sdmfw.put_signature(self.signature) - - # Put the verification key - sdmfw.put_verification_key(self.verification_key) - - # We shouldn't have a checkstring yet - self.failUnlessEqual(sdmfw.get_checkstring(), "") - - d = sdmfw.finish_publishing() - def _then(results): - self.failIf(results[0]) - # this is the correct checkstring - self._expected_checkstring = results[1][0][0] - return self._expected_checkstring + self.init("test_sdmf_writer_preexisting_share") - d.addCallback(_then) - d.addCallback(sdmfw.set_checkstring) - d.addCallback(lambda ignored: - sdmfw.get_checkstring()) - d.addCallback(lambda checkstring: - self.failUnlessEqual(checkstring, self._expected_checkstring)) - d.addCallback(lambda ignored: - sdmfw.finish_publishing()) - def _then_again(results): - self.failUnless(results[0]) - read = self.aa.remote_slot_readv - self.failUnlessEqual(read("si1", [0], [(1, 8)]), - {0: [struct.pack(">Q", 1)]}) - self.failUnlessEqual(read("si1", [0], [(9, len(data) - 9)]), - {0: [data[9:]]}) - d.addCallback(_then_again) + data = self.build_test_sdmf_share() + d = defer.succeed(None) + d.addCallback(lambda ign: self.write_sdmf_share_to_server("si1")) + def _written(ign): + # Now there is a share on the storage server. To successfully + # write, we need to set the checkstring correctly. When we + # don't, no write should occur. + sdmfw = SDMFSlotWriteProxy(0, + self.rref, + "si1", + self.secrets, + 1, 3, 10, 36, 36) + sdmfw.put_block(self.blockdata, 0, self.salt) + + # Put the encprivkey + sdmfw.put_encprivkey(self.encprivkey) + + # Put the block and share hash chains + sdmfw.put_blockhashes(self.block_hash_tree) + sdmfw.put_sharehashes(self.share_hash_chain) + + # Put the root hash + sdmfw.put_root_hash(self.root_hash) + + # Put the signature + sdmfw.put_signature(self.signature) + + # Put the verification key + sdmfw.put_verification_key(self.verification_key) + + # We shouldn't have a checkstring yet + self.failUnlessEqual(sdmfw.get_checkstring(), "") + + d2 = sdmfw.finish_publishing() + def _then(results): + self.failIf(results[0]) + # this is the correct checkstring + self._expected_checkstring = results[1][0][0] + return self._expected_checkstring + d2.addCallback(_then) + d2.addCallback(sdmfw.set_checkstring) + d2.addCallback(lambda ign: sdmfw.get_checkstring()) + d2.addCallback(lambda checkstring: self.failUnlessEqual(checkstring, + self._expected_checkstring)) + d2.addCallback(lambda ign: sdmfw.finish_publishing()) + d2.addCallback(lambda res: self.failUnless(res[0], res)) + d2.addCallback(lambda ign: self.aa.remote_slot_readv("si1", [0], [(1, 8)])) + d2.addCallback(lambda res: self.failUnlessEqual(res, {0: [struct.pack(">Q", 1)]})) + d2.addCallback(lambda ign: self.aa.remote_slot_readv("si1", [0], [(9, len(data) - 9)])) + d2.addCallback(lambda res: self.failUnlessEqual(res, {0: [data[9:]]})) + return d2 + d.addCallback(_written) return d -class Stats(unittest.TestCase): - def setUp(self): - self.sparent = LoggingServiceParent() - self._lease_secret = itertools.count() - - def tearDown(self): - return self.sparent.stopService() - - - def workdir(self, name): - basedir = os.path.join("storage", "Server", name) - return basedir - - def create(self, name): - workdir = self.workdir(name) - server = StorageServer(workdir, "\x00" * 20) - server.setServiceParent(self.sparent) - return server - - +class Stats(WithDiskBackend, unittest.TestCase): def test_latencies(self): server = self.create("test_latencies") for i in range(10000): @@ -2758,19 +3475,9 @@ def remove_tags(s): return s -class BucketCounterTest(unittest.TestCase, CrawlerTestMixin, ReallyEqualMixin): - def setUp(self): - self.s = service.MultiService() - self.s.startService() - - def tearDown(self): - return self.s.stopService() - - +class BucketCounterTest(WithDiskBackend, CrawlerTestMixin, ReallyEqualMixin, unittest.TestCase): def test_bucket_counter(self): - basedir = "storage/BucketCounter/bucket_counter" - fileutil.make_dirs(basedir) - server = StorageServer(basedir, "\x00" * 20) + server = self.create("test_bucket_counter", detached=True) bucket_counter = server.bucket_counter # finish as fast as possible @@ -2779,7 +3486,7 @@ class BucketCounterTest(unittest.TestCase, CrawlerTestMixin, ReallyEqualMixin): d = server.bucket_counter.set_hook('after_prefix') - server.setServiceParent(self.s) + server.setServiceParent(self.sparent) w = StorageStatus(server) @@ -2822,9 +3529,7 @@ class BucketCounterTest(unittest.TestCase, CrawlerTestMixin, ReallyEqualMixin): return d def test_bucket_counter_cleanup(self): - basedir = "storage/BucketCounter/bucket_counter_cleanup" - fileutil.make_dirs(basedir) - server = StorageServer(basedir, "\x00" * 20) + server = self.create("test_bucket_counter_cleanup", detached=True) bucket_counter = server.bucket_counter # finish as fast as possible @@ -2833,7 +3538,7 @@ class BucketCounterTest(unittest.TestCase, CrawlerTestMixin, ReallyEqualMixin): d = bucket_counter.set_hook('after_prefix') - server.setServiceParent(self.s) + server.setServiceParent(self.sparent) def _after_first_prefix(prefix): bucket_counter.save_state() @@ -2844,7 +3549,6 @@ class BucketCounterTest(unittest.TestCase, CrawlerTestMixin, ReallyEqualMixin): # now sneak in and mess with its state, to make sure it cleans up # properly at the end of the cycle state["bucket-counts"][-12] = {} - state["storage-index-samples"]["bogusprefix!"] = (-12, []) bucket_counter.save_state() return bucket_counter.set_hook('after_cycle') @@ -2857,16 +3561,12 @@ class BucketCounterTest(unittest.TestCase, CrawlerTestMixin, ReallyEqualMixin): s = bucket_counter.get_state() self.failIf(-12 in s["bucket-counts"], s["bucket-counts"].keys()) - self.failIf("bogusprefix!" in s["storage-index-samples"], - s["storage-index-samples"].keys()) d.addCallback(_after_first_cycle) d.addBoth(self._wait_for_yield, bucket_counter) return d def test_bucket_counter_eta(self): - basedir = "storage/BucketCounter/bucket_counter_eta" - fileutil.make_dirs(basedir) - server = StorageServer(basedir, "\x00" * 20) + server = self.create("test_bucket_counter_eta", detached=True) bucket_counter = server.bucket_counter # finish as fast as possible @@ -2875,7 +3575,7 @@ class BucketCounterTest(unittest.TestCase, CrawlerTestMixin, ReallyEqualMixin): d = bucket_counter.set_hook('after_prefix') - server.setServiceParent(self.s) + server.setServiceParent(self.sparent) w = StorageStatus(server) @@ -2898,15 +3598,7 @@ class BucketCounterTest(unittest.TestCase, CrawlerTestMixin, ReallyEqualMixin): return d -class AccountingCrawlerTest(unittest.TestCase, CrawlerTestMixin, WebRenderingMixin, ReallyEqualMixin): - def setUp(self): - self.s = service.MultiService() - self.s.startService() - - def tearDown(self): - return self.s.stopService() - - +class AccountingCrawlerTest(CrawlerTestMixin, WebRenderingMixin, ReallyEqualMixin): def make_shares(self, server): aa = server.get_accountant().get_anonymous_account() sa = server.get_accountant().get_starter_account() @@ -2922,6 +3614,8 @@ class AccountingCrawlerTest(unittest.TestCase, CrawlerTestMixin, WebRenderingMix return (hashutil.tagged_hash("renew-%d" % num, si), hashutil.tagged_hash("cancel-%d" % num, si)) + writev = aa.remote_slot_testv_and_readv_and_writev + immutable_si_0, rs0, cs0 = make("\x00" * 16) immutable_si_1, rs1, cs1 = make("\x01" * 16) rs1a, cs1a = make_extra_lease(immutable_si_1, 1) @@ -2934,33 +3628,35 @@ class AccountingCrawlerTest(unittest.TestCase, CrawlerTestMixin, WebRenderingMix # inner contents are not a valid CHK share data = "\xff" * 1000 - a,w = aa.remote_allocate_buckets(immutable_si_0, rs0, cs0, sharenums, - 1000, canary) - w[0].remote_write(0, data) - w[0].remote_close() - - a,w = aa.remote_allocate_buckets(immutable_si_1, rs1, cs1, sharenums, - 1000, canary) - w[0].remote_write(0, data) - w[0].remote_close() - sa.remote_add_lease(immutable_si_1, rs1a, cs1a) - - writev = aa.remote_slot_testv_and_readv_and_writev - writev(mutable_si_2, (we2, rs2, cs2), - {0: ([], [(0,data)], len(data))}, []) - writev(mutable_si_3, (we3, rs3, cs3), - {0: ([], [(0,data)], len(data))}, []) - sa.remote_add_lease(mutable_si_3, rs3a, cs3a) - self.sis = [immutable_si_0, immutable_si_1, mutable_si_2, mutable_si_3] self.renew_secrets = [rs0, rs1, rs1a, rs2, rs3, rs3a] self.cancel_secrets = [cs0, cs1, cs1a, cs2, cs3, cs3a] + d = defer.succeed(None) + d.addCallback(lambda ign: aa.remote_allocate_buckets(immutable_si_0, rs0, cs0, sharenums, + 1000, canary)) + def _got_buckets( (a, w) ): + w[0].remote_write(0, data) + w[0].remote_close() + d.addCallback(_got_buckets) + + d.addCallback(lambda ign: aa.remote_allocate_buckets(immutable_si_1, rs1, cs1, sharenums, + 1000, canary)) + d.addCallback(_got_buckets) + d.addCallback(lambda ign: sa.remote_add_lease(immutable_si_1, rs1a, cs1a)) + + d.addCallback(lambda ign: writev(mutable_si_2, (we2, rs2, cs2), + {0: ([], [(0,data)], len(data))}, [])) + d.addCallback(lambda ign: writev(mutable_si_3, (we3, rs3, cs3), + {0: ([], [(0,data)], len(data))}, [])) + d.addCallback(lambda ign: sa.remote_add_lease(mutable_si_3, rs3a, cs3a)) + return d + def test_basic(self): - basedir = "storage/AccountingCrawler/basic" - fileutil.make_dirs(basedir) + server = self.create("test_basic", detached=True) + ep = ExpirationPolicy(enabled=False) - server = StorageServer(basedir, "\x00" * 20, expiration_policy=ep) + server.get_accountant().set_expiration_policy(ep) aa = server.get_accountant().get_anonymous_account() sa = server.get_accountant().get_starter_account() @@ -2972,136 +3668,155 @@ class AccountingCrawlerTest(unittest.TestCase, CrawlerTestMixin, WebRenderingMix webstatus = StorageStatus(server) # create a few shares, with some leases on them - self.make_shares(server) - [immutable_si_0, immutable_si_1, mutable_si_2, mutable_si_3] = self.sis - - # add a non-sharefile to exercise another code path - fn = os.path.join(server.sharedir, - storage_index_to_dir(immutable_si_0), - "not-a-share") - fileutil.write(fn, "I am not a share.\n") - - # this is before the crawl has started, so we're not in a cycle yet - initial_state = ac.get_state() - self.failIf(ac.get_progress()["cycle-in-progress"]) - self.failIfIn("cycle-to-date", initial_state) - self.failIfIn("estimated-remaining-cycle", initial_state) - self.failIfIn("estimated-current-cycle", initial_state) - self.failUnlessIn("history", initial_state) - self.failUnlessEqual(initial_state["history"], {}) - - server.setServiceParent(self.s) - - DAY = 24*60*60 - - # now examine the state right after the 'aa' prefix has been processed. - d = self._after_prefix(None, 'aa', ac) - def _after_aa_prefix(state): - self.failUnlessIn("cycle-to-date", state) - self.failUnlessIn("estimated-remaining-cycle", state) - self.failUnlessIn("estimated-current-cycle", state) - self.failUnlessIn("history", state) - self.failUnlessEqual(state["history"], {}) - - so_far = state["cycle-to-date"] - self.failUnlessEqual(so_far["expiration-enabled"], False) - self.failUnlessIn("configured-expiration-mode", so_far) - self.failUnlessIn("lease-age-histogram", so_far) - lah = so_far["lease-age-histogram"] - self.failUnlessEqual(type(lah), list) - self.failUnlessEqual(len(lah), 1) - self.failUnlessEqual(lah, [ (0.0, DAY, 1) ] ) - self.failUnlessEqual(so_far["corrupt-shares"], []) - sr1 = so_far["space-recovered"] - self.failUnlessEqual(sr1["examined-buckets"], 1) - self.failUnlessEqual(sr1["examined-shares"], 1) - self.failUnlessEqual(sr1["actual-shares"], 0) - left = state["estimated-remaining-cycle"] - sr2 = left["space-recovered"] - self.failUnless(sr2["examined-buckets"] > 0, sr2["examined-buckets"]) - self.failUnless(sr2["examined-shares"] > 0, sr2["examined-shares"]) - self.failIfEqual(sr2["actual-shares"], None) - d.addCallback(_after_aa_prefix) - - d.addCallback(lambda ign: self.render1(webstatus)) - def _check_html_in_cycle(html): - s = remove_tags(html) - self.failUnlessIn("So far, this cycle has examined " - "1 shares in 1 sharesets (0 mutable / 1 immutable) ", s) - self.failUnlessIn("and has recovered: " - "0 shares, 0 sharesets (0 mutable / 0 immutable), " - "0 B (0 B / 0 B)", s) - - return ac.set_hook('after_cycle') - d.addCallback(_check_html_in_cycle) - - def _after_first_cycle(cycle): - # After the first cycle, nothing should have been removed. - self.failUnlessEqual(cycle, 0) - progress = ac.get_progress() - self.failUnlessReallyEqual(progress["cycle-in-progress"], False) - - s = ac.get_state() - self.failIf("cycle-to-date" in s) - self.failIf("estimated-remaining-cycle" in s) - self.failIf("estimated-current-cycle" in s) - last = s["history"][0] - self.failUnlessEqual(type(last), dict, repr(last)) - self.failUnlessIn("cycle-start-finish-times", last) - self.failUnlessEqual(type(last["cycle-start-finish-times"]), list, repr(last)) - self.failUnlessEqual(last["expiration-enabled"], False) - self.failUnlessIn("configured-expiration-mode", last) - - self.failUnlessIn("lease-age-histogram", last) - lah = last["lease-age-histogram"] - self.failUnlessEqual(type(lah), list) - self.failUnlessEqual(len(lah), 1) - self.failUnlessEqual(tuple(lah[0]), (0.0, DAY, 6) ) - - self.failUnlessEqual(last["corrupt-shares"], []) - - rec = last["space-recovered"] - self.failUnlessEqual(rec["examined-buckets"], 4) - self.failUnlessEqual(rec["examined-shares"], 4) - self.failUnlessEqual(rec["actual-buckets"], 0) - self.failUnlessEqual(rec["actual-shares"], 0) - self.failUnlessEqual(rec["actual-diskbytes"], 0) - - def count_leases(si): - return (len(aa.get_leases(si)), len(sa.get_leases(si))) - self.failUnlessEqual(count_leases(immutable_si_0), (1, 0)) - self.failUnlessEqual(count_leases(immutable_si_1), (1, 1)) - self.failUnlessEqual(count_leases(mutable_si_2), (1, 0)) - self.failUnlessEqual(count_leases(mutable_si_3), (1, 1)) - d.addCallback(_after_first_cycle) + d = self.make_shares(server) + def _do_test(ign): + [immutable_si_0, immutable_si_1, mutable_si_2, mutable_si_3] = self.sis + + if isinstance(server.backend, DiskBackend): + # add a non-sharefile to exercise another code path + fn = os.path.join(server.backend._sharedir, + storage_index_to_dir(immutable_si_0), + "not-a-share") + fileutil.write(fn, "I am not a share.\n") + + # this is before the crawl has started, so we're not in a cycle yet + initial_state = ac.get_state() + self.failIf(ac.get_progress()["cycle-in-progress"]) + self.failIfIn("cycle-to-date", initial_state) + self.failIfIn("estimated-remaining-cycle", initial_state) + self.failIfIn("estimated-current-cycle", initial_state) + self.failUnlessIn("history", initial_state) + self.failUnlessEqual(initial_state["history"], {}) + + server.setServiceParent(self.sparent) + + DAY = 24*60*60 + + # now examine the state right after the 'aa' prefix has been processed. + d2 = self._after_prefix(None, 'aa', ac) + def _after_aa_prefix(state): + self.failUnlessIn("cycle-to-date", state) + self.failUnlessIn("estimated-remaining-cycle", state) + self.failUnlessIn("estimated-current-cycle", state) + self.failUnlessIn("history", state) + self.failUnlessEqual(state["history"], {}) + + so_far = state["cycle-to-date"] + self.failUnlessEqual(so_far["expiration-enabled"], False) + self.failUnlessIn("configured-expiration-mode", so_far) + self.failUnlessIn("lease-age-histogram", so_far) + lah = so_far["lease-age-histogram"] + self.failUnlessEqual(type(lah), list) + self.failUnlessEqual(len(lah), 1) + self.failUnlessEqual(lah, [ (0.0, DAY, 1) ] ) + self.failUnlessEqual(so_far["corrupt-shares"], []) + sr1 = so_far["space-recovered"] + self.failUnlessEqual(sr1["examined-buckets"], 1) + self.failUnlessEqual(sr1["examined-shares"], 1) + self.failUnlessEqual(sr1["actual-shares"], 0) + left = state["estimated-remaining-cycle"] + sr2 = left["space-recovered"] + self.failUnless(sr2["examined-buckets"] > 0, sr2["examined-buckets"]) + self.failUnless(sr2["examined-shares"] > 0, sr2["examined-shares"]) + self.failIfEqual(sr2["actual-shares"], None) + d2.addCallback(_after_aa_prefix) + + d2.addCallback(lambda ign: self.render1(webstatus)) + def _check_html_in_cycle(html): + s = remove_tags(html) + self.failUnlessIn("So far, this cycle has examined " + "1 shares in 1 sharesets (0 mutable / 1 immutable) ", s) + self.failUnlessIn("and has recovered: " + "0 shares, 0 sharesets (0 mutable / 0 immutable), " + "0 B (0 B / 0 B)", s) + + return ac.set_hook('after_cycle') + d2.addCallback(_check_html_in_cycle) + + def _after_first_cycle(cycle): + # After the first cycle, nothing should have been removed. + self.failUnlessEqual(cycle, 0) + progress = ac.get_progress() + self.failUnlessReallyEqual(progress["cycle-in-progress"], False) + + s = ac.get_state() + self.failIf("cycle-to-date" in s) + self.failIf("estimated-remaining-cycle" in s) + self.failIf("estimated-current-cycle" in s) + last = s["history"][0] + self.failUnlessEqual(type(last), dict, repr(last)) + self.failUnlessIn("cycle-start-finish-times", last) + self.failUnlessEqual(type(last["cycle-start-finish-times"]), list, repr(last)) + self.failUnlessEqual(last["expiration-enabled"], False) + self.failUnlessIn("configured-expiration-mode", last) + + self.failUnlessIn("lease-age-histogram", last) + lah = last["lease-age-histogram"] + self.failUnlessEqual(type(lah), list) + self.failUnlessEqual(len(lah), 1) + self.failUnlessEqual(tuple(lah[0]), (0.0, DAY, 6) ) + + self.failUnlessEqual(last["corrupt-shares"], []) + + rec = last["space-recovered"] + self.failUnlessEqual(rec["examined-buckets"], 4) + self.failUnlessEqual(rec["examined-shares"], 4) + self.failUnlessEqual(rec["actual-buckets"], 0) + self.failUnlessEqual(rec["actual-shares"], 0) + self.failUnlessEqual(rec["actual-diskbytes"], 0) + + def count_leases(si): + return (len(aa.get_leases(si)), len(sa.get_leases(si))) + self.failUnlessEqual(count_leases(immutable_si_0), (1, 0)) + self.failUnlessEqual(count_leases(immutable_si_1), (1, 1)) + self.failUnlessEqual(count_leases(mutable_si_2), (1, 0)) + self.failUnlessEqual(count_leases(mutable_si_3), (1, 1)) + d2.addCallback(_after_first_cycle) + + d2.addCallback(lambda ign: self.render1(webstatus)) + def _check_html_after_cycle(html): + s = remove_tags(html) + self.failUnlessIn("recovered: 0 shares, 0 sharesets " + "(0 mutable / 0 immutable), 0 B (0 B / 0 B) ", s) + self.failUnlessIn("and saw a total of 4 shares, 4 sharesets " + "(2 mutable / 2 immutable),", s) + self.failUnlessIn("but expiration was not enabled", s) + d2.addCallback(_check_html_after_cycle) + + d2.addCallback(lambda ign: self.render_json(webstatus)) + def _check_json_after_cycle(json): + data = simplejson.loads(json) + self.failUnlessIn("lease-checker", data) + self.failUnlessIn("lease-checker-progress", data) + d2.addCallback(_check_json_after_cycle) + d2.addBoth(self._wait_for_yield, ac) + return d2 + d.addCallback(_do_test) + return d - d.addCallback(lambda ign: self.render1(webstatus)) - def _check_html_after_cycle(html): - s = remove_tags(html) - self.failUnlessIn("recovered: 0 shares, 0 sharesets " - "(0 mutable / 0 immutable), 0 B (0 B / 0 B) ", s) - self.failUnlessIn("and saw a total of 4 shares, 4 sharesets " - "(2 mutable / 2 immutable),", s) - self.failUnlessIn("but expiration was not enabled", s) - d.addCallback(_check_html_after_cycle) - - d.addCallback(lambda ign: self.render_json(webstatus)) - def _check_json_after_cycle(json): - data = simplejson.loads(json) - self.failUnlessIn("lease-checker", data) - self.failUnlessIn("lease-checker-progress", data) - d.addCallback(_check_json_after_cycle) - d.addBoth(self._wait_for_yield, ac) + def _assert_sharecount(self, server, si, expected): + d = defer.succeed(None) + d.addCallback(lambda ign: server.backend.get_shareset(si).get_shares()) + def _got_shares( (shares, corrupted) ): + self.failUnlessEqual(len(shares), expected, "share count for %r" % (si,)) + self.failUnlessEqual(len(corrupted), 0, str(corrupted)) + d.addCallback(_got_shares) return d + def _assert_leasecount(self, server, si, expected): + aa = server.get_accountant().get_anonymous_account() + sa = server.get_accountant().get_starter_account() + self.failUnlessEqual((len(aa.get_leases(si)), len(sa.get_leases(si))), + expected) + def test_expire_age(self): - basedir = "storage/AccountingCrawler/expire_age" - fileutil.make_dirs(basedir) + server = self.create("test_expire_age", detached=True) + # setting expiration_time to 2000 means that any lease which is more # than 2000s old will be expired. now = time.time() ep = ExpirationPolicy(enabled=True, mode="age", override_lease_duration=2000) - server = StorageServer(basedir, "\x00" * 20, expiration_policy=ep) + server.get_accountant().set_expiration_policy(ep) aa = server.get_accountant().get_anonymous_account() sa = server.get_accountant().get_starter_account() @@ -3113,115 +3828,115 @@ class AccountingCrawlerTest(unittest.TestCase, CrawlerTestMixin, WebRenderingMix webstatus = StorageStatus(server) # create a few shares, with some leases on them - self.make_shares(server) - [immutable_si_0, immutable_si_1, mutable_si_2, mutable_si_3] = self.sis - - def count_shares(si): - return len(list(server._iter_share_files(si))) - def _get_sharefile(si): - return list(server._iter_share_files(si))[0] - def count_leases(si): - return (len(aa.get_leases(si)), len(sa.get_leases(si))) - - self.failUnlessEqual(count_shares(immutable_si_0), 1) - self.failUnlessEqual(count_leases(immutable_si_0), (1, 0)) - self.failUnlessEqual(count_shares(immutable_si_1), 1) - self.failUnlessEqual(count_leases(immutable_si_1), (1, 1)) - self.failUnlessEqual(count_shares(mutable_si_2), 1) - self.failUnlessEqual(count_leases(mutable_si_2), (1, 0)) - self.failUnlessEqual(count_shares(mutable_si_3), 1) - self.failUnlessEqual(count_leases(mutable_si_3), (1, 1)) - - # artificially crank back the renewal time on the first lease of each - # share to 3000s ago, and set the expiration time to 31 days later. - new_renewal_time = now - 3000 - new_expiration_time = new_renewal_time + 31*24*60*60 - - # Some shares have an extra lease which is set to expire at the - # default time in 31 days from now (age=31days). We then run the - # crawler, which will expire the first lease, making some shares get - # deleted and others stay alive (with one remaining lease) - - aa.add_or_renew_lease(immutable_si_0, 0, new_renewal_time, new_expiration_time) - - # immutable_si_1 gets an extra lease - sa.add_or_renew_lease(immutable_si_1, 0, new_renewal_time, new_expiration_time) - - aa.add_or_renew_lease(mutable_si_2, 0, new_renewal_time, new_expiration_time) - - # mutable_si_3 gets an extra lease - sa.add_or_renew_lease(mutable_si_3, 0, new_renewal_time, new_expiration_time) - - server.setServiceParent(self.s) - - # now examine the web status right after the 'aa' prefix has been processed. - d = self._after_prefix(None, 'aa', ac) - d.addCallback(lambda ign: self.render1(webstatus)) - def _check_html_in_cycle(html): - s = remove_tags(html) - # the first shareset encountered gets deleted, and its prefix - # happens to be about 1/5th of the way through the ring, so the - # predictor thinks we'll have 5 shares and that we'll delete them - # all. This part of the test depends upon the SIs landing right - # where they do now. - self.failUnlessIn("The remainder of this cycle is expected to " - "recover: 4 shares, 4 sharesets", s) - self.failUnlessIn("The whole cycle is expected to examine " - "5 shares in 5 sharesets and to recover: " - "5 shares, 5 sharesets", s) - - return ac.set_hook('after_cycle') - d.addCallback(_check_html_in_cycle) - - def _after_first_cycle(ignored): - self.failUnlessEqual(count_shares(immutable_si_0), 0) - self.failUnlessEqual(count_shares(immutable_si_1), 1) - self.failUnlessEqual(count_leases(immutable_si_1), (1, 0)) - self.failUnlessEqual(count_shares(mutable_si_2), 0) - self.failUnlessEqual(count_shares(mutable_si_3), 1) - self.failUnlessEqual(count_leases(mutable_si_3), (1, 0)) - - s = ac.get_state() - last = s["history"][0] - - self.failUnlessEqual(last["expiration-enabled"], True) - cem = last["configured-expiration-mode"] - self.failUnlessEqual(cem[0], "age") - self.failUnlessEqual(cem[1], 2000) - self.failUnlessEqual(cem[2], None) - self.failUnlessEqual(cem[3][0], "mutable") - self.failUnlessEqual(cem[3][1], "immutable") - - rec = last["space-recovered"] - self.failUnlessEqual(rec["examined-buckets"], 4) - self.failUnlessEqual(rec["examined-shares"], 4) - self.failUnlessEqual(rec["actual-buckets"], 2) - self.failUnlessEqual(rec["actual-shares"], 2) - # different platforms have different notions of "blocks used by - # this file", so merely assert that it's a number - self.failUnless(rec["actual-diskbytes"] >= 0, - rec["actual-diskbytes"]) - d.addCallback(_after_first_cycle) - - d.addCallback(lambda ign: self.render1(webstatus)) - def _check_html_after_cycle(html): - s = remove_tags(html) - self.failUnlessIn("Expiration Enabled: expired leases will be removed", s) - self.failUnlessIn("Leases created or last renewed more than 33 minutes ago will be considered expired.", s) - self.failUnlessIn(" recovered: 2 shares, 2 sharesets (1 mutable / 1 immutable), ", s) - d.addCallback(_check_html_after_cycle) - d.addBoth(self._wait_for_yield, ac) + d = self.make_shares(server) + def _do_test(ign): + [immutable_si_0, immutable_si_1, mutable_si_2, mutable_si_3] = self.sis + + d2 = defer.succeed(None) + d2.addCallback(lambda ign: self._assert_sharecount(server, immutable_si_0, 1)) + d2.addCallback(lambda ign: self._assert_leasecount(server, immutable_si_0, (1, 0))) + d2.addCallback(lambda ign: self._assert_sharecount(server, immutable_si_1, 1)) + d2.addCallback(lambda ign: self._assert_leasecount(server, immutable_si_1, (1, 1))) + d2.addCallback(lambda ign: self._assert_sharecount(server, mutable_si_2, 1)) + d2.addCallback(lambda ign: self._assert_leasecount(server, mutable_si_2, (1, 0))) + d2.addCallback(lambda ign: self._assert_sharecount(server, mutable_si_3, 1)) + d2.addCallback(lambda ign: self._assert_leasecount(server, mutable_si_3, (1, 1))) + + def _then(ign): + # artificially crank back the renewal time on the first lease of each + # share to 3000s ago, and set the expiration time to 31 days later. + new_renewal_time = now - 3000 + new_expiration_time = new_renewal_time + 31*24*60*60 + + # Some shares have an extra lease which is set to expire at the + # default time in 31 days from now (age=31days). We then run the + # crawler, which will expire the first lease, making some shares get + # deleted and others stay alive (with one remaining lease) + + aa.add_or_renew_lease(immutable_si_0, 0, new_renewal_time, new_expiration_time) + + # immutable_si_1 gets an extra lease + sa.add_or_renew_lease(immutable_si_1, 0, new_renewal_time, new_expiration_time) + + aa.add_or_renew_lease(mutable_si_2, 0, new_renewal_time, new_expiration_time) + + # mutable_si_3 gets an extra lease + sa.add_or_renew_lease(mutable_si_3, 0, new_renewal_time, new_expiration_time) + + server.setServiceParent(self.sparent) + + # now examine the web status right after the 'aa' prefix has been processed. + d3 = self._after_prefix(None, 'aa', ac) + d3.addCallback(lambda ign: self.render1(webstatus)) + def _check_html_in_cycle(html): + s = remove_tags(html) + # the first shareset encountered gets deleted, and its prefix + # happens to be about 1/5th of the way through the ring, so the + # predictor thinks we'll have 5 shares and that we'll delete them + # all. This part of the test depends upon the SIs landing right + # where they do now. + self.failUnlessIn("The remainder of this cycle is expected to " + "recover: 4 shares, 4 sharesets", s) + self.failUnlessIn("The whole cycle is expected to examine " + "5 shares in 5 sharesets and to recover: " + "5 shares, 5 sharesets", s) + + return ac.set_hook('after_cycle') + d3.addCallback(_check_html_in_cycle) + + d3.addCallback(lambda ign: self._assert_sharecount(server, immutable_si_0, 0)) + d3.addCallback(lambda ign: self._assert_sharecount(server, immutable_si_1, 1)) + d3.addCallback(lambda ign: self._assert_leasecount(server, immutable_si_1, (1, 0))) + d3.addCallback(lambda ign: self._assert_sharecount(server, mutable_si_2, 0)) + d3.addCallback(lambda ign: self._assert_sharecount(server, mutable_si_3, 1)) + d3.addCallback(lambda ign: self._assert_leasecount(server, mutable_si_3, (1, 0))) + + def _after_first_cycle(ignored): + s = ac.get_state() + last = s["history"][0] + + self.failUnlessEqual(last["expiration-enabled"], True) + cem = last["configured-expiration-mode"] + self.failUnlessEqual(cem[0], "age") + self.failUnlessEqual(cem[1], 2000) + self.failUnlessEqual(cem[2], None) + self.failUnlessEqual(cem[3][0], "mutable") + self.failUnlessEqual(cem[3][1], "immutable") + + rec = last["space-recovered"] + self.failUnlessEqual(rec["examined-buckets"], 4) + self.failUnlessEqual(rec["examined-shares"], 4) + self.failUnlessEqual(rec["actual-buckets"], 2) + self.failUnlessEqual(rec["actual-shares"], 2) + # different platforms have different notions of "blocks used by + # this file", so merely assert that it's a number + self.failUnless(rec["actual-diskbytes"] >= 0, + rec["actual-diskbytes"]) + d3.addCallback(_after_first_cycle) + + d3.addCallback(lambda ign: self.render1(webstatus)) + def _check_html_after_cycle(html): + s = remove_tags(html) + self.failUnlessIn("Expiration Enabled: expired leases will be removed", s) + self.failUnlessIn("Leases created or last renewed more than 33 minutes ago will be considered expired.", s) + self.failUnlessIn(" recovered: 2 shares, 2 sharesets (1 mutable / 1 immutable), ", s) + d3.addCallback(_check_html_after_cycle) + d3.addBoth(self._wait_for_yield, ac) + return d3 + d2.addCallback(_then) + return d2 + d.addCallback(_do_test) return d def test_expire_cutoff_date(self): - basedir = "storage/AccountingCrawler/expire_cutoff_date" - fileutil.make_dirs(basedir) + server = self.create("test_expire_cutoff_date", detached=True) + # setting cutoff-date to 2000 seconds ago means that any lease which # is more than 2000s old will be expired. now = time.time() then = int(now - 2000) ep = ExpirationPolicy(enabled=True, mode="cutoff-date", cutoff_date=then) - server = StorageServer(basedir, "\x00" * 20, expiration_policy=ep) + server.get_accountant().set_expiration_policy(ep) aa = server.get_accountant().get_anonymous_account() sa = server.get_accountant().get_starter_account() @@ -3233,107 +3948,107 @@ class AccountingCrawlerTest(unittest.TestCase, CrawlerTestMixin, WebRenderingMix webstatus = StorageStatus(server) # create a few shares, with some leases on them - self.make_shares(server) - [immutable_si_0, immutable_si_1, mutable_si_2, mutable_si_3] = self.sis - - def count_shares(si): - return len(list(server._iter_share_files(si))) - def _get_sharefile(si): - return list(server._iter_share_files(si))[0] - def count_leases(si): - return (len(aa.get_leases(si)), len(sa.get_leases(si))) - - self.failUnlessEqual(count_shares(immutable_si_0), 1) - self.failUnlessEqual(count_leases(immutable_si_0), (1, 0)) - self.failUnlessEqual(count_shares(immutable_si_1), 1) - self.failUnlessEqual(count_leases(immutable_si_1), (1, 1)) - self.failUnlessEqual(count_shares(mutable_si_2), 1) - self.failUnlessEqual(count_leases(mutable_si_2), (1, 0)) - self.failUnlessEqual(count_shares(mutable_si_3), 1) - self.failUnlessEqual(count_leases(mutable_si_3), (1, 1)) - - # artificially crank back the renewal time on the first lease of each - # share to 3000s ago, and set the expiration time to 31 days later. - new_renewal_time = now - 3000 - new_expiration_time = new_renewal_time + 31*24*60*60 - - # Some shares have an extra lease which is set to expire at the - # default time in 31 days from now (age=31days). We then run the - # crawler, which will expire the first lease, making some shares get - # deleted and others stay alive (with one remaining lease) - - aa.add_or_renew_lease(immutable_si_0, 0, new_renewal_time, new_expiration_time) - - # immutable_si_1 gets an extra lease - sa.add_or_renew_lease(immutable_si_1, 0, new_renewal_time, new_expiration_time) - - aa.add_or_renew_lease(mutable_si_2, 0, new_renewal_time, new_expiration_time) - - # mutable_si_3 gets an extra lease - sa.add_or_renew_lease(mutable_si_3, 0, new_renewal_time, new_expiration_time) - - server.setServiceParent(self.s) - - # now examine the web status right after the 'aa' prefix has been processed. - d = self._after_prefix(None, 'aa', ac) - d.addCallback(lambda ign: self.render1(webstatus)) - def _check_html_in_cycle(html): - s = remove_tags(html) - # the first bucket encountered gets deleted, and its prefix - # happens to be about 1/5th of the way through the ring, so the - # predictor thinks we'll have 5 shares and that we'll delete them - # all. This part of the test depends upon the SIs landing right - # where they do now. - self.failUnlessIn("The remainder of this cycle is expected to " - "recover: 4 shares, 4 sharesets", s) - self.failUnlessIn("The whole cycle is expected to examine " - "5 shares in 5 sharesets and to recover: " - "5 shares, 5 sharesets", s) - - return ac.set_hook('after_cycle') - d.addCallback(_check_html_in_cycle) - - def _after_first_cycle(ignored): - self.failUnlessEqual(count_shares(immutable_si_0), 0) - self.failUnlessEqual(count_shares(immutable_si_1), 1) - self.failUnlessEqual(count_leases(immutable_si_1), (1, 0)) - self.failUnlessEqual(count_shares(mutable_si_2), 0) - self.failUnlessEqual(count_shares(mutable_si_3), 1) - self.failUnlessEqual(count_leases(mutable_si_3), (1, 0)) - - s = ac.get_state() - last = s["history"][0] - - self.failUnlessEqual(last["expiration-enabled"], True) - cem = last["configured-expiration-mode"] - self.failUnlessEqual(cem[0], "cutoff-date") - self.failUnlessEqual(cem[1], None) - self.failUnlessEqual(cem[2], then) - self.failUnlessEqual(cem[3][0], "mutable") - self.failUnlessEqual(cem[3][1], "immutable") - - rec = last["space-recovered"] - self.failUnlessEqual(rec["examined-buckets"], 4) - self.failUnlessEqual(rec["examined-shares"], 4) - self.failUnlessEqual(rec["actual-buckets"], 2) - self.failUnlessEqual(rec["actual-shares"], 2) - # different platforms have different notions of "blocks used by - # this file", so merely assert that it's a number - self.failUnless(rec["actual-diskbytes"] >= 0, - rec["actual-diskbytes"]) - d.addCallback(_after_first_cycle) - - d.addCallback(lambda ign: self.render1(webstatus)) - def _check_html_after_cycle(html): - s = remove_tags(html) - self.failUnlessIn("Expiration Enabled:" - " expired leases will be removed", s) - date = time.strftime("%Y-%m-%d (%d-%b-%Y) UTC", time.gmtime(then)) - substr = "Leases created or last renewed before %s will be considered expired." % date - self.failUnlessIn(substr, s) - self.failUnlessIn(" recovered: 2 shares, 2 sharesets (1 mutable / 1 immutable), ", s) - d.addCallback(_check_html_after_cycle) - d.addBoth(self._wait_for_yield, ac) + d = self.make_shares(server) + def _do_test(ign): + [immutable_si_0, immutable_si_1, mutable_si_2, mutable_si_3] = self.sis + + d2 = defer.succeed(None) + d2.addCallback(lambda ign: self._assert_sharecount(server, immutable_si_0, 1)) + d2.addCallback(lambda ign: self._assert_leasecount(server, immutable_si_0, (1, 0))) + d2.addCallback(lambda ign: self._assert_sharecount(server, immutable_si_1, 1)) + d2.addCallback(lambda ign: self._assert_leasecount(server, immutable_si_1, (1, 1))) + d2.addCallback(lambda ign: self._assert_sharecount(server, mutable_si_2, 1)) + d2.addCallback(lambda ign: self._assert_leasecount(server, mutable_si_2, (1, 0))) + d2.addCallback(lambda ign: self._assert_sharecount(server, mutable_si_3, 1)) + d2.addCallback(lambda ign: self._assert_leasecount(server, mutable_si_3, (1, 1))) + + def _then(ign): + # artificially crank back the renewal time on the first lease of each + # share to 3000s ago, and set the expiration time to 31 days later. + new_renewal_time = now - 3000 + new_expiration_time = new_renewal_time + 31*24*60*60 + + # Some shares have an extra lease which is set to expire at the + # default time in 31 days from now (age=31days). We then run the + # crawler, which will expire the first lease, making some shares get + # deleted and others stay alive (with one remaining lease) + + aa.add_or_renew_lease(immutable_si_0, 0, new_renewal_time, new_expiration_time) + + # immutable_si_1 gets an extra lease + sa.add_or_renew_lease(immutable_si_1, 0, new_renewal_time, new_expiration_time) + + aa.add_or_renew_lease(mutable_si_2, 0, new_renewal_time, new_expiration_time) + + # mutable_si_3 gets an extra lease + sa.add_or_renew_lease(mutable_si_3, 0, new_renewal_time, new_expiration_time) + + server.setServiceParent(self.sparent) + + # now examine the web status right after the 'aa' prefix has been processed. + d3 = self._after_prefix(None, 'aa', ac) + d3.addCallback(lambda ign: self.render1(webstatus)) + def _check_html_in_cycle(html): + s = remove_tags(html) + # the first bucket encountered gets deleted, and its prefix + # happens to be about 1/5th of the way through the ring, so the + # predictor thinks we'll have 5 shares and that we'll delete them + # all. This part of the test depends upon the SIs landing right + # where they do now. + self.failUnlessIn("The remainder of this cycle is expected to " + "recover: 4 shares, 4 sharesets", s) + self.failUnlessIn("The whole cycle is expected to examine " + "5 shares in 5 sharesets and to recover: " + "5 shares, 5 sharesets", s) + + return ac.set_hook('after_cycle') + d3.addCallback(_check_html_in_cycle) + + d3.addCallback(lambda ign: self._assert_sharecount(server, immutable_si_0, 0)) + d3.addCallback(lambda ign: self._assert_sharecount(server, immutable_si_1, 1)) + d3.addCallback(lambda ign: self._assert_leasecount(server, immutable_si_1, (1, 0))) + d3.addCallback(lambda ign: self._assert_sharecount(server, mutable_si_2, 0)) + d3.addCallback(lambda ign: self._assert_sharecount(server, mutable_si_3, 1)) + d3.addCallback(lambda ign: self._assert_leasecount(server, mutable_si_3, (1, 0))) + + def _after_first_cycle(ignored): + s = ac.get_state() + last = s["history"][0] + + self.failUnlessEqual(last["expiration-enabled"], True) + cem = last["configured-expiration-mode"] + self.failUnlessEqual(cem[0], "cutoff-date") + self.failUnlessEqual(cem[1], None) + self.failUnlessEqual(cem[2], then) + self.failUnlessEqual(cem[3][0], "mutable") + self.failUnlessEqual(cem[3][1], "immutable") + + rec = last["space-recovered"] + self.failUnlessEqual(rec["examined-buckets"], 4) + self.failUnlessEqual(rec["examined-shares"], 4) + self.failUnlessEqual(rec["actual-buckets"], 2) + self.failUnlessEqual(rec["actual-shares"], 2) + # different platforms have different notions of "blocks used by + # this file", so merely assert that it's a number + self.failUnless(rec["actual-diskbytes"] >= 0, + rec["actual-diskbytes"]) + d3.addCallback(_after_first_cycle) + + d3.addCallback(lambda ign: self.render1(webstatus)) + def _check_html_after_cycle(html): + s = remove_tags(html) + self.failUnlessIn("Expiration Enabled:" + " expired leases will be removed", s) + date = time.strftime("%Y-%m-%d (%d-%b-%Y) UTC", time.gmtime(then)) + substr = "Leases created or last renewed before %s will be considered expired." % date + self.failUnlessIn(substr, s) + self.failUnlessIn(" recovered: 2 shares, 2 sharesets (1 mutable / 1 immutable), ", s) + d3.addCallback(_check_html_after_cycle) + d3.addBoth(self._wait_for_yield, ac) + return d3 + d2.addCallback(_then) + return d2 + d.addCallback(_do_test) return d def test_bad_mode(self): @@ -3361,9 +4076,7 @@ class AccountingCrawlerTest(unittest.TestCase, CrawlerTestMixin, WebRenderingMix self.failUnlessEqual(p("2009-03-18"), 1237334400) def test_limited_history(self): - basedir = "storage/AccountingCrawler/limited_history" - fileutil.make_dirs(basedir) - server = StorageServer(basedir, "\x00" * 20) + server = self.create("test_limited_history", detached=True) # finish as fast as possible RETAINED = 2 @@ -3376,67 +4089,24 @@ class AccountingCrawlerTest(unittest.TestCase, CrawlerTestMixin, WebRenderingMix ac.minimum_cycle_time = 0 # create a few shares, with some leases on them - self.make_shares(server) - - server.setServiceParent(self.s) - - d = ac.set_hook('after_cycle') - def _after_cycle(cycle): - if cycle < CYCLES: - return ac.set_hook('after_cycle').addCallback(_after_cycle) - - state = ac.get_state() - self.failUnlessIn("history", state) - h = state["history"] - self.failUnlessEqual(len(h), RETAINED) - self.failUnlessEqual(max(h.keys()), CYCLES) - self.failUnlessEqual(min(h.keys()), CYCLES-RETAINED+1) - d.addCallback(_after_cycle) - d.addBoth(self._wait_for_yield, ac) - return d - - def OFF_test_unpredictable_future(self): - basedir = "storage/AccountingCrawler/unpredictable_future" - fileutil.make_dirs(basedir) - server = StorageServer(basedir, "\x00" * 20) - - # make it start sooner than usual. - ac = server.get_accounting_crawler() - ac.slow_start = 0 - ac.cpu_slice = -1.0 # stop quickly - - self.make_shares(server) - - server.setServiceParent(self.s) - - d = fireEventually() - def _check(ignored): - # this should fire after the first bucket is complete, but before - # the first prefix is complete, so the progress-measurer won't - # think we've gotten far enough to raise our percent-complete - # above 0%, triggering the cannot-predict-the-future code in - # expirer.py . This will have to change if/when the - # progress-measurer gets smart enough to count buckets (we'll - # have to interrupt it even earlier, before it's finished the - # first bucket). - s = ac.get_state() - if "cycle-to-date" not in s: - return reactor.callLater(0.2, _check) - self.failUnlessIn("cycle-to-date", s) - self.failUnlessIn("estimated-remaining-cycle", s) - self.failUnlessIn("estimated-current-cycle", s) - - left = s["estimated-remaining-cycle"]["space-recovered"] - self.failUnlessEqual(left["actual-buckets"], None) - self.failUnlessEqual(left["actual-shares"], None) - self.failUnlessEqual(left["actual-diskbytes"], None) - - full = s["estimated-remaining-cycle"]["space-recovered"] - self.failUnlessEqual(full["actual-buckets"], None) - self.failUnlessEqual(full["actual-shares"], None) - self.failUnlessEqual(full["actual-diskbytes"], None) - - d.addCallback(_check) + d = self.make_shares(server) + def _do_test(ign): + server.setServiceParent(self.sparent) + + d2 = ac.set_hook('after_cycle') + def _after_cycle(cycle): + if cycle < CYCLES: + return ac.set_hook('after_cycle').addCallback(_after_cycle) + + state = ac.get_state() + self.failUnlessIn("history", state) + h = state["history"] + self.failUnlessEqual(len(h), RETAINED) + self.failUnlessEqual(max(h.keys()), CYCLES) + self.failUnlessEqual(min(h.keys()), CYCLES-RETAINED+1) + d2.addCallback(_after_cycle) + d2.addBoth(self._wait_for_yield, ac) + d.addCallback(_do_test) return d def render_json(self, page): @@ -3444,33 +4114,30 @@ class AccountingCrawlerTest(unittest.TestCase, CrawlerTestMixin, WebRenderingMix return d -class WebStatus(unittest.TestCase, WebRenderingMixin): - def setUp(self): - self.s = service.MultiService() - self.s.startService() +class AccountingCrawlerWithDiskBackend(WithDiskBackend, AccountingCrawlerTest, unittest.TestCase): + pass - def tearDown(self): - return self.s.stopService() +#class AccountingCrawlerWithMockCloudBackend(WithMockCloudBackend, AccountingCrawlerTest, unittest.TestCase): +# pass + +class WebStatusWithDiskBackend(WithDiskBackend, WebRenderingMixin, unittest.TestCase): def test_no_server(self): w = StorageStatus(None) html = w.renderSynchronously() self.failUnlessIn("

No Storage Server Running

", html) def test_status(self): - basedir = "storage/WebStatus/status" - fileutil.make_dirs(basedir) - nodeid = "\x00" * 20 - server = StorageServer(basedir, nodeid) - server.setServiceParent(self.s) + server = self.create("test_status") + w = StorageStatus(server, "nickname") d = self.render1(w) def _check_html(html): self.failUnlessIn("

Storage Server Status

", html) s = remove_tags(html) self.failUnlessIn("Server Nickname: nickname", s) - self.failUnlessIn("Server Nodeid: %s" % base32.b2a(nodeid), s) + self.failUnlessIn("Server Nodeid: %s" % base32.b2a(server.get_serverid()), s) self.failUnlessIn("Accepting new shares: Yes", s) self.failUnlessIn("Reserved space: - 0 B (0)", s) d.addCallback(_check_html) @@ -3485,21 +4152,14 @@ class WebStatus(unittest.TestCase, WebRenderingMixin): d.addCallback(_check_json) return d - - def render_json(self, page): - d = self.render1(page, args={"t": ["json"]}) - return d - @mock.patch('allmydata.util.fileutil.get_disk_stats') def test_status_no_disk_stats(self, mock_get_disk_stats): mock_get_disk_stats.side_effect = AttributeError() # Some platforms may have no disk stats API. Make sure the code can handle that # (test runs on all platforms). - basedir = "storage/WebStatus/status_no_disk_stats" - fileutil.make_dirs(basedir) - server = StorageServer(basedir, "\x00" * 20) - server.setServiceParent(self.s) + server = self.create("test_status_no_disk_stats") + w = StorageStatus(server) html = w.renderSynchronously() self.failUnlessIn("

Storage Server Status

", html) @@ -3515,10 +4175,8 @@ class WebStatus(unittest.TestCase, WebRenderingMixin): # If the API to get disk stats exists but a call to it fails, then the status should # show that no shares will be accepted, and get_available_space() should be 0. - basedir = "storage/WebStatus/status_bad_disk_stats" - fileutil.make_dirs(basedir) - server = StorageServer(basedir, "\x00" * 20) - server.setServiceParent(self.s) + server = self.create("test_status_bad_disk_stats") + w = StorageStatus(server) html = w.renderSynchronously() self.failUnlessIn("

Storage Server Status

", html) @@ -3545,16 +4203,14 @@ class WebStatus(unittest.TestCase, WebRenderingMixin): 'avail': avail, } - basedir = "storage/WebStatus/status_right_disk_stats" - fileutil.make_dirs(basedir) - server = StorageServer(basedir, "\x00" * 20, reserved_space=reserved_space) - expecteddir = server.sharedir - server.setServiceParent(self.s) + server = self.create("test_status_right_disk_stats", reserved_space=GB) + expecteddir = server.backend._sharedir + w = StorageStatus(server) html = w.renderSynchronously() self.failIf([True for args in mock_get_disk_stats.call_args_list if args != ((expecteddir, reserved_space), {})], - mock_get_disk_stats.call_args_list) + (mock_get_disk_stats.call_args_list, expecteddir, reserved_space)) self.failUnlessIn("

Storage Server Status

", html) s = remove_tags(html) @@ -3567,10 +4223,8 @@ class WebStatus(unittest.TestCase, WebRenderingMixin): self.failUnlessEqual(server.get_available_space(), 2*GB) def test_readonly(self): - basedir = "storage/WebStatus/readonly" - fileutil.make_dirs(basedir) - server = StorageServer(basedir, "\x00" * 20, readonly_storage=True) - server.setServiceParent(self.s) + server = self.create("test_readonly", readonly=True) + w = StorageStatus(server) html = w.renderSynchronously() self.failUnlessIn("

Storage Server Status

", html) @@ -3578,21 +4232,8 @@ class WebStatus(unittest.TestCase, WebRenderingMixin): self.failUnlessIn("Accepting new shares: No", s) def test_reserved(self): - basedir = "storage/WebStatus/reserved" - fileutil.make_dirs(basedir) - server = StorageServer(basedir, "\x00" * 20, reserved_space=10e6) - server.setServiceParent(self.s) - w = StorageStatus(server) - html = w.renderSynchronously() - self.failUnlessIn("

Storage Server Status

", html) - s = remove_tags(html) - self.failUnlessIn("Reserved space: - 10.00 MB (10000000)", s) + server = self.create("test_reserved", reserved_space=10e6) - def test_huge_reserved(self): - basedir = "storage/WebStatus/reserved" - fileutil.make_dirs(basedir) - server = StorageServer(basedir, "\x00" * 20, reserved_space=10e6) - server.setServiceParent(self.s) w = StorageStatus(server) html = w.renderSynchronously() self.failUnlessIn("

Storage Server Status

", html) @@ -3607,3 +4248,27 @@ class WebStatus(unittest.TestCase, WebRenderingMixin): self.failUnlessEqual(w.render_abbrev_space(None, 10e6), "10.00 MB") self.failUnlessEqual(remove_prefix("foo.bar", "foo."), "bar") self.failUnlessEqual(remove_prefix("foo.bar", "baz."), None) + + +class WebStatusWithMockCloudBackend(WithMockCloudBackend, WebRenderingMixin, unittest.TestCase): + def test_status(self): + server = self.create("test_status") + + w = StorageStatus(server, "nickname") + d = self.render1(w) + def _check_html(html): + self.failUnlessIn("

Storage Server Status

", html) + s = remove_tags(html) + self.failUnlessIn("Server Nickname: nickname", s) + self.failUnlessIn("Server Nodeid: %s" % base32.b2a(server.get_serverid()), s) + self.failUnlessIn("Accepting new shares: Yes", s) + d.addCallback(_check_html) + d.addCallback(lambda ign: self.render_json(w)) + def _check_json(json): + data = simplejson.loads(json) + s = data["stats"] + self.failUnlessEqual(s["storage_server.accepting_immutable_shares"], 1) + self.failUnlessIn("bucket-counter", data) + self.failUnlessIn("lease-checker", data) + d.addCallback(_check_json) + return d diff --git a/src/allmydata/test/test_system.py b/src/allmydata/test/test_system.py index 175a86e2..355caa45 100644 --- a/src/allmydata/test/test_system.py +++ b/src/allmydata/test/test_system.py @@ -8,12 +8,13 @@ from twisted.internet import threads # CLI tests use deferToThread import allmydata from allmydata import uri -from allmydata.storage.backends.disk.mutable import MutableShareFile +from allmydata.storage.backends.disk.mutable import load_mutable_disk_share +from allmydata.storage.backends.cloud import cloud_common, mock_cloud from allmydata.storage.server import si_a2b from allmydata.immutable import offloaded, upload from allmydata.immutable.literal import LiteralFileNode from allmydata.immutable.filenode import ImmutableFileNode -from allmydata.util import idlib, mathutil +from allmydata.util import idlib, mathutil, fileutil from allmydata.util import log, base32 from allmydata.util.verlib import NormalizedVersion from allmydata.util.encodingutil import quote_output, unicode_to_argv, get_filesystem_encoding @@ -56,11 +57,12 @@ class CountingDataUploadable(upload.Data): self.interrupt_after_d.callback(self) return upload.Data.read(self, length) -class SystemTest(SystemTestMixin, RunBinTahoeMixin, unittest.TestCase): + +class SystemTest(SystemTestMixin, RunBinTahoeMixin): timeout = 3600 # It takes longer than 960 seconds on Zandr's ARM box. def test_connections(self): - self.basedir = "system/SystemTest/test_connections" + self.basedir = self.workdir("test_connections") d = self.set_up_nodes() self.extra_node = None d.addCallback(lambda res: self.add_extra_node(self.numclients)) @@ -88,11 +90,11 @@ class SystemTest(SystemTestMixin, RunBinTahoeMixin, unittest.TestCase): del test_connections def test_upload_and_download_random_key(self): - self.basedir = "system/SystemTest/test_upload_and_download_random_key" + self.basedir = self.workdir("test_upload_and_download_random_key") return self._test_upload_and_download(convergence=None) def test_upload_and_download_convergent(self): - self.basedir = "system/SystemTest/test_upload_and_download_convergent" + self.basedir = self.workdir("test_upload_and_download_convergent") return self._test_upload_and_download(convergence="some convergence string") def _test_upload_and_download(self, convergence): @@ -358,7 +360,8 @@ class SystemTest(SystemTestMixin, RunBinTahoeMixin, unittest.TestCase): (bytes_sent, len(DATA))) n = self.clients[1].create_node_from_uri(cap) return download_to_data(n) - d.addCallback(_uploaded) + # FIXME: renable + #d.addCallback(_uploaded) def _check(newdata): self.failUnlessEqual(newdata, DATA) @@ -370,7 +373,8 @@ class SystemTest(SystemTestMixin, RunBinTahoeMixin, unittest.TestCase): self.failUnlessEqual(files, []) files = os.listdir(os.path.join(basedir, "CHK_incoming")) self.failUnlessEqual(files, []) - d.addCallback(_check) + # FIXME: renable + #d.addCallback(_check) return d d.addCallback(_upload_resumable) @@ -416,60 +420,70 @@ class SystemTest(SystemTestMixin, RunBinTahoeMixin, unittest.TestCase): storage_index_s = pieces[-1] storage_index = si_a2b(storage_index_s) for sharename in filenames: - shnum = int(sharename) - filename = os.path.join(dirpath, sharename) - data = (client_num, storage_index, filename, shnum) - shares.append(data) + # If the share is chunked, only pay attention to the first chunk here. + if '.' not in sharename: + shnum = int(sharename) + filename = os.path.join(dirpath, sharename) + data = (client_num, storage_index, filename, shnum) + shares.append(data) if not shares: self.fail("unable to find any share files in %s" % basedir) return shares - def _corrupt_mutable_share(self, filename, which): - msf = MutableShareFile(filename) - datav = msf.readv([ (0, 1000000) ]) - final_share = datav[0] - assert len(final_share) < 1000000 # ought to be truncated - pieces = mutable_layout.unpack_share(final_share) - (seqnum, root_hash, IV, k, N, segsize, datalen, - verification_key, signature, share_hash_chain, block_hash_tree, - share_data, enc_privkey) = pieces - - if which == "seqnum": - seqnum = seqnum + 15 - elif which == "R": - root_hash = self.flip_bit(root_hash) - elif which == "IV": - IV = self.flip_bit(IV) - elif which == "segsize": - segsize = segsize + 15 - elif which == "pubkey": - verification_key = self.flip_bit(verification_key) - elif which == "signature": - signature = self.flip_bit(signature) - elif which == "share_hash_chain": - nodenum = share_hash_chain.keys()[0] - share_hash_chain[nodenum] = self.flip_bit(share_hash_chain[nodenum]) - elif which == "block_hash_tree": - block_hash_tree[-1] = self.flip_bit(block_hash_tree[-1]) - elif which == "share_data": - share_data = self.flip_bit(share_data) - elif which == "encprivkey": - enc_privkey = self.flip_bit(enc_privkey) - - prefix = mutable_layout.pack_prefix(seqnum, root_hash, IV, k, N, - segsize, datalen) - final_share = mutable_layout.pack_share(prefix, - verification_key, - signature, - share_hash_chain, - block_hash_tree, - share_data, - enc_privkey) - msf.writev( [(0, final_share)], None) - + def _corrupt_mutable_share(self, ign, what, which): + (storageindex, filename, shnum) = what + d = defer.succeed(None) + d.addCallback(lambda ign: load_mutable_disk_share(filename, storageindex, shnum)) + def _got_share(msf): + d2 = msf.readv([ (0, 1000000) ]) + def _got_data(datav): + final_share = datav[0] + assert len(final_share) < 1000000 # ought to be truncated + pieces = mutable_layout.unpack_share(final_share) + (seqnum, root_hash, IV, k, N, segsize, datalen, + verification_key, signature, share_hash_chain, block_hash_tree, + share_data, enc_privkey) = pieces + + if which == "seqnum": + seqnum = seqnum + 15 + elif which == "R": + root_hash = self.flip_bit(root_hash) + elif which == "IV": + IV = self.flip_bit(IV) + elif which == "segsize": + segsize = segsize + 15 + elif which == "pubkey": + verification_key = self.flip_bit(verification_key) + elif which == "signature": + signature = self.flip_bit(signature) + elif which == "share_hash_chain": + nodenum = share_hash_chain.keys()[0] + share_hash_chain[nodenum] = self.flip_bit(share_hash_chain[nodenum]) + elif which == "block_hash_tree": + block_hash_tree[-1] = self.flip_bit(block_hash_tree[-1]) + elif which == "share_data": + share_data = self.flip_bit(share_data) + elif which == "encprivkey": + enc_privkey = self.flip_bit(enc_privkey) + + prefix = mutable_layout.pack_prefix(seqnum, root_hash, IV, k, N, + segsize, datalen) + final_share = mutable_layout.pack_share(prefix, + verification_key, + signature, + share_hash_chain, + block_hash_tree, + share_data, + enc_privkey) + + return msf.writev( [(0, final_share)], None) + d2.addCallback(_got_data) + return d2 + d.addCallback(_got_share) + return d def test_mutable(self): - self.basedir = "system/SystemTest/test_mutable" + self.basedir = self.workdir("test_mutable") DATA = "initial contents go here." # 25 bytes % 3 != 0 DATA_uploadable = MutableData(DATA) NEWDATA = "new contents yay" @@ -504,23 +518,24 @@ class SystemTest(SystemTestMixin, RunBinTahoeMixin, unittest.TestCase): filename], stdout=out, stderr=err) output = out.getvalue() + self.failUnlessEqual(err.getvalue(), "") self.failUnlessEqual(rc, 0) try: - self.failUnless("Mutable slot found:\n" in output) - self.failUnless("share_type: SDMF\n" in output) + self.failUnlessIn("Mutable slot found:\n", output) + self.failUnlessIn("share_type: SDMF\n", output) peerid = idlib.nodeid_b2a(self.clients[client_num].nodeid) - self.failUnless(" WE for nodeid: %s\n" % peerid in output) - self.failUnless(" SDMF contents:\n" in output) - self.failUnless(" seqnum: 1\n" in output) - self.failUnless(" required_shares: 3\n" in output) - self.failUnless(" total_shares: 10\n" in output) - self.failUnless(" segsize: 27\n" in output, (output, filename)) - self.failUnless(" datalen: 25\n" in output) + self.failUnlessIn(" WE for nodeid: %s\n" % peerid, output) + self.failUnlessIn(" SDMF contents:\n", output) + self.failUnlessIn(" seqnum: 1\n", output) + self.failUnlessIn(" required_shares: 3\n", output) + self.failUnlessIn(" total_shares: 10\n", output) + self.failUnlessIn(" segsize: 27\n", output) + self.failUnlessIn(" datalen: 25\n", output) # the exact share_hash_chain nodes depends upon the sharenum, # and is more of a hassle to compute than I want to deal with # now - self.failUnless(" share_hash_chain: " in output) - self.failUnless(" block_hash_tree: 1 nodes\n" in output) + self.failUnlessIn(" share_hash_chain: ", output) + self.failUnlessIn(" block_hash_tree: 1 nodes\n", output) expected = (" verify-cap: URI:SSK-Verifier:%s:" % base32.b2a(storage_index)) self.failUnless(expected in output) @@ -596,11 +611,13 @@ class SystemTest(SystemTestMixin, RunBinTahoeMixin, unittest.TestCase): shares = self._find_all_shares(self.basedir) ## sort by share number #shares.sort( lambda a,b: cmp(a[3], b[3]) ) - where = dict([ (shnum, filename) - for (client_num, storage_index, filename, shnum) + where = dict([ (shnum, (storageindex, filename, shnum)) + for (client_num, storageindex, filename, shnum) in shares ]) assert len(where) == 10 # this test is designed for 3-of-10 - for shnum, filename in where.items(): + + d2 = defer.succeed(None) + for shnum, what in where.items(): # shares 7,8,9 are left alone. read will check # (share_hash_chain, block_hash_tree, share_data). New # seqnum+R pairs will trigger a check of (seqnum, R, IV, @@ -608,23 +625,23 @@ class SystemTest(SystemTestMixin, RunBinTahoeMixin, unittest.TestCase): if shnum == 0: # read: this will trigger "pubkey doesn't match # fingerprint". - self._corrupt_mutable_share(filename, "pubkey") - self._corrupt_mutable_share(filename, "encprivkey") + d2.addCallback(self._corrupt_mutable_share, what, "pubkey") + d2.addCallback(self._corrupt_mutable_share, what, "encprivkey") elif shnum == 1: # triggers "signature is invalid" - self._corrupt_mutable_share(filename, "seqnum") + d2.addCallback(self._corrupt_mutable_share, what, "seqnum") elif shnum == 2: # triggers "signature is invalid" - self._corrupt_mutable_share(filename, "R") + d2.addCallback(self._corrupt_mutable_share, what, "R") elif shnum == 3: # triggers "signature is invalid" - self._corrupt_mutable_share(filename, "segsize") + d2.addCallback(self._corrupt_mutable_share, what, "segsize") elif shnum == 4: - self._corrupt_mutable_share(filename, "share_hash_chain") + d2.addCallback(self._corrupt_mutable_share, what, "share_hash_chain") elif shnum == 5: - self._corrupt_mutable_share(filename, "block_hash_tree") + d2.addCallback(self._corrupt_mutable_share, what, "block_hash_tree") elif shnum == 6: - self._corrupt_mutable_share(filename, "share_data") + d2.addCallback(self._corrupt_mutable_share, what, "share_data") # other things to correct: IV, signature # 7,8,9 are left alone @@ -640,8 +657,8 @@ class SystemTest(SystemTestMixin, RunBinTahoeMixin, unittest.TestCase): # for one failure mode at a time. # when we retrieve this, we should get three signature - # failures (where we've mangled seqnum, R, and segsize). The - # pubkey mangling + # failures (where we've mangled seqnum, R, and segsize). + return d2 d.addCallback(_corrupt_shares) d.addCallback(lambda res: self._newnode3.download_best_version()) @@ -716,7 +733,7 @@ class SystemTest(SystemTestMixin, RunBinTahoeMixin, unittest.TestCase): # plaintext_hash check. def test_filesystem(self): - self.basedir = "system/SystemTest/test_filesystem" + self.basedir = self.workdir("test_filesystem") self.data = LARGE_DATA d = self.set_up_nodes(use_stats_gatherer=True) def _new_happy_semantics(ign): @@ -762,6 +779,63 @@ class SystemTest(SystemTestMixin, RunBinTahoeMixin, unittest.TestCase): d.addCallback(self._test_checker) return d + def test_simple(self): + """ + This test is redundant with test_filesystem, but it is simpler, much shorter, and easier for debugging. + It creates a directory containing a subdirectory, and then puts & gets (immutable, SDMF, MDMF) files in + the subdirectory. + """ + + self.basedir = self.workdir("test_simple") + d = self.set_up_nodes(NUMCLIENTS=1, use_stats_gatherer=True) + def _set_happy_and_nodeargs(ign): + for c in self.clients: + # TODO: this hangs with k = n = 10; figure out why. + c.DEFAULT_ENCODING_PARAMETERS['k'] = 3 + c.DEFAULT_ENCODING_PARAMETERS['happy'] = 1 + c.DEFAULT_ENCODING_PARAMETERS['n'] = 3 + self.nodeargs = [ + "--node-directory", self.getdir("client0"), + ] + d.addCallback(_set_happy_and_nodeargs) + def _publish(ign): + c0 = self.clients[0] + d2 = c0.create_dirnode() + def _made_root(new_dirnode): + self._root_directory_uri = new_dirnode.get_uri() + return c0.create_node_from_uri(self._root_directory_uri) + d2.addCallback(_made_root) + d2.addCallback(lambda root: root.create_subdirectory(u"subdir")) + return d2 + d.addCallback(_publish) + + formats = ([], ["--format=SDMF"], ["--format=MDMF"]) + def _put_and_get(ign, i): + name = "file%d" % i + tahoe_path = "%s/subdir/%s" % (self._root_directory_uri, name) + format_options = formats[i] + fn = os.path.join(self.basedir, name) + data = "%s%d\n" % (LARGE_DATA, i) + fileutil.write(fn, data) + + d2 = defer.succeed(None) + d2.addCallback(lambda ign: self._run_cli(self.nodeargs + ["put"] + format_options + [fn, tahoe_path])) + def _check_put( (out, err) ): + self.failUnlessIn("201 Created", err) + self.failUnlessIn("URI:", out) + d2.addCallback(_check_put) + + d2.addCallback(lambda ign: self._run_cli(self.nodeargs + ["get"] + [tahoe_path])) + def _check_get( (out, err) ): + self.failUnlessEqual(err, "") + self.failUnlessEqual(out, data) + d2.addCallback(_check_get) + return d2 + + for i in range(len(formats)): + d.addCallback(_put_and_get, i) + return d + def _test_introweb(self, res): d = getPage(self.introweb_url, method="GET", followRedirect=True) def _check(res): @@ -1237,16 +1311,12 @@ class SystemTest(SystemTestMixin, RunBinTahoeMixin, unittest.TestCase): # exercise more code paths workdir = os.path.join(self.getdir("client0"), "helper") incfile = os.path.join(workdir, "CHK_incoming", "spurious") - f = open(incfile, "wb") - f.write("small file") - f.close() + fileutil.write(incfile, "small file") then = time.time() - 86400*3 now = time.time() os.utime(incfile, (now, then)) encfile = os.path.join(workdir, "CHK_encoding", "spurious") - f = open(encfile, "wb") - f.write("less small file") - f.close() + fileutil.write(encfile, "less small file") os.utime(encfile, (now, then)) d.addCallback(_got_helper_status) # and that the json form exists @@ -1336,24 +1406,25 @@ class SystemTest(SystemTestMixin, RunBinTahoeMixin, unittest.TestCase): unicode_to_argv(filename)], stdout=out, stderr=err) output = out.getvalue() + self.failUnlessEqual(err.getvalue(), "") self.failUnlessEqual(rc, 0) # we only upload a single file, so we can assert some things about # its size and shares. self.failUnlessIn("share filename: %s" % quote_output(abspath_expanduser_unicode(filename)), output) - self.failUnlessIn("size: %d\n" % len(self.data), output) - self.failUnlessIn("num_segments: 1\n", output) + self.failUnlessIn(" file_size: %d\n" % len(self.data), output) + self.failUnlessIn(" num_segments: 1\n", output) # segment_size is always a multiple of needed_shares - self.failUnlessIn("segment_size: %d\n" % mathutil.next_multiple(len(self.data), 3), output) - self.failUnlessIn("total_shares: 10\n", output) + self.failUnlessIn(" segment_size: %d\n" % mathutil.next_multiple(len(self.data), 3), output) + self.failUnlessIn(" total_shares: 10\n", output) # keys which are supposed to be present - for key in ("size", "num_segments", "segment_size", + for key in ("file_size", "num_segments", "segment_size", "needed_shares", "total_shares", "codec_name", "codec_params", "tail_codec_params", #"plaintext_hash", "plaintext_root_hash", "crypttext_hash", "crypttext_root_hash", "share_root_hash", "UEB_hash"): - self.failUnlessIn("%s: " % key, output) + self.failUnlessIn(" %s: " % key, output) self.failUnlessIn(" verify-cap: URI:CHK-Verifier:", output) # now use its storage index to find the other shares using the @@ -1365,6 +1436,7 @@ class SystemTest(SystemTestMixin, RunBinTahoeMixin, unittest.TestCase): nodedirs = [self.getdir("client%d" % i) for i in range(self.numclients)] cmd = ["debug", "find-shares", storage_index_s] + nodedirs rc = runner.runner(cmd, stdout=out, stderr=err) + self.failUnlessEqual(err.getvalue(), "") self.failUnlessEqual(rc, 0) out.seek(0) sharefiles = [sfn.strip() for sfn in out.readlines()] @@ -1375,10 +1447,11 @@ class SystemTest(SystemTestMixin, RunBinTahoeMixin, unittest.TestCase): nodedirs = [self.getdir("client%d" % i) for i in range(self.numclients)] cmd = ["debug", "catalog-shares"] + nodedirs rc = runner.runner(cmd, stdout=out, stderr=err) + self.failUnlessEqual(err.getvalue(), "") self.failUnlessEqual(rc, 0) out.seek(0) descriptions = [sfn.strip() for sfn in out.readlines()] - self.failUnlessEqual(len(descriptions), 30) + self.failUnlessEqual(len(descriptions), 30, repr((cmd, descriptions))) matching = [line for line in descriptions if line.startswith("CHK %s " % storage_index_s)] @@ -1501,12 +1574,12 @@ class SystemTest(SystemTestMixin, RunBinTahoeMixin, unittest.TestCase): files = [] datas = [] - for i in range(10): + for i in range(11): fn = os.path.join(self.basedir, "file%d" % i) files.append(fn) data = "data to be uploaded: file%d\n" % i datas.append(data) - open(fn,"wb").write(data) + fileutil.write(fn, data) def _check_stdout_against((out,err), filenum=None, data=None): self.failUnlessEqual(err, "") @@ -1520,7 +1593,7 @@ class SystemTest(SystemTestMixin, RunBinTahoeMixin, unittest.TestCase): d.addCallback(run, "put", files[0], "tahoe-file0") def _put_out((out,err)): self.failUnless("URI:LIT:" in out, out) - self.failUnless("201 Created" in err, err) + self.failUnlessIn("201 Created", err) uri0 = out.strip() return run(None, "get", uri0) d.addCallback(_put_out) @@ -1529,13 +1602,23 @@ class SystemTest(SystemTestMixin, RunBinTahoeMixin, unittest.TestCase): d.addCallback(run, "put", files[1], "subdir/tahoe-file1") # tahoe put bar tahoe:FOO d.addCallback(run, "put", files[2], "tahoe:file2") + d.addCallback(run, "put", "--format=SDMF", files[3], "tahoe:file3") - def _check_put_mutable((out,err)): + def _check_put_sdmf((out,err)): + self.failUnlessIn("201 Created", err) self._mutable_file3_uri = out.strip() - d.addCallback(_check_put_mutable) + d.addCallback(_check_put_sdmf) d.addCallback(run, "get", "tahoe:file3") d.addCallback(_check_stdout_against, 3) + d.addCallback(run, "put", "--format=MDMF", files[10], "tahoe:file10") + def _check_put_mdmf((out,err)): + self.failUnlessIn("201 Created", err) + self._mutable_file10_uri = out.strip() + d.addCallback(_check_put_mdmf) + d.addCallback(run, "get", "tahoe:file10") + d.addCallback(_check_stdout_against, 10) + # tahoe put FOO STDIN_DATA = "This is the file to upload from stdin." d.addCallback(run, "put", "-", "tahoe-file-stdin", stdin=STDIN_DATA) @@ -1544,7 +1627,7 @@ class SystemTest(SystemTestMixin, RunBinTahoeMixin, unittest.TestCase): stdin="Other file from stdin.") d.addCallback(run, "ls") - d.addCallback(_check_ls, ["tahoe-file0", "file2", "file3", "subdir", + d.addCallback(_check_ls, ["tahoe-file0", "file2", "file3", "file10", "subdir", "tahoe-file-stdin", "from-stdin"]) d.addCallback(run, "ls", "subdir") d.addCallback(_check_ls, ["tahoe-file1"]) @@ -1585,7 +1668,7 @@ class SystemTest(SystemTestMixin, RunBinTahoeMixin, unittest.TestCase): if "tahoe-file-stdin" in l: self.failUnless(l.startswith("-r-- "), l) self.failUnless(" %d " % len(STDIN_DATA) in l) - if "file3" in l: + if "file3" in l or "file10" in l: self.failUnless(l.startswith("-rw- "), l) # mutable d.addCallback(_check_ls_l) @@ -1595,6 +1678,8 @@ class SystemTest(SystemTestMixin, RunBinTahoeMixin, unittest.TestCase): for l in lines: if "file3" in l: self.failUnless(self._mutable_file3_uri in l) + if "file10" in l: + self.failUnless(self._mutable_file10_uri in l) d.addCallback(_check_ls_uri) d.addCallback(run, "ls", "--readonly-uri") @@ -1603,9 +1688,13 @@ class SystemTest(SystemTestMixin, RunBinTahoeMixin, unittest.TestCase): for l in lines: if "file3" in l: rw_uri = self._mutable_file3_uri - u = uri.from_string_mutable_filenode(rw_uri) - ro_uri = u.get_readonly().to_string() - self.failUnless(ro_uri in l) + elif "file10" in l: + rw_uri = self._mutable_file10_uri + else: + break + u = uri.from_string_mutable_filenode(rw_uri) + ro_uri = u.get_readonly().to_string() + self.failUnless(ro_uri in l) d.addCallback(_check_ls_rouri) @@ -1673,13 +1762,13 @@ class SystemTest(SystemTestMixin, RunBinTahoeMixin, unittest.TestCase): # recursive copy: setup dn = os.path.join(self.basedir, "dir1") os.makedirs(dn) - open(os.path.join(dn, "rfile1"), "wb").write("rfile1") - open(os.path.join(dn, "rfile2"), "wb").write("rfile2") - open(os.path.join(dn, "rfile3"), "wb").write("rfile3") + fileutil.write(os.path.join(dn, "rfile1"), "rfile1") + fileutil.write(os.path.join(dn, "rfile2"), "rfile2") + fileutil.write(os.path.join(dn, "rfile3"), "rfile3") sdn2 = os.path.join(dn, "subdir2") os.makedirs(sdn2) - open(os.path.join(sdn2, "rfile4"), "wb").write("rfile4") - open(os.path.join(sdn2, "rfile5"), "wb").write("rfile5") + fileutil.write(os.path.join(sdn2, "rfile4"), "rfile4") + fileutil.write(os.path.join(sdn2, "rfile5"), "rfile5") # from disk into tahoe d.addCallback(run, "cp", "-r", dn, "tahoe:dir1") @@ -1757,7 +1846,7 @@ class SystemTest(SystemTestMixin, RunBinTahoeMixin, unittest.TestCase): def test_filesystem_with_cli_in_subprocess(self): # We do this in a separate test so that test_filesystem doesn't skip if we can't run bin/tahoe. - self.basedir = "system/SystemTest/test_filesystem_with_cli_in_subprocess" + self.basedir = self.workdir("test_filesystem_with_cli_in_subprocess") d = self.set_up_nodes() def _new_happy_semantics(ign): for c in self.clients: @@ -1800,43 +1889,6 @@ class SystemTest(SystemTestMixin, RunBinTahoeMixin, unittest.TestCase): d.addCallback(_check_ls) return d - def test_debug_trial(self): - def _check_for_line(lines, result, test): - for l in lines: - if result in l and test in l: - return - self.fail("output (prefixed with '##') does not have a line containing both %r and %r:\n## %s" - % (result, test, "\n## ".join(lines))) - - def _check_for_outcome(lines, out, outcome): - self.failUnlessIn(outcome, out, "output (prefixed with '##') does not contain %r:\n## %s" - % (outcome, "\n## ".join(lines))) - - d = self.run_bintahoe(['debug', 'trial', '--reporter=verbose', - 'allmydata.test.trialtest']) - def _check_failure( (out, err, rc) ): - self.failUnlessEqual(rc, 1) - lines = out.split('\n') - _check_for_line(lines, "[SKIPPED]", "test_skip") - _check_for_line(lines, "[TODO]", "test_todo") - _check_for_line(lines, "[FAIL]", "test_fail") - _check_for_line(lines, "[ERROR]", "test_deferred_error") - _check_for_line(lines, "[ERROR]", "test_error") - _check_for_outcome(lines, out, "FAILED") - d.addCallback(_check_failure) - - # the --quiet argument regression-tests a problem in finding which arguments to pass to trial - d.addCallback(lambda ign: self.run_bintahoe(['--quiet', 'debug', 'trial', '--reporter=verbose', - 'allmydata.test.trialtest.Success'])) - def _check_success( (out, err, rc) ): - self.failUnlessEqual(rc, 0) - lines = out.split('\n') - _check_for_line(lines, "[SKIPPED]", "test_skip") - _check_for_line(lines, "[TODO]", "test_todo") - _check_for_outcome(lines, out, "PASSED") - d.addCallback(_check_success) - return d - def _run_cli(self, argv, stdin=""): #print "CLI:", argv stdout, stderr = StringIO(), StringIO() @@ -1885,6 +1937,35 @@ class SystemTest(SystemTestMixin, RunBinTahoeMixin, unittest.TestCase): return d +class SystemWithDiskBackend(SystemTest, unittest.TestCase): + # The disk backend can use default options. + pass + + +class SystemWithMockCloudBackend(SystemTest, unittest.TestCase): + def setUp(self): + SystemTest.setUp(self) + + # A smaller chunk size causes the tests to exercise more cases in the chunking implementation. + self.patch(cloud_common, 'PREFERRED_CHUNK_SIZE', 500) + + # This causes ContainerListMixin to be exercised. + self.patch(mock_cloud, 'MAX_KEYS', 2) + + def _get_extra_config(self, i): + # all nodes are storage servers + return ("[storage]\n" + "backend = mock_cloud\n") + + def test_filesystem(self): + return SystemTest.test_filesystem(self) + test_filesystem.todo = "Share dumping has not been updated to take into account chunked shares." + + def test_mutable(self): + return SystemTest.test_mutable(self) + test_mutable.todo = "Share dumping has not been updated to take into account chunked shares." + + class Connections(SystemTestMixin, unittest.TestCase): def test_rref(self): if NormalizedVersion(foolscap.__version__) < NormalizedVersion('0.6.4'): diff --git a/src/allmydata/test/test_upload.py b/src/allmydata/test/test_upload.py index 7d65926c..47c1afc6 100644 --- a/src/allmydata/test/test_upload.py +++ b/src/allmydata/test/test_upload.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- -import os, shutil +import os from cStringIO import StringIO from twisted.trial import unittest from twisted.python.failure import Failure @@ -11,7 +11,7 @@ import allmydata # for __full_version__ from allmydata import uri, monitor, client from allmydata.immutable import upload, encode from allmydata.interfaces import FileTooLargeError, UploadUnhappinessError -from allmydata.util import log, base32 +from allmydata.util import base32, fileutil from allmydata.util.assertutil import precondition from allmydata.util.deferredutil import DeferredListShouldSucceed from allmydata.test.no_network import GridTestMixin @@ -19,7 +19,6 @@ from allmydata.test.common_util import ShouldFailMixin from allmydata.util.happinessutil import servers_of_happiness, \ shares_by_server, merge_servers from allmydata.storage_client import StorageFarmBroker -from allmydata.storage.server import storage_index_to_dir from allmydata.client import Client MiB = 1024*1024 @@ -755,7 +754,7 @@ class EncodingParameters(GridTestMixin, unittest.TestCase, SetDEPMixin, servertoshnums = {} # k: server, v: set(shnum) for i, c in self.g.servers_by_number.iteritems(): - for (dirp, dirns, fns) in os.walk(c.sharedir): + for (dirp, dirns, fns) in os.walk(c.backend._sharedir): for fn in fns: try: sharenum = int(fn) @@ -815,40 +814,17 @@ class EncodingParameters(GridTestMixin, unittest.TestCase, SetDEPMixin, h = self.g.clients[0].DEFAULT_ENCODING_PARAMETERS['happy'] return is_happy_enough(servertoshnums, h, k) + # for compatibility, before we refactor this class to use the methods in GridTestMixin def _add_server(self, server_number, readonly=False): - assert self.g, "I tried to find a grid at self.g, but failed" - ss = self.g.make_server(server_number, readonly) - log.msg("just created a server, number: %s => %s" % (server_number, ss,)) - self.g.add_server(server_number, ss) - - def _add_server_with_share(self, server_number, share_number=None, - readonly=False): - self._add_server(server_number, readonly) - if share_number is not None: - self._copy_share_to_server(share_number, server_number) + return self.add_server(server_number, readonly=readonly) + def _add_server_with_share(self, server_number, share_number=None, readonly=False): + self.add_server_with_share(self.uri, server_number=server_number, + share_number=share_number, readonly=readonly) def _copy_share_to_server(self, share_number, server_number): - ss = self.g.servers_by_number[server_number] - # Copy share i from the directory associated with the first - # storage server to the directory associated with this one. - assert self.g, "I tried to find a grid at self.g, but failed" - assert self.shares, "I tried to find shares at self.shares, but failed" - old_share_location = self.shares[share_number][2] - new_share_location = os.path.join(ss.storedir, "shares") - si = uri.from_string(self.uri).get_storage_index() - new_share_location = os.path.join(new_share_location, - storage_index_to_dir(si)) - if not os.path.exists(new_share_location): - os.makedirs(new_share_location) - new_share_location = os.path.join(new_share_location, - str(share_number)) - if old_share_location != new_share_location: - shutil.copy(old_share_location, new_share_location) - shares = self.find_uri_shares(self.uri) - # Make sure that the storage server has the share. - self.failUnless((share_number, ss.my_nodeid, new_share_location) - in shares) + self.copy_share_to_server(self.uri, server_number=server_number, + share_number=share_number) def _setup_grid(self): """ @@ -999,8 +975,7 @@ class EncodingParameters(GridTestMixin, unittest.TestCase, SetDEPMixin, readonly=True)) # Remove the first share from server 0. def _remove_share_0_from_server_0(): - share_location = self.shares[0][2] - os.remove(share_location) + fileutil.remove(self.shares[0][2]) d.addCallback(lambda ign: _remove_share_0_from_server_0()) # Set happy = 4 in the client. @@ -1129,8 +1104,7 @@ class EncodingParameters(GridTestMixin, unittest.TestCase, SetDEPMixin, self._copy_share_to_server(i, 2) d.addCallback(_copy_shares) # Remove the first server, and add a placeholder with share 0 - d.addCallback(lambda ign: - self.g.remove_server(self.g.servers_by_number[0].my_nodeid)) + d.addCallback(lambda ign: self.remove_server(0)) d.addCallback(lambda ign: self._add_server_with_share(server_number=4, share_number=0)) # Now try uploading. @@ -1161,8 +1135,7 @@ class EncodingParameters(GridTestMixin, unittest.TestCase, SetDEPMixin, d.addCallback(lambda ign: self._add_server(server_number=4)) d.addCallback(_copy_shares) - d.addCallback(lambda ign: - self.g.remove_server(self.g.servers_by_number[0].my_nodeid)) + d.addCallback(lambda ign: self.remove_server(0)) d.addCallback(_reset_encoding_parameters) d.addCallback(lambda client: client.upload(upload.Data("data" * 10000, convergence=""))) @@ -1224,8 +1197,7 @@ class EncodingParameters(GridTestMixin, unittest.TestCase, SetDEPMixin, self._copy_share_to_server(i, 2) d.addCallback(_copy_shares) # Remove server 0, and add another in its place - d.addCallback(lambda ign: - self.g.remove_server(self.g.servers_by_number[0].my_nodeid)) + d.addCallback(lambda ign: self.remove_server(0)) d.addCallback(lambda ign: self._add_server_with_share(server_number=4, share_number=0, readonly=True)) @@ -1266,8 +1238,7 @@ class EncodingParameters(GridTestMixin, unittest.TestCase, SetDEPMixin, for i in xrange(1, 10): self._copy_share_to_server(i, 2) d.addCallback(_copy_shares) - d.addCallback(lambda ign: - self.g.remove_server(self.g.servers_by_number[0].my_nodeid)) + d.addCallback(lambda ign: self.remove_server(0)) def _reset_encoding_parameters(ign, happy=4): client = self.g.clients[0] client.DEFAULT_ENCODING_PARAMETERS['happy'] = happy @@ -1303,10 +1274,7 @@ class EncodingParameters(GridTestMixin, unittest.TestCase, SetDEPMixin, # remove the original server # (necessary to ensure that the Tahoe2ServerSelector will distribute # all the shares) - def _remove_server(ign): - server = self.g.servers_by_number[0] - self.g.remove_server(server.my_nodeid) - d.addCallback(_remove_server) + d.addCallback(lambda ign: self.remove_server(0)) # This should succeed; we still have 4 servers, and the # happiness of the upload is 4. d.addCallback(lambda ign: @@ -1318,7 +1286,7 @@ class EncodingParameters(GridTestMixin, unittest.TestCase, SetDEPMixin, d.addCallback(lambda ign: self._setup_and_upload()) d.addCallback(_do_server_setup) - d.addCallback(_remove_server) + d.addCallback(lambda ign: self.remove_server(0)) d.addCallback(lambda ign: self.shouldFail(UploadUnhappinessError, "test_dropped_servers_in_encoder", @@ -1340,14 +1308,14 @@ class EncodingParameters(GridTestMixin, unittest.TestCase, SetDEPMixin, self._add_server_with_share(4, 7, readonly=True) self._add_server_with_share(5, 8, readonly=True) d.addCallback(_do_server_setup_2) - d.addCallback(_remove_server) + d.addCallback(lambda ign: self.remove_server(0)) d.addCallback(lambda ign: self._do_upload_with_broken_servers(1)) d.addCallback(_set_basedir) d.addCallback(lambda ign: self._setup_and_upload()) d.addCallback(_do_server_setup_2) - d.addCallback(_remove_server) + d.addCallback(lambda ign: self.remove_server(0)) d.addCallback(lambda ign: self.shouldFail(UploadUnhappinessError, "test_dropped_servers_in_encoder", @@ -1561,8 +1529,7 @@ class EncodingParameters(GridTestMixin, unittest.TestCase, SetDEPMixin, for i in xrange(1, 10): self._copy_share_to_server(i, 1) d.addCallback(_copy_shares) - d.addCallback(lambda ign: - self.g.remove_server(self.g.servers_by_number[0].my_nodeid)) + d.addCallback(lambda ign: self.remove_server(0)) def _prepare_client(ign): client = self.g.clients[0] client.DEFAULT_ENCODING_PARAMETERS['happy'] = 4 @@ -1584,7 +1551,7 @@ class EncodingParameters(GridTestMixin, unittest.TestCase, SetDEPMixin, def _setup(ign): for i in xrange(1, 11): self._add_server(server_number=i) - self.g.remove_server(self.g.servers_by_number[0].my_nodeid) + self.remove_server(0) c = self.g.clients[0] # We set happy to an unsatisfiable value so that we can check the # counting in the exception message. The same progress message @@ -1611,7 +1578,7 @@ class EncodingParameters(GridTestMixin, unittest.TestCase, SetDEPMixin, self._add_server(server_number=i) self._add_server(server_number=11, readonly=True) self._add_server(server_number=12, readonly=True) - self.g.remove_server(self.g.servers_by_number[0].my_nodeid) + self.remove_server(0) c = self.g.clients[0] c.DEFAULT_ENCODING_PARAMETERS['happy'] = 45 return c @@ -1639,8 +1606,7 @@ class EncodingParameters(GridTestMixin, unittest.TestCase, SetDEPMixin, # the first one that the selector sees. for i in xrange(10): self._copy_share_to_server(i, 9) - # Remove server 0, and its contents - self.g.remove_server(self.g.servers_by_number[0].my_nodeid) + self.remove_server(0) # Make happiness unsatisfiable c = self.g.clients[0] c.DEFAULT_ENCODING_PARAMETERS['happy'] = 45 @@ -1660,7 +1626,7 @@ class EncodingParameters(GridTestMixin, unittest.TestCase, SetDEPMixin, def _then(ign): for i in xrange(1, 11): self._add_server(server_number=i, readonly=True) - self.g.remove_server(self.g.servers_by_number[0].my_nodeid) + self.remove_server(0) c = self.g.clients[0] c.DEFAULT_ENCODING_PARAMETERS['k'] = 2 c.DEFAULT_ENCODING_PARAMETERS['happy'] = 4 @@ -1696,8 +1662,7 @@ class EncodingParameters(GridTestMixin, unittest.TestCase, SetDEPMixin, self._add_server(server_number=4, readonly=True)) d.addCallback(lambda ign: self._add_server(server_number=5, readonly=True)) - d.addCallback(lambda ign: - self.g.remove_server(self.g.servers_by_number[0].my_nodeid)) + d.addCallback(lambda ign: self.remove_server(0)) def _reset_encoding_parameters(ign, happy=4): client = self.g.clients[0] client.DEFAULT_ENCODING_PARAMETERS['happy'] = happy @@ -1732,7 +1697,7 @@ class EncodingParameters(GridTestMixin, unittest.TestCase, SetDEPMixin, d.addCallback(lambda ign: self._add_server(server_number=2)) def _break_server_2(ign): - serverid = self.g.servers_by_number[2].my_nodeid + serverid = self.get_server(2).get_serverid() self.g.break_server(serverid) d.addCallback(_break_server_2) d.addCallback(lambda ign: @@ -1741,8 +1706,7 @@ class EncodingParameters(GridTestMixin, unittest.TestCase, SetDEPMixin, self._add_server(server_number=4, readonly=True)) d.addCallback(lambda ign: self._add_server(server_number=5, readonly=True)) - d.addCallback(lambda ign: - self.g.remove_server(self.g.servers_by_number[0].my_nodeid)) + d.addCallback(lambda ign: self.remove_server(0)) d.addCallback(_reset_encoding_parameters) d.addCallback(lambda client: self.shouldFail(UploadUnhappinessError, "test_selection_exceptions", @@ -1853,8 +1817,7 @@ class EncodingParameters(GridTestMixin, unittest.TestCase, SetDEPMixin, # Copy shares self._copy_share_to_server(1, 1) self._copy_share_to_server(2, 1) - # Remove server 0 - self.g.remove_server(self.g.servers_by_number[0].my_nodeid) + self.remove_server(0) client = self.g.clients[0] client.DEFAULT_ENCODING_PARAMETERS['happy'] = 3 return client @@ -1887,9 +1850,9 @@ class EncodingParameters(GridTestMixin, unittest.TestCase, SetDEPMixin, self._copy_share_to_server(3, 1) storedir = self.get_serverdir(0) # remove the storedir, wiping out any existing shares - shutil.rmtree(storedir) + fileutil.rm_dir(storedir) # create an empty storedir to replace the one we just removed - os.mkdir(storedir) + fileutil.make_dirs(storedir) client = self.g.clients[0] client.DEFAULT_ENCODING_PARAMETERS['happy'] = 4 return client @@ -1928,9 +1891,9 @@ class EncodingParameters(GridTestMixin, unittest.TestCase, SetDEPMixin, self._copy_share_to_server(3, 1) storedir = self.get_serverdir(0) # remove the storedir, wiping out any existing shares - shutil.rmtree(storedir) + fileutil.rm_dir(storedir) # create an empty storedir to replace the one we just removed - os.mkdir(storedir) + fileutil.make_dirs(storedir) client = self.g.clients[0] client.DEFAULT_ENCODING_PARAMETERS['happy'] = 4 return client @@ -1968,8 +1931,7 @@ class EncodingParameters(GridTestMixin, unittest.TestCase, SetDEPMixin, readonly=True) self._add_server_with_share(server_number=4, share_number=3, readonly=True) - # Remove server 0. - self.g.remove_server(self.g.servers_by_number[0].my_nodeid) + self.remove_server(0) # Set the client appropriately c = self.g.clients[0] c.DEFAULT_ENCODING_PARAMETERS['happy'] = 4 diff --git a/src/allmydata/test/test_web.py b/src/allmydata/test/test_web.py index ebbf15ea..a119340c 100644 --- a/src/allmydata/test/test_web.py +++ b/src/allmydata/test/test_web.py @@ -4565,20 +4565,22 @@ class Grid(GridTestMixin, WebErrorMixin, ShouldFailMixin, testutil.ReallyEqualMi self.fileurls[which] = "uri/" + urllib.quote(self.uris[which]) d.addCallback(_compute_fileurls) - def _clobber_shares(ignored): - good_shares = self.find_uri_shares(self.uris["good"]) - self.failUnlessReallyEqual(len(good_shares), 10) - sick_shares = self.find_uri_shares(self.uris["sick"]) - os.unlink(sick_shares[0][2]) - dead_shares = self.find_uri_shares(self.uris["dead"]) + d.addCallback(lambda ign: self.find_uri_shares(self.uris["good"])) + d.addCallback(lambda good_shares: self.failUnlessReallyEqual(len(good_shares), 10)) + d.addCallback(lambda ign: self.find_uri_shares(self.uris["sick"])) + d.addCallback(lambda sick_shares: fileutil.remove(sick_shares[0][2])) + d.addCallback(lambda ign: self.find_uri_shares(self.uris["dead"])) + def _remove_dead_shares(dead_shares): for i in range(1, 10): - os.unlink(dead_shares[i][2]) - c_shares = self.find_uri_shares(self.uris["corrupt"]) + fileutil.remove(dead_shares[i][2]) + d.addCallback(_remove_dead_shares) + d.addCallback(lambda ign: self.find_uri_shares(self.uris["corrupt"])) + def _corrupt_shares(c_shares): cso = CorruptShareOptions() cso.stdout = StringIO() cso.parseOptions([c_shares[0][2]]) corrupt_share(cso) - d.addCallback(_clobber_shares) + d.addCallback(_corrupt_shares) d.addCallback(self.CHECK, "good", "t=check") def _got_html_good(res): @@ -4705,20 +4707,22 @@ class Grid(GridTestMixin, WebErrorMixin, ShouldFailMixin, testutil.ReallyEqualMi self.fileurls[which] = "uri/" + urllib.quote(self.uris[which]) d.addCallback(_compute_fileurls) - def _clobber_shares(ignored): - good_shares = self.find_uri_shares(self.uris["good"]) - self.failUnlessReallyEqual(len(good_shares), 10) - sick_shares = self.find_uri_shares(self.uris["sick"]) - os.unlink(sick_shares[0][2]) - dead_shares = self.find_uri_shares(self.uris["dead"]) + d.addCallback(lambda ign: self.find_uri_shares(self.uris["good"])) + d.addCallback(lambda good_shares: self.failUnlessReallyEqual(len(good_shares), 10)) + d.addCallback(lambda ign: self.find_uri_shares(self.uris["sick"])) + d.addCallback(lambda sick_shares: fileutil.remove(sick_shares[0][2])) + d.addCallback(lambda ign: self.find_uri_shares(self.uris["dead"])) + def _remove_dead_shares(dead_shares): for i in range(1, 10): - os.unlink(dead_shares[i][2]) - c_shares = self.find_uri_shares(self.uris["corrupt"]) + fileutil.remove(dead_shares[i][2]) + d.addCallback(_remove_dead_shares) + d.addCallback(lambda ign: self.find_uri_shares(self.uris["corrupt"])) + def _corrupt_shares(c_shares): cso = CorruptShareOptions() cso.stdout = StringIO() cso.parseOptions([c_shares[0][2]]) corrupt_share(cso) - d.addCallback(_clobber_shares) + d.addCallback(_corrupt_shares) d.addCallback(self.CHECK, "good", "t=check&repair=true") def _got_html_good(res): @@ -4774,10 +4778,8 @@ class Grid(GridTestMixin, WebErrorMixin, ShouldFailMixin, testutil.ReallyEqualMi self.fileurls[which] = "uri/" + urllib.quote(self.uris[which]) d.addCallback(_compute_fileurls) - def _clobber_shares(ignored): - sick_shares = self.find_uri_shares(self.uris["sick"]) - os.unlink(sick_shares[0][2]) - d.addCallback(_clobber_shares) + d.addCallback(lambda ign: self.find_uri_shares(self.uris["sick"])) + d.addCallback(lambda sick_shares: fileutil.remove(sick_shares[0][2])) d.addCallback(self.CHECK, "sick", "t=check&repair=true&output=json") def _got_json_sick(res): @@ -5090,9 +5092,7 @@ class Grid(GridTestMixin, WebErrorMixin, ShouldFailMixin, testutil.ReallyEqualMi future_node = UnknownNode(unknown_rwcap, unknown_rocap) d.addCallback(lambda ign: self.rootnode.set_node(u"future", future_node)) - def _clobber_shares(ignored): - self.delete_shares_numbered(self.uris["sick"], [0,1]) - d.addCallback(_clobber_shares) + d.addCallback(lambda ign: self.delete_shares_numbered(self.uris["sick"], [0,1])) # root # root/good @@ -5264,21 +5264,22 @@ class Grid(GridTestMixin, WebErrorMixin, ShouldFailMixin, testutil.ReallyEqualMi #d.addCallback(lambda fn: self.rootnode.set_node(u"corrupt", fn)) #d.addCallback(_stash_uri, "corrupt") - def _clobber_shares(ignored): - good_shares = self.find_uri_shares(self.uris["good"]) - self.failUnlessReallyEqual(len(good_shares), 10) - sick_shares = self.find_uri_shares(self.uris["sick"]) - os.unlink(sick_shares[0][2]) - #dead_shares = self.find_uri_shares(self.uris["dead"]) - #for i in range(1, 10): - # os.unlink(dead_shares[i][2]) - - #c_shares = self.find_uri_shares(self.uris["corrupt"]) - #cso = CorruptShareOptions() - #cso.stdout = StringIO() - #cso.parseOptions([c_shares[0][2]]) - #corrupt_share(cso) - d.addCallback(_clobber_shares) + d.addCallback(lambda ign: self.find_uri_shares(self.uris["good"])) + d.addCallback(lambda good_shares: self.failUnlessReallyEqual(len(good_shares), 10)) + d.addCallback(lambda ign: self.find_uri_shares(self.uris["sick"])) + d.addCallback(lambda sick_shares: fileutil.remove(sick_shares[0][2])) + #d.addCallback(lambda ign: self.find_uri_shares(self.uris["dead"])) + #def _remove_dead_shares(dead_shares): + # for i in range(1, 10): + # fileutil.remove(dead_shares[i][2]) + #d.addCallback(_remove_dead_shares) + #d.addCallback(lambda ign: self.find_uri_shares(self.uris["corrupt"])) + #def _corrupt_shares(c_shares): + # cso = CorruptShareOptions() + # cso.stdout = StringIO() + # cso.parseOptions([c_shares[0][2]]) + # corrupt_share(cso) + #d.addCallback(_corrupt_shares) # root # root/good CHK, 10 shares @@ -5327,7 +5328,7 @@ class Grid(GridTestMixin, WebErrorMixin, ShouldFailMixin, testutil.ReallyEqualMi d.addErrback(self.explain_web_error) return d - def _assert_leasecount(self, ignored, which, expected): + def _assert_leasecount(self, ign, which, expected): u = self.uris[which] si = uri.from_string(u).get_storage_index() num_leases = 0 @@ -5395,6 +5396,8 @@ class Grid(GridTestMixin, WebErrorMixin, ShouldFailMixin, testutil.ReallyEqualMi d.addCallback(self.CHECK, "one", "t=check&add-lease=true", clientnum=1) d.addCallback(_got_html_good) + # XXX why are the checks below commented out? --Zooko 2012-11-27 + #d.addCallback(self._assert_leasecount, "one", 2*N) #d.addCallback(self.CHECK, "mutable", "t=check&add-lease=true") -- 2.45.2