mirror of
https://github.com/gogrlx/nats-server.git
synced 2026-04-02 03:38:42 -07:00
Add code coverage for File logger
This commit is contained in:
@@ -3,10 +3,12 @@ package logger
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -94,6 +96,9 @@ func TestFileLogger(t *testing.T) {
|
|||||||
defer os.RemoveAll(tmpDir)
|
defer os.RemoveAll(tmpDir)
|
||||||
|
|
||||||
file, err := ioutil.TempFile(tmpDir, "gnatsd:log_")
|
file, err := ioutil.TempFile(tmpDir, "gnatsd:log_")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Could not create the temp file: %v", err)
|
||||||
|
}
|
||||||
file.Close()
|
file.Close()
|
||||||
|
|
||||||
logger := NewFileLogger(file.Name(), false, false, false, false)
|
logger := NewFileLogger(file.Name(), false, false, false, false)
|
||||||
@@ -110,6 +115,38 @@ func TestFileLogger(t *testing.T) {
|
|||||||
if string(buf) != "[INF] foo\n" {
|
if string(buf) != "[INF] foo\n" {
|
||||||
t.Fatalf("Expected '%s', received '%s'\n", "[INFO] foo", string(buf))
|
t.Fatalf("Expected '%s', received '%s'\n", "[INFO] foo", string(buf))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
file, err = ioutil.TempFile(tmpDir, "gnatsd:log_")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Could not create the temp file: %v", err)
|
||||||
|
}
|
||||||
|
file.Close()
|
||||||
|
|
||||||
|
logger = NewFileLogger(file.Name(), true, true, true, true)
|
||||||
|
logger.Errorf("foo")
|
||||||
|
|
||||||
|
buf, err = ioutil.ReadFile(file.Name())
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Could not read logfile: %v", err)
|
||||||
|
}
|
||||||
|
if len(buf) <= 0 {
|
||||||
|
t.Fatal("Expected a non-zero length logfile")
|
||||||
|
}
|
||||||
|
str := string(buf)
|
||||||
|
errMsg := fmt.Sprintf("Expected '%s', received '%s'\n", "[pid] <date> [ERR] foo", str)
|
||||||
|
pidEnd := strings.Index(str, " ")
|
||||||
|
infoStart := strings.LastIndex(str, "[ERR]")
|
||||||
|
if pidEnd == -1 || infoStart == -1 {
|
||||||
|
t.Fatalf("%v", errMsg)
|
||||||
|
}
|
||||||
|
pid := str[0:pidEnd]
|
||||||
|
if pid[0] != '[' || pid[len(pid)-1] != ']' {
|
||||||
|
t.Fatalf("%v", errMsg)
|
||||||
|
}
|
||||||
|
//TODO: Parse date.
|
||||||
|
if !strings.HasSuffix(str, "[ERR] foo\n") {
|
||||||
|
t.Fatalf("%v", errMsg)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func expectOutput(t *testing.T, f func(), expected string) {
|
func expectOutput(t *testing.T, f func(), expected string) {
|
||||||
|
|||||||
Reference in New Issue
Block a user