Skip to content

Commit 071b06b

Browse files
mleinartalgorist
authored andcommitted
Added unit tests and fixed the bugs they found
1 parent 6514750 commit 071b06b

4 files changed

Lines changed: 128 additions & 6 deletions

File tree

‎chef/lib/chef/provider/ohai.rb‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,8 @@ def load_current_resource
2828

2929
def action_reload
3030
ohai = ::Ohai::System.new
31-
if not @new_resource.plugin.empty?
32-
@new_resource.plugin.each do |plugin|
33-
ohai.require_plugin plugin
34-
end
31+
if not @new_resource.plugin
32+
ohai.require_plugin @new_resource.plugin
3533
else
3634
ohai.all_plugins
3735
end

‎chef/lib/chef/resource/ohai.rb‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ def initialize(name, run_context=nil)
2525
@resource_name = :ohai
2626
@allowed_actions.push(:reload)
2727
@action = :reload
28-
@plugin = Array.new
28+
@plugin = nil
2929
end
3030

3131
def plugin(arg=nil)
3232
set_or_return(
33-
:plugins,
33+
:plugin,
3434
arg,
3535
:kind_of => [ String ]
3636
)
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#
2+
# Author:: Michael Leinartas (<mleinartas@gmail.com>)
3+
# Copyright:: Copyright (c) 2010 Michael Leinartas
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.join(File.dirname(__FILE__), "..", "..", "spec_helper"))
20+
21+
require 'chef/run_context'
22+
23+
describe Chef::Provider::Ohai do
24+
before(:each) do
25+
# Copied from client_spec
26+
FQDN = "hostname.domainname"
27+
HOSTNAME = "hostname"
28+
PLATFORM = "example-platform"
29+
PLATFORM_VERSION = "example-platform"
30+
Chef::Config[:node_name] = FQDN
31+
mock_ohai = {
32+
:fqdn => FQDN,
33+
:hostname => HOSTNAME,
34+
:platform => PLATFORM,
35+
:platform_version => PLATFORM_VERSION,
36+
:data => {
37+
:origdata => "somevalue"
38+
},
39+
:data2 => {
40+
:origdata => "somevalue",
41+
:newdata => "somevalue"
42+
}
43+
}
44+
mock_ohai.stub!(:all_plugins).and_return(true)
45+
mock_ohai.stub!(:require_plugin).and_return(true)
46+
mock_ohai.stub!(:data).and_return(mock_ohai[:data],
47+
mock_ohai[:data2])
48+
Ohai::System.stub!(:new).and_return(mock_ohai)
49+
Chef::Platform.stub!(:find_platform_and_version).and_return({ "platform" => PLATFORM,
50+
"platform_version" => PLATFORM_VERSION})
51+
# Fake node with a dummy save
52+
@node = Chef::Node.new(HOSTNAME)
53+
@node.name(FQDN)
54+
@node.stub!(:save).and_return(@node)
55+
@run_context = Chef::RunContext.new(@node, {})
56+
@new_resource = Chef::Resource::Ohai.new("ohai_reload")
57+
ohai = Ohai::System.new
58+
ohai.all_plugins
59+
@node.process_external_attrs(ohai.data,{})
60+
61+
@provider = Chef::Provider::Ohai.new(@new_resource, @run_context)
62+
end
63+
64+
describe "when reloading ohai" do
65+
it "should cause node to pick up new values" do
66+
@node[:origdata].should == 'somevalue'
67+
@node[:newdata].should == nil
68+
@provider.action_reload
69+
@node[:origdata].should == 'somevalue'
70+
@node[:newdata].should == 'somevalue'
71+
end
72+
73+
it "should reload a specific plugin and cause node to pick up new values" do
74+
@new_resource.plugin "someplugin"
75+
@provider.action_reload
76+
@node[:origdata].should == 'somevalue'
77+
@node[:newdata].should == 'somevalue'
78+
end
79+
end
80+
end
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#
2+
# Author:: Michael Leinartas (<mleinartas@gmail.com>)
3+
# Copyright:: Copyright (c) 2010 Michael Leinartas
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.join(File.dirname(__FILE__), "..", "..", "spec_helper"))
20+
21+
describe Chef::Resource::Ohai do
22+
23+
before(:each) do
24+
@resource = Chef::Resource::Ohai.new("ohai_reload")
25+
end
26+
27+
it "should create a new Chef::Resource::Ohai" do
28+
@resource.should be_a_kind_of(Chef::Resource)
29+
@resource.should be_a_kind_of(Chef::Resource::Ohai)
30+
end
31+
32+
it "should have a resource name of :ohai" do
33+
@resource.resource_name.should eql(:ohai)
34+
end
35+
36+
it "should have a default action of create" do
37+
@resource.action.should eql(:reload)
38+
end
39+
40+
it "should allow you to set the plugin attribute" do
41+
@resource.plugin "passwd"
42+
@resource.plugin.should eql("passwd")
43+
end
44+
end

0 commit comments

Comments
 (0)