Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nhulston committed Nov 22, 2024
1 parent ec6ca0b commit a65dd20
Showing 1 changed file with 97 additions and 75 deletions.
172 changes: 97 additions & 75 deletions packages/datadog-plugin-aws-sdk/test/dynamodb.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,49 +128,49 @@ describe('Plugin', () => {
describe('span pointers', () => {
beforeEach(() => {
DynamoDb.dynamoPrimaryKeyConfig = null
process.env.DD_AWS_SDK_DYNAMODB_TABLE_PRIMARY_KEYS = null
delete process.env.DD_AWS_SDK_DYNAMODB_TABLE_PRIMARY_KEYS
})

function testSpanPointers ({ expectedLength = 1, expectedHashes, operation }) {
return (done) => {
agent.use(traces => {
try {
const span = traces[0][0]
const links = JSON.parse(span.meta?.['_dd.span_links'] || '[]')
expect(links).to.have.lengthOf(expectedLength)

if (expectedHashes) {
if (Array.isArray(expectedHashes)) {
expectedHashes.forEach((hash, i) => {
expect(links[i].attributes['ptr.hash']).to.equal(hash)
})
} else {
expect(links[0].attributes).to.deep.equal({
'ptr.kind': DYNAMODB_PTR_KIND,
'ptr.dir': SPAN_POINTER_DIRECTION.DOWNSTREAM,
'ptr.hash': expectedHashes,
'link.kind': 'span-pointer'
})
}
}
done()
} catch (error) {
console.log('Test error:', error)
return done(error)
}
}).catch(done)

operation((err) => {
if (err) {
console.log('Test error:', err)
return done(err)
}

agent.use(traces => {
try {
const span = traces[0][0]
const links = JSON.parse(span.meta?.['_dd.span_links'] || '[]')
expect(links).to.have.lengthOf(expectedLength)

if (expectedHashes) {
if (Array.isArray(expectedHashes)) {
expectedHashes.forEach((hash, i) => {
expect(links[i].attributes['ptr.hash']).to.equal(hash)
})
} else {
expect(links[0].attributes).to.deep.equal({
'ptr.kind': DYNAMODB_PTR_KIND,
'ptr.dir': SPAN_POINTER_DIRECTION.DOWNSTREAM,
'ptr.hash': expectedHashes,
'link.kind': 'span-pointer'
})
}
}
return done()
} catch (error) {
return done(error)
}
}).catch(error => {
done(error)
})
})
}
}

describe('1-key table', () => {
it('should add span pointer for putItem when config is valid',
it('should add span pointer for putItem when config is valid', () => {
testSpanPointers({
expectedHashes: '27f424c8202ab35efbf8b0b444b1928f',
operation: (callback) => {
Expand All @@ -185,9 +185,9 @@ describe('Plugin', () => {
}, callback)
}
})
)
})

it('should not add links or error for putItem when config is invalid',
it('should not add links or error for putItem when config is invalid', () => {
testSpanPointers({
expectedLength: 0,
operation: (callback) => {
Expand All @@ -201,9 +201,9 @@ describe('Plugin', () => {
}, callback)
}
})
)
})

it('should not add links or error for putItem when config is missing',
it('should not add links or error for putItem when config is missing', () => {
testSpanPointers({
expectedLength: 0,
operation: (callback) => {
Expand All @@ -217,9 +217,9 @@ describe('Plugin', () => {
}, callback)
}
})
)
})

it('should add span pointer for updateItem',
it('should add span pointer for updateItem', () => {
testSpanPointers({
expectedHashes: '27f424c8202ab35efbf8b0b444b1928f',
operation: (callback) => {
Expand All @@ -235,9 +235,9 @@ describe('Plugin', () => {
}, callback)
}
})
)
})

it('should add span pointer for deleteItem',
it('should add span pointer for deleteItem', () => {
testSpanPointers({
expectedHashes: '27f424c8202ab35efbf8b0b444b1928f',
operation: (callback) => {
Expand All @@ -247,7 +247,7 @@ describe('Plugin', () => {
}, callback)
}
})
)
})

it('should add span pointers for transactWriteItems', () => {
// Skip for older versions that don't support transactWriteItems
Expand Down Expand Up @@ -336,7 +336,7 @@ describe('Plugin', () => {
})

describe('2-key table', () => {
it('should add span pointer for putItem when config is valid',
it('should add span pointer for putItem when config is valid', () => {
testSpanPointers({
expectedHashes: 'cc32f0e49ee05d3f2820ccc999bfe306',
operation: (callback) => {
Expand All @@ -350,9 +350,9 @@ describe('Plugin', () => {
}, callback)
}
})
)
})

it('should not add links or error for putItem when config is invalid',
it('should not add links or error for putItem when config is invalid', () => {
testSpanPointers({
expectedLength: 0,
operation: (callback) => {
Expand All @@ -366,9 +366,9 @@ describe('Plugin', () => {
}, callback)
}
})
)
})

it('should not add links or error for putItem when config is missing',
it('should not add links or error for putItem when config is missing', () => {
testSpanPointers({
expectedLength: 0,
operation: (callback) => {
Expand All @@ -382,43 +382,65 @@ describe('Plugin', () => {
}, callback)
}
})
)
})

it('should add span pointer for updateItem',
testSpanPointers({
expectedHashes: '8a6f801cc4e7d1d5e0dd37e0904e6316',
operation: (callback) => {
dynamo.updateItem({
TableName: twoKeyTableName,
Key: {
id: { N: '3' },
binary: { B: Buffer.from('Hello world 3') }
},
AttributeUpdates: {
someOtherField: {
Action: 'PUT',
Value: { S: 'new value' }
}
}
}, callback)
it('should add span pointer for updateItem', function (done) {
dynamo.putItem({
TableName: twoKeyTableName,
Item: {
id: { N: '100' },
binary: { B: Buffer.from('abc') }
}
}, async function (err) {
if (err) {
return done(err)
}
await new Promise(resolve => setTimeout(resolve, 100))
testSpanPointers({
expectedHashes: '5dac7d25254d596482a3c2c187e51046',
operation: (callback) => {
dynamo.updateItem({
TableName: twoKeyTableName,
Key: {
id: { N: '100' },
binary: { B: Buffer.from('abc') }
},
AttributeUpdates: {
someOtherField: {
Action: 'PUT',
Value: { S: 'new value' }
}
}
}, callback)
}
})(done)
})
)
})

it('should add span pointer for deleteItem',
testSpanPointers({
expectedHashes: '8a6f801cc4e7d1d5e0dd37e0904e6316',
operation: (callback) => {
dynamo.deleteItem({
TableName: twoKeyTableName,
Key: {
id: { N: '3' },
binary: { B: Buffer.from('Hello world 3') }
}
}, callback)
it('should add span pointer for deleteItem', function (done) {
dynamo.putItem({
TableName: twoKeyTableName,
Item: {
id: { N: '200' },
binary: { B: Buffer.from('Hello world') }
}
}, async function (err) {
if (err) return done(err)
await new Promise(resolve => setTimeout(resolve, 100))
testSpanPointers({
expectedHashes: 'c356b0dd48c734d889e95122750c2679',
operation: (callback) => {
dynamo.deleteItem({
TableName: twoKeyTableName,
Key: {
id: { N: '200' },
binary: { B: Buffer.from('Hello world') }
}
}, callback)
}
})(done)
})
)
})

it('should add span pointers for transactWriteItems', () => {
// Skip for older versions that don't support transactWriteItems
Expand Down

0 comments on commit a65dd20

Please sign in to comment.