|
| 1 | +package plugin_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + "time" |
| 6 | + |
| 7 | + prommodel "github.com/prometheus/common/model" |
| 8 | + "github.com/prometheus/prometheus/model/rulefmt" |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | + |
| 11 | + plugin "github.com/slok/sloth/internal/plugin/slo/contrib/alert_for_v1" |
| 12 | + "github.com/slok/sloth/pkg/common/conventions" |
| 13 | + "github.com/slok/sloth/pkg/common/model" |
| 14 | + prometheusv1 "github.com/slok/sloth/pkg/prometheus/api/v1" |
| 15 | + pluginslov1 "github.com/slok/sloth/pkg/prometheus/plugin/slo/v1" |
| 16 | + pluginslov1testing "github.com/slok/sloth/pkg/prometheus/plugin/slo/v1/testing" |
| 17 | +) |
| 18 | + |
| 19 | +func TestPlugin(t *testing.T) { |
| 20 | + tests := map[string]struct { |
| 21 | + pluginFactory func(t *testing.T) (pluginslov1.Plugin, error) |
| 22 | + req pluginslov1.Request |
| 23 | + res pluginslov1.Result |
| 24 | + expRes pluginslov1.Result |
| 25 | + }{ |
| 26 | + "Using the plugin as embedded yaegi plugin, it should set page and ticket `for` durations.": { |
| 27 | + pluginFactory: func(t *testing.T) (pluginslov1.Plugin, error) { |
| 28 | + return pluginslov1testing.NewTestPlugin(t.Context(), pluginslov1testing.TestPluginConfig{}) |
| 29 | + }, |
| 30 | + req: pluginslov1.Request{ |
| 31 | + SLO: model.PromSLO{Name: "requests-availability"}, |
| 32 | + MWMBAlertGroup: model.MWMBAlertGroup{ |
| 33 | + PageQuick: model.MWMBAlert{Severity: model.PageAlertSeverity}, |
| 34 | + TicketQuick: model.MWMBAlert{Severity: model.TicketAlertSeverity}, |
| 35 | + }, |
| 36 | + OriginalSource: model.PromSLOGroupSource{ |
| 37 | + SlothV1: &prometheusv1.Spec{ |
| 38 | + Version: prometheusv1.Version, |
| 39 | + Service: "myservice", |
| 40 | + SLOs: []prometheusv1.SLO{ |
| 41 | + { |
| 42 | + Name: "requests-availability", |
| 43 | + Objective: 99.9, |
| 44 | + SLI: prometheusv1.SLI{Raw: &prometheusv1.SLIRaw{ErrorRatioQuery: "1"}}, |
| 45 | + Alerting: prometheusv1.Alerting{ |
| 46 | + Name: "MyServiceHighErrorRate", |
| 47 | + PageAlert: prometheusv1.Alert{For: prommodel.Duration(5 * time.Minute)}, |
| 48 | + TicketAlert: prometheusv1.Alert{For: prommodel.Duration(10 * time.Minute)}, |
| 49 | + }, |
| 50 | + }, |
| 51 | + }, |
| 52 | + }, |
| 53 | + }, |
| 54 | + }, |
| 55 | + res: pluginslov1.Result{ |
| 56 | + SLORules: model.PromSLORules{ |
| 57 | + AlertRules: model.PromRuleGroup{ |
| 58 | + Rules: []rulefmt.Rule{ |
| 59 | + {Alert: "MyServiceHighErrorRate", Labels: map[string]string{conventions.PromSLOSeverityLabelName: "page"}}, |
| 60 | + {Alert: "MyServiceHighErrorRate", Labels: map[string]string{conventions.PromSLOSeverityLabelName: "ticket"}}, |
| 61 | + }, |
| 62 | + }, |
| 63 | + }, |
| 64 | + }, |
| 65 | + expRes: pluginslov1.Result{ |
| 66 | + SLORules: model.PromSLORules{ |
| 67 | + AlertRules: model.PromRuleGroup{ |
| 68 | + Rules: []rulefmt.Rule{ |
| 69 | + {Alert: "MyServiceHighErrorRate", For: prommodel.Duration(5 * time.Minute), Labels: map[string]string{conventions.PromSLOSeverityLabelName: "page"}}, |
| 70 | + {Alert: "MyServiceHighErrorRate", For: prommodel.Duration(10 * time.Minute), Labels: map[string]string{conventions.PromSLOSeverityLabelName: "ticket"}}, |
| 71 | + }, |
| 72 | + }, |
| 73 | + }, |
| 74 | + }, |
| 75 | + }, |
| 76 | + |
| 77 | + "Using the plugin as compiled Go plugin, it should set page and ticket `for` durations.": { |
| 78 | + pluginFactory: func(t *testing.T) (pluginslov1.Plugin, error) { |
| 79 | + return plugin.NewPlugin(nil, pluginslov1.AppUtils{}) |
| 80 | + }, |
| 81 | + req: pluginslov1.Request{ |
| 82 | + SLO: model.PromSLO{Name: "requests-availability"}, |
| 83 | + MWMBAlertGroup: model.MWMBAlertGroup{ |
| 84 | + PageQuick: model.MWMBAlert{Severity: model.PageAlertSeverity}, |
| 85 | + TicketQuick: model.MWMBAlert{Severity: model.TicketAlertSeverity}, |
| 86 | + }, |
| 87 | + OriginalSource: model.PromSLOGroupSource{ |
| 88 | + SlothV1: &prometheusv1.Spec{ |
| 89 | + Version: prometheusv1.Version, |
| 90 | + Service: "myservice", |
| 91 | + SLOs: []prometheusv1.SLO{ |
| 92 | + { |
| 93 | + Name: "requests-availability", |
| 94 | + Objective: 99.9, |
| 95 | + SLI: prometheusv1.SLI{Raw: &prometheusv1.SLIRaw{ErrorRatioQuery: "1"}}, |
| 96 | + Alerting: prometheusv1.Alerting{ |
| 97 | + Name: "MyServiceHighErrorRate", |
| 98 | + PageAlert: prometheusv1.Alert{For: prommodel.Duration(5 * time.Minute)}, |
| 99 | + TicketAlert: prometheusv1.Alert{For: prommodel.Duration(10 * time.Minute)}, |
| 100 | + }, |
| 101 | + }, |
| 102 | + }, |
| 103 | + }, |
| 104 | + }, |
| 105 | + }, |
| 106 | + res: pluginslov1.Result{ |
| 107 | + SLORules: model.PromSLORules{ |
| 108 | + AlertRules: model.PromRuleGroup{ |
| 109 | + Rules: []rulefmt.Rule{ |
| 110 | + {Alert: "MyServiceHighErrorRate", Labels: map[string]string{conventions.PromSLOSeverityLabelName: "page"}}, |
| 111 | + {Alert: "MyServiceHighErrorRate", Labels: map[string]string{conventions.PromSLOSeverityLabelName: "ticket"}}, |
| 112 | + }, |
| 113 | + }, |
| 114 | + }, |
| 115 | + }, |
| 116 | + expRes: pluginslov1.Result{ |
| 117 | + SLORules: model.PromSLORules{ |
| 118 | + AlertRules: model.PromRuleGroup{ |
| 119 | + Rules: []rulefmt.Rule{ |
| 120 | + {Alert: "MyServiceHighErrorRate", For: prommodel.Duration(5 * time.Minute), Labels: map[string]string{conventions.PromSLOSeverityLabelName: "page"}}, |
| 121 | + {Alert: "MyServiceHighErrorRate", For: prommodel.Duration(10 * time.Minute), Labels: map[string]string{conventions.PromSLOSeverityLabelName: "ticket"}}, |
| 122 | + }, |
| 123 | + }, |
| 124 | + }, |
| 125 | + }, |
| 126 | + }, |
| 127 | + } |
| 128 | + |
| 129 | + for name, test := range tests { |
| 130 | + t.Run(name, func(t *testing.T) { |
| 131 | + assert := assert.New(t) |
| 132 | + |
| 133 | + p, err := test.pluginFactory(t) |
| 134 | + assert.NoError(err) |
| 135 | + |
| 136 | + res := test.res |
| 137 | + err = p.ProcessSLO(t.Context(), &test.req, &res) |
| 138 | + if assert.NoError(err) { |
| 139 | + assert.Equal(test.expRes, res) |
| 140 | + } |
| 141 | + }) |
| 142 | + } |
| 143 | +} |
0 commit comments