0

Am trying to achieve condition that if the group exists in the server it should pass & if not exists it should skip and should not show as failure

describe.one do 
  describe 'testgroup'  do 
    expect(bash("ipa group-show testgroup").exit_status).to eq 0 
    it { should exist }
  end 

  describe 'testgroup' do 
    expect(bash("ipa group-show testgroup").exit_status).to_not eq 0 
    it { should_not exist }
  end
end 

After executing using:

# inspec exec testgroup_test.rb

It throws error output as:

undefined method 'bash' for Rspec::ExampleGroups (No method Error). 

Please advice, how to achieve such condition testing in inspec.

seshadri_c
  • 6,906
  • 2
  • 10
  • 24
  • If its ok for the group to not exist, why test it at all? Not sure you would want to write a test that doesn't fail if the condition doesn't match. – seshadri_c Aug 21 '21 at 08:18
  • the script is common for 2 different environments, in one environment if its available the test should pass and in other env the group is not available it should skip and wont show failure – Sivaguru Nathan Aug 21 '21 at 08:28

1 Answers1

0

To address the error message first before solving the environment specific tests, the correct way of testing a command's result is to use the command resource.

Let's consider a case when I want to pass the test when the command executes successfully, otherwise fail:

describe command('ipa group-show testgroup') do
  its('exit_status') { should eq 0 }
end

Now, let's say we have 2 environments - "dev" and "qa". We can use inputs to conditionally execute the tests.

For an example: We can define an input called env. In "dev" environment it may not be necessary that this command will succeed. While in "qa" it should succeed.

# In "dev" any return code from command is ok
if input('env') == 'dev'
  control "ipa-group-dev" do
    describe command('ipa group-show testgroup') do
      its('exit_status') { should be >= 0 }
    end
  end
end

# In "qa" this command must succeed
if input('env') == 'qa'
  control "ipa-group-qa" do
    describe command('ipa group-show testgroup') do
      its('exit_status') { should eq 0 }
    end
  end
end

We can run the test by passing the value of env as input, example:

inspec exec testgroup_test.rb --input env=dev
seshadri_c
  • 6,906
  • 2
  • 10
  • 24
  • sesadri_c thanks for your valuable input, btw is it possible to get customized output in your code. mean if the condition throw output as "testgroup presents" or "testgroup not available" – Sivaguru Nathan Aug 21 '21 at 13:10
  • I think what you are trying to achieve with inspec does not fit with its design. Please see if [custom inspec resources](https://docs.chef.io/inspec/dsl_resource/) works for you. Else, you should look at other tools (even Shell script at a rudimentary level). – seshadri_c Aug 21 '21 at 15:20