From 3dd7c1101cdfc0077c82330b0502f31eaaff29d4 Mon Sep 17 00:00:00 2001 From: Felipe Barros Cruz Date: Wed, 17 Apr 2019 04:56:26 -0300 Subject: [PATCH 1/7] Add in decorator return statusCode defined to be return --- lib/index.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/index.js b/lib/index.js index ee1e757..7fbea75 100644 --- a/lib/index.js +++ b/lib/index.js @@ -75,8 +75,9 @@ async function register(server, options) { } internals.handler = (route, options) => { - return function hemeraHapiHandler(request) { - return request.hemera.act(internals.getPatternFromRequest(request, options.pattern)) + return async function hemeraHapiHandler(request, h) { + const data = await request.hemera.act(internals.getPatternFromRequest(request, options.pattern)) + return h.response(data).code(options.statusCode || 200) } } From 7613df7a33833c59377a2ac378342782de8505a5 Mon Sep 17 00:00:00 2001 From: Felipe Barros Cruz Date: Wed, 17 Apr 2019 04:56:47 -0300 Subject: [PATCH 2/7] Isolate domain to test the lib with docker and docker-compose --- .dockerignore | 3 +++ Dockerfile | 18 ++++++++++++++++++ docker-compose.yml | 23 +++++++++++++++++++++++ package.json | 10 ++++++---- test/action-mapping.js | 21 +++++++-------------- test/base-pattern.js | 24 ++++++++++-------------- test/basic-custominstance.js | 23 +++++++---------------- test/basic.js | 21 +++++++-------------- test/handler-decorator.js | 25 ++++++++++--------------- test/hemera-plugin.js | 21 +++++++-------------- test/request-decorator.js | 19 ++++--------------- test/toolkit-decorator.js | 19 ++++--------------- 12 files changed, 106 insertions(+), 121 deletions(-) create mode 100644 .dockerignore create mode 100644 Dockerfile create mode 100644 docker-compose.yml diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..1fd04da --- /dev/null +++ b/.dockerignore @@ -0,0 +1,3 @@ +node_modules +coverage +.nyc_output diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..1540257 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,18 @@ +FROM mhart/alpine-node:8.10.0 +MAINTAINER FelipeBarrosCruz + +RUN apk add --update \ + make \ + gcc \ + g++ \ + python \ + git + +WORKDIR /src + +ADD package.json package-lock.json ./ +RUN npm install + +ADD / ./ + +CMD ["npm", "run", "lib:test"] diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..beea3e0 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,23 @@ +version: '3' +services: + nats: + image: 'nats:latest' + entrypoint: "/gnatsd -DV" + logging: + driver: none + expose: + - "4222" + ports: + - "4222:4222" + - "8222:8222" + lib-test: + tty: true + build: + context: . + dockerfile: Dockerfile + environment: + - NATS_URL=nats://nats:4222 + links: + - nats + depends_on: + - nats diff --git a/package.json b/package.json index e17a056..9e7bd7e 100644 --- a/package.json +++ b/package.json @@ -44,16 +44,18 @@ "hemera-testsuite": "^4.1.0", "istanbul": "^0.4.5", "joi": "^14.3.1", - "mocha": "^6.0.2", + "mocha": "^6.1.3", "mocha-lcov-reporter": "^1.3.0", "np": "^4.0.2", + "nyc": "^14.0.0", "prettier": "^1.16.4", "sinon": "^7.2.7" }, "scripts": { - "test": "mocha --exit -t 5000 --recursive", - "coverage": "istanbul cover node_modules/mocha/bin/_mocha -- -R spec -t 5000 --recursive", - "coveralls": "istanbul cover ./node_modules/mocha/bin/_mocha --report text-lcov -- -R spec | node ./node_modules/coveralls/bin/coveralls.js", + "test": "docker-compose up --build --force-recreate --exit-code-from=lib-test", + "lib:test": "mocha --exit -t 5000 --recursive", + "lib:coverage": "istanbul cover node_modules/.bin/mocha -- -R spec -t 5000 --recursive", + "lib:coveralls": "istanbul cover ./node_modules/mocha/bin/_mocha --report text-lcov -- -R spec | node ./node_modules/coveralls/bin/coveralls.js", "lint": "eslint .", "format": "prettier --write ./lib/**/*.js ./test/**/*.js", "patch": "np patch --yolo", diff --git a/test/action-mapping.js b/test/action-mapping.js index afcaf79..a6b2cd8 100644 --- a/test/action-mapping.js +++ b/test/action-mapping.js @@ -2,31 +2,21 @@ const Code = require('code') const Hapi = require('hapi') -const HemeraTestsuite = require('hemera-testsuite') const HapiHemera = require('../') const { expect } = Code describe('Action Mapping', function() { - const PORT = 6242 - const noAuthUrl = `nats://localhost:${PORT}` - let natsServer - - // Start up our own nats-server - before(function(done) { - natsServer = HemeraTestsuite.start_server(PORT, done) - }) - - // Shutdown our server after we are done - after(function() { - natsServer.kill() - }) + const noAuthUrl = process.env.NATS_URL || `nats://localhost:4222` it('Maps an action to a server method', async () => { const server = new Hapi.Server() await server.register({ plugin: HapiHemera, options: { + hemera: { + logLevel: 'silent' + }, nats: { url: noAuthUrl } @@ -62,6 +52,9 @@ describe('Action Mapping', function() { await server.register({ plugin: HapiHemera, options: { + hemera: { + logLevel: 'silent' + }, nats: { url: noAuthUrl } diff --git a/test/base-pattern.js b/test/base-pattern.js index f36ccb6..41ab571 100644 --- a/test/base-pattern.js +++ b/test/base-pattern.js @@ -2,25 +2,12 @@ const Code = require('code') const Hapi = require('hapi') -const HemeraTestsuite = require('hemera-testsuite') const HapiHemera = require('../') const { expect } = Code describe('Base Pattern', function() { - const PORT = 6242 - const noAuthUrl = `nats://localhost:${PORT}` - let natsServer - - // Start up our own nats-server - before(function(done) { - natsServer = HemeraTestsuite.start_server(PORT, done) - }) - - // Shutdown our server after we are done - after(function() { - natsServer.kill() - }) + const noAuthUrl = process.env.NATS_URL || `nats://localhost:4222` it('Should be able to create base pattern with function', async () => { const server = new Hapi.Server() @@ -33,6 +20,9 @@ describe('Base Pattern', function() { } return basePattern }, + hemera: { + logLevel: 'silent' + }, nats: { url: noAuthUrl } @@ -63,6 +53,9 @@ describe('Base Pattern', function() { basePattern: { a: 1 }, + hemera: { + logLevel: 'silent' + }, nats: { url: noAuthUrl } @@ -94,6 +87,9 @@ describe('Base Pattern', function() { a: 1, b: 2 }, + hemera: { + logLevel: 'silent' + }, nats: { url: noAuthUrl } diff --git a/test/basic-custominstance.js b/test/basic-custominstance.js index 4737435..a78463b 100644 --- a/test/basic-custominstance.js +++ b/test/basic-custominstance.js @@ -4,29 +4,18 @@ const Code = require('code') const Hapi = require('hapi') const Hemera = require('nats-hemera') const Nats = require('nats') -const HemeraTestsuite = require('hemera-testsuite') const HapiHemera = require('../') const { expect } = Code describe('Custom Hemera instance', function() { - const PORT = 6242 - const noAuthUrl = `nats://localhost:${PORT}` - let natsServer - - // Start up our own nats-server - before(function(done) { - natsServer = HemeraTestsuite.start_server(PORT, done) - }) - - // Shutdown our server after we are done - after(function() { - natsServer.kill() - }) + const noAuthUrl = process.env.NATS_URL || `nats://localhost:4222` it('Connect to NATS', async () => { const server = new Hapi.Server() - const hemeraInstance = new Hemera(Nats.connect({ url: noAuthUrl })) + const hemeraInstance = new Hemera(Nats.connect({ url: noAuthUrl }), { + logLevel: 'silent' + }) await server.register({ plugin: HapiHemera, options: { @@ -38,7 +27,9 @@ describe('Custom Hemera instance', function() { it('Add / Act', async () => { const server = new Hapi.Server() - const hemeraInstance = new Hemera(Nats.connect({ url: noAuthUrl })) + const hemeraInstance = new Hemera(Nats.connect({ url: noAuthUrl }), { + logLevel: 'silent' + }) await server.register({ plugin: HapiHemera, options: { diff --git a/test/basic.js b/test/basic.js index 49bf466..7905442 100644 --- a/test/basic.js +++ b/test/basic.js @@ -2,31 +2,21 @@ const Code = require('code') const Hapi = require('hapi') -const HemeraTestsuite = require('hemera-testsuite') const HapiHemera = require('../') const { expect } = Code describe('Basic', function() { - const PORT = 6242 - const noAuthUrl = `nats://localhost:${PORT}` - let natsServer - - // Start up our own nats-server - before(function(done) { - natsServer = HemeraTestsuite.start_server(PORT, done) - }) - - // Shutdown our server after we are done - after(function() { - natsServer.kill() - }) + const noAuthUrl = process.env.NATS_URL || `nats://localhost:4222` it('Connect to NATS', async () => { const server = new Hapi.Server() await server.register({ plugin: HapiHemera, options: { + hemera: { + logLevel: 'silent' + }, nats: { url: noAuthUrl } @@ -40,6 +30,9 @@ describe('Basic', function() { await server.register({ plugin: HapiHemera, options: { + hemera: { + logLevel: 'silent' + }, nats: { url: noAuthUrl } diff --git a/test/handler-decorator.js b/test/handler-decorator.js index 1739a17..f6308e9 100644 --- a/test/handler-decorator.js +++ b/test/handler-decorator.js @@ -2,32 +2,21 @@ const Code = require('code') const Hapi = require('hapi') -const HemeraTestsuite = require('hemera-testsuite') const HapiHemera = require('../') const { expect } = Code describe('Handler decorator', function() { - const PORT = 6242 - const noAuthUrl = `nats://localhost:${PORT}` - const flags = [] - let natsServer - - // Start up our own nats-server - before(function(done) { - natsServer = HemeraTestsuite.start_server(PORT, flags, done) - }) - - // Shutdown our server after we are done - after(function() { - natsServer.kill() - }) + const noAuthUrl = process.env.NATS_URL || `nats://localhost:4222` it('Should be able to use handler decorators with query', async () => { const server = new Hapi.Server() await server.register({ plugin: HapiHemera, options: { + hemera: { + logLevel: 'silent' + }, nats: { url: noAuthUrl } @@ -71,6 +60,9 @@ describe('Handler decorator', function() { await server.register({ plugin: HapiHemera, options: { + hemera: { + logLevel: 'silent' + }, nats: { url: noAuthUrl } @@ -115,6 +107,9 @@ describe('Handler decorator', function() { await server.register({ plugin: HapiHemera, options: { + hemera: { + logLevel: 'silent' + }, nats: { url: noAuthUrl } diff --git a/test/hemera-plugin.js b/test/hemera-plugin.js index 6f1ed98..b02c48d 100644 --- a/test/hemera-plugin.js +++ b/test/hemera-plugin.js @@ -1,7 +1,6 @@ 'use strict' const Hapi = require('hapi') -const HemeraTestsuite = require('hemera-testsuite') const hemeraInternalSymbols = require('nats-hemera/lib/symbols') const Code = require('code') const Hp = require('hemera-plugin') @@ -10,19 +9,7 @@ const HapiHemera = require('../lib') const { expect } = Code describe('Hemera plugin registration', function() { - const PORT = 6242 - const noAuthUrl = `nats://localhost:${PORT}` - let natsServer - - // Start up our own nats-server - before(done => { - natsServer = HemeraTestsuite.start_server(PORT, done) - }) - - // Shutdown our server after we are done - after(() => { - natsServer.kill() - }) + const noAuthUrl = process.env.NATS_URL || `nats://localhost:4222` it('Should be able to register a plugin', async () => { const server = new Hapi.Server() @@ -41,6 +28,9 @@ describe('Hemera plugin registration', function() { plugin: HapiHemera, options: { plugins: [myPlugin], + hemera: { + logLevel: 'silent' + }, nats: { url: noAuthUrl } @@ -69,6 +59,9 @@ describe('Hemera plugin registration', function() { plugin: HapiHemera, options: { plugins: [{ register: myPlugin, options: { b: 2 } }], + hemera: { + logLevel: 'silent' + }, nats: { url: noAuthUrl } diff --git a/test/request-decorator.js b/test/request-decorator.js index 6e93021..852475b 100644 --- a/test/request-decorator.js +++ b/test/request-decorator.js @@ -2,26 +2,12 @@ const Code = require('code') const Hapi = require('hapi') -const HemeraTestsuite = require('hemera-testsuite') const HapiHemera = require('../') const { expect } = Code describe('Request decorator', function() { - const PORT = 6242 - const noAuthUrl = `nats://localhost:${PORT}` - const flags = [] - let natsServer - - // Start up our own nats-server - before(function(done) { - natsServer = HemeraTestsuite.start_server(PORT, flags, done) - }) - - // Shutdown our server after we are done - after(function() { - natsServer.kill() - }) + const noAuthUrl = process.env.NATS_URL || `nats://localhost:4222` it('Should be able to act with request hemera instance', async () => { const server = new Hapi.Server() @@ -32,6 +18,9 @@ describe('Request decorator', function() { a: 1, b: 2 }, + hemera: { + logLevel: 'silent' + }, nats: { url: noAuthUrl } diff --git a/test/toolkit-decorator.js b/test/toolkit-decorator.js index 005a74e..996c8e5 100644 --- a/test/toolkit-decorator.js +++ b/test/toolkit-decorator.js @@ -2,32 +2,21 @@ const Code = require('code') const Hapi = require('hapi') -const HemeraTestsuite = require('hemera-testsuite') const HapiHemera = require('../') const { expect } = Code describe('Toolkit decorator', function() { - const PORT = 6242 - const noAuthUrl = `nats://localhost:${PORT}` - const flags = [] - let natsServer - - // Start up our own nats-server - before(function(done) { - natsServer = HemeraTestsuite.start_server(PORT, flags, done) - }) - - // Shutdown our server after we are done - after(function() { - natsServer.kill() - }) + const noAuthUrl = process.env.NATS_URL || `nats://localhost:4222` it('Should be able to act with toolkit hemera interface', async () => { const server = new Hapi.Server() await server.register({ plugin: HapiHemera, options: { + hemera: { + logLevel: 'silent' + }, nats: { url: noAuthUrl } From b091f8bcfec6f36fadccfeba9eedc2cb6d93da63 Mon Sep 17 00:00:00 2001 From: Felipe Barros Cruz Date: Wed, 17 Apr 2019 05:25:07 -0300 Subject: [PATCH 3/7] Versioning docker image with node versions --- .travis.yml | 2 +- Dockerfile | 3 ++- docker-compose.yml | 2 ++ 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index ee16ee7..f9367ec 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,7 +8,7 @@ node_js: script: - npm install - - npm run test + - NODE_VERSION=$(echo $(node -v) | cut -c2-10) npm run test before_script: # install nats diff --git a/Dockerfile b/Dockerfile index 1540257..3251222 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,5 @@ -FROM mhart/alpine-node:8.10.0 +ARG BUILD_VERSION=8.10.0 +FROM mhart/alpine-node:$BUILD_VERSION MAINTAINER FelipeBarrosCruz RUN apk add --update \ diff --git a/docker-compose.yml b/docker-compose.yml index beea3e0..cf74a86 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -15,6 +15,8 @@ services: build: context: . dockerfile: Dockerfile + args: + BUILD_VERSION: 11.0.0 environment: - NATS_URL=nats://nats:4222 links: From 89bed8b26a98864d4aa05ed9a54b9a6b66347a8d Mon Sep 17 00:00:00 2001 From: Felipe Barros Cruz Date: Wed, 17 Apr 2019 05:29:26 -0300 Subject: [PATCH 4/7] Fix unecessary steps in travisci and docker node image by NODE_VERSION env var --- .travis.yml | 1 - docker-compose.yml | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index f9367ec..d3358d9 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,7 +7,6 @@ node_js: - "11" script: - - npm install - NODE_VERSION=$(echo $(node -v) | cut -c2-10) npm run test before_script: diff --git a/docker-compose.yml b/docker-compose.yml index cf74a86..6e0e928 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -16,7 +16,7 @@ services: context: . dockerfile: Dockerfile args: - BUILD_VERSION: 11.0.0 + BUILD_VERSION: ${NODE_VERSION} environment: - NATS_URL=nats://nats:4222 links: From c250ae557b0e84394c2e58b642abbabba2feef0d Mon Sep 17 00:00:00 2001 From: Felipe Barros Cruz Date: Wed, 17 Apr 2019 05:34:31 -0300 Subject: [PATCH 5/7] change app-test base image from alpine to node --- Dockerfile | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/Dockerfile b/Dockerfile index 3251222..897bc2d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,14 +1,7 @@ ARG BUILD_VERSION=8.10.0 -FROM mhart/alpine-node:$BUILD_VERSION +FROM node:$BUILD_VERSION MAINTAINER FelipeBarrosCruz -RUN apk add --update \ - make \ - gcc \ - g++ \ - python \ - git - WORKDIR /src ADD package.json package-lock.json ./ From ab1ff26962793221a71eb7920f7796449d3dbe8c Mon Sep 17 00:00:00 2001 From: Felipe Barros Cruz Date: Wed, 17 Apr 2019 05:39:03 -0300 Subject: [PATCH 6/7] Rollback docker base image --- Dockerfile | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 897bc2d..3251222 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,7 +1,14 @@ ARG BUILD_VERSION=8.10.0 -FROM node:$BUILD_VERSION +FROM mhart/alpine-node:$BUILD_VERSION MAINTAINER FelipeBarrosCruz +RUN apk add --update \ + make \ + gcc \ + g++ \ + python \ + git + WORKDIR /src ADD package.json package-lock.json ./ From 4f5a6c26065495bde6ea7656760d3b1cc2329c6d Mon Sep 17 00:00:00 2001 From: Felipe Barros Cruz Date: Wed, 17 Apr 2019 05:39:12 -0300 Subject: [PATCH 7/7] Fixing node versions --- .travis.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index d3358d9..8577d9e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,9 +2,9 @@ language: node_js sudo: false node_js: - - "8" - - "10" - - "11" + - "8.10.0" + - "10.0.0" + - "11.0.0" script: - NODE_VERSION=$(echo $(node -v) | cut -c2-10) npm run test