Merge pull request #16036 from hashicorp/f-import-state-testing-id-func
helper/resource: Add ImportStateIdFunc
This commit is contained in:
commit
9504d17bd9
|
@ -186,6 +186,10 @@ type TestCheckFunc func(*terraform.State) error
|
||||||
// ImportStateCheckFunc is the check function for ImportState tests
|
// ImportStateCheckFunc is the check function for ImportState tests
|
||||||
type ImportStateCheckFunc func([]*terraform.InstanceState) error
|
type ImportStateCheckFunc func([]*terraform.InstanceState) error
|
||||||
|
|
||||||
|
// ImportStateIdFunc is an ID generation function to help with complex ID
|
||||||
|
// generation for ImportState tests.
|
||||||
|
type ImportStateIdFunc func(*terraform.State) (string, error)
|
||||||
|
|
||||||
// TestCase is a single acceptance test case used to test the apply/destroy
|
// TestCase is a single acceptance test case used to test the apply/destroy
|
||||||
// lifecycle of a resource in a specific configuration.
|
// lifecycle of a resource in a specific configuration.
|
||||||
//
|
//
|
||||||
|
@ -333,6 +337,12 @@ type TestStep struct {
|
||||||
// the unset ImportStateId field.
|
// the unset ImportStateId field.
|
||||||
ImportStateIdPrefix string
|
ImportStateIdPrefix string
|
||||||
|
|
||||||
|
// ImportStateIdFunc is a function that can be used to dynamically generate
|
||||||
|
// the ID for the ImportState tests. It is sent the state, which can be
|
||||||
|
// checked to derive the attributes necessary and generate the string in the
|
||||||
|
// desired format.
|
||||||
|
ImportStateIdFunc ImportStateIdFunc
|
||||||
|
|
||||||
// ImportStateCheck checks the results of ImportState. It should be
|
// ImportStateCheck checks the results of ImportState. It should be
|
||||||
// used to verify that the resulting value of ImportState has the
|
// used to verify that the resulting value of ImportState has the
|
||||||
// proper resources, IDs, and attributes.
|
// proper resources, IDs, and attributes.
|
||||||
|
|
|
@ -16,15 +16,24 @@ func testStepImportState(
|
||||||
state *terraform.State,
|
state *terraform.State,
|
||||||
step TestStep) (*terraform.State, error) {
|
step TestStep) (*terraform.State, error) {
|
||||||
// Determine the ID to import
|
// Determine the ID to import
|
||||||
importId := step.ImportStateId
|
var importId string
|
||||||
if importId == "" {
|
switch {
|
||||||
|
case step.ImportStateIdFunc != nil:
|
||||||
|
var err error
|
||||||
|
importId, err = step.ImportStateIdFunc(state)
|
||||||
|
if err != nil {
|
||||||
|
return state, err
|
||||||
|
}
|
||||||
|
case step.ImportStateId != "":
|
||||||
|
importId = step.ImportStateId
|
||||||
|
default:
|
||||||
resource, err := testResource(step, state)
|
resource, err := testResource(step, state)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return state, err
|
return state, err
|
||||||
}
|
}
|
||||||
|
|
||||||
importId = resource.Primary.ID
|
importId = resource.Primary.ID
|
||||||
}
|
}
|
||||||
|
|
||||||
importPrefix := step.ImportStateIdPrefix
|
importPrefix := step.ImportStateIdPrefix
|
||||||
if importPrefix != "" {
|
if importPrefix != "" {
|
||||||
importId = fmt.Sprintf("%s%s", importPrefix, importId)
|
importId = fmt.Sprintf("%s%s", importPrefix, importId)
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package resource
|
package resource
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
@ -388,3 +389,116 @@ func TestTest_importStateVerifyFail(t *testing.T) {
|
||||||
t.Fatalf("test should fail")
|
t.Fatalf("test should fail")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestTest_importStateIdFunc(t *testing.T) {
|
||||||
|
mp := testProvider()
|
||||||
|
mp.ImportStateFn = func(
|
||||||
|
info *terraform.InstanceInfo, id string) ([]*terraform.InstanceState, error) {
|
||||||
|
if id != "foo:bar" {
|
||||||
|
return nil, fmt.Errorf("bad import ID: %s", id)
|
||||||
|
}
|
||||||
|
|
||||||
|
return []*terraform.InstanceState{
|
||||||
|
{
|
||||||
|
ID: "foo",
|
||||||
|
Ephemeral: terraform.EphemeralState{Type: "test_instance"},
|
||||||
|
},
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
mp.RefreshFn = func(
|
||||||
|
i *terraform.InstanceInfo,
|
||||||
|
s *terraform.InstanceState) (*terraform.InstanceState, error) {
|
||||||
|
return s, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
checked := false
|
||||||
|
checkFn := func(s []*terraform.InstanceState) error {
|
||||||
|
checked = true
|
||||||
|
|
||||||
|
if s[0].ID != "foo" {
|
||||||
|
return fmt.Errorf("bad: %#v", s)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
mt := new(mockT)
|
||||||
|
Test(mt, TestCase{
|
||||||
|
Providers: map[string]terraform.ResourceProvider{
|
||||||
|
"test": mp,
|
||||||
|
},
|
||||||
|
|
||||||
|
Steps: []TestStep{
|
||||||
|
TestStep{
|
||||||
|
Config: testConfigStrProvider,
|
||||||
|
ResourceName: "test_instance.foo",
|
||||||
|
ImportState: true,
|
||||||
|
ImportStateIdFunc: func(*terraform.State) (string, error) { return "foo:bar", nil },
|
||||||
|
ImportStateCheck: checkFn,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
if mt.failed() {
|
||||||
|
t.Fatalf("test failed: %s", mt.failMessage())
|
||||||
|
}
|
||||||
|
if !checked {
|
||||||
|
t.Fatal("didn't call check")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestTest_importStateIdFuncFail(t *testing.T) {
|
||||||
|
mp := testProvider()
|
||||||
|
mp.ImportStateFn = func(
|
||||||
|
info *terraform.InstanceInfo, id string) ([]*terraform.InstanceState, error) {
|
||||||
|
if id != "foo:bar" {
|
||||||
|
return nil, fmt.Errorf("bad import ID: %s", id)
|
||||||
|
}
|
||||||
|
|
||||||
|
return []*terraform.InstanceState{
|
||||||
|
{
|
||||||
|
ID: "foo",
|
||||||
|
Ephemeral: terraform.EphemeralState{Type: "test_instance"},
|
||||||
|
},
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
mp.RefreshFn = func(
|
||||||
|
i *terraform.InstanceInfo,
|
||||||
|
s *terraform.InstanceState) (*terraform.InstanceState, error) {
|
||||||
|
return s, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
checked := false
|
||||||
|
checkFn := func(s []*terraform.InstanceState) error {
|
||||||
|
checked = true
|
||||||
|
|
||||||
|
if s[0].ID != "foo" {
|
||||||
|
return fmt.Errorf("bad: %#v", s)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
mt := new(mockT)
|
||||||
|
Test(mt, TestCase{
|
||||||
|
Providers: map[string]terraform.ResourceProvider{
|
||||||
|
"test": mp,
|
||||||
|
},
|
||||||
|
|
||||||
|
Steps: []TestStep{
|
||||||
|
TestStep{
|
||||||
|
Config: testConfigStrProvider,
|
||||||
|
ResourceName: "test_instance.foo",
|
||||||
|
ImportState: true,
|
||||||
|
ImportStateIdFunc: func(*terraform.State) (string, error) { return "foo:bar", errors.New("foobar") },
|
||||||
|
ImportStateCheck: checkFn,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
if !mt.failed() {
|
||||||
|
t.Fatalf("test should fail")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue