File: destinations_spec.rb

package info (click to toggle)
puppet 4.8.2-5
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 20,736 kB
  • ctags: 14,616
  • sloc: ruby: 236,754; xml: 1,586; sh: 1,178; lisp: 299; sql: 103; yacc: 72; makefile: 52
file content (228 lines) | stat: -rw-r--r-- 6,903 bytes parent folder | download | duplicates (2)
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#! /usr/bin/env ruby
require 'spec_helper'
require 'json'

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
    expect(@dest.new("foo").report).to eq("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
  include PuppetSpec::Files

  before do
    @class = Puppet::Util::Log.desttypes[:file]
  end

  it "should default to autoflush false" do
    expect(@class.new(tmpfile('log')).autoflush).to eq(true)
  end

  describe "when matching" do
    shared_examples_for "file destination" do
      it "should match an absolute path" do
        expect(@class.match?(abspath)).to be_truthy
      end

      it "should not match a relative path" do
        expect(@class.match?(relpath)).to be_falsey
      end
    end

    describe "on POSIX systems", :if => Puppet.features.posix? do
      let (:abspath) { '/tmp/log' }
      let (:relpath) { 'log' }

      it_behaves_like "file destination"

      it "logs an error if it can't chown the file owner & group" do
        FileUtils.expects(:chown).with(Puppet[:user], Puppet[:group], abspath).raises(Errno::EPERM)
        Puppet.features.expects(:root?).returns(true)
        Puppet.expects(:err).with("Unable to set ownership to #{Puppet[:user]}:#{Puppet[:group]} for log file: #{abspath}")

        @class.new(abspath)
      end

      it "doesn't attempt to chown when running as non-root" do
        FileUtils.expects(:chown).with(Puppet[:user], Puppet[:group], abspath).never
        Puppet.features.expects(:root?).returns(false)

        @class.new(abspath)
      end
    end

    describe "on Windows systems", :if => Puppet.features.microsoft_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)

      expect(klass.suitable?(:syslog)).to be_falsey
    end
  end
end

describe Puppet::Util::Log.desttypes[:logstash_event] do

  describe "when using structured log format with logstash_event schema" do
    before :each do
      @msg = Puppet::Util::Log.new(:level => :info, :message => "So long, and thanks for all the fish.", :source => "a dolphin")
    end

    it "format should fix the hash to have the correct structure" do
      dest = described_class.new
      result = dest.format(@msg)
      expect(result["version"]).to eq(1)
      expect(result["level"]).to   eq(:info)
      expect(result["message"]).to eq("So long, and thanks for all the fish.")
      expect(result["source"]).to  eq("a dolphin")
      # timestamp should be within 10 seconds
      expect(Time.parse(result["@timestamp"])).to be >= ( Time.now - 10 )
    end

    it "format returns a structure that can be converted to json" do
      dest = described_class.new
      hash = dest.format(@msg)
      JSON.parse(hash.to_json)
    end

    it "handle should send the output to stdout" do
      $stdout.expects(:puts).once
      dest = described_class.new
      dest.handle(@msg)
    end
  end
end

describe Puppet::Util::Log.desttypes[:console] do
  let (:klass) { Puppet::Util::Log.desttypes[:console] }

  it "should support color output" do
    Puppet[:color] = true
    expect(subject.colorize(:red, 'version')).to eq("\e[0;31mversion\e[0m")
  end

  it "should withhold color output when not appropriate" do
    Puppet[:color] = false
    expect(subject.colorize(:red, 'version')).to eq("version")
  end

  it "should handle multiple overlapping colors in a stack-like way" do
    Puppet[:color] = true
    vstring = subject.colorize(:red, 'version')
    expect(subject.colorize(:green, "(#{vstring})")).to eq("\e[0;32m(\e[0;31mversion\e[0;32m)\e[0m")
  end

  it "should handle resets in a stack-like way" do
    Puppet[:color] = true
    vstring = subject.colorize(:reset, 'version')
    expect(subject.colorize(:green, "(#{vstring})")).to eq("\e[0;32m(\e[mversion\e[0;32m)\e[0m")
  end

  it "should include the log message's source/context in the output when available" do
    Puppet[:color] = false
    $stdout.expects(:puts).with("Info: a hitchhiker: don't panic")

    msg = Puppet::Util::Log.new(:level => :info, :message => "don't panic", :source => "a hitchhiker")
    dest = klass.new
    dest.handle(msg)
  end
end


describe ":eventlog", :if => Puppet::Util::Platform.windows? do
  let(:klass) { Puppet::Util::Log.desttypes[:eventlog] }

  def expects_message_with_type(klass, level, eventlog_type, eventlog_id)
    eventlog = stub('eventlog')
    eventlog.expects(:report_event).with(has_entries(:source => "Puppet", :event_type => eventlog_type, :event_id => eventlog_id, :data => "a hitchhiker: don't panic"))
    Win32::EventLog.stubs(:open).returns(eventlog)

    msg = Puppet::Util::Log.new(:level => level, :message => "don't panic", :source => "a hitchhiker")
    dest = klass.new
    dest.handle(msg)
  end

  it "supports the eventlog feature" do
    expect(Puppet.features.eventlog?).to be_truthy
  end

  it "logs to the Application event log" do
    eventlog = stub('eventlog')
    Win32::EventLog.expects(:open).with('Application').returns(stub('eventlog'))

    klass.new
  end

  it "logs :debug level as an information type event" do
    expects_message_with_type(klass, :debug, klass::EVENTLOG_INFORMATION_TYPE, 0x1)
  end

  it "logs :warning level as an warning type event" do
    expects_message_with_type(klass, :warning, klass::EVENTLOG_WARNING_TYPE, 0x2)
  end

  it "logs :err level as an error type event" do
    expects_message_with_type(klass, :err, klass::EVENTLOG_ERROR_TYPE, 0x3)
  end
end