github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/builtin/providers/aws/resource_aws_autoscaling_schedule_test.go (about)

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