goss-org / goss

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

If condition about not equal to #640

Closed vinodhkumart closed 3 years ago

vinodhkumart commented 4 years ago

Hi ,

I am going through the documentation and unable to find an option to exclude specific os or not equal to specific OS . In this case, eg: is centos.. How can i say to install a package in all the OS(RHEL,SUSE,UBUNTU) except centos.

{{if eq .Env.OS "centos"}}

This test is only when $OS environment variable is set to "centos"

libselinux: installed: true {{end}}

Regards VK

aelsabbahy commented 4 years ago

Goss just uses Golang's built in text/template which can be a bit awkward.

You should be able to do either:

{{ if not (eq .Env.OS "centos") }}

or

{{ if eq .Env.OS "centos" | not }}

Let me know if that resolves it for you.

vinodhkumart commented 4 years ago

Hi @aelsabbahy ,

Thanks for the update. I tried with the above 2 conditions but not working.

Regards Vinod

aelsabbahy commented 4 years ago

Can you provide a full example.

How to reproduce Actual result Expected result

aelsabbahy commented 4 years ago

I'm not able to reproduce your experience, see the following:

$ cat goss.yaml 
file:
  {{ if not (eq .Env.OS "centos") }}
  /tmp/test:
    exists: true
  {{ end }}

$ OS='centos' goss render
{}

$ OS='foo' goss render
file:
  /tmp/test:
    exists: true
    contains: []
vinodhkumart commented 4 years ago

Sorry, was busy bit and could not able to reply on this. Please check the following example.

$ facter os.distro.release.major 7 $ cat goss.yaml package: {{ if not (eq .Vars.os.distro.release.major "7") }} lftp: installed: true {{ end }} $ goss --vars <(facter -j) --gossfile goss.yaml v Error: found 0 tests, source: goss.yaml

As per the above condition, If my OS version is not equal to 7, then lftp package should be installed. When i execute the test, it says 0 tests. But above condition works well with the equal to ....

aelsabbahy commented 4 years ago

If my OS version is not equal to 7, then lftp package should be installed

$ facter os.distro.release.major 7

It is equal to 7, so goss is not running the test. If you chose something other than 7 the test will run.

The error you're seeing about "found 0 tests" is because you have an empty test suite and goss doesn't allow for that.

One way around that would be to leverage the skip flag (if you have no other test) or add more tests.

package:
    lftp:
        installed: true
        skip: {{ eq .Vars.os.distro.release.major "7" }}
vinodhkumart commented 3 years ago

Thanks. Lookslike it is working.. will try for few other options and let you know if any issues.