github.com/leeprovoost/terraform@v0.6.10-0.20160119085442-96f3f76118e7/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_recurrence(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_recurrence,
    41  				Check: resource.ComposeTestCheckFunc(
    42  					testAccCheckScalingScheduleExists("aws_autoscaling_schedule.foobar", &schedule),
    43  					resource.TestCheckResourceAttr("aws_autoscaling_schedule.foobar", "recurrence", "0 8 * * *"),
    44  				),
    45  			},
    46  		},
    47  	})
    48  }
    49  
    50  func testAccCheckScalingScheduleExists(n string, policy *autoscaling.ScheduledUpdateGroupAction) resource.TestCheckFunc {
    51  	return func(s *terraform.State) error {
    52  		rs, ok := s.RootModule().Resources[n]
    53  		if !ok {
    54  			return fmt.Errorf("Not found: %s", n)
    55  		}
    56  
    57  		autoScalingGroup, _ := rs.Primary.Attributes["autoscaling_group_name"]
    58  		conn := testAccProvider.Meta().(*AWSClient).autoscalingconn
    59  		params := &autoscaling.DescribeScheduledActionsInput{
    60  			AutoScalingGroupName: aws.String(autoScalingGroup),
    61  			ScheduledActionNames: []*string{aws.String(rs.Primary.ID)},
    62  		}
    63  
    64  		resp, err := conn.DescribeScheduledActions(params)
    65  		if err != nil {
    66  			return err
    67  		}
    68  		if len(resp.ScheduledUpdateGroupActions) == 0 {
    69  			return fmt.Errorf("Scaling Schedule not found")
    70  		}
    71  
    72  		return nil
    73  	}
    74  }
    75  
    76  func testAccCheckAWSAutoscalingScheduleDestroy(s *terraform.State) error {
    77  	conn := testAccProvider.Meta().(*AWSClient).autoscalingconn
    78  
    79  	for _, rs := range s.RootModule().Resources {
    80  		if rs.Type != "aws_autoscaling_schedule" {
    81  			continue
    82  		}
    83  
    84  		autoScalingGroup, _ := rs.Primary.Attributes["autoscaling_group_name"]
    85  		params := &autoscaling.DescribeScheduledActionsInput{
    86  			AutoScalingGroupName: aws.String(autoScalingGroup),
    87  			ScheduledActionNames: []*string{aws.String(rs.Primary.ID)},
    88  		}
    89  
    90  		resp, err := conn.DescribeScheduledActions(params)
    91  
    92  		if err == nil {
    93  			if len(resp.ScheduledUpdateGroupActions) != 0 &&
    94  				*resp.ScheduledUpdateGroupActions[0].ScheduledActionName == rs.Primary.ID {
    95  				return fmt.Errorf("Scaling Schedule Still Exists: %s", rs.Primary.ID)
    96  			}
    97  		}
    98  	}
    99  
   100  	return nil
   101  }
   102  
   103  var testAccAWSAutoscalingScheduleConfig = fmt.Sprintf(`
   104  resource "aws_launch_configuration" "foobar" {
   105      name = "terraform-test-foobar5"
   106      image_id = "ami-21f78e11"
   107      instance_type = "t1.micro"
   108  }
   109  
   110  resource "aws_autoscaling_group" "foobar" {
   111      availability_zones = ["us-west-2a"]
   112      name = "terraform-test-foobar5"
   113      max_size = 1
   114      min_size = 1
   115      health_check_grace_period = 300
   116      health_check_type = "ELB"
   117      force_delete = true
   118      termination_policies = ["OldestInstance"]
   119      launch_configuration = "${aws_launch_configuration.foobar.name}"
   120      tag {
   121          key = "Foo"
   122          value = "foo-bar"
   123          propagate_at_launch = true
   124      }
   125  }
   126  
   127  resource "aws_autoscaling_schedule" "foobar" {
   128      scheduled_action_name = "foobar"
   129      min_size = 0
   130      max_size = 1
   131      desired_capacity = 0
   132      start_time = "2016-12-11T18:00:00Z"
   133      end_time = "2016-12-12T06:00:00Z"
   134      autoscaling_group_name = "${aws_autoscaling_group.foobar.name}"
   135  }
   136  `)
   137  
   138  var testAccAWSAutoscalingScheduleConfig_recurrence = fmt.Sprintf(`
   139  resource "aws_launch_configuration" "foobar" {
   140      name = "terraform-test-foobar5"
   141      image_id = "ami-21f78e11"
   142      instance_type = "t1.micro"
   143  }
   144  
   145  resource "aws_autoscaling_group" "foobar" {
   146      availability_zones = ["us-west-2a"]
   147      name = "terraform-test-foobar5"
   148      max_size = 1
   149      min_size = 1
   150      health_check_grace_period = 300
   151      health_check_type = "ELB"
   152      force_delete = true
   153      termination_policies = ["OldestInstance"]
   154      launch_configuration = "${aws_launch_configuration.foobar.name}"
   155      tag {
   156          key = "Foo"
   157          value = "foo-bar"
   158          propagate_at_launch = true
   159      }
   160  }
   161  
   162  resource "aws_autoscaling_schedule" "foobar" {
   163      scheduled_action_name = "foobar"
   164      min_size = 0
   165      max_size = 1
   166      desired_capacity = 0
   167      recurrence = "0 8 * * *"
   168      autoscaling_group_name = "${aws_autoscaling_group.foobar.name}"
   169  }
   170  `)