How to use tests method of Inspec Package

Best Inspec_ruby code snippet using Inspec.tests

stack_test_task.rb

Source:stack_test_task.rb Github

copy

Full Screen

...7 define_inspec_task8 define_aws_configuration_task9 end10 def define_inspec_task11 desc 'Run inspec tests'12 task :test do |t, args|13 if has_inspec_tests?14 create_inspec_attributes.call(args)15 run_inspec_profile.call(args)16 else17 puts "NO TESTS FOR STACK ('#{stack_name}')"18 end19 end20 end21 def has_inspec_tests?22 Dir.exist? ("#{stack_type}/#{stack_name}/tests/inspec")23 end24 def create_inspec_attributes25 lambda do |args|26 mkpath "work/tests/inspec"27 attributes_file_path = "work/tests/inspec/attributes-#{stack_type}-#{stack_name}.yml"28 puts "INFO: Writing inspec attributes to file: #{attributes_file_path}"29 File.open(attributes_file_path, 'w') {|f| 30 f.write(test_attributes(args).to_yaml)31 }32 end33 end34 def test_attributes(args)35 stack_config(args).vars.merge(test_vars(args)).merge(configured_api_users(args))36 end37 def test_vars(args)38 stack_config(args).test_vars || {}39 end40 def configured_api_users(args)41 build_user_configuration_hash(stack_config(args).api_users)42 end43 def build_user_configuration_hash(api_user_hash = {})44 { 'configured_api_users' => api_user_hash }45 end46 def define_aws_configuration_task47 task :aws_configuration do |t, args|48 make_aws_configuration_file.call(args)49 end50 end51 def make_aws_configuration_file52 lambda do |args|53 mkpath aws_configuration_folder54 puts "INFO: Writing AWS configuration file: #{aws_configuration_file_path}"55 File.open(aws_configuration_file_path, 'w') {|f| 56 f.write(aws_configuration_contents(args))57 }58 end59 end60 def aws_configuration_folder61 "work/#{stack_type}/#{stack_name}/aws"62 end63 def aws_configuration_file_path64 "#{aws_configuration_folder}/config"65 end66 def aws_configuration_contents(args)67 <<~END_AWS_CONFIG68 [profile #{assume_role_profile(args)}]69 role_arn = #{stack_config(args).vars['assume_role_arn']}70 source_profile = #{stack_config(args).vars['aws_profile']}71 END_AWS_CONFIG72 end73 def assume_role_profile(args)74 "assume-spin_account_manager-#{stack_config(args).component}"75 end76 def run_inspec_profile77 lambda do |args|78 inspec_profiles = Dir.entries("#{stack_type}/#{stack_name}/tests/inspec").select { |inspec_profile|79 inspec_profile != '..' &&80 File.exists?("#{stack_type}/#{stack_name}/tests/inspec/#{inspec_profile}/inspec.yml")81 }.each { |inspec_profile|82 inspec_profile_name = make_inspec_profile_name(inspec_profile)83 puts "INSPEC (inspec_profile '#{inspec_profile_name}'): #{inspec_cmd(84 inspec_profile: inspec_profile,85 inspec_profile_name: inspec_profile_name,86 aws_profile: assume_role_profile(args),87 aws_region: stack_config(args).region88 )}"89 system(90 inspec_cmd(91 inspec_profile: inspec_profile,92 inspec_profile_name: inspec_profile_name,93 aws_profile: assume_role_profile(args),94 aws_region: stack_config(args).region95 )96 )97 }98 end99 end100 def make_inspec_profile_name(inspec_profile)101 inspec_profile != '.' ? inspec_profile : '__root__'102 end103 def inspec_cmd(inspec_profile:, inspec_profile_name:, aws_profile:, aws_region:)104 "inspec exec " \105 "#{stack_type}/#{stack_name}/tests/inspec/#{inspec_profile} " \106 "-t aws://#{aws_region}/#{aws_profile} " \107 "--reporter json-rspec:work/tests/inspec/results-#{stack_type}-#{stack_name}-#{inspec_profile_name}.json " \108 "cli " \109 "--attrs work/tests/inspec/attributes-#{stack_type}-#{stack_name}.yml"110 end111 end112 end113end...

Full Screen

Full Screen

reporter.rb

Source:reporter.rb Github

copy

Full Screen

...18 statistics = mushy_report.statistics19 version = mushy_report.version20 # Some pass/fail information21 test_results = profiles.map(&:controls).flatten.map(&:results).flatten.map(&:status)22 passed_tests = test_results.select { |text| text == "passed" }.count23 failed_tests = test_results.count - passed_tests24 percent_pass = 100.0 * passed_tests / test_results.count25 percent_fail = 100.0 - percent_pass26 # Detailed OS27 platform_arch = runner.backend.backend.os.arch28 platform_name = runner.backend.backend.os.title29 # Allow template-based settings30 template_config = config.fetch("template_config", {})31 # ... also can use all InSpec resources via "inspec_resource.NAME.PROPERTY"32 output(template.result(binding))33 end34 # Return Constraints.35 #36 # @return [String] Always "~> 0.0"37 def self.run_data_schema_constraints38 "~> 0.0"39 end40 private41 # Read contents of requested template.42 #43 # @return [String] ERB Template44 # @raise [IOError]45 def template_contents46 @template_contents ||= File.read full_path(config["template_file"])47 end48 # Initialize configuration with defaults and Plugin config.49 #50 # @return [Hash] Configuration data after merge51 def config52 @config unless @config.nil?53 # Defaults54 @config = {55 "template_file" => "templates/flex.erb",56 }57 @config.merge! inspec_config.fetch_plugin_config("inspec-reporter-flex")58 @config.merge! config_environment59 logger.debug format("Configuration: %<config>s", config: @config)60 @config61 end62 # Allow (top-level) setting overrides from environment.63 #64 # @return [Hash] Configuration data from environment65 def config_environment66 env_reporter = env.select { |var, _| var.start_with?(ENV_PREFIX) }67 env_reporter.transform_keys { |key| key.delete_prefix(ENV_PREFIX).downcase }68 end69 # Return environment variables.70 #71 # @return [Hash] Mapping of environment variables72 def env73 @env ||= ENV74 end75 # Return InSpec Config object.76 #77 # @return [Inspec::Config] The InSpec config object78 def inspec_config79 @inspec_config ||= Inspec::Config.cached80 end81 # Return InSpec logger.82 #83 # @return [Inspec:Log] The Logger object84 def logger85 @logger ||= Inspec::Log86 end87 # Return InSpec Runner for further inspection.88 #89 # @return [Inspec::Runner] Runner instance90 def runner91 require "binding_of_caller"92 binding.callers.find { |b| b.frame_description == "run_tests" }.receiver93 end94 end95end...

Full Screen

Full Screen

01_simp_profile_inspec_spec.rb

Source:01_simp_profile_inspec_spec.rb Github

copy

Full Screen

...34 ]35 puts info.join("\n")36 @inspec.write_report(@inspec_report[:data])37 end38 it 'should have run some tests' do39 expect(@inspec_report[:data][:failed] + @inspec_report[:data][:passed]).to be > 040 end41 it 'should not have any failing tests' do42 # 2 tests erroneously fail43 # - 'All privileged function executions must be audited':44 # - inspec_profiles/profiles/disa_stig-el7-baseline/controls/V-72095.rb45 # - inspec is expecting watches for executables. We are checking with46 # syscalls instead47 # - 'The system must send rsyslog output to a log aggregation server':48 # - inspec_profiles/profiles/disa_stig-el7-baseline/controls/V-72209.rb49 # - inspec should skip, as rsyslog is not setup50 if @inspec_report[:data][:failed] > 051 puts @inspec_report[:data][:report]52 end53 expect( @inspec_report[:data][:failed] ).to eq(0)54 end55 end56 end...

Full Screen

Full Screen

tests

Using AI Code Generation

copy

Full Screen

1describe tests('test') do2 it { should eq 'test' }3describe tests('test') do4 it { should eq 'test' }5describe tests('test') do6 it { should eq 'test' }7describe tests('test') do8 it { should eq 'test' }9describe tests('test') do10 it { should eq 'test' }11describe tests('test') do12 it { should eq 'test' }

Full Screen

Full Screen

tests

Using AI Code Generation

copy

Full Screen

1 describe file('test') do2 it { should exist }3 describe file('test') do4 it { should exist }5 describe file('test') do6 it { should exist }7 describe file('test') do8 it { should exist }9 describe file('test') do10 it { should exist }11 describe file('test') do12 it { should exist }13 describe file('test') do14 it { should exist }15 describe file('test') do16 it { should exist }17 describe file('test') do18 it { should exist }19 describe file('test') do20 it { should exist }21 describe file('test') do22 it { should exist }23 describe file('test') do24 it { should exist }

Full Screen

Full Screen

tests

Using AI Code Generation

copy

Full Screen

1 expect('a').must_equal 'a'2 expect('a').must_equal 'a'3 expect('a').must_equal 'a'4 expect('a').must_equal 'a'5 expect('a').must_equal 'a'6 expect('a').must_equal 'a'7 expect('a').must_equal 'a'8 expect('a').must_equal 'a'9 expect('a').must_equal 'a'10 expect('a').must_equal 'a'

Full Screen

Full Screen

tests

Using AI Code Generation

copy

Full Screen

1class MyTests < Inspec.plugin(2, :resource)2 describe file('/etc/passwd') do3 it { should exist }4JSON.parse(File.read('1.json'))5JSON.pretty_generate(JSON.parse(File.read('1.json')))6JSON.pretty_generate(JSON.parse(File.read('1.json')))['controls']7JSON.pretty_generate(JSON.parse(File.read('1.json')))['controls']['results']8JSON.pretty_generate(JSON.parse(File.read('1.json')))['controls']['results']['status']9JSON.pretty_generate(JSON.parse

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Inspec_ruby automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful