2019-09-03 21:28:06 +00:00
|
|
|
package sshlog
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2020-04-29 20:51:32 +00:00
|
|
|
"github.com/cloudflare/cloudflared/logger"
|
2019-09-03 21:28:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const logFileName = "test-logger.log"
|
|
|
|
|
|
|
|
func createLogger(t *testing.T) *Logger {
|
|
|
|
os.Remove(logFileName)
|
2020-04-29 20:51:32 +00:00
|
|
|
l := logger.NewOutputWriter(logger.NewMockWriteManager())
|
2019-09-03 21:28:06 +00:00
|
|
|
logger, err := NewLogger(logFileName, l, time.Millisecond, 1024)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("couldn't create the logger!", err)
|
|
|
|
}
|
|
|
|
return logger
|
|
|
|
}
|
2020-04-29 20:51:32 +00:00
|
|
|
|
2019-09-30 20:44:23 +00:00
|
|
|
// AUTH-2115 TODO: fix this test
|
|
|
|
//func TestWrite(t *testing.T) {
|
|
|
|
// testStr := "hi"
|
|
|
|
// logger := createLogger(t)
|
|
|
|
// defer func() {
|
|
|
|
// logger.Close()
|
|
|
|
// os.Remove(logFileName)
|
|
|
|
// }()
|
|
|
|
//
|
|
|
|
// logger.Write([]byte(testStr))
|
2020-04-30 05:02:08 +00:00
|
|
|
// time.DelayBeforeReconnect(2 * time.Millisecond)
|
2019-09-30 20:44:23 +00:00
|
|
|
// data, err := ioutil.ReadFile(logFileName)
|
|
|
|
// if err != nil {
|
|
|
|
// t.Fatal("couldn't read the log file!", err)
|
|
|
|
// }
|
|
|
|
// checkStr := string(data)
|
|
|
|
// if checkStr != testStr {
|
|
|
|
// t.Fatal("file data doesn't match!")
|
|
|
|
// }
|
|
|
|
//}
|
2019-09-03 21:28:06 +00:00
|
|
|
|
|
|
|
func TestFilenameRotation(t *testing.T) {
|
|
|
|
newName := rotationName("dir/bob/acoolloggername.log")
|
|
|
|
|
|
|
|
dir := filepath.Dir(newName)
|
|
|
|
if dir != "dir/bob" {
|
|
|
|
t.Fatal("rotation name doesn't respect the directory filepath:", newName)
|
|
|
|
}
|
|
|
|
|
|
|
|
filename := filepath.Base(newName)
|
|
|
|
if !strings.HasPrefix(filename, "acoolloggername") {
|
|
|
|
t.Fatal("rotation filename is wrong:", filename)
|
|
|
|
}
|
|
|
|
|
|
|
|
ext := filepath.Ext(newName)
|
|
|
|
if ext != ".log" {
|
|
|
|
t.Fatal("rotation file extension is wrong:", ext)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRotation(t *testing.T) {
|
|
|
|
logger := createLogger(t)
|
|
|
|
|
|
|
|
for i := 0; i < 2000; i++ {
|
|
|
|
logger.Write([]byte("a string for testing rotation\n"))
|
|
|
|
}
|
|
|
|
logger.Close()
|
|
|
|
|
|
|
|
count := 0
|
|
|
|
filepath.Walk(".", func(path string, info os.FileInfo, err error) error {
|
|
|
|
if err != nil || info.IsDir() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(info.Name(), "test-logger") {
|
|
|
|
log.Println("deleting: ", path)
|
|
|
|
os.Remove(path)
|
|
|
|
count++
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if count < 2 {
|
|
|
|
t.Fatal("rotation didn't roll files:", count)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|