Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get_url: Handle same SHA sum for downloaded files #71435

Merged
merged 1 commit into from Sep 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelogs/fragments/71420_get_url.yml
@@ -0,0 +1,2 @@
minor_changes:
- get_url - handle same SHA sum for checksum file (https://github.com/ansible/ansible/issues/71420).
6 changes: 3 additions & 3 deletions lib/ansible/modules/get_url.py
Expand Up @@ -502,16 +502,16 @@ def main():
with open(checksum_tmpsrc) as f:
lines = [line.rstrip('\n') for line in f]
os.remove(checksum_tmpsrc)
checksum_map = {}
checksum_map = []
for line in lines:
parts = line.split(None, 1)
if len(parts) == 2:
checksum_map[parts[0]] = parts[1]
checksum_map.append((parts[0], parts[1]))
filename = url_filename(url)

# Look through each line in the checksum file for a hash corresponding to
# the filename in the url, returning the first hash that is found.
for cksum in (s for (s, f) in checksum_map.items() if f.strip('./') == filename):
for cksum in (s for (s, f) in checksum_map if f.strip('./') == filename):
checksum = cksum
break
else:
Expand Down
60 changes: 60 additions & 0 deletions test/integration/targets/get_url/tasks/main.yml
Expand Up @@ -335,11 +335,17 @@
dest: '{{ files_dir }}/27617.txt'
content: "ptux"

- name: create duplicate src file
copy:
dest: '{{ files_dir }}/71420.txt'
content: "ptux"

- name: create sha1 checksum file of src
copy:
dest: '{{ files_dir }}/sha1sum.txt'
content: |
a97e6837f60cec6da4491bab387296bbcd72bdba 27617.txt
a97e6837f60cec6da4491bab387296bbcd72bdba 71420.txt
3911340502960ca33aece01129234460bfeb2791 not_target1.txt
1b4b6adf30992cedb0f6edefd6478ff0a593b2e4 not_target2.txt

Expand All @@ -348,6 +354,7 @@
dest: '{{ files_dir }}/sha256sum.txt'
content: |
b1b6ce5073c8fac263a8fc5edfffdbd5dec1980c784e09c5bc69f8fb6056f006. 27617.txt
b1b6ce5073c8fac263a8fc5edfffdbd5dec1980c784e09c5bc69f8fb6056f006. 71420.txt
30949cc401e30ac494d695ab8764a9f76aae17c5d73c67f65e9b558f47eff892 not_target1.txt
d0dbfc1945bc83bf6606b770e442035f2c4e15c886ee0c22fb3901ba19900b5b not_target2.txt

Expand All @@ -356,6 +363,7 @@
dest: '{{ files_dir }}/sha256sum_with_dot.txt'
content: |
b1b6ce5073c8fac263a8fc5edfffdbd5dec1980c784e09c5bc69f8fb6056f006. ./27617.txt
b1b6ce5073c8fac263a8fc5edfffdbd5dec1980c784e09c5bc69f8fb6056f006. ./71420.txt
30949cc401e30ac494d695ab8764a9f76aae17c5d73c67f65e9b558f47eff892 ./not_target1.txt
d0dbfc1945bc83bf6606b770e442035f2c4e15c886ee0c22fb3901ba19900b5b ./not_target2.txt

Expand Down Expand Up @@ -418,6 +426,50 @@
path: "{{ remote_tmp_dir }}/27617sha256_with_dot.txt"
register: stat_result_sha256_with_file_scheme

- name: download 71420.txt with sha1 checksum url
get_url:
url: 'http://localhost:{{ http_port }}/71420.txt'
dest: '{{ remote_tmp_dir }}'
checksum: 'sha1:http://localhost:{{ http_port }}/sha1sum.txt'
register: result_sha1_71420

- stat:
path: "{{ remote_tmp_dir }}/71420.txt"
register: stat_result_sha1_71420

- name: download 71420.txt with sha256 checksum url
get_url:
url: 'http://localhost:{{ http_port }}/71420.txt'
dest: '{{ remote_tmp_dir }}/71420sha256.txt'
checksum: 'sha256:http://localhost:{{ http_port }}/sha256sum.txt'
register: result_sha256_71420

- stat:
path: "{{ remote_tmp_dir }}/71420.txt"
register: stat_result_sha256_71420

- name: download 71420.txt with sha256 checksum url with dot leading paths
get_url:
url: 'http://localhost:{{ http_port }}/71420.txt'
dest: '{{ remote_tmp_dir }}/71420sha256_with_dot.txt'
checksum: 'sha256:http://localhost:{{ http_port }}/sha256sum_with_dot.txt'
register: result_sha256_with_dot_71420

- stat:
path: "{{ remote_tmp_dir }}/71420sha256_with_dot.txt"
register: stat_result_sha256_with_dot_71420

- name: download 71420.txt with sha256 checksum url with file scheme
get_url:
url: 'http://localhost:{{ http_port }}/71420.txt'
dest: '{{ remote_tmp_dir }}/71420sha256_with_file_scheme.txt'
checksum: 'sha256:file://{{ files_dir }}/sha256sum.txt'
register: result_sha256_with_file_scheme_71420

- stat:
path: "{{ remote_tmp_dir }}/71420sha256_with_dot.txt"
register: stat_result_sha256_with_file_scheme_71420

- name: Assert that the file was downloaded
assert:
that:
Expand All @@ -429,6 +481,14 @@
- "stat_result_sha256.stat.exists == true"
- "stat_result_sha256_with_dot.stat.exists == true"
- "stat_result_sha256_with_file_scheme.stat.exists == true"
- result_sha1_71420 is changed
- result_sha256_71420 is changed
- result_sha256_with_dot_71420 is changed
- result_sha256_with_file_scheme_71420 is changed
- "stat_result_sha1_71420.stat.exists == true"
- "stat_result_sha256_71420.stat.exists == true"
- "stat_result_sha256_with_dot_71420.stat.exists == true"
- "stat_result_sha256_with_file_scheme_71420.stat.exists == true"

#https://github.com/ansible/ansible/issues/16191
- name: Test url split with no filename
Expand Down