unarchive: handle content and size differences (#83454)

Consider content differs and size differs while unarchiving
the same tar.gz file

Fixes: #29610

Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>
pull/83456/head
Abhijeet Kasurde 5 months ago committed by GitHub
parent c2c6005842
commit c93af4e148
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,4 @@
---
bugfixes:
- unarchive - trigger change when size and content differ when other
properties are unchanged (https://github.com/ansible/ansible/pull/83454).

@ -275,6 +275,8 @@ ZIP_FILE_MODE_RE = re.compile(r'([r-][w-][SsTtx-]){3}')
INVALID_OWNER_RE = re.compile(r': Invalid owner')
INVALID_GROUP_RE = re.compile(r': Invalid group')
SYMLINK_DIFF_RE = re.compile(r': Symlink differs$')
CONTENT_DIFF_RE = re.compile(r': Contents differ$')
SIZE_DIFF_RE = re.compile(r': Size differs$')
def crc32(path, buffer_size):
@ -891,16 +893,15 @@ class TgzArchive(object):
out += line + '\n'
if not self.file_args['mode'] and MODE_DIFF_RE.search(line):
out += line + '\n'
if MOD_TIME_DIFF_RE.search(line):
out += line + '\n'
if MISSING_FILE_RE.search(line):
out += line + '\n'
if INVALID_OWNER_RE.search(line):
out += line + '\n'
if INVALID_GROUP_RE.search(line):
out += line + '\n'
if SYMLINK_DIFF_RE.search(line):
out += line + '\n'
differ_regexes = [
MOD_TIME_DIFF_RE, MISSING_FILE_RE, INVALID_OWNER_RE,
INVALID_GROUP_RE, SYMLINK_DIFF_RE, CONTENT_DIFF_RE,
SIZE_DIFF_RE
]
for regex in differ_regexes:
if regex.search(line):
out += line + '\n'
if out:
unarchived = False
return dict(unarchived=unarchived, rc=rc, out=out, err=err, cmd=cmd)

@ -5,6 +5,8 @@
- import_tasks: test_tar_gz_creates.yml
- import_tasks: test_tar_gz_owner_group.yml
- import_tasks: test_tar_gz_keep_newer.yml
- import_tasks: test_tar_gz_size_differs.yml
- import_tasks: test_tar_gz_content_differs.yml
- import_tasks: test_tar_zst.yml
- import_tasks: test_zip.yml
- import_tasks: test_exclude.yml

@ -0,0 +1,56 @@
- set_fact:
dest: '{{remote_tmp_dir}}/test-unarchive-tar-gz-content-differs'
- name: create our tar.gz unarchive destination
file:
path: "{{ dest }}"
state: directory
- name: unarchive a tar.gz file
unarchive:
src: 'content_differs.tar.gz'
dest: '{{ dest }}'
remote_src: no
register: unarchive_content_differs_01
- name: verify that the task was marked as changed
assert:
that:
- unarchive_content_differs_01.changed
- name: checksum the file after first unarchive
stat:
path: '{{ dest }}/hello.txt'
checksum_algorithm: sha256
get_checksum: yes
register: first_unarchive_content
- name: unarchive a tar.gz file with same name, size but different content
unarchive:
src: 'content_differs_2.tar.gz'
dest: '{{ dest }}'
remote_src: no
register: unarchive_content_differs_01
- name: verify that the task was marked as changed
assert:
that:
- unarchive_content_differs_01.changed
- name: checksum the file after second unarchive
stat:
path: '{{ dest }}/hello.txt'
checksum_algorithm: sha256
get_checksum: yes
register: second_unarchive_content
- name: verify that unarchive extracted file with new content
assert:
that:
- first_unarchive_content.stat.checksum != second_unarchive_content.stat.checksum
- first_unarchive_content.stat.size == second_unarchive_content.stat.size
- name: remove our tar.gz unarchive destination
file:
path: '{{ dest }}'
state: absent

@ -0,0 +1,52 @@
- set_fact:
dest: '{{remote_tmp_dir}}/test-unarchive-tar-gz-size-differs'
- name: create our tar.gz unarchive destination
file:
path: "{{ dest }}"
state: directory
- name: unarchive a tar.gz file
unarchive:
src: 'size_differs.tar.gz'
dest: '{{ dest }}'
remote_src: no
register: unarchive_size_differs_01
- name: verify that the task was marked as changed
assert:
that:
- unarchive_size_differs_01.changed
- name: Check size after first unarchive
stat:
path: '{{ dest }}/hello.txt'
register: first_unarchive
- name: unarchive a tar.gz file with same name but different size
unarchive:
src: 'size_differs_2.tar.gz'
dest: '{{ dest }}'
remote_src: no
register: unarchive_size_differs_02
- name: verify that the task was marked as changed
assert:
that:
- unarchive_size_differs_02.changed
- name: Check size after unarchive
stat:
path: '{{ dest }}/hello.txt'
register: second_unarchive
- name: verify that unarchive extracted new sized file
assert:
that:
- first_unarchive.stat.size != second_unarchive.stat.size
- first_unarchive.stat.size < second_unarchive.stat.size
- name: remove our tar.gz unarchive destination
file:
path: '{{ dest }}'
state: absent
Loading…
Cancel
Save