f98434362ccb6530d62d23d302fa5ae6001a1d0b
[packages/precise/mcollective.git] / spec / unit / log_spec.rb
1 #!/usr/bin/env rspec
2
3 require 'spec_helper'
4
5 module MCollective
6   describe Log do
7     before do
8       @logger = mock("logger provider")
9       @logger.stubs(:start)
10       @logger.stubs(:set_logging_level)
11       @logger.stubs(:valid_levels)
12       @logger.stubs(:should_log?).returns(true)
13       @logger.stubs(:new).returns(@logger)
14
15       # we stub it out at the top of the test suite
16       Log.unstub(:log)
17       Log.unstub(:logexception)
18       Log.unstub(:logmsg)
19
20       Log.unconfigure
21       Log.set_logger(@logger)
22     end
23
24     describe "#config_and_check_level" do
25       it "should configure then not already configured" do
26         Log.expects(:configure)
27         Log.config_and_check_level(:debug)
28       end
29
30       it "should not reconfigure the logger" do
31         Log.configure(@logger)
32         Log.expects(:configure).never
33         Log.config_and_check_level(:debug)
34       end
35
36       it "should check the level is valid" do
37         Log.configure(@logger)
38         Log.expects(:check_level).with(:debug)
39         Log.config_and_check_level(:debug)
40       end
41
42       it "should respect the loggers decision about levels" do
43         Log.configure(@logger)
44         @logger.expects(:should_log?).returns(false)
45         Log.config_and_check_level(:debug).should == false
46       end
47     end
48
49     describe "#valid_level?" do
50       it "should correctly report for valid levels" do
51         [:error, :fatal, :debug, :warn, :info].each {|level| Log.valid_level?(level).should == true }
52         Log.valid_level?(:rspec).should == false
53       end
54     end
55
56     describe "#message_for" do
57       it "should return the code and retrieved message" do
58         Util.expects(:t).with(:PLMC1, {:rspec => true}).returns("this is PLMC1")
59         Log.message_for(:PLMC1, {:rspec => true}).should == "PLMC1: this is PLMC1"
60       end
61     end
62
63     describe "#logexception" do
64       it "should short circuit messages below current level" do
65         Log.expects(:config_and_check_level).with(:debug).returns(false)
66         Log.expects(:log).never
67         Log.logexception(:PLMC1, :debug, Exception.new, {})
68       end
69
70       it "should request the message including the exception string and log it" do
71         Log.stubs(:config_and_check_level).returns(true)
72         Log.expects(:message_for).with(:PLMC1, {:rspec => "test", :error => "Exception: this is a test"}).returns("This is a test")
73         Log.expects(:log).with(:debug, "This is a test", "test:2")
74
75         e = Exception.new("this is a test")
76         e.set_backtrace ["/some/dir/test:1", "/some/dir/test:2"]
77
78         Log.logexception(:PLMC1, :debug, e, false, {:rspec => "test"})
79       end
80     end
81
82     describe "#logmsg" do
83       it "should short circuit messages below current level" do
84         Log.expects(:config_and_check_level).with(:debug).returns(false)
85         Log.expects(:log).never
86         Log.logmsg(:PLMC1, "", :debug, {})
87       end
88
89       it "should request the message and log it" do
90         Log.stubs(:config_and_check_level).returns(true)
91         Log.expects(:message_for).with(:PLMC1, {:rspec => "test", :default => "default"}).returns("This is a test")
92         Log.expects(:log).with(:debug, "This is a test")
93         Log.logmsg(:PLMC1, "default", :debug, :rspec => "test")
94       end
95     end
96
97     describe "#check_level" do
98       it "should check for valid levels" do
99         Log.expects(:valid_level?).with(:debug).returns(true)
100         Log.check_level(:debug)
101       end
102
103       it "should raise for invalid levels" do
104         expect { Log.check_level(:rspec) }.to raise_error("Unknown log level")
105       end
106     end
107
108     describe "#configure" do
109       it "should default to console logging if called prior to configuration" do
110         Config.instance.instance_variable_set("@configured", false)
111         Log.configure
112         Log.logger.should ==  MCollective::Logger::Console_logger
113       end
114     end
115
116     describe "#instance" do
117       it "should return the correct reference" do
118         Log.configure(@logger)
119         Log.instance.should == MCollective::Log
120       end
121     end
122
123     describe "#log" do
124       it "should log at the right levels" do
125         Log.configure(@logger)
126
127         [:debug, :info, :fatal, :error, :warn].each do |level|
128           @logger.expects(:log).with(level, anything, regexp_matches(/#{level} test/))
129           @logger.expects(:should_log?).with(level).returns(true)
130           Log.send(level, "#{level} test")
131         end
132       end
133     end
134
135     describe "#cycle_level" do
136       it "should cycle logger class levels" do
137         @logger.expects(:cycle_level)
138
139         Log.configure(@logger)
140         Log.cycle_level
141       end
142     end
143   end
144 end