-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcloudlog_test.go
98 lines (79 loc) · 2.03 KB
/
cloudlog_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
package cloudlog
import (
"encoding/json"
"errors"
"io"
"net/http"
"strings"
"testing"
"time"
"github.com/stretchr/testify/require"
)
func MockOptionWithError(_ *CloudLog) error {
return errors.New("mock option error")
}
type MockClient struct {
m map[string]interface{}
}
func (mc *MockClient) Do(req *http.Request) (resp *http.Response, err error) {
b, err := io.ReadAll(req.Body)
if err != nil {
return
}
var m map[string]interface{}
err = json.Unmarshal(b, &m)
if err != nil {
return
}
resp = &http.Response{
Body: io.NopCloser(strings.NewReader("")),
}
resp.StatusCode = 201
mc.m = m
return
}
func TestPushSimpleEvent(t *testing.T) {
req := require.New(t)
conf := NewDefaultConfig()
mc := &MockClient{}
conf.Client = mc
conf.Hostname = "test-host"
cl, err := NewCloudlogWithConfig("abc123", "token", conf)
req.NoError(err)
//simple event
err = cl.PushEvent("test message")
req.NoError(err)
m := mc.m
records := m["records"].([]interface{})
req.Equal(1, len(records))
record1 := records[0].(map[string]interface{})
req.Equal("test message", record1["message"])
req.Equal("test-host", record1["cloudlog_source_host"])
req.Equal("go-client-rest", record1["cloudlog_client_type"])
req.InDelta(time.Now().Unix()*1000, record1["timestamp"], 1000)
}
func TestPushEvent(t *testing.T) {
req := require.New(t)
conf := NewDefaultConfig()
mc := &MockClient{}
conf.Client = mc
conf.Hostname = "test-host"
cl, err := NewCloudlogWithConfig("abc123", "token", conf)
req.NoError(err)
//simple event
input := map[string]interface{}{
"message": "test message",
"value": 1,
}
err = cl.PushEvent(input)
req.NoError(err)
m := mc.m
records := m["records"].([]interface{})
req.Equal(1, len(records))
record1 := records[0].(map[string]interface{})
req.Equal("test message", record1["message"])
req.Equal(1.0, record1["value"])
req.Equal("test-host", record1["cloudlog_source_host"])
req.Equal("go-client-rest", record1["cloudlog_client_type"])
req.InDelta(time.Now().Unix()*1000, record1["timestamp"], 1000)
}