Skip to content

Commit

Permalink
test: use file log in unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
huskar-t committed Nov 29, 2024
1 parent c7dfad4 commit 020b8ac
Show file tree
Hide file tree
Showing 20 changed files with 61 additions and 26 deletions.
2 changes: 1 addition & 1 deletion controller/ws/query/ws_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ func TestMain(m *testing.M) {
viper.Set("logLevel", "trace")
viper.Set("uploadKeeper.enable", false)
config.Init()
db.PrepareConnection()
log.ConfigLog()
db.PrepareConnection()
gin.SetMode(gin.ReleaseMode)
router = gin.New()
controllers := controller.GetControllers()
Expand Down
2 changes: 1 addition & 1 deletion controller/ws/schemaless/schemaless_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ func TestMain(m *testing.M) {
viper.Set("logLevel", "trace")
viper.Set("uploadKeeper.enable", false)
config.Init()
db.PrepareConnection()
log.ConfigLog()
db.PrepareConnection()
gin.SetMode(gin.ReleaseMode)
router = gin.New()
controllers := controller.GetControllers()
Expand Down
2 changes: 1 addition & 1 deletion controller/ws/stmt/stmt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ func TestMain(m *testing.M) {
viper.Set("logLevel", "trace")
viper.Set("uploadKeeper.enable", false)
config.Init()
db.PrepareConnection()
log.ConfigLog()
db.PrepareConnection()
gin.SetMode(gin.ReleaseMode)
router = gin.New()
controllers := controller.GetControllers()
Expand Down
2 changes: 1 addition & 1 deletion controller/ws/tmq/tmq_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ func TestMain(m *testing.M) {
viper.Set("logLevel", "trace")
viper.Set("uploadKeeper.enable", false)
config.Init()
db.PrepareConnection()
log.ConfigLog()
db.PrepareConnection()
gin.SetMode(gin.ReleaseMode)
router = gin.New()
controllers := controller.GetControllers()
Expand Down
2 changes: 1 addition & 1 deletion controller/ws/ws/ws_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ func TestMain(m *testing.M) {
viper.Set("logLevel", "trace")
viper.Set("uploadKeeper.enable", false)
config.Init()
db.PrepareConnection()
log.ConfigLog()
db.PrepareConnection()
gin.SetMode(gin.ReleaseMode)
router = gin.New()
controllers := controller.GetControllers()
Expand Down
4 changes: 2 additions & 2 deletions controller/ws/wstool/error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import (

"github.com/gin-gonic/gin"
"github.com/gorilla/websocket"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
tErrors "github.com/taosdata/driver-go/v3/errors"
"github.com/taosdata/taosadapter/v3/log"
"github.com/taosdata/taosadapter/v3/tools/melody"
)

Expand All @@ -27,7 +27,7 @@ func TestWSError(t *testing.T) {
ErrStr: "test error",
}
commonErr := errors.New("test common error")
logger := logrus.New().WithField("test", "TestWSError")
logger := log.GetLogger("test").WithField("test", "TestWSError")
m.HandleMessage(func(session *melody.Session, data []byte) {
switch data[0] {
case '1':
Expand Down
6 changes: 3 additions & 3 deletions controller/ws/wstool/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"time"

"github.com/gorilla/websocket"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/taosdata/taosadapter/v3/log"
"github.com/taosdata/taosadapter/v3/tools/melody"
)

Expand All @@ -21,15 +21,15 @@ func TestGetDuration(t *testing.T) {
}

func TestGetLogger(t *testing.T) {
logger := logrus.New()
logger := log.GetLogger("test")
session := &melody.Session{}
session.Set("logger", logger.WithField("test_field", "test_value"))
entry := GetLogger(session)
assert.Equal(t, "test_value", entry.Data["test_field"])
}

func TestLogWSError(t *testing.T) {
logger := logrus.New()
logger := log.GetLogger("test")
session := &melody.Session{}
session.Set("logger", logger.WithField("test_field", "test_value"))
LogWSError(session, nil)
Expand Down
17 changes: 17 additions & 0 deletions controller/ws/wstool/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package wstool

import (
"testing"

"github.com/spf13/viper"
"github.com/taosdata/taosadapter/v3/config"
"github.com/taosdata/taosadapter/v3/log"
)

func TestMain(m *testing.M) {
viper.Set("logLevel", "trace")
viper.Set("uploadKeeper.enable", false)
config.Init()
log.ConfigLog()
m.Run()
}
4 changes: 2 additions & 2 deletions controller/ws/wstool/resp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (

"github.com/gin-gonic/gin"
"github.com/gorilla/websocket"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/taosdata/taosadapter/v3/log"
"github.com/taosdata/taosadapter/v3/tools/melody"
)

Expand All @@ -23,7 +23,7 @@ func TestWSWriteJson(t *testing.T) {
Version: "1.0.0",
}
m.HandleMessage(func(session *melody.Session, _ []byte) {
logger := logrus.New().WithField("test", "TestWSWriteJson")
logger := log.GetLogger("test").WithField("test", "TestWSWriteJson")
session.Set("logger", logger)
WSWriteJson(session, logger, data)
})
Expand Down
2 changes: 1 addition & 1 deletion db/async/row_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func TestAsync_TaosExec(t *testing.T) {
wantErr: true,
},
}
var logger = logrus.New().WithField("test", "async_test")
var logger = log.GetLogger("test").WithField("test", "async_test")
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
a := &Async{
Expand Down
1 change: 1 addition & 0 deletions db/syncinterface/wrapper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const isDebug = true

func TestMain(m *testing.M) {
config.Init()
log.ConfigLog()
_ = log.SetLevel("trace")
db.PrepareConnection()
m.Run()
Expand Down
9 changes: 5 additions & 4 deletions db/tool/createdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,20 @@ import (
"testing"
"unsafe"

"github.com/sirupsen/logrus"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
tErrors "github.com/taosdata/driver-go/v3/errors"
"github.com/taosdata/driver-go/v3/wrapper"
"github.com/taosdata/taosadapter/v3/config"
"github.com/taosdata/taosadapter/v3/db"
"github.com/taosdata/taosadapter/v3/log"
)

func TestMain(m *testing.M) {
viper.Set("smlAutoCreateDB", true)
viper.Set("logLevel", "trace")
config.Init()
log.ConfigLog()
db.PrepareConnection()
m.Run()
viper.Set("smlAutoCreateDB", false)
Expand All @@ -26,7 +28,6 @@ func TestMain(m *testing.M) {
// @date: 2021/12/14 15:05
// @description: test creat database with connection
func TestCreateDBWithConnection(t *testing.T) {
db.PrepareConnection()
conn, err := wrapper.TaosConnect("", "root", "taosdata", "", 0)
if err != nil {
t.Error(err)
Expand All @@ -52,7 +53,7 @@ func TestCreateDBWithConnection(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := CreateDBWithConnection(tt.args.connection, logrus.New().WithField("test", "TestCreateDBWithConnection"), false, tt.args.db, 0); (err != nil) != tt.wantErr {
if err := CreateDBWithConnection(tt.args.connection, log.GetLogger("test").WithField("test", "TestCreateDBWithConnection"), false, tt.args.db, 0); (err != nil) != tt.wantErr {
t.Errorf("CreateDBWithConnection() error = %v, wantErr %v", err, tt.wantErr)
}
code := wrapper.TaosSelectDB(tt.args.connection, tt.args.db)
Expand Down Expand Up @@ -107,7 +108,7 @@ func TestSchemalessSelectDB(t *testing.T) {
return
}
wrapper.TaosFreeResult(result)
if err := SchemalessSelectDB(tt.args.taosConnect, logrus.New().WithField("test", "TestSchemalessSelectDB"), false, tt.args.db, 0); (err != nil) != tt.wantErr {
if err := SchemalessSelectDB(tt.args.taosConnect, log.GetLogger("test").WithField("test", "TestSchemalessSelectDB"), false, tt.args.db, 0); (err != nil) != tt.wantErr {
t.Errorf("selectDB() error = %v, wantErr %v", err, tt.wantErr)
}
r := wrapper.TaosQuery(tt.args.taosConnect, fmt.Sprintf("drop database if exists %s", tt.args.db))
Expand Down
4 changes: 3 additions & 1 deletion plugin/influxdb/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/taosdata/driver-go/v3/wrapper"
"github.com/taosdata/taosadapter/v3/config"
"github.com/taosdata/taosadapter/v3/db"
"github.com/taosdata/taosadapter/v3/log"
)

// @author: xftan
Expand All @@ -28,8 +29,9 @@ func TestInfluxdb(t *testing.T) {
rand.Seed(time.Now().UnixNano())
viper.Set("smlAutoCreateDB", true)
defer viper.Set("smlAutoCreateDB", false)
config.Init()
viper.Set("influxdb.enable", true)
config.Init()
log.ConfigLog()
db.PrepareConnection()
p := Influxdb{}
router := gin.Default()
Expand Down
2 changes: 2 additions & 0 deletions plugin/nodeexporter/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/taosdata/driver-go/v3/af"
"github.com/taosdata/taosadapter/v3/config"
"github.com/taosdata/taosadapter/v3/db"
"github.com/taosdata/taosadapter/v3/log"
)

var s = `
Expand All @@ -37,6 +38,7 @@ test_metric{label="value"} 1.0 1490802350000
// @description: test node-exporter plugin
func TestNodeExporter_Gather(t *testing.T) {
config.Init()
log.ConfigLog()
db.PrepareConnection()
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, err := w.Write([]byte(s))
Expand Down
2 changes: 2 additions & 0 deletions plugin/opentsdb/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/taosdata/driver-go/v3/wrapper"
"github.com/taosdata/taosadapter/v3/config"
"github.com/taosdata/taosadapter/v3/db"
"github.com/taosdata/taosadapter/v3/log"
)

// @author: xftan
Expand All @@ -30,6 +31,7 @@ func TestOpentsdb(t *testing.T) {
defer viper.Set("smlAutoCreateDB", false)
config.Init()
viper.Set("opentsdb.enable", true)
log.ConfigLog()
db.PrepareConnection()

p := Plugin{}
Expand Down
2 changes: 2 additions & 0 deletions plugin/opentsdbtelnet/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/taosdata/driver-go/v3/wrapper"
"github.com/taosdata/taosadapter/v3/config"
"github.com/taosdata/taosadapter/v3/db"
"github.com/taosdata/taosadapter/v3/log"
"github.com/taosdata/taosadapter/v3/plugin/opentsdbtelnet"
)

Expand All @@ -26,6 +27,7 @@ func TestPlugin(t *testing.T) {
rand.Seed(time.Now().UnixNano())
p := &opentsdbtelnet.Plugin{}
config.Init()
log.ConfigLog()
db.PrepareConnection()
viper.Set("opentsdb_telnet.enable", true)
viper.Set("opentsdb_telnet.batchSize", 1)
Expand Down
2 changes: 2 additions & 0 deletions plugin/prometheus/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ import (
"github.com/taosdata/driver-go/v3/wrapper"
"github.com/taosdata/taosadapter/v3/config"
"github.com/taosdata/taosadapter/v3/db"
"github.com/taosdata/taosadapter/v3/log"
)

func TestMain(m *testing.M) {
//nolint:staticcheck
rand.Seed(time.Now().UnixNano())
config.Init()
viper.Set("prometheus.enable", true)
log.ConfigLog()
db.PrepareConnection()
conn, err := wrapper.TaosConnect("", "root", "taosdata", "", 0)
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions plugin/statsd/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/taosdata/driver-go/v3/wrapper"
"github.com/taosdata/taosadapter/v3/config"
"github.com/taosdata/taosadapter/v3/db"
"github.com/taosdata/taosadapter/v3/log"
)

// @author: xftan
Expand All @@ -25,6 +26,7 @@ func TestStatsd(t *testing.T) {
rand.Seed(time.Now().UnixNano())
p := &Plugin{}
config.Init()
log.ConfigLog()
db.PrepareConnection()
viper.Set("statsd.gatherInterval", time.Millisecond)
viper.Set("statsd.enable", true)
Expand Down
4 changes: 2 additions & 2 deletions schemaless/capi/influxdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"testing"
"unsafe"

"github.com/sirupsen/logrus"
"github.com/taosdata/driver-go/v3/errors"
"github.com/taosdata/driver-go/v3/wrapper"
"github.com/taosdata/taosadapter/v3/log"
"github.com/taosdata/taosadapter/v3/schemaless/capi"
)

Expand Down Expand Up @@ -79,7 +79,7 @@ func TestInsertInfluxdb(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
logger := logrus.New().WithField("test", "TestInsertInfluxdb")
logger := log.GetLogger("test").WithField("test", "TestInsertInfluxdb").WithField("name", tt.name)
err := capi.InsertInfluxdb(tt.args.taosConnect, tt.args.data, tt.args.db, tt.args.precision, tt.args.ttl, 0, "", logger)
if (err != nil) != tt.wantErr {
t.Errorf("InsertInfluxdb() error = %v, wantErr %v", err, tt.wantErr)
Expand Down
16 changes: 10 additions & 6 deletions schemaless/capi/opentsdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,20 @@ import (
"time"
"unsafe"

"github.com/sirupsen/logrus"
"github.com/spf13/viper"
"github.com/taosdata/driver-go/v3/errors"
"github.com/taosdata/driver-go/v3/wrapper"
"github.com/taosdata/taosadapter/v3/config"
"github.com/taosdata/taosadapter/v3/db"
"github.com/taosdata/taosadapter/v3/log"
"github.com/taosdata/taosadapter/v3/schemaless/capi"
)

func TestMain(m *testing.M) {
viper.Set("logLevel", "trace")
viper.Set("uploadKeeper.enable", false)
config.Init()
log.ConfigLog()
db.PrepareConnection()
m.Run()
}
Expand Down Expand Up @@ -94,10 +98,10 @@ func TestInsertOpentsdbTelnet(t *testing.T) {
wantErr: false,
},
}
logger := logrus.New().WithField("test", "TestInsertOpentsdbTelnet")
logger := log.GetLogger("test").WithField("test", "TestInsertOpentsdbTelnet")
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := capi.InsertOpentsdbTelnet(tt.args.taosConnect, []string{tt.args.data}, tt.args.db, tt.args.ttl, 0, "", logger); (err != nil) != tt.wantErr {
if err := capi.InsertOpentsdbTelnet(tt.args.taosConnect, []string{tt.args.data}, tt.args.db, tt.args.ttl, 0, "", logger.WithField("name", tt.name)); (err != nil) != tt.wantErr {
t.Errorf("InsertOpentsdbTelnet() error = %v, wantErr %v", err, tt.wantErr)
}
})
Expand All @@ -120,7 +124,7 @@ func BenchmarkTelnet(b *testing.B) {
}
wrapper.TaosFreeResult(r)
}()
logger := logrus.New().WithField("test", "BenchmarkTelnet")
logger := log.GetLogger("test").WithField("test", "BenchmarkTelnet")
for i := 0; i < b.N; i++ {
//`sys.if.bytes.out`,`host`=web01,`interface`=eth0
//t_98df8453856519710bfc2f1b5f8202cf
Expand Down Expand Up @@ -234,7 +238,7 @@ func TestInsertOpentsdbJson(t *testing.T) {
wantErr: false,
},
}
logger := logrus.New().WithField("test", "TestInsertOpentsdbJson")
logger := log.GetLogger("test").WithField("test", "TestInsertOpentsdbJson")
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := capi.InsertOpentsdbJson(tt.args.taosConnect, tt.args.data, tt.args.db, tt.args.ttl, 0, "", logger); (err != nil) != tt.wantErr {
Expand Down Expand Up @@ -292,7 +296,7 @@ func TestInsertOpentsdbTelnetBatch(t *testing.T) {
wantErr: false,
},
}
logger := logrus.New().WithField("test", "TestInsertOpentsdbTelnetBatch")
logger := log.GetLogger("test").WithField("test", "TestInsertOpentsdbTelnetBatch")
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := capi.InsertOpentsdbTelnet(tt.args.taosConnect, tt.args.data, tt.args.db, tt.args.ttl, 0, "", logger); (err != nil) != tt.wantErr {
Expand Down

0 comments on commit 020b8ac

Please sign in to comment.