mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
CloudWatch: Add tests to annotations query (#45337)
This commit is contained in:
parent
f1c4da095b
commit
1c1a45a880
106
pkg/tsdb/cloudwatch/annotation_query_test.go
Normal file
106
pkg/tsdb/cloudwatch/annotation_query_test.go
Normal file
@ -0,0 +1,106 @@
|
|||||||
|
package cloudwatch
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/aws/aws-sdk-go/aws"
|
||||||
|
"github.com/aws/aws-sdk-go/aws/session"
|
||||||
|
"github.com/aws/aws-sdk-go/service/cloudwatch"
|
||||||
|
"github.com/aws/aws-sdk-go/service/cloudwatch/cloudwatchiface"
|
||||||
|
"github.com/grafana/grafana-plugin-sdk-go/backend"
|
||||||
|
"github.com/grafana/grafana-plugin-sdk-go/backend/datasource"
|
||||||
|
"github.com/grafana/grafana-plugin-sdk-go/backend/instancemgmt"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestQuery_AnnotationQuery(t *testing.T) {
|
||||||
|
origNewCWClient := NewCWClient
|
||||||
|
t.Cleanup(func() {
|
||||||
|
NewCWClient = origNewCWClient
|
||||||
|
})
|
||||||
|
|
||||||
|
var client FakeCWAnnotationsClient
|
||||||
|
NewCWClient = func(sess *session.Session) cloudwatchiface.CloudWatchAPI {
|
||||||
|
return &client
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Run("DescribeAlarmsForMetric is called with minimum parameters", func(t *testing.T) {
|
||||||
|
client = FakeCWAnnotationsClient{describeAlarmsForMetricOutput: &cloudwatch.DescribeAlarmsForMetricOutput{}}
|
||||||
|
im := datasource.NewInstanceManager(func(s backend.DataSourceInstanceSettings) (instancemgmt.Instance, error) {
|
||||||
|
return datasourceInfo{}, nil
|
||||||
|
})
|
||||||
|
|
||||||
|
executor := newExecutor(im, newTestConfig(), fakeSessionCache{})
|
||||||
|
_, err := executor.QueryData(context.Background(), &backend.QueryDataRequest{
|
||||||
|
PluginContext: backend.PluginContext{
|
||||||
|
DataSourceInstanceSettings: &backend.DataSourceInstanceSettings{},
|
||||||
|
},
|
||||||
|
Queries: []backend.DataQuery{
|
||||||
|
{
|
||||||
|
JSON: json.RawMessage(`{
|
||||||
|
"type": "annotationQuery",
|
||||||
|
"region": "us-east-1",
|
||||||
|
"namespace": "custom",
|
||||||
|
"metricName": "CPUUtilization",
|
||||||
|
"statistic": "Average"
|
||||||
|
}`),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
require.Len(t, client.calls.describeAlarmsForMetric, 1)
|
||||||
|
assert.Equal(t, &cloudwatch.DescribeAlarmsForMetricInput{
|
||||||
|
Namespace: aws.String("custom"),
|
||||||
|
MetricName: aws.String("CPUUtilization"),
|
||||||
|
Statistic: aws.String("Average"),
|
||||||
|
Period: aws.Int64(300),
|
||||||
|
}, client.calls.describeAlarmsForMetric[0])
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("DescribeAlarms is called when prefixMatching is true", func(t *testing.T) {
|
||||||
|
client = FakeCWAnnotationsClient{describeAlarmsOutput: &cloudwatch.DescribeAlarmsOutput{}}
|
||||||
|
im := datasource.NewInstanceManager(func(s backend.DataSourceInstanceSettings) (instancemgmt.Instance, error) {
|
||||||
|
return datasourceInfo{}, nil
|
||||||
|
})
|
||||||
|
|
||||||
|
executor := newExecutor(im, newTestConfig(), fakeSessionCache{})
|
||||||
|
_, err := executor.QueryData(context.Background(), &backend.QueryDataRequest{
|
||||||
|
PluginContext: backend.PluginContext{
|
||||||
|
DataSourceInstanceSettings: &backend.DataSourceInstanceSettings{},
|
||||||
|
},
|
||||||
|
Queries: []backend.DataQuery{
|
||||||
|
{
|
||||||
|
JSON: json.RawMessage(`{
|
||||||
|
"type": "annotationQuery",
|
||||||
|
"region": "us-east-1",
|
||||||
|
"namespace": "custom",
|
||||||
|
"metricName": "CPUUtilization",
|
||||||
|
"statistic": "Average",
|
||||||
|
"prefixMatching": true,
|
||||||
|
"actionPrefix": "some_action_prefix",
|
||||||
|
"alarmNamePrefix": "some_alarm_name_prefix"
|
||||||
|
}`),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
require.Len(t, client.calls.describeAlarms, 1)
|
||||||
|
assert.Equal(t, &cloudwatch.DescribeAlarmsInput{
|
||||||
|
MaxRecords: pointerInt64(100),
|
||||||
|
ActionPrefix: pointerString("some_action_prefix"),
|
||||||
|
AlarmNamePrefix: pointerString("some_alarm_name_prefix"),
|
||||||
|
}, client.calls.describeAlarms[0])
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func pointerString(s string) *string {
|
||||||
|
return &s
|
||||||
|
}
|
||||||
|
func pointerInt64(i int64) *int64 {
|
||||||
|
return &i
|
||||||
|
}
|
@ -79,6 +79,31 @@ func (c FakeCWClient) ListMetricsPages(input *cloudwatch.ListMetricsInput, fn fu
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type FakeCWAnnotationsClient struct {
|
||||||
|
cloudwatchiface.CloudWatchAPI
|
||||||
|
calls annontationsQueryCalls
|
||||||
|
|
||||||
|
describeAlarmsForMetricOutput *cloudwatch.DescribeAlarmsForMetricOutput
|
||||||
|
describeAlarmsOutput *cloudwatch.DescribeAlarmsOutput
|
||||||
|
}
|
||||||
|
|
||||||
|
type annontationsQueryCalls struct {
|
||||||
|
describeAlarmsForMetric []*cloudwatch.DescribeAlarmsForMetricInput
|
||||||
|
describeAlarms []*cloudwatch.DescribeAlarmsInput
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *FakeCWAnnotationsClient) DescribeAlarmsForMetric(params *cloudwatch.DescribeAlarmsForMetricInput) (*cloudwatch.DescribeAlarmsForMetricOutput, error) {
|
||||||
|
c.calls.describeAlarmsForMetric = append(c.calls.describeAlarmsForMetric, params)
|
||||||
|
|
||||||
|
return c.describeAlarmsForMetricOutput, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *FakeCWAnnotationsClient) DescribeAlarms(params *cloudwatch.DescribeAlarmsInput) (*cloudwatch.DescribeAlarmsOutput, error) {
|
||||||
|
c.calls.describeAlarms = append(c.calls.describeAlarms, params)
|
||||||
|
|
||||||
|
return c.describeAlarmsOutput, nil
|
||||||
|
}
|
||||||
|
|
||||||
type fakeEC2Client struct {
|
type fakeEC2Client struct {
|
||||||
ec2iface.EC2API
|
ec2iface.EC2API
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user