/
cypher.go
327 lines (287 loc) · 11.6 KB
/
cypher.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
package integration_tests
import (
"context"
"errors"
"fmt"
"github.com/neo4j/helm-charts/internal/model"
"github.com/neo4j/neo4j-go-driver/v5/neo4j"
"github.com/stretchr/testify/assert"
"strings"
"sync/atomic"
"testing"
"time"
)
// Auth stuff
const dbUri = "neo4j+ssc://localhost"
const boltDbUri = "bolt+ssc://localhost"
const user = "neo4j"
const dbName = "neo4j"
const systemDbName = "system"
var authToUse = neo4j.BasicAuth(user, model.DefaultPassword, "")
// Track the total number of nodes that we've created
var createdNodes = map[model.ReleaseName]*int64{}
// empty param map (makes queries without params more readable)
var noParams = map[string]interface{}{}
func checkNeo4jConfiguration(t *testing.T, releaseName model.ReleaseName, expectedConfiguration *model.Neo4jConfiguration) (err error) {
var runtimeConfig []*neo4j.Record
var expectedOverrides = map[string]string{
"server.https.enabled": "true",
"server.bolt.tls_level": "REQUIRED",
"server.directories.logs": "/logs",
"server.directories.import": "/import",
"server.panic.shutdown_on_panic": "true",
}
if model.Neo4jEdition == "enterprise" {
expectedOverrides["server.directories.metrics"] = "/metrics"
}
deadline := time.Now().Add(3 * time.Minute)
for true {
if !time.Now().Before(deadline) {
msg := fmt.Sprintf("timed out fetching config: %d", len(runtimeConfig))
t.Error(msg)
return errors.New(msg)
}
runtimeConfig, err = runQuery(t, releaseName, "CALL dbms.listConfig() YIELD name, value", nil, model.Neo4jEdition == "community")
if err != nil {
return err
}
if len(runtimeConfig) >= len(expectedConfiguration.Conf()) {
break
}
}
for key, value := range expectedOverrides {
expectedConfiguration.Conf()[key] = value
}
for _, record := range runtimeConfig {
nameUntyped, foundName := record.Get("name")
valueUntyped, foundValue := record.Get("value")
if !(foundName && foundValue) {
return fmt.Errorf("record is missing expected name or value")
}
if valueUntyped == nil {
valueUntyped = ""
}
name := nameUntyped.(string)
value := valueUntyped.(string)
if expectedValue, found := expectedConfiguration.Conf()[name]; found {
assert.Equal(t, strings.ToLower(expectedValue), strings.ToLower(value),
"Expected runtime config for %s to match provided value", name)
}
if name == "server.jvm.additional" {
assert.Equal(t, expectedConfiguration.JvmArgs(), strings.Split(value, "\n"))
}
}
if err == nil {
t.Log("Configuration check passed for:", releaseName.String())
}
return err
}
func createNode(t *testing.T, releaseName model.ReleaseName) error {
_, err := runQuery(t, releaseName, "CREATE (n:Item { id: $id, name: $name }) RETURN n.id, n.name", map[string]interface{}{
"id": 1,
"name": "Item 1",
},
model.Neo4jEdition == "community")
if _, found := createdNodes[releaseName]; !found {
var initialValue int64 = 0
createdNodes[releaseName] = &initialValue
}
if err == nil {
atomic.AddInt64(createdNodes[releaseName], 1)
}
return err
}
// createDatabase runs a cypher query to create a database with the given name
func createDatabase(t *testing.T, releaseName model.ReleaseName, databaseName string) error {
cypherQuery := fmt.Sprintf("CREATE DATABASE %s", databaseName)
_, err := runQuery(t, releaseName, cypherQuery, nil, false)
if !assert.NoError(t, err) {
return fmt.Errorf("error seen while creating database %s , err := %v", databaseName, err)
}
//sleep is required so that CheckDataBase is able to fetch the above created database
//It takes few seconds for the new database to be updated.
// Do not reduce the time to anything less than 10 , tests would fail
time.Sleep(10 * time.Second)
return nil
}
// stopDatabase runs a cypher query to stop a database with the given name
func stopDatabase(t *testing.T, releaseName model.ReleaseName, databaseName string) error {
cypherQuery := fmt.Sprintf("STOP DATABASE %s", databaseName)
_, err := runQueryViaSystemDB(t, releaseName, cypherQuery, nil, false)
if !assert.NoError(t, err) {
return fmt.Errorf("error seen while stopping database %s , err := %v", databaseName, err)
}
return nil
}
// startDatabase runs a cypher query to start a database with the given name
func startDatabase(t *testing.T, releaseName model.ReleaseName, databaseName string) error {
cypherQuery := fmt.Sprintf("START DATABASE %s", databaseName)
_, err := runQueryViaSystemDB(t, releaseName, cypherQuery, nil, false)
if !assert.NoError(t, err) {
return fmt.Errorf("error seen while starting database %s , err := %v", databaseName, err)
}
return nil
}
// createMoviesDataSet runs movie dataset cypher query
func createMoviesDataSet(t *testing.T, releaseName model.ReleaseName) error {
_, err := runQuery(t, releaseName, model.MOVIES_CYPHER, nil, false)
if !assert.NoError(t, err) {
return fmt.Errorf("error seen while creating movies dataset, err := %v", err)
}
return nil
}
// checkDataBaseExists runs a cypher query to check if the given database name exists or not
func checkDataBaseExists(t *testing.T, releaseName model.ReleaseName, databaseName string) error {
cypherQuery := fmt.Sprintf("SHOW DATABASE %s YIELD name", databaseName)
results, err := runQuery(t, releaseName, cypherQuery, nil, false)
if !assert.NoError(t, err) {
t.Logf("%v", err)
return fmt.Errorf("error seen while creating database %s , err := %v", databaseName, err)
}
if !assert.NotEqual(t, len(results), 0) {
return fmt.Errorf("no results received from cypher query")
}
for _, result := range results {
if value, found := result.Get("name"); found {
if assert.Equal(t, value, databaseName) {
return nil
}
}
}
return fmt.Errorf("no record yielded for cypher query %s", cypherQuery)
}
// checkApocConfig fires a apoc cypher query
// It's a way to check if apoc plugin is loaded and the customized apoc config is loaded or not
func checkApocConfig(t *testing.T, releaseName model.ReleaseName) error {
results, err := runQuery(t, releaseName, "CALL apoc.create.node([\"Person\", \"Actor\"], {name: \"Tom Hanks\"});", nil, false)
if !assert.NoError(t, err) {
t.Logf("%v", err)
return fmt.Errorf("error seen while firing apoc cypher \n err := %v", err)
}
if !assert.NotEqual(t, len(results), 0) {
return fmt.Errorf("no results received from cypher query")
}
return nil
}
// checkNodeCount runs the cypher query to get the number of nodes on a cluster core
func checkNodeCount(t *testing.T, releaseName model.ReleaseName) error {
result, err := runQuery(t, releaseName, "MATCH (n) RETURN COUNT(n) AS count", noParams, model.Neo4jEdition == "community")
if err != nil {
return err
}
if value, found := result[0].Get("count"); found {
countedNodes := value.(int64)
assert.Equal(t, atomic.LoadInt64(createdNodes[releaseName]), countedNodes)
return err
} else {
return fmt.Errorf("expected at least one result")
}
}
// checkLdapPassword runs a cypher query to get ldapPassword and checks if the ldapPassword is set or not
func checkLdapPassword(t *testing.T, releaseName model.ReleaseName) error {
result, err := runQuery(t, releaseName, "CALL dbms.listConfig('dbms.security.ldap.authorization.system_password') YIELD value", noParams, model.Neo4jEdition == "community")
if err != nil {
return err
}
value, found := result[0].Get("value")
if !found {
return fmt.Errorf("expected at least one result")
}
ldapPass := value.(string)
assert.NotEqual(t, ldapPass, "No Value", "LdapPassword not set !!")
return nil
}
// checkBloomVersion runs the cypher query to get bloom license info
func checkBloomVersion(t *testing.T, releaseName model.ReleaseName) error {
result, err := runQuery(t, releaseName, "CALL bloom.checkLicenseCompliance() YIELD status;", noParams, model.Neo4jEdition == "community")
if err != nil {
return err
}
value, found := result[0].Get("status")
if !found {
return fmt.Errorf("expected bloom license status, found nothing !!")
}
status := value.(string)
assert.Equal(t, status, "valid", fmt.Sprintf("bloom license status found %s is not matching with 'valid'", status))
return nil
}
func runQuery(t *testing.T, releaseName model.ReleaseName, cypher string, params map[string]interface{}, connectToPod bool) ([]*neo4j.Record, error) {
boltPort, cleanupProxy, proxyErr := proxyBolt(t, releaseName, connectToPod)
defer cleanupProxy()
if proxyErr != nil {
return nil, proxyErr
}
ctx := context.Background()
driver, err := neo4j.NewDriverWithContext(fmt.Sprintf("%s:%d", dbUri, boltPort), authToUse, func(config *neo4j.Config) {
})
// Handle driver lifetime based on your application lifetime requirements driver's lifetime is usually
// bound by the application lifetime, which usually implies one driver instance per application
defer driver.Close(ctx)
if err := awaitConnectivity(t, err, driver, ctx); err != nil {
return nil, err
}
// Sessions are shortlived, cheap to create and NOT thread safe. Typically create one or more sessions
// per request in your web application. Make sure to call Close on the session when done.
// For multidatabase support, set sessionConfig.DatabaseName to requested database
// Session config will default to write mode, if only reads are to be used configure session for
// read mode.
session := driver.NewSession(ctx, neo4j.SessionConfig{DatabaseName: dbName})
defer session.Close(ctx)
result, err := session.Run(ctx, cypher, params)
if err != nil {
return nil, err
}
return result.Collect(ctx)
}
func runQueryViaSystemDB(t *testing.T, releaseName model.ReleaseName, cypher string, params map[string]interface{}, connectToPod bool) ([]*neo4j.Record, error) {
boltPort, cleanupProxy, proxyErr := proxyBolt(t, releaseName, connectToPod)
defer cleanupProxy()
if proxyErr != nil {
return nil, proxyErr
}
ctx := context.Background()
driver, err := neo4j.NewDriverWithContext(fmt.Sprintf("%s:%d", boltDbUri, boltPort), authToUse, func(config *neo4j.Config) {
})
// Handle driver lifetime based on your application lifetime requirements driver's lifetime is usually
// bound by the application lifetime, which usually implies one driver instance per application
defer driver.Close(ctx)
if err := awaitConnectivity(t, err, driver, ctx); err != nil {
return nil, err
}
// Sessions are shortlived, cheap to create and NOT thread safe. Typically create one or more sessions
// per request in your web application. Make sure to call Close on the session when done.
// For multidatabase support, set sessionConfig.DatabaseName to requested database
// Session config will default to write mode, if only reads are to be used configure session for
// read mode.
session := driver.NewSession(ctx, neo4j.SessionConfig{DatabaseName: systemDbName})
defer session.Close(ctx)
result, err := session.Run(ctx, cypher, params)
if err != nil {
return nil, err
}
return result.Collect(ctx)
}
func awaitConnectivity(t *testing.T, err error, driver neo4j.DriverWithContext, ctx context.Context) error {
// This polls verify connectivity until it succeeds or it times out. We should be able to remove this when we have readiness probes (maybe)
start := time.Now()
timeoutAfter := time.Minute * 3
for {
t.Log("Checking connectivity for ", dbUri)
err = driver.VerifyConnectivity(ctx)
if err == nil {
t.Log("Connectivity check passed for ", dbUri)
return nil
} else if neo4j.IsNeo4jError(err) && strings.Contains(err.(*neo4j.Neo4jError).Code, "CredentialsExpired") {
t.Logf("recieved CredentialsExpired message from driver. Attempting to proceed")
return nil
} else {
elapsed := time.Now().Sub(start)
if elapsed > timeoutAfter {
return err
} else {
t.Logf("Connectivity check failed (%s), retrying...", err)
time.Sleep(5 * time.Second)
}
}
}
}