Ejemplo n.º 1
0
 def test_patch_missing_attribute(self):
     fixture = MonkeyPatch(
         'fixtures.tests._fixtures.test_monkeypatch.new_attr', True)
     self.assertFalse('new_attr' in globals())
     fixture.setUp()
     try:
         self.assertEqual(True, new_attr)
     finally:
         fixture.cleanUp()
         self.assertFalse('new_attr' in globals())
Ejemplo n.º 2
0
 def test_patch_and_restore(self):
     fixture = MonkeyPatch(
         'fixtures.tests._fixtures.test_monkeypatch.reference', 45)
     self.assertEqual(23, reference)
     fixture.setUp()
     try:
         self.assertEqual(45, reference)
     finally:
         fixture.cleanUp()
         self.assertEqual(23, reference)
Ejemplo n.º 3
0
 def test_delete_missing_attribute(self):
     fixture = MonkeyPatch(
         'fixtures.tests._fixtures.test_monkeypatch.new_attr',
         MonkeyPatch.delete)
     self.assertFalse('new_attr' in globals())
     fixture.setUp()
     try:
         self.assertFalse('new_attr' in globals())
     finally:
         fixture.cleanUp()
         self.assertFalse('new_attr' in globals())
Ejemplo n.º 4
0
 def test_delete_existing_attribute(self):
     fixture = MonkeyPatch(
         'fixtures.tests._fixtures.test_monkeypatch.reference',
         MonkeyPatch.delete)
     self.assertEqual(23, reference)
     fixture.setUp()
     try:
         self.assertFalse('reference' in globals())
     finally:
         fixture.cleanUp()
         self.assertEqual(23, reference)
Ejemplo n.º 5
0
    def test_stat_shares_mutable(self, storage_index, secrets,
                                 test_and_write_vectors_for_shares, clock):
        """
        Size and lease information about mutable shares can be retrieved from a
        storage server.
        """
        # Hypothesis causes our storage server to be used many times.  Clean
        # up between iterations.
        cleanup_storage_server(self.anonymous_storage_server)

        # anonymous_storage_server uses time.time(), unfortunately.  And
        # useFixture does not interact very well with Hypothesis.
        patch = MonkeyPatch("time.time", clock.seconds)
        try:
            patch.setUp()
            # Create a share we can toy with.
            wrote, read = extract_result(
                self.client.slot_testv_and_readv_and_writev(
                    storage_index,
                    secrets=secrets,
                    tw_vectors={
                        k: v.for_call()
                        for (k,
                             v) in test_and_write_vectors_for_shares.items()
                    },
                    r_vector=[],
                ), )
        finally:
            patch.cleanUp()
        self.assertThat(
            wrote,
            Equals(True),
            u"Server rejected a write to a new mutable slot",
        )

        stats = extract_result(self.client.stat_shares([storage_index]), )
        expected = [{
            sharenum: ShareStat(
                size=get_implied_data_length(
                    vectors.write_vector,
                    vectors.new_length,
                ),
                lease_expiration=int(clock.seconds() + LEASE_INTERVAL),
            )
            for (sharenum,
                 vectors) in test_and_write_vectors_for_shares.items()
        }]
        self.assertThat(
            stats,
            Equals(expected),
        )
Ejemplo n.º 6
0
    def _stat_shares_immutable_test(self, storage_index, sharenum, size, clock,
                                    leases, write_shares):
        # Hypothesis causes our storage server to be used many times.  Clean
        # up between iterations.
        cleanup_storage_server(self.anonymous_storage_server)

        # anonymous_storage_server uses time.time(), unfortunately.  And
        # useFixture does not interact very well with Hypothesis.
        patch = MonkeyPatch("time.time", clock.seconds)
        try:
            patch.setUp()
            # Create a share we can toy with.
            write_shares(
                self.anonymous_storage_server,
                storage_index,
                {sharenum},
                size,
                canary=self.canary,
            )
            # Perhaps put some more leases on it.  Leases might impact our
            # ability to determine share data size.
            for renew_secret in leases:
                self.anonymous_storage_server.remote_add_lease(
                    storage_index,
                    renew_secret,
                    b"",
                )
        finally:
            patch.cleanUp()

        stats = extract_result(self.client.stat_shares([storage_index]), )
        expected = [{
            sharenum:
            ShareStat(
                size=size,
                lease_expiration=int(clock.seconds() + LEASE_INTERVAL),
            ),
        }]
        self.assertThat(
            stats,
            Equals(expected),
        )
Ejemplo n.º 7
0
    def test_mutable_rewrite_renews_expired_lease(
        self,
        storage_index,
        clock,
        sharenum,
        size,
        write_enabler,
        renew_secret,
        cancel_secret,
        test_and_write_vectors_for_shares,
    ):
        """
        When mutable share data with an expired lease is rewritten using
        *slot_testv_and_readv_and_writev* a new lease is paid for and granted.
        """
        # Hypothesis causes our storage server to be used many times.  Clean
        # up between iterations.
        cleanup_storage_server(self.anonymous_storage_server)

        # Make the client and server use our clock.
        self.server._clock = clock
        self.client._clock = clock

        secrets = (write_enabler, renew_secret, cancel_secret)

        def write():
            return self.client.slot_testv_and_readv_and_writev(
                storage_index,
                secrets=secrets,
                tw_vectors={
                    k: v.for_call()
                    for (k, v) in test_and_write_vectors_for_shares.items()
                },
                r_vector=[],
            )

        # anonymous_storage_server uses time.time() to assign leases,
        # unfortunately.
        patch = MonkeyPatch("time.time", clock.seconds)
        try:
            patch.setUp()

            # Create a share we can toy with.
            self.assertThat(write(), is_successful_write())

            # Advance time by more than a lease period so the lease is no
            # longer valid.
            clock.advance(self.server.LEASE_PERIOD.total_seconds() + 1)

            self.assertThat(write(), is_successful_write())
        finally:
            patch.cleanUp()

        # Not only should the write above succeed but the lease should now be
        # marked as expiring one additional lease period into the future.
        self.assertThat(
            self.server.remote_stat_shares([storage_index]),
            Equals([{
                num: ShareStat(
                    size=get_implied_data_length(
                        test_and_write_vectors_for_shares[num].write_vector,
                        test_and_write_vectors_for_shares[num].new_length,
                    ),
                    lease_expiration=int(
                        clock.seconds() +
                        self.server.LEASE_PERIOD.total_seconds()),
                )
                for num in test_and_write_vectors_for_shares
            }]),
        )