cloudwatch: handle invalid time ranges

This commit is contained in:
Marcus Efraimsson 2018-11-21 17:51:02 +01:00
parent 879aed7d06
commit 3534762f49
No known key found for this signature in database
GPG Key ID: EBFE0FB04612DD4A
2 changed files with 24 additions and 2 deletions

View File

@ -211,8 +211,8 @@ func (e *CloudWatchExecutor) executeQuery(ctx context.Context, query *CloudWatch
return nil, err return nil, err
} }
if endTime.Before(startTime) { if !startTime.Before(endTime) {
return nil, fmt.Errorf("Invalid time range: End time can't be before start time") return nil, fmt.Errorf("Invalid time range: Start time must be before end time")
} }
params := &cloudwatch.GetMetricStatisticsInput{ params := &cloudwatch.GetMetricStatisticsInput{

View File

@ -1,9 +1,13 @@
package cloudwatch package cloudwatch
import ( import (
"context"
"testing" "testing"
"time" "time"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/tsdb"
"github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/cloudwatch" "github.com/aws/aws-sdk-go/service/cloudwatch"
"github.com/grafana/grafana/pkg/components/null" "github.com/grafana/grafana/pkg/components/null"
@ -14,6 +18,24 @@ import (
func TestCloudWatch(t *testing.T) { func TestCloudWatch(t *testing.T) {
Convey("CloudWatch", t, func() { Convey("CloudWatch", t, func() {
Convey("executeQuery", func() {
e := &CloudWatchExecutor{
DataSource: &models.DataSource{
JsonData: simplejson.New(),
},
}
Convey("End time before start time should result in error", func() {
_, err := e.executeQuery(context.Background(), &CloudWatchQuery{}, &tsdb.TsdbQuery{TimeRange: tsdb.NewTimeRange("now-1h", "now-2h")})
So(err.Error(), ShouldEqual, "Invalid time range: Start time must be before end time")
})
Convey("End time equals start time should result in error", func() {
_, err := e.executeQuery(context.Background(), &CloudWatchQuery{}, &tsdb.TsdbQuery{TimeRange: tsdb.NewTimeRange("now-1h", "now-1h")})
So(err.Error(), ShouldEqual, "Invalid time range: Start time must be before end time")
})
})
Convey("can parse cloudwatch json model", func() { Convey("can parse cloudwatch json model", func() {
json := ` json := `
{ {