Skip to content

Conversation

stephybun
Copy link
Member

@stephybun stephybun commented Mar 5, 2025

Based off of #431
Supersedes #441

TODOs:

@stephybun stephybun requested a review from a team as a code owner March 5, 2025 12:48
Comment on lines -439 to -450
confRequest := teststep.PrepareConfigurationRequest{
Directory: step.ConfigDirectory,
File: step.ConfigFile,
Raw: mergedConfig,
TestStepConfigRequest: config.TestStepConfigRequest{
StepNumber: stepIndex + 1,
TestName: t.Name(),
},
}.Exec()

appliedCfg = teststep.Configuration(confRequest)

Copy link
Member Author

@stephybun stephybun Mar 5, 2025

Choose a reason for hiding this comment

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

It looks like this was only ever needed and done for the import tests, so I've opted to move this into testing_new_import_state.go and pass the appliedConfig through as a string so it can be easily appended/modified based on the import mode/kind we're using in the test.

Copy link
Collaborator

Choose a reason for hiding this comment

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

💯

@stephybun stephybun mentioned this pull request Mar 5, 2025
1 task
// Not an import block test so nothing to do here
}

confRequest := teststep.PrepareConfigurationRequest{
Copy link
Collaborator

Choose a reason for hiding this comment

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

I wonder how this interacts with the earlier teststep.PrepareConfigurationRequest from step.Config on line 26. Would this change the behavior of any existing ImportCommandWithId tests that define step.Config? I actually don't know 😃

Copy link
Member Author

Choose a reason for hiding this comment

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

I can confirm that this doesn't change the behaviour of existing ImportCommandWithId tests that define a Config in-line.

File: step.ConfigFile,
Raw: cfgRaw,
TestStepConfigRequest: config.TestStepConfigRequest{
StepNumber: stepIndex + 1,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Something I had overlooked in the proof-of-concept pull request: what is the significance of stepIndex + 1 here (and at line 26)?

Copy link
Member Author

Choose a reason for hiding this comment

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

I believe it's to maintain the correct step number which was modified to index starting from 1 for a better UX

stepNumber = stepIndex + 1 // 1-based indexing for humans

This instance (as well as another in testing_new.go) can probably be updated to use stepNumber

@stephybun stephybun requested a review from bbasata March 10, 2025 11:06
Comment on lines +105 to +121
import {
to = %s
id = %q
}
`, step.ResourceName, importId)
Copy link
Contributor

Choose a reason for hiding this comment

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

We are only adding the import block to the config automatically if the ImportBlockWithId mode is set in a step without a config, is this expected? I wonder if we should always add the import block automatically to the config for ImportBlockWithId kinds.

Currently, you can run a test with ImportBlockWithId set with a config that does not have an import block:

{
  Config: `
  terraform {
  	  required_providers {
    	  examplecloud = {
      		  source = "registry.terraform.io/hashicorp/examplecloud"
    	  }
	  }
  }
  
  resource "examplecloud_container" "test" {
	  name = "somename"
	  password = "somevalue"
  }`,
ResourceName:            "examplecloud_container.test",
ImportState:             true,
ImportStateKind:         ImportBlockWithId,
ImportStateVerify:       true,
ImportStateVerifyIgnore: []string{"password"},
},

With the current implementation, this would be running a regular import test (not a block id test) but there is no indication to provider developers that that is the case.

Copy link
Member Author

@stephybun stephybun Mar 11, 2025

Choose a reason for hiding this comment

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

That is a good point, my thinking was purely based on how import tests have been used in the azurerm provider which is that we always re-use the config in the initial create test step so there is never need to specify Config for an import test. That assumption has been blown completely out of the water after looking into how tests using an import block function. Either way you've raised an important point and I think we should be appending the import block to a config that's been defined in the test step if ImportBlockWithId has been specified.

Comment on lines +26 to +32
// Currently import modes `ImportBlockWithId` and `ImportBlockWithResourceIdentity` cannot support config file or directory
// since these modes append the import block to the configuration automatically
if step.ImportStateKind != ImportCommandWithId {
if step.ConfigFile != nil || step.ConfigDirectory != nil {
t.Fatalf("ImportStateKind %q is not supported for config file or directory", step.ImportStateKind)
}
}
Copy link
Member Author

Choose a reason for hiding this comment

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

The behaviour of these new modes is currently:

  1. Generate and append an import block to the test configuration
  2. Run apply instead of the import command

I don't think the testing framework should be modifying test configs defined in files or a directory. Thus the addition of this validation after thinking about this comment.

Longer term (maybe a subsequent PR) I think it's worth adding support for file, I'm just unsure whether it's wise to have a part of the behaviour differ depending on how the config is provided, i.e., if config were provided by a file the import kind should no longer do 1. but only 2..

@stephybun stephybun force-pushed the f/import-state-id-config branch from 87c1654 to 2365829 Compare March 11, 2025 10:05
@stephybun stephybun force-pushed the f/import-state-id-config branch from 2365829 to 6cb9373 Compare March 11, 2025 10:06

const (
// ImportCommandWithId imports the state using the import command
ImportCommandWithId ImportStateKind = iota
Copy link
Collaborator

Choose a reason for hiding this comment

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

I noticed a nice side-effect here. ImportCommandWithId equals zero. So, all existing tests will default to ImportCommandWithId, as TestStep.ImportStateKind will get the zero value.

Copy link
Collaborator

@bbasata bbasata left a comment

Choose a reason for hiding this comment

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

This is very ready to merge 😃

  • Feature toggle: the PR adds a work-in-progress, optional pre-release feature; this functionality is only activated when the test author sets a non-zero (i.e. non-default) value on TestStep.ImportStateKind.

  • Regression-safety: the refactoring to support the new feature looks safe, and this module's tests are passing.

We'll iterate on this feature in follow-up PRs.

This was a joy, @stephybun

@bbasata bbasata merged commit 3673753 into main Mar 20, 2025
37 checks passed
@bbasata bbasata deleted the f/import-state-id-config branch March 20, 2025 12:51
@austinvalle austinvalle added this to the v1.13.0 milestone Mar 25, 2025
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 24, 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.

4 participants