Skip to content

Commit

Permalink
resource/aws_autoscaling_schedule: Remove hardcoded start/end times f…
Browse files Browse the repository at this point in the history
…rom acceptance testing
  • Loading branch information
bflad committed Jan 17, 2018
1 parent 85b7eeb commit 40d2a4e
Showing 1 changed file with 37 additions and 32 deletions.
69 changes: 37 additions & 32 deletions aws/resource_aws_autoscaling_schedule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,8 @@ import (
func TestAccAWSAutoscalingSchedule_basic(t *testing.T) {
var schedule autoscaling.ScheduledUpdateGroupAction
rName := fmt.Sprintf("tf-test-%d", acctest.RandInt())
n := time.Now().UTC()
d, err := time.ParseDuration("2h")
if err != nil {
t.Fatalf("err parsing time duration: %s", err)
}
s, err := time.ParseDuration("1h")
if err != nil {
t.Fatalf("err parsing time duration: %s", err)
}
start := n.Add(s).Format(awsAutoscalingScheduleTimeLayout)
end := n.Add(d).Format(awsAutoscalingScheduleTimeLayout)
start := testAccAWSAutoscalingScheduleValidStart(t)
end := testAccAWSAutoscalingScheduleValidEnd(t)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand All @@ -45,17 +36,8 @@ func TestAccAWSAutoscalingSchedule_basic(t *testing.T) {
func TestAccAWSAutoscalingSchedule_disappears(t *testing.T) {
var schedule autoscaling.ScheduledUpdateGroupAction
rName := fmt.Sprintf("tf-test-%d", acctest.RandInt())
n := time.Now().UTC()
d, err := time.ParseDuration("2h")
if err != nil {
t.Fatalf("err parsing time duration: %s", err)
}
s, err := time.ParseDuration("1h")
if err != nil {
t.Fatalf("err parsing time duration: %s", err)
}
start := n.Add(s).Format(awsAutoscalingScheduleTimeLayout)
end := n.Add(d).Format(awsAutoscalingScheduleTimeLayout)
start := testAccAWSAutoscalingScheduleValidStart(t)
end := testAccAWSAutoscalingScheduleValidEnd(t)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand Down Expand Up @@ -112,13 +94,16 @@ func TestAccAWSAutoscalingSchedule_zeroValues(t *testing.T) {
var schedule autoscaling.ScheduledUpdateGroupAction

rName := fmt.Sprintf("tf-test-%d", acctest.RandInt())
start := testAccAWSAutoscalingScheduleValidStart(t)
end := testAccAWSAutoscalingScheduleValidEnd(t)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSAutoscalingScheduleDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSAutoscalingScheduleConfig_zeroValues(rName),
Config: testAccAWSAutoscalingScheduleConfig_zeroValues(rName, start, end),
Check: resource.ComposeTestCheckFunc(
testAccCheckScalingScheduleExists("aws_autoscaling_schedule.foobar", &schedule),
),
Expand All @@ -131,13 +116,16 @@ func TestAccAWSAutoscalingSchedule_negativeOne(t *testing.T) {
var schedule autoscaling.ScheduledUpdateGroupAction

rName := fmt.Sprintf("tf-test-%d", acctest.RandInt())
start := testAccAWSAutoscalingScheduleValidStart(t)
end := testAccAWSAutoscalingScheduleValidEnd(t)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSAutoscalingScheduleDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSAutoscalingScheduleConfig_negativeOne(rName),
Config: testAccAWSAutoscalingScheduleConfig_negativeOne(rName, start, end),
Check: resource.ComposeTestCheckFunc(
testAccCheckScalingScheduleExists("aws_autoscaling_schedule.foobar", &schedule),
testAccCheckScalingScheduleHasNoDesiredCapacity(&schedule),
Expand All @@ -148,6 +136,23 @@ func TestAccAWSAutoscalingSchedule_negativeOne(t *testing.T) {
})
}

func testAccAWSAutoscalingScheduleValidEnd(t *testing.T) string {
return testAccAWSAutoscalingScheduleTime(t, "2h")
}

func testAccAWSAutoscalingScheduleValidStart(t *testing.T) string {
return testAccAWSAutoscalingScheduleTime(t, "1h")
}

func testAccAWSAutoscalingScheduleTime(t *testing.T, duration string) string {
n := time.Now().UTC()
d, err := time.ParseDuration(duration)
if err != nil {
t.Fatalf("err parsing time duration: %s", err)
}
return n.Add(d).Format(awsAutoscalingScheduleTimeLayout)
}

func testAccCheckScalingScheduleExists(n string, policy *autoscaling.ScheduledUpdateGroupAction) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
Expand Down Expand Up @@ -285,7 +290,7 @@ resource "aws_autoscaling_schedule" "foobar" {
}`, r, r)
}

func testAccAWSAutoscalingScheduleConfig_zeroValues(r string) string {
func testAccAWSAutoscalingScheduleConfig_zeroValues(r, start, end string) string {
return fmt.Sprintf(`
resource "aws_launch_configuration" "foobar" {
name = "%s"
Expand Down Expand Up @@ -315,13 +320,13 @@ resource "aws_autoscaling_schedule" "foobar" {
max_size = 0
min_size = 0
desired_capacity = 0
start_time = "2018-01-16T07:00:00Z"
end_time = "2018-01-16T13:00:00Z"
start_time = "%s"
end_time = "%s"
autoscaling_group_name = "${aws_autoscaling_group.foobar.name}"
}`, r, r)
}`, r, r, start, end)
}

func testAccAWSAutoscalingScheduleConfig_negativeOne(r string) string {
func testAccAWSAutoscalingScheduleConfig_negativeOne(r, start, end string) string {
return fmt.Sprintf(`
resource "aws_launch_configuration" "foobar" {
name = "%s"
Expand Down Expand Up @@ -351,8 +356,8 @@ resource "aws_autoscaling_schedule" "foobar" {
max_size = 3
min_size = 1
desired_capacity = -1
start_time = "2018-01-16T07:00:00Z"
end_time = "2018-01-16T13:00:00Z"
start_time = "%s"
end_time = "%s"
autoscaling_group_name = "${aws_autoscaling_group.foobar.name}"
}`, r, r)
}`, r, r, start, end)
}

0 comments on commit 40d2a4e

Please sign in to comment.