forked from puppetlabs/puppet
/
destinations_spec.rb
executable file
·158 lines (125 loc) · 4.5 KB
/
destinations_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/usr/bin/env rspec
require 'spec_helper'
require 'puppet/util/log'
describe Puppet::Util::Log.desttypes[:report] do
before do
@dest = Puppet::Util::Log.desttypes[:report]
end
it "should require a report at initialization" do
@dest.new("foo").report.should == "foo"
end
it "should send new messages to the report" do
report = mock 'report'
dest = @dest.new(report)
report.expects(:<<).with("my log")
dest.handle "my log"
end
end
describe Puppet::Util::Log.desttypes[:file] do
before do
File.stubs(:open) # prevent actually creating the file
@class = Puppet::Util::Log.desttypes[:file]
end
it "should default to autoflush false" do
@class.new('/tmp/log').autoflush.should == false
end
describe "when matching" do
shared_examples_for "file destination" do
it "should match an absolute path" do
@class.match?(abspath).should be_true
end
it "should not match a relative path" do
@class.match?(relpath).should be_false
end
end
describe "on POSIX systems", :as_platform => :posix do
let (:abspath) { '/tmp/log' }
let (:relpath) { 'log' }
it_behaves_like "file destination"
end
describe "on Windows systems", :as_platform => :windows do
let (:abspath) { 'C:\\temp\\log.txt' }
let (:relpath) { 'log.txt' }
it_behaves_like "file destination"
end
end
end
describe Puppet::Util::Log.desttypes[:syslog] do
let (:klass) { Puppet::Util::Log.desttypes[:syslog] }
# these tests can only be run when syslog is present, because
# we can't stub the top-level Syslog module
describe "when syslog is available", :if => Puppet.features.syslog? do
before :each do
Syslog.stubs(:opened?).returns(false)
Syslog.stubs(:const_get).returns("LOG_KERN").returns(0)
Syslog.stubs(:open)
end
it "should open syslog" do
Syslog.expects(:open)
klass.new
end
it "should close syslog" do
Syslog.expects(:close)
dest = klass.new
dest.close
end
it "should send messages to syslog" do
syslog = mock 'syslog'
syslog.expects(:info).with("don't panic")
Syslog.stubs(:open).returns(syslog)
msg = Puppet::Util::Log.new(:level => :info, :message => "don't panic")
dest = klass.new
dest.handle(msg)
end
end
describe "when syslog is unavailable" do
it "should not be a suitable log destination" do
Puppet.features.stubs(:syslog?).returns(false)
klass.suitable?(:syslog).should be_false
end
end
end
describe Puppet::Util::Log.desttypes[:console] do
describe "when color is available" do
it "should support color output" do
Puppet.stubs(:[]).with(:color).returns(true)
subject.colorize(:red, 'version').should == "\e[0;31mversion\e[0m"
end
it "should withhold color output when not appropriate" do
Puppet.stubs(:[]).with(:color).returns(false)
subject.colorize(:red, 'version').should == "version"
end
it "should handle multiple overlapping colors in a stack-like way" do
Puppet.stubs(:[]).with(:color).returns(true)
vstring = subject.colorize(:red, 'version')
subject.colorize(:green, "(#{vstring})").should == "\e[0;32m(\e[0;31mversion\e[0;32m)\e[0m"
end
it "should handle resets in a stack-like way" do
Puppet.stubs(:[]).with(:color).returns(true)
vstring = subject.colorize(:reset, 'version')
subject.colorize(:green, "(#{vstring})").should == "\e[0;32m(\e[mversion\e[0;32m)\e[0m"
end
end
end
describe Puppet::Util::Log.desttypes[:telly_prototype_console] do
describe "when color is available" do
it "should support color output" do
Puppet.stubs(:[]).with(:color).returns(true)
subject.colorize(:red, 'version').should == "\e[0;31mversion\e[0m"
end
it "should withhold color output when not appropriate" do
Puppet.stubs(:[]).with(:color).returns(false)
subject.colorize(:red, 'version').should == "version"
end
it "should handle multiple overlapping colors in a stack-like way" do
Puppet.stubs(:[]).with(:color).returns(true)
vstring = subject.colorize(:red, 'version')
subject.colorize(:green, "(#{vstring})").should == "\e[0;32m(\e[0;31mversion\e[0;32m)\e[0m"
end
it "should handle resets in a stack-like way" do
Puppet.stubs(:[]).with(:color).returns(true)
vstring = subject.colorize(:reset, 'version')
subject.colorize(:green, "(#{vstring})").should == "\e[0;32m(\e[mversion\e[0;32m)\e[0m"
end
end
end