Repository navigation
Expand file tree
/
Copy patherrors_test.go
More file actions
83 lines (76 loc) · 2.84 KB
/
Copy patherrors_test.go
File metadata and controls
83 lines (76 loc) · 2.84 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
package stackit
import (
"errors"
"testing"
"github.com/stackitcloud/stackit-sdk-go/core/oapierror"
)
func TestIsNotFound(t *testing.T) {
tests := []struct {
name string
err error
want bool
}{
{"nil error", nil, false},
{"plain error", errors.New("boom"), false},
{"404 error", oapierror.NewError(404, "not found"), true},
{"wrapped 404 error", errWrap{oapierror.NewError(404, "not found")}, true},
{"500 error", oapierror.NewError(500, "server error"), false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := IsNotFound(tt.err); got != tt.want {
t.Errorf("IsNotFound(%v) = %v, want %v", tt.err, got, tt.want)
}
})
}
}
func TestIsAlreadyExists(t *testing.T) {
alreadyExistsBody := []byte(`{"code":"AlreadyExists","message":"already exists: cluster with name \"clu\""}`)
tests := []struct {
name string
err error
want bool
}{
{"nil error", nil, false},
{"plain error", errors.New("boom"), false},
{"409 AlreadyExists error", oapierror.NewErrorWithBody(409, "", alreadyExistsBody, nil), true},
{"wrapped 409 AlreadyExists error", errWrap{oapierror.NewErrorWithBody(409, "", alreadyExistsBody, nil)}, true},
{"unrelated 409 error", oapierror.NewErrorWithBody(409, "", []byte(`{"code":"DependentExists"}`), nil), false},
{"404 error", oapierror.NewError(404, "not found"), false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := IsAlreadyExists(tt.err); got != tt.want {
t.Errorf("IsAlreadyExists(%v) = %v, want %v", tt.err, got, tt.want)
}
})
}
}
func TestIsTransientAuthError(t *testing.T) {
invalidGrantBody := []byte(`{"error":"invalid_grant","error_description":"invalid iat"}`)
tests := []struct {
name string
err error
want bool
}{
{"nil error", nil, false},
{"plain error", errors.New("boom"), false},
{"invalid_grant/invalid iat 400 error", oapierror.NewErrorWithBody(400, "", invalidGrantBody, nil), true},
{"wrapped invalid_grant/invalid iat 400 error", errWrap{oapierror.NewErrorWithBody(400, "", invalidGrantBody, nil)}, true},
{"unrelated 400 error", oapierror.NewErrorWithBody(400, "", []byte(`{"error":"invalid_request"}`), nil), false},
{"invalid_grant without invalid iat (e.g. revoked key) is not transient",
oapierror.NewErrorWithBody(400, "", []byte(`{"error":"invalid_grant","error_description":"invalid credentials"}`), nil), false},
{"404 error", oapierror.NewError(404, "not found"), false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := IsTransientAuthError(tt.err); got != tt.want {
t.Errorf("IsTransientAuthError(%v) = %v, want %v", tt.err, got, tt.want)
}
})
}
}
// errWrap wraps an error to exercise errors.As unwrapping in IsNotFound.
type errWrap struct{ err error }
func (e errWrap) Error() string { return e.err.Error() }
func (e errWrap) Unwrap() error { return e.err }