Skip to content

Commit

Permalink
Adding dlq update tags tests
Browse files Browse the repository at this point in the history
  • Loading branch information
CarlosGamero committed Oct 19, 2024
1 parent 4f4a2a8 commit b3033c7
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { SNSClient } from '@aws-sdk/client-sns'
import type { SQSClient } from '@aws-sdk/client-sqs'
import { ListQueueTagsCommand, type SQSClient } from '@aws-sdk/client-sqs'
import { waitAndRetry } from '@lokalise/node-core'
import {
type SQSMessage,
Expand Down Expand Up @@ -54,75 +54,149 @@ describe('SnsSqsPermissionConsumer - dead letter queue', () => {
})

describe('init', () => {
const topicName = 'sometopic'
const queueName = 'myQueue'
const deadLetterQueueName = 'deadLetterQueue'

beforeEach(async () => {
await deleteQueue(sqsClient, queueName)
await deleteQueue(sqsClient, deadLetterQueueName)
await deleteTopic(snsClient, topicName)
})

it('creates a new dead letter queue', async () => {
const newConsumer = new SnsSqsPermissionConsumer(diContainer.cradle, {
creationConfig: {
topic: { Name: 'sometopic' },
queue: { QueueName: 'existingQueue' },
topic: { Name: topicName },
queue: { QueueName: queueName },
updateAttributesIfExists: true,
},
deadLetterQueue: {
redrivePolicy: { maxReceiveCount: 3 },
creationConfig: {
queue: { QueueName: 'deadLetterQueue' },
queue: { QueueName: deadLetterQueueName },
},
},
})

await newConsumer.init()
expect(newConsumer.subscriptionProps.queueUrl).toBe(
'http://sqs.eu-west-1.localstack:4566/000000000000/existingQueue',
`http://sqs.eu-west-1.localstack:4566/000000000000/${queueName}`,
)
expect(newConsumer.subscriptionProps.deadLetterQueueUrl).toBe(
'http://sqs.eu-west-1.localstack:4566/000000000000/deadLetterQueue',
`http://sqs.eu-west-1.localstack:4566/000000000000/${deadLetterQueueName}`,
)

const attributes = await getQueueAttributes(sqsClient, newConsumer.subscriptionProps.queueUrl)

expect(attributes.result?.attributes).toMatchObject({
RedrivePolicy: JSON.stringify({
deadLetterTargetArn: `arn:aws:sqs:eu-west-1:000000000000:deadLetterQueue`,
deadLetterTargetArn: `arn:aws:sqs:eu-west-1:000000000000:${deadLetterQueueName}`,
maxReceiveCount: 3,
}),
})
})

it('using existing dead letter queue', async () => {
await assertQueue(sqsClient, {
QueueName: 'deadLetterQueue',
QueueName: deadLetterQueueName,
})

const newConsumer = new SnsSqsPermissionConsumer(diContainer.cradle, {
creationConfig: {
topic: { Name: 'sometopic' },
queue: { QueueName: 'existingQueue' },
topic: { Name: topicName },
queue: { QueueName: queueName },
updateAttributesIfExists: true,
},
deadLetterQueue: {
redrivePolicy: { maxReceiveCount: 3 },
locatorConfig: {
queueUrl: 'http://sqs.eu-west-1.localstack:4566/000000000000/deadLetterQueue',
queueUrl: `http://sqs.eu-west-1.localstack:4566/000000000000/${deadLetterQueueName}`,
},
},
})

await newConsumer.init()
expect(newConsumer.subscriptionProps.queueUrl).toBe(
'http://sqs.eu-west-1.localstack:4566/000000000000/existingQueue',
`http://sqs.eu-west-1.localstack:4566/000000000000/${queueName}`,
)
expect(newConsumer.subscriptionProps.deadLetterQueueUrl).toBe(
'http://sqs.eu-west-1.localstack:4566/000000000000/deadLetterQueue',
`http://sqs.eu-west-1.localstack:4566/000000000000/${deadLetterQueueName}`,
)

const attributes = await getQueueAttributes(sqsClient, newConsumer.subscriptionProps.queueUrl)

expect(attributes.result?.attributes).toMatchObject({
RedrivePolicy: JSON.stringify({
deadLetterTargetArn: `arn:aws:sqs:eu-west-1:000000000000:deadLetterQueue`,
deadLetterTargetArn: `arn:aws:sqs:eu-west-1:000000000000:${deadLetterQueueName}`,
maxReceiveCount: 3,
}),
})
})

it('should update attributes and tags', async () => {
await assertQueue(sqsClient, {
QueueName: deadLetterQueueName,
Attributes: { KmsMasterKeyId: 'old' },
tags: { tag: 'old' },
})

const newConsumer = new SnsSqsPermissionConsumer(diContainer.cradle, {
creationConfig: {
topic: { Name: topicName },
queue: { QueueName: queueName },
updateAttributesIfExists: true,
},
deadLetterQueue: {
redrivePolicy: { maxReceiveCount: 3 },
creationConfig: {
forceTagUpdate: true,
updateAttributesIfExists: true,
queue: {
QueueName: deadLetterQueueName,
Attributes: { KmsMasterKeyId: 'new' },
tags: { tag: 'new' },
},
},
},
})

await newConsumer.init()
expect(newConsumer.subscriptionProps.queueUrl).toBe(
`http://sqs.eu-west-1.localstack:4566/000000000000/${queueName}`,
)
expect(newConsumer.subscriptionProps.deadLetterQueueUrl).toBe(
`http://sqs.eu-west-1.localstack:4566/000000000000/${deadLetterQueueName}`,
)

const mainQueueAttributes = await getQueueAttributes(
sqsClient,
newConsumer.subscriptionProps.queueUrl,
)
expect(mainQueueAttributes.result?.attributes).toMatchObject({
RedrivePolicy: JSON.stringify({
deadLetterTargetArn: `arn:aws:sqs:eu-west-1:000000000000:${deadLetterQueueName}`,
maxReceiveCount: 3,
}),
})

const dlqAttributes = await getQueueAttributes(
sqsClient,
newConsumer.subscriptionProps.deadLetterQueueUrl!,
)
expect(dlqAttributes.result?.attributes).toMatchObject({
KmsMasterKeyId: 'new',
})

const tags = await sqsClient.send(
new ListQueueTagsCommand({ QueueUrl: newConsumer.subscriptionProps.deadLetterQueueUrl }),
)
expect(tags.Tags).toMatchInlineSnapshot(`
{
"tag": "new",
}
`)
})
})

describe('messages are sent to DLQ', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { SQSClient } from '@aws-sdk/client-sqs'
import { ListQueueTagsCommand, type SQSClient } from '@aws-sdk/client-sqs'
import { SendMessageCommand } from '@aws-sdk/client-sqs'
import { waitAndRetry } from '@lokalise/node-core'
import type { AwilixContainer } from 'awilix'
Expand Down Expand Up @@ -118,6 +118,7 @@ describe('SqsPermissionConsumer - deadLetterQueue', () => {
const result = await assertQueue(sqsClient, {
QueueName: customDeadLetterQueueName,
Attributes: { KmsMasterKeyId: 'my first value' },
tags: { tag: 'old', hello: 'world' },
})
dlqUrl = result.queueUrl
})
Expand Down Expand Up @@ -154,7 +155,7 @@ describe('SqsPermissionConsumer - deadLetterQueue', () => {
expect(consumer.dlqUrl).toBe(dlqUrl)
})

it('updates existing dlq when one with different attributes exist', async () => {
it('updates existing dlq attributes', async () => {
consumer = new SqsPermissionConsumer(diContainer.cradle, {
creationConfig: { queue: { QueueName: customQueueName }, updateAttributesIfExists: true },
deadLetterQueue: {
Expand All @@ -180,6 +181,37 @@ describe('SqsPermissionConsumer - deadLetterQueue', () => {
expect(attributes.result?.attributes!.KmsMasterKeyId).toBe('new value')
})

it('updates existing dlq tags', async () => {
consumer = new SqsPermissionConsumer(diContainer.cradle, {
creationConfig: { queue: { QueueName: customQueueName }, updateAttributesIfExists: true },
deadLetterQueue: {
redrivePolicy: { maxReceiveCount: 5 },
creationConfig: {
forceTagUpdate: true,
queue: {
QueueName: customDeadLetterQueueName,
tags: { tag: 'new', good: 'bye' },
},
},
},
})

await consumer.init()
expect(consumer.queueProps.url).toBe(
`http://sqs.eu-west-1.localstack:4566/000000000000/${customQueueName}`,
)
expect(consumer.dlqUrl).toBe(dlqUrl)

const tags = await sqsClient.send(new ListQueueTagsCommand({ QueueUrl: consumer.dlqUrl }))
expect(tags.Tags).toMatchInlineSnapshot(`
{
"good": "bye",
"hello": "world",
"tag": "new",
}
`)
})

it('connect existing dlq to existing queue', async () => {
const { queueUrl } = await assertQueue(sqsClient, { QueueName: customQueueName })

Expand Down

0 comments on commit b3033c7

Please sign in to comment.