Debian Codegraph @

ansible_collections/community/general/tests/integration/targets/xml/tasks/test-set-element-value-predicate.yml

raw
1 ---
2 # Copyright (c) Ansible Project
3 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
4 # SPDX-License-Identifier: GPL-3.0-or-later
5
6 # Tests for https://github.com/ansible-collections/community.general/issues/8730
7
8 # --- create_if_missing=false: predicate no-match must be a silent no-op ---
9
10 - name: Setup test fixture
11 copy:
12 src: fixtures/ansible-xml-items.xml
13 dest: /tmp/ansible-xml-items.xml
14
15 - name: Update items via predicate (create_if_missing=false, first run - matches)
16 xml:
17 path: /tmp/ansible-xml-items.xml
18 xpath: /business/items/item[text()='value_a']
19 value: value_c
20 create_if_missing: false
21 register: set_predicate_first_run
22
23 - name: Update items via predicate (create_if_missing=false, second run - predicate no longer matches)
24 xml:
25 path: /tmp/ansible-xml-items.xml
26 xpath: /business/items/item[text()='value_a']
27 value: value_c
28 create_if_missing: false
29 register: set_predicate_second_run
30
31 - name: Add trailing newline
32 shell: echo "" >> /tmp/ansible-xml-items.xml
33
34 - name: Compare to expected result
35 copy:
36 src: results/test-set-element-value-predicate.xml
37 dest: /tmp/ansible-xml-items.xml
38 check_mode: true
39 diff: true
40 register: comparison
41
42 - name: Assert create_if_missing=false with predicate no-match is a no-op
43 assert:
44 that:
45 - set_predicate_first_run is changed
46 - set_predicate_second_run is not changed
47 - comparison is not changed # no spurious elements created
48
49 # --- create_if_missing=false: non-existent simple path must also be a no-op ---
50
51 - name: Setup test fixture
52 copy:
53 src: fixtures/ansible-xml-items.xml
54 dest: /tmp/ansible-xml-items.xml
55
56 - name: Set value on non-existent element (create_if_missing=false)
57 xml:
58 path: /tmp/ansible-xml-items.xml
59 xpath: /business/items/nonexistent
60 value: something
61 create_if_missing: false
62 register: set_nonexistent
63
64 - name: Assert create_if_missing=false with missing simple path is a no-op
65 assert:
66 that:
67 - set_nonexistent is not changed
68
69 # --- create_if_missing=true (default): no-match must create the node ---
70
71 - name: Setup test fixture
72 copy:
73 src: fixtures/ansible-xml-items.xml
74 dest: /tmp/ansible-xml-items.xml
75
76 - name: Set value on non-existent element (create_if_missing=true, should create)
77 xml:
78 path: /tmp/ansible-xml-items.xml
79 xpath: /business/items/item[text()='value_z']
80 value: value_z
81 create_if_missing: true
82 register: set_predicate_create
83
84 - name: Set same value again (create_if_missing=true, should be idempotent)
85 xml:
86 path: /tmp/ansible-xml-items.xml
87 xpath: /business/items/item[text()='value_z']
88 value: value_z
89 create_if_missing: true
90 register: set_predicate_create_again
91
92 - name: Assert create_if_missing=true creates the node and is then idempotent
93 assert:
94 that:
95 - set_predicate_create is changed
96 - set_predicate_create_again is not changed