Best Inspec_ruby code snippet using Plugins.resource_failed
resource.rb
Source:resource.rb
...65 cl = Class.new(obj) do # rubocop:disable Metrics/BlockLength66 attr_reader :resource_exception_message67 def initialize(backend, name, *args)68 @resource_skipped = false69 @resource_failed = false70 @supports = Inspec::Resource.supports[name.to_sym]71 @resource_exception_message = nil72 # attach the backend to this instance73 @__backend_runner__ = backend74 @__resource_name__ = name75 # check resource supports76 supported = @supports ? check_supports : true # check_supports has side effects!77 test_backend = defined?(Train::Transports::Mock::Connection) && backend.backend.class == Train::Transports::Mock::Connection78 # raise unless we are supported or in test79 unless supported || test_backend80 msg = "Unsupported resource/backend combination: %s / %s. Exiting." %81 [name, backend.platform.name]82 raise ArgumentError, msg83 end84 # call the resource initializer85 begin86 super(*args)87 rescue Inspec::Exceptions::ResourceSkipped => e88 skip_resource(e.message)89 rescue Inspec::Exceptions::ResourceFailed => e90 fail_resource(e.message)91 rescue NotImplementedError => e92 fail_resource(e.message) unless @resource_failed93 rescue NoMethodError => e94 skip_resource(e.message) unless @resource_failed95 end96 end97 def self.desc(description = nil)98 return @description if description.nil?99 @description = description100 end101 def self.example(example = nil)102 return @example if example.nil?103 @example = example104 end105 def check_supports106 require "inspec/resources/platform"107 status = inspec.platform.supported?(@supports)108 fail_msg = "Resource `#{@__resource_name__}` is not supported on platform #{inspec.platform.name}/#{inspec.platform.release}."109 fail_resource(fail_msg) unless status110 status111 end112 def skip_resource(message)113 @resource_skipped = true114 @resource_exception_message = message115 end116 def resource_skipped?117 @resource_skipped118 end119 def fail_resource(message)120 @resource_failed = true121 @resource_exception_message = message122 end123 def resource_failed?124 @resource_failed125 end126 def inspec127 @__backend_runner__128 end129 end130 # rubocop:enable Lint/NestedMethodDefinition131 # Warn if a resource pack is overwriting a core resource.132 # Suppress warning if the resource is an AWS resource, see #3822133 if __resource_registry.key?(name) && !name.start_with?("aws_")134 Inspec::Log.warn("Overwriting resource #{name}. To reference a specific version of #{name} use the resource() method")135 end136 __resource_registry[name] = cl137 end138 end...
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!!