diff options
Diffstat (limited to 'test/integration/targets/lookup_csvfile')
7 files changed, 102 insertions, 0 deletions
diff --git a/test/integration/targets/lookup_csvfile/aliases b/test/integration/targets/lookup_csvfile/aliases new file mode 100644 index 0000000..3005e4b --- /dev/null +++ b/test/integration/targets/lookup_csvfile/aliases @@ -0,0 +1 @@ +shippable/posix/group4 diff --git a/test/integration/targets/lookup_csvfile/files/cool list of things.csv b/test/integration/targets/lookup_csvfile/files/cool list of things.csv new file mode 100644 index 0000000..b1a74a0 --- /dev/null +++ b/test/integration/targets/lookup_csvfile/files/cool list of things.csv @@ -0,0 +1,3 @@ +woo,i,have,spaces,in,my,filename +i,am,so,cool,haha,be,jealous +maybe,i,will,work,like,i,should diff --git a/test/integration/targets/lookup_csvfile/files/crlf.csv b/test/integration/targets/lookup_csvfile/files/crlf.csv new file mode 100644 index 0000000..a17f6c4 --- /dev/null +++ b/test/integration/targets/lookup_csvfile/files/crlf.csv @@ -0,0 +1,2 @@ +this file,has,crlf,line,endings
+ansible,parses,them,just,fine
diff --git a/test/integration/targets/lookup_csvfile/files/people.csv b/test/integration/targets/lookup_csvfile/files/people.csv new file mode 100644 index 0000000..f93498c --- /dev/null +++ b/test/integration/targets/lookup_csvfile/files/people.csv @@ -0,0 +1,6 @@ +# Last,First,Email,Extension +Smith,Jane,jsmith@example.com,1234 +Ipsum,Lorem,lipsum@another.example.com,9001 +"German von Lastname",Demo,hello@example.com,123123 +Example,Person,"crazy email"@example.com,9876 +"""The Rock"" Johnson",Dwayne,uhoh@example.com,1337 diff --git a/test/integration/targets/lookup_csvfile/files/tabs.csv b/test/integration/targets/lookup_csvfile/files/tabs.csv new file mode 100644 index 0000000..69f4d87 --- /dev/null +++ b/test/integration/targets/lookup_csvfile/files/tabs.csv @@ -0,0 +1,4 @@ +fruit bananas 30 +fruit apples 9 +electronics tvs 8 +shoes sneakers 26 diff --git a/test/integration/targets/lookup_csvfile/files/x1a.csv b/test/integration/targets/lookup_csvfile/files/x1a.csv new file mode 100644 index 0000000..d2d5a0d --- /dev/null +++ b/test/integration/targets/lookup_csvfile/files/x1a.csv @@ -0,0 +1,3 @@ +separatedbyx1achars +againbecause +wecan diff --git a/test/integration/targets/lookup_csvfile/tasks/main.yml b/test/integration/targets/lookup_csvfile/tasks/main.yml new file mode 100644 index 0000000..758da71 --- /dev/null +++ b/test/integration/targets/lookup_csvfile/tasks/main.yml @@ -0,0 +1,83 @@ +- name: using deprecated syntax but missing keyword + set_fact: + this_will_error: "{{ lookup('csvfile', 'file=people.csv, delimiter=, col=1') }}" + ignore_errors: yes + register: no_keyword + +- name: extra arg in k=v syntax (deprecated) + set_fact: + this_will_error: "{{ lookup('csvfile', 'foo file=people.csv delimiter=, col=1 thisarg=doesnotexist') }}" + ignore_errors: yes + register: invalid_arg + +- name: extra arg in config syntax + set_fact: + this_will_error: "{{ lookup('csvfile', 'foo', file='people.csv', delimiter=',' col=1, thisarg='doesnotexist') }}" + ignore_errors: yes + register: invalid_arg2 + +- set_fact: + this_will_error: "{{ lookup('csvfile', 'foo', file='doesnotexist', delimiter=',', col=1) }}" + ignore_errors: yes + register: missing_file + +- name: Make sure we failed above + assert: + that: + - no_keyword is failed + - > + "Search key is required but was not found" in no_keyword.msg + - invalid_arg is failed + - invalid_arg2 is failed + - > + "is not a valid option" in invalid_arg.msg + - missing_file is failed + - > + "need string or buffer" in missing_file.msg or + "expected str, bytes or os.PathLike object" in missing_file.msg or + "No such file or directory" in missing_file.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" + - lookup('csvfile', 'electronics', 'fruit', file='tabs.csv', delimiter='\t', col=1) == "tvs,bananas" + - lookup('csvfile', 'electronics', 'fruit', file='tabs.csv', delimiter='\t', col=1, wantlist=True) == ["tvs", "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" |