-
Notifications
You must be signed in to change notification settings - Fork 232
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding RefreshState test step (#1070)
* Adding RefreshState test step (#1069) * Adding CHANGELOG entry (#1069) * Running TestCheckFunc with refreshed state (#1069) * Adding validation to check conditions for RefreshState (#1069) * Expanding comments on RefreshState (#1069) * Removing option to override config during refresh testing, removing re-init and adding plan following refresh (#1069) * Adding test coverage for to verify expect non-empty plan following refresh (#1069) * Apply suggestions from code review Co-authored-by: Brian Flad <bflad417@gmail.com> * Adding validation to verify that refresh state is not present with config or destroy in a test step (#1069) * Reset time during test step so that ReadContext does not mutate state and result in a diff (#1069) * Updating website docs (#1069) * Apply suggestions from code review Co-authored-by: Brian Flad <bflad417@gmail.com> * Test to verify that setting config and refresh state together is not valid (#1069) Co-authored-by: Brian Flad <bflad417@gmail.com>
- Loading branch information
1 parent
3495894
commit 1dba057
Showing
8 changed files
with
379 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
```release-note:enhancement | ||
helper/resource: Added `TestStep` type `RefreshState` field, which enables a step that refreshes state without an explicit apply or configuration changes | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package resource | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/davecgh/go-spew/spew" | ||
tfjson "github.com/hashicorp/terraform-json" | ||
"github.com/mitchellh/go-testing-interface" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/internal/logging" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/internal/plugintest" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform" | ||
) | ||
|
||
func testStepNewRefreshState(ctx context.Context, t testing.T, wd *plugintest.WorkingDir, step TestStep, providers *providerFactories) error { | ||
t.Helper() | ||
|
||
spewConf := spew.NewDefaultConfig() | ||
spewConf.SortKeys = true | ||
|
||
var err error | ||
// Explicitly ensure prior state exists before refresh. | ||
err = runProviderCommand(ctx, t, func() error { | ||
_, err = getState(ctx, t, wd) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
}, wd, providers) | ||
if err != nil { | ||
t.Fatalf("Error getting state: %s", err) | ||
} | ||
|
||
err = runProviderCommand(ctx, t, func() error { | ||
return wd.Refresh(ctx) | ||
}, wd, providers) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var refreshState *terraform.State | ||
err = runProviderCommand(ctx, t, func() error { | ||
refreshState, err = getState(ctx, t, wd) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
}, wd, providers) | ||
if err != nil { | ||
t.Fatalf("Error getting state: %s", err) | ||
} | ||
|
||
// Go through the refreshed state and verify | ||
if step.Check != nil { | ||
logging.HelperResourceDebug(ctx, "Calling TestStep Check for RefreshState") | ||
|
||
if err := step.Check(refreshState); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
logging.HelperResourceDebug(ctx, "Called TestStep Check for RefreshState") | ||
} | ||
|
||
// do a plan | ||
err = runProviderCommand(ctx, t, func() error { | ||
return wd.CreatePlan(ctx) | ||
}, wd, providers) | ||
if err != nil { | ||
return fmt.Errorf("Error running post-apply plan: %w", err) | ||
} | ||
|
||
var plan *tfjson.Plan | ||
err = runProviderCommand(ctx, t, func() error { | ||
var err error | ||
plan, err = wd.SavedPlan(ctx) | ||
return err | ||
}, wd, providers) | ||
if err != nil { | ||
return fmt.Errorf("Error retrieving post-apply plan: %w", err) | ||
} | ||
|
||
if !planIsEmpty(plan) && !step.ExpectNonEmptyPlan { | ||
var stdout string | ||
err = runProviderCommand(ctx, t, func() error { | ||
var err error | ||
stdout, err = wd.SavedPlanRawStdout(ctx) | ||
return err | ||
}, wd, providers) | ||
if err != nil { | ||
return fmt.Errorf("Error retrieving formatted plan output: %w", err) | ||
} | ||
return fmt.Errorf("After refreshing state during this test step, a followup plan was not empty.\nstdout:\n\n%s", stdout) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.