How to use attributes method of Inspec Package

Best Inspec_ruby code snippet using Inspec.attributes

default_spec.rb

Source:default_spec.rb Github

copy

Full Screen

...16# See the License for the specific language governing permissions and17# limitations under the License.18require 'spec_helper'19describe 'audit::default' do20 context 'When all attributes are default, on an unspecified platform' do21 let(:chef_run) do22 runner = ChefSpec::ServerRunner.new(platform: 'ubuntu', version: '18.04')23 runner.converge(described_recipe)24 end25 it 'installs the inspec gem' do26 expect(chef_run).to install_inspec_gem('inspec')27 end28 it 'converges successfully' do29 expect { chef_run }.to_not raise_error30 end31 end32 context 'When an inspec gem version is specified ' do33 let(:chef_run) do34 ChefSpec::ServerRunner.new(platform: 'centos', version: '6') do |node|35 node.override['audit']['inspec_version'] = '0.0.0'36 end.converge(described_recipe)37 end38 it 'installs the inspec gem with the correct version' do39 expect(chef_run).to install_inspec_gem('inspec').with(version: '0.0.0')40 end41 it 'converges successfully' do42 expect { chef_run }.to_not raise_error43 end44 end45 context 'When an inspec gem alternate source is specified ' do46 let(:chef_run) do47 ChefSpec::ServerRunner.new(platform: 'centos', version: '6') do |node|48 node.override['audit']['inspec_gem_source'] = 'http://0.0.0.0:8080'49 end.converge(described_recipe)50 end51 it 'installs the inspec gem from the alternate source' do52 expect(chef_run).to install_inspec_gem('inspec').with(source: 'http://0.0.0.0:8080')53 end54 it 'converges successfully' do55 expect { chef_run }.to_not raise_error56 end57 end58 context 'When server and refresh_token are specified' do59 let(:chef_run) do60 ChefSpec::ServerRunner.new(platform: 'centos', version: '6') do |node|61 node.override['audit']['collector'] = 'chef-compliance'62 node.override['audit']['profiles'] = [ { 'name': 'myprofile', 'compliance': 'admin/myprofile' } ]63 node.override['audit']['server'] = 'https://my.compliance.test/api'64 node.override['audit']['refresh_token'] = 'abcdefg'65 node.override['audit']['insecure'] = true66 end.converge(described_recipe)67 end68 it 'converges successfully' do69 expect { chef_run }.to_not raise_error70 end71 end72 context 'When two profiles are specified' do73 let(:chef_run) do74 runner = ChefSpec::ServerRunner.new(platform: 'centos', version: '6')75 runner.node.override['audit']['profiles'] = [ { 'name': 'myprofile', 'compliance': 'admin/myprofile' }, { 'name': 'ssh', 'compliance': 'base/ssh' } ]76 runner.node.override['audit']['inspec_version'] = 'latest'77 runner.node.override['audit']['quiet'] = true78 runner.converge(described_recipe)79 end80 let(:myprofile) { chef_run.compliance_profile('myprofile') }81 it 'converges successfully' do82 expect { chef_run }.to_not raise_error83 end84 end85 # TODO: need to implement functionality for this86 # context 'When invalid profile is passed' do87 # let(:chef_run) do88 # runner = ChefSpec::ServerRunner.new(platform: 'centos', version: '6.9')89 # runner.node.override['audit']['profiles'] = [ { 'name': 'myprofile', 'compliance': 'myprofile' } ]90 # runner.converge(described_recipe)91 # end92 # it 'does raise an error' do93 # expect { chef_run }.to raise_error("Invalid profile name 'myprofile'. Must contain /, e.g. 'john/ssh'")94 # end95 # end96 context 'When specifying profiles with alternate sources' do97 let(:chef_run) do98 runner = ChefSpec::ServerRunner.new(platform: 'centos', version: '6')99 runner.node.override['audit']['profiles'] = [100 { 'name': 'linux', 'compliance': 'base/linux' },101 { 'name': 'apache', 'compliance': 'base/apache' },102 { 'name': 'ssh-hardening', 'supermarket': 'hardening/ssh-hardening' },103 { 'name': 'brewinc/tmp_compliance_profile',104 'url': 'https://github.com/nathenharvey/tmp_compliance_profile',105 },106 { 'name': 'brewinc/tmp_compliance_profile-master',107 'path': '/tmp/tmp_compliance_profile-master',108 },109 ]110 runner.converge(described_recipe)111 end112 it 'converges successfully' do113 expect { chef_run }.to_not raise_error114 end115 end116 context 'When specifying a single reporter' do117 let(:chef_run) do118 runner = ChefSpec::ServerRunner.new(platform: 'centos', version: '6')119 runner.node.override['audit']['collector'] = 'json-file'120 runner.node.override['audit']['profiles'] = [121 { 'name': 'linux', 'compliance': 'base/linux' },122 { 'name': 'apache', 'compliance': 'base/apache' },123 { 'name': 'ssh-hardening', 'supermarket': 'hardening/ssh-hardening' },124 { 'name': 'brewinc/tmp_compliance_profile',125 'url': 'https://github.com/nathenharvey/tmp_compliance_profile',126 },127 { 'name': 'brewinc/tmp_compliance_profile-master',128 'path': '/tmp/tmp_compliance_profile-master',129 },130 ]131 runner.converge(described_recipe)132 end133 it 'converges successfully' do134 expect { chef_run }.to_not raise_error135 end136 end137 context 'When specifying multiple reporters' do138 let(:chef_run) do139 runner = ChefSpec::ServerRunner.new(platform: 'centos', version: '6')140 runner.node.override['audit']['collector'] = %w(chef-compliance json-file)141 runner.node.override['audit']['profiles'] = [142 { 'name': 'linux', 'compliance': 'base/linux' },143 { 'name': 'apache', 'compliance': 'base/apache' },144 { 'name': 'ssh-hardening', 'supermarket': 'hardening/ssh-hardening' },145 { 'name': 'brewinc/tmp_compliance_profile',146 'url': 'https://github.com/nathenharvey/tmp_compliance_profile',147 },148 { 'name': 'brewinc/tmp_compliance_profile-master',149 'path': '/tmp/tmp_compliance_profile-master',150 },151 ]152 runner.converge(described_recipe)153 end154 it 'converges successfully' do155 expect { chef_run }.to_not raise_error156 end157 end158 context 'when audit attributes are not removed' do159 let(:chef_run) do160 runner = ChefSpec::ServerRunner.new(platform: 'centos', version: '6')161 runner.node.override['audit']['attributes']['my-inspec-attribute'] = 'ok'162 runner.converge(described_recipe)163 end164 it 'still contains the audit attributes after converge' do165 expect(chef_run.node.attributes['audit']['attributes']).to eq('my-inspec-attribute' => 'ok')166 end167 it 'should contain the inspec attributes in the run_state' do168 expect(chef_run.node.run_state['audit_attributes']).to eq('my-inspec-attribute' => 'ok')169 end170 it 'should not raise an exception' do171 expect { chef_run }.to_not raise_error172 end173 end174 context 'when audit attributes are removed' do175 let(:chef_run) do176 runner = ChefSpec::ServerRunner.new(platform: 'centos', version: '6')177 runner.node.override['audit']['attributes']['my-inspec-attribute'] = 'ok'178 runner.node.override['audit']['attributes_save'] = false179 runner.converge(described_recipe)180 end181 it 'should not contain the audit attributes after converge' do182 expect(chef_run.node.attributes['audit']['attributes']).to eq(nil)183 end184 it 'should contain the inspec attributes in the run_state' do185 expect(chef_run.node.run_state['audit_attributes']).to eq('my-inspec-attribute' => 'ok')186 end187 it 'should not raise an exception' do188 expect { chef_run }.to_not raise_error189 end190 end191end...

Full Screen

Full Screen

audit_report_spec.rb

Source:audit_report_spec.rb Github

copy

Full Screen

...89 expect(opts['output']).to eq('/dev/null')90 expect(opts['logger']).to eq(Chef::Log)91 expect(opts[:waiver_file]).to eq([])92 expect(opts[:backend_cache]).to be true93 expect(opts[:attributes].empty?).to be true94 end95 it 'sets the format to json' do96 allow(File).to receive(:exist?).with('/tmp/exists.yaml').and_return(true)97 allow(File).to receive(:exist?).with('/tmp/missing.yaml').and_return(false)98 format = 'json'99 quiet = true100 set_inspec_backend_cache(true)101 mynode.default['audit']['waiver_file'] = ['/tmp/exists.yaml', '/tmp/missing.yaml']102 opts = @audit_report.get_opts(format, quiet, {})103 expect(opts['report']).to be true104 expect(opts['format']).to eq('json')105 expect(opts['output']).to eq('/dev/null')106 expect(opts['logger']).to eq(Chef::Log)107 expect(opts[:waiver_file]).to eq(['/tmp/exists.yaml'])108 expect(opts[:backend_cache]).to be true109 expect(opts[:attributes].empty?).to be true110 end111 it 'sets the backend_cache to false' do112 format = 'json'113 quiet = true114 set_inspec_backend_cache(false)115 opts = @audit_report.get_opts(format, quiet, {})116 expect(opts['report']).to be true117 expect(opts['format']).to eq('json')118 expect(opts['output']).to eq('/dev/null')119 expect(opts['logger']).to eq(Chef::Log)120 expect(opts[:backend_cache]).to be false121 expect(opts[:attributes].empty?).to be true122 end123 it 'sets the attributes' do124 format = 'json-min'125 quiet = true126 attributes = {127 first: 'value1',128 second: 'value2',129 }130 set_inspec_backend_cache(true)131 opts = @audit_report.get_opts(format, quiet, attributes)132 expect(opts[:attributes][:first]).to eq('value1')133 expect(opts[:attributes][:second]).to eq('value2')134 end135 end136 describe 'call' do137 it 'given a profile, returns a json report' do138 opts = { 'report' => true, 'format' => 'json', 'output' => '/dev/null' }139 path = File.expand_path('../../data/mock_profile.rb', __dir__)140 profiles = [{ 'name': 'example', 'path': path }]141 # we circumvent the default load mechanisms, therefore we have to require inspec142 require 'inspec'143 report = @audit_report.call(opts, profiles)144 expected_report = /^.*profiles.*controls.*version.*statistics.*duration.*controls.*/145 expect(report.to_json).to match(expected_report)146 end147 it 'given a profile, returns a json-min report' do...

Full Screen

Full Screen

stack_test_task.rb

Source:stack_test_task.rb Github

copy

Full Screen

...10 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

attributes

Using AI Code Generation

copy

Full Screen

1 title '1.1 Ensure "root" is the only UID 0 account (Scored)'2 desc 'The "root" account is the system administrator account that has unrestricted access to the entire system. It is important to limit the use of this account to only system administrators.'3 describe users.where(uid: 0) do4 its('entries') { should eq ['root'] }5 title '2.1 Ensure Red Hat Subscription Manager connection is configured (Scored)'6 desc 'Red Hat Subscription Manager (RHSM) is a tool used to register, unregister, and manage subscriptions for Red Hat Enterprise Linux (RHEL) systems.'7 describe file('/etc/rhsm/rhsm.conf') do8 it { should exist }9 its('content') { should match(/^hostname = subscription.rhn.redhat.com$/) }10 title '3.1 Ensure package manager repositories are configured (Not Scored)'11 desc 'Package manager repositories are sources of packages that can be installed on the system. It is important to ensure these repositories are configured correctly.'12 describe file('/etc/yum.repos.d/CentOS-Base.repo') do13 it { should exist }14 its('content') { should match(/^gpgcheck=1$/) }

Full Screen

Full Screen

attributes

Using AI Code Generation

copy

Full Screen

1 it { should eq 'redhat' }2 it { should eq 'redhat' }3 it { should eq '7.6' }4 it { should eq 'redhat' }5 it { should eq 'redhat' }6 it { should eq '7.6' }7 it { should eq 'redhat' }8 it { should eq 'redhat' }9 it { should eq '7.6' }10 it { should eq 'redhat' }11 it { should eq 'redhat' }12 it { should eq '7.6' }13 it { should eq 'redhat' }14 it { should eq 'redhat' }15 it { should eq '7.6' }16 it { should eq 'redhat' }

Full Screen

Full Screen

attributes

Using AI Code Generation

copy

Full Screen

1describe file('/tmp/attributes.yml') do2 its('content') { should match(/name: Inspec/) }3 its('content') { should match(/version: 0.1.0/) }4 its('content') { should match(/description: Inspec attributes example/) }5describe file('/tmp/attributes.yml') do6 its('content') { should match(/name: Inspec/) }7 its('content') { should match(/version: 0.1.0/) }8 its('content') { should match(/description: Inspec attributes example/) }9describe file('/tmp/attributes.yml') do10 its('content') { should match(/name: Inspec/) }11 its('content') { should match(/version: 0.1.0/) }12 its('content') { should match(/description: Inspec attributes example/) }13describe file('/tmp/attributes.yml') do14 its('content') { should match(/name: Inspec/) }15 its('content') { should match(/version: 0.1.0/) }16 its('content') { should match(/description: Inspec attributes example/) }17describe file('/tmp/attributes.yml') do18 its('content') { should match(/name: Inspec/) }19 its('content') { should match(/version: 0.1.0/) }20 its('content') { should match(/description: Inspec attributes example/) }21describe file('/tmp/attributes.yml') do22 its('content') { should match(/name: Inspec/) }

Full Screen

Full Screen

attributes

Using AI Code Generation

copy

Full Screen

1my_attribute = attribute('my_attribute')2 it { should eq 'my_value' }3describe attribute('my_attribute') do4 it { should eq 'my_value' }5 it { should eq 'my_value' }6 it { should eq attribute('my_attribute') }7describe attribute('my_attribute') do8 it { should eq 'my_value' }9describe attribute('my_attribute') do10 it { should eq attribute('my_attribute') }11 it { should eq attribute('my_attribute') }12 it { should eq attribute('my_attribute') }

Full Screen

Full Screen

attributes

Using AI Code Generation

copy

Full Screen

1 it { should cmp Inspec.attributes['attribute1'] }2 it { should cmp Inspec.attribute('attribute2', {}) }3 it { should cmp Inspec.attribute('attribute3', {}) }4 it { should cmp Inspec.attribute('attribute4', {}) }5 it { should cmp Inspec.attribute('attribute5', {}) }

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