Skip to content
This repository has been archived by the owner on May 5, 2022. It is now read-only.

Commit

Permalink
Integration tests (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
Eli Golderg authored and sjanuary committed Aug 13, 2018
1 parent c92a557 commit 0614ffe
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 4 deletions.
7 changes: 5 additions & 2 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
{
"extends": "strongloop",
"env": {
"node": true
"node": true,
"es6": true
},
"parserOptions": { "ecmaVersion": 6 },
"parserOptions": { "ecmaVersion": 9 },
"globals": {
"before": true,
"after": true,
"describe": true,
"it": true,
"xit": true
Expand Down
6 changes: 6 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,9 @@ node_js:
- "8"
- "6"
- "node"
sudo: required
services:
- docker
before_install:
- docker run -d -p 9411:9411 openzipkin/zipkin
- while netstat -lnt | awk '$4 ~ /:9411$/ {exit 1}'; do sleep 10; done
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@
"eslint": "^3.14.1",
"eslint-config-strongloop": "^2.1.0",
"mocha": "^5.2.0",
"request-promise-native": "1.0.5",
"sinon": "^6.1.3",
"tap": "^10.0.1"
},
"scripts": {
"test": "mocha test/",
"test": "npm run test:unit && npm run test:integration",
"test:unit": "mocha test/unit/",
"test:integration": "mocha -t 3000 test/integration/",
"pretest": "eslint .",
"pretravis": "eslint .",
"travis": ""
Expand Down
70 changes: 70 additions & 0 deletions test/integration/request.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
'use strict';

const { expect } = require('chai');

const { request, createServer } = require('../utils/http');
const { getTraces } = require('../utils/zipkinQuery');

const zipkinHost = '127.0.0.1';
const zipkinPort = 9411;
const zipkinSampleRate = 1.0;
const serviceName = 'frontend';

function waitAndGetTraces() {
return new Promise(resolve => {
setTimeout(() => { // We want to let all background requests going to zipkin complete
resolve(getTraces({ zipkinHost, zipkinPort, serviceName }));
}, 1000);
});
}

describe('http requests', () => {
describe('outbound requests', () => {
let server;
let http;

before(() => {
require('../../')({
host: zipkinHost,
port: zipkinPort,
sampleRate: zipkinSampleRate,
serviceName
});

http = require('http');
return createServer({ http, port: 3000 })
.then(createdServer => {
server = createdServer;
});
});
after(() => {
if (server) server.close();
});
it('should reach zipkin with a simple http request (string options)', () => {
let outgoingTraceId;
return request({ http, options: 'http://localhost:3000' })
.then(({ request }) => {
const outgoingHeaders = request._headers;
outgoingTraceId = outgoingHeaders['x-b3-traceid'];
})
.then(waitAndGetTraces)
.then((traces) => {
expect(traces.length > 0).to.be.ok;
expect(traces.some(trace => trace[0].traceId === outgoingTraceId)).to.be.ok;
});
});
it('should reach zipkin with a simple http request (object options)', () => {
let outgoingTraceId;
return request({ http, options: { hostname: 'localhost', port: 3000 } })
.then(({ request }) => {
const outgoingHeaders = request._headers;
outgoingTraceId = outgoingHeaders['x-b3-traceid'];
})
.then(waitAndGetTraces)
.then((traces) => {
expect(traces.length > 0).to.be.ok;
expect(traces.some(trace => trace[0].traceId === outgoingTraceId)).to.be.ok;
});
});
});
});
11 changes: 10 additions & 1 deletion test/aspect.test.js → test/unit/aspect.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const { expect } = require('chai');
const { stub } = require('sinon');
const { before, after, around, findCallbackArg, aroundCallback } = require('../lib/aspect');
const { before, after, around, findCallbackArg, aroundCallback } = require('../../lib/aspect');
describe('aspect', () => {
describe('before', () => {
it('should call hook before target method', () => {
Expand All @@ -13,6 +13,15 @@ describe('aspect', () => {
testObj.a();
expect(hookStub.calledBefore(origStub)).to.be.ok;
});
it('should pass original arguments', () => {
const origStub = stub();
const hookStub = stub();
const testObj = { a: origStub };
before(testObj, ['a'], hookStub);
const testString = 'This is a test string';
testObj.a(testString);
expect(origStub.calledWith(testString)).to.be.ok;
});
});
describe('after', () => {
it('should call hook after target method', () => {
Expand Down
21 changes: 21 additions & 0 deletions test/utils/http.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict';

module.exports.request = ({ http, options }) => {
return new Promise(resolve => {
http.get(options, (res) => {
resolve({ request: res.req, response: res });
});
});
};

module.exports.createServer = ({ http, port }) => {
return new Promise((resolve) => {
let server = http.createServer(function(req, res) {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Hello World\n');
});
server.listen(port, function() {
resolve(server);
});
});
};
17 changes: 17 additions & 0 deletions test/utils/zipkinQuery.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict';

const request = require('request-promise-native');

const url = ({ zipkinHost, zipkinPort }) => `http://${zipkinHost}:${zipkinPort}`;

module.exports.getTraces = ({ zipkinHost, zipkinPort, serviceName }) => {
const zipkinUrl = url({ zipkinHost, zipkinPort });
return request({
url: `${zipkinUrl}/api/v2/traces`,
qs: {
serviceName
},
json: true
});
};

0 comments on commit 0614ffe

Please sign in to comment.