-4

i am using inspec test framework with ruby for infrastructure testing. I have written a test in the controls

Here is my test:

require 'aws-sdk'

credentials = Aws::AssumeRoleCredentials.new(
   role_arn: 'some_value',
   role_session_name: 'pipeline')

client_params = {
  region: 'ap-southeast-2',
  credentials: credentials
}

ec2_client = Aws::EC2::Resource.new(client_params)
instance = ec2_client.instances(filters: [{name:'tag:component', values: ['api', 'fxsnet', 'admin']}])


puts "ec2 Client is : #{ec2_client}"
puts "list of instances based on tag is: #{instance}"


instance.each do |i| 
  puts 'ID:    ' + i.id
  puts 'State: ' + i.state.name

#for each of the instance check if tmp file exist 
  describe file('/tmp') do                  # The actual test
    it { should exist }
  end 
end

but on execution, i get below error

An error occurred while loading ./inspec-infra-tests/controls/apiInstances.rb.
Failure/Error:
  describe file('/tmp') do                  # The actual test
    it { should exist }
  end

NoMethodError:
  undefined method `file' for main:Object
  Did you mean?  fail
# ./inspec-infra-tests/controls/apiInstances.rb:46:in `block in <top (required)>'
# ./inspec-infra-tests/controls/apiInstances.rb:35:in `<top (required)>'
No examples found.

0 examples, 0 failures, 0 passed

file InSpec audit resource to test all system file types, including files, directories, symbolic links, named pipes, sockets etc..

#InspecWithRuby #inspec #inspecResourcesNotIdentified #InspecResourcesNotFound 
rinkoo s
  • 11
  • 2
  • 1
    What is your question? – sawa Jun 04 '18 at 07:59
  • `undefined method 'file'` ... The error message is quite clear! What is your test supposed to be doing? What method are you trying to test? I'm voting to close the question as unclear what you're asking, because you've literally just posted an error message. – Tom Lord Jun 04 '18 at 16:49
  • i have added the test....i am trying to get the instances on ec2 on aws based on certain tags(like api, fxsnet) and then trying to check if i can run any command on them like here checking if /tmp file exits. i hope its clear now – rinkoo s Jun 04 '18 at 23:44
  • 1
    I still don't have a clue what that test is supposed to be doing, sorry. Forget all the rest of the code for a moment, just look at that one method call in isolation: `file('/tmp')`. That method call is not making any use of the `Aws::EC2::Resource` library. What is it supposed to be doing? – Tom Lord Jun 05 '18 at 08:30
  • Maybe that's supposed to be `i.file('/tmp')`, or something?? Whatever it's supposed to be, that should be inside the `it` block, not the `describe` line, but I can't possibly show you the "working" code until you figure out what that method call is supposed to be doing. – Tom Lord Jun 05 '18 at 09:09

2 Answers2

0

It looks to me like you are attempting to run an inspec test by executing with ruby rather then running with inspec exec. I can reproduce locally by pasting your test in a file:

inspec_example.rb

  describe file('/tmp') do                  # The actual test
    it { should exist }
  end

Executing with ruby directly

ruby inspec_example.rb gives:

Traceback (most recent call last):
inspec_example.rb:1:in `<main>': undefined method `file' for main:Object (NoMethodError)
Did you mean?  fail

Executing with inspec exec works as expected:

inspec exec inspec_example.rb gives:

Profile: tests from inspec_example.rb (tests from inspec_example.rb)
Version: (not specified)
Target:  local://

  File /tmp
     ✔  should exist
Brandon Miller
  • 4,695
  • 1
  • 20
  • 27
  • Yes, your simple InSpec test does test for the file; however, you can add Ruby code to the InSpec test in order to loop over a set of files and test for their presence as well, which is what I suspect the OP was trying to do. InSpec and Ruby don't have to be either/or as you imply; they can be both/and. – james.garriss Oct 01 '18 at 17:19
  • @james.garriss I never implied such a thing. I stated that executing the test file using `ruby` directly instead of `inspec exec` caused the given error. The question was also edited to include their example after my answer was given. Previously it only included the error which appeared as if they were just trying to run the simple test. – Brandon Miller Oct 02 '18 at 18:08
0

I'm not familiar with the AWS SDK, but it you want to test for a set of files using InSpec, you can do so like this:

myfiles = %w(temp.err temp.out)

control 'tmp-files-1' do
  title 'Test for a set of temp files.'
  myfiles.each do |myfile|
    describe file('/tmp/' + myfile) do
      it { should exist }
    end
  end
end

If you execute this control, it will return the following (assuming that these files actually exist in your /tmp folder:

  ✔  tmp-files-1: Test for a set of temp files.
     ✔  File /tmp/temp.err should exist
     ✔  File /tmp/temp.out should exist

I'm hoping you can take this example and adapt it to your AWS needs.

james.garriss
  • 12,959
  • 7
  • 83
  • 96