github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/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 TestAccAWSAutoscalingSchedule_zeroValues(t *testing.T) {
    51  	var schedule autoscaling.ScheduledUpdateGroupAction
    52  
    53  	resource.Test(t, resource.TestCase{
    54  		PreCheck:     func() { testAccPreCheck(t) },
    55  		Providers:    testAccProviders,
    56  		CheckDestroy: testAccCheckAWSAutoscalingScheduleDestroy,
    57  		Steps: []resource.TestStep{
    58  			resource.TestStep{
    59  				Config: testAccAWSAutoscalingScheduleConfig_zeroValues,
    60  				Check: resource.ComposeTestCheckFunc(
    61  					testAccCheckScalingScheduleExists("aws_autoscaling_schedule.foobar", &schedule),
    62  				),
    63  			},
    64  		},
    65  	})
    66  }
    67  
    68  func testAccCheckScalingScheduleExists(n string, policy *autoscaling.ScheduledUpdateGroupAction) resource.TestCheckFunc {
    69  	return func(s *terraform.State) error {
    70  		rs, ok := s.RootModule().Resources[n]
    71  		if !ok {
    72  			return fmt.Errorf("Not found: %s", n)
    73  		}
    74  
    75  		autoScalingGroup, _ := rs.Primary.Attributes["autoscaling_group_name"]
    76  		conn := testAccProvider.Meta().(*AWSClient).autoscalingconn
    77  		params := &autoscaling.DescribeScheduledActionsInput{
    78  			AutoScalingGroupName: aws.String(autoScalingGroup),
    79  			ScheduledActionNames: []*string{aws.String(rs.Primary.ID)},
    80  		}
    81  
    82  		resp, err := conn.DescribeScheduledActions(params)
    83  		if err != nil {
    84  			return err
    85  		}
    86  		if len(resp.ScheduledUpdateGroupActions) == 0 {
    87  			return fmt.Errorf("Scaling Schedule not found")
    88  		}
    89  
    90  		return nil
    91  	}
    92  }
    93  
    94  func testAccCheckAWSAutoscalingScheduleDestroy(s *terraform.State) error {
    95  	conn := testAccProvider.Meta().(*AWSClient).autoscalingconn
    96  
    97  	for _, rs := range s.RootModule().Resources {
    98  		if rs.Type != "aws_autoscaling_schedule" {
    99  			continue
   100  		}
   101  
   102  		autoScalingGroup, _ := rs.Primary.Attributes["autoscaling_group_name"]
   103  		params := &autoscaling.DescribeScheduledActionsInput{
   104  			AutoScalingGroupName: aws.String(autoScalingGroup),
   105  			ScheduledActionNames: []*string{aws.String(rs.Primary.ID)},
   106  		}
   107  
   108  		resp, err := conn.DescribeScheduledActions(params)
   109  
   110  		if err == nil {
   111  			if len(resp.ScheduledUpdateGroupActions) != 0 &&
   112  				*resp.ScheduledUpdateGroupActions[0].ScheduledActionName == rs.Primary.ID {
   113  				return fmt.Errorf("Scaling Schedule Still Exists: %s", rs.Primary.ID)
   114  			}
   115  		}
   116  	}
   117  
   118  	return nil
   119  }
   120  
   121  var testAccAWSAutoscalingScheduleConfig = fmt.Sprintf(`
   122  resource "aws_launch_configuration" "foobar" {
   123      name = "terraform-test-foobar5"
   124      image_id = "ami-21f78e11"
   125      instance_type = "t1.micro"
   126  }
   127  
   128  resource "aws_autoscaling_group" "foobar" {
   129      availability_zones = ["us-west-2a"]
   130      name = "terraform-test-foobar5"
   131      max_size = 1
   132      min_size = 1
   133      health_check_grace_period = 300
   134      health_check_type = "ELB"
   135      force_delete = true
   136      termination_policies = ["OldestInstance"]
   137      launch_configuration = "${aws_launch_configuration.foobar.name}"
   138      tag {
   139          key = "Foo"
   140          value = "foo-bar"
   141          propagate_at_launch = true
   142      }
   143  }
   144  
   145  resource "aws_autoscaling_schedule" "foobar" {
   146      scheduled_action_name = "foobar"
   147      min_size = 0
   148      max_size = 1
   149      desired_capacity = 0
   150      start_time = "2016-12-11T18:00:00Z"
   151      end_time = "2016-12-12T06:00:00Z"
   152      autoscaling_group_name = "${aws_autoscaling_group.foobar.name}"
   153  }
   154  `)
   155  
   156  var testAccAWSAutoscalingScheduleConfig_recurrence = fmt.Sprintf(`
   157  resource "aws_launch_configuration" "foobar" {
   158      name = "terraform-test-foobar5"
   159      image_id = "ami-21f78e11"
   160      instance_type = "t1.micro"
   161  }
   162  
   163  resource "aws_autoscaling_group" "foobar" {
   164      availability_zones = ["us-west-2a"]
   165      name = "terraform-test-foobar5"
   166      max_size = 1
   167      min_size = 1
   168      health_check_grace_period = 300
   169      health_check_type = "ELB"
   170      force_delete = true
   171      termination_policies = ["OldestInstance"]
   172      launch_configuration = "${aws_launch_configuration.foobar.name}"
   173      tag {
   174          key = "Foo"
   175          value = "foo-bar"
   176          propagate_at_launch = true
   177      }
   178  }
   179  
   180  resource "aws_autoscaling_schedule" "foobar" {
   181      scheduled_action_name = "foobar"
   182      min_size = 0
   183      max_size = 1
   184      desired_capacity = 0
   185      recurrence = "0 8 * * *"
   186      autoscaling_group_name = "${aws_autoscaling_group.foobar.name}"
   187  }
   188  `)
   189  
   190  var testAccAWSAutoscalingScheduleConfig_zeroValues = fmt.Sprintf(`
   191  resource "aws_launch_configuration" "foobar" {
   192      name = "terraform-test-foobar5"
   193      image_id = "ami-21f78e11"
   194      instance_type = "t1.micro"
   195  }
   196  
   197  resource "aws_autoscaling_group" "foobar" {
   198      availability_zones = ["us-west-2a"]
   199      name = "terraform-test-foobar5"
   200      max_size = 1
   201      min_size = 1
   202      health_check_grace_period = 300
   203      health_check_type = "ELB"
   204      force_delete = true
   205      termination_policies = ["OldestInstance"]
   206      launch_configuration = "${aws_launch_configuration.foobar.name}"
   207      tag {
   208          key = "Foo"
   209          value = "foo-bar"
   210          propagate_at_launch = true
   211      }
   212  }
   213  
   214  resource "aws_autoscaling_schedule" "foobar" {
   215      scheduled_action_name = "foobar"
   216      max_size = 0
   217      min_size = 0
   218      desired_capacity = 0
   219      start_time = "2018-01-16T07:00:00Z"
   220      end_time = "2018-01-16T13:00:00Z"
   221      autoscaling_group_name = "${aws_autoscaling_group.foobar.name}"
   222  }
   223  `)