feat(event-broker): add test for region extraction

Closes #1939
This commit is contained in:
Ben Bangert 2019-07-25 12:53:30 -07:00
Родитель 57b2cbecf8
Коммит fe2b56989c
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 340D6D716D25CCA6
2 изменённых файлов: 36 добавлений и 4 удалений

Просмотреть файл

@ -23,6 +23,15 @@ const db = firestoreEnabled
? createDatastore(FirestoreDatastore, firestoreConfig)
: createDatastore(InMemoryDatastore, {});
export function extractRegionFromUrl(url: string) {
const matchResult = url.match(/(?:.*\/sqs\.)([^\.]+)/i);
if (!matchResult || matchResult.length !== 2) {
return undefined;
} else {
return matchResult[1];
}
}
async function main() {
const capabilityService = new ClientCapabilityService(
logger,
@ -37,15 +46,14 @@ async function main() {
// Extract region for SQS object
const serviceNotificationQueueUrl = Config.get('serviceNotificationQueueUrl');
const matchResult = serviceNotificationQueueUrl.match(/(?:.*\/sqs\.)([^\.]+)/i);
if (!matchResult || matchResult.length !== 2) {
const region = extractRegionFromUrl(serviceNotificationQueueUrl);
if (!region) {
logger.error('invalidServiceUrl', {
message: 'Cant find region in service url',
serviceNotificationQueueUrl
});
process.exit(8);
}
const region = matchResult![1];
const processor = new ServiceNotificationProcessor(
logger,
db,
@ -87,4 +95,6 @@ async function main() {
}
}
main();
if (require.main === module) {
main();
}

Просмотреть файл

@ -0,0 +1,22 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
import { assert } from 'chai';
import 'mocha';
import { extractRegionFromUrl } from '../../bin/worker';
describe('Worker', () => {
it('extracts the region from the config', () => {
const url = 'https://sqs.us-east-1.amazonaws.com/8284828319293/service-notifications';
const region = extractRegionFromUrl(url);
assert.equal(region, 'us-east-1');
});
it('extracts undefined for invalid url', () => {
const url = 'http://example.com/example';
const region = extractRegionFromUrl(url);
assert.equal(region, undefined);
});
});