-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(test-utils): add functions to init and close containers from doc…
…ker-compose for jest globaSetup
- Loading branch information
Showing
10 changed files
with
185 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# docker-compose.yml - Only for local debug purposes | ||
# docker-compose up -d elasticsearch kibana jaeger camunda redis mongo mysql postgres pgadmin | ||
version: '3.9' | ||
|
||
services: | ||
mongo: | ||
image: mongo:5.0 | ||
container_name: local-mongo | ||
restart: always | ||
ports: | ||
- '27017:27017' | ||
environment: | ||
TZ: UTC | ||
MONGO_INITDB_ROOT_USERNAME: root | ||
MONGO_INITDB_ROOT_PASSWORD: 123456 | ||
MONGO_INITDB_DATABASE: test_db | ||
#logging: | ||
# driver: none | ||
#volumes: | ||
# - ./.mongo_data:/data/db |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
packages/test-utils/src/__test__/globalTestContainers.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { closeDockerCompose, initDockerCompose } from '../testcontainers'; | ||
import path from 'path'; | ||
import { StartedDockerComposeEnvironment } from 'testcontainers'; | ||
|
||
describe('globalTestContainers', () => { | ||
const composeFilePath = path.resolve(__dirname, '..', 'fixtures', 'docker-compose'); | ||
const composeFile = 'docker-compose.yml'; | ||
|
||
it('should be initialize service from docker-compose.yml', async () => { | ||
const services = ['mongo']; | ||
await initDockerCompose(services, composeFilePath, composeFile)(); | ||
expect(global.__TESTCONTAINERS__).toBeDefined(); | ||
expect(global.__TESTCONTAINERS__).toBeInstanceOf(StartedDockerComposeEnvironment); | ||
await closeDockerCompose({ removeVolumes: false })(); | ||
}); | ||
|
||
it('should be initialize all services from docker-compose.yml', async () => { | ||
const services = []; | ||
await initDockerCompose(services, composeFilePath, composeFile)(); | ||
expect(global.__TESTCONTAINERS__).toBeDefined(); | ||
expect(global.__TESTCONTAINERS__).toBeInstanceOf(StartedDockerComposeEnvironment); | ||
await closeDockerCompose({ removeVolumes: false })(); | ||
}); | ||
|
||
it('should be initialize services from docker-compose.yml in default path and filename', async () => { | ||
const services = []; | ||
await initDockerCompose(services)(); | ||
expect(global.__TESTCONTAINERS__).toBeDefined(); | ||
expect(global.__TESTCONTAINERS__).toBeInstanceOf(StartedDockerComposeEnvironment); | ||
await closeDockerCompose({ removeVolumes: false })(); | ||
}); | ||
}); |
69 changes: 69 additions & 0 deletions
69
packages/test-utils/src/fixtures/docker-compose/docker-compose.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# docker-compose.yml - Only for local debug purposes | ||
# docker-compose up -d elasticsearch kibana jaeger camunda redis mongo mysql postgres pgadmin | ||
version: '3.9' | ||
|
||
services: | ||
postgres: | ||
image: postgres:13 | ||
container_name: local-postgres | ||
restart: always | ||
ports: | ||
- '5432:5432' | ||
environment: | ||
TZ: UTC | ||
POSTGRES_DB: my_db | ||
POSTGRES_USER: root | ||
POSTGRES_PASSWORD: 123456 | ||
#logging: | ||
# driver: none | ||
#volumes: | ||
# - ./.postgres_data:/var/lib/postgresql/data | ||
|
||
mysql: | ||
image: mysql:5.7 | ||
container_name: local-mysql | ||
restart: always | ||
ports: | ||
- '3306:3306' | ||
environment: | ||
TZ: UTC | ||
MYSQL_ROOT_PASSWORD: 123456 | ||
MYSQL_DATABASE: my_db | ||
MYSQL_PASSWORD: 123456 | ||
#MYSQL_USER: root | ||
#volumes: | ||
# - ./.mysql-data/db:/var/lib/mysql | ||
|
||
mongo: | ||
image: mongo:5.0 | ||
container_name: local-mongo | ||
restart: always | ||
ports: | ||
- '27017:27017' | ||
environment: | ||
TZ: UTC | ||
MONGO_INITDB_ROOT_USERNAME: root | ||
MONGO_INITDB_ROOT_PASSWORD: 123456 | ||
MONGO_INITDB_DATABASE: test_db | ||
#logging: | ||
# driver: none | ||
#volumes: | ||
# - ./.mongo_data:/data/db | ||
|
||
redis: | ||
image: redis:6.2-alpine | ||
container_name: local-redis | ||
restart: always | ||
ports: | ||
- '6379:6379' | ||
environment: | ||
TZ: UTC | ||
REDIS_PORT: 6379 | ||
REDIS_PASSWORD: 123456 | ||
REDIS_HOST: cache | ||
#REDIS_USERNAME: default | ||
command: [ "redis-server", "--appendonly", "yes", "--requirepass","123456" ] | ||
#logging: | ||
# driver: none | ||
#volumes: | ||
# - ./.redis_data:/data |
39 changes: 39 additions & 0 deletions
39
packages/test-utils/src/testcontainers/globalTestContainersTD.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { DockerComposeEnvironment } from 'testcontainers'; | ||
import { DockerComposeDownOptions } from 'testcontainers/dist/src/test-container'; | ||
import * as _ from 'lodash'; | ||
|
||
export const initDockerCompose = ( | ||
_services?: Array<string>, | ||
_composeFilePath = '.', | ||
_composeFile = 'docker-compose.yml', | ||
) => { | ||
return async (): Promise<void> => { | ||
console.info(`🐳 Initialize docker-compose...`); | ||
!_.isEmpty(_services) | ||
? console.log(`• Services from ${_composeFile}: ${_services.join(', ')}`) | ||
: null; | ||
try { | ||
global.__TESTCONTAINERS__ = await new DockerComposeEnvironment( | ||
_composeFilePath, | ||
_composeFile, | ||
).up(_services); | ||
console.info(`✨ Container(s) initialized.`); | ||
} catch (_error) { | ||
/* istanbul ignore next */ | ||
console.error(`😰 Error initializing container(s): ${_error}`); | ||
} | ||
}; | ||
}; | ||
|
||
export const closeDockerCompose = (_options?: Partial<DockerComposeDownOptions>) => { | ||
return async (): Promise<void> => { | ||
console.info('🐳 Terminate docker-compose...'); | ||
try { | ||
await global.__TESTCONTAINERS__.down(_options); | ||
console.info(`👌 Container(s) stopped successfully.`); | ||
} catch (_error) { | ||
/* istanbul ignore next */ | ||
console.error(`😒 Container(s) not initialized.`); | ||
} | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export { default as testContainers } from './testContainersTD'; | ||
export * from './globalTestContainersTD'; | ||
export * from './types'; |