summaryrefslogtreecommitdiffstats
path: root/test/integration/targets/file/tasks/directory_as_dest.yml
blob: 161a12a4beacbc61b3f5a8129a1cc9c55dfcd649 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
# File module tests for overwriting directories
- name: Initialize the test output dir
  import_tasks: initialize.yml

# We need to make this more consistent:
# https://github.com/ansible/proposals/issues/111
#
# This series of tests document the current inconsistencies.  We should not
# break these by accident but if we approve a proposal we can break these on
# purpose.

#
# Setup
#

- name: create a test sub-directory
  file:
    dest: '{{remote_tmp_dir_test}}/sub1'
    state: directory

- name: create a file for linking to
  copy:
    dest: '{{remote_tmp_dir_test}}/file_to_link'
    content: 'Hello World'

#
# Error condtion: specify a directory with state={link,file}, force=False
#

# file raises an error
- name: Try to create a file with directory as dest
  file:
    dest: '{{remote_tmp_dir_test}}/sub1'
    state: file
    force: False
  ignore_errors: True
  register: file1_result

- name: Get stat info to show the directory has not been changed to a file
  stat:
    path: '{{ remote_tmp_dir_test }}/sub1'
    follow: False
  register: file1_dir_stat

- name: verify that the directory was not overwritten
  assert:
    that:
      - 'file1_result is failed'
      - 'file1_dir_stat["stat"].isdir'

# link raises an error
- name: Try to create a symlink with directory as dest
  file:
    src: '{{ remote_tmp_dir_test }}/file_to_link'
    dest: '{{remote_tmp_dir_test}}/sub1'
    state: link
    force: False
  ignore_errors: True
  register: file2_result

- name: Get stat info to show the directory has not been changed to a file
  stat:
    path: '{{ remote_tmp_dir_test }}/sub1'
    follow: False
  register: file2_dir_stat

- name: verify that the directory was not overwritten
  assert:
    that:
      - 'file2_result is failed'
      - 'file2_dir_stat["stat"].isdir'

#
# Error condition: file and link with non-empty directory
#

- copy:
    content: 'test'
    dest: '{{ remote_tmp_dir_test }}/sub1/passwd'

# file raises an error
- name: Try to create a file with directory as dest
  file:
    dest: '{{remote_tmp_dir_test}}/sub1'
    state: file
    force: True
  ignore_errors: True
  register: file3_result

- name: Get stat info to show the directory has not been changed to a file
  stat:
    path: '{{ remote_tmp_dir_test }}/sub1'
    follow: False
  register: file3_dir_stat

- name: verify that the directory was not overwritten
  assert:
    that:
      - 'file3_result is failed'
      - 'file3_dir_stat["stat"].isdir'

# link raises an error
- name: Try to create a symlink with directory as dest
  file:
    src: '{{ remote_tmp_dir_test }}/file_to_link'
    dest: '{{remote_tmp_dir_test}}/sub1'
    state: link
    force: True
  ignore_errors: True
  register: file4_result

- name: Get stat info to show the directory has not been changed to a file
  stat:
    path: '{{ remote_tmp_dir_test }}/sub1'
    follow: False
  register: file4_dir_stat

- name: verify that the directory was not overwritten
  assert:
    that:
      - 'file4_result is failed'
      - 'file4_dir_stat["stat"].isdir'

# Cleanup the file that made it non-empty
- name: Cleanup the file that made the directory nonempty
  file:
    state: 'absent'
    dest: '{{ remote_tmp_dir_test }}/sub1/passwd'

#
# Error condition: file cannot even overwrite an empty directory with force=True
#

# file raises an error
- name: Try to create a file with directory as dest
  file:
    dest: '{{remote_tmp_dir_test}}/sub1'
    state: file
    force: True
  ignore_errors: True
  register: file5_result

- name: Get stat info to show the directory has not been changed to a file
  stat:
    path: '{{ remote_tmp_dir_test }}/sub1'
    follow: False
  register: file5_dir_stat

- name: verify that the directory was not overwritten
  assert:
    that:
      - 'file5_result is failed'
      - 'file5_dir_stat["stat"].isdir'

#
# Directory overwriting - link with force=True will overwrite an empty directory
#

# link can overwrite an empty directory with force=True
- name: Try to create a symlink with directory as dest
  file:
    src: '{{ remote_tmp_dir_test }}/file_to_link'
    dest: '{{remote_tmp_dir_test}}/sub1'
    state: link
    force: True
  register: file6_result

- name: Get stat info to show the directory has been overwritten
  stat:
    path: '{{ remote_tmp_dir_test }}/sub1'
    follow: False
  register: file6_dir_stat

- name: verify that the directory was overwritten
  assert:
    that:
      - 'file6_result is changed'
      - 'not file6_dir_stat["stat"].isdir'
      - 'file6_dir_stat["stat"].islnk'

#
# Cleanup from last set of tests
#

- name: Cleanup the test subdirectory
  file:
    dest: '{{remote_tmp_dir_test}}/sub1'
    state: 'absent'

- name: Re-create the test sub-directory
  file:
    dest: '{{remote_tmp_dir_test}}/sub1'
    state: 'directory'

#
# Hard links have the proposed 111 behaviour already: Place the new file inside the directory
#

- name: Try to create a hardlink with directory as dest
  file:
    src: '{{ remote_tmp_dir_test }}/file_to_link'
    dest: '{{ remote_tmp_dir_test }}/sub1'
    state: hard
    force: False
  ignore_errors: True
  register: file7_result

- name: Get stat info to show the directory has not been changed to a file
  stat:
    path: '{{ remote_tmp_dir_test }}/sub1'
    follow: False
  register: file7_dir_stat

- name: Get stat info to show the link has been created
  stat:
    path: '{{ remote_tmp_dir_test }}/sub1/file_to_link'
    follow: False
  register: file7_link_stat

- debug:
    var: file7_link_stat

- name: verify that the directory was not overwritten
  assert:
    that:
      - 'file7_result is changed'
      - 'file7_dir_stat["stat"].isdir'
      - 'file7_link_stat["stat"].isfile'
      - 'file7_link_stat["stat"].isfile'
  ignore_errors: True

#
# Touch is a bit different than everything else.
# If we need to set timestamps we should probably add atime, mtime, and ctime parameters
# But I think touch was written because state=file didn't create a file if it
# didn't already exist.  We should look at changing that behaviour.
#

- name: Get initial stat info to compare with later
  stat:
    path: '{{ remote_tmp_dir_test }}/sub1'
    follow: False
  register: file8_initial_dir_stat

- name: Pause to ensure stat times are not the exact same
  pause:
    seconds: 1

- name: Use touch with directory as dest
  file:
    dest: '{{remote_tmp_dir_test}}/sub1'
    state: touch
    force: False
  register: file8_result

- name: Get stat info to show the directory has not been changed to a file
  stat:
    path: '{{ remote_tmp_dir_test }}/sub1'
    follow: False
  register: file8_dir_stat

- name: verify that the directory has been updated
  assert:
    that:
      - 'file8_result is changed'
      - 'file8_dir_stat["stat"].isdir'
      - 'file8_dir_stat["stat"]["mtime"] != file8_initial_dir_stat["stat"]["mtime"]'

- name: Get initial stat info to compare with later
  stat:
    path: '{{ remote_tmp_dir_test }}/sub1'
    follow: False
  register: file11_initial_dir_stat

- name: Use touch with directory as dest and keep mtime and atime
  file:
    dest: '{{remote_tmp_dir_test}}/sub1'
    state: touch
    force: False
    modification_time: preserve
    access_time: preserve
  register: file11_result

- name: Get stat info to show the directory has not been changed
  stat:
    path: '{{ remote_tmp_dir_test }}/sub1'
    follow: False
  register: file11_dir_stat

- name: verify that the directory has not been updated
  assert:
    that:
      - 'file11_result is not changed'
      - 'file11_dir_stat["stat"].isdir'
      - 'file11_dir_stat["stat"]["mtime"] == file11_initial_dir_stat["stat"]["mtime"]'
      - 'file11_dir_stat["stat"]["atime"] == file11_initial_dir_stat["stat"]["atime"]'

#
# State=directory realizes that the directory already exists and does nothing
#
- name: Get initial stat info to compare with later
  stat:
    path: '{{ remote_tmp_dir_test }}/sub1'
    follow: False
  register: file9_initial_dir_stat

- name: Use directory with directory as dest
  file:
    dest: '{{remote_tmp_dir_test}}/sub1'
    state: directory
    force: False
  register: file9_result

- name: Get stat info to show the directory has not been changed
  stat:
    path: '{{ remote_tmp_dir_test }}/sub1'
    follow: False
  register: file9_dir_stat

- name: verify that the directory has been updated
  assert:
    that:
      - 'file9_result is not changed'
      - 'file9_dir_stat["stat"].isdir'
      - 'file9_dir_stat["stat"]["mtime"] == file9_initial_dir_stat["stat"]["mtime"]'

- name: Use directory with directory as dest and force=True
  file:
    dest: '{{remote_tmp_dir_test}}/sub1'
    state: directory
    force: True
  register: file10_result

- name: Get stat info to show the directory has not been changed
  stat:
    path: '{{ remote_tmp_dir_test }}/sub1'
    follow: False
  register: file10_dir_stat

- name: verify that the directory has been updated
  assert:
    that:
      - 'file10_result is not changed'
      - 'file10_dir_stat["stat"].isdir'
      - 'file10_dir_stat["stat"]["mtime"] == file9_initial_dir_stat["stat"]["mtime"]'