Skip to content

Commit

Permalink
Running TestCheckFunc with refreshed state (#1069)
Browse files Browse the repository at this point in the history
  • Loading branch information
bendbennett committed Oct 3, 2022
1 parent 204d99e commit 468b72d
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 60 deletions.
2 changes: 1 addition & 1 deletion .changelog/1070.txt
Original file line number Diff line number Diff line change
@@ -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
```
8 changes: 0 additions & 8 deletions helper/resource/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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.
Expand Down
19 changes: 4 additions & 15 deletions helper/resource/testing_new_refresh_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
37 changes: 1 addition & 36 deletions helper/resource/teststep_providers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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" { }`,
Expand Down Expand Up @@ -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]
Expand Down

0 comments on commit 468b72d

Please sign in to comment.