| 1 |
# File module tests for overwriting directories |
| 2 |
- name: Initialize the test output dir |
| 3 |
import_tasks: initialize.yml |
| 4 |
|
| 5 |
# We need to make this more consistent: |
| 6 |
# https://github.com/ansible/proposals/issues/111 |
| 7 |
# |
| 8 |
# This series of tests document the current inconsistencies. We should not |
| 9 |
# break these by accident but if we approve a proposal we can break these on |
| 10 |
# purpose. |
| 11 |
|
| 12 |
# |
| 13 |
# Setup |
| 14 |
# |
| 15 |
|
| 16 |
- name: create a test sub-directory |
| 17 |
file: |
| 18 |
dest: '{{remote_tmp_dir_test}}/sub1' |
| 19 |
state: directory |
| 20 |
|
| 21 |
- name: create a file for linking to |
| 22 |
copy: |
| 23 |
dest: '{{remote_tmp_dir_test}}/file_to_link' |
| 24 |
content: 'Hello World' |
| 25 |
|
| 26 |
# |
| 27 |
# Error condition: specify a directory with state={link,file}, force=False |
| 28 |
# |
| 29 |
|
| 30 |
# file raises an error |
| 31 |
- name: Try to create a file with directory as dest |
| 32 |
file: |
| 33 |
dest: '{{remote_tmp_dir_test}}/sub1' |
| 34 |
state: file |
| 35 |
force: False |
| 36 |
ignore_errors: True |
| 37 |
register: file1_result |
| 38 |
|
| 39 |
- name: Get stat info to show the directory has not been changed to a file |
| 40 |
stat: |
| 41 |
path: '{{ remote_tmp_dir_test }}/sub1' |
| 42 |
follow: False |
| 43 |
register: file1_dir_stat |
| 44 |
|
| 45 |
- name: verify that the directory was not overwritten |
| 46 |
assert: |
| 47 |
that: |
| 48 |
- 'file1_result is failed' |
| 49 |
- 'file1_dir_stat["stat"].isdir' |
| 50 |
|
| 51 |
# link raises an error |
| 52 |
- name: Try to create a symlink with directory as dest |
| 53 |
file: |
| 54 |
src: '{{ remote_tmp_dir_test }}/file_to_link' |
| 55 |
dest: '{{remote_tmp_dir_test}}/sub1' |
| 56 |
state: link |
| 57 |
force: False |
| 58 |
ignore_errors: True |
| 59 |
register: file2_result |
| 60 |
|
| 61 |
- name: Get stat info to show the directory has not been changed to a file |
| 62 |
stat: |
| 63 |
path: '{{ remote_tmp_dir_test }}/sub1' |
| 64 |
follow: False |
| 65 |
register: file2_dir_stat |
| 66 |
|
| 67 |
- name: verify that the directory was not overwritten |
| 68 |
assert: |
| 69 |
that: |
| 70 |
- 'file2_result is failed' |
| 71 |
- 'file2_dir_stat["stat"].isdir' |
| 72 |
|
| 73 |
# |
| 74 |
# Error condition: file and link with non-empty directory |
| 75 |
# |
| 76 |
|
| 77 |
- copy: |
| 78 |
content: 'test' |
| 79 |
dest: '{{ remote_tmp_dir_test }}/sub1/passwd' |
| 80 |
|
| 81 |
# file raises an error |
| 82 |
- name: Try to create a file with directory as dest |
| 83 |
file: |
| 84 |
dest: '{{remote_tmp_dir_test}}/sub1' |
| 85 |
state: file |
| 86 |
force: True |
| 87 |
ignore_errors: True |
| 88 |
register: file3_result |
| 89 |
|
| 90 |
- name: Get stat info to show the directory has not been changed to a file |
| 91 |
stat: |
| 92 |
path: '{{ remote_tmp_dir_test }}/sub1' |
| 93 |
follow: False |
| 94 |
register: file3_dir_stat |
| 95 |
|
| 96 |
- name: verify that the directory was not overwritten |
| 97 |
assert: |
| 98 |
that: |
| 99 |
- 'file3_result is failed' |
| 100 |
- 'file3_dir_stat["stat"].isdir' |
| 101 |
|
| 102 |
# link raises an error |
| 103 |
- name: Try to create a symlink with directory as dest |
| 104 |
file: |
| 105 |
src: '{{ remote_tmp_dir_test }}/file_to_link' |
| 106 |
dest: '{{remote_tmp_dir_test}}/sub1' |
| 107 |
state: link |
| 108 |
force: True |
| 109 |
ignore_errors: True |
| 110 |
register: file4_result |
| 111 |
|
| 112 |
- name: Get stat info to show the directory has not been changed to a file |
| 113 |
stat: |
| 114 |
path: '{{ remote_tmp_dir_test }}/sub1' |
| 115 |
follow: False |
| 116 |
register: file4_dir_stat |
| 117 |
|
| 118 |
- name: verify that the directory was not overwritten |
| 119 |
assert: |
| 120 |
that: |
| 121 |
- 'file4_result is failed' |
| 122 |
- 'file4_dir_stat["stat"].isdir' |
| 123 |
|
| 124 |
# Cleanup the file that made it non-empty |
| 125 |
- name: Cleanup the file that made the directory nonempty |
| 126 |
file: |
| 127 |
state: 'absent' |
| 128 |
dest: '{{ remote_tmp_dir_test }}/sub1/passwd' |
| 129 |
|
| 130 |
# |
| 131 |
# Error condition: file cannot even overwrite an empty directory with force=True |
| 132 |
# |
| 133 |
|
| 134 |
# file raises an error |
| 135 |
- name: Try to create a file with directory as dest |
| 136 |
file: |
| 137 |
dest: '{{remote_tmp_dir_test}}/sub1' |
| 138 |
state: file |
| 139 |
force: True |
| 140 |
ignore_errors: True |
| 141 |
register: file5_result |
| 142 |
|
| 143 |
- name: Get stat info to show the directory has not been changed to a file |
| 144 |
stat: |
| 145 |
path: '{{ remote_tmp_dir_test }}/sub1' |
| 146 |
follow: False |
| 147 |
register: file5_dir_stat |
| 148 |
|
| 149 |
- name: verify that the directory was not overwritten |
| 150 |
assert: |
| 151 |
that: |
| 152 |
- 'file5_result is failed' |
| 153 |
- 'file5_dir_stat["stat"].isdir' |
| 154 |
|
| 155 |
# |
| 156 |
# Directory overwriting - link with force=True will overwrite an empty directory |
| 157 |
# |
| 158 |
|
| 159 |
# link can overwrite an empty directory with force=True |
| 160 |
- name: Try to create a symlink with directory as dest |
| 161 |
file: |
| 162 |
src: '{{ remote_tmp_dir_test }}/file_to_link' |
| 163 |
dest: '{{remote_tmp_dir_test}}/sub1' |
| 164 |
state: link |
| 165 |
force: True |
| 166 |
register: file6_result |
| 167 |
|
| 168 |
- name: Get stat info to show the directory has been overwritten |
| 169 |
stat: |
| 170 |
path: '{{ remote_tmp_dir_test }}/sub1' |
| 171 |
follow: False |
| 172 |
register: file6_dir_stat |
| 173 |
|
| 174 |
- name: verify that the directory was overwritten |
| 175 |
assert: |
| 176 |
that: |
| 177 |
- 'file6_result is changed' |
| 178 |
- 'not file6_dir_stat["stat"].isdir' |
| 179 |
- 'file6_dir_stat["stat"].islnk' |
| 180 |
|
| 181 |
# |
| 182 |
# Cleanup from last set of tests |
| 183 |
# |
| 184 |
|
| 185 |
- name: Cleanup the test subdirectory |
| 186 |
file: |
| 187 |
dest: '{{remote_tmp_dir_test}}/sub1' |
| 188 |
state: 'absent' |
| 189 |
|
| 190 |
- name: Re-create the test sub-directory |
| 191 |
file: |
| 192 |
dest: '{{remote_tmp_dir_test}}/sub1' |
| 193 |
state: 'directory' |
| 194 |
|
| 195 |
# |
| 196 |
# Hard links have the proposed 111 behaviour already: Place the new file inside the directory |
| 197 |
# |
| 198 |
|
| 199 |
- name: Try to create a hardlink with directory as dest |
| 200 |
file: |
| 201 |
src: '{{ remote_tmp_dir_test }}/file_to_link' |
| 202 |
dest: '{{ remote_tmp_dir_test }}/sub1' |
| 203 |
state: hard |
| 204 |
force: False |
| 205 |
ignore_errors: True |
| 206 |
register: file7_result |
| 207 |
|
| 208 |
- name: Get stat info to show the directory has not been changed to a file |
| 209 |
stat: |
| 210 |
path: '{{ remote_tmp_dir_test }}/sub1' |
| 211 |
follow: False |
| 212 |
register: file7_dir_stat |
| 213 |
|
| 214 |
- name: Get stat info to show the link has been created |
| 215 |
stat: |
| 216 |
path: '{{ remote_tmp_dir_test }}/sub1/file_to_link' |
| 217 |
follow: False |
| 218 |
register: file7_link_stat |
| 219 |
|
| 220 |
- debug: |
| 221 |
var: file7_link_stat |
| 222 |
|
| 223 |
- name: verify that the directory was not overwritten |
| 224 |
assert: |
| 225 |
that: |
| 226 |
- 'file7_result is changed' |
| 227 |
- 'file7_dir_stat["stat"].isdir' |
| 228 |
- 'file7_link_stat["stat"].isfile' |
| 229 |
- 'file7_link_stat["stat"].isfile' |
| 230 |
ignore_errors: True |
| 231 |
|
| 232 |
# |
| 233 |
# Touch is a bit different than everything else. |
| 234 |
# If we need to set timestamps we should probably add atime, mtime, and ctime parameters |
| 235 |
# But I think touch was written because state=file didn't create a file if it |
| 236 |
# didn't already exist. We should look at changing that behaviour. |
| 237 |
# |
| 238 |
|
| 239 |
- name: Get initial stat info to compare with later |
| 240 |
stat: |
| 241 |
path: '{{ remote_tmp_dir_test }}/sub1' |
| 242 |
follow: False |
| 243 |
register: file8_initial_dir_stat |
| 244 |
|
| 245 |
- name: Pause to ensure stat times are not the exact same |
| 246 |
pause: |
| 247 |
seconds: 1 |
| 248 |
|
| 249 |
- name: Use touch with directory as dest |
| 250 |
file: |
| 251 |
dest: '{{remote_tmp_dir_test}}/sub1' |
| 252 |
state: touch |
| 253 |
force: False |
| 254 |
register: file8_result |
| 255 |
|
| 256 |
- name: Get stat info to show the directory has not been changed to a file |
| 257 |
stat: |
| 258 |
path: '{{ remote_tmp_dir_test }}/sub1' |
| 259 |
follow: False |
| 260 |
register: file8_dir_stat |
| 261 |
|
| 262 |
- name: verify that the directory has been updated |
| 263 |
assert: |
| 264 |
that: |
| 265 |
- 'file8_result is changed' |
| 266 |
- 'file8_dir_stat["stat"].isdir' |
| 267 |
- 'file8_dir_stat["stat"]["mtime"] != file8_initial_dir_stat["stat"]["mtime"]' |
| 268 |
|
| 269 |
- name: Get initial stat info to compare with later |
| 270 |
stat: |
| 271 |
path: '{{ remote_tmp_dir_test }}/sub1' |
| 272 |
follow: False |
| 273 |
register: file11_initial_dir_stat |
| 274 |
|
| 275 |
- name: Use touch with directory as dest and keep mtime and atime |
| 276 |
file: |
| 277 |
dest: '{{remote_tmp_dir_test}}/sub1' |
| 278 |
state: touch |
| 279 |
force: False |
| 280 |
modification_time: preserve |
| 281 |
access_time: preserve |
| 282 |
register: file11_result |
| 283 |
|
| 284 |
- name: Get stat info to show the directory has not been changed |
| 285 |
stat: |
| 286 |
path: '{{ remote_tmp_dir_test }}/sub1' |
| 287 |
follow: False |
| 288 |
register: file11_dir_stat |
| 289 |
|
| 290 |
- name: verify that the directory has not been updated |
| 291 |
assert: |
| 292 |
that: |
| 293 |
- 'file11_result is not changed' |
| 294 |
- 'file11_dir_stat["stat"].isdir' |
| 295 |
- 'file11_dir_stat["stat"]["mtime"] == file11_initial_dir_stat["stat"]["mtime"]' |
| 296 |
- 'file11_dir_stat["stat"]["atime"] == file11_initial_dir_stat["stat"]["atime"]' |
| 297 |
|
| 298 |
# |
| 299 |
# State=directory realizes that the directory already exists and does nothing |
| 300 |
# |
| 301 |
- name: Get initial stat info to compare with later |
| 302 |
stat: |
| 303 |
path: '{{ remote_tmp_dir_test }}/sub1' |
| 304 |
follow: False |
| 305 |
register: file9_initial_dir_stat |
| 306 |
|
| 307 |
- name: Use directory with directory as dest |
| 308 |
file: |
| 309 |
dest: '{{remote_tmp_dir_test}}/sub1' |
| 310 |
state: directory |
| 311 |
force: False |
| 312 |
register: file9_result |
| 313 |
|
| 314 |
- name: Get stat info to show the directory has not been changed |
| 315 |
stat: |
| 316 |
path: '{{ remote_tmp_dir_test }}/sub1' |
| 317 |
follow: False |
| 318 |
register: file9_dir_stat |
| 319 |
|
| 320 |
- name: verify that the directory has been updated |
| 321 |
assert: |
| 322 |
that: |
| 323 |
- 'file9_result is not changed' |
| 324 |
- 'file9_dir_stat["stat"].isdir' |
| 325 |
- 'file9_dir_stat["stat"]["mtime"] == file9_initial_dir_stat["stat"]["mtime"]' |
| 326 |
|
| 327 |
- name: Use directory with directory as dest and force=True |
| 328 |
file: |
| 329 |
dest: '{{remote_tmp_dir_test}}/sub1' |
| 330 |
state: directory |
| 331 |
force: True |
| 332 |
register: file10_result |
| 333 |
|
| 334 |
- name: Get stat info to show the directory has not been changed |
| 335 |
stat: |
| 336 |
path: '{{ remote_tmp_dir_test }}/sub1' |
| 337 |
follow: False |
| 338 |
register: file10_dir_stat |
| 339 |
|
| 340 |
- name: verify that the directory has been updated |
| 341 |
assert: |
| 342 |
that: |
| 343 |
- 'file10_result is not changed' |
| 344 |
- 'file10_dir_stat["stat"].isdir' |
| 345 |
- 'file10_dir_stat["stat"]["mtime"] == file9_initial_dir_stat["stat"]["mtime"]' |