forked from neo4j-drivers/testkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
427 lines (377 loc) · 15.9 KB
/
main.py
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
"""
Runs all test suites in their appropriate context.
The same test suite might be executed on different Neo4j server versions or
setups, it is the responsibility of this script to setup these contexts and
orchestrate which suites that are executed in each context.
"""
import os
import sys
import atexit
import subprocess
import shutil
from tests.testenv import (
begin_test_suite, end_test_suite, in_teamcity)
import docker
import teamcity
import neo4j
networks = ["the-bridge"]
def cleanup():
docker.cleanup()
for n in networks:
subprocess.run(["docker", "network", "rm", n],
check=False, stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL)
def ensure_driver_image(root_path, driver_glue_path, branch_name, driver_name):
""" Ensures that an up to date Docker image exists for the driver.
"""
# Construct Docker image name from driver name (i.e drivers-go) and
# branch name (i.e 4.2, go-1.14-image)
image_name = "drivers-%s:%s" % (driver_name, branch_name)
# Copy CAs that the driver should know of to the Docker build context
# (first remove any previous...). Each driver container should contain
# those CAs in such a way that driver language can use them as system
# CAs without any custom modification of the driver.
cas_path = os.path.join(driver_glue_path, "CAs")
shutil.rmtree(cas_path, ignore_errors=True)
cas_source_path = os.path.join(root_path, "tests", "tls",
"certs", "driver")
shutil.copytree(cas_source_path, cas_path)
# This will use the driver folder as build context.
print("Building driver Docker image %s from %s"
% (image_name, driver_glue_path))
subprocess.check_call([
"docker", "build", "--tag", image_name, driver_glue_path])
print("Checking for dangling intermediate images")
images = subprocess.check_output([
"docker", "images", "-a", "--filter=dangling=true", "-q"
], encoding="utf-8").splitlines()
if len(images):
print("Cleaning up images: %s" % images)
# Sometimes fails, do not fail build due to that
subprocess.run(["docker", "rmi", " ".join(images)])
return image_name
def ensure_runner_image(root_path, testkitBranch):
# Use Go driver image for this since we need to build TLS server and
# use that in the runner.
# TODO: Use a dedicated Docker image for this.
return ensure_driver_image(
root_path, os.path.join(root_path, "driver", "go"),
testkitBranch, "go")
def get_driver_glue(thisPath, driverName, driverRepo):
""" Locates where driver has it's docker image and Python "glue" scripts
needed to build and run tests for the driver.
Returns a tuple consisting of the absolute path on this machine along with
the path as it will be mounted in the driver container.
"""
in_driver_repo = os.path.join(driverRepo, "testkit")
if os.path.isdir(in_driver_repo):
return (in_driver_repo, "/driver/testkit")
in_this_repo = os.path.join(thisPath, "driver", driverName)
if os.path.isdir(in_this_repo):
return (in_this_repo, "/testkit/driver/%s" % driverName)
raise Exception("No glue found for %s" % driverName)
def main(thisPath, driverName, testkitBranch, driverRepo):
# Path where scripts are that adapts driver to testkit.
# Both absolute path and path relative to driver container.
absGlue, driverGlue = get_driver_glue(
thisPath, driverName, driverRepo)
driverImage = ensure_driver_image(thisPath, absGlue,
testkitBranch, driverName)
# Prepare collecting of artifacts, collected to ./artifcats/
artifactsPath = os.path.abspath(os.path.join(".", "artifacts"))
if not os.path.exists(artifactsPath):
os.makedirs(artifactsPath)
print("Putting artifacts in %s" % artifactsPath)
# Important to stop all docker images upon exit
# Also make sure that none of those images are running at this point
atexit.register(cleanup)
cleanup()
# Create network to be shared among the instances.
# The host running this will be gateway on that network, retrieve that
# address to be able to start services on the network that the driver
# connects to (stub server and TLS server).
subprocess.run([
"docker", "network", "create", "the-bridge"
])
# Bootstrap the driver docker image by running a bootstrap script in
# the image. The driver docker image only contains the tools needed to
# build, not the built driver.
driverContainer = docker.run(
driverImage, "driver",
command=["python3", "/testkit/driver/bootstrap.py"],
mountMap={
thisPath: "/testkit",
driverRepo: "/driver",
artifactsPath: "/artifacts"
},
portMap={9876: 9876}, # For convenience when debugging
network="the-bridge",
workingFolder="/driver")
# Setup environment variables for driver container
driverEnv = {}
# Copy TEST_ variables that might have been set explicit
for varName in os.environ:
if varName.startswith("TEST_"):
driverEnv[varName] = os.environ[varName]
# Clean up artifacts
driverContainer.exec(
["python3", "/testkit/driver/clean_artifacts.py"],
envMap=driverEnv)
# Build the driver and it's testkit backend
print("Build driver and test backend in driver container")
driverContainer.exec(
["python3", os.path.join(driverGlue, "build.py")],
envMap=driverEnv)
print("Finished building driver and test backend")
"""
Unit tests
"""
begin_test_suite('Unit tests')
driverContainer.exec(
["python3", os.path.join(driverGlue, "unittests.py")],
envMap=driverEnv)
end_test_suite('Unit tests')
# Start the test backend in the driver Docker instance.
# Note that this is done detached which means that we don't know for
# sure if the test backend actually started and we will not see
# any output of this command.
# When failing due to not being able to connect from client or seeing
# issues like 'detected possible backend crash', make sure that this
# works simply by commenting detach and see that the backend starts.
print("Start test backend in driver container")
driverContainer.exec_detached(
["python3", os.path.join(driverGlue, "backend.py")],
envMap=driverEnv)
# Wait until backend started
# Use driver container to check for backend availability
driverContainer.exec([
"python3",
"/testkit/driver/wait_for_port.py", "localhost", "%d" % 9876],
envMap=driverEnv)
print("Started test backend")
# Start runner container, responsible for running the unit tests.
runnerImage = ensure_runner_image(thisPath, testkitBranch)
runnerEnv = {
"PYTHONPATH": "/testkit", # To use modules
}
# Copy TEST_ variables that might have been set explicit
for varName in os.environ:
if varName.startswith("TEST_"):
runnerEnv[varName] = os.environ[varName]
# Override with settings that must have a known value
runnerEnv.update({
# Runner connects to backend in driver container
"TEST_BACKEND_HOST": "driver",
# Driver connects to me
"TEST_STUB_HOST": "runner",
})
runnerContainer = docker.run(
runnerImage, "runner",
command=["python3", "/testkit/driver/bootstrap.py"],
mountMap={thisPath: "/testkit"},
envMap=runnerEnv,
network="the-bridge",
aliases=["thehost", "thehostbutwrong"]) # Used when testing TLS
"""
Stub tests
"""
runnerContainer.exec(["python3", "-m", "tests.stub.suites"])
"""
TLS tests
"""
# Build TLS server
runnerContainer.exec(
["go", "build", "-v", "."], workdir="/testkit/tlsserver")
runnerContainer.exec(
["python3", "-m", "tests.tls.suites"])
"""
Neo4j server tests
"""
# Make an artifacts folder where the database can place it's logs, each
# time we start a database server we should use a different folder.
neo4jArtifactsPath = os.path.join(artifactsPath, "neo4j")
os.makedirs(neo4jArtifactsPath)
neo4jServers = [
{
"name": "4.2-cluster",
"image": "neo4j:4.2-enterprise",
"version": "4.2",
"edition": "enterprise",
"cluster": True,
"suite": "", # TODO: Define cluster suite
"scheme": "neo4j"
},
{
"name": "3.5-enterprise",
"image": "neo4j:3.5-enterprise",
"version": "3.5",
"edition": "enterprise",
"cluster": False,
"suite": "3.5",
"scheme": "bolt"
},
{
"name": "4.0-community",
"image": "neo4j:4.0",
"version": "4.0",
"edition": "community",
"cluster": False,
"suite": "4.0",
"scheme": "neo4j"
},
{
"name": "4.1-enterprise",
"image": "neo4j:4.1-enterprise",
"version": "4.1",
"edition": "enterprise",
"cluster": False,
"suite": "4.1",
"scheme": "neo4j"
},
]
if in_teamcity:
# Use last successful build of 4.2.0. Need to update this when a new
# patch is in the baking. When there is an official 4.2 build there
# should be a Docker hub based image above (or added when not in
# Teamcity).
s = {
"name": "4.2-tc-enterprise",
"image": "neo4j:4.2.3-enterprise",
"version": "4.2",
"edition": "enterprise",
"cluster": False,
"suite": "4.2",
"scheme": "neo4j",
"download": teamcity.DockerImage(
"neo4j-enterprise-4.2.3-docker-loadable.tar")
}
neo4jServers.append(s)
for neo4jServer in neo4jServers:
download = neo4jServer.get('download', None)
if download:
print("Downloading Neo4j docker image")
docker.load(download.get())
cluster = neo4jServer["cluster"]
serverName = neo4jServer["name"]
# Start a Neo4j server
if cluster:
print("Starting neo4j cluster (%s)" % serverName)
server = neo4j.Cluster(neo4jServer["image"],
serverName,
neo4jArtifactsPath)
else:
print("Starting neo4j standalone server (%s)" % serverName)
server = neo4j.Standalone(neo4jServer["image"],
serverName,
neo4jArtifactsPath,
"neo4jserver", 7687,
neo4jServer["edition"])
server.start()
hostname, port = server.address()
# Wait until server is listening before running tests
# Use driver container to check for Neo4j availability since connect
# will be done from there
print("Waiting for neo4j service port to be available")
driverContainer.exec([
"python3", "/testkit/driver/wait_for_port.py",
hostname, "%d" % port])
print("Neo4j listens")
# Run the actual test suite within the runner container. The tests
# will connect to driver backend and configure drivers to connect to
# the neo4j instance.
runnerEnv.update({
# Hostname of Docker container runnng db
"TEST_NEO4J_HOST": hostname,
"TEST_NEO4J_USER": neo4j.username,
"TEST_NEO4J_PASS": neo4j.password,
})
# Generic integration tests, requires a backend
suite = neo4jServer["suite"]
if suite:
print("Running test suite %s" % suite)
runnerContainer.exec([
"python3", "-m", "tests.neo4j.suites", suite],
envMap=runnerEnv)
else:
print("No test suite specified for %s" % serverName)
# Parameters that might be used by native stress/integration
# tests suites
driverEnv.update({
"TEST_NEO4J_HOST": hostname,
"TEST_NEO4J_USER": neo4j.username,
"TEST_NEO4J_PASS": neo4j.password,
"TEST_NEO4J_SCHEME": neo4jServer["scheme"],
"TEST_NEO4J_PORT": port,
"TEST_NEO4J_EDITION": neo4jServer["edition"],
"TEST_NEO4J_VERSION": neo4jServer["version"],
})
if cluster:
driverEnv["TEST_NEO4J_IS_CLUSTER"] = "1"
else:
driverEnv.pop("TEST_NEO4J_IS_CLUSTER", None)
# To support the legacy .net integration tests
# TODO: Move this to testkit/driver/dotnet/*.py
envString = ""
if neo4jServer["edition"] == "enterprise":
envString += "-e "
envString += neo4jServer["version"]
driverEnv["NEOCTRL_ARGS"] = envString
# Run the stress test suite within the driver container.
# The stress test suite uses threading and put a bigger load on the
# driver than the integration tests do and are therefore written in
# the driver language.
# None of the drivers will work properly in cluster.
if not cluster or driverName in ['go', 'javascript']:
print("Building and running stress tests...")
driverContainer.exec([
"python3", os.path.join(driverGlue, "stress.py")],
envMap=driverEnv)
else:
print("Skipping stress tests for %s" % serverName)
# Run driver native integration tests within the driver container.
# Driver integration tests should check env variable to skip tests
# depending on if running in cluster or not, this is not properly done
# in any (?) driver right now so skip the suite...
if not cluster or driverName in []:
print("Building and running integration tests...")
driverContainer.exec([
"python3", os.path.join(driverGlue, "integration.py")],
envMap=driverEnv)
else:
print("Skipping integration tests for %s" % serverName)
# Check that all connections to Neo4j has been closed.
# Each test suite should close drivers, sessions properly so any
# pending connections detected here should indicate connection leakage
# in the driver.
print("Checking that connections are closed to the database")
driverContainer.exec([
"python3", "/testkit/driver/assert_conns_closed.py",
hostname, "%d" % port])
server.stop()
if __name__ == "__main__":
driverName = os.environ.get("TEST_DRIVER_NAME")
if not driverName:
print("Missing environment variable TEST_DRIVER_NAME that contains "
"name of the driver")
sys.exit(1)
testkitBranch = os.environ.get("TEST_BRANCH")
if not testkitBranch:
if in_teamcity:
print("Missing environment variable TEST_BRANCH that contains "
"name of testkit branch. "
"This name is used to name Docker repository. ")
sys.exit(1)
testkitBranch = "local"
# Retrieve path to the repository containing this script.
# Use this path as base for locating a whole bunch of other stuff.
# Add this path to python sys path to be able to invoke modules
# from this repo
thisPath = os.path.dirname(os.path.abspath(__file__))
os.environ['PYTHONPATH'] = thisPath
# Retrieve path to driver git repository
driverRepo = os.environ.get("TEST_DRIVER_REPO")
if not driverRepo:
print("Missing environment variable TEST_DRIVER_REPO that contains "
"path to driver repository")
sys.exit(1)
main(thisPath, driverName, testkitBranch, driverRepo)