Skip to content

Commit

Permalink
Add try-except block to guard against NotFound exception from GCS del…
Browse files Browse the repository at this point in the history
…ete (#31603)

* Add try-except block to guard against NotFound exception from GCS delete.

* Skip test if the package cannot be imported.
  • Loading branch information
shunping authored Jun 15, 2024
1 parent b1c8040 commit 481c71a
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion sdks/python/apache_beam/io/gcp/gcsio_integration_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@
except ImportError:
gcsio = None # type: ignore

try:
from google.api_core.exceptions import NotFound
except ImportError:
NotFound = None


@unittest.skipIf(gcsio is None, 'GCP dependencies are not installed')
class GcsIOIntegrationTest(unittest.TestCase):
Expand Down Expand Up @@ -145,6 +150,7 @@ def test_batch_copy_and_delete(self):

@pytest.mark.it_postcommit
@mock.patch('apache_beam.io.gcp.gcsio.default_gcs_bucket_name')
@unittest.skipIf(NotFound is None, 'GCP dependencies are not installed')
def test_create_default_bucket(self, mock_default_gcs_bucket_name):
google_cloud_options = self.test_pipeline.options.view_as(
GoogleCloudOptions)
Expand All @@ -168,7 +174,12 @@ def test_create_default_bucket(self, mock_default_gcs_bucket_name):
# remove the existing bucket with the same name as the default bucket
existing_bucket = self.gcsio.get_bucket(overridden_bucket_name)
if existing_bucket:
existing_bucket.delete()
try:
existing_bucket.delete()
except NotFound:
# Bucket existence check from get_bucket may be inaccurate due to gcs
# cache or delay
pass

bucket = gcsio.get_or_create_default_gcs_bucket(google_cloud_options)
self.assertIsNotNone(bucket)
Expand Down

0 comments on commit 481c71a

Please sign in to comment.