ohai 7.2.0.alpha.0 → 7.2.0.rc.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -18,13 +18,13 @@
18
18
  Ohai.plugin(:RootGroup) do
19
19
  provides 'root_group'
20
20
 
21
+ require 'ohai/util/win32/group_helper' if RUBY_PLATFORM =~ /mswin|mingw32|windows/
22
+
21
23
  collect_data do
22
24
  case ::RbConfig::CONFIG['host_os']
23
25
  when /mswin|mingw32|windows/
24
- # TODO: OHAI-491
25
- # http://tickets.opscode.com/browse/OHAI-491
26
- # The windows implementation of this plugin has been removed because of
27
- # performance considerations (see: OHAI-490).
26
+ group = Ohai::Util::Win32::GroupHelper.windows_root_group_name
27
+ root_group group
28
28
  else
29
29
  root_group Etc.getgrgid(Etc.getpwnam('root').gid).name
30
30
  end
@@ -21,6 +21,12 @@
21
21
  Ohai.plugin(:Virtualization) do
22
22
  provides "virtualization"
23
23
 
24
+ def collect_solaris_guestid
25
+ command = '/usr/sbin/zoneadm list -p'
26
+ so = shell_out(command)
27
+ so.stdout.split(':').first
28
+ end
29
+
24
30
  collect_data(:solaris2) do
25
31
  virtualization Mash.new
26
32
 
@@ -75,6 +81,7 @@ Ohai.plugin(:Virtualization) do
75
81
  virtualization[:system] = 'zone'
76
82
  virtualization[:role] = 'guest'
77
83
  virtualization[:guest_uuid] = zones[first_zone]['uuid']
84
+ virtualization[:guest_id] = collect_solaris_guestid
78
85
  end
79
86
  elsif (zones.length > 1)
80
87
  virtualization[:system] = 'zone'
@@ -29,7 +29,7 @@ require 'ohai/provides_map'
29
29
  require 'ohai/hints'
30
30
  require 'mixlib/shellout'
31
31
 
32
- require 'yajl'
32
+ require 'ffi_yajl'
33
33
 
34
34
  module Ohai
35
35
  class System
@@ -173,14 +173,14 @@ module Ohai
173
173
  # Serialize this object as a hash
174
174
  #
175
175
  def to_json
176
- Yajl::Encoder.new.encode(@data)
176
+ FFI_Yajl::Encoder.new.encode(@data)
177
177
  end
178
178
 
179
179
  #
180
180
  # Pretty Print this object as JSON
181
181
  #
182
182
  def json_pretty_print(item=nil)
183
- Yajl::Encoder.new(:pretty => true).encode(item || @data)
183
+ FFI_Yajl::Encoder.new(:pretty => true).encode(item || @data)
184
184
  end
185
185
 
186
186
  def attributes_print(a)
@@ -0,0 +1,46 @@
1
+ # Author:: Adam Edwards (<adamed@getchef.com>)
2
+ #
3
+ # Copyright:: Copyright (c) 2013-14 Chef Software, Inc.
4
+ #
5
+ # License:: Apache License, Version 2.0
6
+ #
7
+ # Licensed under the Apache License, Version 2.0 (the "License");
8
+ # you may not use this file except in compliance with the License.
9
+ # You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing, software
14
+ # distributed under the License is distributed on an "AS IS" BASIS,
15
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ # See the License for the specific language governing permissions and
17
+ # limitations under the License.
18
+
19
+ module Ohai
20
+ module Util
21
+ module Win32
22
+ if RUBY_PLATFORM =~ /mswin|mingw|windows/
23
+
24
+ require 'ffi'
25
+
26
+ extend FFI::Library
27
+
28
+ ffi_lib 'advapi32'
29
+
30
+ attach_function :lookup_account_sid,
31
+ :LookupAccountSidA,[ :pointer, :pointer, :pointer, :pointer, :pointer, :pointer, :pointer ], :long
32
+
33
+ attach_function :convert_string_sid_to_sid,
34
+ :ConvertStringSidToSidA,[ :pointer, :pointer ], :long
35
+
36
+ ffi_lib 'kernel32'
37
+
38
+ attach_function :local_free,
39
+ :LocalFree, [ :pointer ], :long
40
+
41
+ attach_function :get_last_error,
42
+ :GetLastError, [], :long
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,76 @@
1
+ # Author:: Adam Edwards (<adamed@getchef.com>)
2
+ #
3
+ # Copyright:: Copyright (c) 2013-14 Chef Software, Inc.
4
+ #
5
+ # License:: Apache License, Version 2.0
6
+ #
7
+ # Licensed under the Apache License, Version 2.0 (the "License");
8
+ # you may not use this file except in compliance with the License.
9
+ # You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing, software
14
+ # distributed under the License is distributed on an "AS IS" BASIS,
15
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ # See the License for the specific language governing permissions and
17
+ # limitations under the License.
18
+
19
+ require 'ohai/util/win32'
20
+
21
+ module Ohai
22
+ module Util
23
+ class Win32::GroupHelper
24
+
25
+ # Per http://support.microsoft.com/kb/243330 SID: S-1-5-32-544 is the
26
+ # internal name for the Administrators group, which lets us work
27
+ # properly in environments with a renamed or localized name for the
28
+ # Administrators group
29
+ BUILTIN_ADMINISTRATORS_SID = 'S-1-5-32-544'
30
+
31
+ def self.windows_root_group_name
32
+ administrators_group_name_result = nil
33
+
34
+ administrators_sid_result = FFI::MemoryPointer.new(:pointer)
35
+ convert_result = Win32.convert_string_sid_to_sid(BUILTIN_ADMINISTRATORS_SID, administrators_sid_result)
36
+ last_win32_error = Win32.get_last_error
37
+
38
+ if convert_result == 0
39
+ raise "ERROR: failed to to convert sid string '#{BUILTIN_ADMINISTRATORS_SID}' to a Windows SID structure because Win32 API function ConvertStringSidToSid returned #{last_win32_error}."
40
+ end
41
+
42
+ administrators_group_name_buffer = 0.chr * 260
43
+ administrators_group_name_length = [administrators_group_name_buffer.length].pack('L')
44
+ domain_name_length_buffer = [260].pack('L')
45
+ sid_use_result = 0.chr * 4
46
+
47
+ # Use LookupAccountSid rather than WMI's Win32_Group class because WMI will attempt
48
+ # to include (unneeded) Active Directory groups by querying AD, which is a performance
49
+ # and reliability issue since AD might not be reachable. Additionally, in domains with
50
+ # thousands of groups, the WMI query is very slow, on the order of minutes, even to
51
+ # get the first result. So we use LookupAccountSid which is a purely local lookup
52
+ # of the built-in group, with no need to access AD, and thus no failure modes related
53
+ # to network conditions or query performance.
54
+ lookup_boolean_result = Win32.lookup_account_sid(
55
+ nil,
56
+ administrators_sid_result.read_pointer,
57
+ administrators_group_name_buffer,
58
+ administrators_group_name_length,
59
+ nil,
60
+ domain_name_length_buffer,
61
+ sid_use_result)
62
+
63
+ last_win32_error = Win32.get_last_error
64
+
65
+ Win32.local_free(administrators_sid_result.read_pointer)
66
+
67
+ if lookup_boolean_result == 0
68
+ raise "ERROR: failed to find root group (i.e. builtin\\administrators) for sid #{BUILTIN_ADMINISTRATORS_SID} because Win32 API function LookupAccountSid returned #{last_win32_error}."
69
+ end
70
+
71
+ administrators_group_name_buffer.strip
72
+ end
73
+ end
74
+ end
75
+ end
76
+
@@ -18,5 +18,5 @@
18
18
 
19
19
  module Ohai
20
20
  OHAI_ROOT = File.expand_path(File.dirname(__FILE__))
21
- VERSION = '7.2.0.alpha.0'
21
+ VERSION = '7.2.0.rc.0'
22
22
  end
@@ -0,0 +1,41 @@
1
+ #
2
+ # Author:: Adam Edwards (<adamed@getchef.com>)
3
+ #
4
+ # Copyright:: Copyright (c) 2014 Chef Software, Inc.
5
+ #
6
+ # License:: Apache License, Version 2.0
7
+ #
8
+ # Licensed under the Apache License, Version 2.0 (the "License");
9
+ # you may not use this file except in compliance with the License.
10
+ # You may obtain a copy of the License at
11
+ #
12
+ # http://www.apache.org/licenses/LICENSE-2.0
13
+ #
14
+ # Unless required by applicable law or agreed to in writing, software
15
+ # distributed under the License is distributed on an "AS IS" BASIS,
16
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
+ # See the License for the specific language governing permissions and
18
+ # limitations under the License.
19
+ #
20
+
21
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper.rb')
22
+
23
+ describe Ohai::System, 'root_group plugin' do
24
+ let(:plugin) { get_plugin("root_group") }
25
+
26
+ describe 'windows platform', :windows_only do
27
+ let (:wmi) { wmi = WmiLite::Wmi.new }
28
+
29
+ it 'should return the system\'s administrators (root) group' do
30
+ # Notethat the Win32_Group WMI provider can be slow if your
31
+ # system is domain-joined and has hundreds of thousands of
32
+ # groups in Active Directory -- not a typical test scenario, but
33
+ # something to watch if you run this test in such an environment.
34
+ groups = wmi.query("select * from Win32_Group where sid = 'S-1-5-32-544'")
35
+ groups.length.should == 1
36
+ administrators_group = groups[0]['name'].downcase
37
+ plugin.run
38
+ expect(plugin[:root_group].downcase).to be == administrators_group
39
+ end
40
+ end
41
+ end
@@ -1,4 +1,5 @@
1
1
  require 'rspec'
2
+ require 'rspec/collection_matchers'
2
3
 
3
4
  # require 'pry-debugger'
4
5
  # require 'pry-stack_explorer'
@@ -8,6 +9,7 @@ $:.unshift(File.dirname(__FILE__) + '/../lib')
8
9
 
9
10
  require 'spec/support/platform_helpers'
10
11
  require 'spec/support/integration_helper'
12
+ require 'wmi-lite'
11
13
  require 'ohai'
12
14
  Ohai::Config[:log_level] = :error
13
15
 
@@ -84,7 +86,17 @@ def it_should_check_from_deep_mash(plugin, mash, attribute, from, value)
84
86
  end
85
87
 
86
88
  RSpec.configure do |config|
87
- config.treat_symbols_as_metadata_keys_with_true_values = true
89
+
90
+ config.raise_errors_for_deprecations!
91
+
92
+ # `expect` should be preferred for new tests or when refactoring old tests,
93
+ # but we're not going to do a "big bang" change at this time.
94
+ config.expect_with :rspec do |c|
95
+ c.syntax = [:should, :expect]
96
+ end
97
+ config.mock_with :rspec do |c|
98
+ c.syntax = [:expect, :should]
99
+ end
88
100
 
89
101
  config.filter_run :focus => true
90
102
 
@@ -22,7 +22,7 @@ require File.expand_path("../../../spec_helper.rb", __FILE__)
22
22
  shared_examples "Ohai::DSL::Plugin" do
23
23
  context "#initialize" do
24
24
  it "should set has_run? to false" do
25
- plugin.has_run?.should be_false
25
+ plugin.has_run?.should be false
26
26
  end
27
27
 
28
28
  it "should set the correct plugin version" do
@@ -48,7 +48,7 @@ shared_examples "Ohai::DSL::Plugin" do
48
48
 
49
49
  it "should set has_run? to true" do
50
50
  plugin.run
51
- plugin.has_run?.should be_true
51
+ plugin.has_run?.should be true
52
52
  end
53
53
  end
54
54
 
@@ -69,7 +69,7 @@ shared_examples "Ohai::DSL::Plugin" do
69
69
 
70
70
  it "should set has_run? to true" do
71
71
  plugin.run
72
- plugin.has_run?.should be_true
72
+ plugin.has_run?.should be true
73
73
  end
74
74
  end
75
75
  end
@@ -0,0 +1,64 @@
1
+ #
2
+ # Author:: Patrick Collins (<pat@burned.com>)
3
+ # Copyright:: Copyright (c) 2013 Opscode, Inc.
4
+ # License:: Apache License, Version 2.0
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+
19
+ require File.expand_path(File.dirname(__FILE__) + '/../../../spec_helper.rb')
20
+
21
+ describe Ohai::System, "Darwin Memory Plugin" do
22
+ before(:each) do
23
+ darwin_memsize = <<-DARWIN_MEMSIZE
24
+ 17179869184
25
+ DARWIN_MEMSIZE
26
+ darwin_vm_stat = <<-DARWIN_VM_STAT
27
+ Mach Virtual Memory Statistics: (page size of 4096 bytes)
28
+ Pages free: 2155305.
29
+ Pages active: 924164.
30
+ Pages inactive: 189127.
31
+ Pages speculative: 531321.
32
+ Pages wired down: 391749.
33
+ "Translation faults": 14107520.
34
+ Pages copy-on-write: 810071.
35
+ Pages zero filled: 6981505.
36
+ Pages reactivated: 1397.
37
+ Pageins: 630064.
38
+ Pageouts: 0.
39
+ Object cache: 12 hits of 139872 lookups (0% hit rate)
40
+ DARWIN_VM_STAT
41
+
42
+ @plugin = get_plugin("darwin/memory")
43
+ @plugin.stub(:collect_os).and_return(:darwin)
44
+ @plugin.stub(:shell_out).with("sysctl -n hw.memsize").and_return(mock_shell_out(0, darwin_memsize, ""))
45
+ @plugin.stub(:shell_out).with("vm_stat").and_return(mock_shell_out(0, darwin_vm_stat, ""))
46
+ @plugin.run
47
+ end
48
+
49
+ it "should set memory[:total] to 16384MB" do
50
+ @plugin[:memory][:total].should == '16384MB'
51
+ end
52
+
53
+ it "should set memory[:active] to 5140MB" do
54
+ @plugin[:memory][:active].should == '5140MB'
55
+ end
56
+
57
+ it "should set memory[:inactive] to 738MB" do
58
+ @plugin[:memory][:inactive].should == '738MB'
59
+ end
60
+
61
+ it "should set memory[:free] to 10504MB" do
62
+ @plugin[:memory][:free].should == '10504MB'
63
+ end
64
+ end
@@ -12,7 +12,7 @@
12
12
  #
13
13
  # Unless required by applicable law or agreed to in writing, software
14
14
  # distributed under the License is distributed on an "AS IS" BASIS,
15
- # WITHOUT WARRANTIES OR CONDIT"Net::HTTP Response"NS OF ANY KIND, either express or implied.
15
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
16
  # See the License for the specific language governing permissions and
17
17
  # limitations under the License.
18
18
  #
@@ -157,6 +157,27 @@ describe Ohai::System, "plugin ec2" do
157
157
 
158
158
  @plugin[:ec2].should_not be_nil
159
159
  end
160
+
161
+ it "should complete the run despite unavailable metadata" do
162
+ @http_client.should_receive(:get).
163
+ with("/2012-01-12/meta-data/").
164
+ and_return(double("Net::HTTP Response", :body => "metrics/", :code => "200"))
165
+ @http_client.should_receive(:get).
166
+ with("/2012-01-12/meta-data/metrics/").
167
+ and_return(double("Net::HTTP Response", :body => "vhostmd", :code => "200"))
168
+ @http_client.should_receive(:get).
169
+ with("/2012-01-12/meta-data/metrics/vhostmd").
170
+ and_return(double("Net::HTTP Response", :body => "", :code => "404"))
171
+ @http_client.should_receive(:get).
172
+ with("/2012-01-12/user-data/").
173
+ and_return(double("Net::HTTP Response", :body => "By the pricking of my thumb...", :code => "200"))
174
+
175
+ @plugin.run
176
+
177
+ @plugin[:ec2].should_not be_nil
178
+ @plugin[:ec2]['metrics'].should be_nil
179
+ @plugin[:ec2]['metrics_vhostmd'].should be_nil
180
+ end
160
181
  end
161
182
 
162
183
  describe "with ec2 mac and metadata address connected" do
@@ -72,7 +72,7 @@ describe Ohai::System, "plugin gce" do
72
72
  File.stub(:exist?).with('/etc/chef/ohai/hints/gce.json').and_return(false)
73
73
  File.stub(:exist?).with('C:\chef\ohai\hints/gce.json').and_return(false)
74
74
 
75
- # Raise Errno::ENOENT to simulate the scenario in which metadata server
75
+ # Raise Errno::ENOENT to simulate the scenario in which metadata server
76
76
  # can not be connected
77
77
  t = double("connection")
78
78
  t.stub(:connect_nonblock).and_raise(Errno::ENOENT)
@@ -0,0 +1,74 @@
1
+ require 'spec_helper'
2
+
3
+
4
+ describe Ohai::System, "plugin joyent" do
5
+ before(:each) do
6
+ @plugin = get_plugin('joyent')
7
+ end
8
+
9
+ describe "without joyent" do
10
+ before(:each) do
11
+ @plugin.stub(:is_smartos?).and_return(false)
12
+ end
13
+
14
+ it "should NOT create joyent" do
15
+ @plugin.run
16
+ @plugin[:joyent].should be_nil
17
+ end
18
+ end
19
+
20
+ describe "with joyent" do
21
+ before(:each) do
22
+ @plugin.stub(:is_smartos?).and_return(true)
23
+ @plugin[:virtualization] = Mash.new
24
+ @plugin[:virtualization][:guest_uuid] = "global"
25
+ end
26
+
27
+ it "should create joyent" do
28
+ @plugin.run
29
+ @plugin[:joyent].should_not be_nil
30
+ end
31
+
32
+ describe "under global zone" do
33
+ before(:each) do
34
+ @plugin.run
35
+ end
36
+
37
+ it "should ditect global zone" do
38
+ @plugin[:joyent][:sm_uuid].should eql 'global'
39
+ end
40
+
41
+ it "should NOT create sm_id" do
42
+ @plugin[:joyent][:sm_id].should be_nil
43
+ end
44
+ end
45
+
46
+ describe "under smartmachine" do
47
+ before(:each) do
48
+ @plugin[:virtualization][:guest_uuid] = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxx'
49
+ @plugin[:virtualization][:guest_id] = '30'
50
+ @plugin.stub(:collect_product_file).and_return(["Name: Joyent Instance", "Image: base64 13.4.2", "Documentation: http://wiki.joyent.com/jpc2/SmartMachine+Base"])
51
+ @plugin.stub(:collect_pkgsrc).and_return('http://pkgsrc.joyent.com/packages/SmartOS/2013Q4/x86_64/All')
52
+ @plugin.run
53
+ end
54
+
55
+ it "should retrive zone uuid" do
56
+ @plugin[:joyent][:sm_uuid].should eql 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxx'
57
+ end
58
+
59
+ it "should collect sm_id" do
60
+ @plugin[:joyent][:sm_id].should eql '30'
61
+ end
62
+
63
+ it "should collect images" do
64
+ @plugin[:joyent][:sm_image_id].should_not be_nil
65
+ @plugin[:joyent][:sm_image_ver].should_not be_nil
66
+ end
67
+
68
+ it "should collect pkgsrc" do
69
+ @plugin[:joyent][:sm_pkgsrc].should eql 'http://pkgsrc.joyent.com/packages/SmartOS/2013Q4/x86_64/All'
70
+ end
71
+ end
72
+ end
73
+ end
74
+