|
| 1 | +/** |
| 2 | + * Copyright (c) "Neo4j" |
| 3 | + * Neo4j Sweden AB [http://neo4j.com] |
| 4 | + * |
| 5 | + * This file is part of Neo4j. |
| 6 | + * |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * you may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +type NumberOrString = number | string |
| 21 | +export default class Neo4jContainer { |
| 22 | + private usages: number = 0 |
| 23 | + private container: any | undefined = undefined |
| 24 | + |
| 25 | + constructor ( |
| 26 | + private readonly user: string, |
| 27 | + private readonly password: string, |
| 28 | + private readonly version: string, |
| 29 | + private readonly edition: string | undefined, |
| 30 | + private readonly disabled: boolean, |
| 31 | + private readonly containerLogs: boolean = false |
| 32 | + |
| 33 | + ) { |
| 34 | + } |
| 35 | + |
| 36 | + async start (): Promise<void> { |
| 37 | + if (this.disabled) { |
| 38 | + return |
| 39 | + } |
| 40 | + this.usages++ |
| 41 | + console.log('Starting container') |
| 42 | + if (this.container != null) { |
| 43 | + console.log('Container already started') |
| 44 | + return |
| 45 | + } |
| 46 | + |
| 47 | + const tag = this.edition != null ? `${this.version}-${this.edition}` : this.version |
| 48 | + |
| 49 | + // Browser does not support testcontainers |
| 50 | + // @ts-expect-error |
| 51 | + const path = global.window != null ? './browser/testcontainer' : './node/testcontainer' |
| 52 | + // eslint-disable-next-line @typescript-eslint/no-var-requires |
| 53 | + const { GenericContainer, DockerImageName, Wait } = require(path) |
| 54 | + |
| 55 | + let container = new GenericContainer(new DockerImageName(undefined, 'neo4j', tag).toString()) |
| 56 | + .withEnv('NEO4J_AUTH', `${this.user}/${this.password}`) |
| 57 | + |
| 58 | + if (this.edition === 'enterprise') { |
| 59 | + container = container.withEnv('NEO4J_ACCEPT_LICENSE_AGREEMENT', 'yes') |
| 60 | + } |
| 61 | + |
| 62 | + this.container = await container.withExposedPorts(7687, 7474) |
| 63 | + .withWaitStrategy(Wait.forLogMessage(/Started/)) |
| 64 | + .start() |
| 65 | + |
| 66 | + // eslint-disable-next-line @typescript-eslint/restrict-template-expressions |
| 67 | + console.log('Container started at ' + `${this.container.getHost()}:${this.container.getMappedPort(7687)}`) |
| 68 | + |
| 69 | + if (this.containerLogs) { |
| 70 | + const stream = await this.container.logs() |
| 71 | + stream |
| 72 | + .on('data', (line: string) => console.log(line)) |
| 73 | + .on('err', (line: string) => console.error(line)) |
| 74 | + .on('end', () => console.log('Stream closed')) |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + getBoltPort (defaultPort: NumberOrString = 7687): NumberOrString { |
| 79 | + return this.getMappedPort(7687, defaultPort) |
| 80 | + } |
| 81 | + |
| 82 | + getHttpPort (defaultPort: NumberOrString = 7474): NumberOrString { |
| 83 | + return this.getMappedPort(7474, defaultPort) |
| 84 | + } |
| 85 | + |
| 86 | + getMappedPort (port: number, defaultPort: NumberOrString): NumberOrString { |
| 87 | + return this.container != null ? this.container.getMappedPort(port) : defaultPort |
| 88 | + } |
| 89 | + |
| 90 | + async stop (): Promise<void> { |
| 91 | + this.usages-- |
| 92 | + if (this.usages <= 0) { |
| 93 | + this.usages = 0 |
| 94 | + await this.container?.stop() |
| 95 | + this.container = undefined |
| 96 | + } |
| 97 | + } |
| 98 | +} |
0 commit comments