Debian Codegraph @

test/integration/targets/pip/tasks/pip.yml

raw
1 # test code for the pip module
2 # (c) 2014, Michael DeHaan <[email protected]>
3
4 # This file is part of Ansible
5 #
6 # Ansible is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # Ansible is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with Ansible. If not, see <http://www.gnu.org/licenses/>.
18
19 # FIXME: replace the python test package
20
21 # first some tests installed system-wide
22 # verify things were not installed to start with
23
24 - name: ensure packages are not installed (precondition setup)
25 pip:
26 name: "{{ pip_test_packages }}"
27 state: absent
28
29 # verify that a package that is uninstalled being set to absent
30 # results in an unchanged state and that the test package is not
31 # installed
32
33 - name: ensure packages are not installed
34 pip:
35 name: "{{ pip_test_packages }}"
36 state: absent
37 register: uninstall_result
38
39 - name: removing unremoved packages should return unchanged
40 assert:
41 that:
42 - "not (uninstall_result is changed)"
43
44 - command: "{{ ansible_python.executable }} -c 'import {{ item }}'"
45 register: absent_result
46 failed_when: "absent_result.rc == 0"
47 loop: '{{ pip_test_modules }}'
48
49 # now we're going to install the test package knowing it is uninstalled
50 # and check that installation was ok
51
52 - name: ensure packages are installed
53 pip:
54 name: "{{ pip_test_packages }}"
55 state: present
56 register: install_result
57
58 - name: verify we recorded a change
59 assert:
60 that:
61 - "install_result is changed"
62
63 - command: "{{ ansible_python.executable }} -c 'import {{ item }}'"
64 loop: '{{ pip_test_modules }}'
65
66 # now remove it to test uninstallation of a package we are sure is installed
67
68 - name: now uninstall so we can see that a change occurred
69 pip:
70 name: "{{ pip_test_packages }}"
71 state: absent
72 register: absent2
73
74 - name: assert a change occurred on uninstallation
75 assert:
76 that:
77 - "absent2 is changed"
78
79 # put the test packages back
80
81 - name: now put it back in case someone wanted it (like us!)
82 pip:
83 name: "{{ pip_test_packages }}"
84 state: present
85
86 # Test virtualenv installations
87
88 - name: "make sure the test env doesn't exist"
89 file:
90 state: absent
91 name: "{{ remote_tmp_dir }}/pipenv"
92
93 - name: create a requirement file with an vcs url
94 copy:
95 dest: "{{ remote_tmp_dir }}/pipreq.txt"
96 content: "-e git+https://github.com/dvarrazzo/pyiso8601#egg=iso8601"
97
98 - name: install the requirement file in a virtualenv
99 pip:
100 requirements: "{{ remote_tmp_dir}}/pipreq.txt"
101 virtualenv: "{{ remote_tmp_dir }}/pipenv"
102 register: req_installed
103
104 - name: check that a change occurred
105 assert:
106 that:
107 - "req_installed is changed"
108
109 - name: "repeat installation to check status didn't change"
110 pip:
111 requirements: "{{ remote_tmp_dir}}/pipreq.txt"
112 virtualenv: "{{ remote_tmp_dir }}/pipenv"
113 register: req_installed
114
115 - name: "check that a change didn't occur this time (bug ansible#1705)"
116 assert:
117 that:
118 - "not (req_installed is changed)"
119
120 - name: install the same module from url
121 pip:
122 name: "git+https://github.com/dvarrazzo/pyiso8601#egg=iso8601"
123 virtualenv: "{{ remote_tmp_dir }}/pipenv"
124 editable: True
125 register: url_installed
126
127 - name: "check that a change didn't occur (bug ansible-modules-core#1645)"
128 assert:
129 that:
130 - "not (url_installed is changed)"
131
132 # Test pip package in check mode doesn't always report changed.
133
134 # Special case for pip
135 - name: check for pip package
136 pip:
137 name: pip
138 virtualenv: "{{ remote_tmp_dir }}/pipenv"
139 state: present
140
141 - name: check for pip package in check_mode
142 pip:
143 name: pip
144 virtualenv: "{{ remote_tmp_dir }}/pipenv"
145 state: present
146 check_mode: True
147 register: pip_check_mode
148
149 - name: make sure pip in check_mode doesn't report changed
150 assert:
151 that:
152 - "not (pip_check_mode is changed)"
153
154 # Special case for setuptools
155 - name: check for setuptools package
156 pip:
157 name: setuptools
158 virtualenv: "{{ remote_tmp_dir }}/pipenv"
159 state: present
160
161 - name: check for setuptools package in check_mode
162 pip:
163 name: setuptools
164 virtualenv: "{{ remote_tmp_dir }}/pipenv"
165 state: present
166 check_mode: True
167 register: setuptools_check_mode
168
169 - name: make sure setuptools in check_mode doesn't report changed
170 assert:
171 that:
172 - "not (setuptools_check_mode is changed)"
173
174
175 # Normal case
176 - name: check for q package
177 pip:
178 name: q
179 virtualenv: "{{ remote_tmp_dir }}/pipenv"
180 state: present
181
182 - name: check for q package in check_mode
183 pip:
184 name: q
185 virtualenv: "{{ remote_tmp_dir }}/pipenv"
186 state: present
187 check_mode: True
188 register: q_check_mode
189
190 - name: make sure q in check_mode doesn't report changed
191 assert:
192 that:
193 - "not (q_check_mode is changed)"
194
195 # Case with package name that has a different package name case and an
196 # underscore instead of a hyphen
197 - name: check for Junit-XML package
198 pip:
199 name: Junit-XML
200 virtualenv: "{{ remote_tmp_dir }}/pipenv"
201 state: present
202
203 - name: check for Junit-XML package in check_mode
204 pip:
205 name: Junit-XML
206 virtualenv: "{{ remote_tmp_dir }}/pipenv"
207 state: present
208 check_mode: True
209 register: diff_case_check_mode
210
211 - name: make sure Junit-XML in check_mode doesn't report changed
212 assert:
213 that:
214 - "diff_case_check_mode is not changed"
215
216 - name: ensure is a fresh venv
217 file:
218 state: absent
219 name: "{{ remote_tmp_dir }}/emptyvenv"
220
221 - name: create an empty venv
222 pip:
223 name: []
224 virtualenv: "{{ remote_tmp_dir }}/emptyvenv"
225 register: empty_venv
226
227 - name: check venv was created
228 file:
229 name: "{{ remote_tmp_dir }}/emptyvenv"
230 register: check_empty_venv_exists
231
232 - name: try creating an empty venv so it isn't changed
233 pip:
234 name: []
235 virtualenv: "{{ remote_tmp_dir }}/emptyvenv"
236 register: second_empty_venv
237
238 - name: assert that the venv was created and has no packages
239 assert:
240 that:
241 - empty_venv.changed
242 - not check_empty_venv_exists.changed
243 - not second_empty_venv.changed
244
245 # ansible#23204
246 - name: ensure is a fresh virtualenv
247 file:
248 state: absent
249 name: "{{ remote_tmp_dir }}/pipenv"
250
251 - name: install pip through pip into fresh virtualenv
252 pip:
253 name: pip
254 virtualenv: "{{ remote_tmp_dir }}/pipenv"
255 register: pip_install_venv
256
257 - name: make sure pip in fresh virtualenv reports changed
258 assert:
259 that:
260 - "pip_install_venv is changed"
261
262 # https://github.com/ansible/ansible/issues/37912
263 # support chdir without virtualenv
264 - name: create chdir test directories
265 file:
266 state: directory
267 name: "{{ remote_tmp_dir }}/{{ item }}"
268 loop:
269 - pip_module
270 - pip_root
271 - pip_module/ansible_test_pip_chdir
272
273 - name: copy test module
274 copy:
275 src: "{{ item }}"
276 dest: "{{ remote_tmp_dir }}/pip_module/{{ item }}"
277 loop:
278 - setup.py
279 - ansible_test_pip_chdir/__init__.py
280
281 - name: install test module
282 pip:
283 name: .
284 chdir: "{{ remote_tmp_dir }}/pip_module"
285 extra_args: --user --upgrade --root {{ remote_tmp_dir }}/pip_root
286
287 - name: register python_site_lib
288 command: '{{ ansible_python.executable }} -c "import site; print(site.USER_SITE)"'
289 register: pip_python_site_lib
290
291 - name: register python_user_base
292 command: '{{ ansible_python.executable }} -c "import site; print(site.USER_BASE)"'
293 register: pip_python_user_base
294
295 - name: run test module
296 shell: "PYTHONPATH=$(echo {{ remote_tmp_dir }}/pip_root{{ pip_python_site_lib.stdout }}) {{ remote_tmp_dir }}/pip_root{{ pip_python_user_base.stdout }}/bin/ansible_test_pip_chdir"
297 register: pip_chdir_command
298
299 - name: make sure command ran
300 assert:
301 that:
302 - pip_chdir_command.stdout == "success"
303
304 # https://github.com/ansible/ansible/issues/25122
305 - name: ensure is a fresh virtualenv
306 file:
307 state: absent
308 name: "{{ remote_tmp_dir }}/pipenv"
309
310 - name: install requirements file into virtual + chdir
311 pip:
312 name: q
313 chdir: "{{ remote_tmp_dir }}/"
314 virtualenv: "pipenv"
315 state: present
316 register: venv_chdir
317
318 - name: make sure fresh virtualenv + chdir report changed
319 assert:
320 that:
321 - "venv_chdir is changed"
322
323 # ansible#38785
324 - name: allow empty list of packages
325 pip:
326 name: []
327 register: pip_install_empty
328
329 - name: ensure empty install is successful
330 assert:
331 that:
332 - "not (pip_install_empty is changed)"
333
334 # https://github.com/ansible/ansible/issues/41043
335 - block:
336 - name: Ensure previous virtualenv no longer exists
337 file:
338 state: absent
339 name: "{{ remote_tmp_dir }}/pipenv"
340
341 - name: do not consider an empty string as a version
342 pip:
343 name: q
344 state: present
345 version: ""
346 virtualenv: "{{ remote_tmp_dir }}/pipenv"
347 register: pip_empty_version_string
348
349 - name: test idempotency with empty string
350 pip:
351 name: q
352 state: present
353 version: ""
354 virtualenv: "{{ remote_tmp_dir }}/pipenv"
355 register: pip_empty_version_string_idempotency
356
357 - name: test idempotency without empty string
358 pip:
359 name: q
360 state: present
361 virtualenv: "{{ remote_tmp_dir }}/pipenv"
362 register: pip_no_empty_version_string_idempotency
363
364 # 'present' and version=="" is analogous to latest when first installed
365 - name: ensure we installed the latest version
366 pip:
367 name: q
368 state: latest
369 virtualenv: "{{ remote_tmp_dir }}/pipenv"
370 register: pip_empty_version_idempotency
371
372 - name: ensure that installation worked and is idempotent
373 assert:
374 that:
375 - pip_empty_version_string is changed
376 - pip_empty_version_string is successful
377 - pip_empty_version_idempotency is not changed
378 - pip_no_empty_version_string_idempotency is not changed
379 - pip_empty_version_string_idempotency is not changed
380
381 # test version specifiers
382 - name: make sure no test_package installed now
383 pip:
384 name: "{{ pip_test_packages }}"
385 state: absent
386
387 - name: install package with version specifiers
388 pip:
389 name: "{{ pip_test_package }}"
390 version: "<100,!=1.0,>0.0.0"
391 register: version
392
393 - name: assert package installed correctly
394 assert:
395 that: "version.changed"
396
397 - name: reinstall package
398 pip:
399 name: "{{ pip_test_package }}"
400 version: "<100,!=1.0,>0.0.0"
401 register: version2
402
403 - name: assert no changes occurred
404 assert:
405 that: "not version2.changed"
406
407 - name: test the check_mod
408 pip:
409 name: "{{ pip_test_package }}"
410 version: "<100,!=1.0,>0.0.0"
411 check_mode: yes
412 register: version3
413
414 - name: assert no changes
415 assert:
416 that: "not version3.changed"
417
418 - name: test the check_mod with unsatisfied version
419 pip:
420 name: "{{ pip_test_package }}"
421 version: ">100.0.0"
422 check_mode: yes
423 register: version4
424
425 - name: assert changed
426 assert:
427 that: "version4.changed"
428
429 - name: uninstall test packages for next test
430 pip:
431 name: "{{ pip_test_packages }}"
432 state: absent
433
434 - name: test invalid combination of arguments
435 pip:
436 name: "{{ pip_test_pkg_ver }}"
437 version: "1.11.1"
438 ignore_errors: yes
439 register: version5
440
441 - name: assert the invalid combination should fail
442 assert:
443 that: "version5 is failed"
444
445 - name: another invalid combination of arguments
446 pip:
447 name: "{{ pip_test_pkg_ver[0] }}"
448 version: "<100.0.0"
449 ignore_errors: yes
450 register: version6
451
452 - name: assert invalid combination should fail
453 assert:
454 that: "version6 is failed"
455
456 - name: try to install invalid package
457 pip:
458 name: "{{ pip_test_pkg_ver_unsatisfied }}"
459 ignore_errors: yes
460 register: version7
461
462 - name: assert install should fail
463 assert:
464 that: "version7 is failed"
465
466 - name: test install multi-packages with version specifiers
467 pip:
468 name: "{{ pip_test_pkg_ver }}"
469 register: version8
470
471 - name: assert packages installed correctly
472 assert:
473 that: "version8.changed"
474
475 - name: test install multi-packages with check_mode
476 pip:
477 name: "{{ pip_test_pkg_ver }}"
478 check_mode: yes
479 register: version9
480
481 - name: assert no change
482 assert:
483 that: "not version9.changed"
484
485 - name: test install unsatisfied multi-packages with check_mode
486 pip:
487 name: "{{ pip_test_pkg_ver_unsatisfied }}"
488 check_mode: yes
489 register: version10
490
491 - name: assert changes needed
492 assert:
493 that: "version10.changed"
494
495 - name: uninstall packages for next test
496 pip:
497 name: "{{ pip_test_packages }}"
498 state: absent
499
500 - name: test install multi package provided by one single string
501 pip:
502 name: "{{pip_test_pkg_ver[0]}},{{pip_test_pkg_ver[1]}}"
503 register: version11
504
505 - name: assert the install ran correctly
506 assert:
507 that: "version11.changed"
508
509 - name: test install multi package provided by one single string with check_mode
510 pip:
511 name: "{{pip_test_pkg_ver[0]}},{{pip_test_pkg_ver[1]}}"
512 check_mode: yes
513 register: version12
514
515 - name: assert no changes needed
516 assert:
517 that: "not version12.changed"
518
519 - name: test module can parse the combination of multi-packages one line and git url
520 pip:
521 name:
522 - git+https://github.com/dvarrazzo/pyiso8601#egg=iso8601
523 - "{{pip_test_pkg_ver[0]}},{{pip_test_pkg_ver[1]}}"
524
525 - name: test the invalid package name
526 pip:
527 name: djan=+-~!@#$go>1.11.1,<1.11.3
528 ignore_errors: yes
529 register: version13
530
531 - name: the invalid package should make module failed
532 assert:
533 that: "version13 is failed"
534
535 - name: try install package with setuptools extras
536 pip:
537 name:
538 - "{{pip_test_package}}[test]"
539
540 - name: clean up
541 pip:
542 name: "{{ pip_test_packages }}"
543 state: absent
544
545 - name: Test virtualenv command with venv formatting
546 block:
547 - name: Clean up the virtualenv
548 file:
549 state: absent
550 name: "{{ remote_tmp_dir }}/pipenv"
551
552 # ref: https://github.com/ansible/ansible/issues/76372
553 - name: install using different venv formatting
554 pip:
555 name: "{{ pip_test_package }}"
556 virtualenv: "{{ remote_tmp_dir }}/pipenv"
557 virtualenv_command: "{{ ansible_python_interpreter ~ ' -mvenv' }}"
558 state: present
559 register: version14
560
561 - name: ensure install using virtualenv_command with venv formatting
562 assert:
563 that:
564 - "version14 is changed"
565
566 # https://github.com/ansible/ansible/issues/68592
567 # Handle pre-release version numbers in check_mode for already-installed
568 # packages.
569 - block:
570 - name: Install a pre-release version of a package
571 pip:
572 name: fallible
573 version: 0.0.1a2
574 state: present
575
576 - name: Use check_mode and ensure that the package is shown as installed
577 check_mode: true
578 pip:
579 name: fallible
580 state: present
581 register: pip_prereleases
582
583 - name: Uninstall the pre-release package if we need to
584 pip:
585 name: fallible
586 version: 0.0.1a2
587 state: absent
588 when: pip_prereleases is changed
589
590 - assert:
591 that:
592 - pip_prereleases is successful
593 - pip_prereleases is not changed
594 - '"fallible==0.0.1a2" in pip_prereleases.stdout_lines'
595
596 - name: Test editable
597 vars:
598 venv_path: "{{ remote_tmp_dir }}/emptyvenv"
599 block:
600 - name: Ensure fresh venv
601 file:
602 state: absent
603 path: "{{ venv_path }}"
604
605 - name: Create an empty venv
606 pip:
607 name: []
608 virtualenv: "{{ venv_path }}"
609 register: empty_venv
610
611 - name: Create test package directories
612 file:
613 state: directory
614 name: "{{ remote_tmp_dir }}/{{ item }}"
615 loop:
616 - sample-project
617 - another-project
618
619 - name: Copy test packages to remote
620 copy:
621 src: "{{ item.src }}"
622 dest: "{{ item.dest }}"
623 loop:
624 - { src: "sample-project/", dest: "{{ remote_tmp_dir }}/sample-project/" }
625 - { src: "sample-project/", dest: "{{ remote_tmp_dir }}/another-project/" } # Reusing same source for simplicity
626
627 - name: Update project name in another-project's pyproject.toml
628 lineinfile:
629 path: "{{ remote_tmp_dir }}/another-project/pyproject.toml"
630 regexp: '^name = "sample-project"$'
631 line: 'name = "another-project"'
632
633 - name: Install a package normally
634 pip:
635 name: "{{ remote_tmp_dir }}/sample-project"
636 virtualenv: "{{ venv_path }}"
637
638 - name: Install a package as editable
639 pip:
640 name: "{{ remote_tmp_dir }}/sample-project"
641 virtualenv: "{{ venv_path }}"
642 editable: True
643 register: editable_install
644
645 - name: Assert changed
646 assert:
647 that:
648 - editable_install is changed
649
650 - name: Check status
651 command: "{{ venv_path }}/bin/pip list"
652 register: pip_list_check
653 changed_when: false
654
655 - name: Assert editable
656 assert:
657 that:
658 - pip_list_check.stdout is search("sample-project\s+1.0.0\s+" ~ remote_tmp_dir ~ "/sample-project")
659
660 - name: Clear packages
661 file:
662 state: absent
663 name: "{{ venv_path }}"
664
665 - name: Install multiple packages as editable
666 pip:
667 name:
668 - "{{ remote_tmp_dir }}/sample-project"
669 - "{{ remote_tmp_dir }}/another-project"
670 virtualenv: "{{ venv_path }}"
671 editable: True
672 register: multi_editable_install
673
674 - name: Check status
675 command: "{{ venv_path }}/bin/pip list"
676 register: multi_pip_list_check
677 changed_when: false
678
679 - name: Assert editable
680 assert:
681 that:
682 - multi_editable_install is changed
683 - multi_pip_list_check.stdout is search('sample-project\s+1.0.0\s+' ~ remote_tmp_dir ~ '/sample-project')
684 - multi_pip_list_check.stdout is search('another-project\s+1.0.0\s+' ~ remote_tmp_dir ~ '/another-project')
685
686 # https://github.com/ansible/ansible/issues/81522
687 - block:
688 - name: create checkout-dir for relative chdir virtualenv test
689 file:
690 state: directory
691 path: checkout-dir
692
693 - name: create virtualenv when chdir is a relative path
694 pip:
695 chdir: checkout-dir
696 name: setuptools
697 virtualenv: venv
698 state: present
699 register: pip_relative_chdir_virtualenv
700
701 - name: assert relative chdir resolves virtualenv path correctly
702 assert:
703 that:
704 - pip_relative_chdir_virtualenv is successful
705 - pip_relative_chdir_virtualenv.virtualenv is search('/checkout-dir/venv$')
706 always:
707 - name: remove stale virtualenv from relative chdir test
708 file:
709 state: absent
710 path: "{{ pip_relative_chdir_virtualenv.virtualenv | default('checkout-dir') }}"
711
712 # https://github.com/ansible/ansible/issues/84905
713 - block:
714 - name: remove stale relative virtualenv without chdir
715 file:
716 state: absent
717 path: relative-test-venv
718
719 - name: create virtualenv with relative path and no chdir
720 pip:
721 name: setuptools
722 virtualenv: relative-test-venv
723 state: present
724 register: pip_relative_venv_no_chdir
725
726 - name: assert relative virtualenv without chdir succeeds
727 assert:
728 that:
729 - pip_relative_venv_no_chdir is successful
730 - pip_relative_venv_no_chdir.virtualenv is search('/relative-test-venv$')
731 always:
732 - name: cleanup relative virtualenv without chdir
733 file:
734 state: absent
735 path: "{{ pip_relative_venv_no_chdir.virtualenv | default('relative-test-venv') }}"