2021-07-13 08:32:39 +00:00
|
|
|
package logx
|
2016-04-05 06:50:08 +00:00
|
|
|
|
|
|
|
import (
|
2021-11-06 09:57:34 +00:00
|
|
|
"errors"
|
2017-01-19 09:08:12 +00:00
|
|
|
"testing"
|
|
|
|
|
2016-05-03 01:56:59 +00:00
|
|
|
"github.com/appleboy/gorush/config"
|
2021-07-13 08:32:39 +00:00
|
|
|
"github.com/appleboy/gorush/core"
|
|
|
|
|
2017-06-24 18:23:24 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2016-04-05 07:04:50 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2016-04-05 06:50:08 +00:00
|
|
|
)
|
|
|
|
|
2022-12-20 14:03:29 +00:00
|
|
|
var invalidLevel = "invalid"
|
|
|
|
|
2016-04-05 06:50:08 +00:00
|
|
|
func TestSetLogLevel(t *testing.T) {
|
|
|
|
log := logrus.New()
|
|
|
|
|
|
|
|
err := SetLogLevel(log, "debug")
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
2022-12-20 14:03:29 +00:00
|
|
|
err = SetLogLevel(log, invalidLevel)
|
2016-04-05 06:50:08 +00:00
|
|
|
assert.Equal(t, "not a valid logrus Level: \"invalid\"", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSetLogOut(t *testing.T) {
|
|
|
|
log := logrus.New()
|
|
|
|
|
|
|
|
err := SetLogOut(log, "stdout")
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
err = SetLogOut(log, "stderr")
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
2016-04-07 06:42:38 +00:00
|
|
|
err = SetLogOut(log, "log/access.log")
|
2016-04-05 06:50:08 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
// missing create logs folder.
|
|
|
|
err = SetLogOut(log, "logs/access.log")
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInitDefaultLog(t *testing.T) {
|
2021-07-16 04:10:34 +00:00
|
|
|
cfg, _ := config.LoadConf()
|
2016-04-05 06:50:08 +00:00
|
|
|
|
|
|
|
// no errors on default config
|
2021-07-13 08:32:39 +00:00
|
|
|
assert.Nil(t, InitLog(
|
2021-07-16 04:10:34 +00:00
|
|
|
cfg.Log.AccessLevel,
|
|
|
|
cfg.Log.AccessLog,
|
|
|
|
cfg.Log.ErrorLevel,
|
|
|
|
cfg.Log.ErrorLog,
|
2021-07-13 08:32:39 +00:00
|
|
|
))
|
2016-04-05 06:50:08 +00:00
|
|
|
|
2022-12-20 14:03:29 +00:00
|
|
|
cfg.Log.AccessLevel = invalidLevel
|
2016-04-05 06:50:08 +00:00
|
|
|
|
2021-07-13 08:32:39 +00:00
|
|
|
assert.NotNil(t, InitLog(
|
2021-07-16 04:10:34 +00:00
|
|
|
cfg.Log.AccessLevel,
|
|
|
|
cfg.Log.AccessLog,
|
|
|
|
cfg.Log.ErrorLevel,
|
|
|
|
cfg.Log.ErrorLog,
|
2021-07-13 08:32:39 +00:00
|
|
|
))
|
2021-11-06 09:57:34 +00:00
|
|
|
|
|
|
|
isTerm = true
|
|
|
|
|
|
|
|
assert.NotNil(t, InitLog(
|
|
|
|
cfg.Log.AccessLevel,
|
|
|
|
cfg.Log.AccessLog,
|
|
|
|
cfg.Log.ErrorLevel,
|
|
|
|
cfg.Log.ErrorLog,
|
|
|
|
))
|
2016-04-05 06:50:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAccessLevel(t *testing.T) {
|
2021-07-16 04:10:34 +00:00
|
|
|
cfg, _ := config.LoadConf()
|
2016-04-05 06:50:08 +00:00
|
|
|
|
2022-12-20 14:03:29 +00:00
|
|
|
cfg.Log.AccessLevel = invalidLevel
|
2016-04-05 06:50:08 +00:00
|
|
|
|
2021-07-13 08:32:39 +00:00
|
|
|
assert.NotNil(t, InitLog(
|
2021-07-16 04:10:34 +00:00
|
|
|
cfg.Log.AccessLevel,
|
|
|
|
cfg.Log.AccessLog,
|
|
|
|
cfg.Log.ErrorLevel,
|
|
|
|
cfg.Log.ErrorLog,
|
2021-07-13 08:32:39 +00:00
|
|
|
))
|
2016-04-05 06:50:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestErrorLevel(t *testing.T) {
|
2021-07-16 04:10:34 +00:00
|
|
|
cfg, _ := config.LoadConf()
|
2016-04-05 06:50:08 +00:00
|
|
|
|
2022-12-20 14:03:29 +00:00
|
|
|
cfg.Log.ErrorLevel = invalidLevel
|
2016-04-05 06:50:08 +00:00
|
|
|
|
2021-07-13 08:32:39 +00:00
|
|
|
assert.NotNil(t, InitLog(
|
2021-07-16 04:10:34 +00:00
|
|
|
cfg.Log.AccessLevel,
|
|
|
|
cfg.Log.AccessLog,
|
|
|
|
cfg.Log.ErrorLevel,
|
|
|
|
cfg.Log.ErrorLog,
|
2021-07-13 08:32:39 +00:00
|
|
|
))
|
2016-04-05 06:50:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAccessLogPath(t *testing.T) {
|
2021-07-16 04:10:34 +00:00
|
|
|
cfg, _ := config.LoadConf()
|
2016-04-05 06:50:08 +00:00
|
|
|
|
2021-07-16 04:10:34 +00:00
|
|
|
cfg.Log.AccessLog = "logs/access.log"
|
2016-04-05 06:50:08 +00:00
|
|
|
|
2021-07-13 08:32:39 +00:00
|
|
|
assert.NotNil(t, InitLog(
|
2021-07-16 04:10:34 +00:00
|
|
|
cfg.Log.AccessLevel,
|
|
|
|
cfg.Log.AccessLog,
|
|
|
|
cfg.Log.ErrorLevel,
|
|
|
|
cfg.Log.ErrorLog,
|
2021-07-13 08:32:39 +00:00
|
|
|
))
|
2016-04-05 06:50:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestErrorLogPath(t *testing.T) {
|
2021-07-16 04:10:34 +00:00
|
|
|
cfg, _ := config.LoadConf()
|
2016-04-05 06:50:08 +00:00
|
|
|
|
2021-07-16 04:10:34 +00:00
|
|
|
cfg.Log.ErrorLog = "logs/error.log"
|
2016-04-05 06:50:08 +00:00
|
|
|
|
2021-07-13 08:32:39 +00:00
|
|
|
assert.NotNil(t, InitLog(
|
2021-07-16 04:10:34 +00:00
|
|
|
cfg.Log.AccessLevel,
|
|
|
|
cfg.Log.AccessLog,
|
|
|
|
cfg.Log.ErrorLevel,
|
|
|
|
cfg.Log.ErrorLog,
|
2021-07-13 08:32:39 +00:00
|
|
|
))
|
2016-04-05 06:50:08 +00:00
|
|
|
}
|
2016-04-08 11:31:47 +00:00
|
|
|
|
|
|
|
func TestPlatFormType(t *testing.T) {
|
2021-07-13 08:32:39 +00:00
|
|
|
assert.Equal(t, "ios", typeForPlatForm(core.PlatFormIos))
|
|
|
|
assert.Equal(t, "android", typeForPlatForm(core.PlatFormAndroid))
|
2021-11-06 09:57:34 +00:00
|
|
|
assert.Equal(t, "huawei", typeForPlatForm(core.PlatFormHuawei))
|
2016-04-08 11:31:47 +00:00
|
|
|
assert.Equal(t, "", typeForPlatForm(10000))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPlatFormColor(t *testing.T) {
|
2021-07-13 08:32:39 +00:00
|
|
|
assert.Equal(t, blue, colorForPlatForm(core.PlatFormIos))
|
|
|
|
assert.Equal(t, yellow, colorForPlatForm(core.PlatFormAndroid))
|
2021-11-06 09:57:34 +00:00
|
|
|
assert.Equal(t, green, colorForPlatForm(core.PlatFormHuawei))
|
2016-04-08 11:31:47 +00:00
|
|
|
assert.Equal(t, reset, colorForPlatForm(1000000))
|
|
|
|
}
|
2016-05-16 12:36:56 +00:00
|
|
|
|
|
|
|
func TestHideToken(t *testing.T) {
|
|
|
|
assert.Equal(t, "", hideToken("", 2))
|
|
|
|
assert.Equal(t, "**345678**", hideToken("1234567890", 2))
|
2016-05-16 13:04:05 +00:00
|
|
|
assert.Equal(t, "*****", hideToken("12345", 10))
|
2016-05-16 12:36:56 +00:00
|
|
|
}
|
2021-11-06 09:57:34 +00:00
|
|
|
|
|
|
|
func TestLogPushEntry(t *testing.T) {
|
|
|
|
in := InputLog{}
|
|
|
|
|
|
|
|
in.Platform = 1
|
|
|
|
assert.Equal(t, "ios", GetLogPushEntry(&in).Platform)
|
|
|
|
|
|
|
|
in.Error = errors.New("error")
|
|
|
|
assert.Equal(t, "error", GetLogPushEntry(&in).Error)
|
|
|
|
|
|
|
|
in.Token = "1234567890"
|
|
|
|
in.HideToken = true
|
|
|
|
assert.Equal(t, "**********", GetLogPushEntry(&in).Token)
|
2023-02-21 03:00:24 +00:00
|
|
|
|
|
|
|
in.Message = "hellothisisamessage"
|
|
|
|
in.HideMessage = true
|
|
|
|
assert.Equal(t, "(message redacted)", GetLogPushEntry(&in).Message)
|
2021-11-06 09:57:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestLogPush(t *testing.T) {
|
|
|
|
in := InputLog{}
|
|
|
|
isTerm = true
|
|
|
|
|
|
|
|
in.Format = "json"
|
|
|
|
in.Status = "succeeded-push"
|
|
|
|
assert.Equal(t, "succeeded-push", LogPush(&in).Type)
|
|
|
|
|
|
|
|
in.Format = ""
|
|
|
|
in.Message = "success"
|
|
|
|
assert.Equal(t, "success", LogPush(&in).Message)
|
|
|
|
|
|
|
|
in.Status = "failed-push"
|
|
|
|
in.Message = "failed"
|
|
|
|
assert.Equal(t, "failed", LogPush(&in).Message)
|
|
|
|
}
|