@ -28,7 +28,7 @@ func TestQuery_DescribeLogGroups(t *testing.T) {
var cli FakeCWLogsClient
NewCWLogsClient = func ( sess * session . Session ) cloudwatchlogsiface . CloudWatchLogsAPI {
return cli
return & cli
}
t . Run ( "Empty log group name prefix" , func ( t * testing . T ) {
@ -155,7 +155,7 @@ func TestQuery_GetLogGroupFields(t *testing.T) {
var cli FakeCWLogsClient
NewCWLogsClient = func ( sess * session . Session ) cloudwatchlogsiface . CloudWatchLogsAPI {
return cli
return & cli
}
cli = FakeCWLogsClient {
@ -232,7 +232,7 @@ func TestQuery_StartQuery(t *testing.T) {
var cli FakeCWLogsClient
NewCWLogsClient = func ( sess * session . Session ) cloudwatchlogsiface . CloudWatchLogsAPI {
return cli
return & cli
}
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 ) {
origNewCWLogsClient := NewCWLogsClient
t . Cleanup ( func ( ) {
@ -366,7 +468,7 @@ func TestQuery_StopQuery(t *testing.T) {
var cli FakeCWLogsClient
NewCWLogsClient = func ( sess * session . Session ) cloudwatchlogsiface . CloudWatchLogsAPI {
return cli
return & cli
}
cli = FakeCWLogsClient {
@ -438,7 +540,7 @@ func TestQuery_GetQueryResults(t *testing.T) {
var cli FakeCWLogsClient
NewCWLogsClient = func ( sess * session . Session ) cloudwatchlogsiface . CloudWatchLogsAPI {
return cli
return & cli
}
const refID = "A"