metric_find_query_test.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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/bmizerany/assert"
  10. "github.com/grafana/grafana/pkg/components/simplejson"
  11. "github.com/grafana/grafana/pkg/tsdb"
  12. . "github.com/smartystreets/goconvey/convey"
  13. )
  14. type mockedEc2 struct {
  15. ec2iface.EC2API
  16. Resp ec2.DescribeInstancesOutput
  17. }
  18. func (m mockedEc2) DescribeInstancesPages(in *ec2.DescribeInstancesInput, fn func(*ec2.DescribeInstancesOutput, bool) bool) error {
  19. fn(&m.Resp, true)
  20. return nil
  21. }
  22. func TestCloudWatchMetrics(t *testing.T) {
  23. Convey("When calling getMetricsForCustomMetrics", t, func() {
  24. dsInfo := &DatasourceInfo{
  25. Region: "us-east-1",
  26. Namespace: "Foo",
  27. Profile: "default",
  28. AssumeRoleArn: "",
  29. }
  30. f := func(dsInfo *DatasourceInfo) (cloudwatch.ListMetricsOutput, error) {
  31. return cloudwatch.ListMetricsOutput{
  32. Metrics: []*cloudwatch.Metric{
  33. {
  34. MetricName: aws.String("Test_MetricName"),
  35. Dimensions: []*cloudwatch.Dimension{
  36. {
  37. Name: aws.String("Test_DimensionName"),
  38. },
  39. },
  40. },
  41. },
  42. }, nil
  43. }
  44. metrics, _ := getMetricsForCustomMetrics(dsInfo, f)
  45. Convey("Should contain Test_MetricName", func() {
  46. So(metrics, ShouldContain, "Test_MetricName")
  47. })
  48. })
  49. Convey("When calling getDimensionsForCustomMetrics", t, func() {
  50. dsInfo := &DatasourceInfo{
  51. Region: "us-east-1",
  52. Namespace: "Foo",
  53. Profile: "default",
  54. AssumeRoleArn: "",
  55. }
  56. f := func(dsInfo *DatasourceInfo) (cloudwatch.ListMetricsOutput, error) {
  57. return cloudwatch.ListMetricsOutput{
  58. Metrics: []*cloudwatch.Metric{
  59. {
  60. MetricName: aws.String("Test_MetricName"),
  61. Dimensions: []*cloudwatch.Dimension{
  62. {
  63. Name: aws.String("Test_DimensionName"),
  64. },
  65. },
  66. },
  67. },
  68. }, nil
  69. }
  70. dimensionKeys, _ := getDimensionsForCustomMetrics(dsInfo, f)
  71. Convey("Should contain Test_DimensionName", func() {
  72. So(dimensionKeys, ShouldContain, "Test_DimensionName")
  73. })
  74. })
  75. Convey("When calling handleGetEc2InstanceAttribute", t, func() {
  76. executor := &CloudWatchExecutor{
  77. ec2Svc: mockedEc2{Resp: ec2.DescribeInstancesOutput{
  78. Reservations: []*ec2.Reservation{
  79. {
  80. Instances: []*ec2.Instance{
  81. {
  82. InstanceId: aws.String("i-12345678"),
  83. Tags: []*ec2.Tag{
  84. {
  85. Key: aws.String("Environment"),
  86. Value: aws.String("production"),
  87. },
  88. },
  89. },
  90. },
  91. },
  92. },
  93. }},
  94. }
  95. json := simplejson.New()
  96. json.Set("region", "us-east-1")
  97. json.Set("attributeName", "InstanceId")
  98. filters := make(map[string]interface{})
  99. filters["tag:Environment"] = []string{"production"}
  100. json.Set("filters", filters)
  101. result, _ := executor.handleGetEc2InstanceAttribute(context.Background(), json, &tsdb.TsdbQuery{})
  102. Convey("Should equal production InstanceId", func() {
  103. So(result[0].Text, ShouldEqual, "i-12345678")
  104. })
  105. })
  106. Convey("When calling handleGetEbsVolumeIds", t, func() {
  107. executor := &CloudWatchExecutor{
  108. ec2Svc: mockedEc2{Resp: ec2.DescribeInstancesOutput{
  109. Reservations: []*ec2.Reservation{
  110. {
  111. Instances: []*ec2.Instance{
  112. {
  113. InstanceId: aws.String("i-1"),
  114. BlockDeviceMappings: []*ec2.InstanceBlockDeviceMapping{
  115. {Ebs: &ec2.EbsInstanceBlockDevice{VolumeId: aws.String("vol-1-1")}},
  116. {Ebs: &ec2.EbsInstanceBlockDevice{VolumeId: aws.String("vol-1-2")}},
  117. },
  118. },
  119. {
  120. InstanceId: aws.String("i-2"),
  121. BlockDeviceMappings: []*ec2.InstanceBlockDeviceMapping{
  122. {Ebs: &ec2.EbsInstanceBlockDevice{VolumeId: aws.String("vol-2-1")}},
  123. {Ebs: &ec2.EbsInstanceBlockDevice{VolumeId: aws.String("vol-2-2")}},
  124. },
  125. },
  126. },
  127. },
  128. {
  129. Instances: []*ec2.Instance{
  130. {
  131. InstanceId: aws.String("i-3"),
  132. BlockDeviceMappings: []*ec2.InstanceBlockDeviceMapping{
  133. {Ebs: &ec2.EbsInstanceBlockDevice{VolumeId: aws.String("vol-3-1")}},
  134. {Ebs: &ec2.EbsInstanceBlockDevice{VolumeId: aws.String("vol-3-2")}},
  135. },
  136. },
  137. {
  138. InstanceId: aws.String("i-4"),
  139. BlockDeviceMappings: []*ec2.InstanceBlockDeviceMapping{
  140. {Ebs: &ec2.EbsInstanceBlockDevice{VolumeId: aws.String("vol-4-1")}},
  141. {Ebs: &ec2.EbsInstanceBlockDevice{VolumeId: aws.String("vol-4-2")}},
  142. },
  143. },
  144. },
  145. },
  146. },
  147. }},
  148. }
  149. json := simplejson.New()
  150. json.Set("region", "us-east-1")
  151. json.Set("instanceId", "{i-1, i-2, i-3, i-4}")
  152. result, _ := executor.handleGetEbsVolumeIds(context.Background(), json, &tsdb.TsdbQuery{})
  153. Convey("Should return all 8 VolumeIds", func() {
  154. So(len(result), ShouldEqual, 8)
  155. So(result[0].Text, ShouldEqual, "vol-1-1")
  156. So(result[1].Text, ShouldEqual, "vol-1-2")
  157. So(result[2].Text, ShouldEqual, "vol-2-1")
  158. So(result[3].Text, ShouldEqual, "vol-2-2")
  159. So(result[4].Text, ShouldEqual, "vol-3-1")
  160. So(result[5].Text, ShouldEqual, "vol-3-2")
  161. So(result[6].Text, ShouldEqual, "vol-4-1")
  162. So(result[7].Text, ShouldEqual, "vol-4-2")
  163. })
  164. })
  165. }
  166. func TestParseMultiSelectValue(t *testing.T) {
  167. values := parseMultiSelectValue(" i-someInstance ")
  168. assert.Equal(t, []string{"i-someInstance"}, values)
  169. values = parseMultiSelectValue("{i-05}")
  170. assert.Equal(t, []string{"i-05"}, values)
  171. values = parseMultiSelectValue(" {i-01, i-03, i-04} ")
  172. assert.Equal(t, []string{"i-01", "i-03", "i-04"}, values)
  173. values = parseMultiSelectValue("i-{01}")
  174. assert.Equal(t, []string{"i-{01}"}, values)
  175. }