-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
client.go
329 lines (289 loc) · 9.95 KB
/
client.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
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
/*
* Copyright 2019 Dgraph Labs, Inc. and Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package testutil
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"os/exec"
"strconv"
"strings"
"testing"
"time"
"github.com/dgraph-io/dgo/v2"
"github.com/dgraph-io/dgo/v2/protos/api"
"github.com/dgraph-io/dgraph/x"
"github.com/pkg/errors"
"github.com/spf13/viper"
"github.com/stretchr/testify/require"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
)
// socket addr = IP address and port number
var (
// SockAddr is the address to the gRPC endpoint of the alpha used during tests.
SockAddr string
// SockAddrHttp is the address to the HTTP of alpha used during tests.
SockAddrHttp string
// SockAddrZero is the address to the gRPC endpoint of the zero used during tests.
SockAddrZero string
// SockAddrZeroHttp is the address to the HTTP endpoint of the zero used during tests.
SockAddrZeroHttp string
)
// This allows running (most) tests against dgraph running on the default ports, for example.
// Only the GRPC ports are needed and the others are deduced.
func init() {
var grpcPort int
getPort := func(envVar string, dfault int) int {
p := os.Getenv(envVar)
if p == "" {
return dfault
}
port, _ := strconv.Atoi(p)
return port
}
grpcPort = getPort("TEST_PORT_ALPHA", 9180)
SockAddr = fmt.Sprintf("localhost:%d", grpcPort)
SockAddrHttp = fmt.Sprintf("localhost:%d", grpcPort-1000)
grpcPort = getPort("TEST_PORT_ZERO", 5180)
SockAddrZero = fmt.Sprintf("localhost:%d", grpcPort)
SockAddrZeroHttp = fmt.Sprintf("localhost:%d", grpcPort+1000)
}
// DgraphClientDropAll creates a Dgraph client and drops all existing data.
// It is intended to be called from TestMain() to establish a Dgraph connection shared
// by all tests, so there is no testing.T instance for it to use.
func DgraphClientDropAll(serviceAddr string) (*dgo.Dgraph, error) {
dg, err := DgraphClient(serviceAddr)
for {
// keep retrying until we succeed or receive a non-retriable error
err := dg.Alter(context.Background(), &api.Operation{DropAll: true})
if err == nil || !strings.Contains(err.Error(), "Please retry") {
break
}
time.Sleep(time.Second)
}
return dg, err
}
// DgraphClientWithGroot creates a Dgraph client with groot permissions set up.
// It is intended to be called from TestMain() to establish a Dgraph connection shared
// by all tests, so there is no testing.T instance for it to use.
func DgraphClientWithGroot(serviceAddr string) (*dgo.Dgraph, error) {
dg, err := DgraphClient(serviceAddr)
if err != nil {
return nil, err
}
ctx := context.Background()
for {
// keep retrying until we succeed or receive a non-retriable error
err = dg.Login(ctx, x.GrootId, "password")
if err == nil || !strings.Contains(err.Error(), "Please retry") {
break
}
time.Sleep(time.Second)
}
return dg, err
}
// DgraphClient creates a Dgraph client.
// It is intended to be called from TestMain() to establish a Dgraph connection shared
// by all tests, so there is no testing.T instance for it to use.
func DgraphClient(serviceAddr string) (*dgo.Dgraph, error) {
conn, err := grpc.Dial(serviceAddr, grpc.WithInsecure())
if err != nil {
return nil, err
}
dg := dgo.NewDgraphClient(api.NewDgraphClient(conn))
return dg, nil
}
// DgraphClientWithCerts creates a Dgraph client with TLS configured using the given
// viper configuration.
// It is intended to be called from TestMain() to establish a Dgraph connection shared
// by all tests, so there is no testing.T instance for it to use.
func DgraphClientWithCerts(serviceAddr string, conf *viper.Viper) (*dgo.Dgraph, error) {
tlsCfg, err := x.LoadClientTLSConfig(conf)
if err != nil {
return nil, err
}
dialOpts := []grpc.DialOption{}
if tlsCfg != nil {
dialOpts = append(dialOpts, grpc.WithTransportCredentials(credentials.NewTLS(tlsCfg)))
} else {
dialOpts = append(dialOpts, grpc.WithInsecure())
}
conn, err := grpc.Dial(serviceAddr, dialOpts...)
if err != nil {
return nil, err
}
dg := dgo.NewDgraphClient(api.NewDgraphClient(conn))
return dg, nil
}
// DropAll drops all the data in the Dgraph instance associated with the given client.
func DropAll(t *testing.T, dg *dgo.Dgraph) {
err := dg.Alter(context.Background(), &api.Operation{DropAll: true})
require.NoError(t, err)
}
// RetryQuery will retry a query until it succeeds or a non-retryable error is received.
func RetryQuery(dg *dgo.Dgraph, q string) (*api.Response, error) {
for {
resp, err := dg.NewTxn().Query(context.Background(), q)
if err != nil && strings.Contains(err.Error(), "Please retry") {
time.Sleep(10 * time.Millisecond)
continue
}
return resp, err
}
}
// RetryMutation will retry a mutation until it succeeds or a non-retryable error is received.
// The mutation should have CommitNow set to true.
func RetryMutation(dg *dgo.Dgraph, mu *api.Mutation) error {
for {
_, err := dg.NewTxn().Mutate(context.Background(), mu)
if err != nil && (strings.Contains(err.Error(), "Please retry") ||
strings.Contains(err.Error(), "Tablet isn't being served by this instance")) {
time.Sleep(10 * time.Millisecond)
continue
}
return err
}
}
// LoginParams stores the information needed to perform a login request.
type LoginParams struct {
Endpoint string
UserID string
Passwd string
RefreshJwt string
}
// HttpLogin sends a HTTP request to the server
// and returns the access JWT and refresh JWT extracted from
// the HTTP response
func HttpLogin(params *LoginParams) (string, string, error) {
loginPayload := api.LoginRequest{}
if len(params.RefreshJwt) > 0 {
loginPayload.RefreshToken = params.RefreshJwt
} else {
loginPayload.Userid = params.UserID
loginPayload.Password = params.Passwd
}
body, err := json.Marshal(&loginPayload)
if err != nil {
return "", "", errors.Wrapf(err, "unable to marshal body")
}
req, err := http.NewRequest("POST", params.Endpoint, bytes.NewBuffer(body))
if err != nil {
return "", "", errors.Wrapf(err, "unable to create request")
}
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return "", "", errors.Wrapf(err, "login through curl failed")
}
defer resp.Body.Close()
respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", "", errors.Wrapf(err, "unable to read from response")
}
var outputJson map[string]map[string]string
if err := json.Unmarshal(respBody, &outputJson); err != nil {
var errOutputJson map[string]interface{}
if err := json.Unmarshal(respBody, &errOutputJson); err == nil {
if _, ok := errOutputJson["errors"]; ok {
return "", "", errors.Errorf("response error: %v", string(respBody))
}
}
return "", "", errors.Wrapf(err, "unable to unmarshal the output to get JWTs")
}
data, found := outputJson["data"]
if !found {
return "", "", errors.Wrapf(err, "data entry found in the output")
}
newAccessJwt, found := data["accessJWT"]
if !found {
return "", "", errors.Errorf("no access JWT found in the output")
}
newRefreshJwt, found := data["refreshJWT"]
if !found {
return "", "", errors.Errorf("no refresh JWT found in the output")
}
return newAccessJwt, newRefreshJwt, nil
}
// GrootHttpLogin logins using the groot account with the default password
// and returns the access JWT
func GrootHttpLogin(endpoint string) (string, string) {
accessJwt, refreshJwt, err := HttpLogin(&LoginParams{
Endpoint: endpoint,
UserID: x.GrootId,
Passwd: "password",
})
x.Check(err)
return accessJwt, refreshJwt
}
// CurlFailureConfig stores information about the expected failure of a curl test.
type CurlFailureConfig struct {
ShouldFail bool
CurlErrMsg string
DgraphErrMsg string
}
type curlErrorEntry struct {
Code string `json:"code"`
Message string `json:"message"`
}
type curlOutput struct {
Data map[string]interface{} `json:"data"`
Errors []curlErrorEntry `json:"errors"`
}
func verifyOutput(t *testing.T, bytes []byte, failureConfig *CurlFailureConfig) {
output := curlOutput{}
require.NoError(t, json.Unmarshal(bytes, &output),
"unable to unmarshal the curl output")
if failureConfig.ShouldFail {
require.True(t, len(output.Errors) > 0, "no error entry found")
if len(failureConfig.DgraphErrMsg) > 0 {
errorEntry := output.Errors[0]
require.True(t, strings.Contains(errorEntry.Message, failureConfig.DgraphErrMsg),
fmt.Sprintf("the failure msg\n%s\nis not part of the curl error output:%s\n",
failureConfig.DgraphErrMsg, errorEntry.Message))
}
} else {
require.True(t, output.Data != nil,
fmt.Sprintf("no data entry found in the output:%+v", output))
}
}
// VerifyCurlCmd executes the curl command with the given arguments and verifies
// the result against the expected output.
func VerifyCurlCmd(t *testing.T, args []string,
failureConfig *CurlFailureConfig) {
queryCmd := exec.Command("curl", args...)
output, err := queryCmd.Output()
if len(failureConfig.CurlErrMsg) > 0 {
// the curl command should have returned an non-zero code
require.Error(t, err, "the curl command should have failed")
if ee, ok := err.(*exec.ExitError); ok {
require.True(t, strings.Contains(string(ee.Stderr), failureConfig.CurlErrMsg),
"the curl output does not contain the expected output")
}
} else {
require.NoError(t, err, "the curl command should have succeeded")
verifyOutput(t, output, failureConfig)
}
}
// AssignUids talks to zero to assign the given number of uids.
func AssignUids(num uint64) error {
_, err := http.Get(fmt.Sprintf("http://"+SockAddrZeroHttp+"/assign?what=uids&num=%d", num))
return err
}