update command tests to use new mock provider

This commit is contained in:
James Bardin 2020-10-08 13:42:06 -04:00
parent a0caed541d
commit 027a6c2cf6
3 changed files with 41 additions and 95 deletions

View File

@ -341,43 +341,26 @@ func TestApply_error(t *testing.T) {
var lock sync.Mutex var lock sync.Mutex
errored := false errored := false
p.ApplyFn = func(info *terraform.InstanceInfo, s *terraform.InstanceState, d *terraform.InstanceDiff) (*terraform.InstanceState, error) { p.ApplyResourceChangeFn = func(req providers.ApplyResourceChangeRequest) (resp providers.ApplyResourceChangeResponse) {
lock.Lock() lock.Lock()
defer lock.Unlock() defer lock.Unlock()
if !errored { if !errored {
errored = true errored = true
return nil, fmt.Errorf("error") resp.Diagnostics = resp.Diagnostics.Append(fmt.Errorf("error"))
} }
newState := &terraform.InstanceState{ s := req.PlannedState.AsValueMap()
ID: "foo", s["id"] = cty.StringVal("foo")
Attributes: map[string]string{},
} resp.NewState = cty.ObjectVal(s)
newState.Attributes["id"] = newState.ID return
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(info *terraform.InstanceInfo, s *terraform.InstanceState, rc *terraform.ResourceConfig) (*terraform.InstanceDiff, error) { p.PlanResourceChangeFn = func(req providers.PlanResourceChangeRequest) (resp providers.PlanResourceChangeResponse) {
ret := &terraform.InstanceDiff{ s := req.ProposedNewState.AsValueMap()
Attributes: map[string]*terraform.ResourceAttrDiff{}, s["id"] = cty.UnknownVal(cty.String)
} resp.PlannedState = cty.ObjectVal(s)
if new, ok := rc.Get("ami"); ok { return
ret.Attributes["ami"] = &terraform.ResourceAttrDiff{
New: new.(string),
}
}
if new, ok := rc.Get("error"); ok {
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{
@ -902,25 +885,13 @@ func TestApply_shutdown(t *testing.T) {
return nil return nil
} }
p.DiffFn = func( p.PlanResourceChangeFn = func(req providers.PlanResourceChangeRequest) (resp providers.PlanResourceChangeResponse) {
*terraform.InstanceInfo, resp.PlannedState = req.ProposedNewState
*terraform.InstanceState, return
*terraform.ResourceConfig) (*terraform.InstanceDiff, error) {
return &terraform.InstanceDiff{
Attributes: map[string]*terraform.ResourceAttrDiff{
"ami": &terraform.ResourceAttrDiff{
New: "bar",
},
},
}, nil
} }
var once sync.Once var once sync.Once
p.ApplyFn = func( p.ApplyResourceChangeFn = func(req providers.ApplyResourceChangeRequest) (resp providers.ApplyResourceChangeResponse) {
*terraform.InstanceInfo,
*terraform.InstanceState,
*terraform.InstanceDiff) (*terraform.InstanceState, error) {
// only cancel once // only cancel once
once.Do(func() { once.Do(func() {
shutdownCh <- struct{}{} shutdownCh <- struct{}{}
@ -934,12 +905,8 @@ func TestApply_shutdown(t *testing.T) {
// canceled. // canceled.
time.Sleep(200 * time.Millisecond) time.Sleep(200 * time.Millisecond)
return &terraform.InstanceState{ resp.NewState = req.PlannedState
ID: "foo", return
Attributes: map[string]string{
"ami": "2",
},
}, nil
} }
p.GetSchemaReturn = &terraform.ProviderSchema{ p.GetSchemaReturn = &terraform.ProviderSchema{

View File

@ -110,7 +110,7 @@ func TestImport_providerConfig(t *testing.T) {
} }
configured := false configured := false
p.ConfigureNewFn = func(req providers.ConfigureRequest) providers.ConfigureResponse { p.ConfigureFn = func(req providers.ConfigureRequest) providers.ConfigureResponse {
configured = true configured = true
cfg := req.Config cfg := req.Config
@ -217,7 +217,7 @@ func TestImport_remoteState(t *testing.T) {
} }
configured := false configured := false
p.ConfigureNewFn = func(req providers.ConfigureRequest) providers.ConfigureResponse { p.ConfigureFn = func(req providers.ConfigureRequest) providers.ConfigureResponse {
var diags tfdiags.Diagnostics var diags tfdiags.Diagnostics
configured = true configured = true
if got, want := req.Config.GetAttr("foo"), cty.StringVal("bar"); !want.RawEquals(got) { if got, want := req.Config.GetAttr("foo"), cty.StringVal("bar"); !want.RawEquals(got) {
@ -364,7 +364,7 @@ func TestImport_providerConfigWithVar(t *testing.T) {
} }
configured := false configured := false
p.ConfigureNewFn = func(req providers.ConfigureRequest) providers.ConfigureResponse { p.ConfigureFn = func(req providers.ConfigureRequest) providers.ConfigureResponse {
var diags tfdiags.Diagnostics var diags tfdiags.Diagnostics
configured = true configured = true
if got, want := req.Config.GetAttr("foo"), cty.StringVal("bar"); !want.RawEquals(got) { if got, want := req.Config.GetAttr("foo"), cty.StringVal("bar"); !want.RawEquals(got) {
@ -495,7 +495,7 @@ func TestImport_providerConfigWithVarDefault(t *testing.T) {
} }
configured := false configured := false
p.ConfigureNewFn = func(req providers.ConfigureRequest) providers.ConfigureResponse { p.ConfigureFn = func(req providers.ConfigureRequest) providers.ConfigureResponse {
var diags tfdiags.Diagnostics var diags tfdiags.Diagnostics
configured = true configured = true
if got, want := req.Config.GetAttr("foo"), cty.StringVal("bar"); !want.RawEquals(got) { if got, want := req.Config.GetAttr("foo"), cty.StringVal("bar"); !want.RawEquals(got) {
@ -568,7 +568,7 @@ func TestImport_providerConfigWithVarFile(t *testing.T) {
} }
configured := false configured := false
p.ConfigureNewFn = func(req providers.ConfigureRequest) providers.ConfigureResponse { p.ConfigureFn = func(req providers.ConfigureRequest) providers.ConfigureResponse {
var diags tfdiags.Diagnostics var diags tfdiags.Diagnostics
configured = true configured = true
if got, want := req.Config.GetAttr("foo"), cty.StringVal("bar"); !want.RawEquals(got) { if got, want := req.Config.GetAttr("foo"), cty.StringVal("bar"); !want.RawEquals(got) {

View File

@ -524,15 +524,10 @@ func TestPlan_vars(t *testing.T) {
} }
actual := "" actual := ""
p.DiffFn = func( p.PlanResourceChangeFn = func(req providers.PlanResourceChangeRequest) (resp providers.PlanResourceChangeResponse) {
info *terraform.InstanceInfo, actual = req.ProposedNewState.GetAttr("value").AsString()
s *terraform.InstanceState, resp.PlannedState = req.ProposedNewState
c *terraform.ResourceConfig) (*terraform.InstanceDiff, error) { return
if v, ok := c.Config["value"]; ok {
actual = v.(string)
}
return nil, nil
} }
args := []string{ args := []string{
@ -656,15 +651,10 @@ func TestPlan_varFile(t *testing.T) {
} }
actual := "" actual := ""
p.DiffFn = func( p.PlanResourceChangeFn = func(req providers.PlanResourceChangeRequest) (resp providers.PlanResourceChangeResponse) {
info *terraform.InstanceInfo, actual = req.ProposedNewState.GetAttr("value").AsString()
s *terraform.InstanceState, resp.PlannedState = req.ProposedNewState
c *terraform.ResourceConfig) (*terraform.InstanceDiff, error) { return
if v, ok := c.Config["value"]; ok {
actual = v.(string)
}
return nil, nil
} }
args := []string{ args := []string{
@ -706,15 +696,10 @@ func TestPlan_varFileDefault(t *testing.T) {
} }
actual := "" actual := ""
p.DiffFn = func( p.PlanResourceChangeFn = func(req providers.PlanResourceChangeRequest) (resp providers.PlanResourceChangeResponse) {
info *terraform.InstanceInfo, actual = req.ProposedNewState.GetAttr("value").AsString()
s *terraform.InstanceState, resp.PlannedState = req.ProposedNewState
c *terraform.ResourceConfig) (*terraform.InstanceDiff, error) { return
if v, ok := c.Config["value"]; ok {
actual = v.(string)
}
return nil, nil
} }
args := []string{ args := []string{
@ -824,11 +809,7 @@ func TestPlan_shutdown(t *testing.T) {
var once sync.Once var once sync.Once
p.DiffFn = func( p.PlanResourceChangeFn = func(req providers.PlanResourceChangeRequest) (resp providers.PlanResourceChangeResponse) {
*terraform.InstanceInfo,
*terraform.InstanceState,
*terraform.ResourceConfig) (*terraform.InstanceDiff, error) {
once.Do(func() { once.Do(func() {
shutdownCh <- struct{}{} shutdownCh <- struct{}{}
}) })
@ -841,14 +822,12 @@ func TestPlan_shutdown(t *testing.T) {
// canceled. // canceled.
time.Sleep(200 * time.Millisecond) time.Sleep(200 * time.Millisecond)
return &terraform.InstanceDiff{ s := req.ProposedNewState.AsValueMap()
Attributes: map[string]*terraform.ResourceAttrDiff{ s["ami"] = cty.StringVal("bar")
"ami": &terraform.ResourceAttrDiff{ resp.PlannedState = cty.ObjectVal(s)
New: "bar", return
},
},
}, nil
} }
p.GetSchemaReturn = &terraform.ProviderSchema{ p.GetSchemaReturn = &terraform.ProviderSchema{
ResourceTypes: map[string]*configschema.Block{ ResourceTypes: map[string]*configschema.Block{
"test_instance": { "test_instance": {