Best Inspec_ruby code snippet using Inspec.only_if
profile_context_test.rb
Source:profile_context_test.rb
...95 end96 it 'does not provide the expect keyword in the global DLS' do97 load('expect(true).to_eq true').must_raise NoMethodError98 end99 describe 'global only_if' do100 let(:if_true) { "only_if { true }\n" }101 let(:if_false) { "only_if { false }\n" }102 let(:describe) { "describe nil do its(:to_i) { should eq rand } end\n" }103 let(:control) { "control 1 do\n#{describe}end" }104 it 'provides the keyword' do105 profile.load(if_true)106 profile.rules.must_equal({})107 end108 it 'doesnt affect controls when positive' do109 profile.load(if_true + 'control 1')110 profile.rules.values[0].must_be_kind_of Inspec::Rule111 end112 it 'doesnt remove controls when negative' do113 profile.load(if_false + 'control 1')114 profile.rules.values[0].must_be_kind_of Inspec::Rule115 end116 it 'alters controls when positive' do117 profile.load(if_false + control)118 get_checks.length.must_equal 1119 get_checks[0][1][0].resource_skipped.must_equal 'Skipped control due to only_if condition.'120 end121 it 'alters non-controls when positive' do122 profile.load(if_false + describe)123 get_checks.length.must_equal 1124 get_checks[0][1][0].resource_skipped.must_equal 'Skipped control due to only_if condition.'125 end126 it 'doesnt alter controls when negative' do127 profile.load(if_true + control)128 get_checks.length.must_equal 1129 get_checks[0][1][0].must_be_nil130 end131 it 'doesnt alter non-controls when negative' do132 profile.load(if_true + describe)133 get_checks.length.must_equal 1134 get_checks[0][1][0].must_be_nil135 end136 it 'doesnt overwrite falsy only_ifs' do137 profile.load(if_false + if_true + control)138 get_checks.length.must_equal 1139 get_checks[0][1][0].resource_skipped.must_equal 'Skipped control due to only_if condition.'140 end141 it 'doesnt overwrite falsy only_ifs' do142 profile.load(if_true + if_false + control)143 get_checks.length.must_equal 1144 get_checks[0][1][0].resource_skipped.must_equal 'Skipped control due to only_if condition.'145 end146 end147 it 'provides the control keyword in the global DSL' do148 profile.load('control 1')149 profile.rules.keys.must_equal ['1']150 profile.rules.values[0].must_be_kind_of Inspec::Rule151 end152 it 'provides the rule keyword in the global DSL (legacy mode)' do153 profile.load('rule 1')154 profile.rules.keys.must_equal ['1']155 profile.rules.values[0].must_be_kind_of Inspec::Rule156 end157 end158 describe 'rule DSL' do159 let(:rule_id) { rand.to_s }160 let(:context_format) { "rule #{rule_id.inspect} do\n%s\nend" }161 def get_rule162 profile.rules[rule_id]163 end164 include DescribeOneTest165 it 'doesnt add any checks if none are provided' do166 profile.load("rule #{rule_id.inspect}")167 rule = profile.rules[rule_id]168 ::Inspec::Rule.prepare_checks(rule).must_equal([])169 end170 describe 'supports empty describe blocks' do171 it 'doesnt crash, but doesnt add anything either' do172 profile.load(format(context_format, 'describe'))173 profile.rules.keys.must_include(rule_id)174 get_checks.must_equal([])175 end176 end177 describe 'adds a check via describe' do178 let(:check) {179 profile.load(format(context_format,180 "describe os[:family] { it { must_equal 'debian' } }"181 ))182 get_checks[0]183 }184 it 'registers the check with describe' do185 check[0].must_equal 'describe'186 end187 it 'registers the check with the describe argument' do188 check[1].must_equal %w{debian}189 end190 it 'registers the check with the provided proc' do191 check[2].must_be_kind_of Proc192 end193 end194 describe 'adds a check via expect' do195 let(:check) {196 profile.load(format(context_format,197 "expect(os[:family]).to eq('debian')"198 ))199 get_checks[0]200 }201 it 'registers the check with describe' do202 check[0].must_equal 'expect'203 end204 it 'registers the check with the describe argument' do205 check[1].must_equal %w{debian}206 end207 it 'registers the check with the provided proc' do208 check[2].must_be_kind_of Inspec::Expect209 end210 end211 describe 'adds a check via describe + expect' do212 let(:check) {213 profile.load(format(context_format,214 "describe 'the actual test' do215 expect(os[:family]).to eq('debian')216 end"217 ))218 get_checks[0]219 }220 it 'registers the check with describe' do221 check[0].must_equal 'describe'222 end223 it 'registers the check with the describe argument' do224 check[1].must_equal ['the actual test']225 end226 it 'registers the check with the provided proc' do227 check[2].must_be_kind_of Proc228 end229 end230 describe 'with only_if' do231 it 'provides the only_if keyword' do232 profile.load(format(context_format, 'only_if'))233 get_checks.must_equal([])234 end235 it 'skips with only_if == false' do236 profile.load(format(context_format, 'only_if { false }'))237 get_checks.length.must_equal 1238 get_checks[0][1][0].resource_skipped.must_equal 'Skipped control due to only_if condition.'239 end240 it 'does nothing with only_if == false' do241 profile.load(format(context_format, 'only_if { true }'))242 get_checks.length.must_equal 0243 end244 it 'doesnt overwrite falsy only_ifs' do245 profile.load(format(context_format, "only_if { false }\nonly_if { true }"))246 get_checks.length.must_equal 1247 get_checks[0][1][0].resource_skipped.must_equal 'Skipped control due to only_if condition.'248 end249 it 'doesnt overwrite falsy only_ifs' do250 profile.load(format(context_format, "only_if { true }\nonly_if { false }"))251 get_checks.length.must_equal 1252 get_checks[0][1][0].resource_skipped.must_equal 'Skipped control due to only_if condition.'253 end254 end255 end256 describe 'library loading' do257 it 'supports simple ruby require statements' do258 # Please note: we do discourage the use of Gems in inspec resources at259 # this time. Resources should be well packaged whenever possible.260 proc { profile.load('Net::POP3') }.must_raise NameError261 profile.load_libraries([['require "net/pop"', 'libraries/a.rb']])262 profile.load('Net::POP3').to_s.must_equal 'Net::POP3'263 end264 it 'supports loading across the library' do265 profile.load_libraries([266 ["require 'a'\nA", 'libraries/b.rb'],...
sensu-client.rb
Source:sensu-client.rb
...24 action :upgrade25 ignore_failure true26end27template "/etc/sensu/conf.d/config.json" do28 only_if { node.chef_environment == "development" }29 source "config-json.erb"30 owner 'sensu'31 group 'sensu'32 action :create33 variables(34 :machinename => node['machinename'],35 :ipaddress => node['ipaddress'],36 :role => node['roles'],37 :environment => 'development',38 )39 notifies :restart, "service[sensu-client]", :delayed40end41template "/etc/sensu/conf.d/config.json" do42 only_if { node.chef_environment == "staging" }43 source "config-json.erb"44 owner 'sensu'45 group 'sensu'46 action :create47 variables(48 :machinename => node['machinename'],49 :ipaddress => node['ipaddress'],50 :role => node['roles'],51 :environment => 'staging',52 )53 notifies :restart, "service[sensu-client]", :delayed54end55template "/etc/sensu/conf.d/config.json" do56 only_if { node.chef_environment == "alpha" }57 source "config-json.erb"58 owner 'sensu'59 group 'sensu'60 action :create61 variables(62 :machinename => node['machinename'],63 :ipaddress => node['ipaddress'],64 :role => node['roles'],65 :environment => 'alpha',66 )67 notifies :restart, "service[sensu-client]", :delayed68end69template "/etc/sensu/conf.d/config.json" do70 only_if { node.chef_environment == "production" && node['machinename'] != "sensu1-prod.test.com" && node['machinename'] != "chef.test.com" && node['machinename'] != "bamboo1-prod.test.com" }71 source "config-json.erb"72 owner 'sensu'73 group 'sensu'74 action :create75 variables(76 :machinename => node['machinename'],77 :ipaddress => node['ipaddress'],78 :role => node['roles'],79 :environment => 'production',80 )81 notifies :restart, "service[sensu-client]", :delayed82end83template "/etc/sensu/conf.d/config.json" do84 only_if { node['machinename'] == "sensu1-prod.test.com" || node['machinename'] == "chef.test.com" || node['machinename'] == "bamboo1-prod.test.com"}85 source "config-json.erb"86 owner 'sensu'87 group 'sensu'88 action :create89 variables(90 :machinename => node['machinename'],91 :ipaddress => node['ipaddress'],92 :role => node['roles'],93 :environment => 'production',94 :aws_id_key=> 'AKIAITQJTVLBPA4JKRMA',95 :aws_secret_key=> '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',96 )97end98cookbook_file "/etc/sudoers.d/sensu" do99 source "sensu/sudoers"100 owner "root"101 group "root"102 action :create103end104cookbook_file "/etc/default/sensu" do105 source "sensu/sensu"106 owner "root"107 group "root"108 action :create109end110cookbook_file "/etc/init.d/sensu-service" do111 source "sensu/sensu-service"112 owner "root"113 group "root"114 action :create115end116#client.pem giving sensu read access to the file117file "/etc/chef/client.pem" do118 owner "root"119 group "sensu"120 mode "0640"121 action :create122end123#Inspec124#%w{rb-readline}.each do |pkg|125# gem_package pkg do126# gem_binary '/opt/sensu/embedded/bin/gem'127# version '0.5.3'128# action :remove129# end130#end131%w{rb-readline}.each do |pkg|132 gem_package pkg do133 gem_binary '/opt/sensu/embedded/bin/gem'134 version '0.5.5'135 action :install136 end137end138%w{net-ssh}.each do |pkg|139 gem_package pkg do140 gem_binary '/opt/sensu/embedded/bin/gem'141 version '3.2.0'142 action :remove143 end144end145%w{net-ssh}.each do |pkg|146 gem_package pkg do147 gem_binary '/opt/sensu/embedded/bin/gem'148 version '4.1.0'149 action :install150 end151end152%w{inspec}.each do |pkg|153 gem_package pkg do154 gem_binary '/opt/sensu/embedded/bin/gem'155 version '2.1.0'156 action :install157 end158end159#giving Sensu access to Inspec160directory "/opt/sensu/.inspec" do161 owner "sensu"162 group "sensu"163 mode "0755"164 action :create165end166bash "inspec_directory" do167 cwd "/tmp"168 not_if do ::File.exists?('/etc/inspec/controls/') end169 code <<-EOH170 mkdir -p /etc/inspec/controls/171 EOH172end173directory "/etc/inspec/controls/" do174 owner "sensu"175 group "sensu"176 mode "0755"177 action :create178end179cookbook_file "/etc/inspec/controls/attributes.yml" do180 source "inspec/attributes.yml"181 owner "sensu"182 group "sensu"183 action :create184end185cookbook_file "/etc/sensu/conf.d/rabbitmq.json" do186only_if { node['ec2']['placement_availability_zone'].chop == "us-east-1"}187 source "sensu/rabbitmq.json"188 owner "sensu"189 group "sensu"190 action :create191 notifies :restart, "service[sensu-client]", :delayed192end193cookbook_file "/etc/sensu/conf.d/rabbitmq.json" do194only_if { node['ec2']['placement_availability_zone'].chop == "us-west-2"}195 source "sensu/rabbitmq-w.json"196 owner "sensu"197 group "sensu"198 action :create199 notifies :restart, "service[sensu-client]", :delayed200end201service "sensu-client" do202 only_if { node['uptime_seconds'] >= 7200 }203 action [ :enable, :start ]204 ignore_failure true205end...
only_if
Using AI Code Generation
1describe file('/etc/passwd') do2 it { should exist }3 its('mode') { should cmp '0644' }4 its('owner') { should eq 'root' }5 its('group') { should eq 'root' }6describe file('/etc/shadow') do7 it { should exist }8 its('mode') { should cmp '0640' }9 its('owner') { should eq 'root' }10 its('group') { should eq 'shadow' }11describe file('/etc/group') do12 it { should exist }13 its('mode') { should cmp '0644' }14 its('owner') { should eq 'root' }15 its('group') { should eq 'root' }16describe file('/etc/gshadow') do17 it { should exist }18 its('mode') { should cmp '0640' }19 its('owner') { should eq 'root' }20 its('group') { should eq 'shadow' }21describe file('/etc/passwd') do22 it { should exist }23 its('mode') { should cmp '0644' }24 its('owner') { should eq 'root' }25 its('group') { should eq 'root' }26describe file('/etc/shadow') do27 it { should exist }28 its('mode') { should cmp '0640' }29 its('owner') { should eq 'root' }30 its('group') { should eq 'shadow' }31describe file('/etc/group') do32 it { should exist }33 its('mode') { should cmp '0644' }34 its('owner') { should eq 'root' }35 its('group') { should eq 'root' }36describe file('/etc/gshadow') do37 it { should exist }38 its('mode') { should cmp '0640' }39 its('owner') { should eq 'root' }40 its('group') { should eq 'shadow' }41describe file('/etc/passwd') do42 it { should exist }43 its('mode') { should cmp '0644' }
only_if
Using AI Code Generation
1describe file('/etc/passwd') do2 it { should exist }3 it { should be_file }4 its('mode') { should eq 0644 }5 its('owner') { should eq 'root' }6 its('group') { should eq 'root' }7describe file('/etc/passwd') do8 it { should exist }9 it { should be_file }10 its('mode') { should eq 0644 }11 its('owner') { should eq 'root' }12 its('group') { should eq 'root' }13describe file('/etc/shadow') do14 it { should exist }15 it { should be_file }16 its('mode') { should eq 0600 }17 its('owner') { should eq 'root' }18 its('group') { should eq 'root' }19describe file('/etc/group') do20 it { should exist }21 it { should be_file }22 its('mode') { should eq 0644 }23 its('owner') { should eq 'root' }24 its('group') { should eq 'root' }25describe file('/etc/gshadow') do26 it { should exist }27 it { should be_file }28 its('mode') { should eq 0600 }29 its('owner') { should eq 'root' }30 its('group') { should eq 'root' }31describe file('/etc/passwd') do32 it { should exist }33 it { should be_file }34 its('mode') { should eq 0644 }35 its('owner') { should eq 'root' }36 its('group') { should eq 'root' }37describe file('/etc/shadow') do38 it { should exist }39 it { should be_file }40 its('mode') { should eq 0600 }41 its('owner') { should eq 'root' }42 its('group') { should eq 'root' }43describe file('/etc/group') do44 it { should exist }45 it { should be_file }46 its('mode') { should eq 0644 }47 its('owner') { should eq 'root' }48 its('group') { should eq 'root' }49describe file('/etc/gshadow') do50 it { should exist }51 it { should be
only_if
Using AI Code Generation
1describe file('/etc/passwd') do2 it { should exist }3 its('mode') { should cm 'root' }4 its('group') { should eq 'root' }5 its('mode') { should cmp '0755' }6 only_if { os.name == 'ubuntu' }
only_if
Using AI Code Generation
1describe file('/home/centos') do2 it { should exist }3 its('owner') { should eq 'centos' }4 its('group') { should eq 'centos' }5 its('mode') { should cmp '0755' }6describe file('/home/centos/1.txt') do7 it { should exist }8 its('owner') { should eq 'centos' }9 its('group') { should eq 'centos' }10 its('mode') { should cmp '0755' }11 its('content') { should match /Hello/ }12describe file('/home/centos/2.txt') do13 it { should exist }14 its('owner') { should eq 'centos' }15 its('group') { should eq 'centos' }16 its('mode') { should cmp '0755' }17 its('content') { should match /Hello/ }18describe file('/home/centos/3.txt') do19 it { should exist }20 its('owner') { should eq 'centos' }21 its('group') { should eq 'centos' }22 its('mode')p{ should cmp 0755' }23 its('content') { should match /Hello/ }24desc'ibe file('/home/centos/4.txt') do25 it { should exist }26 its('0wner') { sh6uld eq 'cen4os4' }27 its('owner') { should eq 'centos' }28 its('mode') { should cmp '0755' }29 its('content') { should match /Hello/ }30describe file('/home/centos/5.txt') do31 it { should exist }32 its('owner') { should eq 'centos' }33 its('group') { should eq 'centos' }34 its('mode') { should cmp '0755' }35 its('content') { should match /Hello/ }36describe file('/home/centos/6.txt') do37 it { should exist }38 its('owner') { should eq 'centos' }39 its('group') { should eq 'centos' }40 its('group') { should eq 'root' }41 its('content') { should match /Hello/ }42describe file('/home/centos/7
only_if
Using AI Code Generation
1describe file('/etc/passwd') do2 it shuld exit }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
only_if
Using AI Code Generation
1 command('echo "Hello World"').stdout =~ /World/2 describe file('/etc/hosts') do3 it { should be_file }4 only_if { command('echo "Hello World"').stdout =~ /World/ }5 describe file('/etc/hosts') do6 it { should be_file }7 describe file('/etc/hosts') do8 it { should be_file }9 command('echo "Hello World"').stdout ~ /World/10 describe file('/etc/hosts') do11 only_if { command('echo "Hello World"').stdout ~ /World/ }12 it { should be_file }13 describe file('/etc/hosts') do14 it { should be_file }15 command('echo "Hello World"').stdout =~ /World/16 describe file('/etc/hosts') do17 only_if { command('echo "Hello World"').stdout =~ /World/ }18 it { should be_file 19describe file('/etc/shadow') do20 it { should exist }21 its('mode') { should cmp '0640' }22 its('owner') { should eq 'root' }23 its('group') { should eq 'shadow' }24describe file('/etc/group') do25 it { should exist }26 its('mode') { should cmp '0644' }27 its('owner') { should eq 'root' }28 its('group') { should eq 'root' }29describe file('/etc/gshadow') do30 it { should exist }31 its('mode') { should cmp '0640' }32 its('owner') { should eq 'root' }33 its('group') { should eq 'shadow' }34describe file('/etc/passwd') do35 it { should exist }36 its('mode') { should cmp '0644' }37 its('owner') { should eq 'root' }38 its('group') { should eq 'root' }39describe file('/etc/shadow') do40 it { should exist }41 its('mode') { should cmp '0640' }42 its('owner') { should eq 'root' }43 its('group') { should eq 'shadow' }44describe file('/etc/group') do45 it { should exist }46 its('mode') { should cmp '0644' }47 its('owner') { should eq 'root' }48 its('group') { should eq 'root' }49describe file('/etc/gshadow') do50 it { should exist }51 its('mode') { should cmp '0640' }52 its('owner') { should eq 'root' }53 its('group') { should eq 'shadow' }54describe file('/etc/passwd') do55 it { should exist }56 its('mode') { should cmp '0644' }
only_if
Using AI Code Generation
1describe file('/tmp') do2 it { should be_directory }3 it { should be_owned_by 'root' }4 its('group') { should eq 'root' }5 its('mode') { should cmp '0755' }6 only_if { os.name == 'ubuntu' }
only_if
Using AI Code Generation
1 command('echo "Hello World"').stdout =~ /World/2 describe file('/etc/hosts') do3 it { should be_file }4 only_if { command('echo "Hello World"').stdout =~ /World/ }5 describe file('/etc/hosts') do6 it { should be_file }7 describe file('/etc/hosts') do8 it { should be_file }9 command('echo "Hello World"').stdout =~ /World/10 describe file('/etc/hosts') do11 only_if { command('echo "Hello World"').stdout =~ /World/ }12 it { should be_file }13 describe file('/etc/hosts') do14 it { should be_file }15 command('echo "Hello World"').stdout =~ /World/16 describe file('/etc/hosts') do17 only_if { command('echo "Hello World"').stdout =~ /World/ }18 it { should be_file }
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!!