forked from RedisGraph/redisgraph-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_graph_test.go
161 lines (136 loc) · 3.96 KB
/
example_graph_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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package redisgraph_test
import (
"crypto/tls"
"crypto/x509"
"fmt"
"io/ioutil"
"log"
"os"
"github.com/gomodule/redigo/redis"
"github.com/RedisGraph/redisgraph-go"
)
const port = 6379
func ExampleGraphNew() {
conn, _ := redis.Dial("tcp", fmt.Sprintf("0.0.0.0:%d", port))
graph := redisgraph.GraphNew("social", conn)
q := "CREATE (w:WorkPlace {name:'RedisLabs'}) RETURN w"
res, _ := graph.Query(q)
res.Next()
r := res.Record()
w := r.GetByIndex(0).(*redisgraph.Node)
fmt.Println(w.Labels[0])
// Output: WorkPlace
}
func ExampleGraphNew_pool() {
host := fmt.Sprintf("0.0.0.0:%d", port)
pool := &redis.Pool{Dial: func() (redis.Conn, error) {
return redis.Dial("tcp", host)
}}
graph := redisgraph.GraphNew("social", pool.Get())
q := "CREATE (w:WorkPlace {name:'RedisLabs'}) RETURN w"
res, _ := graph.Query(q)
res.Next()
r := res.Record()
w := r.GetByIndex(0).(*redisgraph.Node)
fmt.Println(w.Labels[0])
// Output: WorkPlace
}
func ExampleGraphNew_tls() {
// Consider the following helper methods that provide us with the connection details (host and password)
// and the paths for:
// tls_cert - A a X.509 certificate to use for authenticating the server to connected clients, masters or cluster peers. The file should be PEM formatted
// tls_key - A a X.509 private key to use for authenticating the server to connected clients, masters or cluster peers. The file should be PEM formatted
// tls_cacert - A PEM encoded CA's certificate file
host, password := getConnectionDetails()
tlsready, tls_cert, tls_key, tls_cacert := getTLSdetails()
// Skip if we dont have all files to properly connect
if tlsready == false {
return
}
// Load client cert
cert, err := tls.LoadX509KeyPair(tls_cert, tls_key)
if err != nil {
log.Fatal(err)
}
// Load CA cert
caCert, err := ioutil.ReadFile(tls_cacert)
if err != nil {
log.Fatal(err)
}
caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM(caCert)
clientTLSConfig := &tls.Config{
Certificates: []tls.Certificate{cert},
RootCAs: caCertPool,
}
// InsecureSkipVerify controls whether a client verifies the
// server's certificate chain and host name.
// If InsecureSkipVerify is true, TLS accepts any certificate
// presented by the server and any host name in that certificate.
// In this mode, TLS is susceptible to man-in-the-middle attacks.
// This should be used only for testing.
clientTLSConfig.InsecureSkipVerify = true
pool := &redis.Pool{Dial: func() (redis.Conn, error) {
return redis.Dial("tcp", host,
redis.DialPassword(password),
redis.DialTLSConfig(clientTLSConfig),
redis.DialUseTLS(true),
redis.DialTLSSkipVerify(true),
)
}}
graph := redisgraph.GraphNew("social", pool.Get())
q := "CREATE (w:WorkPlace {name:'RedisLabs'}) RETURN w"
res, _ := graph.Query(q)
res.Next()
r := res.Record()
w := r.GetByIndex(0).(*redisgraph.Node)
fmt.Println(w.Labels[0])
}
func getConnectionDetails() (host string, password string) {
value, exists := os.LookupEnv("REDISGRAPH_TEST_HOST")
host = fmt.Sprintf("0.0.0.0:%d", port)
if exists && value != "" {
host = value
}
password = ""
valuePassword, existsPassword := os.LookupEnv("REDISGRAPH_TEST_PASSWORD")
if existsPassword && valuePassword != "" {
password = valuePassword
}
return
}
func getTLSdetails() (tlsready bool, tls_cert string, tls_key string, tls_cacert string) {
tlsready = false
value, exists := os.LookupEnv("TLS_CERT")
if exists && value != "" {
info, err := os.Stat(value)
if os.IsNotExist(err) || info.IsDir() {
return
}
tls_cert = value
} else {
return
}
value, exists = os.LookupEnv("TLS_KEY")
if exists && value != "" {
info, err := os.Stat(value)
if os.IsNotExist(err) || info.IsDir() {
return
}
tls_key = value
} else {
return
}
value, exists = os.LookupEnv("TLS_CACERT")
if exists && value != "" {
info, err := os.Stat(value)
if os.IsNotExist(err) || info.IsDir() {
return
}
tls_cacert = value
} else {
return
}
tlsready = true
return
}