Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to limit InSpec test to a specific OS Version

We have a cookbook that is used on centos 6 and 7 machines. On 7 it installs the latest version of node, on 6 it installs a specific version of node. Also on 6 it installs certain other packages that we don't install on 7. I am trying to figure out how to write an InSpec tests that will only exectuce/assert that things are in a give state if we are testing a centos 6 box. How do I do this?

Running this with test kitchen.

like image 380
Kenneth Baltrinic Avatar asked Sep 30 '16 17:09

Kenneth Baltrinic


People also ask

How does a chef InSpec work?

Chef InSpec works by comparing the actual state of your system with the desired state that you express in easy-to-read and easy-to-write Chef InSpec code. Chef InSpec detects violations and displays findings in the form of a report, but puts you in control of remediation.

How do I run an InSpec profile?

Login to the Chef Automate instance via InSpec: inspec compliance login . Upload the profile to Automate: inspec compliance upload <name> . Verify the profile is uploaded correctly: inspec compliance profiles . Run the profile via Automate: inspec compliance exec YOURUSERNAME/<name> .

Is InSpec open source?

InSpec is an open source project that lets you define your compliance requirements in a human- and machine-readable language. Once you've codified your requirements, you can run them as automated tests that audit your systems. InSpec provides a local agent, as well as full remote testing support.


2 Answers

You would use the pseudo-resource os. This exposes a bunch of info about the underlying platform but in this case you want os[:release].start_with?('6') (and similar for 7).

like image 108
coderanger Avatar answered Oct 21 '22 10:10

coderanger


InSpec has an os resource. Here's an example of how to use it:

if os.family == 'debian'
  describe port(69) do
    its('processes') { should include 'in.tftpd' }
  end
elsif os.family == 'redhat'
  describe port(69) do
    its('processes') { should include 'xinetd' }
  end
end

You can find more about this resource (and the example above) in the InSpec reference:

https://www.inspec.io/docs/reference/resources/os/

like image 43
james.garriss Avatar answered Oct 21 '22 12:10

james.garriss