| 1 |
--- |
| 2 |
- set_fact: |
| 3 |
zip_info: | |
| 4 |
param ($Path) |
| 5 |
|
| 6 |
$ErrorActionPreference = 'Stop' |
| 7 |
$Ansible.Changed = $false |
| 8 |
|
| 9 |
$utf8 = New-Object -TypeName Text.UTF8Encoding -ArgumentList $false |
| 10 |
Add-Type -AssemblyName System.IO.Compression -ErrorAction Stop |
| 11 |
Add-Type -AssemblyName System.IO.Compression.FileSystem -ErrorAction Stop |
| 12 |
|
| 13 |
$zip = $null |
| 14 |
$fs = [IO.File]::OpenRead($Path) |
| 15 |
try { |
| 16 |
$zip = New-Object -TypeName IO.Compression.ZipArchive -ArgumentList $fs, 'Read', $false, $utf8 |
| 17 |
|
| 18 |
$zip.Entries | Select-Object -Propert FullName, Length |
| 19 |
} |
| 20 |
finally { |
| 21 |
if ($zip) { $zip.Dispose() } |
| 22 |
$fs.Dispose() |
| 23 |
} |
| 24 |
|
| 25 |
- name: create testdir\src directory for CI |
| 26 |
ansible.windows.win_file: |
| 27 |
path: '{{ win_zip_dir }}\src\' |
| 28 |
state: directory |
| 29 |
|
| 30 |
- name: create testdir\zipped directory for CI |
| 31 |
ansible.windows.win_file: |
| 32 |
path: '{{ win_zip_dir }}\zipped\' |
| 33 |
state: directory |
| 34 |
|
| 35 |
- name: create test files for CI |
| 36 |
ansible.builtin.win_copy: |
| 37 |
content: | |
| 38 |
This is a test file. |
| 39 |
dest: '{{ win_zip_dir }}\src\{{ win_zip_name }}.txt' |
| 40 |
|
| 41 |
# Case01: Check file compression |
| 42 |
- name: compress a file (check) |
| 43 |
win_zip: |
| 44 |
src: '{{ win_zip_dir }}\src\{{ win_zip_name }}.txt' |
| 45 |
dest: '{{ win_zip_dir }}\zipped\test_file.zip' |
| 46 |
register: zip_check |
| 47 |
check_mode: yes |
| 48 |
|
| 49 |
- name: get result of compress zip (check) |
| 50 |
ansible.windows.win_stat: |
| 51 |
path: '{{ win_zip_dir }}\zipped\test_file.zip' |
| 52 |
register: zip_actual_check |
| 53 |
|
| 54 |
- name: assert result of zip (check) |
| 55 |
assert: |
| 56 |
that: |
| 57 |
- zip_check is changed |
| 58 |
- not zip_actual_check.stat.exists |
| 59 |
|
| 60 |
- name: compress a file |
| 61 |
win_zip: |
| 62 |
src: '{{ win_zip_dir }}\src\{{ win_zip_name }}.txt' |
| 63 |
dest: '{{ win_zip_dir }}\zipped\test_file.zip' |
| 64 |
register: zip |
| 65 |
|
| 66 |
- name: get result of compress zip |
| 67 |
ansible.windows.win_powershell: |
| 68 |
script: '{{ zip_info }}' |
| 69 |
parameters: |
| 70 |
Path: '{{ win_zip_dir }}\zipped\test_file.zip' |
| 71 |
register: zip_actual |
| 72 |
|
| 73 |
- name: assert result of zip |
| 74 |
assert: |
| 75 |
that: |
| 76 |
- zip is changed |
| 77 |
- zip_actual.output | length == 1 |
| 78 |
- zip_actual.output[0]['FullName'] == win_zip_name + '.txt' |
| 79 |
- zip_actual.output[0]['Length'] == 21 |
| 80 |
|
| 81 |
# Case02: Check directory compression |
| 82 |
- name: compress a directory (check) |
| 83 |
win_zip: |
| 84 |
src: '{{ win_zip_dir }}\src\' |
| 85 |
dest: '{{ win_zip_dir }}\test_dir.zip' |
| 86 |
register: zip_check |
| 87 |
check_mode: yes |
| 88 |
|
| 89 |
- name: get result of compress zip (check) |
| 90 |
ansible.windows.win_stat: |
| 91 |
path: '{{ win_zip_dir }}\test_dir.zip' |
| 92 |
register: zip_actual_check |
| 93 |
|
| 94 |
- name: assert result of zip (check) |
| 95 |
assert: |
| 96 |
that: |
| 97 |
- zip_check is changed |
| 98 |
- not zip_actual_check.stat.exists |
| 99 |
|
| 100 |
- name: compress a directory |
| 101 |
win_zip: |
| 102 |
src: '{{ win_zip_dir }}\src\' |
| 103 |
dest: '{{ win_zip_dir }}\test_dir.zip' |
| 104 |
register: zip |
| 105 |
|
| 106 |
- name: get result of compress zip |
| 107 |
ansible.windows.win_powershell: |
| 108 |
script: '{{ zip_info }}' |
| 109 |
parameters: |
| 110 |
Path: '{{ win_zip_dir }}\test_dir.zip' |
| 111 |
register: zip_actual |
| 112 |
|
| 113 |
- name: assert result of zip |
| 114 |
assert: |
| 115 |
that: |
| 116 |
- zip is changed |
| 117 |
- zip_actual.output | length == 1 |
| 118 |
# Should contain the original base directory |
| 119 |
- zip_actual.output[0]['FullName'] == "src/" + win_zip_name + '.txt' |
| 120 |
- zip_actual.output[0]['Length'] == 21 |
| 121 |
|
| 122 |
- name: compress a directories contents |
| 123 |
win_zip: |
| 124 |
src: '{{ win_zip_dir }}\src\*' |
| 125 |
dest: '{{ win_zip_dir }}\test_dir_content.zip' |
| 126 |
register: zip |
| 127 |
|
| 128 |
- name: get result of compress zip |
| 129 |
ansible.windows.win_powershell: |
| 130 |
script: '{{ zip_info }}' |
| 131 |
parameters: |
| 132 |
Path: '{{ win_zip_dir }}\test_dir_content.zip' |
| 133 |
register: zip_actual |
| 134 |
|
| 135 |
- name: assert result of zip |
| 136 |
assert: |
| 137 |
that: |
| 138 |
- zip is changed |
| 139 |
- zip_actual.output | length == 1 |
| 140 |
# Should not contain the original base directory |
| 141 |
- zip_actual.output[0]['FullName'] == win_zip_name + '.txt' |
| 142 |
- zip_actual.output[0]['Length'] == 21 |
| 143 |
|
| 144 |
- name: compress a file with existing dest (check) |
| 145 |
win_zip: |
| 146 |
src: '{{ win_zip_dir }}\src\{{ win_zip_name }}.txt' |
| 147 |
dest: '{{ win_zip_dir }}\zipped\test_file.zip' |
| 148 |
register: zip_check |
| 149 |
check_mode: yes |
| 150 |
|
| 151 |
- name: assert result of zip (check) |
| 152 |
assert: |
| 153 |
that: |
| 154 |
- not zip_check is changed |
| 155 |
|
| 156 |
- name: compress a file to existing dest |
| 157 |
win_zip: |
| 158 |
src: '{{ win_zip_dir }}\src\{{ win_zip_name }}.txt' |
| 159 |
dest: '{{ win_zip_dir }}\zipped\test_file.zip' |
| 160 |
register: zip |
| 161 |
|
| 162 |
- name: assert result of zip |
| 163 |
assert: |
| 164 |
that: |
| 165 |
- not zip is changed |