-
Notifications
You must be signed in to change notification settings - Fork 177
fix(mixin-utils): remove std.prune and use std.filter instead #1441
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
8d31da8
6eb9acb
ee07910
96888b6
1375287
1ede888
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,284 @@ | ||
| local utils = import '../utils.libsonnet'; | ||
| local test = import 'github.com/jsonnet-libs/testonnet/main.libsonnet'; | ||
|
|
||
| local config = { | ||
| prometheusAlerts: { | ||
| groups: [ | ||
| { | ||
| name: 'group1', | ||
| rules: [ | ||
| { | ||
| alert: 'alert1', | ||
| }, | ||
| { | ||
| alert: 'alert2', | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| name: 'group2', | ||
| rules: [ | ||
| { | ||
| alert: 'alert3', | ||
| }, | ||
| { | ||
| alert: 'alert4', | ||
| }, | ||
| ], | ||
| }, | ||
| ], | ||
| }, | ||
| prometheusRules: { | ||
| groups: [ | ||
| { | ||
| name: 'group3', | ||
| rules: [ | ||
| { | ||
| record: 'record1', | ||
| }, | ||
| { | ||
| record: 'record2', | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| name: 'group4', | ||
| rules: [ | ||
| { | ||
| record: 'record3', | ||
| }, | ||
| { | ||
| record: 'record4', | ||
| }, | ||
| ], | ||
| }, | ||
| ], | ||
| }, | ||
| }; | ||
|
|
||
| test.new(std.thisFile) | ||
|
|
||
| + test.case.new( | ||
| 'removeAlertRuleGroup', | ||
| test.expect.eq( | ||
| actual=config + utils.removeAlertRuleGroup('group1'), | ||
| expected={ | ||
| prometheusAlerts: { | ||
| groups: [ | ||
| { | ||
| name: 'group2', | ||
| rules: [ | ||
| { | ||
| alert: 'alert3', | ||
| }, | ||
| { | ||
| alert: 'alert4', | ||
| }, | ||
| ], | ||
| }, | ||
| ], | ||
| }, | ||
| prometheusRules: { | ||
| groups: [ | ||
| { | ||
| name: 'group3', | ||
| rules: [ | ||
| { | ||
| record: 'record1', | ||
| }, | ||
| { | ||
| record: 'record2', | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| name: 'group4', | ||
| rules: [ | ||
| { | ||
| record: 'record3', | ||
| }, | ||
| { | ||
| record: 'record4', | ||
| }, | ||
| ], | ||
| }, | ||
| ], | ||
| }, | ||
| } | ||
| ) | ||
| ) | ||
|
|
||
| + test.case.new( | ||
| 'removeRecordingRuleGroup', | ||
| test.expect.eq( | ||
| actual=config + utils.removeRecordingRuleGroup('group4'), | ||
| expected={ | ||
| prometheusAlerts: { | ||
| groups: [ | ||
| { | ||
| name: 'group1', | ||
| rules: [ | ||
| { | ||
| alert: 'alert1', | ||
| }, | ||
| { | ||
| alert: 'alert2', | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| name: 'group2', | ||
| rules: [ | ||
| { | ||
| alert: 'alert3', | ||
| }, | ||
| { | ||
| alert: 'alert4', | ||
| }, | ||
| ], | ||
| }, | ||
| ], | ||
| }, | ||
| prometheusRules: { | ||
| groups: [ | ||
| { | ||
| name: 'group3', | ||
| rules: [ | ||
| { | ||
| record: 'record1', | ||
| }, | ||
| { | ||
| record: 'record2', | ||
| }, | ||
| ], | ||
| }, | ||
| ], | ||
| }, | ||
| } | ||
| ) | ||
| ) | ||
|
|
||
| + test.case.new( | ||
| 'removeAlertRuleGroup with groupname from recording rules (noop)', | ||
| test.expect.eq( | ||
| actual=config + utils.removeAlertRuleGroup('group4'), | ||
| expected=config | ||
| ) | ||
| ) | ||
| + test.case.new( | ||
| 'removeRecordingRuleGroup with groupname from alert rules (noop)', | ||
| test.expect.eq( | ||
| actual=config + utils.removeRecordingRuleGroup('group2'), | ||
| expected=config | ||
| ) | ||
| ) | ||
|
|
||
| + test.case.new( | ||
| 'removeAlerts', | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I ran all but this test case against main. This test case represents a new feature, |
||
| test.expect.eq( | ||
| actual=config + utils.removeAlerts(['alert1', 'alert4']), | ||
| expected={ | ||
| prometheusAlerts: { | ||
| groups: [ | ||
| { | ||
| name: 'group1', | ||
| rules: [ | ||
| { | ||
| alert: 'alert2', | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| name: 'group2', | ||
| rules: [ | ||
| { | ||
| alert: 'alert3', | ||
| }, | ||
| ], | ||
| }, | ||
| ], | ||
| }, | ||
| prometheusRules: { | ||
| groups: [ | ||
| { | ||
| name: 'group3', | ||
| rules: [ | ||
| { | ||
| record: 'record1', | ||
| }, | ||
| { | ||
| record: 'record2', | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| name: 'group4', | ||
| rules: [ | ||
| { | ||
| record: 'record3', | ||
| }, | ||
| { | ||
| record: 'record4', | ||
| }, | ||
| ], | ||
| }, | ||
| ], | ||
| }, | ||
| } | ||
| ) | ||
| ) | ||
|
|
||
| + test.case.new( | ||
| 'removeAlerts - object (backwards compatible)', | ||
| test.expect.eq( | ||
| actual=config + utils.removeAlerts({ alert1: {}, alert4: {} }), | ||
| expected={ | ||
| prometheusAlerts: { | ||
| groups: [ | ||
| { | ||
| name: 'group1', | ||
| rules: [ | ||
| { | ||
| alert: 'alert2', | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| name: 'group2', | ||
| rules: [ | ||
| { | ||
| alert: 'alert3', | ||
| }, | ||
| ], | ||
| }, | ||
| ], | ||
| }, | ||
| prometheusRules: { | ||
| groups: [ | ||
| { | ||
| name: 'group3', | ||
| rules: [ | ||
| { | ||
| record: 'record1', | ||
| }, | ||
| { | ||
| record: 'record2', | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| name: 'group4', | ||
| rules: [ | ||
| { | ||
| record: 'record3', | ||
| }, | ||
| { | ||
| record: 'record4', | ||
| }, | ||
| ], | ||
| }, | ||
| ], | ||
| }, | ||
| } | ||
| ) | ||
| ) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -392,18 +392,16 @@ local g = import 'grafana-builder/grafana.libsonnet'; | |
| for group in groups | ||
| ], | ||
|
|
||
| removeRuleGroup(ruleName):: { | ||
| local removeRuleGroup(rule) = if rule.name == ruleName then null else rule, | ||
| local currentRuleGroups = super.groups, | ||
| groups: std.prune(std.map(removeRuleGroup, currentRuleGroups)), | ||
| removeRuleGroup(groupName):: { | ||
| groups: std.filter(function(group) group.name != groupName, super.groups), | ||
| }, | ||
|
|
||
| removeAlertRuleGroup(ruleName):: { | ||
| prometheusAlerts+:: $.removeRuleGroup(ruleName), | ||
| prometheusAlerts+: $.removeRuleGroup(ruleName), | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| }, | ||
|
|
||
| removeRecordingRuleGroup(ruleName):: { | ||
| prometheusRules+:: $.removeRuleGroup(ruleName), | ||
| prometheusRules+: $.removeRuleGroup(ruleName), | ||
| }, | ||
|
|
||
| overrideAlerts(overrides):: { | ||
|
|
@@ -412,19 +410,20 @@ local g = import 'grafana-builder/grafana.libsonnet'; | |
| then rule + overrides[rule.alert] | ||
| else rule, | ||
| local overrideInGroup(group) = group { rules: std.map(overrideRule, super.rules) }, | ||
| prometheusAlerts+:: { | ||
| prometheusAlerts+: { | ||
| groups: std.map(overrideInGroup, super.groups), | ||
| }, | ||
| }, | ||
|
|
||
| removeAlerts(alerts):: { | ||
| local removeRule(rule) = | ||
| if 'alert' in rule && std.objectHas(alerts, rule.alert) | ||
Duologic marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| then {} | ||
| else rule, | ||
| local removeInGroup(group) = group { rules: std.map(removeRule, super.rules) }, | ||
| prometheusAlerts+:: { | ||
| groups: std.prune(std.map(removeInGroup, super.groups)), | ||
| local alertNames = | ||
| if std.isObject(alerts) | ||
| then std.objectFields(alerts) | ||
| else alerts, | ||
| local removeRule(rule) = !std.member(alertNames, std.get(rule, 'alert', '')), | ||
| local removeInGroup(group) = group { rules: std.filter(removeRule, super.rules) }, | ||
| prometheusAlerts+: { | ||
| groups: std.map(removeInGroup, super.groups), | ||
| }, | ||
| }, | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for adding tests. This also shows how these functions are supposed to be used! That's very useful!