diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index f6e9c32..cae4755 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -1,80 +1,40 @@ # This configuration was generated by # `rubocop --auto-gen-config` -# on 2016-01-18 12:44:48 +0100 using RuboCop version 0.36.0. +# on 2016-10-04 22:36:56 +0200 using RuboCop version 0.38.0. # The point is for the user to remove these configuration records # one by one as the offenses are removed from the code base. # Note that changes in the inspected code, or installation of new # versions of RuboCop, may require this file to be generated again. - -# Offense count: 3 -# Cop supports --auto-correct. -# Configuration parameters: AllowUnusedKeywordArguments, IgnoreEmptyMethods. -Lint/UnusedMethodArgument: - Exclude: - - 'lib/landrush/action/common.rb' - - 'lib/landrush/config.rb' - - 'test/support/fake_ui.rb' - -# Offense count: 8 +# Offense count: 12 Metrics/AbcSize: - Max: 36 - Exclude: - - 'lib/landrush/command.rb' - - 'lib/landrush/cap/guest/all/read_host_visible_ip_address.rb' + Max: 47 -# Offense count: 1 +# Offense count: 3 # Configuration parameters: CountComments. Metrics/ClassLength: - Max: 250 + Max: 218 -# Offense count: 2 +# Offense count: 3 Metrics/CyclomaticComplexity: Max: 12 -# Offense count: 57 +# Offense count: 144 # Configuration parameters: AllowHeredoc, AllowURI, URISchemes. # URISchemes: http, https Metrics/LineLength: - Max: 157 + Max: 154 -# Offense count: 9 +# Offense count: 14 # Configuration parameters: CountComments. Metrics/MethodLength: - Max: 30 + Max: 29 -# Offense count: 1 +# Offense count: 3 Metrics/PerceivedComplexity: Max: 14 -# Offense count: 1 -# Cop supports --auto-correct. -# Configuration parameters: EnforcedStyle, SupportedStyles. -# SupportedStyles: prefer_alias, prefer_alias_method -Style/Alias: - EnforcedStyle: prefer_alias_method - -# Offense count: 2 -# Cop supports --auto-correct. -# Configuration parameters: EnforcedStyle, SupportedStyles. -# SupportedStyles: percent_q, bare_percent -Style/BarePercentLiterals: - Exclude: - - 'lib/landrush/cap/linux/add_iptables_rule.rb' - - 'lib/landrush/cap/linux/read_host_visible_ip_address.rb' - -# Offense count: 2 -# Cop supports --auto-correct. -# Configuration parameters: IndentWhenRelativeTo, SupportedStyles, IndentOneStep, IndentationWidth. -# SupportedStyles: case, end -Style/CaseIndentation: - Enabled: false - -Style/ConditionalAssignment: - Exclude: - - 'lib/landrush/server.rb' - -# Offense count: 21 +# Offense count: 32 Style/Documentation: Enabled: false @@ -84,129 +44,14 @@ Style/DoubleNegation: - 'lib/landrush/config.rb' - 'lib/landrush/server.rb' -# Offense count: 4 -# Configuration parameters: MinBodyLength. -Style/GuardClause: - Exclude: - - 'lib/landrush/action/teardown.rb' - - 'lib/landrush/server.rb' - - 'test/support/test_server_daemon.rb' - -# Offense count: 13 -# Cop supports --auto-correct. -# Configuration parameters: EnforcedStyle, SupportedStyles, UseHashRocketsWithSymbolValues. -# SupportedStyles: ruby19, ruby19_no_mixed_keys, hash_rockets -Style/HashSyntax: - Enabled: false - -# Offense count: 9 -# Cop supports --auto-correct. -# Configuration parameters: MaxLineLength. -Style/IfUnlessModifier: - Exclude: - - 'lib/landrush/action/common.rb' - - 'lib/landrush/config.rb' - - 'lib/landrush/server.rb' - - 'test/support/test_server_daemon.rb' - -# Offense count: 4 -# Cop supports --auto-correct. -# Configuration parameters: SupportedStyles, IndentationWidth. -# SupportedStyles: special_inside_parentheses, consistent, align_brackets -Style/IndentArray: - EnforcedStyle: consistent - -# Offense count: 1 -# Cop supports --auto-correct. -# Configuration parameters: EnforcedStyle, MinBodyLength, SupportedStyles. -# SupportedStyles: skip_modifier_ifs, always -Style/Next: - Exclude: - - 'lib/landrush/action/setup.rb' - -# Offense count: 4 -# Cop supports --auto-correct. -# Configuration parameters: PreferredDelimiters. -Style/PercentLiteralDelimiters: - Exclude: - - 'landrush.gemspec' - - 'lib/landrush/cap/guest/linux/redirect_dns.rb' - - 'test/landrush/action/setup_test.rb' - - 'test/landrush/store_test.rb' - -# Offense count: 1 -# Cop supports --auto-correct. -# Configuration parameters: EnforcedStyle, SupportedStyles. -# SupportedStyles: lower_case_q, upper_case_q -Style/PercentQLiterals: - Exclude: - - 'lib/landrush/cap/guest/linux/read_host_visible_ip_address.rb' - - -# Offense count: 7 -# Cop supports --auto-correct. -Style/RescueModifier: - Exclude: - - 'lib/landrush/server.rb' - - 'lib/landrush/store.rb' - - 'test/landrush/server_test.rb' - -# Offense count: 6 -# Cop supports --auto-correct. -# Configuration parameters: EnforcedStyle, SupportedStyles. -# SupportedStyles: space, no_space -Style/SpaceAroundEqualsInParameterDefault: - Enabled: false - -# Offense count: 4 -# Cop supports --auto-correct. -# Configuration parameters: EnforcedStyle, SupportedStyles, EnforcedStyleForEmptyBraces, SpaceBeforeBlockParameters. -# SupportedStyles: space, no_space -Style/SpaceInsideBlockBraces: - Enabled: false - -# Offense count: 2 -# Cop supports --auto-correct. -# Configuration parameters: EnforcedStyle, EnforcedStyleForEmptyBraces, SupportedStyles. -# SupportedStyles: space, no_space -Style/SpaceInsideHashLiteralBraces: - Enabled: false - -# Offense count: 3 -# Cop supports --auto-correct. -# Configuration parameters: EnforcedStyle, SupportedStyles. -# SupportedStyles: use_perl_names, use_english_names -Style/SpecialGlobalVars: - Enabled: false - -# Offense count: 62 -# Cop supports --auto-correct. -# Configuration parameters: EnforcedStyle, SupportedStyles, ConsistentQuotesInMultiline. -# SupportedStyles: single_quotes, double_quotes -Style/StringLiterals: - Enabled: false - # Offense count: 1 # Cop supports --auto-correct. -# Configuration parameters: IgnoredMethods. -# IgnoredMethods: respond_to -Style/SymbolProc: - Exclude: - - 'lib/landrush/cap/guest/linux/read_host_visible_ip_address.rb' - -# Offense count: 2 -# Cop supports --auto-correct. # Configuration parameters: ExactNameMatch, AllowPredicates, AllowDSLWriters, IgnoreClassMethods, Whitelist. # Whitelist: to_ary, to_a, to_c, to_enum, to_h, to_hash, to_i, to_int, to_io, to_open, to_path, to_proc, to_r, to_regexp, to_str, to_s, to_sym Style/TrivialAccessors: Exclude: - - 'lib/landrush/config.rb' - 'lib/landrush/server.rb' -# Offense count: 3 -# Cop supports --auto-correct. -Style/UnneededPercentQ: +Style/ConditionalAssignment: Exclude: - - 'landrush.gemspec' - - 'lib/landrush/cap/guest/linux/add_iptables_rule.rb' - - 'lib/landrush/cap/guest/linux/read_host_visible_ip_address.rb' + - 'lib/landrush/server.rb' \ No newline at end of file diff --git a/Gemfile b/Gemfile index a002fb4..9343753 100644 --- a/Gemfile +++ b/Gemfile @@ -12,8 +12,8 @@ end group :development do gem 'vagrant', - :git => 'git://github.com/mitchellh/vagrant.git', - :ref => 'v1.8.4' + git: 'git://github.com/mitchellh/vagrant.git', + ref: 'v1.8.4' gem 'rake', '~> 10' gem 'rubocop', '~> 0.38.0' diff --git a/Rakefile b/Rakefile index 4283adb..cd42c7f 100644 --- a/Rakefile +++ b/Rakefile @@ -12,7 +12,7 @@ CLEAN.include('build') task :init do FileUtils.mkdir_p 'build' end -task :features => :init +task features: :init # Default test task desc 'Run all unit tests' @@ -23,7 +23,7 @@ end # Cucumber acceptance test task Cucumber::Rake::Task.new(:features) -task :features => :init +task features: :init task default: [ :rubocop, diff --git a/features/step_definitions/dns.rb b/features/step_definitions/dns.rb index 3271b7e..602b01a 100644 --- a/features/step_definitions/dns.rb +++ b/features/step_definitions/dns.rb @@ -2,7 +2,7 @@ Then(/^the hostname "([^"]+)" should resolve to "([^"]+)" on the internal DNS server$/) do |host, ip| port = Landrush::Server.port - resolver = Resolv::DNS.new(:nameserver_port => [['localhost', port]], :search => ['local'], :ndots => 1) + resolver = Resolv::DNS.new(nameserver_port: [['localhost', port]], search: ['local'], ndots: 1) ip_resolved = resolver.getaddress(host).to_s expect(ip_resolved).to eq(ip) end diff --git a/landrush.gemspec b/landrush.gemspec index 4d63f7d..b07ad10 100644 --- a/landrush.gemspec +++ b/landrush.gemspec @@ -19,11 +19,11 @@ Gem::Specification.new do |spec| and down, and you can configure static entries to be returned from the server as well. See the README for more documentation. DESCRIP - spec.summary = %q{a vagrant plugin providing consistent DNS visible on host and guests} + spec.summary = 'a vagrant plugin providing consistent DNS visible on host and guests' spec.homepage = 'https://github.com/vagrant-landrush/landrush' spec.license = 'MIT' - spec.files = `git ls-files`.split($/) + spec.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR) spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) } spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) spec.require_paths = ['lib'] diff --git a/lib/landrush/action/common.rb b/lib/landrush/action/common.rb index 3fd26bf..c0b4d16 100644 --- a/lib/landrush/action/common.rb +++ b/lib/landrush/action/common.rb @@ -14,7 +14,7 @@ def self.included(base) base.send :attr_reader, :app, :env end - def initialize(app, env) + def initialize(app, _env) @app = app end @@ -47,7 +47,7 @@ def provider raise "The landrush plugin does not support the #{key} provider yet!" end - if provider_name == :parallels && Gem::Version.new(VagrantPlugins::Parallels::VERSION) < Gem::Version.new("1.0.3") + if provider_name == :parallels && Gem::Version.new(VagrantPlugins::Parallels::VERSION) < Gem::Version.new('1.0.3') raise "The landrush plugin supports the Parallels provider v1.0.3 and later. Please, update your 'vagrant-parallels' plugin." end @@ -73,9 +73,7 @@ def machine_hostname end def read_machine_hostname - if machine.config.vm.hostname - return machine.config.vm.hostname - end + return machine.config.vm.hostname if machine.config.vm.hostname "#{Pathname.pwd.basename}.#{config.tld}" end diff --git a/lib/landrush/action/setup.rb b/lib/landrush/action/setup.rb index 566b92d..4757c34 100644 --- a/lib/landrush/action/setup.rb +++ b/lib/landrush/action/setup.rb @@ -66,15 +66,13 @@ def start_server def setup_static_dns config.hosts.each do |hostname, dns_value| dns_value ||= host_ip_address - unless Store.hosts.has?(hostname, dns_value) - info "adding static DNS entry: #{hostname} => #{dns_value}" - Store.hosts.set hostname, dns_value - if ip_address?(dns_value) - reverse_dns = IPAddr.new(dns_value).reverse - info "adding static reverse DNS entry: #{reverse_dns} => #{dns_value}" - Store.hosts.set(reverse_dns, hostname) - end - end + next if Store.hosts.has?(hostname, dns_value) + info "adding static DNS entry: #{hostname} => #{dns_value}" + Store.hosts.set hostname, dns_value + next unless ip_address?(dns_value) + reverse_dns = IPAddr.new(dns_value).reverse + info "adding static reverse DNS entry: #{reverse_dns} => #{dns_value}" + Store.hosts.set(reverse_dns, hostname) end end diff --git a/lib/landrush/action/teardown.rb b/lib/landrush/action/teardown.rb index 5d48546..9ccf3d6 100644 --- a/lib/landrush/action/teardown.rb +++ b/lib/landrush/action/teardown.rb @@ -17,17 +17,15 @@ def teardown teardown_machine_dns DependentVMs.remove(machine_hostname) - if DependentVMs.none? - teardown_static_dns - teardown_server - end + return unless DependentVMs.none? + teardown_static_dns + teardown_server end def teardown_machine_dns - if Store.hosts.has? machine_hostname - info "removing machine entry: #{machine_hostname}" - Store.hosts.delete(machine_hostname) - end + return unless Store.hosts.has? machine_hostname + info "removing machine entry: #{machine_hostname}" + Store.hosts.delete(machine_hostname) end def teardown_static_dns diff --git a/lib/landrush/cap/guest/linux/add_iptables_rule.rb b/lib/landrush/cap/guest/linux/add_iptables_rule.rb index 14688eb..58095e7 100644 --- a/lib/landrush/cap/guest/linux/add_iptables_rule.rb +++ b/lib/landrush/cap/guest/linux/add_iptables_rule.rb @@ -10,7 +10,7 @@ def self._run(machine, command) machine.communicate.sudo(command) do |data, type| if [:stderr, :stdout].include?(type) color = (type == :stdout) ? :green : :red - machine.env.ui.info(data.chomp, :color => color, :prefix => false) + machine.env.ui.info(data.chomp, color: color, prefix: false) end end end diff --git a/lib/landrush/cap/guest/linux/redirect_dns.rb b/lib/landrush/cap/guest/linux/redirect_dns.rb index dcfa83e..4f47cb7 100644 --- a/lib/landrush/cap/guest/linux/redirect_dns.rb +++ b/lib/landrush/cap/guest/linux/redirect_dns.rb @@ -2,9 +2,9 @@ module Landrush module Cap module Linux module RedirectDns - def self.redirect_dns(machine, target={}) + def self.redirect_dns(machine, target = {}) dns_servers = machine.guest.capability(:configured_dns_servers) - %w[tcp udp].each do |proto| + %w(tcp udp).each do |proto| dns_servers.each do |dns_server| machine.guest.capability( :add_iptables_rule, diff --git a/lib/landrush/cap/host/windows/configure_visibility_on_host.rb b/lib/landrush/cap/host/windows/configure_visibility_on_host.rb index 8c1454c..9cba528 100644 --- a/lib/landrush/cap/host/windows/configure_visibility_on_host.rb +++ b/lib/landrush/cap/host/windows/configure_visibility_on_host.rb @@ -180,6 +180,6 @@ def which(cmd) # Only run the following code when this file is the main file being run # instead of having been required or loaded by another file -if __FILE__ == $0 +if __FILE__ == $PROGRAM_NAME Landrush::Cap::Windows::ConfigureVisibilityOnHost.configure_visibility_on_host(nil, ARGV[0], ARGV[1]) end diff --git a/lib/landrush/command.rb b/lib/landrush/command.rb index 95a3ee4..643e8f9 100644 --- a/lib/landrush/command.rb +++ b/lib/landrush/command.rb @@ -3,7 +3,7 @@ class Command < Vagrant.plugin('2', :command) DAEMON_COMMANDS = %w(start stop restart status).freeze def self.synopsis - "manages DNS for both guest and host" + 'manages DNS for both guest and host' end def execute @@ -18,7 +18,7 @@ def execute if DependentVMs.any? @env.ui.info(DependentVMs.list.map { |dvm| " - #{dvm}" }.join("\n")) else - @env.ui.info("No dependent VMs") + @env.ui.info('No dependent VMs') end elsif command == 'ls' || command == 'list' Landrush::Store.hosts.each do |key, value| @@ -40,7 +40,7 @@ def execute end def boom(msg) - raise Vagrant::Errors::CLIInvalidOptions, :help => usage(msg) + raise Vagrant::Errors::CLIInvalidOptions, help: usage(msg) end def usage(msg); <<-EOS.gsub(/^ /, '') diff --git a/lib/landrush/config.rb b/lib/landrush/config.rb index 0eef751..20603b2 100644 --- a/lib/landrush/config.rb +++ b/lib/landrush/config.rb @@ -15,15 +15,15 @@ class Config < Vagrant.plugin('2', :config) INTERFACE_CLASS_INVALID = "Invalid interface class, should be one of: #{INTERFACE_CLASSES.join(', ')}".freeze DEFAULTS = { - :enabled => false, - :tld => 'vagrant.test', - :upstream_servers => [[:udp, '8.8.8.8', 53], [:tcp, '8.8.8.8', 53]], - :host_ip_address => nil, - :guest_redirect_dns => true, - :host_interface => nil, - :host_interface_excludes => [/lo[0-9]*/, /docker[0-9]+/, /tun[0-9]+/], - :host_interface_class => :ipv4, - :host_redirect_dns => true + enabled: false, + tld: 'vagrant.test', + upstream_servers: [[:udp, '8.8.8.8', 53], [:tcp, '8.8.8.8', 53]], + host_ip_address: nil, + guest_redirect_dns: true, + host_interface: nil, + host_interface_excludes: [/lo[0-9]*/, /docker[0-9]+/, /tun[0-9]+/], + host_interface_class: :ipv4, + host_redirect_dns: true }.freeze def initialize @@ -59,14 +59,12 @@ def host_redirect_dns? @host_redirect_dns end - def host(hostname, ip_address=nil) + def host(hostname, ip_address = nil) @hosts[hostname] = ip_address end - def upstream(ip, port=53, protocol=nil) - if @upstream_servers == UNSET_VALUE - @upstream_servers = [] - end + def upstream(ip, port = 53, protocol = nil) + @upstream_servers = [] if @upstream_servers == UNSET_VALUE if !protocol @upstream_servers.push [:udp, ip, port] @@ -90,7 +88,7 @@ def finalize! end end - def validate(machine) + def validate(_machine) errors = _detected_errors errors.concat validate_host_interface_class diff --git a/lib/landrush/server.rb b/lib/landrush/server.rb index d92155e..0b3997a 100644 --- a/lib/landrush/server.rb +++ b/lib/landrush/server.rb @@ -49,7 +49,7 @@ def self.port=(port) def self.upstream_servers # Doing collect to cast protocol to symbol because JSON store doesn't know about symbols - @upstream_servers ||= Store.config.get('upstream').collect {|i| [i[0].to_sym, i[1], i[2]]} + @upstream_servers ||= Store.config.get('upstream').collect { |i| [i[0].to_sym, i[1], i[2]] } end def self.interfaces @@ -85,11 +85,11 @@ def self.start # Today we don't pass any filehandles, so it isn't a problem. # Future self, make sure this doesn't become a problem. - info = Process.create(:command_line => "ruby #{__FILE__} #{port} #{working_dir}", - :creation_flags => Process::DETACHED_PROCESS, - :process_inherit => false, - :thread_inherit => true, - :cwd => working_dir.to_path) + info = Process.create(command_line: "ruby #{__FILE__} #{port} #{working_dir}", + creation_flags: Process::DETACHED_PROCESS, + process_inherit: false, + thread_inherit: true, + cwd: working_dir.to_path) pid = info.process_id else # Fix https://github.com/vagrant-landrush/landrush/issues/249) @@ -97,9 +97,9 @@ def self.start # and by explicitly closing STDIN, STDOUT, and STDERR pid = spawn('ruby', __FILE__, port.to_s, working_dir.to_s, - :in => :close, :out => :close, :err => :close, - :close_others => true, :chdir => working_dir.to_path, - :pgroup => true) + in: :close, out: :close, err: :close, + close_others: true, chdir: working_dir.to_path, + pgroup: true) Process.detach pid end @@ -141,7 +141,9 @@ def self.restart end def self.pid - IO.read(pid_file).to_i rescue nil + IO.read(pid_file).to_i + rescue + nil end def self.running? @@ -155,20 +157,24 @@ def self.running? raise e unless e.class.name.start_with?('Errno::ENXIO') end else - !!Process.kill(0, pid) rescue false + begin + !!Process.kill(0, pid) + rescue + false + end end end def self.status case process_status - when :running - puts "Daemon status: running pid=#{read_pid}" - when :stopped - puts 'Daemon status: stopped' - else - puts 'Daemon status: unknown' - puts "#{pid_file} exists, but process is not running" - puts "Check log file: #{log_file_path}" + when :running + puts "Daemon status: running pid=#{read_pid}" + when :stopped + puts 'Daemon status: stopped' + else + puts 'Daemon status: unknown' + puts "#{pid_file} exists, but process is not running" + puts "Check log file: #{log_file_path}" end end @@ -184,7 +190,7 @@ def self.run(port, working_dir) @logger.level = Logger::INFO # Start the DNS server - run_dns_server(:listen => interfaces, :logger => @logger) do + run_dns_server(listen: interfaces, logger: @logger) do match(/.*/, IN::A) do |transaction| host = Store.hosts.find(transaction.name) if host @@ -215,7 +221,7 @@ def self.run_dns_server(options = {}, &block) server = RubyDNS::RuleBasedServer.new(options, &block) EventMachine.run do - trap("INT") do + trap('INT') do EventMachine.stop end @@ -227,13 +233,15 @@ def self.run_dns_server(options = {}, &block) def self.check_a_record(host, transaction) value = Store.hosts.get(host) - if value.nil? - return - end + return if value.nil? - if (IPAddr.new(value) rescue nil) + if begin + IPAddr.new(value) + rescue + nil + end name = transaction.name =~ /#{host}/ ? transaction.name : host - transaction.respond!(value, :ttl => 0, :name => name) + transaction.respond!(value, ttl: 0, name: name) else transaction.respond!(Name.create(value), resource_class: IN::CNAME, ttl: 0) check_a_record(value, transaction) @@ -243,17 +251,17 @@ def self.check_a_record(host, transaction) # private methods def self.write_pid(pid) ensure_path_exits(pid_file) - File.open(pid_file, 'w') {|f| f << pid.to_s} + File.open(pid_file, 'w') { |f| f << pid.to_s } end def self.read_pid - IO.read(pid_file).to_i rescue nil + IO.read(pid_file).to_i + rescue + nil end def self.delete_pid_file - if File.exist? pid_file - FileUtils.rm(pid_file) - end + FileUtils.rm(pid_file) if File.exist? pid_file end def self.pid_file @@ -261,18 +269,13 @@ def self.pid_file end def self.process_status - if File.exist? pid_file - return running? ? :running : :unknown - else - return :stopped - end + return running? ? :running : :unknown if File.exist? pid_file + :stopped end def self.ensure_path_exits(file_name) dirname = File.dirname(file_name) - unless File.directory?(dirname) - FileUtils.mkdir_p(dirname) - end + FileUtils.mkdir_p(dirname) unless File.directory?(dirname) end def self.terminate_process(pid) @@ -298,11 +301,11 @@ def self.ensure_ruby_on_path vagrant_binary = File.realpath(vagrant_binary) if File.symlink?(vagrant_binary) # in a Vagrant installation the Ruby executable is in ../embedded/bin relative to the vagrant executable # we don't use File.join here, since even on Cygwin we want a Windows path - see https://github.com/vagrant-landrush/landrush/issues/237 - if Vagrant::Util::Platform.windows? - separator = '\\' - else - separator = '/' - end + separator = if Vagrant::Util::Platform.windows? + '\\' + else + '/' + end embedded_bin_dir = File.dirname(File.dirname(vagrant_binary)) + separator + 'embedded' + separator + 'bin' ENV['PATH'] = embedded_bin_dir + File::PATH_SEPARATOR + ENV['PATH'] if File.exist?(embedded_bin_dir) end @@ -314,7 +317,7 @@ def self.ensure_ruby_on_path # Only run the following code when this file is the main file being run # instead of having been required or loaded by another file -if __FILE__ == $0 +if __FILE__ == $PROGRAM_NAME # TODO, Add some argument checks Landrush::Server.run(ARGV[0], ARGV[1]) end diff --git a/lib/landrush/store.rb b/lib/landrush/store.rb index 36d65ce..30a1d80 100644 --- a/lib/landrush/store.rb +++ b/lib/landrush/store.rb @@ -43,7 +43,11 @@ def has?(key, value = nil) end def find(search) - search = IPAddr.new(search).reverse if (IPAddr.new(search) rescue nil) + search = IPAddr.new(search).reverse if begin + IPAddr.new(search) + rescue + nil + end current_config.keys.detect do |key| key.casecmp(search) == 0 || search =~ /#{key}$/i || @@ -74,7 +78,7 @@ def current_config end def write(config) - File.open(backing_file, "w") do |f| + File.open(backing_file, 'w') do |f| f.write(JSON.pretty_generate(config)) end end diff --git a/lib/landrush/util/retry.rb b/lib/landrush/util/retry.rb index d5656bb..b781ad9 100644 --- a/lib/landrush/util/retry.rb +++ b/lib/landrush/util/retry.rb @@ -1,8 +1,8 @@ module Landrush module Util class Retry - def self.retry(opts=nil) - opts = {tries: 1}.merge(opts || {}) + def self.retry(opts = nil) + opts = { tries: 1 }.merge(opts || {}) n = 0 while n < opts[:tries] return true if yield diff --git a/test/landrush/action/setup_test.rb b/test/landrush/action/setup_test.rb index 3681e0a..08f77b6 100644 --- a/test/landrush/action/setup_test.rb +++ b/test/landrush/action/setup_test.rb @@ -11,7 +11,7 @@ module Action env[:machine].config.landrush.host_redirect_dns = false end - it "calls the next app in the chain" do + it 'calls the next app in the chain' do app = -> (e) { e[:called] = true } setup = Setup.new(app, nil) @@ -20,12 +20,12 @@ module Action env[:called].must_equal true end - it "records the booting host as a dependent VM" do + it 'records the booting host as a dependent VM' do setup = Setup.new(app, nil) setup.call(env) - DependentVMs.list.must_equal %w[somehost.vagrant.test] + DependentVMs.list.must_equal %w(somehost.vagrant.test) end it "starts the landrush server if it's not already started" do @@ -48,7 +48,7 @@ module Action Server.pid.must_equal original_pid end - it "does nothing if it is not enabled via config" do + it 'does nothing if it is not enabled via config' do setup = Setup.new(app, nil) env[:machine].config.landrush.disable @@ -57,7 +57,7 @@ module Action DependentVMs.list.must_equal [] end - it "for multiple private network IPs host visible IP cant be retrieved if host_ip_address is set" do + it 'for multiple private network IPs host visible IP cant be retrieved if host_ip_address is set' do setup = Setup.new(app, nil) env[:machine].config.vm.network :private_network, ip: '42.42.42.41' @@ -67,7 +67,7 @@ module Action Store.hosts.get('somehost.vagrant.test').must_equal '42.42.42.42' end - it "is possible to add cnames via the config.landrush.host configuration option" do + it 'is possible to add cnames via the config.landrush.host configuration option' do setup = Setup.new(app, nil) env[:machine].config.landrush.host 'foo', 'bar' @@ -85,7 +85,7 @@ module Action Store.hosts.get('somehost.vagrant.test').must_equal '1.2.3.4' end - it "does nothing if it is not enabled via config" do + it 'does nothing if it is not enabled via config' do setup = Setup.new(app, nil) env = fake_environment(enabled: false) diff --git a/test/landrush/action/teardown_test.rb b/test/landrush/action/teardown_test.rb index 307dc29..f18e5e8 100644 --- a/test/landrush/action/teardown_test.rb +++ b/test/landrush/action/teardown_test.rb @@ -5,7 +5,7 @@ module Landrush module Action describe Teardown do - it "calls the next app in the chain" do + it 'calls the next app in the chain' do env = fake_environment app = -> (e) { e[:called] = true } teardown = Teardown.new(app, nil) @@ -26,7 +26,7 @@ module Action Store.hosts.get('somehost.vagrant.test').must_equal nil end - it "removes the machine as a dependent VM" do + it 'removes the machine as a dependent VM' do app = proc {} teardown = Teardown.new(app, nil) env = fake_environment @@ -37,7 +37,7 @@ module Action DependentVMs.list.must_equal [] end - it "stops the landrush server when there are no dependent machines left" do + it 'stops the landrush server when there are no dependent machines left' do app = proc {} teardown = Teardown.new(app, nil) env = fake_environment @@ -48,7 +48,7 @@ module Action Server.running?.must_equal false end - it "leaves the landrush server when other dependent vms exist" do + it 'leaves the landrush server when other dependent vms exist' do app = proc {} teardown = Teardown.new(app, nil) env = fake_environment @@ -60,7 +60,7 @@ module Action Server.running?.must_equal true end - it "leaves static entries when other dependent vms exist" do + it 'leaves static entries when other dependent vms exist' do app = proc {} teardown = Teardown.new(app, nil) env = fake_environment @@ -83,7 +83,7 @@ module Action Server.running?.must_equal false end - it "does nothing when landrush is disabled" do + it 'does nothing when landrush is disabled' do # somewhat unrealistic since this entry shouldn't be there if it was # disabled in the first place, but oh well Store.hosts.set('somehost.vagrant.test', '1.2.3.4') diff --git a/test/landrush/cap/guest/linux/configured_dns_servers_test.rb b/test/landrush/cap/guest/linux/configured_dns_servers_test.rb index dacf413..7b02fa1 100644 --- a/test/landrush/cap/guest/linux/configured_dns_servers_test.rb +++ b/test/landrush/cap/guest/linux/configured_dns_servers_test.rb @@ -4,30 +4,30 @@ let(:machine) { fake_machine } before do - Landrush::Cap::Linux::ConfiguredDnsServers.instance_variable_set("@dns_servers", nil) + Landrush::Cap::Linux::ConfiguredDnsServers.instance_variable_set('@dns_servers', nil) end describe 'configured_dns_servers' do it 'parses out a single resolv.conf dns server' do - machine.communicate.stubs(:sudo).yields(:stdout, "nameserver 12.23.34.45") + machine.communicate.stubs(:sudo).yields(:stdout, 'nameserver 12.23.34.45') dns_servers = Landrush::Cap::Linux::ConfiguredDnsServers.configured_dns_servers(machine) - dns_servers.must_equal(["12.23.34.45"]) + dns_servers.must_equal(['12.23.34.45']) end it 'parses out multiple the resolv.conf dns servers' do machine.communicate.stubs(:sudo).yields(:stdout, [ - "nameserver 12.23.34.45", - "nameserver 45.34.23.12" + 'nameserver 12.23.34.45', + 'nameserver 45.34.23.12' ].join("\n")) dns_servers = Landrush::Cap::Linux::ConfiguredDnsServers.configured_dns_servers(machine) dns_servers.must_equal([ - "12.23.34.45", - "45.34.23.12" - ]) + '12.23.34.45', + '45.34.23.12' + ]) end end end diff --git a/test/landrush/cap/guest/linux/redirect_dns_test.rb b/test/landrush/cap/guest/linux/redirect_dns_test.rb index 03c1b18..d1bc4c7 100644 --- a/test/landrush/cap/guest/linux/redirect_dns_test.rb +++ b/test/landrush/cap/guest/linux/redirect_dns_test.rb @@ -7,10 +7,10 @@ it 'fetches the dns servers from the machine, and adds one iptables rule per server' do machine.guest.stubs(:capability).with(:configured_dns_servers).returns(%w(1.2.3.4 4.5.6.7)) - machine.guest.expects(:capability).with(:add_iptables_rule, "OUTPUT -t nat -p tcp -d 1.2.3.4 --dport 53 -j DNAT --to-destination 2.3.4.5:4321").once - machine.guest.expects(:capability).with(:add_iptables_rule, "OUTPUT -t nat -p udp -d 1.2.3.4 --dport 53 -j DNAT --to-destination 2.3.4.5:4321").once - machine.guest.expects(:capability).with(:add_iptables_rule, "OUTPUT -t nat -p tcp -d 4.5.6.7 --dport 53 -j DNAT --to-destination 2.3.4.5:4321").once - machine.guest.expects(:capability).with(:add_iptables_rule, "OUTPUT -t nat -p udp -d 4.5.6.7 --dport 53 -j DNAT --to-destination 2.3.4.5:4321").once + machine.guest.expects(:capability).with(:add_iptables_rule, 'OUTPUT -t nat -p tcp -d 1.2.3.4 --dport 53 -j DNAT --to-destination 2.3.4.5:4321').once + machine.guest.expects(:capability).with(:add_iptables_rule, 'OUTPUT -t nat -p udp -d 1.2.3.4 --dport 53 -j DNAT --to-destination 2.3.4.5:4321').once + machine.guest.expects(:capability).with(:add_iptables_rule, 'OUTPUT -t nat -p tcp -d 4.5.6.7 --dport 53 -j DNAT --to-destination 2.3.4.5:4321').once + machine.guest.expects(:capability).with(:add_iptables_rule, 'OUTPUT -t nat -p udp -d 4.5.6.7 --dport 53 -j DNAT --to-destination 2.3.4.5:4321').once Landrush::Cap::Linux::RedirectDns.redirect_dns( machine, diff --git a/test/landrush/config_test.rb b/test/landrush/config_test.rb index b6ac205..c82ae43 100644 --- a/test/landrush/config_test.rb +++ b/test/landrush/config_test.rb @@ -1,7 +1,7 @@ require_relative '../test_helper' describe 'Landrush::Config' do - it "supports enabling via accessor style" do + it 'supports enabling via accessor style' do machine = fake_machine config = machine.config.landrush @@ -11,7 +11,7 @@ config.enabled?.must_equal false end - it "is backwards-compatible with the old method call style" do + it 'is backwards-compatible with the old method call style' do machine = fake_machine config = machine.config.landrush @@ -21,12 +21,12 @@ config.enabled?.must_equal false end - it "should validate host_interface_class" do + it 'should validate host_interface_class' do machine = fake_machine config = machine.config.landrush validation_success = [] - validation_error = [Landrush::Config::INTERFACE_CLASS_INVALID, { :fields => 'host_interface_class' }] + validation_error = [Landrush::Config::INTERFACE_CLASS_INVALID, { fields: 'host_interface_class' }] Landrush::Config::INTERFACE_CLASSES.each do |sym| machine.config.landrush.host_interface_class = sym diff --git a/test/landrush/dependent_vms_test.rb b/test/landrush/dependent_vms_test.rb index f6c3181..c68f82d 100644 --- a/test/landrush/dependent_vms_test.rb +++ b/test/landrush/dependent_vms_test.rb @@ -2,23 +2,23 @@ module Landrush describe DependentVMs do - describe "any?" do - it "reports false when nothing has happened" do + describe 'any?' do + it 'reports false when nothing has happened' do DependentVMs.any?.must_equal false end - it "reports true once a machine has been added" do + it 'reports true once a machine has been added' do DependentVMs.add('recordme.example.test') DependentVMs.any?.must_equal true end - it "reports false if a machine has been added then removed" do + it 'reports false if a machine has been added then removed' do DependentVMs.add('recordme.example.test') DependentVMs.remove('recordme.example.test') DependentVMs.any?.must_equal false end - it "reports true if not all machines have been removed" do + it 'reports true if not all machines have been removed' do DependentVMs.add('recordme.example.test') DependentVMs.add('alsome.example.test') DependentVMs.remove('recordme.example.test') diff --git a/test/landrush/server_test.rb b/test/landrush/server_test.rb index 40e2bba..30301f3 100644 --- a/test/landrush/server_test.rb +++ b/test/landrush/server_test.rb @@ -4,13 +4,17 @@ module Landrush describe Server do def query(host) - Resolv::DNS.open(:nameserver_port => [["127.0.0.1", Server.port]]) do |r| + Resolv::DNS.open(nameserver_port: [['127.0.0.1', Server.port]]) do |r| r.getaddress(host).to_s end end def wait_for_port - sleep 1 until (TCPSocket.open('127.0.0.1', Server.port) rescue nil) + sleep 1 until begin + TCPSocket.open('127.0.0.1', Server.port) + rescue + nil + end end describe 'start/stop' do @@ -32,7 +36,7 @@ def wait_for_port wait_for_port - query("phinze.com").must_match(/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/) + query('phinze.com').must_match(/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/) end it 'responds properly to configured machine entries' do diff --git a/test/landrush/store_test.rb b/test/landrush/store_test.rb index d84a99f..b0953be 100644 --- a/test/landrush/store_test.rb +++ b/test/landrush/store_test.rb @@ -3,7 +3,7 @@ module Landrush describe Store do before do - @temp_file = Tempfile.new(%w[landrush_test_store .json]) + @temp_file = Tempfile.new(%w(landrush_test_store .json)) @store = Store.new(@temp_file) end @@ -11,14 +11,14 @@ module Landrush @temp_file.unlink end - describe "set" do - it "sets the key to the value and makes it available for getting" do + describe 'set' do + it 'sets the key to the value and makes it available for getting' do @store.set('foo', 'bar') @store.get('foo').must_equal 'bar' end - it "allows updating keys that already exist" do + it 'allows updating keys that already exist' do @store.set('foo', 'bar') @store.set('foo', 'qux') @@ -26,12 +26,12 @@ module Landrush end end - describe "get" do - it "returns nil for unset values" do + describe 'get' do + it 'returns nil for unset values' do @store.get('notakey').must_equal nil end - it "returns the latest set value (no caching)" do + it 'returns the latest set value (no caching)' do @store.set('foo', 'first') @store.get('foo').must_equal 'first' @store.set('foo', 'second') @@ -41,8 +41,8 @@ module Landrush end end - describe "delete" do - it "removes the key from the store" do + describe 'delete' do + it 'removes the key from the store' do @store.set('now', 'you see me') @store.get('now').must_equal 'you see me' @@ -53,8 +53,8 @@ module Landrush end end - describe "find" do - it "returns the key that matches the end of the search term" do + describe 'find' do + it 'returns the key that matches the end of the search term' do @store.set('somehost.vagrant.test', 'here') @store.find('foo.somehost.vagrant.test').must_equal 'somehost.vagrant.test' @@ -63,12 +63,12 @@ module Landrush @store.find('host.vagrant.test').must_equal nil end - it "returns exact matches too" do + it 'returns exact matches too' do @store.set('somehost.vagrant.test', 'here') @store.find('somehost.vagrant.test').must_equal 'somehost.vagrant.test' end - it "returns for prefix searches as well" do + it 'returns for prefix searches as well' do @store.set('somehost.vagrant.test', 'here') @store.find('somehost').must_equal 'somehost.vagrant.test' diff --git a/test/support/test_server_daemon.rb b/test/support/test_server_daemon.rb index bec61c0..b4ff628 100644 --- a/test/support/test_server_daemon.rb +++ b/test/support/test_server_daemon.rb @@ -16,7 +16,7 @@ def self.silence def self.included(base) orig_stop_method = base.method(:stop) base.define_singleton_method :stop do - SilenceOutput.silence {orig_stop_method.call} + SilenceOutput.silence { orig_stop_method.call } end end end @@ -31,9 +31,7 @@ module TestServerHooks def teardown super # Cleanup any stray server instances from tests - if Landrush::Server.running? - Landrush::Server.stop - end + Landrush::Server.stop if Landrush::Server.running? Landrush::Store.reset end end diff --git a/test/test_helper.rb b/test/test_helper.rb index 40c58ae..45174d8 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -1,4 +1,4 @@ -$:.push(File.expand_path('../../lib', __FILE__)) +$LOAD_PATH.push(File.expand_path('../../lib', __FILE__)) require 'bundler/setup' require 'minitest/spec' @@ -19,23 +19,23 @@ # Putting include/exclude out of order is kind of the point though ;) def fake_addresses [ - {'name' => 'ipv6empty1', 'ipv4' => '172.28.128.10', 'ipv6' => ''}, - {'name' => 'ipv4empty1', 'ipv4' => '', 'ipv6' => '::10'}, - {'name' => 'ipv6empty2', 'ipv4' => '172.28.128.11', 'ipv6' => ''}, - {'name' => 'ipv4empty2', 'ipv4' => '', 'ipv6' => '::11'}, - {'name' => 'exclude1', 'ipv4' => '172.28.128.1', 'ipv6' => '::1'}, - {'name' => 'include1', 'ipv4' => '172.28.128.2', 'ipv6' => '::2'}, - {'name' => 'include2', 'ipv4' => '172.28.128.3', 'ipv6' => '::3'}, - {'name' => 'include3', 'ipv4' => '172.28.128.4', 'ipv6' => '::4'}, - {'name' => 'exclude2', 'ipv4' => '172.28.128.5', 'ipv6' => '::5'}, - {'name' => 'exclude3', 'ipv4' => '172.28.128.6', 'ipv6' => '::6'} + { 'name' => 'ipv6empty1', 'ipv4' => '172.28.128.10', 'ipv6' => '' }, + { 'name' => 'ipv4empty1', 'ipv4' => '', 'ipv6' => '::10' }, + { 'name' => 'ipv6empty2', 'ipv4' => '172.28.128.11', 'ipv6' => '' }, + { 'name' => 'ipv4empty2', 'ipv4' => '', 'ipv6' => '::11' }, + { 'name' => 'exclude1', 'ipv4' => '172.28.128.1', 'ipv6' => '::1' }, + { 'name' => 'include1', 'ipv4' => '172.28.128.2', 'ipv6' => '::2' }, + { 'name' => 'include2', 'ipv4' => '172.28.128.3', 'ipv6' => '::3' }, + { 'name' => 'include3', 'ipv4' => '172.28.128.4', 'ipv6' => '::4' }, + { 'name' => 'exclude2', 'ipv4' => '172.28.128.5', 'ipv6' => '::5' }, + { 'name' => 'exclude3', 'ipv4' => '172.28.128.6', 'ipv6' => '::6' } ] end -def fake_environment(options = {enabled: true}) +def fake_environment(options = { enabled: true }) # For the home_path we want the base Vagrant directory vagrant_test_home = Pathname(Landrush::Server.working_dir).parent.parent - {machine: fake_machine(options), ui: FakeUI.new, home_path: vagrant_test_home} + { machine: fake_machine(options), ui: FakeUI.new, home_path: vagrant_test_home } end class FakeUI @@ -115,7 +115,7 @@ def vm end end -def fake_machine(options={}) +def fake_machine(options = {}) env = options.fetch(:env, Vagrant::Environment.new) machine = Vagrant::Machine.new( 'fake_machine', @@ -130,7 +130,7 @@ def fake_machine(options={}) env.vagrantfile ) - machine.instance_variable_set("@communicator", RecordingCommunicator.new) + machine.instance_variable_set('@communicator', RecordingCommunicator.new) machine.config.landrush.enabled = options.fetch(:enabled, false) machine.config.landrush.host_interface = nil @@ -149,7 +149,7 @@ def fake_static_entry(env, hostname, ip) module MiniTest class Spec - alias_method :hush, :capture_io + alias hush capture_io end end