From d12847a02f55c3ebc1fb4acc993d902f3a1e6a7f Mon Sep 17 00:00:00 2001
From: abikouo <abikouo@redhat.com>
Date: Fri, 7 May 2021 10:21:24 +0200
Subject: [PATCH] delete all buckets

---
 .../targets/aws_s3/tasks/copy_object.yml      | 22 ++------------
 .../targets/aws_s3/tasks/delete_bucket.yml    | 24 +++++++++++++++
 .../integration/targets/aws_s3/tasks/main.yml | 29 +++----------------
 3 files changed, 30 insertions(+), 45 deletions(-)
 create mode 100644 tests/integration/targets/aws_s3/tasks/delete_bucket.yml

diff --git a/tests/integration/targets/aws_s3/tasks/copy_object.yml b/tests/integration/targets/aws_s3/tasks/copy_object.yml
index cf6d2a2cc92..b60572ff03d 100644
--- a/tests/integration/targets/aws_s3/tasks/copy_object.yml
+++ b/tests/integration/targets/aws_s3/tasks/copy_object.yml
@@ -68,25 +68,7 @@
         - "copy_idempotency.msg == 'ETag from source and destination are the same'"
 
   always:
-    - name: remove uploaded files
-      aws_s3:
-        bucket: "{{ copy_bucket.dst }}"
-        mode: delobj
-        object: destination.txt
-      ignore_errors: yes
-
-    - name: remove uploaded files
-      aws_s3:
-        bucket: "{{ copy_bucket.src }}"
-        mode: delobj
-        object: source.txt
-      ignore_errors: yes
-
-    - name: delete bucket created
-      aws_s3:
-        bucket: "{{ item }}"
-        mode: delete
+    - include_tasks: delete_bucket.yml
       with_items:
-        - "{{ copy_bucket.src }}"
         - "{{ copy_bucket.dst }}"
-      ignore_errors: yes
\ No newline at end of file
+        - "{{ copy_bucket.src }}"
diff --git a/tests/integration/targets/aws_s3/tasks/delete_bucket.yml b/tests/integration/targets/aws_s3/tasks/delete_bucket.yml
new file mode 100644
index 00000000000..bb6bca52ff8
--- /dev/null
+++ b/tests/integration/targets/aws_s3/tasks/delete_bucket.yml
@@ -0,0 +1,24 @@
+- name: delete bucket at the end of Integration tests
+  block:
+    - name: list bucket object
+      aws_s3:
+        bucket: "{{ item }}"
+        mode: list
+      register: objects
+      ignore_errors: true
+
+    - name: remove objects from bucket
+      aws_s3:
+        bucket: "{{ item }}"
+        mode: delobj
+        object: "{{ obj }}"
+      with_items: "{{ objects.s3_keys }}"
+      loop_control:
+        loop_var: obj      
+      ignore_errors: true
+
+    - name: delete the bucket
+      aws_s3:
+        bucket: "{{ item }}"
+        mode: delete
+      ignore_errors: yes
diff --git a/tests/integration/targets/aws_s3/tasks/main.yml b/tests/integration/targets/aws_s3/tasks/main.yml
index aa7ad6cfd90..e47ad29e52c 100644
--- a/tests/integration/targets/aws_s3/tasks/main.yml
+++ b/tests/integration/targets/aws_s3/tasks/main.yml
@@ -642,20 +642,6 @@
     - include_tasks: copy_object.yml
 
   always:
-    - name: remove uploaded files
-      aws_s3:
-        bucket: "{{ bucket_name }}"
-        mode: delobj
-        object: "{{ item }}"
-      loop:
-        - hello.txt
-        - delete.txt
-        - delete_encrypt.txt
-        - delete_encrypt_kms.txt
-        - put-content.txt
-        - put-template.txt
-        - put-binary.txt
-      ignore_errors: yes
 
     - name: delete temporary files
       file:
@@ -663,14 +649,7 @@
         path: "{{ tmpdir.path }}"
       ignore_errors: yes
 
-    - name: delete the bucket
-      aws_s3:
-        bucket: "{{ bucket_name }}"
-        mode: delete
-      ignore_errors: yes
-
-    - name: delete the dot bucket
-      aws_s3:
-        bucket: "{{ bucket_name | hash('md5') + '.bucket' }}"
-        mode: delete
-      ignore_errors: yes
+    - include_tasks: delete_bucket.yml
+      with_items:
+        - "{{ bucket_name }}"
+        - "{{ bucket_name | hash('md5') + '.bucket' }}"