How to use name method of Inspec Package

Best Inspec_ruby code snippet using Inspec.name

helpers.rb

Source:helpers.rb Github

copy

Full Screen

...99def ingest_from_minion(type, ps_cmd, max_retries = 20, sec_timeout = 10)100 # grep "WinRM address:" $(ls -t .kitchen/logs/*.log | head -n2 | tail -n1) | sed 's/^.*: //'101 # Test port open: nc -z -w1 localhost 55985;echo $?102 # nc -z -w1 $(sed -n -e 's/^.*WinRM address: //p' $(ls -t .kitchen/logs/*.log | sed -n '2p') | cut -f1 -d:) $(sed -n -e 's/^.*WinRM address: //p' $(ls -t .kitchen/logs/*.log | sed -n '2p') | cut -f2 -d:); echo $? 103 # cd .kitchen/kitchen-vagrant/{instance name}; vagrant winrm --command whoami104 # cd .kitchen/kitchen-vagrant/{instance name}; vagrant winrm-config105 # cmd ="nc -z -w1 $(sed -n -e 's/^.*WinRM address: //p' $(ls -t .kitchen/logs/*.log | sed -n '2p') | cut -f1 -d:) $(sed -n -e 's/^.*WinRM address: //p' $(ls -t .kitchen/logs/*.log | sed -n '2p') | cut -f2 -d:); echo $?"106 retries ||= 0107 Inspec::Log.debug("Ingesting #{type} content using `#{ps_cmd}` with timout of #{sec_timeout} and a max of #{max_retries} retries.")108 # require 'pry'; binding.pry109 begin110 # https://www.rubydoc.info/gems/train/0.14.1/Train%2FTransports%2FLocal%2FConnection:run_command111 # Train.Plugins.Transport.Connection train.connection.run_command112 Timeout.timeout(sec_timeout) do113 @my_result = JSON.parse(backend::backend.run_command(ps_cmd).stdout) if type == 'json'114 @my_result =YAML.safe_load(backend::backend.run_command(ps_cmd).stdout) if type == 'yaml'115 end116 rescue => e117 Inspec::Log.debug("`#{e.message}`, target may be rebooting after highstate. Remaining retries: #{max_retries - retries}")118 puts("`#{e.message}`, target may be rebooting after highstate. Remaining retries: #{max_retries - retries}")119 if (retries += 1) < max_retries120 retry121 else122 begin123 backend::backend.run_command('whoami').stdout124 rescue => e125 msg = "Unable to get whoami from backend::backend.run_command: #{e.message}"126 puts(msg)127 Inspec::Log.debug(msg)128 end129 if OS.windows?130 pwsh_cmd = '$test_path=".kitchen/kitchen-vagrant/$(Get-ChildItem -Path .kitchen/logs/*.log | Where-Object {$_.Name -ne "kitchen.log"} | Sort-Object -Property @{Expression = {$_.LastWriteTime}; Descending = $True} | Select-Object -Property BaseName -First 1 -expandproperty BaseName)"; Set-Location -Path $test_path; $test_vagrantfile = "$test_path/Vagrantfile"; Set-Content -Path $test_vagrantfile -Value (get-content -Path $test_vagrantfile | Select-String -Pattern "vagrant_vb_guest.rb" -NotMatch); Set-Location -Path $test_path; vagrant winrm'131 cmd = "powershell -command '#{pwsh_cmd}'"132 else133 cmd = "cd .kitchen/kitchen-vagrant/$(ls -t .kitchen/logs/*.log | grep -v .kitchen/logs/kitchen.log | head -n1 | cut -f3 -d/ | awk -F. '{print $1}'); vagrant winrm"134 end135 if system( cmd )136 puts('Successfully connected via `vagrant winrm`')137 Inspec::Log.debug('Successfully connected via `vagrant winrm`')138 else139 msg = 'Failed to connect via `vagrant winrm`'140 puts(msg)141 Inspec::Log.debug(msg)142 # abort msg143 end144 end145 end146 if defined?(@my_result)147 Inspec::Log.debug("Ingested #{type} content successfully from minion using Train.Plugins.Transport.Connection.")148 @my_result149 else150 Inspec::Log.debug('Failed to get content from minion using Train.Plugins.Transport.Connection.')151 puts "WARNING: Failed to get content from minion using Train.Plugins.Transport.Connection."152 []153 end154end155# pp get_mystates_from_highstate('module','chocolatey','bootstrap')156# pp get_mystates_from_highstate('module','user','current')157# pp get_mystates_from_highstate('state','system','hostname')158def get_highstate_from_minion159 if defined?(@cache_highstate_from_minion)160 Inspec::Log.debug('Returning cached @cache_highstate_from_minion.')161 return @cache_highstate_from_minion162 end163 # ingest_from_minion('yaml', 'C:\salt\salt-call.bat --config-dir=C:\Users\vagrant\AppData\Local\Temp\kitchen\etc\salt state.show_highstate --out yaml |Select-String -Pattern "__sls__", "__env__", "- run", "- order:" -NotMatch')164 highstate_from_minion = ingest_from_minion('json', 'C:\salt\salt-call.bat --config-dir=C:\Users\vagrant\AppData\Local\Temp\kitchen\etc\salt state.show_highstate --out json')165 if highstate_from_minion != []166 Inspec::Log.debug('Saving highstate to cache @cache_highstate_from_minion.')167 @cache_highstate_from_minion = highstate_from_minion['local']168 @cache_highstate_from_minion169 else170 Inspec::Log.error('Failed to get highstate.')171 abort 'Failed to get highstate.'172 end173end174# Example modules175# "windows.module.status.uptime"=>176# {"module"=>177# [{"status.uptime"=>[{"human_readable"=>true}]},178# {"require"=>["windows.module.user.current"]},179# "run",180# {"order"=>10004}],181# "__sls__"=>"windows.modules",182# "__env__"=>"base"},183# "chocolatey.bootstrap"=>184# {"module"=>185# [{"chocolatey.bootstrap"=>nil},186# {"unless"=>"where.exe chocolatey"},187# "run",188# {"order"=>10010}],189# "__sls__"=>"windows.system.packages.chocolatey.bootstrap",190# "__env__"=>"base"},191# Example State192# {"windows.state.system.computer_desc.description"=>193# {"system"=>194# [{"name"=>"Saltstack Computer Description"},195# {"require"=>["windows.state.system.hostname.saltstack1"]},196# "computer_desc",197# {"order"=>10000}],198# "__sls__"=>"windows.states",199# "__env__"=>"base"},200def get_mystates_from_highstate(type, find_state, find_function)201 found_states = {}202 highstate = get_highstate_from_minion203 Inspec::Log.debug("Getting #{type}s for #{find_state}.#{find_function}.")204 # puts "highstate class: " + highstate.class.to_s205 # pp highstate206 highstate.each do |state_id, state|207 # puts "state class: " + state.class.to_s208 Inspec::Log.debug("Checking #{state_id} from highstate.")209 case type210 when 'state'211 if state.key?(find_state) && state[find_state].include?(find_function)212 Inspec::Log.debug("Found state #{find_state}.#{find_function} from highstate.")213 found_states[state_id] = state214 end215 when 'module'216 if state.key?('module')217 check_module = state['module'][0]218 # pp check_module219 if check_module.key?("#{find_state}.#{find_function}") || (check_module.key?(find_state) && check_module[find_state][0] == find_function)220 Inspec::Log.debug("Found module #{find_state}.#{find_function} from highstate.")221 found_states[state_id] = state222 end223 end224 end225 end226 if found_states != {}227 Inspec::Log.debug("Got #{type}s for #{find_state}.#{find_function}.")228 # pp found_states229 found_states230 else231 Inspec::Log.error("Unable to get #{type}'s for #{find_state}.#{find_function}'")232 end233end234# example: get_saltstack_package_full_name('7zip') = '7-Zip'235def get_saltstack_package_full_name(package)236 # pillar = YAML.safe_load(File.read('test/salt/pillar/windows.sls'))237 url = 'https://raw.githubusercontent.com/saltstack/salt-winrepo-ng/master/'238 files = [package + '.sls', package + '/init.sls']239 # example: package = "7zip"=>{"version"=>"18.06.00.0", "refresh_minion_env_path"=>false}240 saltstack_package_full_name = files.find do |checkme|241 ps = "$f = (((Get-ChildItem -Path $env:LOCALAPPDATA -Filter 'salt-winrepo-ng' -Recurse -Directory).Fullname[0]) + '\\#{checkme.sub('/', '\\')}'); if (Test-Path $f -PathType Leaf) {Get-Content -Path $f}"242 begin243 file = (open(url + checkme) & :read)244 rescue245 begin246 file = (powershell(ps).stdout)247 rescue248 next249 end250 end251 unless file.nil? || file.empty?252 candidate = file.match(/full_name: '([\S]+).*'/).captures[0]253 end254 break candidate unless candidate.nil?255 end256 Inspec::Log.debug('[get_saltstack_package_full_name] found candidate: ' + saltstack_package_full_name)257 saltstack_package_full_name258end...

Full Screen

Full Screen

ssh_crypto.rb

Source:ssh_crypto.rb Github

copy

Full Screen

1class SshCrypto < Inspec.resource(1)2 name 'ssh_crypto'3 def ssh_version4 inspec.command('ssh -V 2>&1 | cut -f1 -d" " | cut -f2 -d"_"').stdout.to_f5 end6 def valid_ciphers7 # define a set of default ciphers8 ciphers53 = 'aes256-ctr,aes192-ctr,aes128-ctr'9 ciphers66 = 'chacha20-poly1305@openssh.com,aes256-gcm@openssh.com,aes128-gcm@openssh.com,aes256-ctr,aes192-ctr,aes128-ctr'10 ciphers = ciphers5311 # adjust ciphers based on OS + release12 case inspec.os[:name]13 when 'ubuntu'14 case inspec.os[:release]15 when '12.04'16 ciphers = ciphers5317 when '14.04', '15.10', '16.04', '18.04'18 ciphers = ciphers6619 end20 when 'debian'21 case inspec.os[:release]22 when /^6\./, /^7\./23 ciphers = ciphers5324 when /^8\./, /^9\./, /^10\./25 ciphers = ciphers6626 end27 when 'redhat', 'centos', 'oracle'28 case inspec.os[:release]29 when /^6\./30 ciphers = ciphers5331 when /^7\./, /^8\./32 ciphers = ciphers6633 end34 when 'amazon', 'fedora', 'alpine'35 ciphers = ciphers6636 when 'opensuse'37 case inspec.os[:release]38 when /^13\.2/39 ciphers = ciphers6640 when /^42\./41 ciphers = ciphers6642 end43 when 'mac_os_x'44 case inspec.os[:release]45 when /^10.9\./46 ciphers = ciphers5347 when /^10.10\./, /^10.11\./, /^10.12\./48 ciphers = ciphers6649 end50 end51 ciphers52 end53 def valid_kexs54 # define a set of default KEXs55 kex66 = 'curve25519-sha256@libssh.org,diffie-hellman-group-exchange-sha256'56 kex59 = 'diffie-hellman-group-exchange-sha256'57 kex = kex5958 # adjust KEXs based on OS + release59 case inspec.os[:name]60 when 'ubuntu'61 case inspec.os[:release]62 when '12.04'63 kex = kex5964 when '14.04', '15.10', '16.04', '18.04'65 kex = kex6666 end67 when 'debian'68 case inspec.os[:release]69 when /^6\./70 kex = nil71 when /^7\./72 kex = kex5973 when /^8\./, /^9\./, /^10\./74 kex = kex6675 end76 when 'redhat', 'centos', 'oracle'77 case inspec.os[:release]78 when /^6\./79 kex = nil80 when /^7\./, /^8\./81 kex = kex6682 end83 when 'amazon', 'fedora', 'alpine'84 kex = kex6685 when 'opensuse'86 case inspec.os[:release]87 when /^13\.2/88 kex = kex6689 when /^42\./90 kex = kex6691 end92 when 'mac_os_x'93 case inspec.os[:release]94 when /^10.9\./95 kex = kex5996 when /^10.10\./, /^10.11\./, /^10.12\./97 kex = kex6698 end99 end100 kex101 end102 def valid_macs103 # define a set of default MACs104 macs66 = 'hmac-sha2-512-etm@openssh.com,hmac-sha2-256-etm@openssh.com,umac-128-etm@openssh.com,hmac-sha2-512,hmac-sha2-256'105 macs59 = 'hmac-sha2-512,hmac-sha2-256,hmac-ripemd160'106 macs53 = 'hmac-ripemd160,hmac-sha1'107 macs53_el65 = 'hmac-sha2-512,hmac-sha2-256'108 macs = macs59109 # adjust MACs based on OS + release110 case inspec.os[:name]111 when 'ubuntu'112 case inspec.os[:release]113 when '12.04'114 macs = macs59115 when '14.04', '15.10', '16.04', '18.04'116 macs = macs66117 end118 when 'debian'119 case inspec.os[:release]120 when /^6\./121 macs = macs53122 when /^7\./123 macs = macs59124 when /^8\./, /^9\./, /^10\./125 macs = macs66126 end127 when 'redhat', 'centos', 'oracle'128 case inspec.os[:release]129 when /^6\./130 # RedHat Enterprise Linux (and family) backported SHA2 support to their fork of OpenSSH 5.3 in RHEL 6.5.131 # See BZ#969565 at:132 # https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/6/html-single/6.5_technical_notes/index#openssh133 # Because extended support (EUS) updates for 6.x minor releases is no longer available,134 # only the settings available for the supported (latest) 6.x release are recommended.135 macs = macs53_el65136 when /^7\./, /^8\./137 macs = macs66138 end139 when 'amazon', 'fedora', 'alpine'140 macs = macs66141 when 'opensuse'142 case inspec.os[:release]143 when /^13\.2/144 macs = macs66145 when /^42\./146 macs = macs66147 end148 when 'mac_os_x'149 case inspec.os[:release]150 when /^10.9\./151 macs = macs59152 when /^10.10\./, /^10.11\./, /^10.12\./153 macs = macs66154 end155 end156 macs157 end158 def valid_privseparation159 # define privilege separation set160 ps53 = 'yes'161 ps59 = 'sandbox'162 ps75 = nil163 ps = ps59164 # debian 7.x and newer has ssh 5.9+165 # ubuntu 12.04 and newer has ssh 5.9+166 case inspec.os[:name]167 when 'debian'168 case inspec.os[:release]169 when /^6\./170 ps = ps53171 when /^10\./172 ps = ps75173 end174 when 'redhat', 'centos', 'oracle'175 case inspec.os[:release]176 # redhat/centos/oracle 6.x has ssh 5.3177 when /^6\./178 ps = ps53179 when /^7\./180 ps = ps59181 when /^8\./182 ps = ps75183 end184 when 'ubuntu'185 case inspec.os[:release]186 when /^18\./187 ps = ps75188 end189 when 'fedora', 'alpine'190 ps = ps75191 end192 ps193 end194 # return a list of valid algoriths for a current platform195 def valid_algorithms196 alg53 = %w(rsa)197 alg60 = %w(rsa ecdsa)198 alg66 = %w(rsa ecdsa ed25519)199 alg = alg66 # probably its a best suitable set for everything unknown200 case inspec.os[:name]201 when 'ubuntu'202 case inspec.os[:release]203 when '12.04'204 alg = alg53205 when '14.04', '15.10', '16.04', '18.04'206 alg = alg66207 end208 when 'debian'209 case inspec.os[:release]210 when /^7\./211 alg = alg60212 when /^8\./, /^9\./213 alg = alg66214 end...

Full Screen

Full Screen

system.rb

Source:system.rb Github

copy

Full Screen

2# system.rb -- InSpec resources for system values3# Author: Daniel Dehennin <daniel.dehennin@ac-dijon.fr>4# Copyright (C) 2020 Daniel Dehennin <daniel.dehennin@ac-dijon.fr>5class SystemResource < Inspec.resource(1)6 name 'system'7 attr_reader :platform8 def initialize9 super10 @platform = build_platform11 end12 private13 def build_platform14 {15 family: build_platform_family,16 name: build_platform_name,17 release: build_platform_release,18 finger: build_platform_finger19 }20 end21 def build_platform_family22 case inspec.platform[:name]23 when 'arch', 'gentoo'24 inspec.platform[:name]25 else26 inspec.platform[:family]27 end28 end29 def build_platform_name30 case inspec.platform[:name]31 when 'amazon', 'oracle', 'rocky'32 "#{inspec.platform[:name]}linux"33 when /^windows_/34 inspec.platform[:family]35 else36 inspec.platform[:name]37 end38 end39 # rubocop:disable Metrics/MethodLength,Metrics/AbcSize,Metrics/CyclomaticComplexity40 def build_platform_release41 case inspec.platform[:name]42 when 'amazon'43 # `2018` relase is named `1` in `kitchen.yml`44 inspec.platform[:release].gsub(/2018.*/, '1')45 when 'arch'46 'base-latest'47 when 'gentoo'48 "#{inspec.platform[:release].split('.')[0]}-#{derive_gentoo_init_system}"49 when 'opensuse'50 # rubocop:disable Style/NumericLiterals,Layout/LineLength51 inspec.platform[:release].to_i > 20210101 ? 'tumbleweed' : inspec.platform[:release]52 # rubocop:enable Style/NumericLiterals,Layout/LineLength53 when 'windows_8.1_pro'54 '8.1'55 when 'windows_server_2019_datacenter'56 '2019-server'57 when 'windows_server_2016_datacenter'58 '2016-server'59 else60 inspec.platform[:release]61 end62 end63 # rubocop:enable Metrics/MethodLength,Metrics/AbcSize,Metrics/CyclomaticComplexity64 def derive_gentoo_init_system65 inspec.command('systemctl').exist? ? 'sysd' : 'sysv'66 end67 def build_platform_finger68 "#{build_platform_name}-#{build_finger_release}"69 end70 def build_finger_release71 case inspec.platform[:name]72 when 'ubuntu'73 build_platform_release.split('.').slice(0, 2).join('.')74 else75 build_platform_release.split('.')[0]76 end77 end78end...

Full Screen

Full Screen

name

Using AI Code Generation

copy

Full Screen

1 describe file('/tmp') do2 it { should be_directory }3 describe file('/tmp') do4 it { should be_directory }5 describe file('/tmp') do6 it { should be_directory }7 describe file('/tmp') do8 it { should be_directory }9 describe file('/tmp') do10 it { should be_directory }11 describe file('/tmp') do12 it { should be_directory }13 describe file('/tmp') do14 it { should be_directory }15 describe file('/tmp') do16 it { should be_directory }17 describe file('/tmp') do18 it { should be_directory }

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