From 7fe55016bdf10dfba93ad48e719c787fa711c4fc Mon Sep 17 00:00:00 2001 From: stephybun Date: Tue, 10 Dec 2024 16:47:39 +0100 Subject: [PATCH 1/4] simplify test config names --- contributing/topics/guide-new-resource.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/contributing/topics/guide-new-resource.md b/contributing/topics/guide-new-resource.md index 9844fbee91b7..70318179fd35 100755 --- a/contributing/topics/guide-new-resource.md +++ b/contributing/topics/guide-new-resource.md @@ -739,7 +739,7 @@ func (ResourceGroupExampleTestResource) Exists(ctx context.Context, client *clie return pointer.To(resp.Model != nil), nil } -func (ResourceGroupExampleTestResource) basicConfig(data acceptance.TestData) string { +func (ResourceGroupExampleTestResource) basic(data acceptance.TestData) string { return fmt.Sprintf(` provider "azurerm" { features {} @@ -764,7 +764,7 @@ resource "azurerm_resource_group_example" "import" { `, template) } -func (ResourceGroupExampleTestResource) completeConfig(data acceptance.TestData) string { +func (ResourceGroupExampleTestResource) complete(data acceptance.TestData) string { return fmt.Sprintf(` provider "azurerm" { features {} From 377a45ac79d3188ab27c24151c792f2b5d30cc61 Mon Sep 17 00:00:00 2001 From: Steph Date: Tue, 10 Dec 2024 16:55:40 +0100 Subject: [PATCH 2/4] update acceptance test structure --- contributing/topics/guide-new-resource.md | 63 +++++++++++++---------- 1 file changed, 37 insertions(+), 26 deletions(-) diff --git a/contributing/topics/guide-new-resource.md b/contributing/topics/guide-new-resource.md index 70318179fd35..21e395288ac5 100755 --- a/contributing/topics/guide-new-resource.md +++ b/contributing/topics/guide-new-resource.md @@ -681,47 +681,58 @@ In Go tests are expected to be in a file name in the format `{original_file_name package resource_test import ( - "context" - "fmt" - "testing" - - "github.com/hashicorp/terraform-provider-azurerm/internal/acceptance" - "github.com/hashicorp/terraform-provider-azurerm/internal/clients" - "github.com/hashicorp/terraform-provider-azurerm/internal/services/resource/parse" - "github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk" - "github.com/hashicorp/terraform-provider-azurerm/utils" + "context" + "fmt" + "testing" + + "github.com/hashicorp/terraform-provider-azurerm/internal/acceptance" + "github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/check" + "github.com/hashicorp/terraform-provider-azurerm/internal/clients" + "github.com/hashicorp/terraform-provider-azurerm/internal/services/resource/parse" + "github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk" + "github.com/hashicorp/terraform-provider-azurerm/utils" ) type ResourceGroupExampleTestResource struct{} func TestAccResourceGroupExample_basic(t *testing.T) { data := acceptance.BuildTestData(t, "azurerm_resource_group_example", "test") - testResource := ResourceGroupExampleTestResource{} - data.ResourceTest(t, testResource, []acceptance.TestStep{ - data.ApplyStep(testResource.basicConfig, testResource), + r := ResourceGroupExampleTestResource{} + + data.ResourceTest(t, r, []acceptance.TestStep{ + { + Config: r.basic(data), + Check: acceptance.ComposeTestCheckFunc( + check.That(data.ResourceName).ExistsInAzure(r) + ), + }, data.ImportStep(), }) } func TestAccResourceGroupExample_requiresImport(t *testing.T) { data := acceptance.BuildTestData(t, "azurerm_resource_group_example", "test") - testResource := ResourceGroupExampleTestResource{} - data.ResourceTest(t, testResource, []acceptance.TestStep{ - data.ApplyStep(testResource.basicConfig, testResource), - data.RequiresImportErrorStep(testResource.requiresImportConfig), + r := ResourceGroupExampleTestResource{} + + data.ResourceTest(t, r, []acceptance.TestStep{ + { + Config: r.basic(data), + ExpectError: acceptance.RequiresImportError("azurerm_resource_group_example"), + }, }) } func TestAccResourceGroupExample_complete(t *testing.T) { data := acceptance.BuildTestData(t, "azurerm_resource_group_example", "test") - testResource := ResourceGroupExampleTestResource{} - data.ResourceTest(t, testResource, []acceptance.TestStep{ - data.ApplyStep(testResource.completeConfig, testResource), - data.ImportStep(), - data.ApplyStep(testResource.basicConfig, testResource), - data.ImportStep(), - data.ApplyStep(testResource.completeConfig, testResource), - data.ImportStep(), + r := ResourceGroupExampleTestResource{} + + data.ResourceTest(t, r, []acceptance.TestStep{ + { + Config: r.complete(data), + Check: acceptance.ComposeTestCheckFunc( + check.That(data.ResourceName).ExistsInAzure(r) + ), + }, }) } @@ -752,8 +763,8 @@ resource "azurerm_resource_group_example" "test" { `, data.RandomInteger, data.Locations.Primary) } -func (r ResourceGroupExampleTestResource) requiresImportConfig(data acceptance.TestData) string { - template := r.basicConfig(data) +func (r ResourceGroupExampleTestResource) requiresImport(data acceptance.TestData) string { + template := r.basic(data) return fmt.Sprintf(` %s From afeeb83ab1c733b42ace7895f8d60ce9855abe07 Mon Sep 17 00:00:00 2001 From: Steph Date: Tue, 10 Dec 2024 16:56:23 +0100 Subject: [PATCH 3/4] add missing import step for complete test example --- contributing/topics/guide-new-resource.md | 1 + 1 file changed, 1 insertion(+) diff --git a/contributing/topics/guide-new-resource.md b/contributing/topics/guide-new-resource.md index 21e395288ac5..c746bc64e52b 100755 --- a/contributing/topics/guide-new-resource.md +++ b/contributing/topics/guide-new-resource.md @@ -733,6 +733,7 @@ func TestAccResourceGroupExample_complete(t *testing.T) { check.That(data.ResourceName).ExistsInAzure(r) ), }, + data.ImportStep(), }) } From a5b61bf368f2817ed95ed7989d271aa751ddf94e Mon Sep 17 00:00:00 2001 From: Steph Date: Tue, 10 Dec 2024 16:59:38 +0100 Subject: [PATCH 4/4] formatting --- contributing/topics/guide-new-resource.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contributing/topics/guide-new-resource.md b/contributing/topics/guide-new-resource.md index c746bc64e52b..c60f0967167e 100755 --- a/contributing/topics/guide-new-resource.md +++ b/contributing/topics/guide-new-resource.md @@ -704,7 +704,7 @@ func TestAccResourceGroupExample_basic(t *testing.T) { Config: r.basic(data), Check: acceptance.ComposeTestCheckFunc( check.That(data.ResourceName).ExistsInAzure(r) - ), + ), }, data.ImportStep(), })