Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 83 lines (62 sloc) 1.518 kb
53e6d8b9 »
2012-10-02 Integrate gosteno logging
1 // Copyright (c) 2012 VMware, Inc.
2
3 package gonit
4
5 import (
6 "io/ioutil"
e87510fe »
2012-10-17 migrate from 'assert' to 'gocheck' for unit tests
7 . "launchpad.net/gocheck"
53e6d8b9 »
2012-10-02 Integrate gosteno logging
8 "os"
9 )
10
11 func reInitLogger() {
12 (&LoggerConfig{}).Init() // reset to defaults
13 }
14
e87510fe »
2012-10-17 migrate from 'assert' to 'gocheck' for unit tests
15 type LogSuite struct{}
16
17 var _ = Suite(&LogSuite{})
18
19 func (s *LogSuite) TestLogInit(c *C) {
53e6d8b9 »
2012-10-02 Integrate gosteno logging
20 defer reInitLogger()
21
22 config := &LoggerConfig{
23 Level: "debug",
24 }
25
26 err := config.Init()
e87510fe »
2012-10-17 migrate from 'assert' to 'gocheck' for unit tests
27 c.Check(err, IsNil)
28 c.Check(config.file, IsNil)
53e6d8b9 »
2012-10-02 Integrate gosteno logging
29 err = config.Close()
e87510fe »
2012-10-17 migrate from 'assert' to 'gocheck' for unit tests
30 c.Check(err, IsNil)
53e6d8b9 »
2012-10-02 Integrate gosteno logging
31 }
32
e87510fe »
2012-10-17 migrate from 'assert' to 'gocheck' for unit tests
33 func (s *LogSuite) TestInvalidLogLevel(c *C) {
53e6d8b9 »
2012-10-02 Integrate gosteno logging
34 defer reInitLogger()
35
36 config := &LoggerConfig{
37 Level: "enolevel",
38 }
39
40 err := config.Init()
e87510fe »
2012-10-17 migrate from 'assert' to 'gocheck' for unit tests
41 c.Check(err, NotNil)
53e6d8b9 »
2012-10-02 Integrate gosteno logging
42 }
43
e87510fe »
2012-10-17 migrate from 'assert' to 'gocheck' for unit tests
44 func (s *LogSuite) TestLogFile(c *C) {
53e6d8b9 »
2012-10-02 Integrate gosteno logging
45 defer reInitLogger()
46
47 file, err := ioutil.TempFile("", "gonit_log")
e87510fe »
2012-10-17 migrate from 'assert' to 'gocheck' for unit tests
48 c.Check(err, IsNil)
53e6d8b9 »
2012-10-02 Integrate gosteno logging
49 defer os.Remove(file.Name())
50
51 config := &LoggerConfig{
52 FileName: file.Name(),
53 Level: "info",
54 }
55
56 err = config.Init()
e87510fe »
2012-10-17 migrate from 'assert' to 'gocheck' for unit tests
57 c.Check(err, IsNil)
53e6d8b9 »
2012-10-02 Integrate gosteno logging
58
59 fi, err := os.Stat(config.FileName)
e87510fe »
2012-10-17 migrate from 'assert' to 'gocheck' for unit tests
60 c.Check(err, IsNil)
61 c.Check(fi.Size(), Equals, int64(0))
53e6d8b9 »
2012-10-02 Integrate gosteno logging
62
63 // info message should be written to the log file
64 Log.Info("testing")
65 fi, err = os.Stat(config.FileName)
e87510fe »
2012-10-17 migrate from 'assert' to 'gocheck' for unit tests
66 c.Check(err, IsNil)
67 c.Check(fi.Size(), Not(Equals), int64(0))
53e6d8b9 »
2012-10-02 Integrate gosteno logging
68
69 // info message should not
70 Log.Debug("another test")
71 fi2, err := os.Stat(config.FileName)
e87510fe »
2012-10-17 migrate from 'assert' to 'gocheck' for unit tests
72 c.Check(err, IsNil)
73 c.Check(fi.Size(), Equals, fi2.Size())
53e6d8b9 »
2012-10-02 Integrate gosteno logging
74
75 err = config.Close()
e87510fe »
2012-10-17 migrate from 'assert' to 'gocheck' for unit tests
76 c.Check(err, IsNil)
53e6d8b9 »
2012-10-02 Integrate gosteno logging
77
8d200b9b »
2012-11-01 test fixes
78 // check Init returns an error when file create fails
79 config.FileName = "/dev/null/gonnafail"
53e6d8b9 »
2012-10-02 Integrate gosteno logging
80 err = config.Init()
e87510fe »
2012-10-17 migrate from 'assert' to 'gocheck' for unit tests
81 c.Check(err, NotNil)
53e6d8b9 »
2012-10-02 Integrate gosteno logging
82 }
Something went wrong with that request. Please try again.