Skip to content

Commit

Permalink
Tests for mutable leases.
Browse files Browse the repository at this point in the history
  • Loading branch information
itamarst committed Oct 19, 2021
1 parent 4b8b605 commit 2a5dbcb
Showing 1 changed file with 121 additions and 11 deletions.
132 changes: 121 additions & 11 deletions src/allmydata/test/test_istorageserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,9 @@ def test_allocate_buckets_creates_lease(self):
storage_index, _, _ = yield self.create_share()
[lease] = self.server.get_leases(storage_index)
# Lease expires in 31 days.
assert lease.get_expiration_time() - self.fake_time() > (31 * 24 * 60 * 60 - 10)
self.assertTrue(
lease.get_expiration_time() - self.fake_time() > (31 * 24 * 60 * 60 - 10)
)

@inlineCallbacks
def test_add_lease_renewal(self):
Expand Down Expand Up @@ -857,34 +859,142 @@ def test_slot_readv_no_shares(self):
{0: [b"abcdefg"], 1: [b"0123456"], 2: [b"9876543"]},
)

@inlineCallbacks
def create_slot(self):
"""Create a slot with sharenum 0."""
secrets = self.new_secrets()
storage_index = new_storage_index()
(written, _) = yield self.staraw(
storage_index,
secrets,
tw_vectors={
0: ([], [(0, b"abcdefg")], 7),
},
r_vector=[],
)
self.assertEqual(written, True)
returnValue((secrets, storage_index))

@inlineCallbacks
def test_advise_corrupt_share(self):
"""
Calling ``advise_corrupt_share()`` on a mutable share does not
result in error (other behavior is opaque at this level of
abstraction).
"""
secrets = self.new_secrets()
storage_index = new_storage_index()
secrets, storage_index = yield self.create_slot()

yield self.storage_client.advise_corrupt_share(
b"mutable", storage_index, 0, b"ono"
)

@inlineCallbacks
def test_STARAW_create_lease(self):
"""
When STARAW creates a new slot, it also creates a lease.
"""
_, storage_index = yield self.create_slot()
[lease] = self.server.get_slot_leases(storage_index)
# Lease expires in 31 days.
self.assertTrue(
lease.get_expiration_time() - self.fake_time() > (31 * 24 * 60 * 60 - 10)
)

@inlineCallbacks
def test_STARAW_renews_lease(self):
"""
When STARAW is run on an existing slot with same renewal secret, it
renews the lease.
"""
secrets, storage_index = yield self.create_slot()
[lease] = self.server.get_slot_leases(storage_index)
initial_expire = lease.get_expiration_time()

# Time passes...
self.fake_sleep(17)

# We do another write:
(written, _) = yield self.staraw(
storage_index,
secrets,
tw_vectors={
0: ([], [(0, b"abcdefg")], 7),
0: ([], [(0, b"1234567")], 7),
},
r_vector=[],
)
self.assertEqual(written, True)

yield self.storage_client.advise_corrupt_share(
b"mutable", storage_index, 0, b"ono"
# The lease has been renewed:
[lease] = self.server.get_slot_leases(storage_index)
self.assertEqual(lease.get_expiration_time() - initial_expire, 17)

@inlineCallbacks
def test_STARAW_new_lease(self):
"""
When STARAW is run with a new renewal secret on an existing slot, it
adds a new lease.
"""
secrets, storage_index = yield self.create_slot()
[lease] = self.server.get_slot_leases(storage_index)
initial_expire = lease.get_expiration_time()

# Time passes...
self.fake_sleep(19)

# We do another write:
(written, _) = yield self.staraw(
storage_index,
(secrets[0], new_secret(), new_secret()),
tw_vectors={
0: ([], [(0, b"1234567")], 7),
},
r_vector=[],
)
self.assertEqual(written, True)

# A new lease was added:
[lease1, lease2] = self.server.get_slot_leases(storage_index)
self.assertEqual(lease1.get_expiration_time(), initial_expire)
self.assertEqual(lease2.get_expiration_time() - initial_expire, 19)

@inlineCallbacks
def test_add_lease_renewal(self):
"""
If the lease secret is reused, ``add_lease()`` extends the existing
lease.
"""
secrets, storage_index = yield self.create_slot()
[lease] = self.server.get_slot_leases(storage_index)
initial_expiration_time = lease.get_expiration_time()

# TODO STARAW creates lease for new data
# TODO STARAW renews lease if same secret is used on existing data
# TODO STARAW creates new lease for existing data if new secret is given
# TODO add_lease renews lease if existing storage index and secret
# TODO add_lease creates new lease if new secret
# Time passes:
self.fake_sleep(178)

# We renew the lease:
yield self.storage_client.add_lease(storage_index, secrets[1], secrets[2])
[lease] = self.server.get_slot_leases(storage_index)
new_expiration_time = lease.get_expiration_time()
self.assertEqual(new_expiration_time - initial_expiration_time, 178)

@inlineCallbacks
def test_add_new_lease(self):
"""
If a new lease secret is used, ``add_lease()`` creates a new lease.
"""
secrets, storage_index = yield self.create_slot()
[lease] = self.server.get_slot_leases(storage_index)
initial_expiration_time = lease.get_expiration_time()

# Time passes:
self.fake_sleep(167)

# We create a new lease:
renew_secret = new_secret()
cancel_secret = new_secret()
yield self.storage_client.add_lease(storage_index, renew_secret, cancel_secret)
[lease1, lease2] = self.server.get_slot_leases(storage_index)
self.assertEqual(lease1.get_expiration_time(), initial_expiration_time)
self.assertEqual(lease2.get_expiration_time() - initial_expiration_time, 167)


class _FoolscapMixin(SystemTestMixin):
Expand Down

0 comments on commit 2a5dbcb

Please sign in to comment.