forked from tarantool/go-tarantool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
box_error_test.go
499 lines (438 loc) · 13.6 KB
/
box_error_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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
package tarantool_test
import (
"fmt"
"regexp"
"testing"
"github.com/stretchr/testify/require"
"github.com/vmihailenco/msgpack/v5"
. "github.com/tarantool/go-tarantool/v2"
"github.com/tarantool/go-tarantool/v2/test_helpers"
)
var samples = map[string]BoxError{
"SimpleError": {
Type: "ClientError",
File: "config.lua",
Line: uint64(202),
Msg: "Unknown error",
Errno: uint64(0),
Code: uint64(0),
},
"AccessDeniedError": {
Type: "AccessDeniedError",
File: "/__w/sdk/sdk/tarantool-2.10/tarantool/src/box/func.c",
Line: uint64(535),
Msg: "Execute access to function 'forbidden_function' is denied for user 'no_grants'",
Errno: uint64(0),
Code: uint64(42),
Fields: map[string]interface{}{
"object_type": "function",
"object_name": "forbidden_function",
"access_type": "Execute",
},
},
"ChainedError": {
Type: "ClientError",
File: "config.lua",
Line: uint64(205),
Msg: "Timeout exceeded",
Errno: uint64(0),
Code: uint64(78),
Prev: &BoxError{
Type: "ClientError",
File: "config.lua",
Line: uint64(202),
Msg: "Unknown error",
Errno: uint64(0),
Code: uint64(0),
},
},
}
var stringCases = map[string]struct {
e BoxError
s string
}{
"SimpleError": {
samples["SimpleError"],
"Unknown error (ClientError, code 0x0), see config.lua line 202",
},
"AccessDeniedError": {
samples["AccessDeniedError"],
"Execute access to function 'forbidden_function' is denied for user " +
"'no_grants' (AccessDeniedError, code 0x2a), see " +
"/__w/sdk/sdk/tarantool-2.10/tarantool/src/box/func.c line 535",
},
"ChainedError": {
samples["ChainedError"],
"Timeout exceeded (ClientError, code 0x4e), see config.lua line 205: " +
"Unknown error (ClientError, code 0x0), see config.lua line 202",
},
}
func TestBoxErrorStringRepr(t *testing.T) {
for name, testcase := range stringCases {
t.Run(name, func(t *testing.T) {
require.Equal(t, testcase.s, testcase.e.Error())
})
}
}
var mpDecodeSamples = map[string]struct {
b []byte
ok bool
err *regexp.Regexp
}{
"OuterMapInvalidLen": {
[]byte{0xc1},
false,
regexp.MustCompile(`msgpack: (?:unexpected|invalid|unknown) code.c1 decoding map length`),
},
"OuterMapInvalidKey": {
[]byte{0x81, 0xc1},
false,
regexp.MustCompile(`msgpack: (?:unexpected|invalid|unknown) code.c1 decoding int64`),
},
"OuterMapExtraKey": {
[]byte{0x82, 0x00, 0x91, 0x81, 0x02, 0x01, 0x11, 0x00},
true,
regexp.MustCompile(``),
},
"OuterMapExtraInvalidKey": {
[]byte{0x81, 0x11, 0x81},
false,
regexp.MustCompile(`EOF`),
},
"ArrayInvalidLen": {
[]byte{0x81, 0x00, 0xc1},
false,
regexp.MustCompile(`msgpack: (?:unexpected|invalid|unknown) code.c1 decoding array length`),
},
"ArrayZeroLen": {
[]byte{0x81, 0x00, 0x90},
false,
regexp.MustCompile(`msgpack: unexpected empty BoxError stack on decode`),
},
"InnerMapInvalidLen": {
[]byte{0x81, 0x00, 0x91, 0xc1},
false,
regexp.MustCompile(`msgpack: (?:unexpected|invalid|unknown) code.c1 decoding map length`),
},
"InnerMapInvalidKey": {
[]byte{0x81, 0x00, 0x91, 0x81, 0xc1},
false,
regexp.MustCompile(`msgpack: (?:unexpected|invalid|unknown) code.c1 decoding int64`),
},
"InnerMapInvalidErrorType": {
[]byte{0x81, 0x00, 0x91, 0x81, 0x00, 0xc1},
false,
regexp.MustCompile(`msgpack: (?:unexpected|invalid|unknown) code.c1` +
` decoding (?:string\/bytes|bytes) length`),
},
"InnerMapInvalidErrorFile": {
[]byte{0x81, 0x00, 0x91, 0x81, 0x01, 0xc1},
false,
regexp.MustCompile(`msgpack: (?:unexpected|invalid|unknown) code.c1` +
` decoding (?:string\/bytes|bytes) length`),
},
"InnerMapInvalidErrorLine": {
[]byte{0x81, 0x00, 0x91, 0x81, 0x02, 0xc1},
false,
regexp.MustCompile(`msgpack: (?:unexpected|invalid|unknown) code.c1 decoding uint64`),
},
"InnerMapInvalidErrorMessage": {
[]byte{0x81, 0x00, 0x91, 0x81, 0x03, 0xc1},
false,
regexp.MustCompile(`msgpack: (?:unexpected|invalid|unknown) code.c1 decoding` +
` (?:string\/bytes|bytes) length`),
},
"InnerMapInvalidErrorErrno": {
[]byte{0x81, 0x00, 0x91, 0x81, 0x04, 0xc1},
false,
regexp.MustCompile(`msgpack: (?:unexpected|invalid|unknown) code.c1 decoding uint64`),
},
"InnerMapInvalidErrorErrcode": {
[]byte{0x81, 0x00, 0x91, 0x81, 0x05, 0xc1},
false,
regexp.MustCompile(`msgpack: (?:unexpected|invalid|unknown) code.c1 decoding uint64`),
},
"InnerMapInvalidErrorFields": {
[]byte{0x81, 0x00, 0x91, 0x81, 0x06, 0xc1},
false,
regexp.MustCompile(`msgpack: (?:unexpected|invalid|unknown) code.c1 decoding map length`),
},
"InnerMapInvalidErrorFieldsKey": {
[]byte{0x81, 0x00, 0x91, 0x81, 0x06, 0x81, 0xc1},
false,
regexp.MustCompile(`msgpack: (?:unexpected|invalid|unknown) code.c1` +
` decoding (?:string\/bytes|bytes) length`),
},
"InnerMapInvalidErrorFieldsValue": {
[]byte{0x81, 0x00, 0x91, 0x81, 0x06, 0x81, 0xa3, 0x6b, 0x65, 0x79, 0xc1},
false,
regexp.MustCompile(`msgpack: (?:unexpected|invalid|unknown) code.c1 decoding interface{}`),
},
"InnerMapExtraKey": {
[]byte{0x81, 0x00, 0x91, 0x81, 0x21, 0x00},
true,
regexp.MustCompile(``),
},
"InnerMapExtraInvalidKey": {
[]byte{0x81, 0x00, 0x91, 0x81, 0x21, 0x81},
false,
regexp.MustCompile(`EOF`),
},
}
func TestMessagePackDecode(t *testing.T) {
for name, testcase := range mpDecodeSamples {
t.Run(name, func(t *testing.T) {
var val *BoxError = &BoxError{}
err := val.UnmarshalMsgpack(testcase.b)
if testcase.ok {
require.Nilf(t, err, "No errors on decode")
} else {
require.Regexp(t, testcase.err, err.Error())
}
})
}
}
func TestMessagePackUnmarshalToNil(t *testing.T) {
var val *BoxError = nil
require.PanicsWithValue(t, "cannot unmarshal to a nil pointer",
func() { val.UnmarshalMsgpack(mpDecodeSamples["InnerMapExtraKey"].b) })
}
func TestMessagePackEncodeNil(t *testing.T) {
var val *BoxError
_, err := val.MarshalMsgpack()
require.NotNil(t, err)
require.Equal(t, "msgpack: unexpected nil BoxError on encode", err.Error())
}
var space = "test_error_type"
var index = "primary"
type TupleBoxError struct {
pk string // BoxError cannot be used as a primary key.
val BoxError
}
func (t *TupleBoxError) EncodeMsgpack(e *msgpack.Encoder) error {
if err := e.EncodeArrayLen(2); err != nil {
return err
}
if err := e.EncodeString(t.pk); err != nil {
return err
}
return e.Encode(&t.val)
}
func (t *TupleBoxError) DecodeMsgpack(d *msgpack.Decoder) error {
var err error
var l int
if l, err = d.DecodeArrayLen(); err != nil {
return err
}
if l != 2 {
return fmt.Errorf("Array length doesn't match: %d", l)
}
if t.pk, err = d.DecodeString(); err != nil {
return err
}
return d.Decode(&t.val)
}
// Raw bytes encoding test is impossible for
// object with Fields since map iterating is random.
var tupleCases = map[string]struct {
tuple TupleBoxError
ttObj string
}{
"SimpleError": {
TupleBoxError{
"simple_error_pk",
samples["SimpleError"],
},
"simple_error",
},
"AccessDeniedError": {
TupleBoxError{
"access_denied_error_pk",
samples["AccessDeniedError"],
},
"access_denied_error",
},
"ChainedError": {
TupleBoxError{
"chained_error_pk",
samples["ChainedError"],
},
"chained_error",
},
}
func TestErrorTypeMPEncodeDecode(t *testing.T) {
for name, testcase := range tupleCases {
t.Run(name, func(t *testing.T) {
buf, err := msgpack.Marshal(&testcase.tuple)
require.Nil(t, err)
var res TupleBoxError
err = msgpack.Unmarshal(buf, &res)
require.Nil(t, err)
require.Equal(t, testcase.tuple, res)
})
}
}
func TestErrorTypeEval(t *testing.T) {
test_helpers.SkipIfErrorMessagePackTypeUnsupported(t)
conn := test_helpers.ConnectWithValidation(t, dialer, opts)
defer conn.Close()
for name, testcase := range tupleCases {
t.Run(name, func(t *testing.T) {
data, err := conn.Eval("return ...", []interface{}{&testcase.tuple.val})
require.Nil(t, err)
require.NotNil(t, data)
require.Equal(t, len(data), 1)
actual, ok := data[0].(*BoxError)
require.Truef(t, ok, "Response data has valid type")
require.Equal(t, testcase.tuple.val, *actual)
})
}
}
func TestErrorTypeEvalTyped(t *testing.T) {
test_helpers.SkipIfErrorMessagePackTypeUnsupported(t)
conn := test_helpers.ConnectWithValidation(t, dialer, opts)
defer conn.Close()
for name, testcase := range tupleCases {
t.Run(name, func(t *testing.T) {
var res []BoxError
err := conn.EvalTyped("return ...", []interface{}{&testcase.tuple.val}, &res)
require.Nil(t, err)
require.NotNil(t, res)
require.Equal(t, len(res), 1)
require.Equal(t, testcase.tuple.val, res[0])
})
}
}
func TestErrorTypeInsert(t *testing.T) {
test_helpers.SkipIfErrorMessagePackTypeUnsupported(t)
conn := test_helpers.ConnectWithValidation(t, dialer, opts)
defer conn.Close()
truncateEval := fmt.Sprintf("box.space[%q]:truncate()", space)
_, err := conn.Eval(truncateEval, []interface{}{})
require.Nil(t, err)
for name, testcase := range tupleCases {
t.Run(name, func(t *testing.T) {
_, err = conn.Insert(space, &testcase.tuple)
require.Nil(t, err)
checkEval := fmt.Sprintf(`
local err = rawget(_G, %q)
assert(err ~= nil)
local tuple = box.space[%q]:get(%q)
assert(tuple ~= nil)
local tuple_err = tuple[2]
assert(tuple_err ~= nil)
return compare_box_errors(tuple_err, err)
`, testcase.ttObj, space, testcase.tuple.pk)
// In fact, compare_box_errors does not check than File and Line
// of connector BoxError are equal to the Tarantool ones
// since they may differ between different Tarantool versions
// and editions.
_, err := conn.Eval(checkEval, []interface{}{})
require.Nilf(t, err, "Tuple has been successfully inserted")
})
}
}
func TestErrorTypeInsertTyped(t *testing.T) {
test_helpers.SkipIfErrorMessagePackTypeUnsupported(t)
conn := test_helpers.ConnectWithValidation(t, dialer, opts)
defer conn.Close()
truncateEval := fmt.Sprintf("box.space[%q]:truncate()", space)
_, err := conn.Eval(truncateEval, []interface{}{})
require.Nil(t, err)
for name, testcase := range tupleCases {
t.Run(name, func(t *testing.T) {
var res []TupleBoxError
err = conn.InsertTyped(space, &testcase.tuple, &res)
require.Nil(t, err)
require.NotNil(t, res)
require.Equal(t, len(res), 1)
require.Equal(t, testcase.tuple, res[0])
checkEval := fmt.Sprintf(`
local err = rawget(_G, %q)
assert(err ~= nil)
local tuple = box.space[%q]:get(%q)
assert(tuple ~= nil)
local tuple_err = tuple[2]
assert(tuple_err ~= nil)
return compare_box_errors(tuple_err, err)
`, testcase.ttObj, space, testcase.tuple.pk)
// In fact, compare_box_errors does not check than File and Line
// of connector BoxError are equal to the Tarantool ones
// since they may differ between different Tarantool versions
// and editions.
_, err := conn.Eval(checkEval, []interface{}{})
require.Nilf(t, err, "Tuple has been successfully inserted")
})
}
}
func TestErrorTypeSelect(t *testing.T) {
test_helpers.SkipIfErrorMessagePackTypeUnsupported(t)
conn := test_helpers.ConnectWithValidation(t, dialer, opts)
defer conn.Close()
truncateEval := fmt.Sprintf("box.space[%q]:truncate()", space)
_, err := conn.Eval(truncateEval, []interface{}{})
require.Nil(t, err)
for name, testcase := range tupleCases {
t.Run(name, func(t *testing.T) {
insertEval := fmt.Sprintf(`
local err = rawget(_G, %q)
assert(err ~= nil)
local tuple = box.space[%q]:insert{%q, err}
assert(tuple ~= nil)
`, testcase.ttObj, space, testcase.tuple.pk)
_, err := conn.Eval(insertEval, []interface{}{})
require.Nilf(t, err, "Tuple has been successfully inserted")
var offset uint32 = 0
var limit uint32 = 1
data, err := conn.Select(space, index, offset, limit, IterEq,
[]interface{}{testcase.tuple.pk})
require.Nil(t, err)
require.NotNil(t, data)
require.Equalf(t, len(data), 1, "Exactly one tuple had been found")
tpl, ok := data[0].([]interface{})
require.Truef(t, ok, "Tuple has valid type")
require.Equal(t, testcase.tuple.pk, tpl[0])
actual, ok := tpl[1].(*BoxError)
require.Truef(t, ok, "BoxError tuple field has valid type")
// In fact, CheckEqualBoxErrors does not check than File and Line
// of connector BoxError are equal to the Tarantool ones
// since they may differ between different Tarantool versions
// and editions.
test_helpers.CheckEqualBoxErrors(t, testcase.tuple.val, *actual)
})
}
}
func TestErrorTypeSelectTyped(t *testing.T) {
test_helpers.SkipIfErrorMessagePackTypeUnsupported(t)
conn := test_helpers.ConnectWithValidation(t, dialer, opts)
defer conn.Close()
truncateEval := fmt.Sprintf("box.space[%q]:truncate()", space)
_, err := conn.Eval(truncateEval, []interface{}{})
require.Nil(t, err)
for name, testcase := range tupleCases {
t.Run(name, func(t *testing.T) {
insertEval := fmt.Sprintf(`
local err = rawget(_G, %q)
assert(err ~= nil)
local tuple = box.space[%q]:insert{%q, err}
assert(tuple ~= nil)
`, testcase.ttObj, space, testcase.tuple.pk)
_, err := conn.Eval(insertEval, []interface{}{})
require.Nilf(t, err, "Tuple has been successfully inserted")
var offset uint32 = 0
var limit uint32 = 1
var resp []TupleBoxError
err = conn.SelectTyped(space, index, offset, limit, IterEq,
[]interface{}{testcase.tuple.pk}, &resp)
require.Nil(t, err)
require.NotNil(t, resp)
require.Equalf(t, len(resp), 1, "Exactly one tuple had been found")
require.Equal(t, testcase.tuple.pk, resp[0].pk)
// In fact, CheckEqualBoxErrors does not check than File and Line
// of connector BoxError are equal to the Tarantool ones
// since they may differ between different Tarantool versions
// and editions.
test_helpers.CheckEqualBoxErrors(t, testcase.tuple.val, resp[0].val)
})
}
}