1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00
community.general/test/integration/targets/xml/tasks/test-add-element-implicitly.yml

180 lines
7.6 KiB
YAML
Raw Normal View History

New module: Support manipulating XML files (#25323) * Import original unmodified upstream version This is another attempt to get the xml module upstream. https://github.com/cmprescott/ansible-xml/ This is the original file from upstream, without commit 1e7a3f6b6e2bc01aa9cebfd80ac5cd4555032774 * Add additional changes required for upstreaming This PR includes the following changes: - Clean up of DOCUMENTATION - Rename "ensure" parameter to "state" parameter (kept alias) - Added EXAMPLES - Remove explicit type-case using str() for formatting - Clean up AnsibleModule parameter handling - Retained Python 2.4 compatibility - PEP8 compliancy - Various fixes as suggested by abadger during first review This fixes cmprescott/ansible-xml#108 * Added original integration tests There is some room for improvement wrt. idempotency and check-mode testing. * Some tests depend on lxml v3.0alpha1 or higher We are now expecting lxml v2.3.0 or higher. We skips tests if lxml is too old. Plus small fix. * Relicense to GPLv3+ header All past contributors have agreed to relicense this module to GPLv2+, and GPLv3 specifically. See: https://github.com/cmprescott/ansible-xml/issues/113 This fixes cmprescott/ansible-xml#73 * Fix small typo in integration tests * Python 3 support This PR also includes: - Python 3 support - Documentation fixes - Check-mode fixes and improvements - Bugfix in check-mode support - Always return xmlstring, even if there's no change - Check for lxml 2.3.0 or newer * Add return values * Various fixes after review
2017-08-09 05:21:03 +02:00
---
- name: Setup test fixture
copy: src={{ role_path }}/fixtures/ansible-xml-beers.xml dest=/tmp/ansible-xml-beers-implicit.xml
- name: Add a phonenumber element to the business element. Implicit mkdir -p behavior where applicable
xml: file=/tmp/ansible-xml-beers-implicit.xml xpath=/business/phonenumber value=555-555-1234
- name: Add a owner element to the business element, testing implicit mkdir -p behavior 1/2
xml: file=/tmp/ansible-xml-beers-implicit.xml xpath=/business/owner/name/last value=Smith
- name: Add a owner element to the business element, testing implicit mkdir -p behavior 2/2
xml: file=/tmp/ansible-xml-beers-implicit.xml xpath=/business/owner/name/first value=John
- name: Add a validxhtml element to the website element. Note that ensure is present by default and while value defaults to null for elements, if one doesn't specify it we don't know what to do.
xml: file=/tmp/ansible-xml-beers-implicit.xml xpath=/business/website/validxhtml
- name: Add an empty validateon attribute to the validxhtml element. This actually makes the previous example redundant because of the implicit parent-node creation behavior.
xml: file=/tmp/ansible-xml-beers-implicit.xml xpath=/business/website/validxhtml/@validateon
- name: Add an empty validateon attribute to the validxhtml element. Actually verifies the implicit parent-node creation behavior.
xml: file=/tmp/ansible-xml-beers-implicit.xml xpath=/business/website_bis/validxhtml/@validateon
- name: Add an attribute with a value
xml: file=/tmp/ansible-xml-beers-implicit.xml xpath=/business/owner/@dob='1976-04-12'
- name: Add an element with a value, alternate syntax
xml: file=/tmp/ansible-xml-beers-implicit.xml xpath="/business/beers/beer/text()=\"George Killian's Irish Red\"" # note the quote within an XPath string thing
- name: Add an element without special characters
xml: file=/tmp/ansible-xml-beers-implicit.xml xpath=/business/testnormalelement value="xml tag with no special characters" pretty_print=true
- name: Add an element with dash
xml: file=/tmp/ansible-xml-beers-implicit.xml xpath=/business/test-with-dash value="xml tag with dashes" pretty_print=true
- name: Add an element with dot
xml: file=/tmp/ansible-xml-beers-implicit.xml xpath=/business/test-with-dash.and.dot value="xml tag with dashes and dots" pretty_print=true
- name: Add an element with underscore
xml: file=/tmp/ansible-xml-beers-implicit.xml xpath=/business/test-with.dash_and.dot_and-underscores value="xml tag with dashes, dots and underscores" pretty_print=true
- name: Add an attribute on a conditional element
xml: file=/tmp/ansible-xml-beers-implicit.xml xpath="/business/beers/beer[text()=\"George Killian's Irish Red\"]/@color='red'"
- name: Add two attributes on a conditional element
xml: file=/tmp/ansible-xml-beers-implicit.xml xpath="/business/beers/beer[text()=\"Pilsner Urquell\" and @origin='CZ']/@color='blonde'"
- name: Add a owner element to the business element, testing implicit mkdir -p behavior 3/2 -- complex lookup
xml: file=/tmp/ansible-xml-beers-implicit.xml xpath=/business/owner/name[first/text()='John']/middle value=Q
- name: Pretty Print this!
xml: file=/tmp/ansible-xml-beers-implicit.xml pretty_print=True
- name: Test expected result
command: diff -u {{ role_path }}/results/test-add-element-implicitly.yml /tmp/ansible-xml-beers-implicit.xml
#
# Now we repeat the same, just to ensure proper use of namespaces
#
- name: Add a phonenumber element to the business element. Implicit mkdir -p behavior where applicable
xml:
file: /tmp/ansible-xml-beers-implicit.xml
xpath: /business/a:phonenumber
value: 555-555-1234
namespaces:
a: http://example.com/some/namespace
- name: Add a owner element to the business element, testing implicit mkdir -p behavior 1/2
xml:
file: /tmp/ansible-xml-beers-implicit.xml
xpath: /business/a:owner/a:name/a:last
value: Smith
namespaces:
a: http://example.com/some/namespace
- name: Add a owner element to the business element, testing implicit mkdir -p behavior 2/2
xml:
file: /tmp/ansible-xml-beers-implicit.xml
xpath: /business/a:owner/a:name/a:first
value: John
namespaces:
a: http://example.com/some/namespace
- name: Add a validxhtml element to the website element. Note that ensure is present by default and while value defaults to null for elements, if one doesn't specify it we don't know what to do.
xml:
file: /tmp/ansible-xml-beers-implicit.xml
xpath: /business/a:website/a:validxhtml
namespaces:
a: http://example.com/some/namespace
- name: Add an empty validateon attribute to the validxhtml element. This actually makes the previous example redundant because of the implicit parent-node creation behavior.
xml:
file: /tmp/ansible-xml-beers-implicit.xml
xpath: /business/a:website/a:validxhtml/@a:validateon
namespaces:
a: http://example.com/some/namespace
- name: Add an empty validateon attribute to the validxhtml element. Actually verifies the implicit parent-node creation behavior.
xml:
file: /tmp/ansible-xml-beers-implicit.xml
xpath: /business/a:website_bis/a:validxhtml/@a:validateon
namespaces:
a: http://example.com/some/namespace
- name: Add an attribute with a value
xml:
file: /tmp/ansible-xml-beers-implicit.xml
xpath: /business/a:owner/@a:dob='1976-04-12'
namespaces:
a: http://example.com/some/namespace
- name: Add an element with a value, alternate syntax
xml:
file: /tmp/ansible-xml-beers-implicit.xml
xpath: "/business/a:beers/a:beer/text()=\"George Killian's Irish Red\"" # note the quote within an XPath string thing
namespaces:
a: http://example.com/some/namespace
- name: Add an attribute on a conditional element
xml:
file: /tmp/ansible-xml-beers-implicit.xml
xpath: "/business/a:beers/a:beer[text()=\"George Killian's Irish Red\"]/@a:color='red'"
namespaces:
a: http://example.com/some/namespace
- name: Add two attributes on a conditional element
xml:
file: /tmp/ansible-xml-beers-implicit.xml
xpath: "/business/a:beers/a:beer[text()=\"Pilsner Urquell\" and @a:origin='CZ']/@a:color='blonde'"
namespaces:
a: http://example.com/some/namespace
- name: Add a owner element to the business element, testing implicit mkdir -p behavior 3/2 -- complex lookup
xml:
file: /tmp/ansible-xml-beers-implicit.xml
xpath: /business/a:owner/a:name[a:first/text()='John']/a:middle
value: Q
namespaces:
a: http://example.com/some/namespace
- name: Add an element without special characters
xml:
file: /tmp/ansible-xml-beers-implicit.xml
xpath: /business/testnormalelement
value: "xml tag with no special characters"
pretty_print: true
namespaces:
a: http://example.com/some/namespace
- name: Add an element with dash
xml:
file: /tmp/ansible-xml-beers-implicit.xml
xpath: /business/test-with-dash
value: "xml tag with dashes"
pretty_print: true
namespaces:
a: http://example.com/some/namespace
- name: Add an element with dot
xml:
file: /tmp/ansible-xml-beers-implicit.xml
xpath: /business/test-with-dash.and.dot
value: "xml tag with dashes and dots"
pretty_print: true
namespaces:
a: http://example.com/some/namespace
- name: Add an element with underscore
xml:
file: /tmp/ansible-xml-beers-implicit.xml
xpath: /business/test-with.dash_and.dot_and-underscores
value: "xml tag with dashes, dots and underscores"
pretty_print: true
namespaces:
a: http://example.com/some/namespace
- name: Pretty Print this!
xml: file=/tmp/ansible-xml-beers-implicit.xml pretty_print=True