How to use add_test method of Inspec Package

Best Inspec_ruby code snippet using Inspec.add_test

objects_test.rb

Source:objects_test.rb Github

copy

Full Screen

...160 obj = Inspec::Object::Test.new161 obj.matcher = "match"162 obj.negate!163 obj.expectation = "0.0.0.0"164 loop_obj.add_test(obj)165 _(loop_obj.to_ruby).must_equal '166port(25).addresses.each do |entry|167 describe entry do168 it { should_not match "0.0.0.0" }169 end170end171'.strip172 end173 end174 describe "Inspec::Object::List" do175 it "constructs a list filtering test" do176 list_obj = Inspec::Object::List.new([["passwd"]])177 list_obj.qualifier.push(["where { user =~ /^(?!root|sync|shutdown|halt).*$/ }"])178 obj = Inspec::Object::Test.new179 obj.qualifier = list_obj.qualifier180 obj.matcher = "be_empty"181 obj.qualifier.push(["entries"])182 obj.negate!183 _(obj.to_ruby).must_equal '184describe passwd.where { user =~ /^(?!root|sync|shutdown|halt).*$/ } do185 its("entries") { should_not be_empty }186end187'.strip188 end189 end190 describe "Inspec::Object::OrTest and Inspec::Object::Control" do191 let(:obj1) do192 obj1 = Inspec::Object::Test.new193 obj1.qualifier = [["command", "ls /etc"], ["exit_status"]]194 obj1.matcher = "eq"195 obj1.expectation = 0196 obj1197 end198 let(:obj2) do199 obj2 = obj1.dup200 obj2.negate!201 obj2.expectation = 100202 obj2203 end204 it "constructs a simple describe.one block wrapping two tests" do205 or_obj = Inspec::Object::OrTest.new([obj1, obj2])206 _(or_obj.to_ruby).must_equal '207describe.one do208 describe command("ls /etc") do209 its("exit_status") { should eq 0 }210 end211 describe command("ls /etc") do212 its("exit_status") { should_not eq 100 }213 end214end215'.strip216 end217 it "negates a describe.one block, wow!" do218 or_obj = Inspec::Object::OrTest.new([obj1, obj2])219 or_obj.negate!220 _(or_obj.to_ruby).must_equal '221describe command("ls /etc") do222 its("exit_status") { should_not eq 0 }223end224describe command("ls /etc") do225 its("exit_status") { should eq 100 }226end227'.strip228 end229 it "loops a describe.one block, ooooooo!" do230 res = Inspec::Object::EachLoop.new231 res.qualifier.push(["(1..5)"])232 # already defined in the let block:233 obj1.matcher = "eq entity"234 obj2.matcher = "eq entity"235 obj1.remove_expectation236 obj2.remove_expectation237 or_obj = Inspec::Object::OrTest.new([obj1, obj2])238 res.tests = [or_obj]239 _(res.to_ruby).must_equal '240(1..5).each do |entry|241 describe.one do242 describe command("ls /etc") do243 its("exit_status") { should eq entity }244 end245 describe command("ls /etc") do246 its("exit_status") { should_not eq entity }247 end248 end249end250'.strip251 end252 it "constructs a control" do253 control = Inspec::Object::Control.new254 control.add_test(obj1)255 control.id = "sample.control.id"256 control.title = "Sample Control Important Title"257 control.descriptions = {258 default: "The most critical control the world has ever seen",259 rationale: "It is needed to save the planet",260 'more info': "Insert clever joke here",261 }262 control.refs = ["simple ref", { ref: "title", url: "my url" }]263 control.impact = 1.0264 _(control.to_ruby).must_equal '265control "sample.control.id" do266 title "Sample Control Important Title"267 desc "The most critical control the world has ever seen"268 desc "rationale", "It is needed to save the planet"269 desc "more info", "Insert clever joke here"270 impact 1.0271 ref "simple ref"272 ref ({:ref=>"title", :url=>"my url"})273 describe command("ls /etc") do274 its("exit_status") { should eq 0 }275 end276end277'.strip278 end279 it "constructs a control with only_if" do280 control = Inspec::Object::Control.new281 control.add_test(obj1)282 control.only_if = "package('ntp').installed?"283 control.id = "sample.control.id"284 control.title = "Sample Control Important Title"285 control.descriptions = {286 default: "The most critical control the world has ever seen",287 rationale: "It is needed to save the planet",288 'more info': "Insert clever joke here",289 }290 control.refs = ["simple ref", { ref: "title", url: "my url" }]291 control.impact = 1.0292 _(control.to_ruby).must_equal '293control "sample.control.id" do294 title "Sample Control Important Title"295 desc "The most critical control the world has ever seen"296 desc "rationale", "It is needed to save the planet"297 desc "more info", "Insert clever joke here"298 impact 1.0299 ref "simple ref"300 ref ({:ref=>"title", :url=>"my url"})301 only_if { package(\'ntp\').installed? }302 describe command("ls /etc") do303 its("exit_status") { should eq 0 }304 end305end306'.strip307 end308 it "constructs a multiline desc in a control with indentation" do309 control = Inspec::Object::Control.new310 control.descriptions[:default] = "Multiline\n control"311 _(control.to_ruby).must_equal '312control nil do313 desc "314 Multiline315 control316 "317end318'.strip319 end320 it "ignores empty control descriptions" do321 control = Inspec::Object::Control.new322 x = '323control nil do324end325'.strip326 control.descriptions[:default] = ""327 _(control.to_ruby).must_equal x328 control.descriptions[:default] = nil329 _(control.to_ruby).must_equal x330 end331 it "handles non-string descriptions" do332 control = Inspec::Object::Control.new333 control.descriptions[:default] = 123334 _(control.to_ruby).must_equal '335control nil do336 desc "123"337end338'.strip339 end340 end341 describe "Inspec::Object::Variable, take #1" do342 it "constructs a control with variable to instantiate a resource only once" do343 control = Inspec::Object::Control.new344 variable = Inspec::Object::Value.new([["command", "which grep"]])345 variable_id = variable.name_variable.to_s346 obj1 = Inspec::Object::Test.new347 obj1.variables.push(variable)348 obj1.qualifier.push([variable_id])349 obj1.qualifier.push(["exit_status"])350 obj1.matcher = "eq"351 obj1.expectation = 0352 control.add_test(obj1)353 obj2 = Inspec::Object::Test.new354 obj2.qualifier.push([variable_id.to_s])355 obj2.qualifier.push(["stdout"])356 obj2.matcher = "contain"357 obj2.expectation = "grep"358 control.add_test(obj2)359 control.id = "variable.control.id"360 control.title = "Variable Control Important Title"361 control.descriptions[:default] = "The most variable control the world has ever seen"362 control.impact = 1.0363 _(control.to_ruby).must_equal '364control "variable.control.id" do365 title "Variable Control Important Title"366 desc "The most variable control the world has ever seen"367 impact 1.0368 a = command("which grep")369 describe a do370 its("exit_status") { should eq 0 }371 end372 describe a do373 its("stdout") { should contain "grep" }374 end375end376'.strip377 end378 end379 describe "Inspec::Object::Variable, take #2" do380 it "constructs a control with variable, loop and var reference" do381 control = Inspec::Object::Control.new382 command_value = %r{^/usr/bin/chrony}383 pid_filter = ">"384 pid_value = 0385 loopy = Inspec::Object::EachLoop.new386 loopy.qualifier = [["processes", command_value]]387 loopy.qualifier.push(["where { pid #{pid_filter} #{pid_value} }.entries"])388 obj = loopy.add_test389 variable = Inspec::Object::Value.new([['passwd.where { user == "_chrony" }.uids.first']])390 variable_id = variable.name_variable.to_s391 obj.variables.push(variable)392 obj.qualifier = [["user(entry.user)"], ["uid"]]393 obj.matcher = "cmp #{variable_id}"394 control.add_test(obj)395 control.id = "variable.control.id"396 control.impact = 0.1397 _(control.to_ruby).must_equal '398control "variable.control.id" do399 impact 0.1400 a = passwd.where { user == "_chrony" }.uids.first401 describe user(entry.user) do402 its("uid") { should cmp a }403 end404end405'.strip406 end407 end408 describe "Inspec::Object::Tag" do...

Full Screen

Full Screen

generate.rb

Source:generate.rb Github

copy

Full Screen

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

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