From 838c1ba6c79a54f99c137e64f532e59fcb91abf4 Mon Sep 17 00:00:00 2001 From: Ryan Brown Date: Thu, 26 Oct 2017 08:17:13 -0400 Subject: [PATCH] [cloud] Bugfix for aws_s3 empty directory creation (#32169) * [cloud] Bugfix for aws_s3 empty directory creation * Update integration tests --- lib/ansible/modules/cloud/amazon/aws_s3.py | 6 +- test/integration/targets/aws_s3/aliases | 2 +- .../integration/targets/aws_s3/tasks/main.yml | 56 +++++++++++++++---- 3 files changed, 47 insertions(+), 17 deletions(-) diff --git a/lib/ansible/modules/cloud/amazon/aws_s3.py b/lib/ansible/modules/cloud/amazon/aws_s3.py index 2ed2002ee9..12b2199d9c 100644 --- a/lib/ansible/modules/cloud/amazon/aws_s3.py +++ b/lib/ansible/modules/cloud/amazon/aws_s3.py @@ -408,12 +408,10 @@ def create_dirkey(module, s3, bucket, obj): if module.check_mode: module.exit_json(msg="PUT operation skipped - running in check mode", changed=True) try: - bucket = s3.Bucket(bucket) - key = bucket.new_key(obj) - key.set_contents_from_string('') + s3.put_object(Bucket=bucket, Key=obj, Body=b'') for acl in module.params.get('permission'): s3.put_object_acl(ACL=acl, Bucket=bucket, Key=obj) - module.exit_json(msg="Virtual directory %s created in bucket %s" % (obj, bucket.name), changed=True) + module.exit_json(msg="Virtual directory %s created in bucket %s" % (obj, bucket), changed=True) except botocore.exceptions.ClientError as e: module.fail_json(msg="Failed while creating object %s." % obj, exception=traceback.format_exc(), **camel_dict_to_snake_dict(e.response)) diff --git a/test/integration/targets/aws_s3/aliases b/test/integration/targets/aws_s3/aliases index 495c6e74ed..ebdf4aa572 100644 --- a/test/integration/targets/aws_s3/aliases +++ b/test/integration/targets/aws_s3/aliases @@ -1,2 +1,2 @@ cloud/aws -posix/ci/cloud/aws +posix/ci/cloud/group1/aws diff --git a/test/integration/targets/aws_s3/tasks/main.yml b/test/integration/targets/aws_s3/tasks/main.yml index 7a3225a933..b984c44371 100644 --- a/test/integration/targets/aws_s3/tasks/main.yml +++ b/test/integration/targets/aws_s3/tasks/main.yml @@ -2,7 +2,7 @@ # tasks file for test_s3 # ============================================================ - name: test create bucket - s3: + aws_s3: bucket: "{{ bucket_name }}" mode: create aws_access_key: "{{ ec2_access_key }}" @@ -15,7 +15,7 @@ - result.changed == True # ============================================================ - name: trying to create a bucket name that already exists - s3: + aws_s3: bucket: "{{ bucket_name }}" mode: create aws_access_key: "{{ ec2_access_key }}" @@ -45,7 +45,7 @@ register: file1stat # ============================================================ - name: test putting an object in the bucket - s3: + aws_s3: bucket: "{{ bucket_name }}" mode: put src: "{{ tmp1.path }}" @@ -66,7 +66,7 @@ tempfile: register: tmp2 - name: test get object - s3: + aws_s3: bucket: "{{ bucket_name }}" mode: get dest: "{{ tmp2.path }}" @@ -89,7 +89,7 @@ - file1stat.stat.checksum == file2stat.stat.checksum # ============================================================ - name: test geturl of the object - s3: + aws_s3: bucket: "{{ bucket_name }}" mode: geturl object: delete.txt @@ -107,7 +107,7 @@ - result.changed == True # ============================================================ - name: test getstr of the object - s3: + aws_s3: bucket: "{{ bucket_name }}" mode: getstr object: delete.txt @@ -124,7 +124,7 @@ - result.contents == content # ============================================================ - name: test list to get all objects in the bucket - s3: + aws_s3: bucket: "{{ bucket_name }}" mode: list security_token: "{{security_token}}" @@ -140,7 +140,7 @@ - result.msg == "LIST operation complete" # ============================================================ - name: test delobj to just delete an object in the bucket - s3: + aws_s3: bucket: "{{ bucket_name }}" mode: delobj object: delete.txt @@ -155,9 +155,41 @@ that: - "'Object deleted from bucket' in result.msg" - result.changed == True +- name: assert that delete.txt is no longer an object in the bucket deleteme + assert: + that: + - "'Object deleted from bucket' in result.msg" + - result.changed == True +# ============================================================ +- name: test creation of empty path + aws_s3: + bucket: "{{ bucket_name }}" + mode: create + object: foo/bar/baz/ + security_token: "{{ security_token }}" + aws_access_key: "{{ ec2_access_key }}" + aws_secret_key: "{{ ec2_secret_key }}" + retries: 3 + delay: 3 + register: result +- name: assert that empty path is created + assert: + that: + - "'Virtual directory foo/bar/baz/ created' in result.msg" + - result.changed == True +- name: test deletion of empty path + aws_s3: + bucket: "{{ bucket_name }}" + mode: delobj + object: foo/bar/baz/ + security_token: "{{ security_token }}" + aws_access_key: "{{ ec2_access_key }}" + aws_secret_key: "{{ ec2_secret_key }}" + retries: 3 + delay: 3 # ============================================================ - name: test delete bucket - s3: + aws_s3: bucket: "{{ bucket_name }}" mode: delete security_token: "{{security_token}}" @@ -182,7 +214,7 @@ path: "{{ tmp2.path }}" # ============================================================ - name: test create a bucket with a dot in the name - s3: + aws_s3: bucket: "{{ bucket_name + '.bucket' }}" mode: create security_token: "{{security_token}}" @@ -195,7 +227,7 @@ - result.changed == True # ============================================================ - name: test delete a bucket with a dot in the name - s3: + aws_s3: bucket: "{{ bucket_name + '.bucket' }}" mode: delete security_token: "{{security_token}}" @@ -208,7 +240,7 @@ - result.changed == True # ============================================================ - name: test delete a nonexistent bucket - s3: + aws_s3: bucket: "{{ bucket_name + '.bucket' }}" mode: delete security_token: "{{security_token}}"