summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Smith <tsmith84@gmail.com>2021-02-10 17:57:13 -0800
committerTim Smith <tsmith84@gmail.com>2021-02-10 17:57:13 -0800
commit15b1c965cec56b69a0179f927230f329a3065b78 (patch)
tree1ec2d40ba94c2201ce4440565dbff7ebcdc13d98
parent9e51fcc5c37bb69827d249b04e38d9c99be49142 (diff)
downloadohai-15b1c965cec56b69a0179f927230f329a3065b78.tar.gz
Add specs for Alibaba metadata fetching
Signed-off-by: Tim Smith <tsmith@chef.io>
-rw-r--r--lib/ohai/mixin/alibaba_metadata.rb2
-rw-r--r--spec/unit/plugins/alibaba_spec.rb89
2 files changed, 90 insertions, 1 deletions
diff --git a/lib/ohai/mixin/alibaba_metadata.rb b/lib/ohai/mixin/alibaba_metadata.rb
index 7afde2f3..983dfd91 100644
--- a/lib/ohai/mixin/alibaba_metadata.rb
+++ b/lib/ohai/mixin/alibaba_metadata.rb
@@ -49,7 +49,7 @@ module Ohai
elsif response.body.include?("\n")
temp = {}
response.body.split("\n").each do |sub_attr|
- temp[sanitize_key(sub_attr)] = fetch_metadata("#{id}#{sub_attr}")
+ temp[sanitize_key(sub_attr)] = fetch_metadata("#{id}/#{sub_attr}")
end
temp
else
diff --git a/spec/unit/plugins/alibaba_spec.rb b/spec/unit/plugins/alibaba_spec.rb
new file mode 100644
index 00000000..c77e9899
--- /dev/null
+++ b/spec/unit/plugins/alibaba_spec.rb
@@ -0,0 +1,89 @@
+#
+# Author:: Ranjib Dey (dey.ranjib@gmail.com)
+# Author:: Tim Smith (tsmith@chef.io)
+# License:: Apache License, Version 2.0
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDIT"Net::HTTP Response"NS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+require "spec_helper"
+require "open-uri"
+
+describe Ohai::System, "plugin alibaba" do
+ let(:plugin) { get_plugin("alibaba") }
+
+ before do
+ allow(plugin).to receive(:hint?).with("alibaba").and_return(false)
+ allow(plugin).to receive(:file_exist?).with("/sys/class/dmi/id/sys_vendor").and_return(false)
+ end
+
+ shared_examples_for "!alibaba" do
+ it "does NOT attempt to fetch the alibaba metadata" do
+ expect(plugin).not_to receive(:http_get)
+ plugin.run
+ end
+
+ it "does NOT set alibaba attributes" do
+ expect(plugin[:alibaba]).to be_nil
+ plugin.run
+ end
+ end
+
+ shared_examples_for "alibaba" do
+ before do
+ @http_get = double("Net::HTTP client")
+ allow(plugin).to receive(:http_get).with("").and_return(double("Net::HTTP Response", body: "meta-data\n", code: "200"))
+ allow(plugin).to receive(:http_get).with("/meta-data").and_return(double("Net::HTTP Response", body: "hostname\n", code: "200"))
+ allow(plugin).to receive(:http_get).with("/meta-data/hostname").and_return(double("Net::HTTP Response", body: "foo", code: "200"))
+ allow(IO).to receive(:select).and_return([[], [1], []])
+ t = double("connection")
+ allow(t).to receive(:connect_nonblock).and_raise(Errno::EINPROGRESS)
+ allow(Socket).to receive(:new).and_return(t)
+ allow(Socket).to receive(:pack_sockaddr_in).and_return(nil)
+ end
+
+ it "recursively fetches and properly parses json metadata" do
+ plugin.run
+
+ expect(plugin[:alibaba]).not_to be_nil
+ expect(plugin[:alibaba]["meta_data"]).to eq("hostname" => "foo")
+ end
+
+ end
+
+ describe "with hint file and with metadata connection" do
+ before do
+ allow(plugin).to receive(:hint?).with("alibaba").and_return({})
+ end
+
+ it_behaves_like "alibaba"
+ end
+
+ describe "with alibaba dmi sys_vendor data" do
+ before do
+ allow(plugin).to receive(:file_exist?).with("/sys/class/dmi/id/sys_vendor").and_return(true)
+ allow(plugin).to receive(:file_read).with("/sys/class/dmi/id/sys_vendor").and_return("Alibaba Cloud\n")
+ end
+
+ it_behaves_like "alibaba"
+ end
+
+ describe "without hint file and non-alibaba dmi sys_vendor data" do
+ before do
+ allow(plugin).to receive(:file_exist?).with("/sys/class/dmi/id/sys_vendor").and_return(true)
+ allow(plugin).to receive(:file_read).with("/sys/class/dmi/id/sys_vendor").and_return("TimCloud\n")
+ end
+
+ it_behaves_like "!alibaba"
+ end
+end