Skip to content

Commit

Permalink
Create unit-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
direnakkoc committed Aug 24, 2022
1 parent bfda348 commit 7c919f6
Show file tree
Hide file tree
Showing 14 changed files with 586 additions and 399 deletions.
1 change: 1 addition & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ jobs:
run: |
npm i serverless@2
npm run test:v2
finish:
needs: build
runs-on: ubuntu-latest
Expand Down
12 changes: 6 additions & 6 deletions serverless-plugin/alarms-alb.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ module.exports = function ALBlarms (albAlarmConfig, context) {
)

for (const [loadBalancerResourceName, loadBalancerResource] of Object.entries(loadBalancerResources)) {
if (albAlarmConfig.HTTPCode_ELB_5XX_Count.enabled) {
if (albAlarmConfig.HTTPCode_ELB_5XX_Count?.enabled) {
const httpCodeELB5XXCount = createHTTPCodeELB5XXCountAlarm(
loadBalancerResourceName,
loadBalancerResource,
Expand All @@ -45,7 +45,7 @@ module.exports = function ALBlarms (albAlarmConfig, context) {
cfTemplate.addResource(httpCodeELB5XXCount.resourceName, httpCodeELB5XXCount.resource)
}

if (albAlarmConfig.RejectedConnectionCount.enabled) {
if (albAlarmConfig.RejectedConnectionCount?.enabled) {
const rejectedConnectionCount = createRejectedConnectionCountAlarm(
loadBalancerResourceName,
loadBalancerResource,
Expand All @@ -61,7 +61,7 @@ module.exports = function ALBlarms (albAlarmConfig, context) {
for (const [targetGroupResourceName, targetGroupResource] of Object.entries(targetGroupResources)) {
const loadBalancerName = resolveLoadBalancerLogicalIdName(loadBalancerResources)

if (albAlarmConfig.HTTPCode_Target_5XX_Count.enabled) {
if (albAlarmConfig.HTTPCode_Target_5XX_Count?.enabled) {
const httpCodeTarget5XXCount = createHTTPCodeTarget5XXCountAlarm(
targetGroupResourceName,
targetGroupResource,
Expand All @@ -70,7 +70,7 @@ module.exports = function ALBlarms (albAlarmConfig, context) {
)
cfTemplate.addResource(httpCodeTarget5XXCount.resourceName, httpCodeTarget5XXCount.resource)
}
if (albAlarmConfig.UnHealthyHostCount.enabled) {
if (albAlarmConfig.UnHealthyHostCount?.enabled) {
const unHealthyHostCount = createUnHealthyHostCountAlarm(
targetGroupResourceName,
targetGroupResource,
Expand All @@ -79,7 +79,7 @@ module.exports = function ALBlarms (albAlarmConfig, context) {
)
cfTemplate.addResource(unHealthyHostCount.resourceName, unHealthyHostCount.resource)
}
if (albAlarmConfig.LambdaInternalError.enabled) {
if (albAlarmConfig.LambdaInternalError?.enabled) {
const lambdaInternalError = createLambdaInternalErrorAlarm(
targetGroupResourceName,
targetGroupResource,
Expand All @@ -88,7 +88,7 @@ module.exports = function ALBlarms (albAlarmConfig, context) {
)
cfTemplate.addResource(lambdaInternalError.resourceName, lambdaInternalError.resource)
}
if (albAlarmConfig.LambdaUserError.enabled) {
if (albAlarmConfig.LambdaUserError?.enabled) {
const lambdaUserError = createLambdaUserErrorAlarm(
targetGroupResourceName,
targetGroupResource,
Expand Down
4 changes: 3 additions & 1 deletion serverless-plugin/config-schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ const supportedAlarms = {
ECS: ['MemoryUtilization', 'CPUUtilization'],
SNS: ['NumberOfNotificationsFilteredOut-InvalidAttributes', 'NumberOfNotificationsFailed'],
Events: ['FailedInvocations', 'ThrottledRules'],
ApplicationELB: ['HTTPCode_ELB_5XX_Count', 'RejectedConnectionCount', 'HTTPCode_Target_5XX_Count', 'UnHealthyHostCount', 'LambdaInternalError', 'LambdaUserError']
ApplicationELB: ['HTTPCode_ELB_5XX_Count', 'RejectedConnectionCount'],
ApplicationELBTarget: ['HTTPCode_Target_5XX_Count', 'UnHealthyHostCount', 'LambdaInternalError', 'LambdaUserError']

}

const supportedWidgets = {
Expand Down
100 changes: 52 additions & 48 deletions serverless-plugin/dashboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -565,31 +565,33 @@ module.exports = function dashboard (serverless, dashboardConfig, functionDashbo
*/
async function createLoadBalancerWidgets (loadBalancerResources) {
const loadBalancerWidgets = []
const loadBalancerFullName = await getLoadBalancerFullName(loadBalancerResources)
for (const res of Object.values(loadBalancerResources)) {
const loadBalancerName = res.Properties.Name
const widgetMetrics = []
for (const [metric, metricConfig] of Object.entries(getConfiguredMetrics(albDashConfig))) {
if (metricConfig.enabled) {
for (const stat of metricConfig.Statistic) {
widgetMetrics.push({
namespace: 'AWS/ApplicationELB',
metric,
dimensions: {
LoadBalancer: loadBalancerFullName
},
stat
})
if (Object.keys(loadBalancerResources).length) {
const loadBalancerFullName = await getLoadBalancerFullName(loadBalancerResources)
for (const res of Object.values(loadBalancerResources)) {
const loadBalancerName = res.Properties.Name
const widgetMetrics = []
for (const [metric, metricConfig] of Object.entries(getConfiguredMetrics(albDashConfig))) {
if (metricConfig.enabled) {
for (const stat of metricConfig.Statistic) {
widgetMetrics.push({
namespace: 'AWS/ApplicationELB',
metric,
dimensions: {
LoadBalancer: loadBalancerFullName
},
stat
})
}
}
}
}
if (widgetMetrics.length > 0) {
const metricStatWidget = createMetricWidget(
`Application Load Balancer ${loadBalancerName}`,
widgetMetrics,
albDashConfig
)
loadBalancerWidgets.push(metricStatWidget)
if (widgetMetrics.length > 0) {
const metricStatWidget = createMetricWidget(
`Application Load Balancer ${loadBalancerName}`,
widgetMetrics,
albDashConfig
)
loadBalancerWidgets.push(metricStatWidget)
}
}
}
return loadBalancerWidgets
Expand All @@ -603,33 +605,35 @@ module.exports = function dashboard (serverless, dashboardConfig, functionDashbo
*/
async function createTargetGroupWidgets (targetGroupResources, loadBalancerResources) {
const targetGroupWidgets = []
const targetGroupFullName = await getTargetGroupFullName(targetGroupResources)
const loadBalancerFullName = await getLoadBalancerFullName(loadBalancerResources)
for (const res of Object.values(loadBalancerResources)) {
const loadBalancerName = res.Properties.Name
const widgetMetrics = []
for (const [metric, metricConfig] of Object.entries(getConfiguredMetrics(albTargetDashConfig))) {
if (metricConfig.enabled) {
for (const stat of metricConfig.Statistic) {
widgetMetrics.push({
namespace: 'AWS/ApplicationELB',
metric,
dimensions: {
LoadBalancer: loadBalancerFullName,
TargetGroup: targetGroupFullName
},
stat
})
if (Object.keys(targetGroupResources).length && Object.keys(loadBalancerResources).length) {
const targetGroupFullName = await getTargetGroupFullName(targetGroupResources)
const loadBalancerFullName = await getLoadBalancerFullName(loadBalancerResources)
for (const res of Object.values(loadBalancerResources)) {
const loadBalancerName = res.Properties.Name
const widgetMetrics = []
for (const [metric, metricConfig] of Object.entries(getConfiguredMetrics(albTargetDashConfig))) {
if (metricConfig.enabled) {
for (const stat of metricConfig.Statistic) {
widgetMetrics.push({
namespace: 'AWS/ApplicationELB',
metric,
dimensions: {
LoadBalancer: loadBalancerFullName,
TargetGroup: targetGroupFullName
},
stat
})
}
}
}
}
if (widgetMetrics.length > 0) {
const metricStatWidget = createMetricWidget(
`Application Load Balancer/Target Group ${loadBalancerName}`,
widgetMetrics,
albTargetDashConfig
)
targetGroupWidgets.push(metricStatWidget)
if (widgetMetrics.length > 0) {
const metricStatWidget = createMetricWidget(
`Application Load Balancer/Target Group ${loadBalancerName}`,
widgetMetrics,
albTargetDashConfig
)
targetGroupWidgets.push(metricStatWidget)
}
}
}
return targetGroupWidgets
Expand Down
1 change: 1 addition & 0 deletions serverless-plugin/default-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ alarms:
RejectedConnectionCount:
Statistic: Sum
Threshold: 0
ApplicationELBTarget:
HTTPCode_Target_5XX_Count:
Statistic: Sum
Threshold: 0
Expand Down
33 changes: 22 additions & 11 deletions serverless-plugin/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions serverless-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"license": "Apache",
"dependencies": {
"@aws-sdk/client-elastic-load-balancing-v2": "^3.145.0",
"@tapjs/mock-esm": "github:tapjs/mock-esm",
"ajv": "^8.9.0",
"case": "^1.6.3",
"lodash": "^4.17.21",
Expand Down

0 comments on commit 7c919f6

Please sign in to comment.