Skip to content

Commit

Permalink
Merge 816d61c into 6225534
Browse files Browse the repository at this point in the history
  • Loading branch information
litleleprikon committed Jun 23, 2020
2 parents 6225534 + 816d61c commit 3a3f344
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 8 deletions.
9 changes: 9 additions & 0 deletions api/dto/triggers.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ import (

var targetNameRegex = regexp.MustCompile("t(\\d+)")

// TODO(litleleprikon): Remove after https://github.com/moira-alert/moira/issues/550 will be resolved
var asteriskPatternRegex = regexp.MustCompile(`^\*$`)

type TriggersList struct {
Page *int64 `json:"page,omitempty"`
Size *int64 `json:"size,omitempty"`
Expand Down Expand Up @@ -165,6 +168,12 @@ func (trigger *Trigger) Bind(request *http.Request) error {
if err != nil {
return err
}
// TODO(litleleprikon): Remove after https://github.com/moira-alert/moira/issues/550 will be resolved
for _, pattern := range trigger.Patterns {
if asteriskPatternRegex.Match([]byte(pattern)) {
return api.ErrInvalidRequestContent{ValidationError: fmt.Errorf("pattern \"*\" is not allowed to use")}
}
}
middleware.SetTimeSeriesNames(request, metricsDataNames)
if _, err := triggerExpression.Evaluate(); err != nil {
return err
Expand Down
59 changes: 51 additions & 8 deletions api/dto/triggers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ import (
. "github.com/smartystreets/goconvey/convey"
)

func TestExpressionModeMultipleTargetsWarnValue(t *testing.T) {

func TestTriggerValidation(t *testing.T) {
Convey("Tests targets, values and expression validation", t, func() {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
Expand All @@ -27,12 +26,6 @@ func TestExpressionModeMultipleTargetsWarnValue(t *testing.T) {
fetchResult := mock_metric_source.NewMockFetchResult(mockCtrl)
sourceProvider := metricSource.CreateMetricSourceProvider(localSource, remoteSource)

localSource.EXPECT().IsConfigured().Return(true, nil).AnyTimes()
localSource.EXPECT().GetMetricsTTLSeconds().Return(int64(3600)).AnyTimes()
localSource.EXPECT().Fetch(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(fetchResult, nil).AnyTimes()
fetchResult.EXPECT().GetPatterns().Return(make([]string, 0), nil).AnyTimes()
fetchResult.EXPECT().GetMetricsData().Return([]metricSource.MetricData{*metricSource.MakeMetricData("", []float64{}, 0, 0)}).AnyTimes()

request, _ := http.NewRequest("PUT", "/api/trigger", nil)
request.Header.Set("Content-Type", "application/json")
ctx := request.Context()
Expand All @@ -57,6 +50,13 @@ func TestExpressionModeMultipleTargetsWarnValue(t *testing.T) {
}

Convey("Test FallingTrigger", func() {

localSource.EXPECT().IsConfigured().Return(true, nil).AnyTimes()
localSource.EXPECT().GetMetricsTTLSeconds().Return(int64(3600)).AnyTimes()
localSource.EXPECT().Fetch(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(fetchResult, nil).AnyTimes()
fetchResult.EXPECT().GetPatterns().Return(make([]string, 0), nil).AnyTimes()
fetchResult.EXPECT().GetMetricsData().Return([]metricSource.MetricData{*metricSource.MakeMetricData("", []float64{}, 0, 0)}).AnyTimes()

trigger.TriggerType = moira.FallingTrigger

Convey("and one target", func() {
Expand Down Expand Up @@ -95,6 +95,13 @@ func TestExpressionModeMultipleTargetsWarnValue(t *testing.T) {

})
Convey("Test RisingTrigger", func() {

localSource.EXPECT().IsConfigured().Return(true, nil).AnyTimes()
localSource.EXPECT().GetMetricsTTLSeconds().Return(int64(3600)).AnyTimes()
localSource.EXPECT().Fetch(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(fetchResult, nil).AnyTimes()
fetchResult.EXPECT().GetPatterns().Return(make([]string, 0), nil).AnyTimes()
fetchResult.EXPECT().GetMetricsData().Return([]metricSource.MetricData{*metricSource.MakeMetricData("", []float64{}, 0, 0)}).AnyTimes()

trigger.TriggerType = moira.RisingTrigger

Convey("and one target", func() {
Expand Down Expand Up @@ -132,6 +139,13 @@ func TestExpressionModeMultipleTargetsWarnValue(t *testing.T) {
})
})
Convey("Test ExpressionTrigger", func() {

localSource.EXPECT().IsConfigured().Return(true, nil).AnyTimes()
localSource.EXPECT().GetMetricsTTLSeconds().Return(int64(3600)).AnyTimes()
localSource.EXPECT().Fetch(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(fetchResult, nil).AnyTimes()
fetchResult.EXPECT().GetPatterns().Return(make([]string, 0), nil).AnyTimes()
fetchResult.EXPECT().GetMetricsData().Return([]metricSource.MetricData{*metricSource.MakeMetricData("", []float64{}, 0, 0)}).AnyTimes()

trigger.TriggerType = moira.ExpressionTrigger
trigger.Expression = "(t1 < 10 && t2 < 10) ? WARN:OK"
trigger.Targets = []string{
Expand Down Expand Up @@ -162,6 +176,12 @@ func TestExpressionModeMultipleTargetsWarnValue(t *testing.T) {
})

Convey("Test alone metrics", func() {
localSource.EXPECT().IsConfigured().Return(true, nil).AnyTimes()
localSource.EXPECT().GetMetricsTTLSeconds().Return(int64(3600)).AnyTimes()
localSource.EXPECT().Fetch(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(fetchResult, nil).AnyTimes()
fetchResult.EXPECT().GetPatterns().Return(make([]string, 0), nil).AnyTimes()
fetchResult.EXPECT().GetMetricsData().Return([]metricSource.MetricData{*metricSource.MakeMetricData("", []float64{}, 0, 0)}).AnyTimes()

trigger.Targets = []string{"test target"}
trigger.Expression = "OK"
Convey("are empty", func() {
Expand Down Expand Up @@ -189,5 +209,28 @@ func TestExpressionModeMultipleTargetsWarnValue(t *testing.T) {
So(err, ShouldResemble, api.ErrInvalidRequestContent{ValidationError: fmt.Errorf("alone metrics target index should be in range from 1 to length of targets")})
})
})

Convey("Test patterns", func() {
localSource.EXPECT().IsConfigured().Return(true, nil).AnyTimes()
localSource.EXPECT().GetMetricsTTLSeconds().Return(int64(3600)).AnyTimes()
localSource.EXPECT().Fetch(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(fetchResult, nil).AnyTimes()
fetchResult.EXPECT().GetMetricsData().Return([]metricSource.MetricData{*metricSource.MakeMetricData("", []float64{}, 0, 0)}).AnyTimes()

trigger.Expression = "OK"
Convey("do not have asterisk", func() {
trigger.Targets = []string{"sumSeries(some.test.series.*)"}
tr := Trigger{trigger, throttling}
fetchResult.EXPECT().GetPatterns().Return([]string{"some.test.series.*"}, nil).AnyTimes()
err := tr.Bind(request)
So(err, ShouldBeNil)
})
Convey("have asterisk", func() {
trigger.Targets = []string{"sumSeries(*)"}
tr := Trigger{trigger, throttling}
fetchResult.EXPECT().GetPatterns().Return([]string{"*"}, nil).AnyTimes()
err := tr.Bind(request)
So(err, ShouldResemble, api.ErrInvalidRequestContent{ValidationError: fmt.Errorf("pattern \"*\" is not allowed to use")})
})
})
})
}

0 comments on commit 3a3f344

Please sign in to comment.