-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlog_test.go
147 lines (113 loc) · 2.91 KB
/
log_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
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
// SPDX-FileCopyrightText: Andrei Gherzan <[email protected]>
//
// SPDX-License-Identifier: MIT
package mirror
import (
"bytes"
"errors"
"os"
"os/exec"
"testing"
)
// osExitMock is used as a mock function for is.Exit(int).
func osExitMock(exitCode int) {
return
}
// TestNewLogger tests the NewLogger function.
func TestNewLogger(t *testing.T) {
t.Parallel()
if logger := NewLogger(os.Stderr); logger == nil {
t.Fatal("failed to create a logger on stderr")
}
}
// TestGetOutput tests the GetOutput function.
func TestGetOutput(t *testing.T) {
t.Parallel()
logger := NewLogger(os.Stderr)
_ = logger.GetOutput()
}
// TestDebug checks debug logging.
func TestDebug(t *testing.T) {
t.Parallel()
{
// Test debug mode on.
var b bytes.Buffer
logger := NewLogger(&b)
logger.Debug(true, "msg")
if output := b.String(); output != "[DEBUG]: msg\n" {
t.Fatalf("unexpected debug on output: %s", output)
}
}
{
// Test debug mode off.
var b bytes.Buffer
logger := NewLogger(&b)
logger.Debug(false, "msg")
if output := b.String(); output != "" {
t.Fatalf("unexpected debug off output: %s", output)
}
}
}
// TestInfo checks info logging.
func TestInfo(t *testing.T) {
t.Parallel()
var b bytes.Buffer
logger := NewLogger(&b)
logger.Info("msg")
if output := b.String(); output != "[INFO ]: msg\n" {
t.Fatalf("unexpected info output: %s", output)
}
}
// TestWarn checks warn logging.
func TestWarn(t *testing.T) {
t.Parallel()
var b bytes.Buffer
logger := NewLogger(&b)
logger.Warn("msg")
if output := b.String(); output != "[WARN ]: msg\n" {
t.Fatalf("unexpected warn output: %s", output)
}
}
// TestError checks error logging.
func TestError(t *testing.T) {
t.Parallel()
var b bytes.Buffer
logger := NewLogger(&b)
logger.Error("msg")
if output := b.String(); output != "[ERROR]: msg\n" {
t.Fatalf("unexpected error output: %s", output)
}
}
// TestFatalMock checks fatal logging using a mocked exit function.
func TestFatalMock(t *testing.T) {
// We can't run this in parallel with other tests as it messes up with
// os.Exit which other tests might require - for example TestFatal. So
// do not flag it with t.Parallel().
var buf bytes.Buffer
logger := NewLogger(&buf)
origOsExit := osExit
osExit = osExitMock
defer func() { osExit = origOsExit }()
logger.Fatal("msg")
if output := buf.String(); output != "[FATAL]: msg\n" {
t.Fatalf("unexpected fatal output: %s", output)
}
}
// TestFatal checks fatal logging.
func TestFatal(t *testing.T) {
t.Parallel()
if os.Getenv("RUNTESTFATAL") == "1" {
var b bytes.Buffer
logger := NewLogger(&b)
logger.Fatal("msg")
return
}
//#nosec G204 -- This is only for testing
cmd := exec.Command(os.Args[0], "-test.run=TestFatal")
cmd.Env = append(os.Environ(), "RUNTESTFATAL=1")
e := (&exec.ExitError{})
if err := cmd.Run(); errors.As(err, &e) && !e.Success() {
return
}
t.Fatalf("fatal log didn't fail the running process")
}