Skip to content

Commit 2cf6afa

Browse files
committed
aws_autoscaling_policy: Add tests for StepScaling policies.
1 parent 305a450 commit 2cf6afa

2 files changed

Lines changed: 102 additions & 26 deletions

File tree

builtin/providers/aws/resource_aws_autoscaling_policy_test.go

Lines changed: 51 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,20 @@ func TestAccAWSAutoscalingPolicy_basic(t *testing.T) {
2121
resource.TestStep{
2222
Config: testAccAWSAutoscalingPolicyConfig,
2323
Check: resource.ComposeTestCheckFunc(
24-
testAccCheckScalingPolicyExists("aws_autoscaling_policy.foobar", &policy),
25-
resource.TestCheckResourceAttr("aws_autoscaling_policy.foobar", "adjustment_type", "ChangeInCapacity"),
26-
resource.TestCheckResourceAttr("aws_autoscaling_policy.foobar", "cooldown", "300"),
24+
testAccCheckScalingPolicyExists("aws_autoscaling_policy.foobar_simple", &policy),
25+
resource.TestCheckResourceAttr("aws_autoscaling_policy.foobar_simple", "adjustment_type", "ChangeInCapacity"),
26+
resource.TestCheckResourceAttr("aws_autoscaling_policy.foobar_simple", "policy_type", "SimpleScaling"),
27+
resource.TestCheckResourceAttr("aws_autoscaling_policy.foobar_simple", "cooldown", "300"),
28+
resource.TestCheckResourceAttr("aws_autoscaling_policy.foobar_simple", "name", "foobar_simple"),
29+
resource.TestCheckResourceAttr("aws_autoscaling_policy.foobar_simple", "scaling_adjustment", "2"),
30+
resource.TestCheckResourceAttr("aws_autoscaling_policy.foobar_simple", "autoscaling_group_name", "terraform-test-foobar5"),
31+
testAccCheckScalingPolicyExists("aws_autoscaling_policy.foobar_step", &policy),
32+
resource.TestCheckResourceAttr("aws_autoscaling_policy.foobar_step", "adjustment_type", "ChangeInCapacity"),
33+
resource.TestCheckResourceAttr("aws_autoscaling_policy.foobar_step", "policy_type", "StepScaling"),
34+
resource.TestCheckResourceAttr("aws_autoscaling_policy.foobar_step", "name", "foobar_step"),
35+
resource.TestCheckResourceAttr("aws_autoscaling_policy.foobar_step", "metric_aggregation_type", "Minimum"),
36+
resource.TestCheckResourceAttr("aws_autoscaling_policy.foobar_step", "estimated_instance_warmup", "200"),
37+
resource.TestCheckResourceAttr("aws_autoscaling_policy.foobar_step", "autoscaling_group_name", "terraform-test-foobar5"),
2738
),
2839
},
2940
},
@@ -82,33 +93,47 @@ func testAccCheckAWSAutoscalingPolicyDestroy(s *terraform.State) error {
8293

8394
var testAccAWSAutoscalingPolicyConfig = fmt.Sprintf(`
8495
resource "aws_launch_configuration" "foobar" {
85-
name = "terraform-test-foobar5"
86-
image_id = "ami-21f78e11"
87-
instance_type = "t1.micro"
96+
name = "terraform-test-foobar5"
97+
image_id = "ami-21f78e11"
98+
instance_type = "t1.micro"
8899
}
89100
90101
resource "aws_autoscaling_group" "foobar" {
91-
availability_zones = ["us-west-2a"]
92-
name = "terraform-test-foobar5"
93-
max_size = 5
94-
min_size = 2
95-
health_check_grace_period = 300
96-
health_check_type = "ELB"
97-
force_delete = true
98-
termination_policies = ["OldestInstance"]
99-
launch_configuration = "${aws_launch_configuration.foobar.name}"
100-
tag {
101-
key = "Foo"
102-
value = "foo-bar"
103-
propagate_at_launch = true
104-
}
102+
availability_zones = ["us-west-2a"]
103+
name = "terraform-test-foobar5"
104+
max_size = 5
105+
min_size = 2
106+
health_check_grace_period = 300
107+
health_check_type = "ELB"
108+
force_delete = true
109+
termination_policies = ["OldestInstance"]
110+
launch_configuration = "${aws_launch_configuration.foobar.name}"
111+
tag {
112+
key = "Foo"
113+
value = "foo-bar"
114+
propagate_at_launch = true
115+
}
116+
}
117+
118+
resource "aws_autoscaling_policy" "foobar_simple" {
119+
name = "foobar_simple"
120+
adjustment_type = "ChangeInCapacity"
121+
cooldown = 300
122+
policy_type = "SimpleScaling"
123+
scaling_adjustment = 2
124+
autoscaling_group_name = "${aws_autoscaling_group.foobar.name}"
105125
}
106126
107-
resource "aws_autoscaling_policy" "foobar" {
108-
name = "foobar"
109-
scaling_adjustment = 4
110-
adjustment_type = "ChangeInCapacity"
111-
cooldown = 300
112-
autoscaling_group_name = "${aws_autoscaling_group.foobar.name}"
127+
resource "aws_autoscaling_policy" "foobar_step" {
128+
name = "foobar_step"
129+
adjustment_type = "ChangeInCapacity"
130+
policy_type = "StepScaling"
131+
estimated_instance_warmup = 200
132+
metric_aggregation_type = "Minimum"
133+
step_adjustment {
134+
scaling_adjustment = 1
135+
metric_interval_lower_bound = 2.0
136+
}
137+
autoscaling_group_name = "${aws_autoscaling_group.foobar.name}"
113138
}
114139
`)

builtin/providers/aws/structure_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,33 @@ func TestexpandElasticacheParameters(t *testing.T) {
526526
}
527527
}
528528

529+
func TestExpandStepAdjustments(t *testing.T) {
530+
expanded := []interface{}{
531+
map[string]interface{}{
532+
"metric_interval_lower_bound": "1.0",
533+
"metric_interval_upper_bound": "2.0",
534+
"scaling_adjustment": 1,
535+
},
536+
}
537+
parameters, err := expandStepAdjustments(expanded)
538+
if err != nil {
539+
t.Fatalf("bad: %#v", err)
540+
}
541+
542+
expected := &autoscaling.StepAdjustment{
543+
MetricIntervalLowerBound: aws.Float64(1.0),
544+
MetricIntervalUpperBound: aws.Float64(2.0),
545+
ScalingAdjustment: aws.Int64(int64(1)),
546+
}
547+
548+
if !reflect.DeepEqual(parameters[0], expected) {
549+
t.Fatalf(
550+
"Got:\n\n%#v\n\nExpected:\n\n%#v\n",
551+
parameters[0],
552+
expected)
553+
}
554+
}
555+
529556
func TestFlattenParameters(t *testing.T) {
530557
cases := []struct {
531558
Input []*rds.Parameter
@@ -728,6 +755,30 @@ func TestFlattenAttachment(t *testing.T) {
728755
}
729756
}
730757

758+
func TestflattenStepAdjustments(t *testing.T) {
759+
expanded := []*autoscaling.StepAdjustment{
760+
&autoscaling.StepAdjustment{
761+
MetricIntervalLowerBound: aws.Float64(1.0),
762+
MetricIntervalUpperBound: aws.Float64(2.0),
763+
ScalingAdjustment: aws.Int64(int64(1)),
764+
},
765+
}
766+
767+
result := flattenStepAdjustments(expanded)[0]
768+
if result == nil {
769+
t.Fatal("expected result to have value, but got nil")
770+
}
771+
if result["metric_interval_lower_bound"] != float64(1.0) {
772+
t.Fatalf("expected metric_interval_lower_bound to be 1.0, but got %d", result["metric_interval_lower_bound"])
773+
}
774+
if result["metric_interval_upper_bound"] != float64(2.0) {
775+
t.Fatalf("expected metric_interval_upper_bound to be 1.0, but got %d", result["metric_interval_upper_bound"])
776+
}
777+
if result["scaling_adjustment"] != int64(1) {
778+
t.Fatalf("expected scaling_adjustment to be 1, but got %d", result["scaling_adjustment"])
779+
}
780+
}
781+
731782
func TestFlattenResourceRecords(t *testing.T) {
732783
expanded := []*route53.ResourceRecord{
733784
&route53.ResourceRecord{

0 commit comments

Comments
 (0)