XD-DENG commented on a change in pull request #3823: [AIRFLOW-2985] Operators 
for S3 object copying/deleting
URL: https://github.com/apache/incubator-airflow/pull/3823#discussion_r215848223
 
 

 ##########
 File path: tests/contrib/operators/test_s3_delete_objects_operator.py
 ##########
 @@ -0,0 +1,85 @@
+# -*- coding: utf-8 -*-
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+import io
+import unittest
+
+import boto3
+from moto import mock_s3
+
+from airflow.contrib.operators.s3_delete_objects_operator import 
S3DeleteObjectsOperator
+
+
+class TestS3DeleteObjectsOperator(unittest.TestCase):
+
+    @mock_s3
+    def test_s3_delete_single_object(self):
+        bucket = "testbucket"
+        key = "path/data.txt"
+
+        conn = boto3.client('s3')
+        conn.create_bucket(Bucket=bucket)
+        conn.upload_fileobj(Bucket=bucket,
+                            Key=key,
+                            Fileobj=io.BytesIO(b"input"))
+
+        # The object should be detected before the DELETE action is taken
+        objects_in_dest_bucket = conn.list_objects(Bucket=bucket,
+                                                   Prefix=key)
+        self.assertEqual(len(objects_in_dest_bucket['Contents']), 1)
+        self.assertEqual(objects_in_dest_bucket['Contents'][0]['Key'], key)
+
+        t = 
S3DeleteObjectsOperator(task_id="test_task_s3_delete_single_object",
+                                    bucket=bucket,
+                                    keys=key)
+        t.execute(None)
+
+        # There should be no object found in the bucket created earlier
+        self.assertFalse('Contents' in conn.list_objects(Bucket=bucket,
+                                                         Prefix=key))
+
+    @mock_s3
+    def test_s3_delete_multiple_objects(self):
+        bucket = "testbucket"
+        key_pattern = "path/data"
+        n_keys = 3
+        keys = [key_pattern + str(i) for i in range(n_keys)]
+
+        conn = boto3.client('s3')
+        conn.create_bucket(Bucket=bucket)
+        for k in keys:
+            conn.upload_fileobj(Bucket=bucket,
+                                Key=k,
+                                Fileobj=io.BytesIO(b"input"))
+
+        # The objects should be detected before the DELETE action is taken
+        objects_in_dest_bucket = conn.list_objects(Bucket=bucket,
+                                                   Prefix=key_pattern)
+        self.assertEqual(len(objects_in_dest_bucket['Contents']), n_keys)
+        self.assertEqual(sorted([x['Key'] for x in 
objects_in_dest_bucket['Contents']]),
+                         sorted(keys))
+
+        t = 
S3DeleteObjectsOperator(task_id="test_task_s3_delete_multiple_objects",
+                                    bucket=bucket,
+                                    keys=keys)
+        t.execute(None)
+
+        # There should be no object found in the bucket created earlier
+        self.assertFalse('Contents' in conn.list_objects(Bucket=bucket,
+                                                         Prefix=key_pattern))
 
 Review comment:
   I deleted test case `test_s3_delete_non_existent_object`. It can pass CI, 
but I realize it can only be reproduced by `moto.mock_s3`.
   
   That is, when we use `moto.mock_s3` to mirror the S3 environment, deleting 
an non-existent object would result in an `Errors` entry in the response. But 
in AWS S3 and IBM COS (S3-compatible), deleting non-existent object is allowed, 
and the non-existent key would still be in the `Deleted` entry.
   
   **Regarding `S3DeleteObjectsOperator()`, if there is `Errors` entry in the 
response, the operator will raise exception and fail, as you advised earlier.**
   
   **Reference**:
   
https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/s3.html#S3.Client.delete_objects
   Response Syntax
   ```
   {
       'Deleted': [
           {
               'Key': 'string',
               'VersionId': 'string',
               'DeleteMarker': True|False,
               'DeleteMarkerVersionId': 'string'
           },
       ],
       'RequestCharged': 'requester',
       'Errors': [
           {
               'Key': 'string',
               'VersionId': 'string',
               'Code': 'string',
               'Message': 'string'
           },
       ]
   }
   ```

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to