Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix GitHub Issue #432
Fixes GitHub issue #432: cloud_v2 fails to initialize on GCE hosts
without external IP

Resolves a special-case issue where an empty public IP address is not
handled correctly, causing cloud_v2 to fail to initialize when a GCE
instance does not have a public IP. Spec included.

Obvious fix, though I think I signed the CLA last year anyway.
  • Loading branch information
Jeff Goldschrafe authored and btm committed Feb 17, 2015
1 parent 3376e7f commit 1cc9756
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 26 deletions.
2 changes: 1 addition & 1 deletion lib/ohai/plugins/cloud_v2.rb
Expand Up @@ -125,7 +125,7 @@ def on_gce?
def get_gce_values
public_ips = gce['instance']['networkInterfaces'].collect do |interface|
if interface.has_key?('accessConfigs')
interface['accessConfigs'].collect{|ac| ac['externalIp']}
interface['accessConfigs'].collect{|ac| ac['externalIp'] unless ac['externalIp'] == ''}
end
end.flatten.compact

Expand Down
81 changes: 56 additions & 25 deletions spec/unit/plugins/cloud_v2_spec.rb
Expand Up @@ -116,31 +116,62 @@
end

describe "with GCE mash" do
before do
@plugin[:gce] = Mash.new()
@plugin[:gce]['instance'] = Mash.new()
@plugin[:gce]['instance']['networkInterfaces'] = [
{
"accessConfigs" => [ {"externalIp" => "8.35.198.173", "type"=>"ONE_TO_ONE_NAT"} ],
"ip" => "10.240.0.102",
"network"=> "projects/foo/networks/default"
}
]
end

it "populates cloud public ip" do
@plugin.run
expect(@plugin[:cloud_v2][:public_ipv4_addrs][0]).to eq("8.35.198.173")
end

it "populates cloud private ip" do
@plugin.run
expect(@plugin[:cloud_v2][:local_ipv4_addrs][0]).to eq("10.240.0.102")
end

it "populates cloud provider" do
@plugin.run
expect(@plugin[:cloud_v2][:provider]).to eq("gce")
describe "with a public IP" do
before do
@plugin[:gce] = Mash.new()
@plugin[:gce]['instance'] = Mash.new()
@plugin[:gce]['instance']['networkInterfaces'] = [
{
"accessConfigs" => [ {"externalIp" => "8.35.198.173", "type"=>"ONE_TO_ONE_NAT"} ],
"ip" => "10.240.0.102",
"network"=> "projects/foo/networks/default"
}
]
end

it "populates cloud public ip" do
@plugin.run
expect(@plugin[:cloud_v2][:public_ipv4_addrs][0]).to eq("8.35.198.173")
end

it "populates cloud private ip" do
@plugin.run
expect(@plugin[:cloud_v2][:local_ipv4_addrs][0]).to eq("10.240.0.102")
end

it "populates cloud provider" do
@plugin.run
expect(@plugin[:cloud_v2][:provider]).to eq("gce")
end
end

describe "with no public IP" do
before do
@plugin[:gce] = Mash.new()
@plugin[:gce]['instance'] = Mash.new()
@plugin[:gce]['instance']['networkInterfaces'] = [
{
"accessConfigs" => [ {"externalIp" => "", "type" => "ONE_TO_ONE_NAT"} ],
"ip" => "10.240.0.102",
"network" => "projects/foo/networks/default"
}
]
end

it "does not populate cloud public ip" do
@plugin.run
expect(@plugin[:cloud_v2][:public_ipv4_addrs]).to be_nil
end

it "populates cloud private ip" do
@plugin.run
expect(@plugin[:cloud_v2][:local_ipv4_addrs][0]).to eq("10.240.0.102")
end

it "populates cloud provider" do
@plugin.run
expect(@plugin[:cloud_v2][:provider]).to eq("gce")
end
end
end

Expand Down

0 comments on commit 1cc9756

Please sign in to comment.