/
neo4jhelper.go
57 lines (50 loc) · 1.44 KB
/
neo4jhelper.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
package testhelper
import (
"context"
"fmt"
"github.com/hashicorp/vault/helper/testhelpers/docker"
"github.com/neo4j/neo4j-go-driver/v5/neo4j"
"net/url"
"os"
"testing"
)
func PrepareTestContainer(t *testing.T, version string) (func(), string) {
if os.Getenv("NEO4J_URL") != "" {
return func() {}, os.Getenv("NEO4J_URL")
}
if version == "" {
version = "enterprise"
}
runner, err := docker.NewServiceRunner(docker.RunOptions{
ImageRepo: "neo4j",
ImageTag: version,
Env: []string{"NEO4J_ACCEPT_LICENSE_AGREEMENT=yes", "NEO4J_AUTH=neo4j/secret"},
Ports: []string{"7687/tcp", "7474/tcp"},
})
if err != nil {
t.Fatalf("Could not start docker Neo4j: %s", err)
}
svc, err := runner.StartService(context.Background(), connectNeo4j)
if err != nil {
t.Fatalf("Could not start docker Neo4j: %s", err)
}
return svc.Cleanup, svc.Config.URL().String()
}
func connectNeo4j(ctx context.Context, host string, port int) (docker.ServiceConfig, error) {
u := url.URL{
Scheme: "neo4j",
Host: fmt.Sprintf("%s:%d", host, port),
User: url.UserPassword("neo4j", "secret"),
}
pass, _ := u.User.Password()
driver, err := neo4j.NewDriverWithContext(u.String(), neo4j.BasicAuth(u.User.Username(), pass, ""))
if err != nil {
return nil, err
}
defer driver.Close(ctx)
err = driver.VerifyConnectivity(ctx)
if err != nil {
return nil, fmt.Errorf("could not verify neo4j connection: %w", err)
}
return docker.NewServiceURL(u), nil
}