How to use params method of Inspec Package

Best Inspec_ruby code snippet using Inspec.params

package.rb

Source:package.rb Github

copy

Full Screen

...95 class Deb < PkgManagement96 def info(package_name)97 cmd = inspec.command("dpkg -s #{package_name}")98 return {} if cmd.exit_status.to_i != 099 params = SimpleConfig.new(100 cmd.stdout.chomp,101 assignment_regex: /^\s*([^:]*?)\s*:\s*(.*?)\s*$/,102 multiple_values: false,103 ).params104 # If the package is installed, Status is "install ok installed"105 # If the package is installed and marked hold, Status is "hold ok installed"106 # If the package is removed and not purged, Status is "deinstall ok config-files" with exit_status 0107 # If the package is purged cmd fails with non-zero exit status108 {109 name: params['Package'],110 installed: params['Status'].split(' ')[2] == 'installed',111 held: params['Status'].split(' ')[0] == 'hold',112 version: params['Version'],113 type: 'deb',114 }115 end116 end117 # RHEL family118 class Rpm < PkgManagement119 def initialize(inspec, opts)120 super(inspec)121 @dbpath = opts.fetch(:rpm_dbpath, nil)122 end123 def missing_requirements124 missing_requirements = []125 unless @dbpath.nil? || inspec.directory(@dbpath).directory?126 missing_requirements << "RPMDB #{@dbpath} does not exist"127 end128 missing_requirements129 end130 def info(package_name)131 rpm_cmd = rpm_command(package_name)132 cmd = inspec.command(rpm_cmd)133 # CentOS does not return an error code if the package is not installed,134 # therefore we need to check for emptyness135 return {} if cmd.exit_status.to_i != 0 || cmd.stdout.chomp.empty?136 params = SimpleConfig.new(137 cmd.stdout.chomp,138 assignment_regex: /^\s*([^:]*?)\s*:\s*(.*?)\s*$/,139 multiple_values: false,140 ).params141 # On some (all?) systems, the linebreak before the vendor line is missing142 if params['Version'] =~ /\s*Vendor:/143 v = params['Version'].split(' ')[0]144 else145 v = params['Version']146 end147 # On some (all?) systems, the linebreak before the build line is missing148 if params['Release'] =~ /\s*Build Date:/149 r = params['Release'].split(' ')[0]150 else151 r = params['Release']152 end153 {154 name: params['Name'],155 installed: true,156 version: "#{v}-#{r}",157 type: 'rpm',158 }159 end160 private161 def rpm_command(package_name)162 cmd = ''163 cmd += 'rpm -qi'164 cmd += " --dbpath #{@dbpath}" if @dbpath165 cmd += ' ' + package_name166 cmd167 end168 end169 # MacOS / Darwin implementation170 class Brew < PkgManagement171 def info(package_name)172 brew_path = inspec.command('brew').exist? ? 'brew' : '/usr/local/bin/brew'173 cmd = inspec.command("#{brew_path} info --json=v1 #{package_name}")174 # If no available formula exists, then `brew` will exit non-zero175 return {} if cmd.exit_status.to_i != 0176 pkg = JSON.parse(cmd.stdout)[0]177 # If package exists but is not installed, then `brew` output will not178 # contain `pkg['installed'][0]['version']179 return {} unless pkg.dig('installed', 0, 'version')180 {181 name: pkg['name'],182 installed: true,183 version: pkg['installed'][0]['version'],184 type: 'brew',185 }186 rescue JSON::ParserError => e187 raise Inspec::Exceptions::ResourceFailed,188 'Failed to parse JSON from `brew` command. ' \189 "Error: #{e}"190 end191 end192 # Arch Linux193 class Pacman < PkgManagement194 def info(package_name)195 cmd = inspec.command("pacman -Qi #{package_name}")196 return {} if cmd.exit_status.to_i != 0197 params = SimpleConfig.new(198 cmd.stdout.chomp,199 assignment_regex: /^\s*([^:]*?)\s*:\s*(.*?)\s*$/,200 multiple_values: false,201 ).params202 {203 name: params['Name'],204 installed: true,205 version: params['Version'],206 type: 'pacman',207 }208 end209 end210 class HpuxPkg < PkgManagement211 def info(package_name)212 cmd = inspec.command("swlist -l product | grep #{package_name}")213 return {} if cmd.exit_status.to_i != 0214 pkg = cmd.stdout.strip.split(' ')215 {216 name: pkg[0],217 installed: true,218 version: pkg[1],219 type: 'pkg',220 }221 end222 end223 class AlpinePkg < PkgManagement224 def info(package_name)225 cmd = inspec.command("apk info -vv --no-network | grep #{package_name}")226 return {} if cmd.exit_status.to_i != 0227 pkg_info = cmd.stdout.split("\n").delete_if { |e| e =~ /^WARNING/i }228 pkg = pkg_info[0].split(' - ')[0]229 {230 name: pkg.partition('-')[0],231 installed: true,232 version: pkg.partition('-')[2],233 type: 'pkg',234 }235 end236 end237 # Determines the installed packages on Windows using the Windows package registry entries.238 # @see: http://blogs.technet.com/b/heyscriptingguy/archive/2013/11/15/use-powershell-to-find-installed-software.aspx239 class WindowsPkg < PkgManagement240 def info(package_name)241 search_paths = [242 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*',243 'HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*',244 ]245 # add 64 bit search paths246 if inspec.os.arch == 'x86_64'247 search_paths << 'HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*'248 search_paths << 'HKCU:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*'249 end250 # Find the package251 cmd = inspec.command <<-EOF.gsub(/^\s*/, '')252 Get-ItemProperty (@("#{search_paths.join('", "')}") | Where-Object { Test-Path $_ }) |253 Where-Object { $_.DisplayName -match "^\s*#{package_name.shellescape}\.*" -or $_.PSChildName -match "^\s*#{package_name.shellescape}\.*" } |254 Select-Object -Property DisplayName,DisplayVersion | ConvertTo-Json255 EOF256 # We cannot rely on `exit_status` since PowerShell always exits 0 from the257 # above command. Instead, if no package is found the output of the command258 # will be `''` so we can use that to return `{}` to match the behavior of259 # other package managers.260 return {} if cmd.stdout == ''261 begin262 package = JSON.parse(cmd.stdout)263 rescue JSON::ParserError => e264 raise Inspec::Exceptions::ResourceFailed,265 'Failed to parse JSON from PowerShell. ' \266 "Error: #{e}"267 end268 # What if we match multiple packages? just pick the first one for now.269 package = package[0] if package.is_a?(Array)270 {271 name: package['DisplayName'],272 installed: true,273 version: package['DisplayVersion'],274 type: 'windows',275 }276 end277 end278 # AIX279 class BffPkg < PkgManagement280 def info(package_name)281 cmd = inspec.command("lslpp -cL #{package_name}")282 return {} if cmd.exit_status.to_i != 0283 bff_pkg = cmd.stdout.split("\n").last.split(':')284 {285 name: bff_pkg[1],286 installed: true,287 version: bff_pkg[2],288 type: 'bff',289 }290 end291 end292 # Solaris293 class SolarisPkg < PkgManagement294 def info(package_name)295 if inspec.os[:release].to_i <= 10296 solaris10_info(package_name)297 else298 solaris11_info(package_name)299 end300 end301 # solaris 10302 def solaris10_info(package_name)303 cmd = inspec.command("pkginfo -l #{package_name}")304 return {} if cmd.exit_status.to_i != 0305 params = SimpleConfig.new(306 cmd.stdout.chomp,307 assignment_regex: /^\s*([^:]*?)\s*:\s*(.*?)\s*$/,308 multiple_values: false,309 ).params310 # parse 11.10.0,REV=2006.05.18.01.46311 v = params['VERSION'].split(',')312 {313 name: params['PKGINST'],314 installed: true,315 version: v[0] + '-' + v[1].split('=')[1],316 type: 'pkg',317 }318 end319 # solaris 11320 def solaris11_info(package_name)321 cmd = inspec.command("pkg info #{package_name}")322 return {} if cmd.exit_status.to_i != 0323 params = SimpleConfig.new(324 cmd.stdout.chomp,325 assignment_regex: /^\s*([^:]*?)\s*:\s*(.*?)\s*$/,326 multiple_values: false,327 ).params328 {329 name: params['Name'],330 installed: true,331 # 0.5.11-0.175.3.1.0.5.0332 version: "#{params['Version']}-#{params['Branch']}",333 type: 'pkg',334 }335 end336 end337end...

Full Screen

Full Screen

example_config.rb

Source:example_config.rb Github

copy

Full Screen

...14 end15 "16 # Load the configuration file on initialization17 def initialize18 @params = {}19 @path = '/tmp/example/config.yaml'20 @file = inspec.file(@path)21 unless @file.file?22 raise Inspec::Exceptions::ResourceSkipped, "Can't find file `#{@path}`"23 end24 # Protect from invalid YAML content25 begin26 @params = YAML.load(@file.content)27 # Add two extra matchers28 @params['file_size'] = @file.size29 @params['file_path'] = @path30 @params['ruby'] = 'RUBY IS HERE TO HELP ME!'31 rescue StandardError => e32 raise Inspec::Exceptions::ResourceSkipped, "#{@file}: #{e.message}"33 end34 end35 # Example method called by 'it { should exist }'36 # Returns true or false from the 'File.exist?' method37 def exists?38 File.exist?(@path)39 end40 # Example matcher for the number of commas in the file41 def comma_count42 text = @file.content43 text.count(',')44 end45 # Expose all parameters46 def method_missing(name)47 @params[name.to_s]48 end49end...

Full Screen

Full Screen

params

Using AI Code Generation

copy

Full Screen

1 it { should eq 'value' }2 it { should eq 'value' }3 it { should eq 'value' }4 it { should eq 'value' }5 it { should eq 'value' }6 it { should eq 'value' }7 it { should eq 'value' }8 it { should eq 'value' }

Full Screen

Full Screen

params

Using AI Code Generation

copy

Full Screen

1 expect(ENV['MY_ENV_VAR']).to eq('my_value')2 expect(ENV['MY_ENV_VAR']).to eq('my_value')3 expect(ENV['MY_ENV_VAR']).to eq('my_value')4 expect(ENV['MY_ENV_VAR']).to eq('my_value')5 expect(ENV['MY_ENV_VAR']).to eq('my_value')6 expect(ENV['MY_ENV_VAR']).to eq('my_value')7 expect(ENV['MY_ENV_VAR']).to eq('my_value')

Full Screen

Full Screen

params

Using AI Code Generation

copy

Full Screen

1 expect(ENV['MY_ENV_VAR']).to eq('my_value')2 expect(ENV['MY_ENV_VAR']).to eq('my_value')3 expect(ENV['MY_ENV_VAR']).to eq('my_value')4 expect(ENV['MY_ENV_VAR']).to eq('my_value')5 expect(ENV['MY_ENV_VAR']).to eq('my_value')6 expect(ENV['MY_ENV_VAR']).to eq('my_value')7 expect(ENV['MY_ENV_VAR']).to eq('my_value')

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