From a51d950d4aee357d5d60adf9ef338f1726038fe1 Mon Sep 17 00:00:00 2001 From: Eoin Shanaghy Date: Tue, 28 Nov 2023 16:44:50 +0000 Subject: [PATCH] chore: extract and extend SNS dashboard tests --- core/dashboards/dashboard.ts | 7 +- core/dashboards/tests/dashboard-sns.test.ts | 104 ++++++++++++++++++++ core/dashboards/tests/dashboard.test.ts | 21 ---- 3 files changed, 109 insertions(+), 23 deletions(-) create mode 100644 core/dashboards/tests/dashboard-sns.test.ts diff --git a/core/dashboards/dashboard.ts b/core/dashboards/dashboard.ts index 58000843..85a0a195 100644 --- a/core/dashboards/dashboard.ts +++ b/core/dashboards/dashboard.ts @@ -481,7 +481,9 @@ export default function addDashboard (dashboardConfig: SlicWatchInputDashboardCo * Create a set of CloudWatch Dashboard widgets for SNS services. */ function createTopicWidgets (): WidgetWithSize[] { - const configuredResources = getResourceDashboardConfigurationsByType('AWS::SNS::Topic', compiledTemplate, snsDashConfig) + const configuredResources = getResourceDashboardConfigurationsByType( + 'AWS::SNS::Topic', compiledTemplate, snsDashConfig + ) const topicWidgets: WidgetWithSize[] = [] for (const logicalId of Object.keys(configuredResources.resources)) { const widgetMetrics: MetricDefs[] = [] @@ -495,7 +497,8 @@ export default function addDashboard (dashboardConfig: SlicWatchInputDashboardCo dimensions: { TopicName: `\${${logicalId}.TopicName}` }, - stat + stat, + yAxis: metricConfig.yAxis }) } } diff --git a/core/dashboards/tests/dashboard-sns.test.ts b/core/dashboards/tests/dashboard-sns.test.ts new file mode 100644 index 00000000..7e3777b0 --- /dev/null +++ b/core/dashboards/tests/dashboard-sns.test.ts @@ -0,0 +1,104 @@ +import { merge } from 'lodash' +import { test } from 'tap' +import { type MetricWidgetProperties } from 'cloudwatch-dashboard-types' + +import { type ResourceType } from '../../cf-template' +import addDashboard from '../dashboard' +import defaultConfig from '../../inputs/default-config' + +import { createTestCloudFormationTemplate, getDashboardFromTemplate, getDashboardWidgetsByTitle } from '../../tests/testing-utils' + +test('dashboard contains configured SNS resources', (t) => { + t.test('dashboards includes SQS metrics', (t) => { + const template = createTestCloudFormationTemplate() + addDashboard(defaultConfig.dashboard, template) + const { dashboard } = getDashboardFromTemplate(template) + + const widgets = getDashboardWidgetsByTitle(dashboard, + /SNS Topic/ + ) + + const [snsTopicWidget] = widgets + t.ok(snsTopicWidget) + t.same((snsTopicWidget.properties as MetricWidgetProperties).metrics, [ + ['AWS/SNS', 'NumberOfNotificationsFilteredOut-InvalidAttributes', 'TopicName', '${topic.TopicName}', { stat: 'Sum', yAxis: 'left' }], + ['AWS/SNS', 'NumberOfNotificationsFailed', 'TopicName', '${topic.TopicName}', { stat: 'Sum', yAxis: 'left' }] + ]) + + t.end() + }) + + t.test('service config overrides take precedence', (t) => { + const template = createTestCloudFormationTemplate() + + const config = merge({}, defaultConfig.dashboard, { + widgets: { + SNS: { + metricPeriod: 900, + width: 8, + height: 12, + 'NumberOfNotificationsFilteredOut-InvalidAttributes': { + Statistic: ['ts80'], + yAxis: 'right' + }, + NumberOfNotificationsFailed: { + Statistic: ['ts85'], + yAxis: 'left' + } + } + } + }) + + addDashboard(config, template) + const { dashboard } = getDashboardFromTemplate(template) + + const widgets = getDashboardWidgetsByTitle(dashboard, + /SNS Topic/ + ) + + const [snsTopicWidget] = widgets + t.ok(snsTopicWidget) + t.same((snsTopicWidget.properties as MetricWidgetProperties).metrics, [ + ['AWS/SNS', 'NumberOfNotificationsFilteredOut-InvalidAttributes', 'TopicName', '${topic.TopicName}', { stat: 'ts80', yAxis: 'right' }], + ['AWS/SNS', 'NumberOfNotificationsFailed', 'TopicName', '${topic.TopicName}', { stat: 'ts85', yAxis: 'left' }] + ]) + + t.end() + }) + + t.test('resource config overrides take precedence', (t) => { + const template = createTestCloudFormationTemplate(); + (template.Resources as ResourceType).topic.Metadata = { + slicWatch: { + dashboard: { + 'NumberOfNotificationsFilteredOut-InvalidAttributes': { + Statistic: ['ts80'], + yAxis: 'right', + enabled: false + }, + NumberOfNotificationsFailed: { + Statistic: ['ts90'], + yAxis: 'right' + } + } + } + } + + addDashboard(defaultConfig.dashboard, template) + const { dashboard } = getDashboardFromTemplate(template) + + const widgets = getDashboardWidgetsByTitle(dashboard, + /SNS Topic/ + ) + + const [snsTopicWidget] = widgets + t.ok(snsTopicWidget) + t.same((snsTopicWidget.properties as MetricWidgetProperties).metrics, [ + ['AWS/SNS', 'NumberOfNotificationsFailed', 'TopicName', '${topic.TopicName}', { stat: 'ts90', yAxis: 'right' }] + ]) + + t.end() + }) + + t.end() +}) diff --git a/core/dashboards/tests/dashboard.test.ts b/core/dashboards/tests/dashboard.test.ts index 8a05f457..28bfdacf 100644 --- a/core/dashboards/tests/dashboard.test.ts +++ b/core/dashboards/tests/dashboard.test.ts @@ -53,27 +53,6 @@ test('A dashboard includes metrics', (t) => { t.end() }) - t.test('dashboard includes SNS metrics', (t) => { - const widgets = dashboard.widgets.filter(({ properties }) => - ((properties as MetricWidgetProperties).title ?? '').startsWith('SNS') - ) - t.equal(widgets.length, 1) - const namespaces = new Set() - for (const widget of widgets) { - for (const metric of (widget.properties as MetricWidgetProperties).metrics ?? []) { - namespaces.add(metric[0]) - } - } - t.same(namespaces, new Set(['AWS/SNS'])) - const expectedTitles = new Set(['SNS Topic ${topic.TopicName}']) - - const actualTitles = new Set( - widgets.map((widget) => (widget.properties as MetricWidgetProperties).title) - ) - t.same(actualTitles, expectedTitles) - t.end() - }) - t.test('dashboard includes Events metrics', (t) => { const widgets = dashboard.widgets.filter(({ properties }) => ((properties as MetricWidgetProperties).title ?? '').startsWith('EventBridge')