2020-04-25 15:48:20 -05:00
|
|
|
package cloudwatch
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-03-23 10:32:12 -05:00
|
|
|
"encoding/json"
|
2020-04-25 15:48:20 -05:00
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
2020-07-23 11:52:22 -05:00
|
|
|
"github.com/aws/aws-sdk-go/aws/session"
|
2020-04-25 15:48:20 -05:00
|
|
|
"github.com/aws/aws-sdk-go/service/cloudwatchlogs"
|
2020-07-23 11:52:22 -05:00
|
|
|
"github.com/aws/aws-sdk-go/service/cloudwatchlogs/cloudwatchlogsiface"
|
2021-03-23 10:32:12 -05:00
|
|
|
"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"
|
2020-04-25 15:48:20 -05:00
|
|
|
"github.com/grafana/grafana-plugin-sdk-go/data"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2020-05-18 05:25:58 -05:00
|
|
|
"github.com/stretchr/testify/require"
|
2020-04-25 15:48:20 -05:00
|
|
|
)
|
|
|
|
|
2020-07-23 11:52:22 -05:00
|
|
|
func TestQuery_DescribeLogGroups(t *testing.T) {
|
2021-01-07 04:36:13 -06:00
|
|
|
origNewCWLogsClient := NewCWLogsClient
|
2020-07-23 11:52:22 -05:00
|
|
|
t.Cleanup(func() {
|
2021-01-07 04:36:13 -06:00
|
|
|
NewCWLogsClient = origNewCWLogsClient
|
2020-07-23 11:52:22 -05:00
|
|
|
})
|
2020-04-25 15:48:20 -05:00
|
|
|
|
2021-01-07 04:36:13 -06:00
|
|
|
var cli FakeCWLogsClient
|
2020-04-25 15:48:20 -05:00
|
|
|
|
2021-01-07 04:36:13 -06:00
|
|
|
NewCWLogsClient = func(sess *session.Session) cloudwatchlogsiface.CloudWatchLogsAPI {
|
2020-07-23 11:52:22 -05:00
|
|
|
return cli
|
2020-04-25 15:48:20 -05:00
|
|
|
}
|
|
|
|
|
2020-07-23 11:52:22 -05:00
|
|
|
t.Run("Empty log group name prefix", func(t *testing.T) {
|
2021-01-07 04:36:13 -06:00
|
|
|
cli = FakeCWLogsClient{
|
2020-07-23 11:52:22 -05:00
|
|
|
logGroups: cloudwatchlogs.DescribeLogGroupsOutput{
|
|
|
|
LogGroups: []*cloudwatchlogs.LogGroup{
|
|
|
|
{
|
|
|
|
LogGroupName: aws.String("group_a"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
LogGroupName: aws.String("group_b"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
LogGroupName: aws.String("group_c"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-03-23 10:32:12 -05:00
|
|
|
im := datasource.NewInstanceManager(func(s backend.DataSourceInstanceSettings) (instancemgmt.Instance, error) {
|
|
|
|
return datasourceInfo{}, nil
|
|
|
|
})
|
|
|
|
|
|
|
|
executor := newExecutor(nil, im, newTestConfig(), fakeSessionCache{})
|
|
|
|
resp, err := executor.QueryData(context.Background(), &backend.QueryDataRequest{
|
|
|
|
PluginContext: backend.PluginContext{
|
|
|
|
DataSourceInstanceSettings: &backend.DataSourceInstanceSettings{},
|
|
|
|
},
|
|
|
|
Queries: []backend.DataQuery{
|
2020-07-23 11:52:22 -05:00
|
|
|
{
|
2021-03-23 10:32:12 -05:00
|
|
|
JSON: json.RawMessage(`{
|
2020-07-23 11:52:22 -05:00
|
|
|
"type": "logAction",
|
|
|
|
"subtype": "DescribeLogGroups",
|
2021-03-23 10:32:12 -05:00
|
|
|
"limit": 50
|
|
|
|
}`),
|
2020-07-23 11:52:22 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, resp)
|
|
|
|
|
2021-03-23 10:32:12 -05:00
|
|
|
assert.Equal(t, &backend.QueryDataResponse{Responses: backend.Responses{
|
|
|
|
"": backend.DataResponse{
|
|
|
|
Frames: data.Frames{
|
|
|
|
&data.Frame{
|
|
|
|
Name: "logGroups",
|
|
|
|
Fields: []*data.Field{
|
|
|
|
data.NewField("logGroupName", nil, []*string{
|
|
|
|
aws.String("group_a"), aws.String("group_b"), aws.String("group_c"),
|
|
|
|
}),
|
2020-07-23 11:52:22 -05:00
|
|
|
},
|
2021-03-23 10:32:12 -05:00
|
|
|
Meta: &data.FrameMeta{
|
|
|
|
PreferredVisualization: "logs",
|
|
|
|
},
|
|
|
|
},
|
2020-07-23 11:52:22 -05:00
|
|
|
},
|
|
|
|
},
|
2021-03-23 10:32:12 -05:00
|
|
|
},
|
2020-07-23 11:52:22 -05:00
|
|
|
}, resp)
|
2020-04-25 15:48:20 -05:00
|
|
|
})
|
|
|
|
|
2020-07-23 11:52:22 -05:00
|
|
|
t.Run("Non-empty log group name prefix", func(t *testing.T) {
|
2021-01-07 04:36:13 -06:00
|
|
|
cli = FakeCWLogsClient{
|
2020-07-23 11:52:22 -05:00
|
|
|
logGroups: cloudwatchlogs.DescribeLogGroupsOutput{
|
|
|
|
LogGroups: []*cloudwatchlogs.LogGroup{
|
|
|
|
{
|
|
|
|
LogGroupName: aws.String("group_a"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
LogGroupName: aws.String("group_b"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
LogGroupName: aws.String("group_c"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-03-23 10:32:12 -05:00
|
|
|
im := datasource.NewInstanceManager(func(s backend.DataSourceInstanceSettings) (instancemgmt.Instance, error) {
|
|
|
|
return datasourceInfo{}, nil
|
|
|
|
})
|
|
|
|
|
|
|
|
executor := newExecutor(nil, im, newTestConfig(), fakeSessionCache{})
|
|
|
|
resp, err := executor.QueryData(context.Background(), &backend.QueryDataRequest{
|
|
|
|
PluginContext: backend.PluginContext{
|
|
|
|
DataSourceInstanceSettings: &backend.DataSourceInstanceSettings{},
|
|
|
|
},
|
|
|
|
Queries: []backend.DataQuery{
|
2020-07-23 11:52:22 -05:00
|
|
|
{
|
2021-03-23 10:32:12 -05:00
|
|
|
JSON: json.RawMessage(`{
|
|
|
|
"type": "logAction",
|
|
|
|
"subtype": "DescribeLogGroups",
|
|
|
|
"limit": 50,
|
|
|
|
"region": "default",
|
|
|
|
"logGroupNamePrefix": "g"
|
|
|
|
}`),
|
2020-07-23 11:52:22 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, resp)
|
|
|
|
|
2021-03-23 10:32:12 -05:00
|
|
|
assert.Equal(t, &backend.QueryDataResponse{Responses: backend.Responses{
|
|
|
|
"": backend.DataResponse{
|
|
|
|
Frames: data.Frames{
|
|
|
|
&data.Frame{
|
|
|
|
Name: "logGroups",
|
|
|
|
Fields: []*data.Field{
|
|
|
|
data.NewField("logGroupName", nil, []*string{
|
|
|
|
aws.String("group_a"), aws.String("group_b"), aws.String("group_c"),
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
Meta: &data.FrameMeta{
|
|
|
|
PreferredVisualization: "logs",
|
2020-07-23 11:52:22 -05:00
|
|
|
},
|
2021-03-23 10:32:12 -05:00
|
|
|
},
|
2020-07-23 11:52:22 -05:00
|
|
|
},
|
|
|
|
},
|
2021-03-23 10:32:12 -05:00
|
|
|
},
|
2020-07-23 11:52:22 -05:00
|
|
|
}, resp)
|
|
|
|
})
|
|
|
|
}
|
2020-04-25 15:48:20 -05:00
|
|
|
|
2020-07-23 11:52:22 -05:00
|
|
|
func TestQuery_GetLogGroupFields(t *testing.T) {
|
2021-01-07 04:36:13 -06:00
|
|
|
origNewCWLogsClient := NewCWLogsClient
|
2020-07-23 11:52:22 -05:00
|
|
|
t.Cleanup(func() {
|
2021-01-07 04:36:13 -06:00
|
|
|
NewCWLogsClient = origNewCWLogsClient
|
2020-07-23 11:52:22 -05:00
|
|
|
})
|
2020-04-25 15:48:20 -05:00
|
|
|
|
2021-01-07 04:36:13 -06:00
|
|
|
var cli FakeCWLogsClient
|
2020-04-25 15:48:20 -05:00
|
|
|
|
2021-01-07 04:36:13 -06:00
|
|
|
NewCWLogsClient = func(sess *session.Session) cloudwatchlogsiface.CloudWatchLogsAPI {
|
2020-07-23 11:52:22 -05:00
|
|
|
return cli
|
|
|
|
}
|
2020-04-25 15:48:20 -05:00
|
|
|
|
2021-01-07 04:36:13 -06:00
|
|
|
cli = FakeCWLogsClient{
|
2020-07-23 11:52:22 -05:00
|
|
|
logGroupFields: cloudwatchlogs.GetLogGroupFieldsOutput{
|
|
|
|
LogGroupFields: []*cloudwatchlogs.LogGroupField{
|
|
|
|
{
|
|
|
|
Name: aws.String("field_a"),
|
|
|
|
Percent: aws.Int64(100),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: aws.String("field_b"),
|
|
|
|
Percent: aws.Int64(30),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: aws.String("field_c"),
|
|
|
|
Percent: aws.Int64(55),
|
|
|
|
},
|
|
|
|
},
|
2020-04-25 15:48:20 -05:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-07-23 11:52:22 -05:00
|
|
|
const refID = "A"
|
2021-03-10 00:41:22 -06:00
|
|
|
|
2021-03-23 10:32:12 -05:00
|
|
|
im := datasource.NewInstanceManager(func(s backend.DataSourceInstanceSettings) (instancemgmt.Instance, error) {
|
|
|
|
return datasourceInfo{}, nil
|
|
|
|
})
|
|
|
|
|
|
|
|
executor := newExecutor(nil, im, newTestConfig(), fakeSessionCache{})
|
|
|
|
resp, err := executor.QueryData(context.Background(), &backend.QueryDataRequest{
|
|
|
|
PluginContext: backend.PluginContext{
|
|
|
|
DataSourceInstanceSettings: &backend.DataSourceInstanceSettings{},
|
|
|
|
},
|
|
|
|
Queries: []backend.DataQuery{
|
2020-07-23 11:52:22 -05:00
|
|
|
{
|
2021-03-08 00:02:49 -06:00
|
|
|
RefID: refID,
|
2021-03-23 10:32:12 -05:00
|
|
|
JSON: json.RawMessage(`{
|
|
|
|
"type": "logAction",
|
|
|
|
"subtype": "GetLogGroupFields",
|
2020-07-23 11:52:22 -05:00
|
|
|
"logGroupName": "group_a",
|
2021-03-23 10:32:12 -05:00
|
|
|
"limit": 50
|
|
|
|
}`),
|
2020-07-23 11:52:22 -05:00
|
|
|
},
|
|
|
|
},
|
2020-04-25 15:48:20 -05:00
|
|
|
})
|
2020-07-23 11:52:22 -05:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, resp)
|
|
|
|
|
|
|
|
expFrame := &data.Frame{
|
|
|
|
Name: refID,
|
|
|
|
Fields: []*data.Field{
|
|
|
|
data.NewField("name", nil, []*string{
|
|
|
|
aws.String("field_a"), aws.String("field_b"), aws.String("field_c"),
|
|
|
|
}),
|
|
|
|
data.NewField("percent", nil, []*int64{
|
|
|
|
aws.Int64(100), aws.Int64(30), aws.Int64(55),
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
Meta: &data.FrameMeta{
|
|
|
|
PreferredVisualization: "logs",
|
2020-04-25 15:48:20 -05:00
|
|
|
},
|
|
|
|
}
|
2020-07-23 11:52:22 -05:00
|
|
|
expFrame.RefID = refID
|
2021-03-23 10:32:12 -05:00
|
|
|
assert.Equal(t, &backend.QueryDataResponse{Responses: backend.Responses{
|
|
|
|
refID: backend.DataResponse{
|
|
|
|
Frames: data.Frames{expFrame},
|
2020-07-23 11:52:22 -05:00
|
|
|
},
|
2021-03-23 10:32:12 -05:00
|
|
|
},
|
2020-07-23 11:52:22 -05:00
|
|
|
}, resp)
|
|
|
|
}
|
2020-04-25 15:48:20 -05:00
|
|
|
|
2020-07-23 11:52:22 -05:00
|
|
|
func TestQuery_StartQuery(t *testing.T) {
|
2021-01-07 04:36:13 -06:00
|
|
|
origNewCWLogsClient := NewCWLogsClient
|
2020-07-23 11:52:22 -05:00
|
|
|
t.Cleanup(func() {
|
2021-01-07 04:36:13 -06:00
|
|
|
NewCWLogsClient = origNewCWLogsClient
|
2020-04-25 15:48:20 -05:00
|
|
|
})
|
|
|
|
|
2021-01-07 04:36:13 -06:00
|
|
|
var cli FakeCWLogsClient
|
2020-04-25 15:48:20 -05:00
|
|
|
|
2021-01-07 04:36:13 -06:00
|
|
|
NewCWLogsClient = func(sess *session.Session) cloudwatchlogsiface.CloudWatchLogsAPI {
|
2020-07-23 11:52:22 -05:00
|
|
|
return cli
|
2020-04-25 15:48:20 -05:00
|
|
|
}
|
|
|
|
|
2020-07-23 11:52:22 -05:00
|
|
|
t.Run("invalid time range", func(t *testing.T) {
|
2021-01-07 04:36:13 -06:00
|
|
|
cli = FakeCWLogsClient{
|
2020-07-23 11:52:22 -05:00
|
|
|
logGroupFields: cloudwatchlogs.GetLogGroupFieldsOutput{
|
|
|
|
LogGroupFields: []*cloudwatchlogs.LogGroupField{
|
|
|
|
{
|
|
|
|
Name: aws.String("field_a"),
|
|
|
|
Percent: aws.Int64(100),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: aws.String("field_b"),
|
|
|
|
Percent: aws.Int64(30),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: aws.String("field_c"),
|
|
|
|
Percent: aws.Int64(55),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-03-23 10:32:12 -05:00
|
|
|
timeRange := backend.TimeRange{
|
|
|
|
From: time.Unix(1584873443, 0),
|
|
|
|
To: time.Unix(1584700643, 0),
|
2020-07-23 11:52:22 -05:00
|
|
|
}
|
|
|
|
|
2021-03-23 10:32:12 -05:00
|
|
|
im := datasource.NewInstanceManager(func(s backend.DataSourceInstanceSettings) (instancemgmt.Instance, error) {
|
|
|
|
return datasourceInfo{}, nil
|
|
|
|
})
|
|
|
|
|
|
|
|
executor := newExecutor(nil, im, newTestConfig(), fakeSessionCache{})
|
|
|
|
_, err := executor.QueryData(context.Background(), &backend.QueryDataRequest{
|
|
|
|
PluginContext: backend.PluginContext{
|
|
|
|
DataSourceInstanceSettings: &backend.DataSourceInstanceSettings{},
|
|
|
|
},
|
|
|
|
Queries: []backend.DataQuery{
|
2020-07-23 11:52:22 -05:00
|
|
|
{
|
2021-03-23 10:32:12 -05:00
|
|
|
TimeRange: timeRange,
|
|
|
|
JSON: json.RawMessage(`{
|
2020-07-23 11:52:22 -05:00
|
|
|
"type": "logAction",
|
|
|
|
"subtype": "StartQuery",
|
|
|
|
"limit": 50,
|
|
|
|
"region": "default",
|
2021-03-23 10:32:12 -05:00
|
|
|
"queryString": "fields @message"
|
|
|
|
}`),
|
2020-07-23 11:52:22 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
require.Error(t, err)
|
2020-04-25 15:48:20 -05:00
|
|
|
|
2020-07-23 11:52:22 -05:00
|
|
|
assert.Equal(t, fmt.Errorf("invalid time range: start time must be before end time"), err)
|
2020-04-25 15:48:20 -05:00
|
|
|
})
|
|
|
|
|
2020-07-23 11:52:22 -05:00
|
|
|
t.Run("valid time range", func(t *testing.T) {
|
|
|
|
const refID = "A"
|
2021-01-07 04:36:13 -06:00
|
|
|
cli = FakeCWLogsClient{
|
2020-07-23 11:52:22 -05:00
|
|
|
logGroupFields: cloudwatchlogs.GetLogGroupFieldsOutput{
|
|
|
|
LogGroupFields: []*cloudwatchlogs.LogGroupField{
|
|
|
|
{
|
|
|
|
Name: aws.String("field_a"),
|
|
|
|
Percent: aws.Int64(100),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: aws.String("field_b"),
|
|
|
|
Percent: aws.Int64(30),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: aws.String("field_c"),
|
|
|
|
Percent: aws.Int64(55),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-03-23 10:32:12 -05:00
|
|
|
timeRange := backend.TimeRange{
|
|
|
|
From: time.Unix(1584700643000, 0),
|
|
|
|
To: time.Unix(1584873443000, 0),
|
2020-07-23 11:52:22 -05:00
|
|
|
}
|
|
|
|
|
2021-03-23 10:32:12 -05:00
|
|
|
im := datasource.NewInstanceManager(func(s backend.DataSourceInstanceSettings) (instancemgmt.Instance, error) {
|
|
|
|
return datasourceInfo{}, nil
|
|
|
|
})
|
|
|
|
|
|
|
|
executor := newExecutor(nil, im, newTestConfig(), fakeSessionCache{})
|
|
|
|
resp, err := executor.QueryData(context.Background(), &backend.QueryDataRequest{
|
|
|
|
PluginContext: backend.PluginContext{
|
|
|
|
DataSourceInstanceSettings: &backend.DataSourceInstanceSettings{},
|
|
|
|
},
|
|
|
|
Queries: []backend.DataQuery{
|
2020-07-23 11:52:22 -05:00
|
|
|
{
|
2021-03-23 10:32:12 -05:00
|
|
|
RefID: refID,
|
|
|
|
TimeRange: timeRange,
|
|
|
|
JSON: json.RawMessage(`{
|
2020-07-23 11:52:22 -05:00
|
|
|
"type": "logAction",
|
|
|
|
"subtype": "StartQuery",
|
|
|
|
"limit": 50,
|
|
|
|
"region": "default",
|
2021-03-23 10:32:12 -05:00
|
|
|
"queryString": "fields @message"
|
|
|
|
}`),
|
2020-07-23 11:52:22 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
expFrame := data.NewFrame(
|
|
|
|
refID,
|
|
|
|
data.NewField("queryId", nil, []string{"abcd-efgh-ijkl-mnop"}),
|
|
|
|
)
|
|
|
|
expFrame.RefID = refID
|
|
|
|
expFrame.Meta = &data.FrameMeta{
|
|
|
|
Custom: map[string]interface{}{
|
|
|
|
"Region": "default",
|
|
|
|
},
|
|
|
|
PreferredVisualization: "logs",
|
|
|
|
}
|
2021-03-23 10:32:12 -05:00
|
|
|
assert.Equal(t, &backend.QueryDataResponse{Responses: backend.Responses{
|
|
|
|
refID: {
|
|
|
|
Frames: data.Frames{expFrame},
|
2020-07-23 11:52:22 -05:00
|
|
|
},
|
2021-03-23 10:32:12 -05:00
|
|
|
},
|
2020-07-23 11:52:22 -05:00
|
|
|
}, resp)
|
|
|
|
})
|
|
|
|
}
|
2020-04-25 15:48:20 -05:00
|
|
|
|
2020-07-23 11:52:22 -05:00
|
|
|
func TestQuery_StopQuery(t *testing.T) {
|
2021-01-07 04:36:13 -06:00
|
|
|
origNewCWLogsClient := NewCWLogsClient
|
2020-07-23 11:52:22 -05:00
|
|
|
t.Cleanup(func() {
|
2021-01-07 04:36:13 -06:00
|
|
|
NewCWLogsClient = origNewCWLogsClient
|
2020-07-23 11:52:22 -05:00
|
|
|
})
|
2020-04-25 15:48:20 -05:00
|
|
|
|
2021-01-07 04:36:13 -06:00
|
|
|
var cli FakeCWLogsClient
|
2020-04-25 15:48:20 -05:00
|
|
|
|
2021-01-07 04:36:13 -06:00
|
|
|
NewCWLogsClient = func(sess *session.Session) cloudwatchlogsiface.CloudWatchLogsAPI {
|
2020-07-23 11:52:22 -05:00
|
|
|
return cli
|
|
|
|
}
|
2020-04-25 15:48:20 -05:00
|
|
|
|
2021-01-07 04:36:13 -06:00
|
|
|
cli = FakeCWLogsClient{
|
2020-07-23 11:52:22 -05:00
|
|
|
logGroupFields: cloudwatchlogs.GetLogGroupFieldsOutput{
|
|
|
|
LogGroupFields: []*cloudwatchlogs.LogGroupField{
|
|
|
|
{
|
|
|
|
Name: aws.String("field_a"),
|
|
|
|
Percent: aws.Int64(100),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: aws.String("field_b"),
|
|
|
|
Percent: aws.Int64(30),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: aws.String("field_c"),
|
|
|
|
Percent: aws.Int64(55),
|
|
|
|
},
|
|
|
|
},
|
2020-04-25 15:48:20 -05:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-03-23 10:32:12 -05:00
|
|
|
im := datasource.NewInstanceManager(func(s backend.DataSourceInstanceSettings) (instancemgmt.Instance, error) {
|
|
|
|
return datasourceInfo{}, nil
|
|
|
|
})
|
|
|
|
|
|
|
|
timeRange := backend.TimeRange{
|
|
|
|
From: time.Unix(1584873443, 0),
|
|
|
|
To: time.Unix(1584700643, 0),
|
2020-04-25 15:48:20 -05:00
|
|
|
}
|
|
|
|
|
2021-03-23 10:32:12 -05:00
|
|
|
executor := newExecutor(nil, im, newTestConfig(), fakeSessionCache{})
|
|
|
|
resp, err := executor.QueryData(context.Background(), &backend.QueryDataRequest{
|
|
|
|
PluginContext: backend.PluginContext{
|
|
|
|
DataSourceInstanceSettings: &backend.DataSourceInstanceSettings{},
|
|
|
|
},
|
|
|
|
Queries: []backend.DataQuery{
|
2020-07-23 11:52:22 -05:00
|
|
|
{
|
2021-03-23 10:32:12 -05:00
|
|
|
TimeRange: timeRange,
|
|
|
|
JSON: json.RawMessage(`{
|
2020-07-23 11:52:22 -05:00
|
|
|
"type": "logAction",
|
|
|
|
"subtype": "StopQuery",
|
2021-03-23 10:32:12 -05:00
|
|
|
"queryId": "abcd-efgh-ijkl-mnop"
|
|
|
|
}`),
|
2020-07-23 11:52:22 -05:00
|
|
|
},
|
|
|
|
},
|
2020-04-25 15:48:20 -05:00
|
|
|
})
|
2020-07-23 11:52:22 -05:00
|
|
|
require.NoError(t, err)
|
2020-04-25 15:48:20 -05:00
|
|
|
|
2020-07-23 11:52:22 -05:00
|
|
|
expFrame := &data.Frame{
|
|
|
|
Name: "StopQueryResponse",
|
|
|
|
Fields: []*data.Field{
|
|
|
|
data.NewField("success", nil, []bool{true}),
|
2020-04-25 15:48:20 -05:00
|
|
|
},
|
2020-07-23 11:52:22 -05:00
|
|
|
Meta: &data.FrameMeta{
|
|
|
|
PreferredVisualization: "logs",
|
2020-04-25 15:48:20 -05:00
|
|
|
},
|
|
|
|
}
|
2021-03-23 10:32:12 -05:00
|
|
|
assert.Equal(t, &backend.QueryDataResponse{Responses: backend.Responses{
|
|
|
|
"": {
|
|
|
|
Frames: data.Frames{expFrame},
|
2020-07-23 11:52:22 -05:00
|
|
|
},
|
2021-03-23 10:32:12 -05:00
|
|
|
},
|
2020-07-23 11:52:22 -05:00
|
|
|
}, resp)
|
|
|
|
}
|
2020-04-25 15:48:20 -05:00
|
|
|
|
2020-07-23 11:52:22 -05:00
|
|
|
func TestQuery_GetQueryResults(t *testing.T) {
|
2021-01-07 04:36:13 -06:00
|
|
|
origNewCWLogsClient := NewCWLogsClient
|
2020-07-23 11:52:22 -05:00
|
|
|
t.Cleanup(func() {
|
2021-01-07 04:36:13 -06:00
|
|
|
NewCWLogsClient = origNewCWLogsClient
|
2020-04-25 15:48:20 -05:00
|
|
|
})
|
|
|
|
|
2021-01-07 04:36:13 -06:00
|
|
|
var cli FakeCWLogsClient
|
2020-04-25 15:48:20 -05:00
|
|
|
|
2021-01-07 04:36:13 -06:00
|
|
|
NewCWLogsClient = func(sess *session.Session) cloudwatchlogsiface.CloudWatchLogsAPI {
|
2020-07-23 11:52:22 -05:00
|
|
|
return cli
|
|
|
|
}
|
2020-04-25 15:48:20 -05:00
|
|
|
|
2020-07-23 11:52:22 -05:00
|
|
|
const refID = "A"
|
2021-01-07 04:36:13 -06:00
|
|
|
cli = FakeCWLogsClient{
|
2020-07-23 11:52:22 -05:00
|
|
|
queryResults: cloudwatchlogs.GetQueryResultsOutput{
|
|
|
|
Results: [][]*cloudwatchlogs.ResultField{
|
|
|
|
{
|
|
|
|
{
|
|
|
|
Field: aws.String("@timestamp"),
|
|
|
|
Value: aws.String("2020-03-20 10:37:23.000"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Field: aws.String("field_b"),
|
|
|
|
Value: aws.String("b_1"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Field: aws.String("@ptr"),
|
|
|
|
Value: aws.String("abcdefg"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
{
|
|
|
|
Field: aws.String("@timestamp"),
|
|
|
|
Value: aws.String("2020-03-20 10:40:43.000"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Field: aws.String("field_b"),
|
|
|
|
Value: aws.String("b_2"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Field: aws.String("@ptr"),
|
|
|
|
Value: aws.String("hijklmnop"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Statistics: &cloudwatchlogs.QueryStatistics{
|
|
|
|
BytesScanned: aws.Float64(512),
|
|
|
|
RecordsMatched: aws.Float64(256),
|
|
|
|
RecordsScanned: aws.Float64(1024),
|
|
|
|
},
|
|
|
|
Status: aws.String("Complete"),
|
2020-04-25 15:48:20 -05:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-03-23 10:32:12 -05:00
|
|
|
im := datasource.NewInstanceManager(func(s backend.DataSourceInstanceSettings) (instancemgmt.Instance, error) {
|
|
|
|
return datasourceInfo{}, nil
|
|
|
|
})
|
|
|
|
|
|
|
|
executor := newExecutor(nil, im, newTestConfig(), fakeSessionCache{})
|
|
|
|
resp, err := executor.QueryData(context.Background(), &backend.QueryDataRequest{
|
|
|
|
PluginContext: backend.PluginContext{
|
|
|
|
DataSourceInstanceSettings: &backend.DataSourceInstanceSettings{},
|
|
|
|
},
|
|
|
|
Queries: []backend.DataQuery{
|
2020-07-23 11:52:22 -05:00
|
|
|
{
|
2021-03-08 00:02:49 -06:00
|
|
|
RefID: refID,
|
2021-03-23 10:32:12 -05:00
|
|
|
JSON: json.RawMessage(`{
|
2020-07-23 11:52:22 -05:00
|
|
|
"type": "logAction",
|
|
|
|
"subtype": "GetQueryResults",
|
2021-03-23 10:32:12 -05:00
|
|
|
"queryId": "abcd-efgh-ijkl-mnop"
|
|
|
|
}`),
|
2020-07-23 11:52:22 -05:00
|
|
|
},
|
|
|
|
},
|
2020-04-25 15:48:20 -05:00
|
|
|
})
|
2020-05-18 05:25:58 -05:00
|
|
|
require.NoError(t, err)
|
2020-07-23 11:52:22 -05:00
|
|
|
|
|
|
|
time1, err := time.Parse("2006-01-02 15:04:05.000", "2020-03-20 10:37:23.000")
|
2020-05-18 05:25:58 -05:00
|
|
|
require.NoError(t, err)
|
2020-07-23 11:52:22 -05:00
|
|
|
time2, err := time.Parse("2006-01-02 15:04:05.000", "2020-03-20 10:40:43.000")
|
2020-05-18 05:25:58 -05:00
|
|
|
require.NoError(t, err)
|
2020-07-23 11:52:22 -05:00
|
|
|
expField1 := data.NewField("@timestamp", nil, []*time.Time{
|
|
|
|
aws.Time(time1), aws.Time(time2),
|
2020-04-25 15:48:20 -05:00
|
|
|
})
|
2020-07-23 11:52:22 -05:00
|
|
|
expField1.SetConfig(&data.FieldConfig{DisplayName: "Time"})
|
|
|
|
expField2 := data.NewField("field_b", nil, []*string{
|
2020-04-25 15:48:20 -05:00
|
|
|
aws.String("b_1"), aws.String("b_2"),
|
|
|
|
})
|
2020-07-23 11:52:22 -05:00
|
|
|
expFrame := data.NewFrame(refID, expField1, expField2)
|
|
|
|
expFrame.RefID = refID
|
|
|
|
expFrame.Meta = &data.FrameMeta{
|
2020-04-25 15:48:20 -05:00
|
|
|
Custom: map[string]interface{}{
|
|
|
|
"Status": "Complete",
|
2020-08-19 09:18:04 -05:00
|
|
|
},
|
|
|
|
Stats: []data.QueryStat{
|
|
|
|
{
|
|
|
|
FieldConfig: data.FieldConfig{DisplayName: "Bytes scanned"},
|
|
|
|
Value: 512,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
FieldConfig: data.FieldConfig{DisplayName: "Records scanned"},
|
|
|
|
Value: 1024,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
FieldConfig: data.FieldConfig{DisplayName: "Records matched"},
|
|
|
|
Value: 256,
|
2020-04-25 15:48:20 -05:00
|
|
|
},
|
|
|
|
},
|
2020-07-23 11:52:22 -05:00
|
|
|
PreferredVisualization: "logs",
|
2020-04-25 15:48:20 -05:00
|
|
|
}
|
|
|
|
|
2021-03-23 10:32:12 -05:00
|
|
|
assert.Equal(t, &backend.QueryDataResponse{Responses: backend.Responses{
|
|
|
|
refID: {
|
|
|
|
Frames: data.Frames{expFrame},
|
2020-07-23 11:52:22 -05:00
|
|
|
},
|
2021-03-23 10:32:12 -05:00
|
|
|
},
|
2020-07-23 11:52:22 -05:00
|
|
|
}, resp)
|
2020-04-25 15:48:20 -05:00
|
|
|
}
|