summaryrefslogtreecommitdiffstats
path: root/test/util/log.rb
blob: 908347a5086600b63dceafd3df2b95eb1b43f814 (plain)
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
#!/usr/bin/env ruby

require File.expand_path(File.dirname(__FILE__) + '/../lib/puppettest')

require 'puppet'
require 'puppet/util/log'
require 'puppettest'

class TestLog < Test::Unit::TestCase
  include PuppetTest

  def setup
    super
    @oldloglevel = Puppet::Util::Log.level
    Puppet::Util::Log.close_all
  end

  def teardown
    super
    Puppet::Util::Log.close_all
    Puppet::Util::Log.level = @oldloglevel
  end

  def getlevels
    levels = nil
    assert_nothing_raised {
      levels = []
      Puppet::Util::Log.eachlevel { |level| levels << level }
    }
    # Don't test the top levels; too annoying
    levels.reject { |level| level == :emerg or level == :crit }
  end

  def mkmsgs(levels)
    levels.collect { |level|
      next if level == :alert
      assert_nothing_raised {

              Puppet::Util::Log.new(
                
          :level => level,
          :source => "Test",
        
          :message => "Unit test for #{level}"
        )
      }
    }
  end

  def test_logfile
    fact = nil
    levels = nil
    Puppet::Util::Log.level = :debug
    levels = getlevels
    logfile = tempfile
    fact = nil
    assert_nothing_raised {
      Puppet::Util::Log.newdestination(logfile)
    }
    msgs = mkmsgs(levels)
    assert(msgs.length == levels.length)
    Puppet::Util::Log.close_all
    count = 0

    assert(FileTest.exists?(logfile), "Did not create logfile")

    assert_nothing_raised {
      File.open(logfile) { |of|
        count = of.readlines.length
      }
    }
    assert(count == levels.length - 1) # skip alert
  end

  def test_syslog
    levels = nil
    assert_nothing_raised {
      levels = getlevels.reject { |level|
        level == :emerg || level == :crit
      }
    }
    assert_nothing_raised {
      Puppet::Util::Log.newdestination("syslog")
    }
    # there's really no way to verify that we got syslog messages...
    msgs = mkmsgs(levels)
    assert(msgs.length == levels.length)
  end

  def test_levelmethods
    assert_nothing_raised {
      Puppet::Util::Log.newdestination("/dev/null")
    }
    getlevels.each { |level|
      assert_nothing_raised {
        Puppet.send(level,"Testing for #{level}")
      }
    }
  end

  def test_output
    Puppet::Util::Log.level = :notice
    assert(Puppet.err("This is an error").is_a?(Puppet::Util::Log))
    assert(Puppet.debug("This is debugging").nil?)
    Puppet::Util::Log.level = :debug
    assert(Puppet.err("This is an error").is_a?(Puppet::Util::Log))
    assert(Puppet.debug("This is debugging").is_a?(Puppet::Util::Log))
  end

  def test_creatingdirs
    dir = tempfile
    file = File.join(dir, "logfile")
    Puppet::Util::Log.newdestination file
    Puppet.info "testing logs"
    assert(FileTest.directory?(dir))
    assert(FileTest.file?(file))
  end

  # Verify that we can pass strings that match printf args
  def test_percentlogs
    Puppet::Util::Log.newdestination :syslog

    assert_nothing_raised {

            Puppet::Util::Log.new(
                
        :level => :info,
        
        :message => "A message with %s in it"
      )
    }
  end

  # Verify that the error and source are always strings
  def test_argsAreStrings
    msg = nil

          file = Puppet::Type.type(:file).new(
                
      :path => tempfile,
        
      :check => %w{owner group}
    )
    assert_nothing_raised {
      msg = Puppet::Util::Log.new(:level => :info, :message => "This is a message")
    }
    assert_nothing_raised {
      msg.source = file
    }

    assert_instance_of(String, msg.to_s)
    assert_instance_of(String, msg.source)
  end

  def test_destination_matching
    dest = nil
    assert_nothing_raised {
      dest = Puppet::Util::Log.newdesttype("Destine") do
        def handle(msg)
          puts msg
        end
      end
    }

    [:destine, "Destine", "destine"].each do |name|
      assert(dest.match?(name), "Did not match #{name.inspect}")
    end

    assert_nothing_raised {
      dest.match(:yayness)
    }
    assert(dest.match("Yayness"), "Did not match yayness")
    Puppet::Util::Log.close(dest)
  end

  def test_autoflush
    file = tempfile
    Puppet::Util::Log.close(:console)
    Puppet::Util::Log.newdestination(file)
    Puppet.warning "A test"
    assert(File.read(file) !~ /A test/, "File defualted to autoflush")
    Puppet::Util::Log.flush
    assert(File.read(file) =~ /A test/, "File did not flush")
    Puppet::Util::Log.close(file)

    # Now try one with autoflush enabled
    Puppet[:autoflush] = true
    file = tempfile
    Puppet::Util::Log.newdestination(file)
    Puppet.warning "A test"
    assert(File.read(file) =~ /A test/, "File did not autoflush")
    Puppet::Util::Log.close(file)
  end

  def test_reopen
    Puppet[:autoflush] = true
    file = tempfile
    Puppet::Util::Log.close(:console)
    Puppet::Util::Log.newdestination(file)
    Puppet.warning "A test"
    assert(File.read(file) =~ /A test/,
      "File did not flush")
    # Rename the file
    newfile = file + ".old"
    File.rename(file, newfile)

    # Send another log
    Puppet.warning "Another test"
    assert(File.read(newfile) =~ /Another test/,
      "File did not rename")

    # Now reopen the log
    Puppet::Util::Log.reopen
    Puppet.warning "Reopen test"
    assert(File.read(file) =~ /Reopen test/, "File did not reopen")
    Puppet::Util::Log.close(file)
  end
end