command: Apply tests with realistic mock providers

Now that we're actually verifying correct behavior of providers during
plan and apply, our mock providers need to behave like real providers,
properly propagating any configured values through the plan and into the
final state.

For most of these it was simpler to just switch over to using the newer
PlanResourceChangeFn mock interface, away from the legacy DiffFn approach,
because then we can just return the ProposedNewState verbatim because our
schema for these tests does not require any default values to be
populated.
This commit is contained in:
Martin Atkins 2019-02-11 17:46:55 -08:00
parent f4e6431da2
commit 0b7179c363
1 changed files with 48 additions and 56 deletions

View File

@ -341,10 +341,7 @@ func TestApply_error(t *testing.T) {
var lock sync.Mutex var lock sync.Mutex
errored := false errored := false
p.ApplyFn = func( p.ApplyFn = func(info *terraform.InstanceInfo, s *terraform.InstanceState, d *terraform.InstanceDiff) (*terraform.InstanceState, error) {
info *terraform.InstanceInfo,
s *terraform.InstanceState,
d *terraform.InstanceDiff) (*terraform.InstanceState, error) {
lock.Lock() lock.Lock()
defer lock.Unlock() defer lock.Unlock()
@ -353,19 +350,34 @@ func TestApply_error(t *testing.T) {
return nil, fmt.Errorf("error") return nil, fmt.Errorf("error")
} }
return &terraform.InstanceState{ID: "foo"}, nil newState := &terraform.InstanceState{
ID: "foo",
Attributes: map[string]string{},
}
newState.Attributes["id"] = newState.ID
if ad, ok := d.Attributes["ami"]; ok {
newState.Attributes["ami"] = ad.New
}
if ad, ok := d.Attributes["error"]; ok {
newState.Attributes["error"] = ad.New
}
return newState, nil
} }
p.DiffFn = func( p.DiffFn = func(info *terraform.InstanceInfo, s *terraform.InstanceState, rc *terraform.ResourceConfig) (*terraform.InstanceDiff, error) {
*terraform.InstanceInfo, ret := &terraform.InstanceDiff{
*terraform.InstanceState, Attributes: map[string]*terraform.ResourceAttrDiff{},
*terraform.ResourceConfig) (*terraform.InstanceDiff, error) { }
return &terraform.InstanceDiff{ if new, ok := rc.Get("ami"); ok {
Attributes: map[string]*terraform.ResourceAttrDiff{ ret.Attributes["ami"] = &terraform.ResourceAttrDiff{
"ami": &terraform.ResourceAttrDiff{ New: new.(string),
New: "bar", }
}, }
}, if new, ok := rc.Get("error"); ok {
}, nil ret.Attributes["error"] = &terraform.ResourceAttrDiff{
New: fmt.Sprintf("%t", new.(bool)),
}
}
return ret, nil
} }
p.GetSchemaReturn = &terraform.ProviderSchema{ p.GetSchemaReturn = &terraform.ProviderSchema{
ResourceTypes: map[string]*configschema.Block{ ResourceTypes: map[string]*configschema.Block{
@ -1102,6 +1114,7 @@ func TestApply_vars(t *testing.T) {
}, },
} }
actual := ""
p.GetSchemaReturn = &terraform.ProviderSchema{ p.GetSchemaReturn = &terraform.ProviderSchema{
ResourceTypes: map[string]*configschema.Block{ ResourceTypes: map[string]*configschema.Block{
"test_instance": { "test_instance": {
@ -1116,17 +1129,11 @@ func TestApply_vars(t *testing.T) {
NewState: req.PlannedState, NewState: req.PlannedState,
} }
} }
p.PlanResourceChangeFn = func(req providers.PlanResourceChangeRequest) providers.PlanResourceChangeResponse {
actual := "" actual = req.ProposedNewState.GetAttr("value").AsString()
p.DiffFn = func( return providers.PlanResourceChangeResponse{
info *terraform.InstanceInfo, PlannedState: req.ProposedNewState,
s *terraform.InstanceState,
c *terraform.ResourceConfig) (*terraform.InstanceDiff, error) {
if v, ok := c.Config["value"]; ok {
actual = v.(string)
} }
return &terraform.InstanceDiff{}, nil
} }
args := []string{ args := []string{
@ -1161,6 +1168,7 @@ func TestApply_varFile(t *testing.T) {
}, },
} }
actual := ""
p.GetSchemaReturn = &terraform.ProviderSchema{ p.GetSchemaReturn = &terraform.ProviderSchema{
ResourceTypes: map[string]*configschema.Block{ ResourceTypes: map[string]*configschema.Block{
"test_instance": { "test_instance": {
@ -1175,17 +1183,11 @@ func TestApply_varFile(t *testing.T) {
NewState: req.PlannedState, NewState: req.PlannedState,
} }
} }
p.PlanResourceChangeFn = func(req providers.PlanResourceChangeRequest) providers.PlanResourceChangeResponse {
actual := "" actual = req.ProposedNewState.GetAttr("value").AsString()
p.DiffFn = func( return providers.PlanResourceChangeResponse{
info *terraform.InstanceInfo, PlannedState: req.ProposedNewState,
s *terraform.InstanceState,
c *terraform.ResourceConfig) (*terraform.InstanceDiff, error) {
if v, ok := c.Config["value"]; ok {
actual = v.(string)
} }
return &terraform.InstanceDiff{}, nil
} }
args := []string{ args := []string{
@ -1230,6 +1232,7 @@ func TestApply_varFileDefault(t *testing.T) {
}, },
} }
actual := ""
p.GetSchemaReturn = &terraform.ProviderSchema{ p.GetSchemaReturn = &terraform.ProviderSchema{
ResourceTypes: map[string]*configschema.Block{ ResourceTypes: map[string]*configschema.Block{
"test_instance": { "test_instance": {
@ -1244,17 +1247,11 @@ func TestApply_varFileDefault(t *testing.T) {
NewState: req.PlannedState, NewState: req.PlannedState,
} }
} }
p.PlanResourceChangeFn = func(req providers.PlanResourceChangeRequest) providers.PlanResourceChangeResponse {
actual := "" actual = req.ProposedNewState.GetAttr("value").AsString()
p.DiffFn = func( return providers.PlanResourceChangeResponse{
info *terraform.InstanceInfo, PlannedState: req.ProposedNewState,
s *terraform.InstanceState,
c *terraform.ResourceConfig) (*terraform.InstanceDiff, error) {
if v, ok := c.Config["value"]; ok {
actual = v.(string)
} }
return &terraform.InstanceDiff{}, nil
} }
args := []string{ args := []string{
@ -1298,6 +1295,7 @@ func TestApply_varFileDefaultJSON(t *testing.T) {
}, },
} }
actual := ""
p.GetSchemaReturn = &terraform.ProviderSchema{ p.GetSchemaReturn = &terraform.ProviderSchema{
ResourceTypes: map[string]*configschema.Block{ ResourceTypes: map[string]*configschema.Block{
"test_instance": { "test_instance": {
@ -1312,17 +1310,11 @@ func TestApply_varFileDefaultJSON(t *testing.T) {
NewState: req.PlannedState, NewState: req.PlannedState,
} }
} }
p.PlanResourceChangeFn = func(req providers.PlanResourceChangeRequest) providers.PlanResourceChangeResponse {
actual := "" actual = req.ProposedNewState.GetAttr("value").AsString()
p.DiffFn = func( return providers.PlanResourceChangeResponse{
info *terraform.InstanceInfo, PlannedState: req.ProposedNewState,
s *terraform.InstanceState,
c *terraform.ResourceConfig) (*terraform.InstanceDiff, error) {
if v, ok := c.Config["value"]; ok {
actual = v.(string)
} }
return &terraform.InstanceDiff{}, nil
} }
args := []string{ args := []string{