diff --git a/.gitignore b/.gitignore index e56e5cf..5f90c22 100644 --- a/.gitignore +++ b/.gitignore @@ -11,6 +11,7 @@ doc Gemfile.lock # bundler .bundle +vendor/bundle Gemfile.lock # built gems *.gem diff --git a/lib/rubyipmi.rb b/lib/rubyipmi.rb index 5a26792..927c2c6 100644 --- a/lib/rubyipmi.rb +++ b/lib/rubyipmi.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + # Copyright (C) 2014 Corey Osman # # This library is free software; you can redistribute it and/or @@ -22,15 +24,13 @@ require 'open3' class NullLogger < Logger - def initialize(*_args) - end + def initialize(*_args); end - def add(*_args, &_block) - end + def add(*_args, &_block); end end module Rubyipmi - PRIV_TYPES = ['CALLBACK', 'USER', 'OPERATOR', 'ADMINISTRATOR'] + PRIV_TYPES = ['CALLBACK', 'USER', 'OPERATOR', 'ADMINISTRATOR'].freeze attr_accessor :logger, :log_level # set a logger instance yourself to customize where the logs should go @@ -69,6 +69,7 @@ def self.valid_drivers def self.valid_providers ['auto', 'ipmitool', 'freeipmi'] end + # The connect method will create a connection object based the provider type passed in # If provider is left blank the function will use the first available provider # When the driver is set to auto, rubyipmi will try and figure out which driver to use by common error messages. We will most likely be using @@ -98,7 +99,7 @@ def self.connect(user, pass, host, provider = 'any', opts = {:driver => 'lan20', opts[:timeout] ||= 'default' if opts[:privilege] && !supported_privilege_type?(opts[:privilege]) - logger.error("Invalid privilege type :#{opts[:privilege]}, must be one of: #{PRIV_TYPES.join("\n")}") if logger + logger&.error("Invalid privilege type :#{opts[:privilege]}, must be one of: #{PRIV_TYPES.join("\n")}") raise "Invalid privilege type :#{opts[:privilege]}, must be one of: #{PRIV_TYPES.join("\n")}" end @@ -119,7 +120,7 @@ def self.connect(user, pass, host, provider = 'any', opts = {:driver => 'lan20', # Support multiple drivers # Note: these are just generic names of drivers that need to be specified for each provider unless valid_drivers.include?(opts[:driver]) - logger.debug("You must specify a valid driver: #{valid_drivers.join(',')}") if logger + logger&.debug("You must specify a valid driver: #{valid_drivers.join(',')}") raise "You must specify a valid driver: #{valid_drivers.join(',')}" end @@ -130,12 +131,12 @@ def self.connect(user, pass, host, provider = 'any', opts = {:driver => 'lan20', elsif provider == "ipmitool" Rubyipmi::Ipmitool::Connection.new(user, pass, host, opts) else - logger.error("Incorrect provider given, must use one of #{valid_providers.join(', ')}") if logger + logger&.error("Incorrect provider given, must use one of #{valid_providers.join(', ')}") raise "Incorrect provider given, must use one of #{valid_providers.join(', ')}" end else # Can't find the provider command line tool, maybe try other provider? - logger.error("The IPMI provider: #{provider} is not installed") if logger + logger&.error("The IPMI provider: #{provider} is not installed") raise "The IPMI provider: #{provider} is not installed" end end @@ -147,7 +148,7 @@ def self.supported_privilege_type?(type) # test-friendly capture3 def self.capture3(cmd) - return Open3.capture3(*cmd) + Open3.capture3(*cmd) end # method used to find the command which also makes it easier to mock with @@ -156,6 +157,7 @@ def self.locate_command(commandname) logger&.error("Which command returned: #{stderr}") unless status.success? return nil unless status.success? + stdout.strip end @@ -167,7 +169,7 @@ def self.is_provider_installed?(provider) when "ipmitool" cmdpath = locate_command('ipmitool') else - logger.error("Invalid BMC provider type #{provider}") if logger + logger&.error("Invalid BMC provider type #{provider}") false end # return false if command was not found @@ -180,7 +182,7 @@ def self.providers # returns true if any of the providers are installed def self.provider_installed? - providers_installed.length > 0 + providers_installed.length.positive? end def self.providers_installed @@ -208,7 +210,7 @@ def self.get_diag(user, pass, host, opts = {:driver => 'lan20', :timeout => 'def data[:ipmitool] = ipmiconn.get_diag end end - File.open('/tmp/rubyipmi_diag_data.txt', 'w') { |f| f.write(data) } + File.write('/tmp/rubyipmi_diag_data.txt', data) puts "Created file /tmp/rubyipmi_diag_data.txt" end end diff --git a/lib/rubyipmi/commands/basecommand.rb b/lib/rubyipmi/commands/basecommand.rb index 3f5b4ef..9ccafd8 100644 --- a/lib/rubyipmi/commands/basecommand.rb +++ b/lib/rubyipmi/commands/basecommand.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require "observer" require 'tempfile' require 'rubyipmi' @@ -5,9 +7,9 @@ module Rubyipmi class BaseCommand include Observable - attr_reader :cmd, :max_retry_count + + attr_reader :cmd, :max_retry_count, :lastcall, :result attr_accessor :options, :passfile - attr_reader :lastcall, :result def logger Rubyipmi.logger @@ -37,7 +39,7 @@ def initialize(commandname, opts = ObservableHash.new) end def locate_command(commandname) - unless location = Rubyipmi.locate_command(commandname) + unless (location = Rubyipmi.locate_command(commandname)) logger&.error("#{commandname} command not found, is #{commandname} installed?") raise "#{commandname} command not found, is #{commandname} installed?" end @@ -49,8 +51,8 @@ def locate_command(commandname) def runcmd @success = false @success = run - logger.debug(@lastcall.inspect) unless @lastcall.nil? if logger - logger.debug(@result) unless @result.nil? if logger + logger&.debug(@lastcall.inspect) unless @lastcall.nil? + logger&.debug(@result) unless @result.nil? @success end @@ -59,29 +61,27 @@ def run # we also don't want to add this to the initialize since mocking is difficult and we don't want to # throw errors upon object creation retrycount = 0 - process_status = false @cmd = locate_command(@cmdname) setpass @result = nil - logger.debug(makecommand) if logger + logger&.debug(makecommand) begin command = makecommand @lastcall = command @result, @result_err, status = Rubyipmi.capture3(command) # sometimes the command tool does not return the correct result, validate it with additional code - process_status = validate_status(status) - rescue + validate_status(status) + rescue StandardError if retrycount < max_retry_count find_fix(@result) retrycount = retrycount.next retry else - logger.error("Exhausted all auto fixes, cannot determine what the problem is") if logger + logger&.error("Exhausted all auto fixes, cannot determine what the problem is") raise "Exhausted all auto fixes, cannot determine what the problem is" end ensure removepass - process_status end end @@ -91,11 +91,12 @@ def run # this must be overrided in the subclass, as there are no generic errors that fit both providers def find_fix(result) return unless result + # The errorcode code hash contains the fix begin fix = ErrorCodes.search(result) @options.merge_notify!(fix) - rescue + rescue StandardError Rubyipmi.logger.debug("Could not find fix for error code: \n#{result}") if logger raise "Could not find fix for error code: \n#{result}" end diff --git a/lib/rubyipmi/commands/mixins/power_mixin.rb b/lib/rubyipmi/commands/mixins/power_mixin.rb index 1ca78f8..7d077a3 100644 --- a/lib/rubyipmi/commands/mixins/power_mixin.rb +++ b/lib/rubyipmi/commands/mixins/power_mixin.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Rubyipmi module PowerMixin # Turn the system on diff --git a/lib/rubyipmi/commands/mixins/sensors_mixin.rb b/lib/rubyipmi/commands/mixins/sensors_mixin.rb index 5733fe7..955bdce 100644 --- a/lib/rubyipmi/commands/mixins/sensors_mixin.rb +++ b/lib/rubyipmi/commands/mixins/sensors_mixin.rb @@ -1,12 +1,14 @@ +# frozen_string_literal: true + module Rubyipmi module SensorsMixin def refresh - @sensors = nil + @list = nil list end def list - @sensors ||= parse(getsensors) + @list ||= parse(getsensors) end def count @@ -27,18 +29,16 @@ def fanlist(refreshdata = false) def templist(refreshdata = false) refresh if refreshdata list.each_with_object({}) do |(name, sensor), tlist| - tlist[name] = sensor if (sensor[:unit] =~ /.*degree.*/ || name =~ /.*temp.*/) + tlist[name] = sensor if sensor[:unit] =~ /.*degree.*/ || name =~ /.*temp.*/ end end private def method_missing(method, *_args, &_block) - if !list.key?(method.to_s) - raise NoMethodError - else - list[method.to_s] - end + raise NoMethodError unless list.key?(method.to_s) + + list[method.to_s] end def parse(data) diff --git a/lib/rubyipmi/freeipmi/commands/basecommand.rb b/lib/rubyipmi/freeipmi/commands/basecommand.rb index b822228..48ab1ec 100644 --- a/lib/rubyipmi/freeipmi/commands/basecommand.rb +++ b/lib/rubyipmi/freeipmi/commands/basecommand.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'rubyipmi/freeipmi/errorcodes' module Rubyipmi::Freeipmi @@ -20,10 +22,11 @@ def makecommand # must remove from command line as its handled via conf file next if k == 'password' next if k == 'username' - if !v - "--#{k}" - else + + if v "--#{k}=#{v}" + else + "--#{k}" end end @@ -39,11 +42,10 @@ def validate_status(exitstatus) # essentially any result greater than 23 characters is an error raise "Error occurred" if @result.length > 23 when "bmc-config" - if @result.length > 100 && exitstatus.success? - return true - else - raise "Error occurred" - end + return true if @result.length > 100 && exitstatus.success? + + raise "Error occurred" + else super end @@ -54,11 +56,12 @@ def validate_status(exitstatus) # until all the fixes are exhausted or a error not defined in the errorcodes is found def find_fix(result) return unless result + # The errorcode code hash contains the fix begin fix = ErrorCodes.search(result) @options.merge_notify!(fix) - rescue + rescue StandardError raise "Could not find fix for error code: \n#{result}" end end diff --git a/lib/rubyipmi/freeipmi/commands/bmc.rb b/lib/rubyipmi/freeipmi/commands/bmc.rb index 9173c5d..503a1b9 100644 --- a/lib/rubyipmi/freeipmi/commands/bmc.rb +++ b/lib/rubyipmi/freeipmi/commands/bmc.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Rubyipmi::Freeipmi class Bmc < Rubyipmi::Freeipmi::BaseCommand # attr_accessor :options @@ -16,7 +18,7 @@ def version end def info - if @bmcinfo.length > 0 + if @bmcinfo.length.positive? @bmcinfo else information.retrieve @@ -40,11 +42,11 @@ def lan end def information - @info ||= Rubyipmi::Freeipmi::BmcInfo.new(options) + @information ||= Rubyipmi::Freeipmi::BmcInfo.new(options) end def device - @bmcdevice ||= Rubyipmi::Freeipmi::BmcDevice.new(options) + @device ||= Rubyipmi::Freeipmi::BmcDevice.new(options) end end end diff --git a/lib/rubyipmi/freeipmi/commands/bmcconfig.rb b/lib/rubyipmi/freeipmi/commands/bmcconfig.rb index 645ee21..c30859f 100644 --- a/lib/rubyipmi/freeipmi/commands/bmcconfig.rb +++ b/lib/rubyipmi/freeipmi/commands/bmcconfig.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Rubyipmi::Freeipmi class BmcConfig < Rubyipmi::Freeipmi::BaseCommand def initialize(opts = ObservableHash.new) @@ -45,11 +47,11 @@ def configuration # Returns a list of available sections to inspect def listsections - if @sections.length < 1 + if @sections.empty? @options["listsections"] = false value = runcmd @options.delete_notify("listsections") - @sections = @result.split(/\n/) if value + @sections = @result.split("\n") if value end @sections end diff --git a/lib/rubyipmi/freeipmi/commands/bmcdevice.rb b/lib/rubyipmi/freeipmi/commands/bmcdevice.rb index 0f80975..57b610f 100644 --- a/lib/rubyipmi/freeipmi/commands/bmcdevice.rb +++ b/lib/rubyipmi/freeipmi/commands/bmcdevice.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Rubyipmi::Freeipmi class BmcDevice < Rubyipmi::Freeipmi::BaseCommand def initialize(opts = ObservableHash.new) @@ -18,7 +20,7 @@ def reset(type = 'cold') key = "#{type}-reset" command(key) else - logger.error("reset type: #{type} is not a valid choice, use warm or cold") if logger + logger&.error("reset type: #{type} is not a valid choice, use warm or cold") raise "reset type: #{type} is not a valid choice, use warm or cold" end end diff --git a/lib/rubyipmi/freeipmi/commands/bmcinfo.rb b/lib/rubyipmi/freeipmi/commands/bmcinfo.rb index fe77888..8cf7c4a 100644 --- a/lib/rubyipmi/freeipmi/commands/bmcinfo.rb +++ b/lib/rubyipmi/freeipmi/commands/bmcinfo.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Rubyipmi::Freeipmi class BmcInfo < Rubyipmi::Freeipmi::BaseCommand def initialize(opts = ObservableHash.new) @@ -8,40 +10,36 @@ def guid options["get-device-guid"] = false status = runcmd options.delete_notify("get-device-guid") - if !status - raise @result - else - @result.chomp.strip - end + raise @result unless status + + @result.chomp.strip end def retrieve bmcinfo = {} status = runcmd subkey = nil - if !status - raise @result - else - @result.lines.each do |line| - # clean up the data from spaces - item = line.split(':') - key = item.first.strip - value = item.last.strip - # if the following condition is met we have subvalues - if key == value && !subkey - subkey = key - bmcinfo[subkey] = [] - elsif key == value && subkey - # subvalue found - bmcinfo[subkey] << value.gsub(/\[|\]/, "") - else - # Normal key/value pair with no subkeys - subkey = nil - bmcinfo[key] = value - end + raise @result unless status + + @result.lines.each do |line| + # clean up the data from spaces + item = line.split(':') + key = item.first.strip + value = item.last.strip + # if the following condition is met we have subvalues + if key == value && !subkey + subkey = key + bmcinfo[subkey] = [] + elsif key == value && subkey + # subvalue found + bmcinfo[subkey] << value.gsub(/\[|\]/, "") + else + # Normal key/value pair with no subkeys + subkey = nil + bmcinfo[key] = value end - return bmcinfo end + bmcinfo end end end diff --git a/lib/rubyipmi/freeipmi/commands/chassis.rb b/lib/rubyipmi/freeipmi/commands/chassis.rb index 7827d38..624ef4b 100644 --- a/lib/rubyipmi/freeipmi/commands/chassis.rb +++ b/lib/rubyipmi/freeipmi/commands/chassis.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Rubyipmi::Freeipmi class Chassis < Rubyipmi::Freeipmi::BaseCommand def initialize(opts = ObservableHash.new) @@ -6,15 +8,15 @@ def initialize(opts = ObservableHash.new) # Turn the led light on / off or with a delay def identify(status = false, delay = 0) - if status - if delay <= 0 - options["chassis-identify"] = "FORCE" - else - options["chassis-identify"] = delay - end - else - options["chassis-identify"] = "TURN-OFF" - end + options["chassis-identify"] = if status + if delay <= 0 + "FORCE" + else + delay + end + else + "TURN-OFF" + end # Run the command value = runcmd options.delete_notify("chassis-identify") @@ -37,7 +39,7 @@ def bootdevice(device, reboot = false, persistent = false) bootstatus = config.bootdevice(device, persistent) power.cycle if reboot && bootstatus else - logger.error("Device with name: #{device} is not a valid boot device for host #{options['hostname']}") if logger + logger&.error("Device with name: #{device} is not a valid boot device for host #{options['hostname']}") raise "Device with name: #{device} is not a valid boot device for host #{options['hostname']}" end end @@ -74,11 +76,9 @@ def status options["get-status"] = false value = runcmd options.delete_notify("get-status") - if value - return parsestatus - else - return value - end + return parsestatus if value + + value end # A currently unsupported method to retrieve the led status diff --git a/lib/rubyipmi/freeipmi/commands/chassisconfig.rb b/lib/rubyipmi/freeipmi/commands/chassisconfig.rb index f55a74a..605184a 100644 --- a/lib/rubyipmi/freeipmi/commands/chassisconfig.rb +++ b/lib/rubyipmi/freeipmi/commands/chassisconfig.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Rubyipmi::Freeipmi class ChassisConfig < Rubyipmi::Freeipmi::BaseCommand def initialize(opts = ObservableHash.new) @@ -36,11 +38,11 @@ def bootdevices def bootpersistent(value) # TODO: find out if we can specify multiple key-pair values - if value == true - flag = "Chassis_Boot_Flags:Boot_Flags_Persistent=Yes" - else - flag = "Chassis_Boot_Flags:Boot_Flags_Persistent=No" - end + flag = if value == true + "Chassis_Boot_Flags:Boot_Flags_Persistent=Yes" + else + "Chassis_Boot_Flags:Boot_Flags_Persistent=No" + end @options["key-pair"] = "\"#{flag}\"" value = commit @options.delete_notify("key-pair") diff --git a/lib/rubyipmi/freeipmi/commands/fru.rb b/lib/rubyipmi/freeipmi/commands/fru.rb index 2373cf8..fb8164a 100644 --- a/lib/rubyipmi/freeipmi/commands/fru.rb +++ b/lib/rubyipmi/freeipmi/commands/fru.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Rubyipmi::Freeipmi class Fru < Rubyipmi::Freeipmi::BaseCommand attr_accessor :list @@ -11,6 +13,7 @@ def initialize(opts = ObservableHash.new) def get_from_list(key) return unless list.key?(DEFAULT_FRU) + list[DEFAULT_FRU][key] if list[DEFAULT_FRU].key?(key) end @@ -53,16 +56,11 @@ def method_missing(method, *_args, &_block) fru = list.fetch(name, nil) # if the user wanted some data from the default fru, lets show the data for the fru. Otherwise # we return the Fru with the given name - if fru.nil? - if list[DEFAULT_FRU].keys.include?(name) - return list[DEFAULT_FRU][name] - else - # maybe we should return nil instead? hmm... - raise NoMethodError - end - else - return fru - end + return fru unless fru.nil? + return list[DEFAULT_FRU][name] if list[DEFAULT_FRU].keys.include?(name) + + # maybe we should return nil instead? hmm... + raise NoMethodError end # parse the fru information @@ -70,19 +68,17 @@ def parse(data) if !data.nil? && !data.empty? parsed_data = [] data.lines.each do |line| - if line =~ /^FRU.*/ - # this is the either the first line of of the fru or another fru - if parsed_data.count != 0 - # we have reached a new fru device so lets record the previous fru - new_fru = FruData.new(parsed_data) - parsed_data = [] - @list[new_fru[:name]] = new_fru - end + # this is the either the first line of of the fru or another fru + if (line =~ /^FRU.*/) && parsed_data.any? + # we have reached a new fru device so lets record the previous fru + new_fru = FruData.new(parsed_data) + parsed_data = [] + @list[new_fru[:name]] = new_fru end parsed_data << line end # process the last fru - if parsed_data.count != 0 + if parsed_data.any? # we have reached a new fru device so lets record the previous fru new_fru = FruData.new(parsed_data) parsed_data = [] @@ -95,7 +91,7 @@ def parse(data) # run the command and return result def command value = runcmd - return @result if value + @result if value end end @@ -111,14 +107,13 @@ def initialize(data) # parse the fru information that should be an array of lines def parse(data) return unless data + data.each do |line| key, value = line.split(':', 2) if key =~ /^FRU.*/ - if value =~ /([\w\s]*)\(.*\)/ - self[:name] = $~[1].strip.gsub(/\ /, '_').downcase - end + self[:name] = $~[1].strip.gsub(' ', '_').downcase if value =~ /([\w\s]*)\(.*\)/ else - key = key.strip.gsub(/\ /, '_').downcase.gsub(/fru_/, '') + key = key.strip.gsub(' ', '_').downcase.gsub('fru_', '') self[key] = value.strip unless value.nil? end end diff --git a/lib/rubyipmi/freeipmi/commands/lan.rb b/lib/rubyipmi/freeipmi/commands/lan.rb index 102c45e..b65ae9d 100644 --- a/lib/rubyipmi/freeipmi/commands/lan.rb +++ b/lib/rubyipmi/freeipmi/commands/lan.rb @@ -1,8 +1,8 @@ +# frozen_string_literal: true + module Rubyipmi::Freeipmi class Lan - attr_accessor :info - attr_accessor :channel - attr_accessor :config + attr_accessor :info, :channel, :config def initialize(opts) @config = Rubyipmi::Freeipmi::BmcConfig.new(opts) @@ -11,7 +11,7 @@ def initialize(opts) end def info - if @info.length < 1 + if @info.empty? @config.verbose(true) parse(@config.section("Lan_Conf")) @config.verbose(false) @@ -60,12 +60,10 @@ def vlanid # validates that the address, returns true/false def validaddr?(source) - valid = /^([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])){3}$/.match("#{source}") - if valid.nil? - raise "#{source} is not a valid address" - else - return true - end + valid = /^([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])){3}$/.match(source.to_s) + raise "#{source} is not a valid address" if valid.nil? + + true end def ip=(address) @@ -90,7 +88,8 @@ def parse(landata) # clean up the data from spaces next if line.match(/#+/) next if line.match(/Section/i) - line.gsub!(/\t/, '') + + line.gsub!("\t", '') item = line.split(/\s+/) key = item.first.strip.downcase value = item.last.strip diff --git a/lib/rubyipmi/freeipmi/commands/power.rb b/lib/rubyipmi/freeipmi/commands/power.rb index 8bebaca..3c0b6dc 100644 --- a/lib/rubyipmi/freeipmi/commands/power.rb +++ b/lib/rubyipmi/freeipmi/commands/power.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'rubyipmi/commands/mixins/power_mixin' module Rubyipmi::Freeipmi diff --git a/lib/rubyipmi/freeipmi/commands/sensors.rb b/lib/rubyipmi/freeipmi/commands/sensors.rb index f82c8d6..9e01bde 100644 --- a/lib/rubyipmi/freeipmi/commands/sensors.rb +++ b/lib/rubyipmi/freeipmi/commands/sensors.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'rubyipmi/commands/mixins/sensors_mixin' module Rubyipmi::Freeipmi @@ -33,15 +35,15 @@ def initialize(line) private def normalize(text) - text.gsub(/\ /, '_').gsub(/\./, '').downcase + text.gsub(' ', '_').gsub('.', '').downcase end # Parse the individual sensor # Note: not all fields will exist on every server def parse(line) - fields = [:id_num, :name, :value, :unit, :status, :type, :state, :lower_nonrec, - :lower_crit, :lower_noncrit, :upper_crit, :upper_nonrec, :asserts_enabled, :deasserts_enabled] - data = line.split(/\|/) + fields = %i[id_num name value unit status type state lower_nonrec + lower_crit lower_noncrit upper_crit upper_nonrec asserts_enabled deasserts_enabled] + data = line.split('|') # should we ever encounter a field not in the fields list, just use a counter based fieldname so we just # use field1, field2, field3, ... i = 0 diff --git a/lib/rubyipmi/freeipmi/connection.rb b/lib/rubyipmi/freeipmi/connection.rb index 9225518..c8ca069 100644 --- a/lib/rubyipmi/freeipmi/connection.rb +++ b/lib/rubyipmi/freeipmi/connection.rb @@ -1,9 +1,11 @@ +# frozen_string_literal: true + require 'rubyipmi/freeipmi/errorcodes' require 'rubyipmi/observablehash' require 'rubyipmi/commands/basecommand' require 'rubyipmi/freeipmi/commands/basecommand' -Dir[File.dirname(__FILE__) + '/commands/*.rb'].each do |file| +Dir["#{File.dirname(__FILE__)}/commands/*.rb"].sort.each do |file| require file end @@ -15,12 +17,13 @@ class Connection DRIVERS_MAP = { 'lan15' => 'LAN', 'lan20' => 'LAN_2_0', - 'open' => 'OPENIPMI' - } + 'open' => 'OPENIPMI' + }.freeze def initialize(user, pass, host, opts) @options = Rubyipmi::ObservableHash.new raise("Must provide a host to connect to") unless host + @options["hostname"] = host # Credentials can also be stored in the freeipmi configuration file # So they are not required @@ -29,15 +32,15 @@ def initialize(user, pass, host, opts) if opts.key?(:privilege) @options["privilege-level"] = opts[:privilege] # privilege type end - # Note: rubyipmi should auto detect which driver to use so its unnecessary to specify the driver unless + # NOTE: rubyipmi should auto detect which driver to use so its unnecessary to specify the driver unless # the user really wants to @options['driver-type'] = DRIVERS_MAP[opts[:driver]] unless DRIVERS_MAP[opts[:driver]].nil? end # test the connection to ensure we can at least make a single call def connection_works? - ! (bmc.info.nil? || bmc.info.empty?) - rescue + !(bmc.info.nil? || bmc.info.empty?) + rescue StandardError false end diff --git a/lib/rubyipmi/freeipmi/errorcodes.rb b/lib/rubyipmi/freeipmi/errorcodes.rb index 80e8af8..3b3e7ff 100644 --- a/lib/rubyipmi/freeipmi/errorcodes.rb +++ b/lib/rubyipmi/freeipmi/errorcodes.rb @@ -1,9 +1,11 @@ +# frozen_string_literal: true + module Rubyipmi::Freeipmi class ErrorCodes CODES = { "authentication type unavailable for attempted privilege level" => {"driver-type" => "LAN_2_0"}, "authentication type unavailable for attempted privilege level\n" => {"driver-type" => "LAN_2_0"} - } + }.freeze def self.length CODES.length @@ -16,7 +18,7 @@ def self.code def self.search(code) # example error code: # "/usr/local/sbin/bmc-info: authentication type unavailable for attempted privilege level\n" - code.chomp! # clean up newline + code = code.chomp # clean up newline code = code.split(':').last.strip # clean up left hand side and strip white space from sides fix = CODES.fetch(code, nil) if fix.nil? @@ -25,6 +27,7 @@ def self.search(code) end end raise "No Fix found" if fix.nil? + fix end end diff --git a/lib/rubyipmi/ipmitool/commands/basecommand.rb b/lib/rubyipmi/ipmitool/commands/basecommand.rb index b1b04a0..d0a5b57 100644 --- a/lib/rubyipmi/ipmitool/commands/basecommand.rb +++ b/lib/rubyipmi/ipmitool/commands/basecommand.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'rubyipmi/ipmitool/errorcodes' module Rubyipmi::Ipmitool @@ -5,7 +7,7 @@ class BaseCommand < Rubyipmi::BaseCommand def setpass super @options["f"] = @passfile.path - @passfile.write "#{@options['P']}" + @passfile.write @options['P'].to_s @passfile.rewind @passfile.close end @@ -21,6 +23,7 @@ def makecommand # must remove from command line as its handled via conf file next if k == "P" next if k == "cmdargs" + args += ["-#{k}", v] end @@ -35,12 +38,12 @@ def makecommand # until all the fixes are exhausted or a error not defined in the errorcodes is found def find_fix(result) return unless result + # The errorcode code hash contains the fix begin fix = ErrorCodes.search(result) @options.merge_notify!(fix) - - rescue + rescue StandardError raise "Could not find fix for error code: \n#{result}" end end diff --git a/lib/rubyipmi/ipmitool/commands/bmc.rb b/lib/rubyipmi/ipmitool/commands/bmc.rb index 67ec7ad..3dfc6b7 100644 --- a/lib/rubyipmi/ipmitool/commands/bmc.rb +++ b/lib/rubyipmi/ipmitool/commands/bmc.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Rubyipmi::Ipmitool class Bmc < Rubyipmi::Ipmitool::BaseCommand attr_accessor :config @@ -12,7 +14,7 @@ def lan end def info - if @bmcinfo.length > 0 + if @bmcinfo.length.positive? @bmcinfo else retrieve @@ -32,9 +34,9 @@ def reset(type = 'cold') @options["cmdargs"] = "bmc reset #{type}" value = runcmd @options.delete_notify("cmdargs") - return value + value else - logger.error("reset type: #{type} is not a valid choice, use warm or cold") if logger + logger&.error("reset type: #{type} is not a valid choice, use warm or cold") raise "reset type: #{type} is not a valid choice, use warm or cold" end end @@ -44,6 +46,7 @@ def guid value = runcmd @options.delete_notify("cmdargs") return unless value + @result.lines.each do |line| line.chomp line.split(":").last.strip if line =~ /GUID/ @@ -56,29 +59,27 @@ def retrieve status = runcmd @options.delete_notify("cmdargs") subkey = nil - if !status - raise @result - else - @result.lines.each do |line| - # clean up the data from spaces - item = line.split(':') - key = item.first.strip - value = item.last.strip - # if the following condition is met we have subvalues - if value.empty? - subkey = key - @bmcinfo[subkey] = [] - elsif key == value && subkey - # subvalue found - @bmcinfo[subkey] << value - else - # Normal key/value pair with no subkeys - subkey = nil - @bmcinfo[key] = value - end + raise @result unless status + + @result.lines.each do |line| + # clean up the data from spaces + item = line.split(':') + key = item.first.strip + value = item.last.strip + # if the following condition is met we have subvalues + if value.empty? + subkey = key + @bmcinfo[subkey] = [] + elsif key == value && subkey + # subvalue found + @bmcinfo[subkey] << value + else + # Normal key/value pair with no subkeys + subkey = nil + @bmcinfo[key] = value end - return @bmcinfo end + @bmcinfo end end end diff --git a/lib/rubyipmi/ipmitool/commands/chassis.rb b/lib/rubyipmi/ipmitool/commands/chassis.rb index 835130b..9c5acb6 100644 --- a/lib/rubyipmi/ipmitool/commands/chassis.rb +++ b/lib/rubyipmi/ipmitool/commands/chassis.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Rubyipmi::Ipmitool class Chassis < Rubyipmi::Ipmitool::BaseCommand def initialize(opts = ObservableHash.new) @@ -7,15 +9,15 @@ def initialize(opts = ObservableHash.new) # Turn the led light on / off or with a delay # status means to enable or disable the blinking def identify(status = false, delay = 0) - if status - if !delay.between?(1, 255) - options["cmdargs"] = "chassis identify 255" - else - options["cmdargs"] = "chassis identify #{delay}" - end - else - options["cmdargs"] = "chassis identify 0" - end + options["cmdargs"] = if status + if delay.between?(1, 255) + "chassis identify #{delay}" + else + "chassis identify 255" + end + else + "chassis identify 0" + end # Run the command value = runcmd options.delete_notify("cmdargs") @@ -38,7 +40,7 @@ def bootdevice(device, reboot = false, persistent = false) bootstatus = config.bootdevice(device, persistent) power.cycle if reboot && status else - logger.debug("Device with name: #{device} is not a valid boot device for host #{options['hostname']}") if logger + logger&.debug("Device with name: #{device} is not a valid boot device for host #{options['hostname']}") raise "Device with name: #{device} is not a valid boot device for host #{options['hostname']}" end bootstatus diff --git a/lib/rubyipmi/ipmitool/commands/chassisconfig.rb b/lib/rubyipmi/ipmitool/commands/chassisconfig.rb index 4355686..edef941 100644 --- a/lib/rubyipmi/ipmitool/commands/chassisconfig.rb +++ b/lib/rubyipmi/ipmitool/commands/chassisconfig.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Rubyipmi::Ipmitool class ChassisConfig < Rubyipmi::Ipmitool::BaseCommand def initialize(opts = ObservableHash.new) @@ -6,11 +8,11 @@ def initialize(opts = ObservableHash.new) # Set the boot device def bootdevice(device, persistent = false) - if persistent - @options["cmdargs"] = "chassis bootdev #{device}" - else - @options["cmdargs"] = "chassis bootparam set bootflag force_#{device}" - end + @options["cmdargs"] = if persistent + "chassis bootdev #{device}" + else + "chassis bootparam set bootflag force_#{device}" + end value = runcmd @options.delete_notify("cmdargs") value diff --git a/lib/rubyipmi/ipmitool/commands/fru.rb b/lib/rubyipmi/ipmitool/commands/fru.rb index 276e515..d54b8ef 100644 --- a/lib/rubyipmi/ipmitool/commands/fru.rb +++ b/lib/rubyipmi/ipmitool/commands/fru.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Rubyipmi::Ipmitool class Fru < Rubyipmi::Ipmitool::BaseCommand attr_accessor :list @@ -45,37 +47,32 @@ def method_missing(method, *_args, &_block) fru = list.fetch(name, nil) # if the user wanted some data from the default fru, lets show the data for the fru. Otherwise # we return the Fru with the given name - if fru.nil? - if list[DEFAULT_FRU].keys.include?(name) - return list[DEFAULT_FRU][name] - else - # maybe we should return nil instead? hmm... - raise NoMethodError - end - else - return fru - end + return fru unless fru.nil? + return list[DEFAULT_FRU][name] if list[DEFAULT_FRU].keys.include?(name) + + # maybe we should return nil instead? hmm... + raise NoMethodError end # parse the fru information def parse(data) return unless data + parsed_data = [] data.lines.each do |line| - if line =~ /^FRU.*/ - # this is the either the first line of of the fru or another fru - if parsed_data.count != 0 - # we have reached a new fru device so lets record the previous fru - new_fru = FruData.new(parsed_data) - parsed_data = [] - @list[new_fru[:name]] = new_fru - end - + # this is the either the first line of of the fru or another fru + if (line =~ /^FRU.*/) && parsed_data.any? + # we have reached a new fru device so lets record the previous fru + new_fru = FruData.new(parsed_data) + parsed_data = [] + @list[new_fru[:name]] = new_fru end + parsed_data << line end # process the last fru - return if parsed_data.count == 0 + return if parsed_data.none? + # we have reached a new fru device so lets record the previous fru new_fru = FruData.new(parsed_data) parsed_data = [] @@ -87,7 +84,7 @@ def command @options["cmdargs"] = "fru" value = runcmd @options.delete_notify("cmdargs") - return @result if value + @result if value end end @@ -103,14 +100,13 @@ def initialize(data) # parse the fru information that should be an array of lines def parse(data) return unless data + data.each do |line| key, value = line.split(':', 2) if key =~ /^FRU\s+Device.*/ - if value =~ /([\w\s]*)\(.*\)/ - self[:name] = $~[1].strip.gsub(/\ /, '_').downcase - end + self[:name] = $~[1].strip.gsub(' ', '_').downcase if value =~ /([\w\s]*)\(.*\)/ else - key = key.strip.gsub(/\ /, '_').downcase + key = key.strip.gsub(' ', '_').downcase self[key] = value.strip unless value.nil? end end diff --git a/lib/rubyipmi/ipmitool/commands/lan.rb b/lib/rubyipmi/ipmitool/commands/lan.rb index e12588a..a9bf69f 100644 --- a/lib/rubyipmi/ipmitool/commands/lan.rb +++ b/lib/rubyipmi/ipmitool/commands/lan.rb @@ -1,7 +1,9 @@ +# frozen_string_literal: true + module Rubyipmi::Ipmitool class Lan < Rubyipmi::Ipmitool::BaseCommand - attr_accessor :info - attr_accessor :channel + attr_accessor :info, :channel + MAX_RETRY = 1 def initialize(opts = ObservableHash.new) @@ -22,10 +24,10 @@ def channel=(num) def info retrycount = 0 - if @info.length < 1 + if @info.empty? begin parse(print) - rescue + rescue StandardError # sometimes we need to get the info from channel 1, # wait for error to occur then retry using channel 1 if retrycount < MAX_RETRY @@ -131,7 +133,7 @@ def parse(landata) end def normalize(text) - text.gsub(/\ /, '_').gsub(/\./, '').downcase + text.gsub(' ', '_').gsub('.', '').downcase end end end diff --git a/lib/rubyipmi/ipmitool/commands/power.rb b/lib/rubyipmi/ipmitool/commands/power.rb index 1cb4384..d98deaa 100644 --- a/lib/rubyipmi/ipmitool/commands/power.rb +++ b/lib/rubyipmi/ipmitool/commands/power.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'rubyipmi/commands/mixins/power_mixin' module Rubyipmi::Ipmitool diff --git a/lib/rubyipmi/ipmitool/commands/sensors.rb b/lib/rubyipmi/ipmitool/commands/sensors.rb index 2eccd81..68fe404 100644 --- a/lib/rubyipmi/ipmitool/commands/sensors.rb +++ b/lib/rubyipmi/ipmitool/commands/sensors.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'rubyipmi/commands/mixins/sensors_mixin' module Rubyipmi::Ipmitool @@ -29,16 +31,16 @@ def initialize(line) private def normalize(text) - text.gsub(/\ /, '_').gsub(/\./, '').downcase + text.gsub(' ', '_').gsub('.', '').downcase end # Parse the individual sensor # Note: not all fields will exist on every server def parse(line) - fields = [:name, :value, :unit, :status, :type, :state, :lower_nonrec, - :lower_crit, :lower_noncrit, :upper_crit, :upper_nonrec, :asserts_enabled, :deasserts_enabled] + fields = %i[name value unit status type state lower_nonrec + lower_crit lower_noncrit upper_crit upper_nonrec asserts_enabled deasserts_enabled] # skip the header - data = line.split(/\|/) + data = line.split('|') # should we ever encounter a field not in the fields list, just use a counter based fieldname i = 0 data.each do |value| diff --git a/lib/rubyipmi/ipmitool/connection.rb b/lib/rubyipmi/ipmitool/connection.rb index 2fc9fbf..0ff949b 100644 --- a/lib/rubyipmi/ipmitool/connection.rb +++ b/lib/rubyipmi/ipmitool/connection.rb @@ -1,9 +1,11 @@ +# frozen_string_literal: true + require 'rubyipmi/ipmitool/errorcodes' require 'rubyipmi/observablehash' require 'rubyipmi/commands/basecommand' require 'rubyipmi/ipmitool/commands/basecommand' -Dir[File.dirname(__FILE__) + '/commands/*.rb'].each do |file| +Dir["#{File.dirname(__FILE__)}/commands/*.rb"].sort.each do |file| require file end @@ -15,27 +17,28 @@ class Connection DRIVERS_MAP = { 'lan15' => 'lan', 'lan20' => 'lanplus', - 'open' => 'open' - } + 'open' => 'open' + }.freeze def initialize(user, pass, host, opts) @options = Rubyipmi::ObservableHash.new raise("Must provide a host to connect to") unless host + @options["H"] = host # Credentials can also be stored in the freeipmi configuration file # So they are not required @options["U"] = user if user @options["P"] = pass if pass @options["L"] = opts[:privilege] if opts.key?(:privilege) - # Note: rubyipmi should auto detect which driver to use so its unnecessary to specify the driver unless + # NOTE: rubyipmi should auto detect which driver to use so its unnecessary to specify the driver unless # the user really wants to. @options['I'] = DRIVERS_MAP[opts[:driver]] unless DRIVERS_MAP[opts[:driver]].nil? end # test the connection to ensure we can at least make a single call def connection_works? - ! (bmc.info.nil? || bmc.info.empty?) - rescue + !(bmc.info.nil? || bmc.info.empty?) + rescue StandardError false end diff --git a/lib/rubyipmi/ipmitool/errorcodes.rb b/lib/rubyipmi/ipmitool/errorcodes.rb index fd3db40..8b2676f 100644 --- a/lib/rubyipmi/ipmitool/errorcodes.rb +++ b/lib/rubyipmi/ipmitool/errorcodes.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Rubyipmi module Ipmitool class ErrorCodes @@ -6,7 +8,7 @@ class ErrorCodes "Error: Unable to establish LAN session\nGet Device ID command failed\n" => {"I" => "lanplus"}, "Authentication type NONE not supported" => {"I" => "lanplus"}, "Error: Unable to establish LAN session\nGet Device ID command failed\n" => {"I" => "lanplus"} - } + }.freeze def self.length CODES.length @@ -18,14 +20,13 @@ def self.code def self.search(code) fix = CODES.fetch(code, nil) - if fix.nil? - CODES.each do |error, result| - # the error should be a subset of the actual erorr - return result if code =~ /.*#{error}.*/i - end - else - return fix + return fix unless fix.nil? + + CODES.each do |error, result| + # the error should be a subset of the actual erorr + return result if code =~ /.*#{error}.*/i end + raise "No Fix found" if fix.nil? end end diff --git a/lib/rubyipmi/observablehash.rb b/lib/rubyipmi/observablehash.rb index 92d307c..48eb96d 100644 --- a/lib/rubyipmi/observablehash.rb +++ b/lib/rubyipmi/observablehash.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require "observer" module Rubyipmi diff --git a/lib/rubyipmi/version.rb b/lib/rubyipmi/version.rb index 1311830..a87c970 100644 --- a/lib/rubyipmi/version.rb +++ b/lib/rubyipmi/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module Rubyipmi - VERSION = '0.13.0'.freeze + VERSION = '0.13.0' end diff --git a/spec/Vagrantfile b/spec/Vagrantfile index 8bb6ddd..f781815 100644 --- a/spec/Vagrantfile +++ b/spec/Vagrantfile @@ -1,3 +1,5 @@ +# frozen_string_literal: true + # -*- mode: ruby -*- # vi: set ft=ruby : diff --git a/spec/integration/bmc_spec.rb b/spec/integration/bmc_spec.rb index 990f280..4dd7aa0 100644 --- a/spec/integration/bmc_spec.rb +++ b/spec/integration/bmc_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe "Bmc" do diff --git a/spec/integration/chassis_config_spec.rb b/spec/integration/chassis_config_spec.rb index 56ae84d..bbfb75a 100644 --- a/spec/integration/chassis_config_spec.rb +++ b/spec/integration/chassis_config_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe "Chassis Config" do diff --git a/spec/integration/chassis_spec.rb b/spec/integration/chassis_spec.rb index 41c03f8..fc85cf8 100644 --- a/spec/integration/chassis_spec.rb +++ b/spec/integration/chassis_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe "Chassis" do diff --git a/spec/integration/connection_spec.rb b/spec/integration/connection_spec.rb index 558ed4a..c0da538 100644 --- a/spec/integration/connection_spec.rb +++ b/spec/integration/connection_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe "Connection" do @@ -35,10 +37,10 @@ end it 'can get diag info' do - expect(@conn.get_diag.keys).to eq([:provider, :frus, :sensors, :bmc_info, :version]) + expect(@conn.get_diag.keys).to eq(%i[provider frus sensors bmc_info version]) end it 'can get version info' do - expect(@conn.bmc.version).to match(/[\d\.]{3,4}/) + expect(@conn.bmc.version).to match(/[\d.]{3,4}/) end end diff --git a/spec/integration/fru_spec.rb b/spec/integration/fru_spec.rb index 19827b0..d026b01 100644 --- a/spec/integration/fru_spec.rb +++ b/spec/integration/fru_spec.rb @@ -1,7 +1,10 @@ +# frozen_string_literal: true + require 'spec_helper' describe "Fru" do attr_accessor :provider + before :each do user ||= ENV["ipmiuser"] || "admin" pass ||= ENV["ipmipass"] || "password" diff --git a/spec/integration/lan_spec.rb b/spec/integration/lan_spec.rb index b730ed8..ad62169 100644 --- a/spec/integration/lan_spec.rb +++ b/spec/integration/lan_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe "Lan" do diff --git a/spec/integration/power_spec.rb b/spec/integration/power_spec.rb index ecdfabf..038735b 100644 --- a/spec/integration/power_spec.rb +++ b/spec/integration/power_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe "Power" do @@ -24,13 +26,12 @@ it "test to check if power status is off" do @conn.chassis.power.off max_count = 12 - while 0 < max_count - if @conn.chassis.power.off? - break - else - sleep(5) - max_count -= 1 - end + while max_count > 0 + break if @conn.chassis.power.off? + + sleep(5) + max_count -= 1 + end expect(@conn.chassis.power.off?).to be true end diff --git a/spec/integration/rubyipmi_spec.rb b/spec/integration/rubyipmi_spec.rb index a4ab34c..e085cad 100644 --- a/spec/integration/rubyipmi_spec.rb +++ b/spec/integration/rubyipmi_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' require 'fileutils' require 'logger' @@ -19,17 +21,15 @@ it "should test if a provider is present" do value = Rubyipmi.is_provider_installed?("ipmitool") value2 = Rubyipmi.is_provider_installed?("freeipmi") - expect((value | value2)).to eq true + expect(value | value2).to eq true end it "should create a connection object if freeipmi is present" do - begin - conn = Rubyipmi.connect(@user, @pass, @host, "freeipmi") - expect(conn.kind_of?(Rubyipmi::Freeipmi::Connection)).to eq true - rescue Exception => e - expect(e.message.match(/freeipmi\ is\ not\ installed/)).to eq true - puts "#{e.message}" - end + conn = Rubyipmi.connect(@user, @pass, @host, "freeipmi") + expect(conn.kind_of?(Rubyipmi::Freeipmi::Connection)).to eq true + rescue Exception => e + expect(e.message.match(/freeipmi\ is\ not\ installed/)).to eq true + puts e.message end it "should create a connection object if ipmitool is present" do @@ -37,18 +37,16 @@ conn = Rubyipmi.connect(@user, @pass, @host, "ipmitool") rescue Exception => e expect(e.message).to match(/ipmitool\ is\ not\ installed/) - puts "#{e.message}" + puts e.message return true end expect(conn.kind_of?(Rubyipmi::Ipmitool::Connection)).to eq true end it "should not create a connection object if a provider is not present" do - begin - Rubyipmi.connect(@user, @pass, @host, "bogus") - rescue Exception => e - expect(e.message).to match(/The IPMI provider: bogus is not installed/) - end + Rubyipmi.connect(@user, @pass, @host, "bogus") + rescue Exception => e + expect(e.message).to match(/The IPMI provider: bogus is not installed/) end it "check to find any available installed providers" do diff --git a/spec/integration/sensor_spec.rb b/spec/integration/sensor_spec.rb index 22f63ca..199baa9 100644 --- a/spec/integration/sensor_spec.rb +++ b/spec/integration/sensor_spec.rb @@ -1,6 +1,9 @@ +# frozen_string_literal: true + require 'spec_helper' describe "Sensors" do attr_accessor :provider + before :each do user = ENV["ipmiuser"] || 'admin' pass = ENV["ipmipass"] || 'password' diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index e90384f..042b578 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + # Only load Coveralls when running in CI with proper credentials # This prevents firewall issues in restricted environments if ENV['CI'] && ENV['COVERALLS_REPO_TOKEN'] @@ -16,15 +18,15 @@ # Dir["#{File.dirname(__FILE__)}/unit/**/*.rb"].each {|f| require f} def command_is_eql?(source, expected) - src = source.split(' ') - exp = expected.split(' ') + src = source.split + exp = expected.split exp - src end def verify_freeipmi_command(cmdobj, exp_args_count, expcmd) actual = cmdobj.lastcall expect(actual.first).to eq(expcmd) - args_match = actual.select { |arg| arg.match?(/^(-{2}[\w-]*=?[-\w\/]*)/) } + args_match = actual.grep(/^(-{2}[\w-]*=?[-\w\/]*)/) # not sure how to exactly test for arguments since they could vary, so we will need to use count for now # args_match.should =~ exp_args expect(args_match.count).to eq(exp_args_count) @@ -33,7 +35,7 @@ def verify_freeipmi_command(cmdobj, exp_args_count, expcmd) def verify_ipmitool_command(cmdobj, exp_args_count, expcmd, required_args) actual = cmdobj.lastcall expect(actual.first).to eq(expcmd) - args_match = actual.select { |arg| arg.match?(/^(-\w)/) } + args_match = actual.grep(/^(-\w)/) expect(actual.include?(required_args)).to eq true # not sure how to exactly test for arguments since they could vary, so we will need to use count for now # args_match.should =~ exp_args @@ -41,7 +43,7 @@ def verify_ipmitool_command(cmdobj, exp_args_count, expcmd, required_args) end def mock_success_status - instance_double(Process::Status, success?: true) + instance_double(Process::Status, :success? => true) end RSpec.configure do |config| diff --git a/spec/unit/freeipmi/bmc-info_spec.rb b/spec/unit/freeipmi/bmc-info_spec.rb index 64394fc..560fe19 100644 --- a/spec/unit/freeipmi/bmc-info_spec.rb +++ b/spec/unit/freeipmi/bmc-info_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe "Bmc" do diff --git a/spec/unit/freeipmi/bmc_spec.rb b/spec/unit/freeipmi/bmc_spec.rb index 75e6b3a..b21c467 100644 --- a/spec/unit/freeipmi/bmc_spec.rb +++ b/spec/unit/freeipmi/bmc_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe "Bmc" do diff --git a/spec/unit/freeipmi/connection_spec.rb b/spec/unit/freeipmi/connection_spec.rb index 9483ef0..6f06f32 100644 --- a/spec/unit/freeipmi/connection_spec.rb +++ b/spec/unit/freeipmi/connection_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe "Bmc" do diff --git a/spec/unit/freeipmi/errorcodes_spec.rb b/spec/unit/freeipmi/errorcodes_spec.rb index 1f96f18..39899bd 100644 --- a/spec/unit/freeipmi/errorcodes_spec.rb +++ b/spec/unit/freeipmi/errorcodes_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' # require 'rubyipmi/Freeipmi/errorcodes' diff --git a/spec/unit/freeipmi/fru_spec.rb b/spec/unit/freeipmi/fru_spec.rb index 78570b1..13c37dc 100644 --- a/spec/unit/freeipmi/fru_spec.rb +++ b/spec/unit/freeipmi/fru_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe :Fru do before :all do diff --git a/spec/unit/freeipmi/sensors_spec.rb b/spec/unit/freeipmi/sensors_spec.rb index 1478b89..fae0ba6 100644 --- a/spec/unit/freeipmi/sensors_spec.rb +++ b/spec/unit/freeipmi/sensors_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe :Sensors do diff --git a/spec/unit/ipmitool/bmc_spec.rb b/spec/unit/ipmitool/bmc_spec.rb index cd5d97c..4258c36 100644 --- a/spec/unit/ipmitool/bmc_spec.rb +++ b/spec/unit/ipmitool/bmc_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe "Bmc" do @@ -69,4 +71,4 @@ '0x00', '0x30' ] -} +}.freeze diff --git a/spec/unit/ipmitool/connection_spec.rb b/spec/unit/ipmitool/connection_spec.rb index 79a5509..78a037e 100644 --- a/spec/unit/ipmitool/connection_spec.rb +++ b/spec/unit/ipmitool/connection_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe :Connection do diff --git a/spec/unit/ipmitool/errorcodes_spec.rb b/spec/unit/ipmitool/errorcodes_spec.rb index 43d7db5..d24957f 100644 --- a/spec/unit/ipmitool/errorcodes_spec.rb +++ b/spec/unit/ipmitool/errorcodes_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' require 'rubyipmi/ipmitool/errorcodes' diff --git a/spec/unit/ipmitool/fru_spec.rb b/spec/unit/ipmitool/fru_spec.rb index c59e260..116e0a7 100644 --- a/spec/unit/ipmitool/fru_spec.rb +++ b/spec/unit/ipmitool/fru_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe :Fru do diff --git a/spec/unit/ipmitool/lan_spec.rb b/spec/unit/ipmitool/lan_spec.rb index fb75ca4..2c21697 100644 --- a/spec/unit/ipmitool/lan_spec.rb +++ b/spec/unit/ipmitool/lan_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe "Lan" do diff --git a/spec/unit/ipmitool/sensors_spec.rb b/spec/unit/ipmitool/sensors_spec.rb index fb0a985..6c42618 100644 --- a/spec/unit/ipmitool/sensors_spec.rb +++ b/spec/unit/ipmitool/sensors_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe :Sensors do diff --git a/spec/unit/rubyipmi_spec.rb b/spec/unit/rubyipmi_spec.rb index e649e7d..83efa45 100644 --- a/spec/unit/rubyipmi_spec.rb +++ b/spec/unit/rubyipmi_spec.rb @@ -1,4 +1,6 @@ -require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') +# frozen_string_literal: true + +require File.expand_path("#{File.dirname(__FILE__)}/../spec_helper") describe :Rubyipmi do before :each do