helper/resource: Failure case for ImportStateIdFunc

Just to make sure returning an error was working.
This commit is contained in:
Chris Marchesi 2017-09-06 19:51:01 -07:00
parent 7bba1d0c95
commit dc1f155728
No known key found for this signature in database
GPG Key ID: 335B84AFEC615CE4
1 changed files with 56 additions and 0 deletions

View File

@ -1,6 +1,7 @@
package resource
import (
"errors"
"fmt"
"testing"
@ -446,3 +447,58 @@ func TestTest_importStateIdFunc(t *testing.T) {
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")
}
}