Debian Codegraph @

test/integration/targets/apt_repository/tasks/apt.yml

raw
1 - set_fact:
2 test_ppa_name: 'ppa:git-core/ppa'
3 test_ppa_filename: 'git-core'
4 test_ppa_spec: 'deb https://ppa.launchpadcontent.net/git-core/ppa/ubuntu {{ansible_distribution_release}} main'
5 test_ppa_key: 'E1DF1F24' # http://keyserver.ubuntu.com:11371/pks/lookup?search=0xD06AAF4C11DAB86DF421421EFE6B20ECA7AD98A1&op=index
6 python_apt: python3-apt
7
8 - name: verify that comments are preserved unmodified when writing their source file
9 vars:
10 test_source_filename: ansible_test_comment
11 test_source_path: "/etc/apt/sources.list.d/{{ test_source_filename }}.list"
12 block:
13 - name: ensure the test source is absent
14 file:
15 path: "{{ test_source_path }}"
16 state: absent
17
18 - name: add the test PPA to the test source
19 apt_repository:
20 repo: "{{ test_ppa_name }}"
21 filename: "{{ test_source_filename }}"
22 update_cache: false
23 register: add_ppa
24
25 - name: verify the expected test source was added
26 assert:
27 that:
28 - add_ppa.sources_added | length == 1
29 - add_ppa.sources_added[0] == test_source_path
30
31 - name: overwrite the test source with a comment
32 copy:
33 content: "## comment"
34 dest: "{{ test_source_path }}"
35
36 - name: add the test PPA to the test source again
37 apt_repository:
38 repo: "{{ test_ppa_name }}"
39 filename: "{{ test_source_filename }}"
40 update_cache: false
41 register: add_ppa
42
43 - name: verify no sources were added
44 assert:
45 that:
46 - add_ppa.sources_added | length == 0
47
48 - name: read the test source
49 slurp:
50 src: "{{ test_source_path }}"
51 register: test_source
52
53 - name: decode the contents of the test source
54 set_fact:
55 test_source_content: "{{ test_source.content | b64decode }}"
56
57 - name: verify the comment in the test source was preserved
58 assert:
59 that:
60 - '"# # comment\n" in test_source_content' # bug, see: https://github.com/ansible/ansible/issues/54403
61 # - '"## comment\n" in test_source_content' # correct behavior
62 always:
63 - name: ensure the test source is absent
64 file:
65 path: "{{ test_source_path }}"
66 state: absent
67
68 # UNINSTALL 'python-apt'
69 # The `apt_repository` module has the smarts to auto-install `python-apt`. To
70 # test, we will first uninstall `python-apt`.
71 - name: check {{ python_apt }} with dpkg
72 shell: dpkg -s {{ python_apt }}
73 register: dpkg_result
74 ignore_errors: true
75
76 - name: uninstall {{ python_apt }} with apt
77 apt: pkg={{ python_apt }} state=absent purge=yes
78 register: apt_result
79 when: dpkg_result is successful
80
81 #
82 # TEST: apt_repository: repo=<name>
83 #
84 - import_tasks: 'cleanup.yml'
85
86 - name: 'record apt cache mtime'
87 stat: path='/var/cache/apt/pkgcache.bin'
88 register: cache_before
89
90 - name: 'name=<name> (expect: pass)'
91 apt_repository: repo='{{test_ppa_name}}' state=present
92 register: result
93
94 - name: 'assert the apt cache did *NOT* change'
95 assert:
96 that:
97 - 'result.changed'
98 - 'result.state == "present"'
99 - 'result.repo == test_ppa_name'
100
101 - name: 'examine apt cache mtime'
102 stat: path='/var/cache/apt/pkgcache.bin'
103 register: cache_after
104
105 - name: 'assert the apt cache did change'
106 assert:
107 that:
108 - 'cache_before.stat.mtime != cache_after.stat.mtime'
109
110 - name: 'ensure ppa key is installed (expect: pass)'
111 apt_key:
112 id: '{{test_ppa_key}}'
113 state: present
114 keyserver: keyserver.ubuntu.com
115
116 #
117 # TEST: apt_repository: repo=<name> update_cache=no
118 #
119 - import_tasks: 'cleanup.yml'
120
121 - name: 'record apt cache mtime'
122 stat: path='/var/cache/apt/pkgcache.bin'
123 register: cache_before
124
125 - name: 'name=<name> update_cache=no (expect: pass)'
126 apt_repository: repo='{{test_ppa_name}}' state=present update_cache=no
127 register: result
128
129 - assert:
130 that:
131 - 'result.changed'
132 - 'result.state == "present"'
133 - 'result.repo == test_ppa_name'
134
135 - name: 'examine apt cache mtime'
136 stat: path='/var/cache/apt/pkgcache.bin'
137 register: cache_after
138
139 - name: 'assert the apt cache did *NOT* change'
140 assert:
141 that:
142 - 'cache_before.stat.mtime == cache_after.stat.mtime'
143
144 - name: 'ensure ppa key is installed (expect: pass)'
145 apt_key:
146 id: '{{test_ppa_key}}'
147 state: present
148 keyserver: keyserver.ubuntu.com
149
150 #
151 # TEST: apt_repository: repo=<name> update_cache=yes
152 #
153 - import_tasks: 'cleanup.yml'
154
155 - name: 'record apt cache mtime'
156 stat: path='/var/cache/apt/pkgcache.bin'
157 register: cache_before
158
159 - name: 'name=<name> update_cache=yes (expect: pass)'
160 apt_repository: repo='{{test_ppa_name}}' state=present update_cache=yes
161 register: result
162
163 - assert:
164 that:
165 - 'result.changed'
166 - 'result.state == "present"'
167 - 'result.repo == test_ppa_name'
168
169 - name: 'examine apt cache mtime'
170 stat: path='/var/cache/apt/pkgcache.bin'
171 register: cache_after
172
173 - name: 'assert the apt cache did change'
174 assert:
175 that:
176 - 'cache_before.stat.mtime != cache_after.stat.mtime'
177
178 - name: 'ensure ppa key is installed (expect: pass)'
179 apt_key:
180 id: '{{test_ppa_key}}'
181 state: present
182 keyserver: keyserver.ubuntu.com
183
184 #
185 # TEST: apt_repository: repo=<spec>
186 #
187 - import_tasks: 'cleanup.yml'
188
189 - name: 'record apt cache mtime'
190 stat: path='/var/cache/apt/pkgcache.bin'
191 register: cache_before
192
193 - name: ensure ppa key is present before adding repo that requires authentication
194 apt_key:
195 id: '{{test_ppa_key}}'
196 state: present
197 keyserver: keyserver.ubuntu.com
198
199 - name: 'name=<spec> (expect: pass)'
200 apt_repository: repo='{{test_ppa_spec}}' state=present
201 register: result
202
203 - name: update the cache
204 apt:
205 update_cache: true
206 register: result_cache
207
208 - assert:
209 that:
210 - 'result.changed'
211 - 'result.state == "present"'
212 - 'result.repo == test_ppa_spec'
213 - '"sources_added" in result'
214 - 'result.sources_added | length == 1'
215 - '"git" in result.sources_added[0]'
216 - '"sources_removed" in result'
217 - 'result.sources_removed | length == 0'
218 - result_cache is not changed
219
220 - name: 'examine apt cache mtime'
221 stat: path='/var/cache/apt/pkgcache.bin'
222 register: cache_after
223
224 - name: 'assert the apt cache did change'
225 assert:
226 that:
227 - 'cache_before.stat.mtime != cache_after.stat.mtime'
228
229 - name: remove repo by spec
230 apt_repository: repo='{{test_ppa_spec}}' state=absent
231 register: result
232
233 - assert:
234 that:
235 - 'result.changed'
236 - 'result.state == "absent"'
237 - 'result.repo == test_ppa_spec'
238 - '"sources_added" in result'
239 - 'result.sources_added | length == 0'
240 - '"sources_removed" in result'
241 - 'result.sources_removed | length == 1'
242 - '"git" in result.sources_removed[0]'
243
244 # When installing a repo with the spec, the key is *NOT* added
245 - name: 'ensure ppa key is absent (expect: pass)'
246 apt_key: id='{{test_ppa_key}}' state=absent
247
248 #
249 # TEST: apt_repository: repo=<spec> filename=<filename>
250 #
251 - import_tasks: 'cleanup.yml'
252
253 - name: 'record apt cache mtime'
254 stat: path='/var/cache/apt/pkgcache.bin'
255 register: cache_before
256
257 - name: ensure ppa key is present before adding repo that requires authentication
258 apt_key:
259 id: '{{test_ppa_key}}'
260 state: present
261 keyserver: keyserver.ubuntu.com
262
263 - name: 'name=<spec> filename=<filename> (expect: pass)'
264 apt_repository: repo='{{test_ppa_spec}}' filename='{{test_ppa_filename}}' state=present
265 register: result
266
267 - assert:
268 that:
269 - 'result.changed'
270 - 'result.state == "present"'
271 - 'result.repo == test_ppa_spec'
272
273 - name: 'examine source file'
274 stat: path='/etc/apt/sources.list.d/{{test_ppa_filename}}.list'
275 register: source_file
276
277 - name: 'assert source file exists'
278 assert:
279 that:
280 - 'source_file.stat.exists == True'
281
282 - name: 'examine apt cache mtime'
283 stat: path='/var/cache/apt/pkgcache.bin'
284 register: cache_after
285
286 - name: 'assert the apt cache did change'
287 assert:
288 that:
289 - 'cache_before.stat.mtime != cache_after.stat.mtime'
290
291 # When installing a repo with the spec, the key is *NOT* added
292 - name: 'ensure ppa key is absent (expect: pass)'
293 apt_key: id='{{test_ppa_key}}' state=absent
294
295 - name: Test apt_repository with a null value for repo
296 apt_repository:
297 repo:
298 register: result
299 ignore_errors: yes
300
301 - assert:
302 that:
303 - result is failed
304 - result.msg == 'Please set argument \'repo\' to a non-empty value'
305
306 - name: Test apt_repository with an empty value for repo
307 apt_repository:
308 repo: ""
309 register: result
310 ignore_errors: yes
311
312 - assert:
313 that:
314 - result is failed
315 - result.msg == 'Please set argument \'repo\' to a non-empty value'
316
317 #
318 # TEST: keep symlink
319 #
320 - import_tasks: 'cleanup.yml'
321
322 - name: install local-apt-repository with apt
323 apt: pkg=local-apt-repository state=present
324
325 - name: Check if local apt repo file is a symlink
326 stat:
327 path: /etc/apt/sources.list.d/local-apt-repository.list
328 register: stat_result
329
330 - name: Assert if local apt repo file is a symlink
331 assert:
332 that:
333 - stat_result.stat.islnk is defined and stat_result.stat.islnk
334 - stat_result.stat.lnk_source == "/usr/lib/local-apt-repository/local-apt-repository.list"
335
336 - name: Try installing an invalid repo
337 apt_repository:
338 repo: deb http://dl.google.com/linux/chrome/deb2/ stable main
339 state: present
340 filename: google-chrome
341 ignore_errors: true
342
343 - name: Check the stat for the given symlink
344 stat:
345 path: /etc/apt/sources.list.d/local-apt-repository.list
346 register: stat_result2
347
348 - name: Assert that the symlink is intact after apt_repository operation
349 assert:
350 that:
351 - stat_result2.stat.islnk is defined and stat_result2.stat.islnk
352 - stat_result2.stat.lnk_source == "/usr/lib/local-apt-repository/local-apt-repository.list"
353
354 - name: uninstall local-apt-repository with apt
355 apt: pkg=local-apt-repository state=absent purge=yes
356
357 # Invalid repo with no component
358 - name: Add repo with empty component
359 ansible.builtin.apt_repository:
360 repo: "deb http://archive.canonical.com/ubuntu jammy"
361 state: present
362 register: emptycomp_result
363 ignore_errors: true
364
365 - name: Assert that the repo was not added
366 assert:
367 that:
368 - emptycomp_result is failed
369 - "'Invalid repository string' in emptycomp_result.msg"
370
371 #
372 # TEST: PPA HTTPS URL
373 #
374 - name: Add PPA using HTTPS URL
375 apt_repository:
376 repo: 'ppa:deadsnakes'
377 filename: 'deadsnakes'
378 state: present
379 register: result
380
381 - name: Check if PPA using HTTPS URL is added
382 assert:
383 that:
384 - 'result.changed'
385 - 'result.state == "present"'
386 - 'result.repo == "ppa:deadsnakes"'
387
388 - name: 'examine source file'
389 stat:
390 path: '/etc/apt/sources.list.d/deadsnakes.list'
391 register: source_file
392
393 - name: 'assert source file exists'
394 assert:
395 that:
396 - 'source_file.stat.exists == True'
397
398 - name: Check if the PPA URL
399 shell: "grep 'https://ppa.launchpadcontent.net' /etc/apt/sources.list.d/deadsnakes.list"
400 register: r
401
402 - name: Test if PPA URL points to https URL
403 assert:
404 that:
405 - r.changed
406 - "'https://ppa.launchpadcontent.net' in r.stdout"
407
408 - name: Remove PPA file
409 file:
410 path: '/etc/apt/sources.list.d/deadsnakes.list'
411 state: absent
412 #
413 # TEARDOWN
414 #
415 - import_tasks: 'cleanup.yml'