Best Inspec_ruby code snippet using Inspec.describe
generate.rb
Source:generate.rb
1# parses CloudFormation JSON files2require "inspec/objects/control"3require "inspec/objects/ruby_helper"4require "inspec/objects/describe"5require "inspec-iggy/file_helper"6require "inspec-iggy/inspec_helper"7module InspecPlugins::Iggy::CloudFormation8 class Generate9 # parse through the JSON and generate InSpec controls10 def self.parse_generate(cfn_template) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Metrics/PerceivedComplexity11 template = InspecPlugins::Iggy::FileHelper.parse_json(cfn_template)12 absolutename = File.absolute_path(cfn_template)13 # InSpec controls generated14 generated_controls = []15 # iterate over the resources16 cfn_resources = template["Resources"]17 cfn_resources.keys.each do |cfn_res|18 # split out the last ::, these are all AWS19 cfn_resource = cfn_resources[cfn_res]["Type"].split("::").last20 # split camelcase and join with underscores21 cfn_res_type = "aws_" + cfn_resource.split(/(?=[A-Z])/).join("_").downcase22 # add translation layer23 if InspecPlugins::Iggy::InspecHelper::TRANSLATED_RESOURCES.key?(cfn_res_type)24 Inspec::Log.debug "CloudFormation::Generate.parse_generate cfn_res_type = #{cfn_res_type} #{InspecPlugins::Iggy::InspecHelper::TRANSLATED_RESOURCES[cfn_res_type]} TRANSLATED"25 cfn_res_type = InspecPlugins::Iggy::InspecHelper::TRANSLATED_RESOURCES[cfn_res_type]26 end27 # does this match an InSpec resource?28 if InspecPlugins::Iggy::InspecHelper.available_resources.include?(cfn_res_type)29 Inspec::Log.debug "CloudFormation::Generate.parse_generate cfn_res_type = #{cfn_res_type} MATCHED"30 # insert new control based off the resource's ID31 ctrl = Inspec::Control.new32 ctrl.id = "#{cfn_res_type}::#{cfn_res}"33 ctrl.title = "InSpec-Iggy #{cfn_res_type}::#{cfn_res}"34 ctrl.descriptions["default"] = "#{cfn_res_type}::#{cfn_res} from the source file #{absolutename}\nGenerated by InSpec-Iggy v#{InspecPlugins::Iggy::VERSION}"35 ctrl.impact = "1.0"36 describe = Inspec::Describe.new37 # describes the resource with the logical_resource_id as argument, replaced at inspec exec38 describe.qualifier.push([cfn_res_type, "resources[#{cfn_res}]"])39 # ensure the resource exists40 describe.add_test(nil, "exist", nil)41 # EC2 instances should be running42 describe.add_test(nil, "be_running", nil) if cfn_res_type.eql?("aws_ec2_instance")43 # if there's a match, see if there are matching InSpec properties44 inspec_properties = InspecPlugins::Iggy::InspecHelper.resource_properties(cfn_res_type, "aws")45 cfn_resources[cfn_res]["Properties"].keys.each do |attr|46 # insert '_' on the CamelCase to get camel_case47 attr_split = attr.split(/(?=[A-Z])/)48 property = attr_split.join("_").downcase49 if inspec_properties.member?(property)50 Inspec::Log.debug "CloudFormation::Generate.parse_generate #{cfn_res_type} inspec_property = #{property} MATCHED"51 value = cfn_resources[cfn_res]["Properties"][attr]52 if (value.is_a? Hash) || (value.is_a? Array)53 # these get replaced at inspec exec54 if property.eql?("vpc_id") # rubocop:disable Metrics/BlockNesting55 vpc = cfn_resources[cfn_res]["Properties"][attr].values.first56 # https://github.com/inspec/inspec/issues/317357 describe.add_test(property, "cmp", "resources[#{vpc}]") unless cfn_res_type.eql?("aws_route_table") # rubocop:disable Metrics/BlockNesting58 # AMI is a Ref into Parameters59 elsif property.eql?("image_id") # rubocop:disable Metrics/BlockNesting60 amiref = cfn_resources[cfn_res]["Properties"][attr].values.first61 ami = template["Parameters"][amiref]["Default"]62 describe.add_test(property, "cmp", ami)63 end64 else65 describe.add_test(property, "cmp", value)66 end67 else68 Inspec::Log.debug "CloudFormation::Generate.parse_generate #{cfn_res_type} inspec_property = #{property} SKIPPED"69 end70 end71 ctrl.add_test(describe)72 generated_controls.push(ctrl)73 else74 Inspec::Log.debug "CloudFormation::Generate.parse_generate cfn_res_type = #{cfn_res_type} SKIPPED"75 end76 end77 Inspec::Log.debug "CloudFormation::Generate.parse_generate generated_controls = #{generated_controls}"78 generated_controls79 end80 end81end...
task_inspec_spec.rb
Source:task_inspec_spec.rb
1# run a test task2require 'spec_helper_acceptance'3describe 'test::role with inspec' do4 include Beaker::TaskHelper::Inventory5 include BoltSpec::Run6 describe 'test_tool=inspec' do7 it 'fails to run' do8 # inspec requires build-essential for gem install boooooo9 shell('puppet resource package build-essential ensure=installed')10 task_result = task_run('test::role', '', '', '', 'test_tool' => 'inspec')11 expect(task_result[0]['status']).to eq('failure')12 end13 it 'returns helpful error message' do14 task_result = task_run('test::role', '', '', '', 'test_tool' => 'inspec')15 expect(task_result[0]['result']['_output']).to match(%r{unable\sto\sdetect\sthis\snode.*\srole\susing\sfacter})16 end17 end18 describe 'test_tool=inspec, test_file=example_pass.rb' do19 it 'installs inspec gem' do20 task_run('test::role', '', '', '', 'test_tool' => 'inspec', 'test_file' => 'example_pass.rb')21 expect(shell('ls /tmp/puppet_test/inspec/gems/inspec*/inspec.gemspec').exit_code).to eq(0)22 end23 it 'runs a passing test successfully' do24 task_result = task_run('test::role', '', '', '', 'test_tool' => 'inspec', 'test_file' => 'example_pass.rb')25 expect(task_result[0]['status']).to eq('success')26 end27 it 'returns output' do28 task_result = task_run('test::role', '', '', '', 'test_tool' => 'inspec', 'test_file' => 'example_pass.rb')29 expect(task_result[0]['result']['_output']).to match(%r{\d+\sexamples,\s0\sfailures})30 end31 end32 describe 'test_tool=inspec, test_file=example_pass.rb, return_status=false' do33 it 'does not return the status from the test' do34 task_result = task_run('test::role', '', '', '', 'test_tool' => 'inspec', 'test_file' => 'example_fail.rb', 'return_status' => false)35 expect(task_result[0]['status']).to eq('success')36 end37 end38 describe 'test_tool=inspec, test_file=example_pass.rb, return_status=true' do39 it 'returns status from test - success' do40 task_result = task_run('test::role', '', '', '', 'test_tool' => 'inspec', 'test_file' => 'example_pass.rb', 'return_status' => true)41 expect(task_result[0]['status']).to eq('success')42 end43 it 'returns status from test - failure' do44 task_result = task_run('test::role', '', '', '', 'test_tool' => 'inspec', 'test_file' => 'example_fail.rb', 'return_status' => true)45 expect(task_result[0]['status']).to eq('failure')46 end47 end48end...
read_stig_json.rb
Source:read_stig_json.rb
...35 inspec_file = "src/inspec/#{control}.rb"36 if ! File.file?(inspec_file)37 inspec = <<~HEREDOC38 # START_DESCRIBE #{control}39 # describe file('/etc') do40 # it { should be_directory }41 # end42 # END_DESCRIBE #{control}43 HEREDOC44 puts "writing #{inspec_file}"45 File.write(inspec_file, inspec)46 else47 puts "reading #{inspec_file}"48 inspec = File.read(inspec_file)49 end50 inspec51end52# describe service('autofs') do53# it { should_not be_enabled }54# it { should_not be_running }55# end56controls = stig['findings'].keys57controls.each do |control|58 finding = stig['findings'][control]59 output = <<~HEREDOC60 # encoding: utf-861 # copyright: 2016, you62 # license: All rights reserved63 # date: #{stig["date"]}64 # description: #{stig["description"]}65 # impacts66 title '#{control} - #{finding['title']}'...
describe
Using AI Code Generation
1describe service('httpd') do2 it { should be_installed }3 it { should be_enabled }4 it { should be_running }5describe service('httpd') do6 it { should_not be_installed }7 it { should_not be_enabled }8 it { should_not be_running }9describe service('httpd') do10 it { should be_installed }11 it { should_not be_enabled }12 it { should_not be_running }13describe service('httpd') do14 it { should_not be_installed }15 it { should be_enabled }16 it { should_not be_running }17describe service('httpd') do18 it { should_not be_installed }19 it { should_not be_enabled }20 it { should be_running }21describe service('httpd') do22 it { should be_installed }23 it { should be_enabled }24 it { should_not be_running }25describe service('httpd') do26 it { should be_installed }27 it { should_not be_enabled }28 it { should be_running }29describe service('httpd') do30 it { should_not be_installed }31 it { should be_enabled }32 it { should be_running }33describe service('httpd') do34 it { should be_installed }35 it { should be_enabled }36 it { should be_running }37describe service('httpd') do38 it { should_not be_installed }39 it { should_not be_enabled }40 it { should_not be_running }
describe
Using AI Code Generation
1describe file('/etc/passwd') do2 it { should exist }3describe file('/etc/passwd') do4 it { should exist }5describe file('/etc/passwd') do6 it { should exist }7describe file('/etc/passwd') do8 it { should exist }9describe file('/etc/passwd') do10 it { should exist }11describe file('/etc/passwd') do12 it { should exist }13describe file('/etc/passwd') do14 it { should exist }15describe file('/etc/passwd') do16 it { should exist }17describe file('/etc/passwd') do18 it { should exist }19describe file('/etc/passwd') do20 it { should exist }21describe file('/etc/passwd') do22 it { should exist }23describe file('/etc/passwd') do24 it { should exist }25describe file('/etc/passwd') do26 it { should exist }27describe file('/etc/passwd') do28 it { should exist }29describe file('/etc/passwd') do
describe
Using AI Code Generation
1describe command('ls -l /etc/passwd') do2 its('stdout') { should match /root/ }3describe command('ls -l /etc/passwd') do4 its('stdout') { should match /root/ }5describe command('ls -l /etc/passwd') do6 its('stdout') { should match /root/ }7describe command('ls -l /etc/passwd') do8 its('stdout') { should match /root/ }9describe command('ls -l /etc/passwd') do10 its('stdout') { should match /root/ }11describe command('ls -l /etc/passwd') do12 its('stdout') { should match /root/ }13describe command('ls -l /etc/passwd') do14 its('stdout') { should match /root/ }15describe command('ls -l /etc/passwd') do16 its('stdout') { should match /root/ }17describe command('ls -l /etc/passwd') do18 its('stdout') { should match /root/ }19describe command('ls -l /etc/passwd') do20 its('stdout') { should match /root/ }21describe command('ls -l /etc/passwd') do22 its('stdout') { should match /root/ }23describe command('
describe
Using AI Code Generation
1 describe file('/tmp') do2 it { should be_directory }3 describe file('/tmp') do4 it { should be_directory }5 describe file('/tmp') do6 it { should be_directory }
describe
Using AI Code Generation
1describe file('C:\Users\Public\Documents\test.txt') do2 it { should exist }3 its('content') { should match /Hello/ }4describe file('C:\Users\Public\Documents\test.txt') do5 it { should exist }6 its('content') { should match /World/ }7describe file('C:\Users\Public\Documents\test.txt') do8 it { should exist }9 its('content') { should match /Hello World/ }10describe file('C:\Users\Public\Documents\test.txt') do11 it { should exist }12 its('content') { should match /Hello World/ }13describe file('C:\Users\Public\Documents\test.txt') do14 it { should exist }15 its('content') { should match /Hello World/ }16describe file('C:\Users\Public\Documents\test.txt') do17 it { should exist }18 its('content') { should match /Hello World/ }19describe file('C:\Users\Public\Documents\test.txt') do20 it { should exist }21 its('content') { should match /Hello World/ }22describe file('C:\Users\Public\Documents\test.txt') do23 it { should exist }24 its('content') { should match /Hello World/ }25describe file('C:\Users\Public\Documents\test.txt') do26 it { should exist }27 its('content') { should match /Hello World/ }28describe file('C:\
describe
Using AI Code Generation
1describe command('ps -ef | grep httpd | grep -v grep') do2 its('stdout') { should_not eq '' }3describe file('path/to/file') do4 it { should exist }5describe package('package_name') do6 it { should be_installed }7describe service('service_name') do8 it { should be_installed }9 it { should be_enabled }10 it { should be_running }11describe port(80) do12 it { should be_listening }13describe user('user_name') do14 it { should exist }15describe group('group_name') do16 it { should exist }17describe directory('directory_name') do18 it { should exist }19describe file('file_name') do20 its('content') { should match /string/ }21describe file('file_name') do22 its('content') { should_not match /string/ }23describe file('file_name') do24 its('content') { should include 'string' }25describe file('file_name') do26 its('content') { should_not include 'string' }27describe file('file_name') do28 its('content') { should eq 'string' }29describe file('file_name') do30 its('content') { should_not eq 'string' }
describe
Using AI Code Generation
1describe file('/tmp/1.txt') do2 it { should exist }3 its('content') { should match /hello/ }4describe file('/tmp/test.txt') do5 it { should exist }6 its('content') { should match /test/ }
describe
Using AI Code Generation
1describe file('/tmp/test.txt') do2 it { should exist }3 it { should be_file }4 its('content') { should match /hello/ }5describe file('/tmp/test.txt') do6 it { should exist }7 it { should be_file }8 its('content') { should match /world/ }9describe package('httpd') do10 it { should be_installed }11 its('version') { should match /2.4.6/ }12describe package('httpd') do13 it { should be_installed }14 its('version') { should match /2.4.7/ }15describe service('httpd') do16 it { should be_installed }17 it { should be_enabled }18 it { should be_running }
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!