-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdataResolver_test.go
257 lines (241 loc) · 6.56 KB
/
dataResolver_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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package main
import (
"net/http"
"net/http/httptest"
"reflect"
"strconv"
"testing"
"time"
"github.com/gorilla/websocket"
"github.com/posener/wstest"
)
type DBMock struct {
t *testing.T
wantMessage Message
tisExecute bool
}
func (db *DBMock) AddLog(data LogData, index string) {
if !reflect.DeepEqual(db.wantMessage.Attributes, data.Attributes) {
db.t.Errorf("DB and want attributes are not equals want %v got %v", db.wantMessage.Attributes, data.Attributes)
}
db.tisExecute = true
}
func (db *DBMock) AddStats(data StatsData, index string) {
if !reflect.DeepEqual(db.wantMessage.Attributes, data.Attributes) {
db.t.Errorf("DB and want attributes are not equals want %v got %v", db.wantMessage.Attributes, data.Attributes)
}
db.tisExecute = true
}
func (db *DBMock) SetIlmPolicy(p DataRolloverPattern) error {
return nil
}
func (db *DBMock) SetPolicyTemplate() error {
return nil
}
func TestDataServiceWebSocket_SubscribeTestsCallWithNoWebsocketClient(t *testing.T) {
d := DataServiceWebSocket{
ConnectionAllowed: func(r *http.Request) bool {
return true
},
genUID: func() (string, error) { return "1234", nil },
}
s := httptest.NewServer(http.HandlerFunc(d.Subscribe))
defer s.Close()
res, err := http.Get(s.URL)
if err != nil {
t.Fatalf("Error by get request %v", err.Error())
}
if res.StatusCode != 400 {
t.Errorf("Make http request to websocket want statuscode %v got %v", 400, res.StatusCode)
}
}
func TestDataServiceWebSocket_SubscribeTests(t *testing.T) {
mocktime, err := time.Parse("2006-01-02", "2019-06-02")
if err != nil {
t.Fatal(err)
}
type test struct {
name string
connectionAllowedResult bool
want int
wantConnectionSize int
getMessageObjToSend func() Message
breakAfterStauscheck bool
}
data := []test{
test{
name: "ConnectionAllowed returned allow connection so connection will be open and log message will be send",
connectionAllowedResult: true,
want: 101,
wantConnectionSize: 1,
getMessageObjToSend: func() Message {
return Message{
Time: mocktime,
Type: MessageTypeLog,
Data: []string{
"{\"test\":\"test\"",
},
SearchIndex: "testindex",
Attributes: Attributes{
Host: "local.test",
},
}
},
breakAfterStauscheck: false,
},
test{
name: "ConnectionAllowed returned allow connection so connection will be open and statsmessage will be send",
connectionAllowedResult: true,
want: 101,
wantConnectionSize: 1,
getMessageObjToSend: func() Message {
return Message{
Time: mocktime,
Type: MessageTypeStats,
Data: []string{
"{\"test\":\"test\"",
},
SearchIndex: "testindex",
Attributes: Attributes{
Host: "local.test",
},
}
},
breakAfterStauscheck: false,
},
test{
name: "ConnectionAllowed returned disallow connection so no connection will be open ",
connectionAllowedResult: false,
want: 401,
wantConnectionSize: 0,
getMessageObjToSend: func() Message { return Message{} },
breakAfterStauscheck: true,
},
}
const mockHeaderKey = "connectionAllowedResult"
for _, one := range data {
dbMock := &DBMock{
t: t,
wantMessage: one.getMessageObjToSend(),
tisExecute: false,
}
dataserverholder := DataServiceWebSocket{
Db: dbMock,
ConnectionAllowed: func(r *http.Request) bool {
res, _ := strconv.ParseBool(r.Header.Get(mockHeaderKey))
return res
},
ClientConnections: make(map[string]*websocket.Conn),
genUID: func() (string, error) { return "1234", nil },
}
d := wstest.NewDialer(http.HandlerFunc(dataserverholder.Subscribe))
httpHeader := make(http.Header)
httpHeader.Add(mockHeaderKey, strconv.FormatBool(one.connectionAllowedResult))
c, resp, _ := d.Dial("ws://"+"whatever"+"/", httpHeader)
if one.want != resp.StatusCode {
t.Errorf("Error by Status want %v got %v", one.want, resp.StatusCode)
}
if one.breakAfterStauscheck {
break
}
err := c.WriteJSON([]Message{one.getMessageObjToSend()})
if err != nil {
t.Error(err)
}
time.Sleep(1 * time.Second) // find a better solution
if !dbMock.tisExecute {
t.Errorf("Mock db was not executed")
}
if len(dataserverholder.ClientConnections) != one.wantConnectionSize {
t.Errorf("Error by ClientConnections want %v got %v connections", one.wantConnectionSize, len(dataserverholder.ClientConnections))
}
}
}
func Test_getIndexDate(t *testing.T) {
tests := []struct {
name string
time func() time.Time
indextype DataRolloverPattern
want string
}{
{
name: "is weekly so Returns 2016-5",
time: func() time.Time {
res, _ := time.Parse("2006-01-02", "2016-02-02")
return res
},
indextype: Weekly,
want: "2016-5",
},
{
name: "is weekly so Returns 2019-44",
time: func() time.Time {
res, _ := time.Parse("2006-01-02", "2019-10-31")
return res
},
indextype: Weekly,
want: "2019-44",
},
{
name: "is daily so Returns 2019-10-31",
time: func() time.Time {
res, _ := time.Parse("2006-01-02", "2019-10-31")
return res
},
indextype: Daily,
want: "2019-10-31",
},
{
name: "is monthly so Returns 2019-10",
time: func() time.Time {
res, _ := time.Parse("2006-01-02", "2019-10-31")
return res
},
indextype: Monthly,
want: "2019-10",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := getIndexDate(tt.time(), tt.indextype); got != tt.want {
t.Errorf("getIndexDate() = %v, want %v", got, tt.want)
}
})
}
}
func TestDataServiceWebSocket_Root(t *testing.T) {
d := DataServiceWebSocket{}
s := httptest.NewServer(http.HandlerFunc(d.Root))
res, err := http.Get(s.URL)
if err != nil {
t.Fatalf("Error by get root url: %v", err.Error())
}
if res.StatusCode != http.StatusOK {
t.Errorf("Want statuscode %v but got %v", 200, res.StatusCode)
}
}
func Test_getStatsIndexName(t *testing.T) {
tests := []struct {
name string
param string
want string
}{
{
name: "given cumulated so want cumulated back",
param: "lalal_stats_cumulated",
want: "stats_cumulated_funk-",
},
{
name: "given not cumulated so want stats_funk- back",
param: "lalal_stats",
want: "stats_funk-",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := getStatsIndexName(tt.param); got != tt.want {
t.Errorf("getStatsIndexName() = %v, want %v", got, tt.want)
}
})
}
}