goss-org / goss

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

exact/strict matching using contents in a file. #556

Closed shan100github closed 4 years ago

shan100github commented 4 years ago

I would like to know how we can do the exact matching of content through goss. file content: */5 * * * * /usr/local/tmp/tmp1.sh exec > /dev/null 2>&1

currently, I am trying to test with the following goss.yaml

file:
  /var/spool/cron/temp:
    exists: true
    filetype: file
    contains:
    -  "*/5 * * * * /usr/local/tmp/tmp1.sh exec > /dev/null"

I am expecting this to fail because goss.yaml doesn't have 2>&1. If it's not possible through file checking, how we can achieve it.

I have gone through templates & pattern matching but finding difficult to understand it. Sorry for the noise, looking forward to get some guidance.

aelsabbahy commented 4 years ago

As you've noticed string matching isn't an exact match, if you need this you'll have to use regex and use ^ and $ to signal start/end of line, ex: '/^some_regex$/'

In your case:

file:
  /var/spool/cron/temp:
    exists: true
    filetype: file
    contains:
      - '/^\*/5 \* \* \* \* /usr/local/tmp/tmp1.sh exec > /dev/null$/'
shan100github commented 4 years ago

Thanks for your quick reply & it worked.

aelsabbahy commented 4 years ago

Awesome! :)