You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

133 lines
3.6 KiB

  1. package cloudwatch_test
  2. import (
  3. "testing"
  4. "github.com/goamz/goamz/aws"
  5. "github.com/goamz/goamz/cloudwatch"
  6. "github.com/goamz/goamz/testutil"
  7. . "gopkg.in/check.v1"
  8. )
  9. func Test(t *testing.T) {
  10. TestingT(t)
  11. }
  12. type S struct {
  13. cw *cloudwatch.CloudWatch
  14. }
  15. var _ = Suite(&S{})
  16. var testServer = testutil.NewHTTPServer()
  17. func (s *S) SetUpSuite(c *C) {
  18. testServer.Start()
  19. auth := aws.Auth{AccessKey: "abc", SecretKey: "123"}
  20. s.cw, _ = cloudwatch.NewCloudWatch(auth, aws.ServiceInfo{testServer.URL, aws.V2Signature})
  21. }
  22. func (s *S) TearDownTest(c *C) {
  23. testServer.Flush()
  24. }
  25. func getTestAlarm() *cloudwatch.MetricAlarm {
  26. alarm := new(cloudwatch.MetricAlarm)
  27. alarm.AlarmName = "TestAlarm"
  28. alarm.MetricName = "TestMetric"
  29. alarm.Namespace = "TestNamespace"
  30. alarm.ComparisonOperator = "LessThanThreshold"
  31. alarm.Threshold = 1
  32. alarm.EvaluationPeriods = 5
  33. alarm.Period = 60
  34. alarm.Statistic = "Sum"
  35. return alarm
  36. }
  37. func (s *S) TestPutAlarm(c *C) {
  38. testServer.Response(200, nil, "<RequestId>123</RequestId>")
  39. alarm := getTestAlarm()
  40. _, err := s.cw.PutMetricAlarm(alarm)
  41. c.Assert(err, IsNil)
  42. req := testServer.WaitRequest()
  43. c.Assert(req.Method, Equals, "POST")
  44. c.Assert(req.URL.Path, Equals, "/")
  45. c.Assert(req.Form["Action"], DeepEquals, []string{"PutMetricAlarm"})
  46. c.Assert(req.Form["AlarmName"], DeepEquals, []string{"TestAlarm"})
  47. c.Assert(req.Form["ComparisonOperator"], DeepEquals, []string{"LessThanThreshold"})
  48. c.Assert(req.Form["EvaluationPeriods"], DeepEquals, []string{"5"})
  49. c.Assert(req.Form["Threshold"], DeepEquals, []string{"1.0000000000E+00"})
  50. c.Assert(req.Form["Period"], DeepEquals, []string{"60"})
  51. c.Assert(req.Form["Statistic"], DeepEquals, []string{"Sum"})
  52. }
  53. func (s *S) TestPutAlarmWithAction(c *C) {
  54. testServer.Response(200, nil, "<RequestId>123</RequestId>")
  55. alarm := getTestAlarm()
  56. alarm.AlarmActions = []cloudwatch.AlarmAction{
  57. cloudwatch.AlarmAction{
  58. ARN: "123",
  59. },
  60. }
  61. alarm.OkActions = []cloudwatch.AlarmAction{
  62. cloudwatch.AlarmAction{
  63. ARN: "456",
  64. },
  65. }
  66. alarm.InsufficientDataActions = []cloudwatch.AlarmAction{
  67. cloudwatch.AlarmAction{
  68. ARN: "789",
  69. },
  70. }
  71. _, err := s.cw.PutMetricAlarm(alarm)
  72. c.Assert(err, IsNil)
  73. req := testServer.WaitRequest()
  74. c.Assert(req.Method, Equals, "POST")
  75. c.Assert(req.URL.Path, Equals, "/")
  76. c.Assert(req.Form["Action"], DeepEquals, []string{"PutMetricAlarm"})
  77. c.Assert(req.Form["AlarmActions.member.1"], DeepEquals, []string{"123"})
  78. c.Assert(req.Form["OKActions.member.1"], DeepEquals, []string{"456"})
  79. c.Assert(req.Form["InsufficientDataActions.member.1"], DeepEquals, []string{"789"})
  80. c.Assert(req.Form["AlarmName"], DeepEquals, []string{"TestAlarm"})
  81. c.Assert(req.Form["ComparisonOperator"], DeepEquals, []string{"LessThanThreshold"})
  82. c.Assert(req.Form["EvaluationPeriods"], DeepEquals, []string{"5"})
  83. c.Assert(req.Form["Threshold"], DeepEquals, []string{"1.0000000000E+00"})
  84. c.Assert(req.Form["Period"], DeepEquals, []string{"60"})
  85. c.Assert(req.Form["Statistic"], DeepEquals, []string{"Sum"})
  86. }
  87. func (s *S) TestPutAlarmInvalidComapirsonOperator(c *C) {
  88. testServer.Response(200, nil, "<RequestId>123</RequestId>")
  89. alarm := getTestAlarm()
  90. alarm.ComparisonOperator = "LessThan"
  91. _, err := s.cw.PutMetricAlarm(alarm)
  92. c.Assert(err, NotNil)
  93. c.Assert(err.Error(), Equals, "ComparisonOperator is not valid")
  94. }
  95. func (s *S) TestPutAlarmInvalidStatistic(c *C) {
  96. testServer.Response(200, nil, "<RequestId>123</RequestId>")
  97. alarm := getTestAlarm()
  98. alarm.Statistic = "Count"
  99. _, err := s.cw.PutMetricAlarm(alarm)
  100. c.Assert(err, NotNil)
  101. c.Assert(err.Error(), Equals, "Invalid statistic value supplied")
  102. }