diff --git a/.changelog/1070.txt b/.changelog/1070.txt index b830de704a..6b95d99532 100644 --- a/.changelog/1070.txt +++ b/.changelog/1070.txt @@ -1,3 +1,3 @@ ```release-note:enhancement -helper/resource: Add RefreshState and RefreshStateCheck to allow testing of refresh in isolation +helper/resource: Add RefreshState to allow testing of refresh in isolation ``` diff --git a/helper/resource/testing.go b/helper/resource/testing.go index b423a1383e..5fadf24760 100644 --- a/helper/resource/testing.go +++ b/helper/resource/testing.go @@ -292,9 +292,6 @@ type ImportStateCheckFunc func([]*terraform.InstanceState) error // generation for ImportState tests. type ImportStateIdFunc func(*terraform.State) (string, error) -// RefreshStateCheckFunc is the check function for RefreshState tests -type RefreshStateCheckFunc func([]*terraform.InstanceState) error - // ErrorCheckFunc is a function providers can use to handle errors. type ErrorCheckFunc func(error) error @@ -581,11 +578,6 @@ type TestStep struct { // refresh` by refreshing the state. RefreshState bool - // RefreshStateCheck checks state following `terraform refresh`. It - // should be used to verify that the state has the expected resources, - // IDs, and attributes. - RefreshStateCheck RefreshStateCheckFunc - // ProviderFactories can be specified for the providers that are valid for // this TestStep. When providers are specified at the TestStep level, all // TestStep within a TestCase must declare providers. diff --git a/helper/resource/testing_new_refresh_state.go b/helper/resource/testing_new_refresh_state.go index af4c9767f0..5cbba57a10 100644 --- a/helper/resource/testing_new_refresh_state.go +++ b/helper/resource/testing_new_refresh_state.go @@ -72,25 +72,14 @@ func testStepNewRefreshState(ctx context.Context, t testing.T, wd *plugintest.Wo } // Go through the refreshed state and verify - if step.RefreshStateCheck != nil { - logging.HelperResourceTrace(ctx, "Using TestStep RefreshStateCheck") - - var states []*terraform.InstanceState - for _, r := range refreshState.RootModule().Resources { - if r.Primary != nil { - is := r.Primary.DeepCopy() - is.Ephemeral.Type = r.Type // otherwise the check function cannot see the type - states = append(states, is) - } - } - - logging.HelperResourceDebug(ctx, "Calling TestStep RefreshStateCheck") + if step.Check != nil { + logging.HelperResourceDebug(ctx, "Calling TestStep Check for RefreshState") - if err := step.RefreshStateCheck(states); err != nil { + if err := step.Check(refreshState); err != nil { t.Fatal(err) } - logging.HelperResourceDebug(ctx, "Called TestStep RefreshStateCheck") + logging.HelperResourceDebug(ctx, "Called TestStep Check for RefreshState") } return nil diff --git a/helper/resource/teststep_providers_test.go b/helper/resource/teststep_providers_test.go index 45128b8694..01cc247b54 100644 --- a/helper/resource/teststep_providers_test.go +++ b/helper/resource/teststep_providers_test.go @@ -1625,9 +1625,7 @@ func TestTest_TestStep_ProviderFactories_Refresh_Inline(t *testing.T) { { Config: `resource "random_password" "test" { }`, RefreshState: true, - RefreshStateCheck: composeRefreshStateCheck( - testCheckResourceAttrInstanceStateRefresh("min_special", "2"), - ), + Check: TestCheckResourceAttr("random_password.test", "min_special", "2"), }, { Config: `resource "random_password" "test" { }`, @@ -1689,39 +1687,6 @@ func testCheckResourceAttrInstanceState(attributeName, attributeValue string) Im } } -func composeRefreshStateCheck(fs ...RefreshStateCheckFunc) RefreshStateCheckFunc { - return func(s []*terraform.InstanceState) error { - for i, f := range fs { - if err := f(s); err != nil { - return fmt.Errorf("check %d/%d error: %s", i+1, len(fs), err) - } - } - - return nil - } -} - -func testCheckResourceAttrInstanceStateRefresh(attributeName, attributeValue string) RefreshStateCheckFunc { - return func(is []*terraform.InstanceState) error { - if len(is) != 1 { - return fmt.Errorf("unexpected number of instance states: %d", len(is)) - } - - s := is[0] - - attrVal, ok := s.Attributes[attributeName] - if !ok { - return fmt.Errorf("attribute %s found in instance state", attributeName) - } - - if attrVal != attributeValue { - return fmt.Errorf("expected: %s got: %s", attributeValue, attrVal) - } - - return nil - } -} - func testExtractResourceAttr(resourceName string, attributeName string, attributeValue *string) TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[resourceName]