summaryrefslogtreecommitdiff
path: root/test/integration/targets/archive/tasks/main.yml
blob: bf3530b3aa174ce083e0e5abb952a8ae7ed2e07f (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
# Test code for the archive module.
# (c) 2017, Abhijeet Kasurde <akasurde@redhat.com>

# 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/>.
# Make sure we start fresh

- name: Ensure zip is present to create test archive (yum)
  yum: name=zip state=latest
  when: ansible_pkg_mgr == 'yum'

- name: Ensure zip is present to create test archive (apt)
  apt: name=zip state=latest
  when: ansible_pkg_mgr == 'apt'

- name: Install prerequisites for backports.lzma when using python2 (non OSX)
  block:
    - name: Set liblzma package name depending on the OS
      set_fact:
        liblzma_dev_package:
          Debian: liblzma-dev
          RedHat: xz-devel
          Suse: xz-devel
    - name: Ensure liblzma-dev is present to install backports-lzma
      package: name={{ liblzma_dev_package[ansible_os_family] }} state=latest
      when: ansible_os_family in liblzma_dev_package.keys()
  when:
    - ansible_python_version.split('.')[0] == '2'
    - ansible_os_family != 'Darwin'

- name: Install prerequisites for backports.lzma when using python2 (OSX)
  block:
    - name: Find brew binary
      command: which brew
      register: brew_which
    - name: Get owner of brew binary
      stat: path="{{ brew_which.stdout }}"
      register: brew_stat
    - name: "Install package"
      homebrew:
        name: xz
        state: present
        update_homebrew: no
      become: yes
      become_user: "{{ brew_stat.stat.pw_name }}"
      # Newer versions of brew want to compile a package which takes a long time. Do not upgrade homebrew until a
      # proper solution can be found
      environment:
        HOMEBREW_NO_AUTO_UPDATE: True
  when:
    - ansible_python_version.split('.')[0] == '2'
    - ansible_os_family == 'Darwin'

- name: Ensure backports.lzma is present to create test archive (pip)
  pip: name=backports.lzma state=latest
  when: ansible_python_version.split('.')[0] == '2'
  register: backports_lzma_pip

- name: prep our file
  copy: src={{ item }} dest={{output_dir}}/{{ item }}
  with_items:
    - foo.txt
    - bar.txt

- name: archive using gz
  archive:
    path: "{{ output_dir }}/*.txt"
    dest: "{{ output_dir }}/archive_01.gz"
    format: gz
  register: archive_gz_result_01

- debug: msg="{{ archive_gz_result_01 }}"

- name: verify that the files archived
  file: path={{output_dir}}/archive_01.gz state=file

- name: check if gz file exists
  assert:
    that:
      - "{{ archive_gz_result_01.changed }}"
      - "{{ 'archived' in archive_gz_result_01 }}"
      - "{{ archive_gz_result_01['archived'] | length }} == 2"

- name: archive using zip
  archive:
    path: "{{ output_dir }}/*.txt"
    dest: "{{ output_dir }}/archive_01.zip"
    format: zip
  register: archive_zip_result_01

- debug: msg="{{ archive_zip_result_01 }}"

- name: verify that the files archived
  file: path={{output_dir}}/archive_01.zip state=file

- name: check if zip file exists
  assert:
    that:
      - "{{ archive_zip_result_01.changed }}"
      - "{{ 'archived' in archive_zip_result_01 }}"
      - "{{ archive_zip_result_01['archived'] | length }} == 2"

- name: archive using bz2
  archive:
    path: "{{ output_dir }}/*.txt"
    dest: "{{ output_dir }}/archive_01.bz2"
    format: bz2
  register: archive_bz2_result_01

- debug: msg="{{ archive_bz2_result_01 }}"

- name: verify that the files archived
  file: path={{output_dir}}/archive_01.bz2 state=file

- name: check if bzip file exists
  assert:
    that:
      - "{{ archive_bz2_result_01.changed }}"
      - "{{ 'archived' in archive_bz2_result_01 }}"
      - "{{ archive_bz2_result_01['archived'] | length }} == 2"

- name: archive using xz
  archive:
    path: "{{ output_dir }}/*.txt"
    dest: "{{ output_dir }}/archive_01.xz"
    format: xz
  register: archive_xz_result_01

- debug: msg="{{ archive_xz_result_01 }}"

- name: verify that the files archived
  file: path={{output_dir}}/archive_01.xz state=file

- name: check if xz file exists
  assert:
    that:
      - "{{ archive_xz_result_01.changed }}"
      - "{{ 'archived' in archive_xz_result_01 }}"
      - "{{ archive_xz_result_01['archived'] | length }} == 2"

- name: archive and set mode to 0600
  archive:
    path: "{{ output_dir }}/*.txt"
    dest: "{{ output_dir }}/archive_02.gz"
    format: gz
    mode: "u+rwX,g-rwx,o-rwx"
  register: archive_bz2_result_02

- name: Test that the file modes were changed
  stat:
    path: "{{ output_dir }}/archive_02.gz"
  register: archive_02_gz_stat

- debug: msg="{{ archive_02_gz_stat}}"

- name: Test that the file modes were changed
  assert:
    that:
      - "archive_02_gz_stat.changed == False "
      - "archive_02_gz_stat.stat.mode == '0600'"
      - "'archived' in archive_bz2_result_02"
      - "{{ archive_bz2_result_02['archived']| length}} == 2"

- name: remove our gz
  file: path="{{ output_dir }}/archive_02.gz" state=absent


- name: archive and set mode to 0600
  archive:
    path: "{{ output_dir }}/*.txt"
    dest: "{{ output_dir }}/archive_02.zip"
    format: zip
    mode: "u+rwX,g-rwx,o-rwx"
  register: archive_zip_result_02

- name: Test that the file modes were changed
  stat:
    path: "{{ output_dir }}/archive_02.zip"
  register: archive_02_zip_stat

- name: Test that the file modes were changed
  assert:
    that:
      - "archive_02_zip_stat.changed == False"
      - "archive_02_zip_stat.stat.mode == '0600'"
      - "'archived' in archive_zip_result_02"
      - "{{ archive_zip_result_02['archived']| length}} == 2"

- name: remove our zip
  file: path="{{ output_dir }}/archive_02.zip" state=absent


- name: archive and set mode to 0600
  archive:
    path: "{{ output_dir }}/*.txt"
    dest: "{{ output_dir }}/archive_02.bz2"
    format: bz2
    mode: "u+rwX,g-rwx,o-rwx"
  register: archive_bz2_result_02

- name: Test that the file modes were changed
  stat:
    path: "{{ output_dir }}/archive_02.bz2"
  register: archive_02_bz2_stat

- name: Test that the file modes were changed
  assert:
    that:
      - "archive_02_bz2_stat.changed == False"
      - "archive_02_bz2_stat.stat.mode == '0600'"
      - "'archived' in archive_bz2_result_02"
      - "{{ archive_bz2_result_02['archived']| length}} == 2"

- name: remove our bz2
  file: path="{{ output_dir }}/archive_02.bz2" state=absent

- name: archive and set mode to 0600
  archive:
    path: "{{ output_dir }}/*.txt"
    dest: "{{ output_dir }}/archive_02.xz"
    format: xz
    mode: "u+rwX,g-rwx,o-rwx"
  register: archive_xz_result_02

- name: Test that the file modes were changed
  stat:
    path: "{{ output_dir }}/archive_02.xz"
  register: archive_02_xz_stat

- name: Test that the file modes were changed
  assert:
    that:
      - "archive_02_xz_stat.changed == False"
      - "archive_02_xz_stat.stat.mode == '0600'"
      - "'archived' in archive_xz_result_02"
      - "{{ archive_xz_result_02['archived']| length}} == 2"

- name: remove our xz
  file: path="{{ output_dir }}/archive_02.xz" state=absent

- name: test that gz archive that contains non-ascii filenames
  archive:
    path: "{{ output_dir }}/*.txt"
    dest: "{{ output_dir }}/test-archive-nonascii-くらとみ.tar.gz"
    format: gz
  register: nonascii_result_0

- name: Check that file is really there
  stat:
    path: "{{ output_dir }}/test-archive-nonascii-くらとみ.tar.gz"
  register: nonascii_stat0

- name: Assert that nonascii tests succeeded
  assert:
    that:
      - "nonascii_result_0.changed == true"
      - "nonascii_stat0.stat.exists == true"

- name: remove nonascii test
  file: path="{{ output_dir }}/test-archive-nonascii-くらとみ.tar.gz" state=absent

- name: test that bz2 archive that contains non-ascii filenames
  archive:
    path: "{{ output_dir }}/*.txt"
    dest: "{{ output_dir }}/test-archive-nonascii-くらとみ.bz2"
    format: bz2
  register: nonascii_result_1

- name: Check that file is really there
  stat:
    path: "{{ output_dir }}/test-archive-nonascii-くらとみ.bz2"
  register: nonascii_stat_1

- name: Assert that nonascii tests succeeded
  assert:
    that:
      - "nonascii_result_1.changed == true"
      - "nonascii_stat_1.stat.exists == true"

- name: remove nonascii test
  file: path="{{ output_dir }}/test-archive-nonascii-くらとみ.bz2" state=absent

- name: test that xz archive that contains non-ascii filenames
  archive:
    path: "{{ output_dir }}/*.txt"
    dest: "{{ output_dir }}/test-archive-nonascii-くらとみ.xz"
    format: xz
  register: nonascii_result_1

- name: Check that file is really there
  stat:
    path: "{{ output_dir }}/test-archive-nonascii-くらとみ.xz"
  register: nonascii_stat_1

- name: Assert that nonascii tests succeeded
  assert:
    that:
      - "nonascii_result_1.changed == true"
      - "nonascii_stat_1.stat.exists == true"

- name: remove nonascii test
  file: path="{{ output_dir }}/test-archive-nonascii-くらとみ.xz" state=absent

- name: test that zip archive that contains non-ascii filenames
  archive:
    path: "{{ output_dir }}/*.txt"
    dest: "{{ output_dir }}/test-archive-nonascii-くらとみ.zip"
    format: zip
  register: nonascii_result_2

- name: Check that file is really there
  stat:
    path: "{{ output_dir }}/test-archive-nonascii-くらとみ.zip"
  register: nonascii_stat_2

- name: Assert that nonascii tests succeeded
  assert:
    that:
      - "nonascii_result_2.changed == true"
      - "nonascii_stat_2.stat.exists == true"

- name: remove nonascii test
  file: path="{{ output_dir }}/test-archive-nonascii-くらとみ.zip" state=absent

- name: Remove backports.lzma if previously installed (pip)
  pip: name=backports.lzma state=absent
  when: backports_lzma_pip is changed