diff options
author | sawanoboly <sawanoboriyu@higanworks.com> | 2018-09-10 13:53:08 +0900 |
---|---|---|
committer | sawanoboly <sawanoboriyu@higanworks.com> | 2018-09-10 13:53:08 +0900 |
commit | 047e187fe6434bc5a2d683e844f5d7455ef319e1 (patch) | |
tree | 30b5f246742cb18117fee83ff7b0b554723ef349 | |
parent | 68db80ab4b8f21fca6d3a2121ced524ac9f09167 (diff) | |
download | ohai-047e187fe6434bc5a2d683e844f5d7455ef319e1.tar.gz |
override timout by Ohai::Config.ohai[:openstack_metadata_timeout]
Signed-off-by: sawanoboly <sawanoboriyu@higanworks.com>
-rw-r--r-- | lib/ohai/plugins/openstack.rb | 4 | ||||
-rw-r--r-- | spec/unit/plugins/openstack_spec.rb | 26 |
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..f6b2acc4 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) |