|
| 1 | + |
| 2 | +export default class Neo4jContainer { |
| 3 | + constructor ({ user, password, containerLogs, version, edition, disabled }) { |
| 4 | + this._user = user |
| 5 | + this._password = password |
| 6 | + this._containerLogs = containerLogs || false |
| 7 | + this._version = version |
| 8 | + this._edition = edition |
| 9 | + this._container = null |
| 10 | + this._disabled = disabled || false |
| 11 | + } |
| 12 | + |
| 13 | + async start () { |
| 14 | + if (this._disabled) { |
| 15 | + return |
| 16 | + } |
| 17 | + console.log('Starting container') |
| 18 | + if (this._container != null) { |
| 19 | + console.log('Container already started') |
| 20 | + return |
| 21 | + } |
| 22 | + |
| 23 | + const tag = this._edition != null ? `${this._version}-${this._edition}` : this._version |
| 24 | + |
| 25 | + const { GenericContainer, Wait } = require('testcontainers') |
| 26 | + const { DockerImageName } = require('testcontainers/dist/docker-image-name') |
| 27 | + |
| 28 | + let container = new GenericContainer(new DockerImageName(null, 'neo4j', tag).toString()) |
| 29 | + .withEnv('NEO4J_AUTH', `${this._user}/${this._password}`) |
| 30 | + |
| 31 | + if (this._edition === 'enterprise') { |
| 32 | + container = container.withEnv('NEO4J_ACCEPT_LICENSE_AGREEMENT', 'yes') |
| 33 | + } |
| 34 | + |
| 35 | + this._container = await container.withExposedPorts(7687, 7474) |
| 36 | + .withWaitStrategy(Wait.forLogMessage(/Started/)) |
| 37 | + .start() |
| 38 | + |
| 39 | + console.log('Container started at ' + `${this._container.getHost()}:${this._container.getMappedPort(7687)}`) |
| 40 | + |
| 41 | + if (this._containerLogs) { |
| 42 | + const stream = await this._container.logs() |
| 43 | + stream |
| 44 | + .on('data', line => console.log(line)) |
| 45 | + .on('err', line => console.error(line)) |
| 46 | + .on('end', () => console.log('Stream closed')) |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + getBoltPort () { |
| 51 | + return this.getMappedPort(7687) |
| 52 | + } |
| 53 | + |
| 54 | + getHttpPort () { |
| 55 | + return this.getMappedPort(7474) |
| 56 | + } |
| 57 | + |
| 58 | + getMappedPort (port) { |
| 59 | + return this._container != null ? this._container.getMappedPort(port) : port |
| 60 | + } |
| 61 | + |
| 62 | + async stop () { |
| 63 | + await this._container.stop() |
| 64 | + this._container = undefined |
| 65 | + } |
| 66 | +} |
0 commit comments