From afb2b1116b12c4800563e85adb15ebd39e030516 Mon Sep 17 00:00:00 2001 From: Alexander Zeitler Date: Sat, 10 Apr 2021 16:53:14 +0200 Subject: [PATCH] fix(test): fix broken tests Some tests failed because the used `alpine` service did start non-blocking which caused the container to stop immediately. Assertions expecting the container to run then failed. The new structure is to use two blocking `nginx` images which serve a scenario of web and proxy. For the `--abort-on-container-exit` flag tests a third - non-blocking by intend - service based on the `hello-world` image has been added. --- test/docker-compose-2.yml | 8 +-- test/docker-compose-42.yml | 3 +- test/docker-compose.yml | 14 ++-- test/index.test.ts | 140 +++++++++++++++++++------------------ 4 files changed, 88 insertions(+), 77 deletions(-) diff --git a/test/docker-compose-2.yml b/test/docker-compose-2.yml index 603ea0c92..4b2ebf668 100644 --- a/test/docker-compose-2.yml +++ b/test/docker-compose-2.yml @@ -1,11 +1,11 @@ version: '2' services: - db: + web: image: nginx:1.16.0 - container_name: compose_test_nginx_2 + container_name: compose_test_web_2 command: 'nginx -g "daemon off;"' environment: - NGINX_PORT: 5432 + NGINX_PORT: 8888 ports: - - "5432" + - "8888" diff --git a/test/docker-compose-42.yml b/test/docker-compose-42.yml index 97a661fe0..4d635a247 100644 --- a/test/docker-compose-42.yml +++ b/test/docker-compose-42.yml @@ -2,7 +2,8 @@ version: '3' services: some-service: - image: alpine:3.7.3 + image: nginx:1.19.9-alpine + command: 'nginx -g "daemon off;"' volumes: - ./volume:/mountedvolume - db-data:/mountedsecondvolume diff --git a/test/docker-compose.yml b/test/docker-compose.yml index e0c98befa..8edd35cc6 100644 --- a/test/docker-compose.yml +++ b/test/docker-compose.yml @@ -1,10 +1,14 @@ version: '2' services: - db: + web: image: nginx:1.16.0 - container_name: compose_test_nginx + container_name: compose_test_web command: 'nginx -g "daemon off;"' - alpine: - image: alpine:3.7.3 - container_name: compose_test_alpine + proxy: + image: nginx:1.19.9-alpine + container_name: compose_test_proxy + command: 'nginx -g "daemon off;"' + hello: + image: hello-world + container_name: compose_test_hello \ No newline at end of file diff --git a/test/index.test.ts b/test/index.test.ts index e1f3fb2be..12cde9f72 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -53,7 +53,8 @@ test('ensure container gets started', async (): Promise => { await compose.down({ cwd: path.join(__dirname), log: logOutput }); await compose.upAll({ cwd: path.join(__dirname), log: logOutput }); - expect(await isContainerRunning('/compose_test_nginx')).toBeTruthy(); + expect(await isContainerRunning('/compose_test_web')).toBeTruthy(); + expect(await isContainerRunning('/compose_test_proxy')).toBeTruthy(); await compose.down({ cwd: path.join(__dirname), log: logOutput }); }); @@ -139,46 +140,51 @@ test('ensure container command executed with --workdir command option', async () test('ensure only single container gets started', async (): Promise => { await compose.down({ cwd: path.join(__dirname), log: logOutput }); - await compose.upOne('alpine', { cwd: path.join(__dirname), log: logOutput }); + await compose.upOne('web', { cwd: path.join(__dirname), log: logOutput }); - expect(await isContainerRunning('/compose_test_alpine')).toBeTruthy(); - expect(await isContainerRunning('/compose_test_nginx')).toBeFalsy(); + expect(await isContainerRunning('/compose_test_web')).toBeTruthy(); + expect(await isContainerRunning('/compose_test_proxy')).toBeFalsy(); await compose.down({ cwd: path.join(__dirname), log: logOutput }); }); test('ensure only multiple containers get started', async (): Promise => { await compose.down({ cwd: path.join(__dirname), log: logOutput }); - await compose.upMany([ 'alpine' ], { cwd: path.join(__dirname), log: logOutput }); + await compose.upMany([ 'web' ], { cwd: path.join(__dirname), log: logOutput }); - expect(await isContainerRunning('/compose_test_alpine')).toBeTruthy(); - expect(await isContainerRunning('/compose_test_nginx')).toBeFalsy(); + expect(await isContainerRunning('/compose_test_web')).toBeTruthy(); + expect(await isContainerRunning('/compose_test_proxy')).toBeFalsy(); await compose.down({ cwd: path.join(__dirname), log: logOutput }); }); test('ensure container gets down', async (): Promise => { await compose.upAll({ cwd: path.join(__dirname), log: logOutput }); - expect(await isContainerRunning('/compose_test_nginx')).toBeTruthy(); + expect(await isContainerRunning('/compose_test_web')).toBeTruthy(); + expect(await isContainerRunning('/compose_test_proxy')).toBeTruthy(); await compose.down({ cwd: path.join(__dirname), log: logOutput }); - expect(await isContainerRunning('/compose_test_nginx')).toBeFalsy(); + expect(await isContainerRunning('/compose_test_web')).toBeFalsy(); + expect(await isContainerRunning('/compose_test_proxy')).toBeFalsy(); }); test('ensure container gets stopped', async (): Promise => { await compose.upAll({ cwd: path.join(__dirname), log: logOutput }); - expect(await isContainerRunning('/compose_test_nginx')).toBeTruthy(); + expect(await isContainerRunning('/compose_test_web')).toBeTruthy(); + expect(await isContainerRunning('/compose_test_proxy')).toBeTruthy(); await compose.stop({ cwd: path.join(__dirname), log: logOutput }); - expect(await isContainerRunning('/compose_test_nginx')).toBeFalsy(); + expect(await isContainerRunning('/compose_test_web')).toBeFalsy(); + expect(await isContainerRunning('/compose_test_proxy')).toBeFalsy(); await compose.down({ cwd: path.join(__dirname), log: logOutput }); }); test('ensure only single container gets stopped', async (): Promise => { await compose.upAll({ cwd: path.join(__dirname), log: logOutput }); - expect(await isContainerRunning('/compose_test_alpine')).toBeTruthy(); + expect(await isContainerRunning('/compose_test_web')).toBeTruthy(); + expect(await isContainerRunning('/compose_test_proxy')).toBeTruthy(); - await compose.stopOne('alpine', { cwd: path.join(__dirname), log: logOutput }); - expect(await isContainerRunning('/compose_test_alpine')).toBeFalsy(); - expect(await isContainerRunning('/compose_test_nginx')).toBeTruthy(); + await compose.stopOne('proxy', { cwd: path.join(__dirname), log: logOutput }); + expect(await isContainerRunning('/compose_test_web')).toBeTruthy(); + expect(await isContainerRunning('/compose_test_proxy')).toBeFalsy(); await compose.down({ cwd: path.join(__dirname), log: logOutput }); }); @@ -193,8 +199,8 @@ test('ensure container gets started with --abort-on-container-exit option', asyn exitCode: 0 }); - expect(await isContainerRunning('/compose_test_nginx')).toBeFalsy(); - expect(await isContainerRunning('/compose_test_alpine')).toBeFalsy(); + expect(await isContainerRunning('/compose_test_web')).toBeFalsy(); + expect(await isContainerRunning('/compose_test_proxy')).toBeFalsy(); await compose.down({ cwd: path.join(__dirname), log: logOutput }); }); @@ -207,17 +213,19 @@ test('ensure container gets started with --abort-on-container-exit option correc expect(result.out).toMatch(/Aborting on container exit/); - expect(await isContainerRunning('/compose_test_nginx')).toBeFalsy(); - expect(await isContainerRunning('/compose_test_alpine')).toBeFalsy(); + expect(await isContainerRunning('/compose_test_web')).toBeFalsy(); + expect(await isContainerRunning('/compose_test_proxy')).toBeFalsy(); await compose.down({ cwd: path.join(__dirname), log: logOutput }); }); test('ensure container gets killed', async (): Promise => { await compose.upAll({ cwd: path.join(__dirname), log: logOutput }); - expect(await isContainerRunning('/compose_test_nginx')).toBeTruthy(); + expect(await isContainerRunning('/compose_test_web')).toBeTruthy(); + expect(await isContainerRunning('/compose_test_proxy')).toBeTruthy(); await compose.kill({ cwd: path.join(__dirname), log: logOutput }); - expect(await isContainerRunning('/compose_test_nginx')).toBeFalsy(); + expect(await isContainerRunning('/compose_test_web')).toBeFalsy(); + expect(await isContainerRunning('/compose_test_proxy')).toBeFalsy(); await compose.down({ cwd: path.join(__dirname), log: logOutput }); }); @@ -227,11 +235,11 @@ test('ensure custom ymls are working', async (): Promise => { const cwd = path.join(__dirname); await compose.upAll({ cwd, log: logOutput, config }); - expect(await isContainerRunning('/compose_test_nginx_2')).toBeTruthy(); + expect(await isContainerRunning('/compose_test_web_2')).toBeTruthy(); // config & [config] are the same thing, ensures that multiple configs are handled properly await compose.kill({ cwd, log: logOutput, config: [ config ]}); - expect(await isContainerRunning('/compose_test_nginx_2')).toBeFalsy(); + expect(await isContainerRunning('/compose_test_web_2')).toBeFalsy(); await compose.down({ cwd, log: logOutput, config }); }); @@ -255,18 +263,15 @@ test('ensure run and exec are working', async (): Promise => { const opts = { cwd: path.join(__dirname), log: logOutput }; await compose.upAll(opts); + const console = require('console'); + expect(await isContainerRunning('/compose_test_web')).toBeTruthy(); - expect(await isContainerRunning('/compose_test_nginx')).toBeTruthy(); - - let std = await compose.exec('db', 'cat /etc/os-release', opts); + let std = await compose.exec('web', 'cat /etc/os-release', opts); - // expect(std.err).toBeFalsy(); checkOSID(std.out, 'debian'); - std = await compose.run('alpine', 'cat /etc/os-release', opts); - const console = require('console'); - console.log('std.err', std.err) - // expect(std.err).toBeFalsy(); + std = await compose.run('proxy', 'cat /etc/os-release', opts); + checkOSID(std.out, 'alpine'); await compose.down({ cwd: path.join(__dirname), log: logOutput }); @@ -290,14 +295,14 @@ test('ensure run and exec with command defined as array are working', async (): await compose.upAll(opts); - expect(await isContainerRunning('/compose_test_nginx')).toBe(true); + expect(await isContainerRunning('/compose_test_web')).toBe(true); - let std = await compose.exec('db', [ '/bin/sh', '-c', 'cat /etc/os-release' ], opts); + let std = await compose.exec('web', [ '/bin/sh', '-c', 'cat /etc/os-release' ], opts); // expect(std.err).toBeFalsy(); checkOSID(std.out, 'debian'); - std = await compose.run('alpine', [ '/bin/sh', '-c', 'cat /etc/os-release' ], opts); + std = await compose.run('proxy', [ '/bin/sh', '-c', 'cat /etc/os-release' ], opts); // expect(std.err).toBeFalsy(); checkOSID(std.out, 'alpine'); @@ -320,7 +325,7 @@ test('build accepts config as string', async (): Promise => { }; await compose.upAll(config); - const port = await compose.port('db', 5432, config); + const port = await compose.port('web', 8888, config); expect(port.out).toMatch(/.*:[0-9]{1,5}/); await compose.down(config); @@ -386,12 +391,12 @@ test('pull single service', async (): Promise => { config: 'docker-compose.yml' }; - await removeImagesStartingWith('nginx:1.16.0'); - expect(await imageExists('nginx:1.16.0')).toBeFalsy(); + await removeImagesStartingWith('nginx:1.19.9-alpine'); + expect(await imageExists('nginx:1.19.9-alpine')).toBeFalsy(); - await compose.pullOne('db', opts); + await compose.pullOne('proxy', opts); - expect(await imageExists('nginx:1.16.0')).toBeTruthy(); + expect(await imageExists('nginx:1.19.9-alpine')).toBeTruthy(); }); test('pull multiple services', async (): Promise => { @@ -402,15 +407,15 @@ test('pull multiple services', async (): Promise => { }; await removeImagesStartingWith('nginx:1.16.0'); - await removeImagesStartingWith('alpine:3.7.3'); + await removeImagesStartingWith('nginx:1.19.9-alpine'); expect(await imageExists('nginx:1.16.0')).toBeFalsy(); - expect(await imageExists('alpine:3.7.3')).toBeFalsy(); + expect(await imageExists('nginx:1.19.9-alpine')).toBeFalsy(); - await compose.pullMany([ 'db', 'alpine' ], opts); + await compose.pullMany([ 'web', 'proxy' ], opts); expect(await imageExists('nginx:1.16.0')).toBeTruthy(); - expect(await imageExists('alpine:3.7.3')).toBeTruthy(); + expect(await imageExists('nginx:1.19.9-alpine')).toBeTruthy(); }); test('pull all services', async (): Promise => { @@ -421,15 +426,15 @@ test('pull all services', async (): Promise => { }; await removeImagesStartingWith('nginx:1.16.0'); - await removeImagesStartingWith('alpine:3.7.3'); + await removeImagesStartingWith('nginx:1.19.9-alpine'); expect(await imageExists('nginx:1.16.0')).toBeFalsy(); - expect(await imageExists('alpine:3.7.3')).toBeFalsy(); + expect(await imageExists('nginx:1.19.9-alpine')).toBeFalsy(); await compose.pullAll(opts); expect(await imageExists('nginx:1.16.0')).toBeTruthy(); - expect(await imageExists('alpine:3.7.3')).toBeTruthy(); + expect(await imageExists('nginx:1.19.9-alpine')).toBeTruthy(); }); test('teardown', async (): Promise => { @@ -484,20 +489,20 @@ test('ps shows status data for started containers', async (): Promise => { const std = await compose.ps({ cwd: path.join(__dirname), log: logOutput }); expect(std.err).toBeFalsy(); - expect(std.out.includes('compose_test_alpine')).toBeTruthy(); - expect(std.out.includes('compose_test_nginx')).toBeTruthy(); + expect(std.out.includes('compose_test_web')).toBeTruthy(); + expect(std.out.includes('compose_test_proxy')).toBeTruthy(); await compose.down({ cwd: path.join(__dirname), log: logOutput }); }); test('ps does not show status data for stopped containers', async (): Promise => { await compose.down({ cwd: path.join(__dirname), log: logOutput }); - await compose.upOne('alpine', { cwd: path.join(__dirname), log: logOutput }); + await compose.upOne('web', { cwd: path.join(__dirname), log: logOutput }); const std = await compose.ps({ cwd: path.join(__dirname), log: logOutput }); expect(std.err).toBeFalsy(); - expect(std.out.includes('compose_test_alpine')).toBeTruthy(); - expect(std.out.includes('compose_test_nginx')).toBeFalsy(); + expect(std.out.includes('compose_test_web')).toBeTruthy(); + expect(std.out.includes('compose_test_proxy')).toBeFalsy(); await compose.down({ cwd: path.join(__dirname), log: logOutput }); }); @@ -505,31 +510,32 @@ test('restartAll does restart all containers', async (): Promise => { await compose.upAll({ cwd: path.join(__dirname), log: logOutput }); await compose.restartAll({ cwd: path.join(__dirname), log: logOutput }); - expect(await isContainerRunning('/compose_test_nginx')).toBeTruthy(); + expect(await isContainerRunning('/compose_test_web')).toBeTruthy(); + expect(await isContainerRunning('/compose_test_proxy')).toBeTruthy(); await compose.down({ cwd: path.join(__dirname), log: logOutput }); }); test('restartMany does restart selected containers', async (): Promise => { await compose.upAll({ cwd: path.join(__dirname), log: logOutput }); - await compose.restartMany([ 'db', 'alpine' ], { cwd: path.join(__dirname), log: logOutput }); + await compose.restartMany([ 'web', 'proxy' ], { cwd: path.join(__dirname), log: logOutput }); - expect(await isContainerRunning('/compose_test_nginx')).toBeTruthy(); + expect(await isContainerRunning('/compose_test_web')).toBeTruthy(); await compose.down({ cwd: path.join(__dirname), log: logOutput }); }); test('restartOne does restart container', async (): Promise => { await compose.upAll({ cwd: path.join(__dirname), log: logOutput }); - await compose.restartOne('db', { cwd: path.join(__dirname), log: logOutput }); + await compose.restartOne('proxy', { cwd: path.join(__dirname), log: logOutput }); - expect(await isContainerRunning('/compose_test_nginx')).toBeTruthy(); + expect(await isContainerRunning('/compose_test_proxy')).toBeTruthy(); await compose.down({ cwd: path.join(__dirname), log: logOutput }); }); test('logs does follow service logs', async (): Promise => { await compose.upAll({ cwd: path.join(__dirname), log: logOutput }); - await compose.logs('db', { cwd: path.join(__dirname), log: logOutput }); + const std = await compose.logs('proxy', { cwd: path.join(__dirname), log: logOutput }); - expect(await isContainerRunning('/compose_test_nginx')).toBeTruthy(); + expect(std.out.includes('compose_test_proxy')).toBeTruthy(); await compose.down({ cwd: path.join(__dirname), log: logOutput }); }); @@ -541,7 +547,7 @@ test('returns the port for a started service', async (): Promise => { }; await compose.upAll(config); - const port = await compose.port('db', 5432, config); + const port = await compose.port('web', 8888, config); expect(port.out).toMatch(/.*:[0-9]{1,5}/); await compose.down(config); @@ -555,16 +561,16 @@ test('removes container', async (): Promise => { }; await compose.upAll(config); - expect(await isContainerRunning('/compose_test_nginx')).toBeTruthy(); - expect(await isContainerRunning('/compose_test_alpine')).toBeTruthy(); + expect(await isContainerRunning('/compose_test_web')).toBeTruthy(); + expect(await isContainerRunning('/compose_test_proxy')).toBeTruthy(); - await compose.rm({ ...config, commandOptions: ['-s'] }, 'alpine'); - expect(await isContainerRunning('/compose_test_nginx')).toBeTruthy(); - expect(await isContainerRunning('/compose_test_alpine')).toBeFalsy(); + await compose.rm({ ...config, commandOptions: ['-s'] }, 'proxy'); + expect(await isContainerRunning('/compose_test_web')).toBeTruthy(); + expect(await isContainerRunning('/compose_test_proxy')).toBeFalsy(); - await compose.rm({ ...config, commandOptions: ['-s'] }, 'alpine', 'db'); - expect(await isContainerRunning('/compose_test_nginx')).toBeFalsy(); - expect(await isContainerRunning('/compose_test_alpine')).toBeFalsy(); + await compose.rm({ ...config, commandOptions: ['-s'] }, 'proxy', 'web'); + expect(await isContainerRunning('/compose_test_web')).toBeFalsy(); + expect(await isContainerRunning('/compose_test_proxy')).toBeFalsy(); }); test('returns version information', async (): Promise => {