Best Inspec_ruby code snippet using Inspec.supports
profile.rb
Source:profile.rb
...123 )124 @runner_context =125 options[:profile_context] ||126 Inspec::ProfileContext.for_profile(self, @backend)127 @supports_platform = metadata.supports_platform?(@backend)128 @supports_runtime = metadata.supports_runtime?129 end130 def name131 metadata.params[:name]132 end133 def version134 metadata.params[:version]135 end136 def writable?137 @writable138 end139 #140 # Is this profile is supported on the current platform of the141 # backend machine and the current inspec version.142 #143 # @returns [TrueClass, FalseClass]144 #145 def supported?146 supports_platform? && supports_runtime?147 end148 # We need to check if we're using a Mock'd backend for tests to function.149 # @returns [TrueClass, FalseClass]150 def supports_platform?151 if @supports_platform.nil?152 @supports_platform = metadata.supports_platform?(@backend)153 end154 if @backend.backend.class.to_s == 'Train::Transports::Mock::Connection'155 @supports_platform = true156 end157 @supports_platform158 end159 def supports_runtime?160 if @supports_runtime.nil?161 @supports_runtime = metadata.supports_runtime?162 end163 @supports_runtime164 end165 def params166 @params ||= load_params167 end168 def collect_tests(include_list = @controls)169 unless @tests_collected170 return unless supports_platform?171 locked_dependencies.each(&:collect_tests)172 tests.each do |path, content|173 next if content.nil? || content.empty?174 abs_path = source_reader.target.abs_path(path)175 @runner_context.load_control_file(content, abs_path, nil)176 end177 @tests_collected = true178 end179 filter_controls(@runner_context.all_rules, include_list)180 end181 def filter_controls(controls_array, include_list)182 return controls_array if include_list.nil? || include_list.empty?183 # Check for anything that might be a regex in the list, and make it official184 include_list.each_with_index do |inclusion, index|185 next if inclusion.is_a?(Regexp)186 # Insist the user wrap the regex in slashes to demarcate it as a regex187 next unless inclusion.start_with?('/') && inclusion.end_with?('/')188 inclusion = inclusion[1..-2] # Trim slashes189 begin190 re = Regexp.new(inclusion)191 include_list[index] = re192 rescue RegexpError => e193 warn "Ignoring unparseable regex '/#{inclusion}/' in --control CLI option: #{e.message}"194 include_list[index] = nil195 end196 end197 include_list.compact!198 controls_array.select do |c|199 id = ::Inspec::Rule.rule_id(c)200 include_list.any? do |inclusion|201 # Try to see if the inclusion is a regex, and if it matches202 inclusion == id || (inclusion.is_a?(Regexp) && inclusion =~ id)203 end204 end205 end206 def load_libraries207 return @runner_context if @libraries_loaded208 locked_dependencies.dep_list.each_with_index do |(_name, dep), i|209 d = dep.profile210 # this will force a dependent profile load so we are only going to add211 # this metadata if the parent profile is supported.212 if supports_platform? && !d.supports_platform?213 # since ruby 1.9 hashes are ordered so we can just use index values here214 metadata.dependencies[i][:status] = 'skipped'215 msg = "Skipping profile: '#{d.name}' on unsupported platform: '#{d.backend.platform.name}/#{d.backend.platform.release}'."216 metadata.dependencies[i][:skip_message] = msg217 next218 elsif metadata.dependencies[i]219 # Currently wrapper profiles will load all dependencies, and then we220 # load them again when we dive down. This needs to be re-done.221 metadata.dependencies[i][:status] = 'loaded'222 end223 c = d.load_libraries224 @runner_context.add_resources(c)225 end226 libs = libraries.map do |path, content|227 [content, path]228 end229 @runner_context.load_libraries(libs)230 @libraries_loaded = true231 @runner_context232 end233 def to_s234 "Inspec::Profile<#{name}>"235 end236 # return info using uncached params237 def info!238 info(load_params.dup)239 end240 def info(res = params.dup) # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity, Metrics/MethodLength241 # add information about the controls242 res[:controls] = res[:controls].map do |id, rule|243 next if id.to_s.empty?244 data = rule.dup245 data.delete(:checks)246 data[:impact] ||= 0.5247 data[:impact] = 1.0 if data[:impact] > 1.0248 data[:impact] = 0.0 if data[:impact] < 0.0249 data[:id] = id250 # if the code field is empty try and pull info from dependencies251 if data[:code].empty? && parent_profile.nil?252 locked_dependencies.dep_list.each do |_name, dep|253 profile = dep.profile254 code = Inspec::MethodSource.code_at(data[:source_location], profile.source_reader)255 data[:code] = code unless code.nil? || code.empty?256 break if !data[:code].empty?257 end258 end259 data260 end.compact261 # resolve hash structure in groups262 res[:groups] = res[:groups].map do |id, group|263 group[:id] = id264 group265 end266 # add information about the required inputs267 if res[:inputs].nil? || res[:inputs].empty?268 # convert to array for backwards compatability269 res[:inputs] = []270 else271 res[:inputs] = res[:inputs].values.map(&:to_hash)272 end273 res[:sha256] = sha256274 res[:parent_profile] = parent_profile unless parent_profile.nil?275 if !supports_platform?276 res[:status] = 'skipped'277 msg = "Skipping profile: '#{name}' on unsupported platform: '#{backend.platform.name}/#{backend.platform.release}'."278 res[:skip_message] = msg279 else280 res[:status] = 'loaded'281 end282 # convert legacy os-* supports to their platform counterpart283 if res[:supports] && !res[:supports].empty?284 res[:supports].each do |support|285 support[:"platform-family"] = support.delete(:"os-family") if support.key?(:"os-family")286 support[:"platform-name"] = support.delete(:"os-name") if support.key?(:"os-name")287 end288 end289 res290 end291 # Check if the profile is internally well-structured. The logger will be292 # used to print information on errors and warnings which are found.293 #294 # @return [Boolean] true if no errors were found, false otherwise295 def check # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity, Metrics/MethodLength296 # initial values for response object297 result = {298 summary: {...
metadata_test.rb
Source:metadata_test.rb
...5require 'inspec/metadata'6describe 'metadata with supported operating systems' do7 let(:logger) { Minitest::Mock.new }8 let(:empty_options) { {} }9 def supports_meta(params)10 res = Inspec::Metadata.from_yaml('mock', "---", nil, logger)11 # manually inject supported parameters12 res.params[:supports] = params13 Inspec::Metadata.finalize(res, 'mock', empty_options, logger)14 res15 end16 describe 'running on ubuntu 14.04' do17 let (:backend) { MockLoader.new(:ubuntu1404).backend }18 it 'provides all metadata content' do19 s = "---\nname: hello #{rand}"20 res = Inspec::Metadata.from_yaml('mock', s, nil)21 Inspec::Metadata.finalize(res, 'mock', empty_options)22 res.content.must_equal(s)23 end24 it 'finalizes a loaded metadata via Profile ID' do25 res = Inspec::Metadata.from_yaml('mock', '---', nil)26 Inspec::Metadata.finalize(res, 'mock', empty_options)27 res.params[:name].must_equal('mock')28 end29 it 'finalizes a loaded metadata via Profile ID and overwrites the ID' do30 res = Inspec::Metadata.from_yaml('mock', "---\nname: hello", nil)31 Inspec::Metadata.finalize(res, 'mock', empty_options)32 res.params[:name].must_equal('mock')33 end34 it 'reads the version from metadata' do35 res = Inspec::Metadata.from_yaml('mock', "---\nversion: '1.1.0'", nil)36 Inspec::Metadata.finalize(res, 'mock', empty_options)37 res.params[:version].must_equal('1.1.0')38 res.valid_version?(res.params[:version]).must_equal(true)39 end40 it 'does not accept invalid version from metadata' do41 res = Inspec::Metadata.from_yaml('mock', "---\nversion: '1.1.0.1'", nil)42 Inspec::Metadata.finalize(res, 'mock', empty_options)43 res.params[:version].must_equal('1.1.0.1')44 res.valid_version?(res.params[:version]).must_equal(false)45 end46 it 'finalizes a loaded metadata by turning strings into symbols' do47 res = Inspec::Metadata.from_yaml('mock', "---\nauthor: world", nil)48 Inspec::Metadata.finalize(res, 'mock', empty_options)49 res.params[:author].must_equal('world')50 end51 it 'sets a default name with the original target if there is no name, title, or profile_id' do52 res = Inspec::Metadata.from_yaml('mock', '---', nil, logger)53 options = { target: '/path/to/tests' }54 Inspec::Metadata.finalize(res, nil, options, logger)55 res.params[:name].must_equal('tests from /path/to/tests')56 end57 it 'does not overwrite an existing name when name exists and profile_id is nil' do58 res = Inspec::Metadata.from_yaml('mock', "\nname: my_name", nil)59 options = { target: '/path/to/tests' }60 Inspec::Metadata.finalize(res, nil, options, logger)61 res.params[:name].must_equal('my_name')62 end63 it 'does not set a default name if a title is provided and profile_id is nil' do64 res = Inspec::Metadata.from_yaml('mock', "\ntitle: my_title", nil)65 options = { target: '/path/to/tests' }66 Inspec::Metadata.finalize(res, nil, options, logger)67 res.params[:title].must_equal('my_title')68 res.params[:name].must_be_nil69 end70 it 'loads the support field from metadata' do71 res = Inspec::Metadata.from_yaml('mock',72 "---\nsupports:\n - os: ubuntu", nil)73 res.params[:supports].must_equal([{ os: 'ubuntu' }])74 end75 it 'makes sure the supports release field is a string' do76 res = Inspec::Metadata.from_yaml('mock',77 "---\nsupports:\n - release: 12.02", nil)78 res.params[:supports].must_equal([{ release: '12.02' }])79 end80 it 'makes sure the supports release field is nil if not configured' do81 res = Inspec::Metadata.from_yaml('mock',82 "---\nsupports:\n - release: ", nil)83 res.params[:supports].must_equal([{ release: nil }])84 end85 it 'load a profile with empty supports clause' do86 m = supports_meta(nil)87 m.supports_transport?(backend).must_equal true88 end89 it 'supports legacy simple support style, but warns' do90 # i.e. setting this to something that would fail:91 logger.expect :warn, nil, ["Do not use deprecated `supports: linux` syntax. Instead use:\nsupports:\n - os-family: linux\n\n"]92 m = supports_meta('linux')93 m.supports_transport?(backend).must_equal true94 logger.verify95 end96 it 'supports legacy simple support style, but warns' do97 # i.e. setting this to something that would fail:98 logger.expect :warn, nil, ["Do not use deprecated `supports: linux` syntax. Instead use:\nsupports:\n - os-family: linux\n\n"]99 m = supports_meta(['linux'])100 m.supports_transport?(backend).must_equal true101 logger.verify102 end103 it 'loads a profile which supports os ubuntu' do104 m = supports_meta({ 'os' => 'ubuntu' })105 m.supports_transport?(backend).must_equal true106 end107 it 'loads a profile which supports os name ubuntu' do108 m = supports_meta({ 'os-name' => 'ubuntu' })109 m.supports_transport?(backend).must_equal true110 end111 it 'loads a profile which supports os family linux' do112 m = supports_meta({ 'os-family' => 'linux' })113 m.supports_transport?(backend).must_equal true114 end115 it 'loads a profile which supports release 14.04' do116 m = supports_meta({ 'release' => '14.04' })117 m.supports_transport?(backend).must_equal true118 end119 it 'rejects a profile which supports release 12.04' do120 m = supports_meta({ 'release' => '12.04' })121 m.supports_transport?(backend).must_equal false122 end123 it 'loads a profile which supports ubuntu 14.04' do124 m = supports_meta({ 'os-name' => 'ubuntu', 'release' => '14.04' })125 m.supports_transport?(backend).must_equal true126 end127 it 'rejects a profile which supports ubuntu 12.04' do128 m = supports_meta({ 'os-name' => 'ubuntu', 'release' => '12.04' })129 m.supports_transport?(backend).must_equal false130 end131 it 'loads a profile which supports ubuntu float 14.04 as parsed by yml' do132 m = supports_meta({ 'os-name' => 'ubuntu', 'release' => 14.04 })133 m.supports_transport?(backend).must_equal true134 end135 it 'reject unsupported os' do136 m = supports_meta({ 'os-name' => 'windows' })137 m.supports_transport?(backend).must_equal false138 end139 end140 describe 'testing the supported runtime' do141 let(:current_version) { Inspec::VERSION }142 let(:next_version) { Gem::Version.new(current_version).bump.to_s }143 it 'returns true on testing the current version' do144 m = supports_meta({ 'inspec' => current_version })145 m.supports_runtime?.must_equal true146 end147 it 'returns true on testing the current version' do148 m = supports_meta({ 'inspec' => '= ' + current_version })149 m.supports_runtime?.must_equal true150 end151 it 'returns true on testing >= current version' do152 m = supports_meta({ 'inspec' => '>= ' + current_version })153 m.supports_runtime?.must_equal true154 end155 it 'returns false on testing >= the next version' do156 m = supports_meta({ 'inspec' => '>= ' + next_version })157 m.supports_runtime?.must_equal false158 end159 it 'returns false on testing > the next version' do160 m = supports_meta({ 'inspec' => '> ' + next_version })161 m.supports_runtime?.must_equal false162 end163 end164end...
supports
Using AI Code Generation
1 it { should be_supported('os') }2 it { should be_supported('platform') }3 it { should be_supported('platform_family') }4 it { should be_supported('platform_version') }5 it { should be_supported('arch') }6 it { should be_supported('family') }7 it { should be_supported('release') }8 it { should be_supported('name') }9 it { should be_supported('release') }10 it { should be_supported('version') }11 it { should be_supported('kernel') }12 it { should be_supported('virtualization') }13 it { should be_supported('virtualization_role') }14 it { should be_supported('virtualization_system') }15 it { should be_supported('docker') }16 it { should be_supported('network') }17 it { should be_supported('cpu') }18 it { should be_supported('memory') }19 it { should be_supported('file') }20 it { should be_supported('group') }21 it { should be_supported('user') }22 it { should be_supported('package') }23 it { should be_supported('service') }24 it { should be_supported('command') }25 it { should be_supported('port') }26 it { should be_supported('process') }27 it { should be_supported('shell') }28 it { should be_supported('mount') }29 it { should be_supported('sysctl') }30 it { should be_supported('virtualization') }31 it { should be_supported('virtualization_role') }32 it { should be_supported('virtualization_system') }33 it { should be_supported('docker') }34 it { should be_supported('network') }35 it { should be_supported('cpu') }36 it { should be_supported('memory') }37 it { should be_supported('file') }38 it { should be_supported('group') }39 it { should be_supported('user') }40 it { should be_supported('package') }41 it { should be_supported('service') }42 it { should be_supported('command') }43 it { should be_supported('port') }44 it { should be_supported('process') }45 it { should be_supported('shell') }46 it { should be_supported('mount') }47 it { should be_supported('sys
supports
Using AI Code Generation
1 it { should support('os') }2 it { should_not support('windows') }3 it { should_not support('os') }4 it { should support('windows') }5 it { should support('os') }6 it { should support('windows') }7 it { should_not support('os') }8 it { should_not support('windows') }9 it { should support('os') }10 it { should support('windows') }11 it { should support('linux') }12 it { should support('os') }13 it { should support('windows') }14 it { should_not support('linux') }15 it { should support('os') }16 it { should support('windows') }17 it { should support('linux') }18 it { should support('unix') }19 it { should support('os') }20 it { should support('windows') }21 it { should support('linux') }22 it { should_not support('unix') }
supports
Using AI Code Generation
1if inspec.os.supports?('ubuntu', '16.04')2 describe command('lsb_release -a') do3 its('stdout') { should match /Ubuntu/ }4 its('stdout') { should match /16.04/ }5 describe command('lsb_release -a') do6 its('stdout') { should match /Ubuntu/ }7 describe command('lsb_release -a') do8 its('stdout') { should match /16.04/ }
supports
Using AI Code Generation
1 describe file('/etc/passwd') do2 it { should exist }3Method Description inspec.os.name() Returns the name of the Operating System inspec.os.family() Returns the family of the Operating System inspec.os.arch() Returns the architecture of the Operating System inspec.os.release() Returns the release of the Operating System inspec.os.suppored?() Returns true if the Operating System is supported by Inspec4 describe file('/etc/passwd') do5 it { should exist }
supports
Using AI Code Generation
1 describe command('lsb_release -a') do2 its('stdout') { should match /Ubuntu/ }3 describe command('lsb_release -a') do4 its('stdout') { should match /16.04/ }
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!!