github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/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/resource" 11 "github.com/hashicorp/terraform/terraform" 12 ) 13 14 func TestAccAWSCloudWatchMetricAlarm_basic(t *testing.T) { 15 var alarm cloudwatch.MetricAlarm 16 17 resource.Test(t, resource.TestCase{ 18 PreCheck: func() { testAccPreCheck(t) }, 19 Providers: testAccProviders, 20 CheckDestroy: testAccCheckAWSCloudWatchMetricAlarmDestroy, 21 Steps: []resource.TestStep{ 22 { 23 Config: testAccAWSCloudWatchMetricAlarmConfig, 24 Check: resource.ComposeTestCheckFunc( 25 testAccCheckCloudWatchMetricAlarmExists("aws_cloudwatch_metric_alarm.foobar", &alarm), 26 resource.TestCheckResourceAttr("aws_cloudwatch_metric_alarm.foobar", "metric_name", "CPUUtilization"), 27 resource.TestCheckResourceAttr("aws_cloudwatch_metric_alarm.foobar", "statistic", "Average"), 28 testAccCheckCloudWatchMetricAlarmDimension( 29 "aws_cloudwatch_metric_alarm.foobar", "InstanceId", "i-abc123"), 30 ), 31 }, 32 }, 33 }) 34 } 35 36 func TestAccAWSCloudWatchMetricAlarm_extendedStatistic(t *testing.T) { 37 var alarm cloudwatch.MetricAlarm 38 39 resource.Test(t, resource.TestCase{ 40 PreCheck: func() { testAccPreCheck(t) }, 41 Providers: testAccProviders, 42 CheckDestroy: testAccCheckAWSCloudWatchMetricAlarmDestroy, 43 Steps: []resource.TestStep{ 44 { 45 Config: testAccAWSCloudWatchMetricAlarmConfigExtendedStatistic, 46 Check: resource.ComposeTestCheckFunc( 47 testAccCheckCloudWatchMetricAlarmExists("aws_cloudwatch_metric_alarm.foobar", &alarm), 48 resource.TestCheckResourceAttr("aws_cloudwatch_metric_alarm.foobar", "extended_statistic", "p88.0"), 49 ), 50 }, 51 }, 52 }) 53 } 54 55 func TestAccAWSCloudWatchMetricAlarm_missingStatistic(t *testing.T) { 56 resource.Test(t, resource.TestCase{ 57 PreCheck: func() { testAccPreCheck(t) }, 58 Providers: testAccProviders, 59 CheckDestroy: testAccCheckAWSCloudWatchMetricAlarmDestroy, 60 Steps: []resource.TestStep{ 61 { 62 Config: testAccAWSCloudWatchMetricAlarmConfigMissingStatistic, 63 ExpectError: regexp.MustCompile("One of `statistic` or `extended_statistic` must be set for a cloudwatch metric alarm"), 64 }, 65 }, 66 }) 67 } 68 69 func testAccCheckCloudWatchMetricAlarmDimension(n, k, v string) resource.TestCheckFunc { 70 return func(s *terraform.State) error { 71 rs, ok := s.RootModule().Resources[n] 72 if !ok { 73 return fmt.Errorf("Not found: %s", n) 74 } 75 key := fmt.Sprintf("dimensions.%s", k) 76 val, ok := rs.Primary.Attributes[key] 77 if !ok { 78 return fmt.Errorf("Could not find dimension: %s", k) 79 } 80 if val != v { 81 return fmt.Errorf("Expected dimension %s => %s; got: %s", k, v, val) 82 } 83 return nil 84 } 85 } 86 87 func testAccCheckCloudWatchMetricAlarmExists(n string, alarm *cloudwatch.MetricAlarm) resource.TestCheckFunc { 88 return func(s *terraform.State) error { 89 rs, ok := s.RootModule().Resources[n] 90 if !ok { 91 return fmt.Errorf("Not found: %s", n) 92 } 93 94 conn := testAccProvider.Meta().(*AWSClient).cloudwatchconn 95 params := cloudwatch.DescribeAlarmsInput{ 96 AlarmNames: []*string{aws.String(rs.Primary.ID)}, 97 } 98 resp, err := conn.DescribeAlarms(¶ms) 99 if err != nil { 100 return err 101 } 102 if len(resp.MetricAlarms) == 0 { 103 return fmt.Errorf("Alarm not found") 104 } 105 *alarm = *resp.MetricAlarms[0] 106 107 return nil 108 } 109 } 110 111 func testAccCheckAWSCloudWatchMetricAlarmDestroy(s *terraform.State) error { 112 conn := testAccProvider.Meta().(*AWSClient).cloudwatchconn 113 114 for _, rs := range s.RootModule().Resources { 115 if rs.Type != "aws_cloudwatch_metric_alarm" { 116 continue 117 } 118 119 params := cloudwatch.DescribeAlarmsInput{ 120 AlarmNames: []*string{aws.String(rs.Primary.ID)}, 121 } 122 123 resp, err := conn.DescribeAlarms(¶ms) 124 125 if err == nil { 126 if len(resp.MetricAlarms) != 0 && 127 *resp.MetricAlarms[0].AlarmName == rs.Primary.ID { 128 return fmt.Errorf("Alarm Still Exists: %s", rs.Primary.ID) 129 } 130 } 131 } 132 133 return nil 134 } 135 136 var testAccAWSCloudWatchMetricAlarmConfig = fmt.Sprintf(` 137 resource "aws_cloudwatch_metric_alarm" "foobar" { 138 alarm_name = "terraform-test-foobar5" 139 comparison_operator = "GreaterThanOrEqualToThreshold" 140 evaluation_periods = "2" 141 metric_name = "CPUUtilization" 142 namespace = "AWS/EC2" 143 period = "120" 144 statistic = "Average" 145 threshold = "80" 146 alarm_description = "This metric monitors ec2 cpu utilization" 147 insufficient_data_actions = [] 148 dimensions { 149 InstanceId = "i-abc123" 150 } 151 } 152 `) 153 154 var testAccAWSCloudWatchMetricAlarmConfigExtendedStatistic = fmt.Sprintf(` 155 resource "aws_cloudwatch_metric_alarm" "foobar" { 156 alarm_name = "terraform-test-foobar6" 157 comparison_operator = "GreaterThanOrEqualToThreshold" 158 evaluation_periods = "2" 159 metric_name = "CPUUtilization" 160 namespace = "AWS/EC2" 161 period = "120" 162 extended_statistic = "p88.0" 163 threshold = "80" 164 alarm_description = "This metric monitors ec2 cpu utilization" 165 insufficient_data_actions = [] 166 dimensions { 167 InstanceId = "i-abc123" 168 } 169 } 170 `) 171 172 var testAccAWSCloudWatchMetricAlarmConfigMissingStatistic = fmt.Sprintf(` 173 resource "aws_cloudwatch_metric_alarm" "foobar" { 174 alarm_name = "terraform-test-foobar6" 175 comparison_operator = "GreaterThanOrEqualToThreshold" 176 evaluation_periods = "2" 177 metric_name = "CPUUtilization" 178 namespace = "AWS/EC2" 179 period = "120" 180 threshold = "80" 181 alarm_description = "This metric monitors ec2 cpu utilization" 182 insufficient_data_actions = [] 183 dimensions { 184 InstanceId = "i-abc123" 185 } 186 } 187 `)