-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcls_logger_test.go
158 lines (143 loc) · 4.33 KB
/
cls_logger_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
//
//
// Tencent is pleased to support the open source community by making tRPC available.
//
// Copyright (C) 2023 THL A29 Limited, a Tencent company.
// All rights reserved.
//
// If you have downloaded a copy of the tRPC source code from Tencent,
// please note that tRPC source code is licensed under the Apache 2.0 License,
// A copy of the Apache 2.0 License is included in this file.
//
//
package cls
import (
"reflect"
"testing"
"github.com/agiledragon/gomonkey/v2"
cloudsdk "github.com/tencentcloud/tencentcloud-cls-sdk-go"
"gopkg.in/yaml.v3"
"trpc.group/trpc-go/trpc-go/errs"
"trpc.group/trpc-go/trpc-go/log"
)
func TestSetup(t *testing.T) {
p := &LoggerPlugin{}
err := p.Setup("", nil)
if err == nil {
t.Errorf("setup err:%+v", err)
}
patch := gomonkey.ApplyMethod(reflect.TypeOf(p), "SetupCls", func(*LoggerPlugin, *log.OutputConfig) (*Logger, error) {
return &Logger{}, nil
})
err = p.Setup("", &log.Decoder{
OutputConfig: &log.OutputConfig{
FormatConfig: log.FormatConfig{},
RemoteConfig: yaml.Node{
Kind: yaml.ScalarNode,
},
},
})
if err != nil {
t.Errorf("setup err:%+v", err)
}
patch.Reset()
producerConfig := cloudsdk.GetDefaultAsyncProducerClientConfig()
producerConfig.Endpoint = "ap-guangzhou.cls.tencentcs.com"
producerConfig.AccessKeyID = "11"
producerConfig.AccessKeySecret = "11"
producerInstance, err := cloudsdk.NewAsyncProducerClient(producerConfig)
if err != nil {
t.Error(err)
}
patch = gomonkey.ApplyFunc(cloudsdk.NewAsyncProducerClient, func(*cloudsdk.AsyncProducerClientConfig) (*cloudsdk.AsyncProducerClient, error) {
return &cloudsdk.AsyncProducerClient{}, nil
})
patch = gomonkey.ApplyMethod(reflect.TypeOf(producerInstance), "Start", func(*cloudsdk.AsyncProducerClient) {
return
})
_, err = p.SetupCls(&log.OutputConfig{
FormatConfig: log.FormatConfig{},
RemoteConfig: yaml.Node{
Kind: yaml.ScalarNode,
},
})
if err != nil {
t.Errorf("setupCls err:%+v", err)
}
patch.Reset()
_ = p.Type()
}
func TestWrite(t *testing.T) {
log := &Logger{}
producerConfig := cloudsdk.GetDefaultAsyncProducerClientConfig()
producerConfig.Endpoint = "ap-guangzhou.cls.tencentcs.com"
producerConfig.AccessKeyID = "11"
producerConfig.AccessKeySecret = "11"
producerInstance, err := cloudsdk.NewAsyncProducerClient(producerConfig)
if err != nil {
t.Error(err)
}
log.client = producerInstance
patch := gomonkey.ApplyMethod(reflect.TypeOf(log.client), "SendLog", func(*cloudsdk.AsyncProducerClient, string, *cloudsdk.Log, cloudsdk.CallBack) error {
return nil
})
info := []byte(`{"L":"INFO","T":"2020-12-08 11:32:48.051","C":"app/main.go:37","M":"test info:"}`)
n, err := log.Write(info)
if err != nil {
t.Errorf("Write err:%+v", err)
}
if n <= 0 {
t.Errorf("Write err")
}
patch.Reset()
patch = gomonkey.ApplyMethod(reflect.TypeOf(log.client), "SendLog", func(*cloudsdk.AsyncProducerClient, string, *cloudsdk.Log, cloudsdk.CallBack) error {
return errs.New(1, "Over producer set maximum blocking time")
})
info1 := []byte(`{"L":"INFO","Time":"2020-12-08 11:32:48.051","C":"app/main.go:37","M":"test info"}`)
_, err = log.Write(info1)
if err == nil {
t.Errorf("Write err:%+v", err)
}
infoe2 := []byte(`{}`)
_, err = log.Write(infoe2)
if err != nil {
t.Errorf("Write err:%+v", err)
}
infoe3 := []byte(``)
_, err = log.Write(infoe3)
if err != nil {
t.Errorf("Write err:%+v", err)
}
}
func TestDefaultGetReportCLSField(t *testing.T) {
cfg := Config{
FieldMap: map[string]string{
"Level": "level",
},
}
tests := []struct {
name string
sourceField string
wantReportField string
wantNeedReport bool
}{
{name: "remap field", sourceField: "Level", wantReportField: "level", wantNeedReport: true},
{name: "no remap", sourceField: "field", wantReportField: "field", wantNeedReport: true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotReportField, gotNeedReport := GetReportCLSField(tt.sourceField, &cfg)
if gotReportField != tt.wantReportField {
t.Errorf("GetReportCLSField() gotReportField = %v, want %v", gotReportField, tt.wantReportField)
}
if gotNeedReport != tt.wantNeedReport {
t.Errorf("GetReportCLSField() gotNeedReport = %v, want %v", gotNeedReport, tt.wantNeedReport)
}
})
}
}
func TestCallback(t *testing.T) {
var c Callback
c.Success(&cloudsdk.Result{})
c.Fail(&cloudsdk.Result{})
}