github.com/meteor/terraform@v0.6.15-0.20210412225145-79ec4bc057c6/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 TestAccAWSAutoscalingSchedule_negativeOne(t *testing.T) { 131 var schedule autoscaling.ScheduledUpdateGroupAction 132 133 rName := fmt.Sprintf("tf-test-%d", acctest.RandInt()) 134 resource.Test(t, resource.TestCase{ 135 PreCheck: func() { testAccPreCheck(t) }, 136 Providers: testAccProviders, 137 CheckDestroy: testAccCheckAWSAutoscalingScheduleDestroy, 138 Steps: []resource.TestStep{ 139 resource.TestStep{ 140 Config: testAccAWSAutoscalingScheduleConfig_negativeOne(rName), 141 Check: resource.ComposeTestCheckFunc( 142 testAccCheckScalingScheduleExists("aws_autoscaling_schedule.foobar", &schedule), 143 testAccCheckScalingScheduleHasNoDesiredCapacity(&schedule), 144 resource.TestCheckResourceAttr("aws_autoscaling_schedule.foobar", "desired_capacity", "-1"), 145 ), 146 }, 147 }, 148 }) 149 } 150 151 func testAccCheckScalingScheduleExists(n string, policy *autoscaling.ScheduledUpdateGroupAction) resource.TestCheckFunc { 152 return func(s *terraform.State) error { 153 rs, ok := s.RootModule().Resources[n] 154 if !ok { 155 return fmt.Errorf("Not found: %s", n) 156 } 157 158 autoScalingGroup, _ := rs.Primary.Attributes["autoscaling_group_name"] 159 conn := testAccProvider.Meta().(*AWSClient).autoscalingconn 160 params := &autoscaling.DescribeScheduledActionsInput{ 161 AutoScalingGroupName: aws.String(autoScalingGroup), 162 ScheduledActionNames: []*string{aws.String(rs.Primary.ID)}, 163 } 164 165 resp, err := conn.DescribeScheduledActions(params) 166 if err != nil { 167 return err 168 } 169 if len(resp.ScheduledUpdateGroupActions) == 0 { 170 return fmt.Errorf("Scaling Schedule not found") 171 } 172 173 *policy = *resp.ScheduledUpdateGroupActions[0] 174 175 return nil 176 } 177 } 178 179 func testAccCheckAWSAutoscalingScheduleDestroy(s *terraform.State) error { 180 conn := testAccProvider.Meta().(*AWSClient).autoscalingconn 181 182 for _, rs := range s.RootModule().Resources { 183 if rs.Type != "aws_autoscaling_schedule" { 184 continue 185 } 186 187 autoScalingGroup, _ := rs.Primary.Attributes["autoscaling_group_name"] 188 params := &autoscaling.DescribeScheduledActionsInput{ 189 AutoScalingGroupName: aws.String(autoScalingGroup), 190 ScheduledActionNames: []*string{aws.String(rs.Primary.ID)}, 191 } 192 193 resp, err := conn.DescribeScheduledActions(params) 194 195 if err == nil { 196 if len(resp.ScheduledUpdateGroupActions) != 0 && 197 *resp.ScheduledUpdateGroupActions[0].ScheduledActionName == rs.Primary.ID { 198 return fmt.Errorf("Scaling Schedule Still Exists: %s", rs.Primary.ID) 199 } 200 } 201 } 202 203 return nil 204 } 205 206 func testAccCheckScalingScheduleHasNoDesiredCapacity( 207 schedule *autoscaling.ScheduledUpdateGroupAction) resource.TestCheckFunc { 208 return func(s *terraform.State) error { 209 if schedule.DesiredCapacity == nil { 210 return nil 211 } 212 return fmt.Errorf("Expected not to set desired capacity, got %v", 213 aws.Int64Value(schedule.DesiredCapacity)) 214 } 215 } 216 217 func testAccAWSAutoscalingScheduleConfig(r, start, end string) string { 218 return fmt.Sprintf(` 219 resource "aws_launch_configuration" "foobar" { 220 name = "%s" 221 image_id = "ami-21f78e11" 222 instance_type = "t1.micro" 223 } 224 225 resource "aws_autoscaling_group" "foobar" { 226 availability_zones = ["us-west-2a"] 227 name = "%s" 228 max_size = 1 229 min_size = 1 230 health_check_grace_period = 300 231 health_check_type = "ELB" 232 force_delete = true 233 termination_policies = ["OldestInstance"] 234 launch_configuration = "${aws_launch_configuration.foobar.name}" 235 tag { 236 key = "Foo" 237 value = "foo-bar" 238 propagate_at_launch = true 239 } 240 } 241 242 resource "aws_autoscaling_schedule" "foobar" { 243 scheduled_action_name = "foobar" 244 min_size = 0 245 max_size = 1 246 desired_capacity = 0 247 start_time = "%s" 248 end_time = "%s" 249 autoscaling_group_name = "${aws_autoscaling_group.foobar.name}" 250 }`, r, r, start, end) 251 } 252 253 func testAccAWSAutoscalingScheduleConfig_recurrence(r string) string { 254 return fmt.Sprintf(` 255 resource "aws_launch_configuration" "foobar" { 256 name = "%s" 257 image_id = "ami-21f78e11" 258 instance_type = "t1.micro" 259 } 260 261 resource "aws_autoscaling_group" "foobar" { 262 availability_zones = ["us-west-2a"] 263 name = "%s" 264 max_size = 1 265 min_size = 1 266 health_check_grace_period = 300 267 health_check_type = "ELB" 268 force_delete = true 269 termination_policies = ["OldestInstance"] 270 launch_configuration = "${aws_launch_configuration.foobar.name}" 271 tag { 272 key = "Foo" 273 value = "foo-bar" 274 propagate_at_launch = true 275 } 276 } 277 278 resource "aws_autoscaling_schedule" "foobar" { 279 scheduled_action_name = "foobar" 280 min_size = 0 281 max_size = 1 282 desired_capacity = 0 283 recurrence = "0 8 * * *" 284 autoscaling_group_name = "${aws_autoscaling_group.foobar.name}" 285 }`, r, r) 286 } 287 288 func testAccAWSAutoscalingScheduleConfig_zeroValues(r string) string { 289 return fmt.Sprintf(` 290 resource "aws_launch_configuration" "foobar" { 291 name = "%s" 292 image_id = "ami-21f78e11" 293 instance_type = "t1.micro" 294 } 295 296 resource "aws_autoscaling_group" "foobar" { 297 availability_zones = ["us-west-2a"] 298 name = "%s" 299 max_size = 1 300 min_size = 1 301 health_check_grace_period = 300 302 health_check_type = "ELB" 303 force_delete = true 304 termination_policies = ["OldestInstance"] 305 launch_configuration = "${aws_launch_configuration.foobar.name}" 306 tag { 307 key = "Foo" 308 value = "foo-bar" 309 propagate_at_launch = true 310 } 311 } 312 313 resource "aws_autoscaling_schedule" "foobar" { 314 scheduled_action_name = "foobar" 315 max_size = 0 316 min_size = 0 317 desired_capacity = 0 318 start_time = "2018-01-16T07:00:00Z" 319 end_time = "2018-01-16T13:00:00Z" 320 autoscaling_group_name = "${aws_autoscaling_group.foobar.name}" 321 }`, r, r) 322 } 323 324 func testAccAWSAutoscalingScheduleConfig_negativeOne(r string) string { 325 return fmt.Sprintf(` 326 resource "aws_launch_configuration" "foobar" { 327 name = "%s" 328 image_id = "ami-21f78e11" 329 instance_type = "t1.micro" 330 } 331 332 resource "aws_autoscaling_group" "foobar" { 333 availability_zones = ["us-west-2a"] 334 name = "%s" 335 max_size = 1 336 min_size = 1 337 health_check_grace_period = 300 338 health_check_type = "ELB" 339 force_delete = true 340 termination_policies = ["OldestInstance"] 341 launch_configuration = "${aws_launch_configuration.foobar.name}" 342 tag { 343 key = "Foo" 344 value = "foo-bar" 345 propagate_at_launch = true 346 } 347 } 348 349 resource "aws_autoscaling_schedule" "foobar" { 350 scheduled_action_name = "%s" 351 max_size = 3 352 min_size = 1 353 desired_capacity = -1 354 start_time = "2018-01-16T07:00:00Z" 355 end_time = "2018-01-16T13:00:00Z" 356 autoscaling_group_name = "${aws_autoscaling_group.foobar.name}" 357 }`, r, r, r) 358 }