mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
add test
This commit is contained in:
parent
3447b8b299
commit
220c4f4ab4
@ -235,7 +235,8 @@ func parseMultiSelectValue(input string) []string {
|
|||||||
// Please update the region list in public/app/plugins/datasource/cloudwatch/partials/config.html
|
// Please update the region list in public/app/plugins/datasource/cloudwatch/partials/config.html
|
||||||
func (e *CloudWatchExecutor) handleGetRegions(ctx context.Context, parameters *simplejson.Json, queryContext *tsdb.TsdbQuery) ([]suggestData, error) {
|
func (e *CloudWatchExecutor) handleGetRegions(ctx context.Context, parameters *simplejson.Json, queryContext *tsdb.TsdbQuery) ([]suggestData, error) {
|
||||||
dsInfo := e.getDsInfo("default")
|
dsInfo := e.getDsInfo("default")
|
||||||
if cache, ok := regionCache.Load(dsInfo.Profile); ok {
|
profile := dsInfo.Profile
|
||||||
|
if cache, ok := regionCache.Load(profile); ok {
|
||||||
if cache2, ok2 := cache.([]suggestData); ok2 {
|
if cache2, ok2 := cache.([]suggestData); ok2 {
|
||||||
return cache2, nil
|
return cache2, nil
|
||||||
}
|
}
|
||||||
@ -276,7 +277,7 @@ func (e *CloudWatchExecutor) handleGetRegions(ctx context.Context, parameters *s
|
|||||||
for _, region := range regions {
|
for _, region := range regions {
|
||||||
result = append(result, suggestData{Text: region, Value: region})
|
result = append(result, suggestData{Text: region, Value: region})
|
||||||
}
|
}
|
||||||
regionCache.Store(dsInfo.Profile, result)
|
regionCache.Store(profile, result)
|
||||||
|
|
||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
@ -9,20 +9,26 @@ import (
|
|||||||
"github.com/aws/aws-sdk-go/service/ec2"
|
"github.com/aws/aws-sdk-go/service/ec2"
|
||||||
"github.com/aws/aws-sdk-go/service/ec2/ec2iface"
|
"github.com/aws/aws-sdk-go/service/ec2/ec2iface"
|
||||||
"github.com/bmizerany/assert"
|
"github.com/bmizerany/assert"
|
||||||
|
"github.com/grafana/grafana/pkg/components/securejsondata"
|
||||||
"github.com/grafana/grafana/pkg/components/simplejson"
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
||||||
|
"github.com/grafana/grafana/pkg/models"
|
||||||
"github.com/grafana/grafana/pkg/tsdb"
|
"github.com/grafana/grafana/pkg/tsdb"
|
||||||
. "github.com/smartystreets/goconvey/convey"
|
. "github.com/smartystreets/goconvey/convey"
|
||||||
)
|
)
|
||||||
|
|
||||||
type mockedEc2 struct {
|
type mockedEc2 struct {
|
||||||
ec2iface.EC2API
|
ec2iface.EC2API
|
||||||
Resp ec2.DescribeInstancesOutput
|
Resp ec2.DescribeInstancesOutput
|
||||||
|
RespRegions ec2.DescribeRegionsOutput
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m mockedEc2) DescribeInstancesPages(in *ec2.DescribeInstancesInput, fn func(*ec2.DescribeInstancesOutput, bool) bool) error {
|
func (m mockedEc2) DescribeInstancesPages(in *ec2.DescribeInstancesInput, fn func(*ec2.DescribeInstancesOutput, bool) bool) error {
|
||||||
fn(&m.Resp, true)
|
fn(&m.Resp, true)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
func (m mockedEc2) DescribeRegions(in *ec2.DescribeRegionsInput) (*ec2.DescribeRegionsOutput, error) {
|
||||||
|
return &m.RespRegions, nil
|
||||||
|
}
|
||||||
|
|
||||||
func TestCloudWatchMetrics(t *testing.T) {
|
func TestCloudWatchMetrics(t *testing.T) {
|
||||||
|
|
||||||
@ -82,6 +88,31 @@ func TestCloudWatchMetrics(t *testing.T) {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
Convey("When calling handleGetRegions", t, func() {
|
||||||
|
executor := &CloudWatchExecutor{
|
||||||
|
ec2Svc: mockedEc2{RespRegions: ec2.DescribeRegionsOutput{
|
||||||
|
Regions: []*ec2.Region{
|
||||||
|
{
|
||||||
|
RegionName: aws.String("ap-northeast-2"),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}},
|
||||||
|
}
|
||||||
|
jsonData := simplejson.New()
|
||||||
|
jsonData.Set("defaultRegion", "default")
|
||||||
|
executor.DataSource = &models.DataSource{
|
||||||
|
JsonData: jsonData,
|
||||||
|
SecureJsonData: securejsondata.SecureJsonData{},
|
||||||
|
}
|
||||||
|
|
||||||
|
result, _ := executor.handleGetRegions(context.Background(), simplejson.New(), &tsdb.TsdbQuery{})
|
||||||
|
|
||||||
|
Convey("Should return regions", func() {
|
||||||
|
So(result[0].Text, ShouldEqual, "ap-northeast-1")
|
||||||
|
So(result[1].Text, ShouldEqual, "ap-northeast-2")
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
Convey("When calling handleGetEc2InstanceAttribute", t, func() {
|
Convey("When calling handleGetEc2InstanceAttribute", t, func() {
|
||||||
executor := &CloudWatchExecutor{
|
executor := &CloudWatchExecutor{
|
||||||
ec2Svc: mockedEc2{Resp: ec2.DescribeInstancesOutput{
|
ec2Svc: mockedEc2{Resp: ec2.DescribeInstancesOutput{
|
||||||
|
Loading…
Reference in New Issue
Block a user