remove legacy import mock

This commit is contained in:
James Bardin 2020-11-13 14:58:59 -05:00
parent 8a325ea54b
commit 943ef51d67
3 changed files with 176 additions and 124 deletions

View File

@ -22,10 +22,14 @@ func TestContextImport_basic(t *testing.T) {
}, },
}) })
p.ImportStateReturn = []*InstanceState{ p.ImportResourceStateResponse = providers.ImportResourceStateResponse{
&InstanceState{ ImportedResources: []providers.ImportedResource{
ID: "foo", {
Ephemeral: EphemeralState{Type: "aws_instance"}, TypeName: "aws_instance",
State: cty.ObjectVal(map[string]cty.Value{
"id": cty.StringVal("foo"),
}),
},
}, },
} }
@ -59,10 +63,14 @@ func TestContextImport_countIndex(t *testing.T) {
}, },
}) })
p.ImportStateReturn = []*InstanceState{ p.ImportResourceStateResponse = providers.ImportResourceStateResponse{
&InstanceState{ ImportedResources: []providers.ImportedResource{
ID: "foo", {
Ephemeral: EphemeralState{Type: "aws_instance"}, TypeName: "aws_instance",
State: cty.ObjectVal(map[string]cty.Value{
"id": cty.StringVal("foo"),
}),
},
}, },
} }
@ -117,10 +125,14 @@ func TestContextImport_collision(t *testing.T) {
}), }),
}) })
p.ImportStateReturn = []*InstanceState{ p.ImportResourceStateResponse = providers.ImportResourceStateResponse{
&InstanceState{ ImportedResources: []providers.ImportedResource{
ID: "foo", {
Ephemeral: EphemeralState{Type: "aws_instance"}, TypeName: "aws_instance",
State: cty.ObjectVal(map[string]cty.Value{
"id": cty.StringVal("foo"),
}),
},
}, },
} }
@ -152,9 +164,13 @@ func TestContextImport_missingType(t *testing.T) {
p := testProvider("aws") p := testProvider("aws")
m := testModule(t, "import-provider") m := testModule(t, "import-provider")
p.ImportStateReturn = []*InstanceState{ p.ImportResourceStateResponse = providers.ImportResourceStateResponse{
&InstanceState{ ImportedResources: []providers.ImportedResource{
ID: "foo", {
State: cty.ObjectVal(map[string]cty.Value{
"id": cty.StringVal("foo"),
}),
},
}, },
} }
@ -189,10 +205,14 @@ func TestContextImport_missingType(t *testing.T) {
func TestContextImport_moduleProvider(t *testing.T) { func TestContextImport_moduleProvider(t *testing.T) {
p := testProvider("aws") p := testProvider("aws")
p.ImportStateReturn = []*InstanceState{ p.ImportResourceStateResponse = providers.ImportResourceStateResponse{
&InstanceState{ ImportedResources: []providers.ImportedResource{
ID: "foo", {
Ephemeral: EphemeralState{Type: "aws_instance"}, TypeName: "aws_instance",
State: cty.ObjectVal(map[string]cty.Value{
"id": cty.StringVal("foo"),
}),
},
}, },
} }
@ -249,10 +269,14 @@ func TestContextImport_providerModule(t *testing.T) {
}, },
}) })
p.ImportStateReturn = []*InstanceState{ p.ImportResourceStateResponse = providers.ImportResourceStateResponse{
&InstanceState{ ImportedResources: []providers.ImportedResource{
ID: "foo", {
Ephemeral: EphemeralState{Type: "aws_instance"}, TypeName: "aws_instance",
State: cty.ObjectVal(map[string]cty.Value{
"id": cty.StringVal("foo"),
}),
},
}, },
} }
@ -317,10 +341,14 @@ func TestContextImport_providerConfig(t *testing.T) {
}, },
}) })
p.ImportStateReturn = []*InstanceState{ p.ImportResourceStateResponse = providers.ImportResourceStateResponse{
&InstanceState{ ImportedResources: []providers.ImportedResource{
ID: "foo", {
Ephemeral: EphemeralState{Type: "aws_instance"}, TypeName: "aws_instance",
State: cty.ObjectVal(map[string]cty.Value{
"id": cty.StringVal("foo"),
}),
},
}, },
} }
@ -368,10 +396,14 @@ func TestContextImport_providerConfigResources(t *testing.T) {
}, },
}) })
p.ImportStateReturn = []*InstanceState{ p.ImportResourceStateResponse = providers.ImportResourceStateResponse{
&InstanceState{ ImportedResources: []providers.ImportedResource{
ID: "foo", {
Ephemeral: EphemeralState{Type: "aws_instance"}, TypeName: "aws_instance",
State: cty.ObjectVal(map[string]cty.Value{
"id": cty.StringVal("foo"),
}),
},
}, },
} }
@ -403,10 +435,14 @@ func TestContextImport_refresh(t *testing.T) {
}, },
}) })
p.ImportStateReturn = []*InstanceState{ p.ImportResourceStateResponse = providers.ImportResourceStateResponse{
&InstanceState{ ImportedResources: []providers.ImportedResource{
ID: "foo", {
Ephemeral: EphemeralState{Type: "aws_instance"}, TypeName: "aws_instance",
State: cty.ObjectVal(map[string]cty.Value{
"id": cty.StringVal("foo"),
}),
},
}, },
} }
@ -450,10 +486,14 @@ func TestContextImport_refreshNil(t *testing.T) {
}, },
}) })
p.ImportStateReturn = []*InstanceState{ p.ImportResourceStateResponse = providers.ImportResourceStateResponse{
&InstanceState{ ImportedResources: []providers.ImportedResource{
ID: "foo", {
Ephemeral: EphemeralState{Type: "aws_instance"}, TypeName: "aws_instance",
State: cty.ObjectVal(map[string]cty.Value{
"id": cty.StringVal("foo"),
}),
},
}, },
} }
@ -494,10 +534,14 @@ func TestContextImport_module(t *testing.T) {
}, },
}) })
p.ImportStateReturn = []*InstanceState{ p.ImportResourceStateResponse = providers.ImportResourceStateResponse{
&InstanceState{ ImportedResources: []providers.ImportedResource{
ID: "foo", {
Ephemeral: EphemeralState{Type: "aws_instance"}, TypeName: "aws_instance",
State: cty.ObjectVal(map[string]cty.Value{
"id": cty.StringVal("foo"),
}),
},
}, },
} }
@ -532,10 +576,14 @@ func TestContextImport_moduleDepth2(t *testing.T) {
}, },
}) })
p.ImportStateReturn = []*InstanceState{ p.ImportResourceStateResponse = providers.ImportResourceStateResponse{
&InstanceState{ ImportedResources: []providers.ImportedResource{
ID: "foo", {
Ephemeral: EphemeralState{Type: "aws_instance"}, TypeName: "aws_instance",
State: cty.ObjectVal(map[string]cty.Value{
"id": cty.StringVal("foo"),
}),
},
}, },
} }
@ -570,10 +618,14 @@ func TestContextImport_moduleDiff(t *testing.T) {
}, },
}) })
p.ImportStateReturn = []*InstanceState{ p.ImportResourceStateResponse = providers.ImportResourceStateResponse{
&InstanceState{ ImportedResources: []providers.ImportedResource{
ID: "foo", {
Ephemeral: EphemeralState{Type: "aws_instance"}, TypeName: "aws_instance",
State: cty.ObjectVal(map[string]cty.Value{
"id": cty.StringVal("foo"),
}),
},
}, },
} }
@ -622,14 +674,20 @@ func TestContextImport_multiState(t *testing.T) {
}, },
} }
p.ImportStateReturn = []*InstanceState{ p.ImportResourceStateResponse = providers.ImportResourceStateResponse{
&InstanceState{ ImportedResources: []providers.ImportedResource{
ID: "foo", {
Ephemeral: EphemeralState{Type: "aws_instance"}, TypeName: "aws_instance",
}, State: cty.ObjectVal(map[string]cty.Value{
&InstanceState{ "id": cty.StringVal("foo"),
ID: "bar", }),
Ephemeral: EphemeralState{Type: "aws_instance_thing"}, },
{
TypeName: "aws_instance_thing",
State: cty.ObjectVal(map[string]cty.Value{
"id": cty.StringVal("bar"),
}),
},
}, },
} }
@ -685,18 +743,26 @@ func TestContextImport_multiStateSame(t *testing.T) {
}, },
} }
p.ImportStateReturn = []*InstanceState{ p.ImportResourceStateResponse = providers.ImportResourceStateResponse{
&InstanceState{ ImportedResources: []providers.ImportedResource{
ID: "foo", {
Ephemeral: EphemeralState{Type: "aws_instance"}, TypeName: "aws_instance",
}, State: cty.ObjectVal(map[string]cty.Value{
&InstanceState{ "id": cty.StringVal("foo"),
ID: "bar", }),
Ephemeral: EphemeralState{Type: "aws_instance_thing"}, },
}, {
&InstanceState{ TypeName: "aws_instance_thing",
ID: "qux", State: cty.ObjectVal(map[string]cty.Value{
Ephemeral: EphemeralState{Type: "aws_instance_thing"}, "id": cty.StringVal("bar"),
}),
},
{
TypeName: "aws_instance_thing",
State: cty.ObjectVal(map[string]cty.Value{
"id": cty.StringVal("qux"),
}),
},
}, },
} }
@ -778,6 +844,16 @@ resource "test_resource" "unused" {
}, },
} }
p.ImportResourceStateResponse = providers.ImportResourceStateResponse{
ImportedResources: []providers.ImportedResource{
{
TypeName: "test_resource",
State: cty.ObjectVal(map[string]cty.Value{
"id": cty.StringVal("test"),
}),
},
},
}
p.ImportResourceStateResponse = providers.ImportResourceStateResponse{ p.ImportResourceStateResponse = providers.ImportResourceStateResponse{
ImportedResources: []providers.ImportedResource{ ImportedResources: []providers.ImportedResource{
{ {

View File

@ -1,7 +1,7 @@
package terraform package terraform
import ( import (
"encoding/json" "errors"
"sync" "sync"
"github.com/zclconf/go-cty/cty" "github.com/zclconf/go-cty/cty"
@ -76,9 +76,6 @@ type MockProvider struct {
ImportResourceStateResponse providers.ImportResourceStateResponse ImportResourceStateResponse providers.ImportResourceStateResponse
ImportResourceStateRequest providers.ImportResourceStateRequest ImportResourceStateRequest providers.ImportResourceStateRequest
ImportResourceStateFn func(providers.ImportResourceStateRequest) providers.ImportResourceStateResponse ImportResourceStateFn func(providers.ImportResourceStateRequest) providers.ImportResourceStateResponse
// Legacy return type for existing tests, which will be shimmed into an
// ImportResourceStateResponse if set
ImportStateReturn []*InstanceState
ReadDataSourceCalled bool ReadDataSourceCalled bool
ReadDataSourceResponse providers.ReadDataSourceResponse ReadDataSourceResponse providers.ReadDataSourceResponse
@ -323,60 +320,34 @@ func (p *MockProvider) ApplyResourceChange(r providers.ApplyResourceChangeReques
return p.ApplyResourceChangeResponse return p.ApplyResourceChangeResponse
} }
func (p *MockProvider) ImportResourceState(r providers.ImportResourceStateRequest) providers.ImportResourceStateResponse { func (p *MockProvider) ImportResourceState(r providers.ImportResourceStateRequest) (resp providers.ImportResourceStateResponse) {
p.Lock() p.Lock()
defer p.Unlock() defer p.Unlock()
if p.ImportStateReturn != nil {
for _, is := range p.ImportStateReturn {
if is.Attributes == nil {
is.Attributes = make(map[string]string)
}
is.Attributes["id"] = is.ID
typeName := is.Ephemeral.Type
// Use the requested type if the resource has no type of it's own.
// We still return the empty type, which will error, but this prevents a panic.
if typeName == "" {
typeName = r.TypeName
}
schema := p.GetSchemaReturn.ResourceTypes[typeName]
if schema == nil {
panic("no schema found for " + typeName)
}
private, err := json.Marshal(is.Meta)
if err != nil {
panic(err)
}
state, err := hcl2shim.HCL2ValueFromFlatmap(is.Attributes, schema.ImpliedType())
if err != nil {
panic(err)
}
state, err = schema.CoerceValue(state)
if err != nil {
panic(err)
}
p.ImportResourceStateResponse.ImportedResources = append(
p.ImportResourceStateResponse.ImportedResources,
providers.ImportedResource{
TypeName: is.Ephemeral.Type,
State: state,
Private: private,
})
}
}
p.ImportResourceStateCalled = true p.ImportResourceStateCalled = true
p.ImportResourceStateRequest = r p.ImportResourceStateRequest = r
if p.ImportResourceStateFn != nil { if p.ImportResourceStateFn != nil {
return p.ImportResourceStateFn(r) return p.ImportResourceStateFn(r)
} }
// fixup the cty value to match the schema
for i, res := range p.ImportResourceStateResponse.ImportedResources {
schema := p.GetSchemaReturn.ResourceTypes[res.TypeName]
if schema == nil {
resp.Diagnostics = resp.Diagnostics.Append(errors.New("no schema found for " + res.TypeName))
return resp
}
var err error
res.State, err = schema.CoerceValue(res.State)
if err != nil {
resp.Diagnostics = resp.Diagnostics.Append(err)
return resp
}
p.ImportResourceStateResponse.ImportedResources[i] = res
}
return p.ImportResourceStateResponse return p.ImportResourceStateResponse
} }

View File

@ -14,12 +14,17 @@ import (
func TestGraphNodeImportStateExecute(t *testing.T) { func TestGraphNodeImportStateExecute(t *testing.T) {
state := states.NewState() state := states.NewState()
provider := testProvider("aws") provider := testProvider("aws")
provider.ImportStateReturn = []*InstanceState{ provider.ImportResourceStateResponse = providers.ImportResourceStateResponse{
&InstanceState{ ImportedResources: []providers.ImportedResource{
ID: "bar", {
Ephemeral: EphemeralState{Type: "aws_instance"}, TypeName: "aws_instance",
State: cty.ObjectVal(map[string]cty.Value{
"id": cty.StringVal("bar"),
}),
},
}, },
} }
ctx := &MockEvalContext{ ctx := &MockEvalContext{
StateState: state.SyncWrapper(), StateState: state.SyncWrapper(),
ProviderProvider: provider, ProviderProvider: provider,