-
Notifications
You must be signed in to change notification settings - Fork 13
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
Add exported function to retrieve attribute from state for use during tests #83
Comments
We've certainly seen providers that use this class of attribute value extraction across
They're all circling around verifying the resource under test was planned for and/or deleted/recreated properly. More easily enabling provider developers to extract values and perform their own equivalence testing certainly is interesting. |
Is there an official solution for this yet? I'm confused on how to best compare/test attributes across multiple test steps. |
Hey there @marshallford 👋🏻, If I'm understanding your use-case correctly, you may be interested in #295 and the upcoming PR #330 which resolves it |
Thanks for the help @austinvalle! I looked over the PR you linked and I have a couple of questions if you don't mind:
|
Absolutely!
So we're actually in the process of deprecating (originally #266) the The reason we introduced the
Thanks for that feedback! I think you're right that what's in that PR right now mostly classifies as reference documentation. I'll make a note to add a better example, perhaps in our overview sections. (Our testing docs in general could probably use some TLC 😃 ) As for your example, here is what I believe the end state of that test would look like completely using func TestAccNavigatorRun_skip_run(t *testing.T) {
t.Parallel()
// Here we set up the state check comparer, we tell it how we want state values to be compared (we want all state values to be equal)
compareResourceCommand := statecheck.CompareValue(compare.ValuesSame())
resource.Test(t, resource.TestCase{
PreCheck: func() { testPreCheck(t) },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: testTerraformFile(t, filepath.Join("navigator_run", "skip_run")),
ConfigVariables: testDefaultConfigVariables(t),
ConfigStateChecks: []statecheck.StateCheck{
// Here are some equivalent checks to `TestCheckResourceAttrSet`
statecheck.ExpectKnownValue(navigatorRunResource, tfjsonpath.New("id"), knownvalue.NotNull()),
statecheck.ExpectKnownValue(navigatorRunResource, tfjsonpath.New("command"), knownvalue.NotNull()),
// Add the first state value "command" to the comparer. There is nothing to compare to at this point.
compareResourceCommand.AddStateValue(navigatorRunResource, tfjsonpath.New("command")),
},
},
{
Config: testTerraformFile(t, filepath.Join("navigator_run", "skip_run_update")),
ConfigVariables: testConfigVariables(t, config.Variables{
"ansible_navigator_binary": config.StringVariable(acctest.RandString(8)),
}),
ConfigPlanChecks: resource.ConfigPlanChecks{
PreApply: []plancheck.PlanCheck{
plancheck.ExpectNonEmptyPlan(),
},
},
ConfigStateChecks: []statecheck.StateCheck{
// Here are some equivalent checks to `TestCheckResourceAttrSet`
statecheck.ExpectKnownValue(navigatorRunResource, tfjsonpath.New("id"), knownvalue.NotNull()),
statecheck.ExpectKnownValue(navigatorRunResource, tfjsonpath.New("command"), knownvalue.NotNull()),
// Add the second state value "command" to the same comparer. The second value will be compared with the first,
// if they aren't equal, the state check will fail here.
compareResourceCommand.AddStateValue(navigatorRunResource, tfjsonpath.New("command")),
},
},
},
})
} Apologies if there are any compiler errors in that example, I didn't write it in a code editor 😆 |
Thank you Thank you! That all tracks and the example you provided is very helpful. Looking forward to the doc updates as well. |
SDK version
Use-cases
Occasionally, when writing acceptance tests it is useful to be able to extract and store a value from state in order to use in subsequent test steps. For instance, when using the
Taint
field in aTestStep
it is useful to be able to verify that a resource has been destroyed and recreated.Attempted Solutions
Adding a function along the following lines to each provider which needs to be able to compare attributes before and after test steps.
Proposal
Add an exported function to
/helper/resource
that exposes this functionality to avoid code duplication in providers.References
The text was updated successfully, but these errors were encountered: