-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patherrors_test.go
More file actions
442 lines (406 loc) · 11.2 KB
/
Copy patherrors_test.go
File metadata and controls
442 lines (406 loc) · 11.2 KB
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
// SPDX-License-Identifier: MPL-2.0
// Copyright (c) 2025 Daniel Schmidt
package netconf
import (
"strings"
"testing"
)
// TestStandardErrors tests the standard error variables
func TestStandardErrors(t *testing.T) {
tests := []struct {
name string
err error
msg string
}{
{"ErrLockReleaseTimeout", ErrLockReleaseTimeout, "lock release timeout"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.err == nil {
t.Error("expected non-nil error")
}
if !strings.Contains(tt.err.Error(), tt.msg) {
t.Errorf("expected error message to contain %q, got %q", tt.msg, tt.err.Error())
}
})
}
}
// TestNetconfErrorError tests the Error() method
func TestNetconfErrorError(t *testing.T) {
tests := []struct {
name string
err *NetconfError
contains []string
}{
{
name: "simple error",
err: &NetconfError{
Operation: "get",
Message: "operation failed",
},
contains: []string{"netconf:", "get", "failed"},
},
{
name: "error with retries",
err: &NetconfError{
Operation: "edit-config",
Message: "timeout",
Retries: 3,
},
contains: []string{"netconf:", "edit-config", "timeout", "retries: 3"},
},
{
name: "error without retries",
err: &NetconfError{
Operation: "commit",
Message: "configuration error",
Retries: 0,
},
contains: []string{"netconf:", "commit", "configuration error"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
errMsg := tt.err.Error()
for _, substr := range tt.contains {
if !strings.Contains(errMsg, substr) {
t.Errorf("expected error message to contain %q, got %q", substr, errMsg)
}
}
})
}
}
// TestNetconfErrorDetailedError tests the DetailedError() method
func TestNetconfErrorDetailedError(t *testing.T) {
tests := []struct {
name string
err *NetconfError
contains []string
}{
{
name: "error with internal message",
err: &NetconfError{
Operation: "get",
Message: "operation failed",
InternalMsg: "connection reset by peer",
},
contains: []string{"netconf:", "get", "failed", "internal:", "connection reset"},
},
{
name: "error with internal message and retries",
err: &NetconfError{
Operation: "lock",
Message: "lock denied",
InternalMsg: "resource in use by session 1234",
Retries: 5,
},
contains: []string{"netconf:", "lock", "denied", "internal:", "session 1234", "retries: 5"},
},
{
name: "error without internal message",
err: &NetconfError{
Operation: "commit",
Message: "configuration error",
},
contains: []string{"netconf:", "commit", "configuration error"},
},
{
name: "error with empty internal message",
err: &NetconfError{
Operation: "validate",
Message: "validation failed",
InternalMsg: "",
},
contains: []string{"netconf:", "validate", "validation failed"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
errMsg := tt.err.DetailedError()
for _, substr := range tt.contains {
if !strings.Contains(errMsg, substr) {
t.Errorf("expected detailed error to contain %q, got %q", substr, errMsg)
}
}
})
}
}
// TestErrorModelStructure tests the ErrorModel struct
func TestErrorModelStructure(t *testing.T) {
model := ErrorModel{
ErrorType: "application",
ErrorTag: "operation-failed",
ErrorSeverity: "error",
ErrorAppTag: "custom-error",
ErrorPath: "/config/interfaces",
ErrorMessage: "Invalid configuration",
ErrorInfo: "Additional details",
}
if model.ErrorType != "application" {
t.Errorf("expected ErrorType 'application', got %q", model.ErrorType)
}
if model.ErrorTag != "operation-failed" {
t.Errorf("expected ErrorTag 'operation-failed', got %q", model.ErrorTag)
}
if model.ErrorSeverity != "error" {
t.Errorf("expected ErrorSeverity 'error', got %q", model.ErrorSeverity)
}
if model.ErrorAppTag != "custom-error" {
t.Errorf("expected ErrorAppTag 'custom-error', got %q", model.ErrorAppTag)
}
if model.ErrorPath != "/config/interfaces" {
t.Errorf("expected ErrorPath '/config/interfaces', got %q", model.ErrorPath)
}
if model.ErrorMessage != "Invalid configuration" {
t.Errorf("expected ErrorMessage 'Invalid configuration', got %q", model.ErrorMessage)
}
if model.ErrorInfo != "Additional details" {
t.Errorf("expected ErrorInfo 'Additional details', got %q", model.ErrorInfo)
}
}
// TestTransientErrorPatterns tests the TransientError pattern matching
func TestTransientErrorPatterns(t *testing.T) {
// Verify TransientErrors is not empty
if len(TransientErrors) == 0 {
t.Error("expected TransientErrors to have patterns defined")
}
// Verify expected patterns exist (only confirmed patterns)
expectedPatterns := map[string]bool{
"lock-denied": false,
"in-use": false,
"transport-errors": false,
}
for _, pattern := range TransientErrors {
if pattern.ErrorTag == "lock-denied" {
expectedPatterns["lock-denied"] = true
}
if pattern.ErrorTag == "in-use" {
expectedPatterns["in-use"] = true
}
if pattern.ErrorType == "transport" {
expectedPatterns["transport-errors"] = true
}
}
for pattern, found := range expectedPatterns {
if !found {
t.Errorf("expected pattern %q to be in TransientErrors", pattern)
}
}
// Verify we have exactly 3 patterns (no speculative patterns)
if len(TransientErrors) != 3 {
t.Errorf("expected exactly 3 transient error patterns, got %d", len(TransientErrors))
}
}
// TestNetconfErrorWithMultipleErrors tests NetconfError with multiple error models
func TestNetconfErrorWithMultipleErrors(t *testing.T) {
netconfErr := &NetconfError{
Operation: "edit-config",
Message: "multiple errors occurred",
Errors: []ErrorModel{
{
ErrorType: "application",
ErrorTag: "invalid-value",
ErrorMessage: "Invalid hostname",
},
{
ErrorType: "application",
ErrorTag: "missing-attribute",
ErrorMessage: "Required attribute missing",
},
},
}
if len(netconfErr.Errors) != 2 {
t.Errorf("expected 2 errors, got %d", len(netconfErr.Errors))
}
}
// TestNetconfErrorAsError tests that NetconfError implements error interface
func TestNetconfErrorAsError(t *testing.T) {
var err error = &NetconfError{
Operation: "test",
Message: "test error",
}
if err.Error() == "" {
t.Error("expected non-empty error message")
}
}
// TestNetconfErrorTransientFlag tests the IsTransient flag
func TestNetconfErrorTransientFlag(t *testing.T) {
tests := []struct {
name string
isTransient bool
}{
{"transient error", true},
{"non-transient error", false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := &NetconfError{
Operation: "test",
Message: "test",
IsTransient: tt.isTransient,
}
if err.IsTransient != tt.isTransient {
t.Errorf("expected IsTransient %v, got %v", tt.isTransient, err.IsTransient)
}
})
}
}
// TestErrorModelZeroValues tests ErrorModel with zero values
func TestErrorModelZeroValues(t *testing.T) {
model := ErrorModel{}
if model.ErrorType != "" {
t.Error("expected empty ErrorType")
}
if model.ErrorTag != "" {
t.Error("expected empty ErrorTag")
}
if model.ErrorSeverity != "" {
t.Error("expected empty ErrorSeverity")
}
if model.ErrorMessage != "" {
t.Error("expected empty ErrorMessage")
}
}
// TestTransientErrorStructure tests the TransientError struct
func TestTransientErrorStructure(t *testing.T) {
pattern := TransientError{
ErrorType: "transport",
ErrorTag: "operation-failed",
ErrorMessage: ".*timeout.*",
}
if pattern.ErrorType != "transport" {
t.Errorf("expected ErrorType 'transport', got %q", pattern.ErrorType)
}
if pattern.ErrorTag != "operation-failed" {
t.Errorf("expected ErrorTag 'operation-failed', got %q", pattern.ErrorTag)
}
if pattern.ErrorMessage != ".*timeout.*" {
t.Errorf("expected ErrorMessage '.*timeout.*', got %q", pattern.ErrorMessage)
}
}
// TestNetconfErrorWithEmptyFields tests NetconfError with empty fields
func TestNetconfErrorWithEmptyFields(t *testing.T) {
err := &NetconfError{}
errMsg := err.Error()
if !strings.Contains(errMsg, "netconf:") {
t.Errorf("expected error message to contain 'netconf:', got %q", errMsg)
}
detailedMsg := err.DetailedError()
if !strings.Contains(detailedMsg, "netconf:") {
t.Errorf("expected detailed error to contain 'netconf:', got %q", detailedMsg)
}
}
// TestNetconfErrorRetryCount tests various retry counts
func TestNetconfErrorRetryCount(t *testing.T) {
tests := []struct {
name string
retries int
}{
{"no retries", 0},
{"one retry", 1},
{"multiple retries", 5},
{"many retries", 100},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := &NetconfError{
Operation: "test",
Message: "test",
Retries: tt.retries,
}
errMsg := err.Error()
if tt.retries > 0 {
if !strings.Contains(errMsg, "retries:") {
t.Error("expected error message to contain retry count")
}
} else {
if strings.Contains(errMsg, "retries:") {
t.Error("expected error message not to contain retry count for zero retries")
}
}
})
}
}
// TestErrorModelRFC6241Fields tests ErrorModel fields match RFC 6241
func TestErrorModelRFC6241Fields(t *testing.T) {
// Test valid error types per RFC 6241
validErrorTypes := []string{"transport", "rpc", "protocol", "application"}
for _, errorType := range validErrorTypes {
model := ErrorModel{ErrorType: errorType}
if model.ErrorType != errorType {
t.Errorf("expected ErrorType %q, got %q", errorType, model.ErrorType)
}
}
// Test valid severities per RFC 6241
validSeverities := []string{"error", "warning"}
for _, severity := range validSeverities {
model := ErrorModel{ErrorSeverity: severity}
if model.ErrorSeverity != severity {
t.Errorf("expected ErrorSeverity %q, got %q", severity, model.ErrorSeverity)
}
}
}
// TestIsLockDeniedError tests the isLockDeniedError helper method
func TestIsLockDeniedError(t *testing.T) {
tests := []struct {
name string
errors []ErrorModel
expectLockDenied bool
}{
{
name: "lock-denied error",
errors: []ErrorModel{
{ErrorTag: "lock-denied"},
},
expectLockDenied: true,
},
{
name: "in-use error",
errors: []ErrorModel{
{ErrorTag: "in-use"},
},
expectLockDenied: true,
},
{
name: "lock-denied with other errors",
errors: []ErrorModel{
{ErrorTag: "invalid-value"},
{ErrorTag: "lock-denied"},
},
expectLockDenied: true,
},
{
name: "non-lock error",
errors: []ErrorModel{
{ErrorTag: "invalid-value"},
},
expectLockDenied: false,
},
{
name: "transport error",
errors: []ErrorModel{
{ErrorType: "transport"},
},
expectLockDenied: false,
},
{
name: "empty errors",
errors: []ErrorModel{},
expectLockDenied: false,
},
{
name: "nil errors",
errors: nil,
expectLockDenied: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
client := &Client{}
result := client.isLockDeniedError(tt.errors)
if result != tt.expectLockDenied {
t.Errorf("expected isLockDeniedError %v, got %v", tt.expectLockDenied, result)
}
})
}
}