summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Smith <tsmith@chef.io>2018-09-10 10:40:34 -0700
committerGitHub <noreply@github.com>2018-09-10 10:40:34 -0700
commita0551ab20468a49546b763a98e868102079ddd83 (patch)
tree7be416010b682db7ca6c8f6c27b09dcb22fa0f29
parentdb8b538f52a1aa7c3cdabf9dcc6871940f54a937 (diff)
parentd9e430d93bff45b3842bbb455b501f7e148ca52e (diff)
downloadohai-a0551ab20468a49546b763a98e868102079ddd83.tar.gz
Merge pull request #1244 from higanworks/opnstack_timeout_from_Config
override timout by Ohai::Config.ohai[:openstack_metadata_timeout]
-rw-r--r--lib/ohai/plugins/openstack.rb4
-rw-r--r--spec/unit/plugins/openstack_spec.rb26
2 files changed, 25 insertions, 5 deletions
diff --git a/lib/ohai/plugins/openstack.rb b/lib/ohai/plugins/openstack.rb
index 6d34d1a4..e050362b 100644
--- a/lib/ohai/plugins/openstack.rb
+++ b/lib/ohai/plugins/openstack.rb
@@ -64,8 +64,10 @@ Ohai.plugin(:Openstack) do
openstack Mash.new
openstack[:provider] = openstack_provider
+ timeout = Ohai::Config.ohai[:openstack_metadata_timeout] || 2
+
# fetch the metadata if we can do a simple socket connect first
- if can_socket_connect?(Ohai::Mixin::Ec2Metadata::EC2_METADATA_ADDR, 80)
+ if can_socket_connect?(Ohai::Mixin::Ec2Metadata::EC2_METADATA_ADDR, 80, timeout)
fetch_metadata.each do |k, v|
openstack[k] = v
end
diff --git a/spec/unit/plugins/openstack_spec.rb b/spec/unit/plugins/openstack_spec.rb
index 0f5642a4..d9c2df4a 100644
--- a/spec/unit/plugins/openstack_spec.rb
+++ b/spec/unit/plugins/openstack_spec.rb
@@ -21,6 +21,7 @@ require_relative "../../spec_helper.rb"
describe Ohai::System, "plugin openstack" do
let(:plugin) { get_plugin("openstack") }
+ let(:default_timeout) { 2 }
before(:each) do
allow(plugin).to receive(:hint?).with("openstack").and_return(false)
@@ -38,7 +39,7 @@ describe Ohai::System, "plugin openstack" do
context "and the metadata service is not available" do
before do
allow(plugin).to receive(:can_socket_connect?)
- .with(Ohai::Mixin::Ec2Metadata::EC2_METADATA_ADDR, 80)
+ .with(Ohai::Mixin::Ec2Metadata::EC2_METADATA_ADDR, 80, default_timeout)
.and_return(false)
plugin[:dmi] = dmi_data
plugin.run
@@ -77,7 +78,7 @@ describe Ohai::System, "plugin openstack" do
it "sets openstack provider attribute to dreamhost" do
plugin["etc"] = { "passwd" => { "dhc-user" => {} } }
allow(plugin).to receive(:can_socket_connect?)
- .with(Ohai::Mixin::Ec2Metadata::EC2_METADATA_ADDR, 80)
+ .with(Ohai::Mixin::Ec2Metadata::EC2_METADATA_ADDR, 80, default_timeout)
.and_return(false)
plugin[:dmi] = { system: { all_records: [ { Manufacturer: "OpenStack Foundation" } ] } }
plugin.run
@@ -89,7 +90,7 @@ describe Ohai::System, "plugin openstack" do
context "and the metadata service is not available" do
before do
allow(plugin).to receive(:can_socket_connect?)
- .with(Ohai::Mixin::Ec2Metadata::EC2_METADATA_ADDR, 80)
+ .with(Ohai::Mixin::Ec2Metadata::EC2_METADATA_ADDR, 80, default_timeout)
.and_return(false)
allow(plugin).to receive(:hint?).with("openstack").and_return(true)
plugin.run
@@ -102,6 +103,23 @@ describe Ohai::System, "plugin openstack" do
it "doesn't set metadata attributes" do
expect(plugin[:openstack][:instance_id]).to be_nil
end
+ context "when timout was set" do
+ let(:override_timout) { 10 }
+ before do
+ Ohai::Config.ohai[:openstack_metadata_timeout] = override_timout
+ end
+ after do
+ Ohai::Config.ohai[:openstack_metadata_timeout] = nil
+ end
+
+ it "overwrite timout by setting" do
+ allow(plugin).to receive(:can_socket_connect?)
+ .with(Ohai::Mixin::Ec2Metadata::EC2_METADATA_ADDR, 80, override_timout)
+ .and_return(false)
+ allow(plugin).to receive(:hint?).with("openstack").and_return(true)
+ plugin.run
+ end
+ end
end
context "and the metadata service is available" do
@@ -196,7 +214,7 @@ EOM
before do
allow(plugin).to receive(:hint?).with("openstack").and_return(true)
allow(plugin).to receive(:can_socket_connect?)
- .with(Ohai::Mixin::Ec2Metadata::EC2_METADATA_ADDR, 80)
+ .with(Ohai::Mixin::Ec2Metadata::EC2_METADATA_ADDR, 80, default_timeout)
.and_return(true)
allow(Net::HTTP).to receive(:start)