-4

我正在使用带有 ruby​​ 的 inspec 测试框架进行基础设施测试。我在控件中写了一个测试

这是我的测试:

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

但在执行时,我得到以下错误

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 审计资源,用于测试所有系统文件类型,包括文件、目录、符号链接、命名管道、套接字等。

#InspecWithRuby #inspec #inspecResourcesNotIdentified #InspecResourcesNotFound 
4

2 回答 2

0

我不熟悉 AWS 开发工具包,但如果您想使用 InSpec 测试一组文件,您可以这样做:

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

如果您执行此控件,它将返回以下内容(假设这些文件实际存在于您的/tmp文件夹中:

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

我希望您可以采用此示例并使其适应您的 AWS 需求。

于 2018-10-01T17:26:11.573 回答
0

在我看来,您正试图inspec通过执行ruby而不是运行来运行测试inspec exec。我可以通过将您的测试粘贴到文件中来在本地重现:

inspec_example.rb

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

直接用ruby执行

ruby inspec_example.rb给出:

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

inspec exec按预期执行:

inspec exec inspec_example.rb给出:

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

  File /tmp
     ✔  should exist
于 2018-06-07T02:35:27.737 回答