How to use to_s method of Inspec Package

Best Inspec_ruby code snippet using Inspec.to_s

rake_tasks.rb

Source:rake_tasks.rb Github

copy

Full Screen

...27 namespace :serverspec do28 targets = @properties[:targets] || {}29 environments = @properties[:environments] || {}30 desc 'Run all targets and environments'31 task all: targets.keys.map { |key| 'serverspec:' + key.to_s.split('.')[0] }.concat(environments.keys.map {|key| 'serverspec:' + key.to_s.split('.')[0] })32 targets.keys.each do |key|33 target = targets[key]34 process_target(key, target)35 end36 puts37 environments.keys.each do |key|38 desc "Run all tasks in environment #{key}"39 task key.to_sym => "serverspec:#{key}:all"40 namespace key.to_sym do41 environment = environments[key]42 task all: environment[:targets].map { |target, _hash| "serverspec:#{key}:#{target.to_s.split(':')[0].to_sym}" }43 environment[:targets].each do |target, hash|44 process_target("#{target}", hash, 'environment', key.to_s)45 end46 end47 end48 end49 end50 def serverspec_task_array(key, spec_type, target, options)51 env = options[:source] == 'environment' ? ":"+ options[:environment] : ""52 desc "Run serverspec to #{key}"53 task key.to_sym => "serverspec#{env}:#{key}:all"54 namespace key.to_sym do55 desc "Run #{key} against all hosts"56 task :all do57 target[:hosts].each do |host|58 Rake::Task["serverspec#{env}:#{key}:#{host.split(':')[0].to_sym}"].execute59 end60 end61 target[:hosts].each do |host|62 task_name = "#{host || target[:name]}"63 serverspec_rake_task(host, key, task_name, spec_type, options)64 end65 end66 end67 def serverspec_rake_task(host, key, task_name, spec_type, options = {}, target = {})68 desc "Run serverspec to #{key}"69 RSpec::Core::RakeTask.new(task_name.to_s.to_sym) do |t|70 ENV['TARGET_HOST'] = host.to_s71 ENV['TARGET'] = key.to_s72 ENV['TASK_NAME'] = task_name.to_s73 ENV['TASK_SOURCE'] = options[:source]74 ENV['TASK_ENV'] = options[:environment]75 t.pattern = "spec/#{spec_type}_spec.rb"76 t.fail_on_error = options[:fail_on_err]77 report_name = options[:environment] ? "reports/#{options[:environment]}/#{key.to_s}/#{host.to_s}" : "reports/#{key.to_s}/#{host.to_s}"78 set_formatters(report_name, options, t)79 end80 end81 def set_formatters(report_path, options, t)82 opts = t.rspec_opts83 if options[:formatters].include?('junit') || options[:formatters].include?('xml')84 opts = "#{opts} --format RspecJunitFormatter --out #{report_path}.xml"85 end86 if options[:formatters].include?('docs') || options[:formatters].include?('documentation') || options[:formatters].include?('docs_file')87 opts = "#{opts} --format documentation --out #{report_path}.docs"88 end89 if options[:formatters].include?('docs_screen')90 opts = "#{opts} --format documentation"91 end92 if options[:formatters].include?('tick')93 opts = "#{opts} --format RspecTickFormatter"94 end95 if options[:formatters].include?('tick_file')96 opts = "#{opts} --format RspecTickFormatter --out #{report_path}.tick"97 end98 if options[:formatters].include?('progress')99 opts = "#{opts} --format progress"100 end101 if options[:formatters].include?('html')102 opts = "#{opts} --format html --out #{report_path}.html"103 end104 if options[:formatters].include?('html_report') || options[:formatters].include?('html_pretty')105 opts = "#{opts} --format RspecHtmlReporter"106 end107 if options[:formatters].include?('json')108 opts = "#{opts} --format j --out #{report_path}.json"109 end110 if options[:formatters].include?('launcher')111 opts = "#{opts} --format LauncherJsonFormatter --out #{report_path}_extended.json"112 end113 unless options[:color]114 opts = "#{opts} --no-color"115 end116 if File.exist?('.rspec')117 opts = "#{opts} --options .rspec"118 end119 t.rspec_opts = opts120 end121 def debug_tasks122 namespace :debug do123 desc 'View loaded Shared examples'124 task :shared_examples do125 puts 'Loaded Shared Examples:\n======================='126 load_shared_examples @properties[:shared_example_gems] || []127 shared_examples.each do |ex|128 puts "\t#{ex}"129 end130 end131 end132 end133 def self.load(properties = nil)134 props = PropertiesLoader.new properties135 tasks = ServerspecLauncherRakeTasks.new props.properties136 tasks.load_tasks137 end138 private139 def process_target(key, target, task_source = 'target', environment = nil)140 options = {141 fail_on_err: target[:fail_on_err] || @fail_on_err,142 formatters: target[:formatters] || @formatters,143 color: target[:color].nil? ? @colorize : target[:color],144 source: task_source,145 environment: environment146 }147 if target[:backend] == 'inspec'148 inspec_target(key, options, target)149 else150 serverspec_target(key, options, target)151 end152 end153 def inspec_target(key, options, target)154 spec_type, options = get_inspec_type(target, options)155 if target[:hosts].is_a?(Array)156 inspec_task_array(key, spec_type, target, options)157 elsif target[:hosts]158 host = target[:hosts]159 task_name = (key || target[:name]).to_s160 inspec_task(host, "#{key}/#{host}", task_name, spec_type, options, target)161 else162 host = (target[:hosts] || 'local' )163 task_name = (key || target[:name]).to_s164 inspec_task(host, "#{key}/#{host}", task_name, spec_type, options, target)165 end166 end167 def inspec_task_array(key, spec_type, target, options)168 env = options[:source] == 'environment' ? ":"+ options[:environment] : ""169 desc "Run serverspec to #{key}"170 task key.to_sym => "serverspec#{env}:#{key}:all"171 namespace key.to_sym do172 desc "Run #{key} against all hosts"173 task :all do174 target[:hosts].each do |host|175 Rake::Task["serverspec#{env}:#{key}:#{host.split(':')[0].to_sym}"].execute176 end177 end178 target[:hosts].each do |host|179 task_name = "#{host || target[:name]}"180 inspec_task(host, "#{key}/#{host}", task_name, spec_type, options, target)181 end182 end183 end184 def get_inspec_type(target, options)185 spec_type = 'role'186 if (target[:control] && target[:profile]) || (target[:control] && target[:spec_type]) || (target[:spec_type] && target[:profile])187 puts 'WARNING: Multiple options specified: they will be evalated in the follow precidence profile > control > spec_type'188 end189 if target[:spec_type]190 spec_type = target[:spec_type]191 options[:spec_type] = 'spec'192 end193 if target[:control]194 spec_type = target[:control]195 options[:spec_type] = 'control'196 end197 if target[:profile]198 spec_type = target[:profile]199 options[:spec_type] = 'control'200 end201 return spec_type, options202 end203 def inspec_task(host, key, task_name, spec_type, options = {}, target = {})204 protocol = host == 'local' ? 'local' : 'ssh'205 command = inspec_commandline(target,key,host, spec_type, protocol, options)206 Rake::Task.define_task(task_name.to_s.to_sym) do207 unless Docker::Image.exist? 'chef/inspec:latest'208 Docker::Image.create('fromImage' => 'chef/inspec:latest')209 end210 container = Docker::Container.create(211 'Image' => 'chef/inspec:latest',212 'Mounts' => inspec_mounts(target, protocol),213 'Cmd' => command,214 'Tty' => STDIN.tty?,215 'Env' => inspec_environment(target, protocol)216 )217 begin218 puts "inspec #{command.join(' ')}"219 networks = target[:attach_to] || []220 networks.each do |nw|221 network = Docker::Network.get(nw)222 network.connect(container.id) 223 end224 container.start225 container.wait226 rescue Docker::Error::TimeoutError => ex227 container.stop228 puts "A Docker::Error::TimeoutError occurred, most likey because you are using password protected ssh key, whihc is not supported"229 puts "Either agent your ssh key and use 'agent' as the auth_method in your target settings or use an unprotected key (not recommended)"230 puts 'Container Logs: '231 puts container.logs(stderr: true)232 ensure233 puts container.logs(stdout: true)234 container.delete235 #Report have wrong permission hookie fix236 chown_files(protocol, target)237 end238 end239 end240 def chown_files(protocol, target)241 unless Docker::Image.exist? 'alpine:latest'242 Docker::Image.create('fromImage' => 'alpine:latest')243 end244 command = %W[chown -R #{Etc.getpwnam(ENV['USER']).uid}:#{Etc.getpwnam(ENV['USER']).gid} /share/reports/]245 container = Docker::Container.create(246 'Image' => 'alpine:latest',247 'Mounts' => inspec_mounts(target, protocol),248 'Cmd' => command249 )250 container.start251 container.wait252 container.delete253 end254 def inspec_environment(target_info, protocol)255 environment = []256 environment << "SSH_AUTH_SOCK=#{ENV['SSH_AUTH_SOCK']}" if target_info[:auth_method] == 'agent'257 environment258 end259 def inspec_mounts(target_info, protocol)260 mounts = [{261 'Type' => 'bind',262 'Source' => "#{Dir.pwd}",263 'Target' => "/share"264 }, {265 'Type' => 'bind',266 'Source' => "/etc/hosts",267 'Target' => "/etc/hosts"268 }]269 mount_ssh = target_info[:mount_ssh_dir] ? target_info[:mount_ssh_dir] : true270 mounts << { 'Type' => 'bind', 'Source' => "#{File.expand_path('~')}/.ssh", 'Target' => "#{File.expand_path('~')}/.ssh" } if mount_ssh && protocol == 'ssh'271 mounts << { 'Type' => 'bind', 'Source' => "#{ENV['SSH_AUTH_SOCK']}", 'Target' => "#{ENV['SSH_AUTH_SOCK']}" } if target_info[:auth_method] == 'agent'272 mounts273 end274 def inspec_commandline(target_info, key, host, spec_type, protocol, options = {})275 spec = if options[:spec_type] == 'spec'276 "spec/#{spec_type}_spec.rb"277 else278 spec_type279 end280 target = "#{protocol}://#{protocol == 'local' ? '' : host}"281 command = %W[exec #{spec} -t #{target}]282 authmethod = target_info[:auth_method] ? target_info[:auth_method] : 'ssh-keys'283 keyfile = target_info[:keyfile] ? target_info[:keyfile] : "#{File.expand_path('~')}/.ssh/id_rsa"284 if protocol == 'ssh' && authmethod == 'ssh-keys'285 command << '-i'286 command << keyfile287 end288 command << "--user=#{target_info[:user]}" if target_info[:user]289 command << "--port=#{target_info[:ssh_port]}" if target_info[:ssh_port]290 command << "--password=#{target_info[:password]}" if target_info[:password]291 command << "--bastion-host=#{target_info[:bastion_host]}" if target_info[:bastion_host]292 command << "--bastion-port=#{target_info[:bastion_port]}" if target_info[:bastion_port]293 command << "--bastion-user=#{target_info[:bastion_user]}" if target_info[:bastion_user]294 command << set_inspec_reporters(key, host, options)295 command296 end297 def set_inspec_reporters(key, host, options)298 reporters = []299 report_path = options[:environment] ? "/share/reports/#{options[:environment]}/#{key}" : "/share/reports/#{key}"300 reporters << "junit:#{report_path}.xml" if options[:formatters].include?('junit') || options[:formatters].include?('xml')301 reporters << "documentation:#{report_path}.docs" if options[:formatters].include?('docs') || options[:formatters].include?('documentation') || options[:formatters].include?('docs_file')302 reporters << 'documentation' if options[:formatters].include?('docs_screen')303 reporters << 'cli' if options[:formatters].include?('tick')304 reporters << "cli:#{report_path}.tick" if options[:formatters].include?('tick_file')305 reporters << 'progress' if options[:formatters].include?('progress')306 reporters << "html:#{report_path}.html" if options[:formatters].include?('html')307 reporters << "html#{report_path}.html" if (options[:formatters].include?('html_report') || options[:formatters].include?('html_pretty')) && !options[:formatters].include?('html')308 reporters << "json-rspec:#{report_path}.json" if options[:formatters].include?('json')309 "--reporter=#{reporters.join(' ')}"310 end311 def serverspec_target(key, options, target)312 spec_type = target[:spec_type] || 'role'313 if target[:hosts].is_a?(Array)314 serverspec_task_array(key, spec_type, target, options)315 elsif target[:hosts]316 host = target[:hosts]317 task_name = (key || target[:name]).to_s318 serverspec_rake_task(host, key, task_name, spec_type, options)319 else320 task_name = (key || target[:name]).to_s321 serverspec_rake_task(key, key, task_name, spec_type, options)322 end323 end324end325ServerspecLauncherRakeTasks.load unless $dont_load_rake_tasks...

Full Screen

Full Screen

negative.rb

Source:negative.rb Github

copy

Full Screen

...36 describe = Inspec::Describe.new37 qualifier = [iterator, {}]38 unresources["qualifiers"].each do |parameter|39 Inspec::Log.debug "Terraform::Negative.parse_unmatched_resources #{iterator} qualifier found = #{parameter} MATCHED"40 value = resources.deep_find(parameter.to_s) # value comes from another likely source. Assumption is values are consistent for this type of field41 qualifier[1][parameter] = value42 end43 describe.qualifier.push(qualifier)44 describe.add_test(nil, "exist", nil, { negated: true }) # last field is negated45 ctrl.add_test(describe)46 unmatched_controls.push(ctrl)47 end48 Inspec::Log.debug "Terraform::Negative.parse_unmatched_resources negative_controls = #{unmatched_controls}"49 unmatched_controls50 end51 # controls for iterators minus the matched resources52 def self.parse_matched_resources(resources, sourcefile, platform) # rubocop:disable Metrics/AbcSize53 Inspec::Log.debug "Terraform::Negative.parse_matched_resources matched_resources #{resources.keys}"54 matched_controls = []55 resources.keys.each do |resource|56 resources[resource].extend Hashie::Extensions::DeepFind # use to find iterators' values from other attributes57 resource_iterators = InspecPlugins::Iggy::InspecHelper.available_resource_iterators(platform)[resource]58 if resource_iterators.nil?59 Inspec::Log.warn "No iterator matching #{resource} for #{platform} found!"60 next61 else62 iterator = resource_iterators["iterator"]63 index = resource_iterators["index"]64 Inspec::Log.debug "Terraform::Negative.parse_matched_resources iterator:#{iterator} index:#{index}"65 end66 # Nothing but the finest bespoke hand-built InSpec67 ctrl = "control 'NEGATIVE-COVERAGE:#{iterator}' do\n"68 ctrl += " title 'InSpec-Iggy NEGATIVE-COVERAGE:#{iterator}'\n"69 ctrl += " desc \"\n"70 ctrl += " NEGATIVE-COVERAGE:#{iterator} from the source file #{sourcefile}\n\n"71 ctrl += " Generated by InSpec-Iggy v#{InspecPlugins::Iggy::VERSION}\"\n\n"72 ctrl += " impact 1.0\n"73 # get the qualifiers for the resource iterator74 ctrl += " (#{iterator}.where({ "75 if resource_iterators["qualifiers"]76 resource_iterators["qualifiers"].each do |parameter|77 Inspec::Log.debug "Terraform::Negative.parse_matched_resources #{iterator} qualifier found = #{parameter} MATCHED"78 value = resources[resource].deep_find(parameter.to_s) # value comes from resources being evaluated. Assumption is values are consistent for this type of field79 unless value80 Inspec::Log.warn "Terraform::Negative.parse_matched_resources #{resource} no #{parameter} value found, searching outside scope."81 value = resources.deep_find(parameter.to_s)82 end83 ctrl += "#{parameter}: '#{value}', "84 end85 end86 ctrl += "}).#{index} - [\n"87 # iterate over the resources88 resources[resource].keys.each do |resource_name|89 ctrl += " '#{resource_name}',\n"90 end91 ctrl += " ]).each do |id|\n"92 ctrl += " describe #{resource}({ "93 # iterate over resource qualifiers94 first = true95 InspecPlugins::Iggy::InspecHelper.available_resource_qualifiers(platform)[resource].each do |parameter|96 if first # index is first97 ctrl += "#{parameter}: id, "98 first = false99 next100 end101 property = parameter.to_s102 properties = InspecPlugins::Iggy::InspecHelper.available_translated_resource_properties(platform, resource)103 if properties && properties.value?(parameter.to_s)104 property = properties.key(parameter.to_s) # translate back if necessary105 end106 # instead of looking up the key, find by value?107 Inspec::Log.debug "Iggy::Terraform::Negative.parse_matched_resources #{resource} qualifier found = #{property} MATCHED"108 value = resources[resource].deep_find(property) # value comes from resources being evaluated. Assumption is values are consistent for this type of field109 ctrl += "#{property}: '#{value}', "110 end111 ctrl += "}) do\n"112 ctrl += " it { should_not exist }\n"113 ctrl += " end\n"114 ctrl += " end\n"115 ctrl += "end\n\n"116 matched_controls.push(ctrl)117 end118 Inspec::Log.debug "Terraform::Negative.parse_matched_resources negative_controls = #{matched_controls}"...

Full Screen

Full Screen

Rakefile

Source:Rakefile Github

copy

Full Screen

...45 # We can't use .is_alias? here since it fired on the original methods as well46 next if obj.docstring.match?(/chef-sugar backcompat method/)47 # we don't want private API methods48 next unless obj.visibility == :public49 doc_data[obj.name.to_s] = {}50 doc_data[obj.name.to_s]["prefix"] = obj.name.to_s51 doc_data[obj.name.to_s]["body"] = obj.name.to_s52 doc_data[obj.name.to_s]["description"] = obj.docstring53 doc_data[obj.name.to_s]["scope"] = "source.ruby.chef"54 unless doc_data[obj.name.to_s]["description"].end_with?('.')55 doc_data[obj.name.to_s]["description"] << '.'56 end57 if obj.has_tag?('since')58 doc_data[obj.name.to_s]["description"] << " Introduced in Chef Infra Client #{obj.tag('since').text}."59 end60 end61 File.open("../snippets/automated_dsl_snippets.json", "w") do |f|62 f.write(JSON.pretty_generate(doc_data.sort.to_h))63 end64 Rake::Task["generate_inspec_snippets"].invoke65end66task default: :generate_snippets67desc 'Generate InSpec snippets from published resource documentation'68task :generate_inspec_snippets do69 require 'json'70 puts 'Running task generate_inspec_snippets'71 puts 'generate_inspec_snippets :: Validating that InSpec git repository is available locally and up to date.'72 # The InSpec/InSpec repository is used to generate snippets from docs...

Full Screen

Full Screen

to_s

Using AI Code Generation

copy

Full Screen

1 expect('test').to eq('test')2 expect('test').to eq('test')3 expect('test').to eq('test')4 expect('test').to eq('test')5 expect('test').to eq('test')6 expect('test').to eq('test')7 expect('test').to eq('test')8 expect('test').to eq('test')

Full Screen

Full Screen

to_s

Using AI Code Generation

copy

Full Screen

1 it { should respond_to(:to_s) }2 it { should match(/Inspec/) }3 its('to_s') { should match(/Inspec/) }4 its('to_s') { should match(/Inspec/) }5 it { should match(/Inspec/) }6 it { should respond_to(:to_s) }

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