Skip to content

Commit

Permalink
Skip tests when resource unavailable or entire resource is marked unk…
Browse files Browse the repository at this point in the history
…nown (#228)

* Adding Terraform version checks to avoid running tests using terraform_data prior to v1.4.x

* Adding Terraform version checks to avoid running tests when entire resource is marked as unknown prior to v1.3.0

* Adding comment to Terraform version checks to clarify usage.

* Using predefined tfversion package variables
  • Loading branch information
bendbennett authored Nov 29, 2023
1 parent 5f5c3b7 commit a91032e
Show file tree
Hide file tree
Showing 3 changed files with 195 additions and 0 deletions.
120 changes: 120 additions & 0 deletions plancheck/expect_null_output_value_at_path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,20 @@ import (
r "github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/plancheck"
"github.com/hashicorp/terraform-plugin-testing/tfjsonpath"
"github.com/hashicorp/terraform-plugin-testing/tfversion"
)

func Test_ExpectNullOutputValueAtPath_StringAttribute_EmptyConfig(t *testing.T) {
t.Parallel()

r.Test(t, r.TestCase{
// Prior to Terraform v1.3.0 a planned output is marked as fully unknown
// if any attribute is unknown. The id attribute within the test provider
// is unknown.
// Reference: https://github.com/hashicorp/terraform/blob/v1.3/CHANGELOG.md#130-september-21-2022
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
tfversion.SkipBelow(tfversion.Version1_3_0),
},
Steps: []r.TestStep{
{
ProviderFactories: map[string]func() (*schema.Provider, error){
Expand Down Expand Up @@ -46,6 +54,13 @@ func Test_ExpectNullOutputValueAtPath_StringAttribute_NullConfig(t *testing.T) {
t.Parallel()

r.Test(t, r.TestCase{
// Prior to Terraform v1.3.0 a planned output is marked as fully unknown
// if any attribute is unknown. The id attribute within the test provider
// is unknown.
// Reference: https://github.com/hashicorp/terraform/blob/v1.3/CHANGELOG.md#130-september-21-2022
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
tfversion.SkipBelow(tfversion.Version1_3_0),
},
Steps: []r.TestStep{
{
ProviderFactories: map[string]func() (*schema.Provider, error){
Expand Down Expand Up @@ -75,6 +90,13 @@ func Test_ExpectNullOutputValueAtPath_StringAttribute_ExpectErrorNotNull(t *test
t.Parallel()

r.Test(t, r.TestCase{
// Prior to Terraform v1.3.0 a planned output is marked as fully unknown
// if any attribute is unknown. The id attribute within the test provider
// is unknown.
// Reference: https://github.com/hashicorp/terraform/blob/v1.3/CHANGELOG.md#130-september-21-2022
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
tfversion.SkipBelow(tfversion.Version1_3_0),
},
Steps: []r.TestStep{
{
ProviderFactories: map[string]func() (*schema.Provider, error){
Expand Down Expand Up @@ -110,6 +132,13 @@ func Test_ExpectNullOutputValueAtPath_ListAttribute_EmptyConfig(t *testing.T) {
return testProvider(), nil
},
},
// Prior to Terraform v1.3.0 a planned output is marked as fully unknown
// if any attribute is unknown. The id attribute within the test provider
// is unknown.
// Reference: https://github.com/hashicorp/terraform/blob/v1.3/CHANGELOG.md#130-september-21-2022
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
tfversion.SkipBelow(tfversion.Version1_3_0),
},
Steps: []r.TestStep{
{
Config: `resource "test_resource" "test" {
Expand Down Expand Up @@ -138,6 +167,13 @@ func Test_ExpectNullOutputValueAtPath_ListAttribute_NullConfig(t *testing.T) {
return testProvider(), nil
},
},
// Prior to Terraform v1.3.0 a planned output is marked as fully unknown
// if any attribute is unknown. The id attribute within the test provider
// is unknown.
// Reference: https://github.com/hashicorp/terraform/blob/v1.3/CHANGELOG.md#130-september-21-2022
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
tfversion.SkipBelow(tfversion.Version1_3_0),
},
Steps: []r.TestStep{
{
Config: `resource "test_resource" "test" {
Expand Down Expand Up @@ -167,6 +203,13 @@ func Test_ExpectNullOutputValueAtPath_ListAttribute_ExpectErrorNotNull(t *testin
return testProvider(), nil
},
},
// Prior to Terraform v1.3.0 a planned output is marked as fully unknown
// if any attribute is unknown. The id attribute within the test provider
// is unknown.
// Reference: https://github.com/hashicorp/terraform/blob/v1.3/CHANGELOG.md#130-september-21-2022
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
tfversion.SkipBelow(tfversion.Version1_3_0),
},
Steps: []r.TestStep{
{
Config: `resource "test_resource" "test" {
Expand Down Expand Up @@ -197,6 +240,13 @@ func Test_ExpectNullOutputValueAtPath_SetAttribute_EmptyConfig(t *testing.T) {
return testProvider(), nil
},
},
// Prior to Terraform v1.3.0 a planned output is marked as fully unknown
// if any attribute is unknown. The id attribute within the test provider
// is unknown.
// Reference: https://github.com/hashicorp/terraform/blob/v1.3/CHANGELOG.md#130-september-21-2022
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
tfversion.SkipBelow(tfversion.Version1_3_0),
},
Steps: []r.TestStep{
{
Config: `resource "test_resource" "test" {
Expand Down Expand Up @@ -225,6 +275,13 @@ func Test_ExpectNullOutputValueAtPath_SetAttribute_NullConfig(t *testing.T) {
return testProvider(), nil
},
},
// Prior to Terraform v1.3.0 a planned output is marked as fully unknown
// if any attribute is unknown. The id attribute within the test provider
// is unknown.
// Reference: https://github.com/hashicorp/terraform/blob/v1.3/CHANGELOG.md#130-september-21-2022
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
tfversion.SkipBelow(tfversion.Version1_3_0),
},
Steps: []r.TestStep{
{
Config: `resource "test_resource" "test" {
Expand Down Expand Up @@ -254,6 +311,13 @@ func Test_ExpectNullOutputValueAtPath_SetAttribute_ExpectErrorNotNull(t *testing
return testProvider(), nil
},
},
// Prior to Terraform v1.3.0 a planned output is marked as fully unknown
// if any attribute is unknown. The id attribute within the test provider
// is unknown.
// Reference: https://github.com/hashicorp/terraform/blob/v1.3/CHANGELOG.md#130-september-21-2022
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
tfversion.SkipBelow(tfversion.Version1_3_0),
},
Steps: []r.TestStep{
{
Config: `resource "test_resource" "test" {
Expand Down Expand Up @@ -284,6 +348,13 @@ func Test_ExpectNullOutputValueAtPath_MapAttribute_EmptyConfig(t *testing.T) {
return testProvider(), nil
},
},
// Prior to Terraform v1.3.0 a planned output is marked as fully unknown
// if any attribute is unknown. The id attribute within the test provider
// is unknown.
// Reference: https://github.com/hashicorp/terraform/blob/v1.3/CHANGELOG.md#130-september-21-2022
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
tfversion.SkipBelow(tfversion.Version1_3_0),
},
Steps: []r.TestStep{
{
Config: `resource "test_resource" "test" {
Expand Down Expand Up @@ -312,6 +383,13 @@ func Test_ExpectNullOutputValueAtPath_MapAttribute_NullConfig(t *testing.T) {
return testProvider(), nil
},
},
// Prior to Terraform v1.3.0 a planned output is marked as fully unknown
// if any attribute is unknown. The id attribute within the test provider
// is unknown.
// Reference: https://github.com/hashicorp/terraform/blob/v1.3/CHANGELOG.md#130-september-21-2022
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
tfversion.SkipBelow(tfversion.Version1_3_0),
},
Steps: []r.TestStep{
{
Config: `resource "test_resource" "test" {
Expand Down Expand Up @@ -341,6 +419,13 @@ func Test_ExpectNullOutputValueAtPath_MapAttribute_ExpectErrorNotNull(t *testing
return testProvider(), nil
},
},
// Prior to Terraform v1.3.0 a planned output is marked as fully unknown
// if any attribute is unknown. The id attribute within the test provider
// is unknown.
// Reference: https://github.com/hashicorp/terraform/blob/v1.3/CHANGELOG.md#130-september-21-2022
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
tfversion.SkipBelow(tfversion.Version1_3_0),
},
Steps: []r.TestStep{
{
Config: `resource "test_resource" "test" {
Expand Down Expand Up @@ -374,6 +459,13 @@ func Test_ExpectNullOutputValueAtPath_MapAttribute_PartiallyNullConfig_ExpectErr
return testProvider(), nil
},
},
// Prior to Terraform v1.3.0 a planned output is marked as fully unknown
// if any attribute is unknown. The id attribute within the test provider
// is unknown.
// Reference: https://github.com/hashicorp/terraform/blob/v1.3/CHANGELOG.md#130-september-21-2022
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
tfversion.SkipBelow(tfversion.Version1_3_0),
},
Steps: []r.TestStep{
{
Config: `resource "test_resource" "test" {
Expand Down Expand Up @@ -407,6 +499,13 @@ func Test_ExpectNullOutputValueAtPath_ListNestedBlock_EmptyConfig(t *testing.T)
return testProvider(), nil
},
},
// Prior to Terraform v1.3.0 a planned output is marked as fully unknown
// if any attribute is unknown. The id attribute within the test provider
// is unknown.
// Reference: https://github.com/hashicorp/terraform/blob/v1.3/CHANGELOG.md#130-september-21-2022
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
tfversion.SkipBelow(tfversion.Version1_3_0),
},
Steps: []r.TestStep{
{
Config: `resource "test_resource" "test" {
Expand Down Expand Up @@ -436,6 +535,13 @@ func Test_ExpectNullOutputValueAtPath_ListNestedBlock_NullConfig(t *testing.T) {
return testProvider(), nil
},
},
// Prior to Terraform v1.3.0 a planned output is marked as fully unknown
// if any attribute is unknown. The id attribute within the test provider
// is unknown.
// Reference: https://github.com/hashicorp/terraform/blob/v1.3/CHANGELOG.md#130-september-21-2022
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
tfversion.SkipBelow(tfversion.Version1_3_0),
},
Steps: []r.TestStep{
{
Config: `resource "test_resource" "test" {
Expand Down Expand Up @@ -467,6 +573,13 @@ func Test_ExpectNullOutputValueAtPath_ListNestedBlock_ExpectErrorNotNull(t *test
return testProvider(), nil
},
},
// Prior to Terraform v1.3.0 a planned output is marked as fully unknown
// if any attribute is unknown. The id attribute within the test provider
// is unknown.
// Reference: https://github.com/hashicorp/terraform/blob/v1.3/CHANGELOG.md#130-september-21-2022
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
tfversion.SkipBelow(tfversion.Version1_3_0),
},
Steps: []r.TestStep{
{
Config: `resource "test_resource" "test" {
Expand Down Expand Up @@ -499,6 +612,13 @@ func Test_ExpectNullOutputValueAtPath_SetNestedBlock_NullConfig_ExpectErrorNotNu
return testProvider(), nil
},
},
// Prior to Terraform v1.3.0 a planned output is marked as fully unknown
// if any attribute is unknown. The id attribute within the test provider
// is unknown.
// Reference: https://github.com/hashicorp/terraform/blob/v1.3/CHANGELOG.md#130-september-21-2022
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
tfversion.SkipBelow(tfversion.Version1_3_0),
},
Steps: []r.TestStep{
{
Config: `resource "test_resource" "test" {
Expand Down
48 changes: 48 additions & 0 deletions plancheck/expect_unknown_output_value_at_path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
r "github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/plancheck"
"github.com/hashicorp/terraform-plugin-testing/tfjsonpath"
"github.com/hashicorp/terraform-plugin-testing/tfversion"
)

func Test_ExpectUnknownOutputValueAtPath_StringAttribute(t *testing.T) {
Expand All @@ -23,6 +24,11 @@ func Test_ExpectUnknownOutputValueAtPath_StringAttribute(t *testing.T) {
Source: "terraform.io/builtin/terraform",
},
},
// The terraform_data resource is not available prior to Terraform v1.4.0
// Reference: https://github.com/hashicorp/terraform/blob/v1.4/CHANGELOG.md#140-march-08-2023
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
tfversion.SkipBelow(tfversion.Version1_4_0),
},
Steps: []r.TestStep{
{
Config: `resource "terraform_data" "one" {
Expand Down Expand Up @@ -57,6 +63,11 @@ func Test_ExpectUnknownOutputValueAtPath_ListAttribute(t *testing.T) {
return testProvider(), nil
},
},
// The terraform_data resource is not available prior to Terraform v1.4.0
// Reference: https://github.com/hashicorp/terraform/blob/v1.4/CHANGELOG.md#140-march-08-2023
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
tfversion.SkipBelow(tfversion.Version1_4_0),
},
Steps: []r.TestStep{
{
Config: `resource "terraform_data" "one" {
Expand Down Expand Up @@ -95,6 +106,11 @@ func Test_ExpectUnknownOutputValueAtPath_SetAttribute(t *testing.T) {
return testProvider(), nil
},
},
// The terraform_data resource is not available prior to Terraform v1.4.0
// Reference: https://github.com/hashicorp/terraform/blob/v1.4/CHANGELOG.md#140-march-08-2023
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
tfversion.SkipBelow(tfversion.Version1_4_0),
},
Steps: []r.TestStep{
{
Config: `resource "terraform_data" "one" {
Expand Down Expand Up @@ -133,6 +149,11 @@ func Test_ExpectUnknownOutputValueAtPath_MapAttribute(t *testing.T) {
return testProvider(), nil
},
},
// The terraform_data resource is not available prior to Terraform v1.4.0
// Reference: https://github.com/hashicorp/terraform/blob/v1.4/CHANGELOG.md#140-march-08-2023
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
tfversion.SkipBelow(tfversion.Version1_4_0),
},
Steps: []r.TestStep{
{
Config: `resource "terraform_data" "one" {
Expand Down Expand Up @@ -174,6 +195,11 @@ func Test_ExpectUnknownOutputValueAtPath_ListNestedBlock_Resource(t *testing.T)
return testProvider(), nil
},
},
// The terraform_data resource is not available prior to Terraform v1.4.0
// Reference: https://github.com/hashicorp/terraform/blob/v1.4/CHANGELOG.md#140-march-08-2023
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
tfversion.SkipBelow(tfversion.Version1_4_0),
},
Steps: []r.TestStep{
{
Config: `resource "terraform_data" "one" {
Expand Down Expand Up @@ -214,6 +240,11 @@ func Test_ExpectUnknownOutputValueAtPath_ListNestedBlock_ResourceBlocks(t *testi
return testProvider(), nil
},
},
// The terraform_data resource is not available prior to Terraform v1.4.0
// Reference: https://github.com/hashicorp/terraform/blob/v1.4/CHANGELOG.md#140-march-08-2023
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
tfversion.SkipBelow(tfversion.Version1_4_0),
},
Steps: []r.TestStep{
{
Config: `resource "terraform_data" "one" {
Expand Down Expand Up @@ -254,6 +285,11 @@ func Test_ExpectUnknownOutputValueAtPath_ListNestedBlock_ObjectBlockIndex(t *tes
return testProvider(), nil
},
},
// The terraform_data resource is not available prior to Terraform v1.4.0
// Reference: https://github.com/hashicorp/terraform/blob/v1.4/CHANGELOG.md#140-march-08-2023
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
tfversion.SkipBelow(tfversion.Version1_4_0),
},
Steps: []r.TestStep{
{
Config: `resource "terraform_data" "one" {
Expand Down Expand Up @@ -294,6 +330,11 @@ func Test_ExpectUnknownOutputValueAtPath_SetNestedBlock_Object(t *testing.T) {
return testProvider(), nil
},
},
// The terraform_data resource is not available prior to Terraform v1.4.0
// Reference: https://github.com/hashicorp/terraform/blob/v1.4/CHANGELOG.md#140-march-08-2023
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
tfversion.SkipBelow(tfversion.Version1_4_0),
},
Steps: []r.TestStep{
{
Config: `resource "terraform_data" "one" {
Expand Down Expand Up @@ -329,6 +370,13 @@ func Test_ExpectUnknownOutputValueAtPath_ExpectError_KnownValue(t *testing.T) {
return testProvider(), nil
},
},
// Prior to Terraform v1.3.0 a planned output is marked as fully unknown
// if any attribute is unknown. The id attribute within the test provider
// is unknown.
// Reference: https://github.com/hashicorp/terraform/blob/v1.3/CHANGELOG.md#130-september-21-2022
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
tfversion.SkipBelow(tfversion.Version1_3_0),
},
Steps: []r.TestStep{
{
Config: `
Expand Down
Loading

0 comments on commit a91032e

Please sign in to comment.