metric_find_query_test.go 6.5 KB

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