X-Git-Url: https://review.fuel-infra.org/gitweb?a=blobdiff_plain;f=spec%2Funit%2Ffacts%2Fbase_spec.rb;fp=spec%2Funit%2Ffacts%2Fbase_spec.rb;h=5ba96408b82566ab79858d60f84d87db02d43f5b;hb=b87d2f4e68281062df1913440ca5753ae63314a9;hp=0000000000000000000000000000000000000000;hpb=ab0ea530b8ac956091f17b104ab2311336cfc250;p=packages%2Fprecise%2Fmcollective.git diff --git a/spec/unit/facts/base_spec.rb b/spec/unit/facts/base_spec.rb new file mode 100755 index 0000000..5ba9640 --- /dev/null +++ b/spec/unit/facts/base_spec.rb @@ -0,0 +1,118 @@ +#!/usr/bin/env rspec + +require 'spec_helper' + +module MCollective::Facts + describe Base do + before do + class Testfacts "facts_plugin", :class => "MCollective::Facts::Testfacts"} + end + + describe "#inherited" do + it "should add classes to the plugin manager" do + MCollective::PluginManager.expects("<<").with({:type => "facts_plugin", :class => "MCollective::Facts::Bar"}) + + class Bar "bar"}).once + + f = Testfacts.new + f.get_fact("foo") + end + + it "should honor the cache timeout" do + Testfacts.any_instance.stubs("load_facts_from_source").returns({"foo" => "bar"}).once + + f = Testfacts.new + f.get_fact("foo") + f.get_fact("foo") + end + + it "should detect empty facts" do + Testfacts.any_instance.stubs("load_facts_from_source").returns({}) + MCollective::Log.expects("error").with("Failed to load facts: RuntimeError: Got empty facts").once + + f = Testfacts.new + f.get_fact("foo") + end + + it "should convert non string facts to strings" do + Testfacts.any_instance.stubs("load_facts_from_source").returns({:foo => "bar"}) + + f = Testfacts.new + f.get_fact("foo").should == "bar" + end + + it "should not create duplicate facts while converting to strings" do + Testfacts.any_instance.stubs("load_facts_from_source").returns({:foo => "bar"}) + + f = Testfacts.new + f.get_fact(nil).include?(:foo).should == false + end + + it "should update last_facts_load on success" do + Testfacts.any_instance.stubs("load_facts_from_source").returns({"foo" => "bar"}).once + + f = Testfacts.new + f.get_fact("foo") + + f.instance_variable_get("@last_facts_load").should_not == 0 + end + + it "should restore last known good facts on failure" do + Testfacts.any_instance.stubs("load_facts_from_source").returns({}).once + MCollective::Log.expects("error").with("Failed to load facts: RuntimeError: Got empty facts").once + + f = Testfacts.new + f.instance_variable_set("@last_good_facts", {"foo" => "bar"}) + + f.get_fact("foo").should == "bar" + end + + it "should return all facts for nil parameter" do + Testfacts.any_instance.stubs("load_facts_from_source").returns({"foo" => "bar", "bar" => "baz"}) + + f = Testfacts.new + f.get_fact(nil).keys.size.should == 2 + end + + it "should return a specific fact when specified" do + Testfacts.any_instance.stubs("load_facts_from_source").returns({"foo" => "bar", "bar" => "baz"}) + + f = Testfacts.new + f.get_fact("bar").should == "baz" + end + end + + describe "#get_facts" do + it "should load and return all facts" do + Testfacts.any_instance.stubs("load_facts_from_source").returns({"foo" => "bar", "bar" => "baz"}) + + f = Testfacts.new + f.get_facts.should == {"foo" => "bar", "bar" => "baz"} + end + end + + describe "#has_fact?" do + it "should correctly report fact presense" do + Testfacts.any_instance.stubs("load_facts_from_source").returns({"foo" => "bar"}) + + f = Testfacts.new + f.has_fact?("foo").should == true + f.has_fact?("bar").should == false + end + end + + end +end