mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Unexport mocks, use aws-sdk-go helpers, rename test utilities file (#46107)
This commit is contained in:
@@ -22,13 +22,13 @@ func TestQuery_AnnotationQuery(t *testing.T) {
|
||||
NewCWClient = origNewCWClient
|
||||
})
|
||||
|
||||
var client FakeCWAnnotationsClient
|
||||
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{}}
|
||||
client = fakeCWAnnotationsClient{describeAlarmsForMetricOutput: &cloudwatch.DescribeAlarmsForMetricOutput{}}
|
||||
im := datasource.NewInstanceManager(func(s backend.DataSourceInstanceSettings) (instancemgmt.Instance, error) {
|
||||
return datasourceInfo{}, nil
|
||||
})
|
||||
@@ -62,7 +62,7 @@ func TestQuery_AnnotationQuery(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("DescribeAlarms is called when prefixMatching is true", func(t *testing.T) {
|
||||
client = FakeCWAnnotationsClient{describeAlarmsOutput: &cloudwatch.DescribeAlarmsOutput{}}
|
||||
client = fakeCWAnnotationsClient{describeAlarmsOutput: &cloudwatch.DescribeAlarmsOutput{}}
|
||||
im := datasource.NewInstanceManager(func(s backend.DataSourceInstanceSettings) (instancemgmt.Instance, error) {
|
||||
return datasourceInfo{}, nil
|
||||
})
|
||||
@@ -91,9 +91,9 @@ func TestQuery_AnnotationQuery(t *testing.T) {
|
||||
|
||||
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"),
|
||||
MaxRecords: aws.Int64(100),
|
||||
ActionPrefix: aws.String("some_action_prefix"),
|
||||
AlarmNamePrefix: aws.String("some_alarm_name_prefix"),
|
||||
}, client.calls.describeAlarms[0])
|
||||
})
|
||||
}
|
||||
|
||||
@@ -25,14 +25,14 @@ func TestQuery_DescribeLogGroups(t *testing.T) {
|
||||
NewCWLogsClient = origNewCWLogsClient
|
||||
})
|
||||
|
||||
var cli FakeCWLogsClient
|
||||
var cli fakeCWLogsClient
|
||||
|
||||
NewCWLogsClient = func(sess *session.Session) cloudwatchlogsiface.CloudWatchLogsAPI {
|
||||
return &cli
|
||||
}
|
||||
|
||||
t.Run("Empty log group name prefix", func(t *testing.T) {
|
||||
cli = FakeCWLogsClient{
|
||||
cli = fakeCWLogsClient{
|
||||
logGroups: cloudwatchlogs.DescribeLogGroupsOutput{
|
||||
LogGroups: []*cloudwatchlogs.LogGroup{
|
||||
{
|
||||
@@ -88,7 +88,7 @@ func TestQuery_DescribeLogGroups(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("Non-empty log group name prefix", func(t *testing.T) {
|
||||
cli = FakeCWLogsClient{
|
||||
cli = fakeCWLogsClient{
|
||||
logGroups: cloudwatchlogs.DescribeLogGroupsOutput{
|
||||
LogGroups: []*cloudwatchlogs.LogGroup{
|
||||
{
|
||||
@@ -152,13 +152,13 @@ func TestQuery_GetLogGroupFields(t *testing.T) {
|
||||
NewCWLogsClient = origNewCWLogsClient
|
||||
})
|
||||
|
||||
var cli FakeCWLogsClient
|
||||
var cli fakeCWLogsClient
|
||||
|
||||
NewCWLogsClient = func(sess *session.Session) cloudwatchlogsiface.CloudWatchLogsAPI {
|
||||
return &cli
|
||||
}
|
||||
|
||||
cli = FakeCWLogsClient{
|
||||
cli = fakeCWLogsClient{
|
||||
logGroupFields: cloudwatchlogs.GetLogGroupFieldsOutput{
|
||||
LogGroupFields: []*cloudwatchlogs.LogGroupField{
|
||||
{
|
||||
@@ -229,14 +229,14 @@ func TestQuery_StartQuery(t *testing.T) {
|
||||
NewCWLogsClient = origNewCWLogsClient
|
||||
})
|
||||
|
||||
var cli FakeCWLogsClient
|
||||
var cli fakeCWLogsClient
|
||||
|
||||
NewCWLogsClient = func(sess *session.Session) cloudwatchlogsiface.CloudWatchLogsAPI {
|
||||
return &cli
|
||||
}
|
||||
|
||||
t.Run("invalid time range", func(t *testing.T) {
|
||||
cli = FakeCWLogsClient{
|
||||
cli = fakeCWLogsClient{
|
||||
logGroupFields: cloudwatchlogs.GetLogGroupFieldsOutput{
|
||||
LogGroupFields: []*cloudwatchlogs.LogGroupField{
|
||||
{
|
||||
@@ -289,7 +289,7 @@ func TestQuery_StartQuery(t *testing.T) {
|
||||
|
||||
t.Run("valid time range", func(t *testing.T) {
|
||||
const refID = "A"
|
||||
cli = FakeCWLogsClient{
|
||||
cli = fakeCWLogsClient{
|
||||
logGroupFields: cloudwatchlogs.GetLogGroupFieldsOutput{
|
||||
LogGroupFields: []*cloudwatchlogs.LogGroupField{
|
||||
{
|
||||
@@ -363,14 +363,14 @@ func Test_executeStartQuery(t *testing.T) {
|
||||
NewCWLogsClient = origNewCWLogsClient
|
||||
})
|
||||
|
||||
var cli FakeCWLogsClient
|
||||
var cli fakeCWLogsClient
|
||||
|
||||
NewCWLogsClient = func(sess *session.Session) cloudwatchlogsiface.CloudWatchLogsAPI {
|
||||
return &cli
|
||||
}
|
||||
|
||||
t.Run("successfully parses information from JSON to StartQueryWithContext", func(t *testing.T) {
|
||||
cli = FakeCWLogsClient{}
|
||||
cli = fakeCWLogsClient{}
|
||||
im := datasource.NewInstanceManager(func(s backend.DataSourceInstanceSettings) (instancemgmt.Instance, error) {
|
||||
return datasourceInfo{}, nil
|
||||
})
|
||||
@@ -396,17 +396,17 @@ func Test_executeStartQuery(t *testing.T) {
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, []*cloudwatchlogs.StartQueryInput{
|
||||
{
|
||||
StartTime: pointerInt64(0),
|
||||
EndTime: pointerInt64(1),
|
||||
Limit: pointerInt64(12),
|
||||
QueryString: pointerString("fields @timestamp,ltrim(@log) as __log__grafana_internal__,ltrim(@logStream) as __logstream__grafana_internal__|fields @message"),
|
||||
LogGroupNames: []*string{pointerString("some name"), pointerString("another name")},
|
||||
StartTime: aws.Int64(0),
|
||||
EndTime: aws.Int64(1),
|
||||
Limit: aws.Int64(12),
|
||||
QueryString: aws.String("fields @timestamp,ltrim(@log) as __log__grafana_internal__,ltrim(@logStream) as __logstream__grafana_internal__|fields @message"),
|
||||
LogGroupNames: []*string{aws.String("some name"), aws.String("another name")},
|
||||
},
|
||||
}, cli.calls.startQueryWithContext)
|
||||
})
|
||||
|
||||
t.Run("cannot parse limit as float", func(t *testing.T) {
|
||||
cli = FakeCWLogsClient{}
|
||||
cli = fakeCWLogsClient{}
|
||||
im := datasource.NewInstanceManager(func(s backend.DataSourceInstanceSettings) (instancemgmt.Instance, error) {
|
||||
return datasourceInfo{}, nil
|
||||
})
|
||||
@@ -433,7 +433,7 @@ func Test_executeStartQuery(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("does not populate StartQueryInput.limit when no limit provided", func(t *testing.T) {
|
||||
cli = FakeCWLogsClient{}
|
||||
cli = fakeCWLogsClient{}
|
||||
im := datasource.NewInstanceManager(func(s backend.DataSourceInstanceSettings) (instancemgmt.Instance, error) {
|
||||
return datasourceInfo{}, nil
|
||||
})
|
||||
@@ -465,13 +465,13 @@ func TestQuery_StopQuery(t *testing.T) {
|
||||
NewCWLogsClient = origNewCWLogsClient
|
||||
})
|
||||
|
||||
var cli FakeCWLogsClient
|
||||
var cli fakeCWLogsClient
|
||||
|
||||
NewCWLogsClient = func(sess *session.Session) cloudwatchlogsiface.CloudWatchLogsAPI {
|
||||
return &cli
|
||||
}
|
||||
|
||||
cli = FakeCWLogsClient{
|
||||
cli = fakeCWLogsClient{
|
||||
logGroupFields: cloudwatchlogs.GetLogGroupFieldsOutput{
|
||||
LogGroupFields: []*cloudwatchlogs.LogGroupField{
|
||||
{
|
||||
@@ -537,14 +537,14 @@ func TestQuery_GetQueryResults(t *testing.T) {
|
||||
NewCWLogsClient = origNewCWLogsClient
|
||||
})
|
||||
|
||||
var cli FakeCWLogsClient
|
||||
var cli fakeCWLogsClient
|
||||
|
||||
NewCWLogsClient = func(sess *session.Session) cloudwatchlogsiface.CloudWatchLogsAPI {
|
||||
return &cli
|
||||
}
|
||||
|
||||
const refID = "A"
|
||||
cli = FakeCWLogsClient{
|
||||
cli = fakeCWLogsClient{
|
||||
queryResults: cloudwatchlogs.GetQueryResultsOutput{
|
||||
Results: [][]*cloudwatchlogs.ResultField{
|
||||
{
|
||||
|
||||
@@ -29,14 +29,14 @@ func TestQuery_Metrics(t *testing.T) {
|
||||
NewCWClient = origNewCWClient
|
||||
})
|
||||
|
||||
var cwClient FakeCWClient
|
||||
var cwClient fakeCWClient
|
||||
|
||||
NewCWClient = func(sess *session.Session) cloudwatchiface.CloudWatchAPI {
|
||||
return cwClient
|
||||
}
|
||||
|
||||
t.Run("Custom metrics", func(t *testing.T) {
|
||||
cwClient = FakeCWClient{
|
||||
cwClient = fakeCWClient{
|
||||
Metrics: []*cloudwatch.Metric{
|
||||
{
|
||||
MetricName: aws.String("Test_MetricName"),
|
||||
@@ -71,7 +71,7 @@ func TestQuery_Metrics(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("Dimension keys for custom metrics", func(t *testing.T) {
|
||||
cwClient = FakeCWClient{
|
||||
cwClient = fakeCWClient{
|
||||
Metrics: []*cloudwatch.Metric{
|
||||
{
|
||||
MetricName: aws.String("Test_MetricName"),
|
||||
@@ -400,7 +400,7 @@ func TestQuery_GetDimensionKeys(t *testing.T) {
|
||||
NewCWClient = origNewCWClient
|
||||
})
|
||||
|
||||
var client FakeCWClient
|
||||
var client fakeCWClient
|
||||
|
||||
NewCWClient = func(sess *session.Session) cloudwatchiface.CloudWatchAPI {
|
||||
return client
|
||||
@@ -418,7 +418,7 @@ func TestQuery_GetDimensionKeys(t *testing.T) {
|
||||
}
|
||||
|
||||
t.Run("should fetch dimension keys from list metrics api and return unique dimensions when a dimension filter is specified", func(t *testing.T) {
|
||||
client = FakeCWClient{Metrics: metrics, MetricsPerPage: 2}
|
||||
client = fakeCWClient{Metrics: metrics, MetricsPerPage: 2}
|
||||
im := datasource.NewInstanceManager(func(s backend.DataSourceInstanceSettings) (instancemgmt.Instance, error) {
|
||||
return datasourceInfo{}, nil
|
||||
})
|
||||
@@ -524,7 +524,7 @@ func TestQuery_ListMetricsPagination(t *testing.T) {
|
||||
NewCWClient = origNewCWClient
|
||||
})
|
||||
|
||||
var client FakeCWClient
|
||||
var client fakeCWClient
|
||||
|
||||
NewCWClient = func(sess *session.Session) cloudwatchiface.CloudWatchAPI {
|
||||
return client
|
||||
@@ -544,7 +544,7 @@ func TestQuery_ListMetricsPagination(t *testing.T) {
|
||||
}
|
||||
|
||||
t.Run("List Metrics and page limit is reached", func(t *testing.T) {
|
||||
client = FakeCWClient{Metrics: metrics, MetricsPerPage: 2}
|
||||
client = fakeCWClient{Metrics: metrics, MetricsPerPage: 2}
|
||||
im := datasource.NewInstanceManager(func(s backend.DataSourceInstanceSettings) (instancemgmt.Instance, error) {
|
||||
return datasourceInfo{}, nil
|
||||
})
|
||||
@@ -559,7 +559,7 @@ func TestQuery_ListMetricsPagination(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("List Metrics and page limit is not reached", func(t *testing.T) {
|
||||
client = FakeCWClient{Metrics: metrics, MetricsPerPage: 2}
|
||||
client = fakeCWClient{Metrics: metrics, MetricsPerPage: 2}
|
||||
im := datasource.NewInstanceManager(func(s backend.DataSourceInstanceSettings) (instancemgmt.Instance, error) {
|
||||
return datasourceInfo{}, nil
|
||||
})
|
||||
|
||||
@@ -27,14 +27,14 @@ func TestTimeSeriesQuery(t *testing.T) {
|
||||
NewCWClient = origNewCWClient
|
||||
})
|
||||
|
||||
var cwClient FakeCWClient
|
||||
var cwClient fakeCWClient
|
||||
|
||||
NewCWClient = func(sess *session.Session) cloudwatchiface.CloudWatchAPI {
|
||||
return cwClient
|
||||
}
|
||||
|
||||
t.Run("Custom metrics", func(t *testing.T) {
|
||||
cwClient = FakeCWClient{
|
||||
cwClient = fakeCWClient{
|
||||
CloudWatchAPI: nil,
|
||||
GetMetricDataOutput: cloudwatch.GetMetricDataOutput{
|
||||
NextToken: nil,
|
||||
|
||||
@@ -18,7 +18,7 @@ import (
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
)
|
||||
|
||||
type FakeCWLogsClient struct {
|
||||
type fakeCWLogsClient struct {
|
||||
cloudwatchlogsiface.CloudWatchLogsAPI
|
||||
|
||||
calls logsQueryCalls
|
||||
@@ -32,11 +32,11 @@ type logsQueryCalls struct {
|
||||
startQueryWithContext []*cloudwatchlogs.StartQueryInput
|
||||
}
|
||||
|
||||
func (m *FakeCWLogsClient) GetQueryResultsWithContext(ctx context.Context, input *cloudwatchlogs.GetQueryResultsInput, option ...request.Option) (*cloudwatchlogs.GetQueryResultsOutput, error) {
|
||||
func (m *fakeCWLogsClient) GetQueryResultsWithContext(ctx context.Context, input *cloudwatchlogs.GetQueryResultsInput, option ...request.Option) (*cloudwatchlogs.GetQueryResultsOutput, error) {
|
||||
return &m.queryResults, nil
|
||||
}
|
||||
|
||||
func (m *FakeCWLogsClient) StartQueryWithContext(ctx context.Context, input *cloudwatchlogs.StartQueryInput, option ...request.Option) (*cloudwatchlogs.StartQueryOutput, error) {
|
||||
func (m *fakeCWLogsClient) StartQueryWithContext(ctx context.Context, input *cloudwatchlogs.StartQueryInput, option ...request.Option) (*cloudwatchlogs.StartQueryOutput, error) {
|
||||
m.calls.startQueryWithContext = append(m.calls.startQueryWithContext, input)
|
||||
|
||||
return &cloudwatchlogs.StartQueryOutput{
|
||||
@@ -44,21 +44,21 @@ func (m *FakeCWLogsClient) StartQueryWithContext(ctx context.Context, input *clo
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (m *FakeCWLogsClient) StopQueryWithContext(ctx context.Context, input *cloudwatchlogs.StopQueryInput, option ...request.Option) (*cloudwatchlogs.StopQueryOutput, error) {
|
||||
func (m *fakeCWLogsClient) StopQueryWithContext(ctx context.Context, input *cloudwatchlogs.StopQueryInput, option ...request.Option) (*cloudwatchlogs.StopQueryOutput, error) {
|
||||
return &cloudwatchlogs.StopQueryOutput{
|
||||
Success: aws.Bool(true),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (m *FakeCWLogsClient) DescribeLogGroupsWithContext(ctx context.Context, input *cloudwatchlogs.DescribeLogGroupsInput, option ...request.Option) (*cloudwatchlogs.DescribeLogGroupsOutput, error) {
|
||||
func (m *fakeCWLogsClient) DescribeLogGroupsWithContext(ctx context.Context, input *cloudwatchlogs.DescribeLogGroupsInput, option ...request.Option) (*cloudwatchlogs.DescribeLogGroupsOutput, error) {
|
||||
return &m.logGroups, nil
|
||||
}
|
||||
|
||||
func (m *FakeCWLogsClient) GetLogGroupFieldsWithContext(ctx context.Context, input *cloudwatchlogs.GetLogGroupFieldsInput, option ...request.Option) (*cloudwatchlogs.GetLogGroupFieldsOutput, error) {
|
||||
func (m *fakeCWLogsClient) GetLogGroupFieldsWithContext(ctx context.Context, input *cloudwatchlogs.GetLogGroupFieldsInput, option ...request.Option) (*cloudwatchlogs.GetLogGroupFieldsOutput, error) {
|
||||
return &m.logGroupFields, nil
|
||||
}
|
||||
|
||||
type FakeCWClient struct {
|
||||
type fakeCWClient struct {
|
||||
cloudwatchiface.CloudWatchAPI
|
||||
cloudwatch.GetMetricDataOutput
|
||||
|
||||
@@ -67,11 +67,11 @@ type FakeCWClient struct {
|
||||
MetricsPerPage int
|
||||
}
|
||||
|
||||
func (c FakeCWClient) GetMetricDataWithContext(aws.Context, *cloudwatch.GetMetricDataInput, ...request.Option) (*cloudwatch.GetMetricDataOutput, error) {
|
||||
func (c fakeCWClient) GetMetricDataWithContext(aws.Context, *cloudwatch.GetMetricDataInput, ...request.Option) (*cloudwatch.GetMetricDataOutput, error) {
|
||||
return &c.GetMetricDataOutput, nil
|
||||
}
|
||||
|
||||
func (c FakeCWClient) ListMetricsPages(input *cloudwatch.ListMetricsInput, fn func(*cloudwatch.ListMetricsOutput, bool) bool) error {
|
||||
func (c fakeCWClient) ListMetricsPages(input *cloudwatch.ListMetricsInput, fn func(*cloudwatch.ListMetricsOutput, bool) bool) error {
|
||||
if c.MetricsPerPage == 0 {
|
||||
c.MetricsPerPage = 1000
|
||||
}
|
||||
@@ -88,7 +88,7 @@ func (c FakeCWClient) ListMetricsPages(input *cloudwatch.ListMetricsInput, fn fu
|
||||
return nil
|
||||
}
|
||||
|
||||
type FakeCWAnnotationsClient struct {
|
||||
type fakeCWAnnotationsClient struct {
|
||||
cloudwatchiface.CloudWatchAPI
|
||||
calls annontationsQueryCalls
|
||||
|
||||
@@ -101,13 +101,13 @@ type annontationsQueryCalls struct {
|
||||
describeAlarms []*cloudwatch.DescribeAlarmsInput
|
||||
}
|
||||
|
||||
func (c *FakeCWAnnotationsClient) DescribeAlarmsForMetric(params *cloudwatch.DescribeAlarmsForMetricInput) (*cloudwatch.DescribeAlarmsForMetricOutput, error) {
|
||||
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) {
|
||||
func (c *fakeCWAnnotationsClient) DescribeAlarms(params *cloudwatch.DescribeAlarmsInput) (*cloudwatch.DescribeAlarmsOutput, error) {
|
||||
c.calls.describeAlarms = append(c.calls.describeAlarms, params)
|
||||
|
||||
return c.describeAlarmsOutput, nil
|
||||
@@ -228,10 +228,3 @@ func (s fakeSessionCache) GetSession(c awsds.SessionConfig) (*session.Session, e
|
||||
Config: &aws.Config{},
|
||||
}, nil
|
||||
}
|
||||
|
||||
func pointerString(s string) *string {
|
||||
return &s
|
||||
}
|
||||
func pointerInt64(i int64) *int64 {
|
||||
return &i
|
||||
}
|
||||
Reference in New Issue
Block a user