-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathha_test.go
141 lines (129 loc) · 3.26 KB
/
ha_test.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
package ha_6_node
import (
"context"
"github.com/dgraph-io/dgo/v200"
"github.com/dgraph-io/dgo/v200/protos/api"
"github.com/dgraph-io/dgraph/testutil"
"github.com/dgraph-io/dgraph/x"
"github.com/stretchr/testify/require"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"testing"
"time"
)
func runTests(t *testing.T, client *dgo.Dgraph) {
type testCase struct {
query string
wantResult string
}
suite := func(initialSchema string, setJSON string, cases []testCase) {
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
require.NoError(t, client.Alter(ctx, &api.Operation{
DropAll: true,
}))
require.NoError(t, client.Alter(ctx, &api.Operation{
Schema: initialSchema,
}))
txn := client.NewTxn()
_, err := txn.Mutate(ctx, &api.Mutation{SetJson: []byte(setJSON)})
require.NoError(t, err)
require.NoError(t, txn.Commit(ctx))
for _, test := range cases {
txn := client.NewTxn()
reply, err := txn.Query(ctx, test.query)
require.NoError(t, err)
testutil.CompareJSON(t, test.wantResult, string(reply.GetJson()))
}
}
suite(
"name: string @index(term) .",
`[
{ "name": "Michael" },
{ "name": "Amit" },
{ "name": "Luke" },
{ "name": "Darth" },
{ "name": "Sarah" },
{ "name": "Ricky" },
{ "name": "Hugo" }
]`,
[]testCase{
{`
{
q(func: eq(name, "Hugo")) {
name
}
}`, `
{
"q": [
{
"name": "Hugo"
}
]
}`,
},
},
)
}
func TestHAClusterSetup(t *testing.T) {
client := getClientForAlpha(t, "alpha1", "9180")
runTests(t, client)
}
func TestHAClusterDiffClients(t *testing.T) {
client := getClientForAlpha(t, "alpha1", "9180")
client2 := getClientForAlpha(t, "alpha2", "9280")
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
require.NoError(t, client.Alter(ctx, &api.Operation{
DropAll: true,
}))
require.NoError(t, client.Alter(ctx, &api.Operation{
Schema: "name: string @index(term) .",
}))
txn := client.NewTxn()
_, err := txn.Mutate(ctx, &api.Mutation{SetJson: []byte(`[
{ "name": "Michael" },
{ "name": "Amit" },
{ "name": "Luke" },
{ "name": "Darth" },
{ "name": "Sarah" },
{ "name": "Ricky" },
{ "name": "Hugo" }
]`)})
require.NoError(t, err)
require.NoError(t, txn.Commit(ctx))
txn = client2.NewTxn()
reply, err := txn.Query(ctx, `
{
q(func: eq(name, "Hugo")) {
name
}
}`)
require.NoError(t, err)
testutil.CompareJSON(t, `
{
"q": [
{
"name": "Hugo"
}
]
}`, string(reply.GetJson()))
}
func getClientForAlpha(t *testing.T, name string, port string) *dgo.Dgraph {
c := &x.TLSHelperConfig{
CertDir: "../tls/" + name,
CertRequired: true,
Cert: "../tls/" + name + "/client." + name + ".crt",
Key: "../tls/" + name + "/client." + name + ".key",
ServerName: name,
RootCACert: "../tls/" + name + "/ca.crt",
UseSystemCACerts: true,
}
tlsConf, err := x.GenerateClientTLSConfig(c)
require.NoError(t, err)
dgConn, err := grpc.Dial(":" + port, grpc.WithTransportCredentials(credentials.NewTLS(tlsConf)))
require.NoError(t, err)
time.Sleep(time.Second * 6)
client := dgo.NewDgraphClient(api.NewDgraphClient(dgConn))
return client
}