From dc1f155728e8d08d51e1535744a5fd97b7711b53 Mon Sep 17 00:00:00 2001 From: Chris Marchesi Date: Wed, 6 Sep 2017 19:51:01 -0700 Subject: [PATCH] helper/resource: Failure case for ImportStateIdFunc Just to make sure returning an error was working. --- helper/resource/testing_import_state_test.go | 56 ++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/helper/resource/testing_import_state_test.go b/helper/resource/testing_import_state_test.go index c91355108..9f5a3ea9f 100644 --- a/helper/resource/testing_import_state_test.go +++ b/helper/resource/testing_import_state_test.go @@ -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") + } +}