diff options
author | Jay Mundrawala <jdmundrawala@gmail.com> | 2014-08-14 12:51:22 -0700 |
---|---|---|
committer | Jay Mundrawala <jdmundrawala@gmail.com> | 2014-08-14 12:51:22 -0700 |
commit | f4ac76fe2103a060ab0a773e6d0450249ac8fdae (patch) | |
tree | 373e611a901fa0bc90bbb5cc84847293f9b0e8b7 | |
parent | b1f8060ae763d9c8e2304522a94cda292274fa5e (diff) | |
parent | b0f82304d6247cdbea0b91b94ea8d0f4cd7510af (diff) | |
download | ohai-f4ac76fe2103a060ab0a773e6d0450249ac8fdae.tar.gz |
Merge pull request #378 from opscode/jdmundrawala/powershell
Ohai Powershell plugin
-rw-r--r-- | lib/ohai/plugins/powershell.rb | 62 | ||||
-rw-r--r-- | spec/functional/plugins/powershell_spec.rb | 82 | ||||
-rw-r--r-- | spec/unit/plugins/powershell_spec.rb | 68 |
3 files changed, 212 insertions, 0 deletions
diff --git a/lib/ohai/plugins/powershell.rb b/lib/ohai/plugins/powershell.rb new file mode 100644 index 00000000..9c67e0f6 --- /dev/null +++ b/lib/ohai/plugins/powershell.rb @@ -0,0 +1,62 @@ +# +# Copyright:: Copyright (c) 2014 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. +# + +Ohai.plugin(:Powershell) do + provides "languages/powershell" + depends "languages" + + collect_data(:windows) do + powershell = Mash.new + so = shell_out("powershell.exe -NoLogo -NonInteractive -NoProfile -command $PSVersionTable") + # Sample output: + # + # Name Value + # ---- ----- + # PSVersion 4.0 + # WSManStackVersion 3.0 + # SerializationVersion 1.1.0.1 + # CLRVersion 4.0.30319.34014 + # BuildVersion 6.3.9600.16394 + # PSCompatibleVersions {1.0, 2.0, 3.0, 4.0} + # PSRemotingProtocolVersion 2.2 + + if so.exitstatus == 0 + version_info = {} + so.stdout.strip.each_line do |line| + kv = line.strip.split(/\s+/, 2) + version_info[kv[0]] = kv[1] if kv.length == 2 + end + powershell[:version] = version_info['PSVersion'] + powershell[:ws_man_stack_version] = version_info['WSManStackVersion'] + powershell[:serialization_version] = version_info['SerializationVersion'] + powershell[:clr_version] = version_info['CLRVersion'] + powershell[:build_version] = version_info['BuildVersion'] + powershell[:compatible_versions] = parse_compatible_versions(version_info['PSCompatibleVersions']) + powershell[:remoting_protocol_version] = version_info['PSRemotingProtocolVersion'] + languages[:powershell] = powershell if powershell[:version] + end + end + + def parse_compatible_versions(versions_str) + if versions_str + if versions_str.strip.start_with?('{') && versions_str.end_with?('}') + versions = versions_str.gsub(/[{}\s]+/,'').split(',') + versions if versions.length + end + end + end +end diff --git a/spec/functional/plugins/powershell_spec.rb b/spec/functional/plugins/powershell_spec.rb new file mode 100644 index 00000000..ec435923 --- /dev/null +++ b/spec/functional/plugins/powershell_spec.rb @@ -0,0 +1,82 @@ +# +# Copyright:: Copyright (c) 2014 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 File.expand_path(File.dirname(__FILE__) + '/../../spec_helper.rb') + +describe Ohai::System, 'languages plugin' do + VERSION_MATCHING_REGEX = /^(?:[\d]+\.)+[\d]+$/ + describe 'powershell plugin', :windows_only do + RSpec.shared_examples "a version looking thing" do + it 'should be present' do + subject.should_not be_nil + end + it 'should look like a version' do + subject.should match(VERSION_MATCHING_REGEX) + end + end + before(:all) do + @plugin = get_plugin("powershell") + @plugin[:languages] = Mash.new + @plugin.run + end + + subject { @plugin[:languages][:powershell] } + + it 'should have information about powershell' do + subject.should_not be_nil + end + + describe :version do + subject { @plugin.languages[:powershell][described_class] } + it_behaves_like 'a version looking thing' + end + + describe :ws_man_stack_version do + subject { @plugin.languages[:powershell][described_class] } + it_behaves_like 'a version looking thing' + end + + describe :serialization_version do + subject { @plugin.languages[:powershell][described_class] } + it_behaves_like 'a version looking thing' + end + + describe :clr_version do + subject { @plugin.languages[:powershell][described_class] } + it_behaves_like 'a version looking thing' + end + + describe :build_version do + subject { @plugin.languages[:powershell][described_class] } + it_behaves_like 'a version looking thing' + end + + describe :remoting_protocol_version do + subject { @plugin.languages[:powershell][described_class] } + it_behaves_like 'a version looking thing' + end + + describe :compatible_versions do + it 'has compatible_versions that look like versions' do + @plugin.languages[:powershell][described_class].each do |version| + version.should match(VERSION_MATCHING_REGEX) + end + end + end + end +end diff --git a/spec/unit/plugins/powershell_spec.rb b/spec/unit/plugins/powershell_spec.rb new file mode 100644 index 00000000..619f1908 --- /dev/null +++ b/spec/unit/plugins/powershell_spec.rb @@ -0,0 +1,68 @@ +# +# Copyright:: Copyright (c) 2014 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 File.expand_path(File.join(File.dirname(__FILE__), '..', '..', '/spec_helper.rb')) + +describe Ohai::System, "plugin powershell" do + before do + stub_const('::RbConfig::CONFIG', {'host_os' => 'windows'}) + end + + before(:each) do + @plugin = get_plugin("powershell") + @plugin[:languages] = Mash.new + end + + it "should set languages[:powershell][:version] for v4" do + + v4_output = <<END + +Name Value +---- ----- +PSVersion 4.0 +WSManStackVersion 3.0 +SerializationVersion 1.1.0.1 +CLRVersion 4.0.30319.34014 +BuildVersion 6.3.9600.16394 +PSCompatibleVersions {1.0, 2.0, 3.0, 4.0} +PSRemotingProtocolVersion 2.2 + +END + + @plugin.stub(:shell_out).with(anything()).and_return(mock_shell_out(0, v4_output, "")) + @plugin.run + @plugin.languages[:powershell][:version].should eql("4.0") + @plugin.languages[:powershell][:ws_man_stack_version].should eql("3.0") + @plugin.languages[:powershell][:serialization_version].should eql("1.1.0.1") + @plugin.languages[:powershell][:clr_version].should eql("4.0.30319.34014") + @plugin.languages[:powershell][:build_version].should eql("6.3.9600.16394") + @plugin.languages[:powershell][:compatible_versions].should eql(['1.0', '2.0', '3.0', '4.0']) + @plugin.languages[:powershell][:remoting_protocol_version].should eql("2.2") + end + + it "should not set the languages[:powershell] tree up if powershell command fails" do + error_output = <<END +'powershell.exe' is not recognized as an internal or external command, +operable program or batch file. +END + + @plugin.stub(:shell_out).with(anything).and_return(mock_shell_out(1, error_output, "")) + @plugin.run + @plugin.languages.should_not have_key(:powershell) + end + +end |