Best Inspec_ruby code snippet using Inspec.secrets
runner.rb
Source:runner.rb
...8require 'inspec/backend'9require 'inspec/profile_context'10require 'inspec/profile'11require 'inspec/metadata'12require 'inspec/secrets'13require 'inspec/dependencies/cache'14# spec requirements15module Inspec16 #17 # Inspec::Runner coordinates the running of tests and is the main18 # entry point to the application.19 #20 # Users are expected to insantiate a runner, add targets to be run,21 # and then call the run method:22 #23 # ```24 # r = Inspec::Runner.new()25 # r.add_target("/path/to/some/profile")26 # r.add_target("http://url/to/some/profile")27 # r.run28 # ```29 #30 class Runner # rubocop:disable Metrics/ClassLength31 extend Forwardable32 def_delegator :@test_collector, :report33 attr_reader :backend, :rules, :attributes34 def initialize(conf = {})35 @rules = []36 @conf = conf.dup37 @conf[:logger] ||= Logger.new(nil)38 @target_profiles = []39 @controls = @conf[:controls] || []40 @ignore_supports = @conf[:ignore_supports]41 @create_lockfile = @conf[:create_lockfile]42 @cache = Inspec::Cache.new(@conf[:cache])43 @test_collector = @conf.delete(:test_collector) || begin44 require 'inspec/runner_rspec'45 RunnerRspec.new(@conf)46 end47 # list of profile attributes48 @attributes = []49 load_attributes(@conf)50 configure_transport51 end52 def tests53 @test_collector.tests54 end55 def configure_transport56 @backend = Inspec::Backend.create(@conf)57 @test_collector.backend = @backend58 end59 def reset60 @test_collector.reset61 @target_profiles.each do |profile|62 profile.runner_context.rules = {}63 end64 @rules = []65 end66 def load67 all_controls = []68 @target_profiles.each do |profile|69 @test_collector.add_profile(profile)70 write_lockfile(profile) if @create_lockfile71 profile.locked_dependencies72 profile.load_libraries73 @attributes |= profile.runner_context.attributes74 all_controls += profile.collect_tests75 end76 all_controls.each do |rule|77 register_rule(rule) unless rule.nil?78 end79 end80 def run(with = nil)81 Inspec::Log.debug "Starting run with targets: #{@target_profiles.map(&:to_s)}"82 load83 run_tests(with)84 end85 def write_lockfile(profile)86 return false if !profile.writable?87 if profile.lockfile_exists?88 Inspec::Log.debug "Using existing lockfile #{profile.lockfile_path}"89 else90 Inspec::Log.debug "Creating lockfile: #{profile.lockfile_path}"91 lockfile = profile.generate_lockfile92 File.write(profile.lockfile_path, lockfile.to_yaml)93 end94 end95 def run_tests(with = nil)96 @test_collector.run(with)97 end98 # determine all attributes before the execution, fetch data from secrets backend99 def load_attributes(options)100 attributes = {}101 # read endpoints for secrets eg. yml file102 secrets_targets = options['attrs']103 unless secrets_targets.nil?104 secrets_targets.each do |target|105 secrets = Inspec::SecretsBackend.resolve(target)106 # merge hash values107 attributes = attributes.merge(secrets.attributes) unless secrets.nil? || secrets.attributes.nil?108 end109 end110 options['attributes'] = attributes111 end112 #113 # add_target allows the user to add a target whose tests will be114 # run when the user calls the run method.115 #116 # A target is a path or URL that points to a profile. Using this117 # target we generate a Profile and a ProfileContext. The content118 # (libraries, tests, and attributes) from the Profile are loaded119 # into the ProfileContext.120 #121 # If the profile depends on other profiles, those profiles will be...
delivery_inspec.rb
Source:delivery_inspec.rb
...74 #75 # rubocop:disable Metrics/AbcSize76 # rubocop:disable Metrics/MethodLength77 def prepare_linux_inspec78 # Load secrets from delivery-secrets data bag79 secrets = get_project_secrets80 if secrets['inspec'].nil?81 raise 'Could not find secrets for inspec' \82 ' in delivery-secrets data bag.'83 end84 # Variables used for the linux inspec script85 cache = delivery_workspace_cache86 ssh_user = secrets['inspec']['ssh-user']87 ssh_private_key_file = "#{cache}/.ssh/#{secrets['inspec']['ssh-user']}.pem"88 ssh_hostname = @infra_node89 # Create directory for SSH key90 directory = Chef::Resource::Directory.new("#{cache}/.ssh", run_context)91 directory.recursive true92 directory.run_action(:create)93 # Create private key94 file = Chef::Resource::File.new(ssh_private_key_file, run_context).tap do |f|95 f.content secrets['inspec']['ssh-private-key']96 f.sensitive true97 f.mode '0400'98 end99 file.run_action(:create)100 # Create inspec script101 file = Chef::Resource::File.new("#{cache}/inspec.sh", run_context).tap do |f|102 f.content <<-EOF103chef exec inspec exec #{node['delivery']['workspace']['repo']}#{@inspec_test_path} -t ssh://#{ssh_user}@#{ssh_hostname} -i #{ssh_private_key_file}104 EOF105 f.sensitive true106 f.mode '0750'107 end108 file.run_action(:create)109 end110 #111 # Create script for Windows nodes112 #113 def prepare_windows_inspec114 # Load secrets from delivery-secrets data bag115 secrets = get_project_secrets116 if secrets['inspec'].nil?117 raise 'Could not find secrets for inspec' \118 ' in delivery-secrets data bag.'119 end120 # Variables used for the Windows inspec script121 cache = delivery_workspace_cache122 winrm_user = secrets['inspec']['winrm-user']123 winrm_password = secrets['inspec']['winrm-password']124 winrm_hostname = @infra_node125 # Create inspec script126 file = Chef::Resource::File.new("#{cache}/inspec.sh", run_context).tap do |f|127 f.content <<-EOF128chef exec inspec exec #{node['delivery']['workspace']['repo']}#{@inspec_test_path} -t winrm://#{winrm_user}@#{winrm_hostname} --password '#{winrm_password}'129 EOF130 f.sensitive true131 f.mode '0750'132 end133 file.run_action(:create)134 end135 # Returns the Chef::Node Object coming from the run_context136 def node137 run_context && run_context.node...
runner_test.rb
Source:runner_test.rb
...18 end19 end20 describe 'when an attr is provided and has no attributes' do21 it 'returns an empty hash' do22 secrets = mock23 secrets.stubs(:attributes).returns(nil)24 options = { attrs: ['empty.yaml'] }25 Inspec::SecretsBackend.expects(:resolve).with('empty.yaml').returns(secrets)26 runner.load_attributes(options).must_equal({})27 end28 end29 describe 'when an attr is provided and has attributes' do30 it 'returns a hash containing the attributes' do31 options = { attrs: ['file1.yaml'] }32 attributes = { foo: 'bar' }33 secrets = mock34 secrets.stubs(:attributes).returns(attributes)35 Inspec::SecretsBackend.expects(:resolve).with('file1.yaml').returns(secrets)36 runner.load_attributes(options).must_equal(attributes)37 end38 end39 describe 'when multiple attrs are provided and one fails' do40 it 'raises an exception' do41 options = { attrs: ['file1.yaml', 'file2.yaml'] }42 secrets = mock43 secrets.stubs(:attributes).returns(nil)44 Inspec::SecretsBackend.expects(:resolve).with('file1.yaml').returns(secrets)45 Inspec::SecretsBackend.expects(:resolve).with('file2.yaml').returns(nil)46 proc { runner.load_attributes(options) }.must_raise Inspec::Exceptions::SecretsBackendNotFound47 end48 end49 describe 'when multiple attrs are provided and one has no attributes' do50 it 'returns a hash containing the attributes from the valid files' do51 options = { attrs: ['file1.yaml', 'file2.yaml'] }52 attributes = { foo: 'bar' }53 secrets1 = mock54 secrets1.stubs(:attributes).returns(nil)55 secrets2 = mock56 secrets2.stubs(:attributes).returns(attributes)57 Inspec::SecretsBackend.expects(:resolve).with('file1.yaml').returns(secrets1)58 Inspec::SecretsBackend.expects(:resolve).with('file2.yaml').returns(secrets2)59 runner.load_attributes(options).must_equal(attributes)60 end61 end62 describe 'when multiple attrs are provided and all have attributes' do63 it 'returns a hash containing all the attributes' do64 options = { attrs: ['file1.yaml', 'file2.yaml'] }65 secrets1 = mock66 secrets1.stubs(:attributes).returns({ key1: 'value1' })67 secrets2 = mock68 secrets2.stubs(:attributes).returns({ key2: 'value2' })69 Inspec::SecretsBackend.expects(:resolve).with('file1.yaml').returns(secrets1)70 Inspec::SecretsBackend.expects(:resolve).with('file2.yaml').returns(secrets2)71 runner.load_attributes(options).must_equal({ key1: 'value1', key2: 'value2' })72 end73 end74 end75end...
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!!