Files
crawlab/backend/services/log_test.go
yaziming e10d8fd996 refactor(backend): Use more efficient bytes to string methods and remove unnecessary type conversions
detail:
    1. add utils.BytesToString function instead of string() convert bytes to string.
    2. use bytes.NewReader instead of strings.NewReader(string(sb)).
    3. use w.Body.Bytes() instead of []byte(w.Body.String()).
2019-09-03 15:17:32 +08:00

52 lines
1.0 KiB
Go

package services
import (
"crawlab/config"
"crawlab/utils"
"fmt"
"github.com/apex/log"
. "github.com/smartystreets/goconvey/convey"
"github.com/spf13/viper"
"os"
"testing"
)
func TestDeleteLogPeriodically(t *testing.T) {
Convey("Test DeleteLogPeriodically", t, func() {
if err := config.InitConfig("../conf/config.yml"); err != nil {
log.Error("init config error:" + err.Error())
panic(err)
}
log.Info("初始化配置成功")
logDir := viper.GetString("log.path")
log.Info(logDir)
DeleteLogPeriodically()
})
}
func TestGetLocalLog(t *testing.T) {
//create a log file for test
logPath := "../logs/crawlab/test.log"
f, err := os.Create(logPath)
defer f.Close()
if err != nil {
fmt.Println(err.Error())
} else {
_, err = f.WriteString("This is for test")
}
Convey("Test GetLocalLog", t, func() {
Convey("Test response", func() {
logStr, err := GetLocalLog(logPath)
log.Info(utils.BytesToString(logStr))
fmt.Println(err)
So(err, ShouldEqual, nil)
})
})
//delete the test log file
os.Remove(logPath)
}