metric_find_query_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package cloudwatch
  2. import (
  3. "context"
  4. "testing"
  5. "github.com/aws/aws-sdk-go/aws"
  6. "github.com/aws/aws-sdk-go/service/cloudwatch"
  7. "github.com/aws/aws-sdk-go/service/ec2"
  8. "github.com/aws/aws-sdk-go/service/ec2/ec2iface"
  9. "github.com/grafana/grafana/pkg/components/simplejson"
  10. "github.com/grafana/grafana/pkg/tsdb"
  11. . "github.com/smartystreets/goconvey/convey"
  12. )
  13. type mockedEc2 struct {
  14. ec2iface.EC2API
  15. Resp ec2.DescribeInstancesOutput
  16. }
  17. func (m mockedEc2) DescribeInstancesPages(in *ec2.DescribeInstancesInput, fn func(*ec2.DescribeInstancesOutput, bool) bool) error {
  18. fn(&m.Resp, true)
  19. return nil
  20. }
  21. func TestCloudWatchMetrics(t *testing.T) {
  22. Convey("When calling getMetricsForCustomMetrics", t, func() {
  23. dsInfo := &DatasourceInfo{
  24. Region: "us-east-1",
  25. Namespace: "Foo",
  26. Profile: "default",
  27. AssumeRoleArn: "",
  28. }
  29. f := func(dsInfo *DatasourceInfo) (cloudwatch.ListMetricsOutput, error) {
  30. return cloudwatch.ListMetricsOutput{
  31. Metrics: []*cloudwatch.Metric{
  32. {
  33. MetricName: aws.String("Test_MetricName"),
  34. Dimensions: []*cloudwatch.Dimension{
  35. {
  36. Name: aws.String("Test_DimensionName"),
  37. },
  38. },
  39. },
  40. },
  41. }, nil
  42. }
  43. metrics, _ := getMetricsForCustomMetrics(dsInfo, f)
  44. Convey("Should contain Test_MetricName", func() {
  45. So(metrics, ShouldContain, "Test_MetricName")
  46. })
  47. })
  48. Convey("When calling getDimensionsForCustomMetrics", t, func() {
  49. dsInfo := &DatasourceInfo{
  50. Region: "us-east-1",
  51. Namespace: "Foo",
  52. Profile: "default",
  53. AssumeRoleArn: "",
  54. }
  55. f := func(dsInfo *DatasourceInfo) (cloudwatch.ListMetricsOutput, error) {
  56. return cloudwatch.ListMetricsOutput{
  57. Metrics: []*cloudwatch.Metric{
  58. {
  59. MetricName: aws.String("Test_MetricName"),
  60. Dimensions: []*cloudwatch.Dimension{
  61. {
  62. Name: aws.String("Test_DimensionName"),
  63. },
  64. },
  65. },
  66. },
  67. }, nil
  68. }
  69. dimensionKeys, _ := getDimensionsForCustomMetrics(dsInfo, f)
  70. Convey("Should contain Test_DimensionName", func() {
  71. So(dimensionKeys, ShouldContain, "Test_DimensionName")
  72. })
  73. })
  74. Convey("When calling handleGetEc2InstanceAttribute", t, func() {
  75. executor := &CloudWatchExecutor{
  76. ec2Svc: mockedEc2{Resp: ec2.DescribeInstancesOutput{
  77. Reservations: []*ec2.Reservation{
  78. {
  79. Instances: []*ec2.Instance{
  80. {
  81. InstanceId: aws.String("i-12345678"),
  82. Tags: []*ec2.Tag{
  83. {
  84. Key: aws.String("Environment"),
  85. Value: aws.String("production"),
  86. },
  87. },
  88. },
  89. },
  90. },
  91. },
  92. }},
  93. }
  94. json := simplejson.New()
  95. json.Set("region", "us-east-1")
  96. json.Set("attributeName", "InstanceId")
  97. filters := make(map[string]interface{})
  98. filters["tag:Environment"] = []string{"production"}
  99. json.Set("filters", filters)
  100. result, _ := executor.handleGetEc2InstanceAttribute(context.Background(), json, &tsdb.TsdbQuery{})
  101. Convey("Should equal production InstanceId", func() {
  102. So(result[0].Text, ShouldEqual, "i-12345678")
  103. })
  104. })
  105. }