Skip to content

Commit

Permalink
Issue vagrant-landrush#16 Making tests run on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
Hardy Ferentschik authored and hferentschik committed Apr 8, 2016
1 parent 7bf63d2 commit c498614
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 16 deletions.
1 change: 1 addition & 0 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ Style/RescueModifier:
Exclude:
- 'lib/landrush/server.rb'
- 'lib/landrush/store.rb'
- 'test/landrush/server_test.rb'

# Offense count: 6
# Cop supports --auto-correct.
Expand Down
15 changes: 15 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,15 +1,30 @@
require 'bundler/gem_tasks'
require 'rake'
require 'rake/testtask'
require 'rake/clean'
require 'rubocop/rake_task'

CLOBBER.include('pkg/*')

# Default test task
Rake::TestTask.new do |t|
t.pattern = 'test/**/*_test.rb'
t.libs << 'test'
end

targets = []
Dir.glob('./test/**/*_test.rb').each do |test_file|
targets << test_file
end

targets.each do |target|
target_name = 'test-' + File.basename(target).chomp('_test.rb')
Rake::TestTask.new(target_name.to_sym) do |t|
t.pattern = target.to_s
t.libs << 'test'
end
end

task default: [
:rubocop,
:test
Expand Down
2 changes: 1 addition & 1 deletion lib/landrush/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def self.start
end

def self.stop
puts 'Stopping daemon...'
# puts 'Stopping daemon...'

# Check if the pid file exists...
unless File.file?(pid_file)
Expand Down
5 changes: 5 additions & 0 deletions lib/landrush/store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ def self.config
@config ||= new(Server.working_dir.join('config.json'))
end

def self.reset
@config = nil
@hosts = nil
end

attr_accessor :backing_file

def initialize(backing_file)
Expand Down
30 changes: 19 additions & 11 deletions test/landrush/server_test.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
require 'test_helper'
require 'resolv'

module Landrush
describe Server do
def query(host)
output = `dig -p #{Server.port} @127.0.0.1 #{host}`
answer_line = output.split("\n").grep(/^#{Regexp.escape(host)}/).first
answer_line.split.last
Resolv::DNS.open(:nameserver_port => [["127.0.0.1", Server.port]]) do |r|
r.getaddress(host).to_s
end
end

def query_ptr(host)
output = `dig ptr -p #{Server.port} @127.0.0.1 #{host}`
answer_line = output.split("\n").grep(/^#{Regexp.escape(host)}/).first
answer_line.split.last
def wait_for_port
sleep 1 until (TCPSocket.open('127.0.0.1', Server.port) rescue nil)
end

describe 'start/stop' do
Expand All @@ -24,11 +23,15 @@ def query_ptr(host)
end

# FIXME: This test requires network access.
# Which is not airplane hacking friendly. >:p
# Which is not airplane hacking friendly. >:p
it 'can be queried for upstream entries' do
skip("needs network, and I am on an airplane without wifi")
# skip("needs network, and I am on an airplane without wifi")
Store.config.set('upstream', [[:udp, '8.8.8.8', 53], [:tcp, '8.8.8.8', 53]])

Server.start

wait_for_port

query("phinze.com").must_match(/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/)
end

Expand All @@ -40,8 +43,9 @@ def query_ptr(host)

Store.hosts.set(fake_host, fake_ip)

wait_for_port

query(fake_host).must_equal fake_ip
query_ptr(fake_host).must_equal fake_ip + '.'
end

it 'responds properly to configured cname entries' do
Expand All @@ -54,7 +58,9 @@ def query_ptr(host)
Store.hosts.set(fake_host, fake_ip)
Store.hosts.set(fake_cname, fake_host)

query(fake_cname).must_equal fake_host + '.'
wait_for_port

query(fake_cname).must_equal fake_ip
end

it 'also resolves wildcard subdomains to a given machine' do
Expand All @@ -65,6 +71,8 @@ def query_ptr(host)

Store.hosts.set(fake_host, fake_ip)

wait_for_port

query("green.#{fake_host}").must_match(fake_ip)
query("blue.#{fake_host}").must_match(fake_ip)
end
Expand Down
5 changes: 3 additions & 2 deletions test/landrush/store_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
module Landrush
describe Store do
before do
@store = Store.new(Tempfile.new(%w[landrush_test_store .json]))
@temp_file = Tempfile.new(%w[landrush_test_store .json])
@store = Store.new(@temp_file)
end

after do
@store.backing_file.unlink
@temp_file.unlink
end

describe "set" do
Expand Down
5 changes: 4 additions & 1 deletion test/support/fake_resolver_config.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
require 'tmpdir'

module FakeResolverConfigHooks
def setup
super
@test_resolver_config_dir = Pathname('/tmp/landrush_fake_resolver')
tempdir = Dir.mktmpdir('landrush_fake_resolver')
@test_resolver_config_dir = Pathname(tempdir)
Landrush::ResolverConfig.config_dir = @test_resolver_config_dir
Landrush::ResolverConfig.sudo = ''
end
Expand Down
3 changes: 2 additions & 1 deletion test/support/fake_working_dir.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
module FakeWorkingDirHooks
def setup
super
Landrush::Server.working_dir = '/tmp/vagrant_landrush_test_working_dir'
tempdir = Dir.mktmpdir('vagrant_landrush_test_working_dir-')
Landrush::Server.working_dir = tempdir
end

def teardown
Expand Down
1 change: 1 addition & 0 deletions test/support/test_server_daemon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def teardown
# Cleanup any stray server instances from tests
if Landrush::Server.running?
Landrush::Server.stop
Landrush::Store.reset
end
end
end
Expand Down

0 comments on commit c498614

Please sign in to comment.