Update mcollective.init according to OSCI-855
[packages/precise/mcollective.git] / spec / unit / facts / base_spec.rb
1 #!/usr/bin/env rspec
2
3 require 'spec_helper'
4
5 module MCollective::Facts
6   describe Base do
7     before do
8       class Testfacts<Base; end
9
10       MCollective::PluginManager.delete("facts_plugin")
11       MCollective::PluginManager << {:type => "facts_plugin", :class => "MCollective::Facts::Testfacts"}
12     end
13
14     describe "#inherited" do
15       it "should add classes to the plugin manager" do
16         MCollective::PluginManager.expects("<<").with({:type => "facts_plugin", :class => "MCollective::Facts::Bar"})
17
18         class Bar<Base; end
19       end
20
21       it "should be available in the PluginManager" do
22         MCollective::PluginManager["facts_plugin"].class.should == MCollective::Facts::Testfacts
23       end
24     end
25
26     describe "#get_fact" do
27       it "should call the fact provider #load_facts_from_source" do
28         Testfacts.any_instance.stubs("load_facts_from_source").returns({"foo" => "bar"}).once
29
30         f = Testfacts.new
31         f.get_fact("foo")
32       end
33
34       it "should honor the cache timeout" do
35         Testfacts.any_instance.stubs("load_facts_from_source").returns({"foo" => "bar"}).once
36
37         f = Testfacts.new
38         f.get_fact("foo")
39         f.get_fact("foo")
40       end
41
42       it "should detect empty facts" do
43         Testfacts.any_instance.stubs("load_facts_from_source").returns({})
44         MCollective::Log.expects("error").with("Failed to load facts: RuntimeError: Got empty facts").once
45
46         f = Testfacts.new
47         f.get_fact("foo")
48       end
49
50       it "should convert non string facts to strings" do
51         Testfacts.any_instance.stubs("load_facts_from_source").returns({:foo => "bar"})
52
53         f = Testfacts.new
54         f.get_fact("foo").should == "bar"
55       end
56
57       it "should not create duplicate facts while converting to strings" do
58         Testfacts.any_instance.stubs("load_facts_from_source").returns({:foo => "bar"})
59
60         f = Testfacts.new
61         f.get_fact(nil).include?(:foo).should == false
62       end
63
64       it "should update last_facts_load on success" do
65         Testfacts.any_instance.stubs("load_facts_from_source").returns({"foo" => "bar"}).once
66
67         f = Testfacts.new
68         f.get_fact("foo")
69
70         f.instance_variable_get("@last_facts_load").should_not == 0
71       end
72
73       it "should restore last known good facts on failure" do
74         Testfacts.any_instance.stubs("load_facts_from_source").returns({}).once
75         MCollective::Log.expects("error").with("Failed to load facts: RuntimeError: Got empty facts").once
76
77         f = Testfacts.new
78         f.instance_variable_set("@last_good_facts", {"foo" => "bar"})
79
80         f.get_fact("foo").should == "bar"
81       end
82
83       it "should return all facts for nil parameter" do
84         Testfacts.any_instance.stubs("load_facts_from_source").returns({"foo" => "bar", "bar" => "baz"})
85
86         f = Testfacts.new
87         f.get_fact(nil).keys.size.should == 2
88       end
89
90       it "should return a specific fact when specified" do
91         Testfacts.any_instance.stubs("load_facts_from_source").returns({"foo" => "bar", "bar" => "baz"})
92
93         f = Testfacts.new
94         f.get_fact("bar").should == "baz"
95       end
96     end
97
98     describe "#get_facts" do
99       it "should load and return all facts" do
100         Testfacts.any_instance.stubs("load_facts_from_source").returns({"foo" => "bar", "bar" => "baz"})
101
102         f = Testfacts.new
103         f.get_facts.should == {"foo" => "bar", "bar" => "baz"}
104       end
105     end
106
107     describe "#has_fact?" do
108       it "should correctly report fact presense" do
109         Testfacts.any_instance.stubs("load_facts_from_source").returns({"foo" => "bar"})
110
111         f = Testfacts.new
112         f.has_fact?("foo").should == true
113         f.has_fact?("bar").should == false
114       end
115     end
116
117   end
118 end