summaryrefslogtreecommitdiffstats
path: root/test/integration/targets/win_lineinfile/tasks/main.yml
blob: 48c465f2566ae658a569003d703e99ddd32a1fea (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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
# Test code for the win_lineinfile module, adapted from the standard lineinfile module tests
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible.  If not, see <http://www.gnu.org/licenses/>.


- name: deploy the test file for lineinfile
  win_copy: src=test.txt dest={{win_output_dir}}/test.txt
  register: result

- name: assert that the test file was deployed
  assert:
    that:
    - "result.changed == true"

- name: stat the test file
  win_stat: path={{win_output_dir}}/test.txt
  register: result

- name: check win_stat file result
  assert:
    that:
      - "result.stat.exists"
      - "not result.stat.isdir"
      - "result.stat.checksum == '5feac65e442c91f557fc90069ce6efc4d346ab51'"
      - "not result|failed"
      - "not result|changed"


- name: insert a line at the beginning of the file, and back it up
  win_lineinfile: dest={{win_output_dir}}/test.txt state=present line="New line at the beginning" insertbefore="BOF" backup=yes
  register: result

- name: assert that the line was inserted at the head of the file
  assert:
    that:
    - "result.changed == true"
    - "result.msg == 'line added'"
    - "result.backup != ''"

- name: stat the backup file
  win_stat: path={{result.backup}}
  register: result

- name: assert the backup file matches the previous hash
  assert:
    that:
    - "result.stat.checksum == '5feac65e442c91f557fc90069ce6efc4d346ab51'"

- name: stat the test after the insert at the head
  win_stat: path={{win_output_dir}}/test.txt
  register: result

- name: assert test hash is what we expect for the file with the insert at the head
  assert:
    that:
    - "result.stat.checksum == 'b526e2e044defc64dfb0fad2f56e105178f317d8'"

- name: insert a line at the end of the file
  win_lineinfile: dest={{win_output_dir}}/test.txt state=present line="New line at the end" insertafter="EOF"
  register: result

- name: assert that the line was inserted at the end of the file
  assert:
    that:
    - "result.changed == true"
    - "result.msg == 'line added'"

- name: stat the test after the insert at the end
  win_stat: path={{win_output_dir}}/test.txt
  register: result

- name: assert test checksum matches after the insert at the end
  assert:
    that:
    - "result.stat.checksum == 'dd5e207e28ce694ab18e41c2b16deb74fde93b14'"

- name: insert a line after the first line
  win_lineinfile: dest={{win_output_dir}}/test.txt state=present line="New line after line 1" insertafter="^This is line 1$"
  register: result

- name: assert that the line was inserted after the first line
  assert:
    that:
    - "result.changed == true"
    - "result.msg == 'line added'"

- name: stat the test after insert after the first line
  win_stat: path={{win_output_dir}}/test.txt
  register: result

- name: assert test checksum matches after the insert after the first line
  assert:
    that:
    - "result.stat.checksum == '604b17405f2088e6868af9680b7834087acdc8f4'"

- name: insert a line before the last line
  win_lineinfile: dest={{win_output_dir}}/test.txt state=present line="New line before line 5" insertbefore="^This is line 5$"
  register: result

- name: assert that the line was inserted before the last line
  assert:
    that:
    - "result.changed == true"
    - "result.msg == 'line added'"

- name: stat the test after the insert before the last line
  win_stat: path={{win_output_dir}}/test.txt
  register: result

- name: assert test checksum matches after the insert before the last line
  assert:
    that:
    - "result.stat.checksum == '8f5b30e8f01578043d782e5a68d4c327e75a6e34'"

- name: replace a line with backrefs
  win_lineinfile: dest={{win_output_dir}}/test.txt state=present line="This is line 3" backrefs=yes regexp="^(REF).*$"
  register: result

- name: assert that the line with backrefs was changed
  assert:
    that:
    - "result.changed == true"
    - "result.msg == 'line replaced'"

- name: stat the test after the backref line was replaced
  win_stat: path={{win_output_dir}}/test.txt
  register: result

- name: assert test checksum matches after backref line was replaced
  assert:
    that:
    - "result.stat.checksum == 'ef6b02645908511a2cfd2df29d50dd008897c580'"

- name: remove the middle line
  win_lineinfile: dest={{win_output_dir}}/test.txt state=absent regexp="^This is line 3$"
  register: result

- name: assert that the line was removed
  assert:
    that:
    - "result.changed == true"
    - "result.msg == '1 line(s) removed'"

- name: stat the test after the middle line was removed
  win_stat: path={{win_output_dir}}/test.txt
  register: result

- name: assert test checksum matches after the middle line was removed
  assert:
    that:
    - "result.stat.checksum == '11695efa472be5c31c736bc43e055f8ac90eabdf'"

- name: run a validation script that succeeds
  win_lineinfile: dest={{win_output_dir}}/test.txt state=absent regexp="^This is line 5$" validate="sort.exe %s"
  register: result

- name: assert that the file validated after removing a line
  assert:
    that:
    - "result.changed == true"
    - "result.msg == '1 line(s) removed'"

- name: stat the test after the validation succeeded
  win_stat: path={{win_output_dir}}/test.txt
  register: result

- name: assert test checksum matches after the validation succeeded
  assert:
    that:
    - "result.stat.checksum == '39c38a30aa6ac6af9ec41f54c7ed7683f1249347'"

- name: run a validation script that fails
  win_lineinfile: dest={{win_output_dir}}/test.txt state=absent regexp="^This is line 1$" validate="sort.exe %s.foo"
  register: result
  ignore_errors: yes

- name: assert that the validate failed
  assert:
    that:
    - "result.failed == true"

- name: stat the test after the validation failed
  win_stat: path={{win_output_dir}}/test.txt
  register: result

- name: assert test checksum matches the previous after the validation failed
  assert:
    that:
    - "result.stat.checksum == '39c38a30aa6ac6af9ec41f54c7ed7683f1249347'"

- name: use create=yes
  win_lineinfile: dest={{win_output_dir}}/new_test.txt create=yes insertbefore=BOF state=present line="This is a new file"
  register: result

- name: assert that the new file was created
  assert:
    that:
    - "result.changed == true"
    - "result.msg == 'line added'"

- name: validate that the newly created file exists
  win_stat: path={{win_output_dir}}/new_test.txt
  register: result
  ignore_errors: yes

- name: assert the newly created test checksum matches
  assert:
    that:
    - "result.stat.checksum == '84faac1183841c57434693752fc3debc91b9195d'"

# Test EOF in cases where file has no newline at EOF
- name: testnoeof deploy the file for lineinfile
  win_copy: src=testnoeof.txt dest={{win_output_dir}}/testnoeof.txt
  register: result

- name: testnoeof insert a line at the end of the file
  win_lineinfile: dest={{win_output_dir}}/testnoeof.txt state=present line="New line at the end" insertafter="EOF"
  register: result

- name: testempty assert that the line was inserted at the end of the file
  assert:
    that:
    - "result.changed == true"
    - "result.msg == 'line added'"

- name: testnoeof stat the no newline EOF test after the insert at the end
  win_stat: path={{win_output_dir}}/testnoeof.txt
  register: result

- name: testnoeof assert test checksum matches after the insert at the end
  assert:
    that:
    - "result.stat.checksum == '229852b09f7e9921fbcbb0ee0166ba78f7f7f261'"

- name: add multiple lines at the end of the file
  win_lineinfile: dest={{win_output_dir}}/test.txt state=present line="This is a line\r\nwith newline character" insertafter="EOF"
  register: result

- name: assert that the multiple lines was inserted
  assert:
    that:
    - "result.changed == true"
    - "result.msg == 'line added'"

- name: stat file after adding multiple lines
  win_stat: path={{win_output_dir}}/test.txt
  register: result

- name: assert test checksum matches after inserting multiple lines
  assert:
    that:
    - "result.stat.checksum == '1401413cd4eac732be66cd6aceddd334c4240f86'"



# Test EOF with empty file to make sure no unnecessary newline is added
- name: testempty deploy the testempty file for lineinfile
  win_copy: src=testempty.txt dest={{win_output_dir}}/testempty.txt
  register: result

- name: testempty insert a line at the end of the file
  win_lineinfile: dest={{win_output_dir}}/testempty.txt state=present line="New line at the end" insertafter="EOF"
  register: result

- name: testempty assert that the line was inserted at the end of the file
  assert:
    that:
    - "result.changed == true"
    - "result.msg == 'line added'"

- name: testempty stat the test after the insert at the end
  win_stat: path={{win_output_dir}}/testempty.txt
  register: result

- name: testempty assert test checksum matches after the insert at the end
  assert:
    that:
    - "result.stat.checksum == 'd3d34f11edda51be7ca5dcb0757cf3e1257c0bfe'"



- name: replace a line with backrefs included in the line
  win_lineinfile: dest={{win_output_dir}}/test.txt state=present line="New $1 created with the backref" backrefs=yes regexp="^This is (line 4)$"
  register: result

- name: assert that the line with backrefs was changed
  assert:
    that:
    - "result.changed == true"
    - "result.msg == 'line replaced'"

- name: stat the test after the backref line was replaced
  win_stat: path={{win_output_dir}}/test.txt
  register: result

- name: assert test checksum matches after backref line was replaced
  assert:
    that:
    - "result.stat.checksum == 'e6ff42e926dac2274c93dff0b8a323e07ae09149'"

###################################################################
# issue 8535

- name: create a new file for testing quoting issues
  win_copy: src=test_quoting.txt dest={{win_output_dir}}/test_quoting.txt
  register: result

- name: assert the new file was created
  assert:
    that:
    - result.changed

- name: use with_items to add code-like strings to the quoting txt file
  win_lineinfile: >
    dest={{win_output_dir}}/test_quoting.txt
    line="{{ item }}"
    insertbefore="BOF"
  with_items:
    - "'foo'"
    - "dotenv.load();"
    - "var dotenv = require('dotenv');"
  register: result

- name: assert the quote test file was modified correctly
  assert:
    that:
    - result.results|length == 3
    - result.results[0].changed
    - result.results[0].item == "'foo'"
    - result.results[1].changed
    - result.results[1].item == "dotenv.load();"
    - result.results[2].changed
    - result.results[2].item == "var dotenv = require('dotenv');"

- name: stat the quote test file
  win_stat: path={{win_output_dir}}/test_quoting.txt
  register: result

- name: assert test checksum matches for quote test file
  assert:
    that:
    - "result.stat.checksum == 'f3bccdbdfa1d7176c497ef87d04957af40ab48d2'"

- name: append a line into the quoted file with a single quote
  win_lineinfile: dest={{win_output_dir}}/test_quoting.txt line="import g'"
  register: result

- name: assert that the quoted file was changed
  assert:
    that:
    - result.changed

- name: stat the quote test file
  win_stat: path={{win_output_dir}}/test_quoting.txt
  register: result

- name: assert test checksum matches adding line with single quote
  assert:
    that:
    - "result.stat.checksum == 'dabf4cbe471e1797d8dcfc773b6b638c524d5237'"

- name: insert a line into the quoted file with many double quotation strings
  win_lineinfile: dest={{win_output_dir}}/test_quoting.txt line='"quote" and "unquote"'
  register: result

- name: assert that the quoted file was changed
  assert:
    that:
    - result.changed

- name: stat the quote test file
  win_stat: path={{win_output_dir}}/test_quoting.txt
  register: result

- name: assert test checksum matches quoted line added
  assert:
    that:
    - "result.stat.checksum == '9dc1fc1ff19942e2936564102ad37134fa83b91d'"


# Windows vs. Unix line separator test cases

- name: Create windows test file with initial line
  win_lineinfile: dest={{win_output_dir}}/test_windows_sep.txt create=yes insertbefore=BOF state=present line="This is a new file"
  register: result

- name: assert that the new file was created
  assert:
    that:
    - "result.changed == true"
    - "result.msg == 'line added'"

- name: validate that the newly created file exists
  win_stat: path={{win_output_dir}}/test_windows_sep.txt
  register: result

- name: assert the newly created file checksum matches
  assert:
    that:
    - "result.stat.checksum == '84faac1183841c57434693752fc3debc91b9195d'"

- name: Test appending to the file using the default (windows) line separator
  win_lineinfile: dest={{win_output_dir}}/test_windows_sep.txt insertbefore=EOF state=present line="This is the last line"
  register: result

- name: assert that the new line was added
  assert:
    that:
    - "result.changed == true"
    - "result.msg == 'line added'"

- name: stat the file
  win_stat: path={{win_output_dir}}/test_windows_sep.txt
  register: result

- name: assert the file checksum matches expected checksum
  assert:
    that:
    - "result.stat.checksum == '71a17ddd1d57ed7c7912e4fd11ecb2ead0b27033'"


- name: Create unix test file with initial line
  win_lineinfile: dest={{win_output_dir}}/test_unix_sep.txt create=yes insertbefore=BOF state=present line="This is a new file"
  register: result

- name: assert that the new file was created
  assert:
    that:
    - "result.changed == true"
    - "result.msg == 'line added'"

- name: validate that the newly created file exists
  win_stat: path={{win_output_dir}}/test_unix_sep.txt
  register: result

- name: assert the newly created file checksum matches
  assert:
    that:
    - "result.stat.checksum == '84faac1183841c57434693752fc3debc91b9195d'"

- name: Test appending to the file using unix line separator
  win_lineinfile: dest={{win_output_dir}}/test_unix_sep.txt insertbefore=EOF state=present line="This is the last line" newline="unix"
  register: result

- name: assert that the new line was added
  assert:
    that:
    - "result.changed == true"
    - "result.msg == 'line added'"

- name: stat the file
  win_stat: path={{win_output_dir}}/test_unix_sep.txt
  register: result

- name: assert the file checksum matches expected checksum
  assert:
    that:
    - "result.stat.checksum == 'f1f634a37ab1c73efb77a71a5ad2cc87b61b17ae'"


# Encoding management test cases

# Default (auto) encoding should use utf-8 with no BOM
- name: Test create file without explicit encoding results in utf-8 without BOM
  win_lineinfile: dest={{win_output_dir}}/test_auto_utf8.txt create=yes insertbefore=BOF state=present line="This is a new utf-8 file"
  register: result

- name: assert that the new file was created
  assert:
    that:
    - "result.changed == true"
    - "result.msg == 'line added'"
    - "result.encoding == 'utf-8'"

- name: validate that the newly created file exists
  win_stat: path={{win_output_dir}}/test_auto_utf8.txt
  register: result

- name: assert the newly created file checksum matches
  assert:
    that:
    - "result.stat.checksum == 'b69fcbacca8291a4668f57fba91d7c022f1c3dc7'"

- name: Test appending to the utf-8 without BOM file - should autodetect UTF-8 no BOM
  win_lineinfile: dest={{win_output_dir}}/test_auto_utf8.txt insertbefore=EOF state=present line="This is the last line"
  register: result

- name: assert that the new line was added and encoding did not change
  assert:
    that:
    - "result.changed == true"
    - "result.msg == 'line added'"
    - "result.encoding == 'utf-8'"

- name: stat the file
  win_stat: path={{win_output_dir}}/test_auto_utf8.txt
  register: result

- name: assert the file checksum matches
  assert:
    that:
    - "result.stat.checksum == '64d747f1ebf8c9d793dbfd27126e4152d39a3848'"


# UTF-8 explicit (with BOM)
- name: Test create file with explicit utf-8 encoding results in utf-8 with a BOM
  win_lineinfile: dest={{win_output_dir}}/test_utf8.txt create=yes encoding="utf-8" insertbefore=BOF state=present line="This is a new utf-8 file"
  register: result

- name: assert that the new file was created
  assert:
    that:
    - "result.changed == true"
    - "result.msg == 'line added'"
    - "result.encoding == 'utf-8'"

- name: validate that the newly created file exists
  win_stat: path={{win_output_dir}}/test_utf8.txt
  register: result

- name: assert the newly created file checksum matches
  assert:
    that:
    - "result.stat.checksum == 'd45344b2b3bf1cf90eae851b40612f5f37a88bbb'"

- name: Test appending to the utf-8 with BOM file - should autodetect utf-8 with BOM encoding
  win_lineinfile: dest={{win_output_dir}}/test_utf8.txt insertbefore=EOF state=present line="This is the last line"
  register: result

- name: assert that the new line was added and encoding did not change
  assert:
    that:
    - "result.changed == true"
    - "result.msg == 'line added'"
    - "result.encoding == 'utf-8'"

- name: stat the file
  win_stat: path={{win_output_dir}}/test_utf8.txt
  register: result

- name: assert the file checksum matches
  assert:
    that:
    - "result.stat.checksum == '9b84254489f40f258871a4c6573cacc65895ee1a'"


# UTF-16 explicit
- name: Test create file with explicit utf-16 encoding
  win_lineinfile: dest={{win_output_dir}}/test_utf16.txt create=yes encoding="utf-16" insertbefore=BOF state=present line="This is a new utf-16 file"
  register: result

- name: assert that the new file was created
  assert:
    that:
    - "result.changed == true"
    - "result.msg == 'line added'"
    - "result.encoding == 'utf-16'"

- name: validate that the newly created file exists
  win_stat: path={{win_output_dir}}/test_utf16.txt
  register: result

- name: assert the newly created file checksum matches
  assert:
    that:
    - "result.stat.checksum == '785b0693cec13b60e2c232782adeda2f8a967434'"

- name: Test appending to the utf-16 file - should autodetect utf-16 encoding
  win_lineinfile: dest={{win_output_dir}}/test_utf16.txt insertbefore=EOF state=present line="This is the last line"
  register: result

- name: assert that the new line was added and encoding did not change
  assert:
    that:
    - "result.changed == true"
    - "result.msg == 'line added'"
    - "result.encoding == 'utf-16'"

- name: stat the file
  win_stat: path={{win_output_dir}}/test_utf16.txt
  register: result

- name: assert the file checksum matches
  assert:
    that:
    - "result.stat.checksum == '70e4eb3ba795e1ba94d262db47e4fd17c64b2e73'"

# UTF-32 explicit
- name: Test create file with explicit utf-32 encoding
  win_lineinfile: dest={{win_output_dir}}/test_utf32.txt create=yes encoding="utf-32" insertbefore=BOF state=present line="This is a new utf-32 file"
  register: result

- name: assert that the new file was created
  assert:
    that:
    - "result.changed == true"
    - "result.msg == 'line added'"
    - "result.encoding == 'utf-32'"

- name: validate that the newly created file exists
  win_stat: path={{win_output_dir}}/test_utf32.txt
  register: result

- name: assert the newly created file checksum matches
  assert:
    that:
    - "result.stat.checksum == '7a6e3f3604c0def431aaa813173a4ddaa10fd1fb'"

- name: Test appending to the utf-32 file - should autodetect utf-32 encoding
  win_lineinfile: dest={{win_output_dir}}/test_utf32.txt insertbefore=EOF state=present line="This is the last line"
  register: result

- name: assert that the new line was added and encoding did not change
  assert:
    that:
    - "result.changed == true"
    - "result.msg == 'line added'"
    - "result.encoding == 'utf-32'"

- name: stat the file
  win_stat: path={{win_output_dir}}/test_utf32.txt
  register: result

- name: assert the file checksum matches
  assert:
    that:
    - "result.stat.checksum == '66a72e71f42c4775f4326da95cfe82c8830e5022'"