From 6f2092100573915f8589aa7c3f872eafc830167c Mon Sep 17 00:00:00 2001 From: Daniel Rieske Date: Sun, 14 Jul 2024 15:03:13 +0200 Subject: [PATCH] fix: `TestAccEC2AMIIDsDataSource_sorted` by adding relative creation date filter --- .../ec2/ec2_ami_ids_data_source_test.go | 35 ++++++++++++------- 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/internal/service/ec2/ec2_ami_ids_data_source_test.go b/internal/service/ec2/ec2_ami_ids_data_source_test.go index b1307c336ed..f69065a11ee 100644 --- a/internal/service/ec2/ec2_ami_ids_data_source_test.go +++ b/internal/service/ec2/ec2_ami_ids_data_source_test.go @@ -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" @@ -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), ), }, }, @@ -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] } } @@ -120,7 +131,7 @@ data "aws_ami_ids" "test" { sort_ascending = %[1]t } -`, sortAscending) +`, sortAscending, creationDate) } func testAccAMIIDsDataSourceConfig_includeDeprecated(includeDeprecated bool) string {