| 1 |
# test code for the Windows xml module |
| 2 |
# (c) 2017, Richard Levenberg <[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 |
- name: copy a test .xml file |
| 20 |
ansible.windows.win_copy: |
| 21 |
src: config.xml |
| 22 |
dest: "{{ remote_tmp_dir }}\\config.xml" |
| 23 |
|
| 24 |
- name: add an element that only has a text child node |
| 25 |
win_xml: |
| 26 |
path: "{{ remote_tmp_dir }}\\config.xml" |
| 27 |
fragment: '<string key="answer">42</string>' |
| 28 |
xpath: '/config' |
| 29 |
register: element_add_result |
| 30 |
|
| 31 |
- name: check element add result |
| 32 |
assert: |
| 33 |
that: |
| 34 |
- element_add_result is changed |
| 35 |
|
| 36 |
- name: try to add the element that only has a text child node again |
| 37 |
win_xml: |
| 38 |
path: "{{ remote_tmp_dir }}\\config.xml" |
| 39 |
fragment: '<string key="answer">42</string>' |
| 40 |
xpath: '/config' |
| 41 |
register: element_add_result_second |
| 42 |
|
| 43 |
- name: check element add result |
| 44 |
assert: |
| 45 |
that: |
| 46 |
- not element_add_result_second is changed |
| 47 |
|
| 48 |
- name: copy a test log4j.xml |
| 49 |
ansible.windows.win_copy: |
| 50 |
src: log4j.xml |
| 51 |
dest: "{{ remote_tmp_dir }}\\log4j.xml" |
| 52 |
|
| 53 |
- name: change an attribute to fatal logging |
| 54 |
win_xml: |
| 55 |
path: "{{ remote_tmp_dir }}\\log4j.xml" |
| 56 |
xpath: '/log4j:configuration/logger[@name="org.apache.commons.digester"]/level' |
| 57 |
type: attribute |
| 58 |
attribute: 'value' |
| 59 |
fragment: 'FATAL' |
| 60 |
|
| 61 |
- name: try to change the attribute again |
| 62 |
win_xml: |
| 63 |
path: "{{ remote_tmp_dir }}\\log4j.xml" |
| 64 |
xpath: '/log4j:configuration/logger[@name="org.apache.commons.digester"]/level' |
| 65 |
type: attribute |
| 66 |
attribute: 'value' |
| 67 |
fragment: 'FATAL' |
| 68 |
register: attribute_changed_result |
| 69 |
|
| 70 |
- name: check attribute change result |
| 71 |
assert: |
| 72 |
that: |
| 73 |
- attribute_changed_result is not changed |
| 74 |
|
| 75 |
- name: try to add a new attribute |
| 76 |
win_xml: |
| 77 |
path: "{{ remote_tmp_dir }}\\config.xml" |
| 78 |
xpath: '/config/string[@key="foo"]' |
| 79 |
type: attribute |
| 80 |
attribute: spam |
| 81 |
fragment: 'ham' |
| 82 |
register: element_add_attribute_result |
| 83 |
|
| 84 |
- name: check element add attribute result |
| 85 |
assert: |
| 86 |
that: |
| 87 |
- element_add_attribute_result is changed |
| 88 |
|
| 89 |
- name: try to set the added attribute to the same value again |
| 90 |
win_xml: |
| 91 |
path: "{{ remote_tmp_dir }}\\config.xml" |
| 92 |
xpath: '/config/string[@key="foo"]' |
| 93 |
type: attribute |
| 94 |
attribute: spam |
| 95 |
fragment: 'ham' |
| 96 |
register: element_add_attribute_result_second |
| 97 |
|
| 98 |
- name: check element add attribute result |
| 99 |
assert: |
| 100 |
that: |
| 101 |
- not element_add_attribute_result_second is changed |
| 102 |
|
| 103 |
- name: try to add a new element in empty element |
| 104 |
win_xml: |
| 105 |
path: "{{ remote_tmp_dir }}\\config.xml" |
| 106 |
xpath: '/config/setting' |
| 107 |
fragment: "<property />" |
| 108 |
type: element |
| 109 |
register: element_add_empty_element |
| 110 |
|
| 111 |
- name: check element add in empty element result |
| 112 |
assert: |
| 113 |
that: |
| 114 |
- element_add_empty_element is changed |
| 115 |
|
| 116 |
- name: try to add a new element in empty element again |
| 117 |
win_xml: |
| 118 |
path: "{{ remote_tmp_dir }}\\config.xml" |
| 119 |
xpath: '/config/setting' |
| 120 |
fragment: "<property />" |
| 121 |
type: element |
| 122 |
register: element_add_empty_element_second |
| 123 |
|
| 124 |
- name: check element add in empty element result |
| 125 |
assert: |
| 126 |
that: |
| 127 |
- not element_add_empty_element_second is changed |
| 128 |
|
| 129 |
# This testing is for https://github.com/ansible/ansible/issues/48471 |
| 130 |
# The issue was that an .xml with no encoding declaration, but a UTF8 BOM |
| 131 |
# with some UTF-8 characters was being written out with garbage characters. |
| 132 |
# The characters added by win_xml were not UTF-8 characters. |
| 133 |
|
| 134 |
- name: copy test files (https://github.com/ansible/ansible/issues/48471) |
| 135 |
ansible.windows.win_copy: |
| 136 |
src: plane.zip |
| 137 |
dest: "{{ remote_tmp_dir }}\\plane.zip" |
| 138 |
|
| 139 |
- name: unarchive the test files |
| 140 |
win_unzip: |
| 141 |
src: "{{ remote_tmp_dir }}\\plane.zip" |
| 142 |
dest: "{{ remote_tmp_dir }}\\" |
| 143 |
|
| 144 |
- name: change a text value in a file with UTF8 BOM and armenian characters in the description |
| 145 |
win_xml: |
| 146 |
path: "{{ remote_tmp_dir }}\\plane-utf8-bom-armenian-characters.xml" |
| 147 |
xpath: '/plane/year' |
| 148 |
type: text |
| 149 |
fragment: '1988' |
| 150 |
|
| 151 |
- name: register the sha1 of the new file |
| 152 |
ansible.windows.win_stat: |
| 153 |
path: "{{ remote_tmp_dir }}\\plane-utf8-bom-armenian-characters.xml" |
| 154 |
get_checksum: yes |
| 155 |
register: sha1_checksum |
| 156 |
|
| 157 |
- name: verify the checksum |
| 158 |
assert: |
| 159 |
that: |
| 160 |
- sha1_checksum.stat.checksum == 'e3e18c3066e1bfce9a5cf87c81353fa174440944' |
| 161 |
|
| 162 |
- name: change a text value in a file with UTF8 BOM and armenian characters in the description |
| 163 |
win_xml: |
| 164 |
path: "{{ remote_tmp_dir }}\\plane-utf8-bom-armenian-characters.xml" |
| 165 |
xpath: '/plane/year' |
| 166 |
type: text |
| 167 |
fragment: '1989' |
| 168 |
backup: yes |
| 169 |
register: test_backup |
| 170 |
|
| 171 |
- name: check backup_file |
| 172 |
ansible.windows.win_stat: |
| 173 |
path: '{{ test_backup.backup_file }}' |
| 174 |
register: backup_file |
| 175 |
|
| 176 |
- name: Check backup_file |
| 177 |
assert: |
| 178 |
that: |
| 179 |
- test_backup is changed |
| 180 |
- backup_file.stat.exists == true |
| 181 |
|
| 182 |
- name: change a text value in a file with UTF-16 BE BOM and Chinese characters in the description |
| 183 |
win_xml: |
| 184 |
path: "{{ remote_tmp_dir }}\\plane-utf16be-bom-chinese-characters.xml" |
| 185 |
xpath: '/plane/year' |
| 186 |
type: text |
| 187 |
fragment: '1988' |
| 188 |
|
| 189 |
- name: register the sha1 of the new file |
| 190 |
ansible.windows.win_stat: |
| 191 |
path: "{{ remote_tmp_dir }}\\plane-utf16be-bom-chinese-characters.xml" |
| 192 |
get_checksum: yes |
| 193 |
register: sha1_checksum |
| 194 |
|
| 195 |
- name: verify the checksum |
| 196 |
assert: |
| 197 |
that: |
| 198 |
- sha1_checksum.stat.checksum == 'de86f79b409383447cf4cf112b20af8ffffcfdbf' |
| 199 |
|
| 200 |
# features added ansible 2.8 |
| 201 |
# count |
| 202 |
|
| 203 |
- name: count logger nodes in log4j.xml |
| 204 |
win_xml: |
| 205 |
path: "{{ remote_tmp_dir }}\\log4j.xml" |
| 206 |
xpath: //logger |
| 207 |
count: yes |
| 208 |
register: logger_node_count |
| 209 |
|
| 210 |
- name: verify node count |
| 211 |
assert: |
| 212 |
that: |
| 213 |
- logger_node_count.count == 5 |
| 214 |
|
| 215 |
# multiple attribute change |
| 216 |
- name: ensure //logger/level value attributes are set to debug |
| 217 |
win_xml: |
| 218 |
path: "{{ remote_tmp_dir }}\\log4j.xml" |
| 219 |
xpath: '//logger/level[@value="error"]' |
| 220 |
type: attribute |
| 221 |
attribute: value |
| 222 |
fragment: debug |
| 223 |
count: yes |
| 224 |
register: logger_level_value_attrs |
| 225 |
|
| 226 |
- name: verify //logger/level value attributes |
| 227 |
assert: |
| 228 |
that: |
| 229 |
- logger_level_value_attrs.count == 4 |
| 230 |
- logger_level_value_attrs.changed == true |
| 231 |
- logger_level_value_attrs.msg == 'attribute changed' |
| 232 |
|
| 233 |
- name: ensure //logger/level value attributes are set to debug (idempotency) |
| 234 |
win_xml: |
| 235 |
path: "{{ remote_tmp_dir }}\\log4j.xml" |
| 236 |
xpath: '//logger/level[@value="error"]' |
| 237 |
type: attribute |
| 238 |
attribute: value |
| 239 |
fragment: debug |
| 240 |
count: yes |
| 241 |
register: logger_level_value_attrs_again |
| 242 |
|
| 243 |
- name: verify //logger/level value attributes again (idempotency) |
| 244 |
assert: |
| 245 |
that: |
| 246 |
- logger_level_value_attrs_again.count == 0 |
| 247 |
- logger_level_value_attrs_again.changed == false |
| 248 |
- logger_level_value_attrs_again.msg == 'The supplied xpath did not match any nodes. If this is unexpected, check your xpath is valid for the xml file at supplied path.' |
| 249 |
|
| 250 |
# multiple text nodes |
| 251 |
- name: ensure test books.xml is present |
| 252 |
ansible.windows.win_copy: |
| 253 |
src: books.xml |
| 254 |
dest: '{{ remote_tmp_dir }}\books.xml' |
| 255 |
|
| 256 |
- name: demonstrate multi text replace by replacing all title text elements |
| 257 |
win_xml: |
| 258 |
path: '{{ remote_tmp_dir }}\books.xml' |
| 259 |
xpath: //works/title |
| 260 |
type: text |
| 261 |
fragment: _TITLE_TEXT_REMOVED_BY_WIN_XML_MODULE_ |
| 262 |
count: yes |
| 263 |
register: multi_text |
| 264 |
|
| 265 |
- name: verify multi text change |
| 266 |
assert: |
| 267 |
that: |
| 268 |
- multi_text.changed == true |
| 269 |
- multi_text.count == 5 |
| 270 |
- multi_text.msg == 'text changed' |
| 271 |
|
| 272 |
- name: demonstrate multi text replace by replacing all title text elements again (idempotency) |
| 273 |
win_xml: |
| 274 |
path: '{{ remote_tmp_dir }}\books.xml' |
| 275 |
xpath: //works/title |
| 276 |
type: text |
| 277 |
fragment: _TITLE_TEXT_REMOVED_BY_WIN_XML_MODULE_ |
| 278 |
count: yes |
| 279 |
register: multi_text_again |
| 280 |
|
| 281 |
- name: verify multi text again change (idempotency) |
| 282 |
assert: |
| 283 |
that: |
| 284 |
- multi_text_again.changed == false |
| 285 |
- multi_text_again.count == 5 |
| 286 |
- multi_text_again.msg == 'not changed' |
| 287 |
|
| 288 |
# multiple element |
| 289 |
|
| 290 |
#- name: ensure a fresh test books.xml is present |
| 291 |
# ansible.windows.win_copy: |
| 292 |
# src: books.xml |
| 293 |
# dest: '{{ remote_tmp_dir }}\books.xml' |
| 294 |
|
| 295 |
- name: demonstrate multi element should append new information element from fragment |
| 296 |
win_xml: |
| 297 |
path: '{{ remote_tmp_dir }}\books.xml' |
| 298 |
xpath: //works/title |
| 299 |
type: element |
| 300 |
fragment: <information>This element added by ansible</information> |
| 301 |
count: yes |
| 302 |
register: multi_element |
| 303 |
|
| 304 |
- name: verify multi element |
| 305 |
assert: |
| 306 |
that: |
| 307 |
- multi_element.changed == true |
| 308 |
- multi_element.count == 5 |
| 309 |
- multi_element.msg == 'element changed' |
| 310 |
|
| 311 |
- name: demonstrate multi element unchanged (idempotency) |
| 312 |
win_xml: |
| 313 |
path: '{{ remote_tmp_dir }}\books.xml' |
| 314 |
xpath: //works/title |
| 315 |
type: element |
| 316 |
fragment: <information>This element added by ansible</information> |
| 317 |
count: yes |
| 318 |
register: multi_element_again |
| 319 |
|
| 320 |
- name: verify multi element again (idempotency) |
| 321 |
assert: |
| 322 |
that: |
| 323 |
- multi_element_again.changed == false |
| 324 |
- multi_element_again.count == 5 |
| 325 |
- multi_element_again.msg == 'not changed' |
| 326 |
|
| 327 |
# multiple attributes on differing parent nodes |
| 328 |
|
| 329 |
- name: ensure all attribute lang=nl |
| 330 |
win_xml: |
| 331 |
path: '{{ remote_tmp_dir }}\books.xml' |
| 332 |
xpath: //@lang |
| 333 |
type: attribute |
| 334 |
attribute: lang |
| 335 |
fragment: nl |
| 336 |
count: yes |
| 337 |
register: multi_attr |
| 338 |
|
| 339 |
- name: verify multi attribute |
| 340 |
assert: |
| 341 |
that: |
| 342 |
- multi_attr.changed == true |
| 343 |
- multi_attr.count == 6 |
| 344 |
- multi_attr.msg == 'attribute changed' |
| 345 |
|
| 346 |
- name: ensure all attribute lang=nl (idempotency) |
| 347 |
win_xml: |
| 348 |
path: '{{ remote_tmp_dir }}\books.xml' |
| 349 |
xpath: //@lang |
| 350 |
type: attribute |
| 351 |
attribute: lang |
| 352 |
fragment: nl |
| 353 |
count: yes |
| 354 |
register: multi_attr_again |
| 355 |
|
| 356 |
- name: verify multi attribute (idempotency) |
| 357 |
assert: |
| 358 |
that: |
| 359 |
- multi_attr_again.changed == false |
| 360 |
- multi_attr_again.count == 6 |
| 361 |
- multi_attr_again.msg == 'not changed' |