-
Notifications
You must be signed in to change notification settings - Fork 32
/
container_util_test.go
147 lines (122 loc) · 4.26 KB
/
container_util_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
// Copyright (c) 2022 MindStand Technologies, Inc
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package gogm
import (
"context"
"fmt"
"time"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/wait"
)
type neo4jContainerVersion string
const (
neo3 neo4jContainerVersion = "3"
neo4 neo4jContainerVersion = "4"
)
type neo4jContainer struct {
testcontainers.Container
CertDir string
Host string
Port int
}
const neo4jContainerUsername = "neo4j"
const neo4jContainerPassword = "changeme"
func setupNeo4jContainer(ctx context.Context, version neo4jContainerVersion) (*neo4jContainer, error) {
var err error
req := testcontainers.ContainerRequest{
Image: "neo4j:3.5-enterprise",
ExposedPorts: []string{"7474/tcp", "6477/tcp", "7687/tcp"},
Env: map[string]string{
"NEO4J_ACCEPT_LICENSE_AGREEMENT": "yes",
"NEO4J_AUTH": neo4jContainerUsername + "/" + neo4jContainerPassword,
},
WaitingFor: wait.ForLog("Started."),
}
// defaults to empty string if using neo3 (no certs)
certDir := ""
if version == neo4 {
certDir, err = createTempNeoKeypair()
if err != nil {
return nil, err
}
req.Image = "neo4j:4.4-enterprise"
req.Env["NEO4J_dbms_default__listen__address"] = "0.0.0.0"
req.Env["NEO4J_dbms_allow__upgrade"] = "true"
req.Env["NEO4J_dbms_default__database"] = "neo4j"
req.Env["NEO4J_dbms_ssl_policy_bolt_enabled"] = "true"
req.Env["NEO4J_dbms_ssl_policy_bolt_base__directory"] = "/ssl/client_policy"
req.Env["NEO4J_dbms_connector_bolt_tls__level"] = "OPTIONAL"
// mount certificate directory
req.Mounts = testcontainers.ContainerMounts{
testcontainers.BindMount(certDir, "/ssl/client_policy"),
}
}
container, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
ContainerRequest: req,
Started: true,
})
if err != nil {
if certDir != "" {
cleanupTempNeoKeypair(certDir)
}
return nil, err
}
host, err := container.Host(ctx)
if err != nil {
if certDir != "" {
cleanupTempNeoKeypair(certDir)
}
return nil, err
}
port, err := container.MappedPort(ctx, "7687")
if err != nil {
if certDir != "" {
cleanupTempNeoKeypair(certDir)
}
return nil, err
}
return &neo4jContainer{Container: container, CertDir: certDir, Host: host, Port: port.Int()}, nil
}
func (n neo4jContainer) GetGogmConfig() *Config {
return &Config{
Username: neo4jContainerUsername,
Password: neo4jContainerPassword,
Host: n.Host,
IsCluster: false,
Port: n.Port,
PoolSize: 15,
EnableDriverLogs: false,
DefaultTransactionTimeout: 2 * time.Minute,
}
}
func (n *neo4jContainer) Teminate(ctx context.Context) error {
var keyPairCleanupErr error
if n.CertDir != "" {
keyPairCleanupErr = cleanupTempNeoKeypair(n.CertDir)
}
containerCleanupErr := n.Container.Terminate(ctx)
if keyPairCleanupErr != nil && containerCleanupErr != nil {
return fmt.Errorf("container termination (%v) and keypair cleanup (%v) failed", containerCleanupErr, keyPairCleanupErr)
} else if keyPairCleanupErr != nil {
return keyPairCleanupErr
} else if containerCleanupErr != nil {
return containerCleanupErr
}
return nil
}