ansible/test/integration/targets/lookup_csvfile/tasks/main.yml
Rick Elrod 1b4fd23ba6
csvfile: use parse_kv() for args, add tests (#70550)
Change:
- Use parse_kv() for parsing in the csvfile lookup plugin. This allows
  us to handle multi-word search keys and filenames. Previously, the
  plugin split on space and so none of these things worked as expected.
- Add integration tests for csvfile, testing a plethora of weird cases.

Test Plan:
- New integration tests, CI

Tickets:
- Fixes #70545

Signed-off-by: Rick Elrod <rick@elrod.me>
2020-07-10 16:21:03 -05:00

54 lines
1.9 KiB
YAML

- set_fact:
this_will_error: "{{ lookup('csvfile', 'file=people.csv delimiter=, col=1') }}"
ignore_errors: yes
register: no_keyword
- name: Make sure we failed above
assert:
that:
- no_keyword is failed
- >
"Search key is required but was not found" in no_keyword.msg
- name: Check basic comma-separated file
assert:
that:
- lookup('csvfile', 'Smith file=people.csv delimiter=, col=1') == "Jane"
- lookup('csvfile', 'German von Lastname file=people.csv delimiter=, col=1') == "Demo"
- name: Check tab-separated file
assert:
that:
- lookup('csvfile', 'electronics file=tabs.csv delimiter=TAB col=1') == "tvs"
- lookup('csvfile', 'fruit file=tabs.csv delimiter=TAB col=1') == "bananas"
- lookup('csvfile', 'fruit file=tabs.csv delimiter="\t" col=1') == "bananas"
- name: Check \x1a-separated file
assert:
that:
- lookup('csvfile', 'again file=x1a.csv delimiter=\x1a col=1') == "because"
- name: Check CSV file with CRLF line endings
assert:
that:
- lookup('csvfile', 'this file file=crlf.csv delimiter=, col=2') == "crlf"
- lookup('csvfile', 'ansible file=crlf.csv delimiter=, col=1') == "parses"
- name: Check file with multi word filename
assert:
that:
- lookup('csvfile', 'maybe file="cool list of things.csv" delimiter=, col=3') == "work"
- name: Test default behavior
assert:
that:
- lookup('csvfile', 'notfound file=people.csv delimiter=, col=2') == []
- lookup('csvfile', 'notfound file=people.csv delimiter=, col=2, default=what?') == "what?"
# NOTE: For historical reasons, this is correct; quotes in the search field must
# be treated literally as if they appear (escaped as required) in the field in the
# file. They cannot be used to surround the search text in general.
- name: Test quotes in the search field
assert:
that:
- lookup('csvfile', '"The Rock" Johnson file=people.csv delimiter=, col=1') == "Dwayne"