-
-
Notifications
You must be signed in to change notification settings - Fork 502
/
neo4j.go
109 lines (94 loc) · 2.53 KB
/
neo4j.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
package neo4j
import (
"context"
"fmt"
"net/http"
"github.com/docker/go-connections/nat"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/wait"
)
const (
// defaultImage {
defaultImageName = "neo4j"
defaultTag = "4.4"
// }
)
const (
// containerPorts {
defaultBoltPort = "7687"
defaultHttpPort = "7474"
defaultHttpsPort = "7473"
// }
)
// Neo4jContainer represents the Neo4j container type used in the module
type Neo4jContainer struct {
testcontainers.Container
}
// BoltUrl returns the bolt url for the Neo4j container, using the bolt port, in the format of neo4j://host:port
func (c Neo4jContainer) BoltUrl(ctx context.Context) (string, error) {
host, err := c.Host(ctx)
if err != nil {
return "", err
}
containerPort, err := nat.NewPort("tcp", defaultBoltPort)
if err != nil {
return "", err
}
mappedPort, err := c.MappedPort(ctx, containerPort)
if err != nil {
return "", err
}
return fmt.Sprintf("neo4j://%s:%d", host, mappedPort.Int()), nil
}
// RunContainer creates an instance of the Neo4j container type
func RunContainer(ctx context.Context, options ...testcontainers.ContainerCustomizer) (*Neo4jContainer, error) {
httpPort, _ := nat.NewPort("tcp", defaultHttpPort)
request := testcontainers.ContainerRequest{
Image: fmt.Sprintf("docker.io/%s:%s", defaultImageName, defaultTag),
Env: map[string]string{
"NEO4J_AUTH": "none",
},
ExposedPorts: []string{
fmt.Sprintf("%s/tcp", defaultBoltPort),
fmt.Sprintf("%s/tcp", defaultHttpPort),
fmt.Sprintf("%s/tcp", defaultHttpsPort),
},
WaitingFor: &wait.MultiStrategy{
Strategies: []wait.Strategy{
wait.NewLogStrategy("Bolt enabled on"),
&wait.HTTPStrategy{
Port: httpPort,
StatusCodeMatcher: isHttpOk(),
},
},
},
}
genericContainerReq := testcontainers.GenericContainerRequest{
ContainerRequest: request,
Logger: testcontainers.Logger,
Started: true,
}
if len(options) == 0 {
options = append(options, WithoutAuthentication())
}
for _, option := range options {
option.Customize(&genericContainerReq)
}
err := validate(&genericContainerReq)
if err != nil {
return nil, err
}
container, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
ContainerRequest: request,
Started: true,
})
if err != nil {
return nil, err
}
return &Neo4jContainer{Container: container}, nil
}
func isHttpOk() func(status int) bool {
return func(status int) bool {
return status == http.StatusOK
}
}