Skip to content

Commit

Permalink
fix: TestAccEC2AMIIDsDataSource_sorted by adding relative creation …
Browse files Browse the repository at this point in the history
…date filter
  • Loading branch information
DanielRieske committed Jul 14, 2024
1 parent 20c813e commit 6f20921
Showing 1 changed file with 23 additions and 12 deletions.
35 changes: 23 additions & 12 deletions internal/service/ec2/ec2_ami_ids_data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package ec2_test
import (
"fmt"
"testing"
"time"

"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-provider-aws/internal/acctest"
Expand Down Expand Up @@ -35,25 +36,28 @@ func TestAccEC2AMIIDsDataSource_sorted(t *testing.T) {
ctx := acctest.Context(t)
datasourceName := "data.aws_ami_ids.test"

date := time.Now().UTC().AddDate(0, -2, 0)
creationDate := fmt.Sprintf("%d-%02d-*", date.Year(), date.Month())

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t) },
ErrorCheck: acctest.ErrorCheck(t, names.EC2ServiceID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccAMIIDsDataSourceConfig_sorted(false),
Config: testAccAMIIDsDataSourceConfig_sorted(false, creationDate),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(datasourceName, "ids.#", acctest.Ct2),
resource.TestCheckResourceAttrPair(datasourceName, "ids.0", "data.aws_ami.test2", names.AttrID),
resource.TestCheckResourceAttrPair(datasourceName, "ids.1", "data.aws_ami.test1", names.AttrID),
resource.TestCheckResourceAttrPair(datasourceName, "ids.0", "data.aws_ami.test1", names.AttrID),
resource.TestCheckResourceAttrPair(datasourceName, "ids.1", "data.aws_ami.test2", names.AttrID),
),
},
{
Config: testAccAMIIDsDataSourceConfig_sorted(true),
Config: testAccAMIIDsDataSourceConfig_sorted(true, creationDate),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(datasourceName, "ids.#", acctest.Ct2),
resource.TestCheckResourceAttrPair(datasourceName, "ids.0", "data.aws_ami.test1", names.AttrID),
resource.TestCheckResourceAttrPair(datasourceName, "ids.1", "data.aws_ami.test2", names.AttrID),
resource.TestCheckResourceAttrPair(datasourceName, "ids.0", "data.aws_ami.test2", names.AttrID),
resource.TestCheckResourceAttrPair(datasourceName, "ids.1", "data.aws_ami.test1", names.AttrID),
),
},
},
Expand Down Expand Up @@ -90,23 +94,30 @@ data "aws_ami_ids" "test" {
}
`

func testAccAMIIDsDataSourceConfig_sorted(sortAscending bool) string {
func testAccAMIIDsDataSourceConfig_sorted(sortAscending bool, creationDate string) string {
return fmt.Sprintf(`
data "aws_ami" "test1" {
owners = ["amazon"]
owners = ["amazon"]
most_recent = true
filter {
name = "name"
values = ["al2023-ami-2023.4.20240401.1-kernel-6.1-x86_64"]
values = ["al2023-ami-2023.*-x86_64"]
}
}
data "aws_ami" "test2" {
owners = ["amazon"]
owners = ["amazon"]
most_recent = true
filter {
name = "name"
values = ["al2023-ami-2023.4.20240513.0-kernel-6.1-x86_64"]
values = ["al2023-ami-2023.*-x86_64"]
}
filter {
name = "creation-date"
values = [%[2]q]
}
}
Expand All @@ -120,7 +131,7 @@ data "aws_ami_ids" "test" {
sort_ascending = %[1]t
}
`, sortAscending)
`, sortAscending, creationDate)
}

func testAccAMIIDsDataSourceConfig_includeDeprecated(includeDeprecated bool) string {
Expand Down

0 comments on commit 6f20921

Please sign in to comment.