Debian Codegraph @

client/apicallerror_test.go

raw
1 package client_test
2
3 import (
4 "errors"
5 "testing"
6
7 "github.com/stretchr/testify/assert"
8
9 linstor "github.com/LINBIT/golinstor"
10 "github.com/LINBIT/golinstor/client"
11 )
12
13 func TestRcIs(t *testing.T) {
14 cases := []struct {
15 descr string
16 rc int64
17 mask uint64
18 expectIs bool
19 }{{
20 descr: "exact match",
21 rc: -4611686018406153244,
22 mask: linstor.FailNotEnoughNodes,
23 expectIs: true,
24 }, {
25 descr: "wrong value",
26 rc: -4611686018406153244,
27 mask: linstor.FailInvldNodeName,
28 expectIs: false,
29 }}
30
31 for _, c := range cases {
32 r := client.ApiCallRc{RetCode: c.rc}
33
34 is := r.Is(c.mask)
35
36 if is && !c.expectIs {
37 t.Errorf("Case \"%s\": mask unexpectedly matches", c.descr)
38 t.Errorf("RetCode: %064b", uint64(c.rc))
39 t.Errorf("Mask: %064b", c.mask)
40 t.Errorf("And: %064b", uint64(c.rc)&c.mask)
41 }
42 if !is && c.expectIs {
43 t.Errorf("Case \"%s\": mask unexpectedly does not match", c.descr)
44 t.Errorf("RetCode: %064b", uint64(c.rc))
45 t.Errorf("Mask: %064b", c.mask)
46 t.Errorf("And: %064b", uint64(c.rc)&c.mask)
47 }
48 }
49 }
50
51 func TestApiCallErrorIs(t *testing.T) {
52 // Random, observed error
53 err := client.ApiCallError{
54 {
55 RetCode: 4611686018481137428,
56 Message: "Tie breaker marked for deletion",
57 ObjRefs: map[string]string{
58 "RscDfn": "test1",
59 "Node": "n2.example.com",
60 },
61 },
62 {
63 RetCode: 53739522,
64 Message: "Node: n2.example.com, Resource: test1 preparing for deletion.",
65 Details: "Node: n2.example.com, Resource: test1 UUID is: 54cb972d-13b1-4894-aa34-65c87b53e3af",
66 ObjRefs: map[string]string{
67 "RscDfn": "test1",
68 "Node": "n2.example.com",
69 },
70 },
71 {
72 RetCode: -4611686018373647389,
73 Message: "No connection to satellite 'n2.example.com'",
74 ObjRefs: map[string]string{
75 "RscDfn": "test1",
76 "Node": "n2.example.com",
77 },
78 },
79 {
80 RetCode: 53739523,
81 Message: "(n3.example.com) Resource 'test1' [DRBD] adjusted.",
82 ObjRefs: map[string]string{
83 "RscDfn": "test1",
84 "Node": "n2.example.com",
85 },
86 },
87 {
88 RetCode: 53739523,
89 Message: "Preparing deletion of resource on 'n3.example.com'",
90 ObjRefs: map[string]string{
91 "RscDfn": "test1",
92 "Node": "n2.example.com",
93 },
94 },
95 {
96 RetCode: 53739523,
97 Message: "(n1.example.com) Resource 'test1' [DRBD] adjusted.",
98 ObjRefs: map[string]string{
99 "RscDfn": "test1",
100 "Node": "n2.example.com",
101 },
102 },
103 {
104 RetCode: 53739523,
105 Message: "Preparing deletion of resource on 'n1.example.com'",
106 ObjRefs: map[string]string{
107 "RscDfn": "test1",
108 "Node": "n2.example.com",
109 },
110 },
111 {
112 RetCode: -4611686018373647386,
113 Message: "Deletion of resource 'test1' on node 'n2.example.com' failed due to an unhandled exception of type DelayedApiRcException. Exceptions have been converted to responses",
114 Details: "Node: n2.example.com, Resource: test1",
115 ErrorReportIds: []string{
116 "68663093-00000-000003",
117 },
118 ObjRefs: map[string]string{
119 "RscDfn": "test1",
120 "Node": "n2.example.com",
121 },
122 },
123 }
124
125 assert.True(t, client.IsApiCallError(err, linstor.FailNotConnected))
126 assert.True(t, client.IsApiCallError(err, linstor.FailUnknownError))
127 assert.False(t, client.IsApiCallError(err, linstor.FailNotEnoughNodes))
128 assert.False(t, client.IsApiCallError(err, linstor.FailAccDeniedCommand))
129
130 assert.True(t, errors.Is(err, client.ApiCallRcErr(linstor.FailNotConnected)))
131 assert.True(t, errors.Is(err, client.ApiCallRcErr(linstor.FailUnknownError)))
132 assert.False(t, errors.Is(err, client.ApiCallRcErr(linstor.FailNotEnoughNodes)))
133 assert.False(t, errors.Is(err, client.ApiCallRcErr(linstor.FailAccDeniedCommand)))
134 }