Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Crontab regex matching #1526

Closed
ecbrodie opened this issue Mar 1, 2017 · 3 comments
Closed

Crontab regex matching #1526

ecbrodie opened this issue Mar 1, 2017 · 3 comments

Comments

@ecbrodie
Copy link

ecbrodie commented Mar 1, 2017

Description

The crontab resource is unable to apply Regex matching via the to match matcher. It would be nice if this were a feature, since I prefer to match a part of the command instead of the whole command in my test. This is especially useful if there are parts of a command that don't care too much about to verify working behaviour, such as paths to a command that are dynamically created by factors including programming language versions.

For example, suppose that I am adding a cron in my cookbook for running a custom rake task nightly. The rake task could be something like custom_task, so I just want my test to ensure that there is a cron command that matches to the regex /rake custom_task/.

describe corntab do
  its ('commands') { should match /rake custom_task/ }
end

Unfortunately, it looks like I can only do an equality check via include instead of a regex check with match. So I have to add some hardcoded paths to my test code, which makes me cringe slightly.

describe corntab do
  its ('commands') { should include '/home/deploy/ruby-2.3.1/bin/rake custom_task' }
end

InSpec and Platform Version

Inspec version 1.15.0
MacOS 10.12.3 (host machine where I am running tests from)

Replication Case

See Description

Possible Solutions

My workaround is to bite my tongue and just use the include matcher. I do not think I know the best way to write a PR for this. But I imagine that new code should allow for match matcher support in lib/resources/crontab.rb.

@adamleff
Copy link
Contributor

adamleff commented Mar 7, 2017

Hi, @ecbrodie! Thanks for submitting this issue!

With resources such as crontab that use our FilterTable module under the covers, the way we normally accomplish this is with a #where filter:

describe crontab.where { command =~ /rake custom_task/ } do
  its('entries.length') { should cmp 1 }
end

Would this approach meet your needs?

@ecbrodie
Copy link
Author

@adamleff This code did the trick and allowed me to apply the regex matching on my crontabs like I intended.

I would say that it would be nice to support a regex matcher through an its statement instead of through where. If that's not feasible, then I say at least add this an an example to the documentation.

@adamleff
Copy link
Contributor

@ecbrodie I'm glad to hear that you're unblocked.

Native RSpec doesn't support using match with an array return:

     Failure/Error: expect(['echo foo', 'echo bar']).to match(/foo/)

       expected ["echo foo", "echo bar"] to match /foo/
       Diff:
       @@ -1,2 +1,2 @@
       -/foo/
       +["echo foo", "echo bar"]

... requiring you to do something like this:

expect(['echo foo', 'echo bar'].select { |x| x.match(/foo/) }).not_to eq([])

In addition to the where syntax, you could convert the commands array to a string and match that:

describe crontab do
  its('commands.to_s') { should match /foo/ }
end

I think the where syntax is cleaner and allows you to pass a more complicated block if needed, but you do have a few options here.

I will update the documentation with a where example though. Thanks for the suggestion!

adamleff added a commit that referenced this issue Mar 14, 2017
As raised in #1526, adding an additional example showing how
a user can use the `where` accessor to find commands matching
a pattern and write a test using the results.

Signed-off-by: Adam Leff <adam@leff.co>
@adamleff adamleff self-assigned this Mar 14, 2017
chris-rock pushed a commit that referenced this issue Mar 14, 2017
As raised in #1526, adding an additional example showing how
a user can use the `where` accessor to find commands matching
a pattern and write a test using the results.

Signed-off-by: Adam Leff <adam@leff.co>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants