forked from Percona-Lab/pmm-api-tests
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.go
214 lines (179 loc) · 5.58 KB
/
helpers.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
package pmmapitests
import (
"context"
"fmt"
"math/rand"
"reflect"
"testing"
"github.com/percona/pmm/api/inventorypb/json/client"
"github.com/percona/pmm/api/inventorypb/json/client/agents"
"github.com/percona/pmm/api/inventorypb/json/client/nodes"
"github.com/percona/pmm/api/inventorypb/json/client/services"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"google.golang.org/grpc/codes"
)
type ErrorResponse interface {
Code() int
}
// A minimal subset of *testing.T that we use that is also should be implemented by *expectedFailureTestingT.
type TestingT interface {
Helper()
Name() string
Errorf(format string, args ...interface{})
FailNow()
}
// TestString returns semi-random string that can be used as a test data.
func TestString(t TestingT, name string) string {
t.Helper()
n := rand.Int() //nolint:gosec
return fmt.Sprintf("pmm-api-tests/%s/%s/%s/%d", Hostname, t.Name(), name, n)
}
// AssertAPIErrorf check that actual API error equals expected.
func AssertAPIErrorf(t TestingT, actual error, httpStatus int, grpcCode codes.Code, format string, a ...interface{}) {
t.Helper()
require.Error(t, actual)
require.Implementsf(t, new(ErrorResponse), actual, "Wrong response type. Expected %T, got %T.\nError message: %v", new(ErrorResponse), actual, actual)
assert.Equal(t, httpStatus, actual.(ErrorResponse).Code())
// Have to use reflect because there are a lot of types with the same structure and different names.
payload := reflect.ValueOf(actual).Elem().FieldByName("Payload")
require.True(t, payload.IsValid(), "Wrong response structure. There is no field Payload.")
codeField := payload.Elem().FieldByName("Code")
require.True(t, codeField.IsValid(), "Wrong response structure. There is no field Code in Payload.")
assert.Equal(t, int64(grpcCode), codeField.Int(), "gRPC status codes are not equal")
errorField := payload.Elem().FieldByName("Error")
require.True(t, errorField.IsValid(), "Wrong response structure. There is no field Error in Payload.")
if len(a) > 0 {
format = fmt.Sprintf(format, a...)
}
assert.Equal(t, format, errorField.String())
}
func ExpectFailure(t *testing.T, link string) *expectedFailureTestingT {
return &expectedFailureTestingT{
t: t,
link: link,
}
}
// expectedFailureTestingT expects that test will fail.
// if test is failed we skip it
// if it doesn't we call Fail
type expectedFailureTestingT struct {
t *testing.T
errors []string
failed bool
link string
}
func (tt *expectedFailureTestingT) Helper() { tt.t.Helper() }
func (tt *expectedFailureTestingT) Name() string { return tt.t.Name() }
func (tt *expectedFailureTestingT) Errorf(format string, args ...interface{}) {
tt.errors = append(tt.errors, fmt.Sprintf(format, args...))
tt.failed = true
}
func (tt *expectedFailureTestingT) FailNow() {
tt.failed = true
// We have to set unexported testing.T.finished = true to make everything work,
// but we can't call tt.t.FailNow() as it calls Fail().
tt.t.SkipNow()
}
func (tt *expectedFailureTestingT) Check() {
tt.t.Helper()
if tt.failed {
for _, v := range tt.errors {
tt.t.Log(v)
}
tt.t.Skipf("Expected failure: %s", tt.link)
return
}
tt.t.Fatalf("%s expected to fail, but didn't: %s", tt.Name(), tt.link)
}
func RemoveNodes(t TestingT, nodeIDs ...string) {
t.Helper()
for _, nodeID := range nodeIDs {
params := &nodes.RemoveNodeParams{
Body: nodes.RemoveNodeBody{
NodeID: nodeID,
},
Context: context.Background(),
}
res, err := client.Default.Nodes.RemoveNode(params)
assert.NoError(t, err)
assert.NotNil(t, res)
}
}
func RemoveServices(t TestingT, serviceIDs ...string) {
t.Helper()
for _, serviceID := range serviceIDs {
params := &services.RemoveServiceParams{
Body: services.RemoveServiceBody{
ServiceID: serviceID,
Force: true,
},
Context: context.Background(),
}
res, err := client.Default.Services.RemoveService(params)
assert.NoError(t, err)
assert.NotNil(t, res)
}
}
func RemoveAgents(t TestingT, agentIDs ...string) {
t.Helper()
for _, agentID := range agentIDs {
params := &agents.RemoveAgentParams{
Body: agents.RemoveAgentBody{
AgentID: agentID,
},
Context: context.Background(),
}
res, err := client.Default.Agents.RemoveAgent(params)
assert.NoError(t, err)
assert.NotNil(t, res)
}
}
func AddGenericNode(t TestingT, nodeName string) *nodes.AddGenericNodeOKBodyGeneric {
t.Helper()
params := &nodes.AddGenericNodeParams{
Body: nodes.AddGenericNodeBody{
NodeName: nodeName,
Address: "10.10.10.10",
},
Context: Context,
}
res, err := client.Default.Nodes.AddGenericNode(params)
assert.NoError(t, err)
require.NotNil(t, res)
require.NotNil(t, res.Payload)
require.NotNil(t, res.Payload.Generic)
return res.Payload.Generic
}
func AddRemoteNode(t TestingT, nodeName string) *nodes.AddRemoteNodeOKBody {
t.Helper()
params := &nodes.AddRemoteNodeParams{
Body: nodes.AddRemoteNodeBody{
NodeName: nodeName,
Address: "10.10.10.10",
},
Context: Context,
}
res, err := client.Default.Nodes.AddRemoteNode(params)
assert.NoError(t, err)
require.NotNil(t, res)
return res.Payload
}
func AddPMMAgent(t TestingT, nodeID string) *agents.AddPMMAgentOKBody {
t.Helper()
res, err := client.Default.Agents.AddPMMAgent(&agents.AddPMMAgentParams{
Body: agents.AddPMMAgentBody{
RunsOnNodeID: nodeID,
},
Context: Context,
})
assert.NoError(t, err)
require.NotNil(t, res)
return res.Payload
}
// check interfaces
var (
_ assert.TestingT = (*expectedFailureTestingT)(nil)
_ require.TestingT = (*expectedFailureTestingT)(nil)
_ TestingT = (*expectedFailureTestingT)(nil)
)