From 598dac8246003c5c0aef3714f345493cfec3fc25 Mon Sep 17 00:00:00 2001 From: Benjamin Bennett Date: Wed, 10 Jan 2024 10:45:42 +0000 Subject: [PATCH] Modifying ExpectKnown to allow for checking of null values (#266) --- knownvalue/null.go | 34 +++ knownvalue/null_test.go | 61 ++++ statecheck/expect_known_output_value.go | 24 +- .../expect_known_output_value_at_path.go | 24 +- .../expect_known_output_value_at_path_test.go | 286 +----------------- statecheck/expect_known_output_value_test.go | 6 +- statecheck/expect_known_value.go | 26 +- statecheck/expect_known_value_test.go | 5 +- 8 files changed, 110 insertions(+), 356 deletions(-) create mode 100644 knownvalue/null.go create mode 100644 knownvalue/null_test.go diff --git a/knownvalue/null.go b/knownvalue/null.go new file mode 100644 index 000000000..93e773cb0 --- /dev/null +++ b/knownvalue/null.go @@ -0,0 +1,34 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + +package knownvalue + +import ( + "fmt" +) + +var _ Check = NullValue{} + +// NullValue is a Check for asserting equality between the value supplied +// to NullValueExact and the value passed to the CheckValue method. +type NullValue struct{} + +// CheckValue determines whether the passed value is of nil. +func (v NullValue) CheckValue(other any) error { + if other != nil { + return fmt.Errorf("expected value nil for NullValue check, got: %T", other) + } + + return nil +} + +// String returns the string representation of nil. +func (v NullValue) String() string { + return "nil" +} + +// NullValueExact returns a Check for asserting equality nil +// and the value passed to the CheckValue method. +func NullValueExact() NullValue { + return NullValue{} +} diff --git a/knownvalue/null_test.go b/knownvalue/null_test.go new file mode 100644 index 000000000..b185ca220 --- /dev/null +++ b/knownvalue/null_test.go @@ -0,0 +1,61 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + +package knownvalue_test + +import ( + "fmt" + "testing" + + "github.com/google/go-cmp/cmp" + + "github.com/hashicorp/terraform-plugin-testing/knownvalue" +) + +func TestNullValue_CheckValue(t *testing.T) { + t.Parallel() + + testCases := map[string]struct { + self knownvalue.NullValue + other any + expectedError error + }{ + "zero-nil": {}, + "zero-other": { + other: nil, // checking against the underlying value field zero-value + }, + "not-nil": { + self: knownvalue.NullValueExact(), + other: false, + expectedError: fmt.Errorf("expected value nil for NullValue check, got: bool"), + }, + "equal": { + self: knownvalue.NullValueExact(), + other: nil, + }, + } + + for name, testCase := range testCases { + name, testCase := name, testCase + + t.Run(name, func(t *testing.T) { + t.Parallel() + + got := testCase.self.CheckValue(testCase.other) + + if diff := cmp.Diff(got, testCase.expectedError, equateErrorMessage); diff != "" { + t.Errorf("unexpected difference: %s", diff) + } + }) + } +} + +func TestNullValue_String(t *testing.T) { + t.Parallel() + + got := knownvalue.NullValueExact().String() + + if diff := cmp.Diff(got, "nil"); diff != "" { + t.Errorf("unexpected difference: %s", diff) + } +} diff --git a/statecheck/expect_known_output_value.go b/statecheck/expect_known_output_value.go index 2809dff46..1cf6b69e8 100644 --- a/statecheck/expect_known_output_value.go +++ b/statecheck/expect_known_output_value.go @@ -6,7 +6,6 @@ package statecheck import ( "context" "fmt" - "reflect" tfjson "github.com/hashicorp/terraform-json" @@ -56,27 +55,8 @@ func (e expectKnownOutputValue) CheckState(ctx context.Context, req CheckStateRe return } - if result == nil { - resp.Error = fmt.Errorf("value is null") - - return - } - - switch reflect.TypeOf(result).Kind() { - case reflect.Bool, - reflect.Map, - reflect.Slice, - reflect.String: - if err := e.knownValue.CheckValue(result); err != nil { - resp.Error = err - - return - } - default: - errorStr := fmt.Sprintf("unrecognised output type: %T, known value type is %T", result, e.knownValue) - errorStr += "\n\nThis is an error in statecheck.ExpectKnownOutputValue.\nPlease report this to the maintainers." - - resp.Error = fmt.Errorf(errorStr) + if err := e.knownValue.CheckValue(result); err != nil { + resp.Error = err return } diff --git a/statecheck/expect_known_output_value_at_path.go b/statecheck/expect_known_output_value_at_path.go index 31d4516b1..7ff704b06 100644 --- a/statecheck/expect_known_output_value_at_path.go +++ b/statecheck/expect_known_output_value_at_path.go @@ -6,7 +6,6 @@ package statecheck import ( "context" "fmt" - "reflect" tfjson "github.com/hashicorp/terraform-json" @@ -57,27 +56,8 @@ func (e expectKnownOutputValueAtPath) CheckState(ctx context.Context, req CheckS return } - if result == nil { - resp.Error = fmt.Errorf("value is null") - - return - } - - switch reflect.TypeOf(result).Kind() { - case reflect.Bool, - reflect.Map, - reflect.Slice, - reflect.String: - if err := e.knownValue.CheckValue(result); err != nil { - resp.Error = err - - return - } - default: - errorStr := fmt.Sprintf("unrecognised output type: %T, known value type is %T", result, e.knownValue) - errorStr += "\n\nThis is an error in statecheck.ExpectKnownOutputValueAtPath.\nPlease report this to the maintainers." - - resp.Error = fmt.Errorf(errorStr) + if err := e.knownValue.CheckValue(result); err != nil { + resp.Error = err return } diff --git a/statecheck/expect_known_output_value_at_path_test.go b/statecheck/expect_known_output_value_at_path_test.go index 6b9795f90..371847b69 100644 --- a/statecheck/expect_known_output_value_at_path_test.go +++ b/statecheck/expect_known_output_value_at_path_test.go @@ -18,7 +18,6 @@ import ( "github.com/hashicorp/terraform-plugin-testing/knownvalue" "github.com/hashicorp/terraform-plugin-testing/statecheck" "github.com/hashicorp/terraform-plugin-testing/tfjsonpath" - "github.com/hashicorp/terraform-plugin-testing/tfversion" ) func TestExpectKnownOutputValueAtPath_CheckState_ResourceNotFound(t *testing.T) { @@ -30,13 +29,6 @@ func TestExpectKnownOutputValueAtPath_CheckState_ResourceNotFound(t *testing.T) return testProvider(), nil }, }, - // Prior to Terraform v1.3.0 a statened 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" "one" { @@ -69,13 +61,6 @@ func TestExpectKnownOutputValueAtPath_CheckState_AttributeValueNull(t *testing.T return testProvider(), nil }, }, - // Prior to Terraform v1.3.0 a statened 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" "one" {} @@ -88,10 +73,9 @@ func TestExpectKnownOutputValueAtPath_CheckState_AttributeValueNull(t *testing.T statecheck.ExpectKnownOutputValueAtPath( "test_resource_one_output", tfjsonpath.New("bool_attribute"), - knownvalue.BoolValueExact(true), + knownvalue.NullValueExact(), ), }, - ExpectError: regexp.MustCompile("value is null"), }, }, }) @@ -106,13 +90,6 @@ func TestExpectKnownOutputValueAtPath_CheckState_Bool(t *testing.T) { return testProvider(), nil }, }, - // Prior to Terraform v1.3.0 a statened 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" "one" { @@ -144,13 +121,6 @@ func TestExpectKnownOutputValueAtPath_CheckState_Bool_KnownValueWrongType(t *tes return testProvider(), nil }, }, - // Prior to Terraform v1.3.0 a statened 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" "one" { @@ -183,13 +153,6 @@ func TestExpectKnownOutputValueAtPath_CheckState_Bool_KnownValueWrongValue(t *te return testProvider(), nil }, }, - // Prior to Terraform v1.3.0 a statened 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" "one" { @@ -222,13 +185,6 @@ func TestExpectKnownOutputValueAtPath_CheckState_Float64(t *testing.T) { return testProvider(), nil }, }, - // Prior to Terraform v1.3.0 a statened 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" "one" { @@ -261,13 +217,6 @@ func TestExpectKnownOutputValueAtPath_CheckState_Float64_KnownValueWrongType(t * return testProvider(), nil }, }, - // Prior to Terraform v1.3.0 a statened 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" "one" { @@ -300,13 +249,6 @@ func TestExpectKnownOutputValueAtPath_CheckState_Float64_KnownValueWrongValue(t return testProvider(), nil }, }, - // Prior to Terraform v1.3.0 a statened 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" "one" { @@ -339,13 +281,6 @@ func TestExpectKnownOutputValueAtPath_CheckState_Int64(t *testing.T) { return testProvider(), nil }, }, - // Prior to Terraform v1.3.0 a statened 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" "one" { @@ -377,13 +312,6 @@ func TestExpectKnownOutputValueAtPath_CheckState_Int64_KnownValueWrongValue(t *t return testProvider(), nil }, }, - // Prior to Terraform v1.3.0 a statened 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" "one" { @@ -416,13 +344,6 @@ func TestExpectKnownOutputValueAtPath_CheckState_List(t *testing.T) { return testProvider(), nil }, }, - // Prior to Terraform v1.3.0 a statened 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" "one" { @@ -460,13 +381,6 @@ func TestExpectKnownOutputValueAtPath_CheckState_List_KnownValueWrongType(t *tes return testProvider(), nil }, }, - // Prior to Terraform v1.3.0 a statened 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" "one" { @@ -502,13 +416,6 @@ func TestExpectKnownOutputValueAtPath_CheckState_List_KnownValueWrongValue(t *te return testProvider(), nil }, }, - // Prior to Terraform v1.3.0 a statened 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" "one" { @@ -547,13 +454,6 @@ func TestExpectKnownOutputValueAtPath_CheckState_ListPartial(t *testing.T) { return testProvider(), nil }, }, - // Prior to Terraform v1.3.0 a statened 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" "one" { @@ -592,13 +492,6 @@ func TestExpectKnownOutputValueAtPath_CheckState_ListPartial_KnownValueWrongValu return testProvider(), nil }, }, - // Prior to Terraform v1.3.0 a statened 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" "one" { @@ -636,13 +529,6 @@ func TestExpectKnownOutputValueAtPath_CheckState_ListElements(t *testing.T) { return testProvider(), nil }, }, - // Prior to Terraform v1.3.0 a statened 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" "one" { @@ -677,13 +563,6 @@ func TestExpectKnownOutputValueAtPath_CheckState_ListElements_WrongNum(t *testin return testProvider(), nil }, }, - // Prior to Terraform v1.3.0 a statened 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" "one" { @@ -719,13 +598,6 @@ func TestExpectKnownOutputValueAtPath_CheckState_ListNestedBlock(t *testing.T) { return testProvider(), nil }, }, - // Prior to Terraform v1.3.0 a statened 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" "one" { @@ -769,13 +641,6 @@ func TestExpectKnownOutputValueAtPath_CheckState_ListNestedBlockPartial(t *testi return testProvider(), nil }, }, - // Prior to Terraform v1.3.0 a statened 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" "one" { @@ -816,13 +681,6 @@ func TestExpectKnownOutputValueAtPath_CheckState_ListNestedBlockElements(t *test return testProvider(), nil }, }, - // Prior to Terraform v1.3.0 a statened 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" "one" { @@ -859,13 +717,6 @@ func TestExpectKnownOutputValueAtPath_CheckState_Map(t *testing.T) { return testProvider(), nil }, }, - // Prior to Terraform v1.3.0 a statened 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" "one" { @@ -903,13 +754,6 @@ func TestExpectKnownOutputValueAtPath_CheckState_Map_KnownValueWrongType(t *test return testProvider(), nil }, }, - // Prior to Terraform v1.3.0 a statened 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" "one" { @@ -945,13 +789,6 @@ func TestExpectKnownOutputValueAtPath_CheckState_Map_KnownValueWrongValue(t *tes return testProvider(), nil }, }, - // Prior to Terraform v1.3.0 a statened 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" "one" { @@ -990,13 +827,6 @@ func TestExpectKnownOutputValueAtPath_CheckState_MapPartial(t *testing.T) { return testProvider(), nil }, }, - // Prior to Terraform v1.3.0 a statened 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" "one" { @@ -1033,13 +863,6 @@ func TestExpectKnownOutputValueAtPath_CheckState_MapPartial_KnownValueWrongValue return testProvider(), nil }, }, - // Prior to Terraform v1.3.0 a statened 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" "one" { @@ -1077,13 +900,6 @@ func TestExpectKnownOutputValueAtPath_CheckState_MapElements(t *testing.T) { return testProvider(), nil }, }, - // Prior to Terraform v1.3.0 a statened 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" "one" { @@ -1118,13 +934,6 @@ func TestExpectKnownOutputValueAtPath_CheckState_MapElements_WrongNum(t *testing return testProvider(), nil }, }, - // Prior to Terraform v1.3.0 a statened 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" "one" { @@ -1166,13 +975,6 @@ func TestExpectKnownOutputValueAtPath_CheckState_Number(t *testing.T) { return testProvider(), nil }, }, - // Prior to Terraform v1.3.0 a statened 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" "one" { @@ -1210,13 +1012,6 @@ func TestExpectKnownOutputValueAtPath_CheckState_Number_KnownValueWrongValue(t * return testProvider(), nil }, }, - // Prior to Terraform v1.3.0 a statened 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" "one" { @@ -1249,13 +1044,6 @@ func TestExpectKnownOutputValueAtPath_CheckState_Set(t *testing.T) { return testProvider(), nil }, }, - // Prior to Terraform v1.3.0 a statened 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" "one" { @@ -1293,13 +1081,6 @@ func TestExpectKnownOutputValueAtPath_CheckState_Set_KnownValueWrongValue(t *tes return testProvider(), nil }, }, - // Prior to Terraform v1.3.0 a statened 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" "one" { @@ -1338,13 +1119,6 @@ func TestExpectKnownOutputValueAtPath_CheckState_SetPartial(t *testing.T) { return testProvider(), nil }, }, - // Prior to Terraform v1.3.0 a statened 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" "one" { @@ -1381,13 +1155,6 @@ func TestExpectKnownOutputValueAtPath_CheckState_SetPartial_KnownValueWrongValue return testProvider(), nil }, }, - // Prior to Terraform v1.3.0 a statened 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" "one" { @@ -1425,13 +1192,6 @@ func TestExpectKnownOutputValueAtPath_CheckState_SetElements(t *testing.T) { return testProvider(), nil }, }, - // Prior to Terraform v1.3.0 a statened 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" "one" { @@ -1466,13 +1226,6 @@ func TestExpectKnownOutputValueAtPath_CheckState_SetNestedBlock(t *testing.T) { return testProvider(), nil }, }, - // Prior to Terraform v1.3.0 a statened 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" "one" { @@ -1516,13 +1269,6 @@ func TestExpectKnownOutputValueAtPath_CheckState_SetNestedBlockPartial(t *testin return testProvider(), nil }, }, - // Prior to Terraform v1.3.0 a statened 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" "one" { @@ -1563,13 +1309,6 @@ func TestExpectKnownOutputValueAtPath_CheckState_SetNestedBlockElements(t *testi return testProvider(), nil }, }, - // Prior to Terraform v1.3.0 a statened 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" "one" { @@ -1606,13 +1345,6 @@ func TestExpectKnownOutputValueAtPath_CheckState_String(t *testing.T) { return testProvider(), nil }, }, - // Prior to Terraform v1.3.0 a statened 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" "one" { @@ -1643,13 +1375,6 @@ func TestExpectKnownOutputValueAtPath_CheckState_String_KnownValueWrongType(t *t return testProvider(), nil }, }, - // Prior to Terraform v1.3.0 a statened 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" "one" { @@ -1681,13 +1406,6 @@ func TestExpectKnownOutputValueAtPath_CheckState_String_KnownValueWrongValue(t * return testProvider(), nil }, }, - // Prior to Terraform v1.3.0 a statened 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" "one" { @@ -1731,7 +1449,7 @@ func TestExpectKnownOutputValueAtPath_CheckState_UnknownAttributeType(t *testing }, }, }, - expectedErr: fmt.Errorf("unrecognised output type: float32, known value type is knownvalue.Int64Value\n\nThis is an error in statecheck.ExpectKnownOutputValueAtPath.\nPlease report this to the maintainers."), + expectedErr: fmt.Errorf("expected json.Number value for Int64Value check, got: float32"), }, } diff --git a/statecheck/expect_known_output_value_test.go b/statecheck/expect_known_output_value_test.go index 1c1cfcaa7..e081b0716 100644 --- a/statecheck/expect_known_output_value_test.go +++ b/statecheck/expect_known_output_value_test.go @@ -50,6 +50,10 @@ func TestExpectKnownOutputValue_CheckState_OutputNotFound(t *testing.T) { }) } +// TestExpectKnownOutputValue_CheckState_AttributeValueNull shows that outputs that reference +// null values do not appear in state. Indicating that there is no way to discriminate +// between null outputs and non-existent outputs. +// Reference: https://github.com/hashicorp/terraform/issues/34080 func TestExpectKnownOutputValue_CheckState_AttributeValueNull(t *testing.T) { t.Parallel() @@ -1408,7 +1412,7 @@ func TestExpectKnownOutputValue_CheckState_UnknownAttributeType(t *testing.T) { }, }, }, - expectedErr: fmt.Errorf("unrecognised output type: float32, known value type is knownvalue.Int64Value\n\nThis is an error in statecheck.ExpectKnownOutputValue.\nPlease report this to the maintainers."), + expectedErr: fmt.Errorf("expected json.Number value for Int64Value check, got: float32"), }, } diff --git a/statecheck/expect_known_value.go b/statecheck/expect_known_value.go index b8d5a6157..096699cba 100644 --- a/statecheck/expect_known_value.go +++ b/statecheck/expect_known_value.go @@ -6,7 +6,6 @@ package statecheck import ( "context" "fmt" - "reflect" tfjson "github.com/hashicorp/terraform-json" @@ -61,29 +60,8 @@ func (e expectKnownValue) CheckState(ctx context.Context, req CheckStateRequest, return } - if result == nil { - resp.Error = fmt.Errorf("value is null") - - return - } - - switch reflect.TypeOf(result).Kind() { - case reflect.Bool, - reflect.Map, - reflect.Slice, - reflect.String: - if err := e.knownValue.CheckValue(result); err != nil { - resp.Error = err - - return - } - default: - errorStr := fmt.Sprintf("unrecognised attribute type: %T, known value type is %T", result, e.knownValue) - errorStr += "\n\nThis is an error in statecheck.ExpectKnownValue.\nPlease report this to the maintainers." - - resp.Error = fmt.Errorf(errorStr) - - return + if err := e.knownValue.CheckValue(result); err != nil { + resp.Error = err } } diff --git a/statecheck/expect_known_value_test.go b/statecheck/expect_known_value_test.go index bd1024891..ce2d0a5c9 100644 --- a/statecheck/expect_known_value_test.go +++ b/statecheck/expect_known_value_test.go @@ -65,10 +65,9 @@ func TestExpectKnownValue_CheckState_AttributeValueNull(t *testing.T) { statecheck.ExpectKnownValue( "test_resource.one", tfjsonpath.New("bool_attribute"), - knownvalue.BoolValueExact(true), + knownvalue.NullValueExact(), ), }, - ExpectError: regexp.MustCompile("value is null"), }, }, }) @@ -1295,7 +1294,7 @@ func TestExpectKnownValue_CheckState_UnknownAttributeType(t *testing.T) { }, }, }, - expectedErr: fmt.Errorf("unrecognised attribute type: float32, known value type is knownvalue.Int64Value\n\nThis is an error in statecheck.ExpectKnownValue.\nPlease report this to the maintainers."), + expectedErr: fmt.Errorf("expected json.Number value for Int64Value check, got: float32"), }, }