github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/aws/resource_aws_autoscaling_schedule_test.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "testing" 6 "time" 7 8 "github.com/aws/aws-sdk-go/aws" 9 "github.com/aws/aws-sdk-go/service/autoscaling" 10 "github.com/hashicorp/terraform/helper/acctest" 11 "github.com/hashicorp/terraform/helper/resource" 12 "github.com/hashicorp/terraform/terraform" 13 ) 14 15 func TestAccAWSAutoscalingSchedule_basic(t *testing.T) { 16 var schedule autoscaling.ScheduledUpdateGroupAction 17 rName := fmt.Sprintf("tf-test-%d", acctest.RandInt()) 18 n := time.Now().UTC() 19 d, err := time.ParseDuration("2h") 20 if err != nil { 21 t.Fatalf("err parsing time duration: %s", err) 22 } 23 s, err := time.ParseDuration("1h") 24 if err != nil { 25 t.Fatalf("err parsing time duration: %s", err) 26 } 27 start := n.Add(s).Format(awsAutoscalingScheduleTimeLayout) 28 end := n.Add(d).Format(awsAutoscalingScheduleTimeLayout) 29 30 resource.Test(t, resource.TestCase{ 31 PreCheck: func() { testAccPreCheck(t) }, 32 Providers: testAccProviders, 33 CheckDestroy: testAccCheckAWSAutoscalingScheduleDestroy, 34 Steps: []resource.TestStep{ 35 { 36 Config: testAccAWSAutoscalingScheduleConfig(rName, start, end), 37 Check: resource.ComposeTestCheckFunc( 38 testAccCheckScalingScheduleExists("aws_autoscaling_schedule.foobar", &schedule), 39 ), 40 }, 41 }, 42 }) 43 } 44 45 func TestAccAWSAutoscalingSchedule_disappears(t *testing.T) { 46 var schedule autoscaling.ScheduledUpdateGroupAction 47 rName := fmt.Sprintf("tf-test-%d", acctest.RandInt()) 48 n := time.Now().UTC() 49 d, err := time.ParseDuration("2h") 50 if err != nil { 51 t.Fatalf("err parsing time duration: %s", err) 52 } 53 s, err := time.ParseDuration("1h") 54 if err != nil { 55 t.Fatalf("err parsing time duration: %s", err) 56 } 57 start := n.Add(s).Format(awsAutoscalingScheduleTimeLayout) 58 end := n.Add(d).Format(awsAutoscalingScheduleTimeLayout) 59 60 resource.Test(t, resource.TestCase{ 61 PreCheck: func() { testAccPreCheck(t) }, 62 Providers: testAccProviders, 63 CheckDestroy: testAccCheckAWSAutoscalingScheduleDestroy, 64 Steps: []resource.TestStep{ 65 { 66 Config: testAccAWSAutoscalingScheduleConfig(rName, start, end), 67 Check: resource.ComposeTestCheckFunc( 68 testAccCheckScalingScheduleExists("aws_autoscaling_schedule.foobar", &schedule), 69 testAccCheckScalingScheduleDisappears(&schedule), 70 ), 71 ExpectNonEmptyPlan: true, 72 }, 73 }, 74 }) 75 } 76 77 func testAccCheckScalingScheduleDisappears(schedule *autoscaling.ScheduledUpdateGroupAction) resource.TestCheckFunc { 78 return func(s *terraform.State) error { 79 autoscalingconn := testAccProvider.Meta().(*AWSClient).autoscalingconn 80 params := &autoscaling.DeleteScheduledActionInput{ 81 AutoScalingGroupName: schedule.AutoScalingGroupName, 82 ScheduledActionName: schedule.ScheduledActionName, 83 } 84 if _, err := autoscalingconn.DeleteScheduledAction(params); err != nil { 85 return err 86 } 87 return nil 88 } 89 } 90 91 func TestAccAWSAutoscalingSchedule_recurrence(t *testing.T) { 92 var schedule autoscaling.ScheduledUpdateGroupAction 93 94 rName := fmt.Sprintf("tf-test-%d", acctest.RandInt()) 95 resource.Test(t, resource.TestCase{ 96 PreCheck: func() { testAccPreCheck(t) }, 97 Providers: testAccProviders, 98 CheckDestroy: testAccCheckAWSAutoscalingScheduleDestroy, 99 Steps: []resource.TestStep{ 100 { 101 Config: testAccAWSAutoscalingScheduleConfig_recurrence(rName), 102 Check: resource.ComposeTestCheckFunc( 103 testAccCheckScalingScheduleExists("aws_autoscaling_schedule.foobar", &schedule), 104 resource.TestCheckResourceAttr("aws_autoscaling_schedule.foobar", "recurrence", "0 8 * * *"), 105 ), 106 }, 107 }, 108 }) 109 } 110 111 func TestAccAWSAutoscalingSchedule_zeroValues(t *testing.T) { 112 var schedule autoscaling.ScheduledUpdateGroupAction 113 114 rName := fmt.Sprintf("tf-test-%d", acctest.RandInt()) 115 resource.Test(t, resource.TestCase{ 116 PreCheck: func() { testAccPreCheck(t) }, 117 Providers: testAccProviders, 118 CheckDestroy: testAccCheckAWSAutoscalingScheduleDestroy, 119 Steps: []resource.TestStep{ 120 resource.TestStep{ 121 Config: testAccAWSAutoscalingScheduleConfig_zeroValues(rName), 122 Check: resource.ComposeTestCheckFunc( 123 testAccCheckScalingScheduleExists("aws_autoscaling_schedule.foobar", &schedule), 124 ), 125 }, 126 }, 127 }) 128 } 129 130 func testAccCheckScalingScheduleExists(n string, policy *autoscaling.ScheduledUpdateGroupAction) resource.TestCheckFunc { 131 return func(s *terraform.State) error { 132 rs, ok := s.RootModule().Resources[n] 133 if !ok { 134 return fmt.Errorf("Not found: %s", n) 135 } 136 137 autoScalingGroup, _ := rs.Primary.Attributes["autoscaling_group_name"] 138 conn := testAccProvider.Meta().(*AWSClient).autoscalingconn 139 params := &autoscaling.DescribeScheduledActionsInput{ 140 AutoScalingGroupName: aws.String(autoScalingGroup), 141 ScheduledActionNames: []*string{aws.String(rs.Primary.ID)}, 142 } 143 144 resp, err := conn.DescribeScheduledActions(params) 145 if err != nil { 146 return err 147 } 148 if len(resp.ScheduledUpdateGroupActions) == 0 { 149 return fmt.Errorf("Scaling Schedule not found") 150 } 151 152 *policy = *resp.ScheduledUpdateGroupActions[0] 153 154 return nil 155 } 156 } 157 158 func testAccCheckAWSAutoscalingScheduleDestroy(s *terraform.State) error { 159 conn := testAccProvider.Meta().(*AWSClient).autoscalingconn 160 161 for _, rs := range s.RootModule().Resources { 162 if rs.Type != "aws_autoscaling_schedule" { 163 continue 164 } 165 166 autoScalingGroup, _ := rs.Primary.Attributes["autoscaling_group_name"] 167 params := &autoscaling.DescribeScheduledActionsInput{ 168 AutoScalingGroupName: aws.String(autoScalingGroup), 169 ScheduledActionNames: []*string{aws.String(rs.Primary.ID)}, 170 } 171 172 resp, err := conn.DescribeScheduledActions(params) 173 174 if err == nil { 175 if len(resp.ScheduledUpdateGroupActions) != 0 && 176 *resp.ScheduledUpdateGroupActions[0].ScheduledActionName == rs.Primary.ID { 177 return fmt.Errorf("Scaling Schedule Still Exists: %s", rs.Primary.ID) 178 } 179 } 180 } 181 182 return nil 183 } 184 185 func testAccAWSAutoscalingScheduleConfig(r, start, end string) string { 186 return fmt.Sprintf(` 187 resource "aws_launch_configuration" "foobar" { 188 name = "%s" 189 image_id = "ami-21f78e11" 190 instance_type = "t1.micro" 191 } 192 193 resource "aws_autoscaling_group" "foobar" { 194 availability_zones = ["us-west-2a"] 195 name = "%s" 196 max_size = 1 197 min_size = 1 198 health_check_grace_period = 300 199 health_check_type = "ELB" 200 force_delete = true 201 termination_policies = ["OldestInstance"] 202 launch_configuration = "${aws_launch_configuration.foobar.name}" 203 tag { 204 key = "Foo" 205 value = "foo-bar" 206 propagate_at_launch = true 207 } 208 } 209 210 resource "aws_autoscaling_schedule" "foobar" { 211 scheduled_action_name = "foobar" 212 min_size = 0 213 max_size = 1 214 desired_capacity = 0 215 start_time = "%s" 216 end_time = "%s" 217 autoscaling_group_name = "${aws_autoscaling_group.foobar.name}" 218 }`, r, r, start, end) 219 } 220 221 func testAccAWSAutoscalingScheduleConfig_recurrence(r string) string { 222 return fmt.Sprintf(` 223 resource "aws_launch_configuration" "foobar" { 224 name = "%s" 225 image_id = "ami-21f78e11" 226 instance_type = "t1.micro" 227 } 228 229 resource "aws_autoscaling_group" "foobar" { 230 availability_zones = ["us-west-2a"] 231 name = "%s" 232 max_size = 1 233 min_size = 1 234 health_check_grace_period = 300 235 health_check_type = "ELB" 236 force_delete = true 237 termination_policies = ["OldestInstance"] 238 launch_configuration = "${aws_launch_configuration.foobar.name}" 239 tag { 240 key = "Foo" 241 value = "foo-bar" 242 propagate_at_launch = true 243 } 244 } 245 246 resource "aws_autoscaling_schedule" "foobar" { 247 scheduled_action_name = "foobar" 248 min_size = 0 249 max_size = 1 250 desired_capacity = 0 251 recurrence = "0 8 * * *" 252 autoscaling_group_name = "${aws_autoscaling_group.foobar.name}" 253 }`, r, r) 254 } 255 256 func testAccAWSAutoscalingScheduleConfig_zeroValues(r string) string { 257 return fmt.Sprintf(` 258 resource "aws_launch_configuration" "foobar" { 259 name = "%s" 260 image_id = "ami-21f78e11" 261 instance_type = "t1.micro" 262 } 263 264 resource "aws_autoscaling_group" "foobar" { 265 availability_zones = ["us-west-2a"] 266 name = "%s" 267 max_size = 1 268 min_size = 1 269 health_check_grace_period = 300 270 health_check_type = "ELB" 271 force_delete = true 272 termination_policies = ["OldestInstance"] 273 launch_configuration = "${aws_launch_configuration.foobar.name}" 274 tag { 275 key = "Foo" 276 value = "foo-bar" 277 propagate_at_launch = true 278 } 279 } 280 281 resource "aws_autoscaling_schedule" "foobar" { 282 scheduled_action_name = "foobar" 283 max_size = 0 284 min_size = 0 285 desired_capacity = 0 286 start_time = "2018-01-16T07:00:00Z" 287 end_time = "2018-01-16T13:00:00Z" 288 autoscaling_group_name = "${aws_autoscaling_group.foobar.name}" 289 }`, r, r) 290 }