forked from galaxydi/go-loghub
-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathclient_scheduled_sql_test.go
219 lines (207 loc) · 7.02 KB
/
client_scheduled_sql_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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package sls
import (
"fmt"
"github.com/gogo/protobuf/proto"
"github.com/stretchr/testify/suite"
"math/rand"
"os"
"testing"
"time"
)
func TestScheduledSQL(t *testing.T) {
suite.Run(t, new(ScheduledSQLTestSuite))
}
type ScheduledSQLTestSuite struct {
suite.Suite
endpoint string
accessKeyID string
accessKeySecret string
projectName string
sourceLogStore string
targetLogStoreName string
scheduledSQLName string
displayName string
client *Client
}
func (s *ScheduledSQLTestSuite) SetupSuite() {
s.endpoint = os.Getenv("LOG_TEST_ENDPOINT")
s.accessKeyID = os.Getenv("LOG_TEST_ACCESS_KEY_ID")
s.accessKeySecret = os.Getenv("LOG_TEST_ACCESS_KEY_SECRET")
suffix := time.Now().Unix()
s.projectName = fmt.Sprintf("test-scheduled-sql-%d", suffix)
s.sourceLogStore = "test-source"
s.targetLogStoreName = "test-target"
s.scheduledSQLName = fmt.Sprintf("schedulesql-%d", suffix)
s.displayName = fmt.Sprintf("display-%d", suffix)
s.client = &Client{
Endpoint: s.endpoint,
AccessKeyID: s.accessKeyID,
AccessKeySecret: s.accessKeySecret,
}
s.setUp()
}
func (s *ScheduledSQLTestSuite) TearDownSuite() {
err := s.client.DeleteProject(s.projectName)
s.Require().Nil(err)
}
func (s *ScheduledSQLTestSuite) TestClient_CreateAndDeleteScheduledSQL() {
ce := s.client.CreateScheduledSQL(s.projectName, s.getScheduleSQL("111"))
s.Require().Nil(ce)
de := s.client.DeleteScheduledSQL(s.projectName, s.scheduledSQLName)
s.Require().Nil(de)
}
func (s *ScheduledSQLTestSuite) TestClient_UpdateAndGetScheduledSQL() {
ce := s.client.CreateScheduledSQL(s.projectName, s.getScheduleSQL("111"))
s.Require().Nil(ce)
scheduledSQL, ge := s.client.GetScheduledSQL(s.projectName, s.scheduledSQLName)
s.Require().Nil(ge)
s.Require().Equal(s.scheduledSQLName, scheduledSQL.Name)
s.Require().Equal("111", scheduledSQL.Description)
ue := s.client.UpdateScheduledSQL(s.projectName, s.getScheduleSQL("222"))
s.Require().Nil(ue)
scheduledSQL2, ge2 := s.client.GetScheduledSQL(s.projectName, s.scheduledSQLName)
s.Require().Nil(ge2)
s.Require().Equal(s.scheduledSQLName, scheduledSQL2.Name)
s.Require().Equal("222", scheduledSQL2.Description)
de := s.client.DeleteScheduledSQL(s.projectName, s.scheduledSQLName)
s.Require().Nil(de)
}
func (s *ScheduledSQLTestSuite) TestClient_ListScheduledSQL() {
ce := s.client.CreateScheduledSQL(s.projectName, s.getScheduleSQL("111"))
s.Require().Nil(ce)
scheduledsqls, total, count, le := s.client.ListScheduledSQL(s.projectName, "", "", 0, 10)
s.Require().Nil(le)
s.Require().Equal(1, len(scheduledsqls))
s.Require().Equal(1, total)
s.Require().Equal(1, count)
de := s.client.DeleteScheduledSQL(s.projectName, s.scheduledSQLName)
s.Require().Nil(de)
}
func (s *ScheduledSQLTestSuite) TestClient_ScheduledSQLInstances() {
prepareData(s)
ce := s.client.CreateScheduledSQL(s.projectName, s.getScheduleSQL("111"))
s.Require().Nil(ce)
time.Sleep(time.Minute * 5)
status := &InstanceStatus{
FromTime: time.Now().Unix() - 600,
ToTime: time.Now().Unix() + 600,
Offset: 0,
Size: 3,
State: ScheduledSQL_SUCCEEDED,
}
instances, total, count, le := s.client.ListScheduledSQLJobInstances(s.projectName, s.scheduledSQLName, status)
s.Require().Nil(le)
s.Require().Equal(3, len(instances))
s.Require().Equal(int64(3), count)
s.Require().Equal(true, total > 3)
instance := instances[0]
jobInstance, ge := s.client.GetScheduledSQLJobInstance(s.projectName, s.scheduledSQLName, instance.InstanceId, true)
s.Require().Nil(ge)
s.Require().Equal(ScheduledSQL_SUCCEEDED, jobInstance.State)
me := s.client.ModifyScheduledSQLJobInstanceState(s.projectName, s.scheduledSQLName, instance.InstanceId, ScheduledSQL_RUNNING)
s.Require().Nil(me)
jobInstance2, ge2 := s.client.GetScheduledSQLJobInstance(s.projectName, s.scheduledSQLName, instance.InstanceId, true)
s.Require().Nil(ge2)
s.Require().Equal(true, ScheduledSQL_SUCCEEDED != jobInstance2.State)
time.Sleep(time.Second * 5)
de := s.client.DeleteScheduledSQL(s.projectName, s.scheduledSQLName)
s.Require().Nil(de)
}
func prepareData(s *ScheduledSQLTestSuite) {
for loggroupIdx := 0; loggroupIdx < 10; loggroupIdx++ {
var logs []*Log
for logIdx := 0; logIdx < 100; logIdx++ {
var content []*LogContent
for colIdx := 0; colIdx < 10; colIdx++ {
if colIdx == 0 {
content = append(content, &LogContent{
Key: proto.String(fmt.Sprintf("col_%d", colIdx)),
Value: proto.String(fmt.Sprintf("%d", rand.Intn(10000000))),
})
} else {
content = append(content, &LogContent{
Key: proto.String(fmt.Sprintf("col_%d", colIdx)),
Value: proto.String(fmt.Sprintf("logGroup idx: %d, log idx: %d, col idx: %d, value: %d", loggroupIdx, logIdx, colIdx, rand.Intn(10000000))),
})
}
}
log := &Log{
Time: proto.Uint32(uint32(time.Now().Unix())),
Contents: content,
}
logs = append(logs, log)
}
logGroup := &LogGroup{
Topic: proto.String("test"),
Source: proto.String("10.238.222.116"),
Logs: logs,
}
err := s.client.PutLogs(s.projectName, s.sourceLogStore, logGroup)
s.Require().Nil(err)
}
time.Sleep(time.Second * 5)
}
func (s *ScheduledSQLTestSuite) getScheduleSQL(des string) *ScheduledSQL {
return &ScheduledSQL{
Name: s.scheduledSQLName,
DisplayName: s.displayName,
Description: des,
Status: ENABLED,
Configuration: &ScheduledSQLConfiguration{
SourceLogStore: s.sourceLogStore,
DestProject: s.projectName,
DestEndpoint: s.endpoint,
DestLogStore: s.targetLogStoreName,
Script: "*|SELECT COUNT(col_0) as value_count",
SqlType: SEARCH_QUERY,
ResourcePool: DEFAULT,
RoleArn: os.Getenv("LOG_TEST_ROLE_ARN"),
DestRoleArn: os.Getenv("LOG_TEST_ROLE_ARN"),
FromTimeExpr: "@m-1m",
ToTimeExpr: "@m",
MaxRunTimeInSeconds: 60,
MaxRetries: 20,
FromTime: time.Now().Unix() - 300,
ToTime: time.Now().Unix() + 300,
DataFormat: LOG_TO_LOG,
Parameters: nil,
},
Schedule: &Schedule{
Type: "FixedRate",
Interval: "1m",
Delay: 10,
DayOfWeek: 0,
Hour: 0,
},
CreateTime: 0,
LastModifiedTime: 0,
Type: SCHEDULED_SQL_JOB,
}
}
func (s *ScheduledSQLTestSuite) setUp() {
_, ce := s.client.CreateProject(s.projectName, "test scheduled sql")
s.Require().Nil(ce)
time.Sleep(time.Second * 60)
cle := s.client.CreateLogStore(s.projectName, s.sourceLogStore, 3, 2, true, 4)
s.Require().Nil(cle)
cle2 := s.client.CreateLogStore(s.projectName, s.targetLogStoreName, 3, 2, true, 4)
s.Require().Nil(cle2)
cie := s.client.CreateIndex(s.projectName, s.sourceLogStore, 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",
},
},
})
s.Require().Nil(cie)
time.Sleep(time.Second * 60)
}