Skip to content
This repository has been archived by the owner on Aug 29, 2018. It is now read-only.

Commit

Permalink
Merge pull request #295 from BanzaiMan/bz903403
Browse files Browse the repository at this point in the history
Bug 903403
  • Loading branch information
danmcp committed Feb 6, 2013
2 parents 1c58a48 + e376890 commit 2a3791d
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 9 deletions.
13 changes: 7 additions & 6 deletions lib/rhc/commands/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -417,18 +417,19 @@ def dns_propagated?(host, sleep_time=2)
Kernel.sleep 5

# Now start checking for DNS
for i in 0..MAX_RETRIES-1
found = host_exists?(host) || hosts_file_contains?(host)
break if found
host_found = hosts_file_contains?(host) or
1.upto(MAX_RETRIES) { |i|
host_found = host_exists?(host)
break found if host_found

say " retry # #{i+1} - Waiting for DNS: #{host}"
say " retry # #{i} - Waiting for DNS: #{host}"
Kernel.sleep sleep_time.to_i
sleep_time *= DEFAULT_DELAY_THROTTLE
end
}

debug "End checking for application dns @ '#{host} - found=#{found}'"

found
host_found
end

def enable_jenkins?
Expand Down
28 changes: 25 additions & 3 deletions lib/rhc/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -449,10 +449,32 @@ def host_exists?(host)
end

def hosts_file_contains?(host)
with_tolerant_encoding do
begin
resolver = Resolv::Hosts.new
resolver.getaddress host
rescue => e
debug "Error while resolving with Resolv::Hosts: #{e.message}(#{e.class})\n #{e.backtrace.join("\n ")}"
end
end
end

def with_tolerant_encoding(&block)
# :nocov:
resolver = Resolv::Hosts.new
resolver.getaddress host
rescue Resolv::ResolvError
if RUBY_VERSION.to_f >= 1.9
orig_default_internal = Encoding.default_internal
Encoding.default_internal = 'ISO-8859-1'
else
orig_default_kcode = $KCODE
$KCODE = 'N'
end
yield
ensure
if RUBY_VERSION.to_f >= 1.9
Encoding.default_internal = orig_default_internal
else
$KCODE = orig_default_kcode
end
# :nocov:
end

Expand Down
13 changes: 13 additions & 0 deletions spec/rhc/commands/app_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
require 'rest_spec_helper'
require 'rhc/commands/app'
require 'rhc/config'
require 'resolv'

describe RHC::Commands::App do
let!(:rest_client){ MockRestClient.new }
Expand Down Expand Up @@ -60,6 +61,18 @@
it { run_output.should match("Cartridges: mock_standalone_cart-1\n") }
end

context 'when Hosts resolver raises an Exception' do
let(:arguments) { ['app', 'create', 'app1', 'mock_standalone_cart-1', '--noprompt', '--timeout', '10', '--config', 'test.conf', '-l', 'test@test.foo', '-p', 'password'] }
before :each do
resolver = Object.new
Resolv::Hosts.should_receive(:new).and_return(resolver)
resolver.should_receive(:getaddress).with('app1-mockdomain.fake.foo').and_raise(ArgumentError)
end

it { expect { run }.should exit_with_code(0) }
it { run_output.should match("Success") }
end

context 'when run with multiple carts' do
let(:arguments) { ['app', 'create', 'app1', 'mock_standalone_cart-1', 'mock_cart-1', '--noprompt', '-p', 'password'] }
it { expect { run }.should exit_with_code(0) }
Expand Down
29 changes: 29 additions & 0 deletions spec/rhc/helpers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
require 'rhc/config'
require 'rhc/helpers'
require 'date'
require 'resolv'

describe RHC::Helpers do
before(:each) do
Expand Down Expand Up @@ -320,6 +321,34 @@ def options
subject.fingerprint_for_local_key('1').should be_nil
end
end

context "Resolv helper" do
let(:resolver) { Object.new }
let(:existent_host) { 'real_host' }
let(:nonexistent_host) { 'fake_host' }

before :all do
Resolv::Hosts.stub(:new) { resolver }
resolver.stub(:getaddress).with(existent_host) { existent_host }
resolver.stub(:getaddress).with(nonexistent_host){ Resolv::ResolvError }
end

context "when hosts file has the desired host" do
it "does not raise error" do
expect {
subject.hosts_file_contains?(existent_host)
}.to_not raise_error
end
end

context "when hosts file does not have the desired host" do
it "does not raise error" do
expect {
subject.hosts_file_contains?(nonexistent_host)
}.to_not raise_error
end
end
end

class OutputTests
include RHC::Helpers
Expand Down

0 comments on commit 2a3791d

Please sign in to comment.