Skip to content

Commit

Permalink
adds unit tests for abandoned PR aws#527
Browse files Browse the repository at this point in the history
  • Loading branch information
danno-s committed Sep 6, 2023
1 parent 8dce5e0 commit 6bfadb4
Showing 1 changed file with 84 additions and 26 deletions.
110 changes: 84 additions & 26 deletions packages/core/test/unit/patchers/aws3_p.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ var assert = require('chai').assert;
var chai = require('chai');
var sinon = require('sinon');
var sinonChai = require('sinon-chai');
var util = require('util');

var Aws = require('../../../lib/segments/attributes/aws');
var awsPatcher = require('../../../lib/patchers/aws3_p');
Expand All @@ -18,38 +19,38 @@ chai.use(sinonChai);

var traceId = '1-57fbe041-2c7ad569f5d6ff149137be86';

describe('AWS v3 patcher', function() {
describe('#captureAWSClient', function() {
describe('AWS v3 patcher', function () {
describe('#captureAWSClient', function () {
var sandbox, useMiddleware;

var awsClient = {
send: function() {},
send: function () { },
config: {
serviceId: 's3',
},
middlewareStack: constructStack(),
};

beforeEach(function() {
beforeEach(function () {
sandbox = sinon.createSandbox();
useMiddleware = sandbox.stub(awsClient.middlewareStack, 'use');
});

afterEach(function() {
afterEach(function () {
sandbox.restore();
});

it('should call middlewareStack.use and return the service', function() {
it('should call middlewareStack.use and return the service', function () {
const patched = awsPatcher.captureAWSClient(awsClient);
useMiddleware.should.have.been.calledOnce;
assert.equal(patched, awsClient);
});
});

describe('#captureAWSRequest', function() {
var awsClient, awsRequest, sandbox, segment, stubResolve, addNewSubsegmentStub, sub;
describe('#captureAWSRequest', function () {
var awsClient, awsRequest, ddbClient, ddbGetRequest, sandbox, segment, stubResolve, addNewSubsegmentStub, sub;

before(function() {
before(function () {
awsClient = {
send: async (req) => {
const context = {
Expand All @@ -74,9 +75,34 @@ describe('AWS v3 patcher', function() {
},
middlewareStack: constructStack(),
};

ddbClient = {
send: async (req) => {
const context = {
clientName: 'DynamoDBClient',
commandName: 'GetItemCommand',
};
const handler = awsClient.middlewareStack.resolve((args) => {
const error = req.response.error;
if (error) {
const err = new Error(error.message);
err.name = error.code;
err.$metadata = req.response.$metadata;
throw err;
}
return args;
}, context);
await handler(req);
return req.response;
},
config: {
region: async () => 'us-east-1',
},
middlewareStack: constructStack(),
};
});

beforeEach(function() {
beforeEach(function () {
sandbox = sinon.createSandbox();

awsRequest = new (class ListBucketsCommand {
Expand All @@ -99,14 +125,38 @@ describe('AWS v3 patcher', function() {
}
})();

ddbGetRequest = new (class PutCommand {
constructor() {
this.request = {
method: 'GET',
url: '/',
connection: {
remoteAddress: 'localhost'
},
headers: {},
};
this.input = {
TableName: 'TestTableName',
ConsistentRead: true,
};
this.response = {};
this.output = {
$metadata: {
requestId: '123',
extendedRequestId: '456',
}
};
}
})();

segment = new Segment('testSegment', traceId);
segment.additionalTraceData = {'Foo': 'bar'};
segment.additionalTraceData = { 'Foo': 'bar' };
sub = segment.addNewSubsegment('subseg');
stubResolve = sandbox.stub(contextUtils, 'resolveSegment').returns(segment);
addNewSubsegmentStub = sandbox.stub(segment, 'addNewSubsegment').returns(sub);
});

afterEach(function() {
afterEach(function () {
sandbox.restore();
});

Expand All @@ -125,13 +175,13 @@ describe('AWS v3 patcher', function() {

awsClient.send(awsRequest);

setTimeout(function() {
setTimeout(function () {
logStub.should.have.been.calledOnce;
done();
}, 50);
});

it('should inject the tracing headers', async function() {
it('should inject the tracing headers', async function () {
await awsClient.send(awsRequest);
assert.isTrue(addNewSubsegmentStub.calledWith('S3'));

Expand All @@ -140,7 +190,7 @@ describe('AWS v3 patcher', function() {
assert.match(awsRequest.request.headers['X-Amzn-Trace-Id'], expected);
});

it('should close on complete with no errors when code 200 is seen', async function() {
it('should close on complete with no errors when code 200 is seen', async function () {
const closeStub = sandbox.stub(sub, 'close').returns();
sandbox.stub(sub, 'addAttribute').returns();
sandbox.stub(Aws.prototype, 'init').returns();
Expand All @@ -154,7 +204,7 @@ describe('AWS v3 patcher', function() {
closeStub.should.have.been.calledWithExactly();
});

it('should mark the subsegment as throttled and error if code 429 is seen', async function() {
it('should mark the subsegment as throttled and error if code 429 is seen', async function () {
const throttleStub = sandbox.stub(sub, 'addThrottleFlag').returns();

sandbox.stub(sub, 'addAttribute').returns();
Expand All @@ -171,7 +221,7 @@ describe('AWS v3 patcher', function() {
assert.isTrue(sub.error);
});

it('should mark the subsegment as throttled and error if code service.throttledError returns true, regardless of status code', async function() {
it('should mark the subsegment as throttled and error if code service.throttledError returns true, regardless of status code', async function () {
const throttleStub = sandbox.stub(sub, 'addThrottleFlag').returns();

sandbox.stub(sub, 'addAttribute').returns();
Expand All @@ -188,7 +238,7 @@ describe('AWS v3 patcher', function() {
assert.isTrue(sub.error);
});

it('should capture an error on the response and mark exception as remote', async function() {
it('should capture an error on the response and mark exception as remote', async function () {
const closeStub = sandbox.stub(sub, 'close').returns();
const getCauseStub = sandbox.stub(Utils, 'getCauseTypeFromHttpStatus').returns();

Expand All @@ -205,7 +255,15 @@ describe('AWS v3 patcher', function() {
await awsClient.send(awsRequest).catch(() => null);

getCauseStub.should.have.been.calledWithExactly(500);
closeStub.should.have.been.calledWithExactly(sinon.match({ message: error.message, name: error.code}), true);
closeStub.should.have.been.calledWithExactly(sinon.match({ message: error.message, name: error.code }), true);
});

it('should pass params into segment', async function () {
await ddbClient.send(ddbGetRequest);

assert.isTrue(addNewSubsegmentStub.calledWith('DynamoDB'));
addNewSubsegmentStub.returnValues[0].should.have.property('aws');
addNewSubsegmentStub.returnValues[0].aws.should.include({ consistent_read: true, table_name: 'TestTableName' });
});
});

Expand All @@ -220,7 +278,7 @@ describe('AWS v3 patcher', function() {

awsClient.send(awsRequest);

setTimeout(function() {
setTimeout(function () {
logStub.should.have.been.calledOnce;
done();
}, 50);
Expand Down Expand Up @@ -250,10 +308,10 @@ describe('AWS v3 patcher', function() {
});


describe('#captureAWSRequest-Unsampled', function() {
describe('#captureAWSRequest-Unsampled', function () {
var awsClient, awsRequest, sandbox, segment, stubResolve, addNewSubsegmentStub, sub, service, addNewServiceSubsegmentStub;

before(function() {
before(function () {
awsClient = {
send: async (req) => {
const context = {
Expand All @@ -280,7 +338,7 @@ describe('AWS v3 patcher', function() {
};
});

beforeEach(function() {
beforeEach(function () {
sandbox = sinon.createSandbox();

awsRequest = new (class ListBucketsCommand {
Expand All @@ -304,7 +362,7 @@ describe('AWS v3 patcher', function() {
})();

segment = new Segment('testSegment', traceId);
segment.additionalTraceData = {'Foo': 'bar'};
segment.additionalTraceData = { 'Foo': 'bar' };
sub = segment.addNewSubsegmentWithoutSampling('subseg');
service = sub.addNewSubsegmentWithoutSampling('service');

Expand All @@ -314,7 +372,7 @@ describe('AWS v3 patcher', function() {

});

afterEach(function() {
afterEach(function () {
sandbox.restore();
});

Expand All @@ -328,7 +386,7 @@ describe('AWS v3 patcher', function() {
});


it('should inject the tracing headers', async function() {
it('should inject the tracing headers', async function () {
await awsClient.send(awsRequest);
assert.isTrue(addNewServiceSubsegmentStub.calledWith('S3'));

Expand Down

0 comments on commit 6bfadb4

Please sign in to comment.