-
Notifications
You must be signed in to change notification settings - Fork 0
/
logging_test.go
106 lines (90 loc) · 2.16 KB
/
logging_test.go
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
package slog
import (
"io/ioutil"
"os"
"strings"
"testing"
)
func TestLogging(t *testing.T) {
cfg := Config{
File: "/tmp/a",
Debug: false,
Prefix: "TST",
}
Init(cfg)
defer os.Remove(cfg.File)
P("this p-test should be in `%s'", cfg.File)
D("AIYO d-test should not appear in `%s'", cfg.File)
txt, err := ioutil.ReadFile(cfg.File)
if err != nil {
t.Errorf("failed to read `%s': %v", cfg.File, err)
}
s := string(txt)
if strings.Contains(s, "WARN") == false {
t.Errorf("logfile `%s' does not WARN prefix", cfg.File)
}
if strings.Contains(s, "AIYO") == true {
t.Errorf("logfile `%s' contains debug when debug not set", cfg.File)
}
}
func TestStack(t *testing.T) {
cfg := Config{
File: "/tmp/a",
Debug: true,
Prefix: "TST",
}
Init(cfg)
defer os.Remove(cfg.File)
f := func() { P("say hi to stack") }
f()
txt, err := ioutil.ReadFile(cfg.File)
if err != nil {
t.Errorf("failed to read `%s': %v", cfg.File, err)
}
s := string(txt)
if strings.Contains(s, "hi") == false {
t.Errorf("logfile `%s' does not contain canary ", cfg.File)
}
}
func TestDebug(t *testing.T) {
cfg := Config{
File: "/tmp/a",
Debug: true,
Prefix: "TST",
}
Init(cfg)
defer os.Remove(cfg.File)
P("this p-test should be in `%s'", cfg.File)
D("AIYO d-test should appear in `%s'", cfg.File)
txt, err := ioutil.ReadFile(cfg.File)
if err != nil {
t.Errorf("failed to read `%s': %v", cfg.File, err)
}
s := string(txt)
if strings.Contains(s, "WARN") == false {
t.Errorf("logfile `%s' does not WARN prefix", cfg.File)
}
if strings.Contains(s, "AIYO") == false {
t.Errorf("logfile `%s' does not contain debug when debug set", cfg.File)
}
}
func TestAuditLogging(t *testing.T) {
cfg := Config{
File: "/tmp/a",
Debug: false,
AuditFile: "/tmp/b",
Prefix: "TST",
}
Init(cfg)
defer os.Remove(cfg.File)
defer os.Remove(cfg.AuditFile)
A("a-test did some testing for `%s'", cfg.AuditFile)
txt, err := ioutil.ReadFile(cfg.AuditFile)
if err != nil {
t.Errorf("failed to read `%s': %v", cfg.AuditFile, err)
}
s := string(txt)
if strings.Contains(s, "testing") == false {
t.Errorf("logfile `%s' does not testing line", cfg.AuditFile)
}
}