blob: 3030e0d3e858a01a292c7f8a7f385b6dbed017fc [file] [log] [blame]
Scott Baker20481aa2019-06-20 11:00:54 -07001/*
2 * Portions copyright 2019-present Open Networking Foundation
3 * Original copyright 2019-present Ciena Corporation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17package error
18
19import (
20 "fmt"
21 "github.com/stretchr/testify/assert"
Scott Bakera55e6452019-06-25 11:10:30 -070022 "google.golang.org/grpc/codes"
23 "google.golang.org/grpc/status"
Scott Baker20481aa2019-06-20 11:00:54 -070024 "testing"
25)
26
Scott Baker20481aa2019-06-20 11:00:54 -070027func TestGenericError(t *testing.T) {
28 var err error
29
30 err = fmt.Errorf("Some error")
31
32 // Type conversion from `error` to ChecksumMismatchError should fail
33 _, ok := err.(ChecksumMismatchError)
34 assert.False(t, ok)
35
36 // Type conversion from `error` to CordCtlError should fail
37 _, ok = err.(CordCtlError)
38 assert.False(t, ok)
39}
40
41func TestChecksumMismatchError(t *testing.T) {
42 var err error
43
44 err = WithStackTrace(&ChecksumMismatchError{Actual: "123", Expected: "456"})
45
Scott Baker20481aa2019-06-20 11:00:54 -070046 // Check that the Error() function returns the right text
Scott Bakera55e6452019-06-25 11:10:30 -070047 assert.Equal(t, err.Error(), "checksum mismatch (actual=456, expected=123)")
Scott Baker20481aa2019-06-20 11:00:54 -070048
49 // Type conversion from `error` to ChecksumMismatchError should succeed
50 _, ok := err.(*ChecksumMismatchError)
51 assert.True(t, ok)
52
53 // Type switch is another way of doing the same
54 switch err.(type) {
55 case *ChecksumMismatchError:
56 // do nothing
57 case CordCtlError:
58 assert.Fail(t, "Should have used the ChecksumMismatchError case instead")
59 default:
60 assert.Fail(t, "Wrong part of switch statement was called")
61 }
62
63 // Type conversion from `error` to CordCtlError should succeed
64 cce, ok := err.(CordCtlError)
65 assert.True(t, ok)
66
67 // ShouldDumpStack() returned from a ChecksumMismatchError should be false
68 assert.False(t, cce.ShouldDumpStack())
69}
70
71func TestUnknownModelTypeError(t *testing.T) {
72 var err error
73
74 err = WithStackTrace(&UnknownModelTypeError{Name: "foo"})
75
Scott Bakera55e6452019-06-25 11:10:30 -070076 // Check that the Error() function returns the right text
77 assert.Equal(t, err.Error(), "Model foo does not exist. Use `cordctl modeltype list` to get a list of available models")
78}
79
80func TestRpcErrorToCordError(t *testing.T) {
81 // InternalError
82 err := status.Error(codes.Unknown, "A fake Unknown error")
83
84 cordErr := RpcErrorToCordError(err)
85
86 _, ok := cordErr.(*InternalError)
87 assert.True(t, ok)
88 assert.Equal(t, cordErr.Error(), "Internal Error: A fake Unknown error")
89
90 // NotFound
91 err = status.Error(codes.NotFound, "A fake not found error")
92
93 cordErr = RpcErrorToCordError(err)
94
95 _, ok = cordErr.(*ModelNotFoundError)
96 assert.True(t, ok)
97 assert.Equal(t, cordErr.Error(), "Not Found")
98
99 // PermissionDeniedError
100 err = status.Error(codes.PermissionDenied, "A fake Permission error")
101
102 cordErr = RpcErrorToCordError(err)
103
104 _, ok = cordErr.(*PermissionDeniedError)
105 assert.True(t, ok)
106 assert.Equal(t, cordErr.Error(), "Permission Denied. Please verify username and password are correct")
107}
108
109func TestRpcErrorWithModelNameToCordError(t *testing.T) {
110 // InternalError
111 err := status.Error(codes.Unknown, "A fake Unknown error")
112
113 cordErr := RpcErrorWithModelNameToCordError(err, "Foo")
114
115 _, ok := cordErr.(*InternalError)
116 assert.True(t, ok)
117 assert.Equal(t, cordErr.Error(), "Internal Error [on model Foo]: A fake Unknown error")
118}
119
120func TestRpcErrorWithIdToCordError(t *testing.T) {
121 // InternalError
122 err := status.Error(codes.Unknown, "A fake Unknown error")
123
124 cordErr := RpcErrorWithIdToCordError(err, "Foo", 7)
125
126 _, ok := cordErr.(*InternalError)
127 assert.True(t, ok)
128 assert.Equal(t, cordErr.Error(), "Internal Error [on model Foo <id=7>]: A fake Unknown error")
129}
130
131func TestRpcErrorWithQueriesToCordError(t *testing.T) {
132 // InternalError
133 err := status.Error(codes.Unknown, "A fake Unknown error")
134
135 cordErr := RpcErrorWithQueriesToCordError(err, "Foo", map[string]string{"id": "=3"})
136
137 _, ok := cordErr.(*InternalError)
138 assert.True(t, ok)
139 assert.Equal(t, cordErr.Error(), "Internal Error [on model Foo <id=3>]: A fake Unknown error")
140}
141
142func TestStackTrace(t *testing.T) {
143 var err error
144
145 err = WithStackTrace(&UnknownModelTypeError{Name: "foo"})
146
147 // goexit occurs near the end of the stack trace
148 assert.Contains(t, err.(CordCtlError).Stack(), "goexit")
Scott Baker20481aa2019-06-20 11:00:54 -0700149}