Skip to content

Commit

Permalink
chore: extract and extend SNS dashboard tests
Browse files Browse the repository at this point in the history
  • Loading branch information
eoinsha committed Nov 28, 2023
1 parent f11d348 commit a51d950
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 23 deletions.
7 changes: 5 additions & 2 deletions core/dashboards/dashboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[] = []
Expand All @@ -495,7 +497,8 @@ export default function addDashboard (dashboardConfig: SlicWatchInputDashboardCo
dimensions: {
TopicName: `\${${logicalId}.TopicName}`
},
stat
stat,
yAxis: metricConfig.yAxis
})
}
}
Expand Down
104 changes: 104 additions & 0 deletions core/dashboards/tests/dashboard-sns.test.ts
Original file line number Diff line number Diff line change
@@ -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()
})
21 changes: 0 additions & 21 deletions core/dashboards/tests/dashboard.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down

0 comments on commit a51d950

Please sign in to comment.