5 module MCollective::Facts
8 class Testfacts<Base; end
10 MCollective::PluginManager.delete("facts_plugin")
11 MCollective::PluginManager << {:type => "facts_plugin", :class => "MCollective::Facts::Testfacts"}
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"})
21 it "should be available in the PluginManager" do
22 MCollective::PluginManager["facts_plugin"].class.should == MCollective::Facts::Testfacts
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
34 it "should honor the cache timeout" do
35 Testfacts.any_instance.stubs("load_facts_from_source").returns({"foo" => "bar"}).once
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
50 it "should convert non string facts to strings" do
51 Testfacts.any_instance.stubs("load_facts_from_source").returns({:foo => "bar"})
54 f.get_fact("foo").should == "bar"
57 it "should not create duplicate facts while converting to strings" do
58 Testfacts.any_instance.stubs("load_facts_from_source").returns({:foo => "bar"})
61 f.get_fact(nil).include?(:foo).should == false
64 it "should update last_facts_load on success" do
65 Testfacts.any_instance.stubs("load_facts_from_source").returns({"foo" => "bar"}).once
70 f.instance_variable_get("@last_facts_load").should_not == 0
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
78 f.instance_variable_set("@last_good_facts", {"foo" => "bar"})
80 f.get_fact("foo").should == "bar"
83 it "should return all facts for nil parameter" do
84 Testfacts.any_instance.stubs("load_facts_from_source").returns({"foo" => "bar", "bar" => "baz"})
87 f.get_fact(nil).keys.size.should == 2
90 it "should return a specific fact when specified" do
91 Testfacts.any_instance.stubs("load_facts_from_source").returns({"foo" => "bar", "bar" => "baz"})
94 f.get_fact("bar").should == "baz"
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"})
103 f.get_facts.should == {"foo" => "bar", "bar" => "baz"}
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"})
112 f.has_fact?("foo").should == true
113 f.has_fact?("bar").should == false