File: logging_spec.rb

package info (click to toggle)
puppet-agent 7.23.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 19,092 kB
  • sloc: ruby: 245,074; sh: 456; makefile: 38; xml: 33
file content (62 lines) | stat: -rw-r--r-- 2,098 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
require 'spec_helper'
require 'puppet_spec/compiler'

describe 'the log function' do
  include PuppetSpec::Compiler

  def collect_logs(code)
    Puppet[:code] = code
    Puppet[:environment_timeout] = 10
    node = Puppet::Node.new('logtest')
    compiler = Puppet::Parser::Compiler.new(node)
    node.environment.check_for_reparse
    logs = []
    Puppet::Util::Log.with_destination(Puppet::Test::LogCollector.new(logs)) do
      compiler.compile
    end
    logs
  end

  def expect_log(code, log_level, message)
    logs = collect_logs(code)

    # There's a chance that additional messages could have been
    # added during code compilation like debug messages from
    # Facter. This happened on Appveyor when the Appveyor image
    # failed to resolve the domain fact. Since these messages are
    # not relevant to our test, and since they can (possibly) be
    # injected at any location in our logs array, it is good enough
    # to assert that our desired log _is_ included in the logs array
    # instead of trying to figure out _where_ it is included.
    expect(logs).to include(satisfy { |log| log.level == log_level && log.message == message })
  end

  before(:each) do
    Puppet[:log_level] = 'debug'
  end

  Puppet::Util::Log.levels.each do |level|
    context "for log level '#{level}'" do
      it 'can be called' do
        expect_log("#{level.to_s}('yay')", level, 'yay')
      end

      it 'joins multiple arguments using space' do
        # Not using the evaluator would result in yay {"a"=>"b", "c"=>"d"}
        expect_log("#{level.to_s}('a', 'b', 3)", level, 'a b 3')
      end

      it 'uses the evaluator to format output' do
        # Not using the evaluator would result in yay {"a"=>"b", "c"=>"d"}
        expect_log("#{level.to_s}('yay', {a => b, c => d})", level, 'yay {a => b, c => d}')
      end

      it 'returns undef value' do
        logs = collect_logs("notice(type(#{level.to_s}('yay')))")
        expect(logs.size).to eql(2)
        expect(logs[1].level).to eql(:notice)
        expect(logs[1].message).to eql('Undef')
      end
    end
  end
end