-
Notifications
You must be signed in to change notification settings - Fork 16
Add additional import test modes #442
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
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) | ||
|
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💯
// Not an import block test so nothing to do here | ||
} | ||
|
||
confRequest := teststep.PrepareConfigurationRequest{ |
There was a problem hiding this comment.
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 😃
There was a problem hiding this comment.
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, |
There was a problem hiding this comment.
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)?
There was a problem hiding this comment.
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
import { | ||
to = %s | ||
id = %q | ||
} | ||
`, step.ResourceName, importId) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
// 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) | ||
} | ||
} |
There was a problem hiding this comment.
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:
- Generate and append an import block to the test configuration
- 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..
87c1654
to
2365829
Compare
…ks with an id string and calling apply
…es or directories
2365829
to
6cb9373
Compare
|
||
const ( | ||
// ImportCommandWithId imports the state using the import command | ||
ImportCommandWithId ImportStateKind = iota |
There was a problem hiding this comment.
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.
There was a problem hiding this 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 ✨
Based off of #431
Supersedes #441
TODOs:
ImportStateVerifyIgnore
,SkipDataSource
?terraform-plugin-testing/helper/resource/testing_new_import_state.go
Line 216 in 805da5a
testing_new_plannable_import.go
terraform-plugin-testing/helper/resource/testing_new_import_block_id_test.go
Lines 445 to 451 in 1b47671