github.com/ricardclau/terraform@v0.6.17-0.20160519222547-283e3ae6b5a9/builtin/providers/aws/resource_aws_autoscaling_policy_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 TestAccAWSAutoscalingPolicy_basic(t *testing.T) {
    15  	var policy autoscaling.ScalingPolicy
    16  
    17  	resource.Test(t, resource.TestCase{
    18  		PreCheck:     func() { testAccPreCheck(t) },
    19  		Providers:    testAccProviders,
    20  		CheckDestroy: testAccCheckAWSAutoscalingPolicyDestroy,
    21  		Steps: []resource.TestStep{
    22  			resource.TestStep{
    23  				Config: testAccAWSAutoscalingPolicyConfig,
    24  				Check: resource.ComposeTestCheckFunc(
    25  					testAccCheckScalingPolicyExists("aws_autoscaling_policy.foobar_simple", &policy),
    26  					resource.TestCheckResourceAttr("aws_autoscaling_policy.foobar_simple", "adjustment_type", "ChangeInCapacity"),
    27  					resource.TestCheckResourceAttr("aws_autoscaling_policy.foobar_simple", "policy_type", "SimpleScaling"),
    28  					resource.TestCheckResourceAttr("aws_autoscaling_policy.foobar_simple", "cooldown", "300"),
    29  					resource.TestCheckResourceAttr("aws_autoscaling_policy.foobar_simple", "name", "foobar_simple"),
    30  					resource.TestCheckResourceAttr("aws_autoscaling_policy.foobar_simple", "scaling_adjustment", "2"),
    31  					resource.TestCheckResourceAttr("aws_autoscaling_policy.foobar_simple", "autoscaling_group_name", "terraform-test-foobar5"),
    32  					testAccCheckScalingPolicyExists("aws_autoscaling_policy.foobar_step", &policy),
    33  					resource.TestCheckResourceAttr("aws_autoscaling_policy.foobar_step", "adjustment_type", "ChangeInCapacity"),
    34  					resource.TestCheckResourceAttr("aws_autoscaling_policy.foobar_step", "policy_type", "StepScaling"),
    35  					resource.TestCheckResourceAttr("aws_autoscaling_policy.foobar_step", "name", "foobar_step"),
    36  					resource.TestCheckResourceAttr("aws_autoscaling_policy.foobar_step", "metric_aggregation_type", "Minimum"),
    37  					resource.TestCheckResourceAttr("aws_autoscaling_policy.foobar_step", "estimated_instance_warmup", "200"),
    38  					resource.TestCheckResourceAttr("aws_autoscaling_policy.foobar_step", "autoscaling_group_name", "terraform-test-foobar5"),
    39  				),
    40  			},
    41  		},
    42  	})
    43  }
    44  
    45  func TestAccAWSAutoscalingPolicy_upgrade(t *testing.T) {
    46  	var policy autoscaling.ScalingPolicy
    47  
    48  	name := acctest.RandString(5)
    49  
    50  	resource.Test(t, resource.TestCase{
    51  		PreCheck:     func() { testAccPreCheck(t) },
    52  		Providers:    testAccProviders,
    53  		CheckDestroy: testAccCheckAWSAutoscalingPolicyDestroy,
    54  		Steps: []resource.TestStep{
    55  			resource.TestStep{
    56  				Config: testAccAWSAutoscalingPolicyConfig_upgrade_614(name),
    57  				Check: resource.ComposeTestCheckFunc(
    58  					testAccCheckScalingPolicyExists("aws_autoscaling_policy.foobar_simple", &policy),
    59  					resource.TestCheckResourceAttr("aws_autoscaling_policy.foobar_simple", "min_adjustment_step", "0"),
    60  					resource.TestCheckResourceAttr("aws_autoscaling_policy.foobar_simple", "min_adjustment_magnitude", "1"),
    61  				),
    62  				ExpectNonEmptyPlan: true,
    63  			},
    64  
    65  			resource.TestStep{
    66  				Config: testAccAWSAutoscalingPolicyConfig_upgrade_615(name),
    67  				Check: resource.ComposeTestCheckFunc(
    68  					testAccCheckScalingPolicyExists("aws_autoscaling_policy.foobar_simple", &policy),
    69  					resource.TestCheckResourceAttr("aws_autoscaling_policy.foobar_simple", "min_adjustment_step", "0"),
    70  					resource.TestCheckResourceAttr("aws_autoscaling_policy.foobar_simple", "min_adjustment_magnitude", "1"),
    71  				),
    72  			},
    73  		},
    74  	})
    75  }
    76  
    77  func testAccCheckScalingPolicyExists(n string, policy *autoscaling.ScalingPolicy) resource.TestCheckFunc {
    78  	return func(s *terraform.State) error {
    79  		rs, ok := s.RootModule().Resources[n]
    80  		if !ok {
    81  			return fmt.Errorf("Not found: %s", n)
    82  		}
    83  
    84  		conn := testAccProvider.Meta().(*AWSClient).autoscalingconn
    85  		params := &autoscaling.DescribePoliciesInput{
    86  			AutoScalingGroupName: aws.String(rs.Primary.Attributes["autoscaling_group_name"]),
    87  			PolicyNames:          []*string{aws.String(rs.Primary.ID)},
    88  		}
    89  		resp, err := conn.DescribePolicies(params)
    90  		if err != nil {
    91  			return err
    92  		}
    93  		if len(resp.ScalingPolicies) == 0 {
    94  			return fmt.Errorf("ScalingPolicy not found")
    95  		}
    96  
    97  		return nil
    98  	}
    99  }
   100  
   101  func testAccCheckAWSAutoscalingPolicyDestroy(s *terraform.State) error {
   102  	conn := testAccProvider.Meta().(*AWSClient).autoscalingconn
   103  
   104  	for _, rs := range s.RootModule().Resources {
   105  		if rs.Type != "aws_autoscaling_group" {
   106  			continue
   107  		}
   108  
   109  		params := autoscaling.DescribePoliciesInput{
   110  			AutoScalingGroupName: aws.String(rs.Primary.Attributes["autoscaling_group_name"]),
   111  			PolicyNames:          []*string{aws.String(rs.Primary.ID)},
   112  		}
   113  
   114  		resp, err := conn.DescribePolicies(&params)
   115  
   116  		if err == nil {
   117  			if len(resp.ScalingPolicies) != 0 &&
   118  				*resp.ScalingPolicies[0].PolicyName == rs.Primary.ID {
   119  				return fmt.Errorf("Scaling Policy Still Exists: %s", rs.Primary.ID)
   120  			}
   121  		}
   122  	}
   123  
   124  	return nil
   125  }
   126  
   127  var testAccAWSAutoscalingPolicyConfig = fmt.Sprintf(`
   128  resource "aws_launch_configuration" "foobar" {
   129  	name = "terraform-test-foobar5"
   130  	image_id = "ami-21f78e11"
   131  	instance_type = "t1.micro"
   132  }
   133  
   134  resource "aws_autoscaling_group" "foobar" {
   135  	availability_zones = ["us-west-2a"]
   136  	name = "terraform-test-foobar5"
   137  	max_size = 5
   138  	min_size = 2
   139  	health_check_grace_period = 300
   140  	health_check_type = "ELB"
   141  	force_delete = true
   142  	termination_policies = ["OldestInstance"]
   143  	launch_configuration = "${aws_launch_configuration.foobar.name}"
   144  	tag {
   145  		key = "Foo"
   146  		value = "foo-bar"
   147  		propagate_at_launch = true
   148  	}
   149  }
   150  
   151  resource "aws_autoscaling_policy" "foobar_simple" {
   152  	name = "foobar_simple"
   153  	adjustment_type = "ChangeInCapacity"
   154  	cooldown = 300
   155  	policy_type = "SimpleScaling"
   156  	scaling_adjustment = 2
   157  	autoscaling_group_name = "${aws_autoscaling_group.foobar.name}"
   158  }
   159  
   160  resource "aws_autoscaling_policy" "foobar_step" {
   161  	name = "foobar_step"
   162  	adjustment_type = "ChangeInCapacity"
   163  	policy_type = "StepScaling"
   164  	estimated_instance_warmup = 200
   165  	metric_aggregation_type = "Minimum"
   166  	step_adjustment {
   167  		scaling_adjustment = 1
   168  		metric_interval_lower_bound = 2.0
   169  	}
   170  	autoscaling_group_name = "${aws_autoscaling_group.foobar.name}"
   171  }
   172  `)
   173  
   174  func testAccAWSAutoscalingPolicyConfig_upgrade_614(name string) string {
   175  	return fmt.Sprintf(`
   176  resource "aws_launch_configuration" "foobar" {
   177    name          = "tf-test-%s"
   178    image_id      = "ami-21f78e11"
   179    instance_type = "t1.micro"
   180  }
   181  
   182  resource "aws_autoscaling_group" "foobar" {
   183    availability_zones        = ["us-west-2a"]
   184    name                      = "terraform-test-%s"
   185    max_size                  = 5
   186    min_size                  = 1
   187    health_check_grace_period = 300
   188    health_check_type         = "ELB"
   189    force_delete              = true
   190    termination_policies      = ["OldestInstance"]
   191    launch_configuration      = "${aws_launch_configuration.foobar.name}"
   192  
   193    tag {
   194      key                 = "Foo"
   195      value               = "foo-bar"
   196      propagate_at_launch = true
   197    }
   198  }
   199  
   200  resource "aws_autoscaling_policy" "foobar_simple" {
   201    name                   = "foobar_simple_%s"
   202    adjustment_type        = "PercentChangeInCapacity"
   203    cooldown               = 300
   204    policy_type            = "SimpleScaling"
   205    scaling_adjustment     = 2
   206    min_adjustment_step    = 1
   207    autoscaling_group_name = "${aws_autoscaling_group.foobar.name}"
   208  }
   209  `, name, name, name)
   210  }
   211  
   212  func testAccAWSAutoscalingPolicyConfig_upgrade_615(name string) string {
   213  	return fmt.Sprintf(`
   214  resource "aws_launch_configuration" "foobar" {
   215    name          = "tf-test-%s"
   216    image_id      = "ami-21f78e11"
   217    instance_type = "t1.micro"
   218  }
   219  
   220  resource "aws_autoscaling_group" "foobar" {
   221    availability_zones        = ["us-west-2a"]
   222    name                      = "terraform-test-%s"
   223    max_size                  = 5
   224    min_size                  = 1
   225    health_check_grace_period = 300
   226    health_check_type         = "ELB"
   227    force_delete              = true
   228    termination_policies      = ["OldestInstance"]
   229    launch_configuration      = "${aws_launch_configuration.foobar.name}"
   230  
   231    tag {
   232      key                 = "Foo"
   233      value               = "foo-bar"
   234      propagate_at_launch = true
   235    }
   236  }
   237  
   238  resource "aws_autoscaling_policy" "foobar_simple" {
   239    name                     = "foobar_simple_%s"
   240    adjustment_type          = "PercentChangeInCapacity"
   241    cooldown                 = 300
   242    policy_type              = "SimpleScaling"
   243    scaling_adjustment       = 2
   244    min_adjustment_magnitude = 1
   245    autoscaling_group_name   = "${aws_autoscaling_group.foobar.name}"
   246  }
   247  `, name, name, name)
   248  }