Skip to content

Commit 002c38f

Browse files
committed
logs: add more tests
1 parent fc42755 commit 002c38f

File tree

1 file changed

+93
-0
lines changed

1 file changed

+93
-0
lines changed

logs/logs_test.go

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,14 @@ package logs
66
import (
77
"bytes"
88
"errors"
9+
"github.com/etix/mirrorbits/core"
910
"github.com/etix/mirrorbits/filesystem"
1011
"github.com/etix/mirrorbits/mirrors"
1112
"github.com/etix/mirrorbits/network"
13+
"github.com/op/go-logging"
1214
"io/ioutil"
1315
"os"
16+
"reflect"
1417
"strings"
1518
"testing"
1619
)
@@ -48,6 +51,96 @@ func TestDownloadsLogger_Close(t *testing.T) {
4851
}
4952
}
5053

54+
func TestIsTerminal(t *testing.T) {
55+
stat, _ := os.Stdout.Stat()
56+
if (stat.Mode() & os.ModeCharDevice) == 0 {
57+
t.Skip("Cannot test without a valid terminal")
58+
}
59+
60+
if !isTerminal(os.Stdout) {
61+
t.Fatalf("The current terminal is supposed to support colors")
62+
}
63+
64+
f, err := ioutil.TempFile("", "mirrorbits-tests")
65+
if err != nil {
66+
t.Errorf("Unable to create a temporary file: %s", err.Error())
67+
return
68+
}
69+
defer os.Remove(f.Name())
70+
71+
if isTerminal(f) {
72+
t.Fatalf("The given file cannot be a terminal")
73+
}
74+
}
75+
76+
func TestReloadRuntimeLogs(t *testing.T) {
77+
rlogger.f = nil
78+
79+
ReloadRuntimeLogs()
80+
81+
if rlogger.f == nil {
82+
t.Fatalf("The logger output must be setup")
83+
}
84+
if rlogger.f != os.Stderr {
85+
t.Fatalf("The logger output is expected to be Stderr")
86+
}
87+
if logging.GetLevel("main") != logging.INFO {
88+
t.Fatalf("Log level is supposed to be INFO by default")
89+
}
90+
91+
ptr := reflect.ValueOf(rlogger.f).Pointer()
92+
ReloadRuntimeLogs()
93+
if reflect.ValueOf(rlogger.f).Pointer() != ptr {
94+
t.Fatalf("The logger must not be reloaded when writing on Stderr")
95+
}
96+
97+
/* */
98+
core.RunLog = "/"
99+
ReloadRuntimeLogs()
100+
if rlogger.f != os.Stderr {
101+
t.Fatalf("Opening an invalid file must fallback to Stderr")
102+
}
103+
104+
/* */
105+
f, err := ioutil.TempFile("", "mirrorbits-tests")
106+
if err != nil {
107+
t.Errorf("Unable to create a temporary file: %s", err.Error())
108+
return
109+
}
110+
defer os.Remove(f.Name())
111+
112+
core.RunLog = f.Name()
113+
core.Debug = true
114+
115+
ReloadRuntimeLogs()
116+
117+
if logging.GetLevel("main") != logging.DEBUG {
118+
t.Fatalf("Log level is supposed to be DEBUG")
119+
}
120+
121+
if rlogger.f == os.Stderr {
122+
t.Fatalf("The output is expected to be a file, not Stderr")
123+
}
124+
125+
/* */
126+
testString := "Testing42"
127+
log.Error(testString)
128+
129+
buf, _ := ioutil.ReadAll(f)
130+
if !strings.Contains(string(buf), testString) {
131+
t.Fatalf("The log doesn't contain the string %s", testString)
132+
}
133+
134+
/* */
135+
core.RunLog = ""
136+
ReloadRuntimeLogs()
137+
138+
if rlogger.f != os.Stderr {
139+
t.Fatalf("The output is expected to be Stderr")
140+
}
141+
142+
}
143+
51144
func TestOpenLogFile(t *testing.T) {
52145
path, err := ioutil.TempDir("", "mirrorbits-tests")
53146
if err != nil {

0 commit comments

Comments
 (0)