Skip to content

Storage: assigning 'Bucket.retention_policy' to None fails in systests #6368

Closed

Description

@tseaver

See the non-flaky-looking failures today:

__________ TestRetentionPolicy.test_bucket_w_retention_period ______________

 = <tests.system.TestRetentionPolicy testMethod=test_bucket_w_retention_period>

def test_bucket_w_retention_period(self):
    import datetime
    from google.api_core import exceptions

    period_secs = 10

    new_bucket_name = 'w-retention-period' + unique_resource_id('-')
    bucket = Config.CLIENT.create_bucket(new_bucket_name)
    self.case_buckets_to_delete.append(new_bucket_name)

    bucket.retention_period = period_secs
    bucket.default_event_based_hold = False
    bucket.patch()

    self.assertEqual(bucket.retention_period, period_secs)
    self.assertIsInstance(
        bucket.retention_policy_effective_time, datetime.datetime)
    self.assertFalse(bucket.default_event_based_hold)
    self.assertFalse(bucket.retention_policy_locked)

    blob_name = 'test-blob'
    payload = b'DEADBEEF'
    blob = bucket.blob(blob_name)
    blob.upload_from_string(payload)

    other = bucket.get_blob(blob_name)

    self.assertFalse(other.event_based_hold)
    self.assertFalse(other.temporary_hold)
    self.assertIsInstance(
        other.retention_expiration_time, datetime.datetime)

    with self.assertRaises(exceptions.Forbidden):
        other.delete()

    bucket.retention_period = None
    bucket.patch()
...

        BadRequest: 400 PATCH https://www.googleapis.com/storage/v1/b/w-retention-period-1541108415593?projection=full: Retention policy must have a retention period greater than 0 and less than 100 years.

@frankyn Has something changed on the back-end about this case? These tests have been passing.

Activity

  1. added
    type: bug
    api: storage
    priority: p2
    on Nov 1, 2018
  2. frankyn commented on Nov 2, 2018

    @frankyn
    Contributor

    This is strange, I'm researching, from a brief inspection it is as if it no longer accepts the None value in Python.

  3. frankyn commented on Nov 2, 2018

    @frankyn
    Contributor

    I think it changed based on it no longer accepts None when a Retention policy is defined. It used to accept it. I will follow-up on such subtle changes in the backend.

    As for the fix, I missed this during the review, because I have learned more!
    From https://github.com/googleapis/google-cloud-python/blob/master/storage/google/cloud/storage/bucket.py#L1348-L1352
    Change to:

        @retention_period.setter
        def retention_period(self, value):
            """Set the retention period for items in the bucket.
            :type value: int
            :param value:
                number of seconds to retain items after upload or release from
                event-based lock.
            :raises ValueError: if the bucket's retention policy is locked.
            """
            policy = self._properties.setdefault('retentionPolicy', {})
            if value is not None:
                policy['retentionPeriod'] = str(value)
            else:
                policy = None
            self._patch_property('retentionPolicy', policy)
  4. frankyn commented on Nov 2, 2018

    @frankyn
    Contributor

    Internal tracking bug: 115765214

  5. assigned and unassigned on Nov 2, 2018
  6. added a commit that references this issue on Nov 5, 2018
    062235c
  7. frankyn commented on Nov 5, 2018

    @frankyn
    Contributor

    Short update: turns out it was a bug in the feature that has since been fixed. The client shouldn't have depended on this route in the first place. I missed this during the review.

    Thanks @tseaver for the fast fix!

  8. added a commit that references this issue on Mar 9, 2026
    10ca0a1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

api: storagebackendpriority: p2type: bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions