summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStuart Preston <stuart@chef.io>2018-07-04 16:11:37 +0100
committerStuart Preston <stuart@chef.io>2018-07-04 16:11:37 +0100
commit69c7cd09d35cf1986f291f8cf842406fed8a7835 (patch)
tree780fc059505d3308053b433f9e8ad0db67ac456b
parent7d4341c69b3481fb104f525a055ac6ebc521d67e (diff)
downloadohai-69c7cd09d35cf1986f291f8cf842406fed8a7835.tar.gz
Use wmilite instead of shelling out to PowerShell
Signed-off-by: Stuart Preston <stuart@chef.io>
-rw-r--r--lib/ohai/plugins/windows/system_enclosure.rb14
-rw-r--r--spec/unit/plugins/windows/system_enclosure_spec.rb47
2 files changed, 54 insertions, 7 deletions
diff --git a/lib/ohai/plugins/windows/system_enclosure.rb b/lib/ohai/plugins/windows/system_enclosure.rb
index 8d29daae..216c923a 100644
--- a/lib/ohai/plugins/windows/system_enclosure.rb
+++ b/lib/ohai/plugins/windows/system_enclosure.rb
@@ -20,13 +20,13 @@ Ohai.plugin :SystemEnclosure do
provides "system_enclosure"
collect_data(:windows) do
- system_enclosure(Mash.new)
- so = shell_out('powershell.exe -Command "get-ciminstance win32_systemenclosure"')
- if so.exitstatus == 0
- so.stdout.strip.each_line do |line|
- kv = line.split(/:/, 2).map(&:strip)
- system_enclosure[kv[0].downcase] = kv[1] if kv.length == 2
- end
+ require "wmi-lite/wmi"
+ system_enclosure Mash.new
+ wmi = WmiLite::Wmi.new
+ wmi_object = wmi.first_of("Win32_SystemEnclosure").wmi_ole_object
+ wmi_object.properties_.each do |property|
+ value = wmi_object.invoke(property.name)
+ system_enclosure[property.name.downcase] = value unless value.nil?
end
end
end
diff --git a/spec/unit/plugins/windows/system_enclosure_spec.rb b/spec/unit/plugins/windows/system_enclosure_spec.rb
new file mode 100644
index 00000000..8d8327f8
--- /dev/null
+++ b/spec/unit/plugins/windows/system_enclosure_spec.rb
@@ -0,0 +1,47 @@
+#
+# Author:: Pavel Yudin (<pyudin@parallels.com>)
+# Author:: Tim Smith (<tsmith@chef.io>)
+# Copyright:: Copyright (c) 2015 Pavel Yudin
+# Copyright:: Copyright (c) 2015-2016 Chef Software, Inc.
+# 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 CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+require_relative "../../../spec_helper.rb"
+
+describe Ohai::System, "System Enclosure", :windows_only do
+ before do
+ require "wmi-lite/wmi"
+ @plugin = get_plugin("windows/system_enclosure")
+ manufacturer = double("WIN32OLE", :name => "manufacturer", :value => "Microsoft Corporation")
+ serialnumber = double("WIN32OLE", :name => "serialnumber", :value => "000430775257")
+ property_map = [ manufacturer, serialnumber ]
+
+ wmi_ole_object = double( "WIN32OLE", :properties_ => property_map)
+ allow(wmi_ole_object).to receive(:invoke).with(manufacturer.name).and_return(manufacturer.value)
+ allow(wmi_ole_object).to receive(:invoke).with(serialnumber.name).and_return(serialnumber.value)
+ wmi_object = WmiLite::Wmi::Instance.new(wmi_ole_object)
+ expect_any_instance_of(WmiLite::Wmi).to receive(:first_of).with(("Win32_SystemEnclosure")).and_return(wmi_object)
+ end
+
+ it "should return the manufacturer" do
+ @plugin.run
+ expect(@plugin["system_enclosure"]["manufacturer"]).to eql("Microsoft Corporation")
+ end
+
+ it "should return a serial number" do
+ @plugin.run
+ expect(@plugin["system_enclosure"]["serialnumber"]).to eql("000430775257")
+ end
+end