mirror of
https://github.com/opentofu/opentofu.git
synced 2025-02-25 18:45:20 -06:00
backend/local tests
This commit is contained in:
parent
f54113ec08
commit
739bd5ef0d
@ -3,7 +3,6 @@ package local
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
@ -20,6 +19,7 @@ import (
|
|||||||
"github.com/hashicorp/terraform/states"
|
"github.com/hashicorp/terraform/states"
|
||||||
"github.com/hashicorp/terraform/states/statemgr"
|
"github.com/hashicorp/terraform/states/statemgr"
|
||||||
"github.com/hashicorp/terraform/terraform"
|
"github.com/hashicorp/terraform/terraform"
|
||||||
|
"github.com/hashicorp/terraform/tfdiags"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestLocal_applyBasic(t *testing.T) {
|
func TestLocal_applyBasic(t *testing.T) {
|
||||||
@ -122,45 +122,32 @@ func TestLocal_applyEmptyDirDestroy(t *testing.T) {
|
|||||||
func TestLocal_applyError(t *testing.T) {
|
func TestLocal_applyError(t *testing.T) {
|
||||||
b, cleanup := TestLocal(t)
|
b, cleanup := TestLocal(t)
|
||||||
defer cleanup()
|
defer cleanup()
|
||||||
p := TestLocalProvider(t, b, "test", nil)
|
p := TestLocalProvider(t, b, "test", applyFixtureSchema())
|
||||||
|
|
||||||
var lock sync.Mutex
|
var lock sync.Mutex
|
||||||
errored := false
|
errored := false
|
||||||
p.GetSchemaReturn = &terraform.ProviderSchema{
|
p.ApplyResourceChangeFn = func(
|
||||||
ResourceTypes: map[string]*configschema.Block{
|
r providers.ApplyResourceChangeRequest) providers.ApplyResourceChangeResponse {
|
||||||
"test_instance": {
|
|
||||||
Attributes: map[string]*configschema.Attribute{
|
|
||||||
"ami": {Type: cty.String, Optional: true},
|
|
||||||
"error": {Type: cty.String, Optional: true},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
p.ApplyFn = func(
|
|
||||||
info *terraform.InstanceInfo,
|
|
||||||
s *terraform.InstanceState,
|
|
||||||
d *terraform.InstanceDiff) (*terraform.InstanceState, error) {
|
|
||||||
lock.Lock()
|
lock.Lock()
|
||||||
defer lock.Unlock()
|
defer lock.Unlock()
|
||||||
|
var diags tfdiags.Diagnostics
|
||||||
|
|
||||||
if !errored && info.Id == "test_instance.bar" {
|
ami := r.Config.GetAttr("ami").AsString()
|
||||||
|
if !errored && ami == "error" {
|
||||||
errored = true
|
errored = true
|
||||||
return nil, fmt.Errorf("error")
|
diags = diags.Append(errors.New("error"))
|
||||||
|
return providers.ApplyResourceChangeResponse{
|
||||||
|
Diagnostics: diags,
|
||||||
}
|
}
|
||||||
|
|
||||||
return &terraform.InstanceState{ID: "foo"}, nil
|
|
||||||
}
|
}
|
||||||
p.DiffFn = func(
|
return providers.ApplyResourceChangeResponse{
|
||||||
*terraform.InstanceInfo,
|
Diagnostics: diags,
|
||||||
*terraform.InstanceState,
|
NewState: cty.ObjectVal(map[string]cty.Value{
|
||||||
*terraform.ResourceConfig) (*terraform.InstanceDiff, error) {
|
"id": cty.StringVal("foo"),
|
||||||
return &terraform.InstanceDiff{
|
"ami": cty.StringVal("bar"),
|
||||||
Attributes: map[string]*terraform.ResourceAttrDiff{
|
}),
|
||||||
"ami": &terraform.ResourceAttrDiff{
|
}
|
||||||
New: "bar",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
op, configCleanup := testOperationApply(t, "./test-fixtures/apply-error")
|
op, configCleanup := testOperationApply(t, "./test-fixtures/apply-error")
|
||||||
@ -179,6 +166,7 @@ func TestLocal_applyError(t *testing.T) {
|
|||||||
test_instance.foo:
|
test_instance.foo:
|
||||||
ID = foo
|
ID = foo
|
||||||
provider = provider.test
|
provider = provider.test
|
||||||
|
ami = bar
|
||||||
`)
|
`)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3,5 +3,5 @@ resource "test_instance" "foo" {
|
|||||||
}
|
}
|
||||||
|
|
||||||
resource "test_instance" "bar" {
|
resource "test_instance" "bar" {
|
||||||
error = "true"
|
ami = "error"
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user