forked from galaxydi/go-loghub
-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathclient_store_test.go
146 lines (133 loc) · 3.75 KB
/
client_store_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
package sls
import (
"fmt"
"os"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
)
func TestLostore(t *testing.T) {
suite.Run(t, new(LostoreTestSuite))
}
type LostoreTestSuite struct {
suite.Suite
endpoint string
projectName string
logstoreName string
accessKeyID string
accessKeySecret string
client Client
recordId string
tagName string
resourceName string
}
func (s *LostoreTestSuite) SetupSuite() {
s.endpoint = os.Getenv("LOG_TEST_ENDPOINT")
s.projectName = fmt.Sprintf("test-go-client-store-%d", time.Now().Unix())
s.logstoreName = fmt.Sprintf("logstore-%d", time.Now().Unix())
s.accessKeyID = os.Getenv("LOG_TEST_ACCESS_KEY_ID")
s.accessKeySecret = os.Getenv("LOG_TEST_ACCESS_KEY_SECRET")
s.client.AccessKeyID = s.accessKeyID
s.client.AccessKeySecret = s.accessKeySecret
s.client.Endpoint = s.endpoint
require.NotEmpty(s.T(), s.endpoint)
require.NotEmpty(s.T(), s.projectName)
require.NotEmpty(s.T(), s.logstoreName)
require.NotEmpty(s.T(), s.accessKeyID)
require.NotEmpty(s.T(), s.accessKeySecret)
_, err := s.client.CreateProject(s.projectName, "ProjectAlreadyExist")
require.True(s.T(), err == nil || strings.Contains(err.Error(), ""))
err = s.client.CreateLogStore(s.projectName, s.logstoreName, 12, 1, false, 64)
require.NoError(s.T(), err)
logstore, err := s.client.GetLogStore(s.projectName, s.logstoreName)
require.NoError(s.T(), err)
require.Equal(s.T(), logstore.ProductType, "")
err = s.client.CreateIndex(s.projectName, s.logstoreName, Index{
Keys: map[string]IndexKey{
"col_0": {
Token: []string{" "},
DocValue: true,
CaseSensitive: false,
Type: "long",
},
"col_1": {
Token: []string{",", ":", " "},
DocValue: true,
CaseSensitive: false,
Type: "text",
},
},
})
require.NoError(s.T(), err)
}
func (s *LostoreTestSuite) TearDownSuite() {
err := s.client.DeleteIndex(s.projectName, s.logstoreName)
assert.NoError(s.T(), err)
err = s.client.DeleteLogStore(s.projectName, s.logstoreName)
assert.NoError(s.T(), err)
err = s.client.DeleteProject(s.projectName)
assert.NoError(s.T(), err)
}
func (s *LostoreTestSuite) TestSplitShardDefault() {
find := func() (num int, id int) {
shards, err := s.client.ListShards(s.projectName, s.logstoreName)
assert.NoError(s.T(), err)
id = -1
for _, shard := range shards {
if shard.Status == "readwrite" {
num++
id = shard.ShardID
}
}
return
}
num, id := find()
assert.True(s.T(), id != -1)
assert.Equal(s.T(), num, 1)
_, err := s.client.SplitShard(s.projectName, s.logstoreName, id, "ef000000000000000000000000000000")
assert.NoError(s.T(), err)
time.Sleep(60 * time.Second)
num2, id2 := find()
assert.True(s.T(), id2 != -1)
assert.Equal(s.T(), num2, 2)
_, err = s.client.SplitNumShard(s.projectName, s.logstoreName, id2, 3)
assert.NoError(s.T(), err)
time.Sleep(60 * time.Second)
num3, id3 := find()
assert.True(s.T(), id3 != -1)
assert.Equal(s.T(), 4, num3)
}
func (s *LostoreTestSuite) TestGetLogsV3ToCompleted() {
key := "key"
value := "val"
n := uint32(time.Now().Unix())
lg := &LogGroup{
Logs: []*Log{
{
Time: &n,
Contents: []*LogContent{
{
Key: &key,
Value: &value,
},
},
},
},
}
project := s.projectName
logstore := s.logstoreName
err := s.client.PostLogStoreLogs(project, logstore, lg, nil)
s.Require().NoError(err)
time.Sleep(time.Second * 10)
resp, err := s.client.GetLogsToCompletedV3(project, logstore, &GetLogRequest{
From: time.Now().Unix() - 30000,
To: time.Now().Unix(),
Lines: 100,
Topic: "",
})
s.Require().NoError(err)
s.GreaterOrEqual(resp.Meta.Count, int64(1))
}