Skip to content

Conversation

bbasata
Copy link
Collaborator

@bbasata bbasata commented Mar 25, 2025

To be merged after #465

This change introduces the TestStep.ImportPlanVerify option. With this option, a plannable import test compares a generated plan to the known state of a previous test step.

This unlocks verification for plannable import without invoking the apply command.

There is no attempt in this PR to mirror these existing TestStep.ImportStateVerify options:

  • ImportStateVerifyIdentifierAttribute
  • ImportStateVerifyIgnore

If there's value, we can add these in a follow-up PR.

@austinvalle austinvalle added this to the v1.13.0 milestone Mar 25, 2025
@bbasata bbasata force-pushed the import-compare-plan-to-previous-state branch from 1298559 to 4cf6a87 Compare March 25, 2025 18:34
@bbasata bbasata marked this pull request as ready for review March 25, 2025 18:51
@bbasata bbasata requested a review from a team as a code owner March 25, 2025 18:51
Copy link
Member

@austinvalle austinvalle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good so far! I left some comments to consider 👀

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I'd love to see a negative test here as well, like one where the follow-up plan has changes.

Might need to do some awkwardness to create that "inconsistency" without having a remote system, especially since the end of test step 1 will refresh. In the past I've used the provider factories on each test step to inject the differences, which is a potential option: https://github.com/hashicorp/terraform-provider-corner/blob/52e22f4ee878680727ff68a2e44967b496242a76/internal/protocolv6provider/writeonly_datacheck_test.go#L182-L204

Comment on lines +201 to +202
// In prerelease, we are choosing that ImportBlockWithID will not perform an apply, so it will not produce a new state,
// and there is no new state for ImportStateVerify to do anything meaningful with.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Are we planning on revisiting this decision later? If so, maybe a TODO prefix here (I know I'll forget what the original intention was of this comment 😆)

Same question for the other test below

ImportStateVerify: true,
ResourceName: "examplecloud_container.test",
ImportState: true,
ImportStateKind: r.ImportBlockWithID,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Is there a reason we don't want to use ImportStateVerify here?

Comment on lines 65 to 67
if attr != vs {
return fmt.Errorf("importing resource %s: expected %s in known state to be %q, got %q", rc.Address, k, oldResource.Primary.Attributes[k], vs)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this meant to work for slices/maps? (collections, nested types, etc.)

}
}

for _, rc := range plan.ResourceChanges {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One of the things we will run into here is actually the exact same problem as ImportStateVerify, which is that because we are comparing two raw data objects from Terraform (state or plan, doesn't matter tbh), if we compare them without the schema itself, we can't respect set's unordered nature.

The only real solution I can see to that problem is to build the logic to grab a provider/resource schema from Terraform, then use it to compare two data objects.

Bonus points if the logic is generic enough that we can pass any JSON objects into it. (we could re-use it for ImportStateVerify with two state objects vs. one plan and one state object)

"github.com/hashicorp/terraform-plugin-testing/terraform"
)

func Test_VerifyImportPlan(t *testing.T) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One nice thing for this test would be to expand it for the other types of data outside of primitives (lists, sets, maps, single nested object/block, list nested objects/blocks, set nested objects/blocks, map nested attribute)

bbasata and others added 2 commits March 26, 2025 21:30
Co-authored-by: Austin Valle <austinvalle@gmail.com>
Co-authored-by: Austin Valle <austinvalle@gmail.com>
Base automatically changed from import-config-file-and-directory to main March 31, 2025 12:53
@bbasata
Copy link
Collaborator Author

bbasata commented Apr 11, 2025

Closing in favor of #476.

@bbasata bbasata closed this Apr 11, 2025
@bbasata bbasata deleted the import-compare-plan-to-previous-state branch April 11, 2025 16:13
@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 11, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants