From 553e3269bc0aad74c706efdddb311f9840719425 Mon Sep 17 00:00:00 2001
From: Jean-Paul Calderone <exarkun@twistedmatrix.com>
Date: Wed, 15 Sep 2021 14:24:40 -0400
Subject: [PATCH] Make sure that test_renewed is exercising the interesting
 code path

Previously Hypothesis spent a long while putting storage indexes on the
storage servers and creating a list of nodes with non-overlapping storage
indexes and then asserting that, out of no relevant shares, none had expired
leases.

Now all storage servers in the test will have all of the relevant shares and
so all should be visited and updated.
---
 src/_zkapauthorizer/tests/strategies.py       |  2 +-
 .../tests/test_lease_maintenance.py           | 47 ++++++++++++++++++-
 2 files changed, 47 insertions(+), 2 deletions(-)

diff --git a/src/_zkapauthorizer/tests/strategies.py b/src/_zkapauthorizer/tests/strategies.py
index 2ba763a..70ec582 100644
--- a/src/_zkapauthorizer/tests/strategies.py
+++ b/src/_zkapauthorizer/tests/strategies.py
@@ -827,7 +827,7 @@ def clocks(now=posix_safe_datetimes()):
 
 
 @implementer(IFilesystemNode)
-@attr.s
+@attr.s(frozen=True)
 class _LeafNode(object):
     _storage_index = attr.ib()
 
diff --git a/src/_zkapauthorizer/tests/test_lease_maintenance.py b/src/_zkapauthorizer/tests/test_lease_maintenance.py
index 4e0143c..48d6903 100644
--- a/src/_zkapauthorizer/tests/test_lease_maintenance.py
+++ b/src/_zkapauthorizer/tests/test_lease_maintenance.py
@@ -166,6 +166,35 @@ class DummyStorageServer(object):
         )
 
 
+class SharesAlreadyExist(Exception):
+    pass
+
+
+def create_shares(storage_server, storage_index, size, lease_expiration):
+    """
+    Initialize a storage index ("bucket") with some shares.
+
+    :param DummyServer storage_server: The server to populate with shares.
+    :param bytes storage_index: The storage index of the shares.
+    :param int size: The application data size of the shares.
+    :param int lease_expiration: The expiration time for the lease to attach
+        to the shares.
+
+    :raise SharesAlreadyExist: If there are already shares at the given
+        storage index.
+
+    :return: ``None``
+    """
+    if storage_index in storage_server.buckets:
+        raise SharesAlready(
+            "Cannot create shares for storage index where they already exist.",
+        )
+    storage_server.buckets[storage_index] = ShareStat(
+        size=size,
+        lease_expiration=lease_expiration,
+    )
+
+
 def lease_seeds():
     return binary(
         min_size=20,
@@ -439,7 +468,7 @@ class RenewLeasesTests(TestCase):
     """
     Tests for ``renew_leases``.
     """
-    @given(storage_brokers(clocks()), lists(leaf_nodes()))
+    @given(storage_brokers(clocks()), lists(leaf_nodes(), unique=True))
     def test_renewed(self, storage_broker, nodes):
         """
         ``renew_leases`` renews the leases of shares on all storage servers which
@@ -451,6 +480,22 @@ class RenewLeasesTests(TestCase):
         secret_holder = SecretHolder(lease_secret, convergence_secret)
         min_lease_remaining = timedelta(days=3)
 
+        # Make sure that the storage brokers have shares at the storage
+        # indexes we're going to operate on.
+        for storage_server in storage_broker.get_connected_servers():
+            for node in nodes:
+                try:
+                    create_shares(
+                        storage_server.get_storage_server(),
+                        node.get_storage_index(),
+                        size=123,
+                        lease_expiration=int(storage_broker.clock.seconds()),
+                    )
+                except SharesAlreadyExist:
+                    # If Hypothesis already put some shares in this storage
+                    # index, that's okay too.
+                    pass
+
         def get_now():
             return datetime.utcfromtimestamp(
                 storage_broker.clock.seconds(),
-- 
GitLab