goss-org / goss

Quick and Easy server testing/validation
https://goss.rocks
Apache License 2.0
5.5k stars 470 forks source link

Is there a way to pass in a list as a variable? #912

Closed goharma closed 1 month ago

goharma commented 1 month ago

Is there a way to pass in a list as a variable?

SCENARIO 1:

GOSSFILE: file: my_file: exists: true contents: "{{ .Vars.my_list }}"

VARS FILE: my_list:

EXECUTION

goss -g test_goss.yaml --vars test_vars.yaml v .F

Failures/Skipped:

File: my_file: contents: Expected "one\ntwo\n" to equal "[one two]" diff --- test +++ actual @@ -1 +1,3 @@ -[one two] +one +two +

Total Duration: 0.001s Count: 2, Failed: 1, Skipped: 0

SCENARIO 2:

GOSSFILE: file: my_file: exists: true contents: ${.Vars.my_list}

VARS FILE: my_list:

EXECUTION

goss -g test_goss.yaml --vars test_vars.yaml v .F

Failures/Skipped:

File: my_file: contents: Expected "one\ntwo\n" to equal "${.Vars.my_list}" diff --- test +++ actual @@ -1 +1,3 @@ -${.Vars.my_list} +one +two +

Total Duration: 0.001s Count: 2, Failed: 1, Skipped: 0

dklimpel commented 1 month ago

Here ist an example of using a list. You need the range.

https://github.com/bitnami/containers/blob/main/.vib/common/goss/templates/check-binaries.yaml

goharma commented 1 month ago

Thank you

goharma commented 1 month ago

I added [] to the contents and it works. Thanks again.

file: {{ range $item := .Vars.my_list }} my_file-{{ $item }}: path: my_file exists: true contents: [ {{ $item }} ] {{ end }}


Not sure if I have something wrong, but I'm still getting errors.

goss.yaml

file: {{ range $item := .Vars.my_list }} my_file-{{ $item }}: path: my_file exists: true contents: {{ $item }} {{ end }}

vars.yaml

my_list:

my_file

one two

goss -g goss.yaml --vars vars.yaml v

.F.F

Failures/Skipped:

File: my_file-one: my_file: contents: Expected "one\ntwo" to equal "one" diff --- test +++ actual @@ -1 +1,2 @@ one +two

File: my_file-two: my_file: contents: Expected "one\ntwo" to equal "two" diff --- test +++ actual @@ -1 +1,2 @@ +one two

Total Duration: 0.001s Count: 4, Failed: 2, Skipped: 0

aelsabbahy commented 1 month ago

Thank you @dklimpel !

Anyone reading this issue, feel free to submit a PR improving the documentation if you feel it will help.