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