github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/aws/resource_aws_cloudwatch_metric_alarm_test.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "regexp" 6 "testing" 7 8 "github.com/aws/aws-sdk-go/aws" 9 "github.com/aws/aws-sdk-go/service/cloudwatch" 10 "github.com/hashicorp/terraform/helper/acctest" 11 "github.com/hashicorp/terraform/helper/resource" 12 "github.com/hashicorp/terraform/terraform" 13 ) 14 15 func TestAccAWSCloudWatchMetricAlarm_basic(t *testing.T) { 16 var alarm cloudwatch.MetricAlarm 17 rInt := acctest.RandInt() 18 resource.Test(t, resource.TestCase{ 19 PreCheck: func() { testAccPreCheck(t) }, 20 Providers: testAccProviders, 21 CheckDestroy: testAccCheckAWSCloudWatchMetricAlarmDestroy, 22 Steps: []resource.TestStep{ 23 { 24 Config: testAccAWSCloudWatchMetricAlarmConfig(rInt), 25 Check: resource.ComposeTestCheckFunc( 26 testAccCheckCloudWatchMetricAlarmExists("aws_cloudwatch_metric_alarm.foobar", &alarm), 27 resource.TestCheckResourceAttr("aws_cloudwatch_metric_alarm.foobar", "metric_name", "CPUUtilization"), 28 resource.TestCheckResourceAttr("aws_cloudwatch_metric_alarm.foobar", "statistic", "Average"), 29 testAccCheckCloudWatchMetricAlarmDimension( 30 "aws_cloudwatch_metric_alarm.foobar", "InstanceId", "i-abc123"), 31 ), 32 }, 33 }, 34 }) 35 } 36 37 func TestAccAWSCloudWatchMetricAlarm_treatMissingData(t *testing.T) { 38 var alarm cloudwatch.MetricAlarm 39 rInt := acctest.RandInt() 40 41 resource.Test(t, resource.TestCase{ 42 PreCheck: func() { testAccPreCheck(t) }, 43 Providers: testAccProviders, 44 CheckDestroy: testAccCheckAWSCloudWatchMetricAlarmDestroy, 45 Steps: []resource.TestStep{ 46 { 47 Config: testAccAWSCloudWatchMetricAlarmConfigTreatMissingData(rInt), 48 Check: resource.ComposeTestCheckFunc( 49 testAccCheckCloudWatchMetricAlarmExists("aws_cloudwatch_metric_alarm.foobar", &alarm), 50 resource.TestCheckResourceAttr("aws_cloudwatch_metric_alarm.foobar", "treat_missing_data", "missing"), 51 ), 52 }, 53 { 54 Config: testAccAWSCloudWatchMetricAlarmConfigTreatMissingDataUpdate(rInt), 55 Check: resource.ComposeTestCheckFunc( 56 testAccCheckCloudWatchMetricAlarmExists("aws_cloudwatch_metric_alarm.foobar", &alarm), 57 resource.TestCheckResourceAttr("aws_cloudwatch_metric_alarm.foobar", "treat_missing_data", "breaching"), 58 ), 59 }, 60 }, 61 }) 62 } 63 64 func TestAccAWSCloudWatchMetricAlarm_evaluateLowSampleCountPercentiles(t *testing.T) { 65 var alarm cloudwatch.MetricAlarm 66 rInt := acctest.RandInt() 67 68 resource.Test(t, resource.TestCase{ 69 PreCheck: func() { testAccPreCheck(t) }, 70 Providers: testAccProviders, 71 CheckDestroy: testAccCheckAWSCloudWatchMetricAlarmDestroy, 72 Steps: []resource.TestStep{ 73 { 74 Config: testAccAWSCloudWatchMetricAlarmConfigTreatEvaluateLowSampleCountPercentiles(rInt), 75 Check: resource.ComposeTestCheckFunc( 76 testAccCheckCloudWatchMetricAlarmExists("aws_cloudwatch_metric_alarm.foobar", &alarm), 77 resource.TestCheckResourceAttr("aws_cloudwatch_metric_alarm.foobar", "evaluate_low_sample_count_percentiles", "evaluate"), 78 ), 79 }, 80 { 81 Config: testAccAWSCloudWatchMetricAlarmConfigTreatEvaluateLowSampleCountPercentilesUpdated(rInt), 82 Check: resource.ComposeTestCheckFunc( 83 testAccCheckCloudWatchMetricAlarmExists("aws_cloudwatch_metric_alarm.foobar", &alarm), 84 resource.TestCheckResourceAttr("aws_cloudwatch_metric_alarm.foobar", "evaluate_low_sample_count_percentiles", "ignore"), 85 ), 86 }, 87 }, 88 }) 89 } 90 91 func TestAccAWSCloudWatchMetricAlarm_extendedStatistic(t *testing.T) { 92 var alarm cloudwatch.MetricAlarm 93 rInt := acctest.RandInt() 94 95 resource.Test(t, resource.TestCase{ 96 PreCheck: func() { testAccPreCheck(t) }, 97 Providers: testAccProviders, 98 CheckDestroy: testAccCheckAWSCloudWatchMetricAlarmDestroy, 99 Steps: []resource.TestStep{ 100 { 101 Config: testAccAWSCloudWatchMetricAlarmConfigExtendedStatistic(rInt), 102 Check: resource.ComposeTestCheckFunc( 103 testAccCheckCloudWatchMetricAlarmExists("aws_cloudwatch_metric_alarm.foobar", &alarm), 104 resource.TestCheckResourceAttr("aws_cloudwatch_metric_alarm.foobar", "extended_statistic", "p88.0"), 105 ), 106 }, 107 }, 108 }) 109 } 110 111 func TestAccAWSCloudWatchMetricAlarm_missingStatistic(t *testing.T) { 112 rInt := acctest.RandInt() 113 resource.Test(t, resource.TestCase{ 114 PreCheck: func() { testAccPreCheck(t) }, 115 Providers: testAccProviders, 116 CheckDestroy: testAccCheckAWSCloudWatchMetricAlarmDestroy, 117 Steps: []resource.TestStep{ 118 { 119 Config: testAccAWSCloudWatchMetricAlarmConfigMissingStatistic(rInt), 120 ExpectError: regexp.MustCompile("One of `statistic` or `extended_statistic` must be set for a cloudwatch metric alarm"), 121 }, 122 }, 123 }) 124 } 125 126 func testAccCheckCloudWatchMetricAlarmDimension(n, k, v string) resource.TestCheckFunc { 127 return func(s *terraform.State) error { 128 rs, ok := s.RootModule().Resources[n] 129 if !ok { 130 return fmt.Errorf("Not found: %s", n) 131 } 132 key := fmt.Sprintf("dimensions.%s", k) 133 val, ok := rs.Primary.Attributes[key] 134 if !ok { 135 return fmt.Errorf("Could not find dimension: %s", k) 136 } 137 if val != v { 138 return fmt.Errorf("Expected dimension %s => %s; got: %s", k, v, val) 139 } 140 return nil 141 } 142 } 143 144 func testAccCheckCloudWatchMetricAlarmExists(n string, alarm *cloudwatch.MetricAlarm) resource.TestCheckFunc { 145 return func(s *terraform.State) error { 146 rs, ok := s.RootModule().Resources[n] 147 if !ok { 148 return fmt.Errorf("Not found: %s", n) 149 } 150 151 conn := testAccProvider.Meta().(*AWSClient).cloudwatchconn 152 params := cloudwatch.DescribeAlarmsInput{ 153 AlarmNames: []*string{aws.String(rs.Primary.ID)}, 154 } 155 resp, err := conn.DescribeAlarms(¶ms) 156 if err != nil { 157 return err 158 } 159 if len(resp.MetricAlarms) == 0 { 160 return fmt.Errorf("Alarm not found") 161 } 162 *alarm = *resp.MetricAlarms[0] 163 164 return nil 165 } 166 } 167 168 func testAccCheckAWSCloudWatchMetricAlarmDestroy(s *terraform.State) error { 169 conn := testAccProvider.Meta().(*AWSClient).cloudwatchconn 170 171 for _, rs := range s.RootModule().Resources { 172 if rs.Type != "aws_cloudwatch_metric_alarm" { 173 continue 174 } 175 176 params := cloudwatch.DescribeAlarmsInput{ 177 AlarmNames: []*string{aws.String(rs.Primary.ID)}, 178 } 179 180 resp, err := conn.DescribeAlarms(¶ms) 181 182 if err == nil { 183 if len(resp.MetricAlarms) != 0 && 184 *resp.MetricAlarms[0].AlarmName == rs.Primary.ID { 185 return fmt.Errorf("Alarm Still Exists: %s", rs.Primary.ID) 186 } 187 } 188 } 189 190 return nil 191 } 192 193 func testAccAWSCloudWatchMetricAlarmConfig(rInt int) string { 194 return fmt.Sprintf(` 195 resource "aws_cloudwatch_metric_alarm" "foobar" { 196 alarm_name = "terraform-test-foobar%d" 197 comparison_operator = "GreaterThanOrEqualToThreshold" 198 evaluation_periods = "2" 199 metric_name = "CPUUtilization" 200 namespace = "AWS/EC2" 201 period = "120" 202 statistic = "Average" 203 threshold = "80" 204 alarm_description = "This metric monitors ec2 cpu utilization" 205 insufficient_data_actions = [] 206 dimensions { 207 InstanceId = "i-abc123" 208 } 209 }`, rInt) 210 } 211 212 func testAccAWSCloudWatchMetricAlarmConfigTreatMissingData(rInt int) string { 213 return fmt.Sprintf(` 214 resource "aws_cloudwatch_metric_alarm" "foobar" { 215 alarm_name = "terraform-test-foobar%d" 216 comparison_operator = "GreaterThanOrEqualToThreshold" 217 evaluation_periods = "2" 218 metric_name = "CPUUtilization" 219 namespace = "AWS/EC2" 220 period = "120" 221 statistic = "Average" 222 threshold = "80" 223 alarm_description = "This metric monitors ec2 cpu utilization" 224 treat_missing_data = "missing" 225 insufficient_data_actions = [] 226 dimensions { 227 InstanceId = "i-abc123" 228 } 229 }`, rInt) 230 } 231 232 func testAccAWSCloudWatchMetricAlarmConfigTreatMissingDataUpdate(rInt int) string { 233 return fmt.Sprintf(` 234 resource "aws_cloudwatch_metric_alarm" "foobar" { 235 alarm_name = "terraform-test-foobar%d" 236 comparison_operator = "GreaterThanOrEqualToThreshold" 237 evaluation_periods = "2" 238 metric_name = "CPUUtilization" 239 namespace = "AWS/EC2" 240 period = "120" 241 statistic = "Average" 242 threshold = "80" 243 alarm_description = "This metric monitors ec2 cpu utilization" 244 treat_missing_data = "breaching" 245 insufficient_data_actions = [] 246 dimensions { 247 InstanceId = "i-abc123" 248 } 249 }`, rInt) 250 } 251 252 func testAccAWSCloudWatchMetricAlarmConfigTreatEvaluateLowSampleCountPercentiles(rInt int) string { 253 return fmt.Sprintf(` 254 resource "aws_cloudwatch_metric_alarm" "foobar" { 255 alarm_name = "terraform-test-foobar%d" 256 comparison_operator = "GreaterThanOrEqualToThreshold" 257 evaluation_periods = "2" 258 metric_name = "CPUUtilization" 259 namespace = "AWS/EC2" 260 period = "120" 261 extended_statistic = "p88.0" 262 threshold = "80" 263 alarm_description = "This metric monitors ec2 cpu utilization" 264 evaluate_low_sample_count_percentiles = "evaluate" 265 insufficient_data_actions = [] 266 dimensions { 267 InstanceId = "i-abc123" 268 } 269 }`, rInt) 270 } 271 272 func testAccAWSCloudWatchMetricAlarmConfigTreatEvaluateLowSampleCountPercentilesUpdated(rInt int) string { 273 return fmt.Sprintf(` 274 resource "aws_cloudwatch_metric_alarm" "foobar" { 275 alarm_name = "terraform-test-foobar%d" 276 comparison_operator = "GreaterThanOrEqualToThreshold" 277 evaluation_periods = "2" 278 metric_name = "CPUUtilization" 279 namespace = "AWS/EC2" 280 period = "120" 281 extended_statistic = "p88.0" 282 threshold = "80" 283 alarm_description = "This metric monitors ec2 cpu utilization" 284 evaluate_low_sample_count_percentiles = "ignore" 285 insufficient_data_actions = [] 286 dimensions { 287 InstanceId = "i-abc123" 288 } 289 }`, rInt) 290 } 291 292 func testAccAWSCloudWatchMetricAlarmConfigExtendedStatistic(rInt int) string { 293 return fmt.Sprintf(` 294 resource "aws_cloudwatch_metric_alarm" "foobar" { 295 alarm_name = "terraform-test-foobar%d" 296 comparison_operator = "GreaterThanOrEqualToThreshold" 297 evaluation_periods = "2" 298 metric_name = "CPUUtilization" 299 namespace = "AWS/EC2" 300 period = "120" 301 extended_statistic = "p88.0" 302 threshold = "80" 303 alarm_description = "This metric monitors ec2 cpu utilization" 304 insufficient_data_actions = [] 305 dimensions { 306 InstanceId = "i-abc123" 307 } 308 }`, rInt) 309 } 310 311 func testAccAWSCloudWatchMetricAlarmConfigMissingStatistic(rInt int) string { 312 return fmt.Sprintf(` 313 resource "aws_cloudwatch_metric_alarm" "foobar" { 314 alarm_name = "terraform-test-foobar%d" 315 comparison_operator = "GreaterThanOrEqualToThreshold" 316 evaluation_periods = "2" 317 metric_name = "CPUUtilization" 318 namespace = "AWS/EC2" 319 period = "120" 320 threshold = "80" 321 alarm_description = "This metric monitors ec2 cpu utilization" 322 insufficient_data_actions = [] 323 dimensions { 324 InstanceId = "i-abc123" 325 } 326 }`, rInt) 327 }