mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
CloudWatch: Add test to executeStartQuery (#45888)
* CloudWatch: Add test to executeStartQuery * Add test for absence of limit * Restrict assertions to limit in some tests
This commit is contained in:
parent
f0f43e4635
commit
a68a570e92
@ -97,10 +97,3 @@ func TestQuery_AnnotationQuery(t *testing.T) {
|
|||||||
}, client.calls.describeAlarms[0])
|
}, client.calls.describeAlarms[0])
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func pointerString(s string) *string {
|
|
||||||
return &s
|
|
||||||
}
|
|
||||||
func pointerInt64(i int64) *int64 {
|
|
||||||
return &i
|
|
||||||
}
|
|
||||||
|
@ -28,7 +28,7 @@ func TestQuery_DescribeLogGroups(t *testing.T) {
|
|||||||
var cli FakeCWLogsClient
|
var cli FakeCWLogsClient
|
||||||
|
|
||||||
NewCWLogsClient = func(sess *session.Session) cloudwatchlogsiface.CloudWatchLogsAPI {
|
NewCWLogsClient = func(sess *session.Session) cloudwatchlogsiface.CloudWatchLogsAPI {
|
||||||
return cli
|
return &cli
|
||||||
}
|
}
|
||||||
|
|
||||||
t.Run("Empty log group name prefix", func(t *testing.T) {
|
t.Run("Empty log group name prefix", func(t *testing.T) {
|
||||||
@ -155,7 +155,7 @@ func TestQuery_GetLogGroupFields(t *testing.T) {
|
|||||||
var cli FakeCWLogsClient
|
var cli FakeCWLogsClient
|
||||||
|
|
||||||
NewCWLogsClient = func(sess *session.Session) cloudwatchlogsiface.CloudWatchLogsAPI {
|
NewCWLogsClient = func(sess *session.Session) cloudwatchlogsiface.CloudWatchLogsAPI {
|
||||||
return cli
|
return &cli
|
||||||
}
|
}
|
||||||
|
|
||||||
cli = FakeCWLogsClient{
|
cli = FakeCWLogsClient{
|
||||||
@ -232,7 +232,7 @@ func TestQuery_StartQuery(t *testing.T) {
|
|||||||
var cli FakeCWLogsClient
|
var cli FakeCWLogsClient
|
||||||
|
|
||||||
NewCWLogsClient = func(sess *session.Session) cloudwatchlogsiface.CloudWatchLogsAPI {
|
NewCWLogsClient = func(sess *session.Session) cloudwatchlogsiface.CloudWatchLogsAPI {
|
||||||
return cli
|
return &cli
|
||||||
}
|
}
|
||||||
|
|
||||||
t.Run("invalid time range", func(t *testing.T) {
|
t.Run("invalid time range", func(t *testing.T) {
|
||||||
@ -357,6 +357,108 @@ func TestQuery_StartQuery(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Test_executeStartQuery(t *testing.T) {
|
||||||
|
origNewCWLogsClient := NewCWLogsClient
|
||||||
|
t.Cleanup(func() {
|
||||||
|
NewCWLogsClient = origNewCWLogsClient
|
||||||
|
})
|
||||||
|
|
||||||
|
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{}
|
||||||
|
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{
|
||||||
|
{
|
||||||
|
RefID: "A",
|
||||||
|
TimeRange: backend.TimeRange{From: time.Unix(0, 0), To: time.Unix(1, 0)},
|
||||||
|
JSON: json.RawMessage(`{
|
||||||
|
"type": "logAction",
|
||||||
|
"subtype": "StartQuery",
|
||||||
|
"limit": 12,
|
||||||
|
"queryString":"fields @message",
|
||||||
|
"logGroupNames":["some name","another name"]
|
||||||
|
}`),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
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")},
|
||||||
|
},
|
||||||
|
}, cli.calls.startQueryWithContext)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("cannot parse limit as float", func(t *testing.T) {
|
||||||
|
cli = FakeCWLogsClient{}
|
||||||
|
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{
|
||||||
|
{
|
||||||
|
RefID: "A",
|
||||||
|
TimeRange: backend.TimeRange{From: time.Unix(0, 0), To: time.Unix(1, 0)},
|
||||||
|
JSON: json.RawMessage(`{
|
||||||
|
"type": "logAction",
|
||||||
|
"subtype": "StartQuery",
|
||||||
|
"limit": 12.0
|
||||||
|
}`),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
assert.NoError(t, err)
|
||||||
|
require.Len(t, cli.calls.startQueryWithContext, 1)
|
||||||
|
assert.Nil(t, cli.calls.startQueryWithContext[0].Limit)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("does not populate StartQueryInput.limit when no limit provided", func(t *testing.T) {
|
||||||
|
cli = FakeCWLogsClient{}
|
||||||
|
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{
|
||||||
|
{
|
||||||
|
RefID: "A",
|
||||||
|
TimeRange: backend.TimeRange{From: time.Unix(0, 0), To: time.Unix(1, 0)},
|
||||||
|
JSON: json.RawMessage(`{
|
||||||
|
"type": "logAction",
|
||||||
|
"subtype": "StartQuery"
|
||||||
|
}`),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
assert.NoError(t, err)
|
||||||
|
require.Len(t, cli.calls.startQueryWithContext, 1)
|
||||||
|
assert.Nil(t, cli.calls.startQueryWithContext[0].Limit)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func TestQuery_StopQuery(t *testing.T) {
|
func TestQuery_StopQuery(t *testing.T) {
|
||||||
origNewCWLogsClient := NewCWLogsClient
|
origNewCWLogsClient := NewCWLogsClient
|
||||||
t.Cleanup(func() {
|
t.Cleanup(func() {
|
||||||
@ -366,7 +468,7 @@ func TestQuery_StopQuery(t *testing.T) {
|
|||||||
var cli FakeCWLogsClient
|
var cli FakeCWLogsClient
|
||||||
|
|
||||||
NewCWLogsClient = func(sess *session.Session) cloudwatchlogsiface.CloudWatchLogsAPI {
|
NewCWLogsClient = func(sess *session.Session) cloudwatchlogsiface.CloudWatchLogsAPI {
|
||||||
return cli
|
return &cli
|
||||||
}
|
}
|
||||||
|
|
||||||
cli = FakeCWLogsClient{
|
cli = FakeCWLogsClient{
|
||||||
@ -438,7 +540,7 @@ func TestQuery_GetQueryResults(t *testing.T) {
|
|||||||
var cli FakeCWLogsClient
|
var cli FakeCWLogsClient
|
||||||
|
|
||||||
NewCWLogsClient = func(sess *session.Session) cloudwatchlogsiface.CloudWatchLogsAPI {
|
NewCWLogsClient = func(sess *session.Session) cloudwatchlogsiface.CloudWatchLogsAPI {
|
||||||
return cli
|
return &cli
|
||||||
}
|
}
|
||||||
|
|
||||||
const refID = "A"
|
const refID = "A"
|
||||||
|
@ -20,32 +20,41 @@ import (
|
|||||||
|
|
||||||
type FakeCWLogsClient struct {
|
type FakeCWLogsClient struct {
|
||||||
cloudwatchlogsiface.CloudWatchLogsAPI
|
cloudwatchlogsiface.CloudWatchLogsAPI
|
||||||
|
|
||||||
|
calls logsQueryCalls
|
||||||
|
|
||||||
logGroups cloudwatchlogs.DescribeLogGroupsOutput
|
logGroups cloudwatchlogs.DescribeLogGroupsOutput
|
||||||
logGroupFields cloudwatchlogs.GetLogGroupFieldsOutput
|
logGroupFields cloudwatchlogs.GetLogGroupFieldsOutput
|
||||||
queryResults cloudwatchlogs.GetQueryResultsOutput
|
queryResults cloudwatchlogs.GetQueryResultsOutput
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m FakeCWLogsClient) GetQueryResultsWithContext(ctx context.Context, input *cloudwatchlogs.GetQueryResultsInput, option ...request.Option) (*cloudwatchlogs.GetQueryResultsOutput, error) {
|
type logsQueryCalls struct {
|
||||||
|
startQueryWithContext []*cloudwatchlogs.StartQueryInput
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *FakeCWLogsClient) GetQueryResultsWithContext(ctx context.Context, input *cloudwatchlogs.GetQueryResultsInput, option ...request.Option) (*cloudwatchlogs.GetQueryResultsOutput, error) {
|
||||||
return &m.queryResults, nil
|
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{
|
return &cloudwatchlogs.StartQueryOutput{
|
||||||
QueryId: aws.String("abcd-efgh-ijkl-mnop"),
|
QueryId: aws.String("abcd-efgh-ijkl-mnop"),
|
||||||
}, nil
|
}, 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{
|
return &cloudwatchlogs.StopQueryOutput{
|
||||||
Success: aws.Bool(true),
|
Success: aws.Bool(true),
|
||||||
}, nil
|
}, 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
|
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
|
return &m.logGroupFields, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -192,3 +201,10 @@ func (s fakeSessionCache) GetSession(c awsds.SessionConfig) (*session.Session, e
|
|||||||
Config: &aws.Config{},
|
Config: &aws.Config{},
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func pointerString(s string) *string {
|
||||||
|
return &s
|
||||||
|
}
|
||||||
|
func pointerInt64(i int64) *int64 {
|
||||||
|
return &i
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user