ext/syslog/test.rb


DEFINITIONS

This source file includes following functions.


   1  #!/usr/bin/env ruby
   2  # $RoughId: test.rb,v 1.9 2002/02/25 08:20:14 knu Exp $
   3  # $Id: test.rb,v 1.2 2002/02/25 12:13:30 knu Exp $
   4  
   5  # Please only run this test on machines reasonable for testing.
   6  # If in doubt, ask your admin.
   7  
   8  require 'runit/testcase'
   9  require 'runit/cui/testrunner'
  10  
  11  # Prepend current directory to load path for testing.
  12  $:.unshift('.')
  13  
  14  require 'syslog'
  15  
  16  class TestSyslog < RUNIT::TestCase
  17    def test_new
  18      assert_exception(NameError) {
  19        Syslog.new
  20      }
  21    end
  22  
  23    def test_instance
  24      sl1 = Syslog.instance
  25      sl2 = Syslog.open
  26      sl3 = Syslog.instance
  27  
  28      assert_equal(Syslog, sl1)
  29      assert_equal(Syslog, sl2)
  30      assert_equal(Syslog, sl3)
  31    ensure
  32      Syslog.close
  33    end
  34  
  35    def test_open
  36      # default parameters
  37      Syslog.open
  38  
  39      assert_equal($0, Syslog.ident)
  40      assert_equal(Syslog::LOG_PID | Syslog::LOG_CONS, Syslog.options)
  41      assert_equal(Syslog::LOG_USER, Syslog.facility)
  42  
  43      # open without close
  44      assert_exception(RuntimeError) {
  45        Syslog.open
  46      }
  47  
  48      Syslog.close
  49  
  50      # given parameters
  51      Syslog.open("foo", Syslog::LOG_NDELAY | Syslog::LOG_PERROR, Syslog::LOG_DAEMON) 
  52  
  53      assert_equal('foo', Syslog.ident)
  54      assert_equal(Syslog::LOG_NDELAY | Syslog::LOG_PERROR, Syslog.options)
  55      assert_equal(Syslog::LOG_DAEMON, Syslog.facility)
  56  
  57      Syslog.close
  58  
  59      # default parameters again (after close)
  60      Syslog.open
  61      Syslog.close
  62  
  63      assert_equal($0, Syslog.ident)
  64      assert_equal(Syslog::LOG_PID | Syslog::LOG_CONS, Syslog.options)
  65      assert_equal(Syslog::LOG_USER, Syslog.facility)
  66  
  67      # block
  68      param = nil
  69      Syslog.open { |param| }
  70      assert_equal(Syslog, param)
  71    ensure
  72      Syslog.close
  73    end
  74  
  75    def test_opened?
  76      assert_equal(false, Syslog.opened?)
  77  
  78      Syslog.open
  79      assert_equal(true, Syslog.opened?)
  80  
  81      Syslog.close
  82      assert_equal(false, Syslog.opened?)
  83  
  84      Syslog.open {
  85        assert_equal(true, Syslog.opened?)
  86      }
  87  
  88      assert_equal(false, Syslog.opened?)
  89    end
  90  
  91    def test_mask
  92      Syslog.open
  93  
  94      orig = Syslog.mask
  95  
  96      Syslog.mask = Syslog.LOG_UPTO(Syslog::LOG_ERR)
  97      assert_equal(Syslog.LOG_UPTO(Syslog::LOG_ERR), Syslog.mask)
  98  
  99      Syslog.mask = Syslog.LOG_MASK(Syslog::LOG_CRIT)
 100      assert_equal(Syslog.LOG_MASK(Syslog::LOG_CRIT), Syslog.mask)
 101  
 102      Syslog.mask = orig
 103    ensure
 104      Syslog.close
 105    end
 106  
 107    def test_log
 108      stderr = IO::pipe
 109  
 110      pid = fork {
 111        stderr[0].close
 112        STDERR.reopen(stderr[1])
 113        stderr[1].close
 114  
 115        options = Syslog::LOG_PERROR | Syslog::LOG_NDELAY
 116  
 117        Syslog.open("syslog_test", options) { |sl|
 118          sl.log(Syslog::LOG_NOTICE, "test1 - hello, %s!", "world")
 119          sl.notice("test1 - hello, %s!", "world")
 120        }
 121  
 122        Syslog.open("syslog_test", options | Syslog::LOG_PID) { |sl|
 123          sl.log(Syslog::LOG_CRIT, "test2 - pid")
 124          sl.crit("test2 - pid")
 125        }
 126        exit!
 127      }
 128  
 129      stderr[1].close
 130      Process.waitpid(pid)
 131  
 132      # LOG_PERROR is not yet implemented on Cygwin.
 133      return if RUBY_PLATFORM =~ /cygwin/
 134  
 135      2.times {
 136        assert_equal("syslog_test: test1 - hello, world!\n", stderr[0].gets)
 137      }
 138  
 139      2.times {
 140        assert_equal(format("syslog_test[%d]: test2 - pid\n", pid), stderr[0].gets)
 141      }
 142    end
 143  
 144    def test_inspect
 145      Syslog.open { |sl|
 146        assert_equal(format('<#%s: opened=%s, ident="%s", ' +
 147                            'options=%d, facility=%d, mask=%d>',
 148                            Syslog, sl.opened?, sl.ident,
 149                            sl.options, sl.facility, sl.mask),
 150                     sl.inspect)
 151      }
 152    end
 153  end
 154  
 155  if $0 == __FILE__
 156    suite = RUNIT::TestSuite.new
 157  
 158    suite.add_test(TestSyslog.suite)
 159  
 160    RUNIT::CUI::TestRunner.run(suite)
 161  end