Skip to content

Commit

Permalink
fix: replace python lifecycle action parsing ValueError with warning (#…
Browse files Browse the repository at this point in the history
…437)

* fix: replace python lifecycle action parsing ValueError with warning

* fix lint

* add client upgrade suggestion to unknown OLM rule warning

* update warning message
  • Loading branch information
cojenco committed May 8, 2021
1 parent 246a13b commit 2532d50
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
8 changes: 7 additions & 1 deletion google/cloud/storage/bucket.py
Expand Up @@ -2361,7 +2361,13 @@ def lifecycle_rules(self):
elif action_type == "SetStorageClass":
yield LifecycleRuleSetStorageClass.from_api_repr(rule)
else:
raise ValueError("Unknown lifecycle rule: {}".format(rule))
warnings.warn(
"Unknown lifecycle rule type received: {}. Please upgrade to the latest version of google-cloud-storage.".format(
rule
),
UserWarning,
stacklevel=1,
)

@lifecycle_rules.setter
def lifecycle_rules(self, rules):
Expand Down
13 changes: 10 additions & 3 deletions tests/unit/test_bucket.py
Expand Up @@ -1783,15 +1783,22 @@ def test_iam_configuration_policy_w_entry(self):
self.assertTrue(config.uniform_bucket_level_access_enabled)
self.assertEqual(config.uniform_bucket_level_access_locked_time, now)

def test_lifecycle_rules_getter_unknown_action_type(self):
@mock.patch("warnings.warn")
def test_lifecycle_rules_getter_unknown_action_type(self, mock_warn):
NAME = "name"
BOGUS_RULE = {"action": {"type": "Bogus"}, "condition": {"age": 42}}
rules = [BOGUS_RULE]
properties = {"lifecycle": {"rule": rules}}
bucket = self._make_one(name=NAME, properties=properties)

with self.assertRaises(ValueError):
list(bucket.lifecycle_rules)
list(bucket.lifecycle_rules)
mock_warn.assert_called_with(
"Unknown lifecycle rule type received: {}. Please upgrade to the latest version of google-cloud-storage.".format(
BOGUS_RULE
),
UserWarning,
stacklevel=1,
)

def test_lifecycle_rules_getter(self):
from google.cloud.storage.bucket import (
Expand Down

0 comments on commit 2532d50

Please sign in to comment.