helper/resource: testing of almost all aspects of ImportState tests

This commit is contained in:
Mitchell Hashimoto 2016-05-09 20:46:16 -07:00
parent 2d99c451fb
commit ec02c8c0e2
No known key found for this signature in database
GPG Key ID: 744E147AA52F5B0A
2 changed files with 124 additions and 2 deletions

View File

@ -55,3 +55,125 @@ func TestTest_importState(t *testing.T) {
t.Fatal("didn't call check") t.Fatal("didn't call check")
} }
} }
func TestTest_importStateFail(t *testing.T) {
mp := testProvider()
mp.ImportStateReturn = []*terraform.InstanceState{
&terraform.InstanceState{
ID: "bar",
Ephemeral: terraform.EphemeralState{Type: "test_instance"},
},
}
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{
ResourceName: "test_instance.foo",
ImportState: true,
ImportStateId: "foo",
ImportStateCheck: checkFn,
},
},
})
if !mt.failed() {
t.Fatal("should fail")
}
if !checked {
t.Fatal("didn't call check")
}
}
func TestTest_importStateDetectId(t *testing.T) {
mp := testProvider()
mp.DiffReturn = nil
mp.ApplyFn = func(
info *terraform.InstanceInfo,
state *terraform.InstanceState,
diff *terraform.InstanceDiff) (*terraform.InstanceState, error) {
if !diff.Destroy {
return &terraform.InstanceState{
ID: "foo",
}, nil
}
return nil, nil
}
mp.RefreshFn = func(
i *terraform.InstanceInfo,
s *terraform.InstanceState) (*terraform.InstanceState, error) {
return s, nil
}
mp.ImportStateFn = func(
info *terraform.InstanceInfo, id string) ([]*terraform.InstanceState, error) {
if id != "foo" {
return nil, fmt.Errorf("bad import ID: %s", id)
}
return []*terraform.InstanceState{
&terraform.InstanceState{
ID: "bar",
Ephemeral: terraform.EphemeralState{Type: "test_instance"},
},
}, nil
}
checked := false
checkFn := func(s []*terraform.InstanceState) error {
checked = true
if s[0].ID != "bar" {
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: testConfigStr,
},
TestStep{
ResourceName: "test_instance.foo",
ImportState: true,
ImportStateCheck: checkFn,
},
},
})
if mt.failed() {
t.Fatalf("test failed: %s", mt.failMessage())
}
if !checked {
t.Fatal("didn't call check")
}
}

View File

@ -61,7 +61,7 @@ type MockResourceProvider struct {
ImportStateID string ImportStateID string
ImportStateReturn []*InstanceState ImportStateReturn []*InstanceState
ImportStateReturnError error ImportStateReturnError error
ImportStateFn func(*InstanceInfo) ([]*InstanceState, error) ImportStateFn func(*InstanceInfo, string) ([]*InstanceState, error)
} }
func (p *MockResourceProvider) Close() error { func (p *MockResourceProvider) Close() error {
@ -191,7 +191,7 @@ func (p *MockResourceProvider) ImportState(info *InstanceInfo, id string) ([]*In
p.ImportStateInfo = info p.ImportStateInfo = info
p.ImportStateID = id p.ImportStateID = id
if p.ImportStateFn != nil { if p.ImportStateFn != nil {
return p.ImportStateFn(info) return p.ImportStateFn(info, id)
} }
return p.ImportStateReturn, p.ImportStateReturnError return p.ImportStateReturn, p.ImportStateReturnError